blob: 1977c3a1bfcd39884ecfe41878b1319b5226e948 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file compiles an abstract syntax tree (AST) into Python bytecode.
3 *
4 * The primary entry point is PyAST_Compile(), which returns a
5 * PyCodeObject. The compiler makes several passes to build the code
6 * object:
7 * 1. Checks for future statements. See future.c
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00008 * 2. Builds a symbol table. See symtable.c.
Thomas Wouters89f507f2006-12-13 04:49:30 +00009 * 3. Generate code for basic blocks. See compiler_mod() in this file.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000010 * 4. Assemble the basic blocks into final code. See assemble() in
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000011 * this file.
Thomas Wouters89f507f2006-12-13 04:49:30 +000012 * 5. Optimize the byte code (peephole optimizations). See peephole.c
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013 *
14 * Note that compiler_mod() suggests module, but the module ast type
15 * (mod_ty) has cases for expressions and interactive statements.
Nick Coghlan944d3eb2005-11-16 12:46:55 +000016 *
Jeremy Hyltone9357b22006-03-01 15:47:05 +000017 * CAUTION: The VISIT_* macros abort the current function when they
18 * encounter a problem. So don't invoke them when there is memory
19 * which needs to be released. Code blocks are OK, as the compiler
Thomas Wouters89f507f2006-12-13 04:49:30 +000020 * structure takes care of releasing those. Use the arena to manage
21 * objects.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022 */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000023
Guido van Rossum79f25d91997-04-29 20:08:16 +000024#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000025
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000026#include "Python-ast.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000027#include "node.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000028#include "ast.h"
29#include "code.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000030#include "symtable.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031#include "opcode.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000032
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000033#define DEFAULT_BLOCK_SIZE 16
34#define DEFAULT_BLOCKS 8
35#define DEFAULT_CODE_SIZE 128
36#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000037
Nick Coghlan650f0d02007-04-15 12:05:43 +000038#define COMP_GENEXP 0
39#define COMP_LISTCOMP 1
40#define COMP_SETCOMP 2
Guido van Rossum992d4a32007-07-11 13:09:30 +000041#define COMP_DICTCOMP 3
Nick Coghlan650f0d02007-04-15 12:05:43 +000042
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000043struct instr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044 unsigned i_jabs : 1;
45 unsigned i_jrel : 1;
46 unsigned i_hasarg : 1;
47 unsigned char i_opcode;
48 int i_oparg;
49 struct basicblock_ *i_target; /* target block (if jump instruction) */
50 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000051};
52
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000053typedef struct basicblock_ {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000054 /* Each basicblock in a compilation unit is linked via b_list in the
55 reverse order that the block are allocated. b_list points to the next
56 block, not to be confused with b_next, which is next by control flow. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 struct basicblock_ *b_list;
58 /* number of instructions used */
59 int b_iused;
60 /* length of instruction array (b_instr) */
61 int b_ialloc;
62 /* pointer to an array of instructions, initially NULL */
63 struct instr *b_instr;
64 /* If b_next is non-NULL, it is a pointer to the next
65 block reached by normal control flow. */
66 struct basicblock_ *b_next;
67 /* b_seen is used to perform a DFS of basicblocks. */
68 unsigned b_seen : 1;
69 /* b_return is true if a RETURN_VALUE opcode is inserted. */
70 unsigned b_return : 1;
71 /* depth of stack upon entry of block, computed by stackdepth() */
72 int b_startdepth;
73 /* instruction offset for block, computed by assemble_jump_offsets() */
74 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000075} basicblock;
76
77/* fblockinfo tracks the current frame block.
78
Jeremy Hyltone9357b22006-03-01 15:47:05 +000079A frame block is used to handle loops, try/except, and try/finally.
80It's called a frame block to distinguish it from a basic block in the
81compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000082*/
83
84enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
85
86struct fblockinfo {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 enum fblocktype fb_type;
88 basicblock *fb_block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000089};
90
Antoine Pitrou86a36b52011-11-25 18:56:07 +010091enum {
92 COMPILER_SCOPE_MODULE,
93 COMPILER_SCOPE_CLASS,
94 COMPILER_SCOPE_FUNCTION,
Yury Selivanov75445082015-05-11 22:57:16 -040095 COMPILER_SCOPE_ASYNC_FUNCTION,
Benjamin Peterson6b4f7802013-10-20 17:50:28 -040096 COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +010097 COMPILER_SCOPE_COMPREHENSION,
98};
99
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000100/* The following items change on entry and exit of code blocks.
101 They must be saved and restored when returning to a block.
102*/
103struct compiler_unit {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 PySTEntryObject *u_ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 PyObject *u_name;
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400107 PyObject *u_qualname; /* dot-separated qualified name (lazy) */
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100108 int u_scope_type;
109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 /* The following fields are dicts that map objects to
111 the index of them in co_XXX. The index is used as
112 the argument for opcodes that refer to those collections.
113 */
114 PyObject *u_consts; /* all constants */
115 PyObject *u_names; /* all names */
116 PyObject *u_varnames; /* local variables */
117 PyObject *u_cellvars; /* cell variables */
118 PyObject *u_freevars; /* free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 PyObject *u_private; /* for private name mangling */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000121
Victor Stinnerf8e32212013-11-19 23:56:34 +0100122 Py_ssize_t u_argcount; /* number of arguments for block */
123 Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 /* Pointer to the most recently allocated block. By following b_list
125 members, you can reach all early allocated blocks. */
126 basicblock *u_blocks;
127 basicblock *u_curblock; /* pointer to current block */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 int u_nfblocks;
130 struct fblockinfo u_fblock[CO_MAXBLOCKS];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 int u_firstlineno; /* the first lineno of the block */
133 int u_lineno; /* the lineno for the current stmt */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000134 int u_col_offset; /* the offset of the current stmt */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 int u_lineno_set; /* boolean to indicate whether instr
136 has been generated with current lineno */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000137};
138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139/* This struct captures the global state of a compilation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000140
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000141The u pointer points to the current compilation unit, while units
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142for enclosing blocks are stored in c_stack. The u and c_stack are
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000143managed by compiler_enter_scope() and compiler_exit_scope().
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000144
145Note that we don't track recursion levels during compilation - the
146task of detecting and rejecting excessive levels of nesting is
147handled by the symbol analysis pass.
148
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000149*/
150
151struct compiler {
Victor Stinner14e461d2013-08-26 22:28:21 +0200152 PyObject *c_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 struct symtable *c_st;
154 PyFutureFeatures *c_future; /* pointer to module's __future__ */
155 PyCompilerFlags *c_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000156
Georg Brandl8334fd92010-12-04 10:26:46 +0000157 int c_optimize; /* optimization level */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 int c_interactive; /* true if in interactive mode */
159 int c_nestlevel;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 struct compiler_unit *u; /* compiler state for current block */
162 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
163 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000164};
165
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100166static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000167static void compiler_free(struct compiler *);
168static basicblock *compiler_new_block(struct compiler *);
169static int compiler_next_instr(struct compiler *, basicblock *);
170static int compiler_addop(struct compiler *, int);
171static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
Victor Stinnerf8e32212013-11-19 23:56:34 +0100172static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000173static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000174static basicblock *compiler_use_new_block(struct compiler *);
175static int compiler_error(struct compiler *, const char *);
176static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
177
178static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
179static int compiler_visit_stmt(struct compiler *, stmt_ty);
180static int compiler_visit_keyword(struct compiler *, keyword_ty);
181static int compiler_visit_expr(struct compiler *, expr_ty);
182static int compiler_augassign(struct compiler *, stmt_ty);
183static int compiler_visit_slice(struct compiler *, slice_ty,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 expr_context_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185
186static int compiler_push_fblock(struct compiler *, enum fblocktype,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000188static void compiler_pop_fblock(struct compiler *, enum fblocktype,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 basicblock *);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000190/* Returns true if there is a loop on the fblock stack. */
191static int compiler_in_loop(struct compiler *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000192
193static int inplace_binop(struct compiler *, operator_ty);
Georg Brandl8334fd92010-12-04 10:26:46 +0000194static int expr_constant(struct compiler *, expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500196static int compiler_with(struct compiler *, stmt_ty, int);
Yury Selivanov75445082015-05-11 22:57:16 -0400197static int compiler_async_with(struct compiler *, stmt_ty, int);
198static int compiler_async_for(struct compiler *, stmt_ty);
Victor Stinnerad9a0662013-11-19 22:23:20 +0100199static int compiler_call_helper(struct compiler *c, Py_ssize_t n,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 asdl_seq *args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400201 asdl_seq *keywords);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500202static int compiler_try_except(struct compiler *, stmt_ty);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400203static int compiler_set_qualname(struct compiler *);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000204
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205static PyCodeObject *assemble(struct compiler *, int addNone);
206static PyObject *__doc__;
207
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400208#define CAPSULE_NAME "compile.c compiler unit"
Benjamin Petersonb173f782009-05-05 22:31:58 +0000209
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000211_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 /* Name mangling: __private becomes _classname__private.
214 This is independent from how the name is used. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200215 PyObject *result;
216 size_t nlen, plen, ipriv;
217 Py_UCS4 maxchar;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200219 PyUnicode_READ_CHAR(ident, 0) != '_' ||
220 PyUnicode_READ_CHAR(ident, 1) != '_') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 Py_INCREF(ident);
222 return ident;
223 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200224 nlen = PyUnicode_GET_LENGTH(ident);
225 plen = PyUnicode_GET_LENGTH(privateobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 /* Don't mangle __id__ or names with dots.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 The only time a name with a dot can occur is when
229 we are compiling an import statement that has a
230 package name.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 TODO(jhylton): Decide whether we want to support
233 mangling of the module name, e.g. __M.X.
234 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200235 if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
236 PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
237 PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 Py_INCREF(ident);
239 return ident; /* Don't mangle __whatever__ */
240 }
241 /* Strip leading underscores from class name */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200242 ipriv = 0;
243 while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
244 ipriv++;
245 if (ipriv == plen) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 Py_INCREF(ident);
247 return ident; /* Don't mangle if class is just underscores */
248 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200249 plen -= ipriv;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000250
Antoine Pitrou55bff892013-04-06 21:21:04 +0200251 if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
252 PyErr_SetString(PyExc_OverflowError,
253 "private identifier too large to be mangled");
254 return NULL;
255 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000256
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200257 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
258 if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
259 maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
260
261 result = PyUnicode_New(1 + nlen + plen, maxchar);
262 if (!result)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200264 /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
265 PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
Victor Stinner6c7a52a2011-09-28 21:39:17 +0200266 if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
267 Py_DECREF(result);
268 return NULL;
269 }
270 if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
271 Py_DECREF(result);
272 return NULL;
273 }
Victor Stinner8f825062012-04-27 13:55:39 +0200274 assert(_PyUnicode_CheckConsistency(result, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200275 return result;
Michael W. Hudson60934622004-08-12 17:56:29 +0000276}
277
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000278static int
279compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 c->c_stack = PyList_New(0);
284 if (!c->c_stack)
285 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000288}
289
290PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200291PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
292 int optimize, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 struct compiler c;
295 PyCodeObject *co = NULL;
296 PyCompilerFlags local_flags;
297 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 if (!__doc__) {
300 __doc__ = PyUnicode_InternFromString("__doc__");
301 if (!__doc__)
302 return NULL;
303 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 if (!compiler_init(&c))
306 return NULL;
Victor Stinner14e461d2013-08-26 22:28:21 +0200307 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 c.c_filename = filename;
309 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200310 c.c_future = PyFuture_FromASTObject(mod, filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 if (c.c_future == NULL)
312 goto finally;
313 if (!flags) {
314 local_flags.cf_flags = 0;
315 flags = &local_flags;
316 }
317 merged = c.c_future->ff_features | flags->cf_flags;
318 c.c_future->ff_features = merged;
319 flags->cf_flags = merged;
320 c.c_flags = flags;
Georg Brandl8334fd92010-12-04 10:26:46 +0000321 c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000323
Victor Stinner14e461d2013-08-26 22:28:21 +0200324 c.c_st = PySymtable_BuildObject(mod, filename, c.c_future);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 if (c.c_st == NULL) {
326 if (!PyErr_Occurred())
327 PyErr_SetString(PyExc_SystemError, "no symtable");
328 goto finally;
329 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000332
Thomas Wouters1175c432006-02-27 22:49:54 +0000333 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 compiler_free(&c);
335 assert(co || PyErr_Occurred());
336 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000337}
338
339PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200340PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags,
341 int optimize, PyArena *arena)
342{
343 PyObject *filename;
344 PyCodeObject *co;
345 filename = PyUnicode_DecodeFSDefault(filename_str);
346 if (filename == NULL)
347 return NULL;
348 co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
349 Py_DECREF(filename);
350 return co;
351
352}
353
354PyCodeObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000355PyNode_Compile(struct _node *n, const char *filename)
356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 PyCodeObject *co = NULL;
358 mod_ty mod;
359 PyArena *arena = PyArena_New();
360 if (!arena)
361 return NULL;
362 mod = PyAST_FromNode(n, NULL, filename, arena);
363 if (mod)
364 co = PyAST_Compile(mod, filename, NULL, arena);
365 PyArena_Free(arena);
366 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000367}
368
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000369static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000370compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000371{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 if (c->c_st)
373 PySymtable_Free(c->c_st);
374 if (c->c_future)
375 PyObject_Free(c->c_future);
Victor Stinner14e461d2013-08-26 22:28:21 +0200376 Py_XDECREF(c->c_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000378}
379
Guido van Rossum79f25d91997-04-29 20:08:16 +0000380static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 Py_ssize_t i, n;
384 PyObject *v, *k;
385 PyObject *dict = PyDict_New();
386 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 n = PyList_Size(list);
389 for (i = 0; i < n; i++) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100390 v = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 if (!v) {
392 Py_DECREF(dict);
393 return NULL;
394 }
395 k = PyList_GET_ITEM(list, i);
396 k = PyTuple_Pack(2, k, k->ob_type);
397 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
398 Py_XDECREF(k);
399 Py_DECREF(v);
400 Py_DECREF(dict);
401 return NULL;
402 }
403 Py_DECREF(k);
404 Py_DECREF(v);
405 }
406 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000407}
408
409/* Return new dict containing names from src that match scope(s).
410
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000411src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000413values are integers, starting at offset and increasing by one for
414each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000415*/
416
417static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +0100418dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000419{
Benjamin Peterson51ab2832012-07-18 15:12:47 -0700420 Py_ssize_t i = offset, scope, num_keys, key_i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 PyObject *k, *v, *dest = PyDict_New();
Meador Inge2ca63152012-07-18 14:20:11 -0500422 PyObject *sorted_keys;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 assert(offset >= 0);
425 if (dest == NULL)
426 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000427
Meador Inge2ca63152012-07-18 14:20:11 -0500428 /* Sort the keys so that we have a deterministic order on the indexes
429 saved in the returned dictionary. These indexes are used as indexes
430 into the free and cell var storage. Therefore if they aren't
431 deterministic, then the generated bytecode is not deterministic.
432 */
433 sorted_keys = PyDict_Keys(src);
434 if (sorted_keys == NULL)
435 return NULL;
436 if (PyList_Sort(sorted_keys) != 0) {
437 Py_DECREF(sorted_keys);
438 return NULL;
439 }
Meador Ingef69e24e2012-07-18 16:41:03 -0500440 num_keys = PyList_GET_SIZE(sorted_keys);
Meador Inge2ca63152012-07-18 14:20:11 -0500441
442 for (key_i = 0; key_i < num_keys; key_i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 /* XXX this should probably be a macro in symtable.h */
444 long vi;
Meador Inge2ca63152012-07-18 14:20:11 -0500445 k = PyList_GET_ITEM(sorted_keys, key_i);
446 v = PyDict_GetItem(src, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 assert(PyLong_Check(v));
448 vi = PyLong_AS_LONG(v);
449 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 if (scope == scope_type || vi & flag) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100452 PyObject *tuple, *item = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 if (item == NULL) {
Meador Inge2ca63152012-07-18 14:20:11 -0500454 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 Py_DECREF(dest);
456 return NULL;
457 }
458 i++;
459 tuple = PyTuple_Pack(2, k, k->ob_type);
460 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
Meador Inge2ca63152012-07-18 14:20:11 -0500461 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 Py_DECREF(item);
463 Py_DECREF(dest);
464 Py_XDECREF(tuple);
465 return NULL;
466 }
467 Py_DECREF(item);
468 Py_DECREF(tuple);
469 }
470 }
Meador Inge2ca63152012-07-18 14:20:11 -0500471 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000473}
474
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000475static void
476compiler_unit_check(struct compiler_unit *u)
477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 basicblock *block;
479 for (block = u->u_blocks; block != NULL; block = block->b_list) {
480 assert((void *)block != (void *)0xcbcbcbcb);
481 assert((void *)block != (void *)0xfbfbfbfb);
482 assert((void *)block != (void *)0xdbdbdbdb);
483 if (block->b_instr != NULL) {
484 assert(block->b_ialloc > 0);
485 assert(block->b_iused > 0);
486 assert(block->b_ialloc >= block->b_iused);
487 }
488 else {
489 assert (block->b_iused == 0);
490 assert (block->b_ialloc == 0);
491 }
492 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000493}
494
495static void
496compiler_unit_free(struct compiler_unit *u)
497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 compiler_unit_check(u);
501 b = u->u_blocks;
502 while (b != NULL) {
503 if (b->b_instr)
504 PyObject_Free((void *)b->b_instr);
505 next = b->b_list;
506 PyObject_Free((void *)b);
507 b = next;
508 }
509 Py_CLEAR(u->u_ste);
510 Py_CLEAR(u->u_name);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400511 Py_CLEAR(u->u_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 Py_CLEAR(u->u_consts);
513 Py_CLEAR(u->u_names);
514 Py_CLEAR(u->u_varnames);
515 Py_CLEAR(u->u_freevars);
516 Py_CLEAR(u->u_cellvars);
517 Py_CLEAR(u->u_private);
518 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519}
520
521static int
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100522compiler_enter_scope(struct compiler *c, identifier name,
523 int scope_type, void *key, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
528 struct compiler_unit));
529 if (!u) {
530 PyErr_NoMemory();
531 return 0;
532 }
533 memset(u, 0, sizeof(struct compiler_unit));
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100534 u->u_scope_type = scope_type;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 u->u_argcount = 0;
536 u->u_kwonlyargcount = 0;
537 u->u_ste = PySymtable_Lookup(c->c_st, key);
538 if (!u->u_ste) {
539 compiler_unit_free(u);
540 return 0;
541 }
542 Py_INCREF(name);
543 u->u_name = name;
544 u->u_varnames = list2dict(u->u_ste->ste_varnames);
545 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
546 if (!u->u_varnames || !u->u_cellvars) {
547 compiler_unit_free(u);
548 return 0;
549 }
Benjamin Peterson312595c2013-05-15 15:26:42 -0500550 if (u->u_ste->ste_needs_class_closure) {
551 /* Cook up a implicit __class__ cell. */
552 _Py_IDENTIFIER(__class__);
553 PyObject *tuple, *name, *zero;
554 int res;
555 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
556 assert(PyDict_Size(u->u_cellvars) == 0);
557 name = _PyUnicode_FromId(&PyId___class__);
558 if (!name) {
559 compiler_unit_free(u);
560 return 0;
561 }
562 tuple = PyTuple_Pack(2, name, Py_TYPE(name));
563 if (!tuple) {
564 compiler_unit_free(u);
565 return 0;
566 }
567 zero = PyLong_FromLong(0);
568 if (!zero) {
569 Py_DECREF(tuple);
570 compiler_unit_free(u);
571 return 0;
572 }
573 res = PyDict_SetItem(u->u_cellvars, tuple, zero);
574 Py_DECREF(tuple);
575 Py_DECREF(zero);
576 if (res < 0) {
577 compiler_unit_free(u);
578 return 0;
579 }
580 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
583 PyDict_Size(u->u_cellvars));
584 if (!u->u_freevars) {
585 compiler_unit_free(u);
586 return 0;
587 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 u->u_blocks = NULL;
590 u->u_nfblocks = 0;
591 u->u_firstlineno = lineno;
592 u->u_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000593 u->u_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 u->u_lineno_set = 0;
595 u->u_consts = PyDict_New();
596 if (!u->u_consts) {
597 compiler_unit_free(u);
598 return 0;
599 }
600 u->u_names = PyDict_New();
601 if (!u->u_names) {
602 compiler_unit_free(u);
603 return 0;
604 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 /* Push the old compiler_unit on the stack. */
609 if (c->u) {
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400610 PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
612 Py_XDECREF(capsule);
613 compiler_unit_free(u);
614 return 0;
615 }
616 Py_DECREF(capsule);
617 u->u_private = c->u->u_private;
618 Py_XINCREF(u->u_private);
619 }
620 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 c->c_nestlevel++;
623 if (compiler_use_new_block(c) == NULL)
624 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000625
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400626 if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
627 if (!compiler_set_qualname(c))
628 return 0;
629 }
630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000632}
633
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000634static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000635compiler_exit_scope(struct compiler *c)
636{
Victor Stinnerad9a0662013-11-19 22:23:20 +0100637 Py_ssize_t n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 PyObject *capsule;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 c->c_nestlevel--;
641 compiler_unit_free(c->u);
642 /* Restore c->u to the parent unit. */
643 n = PyList_GET_SIZE(c->c_stack) - 1;
644 if (n >= 0) {
645 capsule = PyList_GET_ITEM(c->c_stack, n);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400646 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 assert(c->u);
648 /* we are deleting from a list so this really shouldn't fail */
649 if (PySequence_DelItem(c->c_stack, n) < 0)
650 Py_FatalError("compiler_exit_scope()");
651 compiler_unit_check(c->u);
652 }
653 else
654 c->u = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656}
657
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400658static int
659compiler_set_qualname(struct compiler *c)
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100660{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100661 _Py_static_string(dot, ".");
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400662 _Py_static_string(dot_locals, ".<locals>");
663 Py_ssize_t stack_size;
664 struct compiler_unit *u = c->u;
665 PyObject *name, *base, *dot_str, *dot_locals_str;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100666
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400667 base = NULL;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100668 stack_size = PyList_GET_SIZE(c->c_stack);
Benjamin Petersona8a38b82013-10-19 16:14:39 -0400669 assert(stack_size >= 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400670 if (stack_size > 1) {
671 int scope, force_global = 0;
672 struct compiler_unit *parent;
673 PyObject *mangled, *capsule;
674
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400675 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400676 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400677 assert(parent);
678
Yury Selivanov75445082015-05-11 22:57:16 -0400679 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
680 || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
681 || u->u_scope_type == COMPILER_SCOPE_CLASS) {
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400682 assert(u->u_name);
683 mangled = _Py_Mangle(parent->u_private, u->u_name);
684 if (!mangled)
685 return 0;
686 scope = PyST_GetScope(parent->u_ste, mangled);
687 Py_DECREF(mangled);
688 assert(scope != GLOBAL_IMPLICIT);
689 if (scope == GLOBAL_EXPLICIT)
690 force_global = 1;
691 }
692
693 if (!force_global) {
694 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
Yury Selivanov75445082015-05-11 22:57:16 -0400695 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400696 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
697 dot_locals_str = _PyUnicode_FromId(&dot_locals);
698 if (dot_locals_str == NULL)
699 return 0;
700 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
701 if (base == NULL)
702 return 0;
703 }
704 else {
705 Py_INCREF(parent->u_qualname);
706 base = parent->u_qualname;
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400707 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100708 }
709 }
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400710
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400711 if (base != NULL) {
712 dot_str = _PyUnicode_FromId(&dot);
713 if (dot_str == NULL) {
714 Py_DECREF(base);
715 return 0;
716 }
717 name = PyUnicode_Concat(base, dot_str);
718 Py_DECREF(base);
719 if (name == NULL)
720 return 0;
721 PyUnicode_Append(&name, u->u_name);
722 if (name == NULL)
723 return 0;
724 }
725 else {
726 Py_INCREF(u->u_name);
727 name = u->u_name;
728 }
729 u->u_qualname = name;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100730
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400731 return 1;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100732}
733
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000734/* Allocate a new block and return a pointer to it.
735 Returns NULL on error.
736*/
737
738static basicblock *
739compiler_new_block(struct compiler *c)
740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 basicblock *b;
742 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 u = c->u;
745 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
746 if (b == NULL) {
747 PyErr_NoMemory();
748 return NULL;
749 }
750 memset((void *)b, 0, sizeof(basicblock));
751 /* Extend the singly linked list of blocks with new block. */
752 b->b_list = u->u_blocks;
753 u->u_blocks = b;
754 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000755}
756
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000757static basicblock *
758compiler_use_new_block(struct compiler *c)
759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 basicblock *block = compiler_new_block(c);
761 if (block == NULL)
762 return NULL;
763 c->u->u_curblock = block;
764 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765}
766
767static basicblock *
768compiler_next_block(struct compiler *c)
769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 basicblock *block = compiler_new_block(c);
771 if (block == NULL)
772 return NULL;
773 c->u->u_curblock->b_next = block;
774 c->u->u_curblock = block;
775 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776}
777
778static basicblock *
779compiler_use_next_block(struct compiler *c, basicblock *block)
780{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 assert(block != NULL);
782 c->u->u_curblock->b_next = block;
783 c->u->u_curblock = block;
784 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785}
786
787/* Returns the offset of the next instruction in the current block's
788 b_instr array. Resizes the b_instr as necessary.
789 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000790*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791
792static int
793compiler_next_instr(struct compiler *c, basicblock *b)
794{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 assert(b != NULL);
796 if (b->b_instr == NULL) {
797 b->b_instr = (struct instr *)PyObject_Malloc(
798 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
799 if (b->b_instr == NULL) {
800 PyErr_NoMemory();
801 return -1;
802 }
803 b->b_ialloc = DEFAULT_BLOCK_SIZE;
804 memset((char *)b->b_instr, 0,
805 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
806 }
807 else if (b->b_iused == b->b_ialloc) {
808 struct instr *tmp;
809 size_t oldsize, newsize;
810 oldsize = b->b_ialloc * sizeof(struct instr);
811 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 if (oldsize > (PY_SIZE_MAX >> 1)) {
814 PyErr_NoMemory();
815 return -1;
816 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 if (newsize == 0) {
819 PyErr_NoMemory();
820 return -1;
821 }
822 b->b_ialloc <<= 1;
823 tmp = (struct instr *)PyObject_Realloc(
824 (void *)b->b_instr, newsize);
825 if (tmp == NULL) {
826 PyErr_NoMemory();
827 return -1;
828 }
829 b->b_instr = tmp;
830 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
831 }
832 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833}
834
Christian Heimes2202f872008-02-06 14:31:34 +0000835/* Set the i_lineno member of the instruction at offset off if the
836 line number for the current expression/statement has not
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000837 already been set. If it has been set, the call has no effect.
838
Christian Heimes2202f872008-02-06 14:31:34 +0000839 The line number is reset in the following cases:
840 - when entering a new scope
841 - on each statement
842 - on each expression that start a new line
843 - before the "except" clause
844 - before the "for" and "while" expressions
Thomas Wouters89f507f2006-12-13 04:49:30 +0000845*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000846
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000847static void
848compiler_set_lineno(struct compiler *c, int off)
849{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 basicblock *b;
851 if (c->u->u_lineno_set)
852 return;
853 c->u->u_lineno_set = 1;
854 b = c->u->u_curblock;
855 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856}
857
Larry Hastings3a907972013-11-23 14:49:22 -0800858int
859PyCompile_OpcodeStackEffect(int opcode, int oparg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 switch (opcode) {
862 case POP_TOP:
863 return -1;
864 case ROT_TWO:
865 case ROT_THREE:
866 return 0;
867 case DUP_TOP:
868 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000869 case DUP_TOP_TWO:
870 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 case UNARY_POSITIVE:
873 case UNARY_NEGATIVE:
874 case UNARY_NOT:
875 case UNARY_INVERT:
876 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 case SET_ADD:
879 case LIST_APPEND:
880 return -1;
881 case MAP_ADD:
882 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 case BINARY_POWER:
885 case BINARY_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400886 case BINARY_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 case BINARY_MODULO:
888 case BINARY_ADD:
889 case BINARY_SUBTRACT:
890 case BINARY_SUBSCR:
891 case BINARY_FLOOR_DIVIDE:
892 case BINARY_TRUE_DIVIDE:
893 return -1;
894 case INPLACE_FLOOR_DIVIDE:
895 case INPLACE_TRUE_DIVIDE:
896 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 case INPLACE_ADD:
899 case INPLACE_SUBTRACT:
900 case INPLACE_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400901 case INPLACE_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 case INPLACE_MODULO:
903 return -1;
904 case STORE_SUBSCR:
905 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 case DELETE_SUBSCR:
907 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 case BINARY_LSHIFT:
910 case BINARY_RSHIFT:
911 case BINARY_AND:
912 case BINARY_XOR:
913 case BINARY_OR:
914 return -1;
915 case INPLACE_POWER:
916 return -1;
917 case GET_ITER:
918 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 case PRINT_EXPR:
921 return -1;
922 case LOAD_BUILD_CLASS:
923 return 1;
924 case INPLACE_LSHIFT:
925 case INPLACE_RSHIFT:
926 case INPLACE_AND:
927 case INPLACE_XOR:
928 case INPLACE_OR:
929 return -1;
930 case BREAK_LOOP:
931 return 0;
932 case SETUP_WITH:
933 return 7;
Yury Selivanov75445082015-05-11 22:57:16 -0400934 case WITH_CLEANUP_START:
935 return 1;
936 case WITH_CLEANUP_FINISH:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 return -1; /* XXX Sometimes more */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 case RETURN_VALUE:
939 return -1;
940 case IMPORT_STAR:
941 return -1;
942 case YIELD_VALUE:
943 return 0;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500944 case YIELD_FROM:
945 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 case POP_BLOCK:
947 return 0;
948 case POP_EXCEPT:
949 return 0; /* -3 except if bad bytecode */
950 case END_FINALLY:
951 return -1; /* or -2 or -3 if exception occurred */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 case STORE_NAME:
954 return -1;
955 case DELETE_NAME:
956 return 0;
957 case UNPACK_SEQUENCE:
958 return oparg-1;
959 case UNPACK_EX:
960 return (oparg&0xFF) + (oparg>>8);
961 case FOR_ITER:
962 return 1; /* or -1, at end of iterator */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 case STORE_ATTR:
965 return -2;
966 case DELETE_ATTR:
967 return -1;
968 case STORE_GLOBAL:
969 return -1;
970 case DELETE_GLOBAL:
971 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 case LOAD_CONST:
973 return 1;
974 case LOAD_NAME:
975 return 1;
976 case BUILD_TUPLE:
977 case BUILD_LIST:
978 case BUILD_SET:
979 return 1-oparg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400980 case BUILD_LIST_UNPACK:
981 case BUILD_TUPLE_UNPACK:
982 case BUILD_SET_UNPACK:
983 case BUILD_MAP_UNPACK:
984 return 1 - oparg;
985 case BUILD_MAP_UNPACK_WITH_CALL:
986 return 1 - (oparg & 0xFF);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 case BUILD_MAP:
988 return 1;
989 case LOAD_ATTR:
990 return 0;
991 case COMPARE_OP:
992 return -1;
993 case IMPORT_NAME:
994 return -1;
995 case IMPORT_FROM:
996 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 case JUMP_FORWARD:
999 case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */
1000 case JUMP_IF_FALSE_OR_POP: /* "" */
1001 case JUMP_ABSOLUTE:
1002 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 case POP_JUMP_IF_FALSE:
1005 case POP_JUMP_IF_TRUE:
1006 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 case LOAD_GLOBAL:
1009 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 case CONTINUE_LOOP:
1012 return 0;
1013 case SETUP_LOOP:
1014 return 0;
1015 case SETUP_EXCEPT:
1016 case SETUP_FINALLY:
1017 return 6; /* can push 3 values for the new exception
1018 + 3 others for the previous exception state */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 case LOAD_FAST:
1021 return 1;
1022 case STORE_FAST:
1023 return -1;
1024 case DELETE_FAST:
1025 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 case RAISE_VARARGS:
1028 return -oparg;
Neal Norwitzc1505362006-12-28 06:47:50 +00001029#define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 case CALL_FUNCTION:
1031 return -NARGS(oparg);
1032 case CALL_FUNCTION_VAR:
1033 case CALL_FUNCTION_KW:
1034 return -NARGS(oparg)-1;
1035 case CALL_FUNCTION_VAR_KW:
1036 return -NARGS(oparg)-2;
1037 case MAKE_FUNCTION:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001038 return -1 -NARGS(oparg) - ((oparg >> 16) & 0xffff);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 case MAKE_CLOSURE:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001040 return -2 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001041#undef NARGS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 case BUILD_SLICE:
1043 if (oparg == 3)
1044 return -2;
1045 else
1046 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 case LOAD_CLOSURE:
1049 return 1;
1050 case LOAD_DEREF:
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001051 case LOAD_CLASSDEREF:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 return 1;
1053 case STORE_DEREF:
1054 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001055 case DELETE_DEREF:
1056 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001057 case GET_AWAITABLE:
1058 return 0;
1059 case SETUP_ASYNC_WITH:
1060 return 6;
1061 case BEFORE_ASYNC_WITH:
1062 return 1;
1063 case GET_AITER:
1064 return 0;
1065 case GET_ANEXT:
1066 return 1;
Yury Selivanov5376ba92015-06-22 12:19:30 -04001067 case GET_YIELD_FROM_ITER:
1068 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 default:
Larry Hastings3a907972013-11-23 14:49:22 -08001070 return PY_INVALID_STACK_EFFECT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 }
Larry Hastings3a907972013-11-23 14:49:22 -08001072 return PY_INVALID_STACK_EFFECT; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001073}
1074
1075/* Add an opcode with no argument.
1076 Returns 0 on failure, 1 on success.
1077*/
1078
1079static int
1080compiler_addop(struct compiler *c, int opcode)
1081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 basicblock *b;
1083 struct instr *i;
1084 int off;
1085 off = compiler_next_instr(c, c->u->u_curblock);
1086 if (off < 0)
1087 return 0;
1088 b = c->u->u_curblock;
1089 i = &b->b_instr[off];
1090 i->i_opcode = opcode;
1091 i->i_hasarg = 0;
1092 if (opcode == RETURN_VALUE)
1093 b->b_return = 1;
1094 compiler_set_lineno(c, off);
1095 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096}
1097
Victor Stinnerf8e32212013-11-19 23:56:34 +01001098static Py_ssize_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
1100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 PyObject *t, *v;
1102 Py_ssize_t arg;
1103 double d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001104
Serhiy Storchaka95949422013-08-27 19:40:23 +03001105 /* necessary to make sure types aren't coerced (e.g., float and complex) */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
1107 if (PyFloat_Check(o)) {
1108 d = PyFloat_AS_DOUBLE(o);
1109 /* all we need is to make the tuple different in either the 0.0
1110 * or -0.0 case from all others, just to avoid the "coercion".
1111 */
1112 if (d == 0.0 && copysign(1.0, d) < 0.0)
1113 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
1114 else
1115 t = PyTuple_Pack(2, o, o->ob_type);
1116 }
1117 else if (PyComplex_Check(o)) {
1118 Py_complex z;
1119 int real_negzero, imag_negzero;
1120 /* For the complex case we must make complex(x, 0.)
1121 different from complex(x, -0.) and complex(0., y)
1122 different from complex(-0., y), for any x and y.
1123 All four complex zeros must be distinguished.*/
1124 z = PyComplex_AsCComplex(o);
1125 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
1126 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
1127 if (real_negzero && imag_negzero) {
1128 t = PyTuple_Pack(5, o, o->ob_type,
1129 Py_None, Py_None, Py_None);
Christian Heimes400adb02008-02-01 08:12:03 +00001130 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 else if (imag_negzero) {
1132 t = PyTuple_Pack(4, o, o->ob_type, Py_None, Py_None);
Guido van Rossum04110fb2007-08-24 16:32:05 +00001133 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 else if (real_negzero) {
1135 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
1136 }
1137 else {
1138 t = PyTuple_Pack(2, o, o->ob_type);
1139 }
1140 }
1141 else {
1142 t = PyTuple_Pack(2, o, o->ob_type);
1143 }
1144 if (t == NULL)
1145 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 v = PyDict_GetItem(dict, t);
1148 if (!v) {
1149 if (PyErr_Occurred())
1150 return -1;
1151 arg = PyDict_Size(dict);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001152 v = PyLong_FromSsize_t(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 if (!v) {
1154 Py_DECREF(t);
1155 return -1;
1156 }
1157 if (PyDict_SetItem(dict, t, v) < 0) {
1158 Py_DECREF(t);
1159 Py_DECREF(v);
1160 return -1;
1161 }
1162 Py_DECREF(v);
1163 }
1164 else
1165 arg = PyLong_AsLong(v);
1166 Py_DECREF(t);
1167 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001168}
1169
1170static int
1171compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001174 Py_ssize_t arg = compiler_add_o(c, dict, o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001176 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001177 return compiler_addop_i(c, opcode, arg);
1178}
1179
1180static int
1181compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001183{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001184 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001185 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1186 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001187 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188 arg = compiler_add_o(c, dict, mangled);
1189 Py_DECREF(mangled);
1190 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001191 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192 return compiler_addop_i(c, opcode, arg);
1193}
1194
1195/* Add an opcode with an integer argument.
1196 Returns 0 on failure, 1 on success.
1197*/
1198
1199static int
Victor Stinnerf8e32212013-11-19 23:56:34 +01001200compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 struct instr *i;
1203 int off;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001204
1205 /* Integer arguments are limit to 16-bit. There is an extension for 32-bit
1206 integer arguments. */
Victor Stinnerf8e32212013-11-19 23:56:34 +01001207 assert((-2147483647-1) <= oparg);
1208 assert(oparg <= 2147483647);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 off = compiler_next_instr(c, c->u->u_curblock);
1211 if (off < 0)
1212 return 0;
1213 i = &c->u->u_curblock->b_instr[off];
Victor Stinnerf8e32212013-11-19 23:56:34 +01001214 i->i_opcode = opcode;
1215 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 i->i_hasarg = 1;
1217 compiler_set_lineno(c, off);
1218 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219}
1220
1221static int
1222compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1223{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 struct instr *i;
1225 int off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 assert(b != NULL);
1228 off = compiler_next_instr(c, c->u->u_curblock);
1229 if (off < 0)
1230 return 0;
1231 i = &c->u->u_curblock->b_instr[off];
1232 i->i_opcode = opcode;
1233 i->i_target = b;
1234 i->i_hasarg = 1;
1235 if (absolute)
1236 i->i_jabs = 1;
1237 else
1238 i->i_jrel = 1;
1239 compiler_set_lineno(c, off);
1240 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241}
1242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1244 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001245 it as the current block. NEXT_BLOCK() also creates an implicit jump
1246 from the current block to the new block.
1247*/
1248
Thomas Wouters89f507f2006-12-13 04:49:30 +00001249/* The returns inside these macros make it impossible to decref objects
1250 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251*/
1252
1253
1254#define NEW_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 if (compiler_use_new_block((C)) == NULL) \
1256 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257}
1258
1259#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 if (compiler_next_block((C)) == NULL) \
1261 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262}
1263
1264#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 if (!compiler_addop((C), (OP))) \
1266 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267}
1268
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001269#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 if (!compiler_addop((C), (OP))) { \
1271 compiler_exit_scope(c); \
1272 return 0; \
1273 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001274}
1275
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1278 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001279}
1280
1281#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1283 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001284}
1285
1286#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 if (!compiler_addop_i((C), (OP), (O))) \
1288 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001289}
1290
1291#define ADDOP_JABS(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 if (!compiler_addop_j((C), (OP), (O), 1)) \
1293 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001294}
1295
1296#define ADDOP_JREL(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 if (!compiler_addop_j((C), (OP), (O), 0)) \
1298 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001299}
1300
1301/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1302 the ASDL name to synthesize the name of the C type and the visit function.
1303*/
1304
1305#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 if (!compiler_visit_ ## TYPE((C), (V))) \
1307 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001308}
1309
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001310#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 if (!compiler_visit_ ## TYPE((C), (V))) { \
1312 compiler_exit_scope(c); \
1313 return 0; \
1314 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001315}
1316
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 if (!compiler_visit_slice((C), (V), (CTX))) \
1319 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320}
1321
1322#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 int _i; \
1324 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1325 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1326 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1327 if (!compiler_visit_ ## TYPE((C), elt)) \
1328 return 0; \
1329 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001330}
1331
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001332#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 int _i; \
1334 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1335 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1336 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1337 if (!compiler_visit_ ## TYPE((C), elt)) { \
1338 compiler_exit_scope(c); \
1339 return 0; \
1340 } \
1341 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001342}
1343
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344static int
1345compiler_isdocstring(stmt_ty s)
1346{
1347 if (s->kind != Expr_kind)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001348 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001349 return s->v.Expr.value->kind == Str_kind;
1350}
1351
1352/* Compile a sequence of statements, checking for a docstring. */
1353
1354static int
1355compiler_body(struct compiler *c, asdl_seq *stmts)
1356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 int i = 0;
1358 stmt_ty st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 if (!asdl_seq_LEN(stmts))
1361 return 1;
1362 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Georg Brandl8334fd92010-12-04 10:26:46 +00001363 if (compiler_isdocstring(st) && c->c_optimize < 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 /* don't generate docstrings if -OO */
1365 i = 1;
1366 VISIT(c, expr, st->v.Expr.value);
1367 if (!compiler_nameop(c, __doc__, Store))
1368 return 0;
1369 }
1370 for (; i < asdl_seq_LEN(stmts); i++)
1371 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
1372 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001373}
1374
1375static PyCodeObject *
1376compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001377{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 PyCodeObject *co;
1379 int addNone = 1;
1380 static PyObject *module;
1381 if (!module) {
1382 module = PyUnicode_InternFromString("<module>");
1383 if (!module)
1384 return NULL;
1385 }
1386 /* Use 0 for firstlineno initially, will fixup in assemble(). */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001387 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 return NULL;
1389 switch (mod->kind) {
1390 case Module_kind:
1391 if (!compiler_body(c, mod->v.Module.body)) {
1392 compiler_exit_scope(c);
1393 return 0;
1394 }
1395 break;
1396 case Interactive_kind:
1397 c->c_interactive = 1;
1398 VISIT_SEQ_IN_SCOPE(c, stmt,
1399 mod->v.Interactive.body);
1400 break;
1401 case Expression_kind:
1402 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1403 addNone = 0;
1404 break;
1405 case Suite_kind:
1406 PyErr_SetString(PyExc_SystemError,
1407 "suite should not be possible");
1408 return 0;
1409 default:
1410 PyErr_Format(PyExc_SystemError,
1411 "module kind %d should not be possible",
1412 mod->kind);
1413 return 0;
1414 }
1415 co = assemble(c, addNone);
1416 compiler_exit_scope(c);
1417 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001418}
1419
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420/* The test for LOCAL must come before the test for FREE in order to
1421 handle classes where name is both local and free. The local var is
1422 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001423*/
1424
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425static int
1426get_ref_type(struct compiler *c, PyObject *name)
1427{
Victor Stinner0b1bc562013-05-16 22:17:17 +02001428 int scope;
Benjamin Peterson312595c2013-05-15 15:26:42 -05001429 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
1430 !PyUnicode_CompareWithASCIIString(name, "__class__"))
1431 return CELL;
Victor Stinner0b1bc562013-05-16 22:17:17 +02001432 scope = PyST_GetScope(c->u->u_ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 if (scope == 0) {
1434 char buf[350];
1435 PyOS_snprintf(buf, sizeof(buf),
Victor Stinner14e461d2013-08-26 22:28:21 +02001436 "unknown scope for %.100s in %.100s(%s)\n"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 "symbols: %s\nlocals: %s\nglobals: %s",
Serhiy Storchakadf4518c2014-11-18 23:34:33 +02001438 PyUnicode_AsUTF8(name),
1439 PyUnicode_AsUTF8(c->u->u_name),
1440 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)),
1441 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)),
1442 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)),
1443 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 );
1445 Py_FatalError(buf);
1446 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449}
1450
1451static int
1452compiler_lookup_arg(PyObject *dict, PyObject *name)
1453{
1454 PyObject *k, *v;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001455 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001456 if (k == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001457 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001458 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001459 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001460 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001461 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001462 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463}
1464
1465static int
Victor Stinnerad9a0662013-11-19 22:23:20 +01001466compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t args, PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001468 Py_ssize_t i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001469 if (qualname == NULL)
1470 qualname = co->co_name;
1471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 if (free == 0) {
1473 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001474 ADDOP_O(c, LOAD_CONST, qualname, consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 ADDOP_I(c, MAKE_FUNCTION, args);
1476 return 1;
1477 }
1478 for (i = 0; i < free; ++i) {
1479 /* Bypass com_addop_varname because it will generate
1480 LOAD_DEREF but LOAD_CLOSURE is needed.
1481 */
1482 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1483 int arg, reftype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 /* Special case: If a class contains a method with a
1486 free variable that has the same name as a method,
1487 the name will be considered free *and* local in the
1488 class. It should be handled by the closure, as
1489 well as by the normal name loookup logic.
1490 */
1491 reftype = get_ref_type(c, name);
1492 if (reftype == CELL)
1493 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1494 else /* (reftype == FREE) */
1495 arg = compiler_lookup_arg(c->u->u_freevars, name);
1496 if (arg == -1) {
1497 fprintf(stderr,
1498 "lookup %s in %s %d %d\n"
1499 "freevars of %s: %s\n",
Serhiy Storchakadf4518c2014-11-18 23:34:33 +02001500 PyUnicode_AsUTF8(PyObject_Repr(name)),
1501 PyUnicode_AsUTF8(c->u->u_name),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 reftype, arg,
Serhiy Storchakadf4518c2014-11-18 23:34:33 +02001503 PyUnicode_AsUTF8(co->co_name),
1504 PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 Py_FatalError("compiler_make_closure()");
1506 }
1507 ADDOP_I(c, LOAD_CLOSURE, arg);
1508 }
1509 ADDOP_I(c, BUILD_TUPLE, free);
1510 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001511 ADDOP_O(c, LOAD_CONST, qualname, consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 ADDOP_I(c, MAKE_CLOSURE, args);
1513 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514}
1515
1516static int
1517compiler_decorators(struct compiler *c, asdl_seq* decos)
1518{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 if (!decos)
1522 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1525 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1526 }
1527 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528}
1529
1530static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001531compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 asdl_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001533{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 int i, default_count = 0;
1535 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1536 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1537 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1538 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04001539 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
1540 if (!mangled)
1541 return -1;
1542 ADDOP_O(c, LOAD_CONST, mangled, consts);
1543 Py_DECREF(mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 if (!compiler_visit_expr(c, default_)) {
1545 return -1;
1546 }
1547 default_count++;
1548 }
1549 }
1550 return default_count;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001551}
1552
1553static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001554compiler_visit_argannotation(struct compiler *c, identifier id,
1555 expr_ty annotation, PyObject *names)
1556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 if (annotation) {
Victor Stinner065efc32014-02-18 22:07:56 +01001558 PyObject *mangled;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 VISIT(c, expr, annotation);
Victor Stinner065efc32014-02-18 22:07:56 +01001560 mangled = _Py_Mangle(c->u->u_private, id);
Yury Selivanov34ce99f2014-02-18 12:49:41 -05001561 if (!mangled)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 return -1;
Yury Selivanov34ce99f2014-02-18 12:49:41 -05001563 if (PyList_Append(names, mangled) < 0) {
1564 Py_DECREF(mangled);
1565 return -1;
1566 }
1567 Py_DECREF(mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 }
1569 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001570}
1571
1572static int
1573compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1574 PyObject *names)
1575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 int i, error;
1577 for (i = 0; i < asdl_seq_LEN(args); i++) {
1578 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1579 error = compiler_visit_argannotation(
1580 c,
1581 arg->arg,
1582 arg->annotation,
1583 names);
1584 if (error)
1585 return error;
1586 }
1587 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001588}
1589
1590static int
1591compiler_visit_annotations(struct compiler *c, arguments_ty args,
1592 expr_ty returns)
1593{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 /* Push arg annotations and a list of the argument names. Return the #
1595 of items pushed. The expressions are evaluated out-of-order wrt the
1596 source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00001597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1599 */
1600 static identifier return_str;
1601 PyObject *names;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001602 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 names = PyList_New(0);
1604 if (!names)
1605 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 if (compiler_visit_argannotations(c, args->args, names))
1608 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001609 if (args->vararg && args->vararg->annotation &&
1610 compiler_visit_argannotation(c, args->vararg->arg,
1611 args->vararg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 goto error;
1613 if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1614 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001615 if (args->kwarg && args->kwarg->annotation &&
1616 compiler_visit_argannotation(c, args->kwarg->arg,
1617 args->kwarg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 if (!return_str) {
1621 return_str = PyUnicode_InternFromString("return");
1622 if (!return_str)
1623 goto error;
1624 }
1625 if (compiler_visit_argannotation(c, return_str, returns, names)) {
1626 goto error;
1627 }
1628
1629 len = PyList_GET_SIZE(names);
1630 if (len > 65534) {
1631 /* len must fit in 16 bits, and len is incremented below */
1632 PyErr_SetString(PyExc_SyntaxError,
1633 "too many annotations");
1634 goto error;
1635 }
1636 if (len) {
1637 /* convert names to a tuple and place on stack */
1638 PyObject *elt;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001639 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 PyObject *s = PyTuple_New(len);
1641 if (!s)
1642 goto error;
1643 for (i = 0; i < len; i++) {
1644 elt = PyList_GET_ITEM(names, i);
1645 Py_INCREF(elt);
1646 PyTuple_SET_ITEM(s, i, elt);
1647 }
1648 ADDOP_O(c, LOAD_CONST, s, consts);
1649 Py_DECREF(s);
1650 len++; /* include the just-pushed tuple */
1651 }
1652 Py_DECREF(names);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001653
1654 /* We just checked that len <= 65535, see above */
1655 return Py_SAFE_DOWNCAST(len, Py_ssize_t, int);
Neal Norwitzc1505362006-12-28 06:47:50 +00001656
1657error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 Py_DECREF(names);
1659 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001660}
1661
1662static int
Yury Selivanov75445082015-05-11 22:57:16 -04001663compiler_function(struct compiler *c, stmt_ty s, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001664{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001666 PyObject *qualname, *first_const = Py_None;
Yury Selivanov75445082015-05-11 22:57:16 -04001667 arguments_ty args;
1668 expr_ty returns;
1669 identifier name;
1670 asdl_seq* decos;
1671 asdl_seq *body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 stmt_ty st;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001673 Py_ssize_t i, n, arglength;
1674 int docstring, kw_default_count = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 int num_annotations;
Yury Selivanov75445082015-05-11 22:57:16 -04001676 int scope_type;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677
Yury Selivanov75445082015-05-11 22:57:16 -04001678
1679 if (is_async) {
1680 assert(s->kind == AsyncFunctionDef_kind);
1681
1682 args = s->v.AsyncFunctionDef.args;
1683 returns = s->v.AsyncFunctionDef.returns;
1684 decos = s->v.AsyncFunctionDef.decorator_list;
1685 name = s->v.AsyncFunctionDef.name;
1686 body = s->v.AsyncFunctionDef.body;
1687
1688 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
1689 } else {
1690 assert(s->kind == FunctionDef_kind);
1691
1692 args = s->v.FunctionDef.args;
1693 returns = s->v.FunctionDef.returns;
1694 decos = s->v.FunctionDef.decorator_list;
1695 name = s->v.FunctionDef.name;
1696 body = s->v.FunctionDef.body;
1697
1698 scope_type = COMPILER_SCOPE_FUNCTION;
1699 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 if (!compiler_decorators(c, decos))
1702 return 0;
Benjamin Peterson1ef876c2013-02-10 09:29:59 -05001703 if (args->defaults)
1704 VISIT_SEQ(c, expr, args->defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 if (args->kwonlyargs) {
1706 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1707 args->kw_defaults);
1708 if (res < 0)
1709 return 0;
1710 kw_default_count = res;
1711 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 num_annotations = compiler_visit_annotations(c, args, returns);
1713 if (num_annotations < 0)
1714 return 0;
1715 assert((num_annotations & 0xFFFF) == num_annotations);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001716
Yury Selivanov75445082015-05-11 22:57:16 -04001717 if (!compiler_enter_scope(c, name,
1718 scope_type, (void *)s,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 s->lineno))
1720 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001721
Yury Selivanov75445082015-05-11 22:57:16 -04001722 st = (stmt_ty)asdl_seq_GET(body, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 docstring = compiler_isdocstring(st);
Georg Brandl8334fd92010-12-04 10:26:46 +00001724 if (docstring && c->c_optimize < 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 first_const = st->v.Expr.value->v.Str.s;
1726 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1727 compiler_exit_scope(c);
1728 return 0;
1729 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 c->u->u_argcount = asdl_seq_LEN(args->args);
1732 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Yury Selivanov75445082015-05-11 22:57:16 -04001733 n = asdl_seq_LEN(body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 /* if there was a docstring, we need to skip the first statement */
1735 for (i = docstring; i < n; i++) {
Yury Selivanov75445082015-05-11 22:57:16 -04001736 st = (stmt_ty)asdl_seq_GET(body, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 VISIT_IN_SCOPE(c, stmt, st);
1738 }
1739 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001740 qualname = c->u->u_qualname;
1741 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001743 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001744 Py_XDECREF(qualname);
1745 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001747 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 arglength = asdl_seq_LEN(args->defaults);
1750 arglength |= kw_default_count << 8;
1751 arglength |= num_annotations << 16;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001752 compiler_make_closure(c, co, arglength, qualname);
1753 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001755
Yury Selivanov5376ba92015-06-22 12:19:30 -04001756 if (is_async)
Yury Selivanov75445082015-05-11 22:57:16 -04001757 co->co_flags |= CO_COROUTINE;
Yury Selivanov75445082015-05-11 22:57:16 -04001758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 /* decorators */
1760 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1761 ADDOP_I(c, CALL_FUNCTION, 1);
1762 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763
Yury Selivanov75445082015-05-11 22:57:16 -04001764 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001765}
1766
1767static int
1768compiler_class(struct compiler *c, stmt_ty s)
1769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 PyCodeObject *co;
1771 PyObject *str;
1772 int i;
1773 asdl_seq* decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 if (!compiler_decorators(c, decos))
1776 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 /* ultimately generate code for:
1779 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1780 where:
1781 <func> is a function/closure created from the class body;
1782 it has a single argument (__locals__) where the dict
1783 (or MutableSequence) representing the locals is passed
1784 <name> is the class name
1785 <bases> is the positional arguments and *varargs argument
1786 <keywords> is the keyword arguments and **kwds argument
1787 This borrows from compiler_call.
1788 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001791 if (!compiler_enter_scope(c, s->v.ClassDef.name,
1792 COMPILER_SCOPE_CLASS, (void *)s, s->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 return 0;
1794 /* this block represents what we do in the new scope */
1795 {
1796 /* use the class name for name mangling */
1797 Py_INCREF(s->v.ClassDef.name);
1798 Py_XDECREF(c->u->u_private);
1799 c->u->u_private = s->v.ClassDef.name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 /* load (global) __name__ ... */
1801 str = PyUnicode_InternFromString("__name__");
1802 if (!str || !compiler_nameop(c, str, Load)) {
1803 Py_XDECREF(str);
1804 compiler_exit_scope(c);
1805 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001806 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 Py_DECREF(str);
1808 /* ... and store it as __module__ */
1809 str = PyUnicode_InternFromString("__module__");
1810 if (!str || !compiler_nameop(c, str, Store)) {
1811 Py_XDECREF(str);
1812 compiler_exit_scope(c);
1813 return 0;
1814 }
1815 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001816 assert(c->u->u_qualname);
1817 ADDOP_O(c, LOAD_CONST, c->u->u_qualname, consts);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001818 str = PyUnicode_InternFromString("__qualname__");
1819 if (!str || !compiler_nameop(c, str, Store)) {
1820 Py_XDECREF(str);
1821 compiler_exit_scope(c);
1822 return 0;
1823 }
1824 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 /* compile the body proper */
1826 if (!compiler_body(c, s->v.ClassDef.body)) {
1827 compiler_exit_scope(c);
1828 return 0;
1829 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05001830 if (c->u->u_ste->ste_needs_class_closure) {
1831 /* return the (empty) __class__ cell */
1832 str = PyUnicode_InternFromString("__class__");
1833 if (str == NULL) {
1834 compiler_exit_scope(c);
1835 return 0;
1836 }
1837 i = compiler_lookup_arg(c->u->u_cellvars, str);
1838 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01001839 if (i < 0) {
1840 compiler_exit_scope(c);
1841 return 0;
1842 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05001843 assert(i == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 /* Return the cell where to store __class__ */
1845 ADDOP_I(c, LOAD_CLOSURE, i);
1846 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05001847 else {
1848 assert(PyDict_Size(c->u->u_cellvars) == 0);
1849 /* This happens when nobody references the cell. Return None. */
1850 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1851 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001852 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1853 /* create the code object */
1854 co = assemble(c, 1);
1855 }
1856 /* leave the new scope */
1857 compiler_exit_scope(c);
1858 if (co == NULL)
1859 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 /* 2. load the 'build_class' function */
1862 ADDOP(c, LOAD_BUILD_CLASS);
1863
1864 /* 3. load a function (or closure) made from the code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001865 compiler_make_closure(c, co, 0, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 Py_DECREF(co);
1867
1868 /* 4. load class name */
1869 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1870
1871 /* 5. generate the rest of the code for the call */
1872 if (!compiler_call_helper(c, 2,
1873 s->v.ClassDef.bases,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001874 s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 return 0;
1876
1877 /* 6. apply decorators */
1878 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1879 ADDOP_I(c, CALL_FUNCTION, 1);
1880 }
1881
1882 /* 7. store into <name> */
1883 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1884 return 0;
1885 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001886}
1887
1888static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001889compiler_ifexp(struct compiler *c, expr_ty e)
1890{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 basicblock *end, *next;
1892
1893 assert(e->kind == IfExp_kind);
1894 end = compiler_new_block(c);
1895 if (end == NULL)
1896 return 0;
1897 next = compiler_new_block(c);
1898 if (next == NULL)
1899 return 0;
1900 VISIT(c, expr, e->v.IfExp.test);
1901 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1902 VISIT(c, expr, e->v.IfExp.body);
1903 ADDOP_JREL(c, JUMP_FORWARD, end);
1904 compiler_use_next_block(c, next);
1905 VISIT(c, expr, e->v.IfExp.orelse);
1906 compiler_use_next_block(c, end);
1907 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001908}
1909
1910static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001911compiler_lambda(struct compiler *c, expr_ty e)
1912{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001914 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 static identifier name;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001916 int kw_default_count = 0;
1917 Py_ssize_t arglength;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 arguments_ty args = e->v.Lambda.args;
1919 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 if (!name) {
1922 name = PyUnicode_InternFromString("<lambda>");
1923 if (!name)
1924 return 0;
1925 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001926
Benjamin Peterson419d9a82013-02-10 09:48:22 -05001927 if (args->defaults)
1928 VISIT_SEQ(c, expr, args->defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 if (args->kwonlyargs) {
1930 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1931 args->kw_defaults);
1932 if (res < 0) return 0;
1933 kw_default_count = res;
1934 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001935 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001936 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 /* Make None the first constant, so the lambda can't have a
1940 docstring. */
1941 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0)
1942 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 c->u->u_argcount = asdl_seq_LEN(args->args);
1945 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1946 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1947 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02001948 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 }
1950 else {
1951 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02001952 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001954 qualname = c->u->u_qualname;
1955 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001957 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 arglength = asdl_seq_LEN(args->defaults);
1961 arglength |= kw_default_count << 8;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001962 compiler_make_closure(c, co, arglength, qualname);
1963 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 Py_DECREF(co);
1965
1966 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001967}
1968
1969static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001970compiler_if(struct compiler *c, stmt_ty s)
1971{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 basicblock *end, *next;
1973 int constant;
1974 assert(s->kind == If_kind);
1975 end = compiler_new_block(c);
1976 if (end == NULL)
1977 return 0;
1978
Georg Brandl8334fd92010-12-04 10:26:46 +00001979 constant = expr_constant(c, s->v.If.test);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 /* constant = 0: "if 0"
1981 * constant = 1: "if 1", "if 2", ...
1982 * constant = -1: rest */
1983 if (constant == 0) {
1984 if (s->v.If.orelse)
1985 VISIT_SEQ(c, stmt, s->v.If.orelse);
1986 } else if (constant == 1) {
1987 VISIT_SEQ(c, stmt, s->v.If.body);
1988 } else {
Antoine Pitroue7811fc2014-09-18 03:06:50 +02001989 if (asdl_seq_LEN(s->v.If.orelse)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 next = compiler_new_block(c);
1991 if (next == NULL)
1992 return 0;
1993 }
1994 else
1995 next = end;
1996 VISIT(c, expr, s->v.If.test);
1997 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1998 VISIT_SEQ(c, stmt, s->v.If.body);
Antoine Pitroue7811fc2014-09-18 03:06:50 +02001999 if (asdl_seq_LEN(s->v.If.orelse)) {
2000 ADDOP_JREL(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001 compiler_use_next_block(c, next);
2002 VISIT_SEQ(c, stmt, s->v.If.orelse);
2003 }
2004 }
2005 compiler_use_next_block(c, end);
2006 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002007}
2008
2009static int
2010compiler_for(struct compiler *c, stmt_ty s)
2011{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 basicblock *start, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 start = compiler_new_block(c);
2015 cleanup = compiler_new_block(c);
2016 end = compiler_new_block(c);
2017 if (start == NULL || end == NULL || cleanup == NULL)
2018 return 0;
2019 ADDOP_JREL(c, SETUP_LOOP, end);
2020 if (!compiler_push_fblock(c, LOOP, start))
2021 return 0;
2022 VISIT(c, expr, s->v.For.iter);
2023 ADDOP(c, GET_ITER);
2024 compiler_use_next_block(c, start);
2025 ADDOP_JREL(c, FOR_ITER, cleanup);
2026 VISIT(c, expr, s->v.For.target);
2027 VISIT_SEQ(c, stmt, s->v.For.body);
2028 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2029 compiler_use_next_block(c, cleanup);
2030 ADDOP(c, POP_BLOCK);
2031 compiler_pop_fblock(c, LOOP, start);
2032 VISIT_SEQ(c, stmt, s->v.For.orelse);
2033 compiler_use_next_block(c, end);
2034 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035}
2036
Yury Selivanov75445082015-05-11 22:57:16 -04002037
2038static int
2039compiler_async_for(struct compiler *c, stmt_ty s)
2040{
2041 static PyObject *stopiter_error = NULL;
2042 basicblock *try, *except, *end, *after_try, *try_cleanup,
2043 *after_loop, *after_loop_else;
2044
2045 if (stopiter_error == NULL) {
2046 stopiter_error = PyUnicode_InternFromString("StopAsyncIteration");
2047 if (stopiter_error == NULL)
2048 return 0;
2049 }
2050
2051 try = compiler_new_block(c);
2052 except = compiler_new_block(c);
2053 end = compiler_new_block(c);
2054 after_try = compiler_new_block(c);
2055 try_cleanup = compiler_new_block(c);
2056 after_loop = compiler_new_block(c);
2057 after_loop_else = compiler_new_block(c);
2058
2059 if (try == NULL || except == NULL || end == NULL
2060 || after_try == NULL || try_cleanup == NULL)
2061 return 0;
2062
2063 ADDOP_JREL(c, SETUP_LOOP, after_loop);
2064 if (!compiler_push_fblock(c, LOOP, try))
2065 return 0;
2066
2067 VISIT(c, expr, s->v.AsyncFor.iter);
2068 ADDOP(c, GET_AITER);
2069 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2070 ADDOP(c, YIELD_FROM);
2071
2072 compiler_use_next_block(c, try);
2073
2074
2075 ADDOP_JREL(c, SETUP_EXCEPT, except);
2076 if (!compiler_push_fblock(c, EXCEPT, try))
2077 return 0;
2078
2079 ADDOP(c, GET_ANEXT);
2080 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2081 ADDOP(c, YIELD_FROM);
2082 VISIT(c, expr, s->v.AsyncFor.target);
2083 ADDOP(c, POP_BLOCK);
2084 compiler_pop_fblock(c, EXCEPT, try);
2085 ADDOP_JREL(c, JUMP_FORWARD, after_try);
2086
2087
2088 compiler_use_next_block(c, except);
2089 ADDOP(c, DUP_TOP);
2090 ADDOP_O(c, LOAD_GLOBAL, stopiter_error, names);
2091 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2092 ADDOP_JABS(c, POP_JUMP_IF_FALSE, try_cleanup);
2093
2094 ADDOP(c, POP_TOP);
2095 ADDOP(c, POP_TOP);
2096 ADDOP(c, POP_TOP);
2097 ADDOP(c, POP_EXCEPT); /* for SETUP_EXCEPT */
2098 ADDOP(c, POP_BLOCK); /* for SETUP_LOOP */
2099 ADDOP_JABS(c, JUMP_ABSOLUTE, after_loop_else);
2100
2101
2102 compiler_use_next_block(c, try_cleanup);
2103 ADDOP(c, END_FINALLY);
2104
2105 compiler_use_next_block(c, after_try);
2106 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
2107 ADDOP_JABS(c, JUMP_ABSOLUTE, try);
2108
2109 ADDOP(c, POP_BLOCK); /* for SETUP_LOOP */
2110 compiler_pop_fblock(c, LOOP, try);
2111
2112 compiler_use_next_block(c, after_loop);
2113 ADDOP_JABS(c, JUMP_ABSOLUTE, end);
2114
2115 compiler_use_next_block(c, after_loop_else);
2116 VISIT_SEQ(c, stmt, s->v.For.orelse);
2117
2118 compiler_use_next_block(c, end);
2119
2120 return 1;
2121}
2122
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002123static int
2124compiler_while(struct compiler *c, stmt_ty s)
2125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002126 basicblock *loop, *orelse, *end, *anchor = NULL;
Georg Brandl8334fd92010-12-04 10:26:46 +00002127 int constant = expr_constant(c, s->v.While.test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 if (constant == 0) {
2130 if (s->v.While.orelse)
2131 VISIT_SEQ(c, stmt, s->v.While.orelse);
2132 return 1;
2133 }
2134 loop = compiler_new_block(c);
2135 end = compiler_new_block(c);
2136 if (constant == -1) {
2137 anchor = compiler_new_block(c);
2138 if (anchor == NULL)
2139 return 0;
2140 }
2141 if (loop == NULL || end == NULL)
2142 return 0;
2143 if (s->v.While.orelse) {
2144 orelse = compiler_new_block(c);
2145 if (orelse == NULL)
2146 return 0;
2147 }
2148 else
2149 orelse = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151 ADDOP_JREL(c, SETUP_LOOP, end);
2152 compiler_use_next_block(c, loop);
2153 if (!compiler_push_fblock(c, LOOP, loop))
2154 return 0;
2155 if (constant == -1) {
2156 VISIT(c, expr, s->v.While.test);
2157 ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);
2158 }
2159 VISIT_SEQ(c, stmt, s->v.While.body);
2160 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 /* XXX should the two POP instructions be in a separate block
2163 if there is no else clause ?
2164 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165
Benjamin Peterson3cda0ed2014-12-13 16:06:19 -05002166 if (constant == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 compiler_use_next_block(c, anchor);
Benjamin Peterson3cda0ed2014-12-13 16:06:19 -05002168 ADDOP(c, POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 compiler_pop_fblock(c, LOOP, loop);
2170 if (orelse != NULL) /* what if orelse is just pass? */
2171 VISIT_SEQ(c, stmt, s->v.While.orelse);
2172 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002174 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002175}
2176
2177static int
2178compiler_continue(struct compiler *c)
2179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
2181 static const char IN_FINALLY_ERROR_MSG[] =
2182 "'continue' not supported inside 'finally' clause";
2183 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 if (!c->u->u_nfblocks)
2186 return compiler_error(c, LOOP_ERROR_MSG);
2187 i = c->u->u_nfblocks - 1;
2188 switch (c->u->u_fblock[i].fb_type) {
2189 case LOOP:
2190 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
2191 break;
2192 case EXCEPT:
2193 case FINALLY_TRY:
2194 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
2195 /* Prevent continue anywhere under a finally
2196 even if hidden in a sub-try or except. */
2197 if (c->u->u_fblock[i].fb_type == FINALLY_END)
2198 return compiler_error(c, IN_FINALLY_ERROR_MSG);
2199 }
2200 if (i == -1)
2201 return compiler_error(c, LOOP_ERROR_MSG);
2202 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
2203 break;
2204 case FINALLY_END:
2205 return compiler_error(c, IN_FINALLY_ERROR_MSG);
2206 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002209}
2210
2211/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212
2213 SETUP_FINALLY L
2214 <code for body>
2215 POP_BLOCK
2216 LOAD_CONST <None>
2217 L: <code for finalbody>
2218 END_FINALLY
2219
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002220 The special instructions use the block stack. Each block
2221 stack entry contains the instruction that created it (here
2222 SETUP_FINALLY), the level of the value stack at the time the
2223 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002225 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 Pushes the current value stack level and the label
2227 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002228 POP_BLOCK:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 Pops en entry from the block stack, and pops the value
2230 stack until its level is the same as indicated on the
2231 block stack. (The label is ignored.)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002232 END_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 Pops a variable number of entries from the *value* stack
2234 and re-raises the exception they specify. The number of
2235 entries popped depends on the (pseudo) exception type.
2236
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002237 The block stack is unwound when an exception is raised:
2238 when a SETUP_FINALLY entry is found, the exception is pushed
2239 onto the value stack (and the exception condition is cleared),
2240 and the interpreter jumps to the label gotten from the block
2241 stack.
2242*/
2243
2244static int
2245compiler_try_finally(struct compiler *c, stmt_ty s)
2246{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 basicblock *body, *end;
2248 body = compiler_new_block(c);
2249 end = compiler_new_block(c);
2250 if (body == NULL || end == NULL)
2251 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 ADDOP_JREL(c, SETUP_FINALLY, end);
2254 compiler_use_next_block(c, body);
2255 if (!compiler_push_fblock(c, FINALLY_TRY, body))
2256 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002257 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
2258 if (!compiler_try_except(c, s))
2259 return 0;
2260 }
2261 else {
2262 VISIT_SEQ(c, stmt, s->v.Try.body);
2263 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 ADDOP(c, POP_BLOCK);
2265 compiler_pop_fblock(c, FINALLY_TRY, body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002267 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2268 compiler_use_next_block(c, end);
2269 if (!compiler_push_fblock(c, FINALLY_END, end))
2270 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002271 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 ADDOP(c, END_FINALLY);
2273 compiler_pop_fblock(c, FINALLY_END, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276}
2277
2278/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002279 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002280 (The contents of the value stack is shown in [], with the top
2281 at the right; 'tb' is trace-back info, 'val' the exception's
2282 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283
2284 Value stack Label Instruction Argument
2285 [] SETUP_EXCEPT L1
2286 [] <code for S>
2287 [] POP_BLOCK
2288 [] JUMP_FORWARD L0
2289
2290 [tb, val, exc] L1: DUP )
2291 [tb, val, exc, exc] <evaluate E1> )
2292 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2293 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
2294 [tb, val, exc] POP
2295 [tb, val] <assign to V1> (or POP if no V1)
2296 [tb] POP
2297 [] <code for S1>
2298 JUMP_FORWARD L0
2299
2300 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301 .............................etc.......................
2302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002303 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
2304
2305 [] L0: <next statement>
2306
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002307 Of course, parts are not generated if Vi or Ei is not present.
2308*/
2309static int
2310compiler_try_except(struct compiler *c, stmt_ty s)
2311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002312 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01002313 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 body = compiler_new_block(c);
2316 except = compiler_new_block(c);
2317 orelse = compiler_new_block(c);
2318 end = compiler_new_block(c);
2319 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
2320 return 0;
2321 ADDOP_JREL(c, SETUP_EXCEPT, except);
2322 compiler_use_next_block(c, body);
2323 if (!compiler_push_fblock(c, EXCEPT, body))
2324 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002325 VISIT_SEQ(c, stmt, s->v.Try.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 ADDOP(c, POP_BLOCK);
2327 compiler_pop_fblock(c, EXCEPT, body);
2328 ADDOP_JREL(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002329 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 compiler_use_next_block(c, except);
2331 for (i = 0; i < n; i++) {
2332 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002333 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 if (!handler->v.ExceptHandler.type && i < n-1)
2335 return compiler_error(c, "default 'except:' must be last");
2336 c->u->u_lineno_set = 0;
2337 c->u->u_lineno = handler->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00002338 c->u->u_col_offset = handler->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 except = compiler_new_block(c);
2340 if (except == NULL)
2341 return 0;
2342 if (handler->v.ExceptHandler.type) {
2343 ADDOP(c, DUP_TOP);
2344 VISIT(c, expr, handler->v.ExceptHandler.type);
2345 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2346 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
2347 }
2348 ADDOP(c, POP_TOP);
2349 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002350 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00002351
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002352 cleanup_end = compiler_new_block(c);
2353 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05002354 if (!(cleanup_end || cleanup_body))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002355 return 0;
Guido van Rossumb940e112007-01-10 16:19:56 +00002356
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002357 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
2358 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002360 /*
2361 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03002362 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002363 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03002364 try:
2365 # body
2366 finally:
2367 name = None
2368 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002369 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002371 /* second try: */
2372 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2373 compiler_use_next_block(c, cleanup_body);
2374 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2375 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002377 /* second # body */
2378 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2379 ADDOP(c, POP_BLOCK);
2380 ADDOP(c, POP_EXCEPT);
2381 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002383 /* finally: */
2384 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2385 compiler_use_next_block(c, cleanup_end);
2386 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
2387 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002389 /* name = None */
2390 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2391 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002393 /* del name */
2394 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002396 ADDOP(c, END_FINALLY);
2397 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 }
2399 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002400 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002402 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05002403 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002404 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405
Guido van Rossumb940e112007-01-10 16:19:56 +00002406 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002407 ADDOP(c, POP_TOP);
2408 compiler_use_next_block(c, cleanup_body);
2409 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2410 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002412 ADDOP(c, POP_EXCEPT);
2413 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 }
2415 ADDOP_JREL(c, JUMP_FORWARD, end);
2416 compiler_use_next_block(c, except);
2417 }
2418 ADDOP(c, END_FINALLY);
2419 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002420 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 compiler_use_next_block(c, end);
2422 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002423}
2424
2425static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002426compiler_try(struct compiler *c, stmt_ty s) {
2427 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
2428 return compiler_try_finally(c, s);
2429 else
2430 return compiler_try_except(c, s);
2431}
2432
2433
2434static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002435compiler_import_as(struct compiler *c, identifier name, identifier asname)
2436{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 /* The IMPORT_NAME opcode was already generated. This function
2438 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 If there is a dot in name, we need to split it and emit a
2441 LOAD_ATTR for each name.
2442 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002443 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
2444 PyUnicode_GET_LENGTH(name), 1);
2445 if (dot == -2)
2446 return -1;
2447 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448 /* Consume the base module name to get the first attribute */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002449 Py_ssize_t pos = dot + 1;
2450 while (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 PyObject *attr;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002452 dot = PyUnicode_FindChar(name, '.', pos,
2453 PyUnicode_GET_LENGTH(name), 1);
2454 if (dot == -2)
2455 return -1;
2456 attr = PyUnicode_Substring(name, pos,
2457 (dot != -1) ? dot :
2458 PyUnicode_GET_LENGTH(name));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 if (!attr)
2460 return -1;
2461 ADDOP_O(c, LOAD_ATTR, attr, names);
2462 Py_DECREF(attr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002463 pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464 }
2465 }
2466 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002467}
2468
2469static int
2470compiler_import(struct compiler *c, stmt_ty s)
2471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002472 /* The Import node stores a module name like a.b.c as a single
2473 string. This is convenient for all cases except
2474 import a.b.c as d
2475 where we need to parse that string to extract the individual
2476 module names.
2477 XXX Perhaps change the representation to make this case simpler?
2478 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01002479 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 for (i = 0; i < n; i++) {
2482 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
2483 int r;
2484 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 level = PyLong_FromLong(0);
2487 if (level == NULL)
2488 return 0;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 ADDOP_O(c, LOAD_CONST, level, consts);
2491 Py_DECREF(level);
2492 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2493 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002495 if (alias->asname) {
2496 r = compiler_import_as(c, alias->name, alias->asname);
2497 if (!r)
2498 return r;
2499 }
2500 else {
2501 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002502 Py_ssize_t dot = PyUnicode_FindChar(
2503 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02002504 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002505 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02002506 if (tmp == NULL)
2507 return 0;
2508 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002510 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002511 Py_DECREF(tmp);
2512 }
2513 if (!r)
2514 return r;
2515 }
2516 }
2517 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002518}
2519
2520static int
2521compiler_from_import(struct compiler *c, stmt_ty s)
2522{
Victor Stinnerad9a0662013-11-19 22:23:20 +01002523 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002525 PyObject *names = PyTuple_New(n);
2526 PyObject *level;
2527 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00002528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 if (!empty_string) {
2530 empty_string = PyUnicode_FromString("");
2531 if (!empty_string)
2532 return 0;
2533 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002535 if (!names)
2536 return 0;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 level = PyLong_FromLong(s->v.ImportFrom.level);
2539 if (!level) {
2540 Py_DECREF(names);
2541 return 0;
2542 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002544 /* build up the names */
2545 for (i = 0; i < n; i++) {
2546 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2547 Py_INCREF(alias->name);
2548 PyTuple_SET_ITEM(names, i, alias->name);
2549 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002551 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
2552 !PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module, "__future__")) {
2553 Py_DECREF(level);
2554 Py_DECREF(names);
2555 return compiler_error(c, "from __future__ imports must occur "
2556 "at the beginning of the file");
2557 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002559 ADDOP_O(c, LOAD_CONST, level, consts);
2560 Py_DECREF(level);
2561 ADDOP_O(c, LOAD_CONST, names, consts);
2562 Py_DECREF(names);
2563 if (s->v.ImportFrom.module) {
2564 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2565 }
2566 else {
2567 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
2568 }
2569 for (i = 0; i < n; i++) {
2570 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2571 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002572
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002573 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002574 assert(n == 1);
2575 ADDOP(c, IMPORT_STAR);
2576 return 1;
2577 }
2578
2579 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2580 store_name = alias->name;
2581 if (alias->asname)
2582 store_name = alias->asname;
2583
2584 if (!compiler_nameop(c, store_name, Store)) {
2585 Py_DECREF(names);
2586 return 0;
2587 }
2588 }
2589 /* remove imported module */
2590 ADDOP(c, POP_TOP);
2591 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002592}
2593
2594static int
2595compiler_assert(struct compiler *c, stmt_ty s)
2596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002597 static PyObject *assertion_error = NULL;
2598 basicblock *end;
Victor Stinner14e461d2013-08-26 22:28:21 +02002599 PyObject* msg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002600
Georg Brandl8334fd92010-12-04 10:26:46 +00002601 if (c->c_optimize)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002602 return 1;
2603 if (assertion_error == NULL) {
2604 assertion_error = PyUnicode_InternFromString("AssertionError");
2605 if (assertion_error == NULL)
2606 return 0;
2607 }
2608 if (s->v.Assert.test->kind == Tuple_kind &&
2609 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
Victor Stinner14e461d2013-08-26 22:28:21 +02002610 msg = PyUnicode_FromString("assertion is always true, "
2611 "perhaps remove parentheses?");
2612 if (msg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002613 return 0;
Victor Stinner14e461d2013-08-26 22:28:21 +02002614 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
2615 c->c_filename, c->u->u_lineno,
2616 NULL, NULL) == -1) {
2617 Py_DECREF(msg);
2618 return 0;
2619 }
2620 Py_DECREF(msg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 }
2622 VISIT(c, expr, s->v.Assert.test);
2623 end = compiler_new_block(c);
2624 if (end == NULL)
2625 return 0;
2626 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
2627 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2628 if (s->v.Assert.msg) {
2629 VISIT(c, expr, s->v.Assert.msg);
2630 ADDOP_I(c, CALL_FUNCTION, 1);
2631 }
2632 ADDOP_I(c, RAISE_VARARGS, 1);
2633 compiler_use_next_block(c, end);
2634 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002635}
2636
2637static int
2638compiler_visit_stmt(struct compiler *c, stmt_ty s)
2639{
Victor Stinnerad9a0662013-11-19 22:23:20 +01002640 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002642 /* Always assign a lineno to the next instruction for a stmt. */
2643 c->u->u_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00002644 c->u->u_col_offset = s->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 switch (s->kind) {
2648 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04002649 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 case ClassDef_kind:
2651 return compiler_class(c, s);
2652 case Return_kind:
2653 if (c->u->u_ste->ste_type != FunctionBlock)
2654 return compiler_error(c, "'return' outside function");
2655 if (s->v.Return.value) {
2656 VISIT(c, expr, s->v.Return.value);
2657 }
2658 else
2659 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2660 ADDOP(c, RETURN_VALUE);
2661 break;
2662 case Delete_kind:
2663 VISIT_SEQ(c, expr, s->v.Delete.targets)
2664 break;
2665 case Assign_kind:
2666 n = asdl_seq_LEN(s->v.Assign.targets);
2667 VISIT(c, expr, s->v.Assign.value);
2668 for (i = 0; i < n; i++) {
2669 if (i < n - 1)
2670 ADDOP(c, DUP_TOP);
2671 VISIT(c, expr,
2672 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2673 }
2674 break;
2675 case AugAssign_kind:
2676 return compiler_augassign(c, s);
2677 case For_kind:
2678 return compiler_for(c, s);
2679 case While_kind:
2680 return compiler_while(c, s);
2681 case If_kind:
2682 return compiler_if(c, s);
2683 case Raise_kind:
2684 n = 0;
2685 if (s->v.Raise.exc) {
2686 VISIT(c, expr, s->v.Raise.exc);
2687 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01002688 if (s->v.Raise.cause) {
2689 VISIT(c, expr, s->v.Raise.cause);
2690 n++;
2691 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002692 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01002693 ADDOP_I(c, RAISE_VARARGS, (int)n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002694 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002695 case Try_kind:
2696 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002697 case Assert_kind:
2698 return compiler_assert(c, s);
2699 case Import_kind:
2700 return compiler_import(c, s);
2701 case ImportFrom_kind:
2702 return compiler_from_import(c, s);
2703 case Global_kind:
2704 case Nonlocal_kind:
2705 break;
2706 case Expr_kind:
2707 if (c->c_interactive && c->c_nestlevel <= 1) {
2708 VISIT(c, expr, s->v.Expr.value);
2709 ADDOP(c, PRINT_EXPR);
2710 }
2711 else if (s->v.Expr.value->kind != Str_kind &&
2712 s->v.Expr.value->kind != Num_kind) {
2713 VISIT(c, expr, s->v.Expr.value);
2714 ADDOP(c, POP_TOP);
2715 }
2716 break;
2717 case Pass_kind:
2718 break;
2719 case Break_kind:
2720 if (!compiler_in_loop(c))
2721 return compiler_error(c, "'break' outside loop");
2722 ADDOP(c, BREAK_LOOP);
2723 break;
2724 case Continue_kind:
2725 return compiler_continue(c);
2726 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002727 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04002728 case AsyncFunctionDef_kind:
2729 return compiler_function(c, s, 1);
2730 case AsyncWith_kind:
2731 return compiler_async_with(c, s, 0);
2732 case AsyncFor_kind:
2733 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002734 }
Yury Selivanov75445082015-05-11 22:57:16 -04002735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002736 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002737}
2738
2739static int
2740unaryop(unaryop_ty op)
2741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 switch (op) {
2743 case Invert:
2744 return UNARY_INVERT;
2745 case Not:
2746 return UNARY_NOT;
2747 case UAdd:
2748 return UNARY_POSITIVE;
2749 case USub:
2750 return UNARY_NEGATIVE;
2751 default:
2752 PyErr_Format(PyExc_SystemError,
2753 "unary op %d should not be possible", op);
2754 return 0;
2755 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002756}
2757
2758static int
2759binop(struct compiler *c, operator_ty op)
2760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 switch (op) {
2762 case Add:
2763 return BINARY_ADD;
2764 case Sub:
2765 return BINARY_SUBTRACT;
2766 case Mult:
2767 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04002768 case MatMult:
2769 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002770 case Div:
2771 return BINARY_TRUE_DIVIDE;
2772 case Mod:
2773 return BINARY_MODULO;
2774 case Pow:
2775 return BINARY_POWER;
2776 case LShift:
2777 return BINARY_LSHIFT;
2778 case RShift:
2779 return BINARY_RSHIFT;
2780 case BitOr:
2781 return BINARY_OR;
2782 case BitXor:
2783 return BINARY_XOR;
2784 case BitAnd:
2785 return BINARY_AND;
2786 case FloorDiv:
2787 return BINARY_FLOOR_DIVIDE;
2788 default:
2789 PyErr_Format(PyExc_SystemError,
2790 "binary op %d should not be possible", op);
2791 return 0;
2792 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002793}
2794
2795static int
2796cmpop(cmpop_ty op)
2797{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002798 switch (op) {
2799 case Eq:
2800 return PyCmp_EQ;
2801 case NotEq:
2802 return PyCmp_NE;
2803 case Lt:
2804 return PyCmp_LT;
2805 case LtE:
2806 return PyCmp_LE;
2807 case Gt:
2808 return PyCmp_GT;
2809 case GtE:
2810 return PyCmp_GE;
2811 case Is:
2812 return PyCmp_IS;
2813 case IsNot:
2814 return PyCmp_IS_NOT;
2815 case In:
2816 return PyCmp_IN;
2817 case NotIn:
2818 return PyCmp_NOT_IN;
2819 default:
2820 return PyCmp_BAD;
2821 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002822}
2823
2824static int
2825inplace_binop(struct compiler *c, operator_ty op)
2826{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 switch (op) {
2828 case Add:
2829 return INPLACE_ADD;
2830 case Sub:
2831 return INPLACE_SUBTRACT;
2832 case Mult:
2833 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04002834 case MatMult:
2835 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 case Div:
2837 return INPLACE_TRUE_DIVIDE;
2838 case Mod:
2839 return INPLACE_MODULO;
2840 case Pow:
2841 return INPLACE_POWER;
2842 case LShift:
2843 return INPLACE_LSHIFT;
2844 case RShift:
2845 return INPLACE_RSHIFT;
2846 case BitOr:
2847 return INPLACE_OR;
2848 case BitXor:
2849 return INPLACE_XOR;
2850 case BitAnd:
2851 return INPLACE_AND;
2852 case FloorDiv:
2853 return INPLACE_FLOOR_DIVIDE;
2854 default:
2855 PyErr_Format(PyExc_SystemError,
2856 "inplace binary op %d should not be possible", op);
2857 return 0;
2858 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002859}
2860
2861static int
2862compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2863{
Victor Stinnerad9a0662013-11-19 22:23:20 +01002864 int op, scope;
2865 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 PyObject *dict = c->u->u_names;
2869 PyObject *mangled;
2870 /* XXX AugStore isn't used anywhere! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 mangled = _Py_Mangle(c->u->u_private, name);
2873 if (!mangled)
2874 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002875
Benjamin Peterson70b224d2012-12-06 17:49:58 -05002876 assert(PyUnicode_CompareWithASCIIString(name, "None") &&
2877 PyUnicode_CompareWithASCIIString(name, "True") &&
2878 PyUnicode_CompareWithASCIIString(name, "False"));
2879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 op = 0;
2881 optype = OP_NAME;
2882 scope = PyST_GetScope(c->u->u_ste, mangled);
2883 switch (scope) {
2884 case FREE:
2885 dict = c->u->u_freevars;
2886 optype = OP_DEREF;
2887 break;
2888 case CELL:
2889 dict = c->u->u_cellvars;
2890 optype = OP_DEREF;
2891 break;
2892 case LOCAL:
2893 if (c->u->u_ste->ste_type == FunctionBlock)
2894 optype = OP_FAST;
2895 break;
2896 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04002897 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002898 optype = OP_GLOBAL;
2899 break;
2900 case GLOBAL_EXPLICIT:
2901 optype = OP_GLOBAL;
2902 break;
2903 default:
2904 /* scope can be 0 */
2905 break;
2906 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002909 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 switch (optype) {
2912 case OP_DEREF:
2913 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002914 case Load:
2915 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
2916 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 case Store: op = STORE_DEREF; break;
2918 case AugLoad:
2919 case AugStore:
2920 break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002921 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002922 case Param:
2923 default:
2924 PyErr_SetString(PyExc_SystemError,
2925 "param invalid for deref variable");
2926 return 0;
2927 }
2928 break;
2929 case OP_FAST:
2930 switch (ctx) {
2931 case Load: op = LOAD_FAST; break;
2932 case Store: op = STORE_FAST; break;
2933 case Del: op = DELETE_FAST; break;
2934 case AugLoad:
2935 case AugStore:
2936 break;
2937 case Param:
2938 default:
2939 PyErr_SetString(PyExc_SystemError,
2940 "param invalid for local variable");
2941 return 0;
2942 }
2943 ADDOP_O(c, op, mangled, varnames);
2944 Py_DECREF(mangled);
2945 return 1;
2946 case OP_GLOBAL:
2947 switch (ctx) {
2948 case Load: op = LOAD_GLOBAL; break;
2949 case Store: op = STORE_GLOBAL; break;
2950 case Del: op = DELETE_GLOBAL; break;
2951 case AugLoad:
2952 case AugStore:
2953 break;
2954 case Param:
2955 default:
2956 PyErr_SetString(PyExc_SystemError,
2957 "param invalid for global variable");
2958 return 0;
2959 }
2960 break;
2961 case OP_NAME:
2962 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002963 case Load: op = LOAD_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002964 case Store: op = STORE_NAME; break;
2965 case Del: op = DELETE_NAME; break;
2966 case AugLoad:
2967 case AugStore:
2968 break;
2969 case Param:
2970 default:
2971 PyErr_SetString(PyExc_SystemError,
2972 "param invalid for name variable");
2973 return 0;
2974 }
2975 break;
2976 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 assert(op);
2979 arg = compiler_add_o(c, dict, mangled);
2980 Py_DECREF(mangled);
2981 if (arg < 0)
2982 return 0;
2983 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002984}
2985
2986static int
2987compiler_boolop(struct compiler *c, expr_ty e)
2988{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01002990 int jumpi;
2991 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 asdl_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 assert(e->kind == BoolOp_kind);
2995 if (e->v.BoolOp.op == And)
2996 jumpi = JUMP_IF_FALSE_OR_POP;
2997 else
2998 jumpi = JUMP_IF_TRUE_OR_POP;
2999 end = compiler_new_block(c);
3000 if (end == NULL)
3001 return 0;
3002 s = e->v.BoolOp.values;
3003 n = asdl_seq_LEN(s) - 1;
3004 assert(n >= 0);
3005 for (i = 0; i < n; ++i) {
3006 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
3007 ADDOP_JABS(c, jumpi, end);
3008 }
3009 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3010 compiler_use_next_block(c, end);
3011 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003012}
3013
3014static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003015starunpack_helper(struct compiler *c, asdl_seq *elts,
3016 int single_op, int inner_op, int outer_op)
3017{
3018 Py_ssize_t n = asdl_seq_LEN(elts);
3019 Py_ssize_t i, nsubitems = 0, nseen = 0;
3020 for (i = 0; i < n; i++) {
3021 expr_ty elt = asdl_seq_GET(elts, i);
3022 if (elt->kind == Starred_kind) {
3023 if (nseen) {
3024 ADDOP_I(c, inner_op, nseen);
3025 nseen = 0;
3026 nsubitems++;
3027 }
3028 VISIT(c, expr, elt->v.Starred.value);
3029 nsubitems++;
3030 }
3031 else {
3032 VISIT(c, expr, elt);
3033 nseen++;
3034 }
3035 }
3036 if (nsubitems) {
3037 if (nseen) {
3038 ADDOP_I(c, inner_op, nseen);
3039 nsubitems++;
3040 }
3041 ADDOP_I(c, outer_op, nsubitems);
3042 }
3043 else
3044 ADDOP_I(c, single_op, nseen);
3045 return 1;
3046}
3047
3048static int
3049assignment_helper(struct compiler *c, asdl_seq *elts)
3050{
3051 Py_ssize_t n = asdl_seq_LEN(elts);
3052 Py_ssize_t i;
3053 int seen_star = 0;
3054 for (i = 0; i < n; i++) {
3055 expr_ty elt = asdl_seq_GET(elts, i);
3056 if (elt->kind == Starred_kind && !seen_star) {
3057 if ((i >= (1 << 8)) ||
3058 (n-i-1 >= (INT_MAX >> 8)))
3059 return compiler_error(c,
3060 "too many expressions in "
3061 "star-unpacking assignment");
3062 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3063 seen_star = 1;
3064 asdl_seq_SET(elts, i, elt->v.Starred.value);
3065 }
3066 else if (elt->kind == Starred_kind) {
3067 return compiler_error(c,
3068 "two starred expressions in assignment");
3069 }
3070 }
3071 if (!seen_star) {
3072 ADDOP_I(c, UNPACK_SEQUENCE, n);
3073 }
3074 VISIT_SEQ(c, expr, elts);
3075 return 1;
3076}
3077
3078static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003079compiler_list(struct compiler *c, expr_ty e)
3080{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003081 asdl_seq *elts = e->v.List.elts;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003083 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003084 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003085 else if (e->v.List.ctx == Load) {
3086 return starunpack_helper(c, elts,
3087 BUILD_LIST, BUILD_TUPLE, BUILD_LIST_UNPACK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003089 else
3090 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003091 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003092}
3093
3094static int
3095compiler_tuple(struct compiler *c, expr_ty e)
3096{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003097 asdl_seq *elts = e->v.Tuple.elts;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003098 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003099 return assignment_helper(c, elts);
3100 }
3101 else if (e->v.Tuple.ctx == Load) {
3102 return starunpack_helper(c, elts,
3103 BUILD_TUPLE, BUILD_TUPLE, BUILD_TUPLE_UNPACK);
3104 }
3105 else
3106 VISIT_SEQ(c, expr, elts);
3107 return 1;
3108}
3109
3110static int
3111compiler_set(struct compiler *c, expr_ty e)
3112{
3113 return starunpack_helper(c, e->v.Set.elts, BUILD_SET,
3114 BUILD_SET, BUILD_SET_UNPACK);
3115}
3116
3117static int
3118compiler_dict(struct compiler *c, expr_ty e)
3119{
3120 Py_ssize_t i, n, containers, elements;
3121 int is_unpacking = 0;
3122 n = asdl_seq_LEN(e->v.Dict.values);
3123 containers = 0;
3124 elements = 0;
3125 for (i = 0; i < n; i++) {
3126 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
3127 if (elements == 0xFFFF || (elements && is_unpacking)) {
3128 ADDOP_I(c, BUILD_MAP, elements);
3129 containers++;
3130 elements = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003132 if (is_unpacking) {
3133 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3134 containers++;
3135 }
3136 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003137 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Benjamin Petersonee853392015-05-28 14:30:26 -05003138 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003139 elements++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003140 }
3141 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003142 if (elements || containers == 0) {
3143 ADDOP_I(c, BUILD_MAP, elements);
3144 containers++;
3145 }
3146 /* If there is more than one dict, they need to be merged into a new
3147 * dict. If there is one dict and it's an unpacking, then it needs
3148 * to be copied into a new dict." */
3149 while (containers > 1 || is_unpacking) {
3150 int oparg = containers < 255 ? containers : 255;
3151 ADDOP_I(c, BUILD_MAP_UNPACK, oparg);
3152 containers -= (oparg - 1);
3153 is_unpacking = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 }
3155 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003156}
3157
3158static int
3159compiler_compare(struct compiler *c, expr_ty e)
3160{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003161 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003162 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
3165 VISIT(c, expr, e->v.Compare.left);
3166 n = asdl_seq_LEN(e->v.Compare.ops);
3167 assert(n > 0);
3168 if (n > 1) {
3169 cleanup = compiler_new_block(c);
3170 if (cleanup == NULL)
3171 return 0;
3172 VISIT(c, expr,
3173 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
3174 }
3175 for (i = 1; i < n; i++) {
3176 ADDOP(c, DUP_TOP);
3177 ADDOP(c, ROT_THREE);
3178 ADDOP_I(c, COMPARE_OP,
3179 cmpop((cmpop_ty)(asdl_seq_GET(
3180 e->v.Compare.ops, i - 1))));
3181 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
3182 NEXT_BLOCK(c);
3183 if (i < (n - 1))
3184 VISIT(c, expr,
3185 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
3186 }
3187 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
3188 ADDOP_I(c, COMPARE_OP,
3189 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
3190 if (n > 1) {
3191 basicblock *end = compiler_new_block(c);
3192 if (end == NULL)
3193 return 0;
3194 ADDOP_JREL(c, JUMP_FORWARD, end);
3195 compiler_use_next_block(c, cleanup);
3196 ADDOP(c, ROT_TWO);
3197 ADDOP(c, POP_TOP);
3198 compiler_use_next_block(c, end);
3199 }
3200 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003201}
3202
3203static int
3204compiler_call(struct compiler *c, expr_ty e)
3205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003206 VISIT(c, expr, e->v.Call.func);
3207 return compiler_call_helper(c, 0,
3208 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003209 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003210}
3211
3212/* shared code between compiler_call and compiler_class */
3213static int
3214compiler_call_helper(struct compiler *c,
Victor Stinnerad9a0662013-11-19 22:23:20 +01003215 Py_ssize_t n, /* Args already pushed */
3216 asdl_seq *args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003217 asdl_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 int code = 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003220 Py_ssize_t nelts, i, nseen, nkw;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003221
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003222 /* the number of tuples and dictionaries on the stack */
3223 Py_ssize_t nsubargs = 0, nsubkwargs = 0;
3224
3225 nkw = 0;
3226 nseen = 0; /* the number of positional arguments on the stack */
3227 nelts = asdl_seq_LEN(args);
3228 for (i = 0; i < nelts; i++) {
3229 expr_ty elt = asdl_seq_GET(args, i);
3230 if (elt->kind == Starred_kind) {
3231 /* A star-arg. If we've seen positional arguments,
3232 pack the positional arguments into a
3233 tuple. */
3234 if (nseen) {
3235 ADDOP_I(c, BUILD_TUPLE, nseen);
3236 nseen = 0;
3237 nsubargs++;
3238 }
3239 VISIT(c, expr, elt->v.Starred.value);
3240 nsubargs++;
3241 }
3242 else if (nsubargs) {
3243 /* We've seen star-args already, so we
3244 count towards items-to-pack-into-tuple. */
3245 VISIT(c, expr, elt);
3246 nseen++;
3247 }
3248 else {
3249 /* Positional arguments before star-arguments
3250 are left on the stack. */
3251 VISIT(c, expr, elt);
3252 n++;
3253 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003255 if (nseen) {
3256 /* Pack up any trailing positional arguments. */
3257 ADDOP_I(c, BUILD_TUPLE, nseen);
3258 nsubargs++;
3259 }
3260 if (nsubargs) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 code |= 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003262 if (nsubargs > 1) {
3263 /* If we ended up with more than one stararg, we need
3264 to concatenate them into a single sequence. */
3265 ADDOP_I(c, BUILD_LIST_UNPACK, nsubargs);
3266 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003267 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003268
3269 /* Same dance again for keyword arguments */
3270 nseen = 0; /* the number of keyword arguments on the stack following */
3271 nelts = asdl_seq_LEN(keywords);
3272 for (i = 0; i < nelts; i++) {
3273 keyword_ty kw = asdl_seq_GET(keywords, i);
3274 if (kw->arg == NULL) {
3275 /* A keyword argument unpacking. */
3276 if (nseen) {
3277 ADDOP_I(c, BUILD_MAP, nseen);
3278 nseen = 0;
3279 nsubkwargs++;
3280 }
3281 VISIT(c, expr, kw->value);
3282 nsubkwargs++;
3283 }
3284 else if (nsubkwargs) {
3285 /* A keyword argument and we already have a dict. */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003286 ADDOP_O(c, LOAD_CONST, kw->arg, consts);
Benjamin Petersonee853392015-05-28 14:30:26 -05003287 VISIT(c, expr, kw->value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003288 nseen++;
3289 }
3290 else {
3291 /* keyword argument */
3292 VISIT(c, keyword, kw)
3293 nkw++;
3294 }
3295 }
3296 if (nseen) {
3297 /* Pack up any trailing keyword arguments. */
3298 ADDOP_I(c, BUILD_MAP, nseen);
3299 nsubkwargs++;
3300 }
3301 if (nsubkwargs) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 code |= 2;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003303 if (nsubkwargs > 1) {
3304 /* Pack it all up */
3305 int function_pos = n + (code & 1) + nkw + 1;
3306 ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs | (function_pos << 8));
3307 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003308 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003309 assert(n < 1<<8);
3310 assert(nkw < 1<<24);
3311 n |= nkw << 8;
3312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003313 switch (code) {
3314 case 0:
3315 ADDOP_I(c, CALL_FUNCTION, n);
3316 break;
3317 case 1:
3318 ADDOP_I(c, CALL_FUNCTION_VAR, n);
3319 break;
3320 case 2:
3321 ADDOP_I(c, CALL_FUNCTION_KW, n);
3322 break;
3323 case 3:
3324 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
3325 break;
3326 }
3327 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003328}
3329
Nick Coghlan650f0d02007-04-15 12:05:43 +00003330
3331/* List and set comprehensions and generator expressions work by creating a
3332 nested function to perform the actual iteration. This means that the
3333 iteration variables don't leak into the current scope.
3334 The defined function is called immediately following its definition, with the
3335 result of that call being the result of the expression.
3336 The LC/SC version returns the populated container, while the GE version is
3337 flagged in symtable.c as a generator, so it returns the generator object
3338 when the function is called.
3339 This code *knows* that the loop cannot contain break, continue, or return,
3340 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
3341
3342 Possible cleanups:
3343 - iterate over the generator sequence instead of using recursion
3344*/
3345
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003346static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003347compiler_comprehension_generator(struct compiler *c,
3348 asdl_seq *generators, int gen_index,
3349 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003351 /* generate code for the iterator, then each of the ifs,
3352 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003354 comprehension_ty gen;
3355 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003356 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003358 start = compiler_new_block(c);
3359 skip = compiler_new_block(c);
3360 if_cleanup = compiler_new_block(c);
3361 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003363 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3364 anchor == NULL)
3365 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003367 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003369 if (gen_index == 0) {
3370 /* Receive outermost iter as an implicit argument */
3371 c->u->u_argcount = 1;
3372 ADDOP_I(c, LOAD_FAST, 0);
3373 }
3374 else {
3375 /* Sub-iter - calculate on the fly */
3376 VISIT(c, expr, gen->iter);
3377 ADDOP(c, GET_ITER);
3378 }
3379 compiler_use_next_block(c, start);
3380 ADDOP_JREL(c, FOR_ITER, anchor);
3381 NEXT_BLOCK(c);
3382 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003384 /* XXX this needs to be cleaned up...a lot! */
3385 n = asdl_seq_LEN(gen->ifs);
3386 for (i = 0; i < n; i++) {
3387 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
3388 VISIT(c, expr, e);
3389 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
3390 NEXT_BLOCK(c);
3391 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003393 if (++gen_index < asdl_seq_LEN(generators))
3394 if (!compiler_comprehension_generator(c,
3395 generators, gen_index,
3396 elt, val, type))
3397 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399 /* only append after the last for generator */
3400 if (gen_index >= asdl_seq_LEN(generators)) {
3401 /* comprehension specific code */
3402 switch (type) {
3403 case COMP_GENEXP:
3404 VISIT(c, expr, elt);
3405 ADDOP(c, YIELD_VALUE);
3406 ADDOP(c, POP_TOP);
3407 break;
3408 case COMP_LISTCOMP:
3409 VISIT(c, expr, elt);
3410 ADDOP_I(c, LIST_APPEND, gen_index + 1);
3411 break;
3412 case COMP_SETCOMP:
3413 VISIT(c, expr, elt);
3414 ADDOP_I(c, SET_ADD, gen_index + 1);
3415 break;
3416 case COMP_DICTCOMP:
3417 /* With 'd[k] = v', v is evaluated before k, so we do
3418 the same. */
3419 VISIT(c, expr, val);
3420 VISIT(c, expr, elt);
3421 ADDOP_I(c, MAP_ADD, gen_index + 1);
3422 break;
3423 default:
3424 return 0;
3425 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003427 compiler_use_next_block(c, skip);
3428 }
3429 compiler_use_next_block(c, if_cleanup);
3430 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3431 compiler_use_next_block(c, anchor);
3432
3433 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003434}
3435
3436static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003437compiler_comprehension(struct compiler *c, expr_ty e, int type,
3438 identifier name, asdl_seq *generators, expr_ty elt,
3439 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003441 PyCodeObject *co = NULL;
3442 expr_ty outermost_iter;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003443 PyObject *qualname = NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445 outermost_iter = ((comprehension_ty)
3446 asdl_seq_GET(generators, 0))->iter;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003447
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003448 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
3449 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003452 if (type != COMP_GENEXP) {
3453 int op;
3454 switch (type) {
3455 case COMP_LISTCOMP:
3456 op = BUILD_LIST;
3457 break;
3458 case COMP_SETCOMP:
3459 op = BUILD_SET;
3460 break;
3461 case COMP_DICTCOMP:
3462 op = BUILD_MAP;
3463 break;
3464 default:
3465 PyErr_Format(PyExc_SystemError,
3466 "unknown comprehension type %d", type);
3467 goto error_in_scope;
3468 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003470 ADDOP_I(c, op, 0);
3471 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003473 if (!compiler_comprehension_generator(c, generators, 0, elt,
3474 val, type))
3475 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 if (type != COMP_GENEXP) {
3478 ADDOP(c, RETURN_VALUE);
3479 }
3480
3481 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04003482 qualname = c->u->u_qualname;
3483 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003484 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04003485 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003486 goto error;
3487
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003488 if (!compiler_make_closure(c, co, 0, qualname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 goto error;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003490 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003491 Py_DECREF(co);
3492
3493 VISIT(c, expr, outermost_iter);
3494 ADDOP(c, GET_ITER);
3495 ADDOP_I(c, CALL_FUNCTION, 1);
3496 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003497error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003498 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003499error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003500 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003501 Py_XDECREF(co);
3502 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003503}
3504
3505static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003506compiler_genexp(struct compiler *c, expr_ty e)
3507{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 static identifier name;
3509 if (!name) {
3510 name = PyUnicode_FromString("<genexpr>");
3511 if (!name)
3512 return 0;
3513 }
3514 assert(e->kind == GeneratorExp_kind);
3515 return compiler_comprehension(c, e, COMP_GENEXP, name,
3516 e->v.GeneratorExp.generators,
3517 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003518}
3519
3520static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00003521compiler_listcomp(struct compiler *c, expr_ty e)
3522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003523 static identifier name;
3524 if (!name) {
3525 name = PyUnicode_FromString("<listcomp>");
3526 if (!name)
3527 return 0;
3528 }
3529 assert(e->kind == ListComp_kind);
3530 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
3531 e->v.ListComp.generators,
3532 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003533}
3534
3535static int
3536compiler_setcomp(struct compiler *c, expr_ty e)
3537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003538 static identifier name;
3539 if (!name) {
3540 name = PyUnicode_FromString("<setcomp>");
3541 if (!name)
3542 return 0;
3543 }
3544 assert(e->kind == SetComp_kind);
3545 return compiler_comprehension(c, e, COMP_SETCOMP, name,
3546 e->v.SetComp.generators,
3547 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00003548}
3549
3550
3551static int
3552compiler_dictcomp(struct compiler *c, expr_ty e)
3553{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003554 static identifier name;
3555 if (!name) {
3556 name = PyUnicode_FromString("<dictcomp>");
3557 if (!name)
3558 return 0;
3559 }
3560 assert(e->kind == DictComp_kind);
3561 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
3562 e->v.DictComp.generators,
3563 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003564}
3565
3566
3567static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003568compiler_visit_keyword(struct compiler *c, keyword_ty k)
3569{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003570 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3571 VISIT(c, expr, k->value);
3572 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003573}
3574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003575/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003576 whether they are true or false.
3577
3578 Return values: 1 for true, 0 for false, -1 for non-constant.
3579 */
3580
3581static int
Georg Brandl8334fd92010-12-04 10:26:46 +00003582expr_constant(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003583{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 char *id;
3585 switch (e->kind) {
3586 case Ellipsis_kind:
3587 return 1;
3588 case Num_kind:
3589 return PyObject_IsTrue(e->v.Num.n);
3590 case Str_kind:
3591 return PyObject_IsTrue(e->v.Str.s);
3592 case Name_kind:
3593 /* optimize away names that can't be reassigned */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003594 id = PyUnicode_AsUTF8(e->v.Name.id);
Benjamin Peterson442f2092012-12-06 17:41:04 -05003595 if (id && strcmp(id, "__debug__") == 0)
3596 return !c->c_optimize;
3597 return -1;
3598 case NameConstant_kind: {
3599 PyObject *o = e->v.NameConstant.value;
3600 if (o == Py_None)
3601 return 0;
3602 else if (o == Py_True)
3603 return 1;
3604 else if (o == Py_False)
3605 return 0;
3606 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003607 default:
3608 return -1;
3609 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003610}
3611
Yury Selivanov75445082015-05-11 22:57:16 -04003612
3613/*
3614 Implements the async with statement.
3615
3616 The semantics outlined in that PEP are as follows:
3617
3618 async with EXPR as VAR:
3619 BLOCK
3620
3621 It is implemented roughly as:
3622
3623 context = EXPR
3624 exit = context.__aexit__ # not calling it
3625 value = await context.__aenter__()
3626 try:
3627 VAR = value # if VAR present in the syntax
3628 BLOCK
3629 finally:
3630 if an exception was raised:
3631 exc = copy of (exception, instance, traceback)
3632 else:
3633 exc = (None, None, None)
3634 if not (await exit(*exc)):
3635 raise
3636 */
3637static int
3638compiler_async_with(struct compiler *c, stmt_ty s, int pos)
3639{
3640 basicblock *block, *finally;
3641 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
3642
3643 assert(s->kind == AsyncWith_kind);
3644
3645 block = compiler_new_block(c);
3646 finally = compiler_new_block(c);
3647 if (!block || !finally)
3648 return 0;
3649
3650 /* Evaluate EXPR */
3651 VISIT(c, expr, item->context_expr);
3652
3653 ADDOP(c, BEFORE_ASYNC_WITH);
3654 ADDOP(c, GET_AWAITABLE);
3655 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3656 ADDOP(c, YIELD_FROM);
3657
3658 ADDOP_JREL(c, SETUP_ASYNC_WITH, finally);
3659
3660 /* SETUP_ASYNC_WITH pushes a finally block. */
3661 compiler_use_next_block(c, block);
3662 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
3663 return 0;
3664 }
3665
3666 if (item->optional_vars) {
3667 VISIT(c, expr, item->optional_vars);
3668 }
3669 else {
3670 /* Discard result from context.__aenter__() */
3671 ADDOP(c, POP_TOP);
3672 }
3673
3674 pos++;
3675 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
3676 /* BLOCK code */
3677 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
3678 else if (!compiler_async_with(c, s, pos))
3679 return 0;
3680
3681 /* End of try block; start the finally block */
3682 ADDOP(c, POP_BLOCK);
3683 compiler_pop_fblock(c, FINALLY_TRY, block);
3684
3685 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3686 compiler_use_next_block(c, finally);
3687 if (!compiler_push_fblock(c, FINALLY_END, finally))
3688 return 0;
3689
3690 /* Finally block starts; context.__exit__ is on the stack under
3691 the exception or return information. Just issue our magic
3692 opcode. */
3693 ADDOP(c, WITH_CLEANUP_START);
3694
3695 ADDOP(c, GET_AWAITABLE);
3696 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3697 ADDOP(c, YIELD_FROM);
3698
3699 ADDOP(c, WITH_CLEANUP_FINISH);
3700
3701 /* Finally block ends. */
3702 ADDOP(c, END_FINALLY);
3703 compiler_pop_fblock(c, FINALLY_END, finally);
3704 return 1;
3705}
3706
3707
Guido van Rossumc2e20742006-02-27 22:32:47 +00003708/*
3709 Implements the with statement from PEP 343.
3710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 The semantics outlined in that PEP are as follows:
Guido van Rossumc2e20742006-02-27 22:32:47 +00003712
3713 with EXPR as VAR:
3714 BLOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003715
Guido van Rossumc2e20742006-02-27 22:32:47 +00003716 It is implemented roughly as:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003717
Thomas Wouters477c8d52006-05-27 19:21:47 +00003718 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00003719 exit = context.__exit__ # not calling it
3720 value = context.__enter__()
3721 try:
3722 VAR = value # if VAR present in the syntax
3723 BLOCK
3724 finally:
3725 if an exception was raised:
Serhiy Storchakad741a882015-06-11 00:06:39 +03003726 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003727 else:
Serhiy Storchakad741a882015-06-11 00:06:39 +03003728 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003729 exit(*exc)
3730 */
3731static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003732compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003733{
Guido van Rossumc2e20742006-02-27 22:32:47 +00003734 basicblock *block, *finally;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003735 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003736
3737 assert(s->kind == With_kind);
3738
Guido van Rossumc2e20742006-02-27 22:32:47 +00003739 block = compiler_new_block(c);
3740 finally = compiler_new_block(c);
3741 if (!block || !finally)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003742 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003743
Thomas Wouters477c8d52006-05-27 19:21:47 +00003744 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003745 VISIT(c, expr, item->context_expr);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003746 ADDOP_JREL(c, SETUP_WITH, finally);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003747
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003748 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003749 compiler_use_next_block(c, block);
3750 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003751 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003752 }
3753
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003754 if (item->optional_vars) {
3755 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003756 }
3757 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003759 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003760 }
3761
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003762 pos++;
3763 if (pos == asdl_seq_LEN(s->v.With.items))
3764 /* BLOCK code */
3765 VISIT_SEQ(c, stmt, s->v.With.body)
3766 else if (!compiler_with(c, s, pos))
3767 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003768
3769 /* End of try block; start the finally block */
3770 ADDOP(c, POP_BLOCK);
3771 compiler_pop_fblock(c, FINALLY_TRY, block);
3772
3773 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3774 compiler_use_next_block(c, finally);
3775 if (!compiler_push_fblock(c, FINALLY_END, finally))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003776 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003777
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003778 /* Finally block starts; context.__exit__ is on the stack under
3779 the exception or return information. Just issue our magic
3780 opcode. */
Yury Selivanov75445082015-05-11 22:57:16 -04003781 ADDOP(c, WITH_CLEANUP_START);
3782 ADDOP(c, WITH_CLEANUP_FINISH);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003783
3784 /* Finally block ends. */
3785 ADDOP(c, END_FINALLY);
3786 compiler_pop_fblock(c, FINALLY_END, finally);
3787 return 1;
3788}
3789
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003790static int
3791compiler_visit_expr(struct compiler *c, expr_ty e)
3792{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003793 /* If expr e has a different line number than the last expr/stmt,
3794 set a new line number for the next instruction.
3795 */
3796 if (e->lineno > c->u->u_lineno) {
3797 c->u->u_lineno = e->lineno;
3798 c->u->u_lineno_set = 0;
3799 }
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00003800 /* Updating the column offset is always harmless. */
3801 c->u->u_col_offset = e->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003802 switch (e->kind) {
3803 case BoolOp_kind:
3804 return compiler_boolop(c, e);
3805 case BinOp_kind:
3806 VISIT(c, expr, e->v.BinOp.left);
3807 VISIT(c, expr, e->v.BinOp.right);
3808 ADDOP(c, binop(c, e->v.BinOp.op));
3809 break;
3810 case UnaryOp_kind:
3811 VISIT(c, expr, e->v.UnaryOp.operand);
3812 ADDOP(c, unaryop(e->v.UnaryOp.op));
3813 break;
3814 case Lambda_kind:
3815 return compiler_lambda(c, e);
3816 case IfExp_kind:
3817 return compiler_ifexp(c, e);
3818 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003819 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003820 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003821 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003822 case GeneratorExp_kind:
3823 return compiler_genexp(c, e);
3824 case ListComp_kind:
3825 return compiler_listcomp(c, e);
3826 case SetComp_kind:
3827 return compiler_setcomp(c, e);
3828 case DictComp_kind:
3829 return compiler_dictcomp(c, e);
3830 case Yield_kind:
3831 if (c->u->u_ste->ste_type != FunctionBlock)
3832 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04003833 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
3834 return compiler_error(c, "'yield' inside async function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00003835 if (e->v.Yield.value) {
3836 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003837 }
3838 else {
3839 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3840 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00003841 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003842 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00003843 case YieldFrom_kind:
3844 if (c->u->u_ste->ste_type != FunctionBlock)
3845 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04003846
3847 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
3848 return compiler_error(c, "'yield from' inside async function");
3849
Mark Dickinsonded35ae2012-11-25 14:36:26 +00003850 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003851 ADDOP(c, GET_YIELD_FROM_ITER);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00003852 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3853 ADDOP(c, YIELD_FROM);
3854 break;
Yury Selivanov75445082015-05-11 22:57:16 -04003855 case Await_kind:
3856 if (c->u->u_ste->ste_type != FunctionBlock)
3857 return compiler_error(c, "'await' outside function");
3858
3859 /* this check won't be triggered while we have AWAIT token */
3860 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION)
3861 return compiler_error(c, "'await' outside async function");
3862
3863 VISIT(c, expr, e->v.Await.value);
3864 ADDOP(c, GET_AWAITABLE);
3865 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3866 ADDOP(c, YIELD_FROM);
3867 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003868 case Compare_kind:
3869 return compiler_compare(c, e);
3870 case Call_kind:
3871 return compiler_call(c, e);
3872 case Num_kind:
3873 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3874 break;
3875 case Str_kind:
3876 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3877 break;
3878 case Bytes_kind:
3879 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
3880 break;
3881 case Ellipsis_kind:
3882 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3883 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05003884 case NameConstant_kind:
3885 ADDOP_O(c, LOAD_CONST, e->v.NameConstant.value, consts);
3886 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003887 /* The following exprs can be assignment targets. */
3888 case Attribute_kind:
3889 if (e->v.Attribute.ctx != AugStore)
3890 VISIT(c, expr, e->v.Attribute.value);
3891 switch (e->v.Attribute.ctx) {
3892 case AugLoad:
3893 ADDOP(c, DUP_TOP);
3894 /* Fall through to load */
3895 case Load:
3896 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3897 break;
3898 case AugStore:
3899 ADDOP(c, ROT_TWO);
3900 /* Fall through to save */
3901 case Store:
3902 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3903 break;
3904 case Del:
3905 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3906 break;
3907 case Param:
3908 default:
3909 PyErr_SetString(PyExc_SystemError,
3910 "param invalid in attribute expression");
3911 return 0;
3912 }
3913 break;
3914 case Subscript_kind:
3915 switch (e->v.Subscript.ctx) {
3916 case AugLoad:
3917 VISIT(c, expr, e->v.Subscript.value);
3918 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3919 break;
3920 case Load:
3921 VISIT(c, expr, e->v.Subscript.value);
3922 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3923 break;
3924 case AugStore:
3925 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3926 break;
3927 case Store:
3928 VISIT(c, expr, e->v.Subscript.value);
3929 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3930 break;
3931 case Del:
3932 VISIT(c, expr, e->v.Subscript.value);
3933 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3934 break;
3935 case Param:
3936 default:
3937 PyErr_SetString(PyExc_SystemError,
3938 "param invalid in subscript expression");
3939 return 0;
3940 }
3941 break;
3942 case Starred_kind:
3943 switch (e->v.Starred.ctx) {
3944 case Store:
3945 /* In all legitimate cases, the Starred node was already replaced
3946 * by compiler_list/compiler_tuple. XXX: is that okay? */
3947 return compiler_error(c,
3948 "starred assignment target must be in a list or tuple");
3949 default:
3950 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003951 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003952 }
3953 break;
3954 case Name_kind:
3955 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3956 /* child nodes of List and Tuple will have expr_context set */
3957 case List_kind:
3958 return compiler_list(c, e);
3959 case Tuple_kind:
3960 return compiler_tuple(c, e);
3961 }
3962 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003963}
3964
3965static int
3966compiler_augassign(struct compiler *c, stmt_ty s)
3967{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003968 expr_ty e = s->v.AugAssign.target;
3969 expr_ty auge;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003971 assert(s->kind == AugAssign_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003973 switch (e->kind) {
3974 case Attribute_kind:
3975 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
3976 AugLoad, e->lineno, e->col_offset, c->c_arena);
3977 if (auge == NULL)
3978 return 0;
3979 VISIT(c, expr, auge);
3980 VISIT(c, expr, s->v.AugAssign.value);
3981 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3982 auge->v.Attribute.ctx = AugStore;
3983 VISIT(c, expr, auge);
3984 break;
3985 case Subscript_kind:
3986 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
3987 AugLoad, e->lineno, e->col_offset, c->c_arena);
3988 if (auge == NULL)
3989 return 0;
3990 VISIT(c, expr, auge);
3991 VISIT(c, expr, s->v.AugAssign.value);
3992 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3993 auge->v.Subscript.ctx = AugStore;
3994 VISIT(c, expr, auge);
3995 break;
3996 case Name_kind:
3997 if (!compiler_nameop(c, e->v.Name.id, Load))
3998 return 0;
3999 VISIT(c, expr, s->v.AugAssign.value);
4000 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
4001 return compiler_nameop(c, e->v.Name.id, Store);
4002 default:
4003 PyErr_Format(PyExc_SystemError,
4004 "invalid node type (%d) for augmented assignment",
4005 e->kind);
4006 return 0;
4007 }
4008 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004009}
4010
4011static int
4012compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
4013{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004014 struct fblockinfo *f;
4015 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
4016 PyErr_SetString(PyExc_SystemError,
4017 "too many statically nested blocks");
4018 return 0;
4019 }
4020 f = &c->u->u_fblock[c->u->u_nfblocks++];
4021 f->fb_type = t;
4022 f->fb_block = b;
4023 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004024}
4025
4026static void
4027compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
4028{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004029 struct compiler_unit *u = c->u;
4030 assert(u->u_nfblocks > 0);
4031 u->u_nfblocks--;
4032 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
4033 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004034}
4035
Thomas Wouters89f507f2006-12-13 04:49:30 +00004036static int
4037compiler_in_loop(struct compiler *c) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004038 int i;
4039 struct compiler_unit *u = c->u;
4040 for (i = 0; i < u->u_nfblocks; ++i) {
4041 if (u->u_fblock[i].fb_type == LOOP)
4042 return 1;
4043 }
4044 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004045}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004046/* Raises a SyntaxError and returns 0.
4047 If something goes wrong, a different exception may be raised.
4048*/
4049
4050static int
4051compiler_error(struct compiler *c, const char *errstr)
4052{
Benjamin Peterson43b06862011-05-27 09:08:01 -05004053 PyObject *loc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004054 PyObject *u = NULL, *v = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004055
Victor Stinner14e461d2013-08-26 22:28:21 +02004056 loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004057 if (!loc) {
4058 Py_INCREF(Py_None);
4059 loc = Py_None;
4060 }
Victor Stinner14e461d2013-08-26 22:28:21 +02004061 u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00004062 c->u->u_col_offset, loc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004063 if (!u)
4064 goto exit;
4065 v = Py_BuildValue("(zO)", errstr, u);
4066 if (!v)
4067 goto exit;
4068 PyErr_SetObject(PyExc_SyntaxError, v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004069 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004070 Py_DECREF(loc);
4071 Py_XDECREF(u);
4072 Py_XDECREF(v);
4073 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004074}
4075
4076static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004077compiler_handle_subscr(struct compiler *c, const char *kind,
4078 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004079{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004080 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004082 /* XXX this code is duplicated */
4083 switch (ctx) {
4084 case AugLoad: /* fall through to Load */
4085 case Load: op = BINARY_SUBSCR; break;
4086 case AugStore:/* fall through to Store */
4087 case Store: op = STORE_SUBSCR; break;
4088 case Del: op = DELETE_SUBSCR; break;
4089 case Param:
4090 PyErr_Format(PyExc_SystemError,
4091 "invalid %s kind %d in subscript\n",
4092 kind, ctx);
4093 return 0;
4094 }
4095 if (ctx == AugLoad) {
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00004096 ADDOP(c, DUP_TOP_TWO);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004097 }
4098 else if (ctx == AugStore) {
4099 ADDOP(c, ROT_THREE);
4100 }
4101 ADDOP(c, op);
4102 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004103}
4104
4105static int
4106compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
4107{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004108 int n = 2;
4109 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004111 /* only handles the cases where BUILD_SLICE is emitted */
4112 if (s->v.Slice.lower) {
4113 VISIT(c, expr, s->v.Slice.lower);
4114 }
4115 else {
4116 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4117 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004119 if (s->v.Slice.upper) {
4120 VISIT(c, expr, s->v.Slice.upper);
4121 }
4122 else {
4123 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4124 }
4125
4126 if (s->v.Slice.step) {
4127 n++;
4128 VISIT(c, expr, s->v.Slice.step);
4129 }
4130 ADDOP_I(c, BUILD_SLICE, n);
4131 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004132}
4133
4134static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004135compiler_visit_nested_slice(struct compiler *c, slice_ty s,
4136 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004138 switch (s->kind) {
4139 case Slice_kind:
4140 return compiler_slice(c, s, ctx);
4141 case Index_kind:
4142 VISIT(c, expr, s->v.Index.value);
4143 break;
4144 case ExtSlice_kind:
4145 default:
4146 PyErr_SetString(PyExc_SystemError,
4147 "extended slice invalid in nested slice");
4148 return 0;
4149 }
4150 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004151}
4152
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004153static int
4154compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
4155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004156 char * kindname = NULL;
4157 switch (s->kind) {
4158 case Index_kind:
4159 kindname = "index";
4160 if (ctx != AugStore) {
4161 VISIT(c, expr, s->v.Index.value);
4162 }
4163 break;
4164 case Slice_kind:
4165 kindname = "slice";
4166 if (ctx != AugStore) {
4167 if (!compiler_slice(c, s, ctx))
4168 return 0;
4169 }
4170 break;
4171 case ExtSlice_kind:
4172 kindname = "extended slice";
4173 if (ctx != AugStore) {
Victor Stinnerad9a0662013-11-19 22:23:20 +01004174 Py_ssize_t i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004175 for (i = 0; i < n; i++) {
4176 slice_ty sub = (slice_ty)asdl_seq_GET(
4177 s->v.ExtSlice.dims, i);
4178 if (!compiler_visit_nested_slice(c, sub, ctx))
4179 return 0;
4180 }
4181 ADDOP_I(c, BUILD_TUPLE, n);
4182 }
4183 break;
4184 default:
4185 PyErr_Format(PyExc_SystemError,
4186 "invalid subscript kind %d", s->kind);
4187 return 0;
4188 }
4189 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004190}
4191
Thomas Wouters89f507f2006-12-13 04:49:30 +00004192/* End of the compiler section, beginning of the assembler section */
4193
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004194/* do depth-first search of basic block graph, starting with block.
4195 post records the block indices in post-order.
4196
4197 XXX must handle implicit jumps from one block to next
4198*/
4199
Thomas Wouters89f507f2006-12-13 04:49:30 +00004200struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004201 PyObject *a_bytecode; /* string containing bytecode */
4202 int a_offset; /* offset into bytecode */
4203 int a_nblocks; /* number of reachable blocks */
4204 basicblock **a_postorder; /* list of blocks in dfs postorder */
4205 PyObject *a_lnotab; /* string containing lnotab */
4206 int a_lnotab_off; /* offset into lnotab */
4207 int a_lineno; /* last lineno of emitted instruction */
4208 int a_lineno_off; /* bytecode offset of last lineno */
Thomas Wouters89f507f2006-12-13 04:49:30 +00004209};
4210
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004211static void
4212dfs(struct compiler *c, basicblock *b, struct assembler *a)
4213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 int i;
4215 struct instr *instr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004217 if (b->b_seen)
4218 return;
4219 b->b_seen = 1;
4220 if (b->b_next != NULL)
4221 dfs(c, b->b_next, a);
4222 for (i = 0; i < b->b_iused; i++) {
4223 instr = &b->b_instr[i];
4224 if (instr->i_jrel || instr->i_jabs)
4225 dfs(c, instr->i_target, a);
4226 }
4227 a->a_postorder[a->a_nblocks++] = b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004228}
4229
Neal Norwitz2744c6c2005-11-13 01:08:38 +00004230static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004231stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
4232{
Larry Hastings3a907972013-11-23 14:49:22 -08004233 int i, target_depth, effect;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004234 struct instr *instr;
4235 if (b->b_seen || b->b_startdepth >= depth)
4236 return maxdepth;
4237 b->b_seen = 1;
4238 b->b_startdepth = depth;
4239 for (i = 0; i < b->b_iused; i++) {
4240 instr = &b->b_instr[i];
Larry Hastings3a907972013-11-23 14:49:22 -08004241 effect = PyCompile_OpcodeStackEffect(instr->i_opcode, instr->i_oparg);
4242 if (effect == PY_INVALID_STACK_EFFECT) {
4243 fprintf(stderr, "opcode = %d\n", instr->i_opcode);
4244 Py_FatalError("PyCompile_OpcodeStackEffect()");
4245 }
4246 depth += effect;
4247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004248 if (depth > maxdepth)
4249 maxdepth = depth;
4250 assert(depth >= 0); /* invalid code or bug in stackdepth() */
4251 if (instr->i_jrel || instr->i_jabs) {
4252 target_depth = depth;
4253 if (instr->i_opcode == FOR_ITER) {
4254 target_depth = depth-2;
Antoine Pitrou99614052014-05-23 11:46:03 +02004255 }
4256 else if (instr->i_opcode == SETUP_FINALLY ||
4257 instr->i_opcode == SETUP_EXCEPT) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004258 target_depth = depth+3;
4259 if (target_depth > maxdepth)
4260 maxdepth = target_depth;
4261 }
Antoine Pitrou99614052014-05-23 11:46:03 +02004262 else if (instr->i_opcode == JUMP_IF_TRUE_OR_POP ||
4263 instr->i_opcode == JUMP_IF_FALSE_OR_POP)
4264 depth = depth - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004265 maxdepth = stackdepth_walk(c, instr->i_target,
4266 target_depth, maxdepth);
4267 if (instr->i_opcode == JUMP_ABSOLUTE ||
4268 instr->i_opcode == JUMP_FORWARD) {
4269 goto out; /* remaining code is dead */
4270 }
4271 }
4272 }
4273 if (b->b_next)
4274 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004275out:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004276 b->b_seen = 0;
4277 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004278}
4279
4280/* Find the flow path that needs the largest stack. We assume that
4281 * cycles in the flow graph have no net effect on the stack depth.
4282 */
4283static int
4284stackdepth(struct compiler *c)
4285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004286 basicblock *b, *entryblock;
4287 entryblock = NULL;
4288 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4289 b->b_seen = 0;
4290 b->b_startdepth = INT_MIN;
4291 entryblock = b;
4292 }
4293 if (!entryblock)
4294 return 0;
4295 return stackdepth_walk(c, entryblock, 0, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004296}
4297
4298static int
4299assemble_init(struct assembler *a, int nblocks, int firstlineno)
4300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004301 memset(a, 0, sizeof(struct assembler));
4302 a->a_lineno = firstlineno;
4303 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
4304 if (!a->a_bytecode)
4305 return 0;
4306 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
4307 if (!a->a_lnotab)
4308 return 0;
Christian Heimes724b8282013-12-04 08:42:46 +01004309 if ((size_t)nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004310 PyErr_NoMemory();
4311 return 0;
4312 }
4313 a->a_postorder = (basicblock **)PyObject_Malloc(
4314 sizeof(basicblock *) * nblocks);
4315 if (!a->a_postorder) {
4316 PyErr_NoMemory();
4317 return 0;
4318 }
4319 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004320}
4321
4322static void
4323assemble_free(struct assembler *a)
4324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004325 Py_XDECREF(a->a_bytecode);
4326 Py_XDECREF(a->a_lnotab);
4327 if (a->a_postorder)
4328 PyObject_Free(a->a_postorder);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004329}
4330
4331/* Return the size of a basic block in bytes. */
4332
4333static int
4334instrsize(struct instr *instr)
4335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004336 if (!instr->i_hasarg)
4337 return 1; /* 1 byte for the opcode*/
4338 if (instr->i_oparg > 0xffff)
4339 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
4340 return 3; /* 1 (opcode) + 2 (oparg) */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004341}
4342
4343static int
4344blocksize(basicblock *b)
4345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004346 int i;
4347 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004349 for (i = 0; i < b->b_iused; i++)
4350 size += instrsize(&b->b_instr[i]);
4351 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004352}
4353
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004354/* Appends a pair to the end of the line number table, a_lnotab, representing
4355 the instruction's bytecode offset and line number. See
4356 Objects/lnotab_notes.txt for the description of the line number table. */
Tim Peters2a7f3842001-06-09 09:26:21 +00004357
Guido van Rossumf68d8e52001-04-14 17:55:09 +00004358static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004359assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004361 int d_bytecode, d_lineno;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004362 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004363 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004365 d_bytecode = a->a_offset - a->a_lineno_off;
4366 d_lineno = i->i_lineno - a->a_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004368 assert(d_bytecode >= 0);
4369 assert(d_lineno >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004371 if(d_bytecode == 0 && d_lineno == 0)
4372 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00004373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374 if (d_bytecode > 255) {
4375 int j, nbytes, ncodes = d_bytecode / 255;
4376 nbytes = a->a_lnotab_off + 2 * ncodes;
4377 len = PyBytes_GET_SIZE(a->a_lnotab);
4378 if (nbytes >= len) {
4379 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
4380 len = nbytes;
4381 else if (len <= INT_MAX / 2)
4382 len *= 2;
4383 else {
4384 PyErr_NoMemory();
4385 return 0;
4386 }
4387 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
4388 return 0;
4389 }
4390 lnotab = (unsigned char *)
4391 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
4392 for (j = 0; j < ncodes; j++) {
4393 *lnotab++ = 255;
4394 *lnotab++ = 0;
4395 }
4396 d_bytecode -= ncodes * 255;
4397 a->a_lnotab_off += ncodes * 2;
4398 }
4399 assert(d_bytecode <= 255);
4400 if (d_lineno > 255) {
4401 int j, nbytes, ncodes = d_lineno / 255;
4402 nbytes = a->a_lnotab_off + 2 * ncodes;
4403 len = PyBytes_GET_SIZE(a->a_lnotab);
4404 if (nbytes >= len) {
4405 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
4406 len = nbytes;
4407 else if (len <= INT_MAX / 2)
4408 len *= 2;
4409 else {
4410 PyErr_NoMemory();
4411 return 0;
4412 }
4413 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
4414 return 0;
4415 }
4416 lnotab = (unsigned char *)
4417 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
4418 *lnotab++ = d_bytecode;
4419 *lnotab++ = 255;
4420 d_bytecode = 0;
4421 for (j = 1; j < ncodes; j++) {
4422 *lnotab++ = 0;
4423 *lnotab++ = 255;
4424 }
4425 d_lineno -= ncodes * 255;
4426 a->a_lnotab_off += ncodes * 2;
4427 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004429 len = PyBytes_GET_SIZE(a->a_lnotab);
4430 if (a->a_lnotab_off + 2 >= len) {
4431 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
4432 return 0;
4433 }
4434 lnotab = (unsigned char *)
4435 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00004436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004437 a->a_lnotab_off += 2;
4438 if (d_bytecode) {
4439 *lnotab++ = d_bytecode;
Victor Stinner4f2dab52011-05-27 16:46:51 +02004440 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004441 }
4442 else { /* First line of a block; def stmt, etc. */
4443 *lnotab++ = 0;
Victor Stinner4f2dab52011-05-27 16:46:51 +02004444 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004445 }
4446 a->a_lineno = i->i_lineno;
4447 a->a_lineno_off = a->a_offset;
4448 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004449}
4450
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004451/* assemble_emit()
4452 Extend the bytecode with a new instruction.
4453 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00004454*/
4455
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00004456static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004457assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00004458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004459 int size, arg = 0, ext = 0;
4460 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
4461 char *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004463 size = instrsize(i);
4464 if (i->i_hasarg) {
4465 arg = i->i_oparg;
4466 ext = arg >> 16;
4467 }
4468 if (i->i_lineno && !assemble_lnotab(a, i))
4469 return 0;
4470 if (a->a_offset + size >= len) {
4471 if (len > PY_SSIZE_T_MAX / 2)
4472 return 0;
4473 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
4474 return 0;
4475 }
4476 code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
4477 a->a_offset += size;
4478 if (size == 6) {
4479 assert(i->i_hasarg);
4480 *code++ = (char)EXTENDED_ARG;
4481 *code++ = ext & 0xff;
4482 *code++ = ext >> 8;
4483 arg &= 0xffff;
4484 }
4485 *code++ = i->i_opcode;
4486 if (i->i_hasarg) {
4487 assert(size == 3 || size == 6);
4488 *code++ = arg & 0xff;
Victor Stinner4f2dab52011-05-27 16:46:51 +02004489 *code++ = arg >> 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004490 }
4491 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00004492}
4493
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004494static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004495assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00004496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004497 basicblock *b;
4498 int bsize, totsize, extended_arg_count = 0, last_extended_arg_count;
4499 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00004500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 /* Compute the size of each block and fixup jump args.
4502 Replace block pointer with position in bytecode. */
4503 do {
4504 totsize = 0;
4505 for (i = a->a_nblocks - 1; i >= 0; i--) {
4506 b = a->a_postorder[i];
4507 bsize = blocksize(b);
4508 b->b_offset = totsize;
4509 totsize += bsize;
4510 }
4511 last_extended_arg_count = extended_arg_count;
4512 extended_arg_count = 0;
4513 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4514 bsize = b->b_offset;
4515 for (i = 0; i < b->b_iused; i++) {
4516 struct instr *instr = &b->b_instr[i];
4517 /* Relative jumps are computed relative to
4518 the instruction pointer after fetching
4519 the jump instruction.
4520 */
4521 bsize += instrsize(instr);
4522 if (instr->i_jabs)
4523 instr->i_oparg = instr->i_target->b_offset;
4524 else if (instr->i_jrel) {
4525 int delta = instr->i_target->b_offset - bsize;
4526 instr->i_oparg = delta;
4527 }
4528 else
4529 continue;
4530 if (instr->i_oparg > 0xffff)
4531 extended_arg_count++;
4532 }
4533 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00004534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004535 /* XXX: This is an awful hack that could hurt performance, but
4536 on the bright side it should work until we come up
4537 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00004538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004539 The issue is that in the first loop blocksize() is called
4540 which calls instrsize() which requires i_oparg be set
4541 appropriately. There is a bootstrap problem because
4542 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00004543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004544 So we loop until we stop seeing new EXTENDED_ARGs.
4545 The only EXTENDED_ARGs that could be popping up are
4546 ones in jump instructions. So this should converge
4547 fairly quickly.
4548 */
4549 } while (last_extended_arg_count != extended_arg_count);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004550}
4551
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004552static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01004553dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004555 PyObject *tuple, *k, *v;
4556 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004558 tuple = PyTuple_New(size);
4559 if (tuple == NULL)
4560 return NULL;
4561 while (PyDict_Next(dict, &pos, &k, &v)) {
4562 i = PyLong_AS_LONG(v);
4563 /* The keys of the dictionary are tuples. (see compiler_add_o)
4564 The object we want is always first, though. */
4565 k = PyTuple_GET_ITEM(k, 0);
4566 Py_INCREF(k);
4567 assert((i - offset) < size);
4568 assert((i - offset) >= 0);
4569 PyTuple_SET_ITEM(tuple, i - offset, k);
4570 }
4571 return tuple;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004572}
4573
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004574static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004575compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004576{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004577 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004578 int flags = 0;
4579 Py_ssize_t n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004580 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04004581 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004582 if (ste->ste_nested)
4583 flags |= CO_NESTED;
4584 if (ste->ste_generator)
4585 flags |= CO_GENERATOR;
4586 if (ste->ste_varargs)
4587 flags |= CO_VARARGS;
4588 if (ste->ste_varkeywords)
4589 flags |= CO_VARKEYWORDS;
4590 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00004591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004592 /* (Only) inherit compilerflags in PyCF_MASK */
4593 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00004594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004595 n = PyDict_Size(c->u->u_freevars);
4596 if (n < 0)
4597 return -1;
4598 if (n == 0) {
4599 n = PyDict_Size(c->u->u_cellvars);
4600 if (n < 0)
Victor Stinnerad9a0662013-11-19 22:23:20 +01004601 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004602 if (n == 0) {
Victor Stinnerad9a0662013-11-19 22:23:20 +01004603 flags |= CO_NOFREE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004604 }
4605 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004607 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004608}
4609
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004610static PyCodeObject *
4611makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004613 PyObject *tmp;
4614 PyCodeObject *co = NULL;
4615 PyObject *consts = NULL;
4616 PyObject *names = NULL;
4617 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004618 PyObject *name = NULL;
4619 PyObject *freevars = NULL;
4620 PyObject *cellvars = NULL;
4621 PyObject *bytecode = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004622 Py_ssize_t nlocals;
4623 int nlocals_int;
4624 int flags;
Victor Stinnerf8e32212013-11-19 23:56:34 +01004625 int argcount, kwonlyargcount;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004627 tmp = dict_keys_inorder(c->u->u_consts, 0);
4628 if (!tmp)
4629 goto error;
4630 consts = PySequence_List(tmp); /* optimize_code requires a list */
4631 Py_DECREF(tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004633 names = dict_keys_inorder(c->u->u_names, 0);
4634 varnames = dict_keys_inorder(c->u->u_varnames, 0);
4635 if (!consts || !names || !varnames)
4636 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004638 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4639 if (!cellvars)
4640 goto error;
4641 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4642 if (!freevars)
4643 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004645 nlocals = PyDict_Size(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01004646 assert(nlocals < INT_MAX);
4647 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
4648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004649 flags = compute_code_flags(c);
4650 if (flags < 0)
4651 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004653 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
4654 if (!bytecode)
4655 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004657 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4658 if (!tmp)
4659 goto error;
4660 Py_DECREF(consts);
4661 consts = tmp;
4662
Victor Stinnerf8e32212013-11-19 23:56:34 +01004663 argcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
4664 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
4665 co = PyCode_New(argcount, kwonlyargcount,
Victor Stinnerad9a0662013-11-19 22:23:20 +01004666 nlocals_int, stackdepth(c), flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004667 bytecode, consts, names, varnames,
4668 freevars, cellvars,
Victor Stinner14e461d2013-08-26 22:28:21 +02004669 c->c_filename, c->u->u_name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004670 c->u->u_firstlineno,
4671 a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004672 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004673 Py_XDECREF(consts);
4674 Py_XDECREF(names);
4675 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004676 Py_XDECREF(name);
4677 Py_XDECREF(freevars);
4678 Py_XDECREF(cellvars);
4679 Py_XDECREF(bytecode);
4680 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004681}
4682
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004683
4684/* For debugging purposes only */
4685#if 0
4686static void
4687dump_instr(const struct instr *i)
4688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004689 const char *jrel = i->i_jrel ? "jrel " : "";
4690 const char *jabs = i->i_jabs ? "jabs " : "";
4691 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004693 *arg = '\0';
4694 if (i->i_hasarg)
4695 sprintf(arg, "arg: %d ", i->i_oparg);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004697 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4698 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004699}
4700
4701static void
4702dump_basicblock(const basicblock *b)
4703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004704 const char *seen = b->b_seen ? "seen " : "";
4705 const char *b_return = b->b_return ? "return " : "";
4706 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4707 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4708 if (b->b_instr) {
4709 int i;
4710 for (i = 0; i < b->b_iused; i++) {
4711 fprintf(stderr, " [%02d] ", i);
4712 dump_instr(b->b_instr + i);
4713 }
4714 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004715}
4716#endif
4717
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004718static PyCodeObject *
4719assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004720{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004721 basicblock *b, *entryblock;
4722 struct assembler a;
4723 int i, j, nblocks;
4724 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004726 /* Make sure every block that falls off the end returns None.
4727 XXX NEXT_BLOCK() isn't quite right, because if the last
4728 block ends with a jump or return b_next shouldn't set.
4729 */
4730 if (!c->u->u_curblock->b_return) {
4731 NEXT_BLOCK(c);
4732 if (addNone)
4733 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4734 ADDOP(c, RETURN_VALUE);
4735 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004737 nblocks = 0;
4738 entryblock = NULL;
4739 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4740 nblocks++;
4741 entryblock = b;
4742 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004744 /* Set firstlineno if it wasn't explicitly set. */
4745 if (!c->u->u_firstlineno) {
4746 if (entryblock && entryblock->b_instr)
4747 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4748 else
4749 c->u->u_firstlineno = 1;
4750 }
4751 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4752 goto error;
4753 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004755 /* Can't modify the bytecode after computing jump offsets. */
4756 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004758 /* Emit code in reverse postorder from dfs. */
4759 for (i = a.a_nblocks - 1; i >= 0; i--) {
4760 b = a.a_postorder[i];
4761 for (j = 0; j < b->b_iused; j++)
4762 if (!assemble_emit(&a, &b->b_instr[j]))
4763 goto error;
4764 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004766 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4767 goto error;
4768 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset) < 0)
4769 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004771 co = makecode(c, &a);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004772 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004773 assemble_free(&a);
4774 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004775}
Georg Brandl8334fd92010-12-04 10:26:46 +00004776
4777#undef PyAST_Compile
4778PyAPI_FUNC(PyCodeObject *)
4779PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
4780 PyArena *arena)
4781{
4782 return PyAST_CompileEx(mod, filename, flags, -1, arena);
4783}
4784