blob: 31efc28da43fa0e09b8383318ac9ba769dc8888b [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file compiles an abstract syntax tree (AST) into Python bytecode.
3 *
4 * The primary entry point is PyAST_Compile(), which returns a
5 * PyCodeObject. The compiler makes several passes to build the code
6 * object:
7 * 1. Checks for future statements. See future.c
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00008 * 2. Builds a symbol table. See symtable.c.
Thomas Wouters89f507f2006-12-13 04:49:30 +00009 * 3. Generate code for basic blocks. See compiler_mod() in this file.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000010 * 4. Assemble the basic blocks into final code. See assemble() in
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000011 * this file.
Thomas Wouters89f507f2006-12-13 04:49:30 +000012 * 5. Optimize the byte code (peephole optimizations). See peephole.c
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013 *
14 * Note that compiler_mod() suggests module, but the module ast type
15 * (mod_ty) has cases for expressions and interactive statements.
Nick Coghlan944d3eb2005-11-16 12:46:55 +000016 *
Jeremy Hyltone9357b22006-03-01 15:47:05 +000017 * CAUTION: The VISIT_* macros abort the current function when they
18 * encounter a problem. So don't invoke them when there is memory
19 * which needs to be released. Code blocks are OK, as the compiler
Thomas Wouters89f507f2006-12-13 04:49:30 +000020 * structure takes care of releasing those. Use the arena to manage
21 * objects.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022 */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000023
Guido van Rossum79f25d91997-04-29 20:08:16 +000024#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000025
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000026#include "Python-ast.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000027#include "node.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000028#include "ast.h"
29#include "code.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000030#include "symtable.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031#include "opcode.h"
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030032#include "wordcode_helpers.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000033
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000034#define DEFAULT_BLOCK_SIZE 16
35#define DEFAULT_BLOCKS 8
36#define DEFAULT_CODE_SIZE 128
37#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000038
Nick Coghlan650f0d02007-04-15 12:05:43 +000039#define COMP_GENEXP 0
40#define COMP_LISTCOMP 1
41#define COMP_SETCOMP 2
Guido van Rossum992d4a32007-07-11 13:09:30 +000042#define COMP_DICTCOMP 3
Nick Coghlan650f0d02007-04-15 12:05:43 +000043
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000044struct instr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 unsigned i_jabs : 1;
46 unsigned i_jrel : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 unsigned char i_opcode;
48 int i_oparg;
49 struct basicblock_ *i_target; /* target block (if jump instruction) */
50 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000051};
52
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000053typedef struct basicblock_ {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000054 /* Each basicblock in a compilation unit is linked via b_list in the
55 reverse order that the block are allocated. b_list points to the next
56 block, not to be confused with b_next, which is next by control flow. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 struct basicblock_ *b_list;
58 /* number of instructions used */
59 int b_iused;
60 /* length of instruction array (b_instr) */
61 int b_ialloc;
62 /* pointer to an array of instructions, initially NULL */
63 struct instr *b_instr;
64 /* If b_next is non-NULL, it is a pointer to the next
65 block reached by normal control flow. */
66 struct basicblock_ *b_next;
67 /* b_seen is used to perform a DFS of basicblocks. */
68 unsigned b_seen : 1;
69 /* b_return is true if a RETURN_VALUE opcode is inserted. */
70 unsigned b_return : 1;
71 /* depth of stack upon entry of block, computed by stackdepth() */
72 int b_startdepth;
73 /* instruction offset for block, computed by assemble_jump_offsets() */
74 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000075} basicblock;
76
77/* fblockinfo tracks the current frame block.
78
Jeremy Hyltone9357b22006-03-01 15:47:05 +000079A frame block is used to handle loops, try/except, and try/finally.
80It's called a frame block to distinguish it from a basic block in the
81compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000082*/
83
84enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
85
86struct fblockinfo {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 enum fblocktype fb_type;
88 basicblock *fb_block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000089};
90
Antoine Pitrou86a36b52011-11-25 18:56:07 +010091enum {
92 COMPILER_SCOPE_MODULE,
93 COMPILER_SCOPE_CLASS,
94 COMPILER_SCOPE_FUNCTION,
Yury Selivanov75445082015-05-11 22:57:16 -040095 COMPILER_SCOPE_ASYNC_FUNCTION,
Benjamin Peterson6b4f7802013-10-20 17:50:28 -040096 COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +010097 COMPILER_SCOPE_COMPREHENSION,
98};
99
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000100/* The following items change on entry and exit of code blocks.
101 They must be saved and restored when returning to a block.
102*/
103struct compiler_unit {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 PySTEntryObject *u_ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 PyObject *u_name;
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400107 PyObject *u_qualname; /* dot-separated qualified name (lazy) */
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100108 int u_scope_type;
109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 /* The following fields are dicts that map objects to
111 the index of them in co_XXX. The index is used as
112 the argument for opcodes that refer to those collections.
113 */
114 PyObject *u_consts; /* all constants */
115 PyObject *u_names; /* all names */
116 PyObject *u_varnames; /* local variables */
117 PyObject *u_cellvars; /* cell variables */
118 PyObject *u_freevars; /* free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 PyObject *u_private; /* for private name mangling */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000121
Victor Stinnerf8e32212013-11-19 23:56:34 +0100122 Py_ssize_t u_argcount; /* number of arguments for block */
123 Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 /* Pointer to the most recently allocated block. By following b_list
125 members, you can reach all early allocated blocks. */
126 basicblock *u_blocks;
127 basicblock *u_curblock; /* pointer to current block */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 int u_nfblocks;
130 struct fblockinfo u_fblock[CO_MAXBLOCKS];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 int u_firstlineno; /* the first lineno of the block */
133 int u_lineno; /* the lineno for the current stmt */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000134 int u_col_offset; /* the offset of the current stmt */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 int u_lineno_set; /* boolean to indicate whether instr
136 has been generated with current lineno */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000137};
138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139/* This struct captures the global state of a compilation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000140
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000141The u pointer points to the current compilation unit, while units
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142for enclosing blocks are stored in c_stack. The u and c_stack are
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000143managed by compiler_enter_scope() and compiler_exit_scope().
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000144
145Note that we don't track recursion levels during compilation - the
146task of detecting and rejecting excessive levels of nesting is
147handled by the symbol analysis pass.
148
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000149*/
150
151struct compiler {
Victor Stinner14e461d2013-08-26 22:28:21 +0200152 PyObject *c_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 struct symtable *c_st;
154 PyFutureFeatures *c_future; /* pointer to module's __future__ */
155 PyCompilerFlags *c_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000156
Georg Brandl8334fd92010-12-04 10:26:46 +0000157 int c_optimize; /* optimization level */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 int c_interactive; /* true if in interactive mode */
159 int c_nestlevel;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 struct compiler_unit *u; /* compiler state for current block */
162 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
163 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000164};
165
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100166static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000167static void compiler_free(struct compiler *);
168static basicblock *compiler_new_block(struct compiler *);
169static int compiler_next_instr(struct compiler *, basicblock *);
170static int compiler_addop(struct compiler *, int);
171static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
Victor Stinnerf8e32212013-11-19 23:56:34 +0100172static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000173static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000174static int compiler_error(struct compiler *, const char *);
175static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
176
177static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
178static int compiler_visit_stmt(struct compiler *, stmt_ty);
179static int compiler_visit_keyword(struct compiler *, keyword_ty);
180static int compiler_visit_expr(struct compiler *, expr_ty);
181static int compiler_augassign(struct compiler *, stmt_ty);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700182static int compiler_annassign(struct compiler *, stmt_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000183static 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);
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +0200194static int expr_constant(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 Stinner976bb402016-03-23 11:36:19 +0100199static int compiler_call_helper(struct compiler *c, int 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
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700205static int compiler_sync_comprehension_generator(
206 struct compiler *c,
207 asdl_seq *generators, int gen_index,
208 expr_ty elt, expr_ty val, int type);
209
210static int compiler_async_comprehension_generator(
211 struct compiler *c,
212 asdl_seq *generators, int gen_index,
213 expr_ty elt, expr_ty val, int type);
214
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000215static PyCodeObject *assemble(struct compiler *, int addNone);
216static PyObject *__doc__;
217
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400218#define CAPSULE_NAME "compile.c compiler unit"
Benjamin Petersonb173f782009-05-05 22:31:58 +0000219
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000220PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000221_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 /* Name mangling: __private becomes _classname__private.
224 This is independent from how the name is used. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200225 PyObject *result;
226 size_t nlen, plen, ipriv;
227 Py_UCS4 maxchar;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200229 PyUnicode_READ_CHAR(ident, 0) != '_' ||
230 PyUnicode_READ_CHAR(ident, 1) != '_') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 Py_INCREF(ident);
232 return ident;
233 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200234 nlen = PyUnicode_GET_LENGTH(ident);
235 plen = PyUnicode_GET_LENGTH(privateobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 /* Don't mangle __id__ or names with dots.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 The only time a name with a dot can occur is when
239 we are compiling an import statement that has a
240 package name.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 TODO(jhylton): Decide whether we want to support
243 mangling of the module name, e.g. __M.X.
244 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200245 if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
246 PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
247 PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 Py_INCREF(ident);
249 return ident; /* Don't mangle __whatever__ */
250 }
251 /* Strip leading underscores from class name */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200252 ipriv = 0;
253 while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
254 ipriv++;
255 if (ipriv == plen) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 Py_INCREF(ident);
257 return ident; /* Don't mangle if class is just underscores */
258 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200259 plen -= ipriv;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000260
Antoine Pitrou55bff892013-04-06 21:21:04 +0200261 if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
262 PyErr_SetString(PyExc_OverflowError,
263 "private identifier too large to be mangled");
264 return NULL;
265 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000266
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200267 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
268 if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
269 maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
270
271 result = PyUnicode_New(1 + nlen + plen, maxchar);
272 if (!result)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200274 /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
275 PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
Victor Stinner6c7a52a2011-09-28 21:39:17 +0200276 if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
277 Py_DECREF(result);
278 return NULL;
279 }
280 if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
281 Py_DECREF(result);
282 return NULL;
283 }
Victor Stinner8f825062012-04-27 13:55:39 +0200284 assert(_PyUnicode_CheckConsistency(result, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200285 return result;
Michael W. Hudson60934622004-08-12 17:56:29 +0000286}
287
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000288static int
289compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 c->c_stack = PyList_New(0);
294 if (!c->c_stack)
295 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000298}
299
300PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200301PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
302 int optimize, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 struct compiler c;
305 PyCodeObject *co = NULL;
306 PyCompilerFlags local_flags;
307 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 if (!__doc__) {
310 __doc__ = PyUnicode_InternFromString("__doc__");
311 if (!__doc__)
312 return NULL;
313 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 if (!compiler_init(&c))
316 return NULL;
Victor Stinner14e461d2013-08-26 22:28:21 +0200317 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 c.c_filename = filename;
319 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200320 c.c_future = PyFuture_FromASTObject(mod, filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 if (c.c_future == NULL)
322 goto finally;
323 if (!flags) {
324 local_flags.cf_flags = 0;
325 flags = &local_flags;
326 }
327 merged = c.c_future->ff_features | flags->cf_flags;
328 c.c_future->ff_features = merged;
329 flags->cf_flags = merged;
330 c.c_flags = flags;
Georg Brandl8334fd92010-12-04 10:26:46 +0000331 c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000333
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +0200334 if (!_PyAST_Optimize(mod, arena, c.c_optimize)) {
INADA Naoki7ea143a2017-12-14 16:47:20 +0900335 goto finally;
336 }
337
Victor Stinner14e461d2013-08-26 22:28:21 +0200338 c.c_st = PySymtable_BuildObject(mod, filename, c.c_future);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 if (c.c_st == NULL) {
340 if (!PyErr_Occurred())
341 PyErr_SetString(PyExc_SystemError, "no symtable");
342 goto finally;
343 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000346
Thomas Wouters1175c432006-02-27 22:49:54 +0000347 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 compiler_free(&c);
349 assert(co || PyErr_Occurred());
350 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000351}
352
353PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200354PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags,
355 int optimize, PyArena *arena)
356{
357 PyObject *filename;
358 PyCodeObject *co;
359 filename = PyUnicode_DecodeFSDefault(filename_str);
360 if (filename == NULL)
361 return NULL;
362 co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
363 Py_DECREF(filename);
364 return co;
365
366}
367
368PyCodeObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000369PyNode_Compile(struct _node *n, const char *filename)
370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 PyCodeObject *co = NULL;
372 mod_ty mod;
373 PyArena *arena = PyArena_New();
374 if (!arena)
375 return NULL;
376 mod = PyAST_FromNode(n, NULL, filename, arena);
377 if (mod)
378 co = PyAST_Compile(mod, filename, NULL, arena);
379 PyArena_Free(arena);
380 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000381}
382
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000383static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000384compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 if (c->c_st)
387 PySymtable_Free(c->c_st);
388 if (c->c_future)
389 PyObject_Free(c->c_future);
Victor Stinner14e461d2013-08-26 22:28:21 +0200390 Py_XDECREF(c->c_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000392}
393
Guido van Rossum79f25d91997-04-29 20:08:16 +0000394static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000395list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 Py_ssize_t i, n;
398 PyObject *v, *k;
399 PyObject *dict = PyDict_New();
400 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 n = PyList_Size(list);
403 for (i = 0; i < n; i++) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100404 v = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 if (!v) {
406 Py_DECREF(dict);
407 return NULL;
408 }
409 k = PyList_GET_ITEM(list, i);
Victor Stinnerefb24132016-01-22 12:33:12 +0100410 k = _PyCode_ConstantKey(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
412 Py_XDECREF(k);
413 Py_DECREF(v);
414 Py_DECREF(dict);
415 return NULL;
416 }
417 Py_DECREF(k);
418 Py_DECREF(v);
419 }
420 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000421}
422
423/* Return new dict containing names from src that match scope(s).
424
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000425src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000427values are integers, starting at offset and increasing by one for
428each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000429*/
430
431static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +0100432dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000433{
Benjamin Peterson51ab2832012-07-18 15:12:47 -0700434 Py_ssize_t i = offset, scope, num_keys, key_i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 PyObject *k, *v, *dest = PyDict_New();
Meador Inge2ca63152012-07-18 14:20:11 -0500436 PyObject *sorted_keys;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 assert(offset >= 0);
439 if (dest == NULL)
440 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000441
Meador Inge2ca63152012-07-18 14:20:11 -0500442 /* Sort the keys so that we have a deterministic order on the indexes
443 saved in the returned dictionary. These indexes are used as indexes
444 into the free and cell var storage. Therefore if they aren't
445 deterministic, then the generated bytecode is not deterministic.
446 */
447 sorted_keys = PyDict_Keys(src);
448 if (sorted_keys == NULL)
449 return NULL;
450 if (PyList_Sort(sorted_keys) != 0) {
451 Py_DECREF(sorted_keys);
452 return NULL;
453 }
Meador Ingef69e24e2012-07-18 16:41:03 -0500454 num_keys = PyList_GET_SIZE(sorted_keys);
Meador Inge2ca63152012-07-18 14:20:11 -0500455
456 for (key_i = 0; key_i < num_keys; key_i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 /* XXX this should probably be a macro in symtable.h */
458 long vi;
Meador Inge2ca63152012-07-18 14:20:11 -0500459 k = PyList_GET_ITEM(sorted_keys, key_i);
460 v = PyDict_GetItem(src, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 assert(PyLong_Check(v));
462 vi = PyLong_AS_LONG(v);
463 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 if (scope == scope_type || vi & flag) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100466 PyObject *tuple, *item = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 if (item == NULL) {
Meador Inge2ca63152012-07-18 14:20:11 -0500468 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 Py_DECREF(dest);
470 return NULL;
471 }
472 i++;
Victor Stinnerefb24132016-01-22 12:33:12 +0100473 tuple = _PyCode_ConstantKey(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
Meador Inge2ca63152012-07-18 14:20:11 -0500475 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 Py_DECREF(item);
477 Py_DECREF(dest);
478 Py_XDECREF(tuple);
479 return NULL;
480 }
481 Py_DECREF(item);
482 Py_DECREF(tuple);
483 }
484 }
Meador Inge2ca63152012-07-18 14:20:11 -0500485 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000487}
488
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000489static void
490compiler_unit_check(struct compiler_unit *u)
491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 basicblock *block;
493 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Benjamin Petersonca470632016-09-06 13:47:26 -0700494 assert((uintptr_t)block != 0xcbcbcbcbU);
495 assert((uintptr_t)block != 0xfbfbfbfbU);
496 assert((uintptr_t)block != 0xdbdbdbdbU);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 if (block->b_instr != NULL) {
498 assert(block->b_ialloc > 0);
499 assert(block->b_iused > 0);
500 assert(block->b_ialloc >= block->b_iused);
501 }
502 else {
503 assert (block->b_iused == 0);
504 assert (block->b_ialloc == 0);
505 }
506 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000507}
508
509static void
510compiler_unit_free(struct compiler_unit *u)
511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 compiler_unit_check(u);
515 b = u->u_blocks;
516 while (b != NULL) {
517 if (b->b_instr)
518 PyObject_Free((void *)b->b_instr);
519 next = b->b_list;
520 PyObject_Free((void *)b);
521 b = next;
522 }
523 Py_CLEAR(u->u_ste);
524 Py_CLEAR(u->u_name);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400525 Py_CLEAR(u->u_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 Py_CLEAR(u->u_consts);
527 Py_CLEAR(u->u_names);
528 Py_CLEAR(u->u_varnames);
529 Py_CLEAR(u->u_freevars);
530 Py_CLEAR(u->u_cellvars);
531 Py_CLEAR(u->u_private);
532 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000533}
534
535static int
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100536compiler_enter_scope(struct compiler *c, identifier name,
537 int scope_type, void *key, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 struct compiler_unit *u;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100540 basicblock *block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
543 struct compiler_unit));
544 if (!u) {
545 PyErr_NoMemory();
546 return 0;
547 }
548 memset(u, 0, sizeof(struct compiler_unit));
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100549 u->u_scope_type = scope_type;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 u->u_argcount = 0;
551 u->u_kwonlyargcount = 0;
552 u->u_ste = PySymtable_Lookup(c->c_st, key);
553 if (!u->u_ste) {
554 compiler_unit_free(u);
555 return 0;
556 }
557 Py_INCREF(name);
558 u->u_name = name;
559 u->u_varnames = list2dict(u->u_ste->ste_varnames);
560 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
561 if (!u->u_varnames || !u->u_cellvars) {
562 compiler_unit_free(u);
563 return 0;
564 }
Benjamin Peterson312595c2013-05-15 15:26:42 -0500565 if (u->u_ste->ste_needs_class_closure) {
Martin Panter7462b6492015-11-02 03:37:02 +0000566 /* Cook up an implicit __class__ cell. */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500567 _Py_IDENTIFIER(__class__);
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300568 PyObject *tuple, *name;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500569 int res;
570 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200571 assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500572 name = _PyUnicode_FromId(&PyId___class__);
573 if (!name) {
574 compiler_unit_free(u);
575 return 0;
576 }
Victor Stinnerefb24132016-01-22 12:33:12 +0100577 tuple = _PyCode_ConstantKey(name);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500578 if (!tuple) {
579 compiler_unit_free(u);
580 return 0;
581 }
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300582 res = PyDict_SetItem(u->u_cellvars, tuple, _PyLong_Zero);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500583 Py_DECREF(tuple);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500584 if (res < 0) {
585 compiler_unit_free(u);
586 return 0;
587 }
588 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200591 PyDict_GET_SIZE(u->u_cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 if (!u->u_freevars) {
593 compiler_unit_free(u);
594 return 0;
595 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 u->u_blocks = NULL;
598 u->u_nfblocks = 0;
599 u->u_firstlineno = lineno;
600 u->u_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000601 u->u_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 u->u_lineno_set = 0;
603 u->u_consts = PyDict_New();
604 if (!u->u_consts) {
605 compiler_unit_free(u);
606 return 0;
607 }
608 u->u_names = PyDict_New();
609 if (!u->u_names) {
610 compiler_unit_free(u);
611 return 0;
612 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 /* Push the old compiler_unit on the stack. */
617 if (c->u) {
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400618 PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
620 Py_XDECREF(capsule);
621 compiler_unit_free(u);
622 return 0;
623 }
624 Py_DECREF(capsule);
625 u->u_private = c->u->u_private;
626 Py_XINCREF(u->u_private);
627 }
628 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 c->c_nestlevel++;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100631
632 block = compiler_new_block(c);
633 if (block == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 return 0;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100635 c->u->u_curblock = block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000636
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400637 if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
638 if (!compiler_set_qualname(c))
639 return 0;
640 }
641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643}
644
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000645static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000646compiler_exit_scope(struct compiler *c)
647{
Victor Stinnerad9a0662013-11-19 22:23:20 +0100648 Py_ssize_t n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 PyObject *capsule;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 c->c_nestlevel--;
652 compiler_unit_free(c->u);
653 /* Restore c->u to the parent unit. */
654 n = PyList_GET_SIZE(c->c_stack) - 1;
655 if (n >= 0) {
656 capsule = PyList_GET_ITEM(c->c_stack, n);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400657 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 assert(c->u);
659 /* we are deleting from a list so this really shouldn't fail */
660 if (PySequence_DelItem(c->c_stack, n) < 0)
661 Py_FatalError("compiler_exit_scope()");
662 compiler_unit_check(c->u);
663 }
664 else
665 c->u = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000666
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000667}
668
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400669static int
670compiler_set_qualname(struct compiler *c)
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100671{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100672 _Py_static_string(dot, ".");
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400673 _Py_static_string(dot_locals, ".<locals>");
674 Py_ssize_t stack_size;
675 struct compiler_unit *u = c->u;
676 PyObject *name, *base, *dot_str, *dot_locals_str;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100677
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400678 base = NULL;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100679 stack_size = PyList_GET_SIZE(c->c_stack);
Benjamin Petersona8a38b82013-10-19 16:14:39 -0400680 assert(stack_size >= 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400681 if (stack_size > 1) {
682 int scope, force_global = 0;
683 struct compiler_unit *parent;
684 PyObject *mangled, *capsule;
685
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400686 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400687 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400688 assert(parent);
689
Yury Selivanov75445082015-05-11 22:57:16 -0400690 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
691 || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
692 || u->u_scope_type == COMPILER_SCOPE_CLASS) {
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400693 assert(u->u_name);
694 mangled = _Py_Mangle(parent->u_private, u->u_name);
695 if (!mangled)
696 return 0;
697 scope = PyST_GetScope(parent->u_ste, mangled);
698 Py_DECREF(mangled);
699 assert(scope != GLOBAL_IMPLICIT);
700 if (scope == GLOBAL_EXPLICIT)
701 force_global = 1;
702 }
703
704 if (!force_global) {
705 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
Yury Selivanov75445082015-05-11 22:57:16 -0400706 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400707 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
708 dot_locals_str = _PyUnicode_FromId(&dot_locals);
709 if (dot_locals_str == NULL)
710 return 0;
711 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
712 if (base == NULL)
713 return 0;
714 }
715 else {
716 Py_INCREF(parent->u_qualname);
717 base = parent->u_qualname;
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400718 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100719 }
720 }
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400721
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400722 if (base != NULL) {
723 dot_str = _PyUnicode_FromId(&dot);
724 if (dot_str == NULL) {
725 Py_DECREF(base);
726 return 0;
727 }
728 name = PyUnicode_Concat(base, dot_str);
729 Py_DECREF(base);
730 if (name == NULL)
731 return 0;
732 PyUnicode_Append(&name, u->u_name);
733 if (name == NULL)
734 return 0;
735 }
736 else {
737 Py_INCREF(u->u_name);
738 name = u->u_name;
739 }
740 u->u_qualname = name;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100741
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400742 return 1;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100743}
744
Eric V. Smith235a6f02015-09-19 14:51:32 -0400745
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000746/* Allocate a new block and return a pointer to it.
747 Returns NULL on error.
748*/
749
750static basicblock *
751compiler_new_block(struct compiler *c)
752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 basicblock *b;
754 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 u = c->u;
757 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
758 if (b == NULL) {
759 PyErr_NoMemory();
760 return NULL;
761 }
762 memset((void *)b, 0, sizeof(basicblock));
763 /* Extend the singly linked list of blocks with new block. */
764 b->b_list = u->u_blocks;
765 u->u_blocks = b;
766 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767}
768
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000769static basicblock *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770compiler_next_block(struct compiler *c)
771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 basicblock *block = compiler_new_block(c);
773 if (block == NULL)
774 return NULL;
775 c->u->u_curblock->b_next = block;
776 c->u->u_curblock = block;
777 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778}
779
780static basicblock *
781compiler_use_next_block(struct compiler *c, basicblock *block)
782{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 assert(block != NULL);
784 c->u->u_curblock->b_next = block;
785 c->u->u_curblock = block;
786 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000787}
788
789/* Returns the offset of the next instruction in the current block's
790 b_instr array. Resizes the b_instr as necessary.
791 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000792*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793
794static int
795compiler_next_instr(struct compiler *c, basicblock *b)
796{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 assert(b != NULL);
798 if (b->b_instr == NULL) {
799 b->b_instr = (struct instr *)PyObject_Malloc(
800 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
801 if (b->b_instr == NULL) {
802 PyErr_NoMemory();
803 return -1;
804 }
805 b->b_ialloc = DEFAULT_BLOCK_SIZE;
806 memset((char *)b->b_instr, 0,
807 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
808 }
809 else if (b->b_iused == b->b_ialloc) {
810 struct instr *tmp;
811 size_t oldsize, newsize;
812 oldsize = b->b_ialloc * sizeof(struct instr);
813 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000814
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -0700815 if (oldsize > (SIZE_MAX >> 1)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 PyErr_NoMemory();
817 return -1;
818 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 if (newsize == 0) {
821 PyErr_NoMemory();
822 return -1;
823 }
824 b->b_ialloc <<= 1;
825 tmp = (struct instr *)PyObject_Realloc(
826 (void *)b->b_instr, newsize);
827 if (tmp == NULL) {
828 PyErr_NoMemory();
829 return -1;
830 }
831 b->b_instr = tmp;
832 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
833 }
834 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835}
836
Christian Heimes2202f872008-02-06 14:31:34 +0000837/* Set the i_lineno member of the instruction at offset off if the
838 line number for the current expression/statement has not
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000839 already been set. If it has been set, the call has no effect.
840
Christian Heimes2202f872008-02-06 14:31:34 +0000841 The line number is reset in the following cases:
842 - when entering a new scope
843 - on each statement
844 - on each expression that start a new line
845 - before the "except" clause
846 - before the "for" and "while" expressions
Thomas Wouters89f507f2006-12-13 04:49:30 +0000847*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000848
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849static void
850compiler_set_lineno(struct compiler *c, int off)
851{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 basicblock *b;
853 if (c->u->u_lineno_set)
854 return;
855 c->u->u_lineno_set = 1;
856 b = c->u->u_curblock;
857 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858}
859
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200860/* Return the stack effect of opcode with argument oparg.
861
862 Some opcodes have different stack effect when jump to the target and
863 when not jump. The 'jump' parameter specifies the case:
864
865 * 0 -- when not jump
866 * 1 -- when jump
867 * -1 -- maximal
868 */
869/* XXX Make the stack effect of WITH_CLEANUP_START and
870 WITH_CLEANUP_FINISH deterministic. */
871static int
872stack_effect(int opcode, int oparg, int jump)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 switch (opcode) {
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200875 /* Stack manipulation */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 case POP_TOP:
877 return -1;
878 case ROT_TWO:
879 case ROT_THREE:
880 return 0;
881 case DUP_TOP:
882 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000883 case DUP_TOP_TWO:
884 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000885
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200886 /* Unary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 case UNARY_POSITIVE:
888 case UNARY_NEGATIVE:
889 case UNARY_NOT:
890 case UNARY_INVERT:
891 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 case SET_ADD:
894 case LIST_APPEND:
895 return -1;
896 case MAP_ADD:
897 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000898
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200899 /* Binary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 case BINARY_POWER:
901 case BINARY_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400902 case BINARY_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 case BINARY_MODULO:
904 case BINARY_ADD:
905 case BINARY_SUBTRACT:
906 case BINARY_SUBSCR:
907 case BINARY_FLOOR_DIVIDE:
908 case BINARY_TRUE_DIVIDE:
909 return -1;
910 case INPLACE_FLOOR_DIVIDE:
911 case INPLACE_TRUE_DIVIDE:
912 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 case INPLACE_ADD:
915 case INPLACE_SUBTRACT:
916 case INPLACE_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400917 case INPLACE_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 case INPLACE_MODULO:
919 return -1;
920 case STORE_SUBSCR:
921 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 case DELETE_SUBSCR:
923 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 case BINARY_LSHIFT:
926 case BINARY_RSHIFT:
927 case BINARY_AND:
928 case BINARY_XOR:
929 case BINARY_OR:
930 return -1;
931 case INPLACE_POWER:
932 return -1;
933 case GET_ITER:
934 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 case PRINT_EXPR:
937 return -1;
938 case LOAD_BUILD_CLASS:
939 return 1;
940 case INPLACE_LSHIFT:
941 case INPLACE_RSHIFT:
942 case INPLACE_AND:
943 case INPLACE_XOR:
944 case INPLACE_OR:
945 return -1;
946 case BREAK_LOOP:
947 return 0;
948 case SETUP_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200949 /* 1 in the normal flow.
950 * Restore the stack position and push 6 values before jumping to
951 * the handler if an exception be raised. */
952 return jump ? 6 : 1;
Yury Selivanov75445082015-05-11 22:57:16 -0400953 case WITH_CLEANUP_START:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200954 return 2; /* or 1, depending on TOS */
Yury Selivanov75445082015-05-11 22:57:16 -0400955 case WITH_CLEANUP_FINISH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200956 /* Pop a variable number of values pushed by WITH_CLEANUP_START
957 * + __exit__ or __aexit__. */
958 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 case RETURN_VALUE:
960 return -1;
961 case IMPORT_STAR:
962 return -1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700963 case SETUP_ANNOTATIONS:
964 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 case YIELD_VALUE:
966 return 0;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500967 case YIELD_FROM:
968 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 case POP_BLOCK:
970 return 0;
971 case POP_EXCEPT:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200972 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 case END_FINALLY:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200974 /* Pop 6 values when an exception was raised. */
975 return -6;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 case STORE_NAME:
978 return -1;
979 case DELETE_NAME:
980 return 0;
981 case UNPACK_SEQUENCE:
982 return oparg-1;
983 case UNPACK_EX:
984 return (oparg&0xFF) + (oparg>>8);
985 case FOR_ITER:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200986 /* -1 at end of iterator, 1 if continue iterating. */
987 return jump > 0 ? -1 : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 case STORE_ATTR:
990 return -2;
991 case DELETE_ATTR:
992 return -1;
993 case STORE_GLOBAL:
994 return -1;
995 case DELETE_GLOBAL:
996 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 case LOAD_CONST:
998 return 1;
999 case LOAD_NAME:
1000 return 1;
1001 case BUILD_TUPLE:
1002 case BUILD_LIST:
1003 case BUILD_SET:
Serhiy Storchakaea525a22016-09-06 22:07:53 +03001004 case BUILD_STRING:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 return 1-oparg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001006 case BUILD_LIST_UNPACK:
1007 case BUILD_TUPLE_UNPACK:
Serhiy Storchaka73442852016-10-02 10:33:46 +03001008 case BUILD_TUPLE_UNPACK_WITH_CALL:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001009 case BUILD_SET_UNPACK:
1010 case BUILD_MAP_UNPACK:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001011 case BUILD_MAP_UNPACK_WITH_CALL:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001012 return 1 - oparg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 case BUILD_MAP:
Benjamin Petersonb6855152015-09-10 21:02:39 -07001014 return 1 - 2*oparg;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001015 case BUILD_CONST_KEY_MAP:
1016 return -oparg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 case LOAD_ATTR:
1018 return 0;
1019 case COMPARE_OP:
1020 return -1;
1021 case IMPORT_NAME:
1022 return -1;
1023 case IMPORT_FROM:
1024 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001025
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001026 /* Jumps */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 case JUMP_FORWARD:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 case JUMP_ABSOLUTE:
1029 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001030
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001031 case JUMP_IF_TRUE_OR_POP:
1032 case JUMP_IF_FALSE_OR_POP:
1033 return jump ? 0 : -1;
1034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 case POP_JUMP_IF_FALSE:
1036 case POP_JUMP_IF_TRUE:
1037 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 case LOAD_GLOBAL:
1040 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 case CONTINUE_LOOP:
1043 return 0;
1044 case SETUP_LOOP:
1045 return 0;
1046 case SETUP_EXCEPT:
1047 case SETUP_FINALLY:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001048 /* 0 in the normal flow.
1049 * Restore the stack position and push 6 values before jumping to
1050 * the handler if an exception be raised. */
1051 return jump ? 6 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 case LOAD_FAST:
1054 return 1;
1055 case STORE_FAST:
1056 return -1;
1057 case DELETE_FAST:
1058 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001059 case STORE_ANNOTATION:
1060 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 case RAISE_VARARGS:
1063 return -oparg;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001064
1065 /* Functions and calls */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 case CALL_FUNCTION:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001067 return -oparg;
Yury Selivanovf2392132016-12-13 19:03:51 -05001068 case CALL_METHOD:
1069 return -oparg-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 case CALL_FUNCTION_KW:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001071 return -oparg-1;
1072 case CALL_FUNCTION_EX:
Matthieu Dartiailh3a9ac822017-02-21 14:25:22 +01001073 return -1 - ((oparg & 0x01) != 0);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001074 case MAKE_FUNCTION:
1075 return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1076 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 case BUILD_SLICE:
1078 if (oparg == 3)
1079 return -2;
1080 else
1081 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001082
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001083 /* Closures */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 case LOAD_CLOSURE:
1085 return 1;
1086 case LOAD_DEREF:
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001087 case LOAD_CLASSDEREF:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 return 1;
1089 case STORE_DEREF:
1090 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001091 case DELETE_DEREF:
1092 return 0;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001093
1094 /* Iterators and generators */
Yury Selivanov75445082015-05-11 22:57:16 -04001095 case GET_AWAITABLE:
1096 return 0;
1097 case SETUP_ASYNC_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001098 /* 0 in the normal flow.
1099 * Restore the stack position to the position before the result
1100 * of __aenter__ and push 6 values before jumping to the handler
1101 * if an exception be raised. */
1102 return jump ? -1 + 6 : 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001103 case BEFORE_ASYNC_WITH:
1104 return 1;
1105 case GET_AITER:
1106 return 0;
1107 case GET_ANEXT:
1108 return 1;
Yury Selivanov5376ba92015-06-22 12:19:30 -04001109 case GET_YIELD_FROM_ITER:
1110 return 0;
Eric V. Smitha78c7952015-11-03 12:45:05 -05001111 case FORMAT_VALUE:
1112 /* If there's a fmt_spec on the stack, we go from 2->1,
1113 else 1->1. */
1114 return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
Yury Selivanovf2392132016-12-13 19:03:51 -05001115 case LOAD_METHOD:
1116 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 default:
Larry Hastings3a907972013-11-23 14:49:22 -08001118 return PY_INVALID_STACK_EFFECT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 }
Larry Hastings3a907972013-11-23 14:49:22 -08001120 return PY_INVALID_STACK_EFFECT; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121}
1122
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001123int
1124PyCompile_OpcodeStackEffect(int opcode, int oparg)
1125{
1126 return stack_effect(opcode, oparg, -1);
1127}
1128
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129/* Add an opcode with no argument.
1130 Returns 0 on failure, 1 on success.
1131*/
1132
1133static int
1134compiler_addop(struct compiler *c, int opcode)
1135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 basicblock *b;
1137 struct instr *i;
1138 int off;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001139 assert(!HAS_ARG(opcode));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 off = compiler_next_instr(c, c->u->u_curblock);
1141 if (off < 0)
1142 return 0;
1143 b = c->u->u_curblock;
1144 i = &b->b_instr[off];
1145 i->i_opcode = opcode;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001146 i->i_oparg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 if (opcode == RETURN_VALUE)
1148 b->b_return = 1;
1149 compiler_set_lineno(c, off);
1150 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151}
1152
Victor Stinnerf8e32212013-11-19 23:56:34 +01001153static Py_ssize_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
1155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 PyObject *t, *v;
1157 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158
Victor Stinnerefb24132016-01-22 12:33:12 +01001159 t = _PyCode_ConstantKey(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 if (t == NULL)
1161 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 v = PyDict_GetItem(dict, t);
1164 if (!v) {
Stefan Krahc0cbed12015-07-27 12:56:49 +02001165 if (PyErr_Occurred()) {
1166 Py_DECREF(t);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 return -1;
Stefan Krahc0cbed12015-07-27 12:56:49 +02001168 }
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001169 arg = PyDict_GET_SIZE(dict);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001170 v = PyLong_FromSsize_t(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 if (!v) {
1172 Py_DECREF(t);
1173 return -1;
1174 }
1175 if (PyDict_SetItem(dict, t, v) < 0) {
1176 Py_DECREF(t);
1177 Py_DECREF(v);
1178 return -1;
1179 }
1180 Py_DECREF(v);
1181 }
1182 else
1183 arg = PyLong_AsLong(v);
1184 Py_DECREF(t);
1185 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001186}
1187
1188static int
1189compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001191{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001192 Py_ssize_t arg = compiler_add_o(c, dict, o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001193 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001194 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 return compiler_addop_i(c, opcode, arg);
1196}
1197
1198static int
1199compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001202 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1204 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001205 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001206 arg = compiler_add_o(c, dict, mangled);
1207 Py_DECREF(mangled);
1208 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001209 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210 return compiler_addop_i(c, opcode, arg);
1211}
1212
1213/* Add an opcode with an integer argument.
1214 Returns 0 on failure, 1 on success.
1215*/
1216
1217static int
Victor Stinnerf8e32212013-11-19 23:56:34 +01001218compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 struct instr *i;
1221 int off;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001222
Victor Stinner2ad474b2016-03-01 23:34:47 +01001223 /* oparg value is unsigned, but a signed C int is usually used to store
1224 it in the C code (like Python/ceval.c).
1225
1226 Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1227
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001228 The argument of a concrete bytecode instruction is limited to 8-bit.
1229 EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1230 assert(HAS_ARG(opcode));
Victor Stinner2ad474b2016-03-01 23:34:47 +01001231 assert(0 <= oparg && oparg <= 2147483647);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 off = compiler_next_instr(c, c->u->u_curblock);
1234 if (off < 0)
1235 return 0;
1236 i = &c->u->u_curblock->b_instr[off];
Victor Stinnerf8e32212013-11-19 23:56:34 +01001237 i->i_opcode = opcode;
1238 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 compiler_set_lineno(c, off);
1240 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241}
1242
1243static int
1244compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1245{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 struct instr *i;
1247 int off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001249 assert(HAS_ARG(opcode));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 assert(b != NULL);
1251 off = compiler_next_instr(c, c->u->u_curblock);
1252 if (off < 0)
1253 return 0;
1254 i = &c->u->u_curblock->b_instr[off];
1255 i->i_opcode = opcode;
1256 i->i_target = b;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 if (absolute)
1258 i->i_jabs = 1;
1259 else
1260 i->i_jrel = 1;
1261 compiler_set_lineno(c, off);
1262 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001263}
1264
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +01001265/* NEXT_BLOCK() creates an implicit jump from the current block
1266 to the new block.
1267
1268 The returns inside this macro make it impossible to decref objects
1269 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001271#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 if (compiler_next_block((C)) == NULL) \
1273 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274}
1275
1276#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 if (!compiler_addop((C), (OP))) \
1278 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001279}
1280
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001281#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 if (!compiler_addop((C), (OP))) { \
1283 compiler_exit_scope(c); \
1284 return 0; \
1285 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001286}
1287
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001288#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1290 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291}
1292
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001293/* Same as ADDOP_O, but steals a reference. */
1294#define ADDOP_N(C, OP, O, TYPE) { \
1295 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1296 Py_DECREF((O)); \
1297 return 0; \
1298 } \
1299 Py_DECREF((O)); \
1300}
1301
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001302#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1304 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001305}
1306
1307#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 if (!compiler_addop_i((C), (OP), (O))) \
1309 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310}
1311
1312#define ADDOP_JABS(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 if (!compiler_addop_j((C), (OP), (O), 1)) \
1314 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001315}
1316
1317#define ADDOP_JREL(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 if (!compiler_addop_j((C), (OP), (O), 0)) \
1319 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320}
1321
1322/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1323 the ASDL name to synthesize the name of the C type and the visit function.
1324*/
1325
1326#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 if (!compiler_visit_ ## TYPE((C), (V))) \
1328 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001329}
1330
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001331#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 if (!compiler_visit_ ## TYPE((C), (V))) { \
1333 compiler_exit_scope(c); \
1334 return 0; \
1335 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001336}
1337
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001338#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 if (!compiler_visit_slice((C), (V), (CTX))) \
1340 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341}
1342
1343#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 int _i; \
1345 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1346 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1347 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1348 if (!compiler_visit_ ## TYPE((C), elt)) \
1349 return 0; \
1350 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001351}
1352
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001353#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 int _i; \
1355 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1356 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1357 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1358 if (!compiler_visit_ ## TYPE((C), elt)) { \
1359 compiler_exit_scope(c); \
1360 return 0; \
1361 } \
1362 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001363}
1364
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365static int
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001366is_const(expr_ty e)
1367{
1368 switch (e->kind) {
1369 case Constant_kind:
1370 case Num_kind:
1371 case Str_kind:
1372 case Bytes_kind:
1373 case Ellipsis_kind:
1374 case NameConstant_kind:
1375 return 1;
1376 default:
1377 return 0;
1378 }
1379}
1380
1381static PyObject *
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02001382get_const_value(expr_ty e)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001383{
1384 switch (e->kind) {
1385 case Constant_kind:
1386 return e->v.Constant.value;
1387 case Num_kind:
1388 return e->v.Num.n;
1389 case Str_kind:
1390 return e->v.Str.s;
1391 case Bytes_kind:
1392 return e->v.Bytes.s;
1393 case Ellipsis_kind:
1394 return Py_Ellipsis;
1395 case NameConstant_kind:
1396 return e->v.NameConstant.value;
1397 default:
Barry Warsawb2e57942017-09-14 18:13:16 -07001398 Py_UNREACHABLE();
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001399 }
1400}
1401
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001402/* Search if variable annotations are present statically in a block. */
1403
1404static int
1405find_ann(asdl_seq *stmts)
1406{
1407 int i, j, res = 0;
1408 stmt_ty st;
1409
1410 for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1411 st = (stmt_ty)asdl_seq_GET(stmts, i);
1412 switch (st->kind) {
1413 case AnnAssign_kind:
1414 return 1;
1415 case For_kind:
1416 res = find_ann(st->v.For.body) ||
1417 find_ann(st->v.For.orelse);
1418 break;
1419 case AsyncFor_kind:
1420 res = find_ann(st->v.AsyncFor.body) ||
1421 find_ann(st->v.AsyncFor.orelse);
1422 break;
1423 case While_kind:
1424 res = find_ann(st->v.While.body) ||
1425 find_ann(st->v.While.orelse);
1426 break;
1427 case If_kind:
1428 res = find_ann(st->v.If.body) ||
1429 find_ann(st->v.If.orelse);
1430 break;
1431 case With_kind:
1432 res = find_ann(st->v.With.body);
1433 break;
1434 case AsyncWith_kind:
1435 res = find_ann(st->v.AsyncWith.body);
1436 break;
1437 case Try_kind:
1438 for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1439 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1440 st->v.Try.handlers, j);
1441 if (find_ann(handler->v.ExceptHandler.body)) {
1442 return 1;
1443 }
1444 }
1445 res = find_ann(st->v.Try.body) ||
1446 find_ann(st->v.Try.finalbody) ||
1447 find_ann(st->v.Try.orelse);
1448 break;
1449 default:
1450 res = 0;
1451 }
1452 if (res) {
1453 break;
1454 }
1455 }
1456 return res;
1457}
1458
1459/* Compile a sequence of statements, checking for a docstring
1460 and for annotations. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461
1462static int
INADA Naokicb41b272017-02-23 00:31:59 +09001463compiler_body(struct compiler *c, asdl_seq *stmts, string docstring)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001464{
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001465 /* Set current line number to the line number of first statement.
1466 This way line number for SETUP_ANNOTATIONS will always
1467 coincide with the line number of first "real" statement in module.
1468 If body is empy, then lineno will be set later in assemble. */
1469 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE &&
1470 !c->u->u_lineno && asdl_seq_LEN(stmts)) {
INADA Naokicb41b272017-02-23 00:31:59 +09001471 stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, 0);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001472 c->u->u_lineno = st->lineno;
1473 }
1474 /* Every annotated class and module should have __annotations__. */
1475 if (find_ann(stmts)) {
1476 ADDOP(c, SETUP_ANNOTATIONS);
1477 }
INADA Naokicb41b272017-02-23 00:31:59 +09001478 /* if not -OO mode, set docstring */
1479 if (c->c_optimize < 2 && docstring) {
1480 ADDOP_O(c, LOAD_CONST, docstring, consts);
1481 ADDOP_NAME(c, STORE_NAME, __doc__, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 }
INADA Naokicb41b272017-02-23 00:31:59 +09001483 VISIT_SEQ(c, stmt, stmts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485}
1486
1487static PyCodeObject *
1488compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001489{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 PyCodeObject *co;
1491 int addNone = 1;
1492 static PyObject *module;
1493 if (!module) {
1494 module = PyUnicode_InternFromString("<module>");
1495 if (!module)
1496 return NULL;
1497 }
1498 /* Use 0 for firstlineno initially, will fixup in assemble(). */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001499 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 return NULL;
1501 switch (mod->kind) {
1502 case Module_kind:
INADA Naokicb41b272017-02-23 00:31:59 +09001503 if (!compiler_body(c, mod->v.Module.body, mod->v.Module.docstring)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 compiler_exit_scope(c);
1505 return 0;
1506 }
1507 break;
1508 case Interactive_kind:
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001509 if (find_ann(mod->v.Interactive.body)) {
1510 ADDOP(c, SETUP_ANNOTATIONS);
1511 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 c->c_interactive = 1;
1513 VISIT_SEQ_IN_SCOPE(c, stmt,
1514 mod->v.Interactive.body);
1515 break;
1516 case Expression_kind:
1517 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1518 addNone = 0;
1519 break;
1520 case Suite_kind:
1521 PyErr_SetString(PyExc_SystemError,
1522 "suite should not be possible");
1523 return 0;
1524 default:
1525 PyErr_Format(PyExc_SystemError,
1526 "module kind %d should not be possible",
1527 mod->kind);
1528 return 0;
1529 }
1530 co = assemble(c, addNone);
1531 compiler_exit_scope(c);
1532 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001533}
1534
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535/* The test for LOCAL must come before the test for FREE in order to
1536 handle classes where name is both local and free. The local var is
1537 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001538*/
1539
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001540static int
1541get_ref_type(struct compiler *c, PyObject *name)
1542{
Victor Stinner0b1bc562013-05-16 22:17:17 +02001543 int scope;
Benjamin Peterson312595c2013-05-15 15:26:42 -05001544 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001545 _PyUnicode_EqualToASCIIString(name, "__class__"))
Benjamin Peterson312595c2013-05-15 15:26:42 -05001546 return CELL;
Victor Stinner0b1bc562013-05-16 22:17:17 +02001547 scope = PyST_GetScope(c->u->u_ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 if (scope == 0) {
1549 char buf[350];
1550 PyOS_snprintf(buf, sizeof(buf),
Victor Stinner14e461d2013-08-26 22:28:21 +02001551 "unknown scope for %.100s in %.100s(%s)\n"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 "symbols: %s\nlocals: %s\nglobals: %s",
Serhiy Storchakadf4518c2014-11-18 23:34:33 +02001553 PyUnicode_AsUTF8(name),
1554 PyUnicode_AsUTF8(c->u->u_name),
1555 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)),
1556 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)),
1557 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)),
1558 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 );
1560 Py_FatalError(buf);
1561 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564}
1565
1566static int
1567compiler_lookup_arg(PyObject *dict, PyObject *name)
1568{
1569 PyObject *k, *v;
Victor Stinnerefb24132016-01-22 12:33:12 +01001570 k = _PyCode_ConstantKey(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001571 if (k == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001572 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001573 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001574 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001575 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001576 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001577 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001578}
1579
1580static int
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001581compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001582{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001583 Py_ssize_t i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001584 if (qualname == NULL)
1585 qualname = co->co_name;
1586
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001587 if (free) {
1588 for (i = 0; i < free; ++i) {
1589 /* Bypass com_addop_varname because it will generate
1590 LOAD_DEREF but LOAD_CLOSURE is needed.
1591 */
1592 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1593 int arg, reftype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001594
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001595 /* Special case: If a class contains a method with a
1596 free variable that has the same name as a method,
1597 the name will be considered free *and* local in the
1598 class. It should be handled by the closure, as
1599 well as by the normal name loookup logic.
1600 */
1601 reftype = get_ref_type(c, name);
1602 if (reftype == CELL)
1603 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1604 else /* (reftype == FREE) */
1605 arg = compiler_lookup_arg(c->u->u_freevars, name);
1606 if (arg == -1) {
1607 fprintf(stderr,
1608 "lookup %s in %s %d %d\n"
1609 "freevars of %s: %s\n",
1610 PyUnicode_AsUTF8(PyObject_Repr(name)),
1611 PyUnicode_AsUTF8(c->u->u_name),
1612 reftype, arg,
1613 PyUnicode_AsUTF8(co->co_name),
1614 PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars)));
1615 Py_FatalError("compiler_make_closure()");
1616 }
1617 ADDOP_I(c, LOAD_CLOSURE, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001619 flags |= 0x08;
1620 ADDOP_I(c, BUILD_TUPLE, free);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001623 ADDOP_O(c, LOAD_CONST, qualname, consts);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001624 ADDOP_I(c, MAKE_FUNCTION, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626}
1627
1628static int
1629compiler_decorators(struct compiler *c, asdl_seq* decos)
1630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 if (!decos)
1634 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1637 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1638 }
1639 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001640}
1641
1642static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001643compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 asdl_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001645{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001646 /* Push a dict of keyword-only default values.
1647
1648 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
1649 */
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001650 int i;
1651 PyObject *keys = NULL;
1652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1654 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1655 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1656 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04001657 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001658 if (!mangled) {
1659 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001661 if (keys == NULL) {
1662 keys = PyList_New(1);
1663 if (keys == NULL) {
1664 Py_DECREF(mangled);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001665 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001666 }
1667 PyList_SET_ITEM(keys, 0, mangled);
1668 }
1669 else {
1670 int res = PyList_Append(keys, mangled);
1671 Py_DECREF(mangled);
1672 if (res == -1) {
1673 goto error;
1674 }
1675 }
1676 if (!compiler_visit_expr(c, default_)) {
1677 goto error;
1678 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 }
1680 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001681 if (keys != NULL) {
1682 Py_ssize_t default_count = PyList_GET_SIZE(keys);
1683 PyObject *keys_tuple = PyList_AsTuple(keys);
1684 Py_DECREF(keys);
1685 if (keys_tuple == NULL) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001686 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001687 }
1688 ADDOP_N(c, LOAD_CONST, keys_tuple, consts);
1689 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001690 assert(default_count > 0);
1691 return 1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001692 }
1693 else {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001694 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001695 }
1696
1697error:
1698 Py_XDECREF(keys);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001699 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001700}
1701
1702static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001703compiler_visit_argannotation(struct compiler *c, identifier id,
1704 expr_ty annotation, PyObject *names)
1705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 if (annotation) {
Victor Stinner065efc32014-02-18 22:07:56 +01001707 PyObject *mangled;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 VISIT(c, expr, annotation);
Victor Stinner065efc32014-02-18 22:07:56 +01001709 mangled = _Py_Mangle(c->u->u_private, id);
Yury Selivanov34ce99f2014-02-18 12:49:41 -05001710 if (!mangled)
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001711 return 0;
Yury Selivanov34ce99f2014-02-18 12:49:41 -05001712 if (PyList_Append(names, mangled) < 0) {
1713 Py_DECREF(mangled);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001714 return 0;
Yury Selivanov34ce99f2014-02-18 12:49:41 -05001715 }
1716 Py_DECREF(mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001718 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001719}
1720
1721static int
1722compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1723 PyObject *names)
1724{
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001725 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 for (i = 0; i < asdl_seq_LEN(args); i++) {
1727 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001728 if (!compiler_visit_argannotation(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 c,
1730 arg->arg,
1731 arg->annotation,
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001732 names))
1733 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001735 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001736}
1737
1738static int
1739compiler_visit_annotations(struct compiler *c, arguments_ty args,
1740 expr_ty returns)
1741{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001742 /* Push arg annotation dict.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001743 The expressions are evaluated out-of-order wrt the source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00001744
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001745 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 */
1747 static identifier return_str;
1748 PyObject *names;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001749 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 names = PyList_New(0);
1751 if (!names)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03001752 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001753
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001754 if (!compiler_visit_argannotations(c, args->args, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001756 if (args->vararg && args->vararg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001757 !compiler_visit_argannotation(c, args->vararg->arg,
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001758 args->vararg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 goto error;
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001760 if (!compiler_visit_argannotations(c, args->kwonlyargs, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001762 if (args->kwarg && args->kwarg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001763 !compiler_visit_argannotation(c, args->kwarg->arg,
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001764 args->kwarg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001767 if (!return_str) {
1768 return_str = PyUnicode_InternFromString("return");
1769 if (!return_str)
1770 goto error;
1771 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001772 if (!compiler_visit_argannotation(c, return_str, returns, names)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 goto error;
1774 }
1775
1776 len = PyList_GET_SIZE(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 if (len) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001778 PyObject *keytuple = PyList_AsTuple(names);
1779 Py_DECREF(names);
1780 if (keytuple == NULL) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001781 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001783 ADDOP_N(c, LOAD_CONST, keytuple, consts);
1784 ADDOP_I(c, BUILD_CONST_KEY_MAP, len);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001785 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001787 else {
1788 Py_DECREF(names);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001789 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001790 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001791
1792error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 Py_DECREF(names);
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03001794 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001795}
1796
1797static int
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001798compiler_visit_defaults(struct compiler *c, arguments_ty args)
1799{
1800 VISIT_SEQ(c, expr, args->defaults);
1801 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
1802 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001803}
1804
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001805static Py_ssize_t
1806compiler_default_arguments(struct compiler *c, arguments_ty args)
1807{
1808 Py_ssize_t funcflags = 0;
1809 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001810 if (!compiler_visit_defaults(c, args))
1811 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001812 funcflags |= 0x01;
1813 }
1814 if (args->kwonlyargs) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001815 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001816 args->kw_defaults);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001817 if (res == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001818 return -1;
1819 }
1820 else if (res > 0) {
1821 funcflags |= 0x02;
1822 }
1823 }
1824 return funcflags;
1825}
1826
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001827static int
Yury Selivanov75445082015-05-11 22:57:16 -04001828compiler_function(struct compiler *c, stmt_ty s, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001829{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 PyCodeObject *co;
INADA Naokicb41b272017-02-23 00:31:59 +09001831 PyObject *qualname, *docstring = Py_None;
Yury Selivanov75445082015-05-11 22:57:16 -04001832 arguments_ty args;
1833 expr_ty returns;
1834 identifier name;
1835 asdl_seq* decos;
1836 asdl_seq *body;
INADA Naokicb41b272017-02-23 00:31:59 +09001837 Py_ssize_t i, funcflags;
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001838 int annotations;
Yury Selivanov75445082015-05-11 22:57:16 -04001839 int scope_type;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001840
Yury Selivanov75445082015-05-11 22:57:16 -04001841 if (is_async) {
1842 assert(s->kind == AsyncFunctionDef_kind);
1843
1844 args = s->v.AsyncFunctionDef.args;
1845 returns = s->v.AsyncFunctionDef.returns;
1846 decos = s->v.AsyncFunctionDef.decorator_list;
1847 name = s->v.AsyncFunctionDef.name;
1848 body = s->v.AsyncFunctionDef.body;
1849
1850 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
1851 } else {
1852 assert(s->kind == FunctionDef_kind);
1853
1854 args = s->v.FunctionDef.args;
1855 returns = s->v.FunctionDef.returns;
1856 decos = s->v.FunctionDef.decorator_list;
1857 name = s->v.FunctionDef.name;
1858 body = s->v.FunctionDef.body;
1859
1860 scope_type = COMPILER_SCOPE_FUNCTION;
1861 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 if (!compiler_decorators(c, decos))
1864 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001865
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001866 funcflags = compiler_default_arguments(c, args);
1867 if (funcflags == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001869 }
1870
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001871 annotations = compiler_visit_annotations(c, args, returns);
1872 if (annotations == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001873 return 0;
1874 }
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001875 else if (annotations > 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001876 funcflags |= 0x04;
1877 }
1878
1879 if (!compiler_enter_scope(c, name, scope_type, (void *)s, s->lineno)) {
1880 return 0;
1881 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001882
INADA Naokicb41b272017-02-23 00:31:59 +09001883 /* if not -OO mode, add docstring */
1884 if (c->c_optimize < 2 && s->v.FunctionDef.docstring)
1885 docstring = s->v.FunctionDef.docstring;
1886 if (compiler_add_o(c, c->u->u_consts, docstring) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 compiler_exit_scope(c);
1888 return 0;
1889 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 c->u->u_argcount = asdl_seq_LEN(args->args);
1892 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 /* if there was a docstring, we need to skip the first statement */
INADA Naokicb41b272017-02-23 00:31:59 +09001894 VISIT_SEQ_IN_SCOPE(c, stmt, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001896 qualname = c->u->u_qualname;
1897 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001899 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001900 Py_XDECREF(qualname);
1901 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001903 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001904
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001905 compiler_make_closure(c, co, funcflags, qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001906 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 /* decorators */
1910 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1911 ADDOP_I(c, CALL_FUNCTION, 1);
1912 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913
Yury Selivanov75445082015-05-11 22:57:16 -04001914 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001915}
1916
1917static int
1918compiler_class(struct compiler *c, stmt_ty s)
1919{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 PyCodeObject *co;
1921 PyObject *str;
1922 int i;
1923 asdl_seq* decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 if (!compiler_decorators(c, decos))
1926 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 /* ultimately generate code for:
1929 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1930 where:
1931 <func> is a function/closure created from the class body;
1932 it has a single argument (__locals__) where the dict
1933 (or MutableSequence) representing the locals is passed
1934 <name> is the class name
1935 <bases> is the positional arguments and *varargs argument
1936 <keywords> is the keyword arguments and **kwds argument
1937 This borrows from compiler_call.
1938 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001941 if (!compiler_enter_scope(c, s->v.ClassDef.name,
1942 COMPILER_SCOPE_CLASS, (void *)s, s->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 return 0;
1944 /* this block represents what we do in the new scope */
1945 {
1946 /* use the class name for name mangling */
1947 Py_INCREF(s->v.ClassDef.name);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001948 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 /* load (global) __name__ ... */
1950 str = PyUnicode_InternFromString("__name__");
1951 if (!str || !compiler_nameop(c, str, Load)) {
1952 Py_XDECREF(str);
1953 compiler_exit_scope(c);
1954 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001955 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 Py_DECREF(str);
1957 /* ... and store it as __module__ */
1958 str = PyUnicode_InternFromString("__module__");
1959 if (!str || !compiler_nameop(c, str, Store)) {
1960 Py_XDECREF(str);
1961 compiler_exit_scope(c);
1962 return 0;
1963 }
1964 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001965 assert(c->u->u_qualname);
1966 ADDOP_O(c, LOAD_CONST, c->u->u_qualname, consts);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001967 str = PyUnicode_InternFromString("__qualname__");
1968 if (!str || !compiler_nameop(c, str, Store)) {
1969 Py_XDECREF(str);
1970 compiler_exit_scope(c);
1971 return 0;
1972 }
1973 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 /* compile the body proper */
INADA Naokicb41b272017-02-23 00:31:59 +09001975 if (!compiler_body(c, s->v.ClassDef.body, s->v.ClassDef.docstring)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 compiler_exit_scope(c);
1977 return 0;
1978 }
Nick Coghlan19d24672016-12-05 16:47:55 +10001979 /* Return __classcell__ if it is referenced, otherwise return None */
Benjamin Peterson312595c2013-05-15 15:26:42 -05001980 if (c->u->u_ste->ste_needs_class_closure) {
Nick Coghlan19d24672016-12-05 16:47:55 +10001981 /* Store __classcell__ into class namespace & return it */
Benjamin Peterson312595c2013-05-15 15:26:42 -05001982 str = PyUnicode_InternFromString("__class__");
1983 if (str == NULL) {
1984 compiler_exit_scope(c);
1985 return 0;
1986 }
1987 i = compiler_lookup_arg(c->u->u_cellvars, str);
1988 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01001989 if (i < 0) {
1990 compiler_exit_scope(c);
1991 return 0;
1992 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05001993 assert(i == 0);
Nick Coghlan944368e2016-09-11 14:45:49 +10001994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 ADDOP_I(c, LOAD_CLOSURE, i);
Nick Coghlan19d24672016-12-05 16:47:55 +10001996 ADDOP(c, DUP_TOP);
Nick Coghlan944368e2016-09-11 14:45:49 +10001997 str = PyUnicode_InternFromString("__classcell__");
1998 if (!str || !compiler_nameop(c, str, Store)) {
1999 Py_XDECREF(str);
2000 compiler_exit_scope(c);
2001 return 0;
2002 }
2003 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002005 else {
Nick Coghlan19d24672016-12-05 16:47:55 +10002006 /* No methods referenced __class__, so just return None */
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002007 assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
Nick Coghlan19d24672016-12-05 16:47:55 +10002008 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Benjamin Peterson312595c2013-05-15 15:26:42 -05002009 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002010 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 /* create the code object */
2012 co = assemble(c, 1);
2013 }
2014 /* leave the new scope */
2015 compiler_exit_scope(c);
2016 if (co == NULL)
2017 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 /* 2. load the 'build_class' function */
2020 ADDOP(c, LOAD_BUILD_CLASS);
2021
2022 /* 3. load a function (or closure) made from the code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002023 compiler_make_closure(c, co, 0, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 Py_DECREF(co);
2025
2026 /* 4. load class name */
2027 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
2028
2029 /* 5. generate the rest of the code for the call */
2030 if (!compiler_call_helper(c, 2,
2031 s->v.ClassDef.bases,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002032 s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 return 0;
2034
2035 /* 6. apply decorators */
2036 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2037 ADDOP_I(c, CALL_FUNCTION, 1);
2038 }
2039
2040 /* 7. store into <name> */
2041 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2042 return 0;
2043 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002044}
2045
2046static int
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002047cmpop(cmpop_ty op)
2048{
2049 switch (op) {
2050 case Eq:
2051 return PyCmp_EQ;
2052 case NotEq:
2053 return PyCmp_NE;
2054 case Lt:
2055 return PyCmp_LT;
2056 case LtE:
2057 return PyCmp_LE;
2058 case Gt:
2059 return PyCmp_GT;
2060 case GtE:
2061 return PyCmp_GE;
2062 case Is:
2063 return PyCmp_IS;
2064 case IsNot:
2065 return PyCmp_IS_NOT;
2066 case In:
2067 return PyCmp_IN;
2068 case NotIn:
2069 return PyCmp_NOT_IN;
2070 default:
2071 return PyCmp_BAD;
2072 }
2073}
2074
2075static int
2076compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2077{
2078 switch (e->kind) {
2079 case UnaryOp_kind:
2080 if (e->v.UnaryOp.op == Not)
2081 return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2082 /* fallback to general implementation */
2083 break;
2084 case BoolOp_kind: {
2085 asdl_seq *s = e->v.BoolOp.values;
2086 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2087 assert(n >= 0);
2088 int cond2 = e->v.BoolOp.op == Or;
2089 basicblock *next2 = next;
2090 if (!cond2 != !cond) {
2091 next2 = compiler_new_block(c);
2092 if (next2 == NULL)
2093 return 0;
2094 }
2095 for (i = 0; i < n; ++i) {
2096 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2097 return 0;
2098 }
2099 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2100 return 0;
2101 if (next2 != next)
2102 compiler_use_next_block(c, next2);
2103 return 1;
2104 }
2105 case IfExp_kind: {
2106 basicblock *end, *next2;
2107 end = compiler_new_block(c);
2108 if (end == NULL)
2109 return 0;
2110 next2 = compiler_new_block(c);
2111 if (next2 == NULL)
2112 return 0;
2113 if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2114 return 0;
2115 if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2116 return 0;
2117 ADDOP_JREL(c, JUMP_FORWARD, end);
2118 compiler_use_next_block(c, next2);
2119 if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2120 return 0;
2121 compiler_use_next_block(c, end);
2122 return 1;
2123 }
2124 case Compare_kind: {
2125 Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2126 if (n > 0) {
2127 basicblock *cleanup = compiler_new_block(c);
2128 if (cleanup == NULL)
2129 return 0;
2130 VISIT(c, expr, e->v.Compare.left);
2131 for (i = 0; i < n; i++) {
2132 VISIT(c, expr,
2133 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2134 ADDOP(c, DUP_TOP);
2135 ADDOP(c, ROT_THREE);
2136 ADDOP_I(c, COMPARE_OP,
2137 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, i))));
2138 ADDOP_JABS(c, POP_JUMP_IF_FALSE, cleanup);
2139 NEXT_BLOCK(c);
2140 }
2141 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
2142 ADDOP_I(c, COMPARE_OP,
2143 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n))));
2144 ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
2145 basicblock *end = compiler_new_block(c);
2146 if (end == NULL)
2147 return 0;
2148 ADDOP_JREL(c, JUMP_FORWARD, end);
2149 compiler_use_next_block(c, cleanup);
2150 ADDOP(c, POP_TOP);
2151 if (!cond) {
2152 ADDOP_JREL(c, JUMP_FORWARD, next);
2153 }
2154 compiler_use_next_block(c, end);
2155 return 1;
2156 }
2157 /* fallback to general implementation */
2158 break;
2159 }
2160 default:
2161 /* fallback to general implementation */
2162 break;
2163 }
2164
2165 /* general implementation */
2166 VISIT(c, expr, e);
2167 ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
2168 return 1;
2169}
2170
2171static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002172compiler_ifexp(struct compiler *c, expr_ty e)
2173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002174 basicblock *end, *next;
2175
2176 assert(e->kind == IfExp_kind);
2177 end = compiler_new_block(c);
2178 if (end == NULL)
2179 return 0;
2180 next = compiler_new_block(c);
2181 if (next == NULL)
2182 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002183 if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2184 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 VISIT(c, expr, e->v.IfExp.body);
2186 ADDOP_JREL(c, JUMP_FORWARD, end);
2187 compiler_use_next_block(c, next);
2188 VISIT(c, expr, e->v.IfExp.orelse);
2189 compiler_use_next_block(c, end);
2190 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002191}
2192
2193static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002194compiler_lambda(struct compiler *c, expr_ty e)
2195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002197 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 static identifier name;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002199 Py_ssize_t funcflags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 arguments_ty args = e->v.Lambda.args;
2201 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002203 if (!name) {
2204 name = PyUnicode_InternFromString("<lambda>");
2205 if (!name)
2206 return 0;
2207 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002208
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002209 funcflags = compiler_default_arguments(c, args);
2210 if (funcflags == -1) {
2211 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002213
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002214 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002215 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 /* Make None the first constant, so the lambda can't have a
2219 docstring. */
2220 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0)
2221 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 c->u->u_argcount = asdl_seq_LEN(args->args);
2224 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2225 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2226 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002227 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 }
2229 else {
2230 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002231 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002233 qualname = c->u->u_qualname;
2234 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002236 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002238
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002239 compiler_make_closure(c, co, funcflags, qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002240 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002241 Py_DECREF(co);
2242
2243 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002244}
2245
2246static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002247compiler_if(struct compiler *c, stmt_ty s)
2248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 basicblock *end, *next;
2250 int constant;
2251 assert(s->kind == If_kind);
2252 end = compiler_new_block(c);
2253 if (end == NULL)
2254 return 0;
2255
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02002256 constant = expr_constant(s->v.If.test);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 /* constant = 0: "if 0"
2258 * constant = 1: "if 1", "if 2", ...
2259 * constant = -1: rest */
2260 if (constant == 0) {
2261 if (s->v.If.orelse)
2262 VISIT_SEQ(c, stmt, s->v.If.orelse);
2263 } else if (constant == 1) {
2264 VISIT_SEQ(c, stmt, s->v.If.body);
2265 } else {
Antoine Pitroue7811fc2014-09-18 03:06:50 +02002266 if (asdl_seq_LEN(s->v.If.orelse)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002267 next = compiler_new_block(c);
2268 if (next == NULL)
2269 return 0;
2270 }
2271 else
2272 next = end;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002273 if (!compiler_jump_if(c, s->v.If.test, next, 0))
2274 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 VISIT_SEQ(c, stmt, s->v.If.body);
Antoine Pitroue7811fc2014-09-18 03:06:50 +02002276 if (asdl_seq_LEN(s->v.If.orelse)) {
2277 ADDOP_JREL(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 compiler_use_next_block(c, next);
2279 VISIT_SEQ(c, stmt, s->v.If.orelse);
2280 }
2281 }
2282 compiler_use_next_block(c, end);
2283 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002284}
2285
2286static int
2287compiler_for(struct compiler *c, stmt_ty s)
2288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 basicblock *start, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 start = compiler_new_block(c);
2292 cleanup = compiler_new_block(c);
2293 end = compiler_new_block(c);
2294 if (start == NULL || end == NULL || cleanup == NULL)
2295 return 0;
2296 ADDOP_JREL(c, SETUP_LOOP, end);
2297 if (!compiler_push_fblock(c, LOOP, start))
2298 return 0;
2299 VISIT(c, expr, s->v.For.iter);
2300 ADDOP(c, GET_ITER);
2301 compiler_use_next_block(c, start);
2302 ADDOP_JREL(c, FOR_ITER, cleanup);
2303 VISIT(c, expr, s->v.For.target);
2304 VISIT_SEQ(c, stmt, s->v.For.body);
2305 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2306 compiler_use_next_block(c, cleanup);
2307 ADDOP(c, POP_BLOCK);
2308 compiler_pop_fblock(c, LOOP, start);
2309 VISIT_SEQ(c, stmt, s->v.For.orelse);
2310 compiler_use_next_block(c, end);
2311 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002312}
2313
Yury Selivanov75445082015-05-11 22:57:16 -04002314
2315static int
2316compiler_async_for(struct compiler *c, stmt_ty s)
2317{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002318 _Py_IDENTIFIER(StopAsyncIteration);
2319
Yury Selivanov75445082015-05-11 22:57:16 -04002320 basicblock *try, *except, *end, *after_try, *try_cleanup,
2321 *after_loop, *after_loop_else;
2322
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002323 PyObject *stop_aiter_error = _PyUnicode_FromId(&PyId_StopAsyncIteration);
2324 if (stop_aiter_error == NULL) {
2325 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04002326 }
2327
2328 try = compiler_new_block(c);
2329 except = compiler_new_block(c);
2330 end = compiler_new_block(c);
2331 after_try = compiler_new_block(c);
2332 try_cleanup = compiler_new_block(c);
2333 after_loop = compiler_new_block(c);
2334 after_loop_else = compiler_new_block(c);
2335
2336 if (try == NULL || except == NULL || end == NULL
2337 || after_try == NULL || try_cleanup == NULL)
2338 return 0;
2339
2340 ADDOP_JREL(c, SETUP_LOOP, after_loop);
2341 if (!compiler_push_fblock(c, LOOP, try))
2342 return 0;
2343
2344 VISIT(c, expr, s->v.AsyncFor.iter);
2345 ADDOP(c, GET_AITER);
Yury Selivanov75445082015-05-11 22:57:16 -04002346
2347 compiler_use_next_block(c, try);
2348
2349
2350 ADDOP_JREL(c, SETUP_EXCEPT, except);
2351 if (!compiler_push_fblock(c, EXCEPT, try))
2352 return 0;
2353
2354 ADDOP(c, GET_ANEXT);
2355 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2356 ADDOP(c, YIELD_FROM);
2357 VISIT(c, expr, s->v.AsyncFor.target);
2358 ADDOP(c, POP_BLOCK);
2359 compiler_pop_fblock(c, EXCEPT, try);
2360 ADDOP_JREL(c, JUMP_FORWARD, after_try);
2361
2362
2363 compiler_use_next_block(c, except);
2364 ADDOP(c, DUP_TOP);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002365 ADDOP_O(c, LOAD_GLOBAL, stop_aiter_error, names);
Yury Selivanov75445082015-05-11 22:57:16 -04002366 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2367 ADDOP_JABS(c, POP_JUMP_IF_FALSE, try_cleanup);
2368
2369 ADDOP(c, POP_TOP);
2370 ADDOP(c, POP_TOP);
2371 ADDOP(c, POP_TOP);
2372 ADDOP(c, POP_EXCEPT); /* for SETUP_EXCEPT */
Serhiy Storchakad4864c62018-01-09 21:54:52 +02002373 ADDOP(c, POP_TOP); /* for correct calculation of stack effect */
Yury Selivanov75445082015-05-11 22:57:16 -04002374 ADDOP(c, POP_BLOCK); /* for SETUP_LOOP */
2375 ADDOP_JABS(c, JUMP_ABSOLUTE, after_loop_else);
2376
2377
2378 compiler_use_next_block(c, try_cleanup);
2379 ADDOP(c, END_FINALLY);
2380
2381 compiler_use_next_block(c, after_try);
2382 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
2383 ADDOP_JABS(c, JUMP_ABSOLUTE, try);
2384
2385 ADDOP(c, POP_BLOCK); /* for SETUP_LOOP */
2386 compiler_pop_fblock(c, LOOP, try);
2387
2388 compiler_use_next_block(c, after_loop);
2389 ADDOP_JABS(c, JUMP_ABSOLUTE, end);
2390
2391 compiler_use_next_block(c, after_loop_else);
2392 VISIT_SEQ(c, stmt, s->v.For.orelse);
2393
2394 compiler_use_next_block(c, end);
2395
2396 return 1;
2397}
2398
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002399static int
2400compiler_while(struct compiler *c, stmt_ty s)
2401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 basicblock *loop, *orelse, *end, *anchor = NULL;
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02002403 int constant = expr_constant(s->v.While.test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 if (constant == 0) {
2406 if (s->v.While.orelse)
2407 VISIT_SEQ(c, stmt, s->v.While.orelse);
2408 return 1;
2409 }
2410 loop = compiler_new_block(c);
2411 end = compiler_new_block(c);
2412 if (constant == -1) {
2413 anchor = compiler_new_block(c);
2414 if (anchor == NULL)
2415 return 0;
2416 }
2417 if (loop == NULL || end == NULL)
2418 return 0;
2419 if (s->v.While.orelse) {
2420 orelse = compiler_new_block(c);
2421 if (orelse == NULL)
2422 return 0;
2423 }
2424 else
2425 orelse = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002427 ADDOP_JREL(c, SETUP_LOOP, end);
2428 compiler_use_next_block(c, loop);
2429 if (!compiler_push_fblock(c, LOOP, loop))
2430 return 0;
2431 if (constant == -1) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002432 if (!compiler_jump_if(c, s->v.While.test, anchor, 0))
2433 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 }
2435 VISIT_SEQ(c, stmt, s->v.While.body);
2436 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002438 /* XXX should the two POP instructions be in a separate block
2439 if there is no else clause ?
2440 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002441
Benjamin Peterson3cda0ed2014-12-13 16:06:19 -05002442 if (constant == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 compiler_use_next_block(c, anchor);
Benjamin Peterson3cda0ed2014-12-13 16:06:19 -05002444 ADDOP(c, POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 compiler_pop_fblock(c, LOOP, loop);
2446 if (orelse != NULL) /* what if orelse is just pass? */
2447 VISIT_SEQ(c, stmt, s->v.While.orelse);
2448 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002450 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002451}
2452
2453static int
2454compiler_continue(struct compiler *c)
2455{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002456 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
2457 static const char IN_FINALLY_ERROR_MSG[] =
2458 "'continue' not supported inside 'finally' clause";
2459 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 if (!c->u->u_nfblocks)
2462 return compiler_error(c, LOOP_ERROR_MSG);
2463 i = c->u->u_nfblocks - 1;
2464 switch (c->u->u_fblock[i].fb_type) {
2465 case LOOP:
2466 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
2467 break;
2468 case EXCEPT:
2469 case FINALLY_TRY:
2470 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
2471 /* Prevent continue anywhere under a finally
2472 even if hidden in a sub-try or except. */
2473 if (c->u->u_fblock[i].fb_type == FINALLY_END)
2474 return compiler_error(c, IN_FINALLY_ERROR_MSG);
2475 }
2476 if (i == -1)
2477 return compiler_error(c, LOOP_ERROR_MSG);
2478 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
2479 break;
2480 case FINALLY_END:
2481 return compiler_error(c, IN_FINALLY_ERROR_MSG);
2482 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485}
2486
2487/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488
2489 SETUP_FINALLY L
2490 <code for body>
2491 POP_BLOCK
2492 LOAD_CONST <None>
2493 L: <code for finalbody>
2494 END_FINALLY
2495
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002496 The special instructions use the block stack. Each block
2497 stack entry contains the instruction that created it (here
2498 SETUP_FINALLY), the level of the value stack at the time the
2499 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002501 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502 Pushes the current value stack level and the label
2503 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002504 POP_BLOCK:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 Pops en entry from the block stack, and pops the value
2506 stack until its level is the same as indicated on the
2507 block stack. (The label is ignored.)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002508 END_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 Pops a variable number of entries from the *value* stack
2510 and re-raises the exception they specify. The number of
2511 entries popped depends on the (pseudo) exception type.
2512
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002513 The block stack is unwound when an exception is raised:
2514 when a SETUP_FINALLY entry is found, the exception is pushed
2515 onto the value stack (and the exception condition is cleared),
2516 and the interpreter jumps to the label gotten from the block
2517 stack.
2518*/
2519
2520static int
2521compiler_try_finally(struct compiler *c, stmt_ty s)
2522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 basicblock *body, *end;
2524 body = compiler_new_block(c);
2525 end = compiler_new_block(c);
2526 if (body == NULL || end == NULL)
2527 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 ADDOP_JREL(c, SETUP_FINALLY, end);
2530 compiler_use_next_block(c, body);
2531 if (!compiler_push_fblock(c, FINALLY_TRY, body))
2532 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002533 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
2534 if (!compiler_try_except(c, s))
2535 return 0;
2536 }
2537 else {
2538 VISIT_SEQ(c, stmt, s->v.Try.body);
2539 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 ADDOP(c, POP_BLOCK);
2541 compiler_pop_fblock(c, FINALLY_TRY, body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2544 compiler_use_next_block(c, end);
2545 if (!compiler_push_fblock(c, FINALLY_END, end))
2546 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002547 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002548 ADDOP(c, END_FINALLY);
2549 compiler_pop_fblock(c, FINALLY_END, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002551 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002552}
2553
2554/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002555 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556 (The contents of the value stack is shown in [], with the top
2557 at the right; 'tb' is trace-back info, 'val' the exception's
2558 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002559
2560 Value stack Label Instruction Argument
2561 [] SETUP_EXCEPT L1
2562 [] <code for S>
2563 [] POP_BLOCK
2564 [] JUMP_FORWARD L0
2565
2566 [tb, val, exc] L1: DUP )
2567 [tb, val, exc, exc] <evaluate E1> )
2568 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2569 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
2570 [tb, val, exc] POP
2571 [tb, val] <assign to V1> (or POP if no V1)
2572 [tb] POP
2573 [] <code for S1>
2574 JUMP_FORWARD L0
2575
2576 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002577 .............................etc.......................
2578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002579 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
2580
2581 [] L0: <next statement>
2582
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002583 Of course, parts are not generated if Vi or Ei is not present.
2584*/
2585static int
2586compiler_try_except(struct compiler *c, stmt_ty s)
2587{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002588 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01002589 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002591 body = compiler_new_block(c);
2592 except = compiler_new_block(c);
2593 orelse = compiler_new_block(c);
2594 end = compiler_new_block(c);
2595 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
2596 return 0;
2597 ADDOP_JREL(c, SETUP_EXCEPT, except);
2598 compiler_use_next_block(c, body);
2599 if (!compiler_push_fblock(c, EXCEPT, body))
2600 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002601 VISIT_SEQ(c, stmt, s->v.Try.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002602 ADDOP(c, POP_BLOCK);
2603 compiler_pop_fblock(c, EXCEPT, body);
2604 ADDOP_JREL(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002605 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002606 compiler_use_next_block(c, except);
2607 for (i = 0; i < n; i++) {
2608 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002609 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002610 if (!handler->v.ExceptHandler.type && i < n-1)
2611 return compiler_error(c, "default 'except:' must be last");
2612 c->u->u_lineno_set = 0;
2613 c->u->u_lineno = handler->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00002614 c->u->u_col_offset = handler->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002615 except = compiler_new_block(c);
2616 if (except == NULL)
2617 return 0;
2618 if (handler->v.ExceptHandler.type) {
2619 ADDOP(c, DUP_TOP);
2620 VISIT(c, expr, handler->v.ExceptHandler.type);
2621 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2622 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
2623 }
2624 ADDOP(c, POP_TOP);
2625 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002626 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00002627
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002628 cleanup_end = compiler_new_block(c);
2629 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05002630 if (!(cleanup_end || cleanup_body))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002631 return 0;
Guido van Rossumb940e112007-01-10 16:19:56 +00002632
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002633 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
2634 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002635
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002636 /*
2637 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03002638 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002639 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03002640 try:
2641 # body
2642 finally:
2643 name = None
2644 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002645 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002647 /* second try: */
2648 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2649 compiler_use_next_block(c, cleanup_body);
2650 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2651 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002652
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002653 /* second # body */
2654 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2655 ADDOP(c, POP_BLOCK);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002656 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002657
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002658 /* finally: */
2659 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2660 compiler_use_next_block(c, cleanup_end);
2661 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
2662 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002664 /* name = None */
2665 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2666 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002667
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002668 /* del name */
2669 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002670
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002671 ADDOP(c, END_FINALLY);
Serhiy Storchakad4864c62018-01-09 21:54:52 +02002672 ADDOP(c, POP_EXCEPT);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002673 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 }
2675 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002676 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002678 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05002679 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002680 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002681
Guido van Rossumb940e112007-01-10 16:19:56 +00002682 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002683 ADDOP(c, POP_TOP);
2684 compiler_use_next_block(c, cleanup_body);
2685 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2686 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002687 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002688 ADDOP(c, POP_EXCEPT);
2689 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002690 }
2691 ADDOP_JREL(c, JUMP_FORWARD, end);
2692 compiler_use_next_block(c, except);
2693 }
2694 ADDOP(c, END_FINALLY);
2695 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002696 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002697 compiler_use_next_block(c, end);
2698 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002699}
2700
2701static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002702compiler_try(struct compiler *c, stmt_ty s) {
2703 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
2704 return compiler_try_finally(c, s);
2705 else
2706 return compiler_try_except(c, s);
2707}
2708
2709
2710static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002711compiler_import_as(struct compiler *c, identifier name, identifier asname)
2712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002713 /* The IMPORT_NAME opcode was already generated. This function
2714 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 If there is a dot in name, we need to split it and emit a
Serhiy Storchakaf93234b2017-05-09 22:31:05 +03002717 IMPORT_FROM for each name.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002718 */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03002719 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
2720 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002721 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03002722 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002723 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002724 /* Consume the base module name to get the first attribute */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03002725 while (1) {
2726 Py_ssize_t pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002727 PyObject *attr;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03002728 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002729 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03002730 return 0;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03002731 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002732 if (!attr)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03002733 return 0;
Serhiy Storchakaf93234b2017-05-09 22:31:05 +03002734 ADDOP_O(c, IMPORT_FROM, attr, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002735 Py_DECREF(attr);
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03002736 if (dot == -1) {
2737 break;
2738 }
2739 ADDOP(c, ROT_TWO);
2740 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002741 }
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03002742 if (!compiler_nameop(c, asname, Store)) {
2743 return 0;
2744 }
2745 ADDOP(c, POP_TOP);
2746 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 }
2748 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002749}
2750
2751static int
2752compiler_import(struct compiler *c, stmt_ty s)
2753{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 /* The Import node stores a module name like a.b.c as a single
2755 string. This is convenient for all cases except
2756 import a.b.c as d
2757 where we need to parse that string to extract the individual
2758 module names.
2759 XXX Perhaps change the representation to make this case simpler?
2760 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01002761 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 for (i = 0; i < n; i++) {
2764 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
2765 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002766
Serhiy Storchakaba85d692017-03-30 09:09:41 +03002767 ADDOP_O(c, LOAD_CONST, _PyLong_Zero, consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002768 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2769 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002771 if (alias->asname) {
2772 r = compiler_import_as(c, alias->name, alias->asname);
2773 if (!r)
2774 return r;
2775 }
2776 else {
2777 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002778 Py_ssize_t dot = PyUnicode_FindChar(
2779 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02002780 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002781 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02002782 if (tmp == NULL)
2783 return 0;
2784 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002786 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002787 Py_DECREF(tmp);
2788 }
2789 if (!r)
2790 return r;
2791 }
2792 }
2793 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002794}
2795
2796static int
2797compiler_from_import(struct compiler *c, stmt_ty s)
2798{
Victor Stinnerad9a0662013-11-19 22:23:20 +01002799 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 PyObject *names = PyTuple_New(n);
2802 PyObject *level;
2803 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00002804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 if (!empty_string) {
2806 empty_string = PyUnicode_FromString("");
2807 if (!empty_string)
2808 return 0;
2809 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 if (!names)
2812 return 0;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 level = PyLong_FromLong(s->v.ImportFrom.level);
2815 if (!level) {
2816 Py_DECREF(names);
2817 return 0;
2818 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002820 /* build up the names */
2821 for (i = 0; i < n; i++) {
2822 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2823 Py_INCREF(alias->name);
2824 PyTuple_SET_ITEM(names, i, alias->name);
2825 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02002828 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 Py_DECREF(level);
2830 Py_DECREF(names);
2831 return compiler_error(c, "from __future__ imports must occur "
2832 "at the beginning of the file");
2833 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002835 ADDOP_O(c, LOAD_CONST, level, consts);
2836 Py_DECREF(level);
2837 ADDOP_O(c, LOAD_CONST, names, consts);
2838 Py_DECREF(names);
2839 if (s->v.ImportFrom.module) {
2840 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2841 }
2842 else {
2843 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
2844 }
2845 for (i = 0; i < n; i++) {
2846 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2847 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002848
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002849 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002850 assert(n == 1);
2851 ADDOP(c, IMPORT_STAR);
2852 return 1;
2853 }
2854
2855 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2856 store_name = alias->name;
2857 if (alias->asname)
2858 store_name = alias->asname;
2859
2860 if (!compiler_nameop(c, store_name, Store)) {
2861 Py_DECREF(names);
2862 return 0;
2863 }
2864 }
2865 /* remove imported module */
2866 ADDOP(c, POP_TOP);
2867 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002868}
2869
2870static int
2871compiler_assert(struct compiler *c, stmt_ty s)
2872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 static PyObject *assertion_error = NULL;
2874 basicblock *end;
Victor Stinner14e461d2013-08-26 22:28:21 +02002875 PyObject* msg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002876
Georg Brandl8334fd92010-12-04 10:26:46 +00002877 if (c->c_optimize)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 return 1;
2879 if (assertion_error == NULL) {
2880 assertion_error = PyUnicode_InternFromString("AssertionError");
2881 if (assertion_error == NULL)
2882 return 0;
2883 }
2884 if (s->v.Assert.test->kind == Tuple_kind &&
2885 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
Victor Stinner14e461d2013-08-26 22:28:21 +02002886 msg = PyUnicode_FromString("assertion is always true, "
2887 "perhaps remove parentheses?");
2888 if (msg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002889 return 0;
Victor Stinner14e461d2013-08-26 22:28:21 +02002890 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
2891 c->c_filename, c->u->u_lineno,
2892 NULL, NULL) == -1) {
2893 Py_DECREF(msg);
2894 return 0;
2895 }
2896 Py_DECREF(msg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002898 end = compiler_new_block(c);
2899 if (end == NULL)
2900 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002901 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
2902 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2904 if (s->v.Assert.msg) {
2905 VISIT(c, expr, s->v.Assert.msg);
2906 ADDOP_I(c, CALL_FUNCTION, 1);
2907 }
2908 ADDOP_I(c, RAISE_VARARGS, 1);
2909 compiler_use_next_block(c, end);
2910 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002911}
2912
2913static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01002914compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
2915{
2916 if (c->c_interactive && c->c_nestlevel <= 1) {
2917 VISIT(c, expr, value);
2918 ADDOP(c, PRINT_EXPR);
2919 return 1;
2920 }
2921
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002922 if (is_const(value)) {
Victor Stinner15a30952016-02-08 22:45:06 +01002923 /* ignore constant statement */
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01002924 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01002925 }
2926
2927 VISIT(c, expr, value);
2928 ADDOP(c, POP_TOP);
2929 return 1;
2930}
2931
2932static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002933compiler_visit_stmt(struct compiler *c, stmt_ty s)
2934{
Victor Stinnerad9a0662013-11-19 22:23:20 +01002935 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002937 /* Always assign a lineno to the next instruction for a stmt. */
2938 c->u->u_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00002939 c->u->u_col_offset = s->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002940 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 switch (s->kind) {
2943 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04002944 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002945 case ClassDef_kind:
2946 return compiler_class(c, s);
2947 case Return_kind:
2948 if (c->u->u_ste->ste_type != FunctionBlock)
2949 return compiler_error(c, "'return' outside function");
2950 if (s->v.Return.value) {
Yury Selivanoveb636452016-09-08 22:01:51 -07002951 if (c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2952 return compiler_error(
2953 c, "'return' with value in async generator");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002954 VISIT(c, expr, s->v.Return.value);
2955 }
2956 else
2957 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2958 ADDOP(c, RETURN_VALUE);
2959 break;
2960 case Delete_kind:
2961 VISIT_SEQ(c, expr, s->v.Delete.targets)
2962 break;
2963 case Assign_kind:
2964 n = asdl_seq_LEN(s->v.Assign.targets);
2965 VISIT(c, expr, s->v.Assign.value);
2966 for (i = 0; i < n; i++) {
2967 if (i < n - 1)
2968 ADDOP(c, DUP_TOP);
2969 VISIT(c, expr,
2970 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2971 }
2972 break;
2973 case AugAssign_kind:
2974 return compiler_augassign(c, s);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002975 case AnnAssign_kind:
2976 return compiler_annassign(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 case For_kind:
2978 return compiler_for(c, s);
2979 case While_kind:
2980 return compiler_while(c, s);
2981 case If_kind:
2982 return compiler_if(c, s);
2983 case Raise_kind:
2984 n = 0;
2985 if (s->v.Raise.exc) {
2986 VISIT(c, expr, s->v.Raise.exc);
2987 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01002988 if (s->v.Raise.cause) {
2989 VISIT(c, expr, s->v.Raise.cause);
2990 n++;
2991 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01002993 ADDOP_I(c, RAISE_VARARGS, (int)n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002995 case Try_kind:
2996 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002997 case Assert_kind:
2998 return compiler_assert(c, s);
2999 case Import_kind:
3000 return compiler_import(c, s);
3001 case ImportFrom_kind:
3002 return compiler_from_import(c, s);
3003 case Global_kind:
3004 case Nonlocal_kind:
3005 break;
3006 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003007 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003008 case Pass_kind:
3009 break;
3010 case Break_kind:
3011 if (!compiler_in_loop(c))
3012 return compiler_error(c, "'break' outside loop");
3013 ADDOP(c, BREAK_LOOP);
3014 break;
3015 case Continue_kind:
3016 return compiler_continue(c);
3017 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003018 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04003019 case AsyncFunctionDef_kind:
3020 return compiler_function(c, s, 1);
3021 case AsyncWith_kind:
3022 return compiler_async_with(c, s, 0);
3023 case AsyncFor_kind:
3024 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003025 }
Yury Selivanov75445082015-05-11 22:57:16 -04003026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003027 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003028}
3029
3030static int
3031unaryop(unaryop_ty op)
3032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 switch (op) {
3034 case Invert:
3035 return UNARY_INVERT;
3036 case Not:
3037 return UNARY_NOT;
3038 case UAdd:
3039 return UNARY_POSITIVE;
3040 case USub:
3041 return UNARY_NEGATIVE;
3042 default:
3043 PyErr_Format(PyExc_SystemError,
3044 "unary op %d should not be possible", op);
3045 return 0;
3046 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003047}
3048
3049static int
3050binop(struct compiler *c, operator_ty op)
3051{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003052 switch (op) {
3053 case Add:
3054 return BINARY_ADD;
3055 case Sub:
3056 return BINARY_SUBTRACT;
3057 case Mult:
3058 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003059 case MatMult:
3060 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 case Div:
3062 return BINARY_TRUE_DIVIDE;
3063 case Mod:
3064 return BINARY_MODULO;
3065 case Pow:
3066 return BINARY_POWER;
3067 case LShift:
3068 return BINARY_LSHIFT;
3069 case RShift:
3070 return BINARY_RSHIFT;
3071 case BitOr:
3072 return BINARY_OR;
3073 case BitXor:
3074 return BINARY_XOR;
3075 case BitAnd:
3076 return BINARY_AND;
3077 case FloorDiv:
3078 return BINARY_FLOOR_DIVIDE;
3079 default:
3080 PyErr_Format(PyExc_SystemError,
3081 "binary op %d should not be possible", op);
3082 return 0;
3083 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003084}
3085
3086static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003087inplace_binop(struct compiler *c, operator_ty op)
3088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089 switch (op) {
3090 case Add:
3091 return INPLACE_ADD;
3092 case Sub:
3093 return INPLACE_SUBTRACT;
3094 case Mult:
3095 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003096 case MatMult:
3097 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003098 case Div:
3099 return INPLACE_TRUE_DIVIDE;
3100 case Mod:
3101 return INPLACE_MODULO;
3102 case Pow:
3103 return INPLACE_POWER;
3104 case LShift:
3105 return INPLACE_LSHIFT;
3106 case RShift:
3107 return INPLACE_RSHIFT;
3108 case BitOr:
3109 return INPLACE_OR;
3110 case BitXor:
3111 return INPLACE_XOR;
3112 case BitAnd:
3113 return INPLACE_AND;
3114 case FloorDiv:
3115 return INPLACE_FLOOR_DIVIDE;
3116 default:
3117 PyErr_Format(PyExc_SystemError,
3118 "inplace binary op %d should not be possible", op);
3119 return 0;
3120 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003121}
3122
3123static int
3124compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3125{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003126 int op, scope;
3127 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003128 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003130 PyObject *dict = c->u->u_names;
3131 PyObject *mangled;
3132 /* XXX AugStore isn't used anywhere! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003134 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3135 !_PyUnicode_EqualToASCIIString(name, "True") &&
3136 !_PyUnicode_EqualToASCIIString(name, "False"));
Benjamin Peterson70b224d2012-12-06 17:49:58 -05003137
Serhiy Storchakabd6ec4d2017-12-18 14:29:12 +02003138 mangled = _Py_Mangle(c->u->u_private, name);
3139 if (!mangled)
3140 return 0;
3141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003142 op = 0;
3143 optype = OP_NAME;
3144 scope = PyST_GetScope(c->u->u_ste, mangled);
3145 switch (scope) {
3146 case FREE:
3147 dict = c->u->u_freevars;
3148 optype = OP_DEREF;
3149 break;
3150 case CELL:
3151 dict = c->u->u_cellvars;
3152 optype = OP_DEREF;
3153 break;
3154 case LOCAL:
3155 if (c->u->u_ste->ste_type == FunctionBlock)
3156 optype = OP_FAST;
3157 break;
3158 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04003159 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003160 optype = OP_GLOBAL;
3161 break;
3162 case GLOBAL_EXPLICIT:
3163 optype = OP_GLOBAL;
3164 break;
3165 default:
3166 /* scope can be 0 */
3167 break;
3168 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003170 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003171 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003173 switch (optype) {
3174 case OP_DEREF:
3175 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003176 case Load:
3177 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3178 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003179 case Store: op = STORE_DEREF; break;
3180 case AugLoad:
3181 case AugStore:
3182 break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003183 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003184 case Param:
3185 default:
3186 PyErr_SetString(PyExc_SystemError,
3187 "param invalid for deref variable");
3188 return 0;
3189 }
3190 break;
3191 case OP_FAST:
3192 switch (ctx) {
3193 case Load: op = LOAD_FAST; break;
3194 case Store: op = STORE_FAST; break;
3195 case Del: op = DELETE_FAST; break;
3196 case AugLoad:
3197 case AugStore:
3198 break;
3199 case Param:
3200 default:
3201 PyErr_SetString(PyExc_SystemError,
3202 "param invalid for local variable");
3203 return 0;
3204 }
3205 ADDOP_O(c, op, mangled, varnames);
3206 Py_DECREF(mangled);
3207 return 1;
3208 case OP_GLOBAL:
3209 switch (ctx) {
3210 case Load: op = LOAD_GLOBAL; break;
3211 case Store: op = STORE_GLOBAL; break;
3212 case Del: op = DELETE_GLOBAL; break;
3213 case AugLoad:
3214 case AugStore:
3215 break;
3216 case Param:
3217 default:
3218 PyErr_SetString(PyExc_SystemError,
3219 "param invalid for global variable");
3220 return 0;
3221 }
3222 break;
3223 case OP_NAME:
3224 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00003225 case Load: op = LOAD_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003226 case Store: op = STORE_NAME; break;
3227 case Del: op = DELETE_NAME; break;
3228 case AugLoad:
3229 case AugStore:
3230 break;
3231 case Param:
3232 default:
3233 PyErr_SetString(PyExc_SystemError,
3234 "param invalid for name variable");
3235 return 0;
3236 }
3237 break;
3238 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003240 assert(op);
3241 arg = compiler_add_o(c, dict, mangled);
3242 Py_DECREF(mangled);
3243 if (arg < 0)
3244 return 0;
3245 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003246}
3247
3248static int
3249compiler_boolop(struct compiler *c, expr_ty e)
3250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003251 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003252 int jumpi;
3253 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 asdl_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003256 assert(e->kind == BoolOp_kind);
3257 if (e->v.BoolOp.op == And)
3258 jumpi = JUMP_IF_FALSE_OR_POP;
3259 else
3260 jumpi = JUMP_IF_TRUE_OR_POP;
3261 end = compiler_new_block(c);
3262 if (end == NULL)
3263 return 0;
3264 s = e->v.BoolOp.values;
3265 n = asdl_seq_LEN(s) - 1;
3266 assert(n >= 0);
3267 for (i = 0; i < n; ++i) {
3268 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
3269 ADDOP_JABS(c, jumpi, end);
3270 }
3271 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3272 compiler_use_next_block(c, end);
3273 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003274}
3275
3276static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003277starunpack_helper(struct compiler *c, asdl_seq *elts,
3278 int single_op, int inner_op, int outer_op)
3279{
3280 Py_ssize_t n = asdl_seq_LEN(elts);
3281 Py_ssize_t i, nsubitems = 0, nseen = 0;
3282 for (i = 0; i < n; i++) {
3283 expr_ty elt = asdl_seq_GET(elts, i);
3284 if (elt->kind == Starred_kind) {
3285 if (nseen) {
3286 ADDOP_I(c, inner_op, nseen);
3287 nseen = 0;
3288 nsubitems++;
3289 }
3290 VISIT(c, expr, elt->v.Starred.value);
3291 nsubitems++;
3292 }
3293 else {
3294 VISIT(c, expr, elt);
3295 nseen++;
3296 }
3297 }
3298 if (nsubitems) {
3299 if (nseen) {
3300 ADDOP_I(c, inner_op, nseen);
3301 nsubitems++;
3302 }
3303 ADDOP_I(c, outer_op, nsubitems);
3304 }
3305 else
3306 ADDOP_I(c, single_op, nseen);
3307 return 1;
3308}
3309
3310static int
3311assignment_helper(struct compiler *c, asdl_seq *elts)
3312{
3313 Py_ssize_t n = asdl_seq_LEN(elts);
3314 Py_ssize_t i;
3315 int seen_star = 0;
3316 for (i = 0; i < n; i++) {
3317 expr_ty elt = asdl_seq_GET(elts, i);
3318 if (elt->kind == Starred_kind && !seen_star) {
3319 if ((i >= (1 << 8)) ||
3320 (n-i-1 >= (INT_MAX >> 8)))
3321 return compiler_error(c,
3322 "too many expressions in "
3323 "star-unpacking assignment");
3324 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3325 seen_star = 1;
3326 asdl_seq_SET(elts, i, elt->v.Starred.value);
3327 }
3328 else if (elt->kind == Starred_kind) {
3329 return compiler_error(c,
3330 "two starred expressions in assignment");
3331 }
3332 }
3333 if (!seen_star) {
3334 ADDOP_I(c, UNPACK_SEQUENCE, n);
3335 }
3336 VISIT_SEQ(c, expr, elts);
3337 return 1;
3338}
3339
3340static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003341compiler_list(struct compiler *c, expr_ty e)
3342{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003343 asdl_seq *elts = e->v.List.elts;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003344 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003345 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003346 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003347 else if (e->v.List.ctx == Load) {
3348 return starunpack_helper(c, elts,
3349 BUILD_LIST, BUILD_TUPLE, BUILD_LIST_UNPACK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003351 else
3352 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003353 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003354}
3355
3356static int
3357compiler_tuple(struct compiler *c, expr_ty e)
3358{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003359 asdl_seq *elts = e->v.Tuple.elts;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003360 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003361 return assignment_helper(c, elts);
3362 }
3363 else if (e->v.Tuple.ctx == Load) {
3364 return starunpack_helper(c, elts,
3365 BUILD_TUPLE, BUILD_TUPLE, BUILD_TUPLE_UNPACK);
3366 }
3367 else
3368 VISIT_SEQ(c, expr, elts);
3369 return 1;
3370}
3371
3372static int
3373compiler_set(struct compiler *c, expr_ty e)
3374{
3375 return starunpack_helper(c, e->v.Set.elts, BUILD_SET,
3376 BUILD_SET, BUILD_SET_UNPACK);
3377}
3378
3379static int
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003380are_all_items_const(asdl_seq *seq, Py_ssize_t begin, Py_ssize_t end)
3381{
3382 Py_ssize_t i;
3383 for (i = begin; i < end; i++) {
3384 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
3385 if (key == NULL || !is_const(key))
3386 return 0;
3387 }
3388 return 1;
3389}
3390
3391static int
3392compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3393{
3394 Py_ssize_t i, n = end - begin;
3395 PyObject *keys, *key;
3396 if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) {
3397 for (i = begin; i < end; i++) {
3398 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3399 }
3400 keys = PyTuple_New(n);
3401 if (keys == NULL) {
3402 return 0;
3403 }
3404 for (i = begin; i < end; i++) {
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02003405 key = get_const_value((expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003406 Py_INCREF(key);
3407 PyTuple_SET_ITEM(keys, i - begin, key);
3408 }
3409 ADDOP_N(c, LOAD_CONST, keys, consts);
3410 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3411 }
3412 else {
3413 for (i = begin; i < end; i++) {
3414 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3415 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3416 }
3417 ADDOP_I(c, BUILD_MAP, n);
3418 }
3419 return 1;
3420}
3421
3422static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003423compiler_dict(struct compiler *c, expr_ty e)
3424{
Victor Stinner976bb402016-03-23 11:36:19 +01003425 Py_ssize_t i, n, elements;
3426 int containers;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003427 int is_unpacking = 0;
3428 n = asdl_seq_LEN(e->v.Dict.values);
3429 containers = 0;
3430 elements = 0;
3431 for (i = 0; i < n; i++) {
3432 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
3433 if (elements == 0xFFFF || (elements && is_unpacking)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003434 if (!compiler_subdict(c, e, i - elements, i))
3435 return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003436 containers++;
3437 elements = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003439 if (is_unpacking) {
3440 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3441 containers++;
3442 }
3443 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003444 elements++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445 }
3446 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003447 if (elements || containers == 0) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003448 if (!compiler_subdict(c, e, n - elements, n))
3449 return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003450 containers++;
3451 }
3452 /* If there is more than one dict, they need to be merged into a new
3453 * dict. If there is one dict and it's an unpacking, then it needs
3454 * to be copied into a new dict." */
Serhiy Storchaka3d85fae2016-11-28 20:56:37 +02003455 if (containers > 1 || is_unpacking) {
3456 ADDOP_I(c, BUILD_MAP_UNPACK, containers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003457 }
3458 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003459}
3460
3461static int
3462compiler_compare(struct compiler *c, expr_ty e)
3463{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003464 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003466 VISIT(c, expr, e->v.Compare.left);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003467 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
3468 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
3469 if (n == 0) {
3470 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
3471 ADDOP_I(c, COMPARE_OP,
3472 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, 0))));
3473 }
3474 else {
3475 basicblock *cleanup = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003476 if (cleanup == NULL)
3477 return 0;
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003478 for (i = 0; i < n; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003479 VISIT(c, expr,
3480 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003481 ADDOP(c, DUP_TOP);
3482 ADDOP(c, ROT_THREE);
3483 ADDOP_I(c, COMPARE_OP,
3484 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, i))));
3485 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
3486 NEXT_BLOCK(c);
3487 }
3488 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
3489 ADDOP_I(c, COMPARE_OP,
3490 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n))));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003491 basicblock *end = compiler_new_block(c);
3492 if (end == NULL)
3493 return 0;
3494 ADDOP_JREL(c, JUMP_FORWARD, end);
3495 compiler_use_next_block(c, cleanup);
3496 ADDOP(c, ROT_TWO);
3497 ADDOP(c, POP_TOP);
3498 compiler_use_next_block(c, end);
3499 }
3500 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003501}
3502
3503static int
Yury Selivanovf2392132016-12-13 19:03:51 -05003504maybe_optimize_method_call(struct compiler *c, expr_ty e)
3505{
3506 Py_ssize_t argsl, i;
3507 expr_ty meth = e->v.Call.func;
3508 asdl_seq *args = e->v.Call.args;
3509
3510 /* Check that the call node is an attribute access, and that
3511 the call doesn't have keyword parameters. */
3512 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
3513 asdl_seq_LEN(e->v.Call.keywords))
3514 return -1;
3515
3516 /* Check that there are no *varargs types of arguments. */
3517 argsl = asdl_seq_LEN(args);
3518 for (i = 0; i < argsl; i++) {
3519 expr_ty elt = asdl_seq_GET(args, i);
3520 if (elt->kind == Starred_kind) {
3521 return -1;
3522 }
3523 }
3524
3525 /* Alright, we can optimize the code. */
3526 VISIT(c, expr, meth->v.Attribute.value);
3527 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
3528 VISIT_SEQ(c, expr, e->v.Call.args);
3529 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
3530 return 1;
3531}
3532
3533static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003534compiler_call(struct compiler *c, expr_ty e)
3535{
Yury Selivanovf2392132016-12-13 19:03:51 -05003536 if (maybe_optimize_method_call(c, e) > 0)
3537 return 1;
3538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003539 VISIT(c, expr, e->v.Call.func);
3540 return compiler_call_helper(c, 0,
3541 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003542 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003543}
3544
Eric V. Smith235a6f02015-09-19 14:51:32 -04003545static int
3546compiler_joined_str(struct compiler *c, expr_ty e)
3547{
Eric V. Smith235a6f02015-09-19 14:51:32 -04003548 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02003549 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1)
3550 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
Eric V. Smith235a6f02015-09-19 14:51:32 -04003551 return 1;
3552}
3553
Eric V. Smitha78c7952015-11-03 12:45:05 -05003554/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04003555static int
3556compiler_formatted_value(struct compiler *c, expr_ty e)
3557{
Eric V. Smitha78c7952015-11-03 12:45:05 -05003558 /* Our oparg encodes 2 pieces of information: the conversion
3559 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04003560
Eric V. Smitha78c7952015-11-03 12:45:05 -05003561 Convert the conversion char to 2 bits:
3562 None: 000 0x0 FVC_NONE
3563 !s : 001 0x1 FVC_STR
3564 !r : 010 0x2 FVC_REPR
3565 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04003566
Eric V. Smitha78c7952015-11-03 12:45:05 -05003567 next bit is whether or not we have a format spec:
3568 yes : 100 0x4
3569 no : 000 0x0
3570 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04003571
Eric V. Smitha78c7952015-11-03 12:45:05 -05003572 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003573
Eric V. Smitha78c7952015-11-03 12:45:05 -05003574 /* Evaluate the expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04003575 VISIT(c, expr, e->v.FormattedValue.value);
3576
Eric V. Smitha78c7952015-11-03 12:45:05 -05003577 switch (e->v.FormattedValue.conversion) {
3578 case 's': oparg = FVC_STR; break;
3579 case 'r': oparg = FVC_REPR; break;
3580 case 'a': oparg = FVC_ASCII; break;
3581 case -1: oparg = FVC_NONE; break;
3582 default:
3583 PyErr_SetString(PyExc_SystemError,
3584 "Unrecognized conversion character");
3585 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003586 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04003587 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003588 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04003589 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003590 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003591 }
3592
Eric V. Smitha78c7952015-11-03 12:45:05 -05003593 /* And push our opcode and oparg */
3594 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith235a6f02015-09-19 14:51:32 -04003595 return 1;
3596}
3597
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003598static int
3599compiler_subkwargs(struct compiler *c, asdl_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
3600{
3601 Py_ssize_t i, n = end - begin;
3602 keyword_ty kw;
3603 PyObject *keys, *key;
3604 assert(n > 0);
3605 if (n > 1) {
3606 for (i = begin; i < end; i++) {
3607 kw = asdl_seq_GET(keywords, i);
3608 VISIT(c, expr, kw->value);
3609 }
3610 keys = PyTuple_New(n);
3611 if (keys == NULL) {
3612 return 0;
3613 }
3614 for (i = begin; i < end; i++) {
3615 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
3616 Py_INCREF(key);
3617 PyTuple_SET_ITEM(keys, i - begin, key);
3618 }
3619 ADDOP_N(c, LOAD_CONST, keys, consts);
3620 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3621 }
3622 else {
3623 /* a for loop only executes once */
3624 for (i = begin; i < end; i++) {
3625 kw = asdl_seq_GET(keywords, i);
3626 ADDOP_O(c, LOAD_CONST, kw->arg, consts);
3627 VISIT(c, expr, kw->value);
3628 }
3629 ADDOP_I(c, BUILD_MAP, n);
3630 }
3631 return 1;
3632}
3633
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003634/* shared code between compiler_call and compiler_class */
3635static int
3636compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01003637 int n, /* Args already pushed */
Victor Stinnerad9a0662013-11-19 22:23:20 +01003638 asdl_seq *args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003639 asdl_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003640{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003641 Py_ssize_t i, nseen, nelts, nkwelts;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003642 int mustdictunpack = 0;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003643
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003644 /* the number of tuples and dictionaries on the stack */
3645 Py_ssize_t nsubargs = 0, nsubkwargs = 0;
3646
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003647 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003648 nkwelts = asdl_seq_LEN(keywords);
3649
3650 for (i = 0; i < nkwelts; i++) {
3651 keyword_ty kw = asdl_seq_GET(keywords, i);
3652 if (kw->arg == NULL) {
3653 mustdictunpack = 1;
3654 break;
3655 }
3656 }
3657
3658 nseen = n; /* the number of positional arguments on the stack */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003659 for (i = 0; i < nelts; i++) {
3660 expr_ty elt = asdl_seq_GET(args, i);
3661 if (elt->kind == Starred_kind) {
3662 /* A star-arg. If we've seen positional arguments,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003663 pack the positional arguments into a tuple. */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003664 if (nseen) {
3665 ADDOP_I(c, BUILD_TUPLE, nseen);
3666 nseen = 0;
3667 nsubargs++;
3668 }
3669 VISIT(c, expr, elt->v.Starred.value);
3670 nsubargs++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003671 }
3672 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003673 VISIT(c, expr, elt);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003674 nseen++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003675 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003676 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003677
3678 /* Same dance again for keyword arguments */
Serhiy Storchakab7281052016-09-12 00:52:40 +03003679 if (nsubargs || mustdictunpack) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003680 if (nseen) {
3681 /* Pack up any trailing positional arguments. */
3682 ADDOP_I(c, BUILD_TUPLE, nseen);
3683 nsubargs++;
3684 }
Serhiy Storchakab7281052016-09-12 00:52:40 +03003685 if (nsubargs > 1) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003686 /* If we ended up with more than one stararg, we need
3687 to concatenate them into a single sequence. */
Serhiy Storchaka73442852016-10-02 10:33:46 +03003688 ADDOP_I(c, BUILD_TUPLE_UNPACK_WITH_CALL, nsubargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003689 }
3690 else if (nsubargs == 0) {
3691 ADDOP_I(c, BUILD_TUPLE, 0);
3692 }
3693 nseen = 0; /* the number of keyword arguments on the stack following */
3694 for (i = 0; i < nkwelts; i++) {
3695 keyword_ty kw = asdl_seq_GET(keywords, i);
3696 if (kw->arg == NULL) {
3697 /* A keyword argument unpacking. */
3698 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003699 if (!compiler_subkwargs(c, keywords, i - nseen, i))
3700 return 0;
3701 nsubkwargs++;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003702 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003703 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003704 VISIT(c, expr, kw->value);
3705 nsubkwargs++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003706 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003707 else {
3708 nseen++;
3709 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003710 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003711 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003712 /* Pack up any trailing keyword arguments. */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003713 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts))
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003714 return 0;
3715 nsubkwargs++;
3716 }
Serhiy Storchakab7281052016-09-12 00:52:40 +03003717 if (nsubkwargs > 1) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003718 /* Pack it all up */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003719 ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003720 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003721 ADDOP_I(c, CALL_FUNCTION_EX, nsubkwargs > 0);
3722 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003723 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003724 else if (nkwelts) {
3725 PyObject *names;
3726 VISIT_SEQ(c, keyword, keywords);
3727 names = PyTuple_New(nkwelts);
3728 if (names == NULL) {
3729 return 0;
3730 }
3731 for (i = 0; i < nkwelts; i++) {
3732 keyword_ty kw = asdl_seq_GET(keywords, i);
3733 Py_INCREF(kw->arg);
3734 PyTuple_SET_ITEM(names, i, kw->arg);
3735 }
3736 ADDOP_N(c, LOAD_CONST, names, consts);
3737 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
3738 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003739 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003740 else {
3741 ADDOP_I(c, CALL_FUNCTION, n + nelts);
3742 return 1;
3743 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003744}
3745
Nick Coghlan650f0d02007-04-15 12:05:43 +00003746
3747/* List and set comprehensions and generator expressions work by creating a
3748 nested function to perform the actual iteration. This means that the
3749 iteration variables don't leak into the current scope.
3750 The defined function is called immediately following its definition, with the
3751 result of that call being the result of the expression.
3752 The LC/SC version returns the populated container, while the GE version is
3753 flagged in symtable.c as a generator, so it returns the generator object
3754 when the function is called.
3755 This code *knows* that the loop cannot contain break, continue, or return,
3756 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
3757
3758 Possible cleanups:
3759 - iterate over the generator sequence instead of using recursion
3760*/
3761
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003762
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003763static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003764compiler_comprehension_generator(struct compiler *c,
3765 asdl_seq *generators, int gen_index,
3766 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003767{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003768 comprehension_ty gen;
3769 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
3770 if (gen->is_async) {
3771 return compiler_async_comprehension_generator(
3772 c, generators, gen_index, elt, val, type);
3773 } else {
3774 return compiler_sync_comprehension_generator(
3775 c, generators, gen_index, elt, val, type);
3776 }
3777}
3778
3779static int
3780compiler_sync_comprehension_generator(struct compiler *c,
3781 asdl_seq *generators, int gen_index,
3782 expr_ty elt, expr_ty val, int type)
3783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 /* generate code for the iterator, then each of the ifs,
3785 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003787 comprehension_ty gen;
3788 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003789 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003791 start = compiler_new_block(c);
3792 skip = compiler_new_block(c);
3793 if_cleanup = compiler_new_block(c);
3794 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003796 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3797 anchor == NULL)
3798 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003800 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003802 if (gen_index == 0) {
3803 /* Receive outermost iter as an implicit argument */
3804 c->u->u_argcount = 1;
3805 ADDOP_I(c, LOAD_FAST, 0);
3806 }
3807 else {
3808 /* Sub-iter - calculate on the fly */
3809 VISIT(c, expr, gen->iter);
3810 ADDOP(c, GET_ITER);
3811 }
3812 compiler_use_next_block(c, start);
3813 ADDOP_JREL(c, FOR_ITER, anchor);
3814 NEXT_BLOCK(c);
3815 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003817 /* XXX this needs to be cleaned up...a lot! */
3818 n = asdl_seq_LEN(gen->ifs);
3819 for (i = 0; i < n; i++) {
3820 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003821 if (!compiler_jump_if(c, e, if_cleanup, 0))
3822 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003823 NEXT_BLOCK(c);
3824 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003826 if (++gen_index < asdl_seq_LEN(generators))
3827 if (!compiler_comprehension_generator(c,
3828 generators, gen_index,
3829 elt, val, type))
3830 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003832 /* only append after the last for generator */
3833 if (gen_index >= asdl_seq_LEN(generators)) {
3834 /* comprehension specific code */
3835 switch (type) {
3836 case COMP_GENEXP:
3837 VISIT(c, expr, elt);
3838 ADDOP(c, YIELD_VALUE);
3839 ADDOP(c, POP_TOP);
3840 break;
3841 case COMP_LISTCOMP:
3842 VISIT(c, expr, elt);
3843 ADDOP_I(c, LIST_APPEND, gen_index + 1);
3844 break;
3845 case COMP_SETCOMP:
3846 VISIT(c, expr, elt);
3847 ADDOP_I(c, SET_ADD, gen_index + 1);
3848 break;
3849 case COMP_DICTCOMP:
3850 /* With 'd[k] = v', v is evaluated before k, so we do
3851 the same. */
3852 VISIT(c, expr, val);
3853 VISIT(c, expr, elt);
3854 ADDOP_I(c, MAP_ADD, gen_index + 1);
3855 break;
3856 default:
3857 return 0;
3858 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003860 compiler_use_next_block(c, skip);
3861 }
3862 compiler_use_next_block(c, if_cleanup);
3863 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3864 compiler_use_next_block(c, anchor);
3865
3866 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003867}
3868
3869static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003870compiler_async_comprehension_generator(struct compiler *c,
3871 asdl_seq *generators, int gen_index,
3872 expr_ty elt, expr_ty val, int type)
3873{
3874 _Py_IDENTIFIER(StopAsyncIteration);
3875
3876 comprehension_ty gen;
3877 basicblock *anchor, *skip, *if_cleanup, *try,
3878 *after_try, *except, *try_cleanup;
3879 Py_ssize_t i, n;
3880
3881 PyObject *stop_aiter_error = _PyUnicode_FromId(&PyId_StopAsyncIteration);
3882 if (stop_aiter_error == NULL) {
3883 return 0;
3884 }
3885
3886 try = compiler_new_block(c);
3887 after_try = compiler_new_block(c);
3888 try_cleanup = compiler_new_block(c);
3889 except = compiler_new_block(c);
3890 skip = compiler_new_block(c);
3891 if_cleanup = compiler_new_block(c);
3892 anchor = compiler_new_block(c);
3893
3894 if (skip == NULL || if_cleanup == NULL || anchor == NULL ||
3895 try == NULL || after_try == NULL ||
3896 except == NULL || after_try == NULL) {
3897 return 0;
3898 }
3899
3900 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
3901
3902 if (gen_index == 0) {
3903 /* Receive outermost iter as an implicit argument */
3904 c->u->u_argcount = 1;
3905 ADDOP_I(c, LOAD_FAST, 0);
3906 }
3907 else {
3908 /* Sub-iter - calculate on the fly */
3909 VISIT(c, expr, gen->iter);
3910 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003911 }
3912
3913 compiler_use_next_block(c, try);
3914
3915
3916 ADDOP_JREL(c, SETUP_EXCEPT, except);
3917 if (!compiler_push_fblock(c, EXCEPT, try))
3918 return 0;
3919
3920 ADDOP(c, GET_ANEXT);
3921 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3922 ADDOP(c, YIELD_FROM);
3923 VISIT(c, expr, gen->target);
3924 ADDOP(c, POP_BLOCK);
3925 compiler_pop_fblock(c, EXCEPT, try);
3926 ADDOP_JREL(c, JUMP_FORWARD, after_try);
3927
3928
3929 compiler_use_next_block(c, except);
3930 ADDOP(c, DUP_TOP);
3931 ADDOP_O(c, LOAD_GLOBAL, stop_aiter_error, names);
3932 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
3933 ADDOP_JABS(c, POP_JUMP_IF_FALSE, try_cleanup);
3934
3935 ADDOP(c, POP_TOP);
3936 ADDOP(c, POP_TOP);
3937 ADDOP(c, POP_TOP);
3938 ADDOP(c, POP_EXCEPT); /* for SETUP_EXCEPT */
3939 ADDOP_JABS(c, JUMP_ABSOLUTE, anchor);
3940
3941
3942 compiler_use_next_block(c, try_cleanup);
3943 ADDOP(c, END_FINALLY);
3944
3945 compiler_use_next_block(c, after_try);
3946
3947 n = asdl_seq_LEN(gen->ifs);
3948 for (i = 0; i < n; i++) {
3949 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003950 if (!compiler_jump_if(c, e, if_cleanup, 0))
3951 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003952 NEXT_BLOCK(c);
3953 }
3954
3955 if (++gen_index < asdl_seq_LEN(generators))
3956 if (!compiler_comprehension_generator(c,
3957 generators, gen_index,
3958 elt, val, type))
3959 return 0;
3960
3961 /* only append after the last for generator */
3962 if (gen_index >= asdl_seq_LEN(generators)) {
3963 /* comprehension specific code */
3964 switch (type) {
3965 case COMP_GENEXP:
3966 VISIT(c, expr, elt);
3967 ADDOP(c, YIELD_VALUE);
3968 ADDOP(c, POP_TOP);
3969 break;
3970 case COMP_LISTCOMP:
3971 VISIT(c, expr, elt);
3972 ADDOP_I(c, LIST_APPEND, gen_index + 1);
3973 break;
3974 case COMP_SETCOMP:
3975 VISIT(c, expr, elt);
3976 ADDOP_I(c, SET_ADD, gen_index + 1);
3977 break;
3978 case COMP_DICTCOMP:
3979 /* With 'd[k] = v', v is evaluated before k, so we do
3980 the same. */
3981 VISIT(c, expr, val);
3982 VISIT(c, expr, elt);
3983 ADDOP_I(c, MAP_ADD, gen_index + 1);
3984 break;
3985 default:
3986 return 0;
3987 }
3988
3989 compiler_use_next_block(c, skip);
3990 }
3991 compiler_use_next_block(c, if_cleanup);
3992 ADDOP_JABS(c, JUMP_ABSOLUTE, try);
3993 compiler_use_next_block(c, anchor);
3994 ADDOP(c, POP_TOP);
3995
3996 return 1;
3997}
3998
3999static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004000compiler_comprehension(struct compiler *c, expr_ty e, int type,
4001 identifier name, asdl_seq *generators, expr_ty elt,
4002 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004003{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004004 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004005 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004006 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004007 int is_async_function = c->u->u_ste->ste_coroutine;
4008 int is_async_generator = 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004009
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004010 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004011
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004012 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4013 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004014 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004015 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004016 }
4017
4018 is_async_generator = c->u->u_ste->ste_coroutine;
4019
Yury Selivanovb8ab9d32017-10-06 02:58:28 -04004020 if (is_async_generator && !is_async_function && type != COMP_GENEXP) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004021 if (e->lineno > c->u->u_lineno) {
4022 c->u->u_lineno = e->lineno;
4023 c->u->u_lineno_set = 0;
4024 }
4025 compiler_error(c, "asynchronous comprehension outside of "
4026 "an asynchronous function");
4027 goto error_in_scope;
4028 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004030 if (type != COMP_GENEXP) {
4031 int op;
4032 switch (type) {
4033 case COMP_LISTCOMP:
4034 op = BUILD_LIST;
4035 break;
4036 case COMP_SETCOMP:
4037 op = BUILD_SET;
4038 break;
4039 case COMP_DICTCOMP:
4040 op = BUILD_MAP;
4041 break;
4042 default:
4043 PyErr_Format(PyExc_SystemError,
4044 "unknown comprehension type %d", type);
4045 goto error_in_scope;
4046 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004048 ADDOP_I(c, op, 0);
4049 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004051 if (!compiler_comprehension_generator(c, generators, 0, elt,
4052 val, type))
4053 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004055 if (type != COMP_GENEXP) {
4056 ADDOP(c, RETURN_VALUE);
4057 }
4058
4059 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004060 qualname = c->u->u_qualname;
4061 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004062 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004063 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004064 goto error;
4065
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004066 if (!compiler_make_closure(c, co, 0, qualname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004067 goto error;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004068 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004069 Py_DECREF(co);
4070
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004071 VISIT(c, expr, outermost->iter);
4072
4073 if (outermost->is_async) {
4074 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004075 } else {
4076 ADDOP(c, GET_ITER);
4077 }
4078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004079 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004080
4081 if (is_async_generator && type != COMP_GENEXP) {
4082 ADDOP(c, GET_AWAITABLE);
4083 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4084 ADDOP(c, YIELD_FROM);
4085 }
4086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004087 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004088error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004089 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004090error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004091 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004092 Py_XDECREF(co);
4093 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004094}
4095
4096static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004097compiler_genexp(struct compiler *c, expr_ty e)
4098{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004099 static identifier name;
4100 if (!name) {
4101 name = PyUnicode_FromString("<genexpr>");
4102 if (!name)
4103 return 0;
4104 }
4105 assert(e->kind == GeneratorExp_kind);
4106 return compiler_comprehension(c, e, COMP_GENEXP, name,
4107 e->v.GeneratorExp.generators,
4108 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004109}
4110
4111static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004112compiler_listcomp(struct compiler *c, expr_ty e)
4113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004114 static identifier name;
4115 if (!name) {
4116 name = PyUnicode_FromString("<listcomp>");
4117 if (!name)
4118 return 0;
4119 }
4120 assert(e->kind == ListComp_kind);
4121 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4122 e->v.ListComp.generators,
4123 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004124}
4125
4126static int
4127compiler_setcomp(struct compiler *c, expr_ty e)
4128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004129 static identifier name;
4130 if (!name) {
4131 name = PyUnicode_FromString("<setcomp>");
4132 if (!name)
4133 return 0;
4134 }
4135 assert(e->kind == SetComp_kind);
4136 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4137 e->v.SetComp.generators,
4138 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004139}
4140
4141
4142static int
4143compiler_dictcomp(struct compiler *c, expr_ty e)
4144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004145 static identifier name;
4146 if (!name) {
4147 name = PyUnicode_FromString("<dictcomp>");
4148 if (!name)
4149 return 0;
4150 }
4151 assert(e->kind == DictComp_kind);
4152 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4153 e->v.DictComp.generators,
4154 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004155}
4156
4157
4158static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004159compiler_visit_keyword(struct compiler *c, keyword_ty k)
4160{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004161 VISIT(c, expr, k->value);
4162 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004163}
4164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004165/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004166 whether they are true or false.
4167
4168 Return values: 1 for true, 0 for false, -1 for non-constant.
4169 */
4170
4171static int
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02004172expr_constant(expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004173{
Serhiy Storchaka3325a672017-12-15 12:35:48 +02004174 if (is_const(e)) {
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02004175 return PyObject_IsTrue(get_const_value(e));
Benjamin Peterson442f2092012-12-06 17:41:04 -05004176 }
Serhiy Storchaka3325a672017-12-15 12:35:48 +02004177 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004178}
4179
Yury Selivanov75445082015-05-11 22:57:16 -04004180
4181/*
4182 Implements the async with statement.
4183
4184 The semantics outlined in that PEP are as follows:
4185
4186 async with EXPR as VAR:
4187 BLOCK
4188
4189 It is implemented roughly as:
4190
4191 context = EXPR
4192 exit = context.__aexit__ # not calling it
4193 value = await context.__aenter__()
4194 try:
4195 VAR = value # if VAR present in the syntax
4196 BLOCK
4197 finally:
4198 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004199 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04004200 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004201 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04004202 if not (await exit(*exc)):
4203 raise
4204 */
4205static int
4206compiler_async_with(struct compiler *c, stmt_ty s, int pos)
4207{
4208 basicblock *block, *finally;
4209 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
4210
4211 assert(s->kind == AsyncWith_kind);
4212
4213 block = compiler_new_block(c);
4214 finally = compiler_new_block(c);
4215 if (!block || !finally)
4216 return 0;
4217
4218 /* Evaluate EXPR */
4219 VISIT(c, expr, item->context_expr);
4220
4221 ADDOP(c, BEFORE_ASYNC_WITH);
4222 ADDOP(c, GET_AWAITABLE);
4223 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4224 ADDOP(c, YIELD_FROM);
4225
4226 ADDOP_JREL(c, SETUP_ASYNC_WITH, finally);
4227
4228 /* SETUP_ASYNC_WITH pushes a finally block. */
4229 compiler_use_next_block(c, block);
4230 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
4231 return 0;
4232 }
4233
4234 if (item->optional_vars) {
4235 VISIT(c, expr, item->optional_vars);
4236 }
4237 else {
4238 /* Discard result from context.__aenter__() */
4239 ADDOP(c, POP_TOP);
4240 }
4241
4242 pos++;
4243 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
4244 /* BLOCK code */
4245 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
4246 else if (!compiler_async_with(c, s, pos))
4247 return 0;
4248
4249 /* End of try block; start the finally block */
4250 ADDOP(c, POP_BLOCK);
4251 compiler_pop_fblock(c, FINALLY_TRY, block);
4252
4253 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4254 compiler_use_next_block(c, finally);
4255 if (!compiler_push_fblock(c, FINALLY_END, finally))
4256 return 0;
4257
4258 /* Finally block starts; context.__exit__ is on the stack under
4259 the exception or return information. Just issue our magic
4260 opcode. */
4261 ADDOP(c, WITH_CLEANUP_START);
4262
4263 ADDOP(c, GET_AWAITABLE);
4264 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4265 ADDOP(c, YIELD_FROM);
4266
4267 ADDOP(c, WITH_CLEANUP_FINISH);
4268
4269 /* Finally block ends. */
4270 ADDOP(c, END_FINALLY);
4271 compiler_pop_fblock(c, FINALLY_END, finally);
4272 return 1;
4273}
4274
4275
Guido van Rossumc2e20742006-02-27 22:32:47 +00004276/*
4277 Implements the with statement from PEP 343.
4278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004279 The semantics outlined in that PEP are as follows:
Guido van Rossumc2e20742006-02-27 22:32:47 +00004280
4281 with EXPR as VAR:
4282 BLOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004283
Guido van Rossumc2e20742006-02-27 22:32:47 +00004284 It is implemented roughly as:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004285
Thomas Wouters477c8d52006-05-27 19:21:47 +00004286 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00004287 exit = context.__exit__ # not calling it
4288 value = context.__enter__()
4289 try:
4290 VAR = value # if VAR present in the syntax
4291 BLOCK
4292 finally:
4293 if an exception was raised:
Serhiy Storchakad741a882015-06-11 00:06:39 +03004294 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004295 else:
Serhiy Storchakad741a882015-06-11 00:06:39 +03004296 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004297 exit(*exc)
4298 */
4299static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004300compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004301{
Guido van Rossumc2e20742006-02-27 22:32:47 +00004302 basicblock *block, *finally;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004303 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004304
4305 assert(s->kind == With_kind);
4306
Guido van Rossumc2e20742006-02-27 22:32:47 +00004307 block = compiler_new_block(c);
4308 finally = compiler_new_block(c);
4309 if (!block || !finally)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004310 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004311
Thomas Wouters477c8d52006-05-27 19:21:47 +00004312 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004313 VISIT(c, expr, item->context_expr);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004314 ADDOP_JREL(c, SETUP_WITH, finally);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004315
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004316 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00004317 compiler_use_next_block(c, block);
4318 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004319 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004320 }
4321
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004322 if (item->optional_vars) {
4323 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004324 }
4325 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004326 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004327 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004328 }
4329
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004330 pos++;
4331 if (pos == asdl_seq_LEN(s->v.With.items))
4332 /* BLOCK code */
4333 VISIT_SEQ(c, stmt, s->v.With.body)
4334 else if (!compiler_with(c, s, pos))
4335 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004336
4337 /* End of try block; start the finally block */
4338 ADDOP(c, POP_BLOCK);
4339 compiler_pop_fblock(c, FINALLY_TRY, block);
4340
4341 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4342 compiler_use_next_block(c, finally);
4343 if (!compiler_push_fblock(c, FINALLY_END, finally))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004344 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004345
Christian Heimesdd15f6c2008-03-16 00:07:10 +00004346 /* Finally block starts; context.__exit__ is on the stack under
4347 the exception or return information. Just issue our magic
4348 opcode. */
Yury Selivanov75445082015-05-11 22:57:16 -04004349 ADDOP(c, WITH_CLEANUP_START);
4350 ADDOP(c, WITH_CLEANUP_FINISH);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004351
4352 /* Finally block ends. */
4353 ADDOP(c, END_FINALLY);
4354 compiler_pop_fblock(c, FINALLY_END, finally);
4355 return 1;
4356}
4357
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004358static int
4359compiler_visit_expr(struct compiler *c, expr_ty e)
4360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004361 /* If expr e has a different line number than the last expr/stmt,
4362 set a new line number for the next instruction.
4363 */
4364 if (e->lineno > c->u->u_lineno) {
4365 c->u->u_lineno = e->lineno;
4366 c->u->u_lineno_set = 0;
4367 }
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00004368 /* Updating the column offset is always harmless. */
4369 c->u->u_col_offset = e->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004370 switch (e->kind) {
4371 case BoolOp_kind:
4372 return compiler_boolop(c, e);
4373 case BinOp_kind:
4374 VISIT(c, expr, e->v.BinOp.left);
4375 VISIT(c, expr, e->v.BinOp.right);
4376 ADDOP(c, binop(c, e->v.BinOp.op));
4377 break;
4378 case UnaryOp_kind:
4379 VISIT(c, expr, e->v.UnaryOp.operand);
4380 ADDOP(c, unaryop(e->v.UnaryOp.op));
4381 break;
4382 case Lambda_kind:
4383 return compiler_lambda(c, e);
4384 case IfExp_kind:
4385 return compiler_ifexp(c, e);
4386 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004387 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004388 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004389 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004390 case GeneratorExp_kind:
4391 return compiler_genexp(c, e);
4392 case ListComp_kind:
4393 return compiler_listcomp(c, e);
4394 case SetComp_kind:
4395 return compiler_setcomp(c, e);
4396 case DictComp_kind:
4397 return compiler_dictcomp(c, e);
4398 case Yield_kind:
4399 if (c->u->u_ste->ste_type != FunctionBlock)
4400 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00004401 if (e->v.Yield.value) {
4402 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004403 }
4404 else {
4405 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4406 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00004407 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004408 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00004409 case YieldFrom_kind:
4410 if (c->u->u_ste->ste_type != FunctionBlock)
4411 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04004412
4413 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
4414 return compiler_error(c, "'yield from' inside async function");
4415
Mark Dickinsonded35ae2012-11-25 14:36:26 +00004416 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004417 ADDOP(c, GET_YIELD_FROM_ITER);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00004418 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4419 ADDOP(c, YIELD_FROM);
4420 break;
Yury Selivanov75445082015-05-11 22:57:16 -04004421 case Await_kind:
4422 if (c->u->u_ste->ste_type != FunctionBlock)
4423 return compiler_error(c, "'await' outside function");
4424
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004425 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
4426 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION)
Yury Selivanov75445082015-05-11 22:57:16 -04004427 return compiler_error(c, "'await' outside async function");
4428
4429 VISIT(c, expr, e->v.Await.value);
4430 ADDOP(c, GET_AWAITABLE);
4431 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4432 ADDOP(c, YIELD_FROM);
4433 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004434 case Compare_kind:
4435 return compiler_compare(c, e);
4436 case Call_kind:
4437 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01004438 case Constant_kind:
4439 ADDOP_O(c, LOAD_CONST, e->v.Constant.value, consts);
4440 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004441 case Num_kind:
4442 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
4443 break;
4444 case Str_kind:
4445 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
4446 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004447 case JoinedStr_kind:
4448 return compiler_joined_str(c, e);
4449 case FormattedValue_kind:
4450 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004451 case Bytes_kind:
4452 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
4453 break;
4454 case Ellipsis_kind:
4455 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
4456 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05004457 case NameConstant_kind:
4458 ADDOP_O(c, LOAD_CONST, e->v.NameConstant.value, consts);
4459 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004460 /* The following exprs can be assignment targets. */
4461 case Attribute_kind:
4462 if (e->v.Attribute.ctx != AugStore)
4463 VISIT(c, expr, e->v.Attribute.value);
4464 switch (e->v.Attribute.ctx) {
4465 case AugLoad:
4466 ADDOP(c, DUP_TOP);
Stefan Krahf432a322017-08-21 13:09:59 +02004467 /* Fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004468 case Load:
4469 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
4470 break;
4471 case AugStore:
4472 ADDOP(c, ROT_TWO);
Stefan Krahf432a322017-08-21 13:09:59 +02004473 /* Fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004474 case Store:
4475 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
4476 break;
4477 case Del:
4478 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
4479 break;
4480 case Param:
4481 default:
4482 PyErr_SetString(PyExc_SystemError,
4483 "param invalid in attribute expression");
4484 return 0;
4485 }
4486 break;
4487 case Subscript_kind:
4488 switch (e->v.Subscript.ctx) {
4489 case AugLoad:
4490 VISIT(c, expr, e->v.Subscript.value);
4491 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
4492 break;
4493 case Load:
4494 VISIT(c, expr, e->v.Subscript.value);
4495 VISIT_SLICE(c, e->v.Subscript.slice, Load);
4496 break;
4497 case AugStore:
4498 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
4499 break;
4500 case Store:
4501 VISIT(c, expr, e->v.Subscript.value);
4502 VISIT_SLICE(c, e->v.Subscript.slice, Store);
4503 break;
4504 case Del:
4505 VISIT(c, expr, e->v.Subscript.value);
4506 VISIT_SLICE(c, e->v.Subscript.slice, Del);
4507 break;
4508 case Param:
4509 default:
4510 PyErr_SetString(PyExc_SystemError,
4511 "param invalid in subscript expression");
4512 return 0;
4513 }
4514 break;
4515 case Starred_kind:
4516 switch (e->v.Starred.ctx) {
4517 case Store:
4518 /* In all legitimate cases, the Starred node was already replaced
4519 * by compiler_list/compiler_tuple. XXX: is that okay? */
4520 return compiler_error(c,
4521 "starred assignment target must be in a list or tuple");
4522 default:
4523 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004524 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004525 }
4526 break;
4527 case Name_kind:
4528 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
4529 /* child nodes of List and Tuple will have expr_context set */
4530 case List_kind:
4531 return compiler_list(c, e);
4532 case Tuple_kind:
4533 return compiler_tuple(c, e);
4534 }
4535 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004536}
4537
4538static int
4539compiler_augassign(struct compiler *c, stmt_ty s)
4540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004541 expr_ty e = s->v.AugAssign.target;
4542 expr_ty auge;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004544 assert(s->kind == AugAssign_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004546 switch (e->kind) {
4547 case Attribute_kind:
4548 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
4549 AugLoad, e->lineno, e->col_offset, c->c_arena);
4550 if (auge == NULL)
4551 return 0;
4552 VISIT(c, expr, auge);
4553 VISIT(c, expr, s->v.AugAssign.value);
4554 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
4555 auge->v.Attribute.ctx = AugStore;
4556 VISIT(c, expr, auge);
4557 break;
4558 case Subscript_kind:
4559 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
4560 AugLoad, e->lineno, e->col_offset, c->c_arena);
4561 if (auge == NULL)
4562 return 0;
4563 VISIT(c, expr, auge);
4564 VISIT(c, expr, s->v.AugAssign.value);
4565 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
4566 auge->v.Subscript.ctx = AugStore;
4567 VISIT(c, expr, auge);
4568 break;
4569 case Name_kind:
4570 if (!compiler_nameop(c, e->v.Name.id, Load))
4571 return 0;
4572 VISIT(c, expr, s->v.AugAssign.value);
4573 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
4574 return compiler_nameop(c, e->v.Name.id, Store);
4575 default:
4576 PyErr_Format(PyExc_SystemError,
4577 "invalid node type (%d) for augmented assignment",
4578 e->kind);
4579 return 0;
4580 }
4581 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004582}
4583
4584static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07004585check_ann_expr(struct compiler *c, expr_ty e)
4586{
4587 VISIT(c, expr, e);
4588 ADDOP(c, POP_TOP);
4589 return 1;
4590}
4591
4592static int
4593check_annotation(struct compiler *c, stmt_ty s)
4594{
4595 /* Annotations are only evaluated in a module or class. */
4596 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
4597 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
4598 return check_ann_expr(c, s->v.AnnAssign.annotation);
4599 }
4600 return 1;
4601}
4602
4603static int
4604check_ann_slice(struct compiler *c, slice_ty sl)
4605{
4606 switch(sl->kind) {
4607 case Index_kind:
4608 return check_ann_expr(c, sl->v.Index.value);
4609 case Slice_kind:
4610 if (sl->v.Slice.lower && !check_ann_expr(c, sl->v.Slice.lower)) {
4611 return 0;
4612 }
4613 if (sl->v.Slice.upper && !check_ann_expr(c, sl->v.Slice.upper)) {
4614 return 0;
4615 }
4616 if (sl->v.Slice.step && !check_ann_expr(c, sl->v.Slice.step)) {
4617 return 0;
4618 }
4619 break;
4620 default:
4621 PyErr_SetString(PyExc_SystemError,
4622 "unexpected slice kind");
4623 return 0;
4624 }
4625 return 1;
4626}
4627
4628static int
4629check_ann_subscr(struct compiler *c, slice_ty sl)
4630{
4631 /* We check that everything in a subscript is defined at runtime. */
4632 Py_ssize_t i, n;
4633
4634 switch (sl->kind) {
4635 case Index_kind:
4636 case Slice_kind:
4637 if (!check_ann_slice(c, sl)) {
4638 return 0;
4639 }
4640 break;
4641 case ExtSlice_kind:
4642 n = asdl_seq_LEN(sl->v.ExtSlice.dims);
4643 for (i = 0; i < n; i++) {
4644 slice_ty subsl = (slice_ty)asdl_seq_GET(sl->v.ExtSlice.dims, i);
4645 switch (subsl->kind) {
4646 case Index_kind:
4647 case Slice_kind:
4648 if (!check_ann_slice(c, subsl)) {
4649 return 0;
4650 }
4651 break;
4652 case ExtSlice_kind:
4653 default:
4654 PyErr_SetString(PyExc_SystemError,
4655 "extended slice invalid in nested slice");
4656 return 0;
4657 }
4658 }
4659 break;
4660 default:
4661 PyErr_Format(PyExc_SystemError,
4662 "invalid subscript kind %d", sl->kind);
4663 return 0;
4664 }
4665 return 1;
4666}
4667
4668static int
4669compiler_annassign(struct compiler *c, stmt_ty s)
4670{
4671 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07004672 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07004673
4674 assert(s->kind == AnnAssign_kind);
4675
4676 /* We perform the actual assignment first. */
4677 if (s->v.AnnAssign.value) {
4678 VISIT(c, expr, s->v.AnnAssign.value);
4679 VISIT(c, expr, targ);
4680 }
4681 switch (targ->kind) {
4682 case Name_kind:
4683 /* If we have a simple name in a module or class, store annotation. */
4684 if (s->v.AnnAssign.simple &&
4685 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
4686 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Guido van Rossum015d8742016-09-11 09:45:24 -07004687 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
4688 if (!mangled) {
4689 return 0;
4690 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07004691 VISIT(c, expr, s->v.AnnAssign.annotation);
Guido van Rossum015d8742016-09-11 09:45:24 -07004692 /* ADDOP_N decrefs its argument */
4693 ADDOP_N(c, STORE_ANNOTATION, mangled, names);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07004694 }
4695 break;
4696 case Attribute_kind:
4697 if (!s->v.AnnAssign.value &&
4698 !check_ann_expr(c, targ->v.Attribute.value)) {
4699 return 0;
4700 }
4701 break;
4702 case Subscript_kind:
4703 if (!s->v.AnnAssign.value &&
4704 (!check_ann_expr(c, targ->v.Subscript.value) ||
4705 !check_ann_subscr(c, targ->v.Subscript.slice))) {
4706 return 0;
4707 }
4708 break;
4709 default:
4710 PyErr_Format(PyExc_SystemError,
4711 "invalid node type (%d) for annotated assignment",
4712 targ->kind);
4713 return 0;
4714 }
4715 /* Annotation is evaluated last. */
4716 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
4717 return 0;
4718 }
4719 return 1;
4720}
4721
4722static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004723compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
4724{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004725 struct fblockinfo *f;
4726 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
Benjamin Petersone09ed542016-07-14 22:00:03 -07004727 PyErr_SetString(PyExc_SyntaxError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004728 "too many statically nested blocks");
4729 return 0;
4730 }
4731 f = &c->u->u_fblock[c->u->u_nfblocks++];
4732 f->fb_type = t;
4733 f->fb_block = b;
4734 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004735}
4736
4737static void
4738compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
4739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004740 struct compiler_unit *u = c->u;
4741 assert(u->u_nfblocks > 0);
4742 u->u_nfblocks--;
4743 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
4744 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004745}
4746
Thomas Wouters89f507f2006-12-13 04:49:30 +00004747static int
4748compiler_in_loop(struct compiler *c) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004749 int i;
4750 struct compiler_unit *u = c->u;
4751 for (i = 0; i < u->u_nfblocks; ++i) {
4752 if (u->u_fblock[i].fb_type == LOOP)
4753 return 1;
4754 }
4755 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004756}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004757/* Raises a SyntaxError and returns 0.
4758 If something goes wrong, a different exception may be raised.
4759*/
4760
4761static int
4762compiler_error(struct compiler *c, const char *errstr)
4763{
Benjamin Peterson43b06862011-05-27 09:08:01 -05004764 PyObject *loc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004765 PyObject *u = NULL, *v = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004766
Victor Stinner14e461d2013-08-26 22:28:21 +02004767 loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004768 if (!loc) {
4769 Py_INCREF(Py_None);
4770 loc = Py_None;
4771 }
Victor Stinner14e461d2013-08-26 22:28:21 +02004772 u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00004773 c->u->u_col_offset, loc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004774 if (!u)
4775 goto exit;
4776 v = Py_BuildValue("(zO)", errstr, u);
4777 if (!v)
4778 goto exit;
4779 PyErr_SetObject(PyExc_SyntaxError, v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004780 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004781 Py_DECREF(loc);
4782 Py_XDECREF(u);
4783 Py_XDECREF(v);
4784 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004785}
4786
4787static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004788compiler_handle_subscr(struct compiler *c, const char *kind,
4789 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004790{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004791 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004792
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004793 /* XXX this code is duplicated */
4794 switch (ctx) {
4795 case AugLoad: /* fall through to Load */
4796 case Load: op = BINARY_SUBSCR; break;
4797 case AugStore:/* fall through to Store */
4798 case Store: op = STORE_SUBSCR; break;
4799 case Del: op = DELETE_SUBSCR; break;
4800 case Param:
4801 PyErr_Format(PyExc_SystemError,
4802 "invalid %s kind %d in subscript\n",
4803 kind, ctx);
4804 return 0;
4805 }
4806 if (ctx == AugLoad) {
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00004807 ADDOP(c, DUP_TOP_TWO);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004808 }
4809 else if (ctx == AugStore) {
4810 ADDOP(c, ROT_THREE);
4811 }
4812 ADDOP(c, op);
4813 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004814}
4815
4816static int
4817compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
4818{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004819 int n = 2;
4820 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004822 /* only handles the cases where BUILD_SLICE is emitted */
4823 if (s->v.Slice.lower) {
4824 VISIT(c, expr, s->v.Slice.lower);
4825 }
4826 else {
4827 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4828 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004830 if (s->v.Slice.upper) {
4831 VISIT(c, expr, s->v.Slice.upper);
4832 }
4833 else {
4834 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4835 }
4836
4837 if (s->v.Slice.step) {
4838 n++;
4839 VISIT(c, expr, s->v.Slice.step);
4840 }
4841 ADDOP_I(c, BUILD_SLICE, n);
4842 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004843}
4844
4845static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004846compiler_visit_nested_slice(struct compiler *c, slice_ty s,
4847 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004848{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004849 switch (s->kind) {
4850 case Slice_kind:
4851 return compiler_slice(c, s, ctx);
4852 case Index_kind:
4853 VISIT(c, expr, s->v.Index.value);
4854 break;
4855 case ExtSlice_kind:
4856 default:
4857 PyErr_SetString(PyExc_SystemError,
4858 "extended slice invalid in nested slice");
4859 return 0;
4860 }
4861 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004862}
4863
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004864static int
4865compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
4866{
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02004867 const char * kindname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004868 switch (s->kind) {
4869 case Index_kind:
4870 kindname = "index";
4871 if (ctx != AugStore) {
4872 VISIT(c, expr, s->v.Index.value);
4873 }
4874 break;
4875 case Slice_kind:
4876 kindname = "slice";
4877 if (ctx != AugStore) {
4878 if (!compiler_slice(c, s, ctx))
4879 return 0;
4880 }
4881 break;
4882 case ExtSlice_kind:
4883 kindname = "extended slice";
4884 if (ctx != AugStore) {
Victor Stinnerad9a0662013-11-19 22:23:20 +01004885 Py_ssize_t i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004886 for (i = 0; i < n; i++) {
4887 slice_ty sub = (slice_ty)asdl_seq_GET(
4888 s->v.ExtSlice.dims, i);
4889 if (!compiler_visit_nested_slice(c, sub, ctx))
4890 return 0;
4891 }
4892 ADDOP_I(c, BUILD_TUPLE, n);
4893 }
4894 break;
4895 default:
4896 PyErr_Format(PyExc_SystemError,
4897 "invalid subscript kind %d", s->kind);
4898 return 0;
4899 }
4900 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004901}
4902
Thomas Wouters89f507f2006-12-13 04:49:30 +00004903/* End of the compiler section, beginning of the assembler section */
4904
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004905/* do depth-first search of basic block graph, starting with block.
4906 post records the block indices in post-order.
4907
4908 XXX must handle implicit jumps from one block to next
4909*/
4910
Thomas Wouters89f507f2006-12-13 04:49:30 +00004911struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004912 PyObject *a_bytecode; /* string containing bytecode */
4913 int a_offset; /* offset into bytecode */
4914 int a_nblocks; /* number of reachable blocks */
4915 basicblock **a_postorder; /* list of blocks in dfs postorder */
4916 PyObject *a_lnotab; /* string containing lnotab */
4917 int a_lnotab_off; /* offset into lnotab */
4918 int a_lineno; /* last lineno of emitted instruction */
4919 int a_lineno_off; /* bytecode offset of last lineno */
Thomas Wouters89f507f2006-12-13 04:49:30 +00004920};
4921
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004922static void
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02004923dfs(struct compiler *c, basicblock *b, struct assembler *a, int end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004924{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02004925 int i, j;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004926
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02004927 /* Get rid of recursion for normal control flow.
4928 Since the number of blocks is limited, unused space in a_postorder
4929 (from a_nblocks to end) can be used as a stack for still not ordered
4930 blocks. */
4931 for (j = end; b && !b->b_seen; b = b->b_next) {
4932 b->b_seen = 1;
4933 assert(a->a_nblocks < j);
4934 a->a_postorder[--j] = b;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004935 }
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02004936 while (j < end) {
4937 b = a->a_postorder[j++];
4938 for (i = 0; i < b->b_iused; i++) {
4939 struct instr *instr = &b->b_instr[i];
4940 if (instr->i_jrel || instr->i_jabs)
4941 dfs(c, instr->i_target, a, j);
4942 }
4943 assert(a->a_nblocks < j);
4944 a->a_postorder[a->a_nblocks++] = b;
4945 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004946}
4947
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02004948Py_LOCAL_INLINE(void)
4949stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004950{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02004951 /* XXX b->b_startdepth > depth only for the target of SETUP_FINALLY,
4952 * SETUP_WITH and SETUP_ASYNC_WITH. */
4953 assert(b->b_startdepth < 0 || b->b_startdepth >= depth);
4954 if (b->b_startdepth < depth) {
4955 assert(b->b_startdepth < 0);
4956 b->b_startdepth = depth;
4957 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02004958 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004959}
4960
4961/* Find the flow path that needs the largest stack. We assume that
4962 * cycles in the flow graph have no net effect on the stack depth.
4963 */
4964static int
4965stackdepth(struct compiler *c)
4966{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02004967 basicblock *b, *entryblock = NULL;
4968 basicblock **stack, **sp;
4969 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004970 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004971 b->b_startdepth = INT_MIN;
4972 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02004973 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004974 }
4975 if (!entryblock)
4976 return 0;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02004977 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
4978 if (!stack) {
4979 PyErr_NoMemory();
4980 return -1;
4981 }
4982
4983 sp = stack;
4984 stackdepth_push(&sp, entryblock, 0);
4985 while (sp != stack) {
4986 b = *--sp;
4987 int depth = b->b_startdepth;
4988 assert(depth >= 0);
4989 basicblock *next = b->b_next;
4990 for (int i = 0; i < b->b_iused; i++) {
4991 struct instr *instr = &b->b_instr[i];
4992 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
4993 if (effect == PY_INVALID_STACK_EFFECT) {
4994 fprintf(stderr, "opcode = %d\n", instr->i_opcode);
4995 Py_FatalError("PyCompile_OpcodeStackEffect()");
4996 }
4997 int new_depth = depth + effect;
4998 if (new_depth > maxdepth) {
4999 maxdepth = new_depth;
5000 }
5001 assert(depth >= 0); /* invalid code or bug in stackdepth() */
5002 if (instr->i_jrel || instr->i_jabs) {
5003 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
5004 assert(effect != PY_INVALID_STACK_EFFECT);
5005 int target_depth = depth + effect;
5006 if (target_depth > maxdepth) {
5007 maxdepth = target_depth;
5008 }
5009 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
5010 if (instr->i_opcode == CONTINUE_LOOP) {
5011 /* Pops a variable number of values from the stack,
5012 * but the target should be already proceeding.
5013 */
5014 assert(instr->i_target->b_startdepth >= 0);
5015 assert(instr->i_target->b_startdepth <= depth);
5016 /* remaining code is dead */
5017 next = NULL;
5018 break;
5019 }
5020 stackdepth_push(&sp, instr->i_target, target_depth);
5021 }
5022 depth = new_depth;
5023 if (instr->i_opcode == JUMP_ABSOLUTE ||
5024 instr->i_opcode == JUMP_FORWARD ||
5025 instr->i_opcode == RETURN_VALUE ||
5026 instr->i_opcode == RAISE_VARARGS ||
5027 instr->i_opcode == BREAK_LOOP)
5028 {
5029 /* remaining code is dead */
5030 next = NULL;
5031 break;
5032 }
5033 }
5034 if (next != NULL) {
5035 stackdepth_push(&sp, next, depth);
5036 }
5037 }
5038 PyObject_Free(stack);
5039 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005040}
5041
5042static int
5043assemble_init(struct assembler *a, int nblocks, int firstlineno)
5044{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005045 memset(a, 0, sizeof(struct assembler));
5046 a->a_lineno = firstlineno;
5047 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
5048 if (!a->a_bytecode)
5049 return 0;
5050 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
5051 if (!a->a_lnotab)
5052 return 0;
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07005053 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005054 PyErr_NoMemory();
5055 return 0;
5056 }
5057 a->a_postorder = (basicblock **)PyObject_Malloc(
5058 sizeof(basicblock *) * nblocks);
5059 if (!a->a_postorder) {
5060 PyErr_NoMemory();
5061 return 0;
5062 }
5063 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005064}
5065
5066static void
5067assemble_free(struct assembler *a)
5068{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005069 Py_XDECREF(a->a_bytecode);
5070 Py_XDECREF(a->a_lnotab);
5071 if (a->a_postorder)
5072 PyObject_Free(a->a_postorder);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005073}
5074
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005075static int
5076blocksize(basicblock *b)
5077{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005078 int i;
5079 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005081 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005082 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005083 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005084}
5085
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00005086/* Appends a pair to the end of the line number table, a_lnotab, representing
5087 the instruction's bytecode offset and line number. See
5088 Objects/lnotab_notes.txt for the description of the line number table. */
Tim Peters2a7f3842001-06-09 09:26:21 +00005089
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005090static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005091assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005092{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005093 int d_bytecode, d_lineno;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005094 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005095 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005096
Serhiy Storchakaab874002016-09-11 13:48:15 +03005097 d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005098 d_lineno = i->i_lineno - a->a_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005100 assert(d_bytecode >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005102 if(d_bytecode == 0 && d_lineno == 0)
5103 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00005104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005105 if (d_bytecode > 255) {
5106 int j, nbytes, ncodes = d_bytecode / 255;
5107 nbytes = a->a_lnotab_off + 2 * ncodes;
5108 len = PyBytes_GET_SIZE(a->a_lnotab);
5109 if (nbytes >= len) {
5110 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
5111 len = nbytes;
5112 else if (len <= INT_MAX / 2)
5113 len *= 2;
5114 else {
5115 PyErr_NoMemory();
5116 return 0;
5117 }
5118 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
5119 return 0;
5120 }
5121 lnotab = (unsigned char *)
5122 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
5123 for (j = 0; j < ncodes; j++) {
5124 *lnotab++ = 255;
5125 *lnotab++ = 0;
5126 }
5127 d_bytecode -= ncodes * 255;
5128 a->a_lnotab_off += ncodes * 2;
5129 }
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005130 assert(0 <= d_bytecode && d_bytecode <= 255);
5131
5132 if (d_lineno < -128 || 127 < d_lineno) {
5133 int j, nbytes, ncodes, k;
5134 if (d_lineno < 0) {
5135 k = -128;
5136 /* use division on positive numbers */
5137 ncodes = (-d_lineno) / 128;
5138 }
5139 else {
5140 k = 127;
5141 ncodes = d_lineno / 127;
5142 }
5143 d_lineno -= ncodes * k;
5144 assert(ncodes >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005145 nbytes = a->a_lnotab_off + 2 * ncodes;
5146 len = PyBytes_GET_SIZE(a->a_lnotab);
5147 if (nbytes >= len) {
5148 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
5149 len = nbytes;
5150 else if (len <= INT_MAX / 2)
5151 len *= 2;
5152 else {
5153 PyErr_NoMemory();
5154 return 0;
5155 }
5156 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
5157 return 0;
5158 }
5159 lnotab = (unsigned char *)
5160 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
5161 *lnotab++ = d_bytecode;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005162 *lnotab++ = k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005163 d_bytecode = 0;
5164 for (j = 1; j < ncodes; j++) {
5165 *lnotab++ = 0;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005166 *lnotab++ = k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005167 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005168 a->a_lnotab_off += ncodes * 2;
5169 }
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005170 assert(-128 <= d_lineno && d_lineno <= 127);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005172 len = PyBytes_GET_SIZE(a->a_lnotab);
5173 if (a->a_lnotab_off + 2 >= len) {
5174 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
5175 return 0;
5176 }
5177 lnotab = (unsigned char *)
5178 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00005179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005180 a->a_lnotab_off += 2;
5181 if (d_bytecode) {
5182 *lnotab++ = d_bytecode;
Victor Stinner4f2dab52011-05-27 16:46:51 +02005183 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005184 }
5185 else { /* First line of a block; def stmt, etc. */
5186 *lnotab++ = 0;
Victor Stinner4f2dab52011-05-27 16:46:51 +02005187 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005188 }
5189 a->a_lineno = i->i_lineno;
5190 a->a_lineno_off = a->a_offset;
5191 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005192}
5193
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005194/* assemble_emit()
5195 Extend the bytecode with a new instruction.
5196 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00005197*/
5198
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00005199static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005200assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00005201{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005202 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005203 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03005204 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005205
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005206 arg = i->i_oparg;
5207 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005208 if (i->i_lineno && !assemble_lnotab(a, i))
5209 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005210 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005211 if (len > PY_SSIZE_T_MAX / 2)
5212 return 0;
5213 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
5214 return 0;
5215 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03005216 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005217 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005218 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005219 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00005220}
5221
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00005222static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005223assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00005224{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005225 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005226 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005227 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00005228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005229 /* Compute the size of each block and fixup jump args.
5230 Replace block pointer with position in bytecode. */
5231 do {
5232 totsize = 0;
5233 for (i = a->a_nblocks - 1; i >= 0; i--) {
5234 b = a->a_postorder[i];
5235 bsize = blocksize(b);
5236 b->b_offset = totsize;
5237 totsize += bsize;
5238 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005239 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005240 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
5241 bsize = b->b_offset;
5242 for (i = 0; i < b->b_iused; i++) {
5243 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005244 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005245 /* Relative jumps are computed relative to
5246 the instruction pointer after fetching
5247 the jump instruction.
5248 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005249 bsize += isize;
5250 if (instr->i_jabs || instr->i_jrel) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005251 instr->i_oparg = instr->i_target->b_offset;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005252 if (instr->i_jrel) {
5253 instr->i_oparg -= bsize;
5254 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03005255 instr->i_oparg *= sizeof(_Py_CODEUNIT);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005256 if (instrsize(instr->i_oparg) != isize) {
5257 extended_arg_recompile = 1;
5258 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005259 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005260 }
5261 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00005262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005263 /* XXX: This is an awful hack that could hurt performance, but
5264 on the bright side it should work until we come up
5265 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00005266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005267 The issue is that in the first loop blocksize() is called
5268 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005269 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005270 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00005271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005272 So we loop until we stop seeing new EXTENDED_ARGs.
5273 The only EXTENDED_ARGs that could be popping up are
5274 ones in jump instructions. So this should converge
5275 fairly quickly.
5276 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005277 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005278}
5279
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005280static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01005281dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005283 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02005284 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005286 tuple = PyTuple_New(size);
5287 if (tuple == NULL)
5288 return NULL;
5289 while (PyDict_Next(dict, &pos, &k, &v)) {
5290 i = PyLong_AS_LONG(v);
Victor Stinnerefb24132016-01-22 12:33:12 +01005291 /* The keys of the dictionary are tuples. (see compiler_add_o
5292 * and _PyCode_ConstantKey). The object we want is always second,
5293 * though. */
5294 k = PyTuple_GET_ITEM(k, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005295 Py_INCREF(k);
5296 assert((i - offset) < size);
5297 assert((i - offset) >= 0);
5298 PyTuple_SET_ITEM(tuple, i - offset, k);
5299 }
5300 return tuple;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005301}
5302
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005303static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005304compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005306 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005307 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005308 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04005309 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005310 if (ste->ste_nested)
5311 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07005312 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005313 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07005314 if (!ste->ste_generator && ste->ste_coroutine)
5315 flags |= CO_COROUTINE;
5316 if (ste->ste_generator && ste->ste_coroutine)
5317 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005318 if (ste->ste_varargs)
5319 flags |= CO_VARARGS;
5320 if (ste->ste_varkeywords)
5321 flags |= CO_VARKEYWORDS;
5322 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00005323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005324 /* (Only) inherit compilerflags in PyCF_MASK */
5325 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00005326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005327 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005328}
5329
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005330static PyCodeObject *
5331makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005333 PyObject *tmp;
5334 PyCodeObject *co = NULL;
5335 PyObject *consts = NULL;
5336 PyObject *names = NULL;
5337 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005338 PyObject *name = NULL;
5339 PyObject *freevars = NULL;
5340 PyObject *cellvars = NULL;
5341 PyObject *bytecode = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005342 Py_ssize_t nlocals;
5343 int nlocals_int;
5344 int flags;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005345 int argcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005347 tmp = dict_keys_inorder(c->u->u_consts, 0);
5348 if (!tmp)
5349 goto error;
5350 consts = PySequence_List(tmp); /* optimize_code requires a list */
5351 Py_DECREF(tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005353 names = dict_keys_inorder(c->u->u_names, 0);
5354 varnames = dict_keys_inorder(c->u->u_varnames, 0);
5355 if (!consts || !names || !varnames)
5356 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005358 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
5359 if (!cellvars)
5360 goto error;
5361 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
5362 if (!freevars)
5363 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005364
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02005365 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01005366 assert(nlocals < INT_MAX);
5367 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
5368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005369 flags = compute_code_flags(c);
5370 if (flags < 0)
5371 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005373 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
5374 if (!bytecode)
5375 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005377 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
5378 if (!tmp)
5379 goto error;
5380 Py_DECREF(consts);
5381 consts = tmp;
5382
Victor Stinnerf8e32212013-11-19 23:56:34 +01005383 argcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
5384 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005385 maxdepth = stackdepth(c);
5386 if (maxdepth < 0) {
5387 goto error;
5388 }
Victor Stinnerf8e32212013-11-19 23:56:34 +01005389 co = PyCode_New(argcount, kwonlyargcount,
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005390 nlocals_int, maxdepth, flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005391 bytecode, consts, names, varnames,
5392 freevars, cellvars,
Victor Stinner14e461d2013-08-26 22:28:21 +02005393 c->c_filename, c->u->u_name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005394 c->u->u_firstlineno,
5395 a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005396 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005397 Py_XDECREF(consts);
5398 Py_XDECREF(names);
5399 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005400 Py_XDECREF(name);
5401 Py_XDECREF(freevars);
5402 Py_XDECREF(cellvars);
5403 Py_XDECREF(bytecode);
5404 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005405}
5406
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005407
5408/* For debugging purposes only */
5409#if 0
5410static void
5411dump_instr(const struct instr *i)
5412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005413 const char *jrel = i->i_jrel ? "jrel " : "";
5414 const char *jabs = i->i_jabs ? "jabs " : "";
5415 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005417 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005418 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005419 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005420 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005421 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
5422 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005423}
5424
5425static void
5426dump_basicblock(const basicblock *b)
5427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005428 const char *seen = b->b_seen ? "seen " : "";
5429 const char *b_return = b->b_return ? "return " : "";
5430 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
5431 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
5432 if (b->b_instr) {
5433 int i;
5434 for (i = 0; i < b->b_iused; i++) {
5435 fprintf(stderr, " [%02d] ", i);
5436 dump_instr(b->b_instr + i);
5437 }
5438 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005439}
5440#endif
5441
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005442static PyCodeObject *
5443assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005445 basicblock *b, *entryblock;
5446 struct assembler a;
5447 int i, j, nblocks;
5448 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005450 /* Make sure every block that falls off the end returns None.
5451 XXX NEXT_BLOCK() isn't quite right, because if the last
5452 block ends with a jump or return b_next shouldn't set.
5453 */
5454 if (!c->u->u_curblock->b_return) {
5455 NEXT_BLOCK(c);
5456 if (addNone)
5457 ADDOP_O(c, LOAD_CONST, Py_None, consts);
5458 ADDOP(c, RETURN_VALUE);
5459 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005461 nblocks = 0;
5462 entryblock = NULL;
5463 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
5464 nblocks++;
5465 entryblock = b;
5466 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005468 /* Set firstlineno if it wasn't explicitly set. */
5469 if (!c->u->u_firstlineno) {
Ned Deilydc35cda2016-08-17 17:18:33 -04005470 if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005471 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
5472 else
5473 c->u->u_firstlineno = 1;
5474 }
5475 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
5476 goto error;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005477 dfs(c, entryblock, &a, nblocks);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005479 /* Can't modify the bytecode after computing jump offsets. */
5480 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00005481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005482 /* Emit code in reverse postorder from dfs. */
5483 for (i = a.a_nblocks - 1; i >= 0; i--) {
5484 b = a.a_postorder[i];
5485 for (j = 0; j < b->b_iused; j++)
5486 if (!assemble_emit(&a, &b->b_instr[j]))
5487 goto error;
5488 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00005489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005490 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
5491 goto error;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005492 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005493 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005495 co = makecode(c, &a);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005496 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005497 assemble_free(&a);
5498 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005499}
Georg Brandl8334fd92010-12-04 10:26:46 +00005500
5501#undef PyAST_Compile
5502PyAPI_FUNC(PyCodeObject *)
5503PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
5504 PyArena *arena)
5505{
5506 return PyAST_CompileEx(mod, filename, flags, -1, arena);
5507}