blob: b459096c6f00c61ec9b8415f9fcfe86dc2b9f657 [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);
Mark Shannon332cd5e2018-01-30 00:41:04 +0000216static PyObject *__doc__, *__annotations__;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000217
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 }
Mark Shannon332cd5e2018-01-30 00:41:04 +0000314 if (!__annotations__) {
315 __annotations__ = PyUnicode_InternFromString("__annotations__");
316 if (!__annotations__)
317 return NULL;
318 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 if (!compiler_init(&c))
320 return NULL;
Victor Stinner14e461d2013-08-26 22:28:21 +0200321 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 c.c_filename = filename;
323 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200324 c.c_future = PyFuture_FromASTObject(mod, filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 if (c.c_future == NULL)
326 goto finally;
327 if (!flags) {
328 local_flags.cf_flags = 0;
329 flags = &local_flags;
330 }
331 merged = c.c_future->ff_features | flags->cf_flags;
332 c.c_future->ff_features = merged;
333 flags->cf_flags = merged;
334 c.c_flags = flags;
Georg Brandl8334fd92010-12-04 10:26:46 +0000335 c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000337
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +0200338 if (!_PyAST_Optimize(mod, arena, c.c_optimize)) {
INADA Naoki7ea143a2017-12-14 16:47:20 +0900339 goto finally;
340 }
341
Victor Stinner14e461d2013-08-26 22:28:21 +0200342 c.c_st = PySymtable_BuildObject(mod, filename, c.c_future);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 if (c.c_st == NULL) {
344 if (!PyErr_Occurred())
345 PyErr_SetString(PyExc_SystemError, "no symtable");
346 goto finally;
347 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000350
Thomas Wouters1175c432006-02-27 22:49:54 +0000351 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 compiler_free(&c);
353 assert(co || PyErr_Occurred());
354 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000355}
356
357PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200358PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags,
359 int optimize, PyArena *arena)
360{
361 PyObject *filename;
362 PyCodeObject *co;
363 filename = PyUnicode_DecodeFSDefault(filename_str);
364 if (filename == NULL)
365 return NULL;
366 co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
367 Py_DECREF(filename);
368 return co;
369
370}
371
372PyCodeObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000373PyNode_Compile(struct _node *n, const char *filename)
374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 PyCodeObject *co = NULL;
376 mod_ty mod;
377 PyArena *arena = PyArena_New();
378 if (!arena)
379 return NULL;
380 mod = PyAST_FromNode(n, NULL, filename, arena);
381 if (mod)
382 co = PyAST_Compile(mod, filename, NULL, arena);
383 PyArena_Free(arena);
384 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000385}
386
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000387static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000388compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000389{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 if (c->c_st)
391 PySymtable_Free(c->c_st);
392 if (c->c_future)
393 PyObject_Free(c->c_future);
Victor Stinner14e461d2013-08-26 22:28:21 +0200394 Py_XDECREF(c->c_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000396}
397
Guido van Rossum79f25d91997-04-29 20:08:16 +0000398static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000399list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 Py_ssize_t i, n;
402 PyObject *v, *k;
403 PyObject *dict = PyDict_New();
404 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 n = PyList_Size(list);
407 for (i = 0; i < n; i++) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100408 v = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 if (!v) {
410 Py_DECREF(dict);
411 return NULL;
412 }
413 k = PyList_GET_ITEM(list, i);
Victor Stinnerefb24132016-01-22 12:33:12 +0100414 k = _PyCode_ConstantKey(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
416 Py_XDECREF(k);
417 Py_DECREF(v);
418 Py_DECREF(dict);
419 return NULL;
420 }
421 Py_DECREF(k);
422 Py_DECREF(v);
423 }
424 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000425}
426
427/* Return new dict containing names from src that match scope(s).
428
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000429src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000431values are integers, starting at offset and increasing by one for
432each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000433*/
434
435static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +0100436dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000437{
Benjamin Peterson51ab2832012-07-18 15:12:47 -0700438 Py_ssize_t i = offset, scope, num_keys, key_i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 PyObject *k, *v, *dest = PyDict_New();
Meador Inge2ca63152012-07-18 14:20:11 -0500440 PyObject *sorted_keys;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 assert(offset >= 0);
443 if (dest == NULL)
444 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000445
Meador Inge2ca63152012-07-18 14:20:11 -0500446 /* Sort the keys so that we have a deterministic order on the indexes
447 saved in the returned dictionary. These indexes are used as indexes
448 into the free and cell var storage. Therefore if they aren't
449 deterministic, then the generated bytecode is not deterministic.
450 */
451 sorted_keys = PyDict_Keys(src);
452 if (sorted_keys == NULL)
453 return NULL;
454 if (PyList_Sort(sorted_keys) != 0) {
455 Py_DECREF(sorted_keys);
456 return NULL;
457 }
Meador Ingef69e24e2012-07-18 16:41:03 -0500458 num_keys = PyList_GET_SIZE(sorted_keys);
Meador Inge2ca63152012-07-18 14:20:11 -0500459
460 for (key_i = 0; key_i < num_keys; key_i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 /* XXX this should probably be a macro in symtable.h */
462 long vi;
Meador Inge2ca63152012-07-18 14:20:11 -0500463 k = PyList_GET_ITEM(sorted_keys, key_i);
464 v = PyDict_GetItem(src, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 assert(PyLong_Check(v));
466 vi = PyLong_AS_LONG(v);
467 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 if (scope == scope_type || vi & flag) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100470 PyObject *tuple, *item = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 if (item == NULL) {
Meador Inge2ca63152012-07-18 14:20:11 -0500472 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 Py_DECREF(dest);
474 return NULL;
475 }
476 i++;
Victor Stinnerefb24132016-01-22 12:33:12 +0100477 tuple = _PyCode_ConstantKey(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
Meador Inge2ca63152012-07-18 14:20:11 -0500479 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 Py_DECREF(item);
481 Py_DECREF(dest);
482 Py_XDECREF(tuple);
483 return NULL;
484 }
485 Py_DECREF(item);
486 Py_DECREF(tuple);
487 }
488 }
Meador Inge2ca63152012-07-18 14:20:11 -0500489 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000491}
492
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000493static void
494compiler_unit_check(struct compiler_unit *u)
495{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 basicblock *block;
497 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Benjamin Petersonca470632016-09-06 13:47:26 -0700498 assert((uintptr_t)block != 0xcbcbcbcbU);
499 assert((uintptr_t)block != 0xfbfbfbfbU);
500 assert((uintptr_t)block != 0xdbdbdbdbU);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 if (block->b_instr != NULL) {
502 assert(block->b_ialloc > 0);
503 assert(block->b_iused > 0);
504 assert(block->b_ialloc >= block->b_iused);
505 }
506 else {
507 assert (block->b_iused == 0);
508 assert (block->b_ialloc == 0);
509 }
510 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000511}
512
513static void
514compiler_unit_free(struct compiler_unit *u)
515{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 compiler_unit_check(u);
519 b = u->u_blocks;
520 while (b != NULL) {
521 if (b->b_instr)
522 PyObject_Free((void *)b->b_instr);
523 next = b->b_list;
524 PyObject_Free((void *)b);
525 b = next;
526 }
527 Py_CLEAR(u->u_ste);
528 Py_CLEAR(u->u_name);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400529 Py_CLEAR(u->u_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 Py_CLEAR(u->u_consts);
531 Py_CLEAR(u->u_names);
532 Py_CLEAR(u->u_varnames);
533 Py_CLEAR(u->u_freevars);
534 Py_CLEAR(u->u_cellvars);
535 Py_CLEAR(u->u_private);
536 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000537}
538
539static int
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100540compiler_enter_scope(struct compiler *c, identifier name,
541 int scope_type, void *key, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000542{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 struct compiler_unit *u;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100544 basicblock *block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
547 struct compiler_unit));
548 if (!u) {
549 PyErr_NoMemory();
550 return 0;
551 }
552 memset(u, 0, sizeof(struct compiler_unit));
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100553 u->u_scope_type = scope_type;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 u->u_argcount = 0;
555 u->u_kwonlyargcount = 0;
556 u->u_ste = PySymtable_Lookup(c->c_st, key);
557 if (!u->u_ste) {
558 compiler_unit_free(u);
559 return 0;
560 }
561 Py_INCREF(name);
562 u->u_name = name;
563 u->u_varnames = list2dict(u->u_ste->ste_varnames);
564 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
565 if (!u->u_varnames || !u->u_cellvars) {
566 compiler_unit_free(u);
567 return 0;
568 }
Benjamin Peterson312595c2013-05-15 15:26:42 -0500569 if (u->u_ste->ste_needs_class_closure) {
Martin Panter7462b6492015-11-02 03:37:02 +0000570 /* Cook up an implicit __class__ cell. */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500571 _Py_IDENTIFIER(__class__);
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300572 PyObject *tuple, *name;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500573 int res;
574 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200575 assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500576 name = _PyUnicode_FromId(&PyId___class__);
577 if (!name) {
578 compiler_unit_free(u);
579 return 0;
580 }
Victor Stinnerefb24132016-01-22 12:33:12 +0100581 tuple = _PyCode_ConstantKey(name);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500582 if (!tuple) {
583 compiler_unit_free(u);
584 return 0;
585 }
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300586 res = PyDict_SetItem(u->u_cellvars, tuple, _PyLong_Zero);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500587 Py_DECREF(tuple);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500588 if (res < 0) {
589 compiler_unit_free(u);
590 return 0;
591 }
592 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200595 PyDict_GET_SIZE(u->u_cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 if (!u->u_freevars) {
597 compiler_unit_free(u);
598 return 0;
599 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 u->u_blocks = NULL;
602 u->u_nfblocks = 0;
603 u->u_firstlineno = lineno;
604 u->u_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000605 u->u_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 u->u_lineno_set = 0;
607 u->u_consts = PyDict_New();
608 if (!u->u_consts) {
609 compiler_unit_free(u);
610 return 0;
611 }
612 u->u_names = PyDict_New();
613 if (!u->u_names) {
614 compiler_unit_free(u);
615 return 0;
616 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 /* Push the old compiler_unit on the stack. */
621 if (c->u) {
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400622 PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
624 Py_XDECREF(capsule);
625 compiler_unit_free(u);
626 return 0;
627 }
628 Py_DECREF(capsule);
629 u->u_private = c->u->u_private;
630 Py_XINCREF(u->u_private);
631 }
632 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 c->c_nestlevel++;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100635
636 block = compiler_new_block(c);
637 if (block == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 return 0;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100639 c->u->u_curblock = block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000640
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400641 if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
642 if (!compiler_set_qualname(c))
643 return 0;
644 }
645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000647}
648
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000649static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650compiler_exit_scope(struct compiler *c)
651{
Victor Stinnerad9a0662013-11-19 22:23:20 +0100652 Py_ssize_t n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 PyObject *capsule;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 c->c_nestlevel--;
656 compiler_unit_free(c->u);
657 /* Restore c->u to the parent unit. */
658 n = PyList_GET_SIZE(c->c_stack) - 1;
659 if (n >= 0) {
660 capsule = PyList_GET_ITEM(c->c_stack, n);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400661 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 assert(c->u);
663 /* we are deleting from a list so this really shouldn't fail */
664 if (PySequence_DelItem(c->c_stack, n) < 0)
665 Py_FatalError("compiler_exit_scope()");
666 compiler_unit_check(c->u);
667 }
668 else
669 c->u = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000670
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000671}
672
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400673static int
674compiler_set_qualname(struct compiler *c)
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100675{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100676 _Py_static_string(dot, ".");
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400677 _Py_static_string(dot_locals, ".<locals>");
678 Py_ssize_t stack_size;
679 struct compiler_unit *u = c->u;
680 PyObject *name, *base, *dot_str, *dot_locals_str;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100681
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400682 base = NULL;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100683 stack_size = PyList_GET_SIZE(c->c_stack);
Benjamin Petersona8a38b82013-10-19 16:14:39 -0400684 assert(stack_size >= 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400685 if (stack_size > 1) {
686 int scope, force_global = 0;
687 struct compiler_unit *parent;
688 PyObject *mangled, *capsule;
689
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400690 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400691 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400692 assert(parent);
693
Yury Selivanov75445082015-05-11 22:57:16 -0400694 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
695 || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
696 || u->u_scope_type == COMPILER_SCOPE_CLASS) {
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400697 assert(u->u_name);
698 mangled = _Py_Mangle(parent->u_private, u->u_name);
699 if (!mangled)
700 return 0;
701 scope = PyST_GetScope(parent->u_ste, mangled);
702 Py_DECREF(mangled);
703 assert(scope != GLOBAL_IMPLICIT);
704 if (scope == GLOBAL_EXPLICIT)
705 force_global = 1;
706 }
707
708 if (!force_global) {
709 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
Yury Selivanov75445082015-05-11 22:57:16 -0400710 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400711 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
712 dot_locals_str = _PyUnicode_FromId(&dot_locals);
713 if (dot_locals_str == NULL)
714 return 0;
715 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
716 if (base == NULL)
717 return 0;
718 }
719 else {
720 Py_INCREF(parent->u_qualname);
721 base = parent->u_qualname;
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400722 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100723 }
724 }
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400725
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400726 if (base != NULL) {
727 dot_str = _PyUnicode_FromId(&dot);
728 if (dot_str == NULL) {
729 Py_DECREF(base);
730 return 0;
731 }
732 name = PyUnicode_Concat(base, dot_str);
733 Py_DECREF(base);
734 if (name == NULL)
735 return 0;
736 PyUnicode_Append(&name, u->u_name);
737 if (name == NULL)
738 return 0;
739 }
740 else {
741 Py_INCREF(u->u_name);
742 name = u->u_name;
743 }
744 u->u_qualname = name;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100745
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400746 return 1;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100747}
748
Eric V. Smith235a6f02015-09-19 14:51:32 -0400749
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000750/* Allocate a new block and return a pointer to it.
751 Returns NULL on error.
752*/
753
754static basicblock *
755compiler_new_block(struct compiler *c)
756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 basicblock *b;
758 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 u = c->u;
761 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
762 if (b == NULL) {
763 PyErr_NoMemory();
764 return NULL;
765 }
766 memset((void *)b, 0, sizeof(basicblock));
767 /* Extend the singly linked list of blocks with new block. */
768 b->b_list = u->u_blocks;
769 u->u_blocks = b;
770 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771}
772
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773static basicblock *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774compiler_next_block(struct compiler *c)
775{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 basicblock *block = compiler_new_block(c);
777 if (block == NULL)
778 return NULL;
779 c->u->u_curblock->b_next = block;
780 c->u->u_curblock = block;
781 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782}
783
784static basicblock *
785compiler_use_next_block(struct compiler *c, basicblock *block)
786{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 assert(block != NULL);
788 c->u->u_curblock->b_next = block;
789 c->u->u_curblock = block;
790 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791}
792
793/* Returns the offset of the next instruction in the current block's
794 b_instr array. Resizes the b_instr as necessary.
795 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000796*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797
798static int
799compiler_next_instr(struct compiler *c, basicblock *b)
800{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 assert(b != NULL);
802 if (b->b_instr == NULL) {
803 b->b_instr = (struct instr *)PyObject_Malloc(
804 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
805 if (b->b_instr == NULL) {
806 PyErr_NoMemory();
807 return -1;
808 }
809 b->b_ialloc = DEFAULT_BLOCK_SIZE;
810 memset((char *)b->b_instr, 0,
811 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
812 }
813 else if (b->b_iused == b->b_ialloc) {
814 struct instr *tmp;
815 size_t oldsize, newsize;
816 oldsize = b->b_ialloc * sizeof(struct instr);
817 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000818
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -0700819 if (oldsize > (SIZE_MAX >> 1)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 PyErr_NoMemory();
821 return -1;
822 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 if (newsize == 0) {
825 PyErr_NoMemory();
826 return -1;
827 }
828 b->b_ialloc <<= 1;
829 tmp = (struct instr *)PyObject_Realloc(
830 (void *)b->b_instr, newsize);
831 if (tmp == NULL) {
832 PyErr_NoMemory();
833 return -1;
834 }
835 b->b_instr = tmp;
836 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
837 }
838 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839}
840
Christian Heimes2202f872008-02-06 14:31:34 +0000841/* Set the i_lineno member of the instruction at offset off if the
842 line number for the current expression/statement has not
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000843 already been set. If it has been set, the call has no effect.
844
Christian Heimes2202f872008-02-06 14:31:34 +0000845 The line number is reset in the following cases:
846 - when entering a new scope
847 - on each statement
848 - on each expression that start a new line
849 - before the "except" clause
850 - before the "for" and "while" expressions
Thomas Wouters89f507f2006-12-13 04:49:30 +0000851*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000852
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853static void
854compiler_set_lineno(struct compiler *c, int off)
855{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 basicblock *b;
857 if (c->u->u_lineno_set)
858 return;
859 c->u->u_lineno_set = 1;
860 b = c->u->u_curblock;
861 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000862}
863
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200864/* Return the stack effect of opcode with argument oparg.
865
866 Some opcodes have different stack effect when jump to the target and
867 when not jump. The 'jump' parameter specifies the case:
868
869 * 0 -- when not jump
870 * 1 -- when jump
871 * -1 -- maximal
872 */
873/* XXX Make the stack effect of WITH_CLEANUP_START and
874 WITH_CLEANUP_FINISH deterministic. */
875static int
876stack_effect(int opcode, int oparg, int jump)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000877{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 switch (opcode) {
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200879 /* Stack manipulation */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 case POP_TOP:
881 return -1;
882 case ROT_TWO:
883 case ROT_THREE:
884 return 0;
885 case DUP_TOP:
886 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000887 case DUP_TOP_TWO:
888 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000889
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200890 /* Unary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 case UNARY_POSITIVE:
892 case UNARY_NEGATIVE:
893 case UNARY_NOT:
894 case UNARY_INVERT:
895 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 case SET_ADD:
898 case LIST_APPEND:
899 return -1;
900 case MAP_ADD:
901 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000902
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200903 /* Binary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 case BINARY_POWER:
905 case BINARY_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400906 case BINARY_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 case BINARY_MODULO:
908 case BINARY_ADD:
909 case BINARY_SUBTRACT:
910 case BINARY_SUBSCR:
911 case BINARY_FLOOR_DIVIDE:
912 case BINARY_TRUE_DIVIDE:
913 return -1;
914 case INPLACE_FLOOR_DIVIDE:
915 case INPLACE_TRUE_DIVIDE:
916 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 case INPLACE_ADD:
919 case INPLACE_SUBTRACT:
920 case INPLACE_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400921 case INPLACE_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 case INPLACE_MODULO:
923 return -1;
924 case STORE_SUBSCR:
925 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 case DELETE_SUBSCR:
927 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 case BINARY_LSHIFT:
930 case BINARY_RSHIFT:
931 case BINARY_AND:
932 case BINARY_XOR:
933 case BINARY_OR:
934 return -1;
935 case INPLACE_POWER:
936 return -1;
937 case GET_ITER:
938 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 case PRINT_EXPR:
941 return -1;
942 case LOAD_BUILD_CLASS:
943 return 1;
944 case INPLACE_LSHIFT:
945 case INPLACE_RSHIFT:
946 case INPLACE_AND:
947 case INPLACE_XOR:
948 case INPLACE_OR:
949 return -1;
950 case BREAK_LOOP:
951 return 0;
952 case SETUP_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200953 /* 1 in the normal flow.
954 * Restore the stack position and push 6 values before jumping to
955 * the handler if an exception be raised. */
956 return jump ? 6 : 1;
Yury Selivanov75445082015-05-11 22:57:16 -0400957 case WITH_CLEANUP_START:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200958 return 2; /* or 1, depending on TOS */
Yury Selivanov75445082015-05-11 22:57:16 -0400959 case WITH_CLEANUP_FINISH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200960 /* Pop a variable number of values pushed by WITH_CLEANUP_START
961 * + __exit__ or __aexit__. */
962 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 case RETURN_VALUE:
964 return -1;
965 case IMPORT_STAR:
966 return -1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700967 case SETUP_ANNOTATIONS:
968 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 case YIELD_VALUE:
970 return 0;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500971 case YIELD_FROM:
972 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 case POP_BLOCK:
974 return 0;
975 case POP_EXCEPT:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200976 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 case END_FINALLY:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200978 /* Pop 6 values when an exception was raised. */
979 return -6;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 case STORE_NAME:
982 return -1;
983 case DELETE_NAME:
984 return 0;
985 case UNPACK_SEQUENCE:
986 return oparg-1;
987 case UNPACK_EX:
988 return (oparg&0xFF) + (oparg>>8);
989 case FOR_ITER:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200990 /* -1 at end of iterator, 1 if continue iterating. */
991 return jump > 0 ? -1 : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 case STORE_ATTR:
994 return -2;
995 case DELETE_ATTR:
996 return -1;
997 case STORE_GLOBAL:
998 return -1;
999 case DELETE_GLOBAL:
1000 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 case LOAD_CONST:
1002 return 1;
1003 case LOAD_NAME:
1004 return 1;
1005 case BUILD_TUPLE:
1006 case BUILD_LIST:
1007 case BUILD_SET:
Serhiy Storchakaea525a22016-09-06 22:07:53 +03001008 case BUILD_STRING:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 return 1-oparg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001010 case BUILD_LIST_UNPACK:
1011 case BUILD_TUPLE_UNPACK:
Serhiy Storchaka73442852016-10-02 10:33:46 +03001012 case BUILD_TUPLE_UNPACK_WITH_CALL:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001013 case BUILD_SET_UNPACK:
1014 case BUILD_MAP_UNPACK:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001015 case BUILD_MAP_UNPACK_WITH_CALL:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001016 return 1 - oparg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 case BUILD_MAP:
Benjamin Petersonb6855152015-09-10 21:02:39 -07001018 return 1 - 2*oparg;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001019 case BUILD_CONST_KEY_MAP:
1020 return -oparg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 case LOAD_ATTR:
1022 return 0;
1023 case COMPARE_OP:
1024 return -1;
1025 case IMPORT_NAME:
1026 return -1;
1027 case IMPORT_FROM:
1028 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001030 /* Jumps */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 case JUMP_FORWARD:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 case JUMP_ABSOLUTE:
1033 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001035 case JUMP_IF_TRUE_OR_POP:
1036 case JUMP_IF_FALSE_OR_POP:
1037 return jump ? 0 : -1;
1038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 case POP_JUMP_IF_FALSE:
1040 case POP_JUMP_IF_TRUE:
1041 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 case LOAD_GLOBAL:
1044 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 case CONTINUE_LOOP:
1047 return 0;
1048 case SETUP_LOOP:
1049 return 0;
1050 case SETUP_EXCEPT:
1051 case SETUP_FINALLY:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001052 /* 0 in the normal flow.
1053 * Restore the stack position and push 6 values before jumping to
1054 * the handler if an exception be raised. */
1055 return jump ? 6 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 case LOAD_FAST:
1058 return 1;
1059 case STORE_FAST:
1060 return -1;
1061 case DELETE_FAST:
1062 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 case RAISE_VARARGS:
1065 return -oparg;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001066
1067 /* Functions and calls */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 case CALL_FUNCTION:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001069 return -oparg;
Yury Selivanovf2392132016-12-13 19:03:51 -05001070 case CALL_METHOD:
1071 return -oparg-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 case CALL_FUNCTION_KW:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001073 return -oparg-1;
1074 case CALL_FUNCTION_EX:
Matthieu Dartiailh3a9ac822017-02-21 14:25:22 +01001075 return -1 - ((oparg & 0x01) != 0);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001076 case MAKE_FUNCTION:
1077 return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1078 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 case BUILD_SLICE:
1080 if (oparg == 3)
1081 return -2;
1082 else
1083 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001085 /* Closures */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 case LOAD_CLOSURE:
1087 return 1;
1088 case LOAD_DEREF:
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001089 case LOAD_CLASSDEREF:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 return 1;
1091 case STORE_DEREF:
1092 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001093 case DELETE_DEREF:
1094 return 0;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001095
1096 /* Iterators and generators */
Yury Selivanov75445082015-05-11 22:57:16 -04001097 case GET_AWAITABLE:
1098 return 0;
1099 case SETUP_ASYNC_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001100 /* 0 in the normal flow.
1101 * Restore the stack position to the position before the result
1102 * of __aenter__ and push 6 values before jumping to the handler
1103 * if an exception be raised. */
1104 return jump ? -1 + 6 : 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001105 case BEFORE_ASYNC_WITH:
1106 return 1;
1107 case GET_AITER:
1108 return 0;
1109 case GET_ANEXT:
1110 return 1;
Yury Selivanov5376ba92015-06-22 12:19:30 -04001111 case GET_YIELD_FROM_ITER:
1112 return 0;
Eric V. Smitha78c7952015-11-03 12:45:05 -05001113 case FORMAT_VALUE:
1114 /* If there's a fmt_spec on the stack, we go from 2->1,
1115 else 1->1. */
1116 return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
Yury Selivanovf2392132016-12-13 19:03:51 -05001117 case LOAD_METHOD:
1118 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 default:
Larry Hastings3a907972013-11-23 14:49:22 -08001120 return PY_INVALID_STACK_EFFECT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 }
Larry Hastings3a907972013-11-23 14:49:22 -08001122 return PY_INVALID_STACK_EFFECT; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123}
1124
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001125int
1126PyCompile_OpcodeStackEffect(int opcode, int oparg)
1127{
1128 return stack_effect(opcode, oparg, -1);
1129}
1130
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131/* Add an opcode with no argument.
1132 Returns 0 on failure, 1 on success.
1133*/
1134
1135static int
1136compiler_addop(struct compiler *c, int opcode)
1137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 basicblock *b;
1139 struct instr *i;
1140 int off;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001141 assert(!HAS_ARG(opcode));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 off = compiler_next_instr(c, c->u->u_curblock);
1143 if (off < 0)
1144 return 0;
1145 b = c->u->u_curblock;
1146 i = &b->b_instr[off];
1147 i->i_opcode = opcode;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001148 i->i_oparg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 if (opcode == RETURN_VALUE)
1150 b->b_return = 1;
1151 compiler_set_lineno(c, off);
1152 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153}
1154
Victor Stinnerf8e32212013-11-19 23:56:34 +01001155static Py_ssize_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001156compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
1157{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 PyObject *t, *v;
1159 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160
Victor Stinnerefb24132016-01-22 12:33:12 +01001161 t = _PyCode_ConstantKey(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 if (t == NULL)
1163 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 v = PyDict_GetItem(dict, t);
1166 if (!v) {
Stefan Krahc0cbed12015-07-27 12:56:49 +02001167 if (PyErr_Occurred()) {
1168 Py_DECREF(t);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 return -1;
Stefan Krahc0cbed12015-07-27 12:56:49 +02001170 }
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001171 arg = PyDict_GET_SIZE(dict);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001172 v = PyLong_FromSsize_t(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 if (!v) {
1174 Py_DECREF(t);
1175 return -1;
1176 }
1177 if (PyDict_SetItem(dict, t, v) < 0) {
1178 Py_DECREF(t);
1179 Py_DECREF(v);
1180 return -1;
1181 }
1182 Py_DECREF(v);
1183 }
1184 else
1185 arg = PyLong_AsLong(v);
1186 Py_DECREF(t);
1187 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188}
1189
1190static int
1191compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001193{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001194 Py_ssize_t arg = compiler_add_o(c, dict, o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001196 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197 return compiler_addop_i(c, opcode, arg);
1198}
1199
1200static int
1201compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001204 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1206 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001207 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208 arg = compiler_add_o(c, dict, mangled);
1209 Py_DECREF(mangled);
1210 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001211 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212 return compiler_addop_i(c, opcode, arg);
1213}
1214
1215/* Add an opcode with an integer argument.
1216 Returns 0 on failure, 1 on success.
1217*/
1218
1219static int
Victor Stinnerf8e32212013-11-19 23:56:34 +01001220compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 struct instr *i;
1223 int off;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001224
Victor Stinner2ad474b2016-03-01 23:34:47 +01001225 /* oparg value is unsigned, but a signed C int is usually used to store
1226 it in the C code (like Python/ceval.c).
1227
1228 Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1229
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001230 The argument of a concrete bytecode instruction is limited to 8-bit.
1231 EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1232 assert(HAS_ARG(opcode));
Victor Stinner2ad474b2016-03-01 23:34:47 +01001233 assert(0 <= oparg && oparg <= 2147483647);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 off = compiler_next_instr(c, c->u->u_curblock);
1236 if (off < 0)
1237 return 0;
1238 i = &c->u->u_curblock->b_instr[off];
Victor Stinnerf8e32212013-11-19 23:56:34 +01001239 i->i_opcode = opcode;
1240 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 compiler_set_lineno(c, off);
1242 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001243}
1244
1245static int
1246compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1247{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 struct instr *i;
1249 int off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001250
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001251 assert(HAS_ARG(opcode));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 assert(b != NULL);
1253 off = compiler_next_instr(c, c->u->u_curblock);
1254 if (off < 0)
1255 return 0;
1256 i = &c->u->u_curblock->b_instr[off];
1257 i->i_opcode = opcode;
1258 i->i_target = b;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 if (absolute)
1260 i->i_jabs = 1;
1261 else
1262 i->i_jrel = 1;
1263 compiler_set_lineno(c, off);
1264 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265}
1266
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +01001267/* NEXT_BLOCK() creates an implicit jump from the current block
1268 to the new block.
1269
1270 The returns inside this macro make it impossible to decref objects
1271 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 if (compiler_next_block((C)) == NULL) \
1275 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276}
1277
1278#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 if (!compiler_addop((C), (OP))) \
1280 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001281}
1282
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001283#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 if (!compiler_addop((C), (OP))) { \
1285 compiler_exit_scope(c); \
1286 return 0; \
1287 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001288}
1289
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001290#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1292 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001293}
1294
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001295/* Same as ADDOP_O, but steals a reference. */
1296#define ADDOP_N(C, OP, O, TYPE) { \
1297 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1298 Py_DECREF((O)); \
1299 return 0; \
1300 } \
1301 Py_DECREF((O)); \
1302}
1303
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001304#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1306 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307}
1308
1309#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 if (!compiler_addop_i((C), (OP), (O))) \
1311 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001312}
1313
1314#define ADDOP_JABS(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 if (!compiler_addop_j((C), (OP), (O), 1)) \
1316 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317}
1318
1319#define ADDOP_JREL(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 if (!compiler_addop_j((C), (OP), (O), 0)) \
1321 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001322}
1323
1324/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1325 the ASDL name to synthesize the name of the C type and the visit function.
1326*/
1327
1328#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 if (!compiler_visit_ ## TYPE((C), (V))) \
1330 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001331}
1332
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001333#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 if (!compiler_visit_ ## TYPE((C), (V))) { \
1335 compiler_exit_scope(c); \
1336 return 0; \
1337 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001338}
1339
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001340#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 if (!compiler_visit_slice((C), (V), (CTX))) \
1342 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343}
1344
1345#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 int _i; \
1347 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1348 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1349 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1350 if (!compiler_visit_ ## TYPE((C), elt)) \
1351 return 0; \
1352 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001353}
1354
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001355#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 int _i; \
1357 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1358 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1359 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1360 if (!compiler_visit_ ## TYPE((C), elt)) { \
1361 compiler_exit_scope(c); \
1362 return 0; \
1363 } \
1364 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001365}
1366
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001367static int
Serhiy Storchaka2641ee52018-05-29 10:49:10 +03001368compiler_isdocstring(stmt_ty s)
1369{
1370 if (s->kind != Expr_kind)
1371 return 0;
1372 if (s->v.Expr.value->kind == Str_kind)
1373 return 1;
1374 if (s->v.Expr.value->kind == Constant_kind)
1375 return PyUnicode_CheckExact(s->v.Expr.value->v.Constant.value);
1376 return 0;
1377}
1378
1379static int
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001380is_const(expr_ty e)
1381{
1382 switch (e->kind) {
1383 case Constant_kind:
1384 case Num_kind:
1385 case Str_kind:
1386 case Bytes_kind:
1387 case Ellipsis_kind:
1388 case NameConstant_kind:
1389 return 1;
1390 default:
1391 return 0;
1392 }
1393}
1394
1395static PyObject *
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02001396get_const_value(expr_ty e)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001397{
1398 switch (e->kind) {
1399 case Constant_kind:
1400 return e->v.Constant.value;
1401 case Num_kind:
1402 return e->v.Num.n;
1403 case Str_kind:
1404 return e->v.Str.s;
1405 case Bytes_kind:
1406 return e->v.Bytes.s;
1407 case Ellipsis_kind:
1408 return Py_Ellipsis;
1409 case NameConstant_kind:
1410 return e->v.NameConstant.value;
1411 default:
Barry Warsawb2e57942017-09-14 18:13:16 -07001412 Py_UNREACHABLE();
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001413 }
1414}
1415
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001416/* Search if variable annotations are present statically in a block. */
1417
1418static int
1419find_ann(asdl_seq *stmts)
1420{
1421 int i, j, res = 0;
1422 stmt_ty st;
1423
1424 for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1425 st = (stmt_ty)asdl_seq_GET(stmts, i);
1426 switch (st->kind) {
1427 case AnnAssign_kind:
1428 return 1;
1429 case For_kind:
1430 res = find_ann(st->v.For.body) ||
1431 find_ann(st->v.For.orelse);
1432 break;
1433 case AsyncFor_kind:
1434 res = find_ann(st->v.AsyncFor.body) ||
1435 find_ann(st->v.AsyncFor.orelse);
1436 break;
1437 case While_kind:
1438 res = find_ann(st->v.While.body) ||
1439 find_ann(st->v.While.orelse);
1440 break;
1441 case If_kind:
1442 res = find_ann(st->v.If.body) ||
1443 find_ann(st->v.If.orelse);
1444 break;
1445 case With_kind:
1446 res = find_ann(st->v.With.body);
1447 break;
1448 case AsyncWith_kind:
1449 res = find_ann(st->v.AsyncWith.body);
1450 break;
1451 case Try_kind:
1452 for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1453 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1454 st->v.Try.handlers, j);
1455 if (find_ann(handler->v.ExceptHandler.body)) {
1456 return 1;
1457 }
1458 }
1459 res = find_ann(st->v.Try.body) ||
1460 find_ann(st->v.Try.finalbody) ||
1461 find_ann(st->v.Try.orelse);
1462 break;
1463 default:
1464 res = 0;
1465 }
1466 if (res) {
1467 break;
1468 }
1469 }
1470 return res;
1471}
1472
1473/* Compile a sequence of statements, checking for a docstring
1474 and for annotations. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001475
1476static int
Serhiy Storchaka2641ee52018-05-29 10:49:10 +03001477compiler_body(struct compiler *c, asdl_seq *stmts)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001478{
Serhiy Storchaka2641ee52018-05-29 10:49:10 +03001479 int i = 0;
1480 stmt_ty st;
1481
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001482 /* Set current line number to the line number of first statement.
1483 This way line number for SETUP_ANNOTATIONS will always
1484 coincide with the line number of first "real" statement in module.
1485 If body is empy, then lineno will be set later in assemble. */
1486 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE &&
1487 !c->u->u_lineno && asdl_seq_LEN(stmts)) {
Serhiy Storchaka2641ee52018-05-29 10:49:10 +03001488 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001489 c->u->u_lineno = st->lineno;
1490 }
1491 /* Every annotated class and module should have __annotations__. */
1492 if (find_ann(stmts)) {
1493 ADDOP(c, SETUP_ANNOTATIONS);
1494 }
Serhiy Storchaka2641ee52018-05-29 10:49:10 +03001495 if (!asdl_seq_LEN(stmts))
1496 return 1;
1497 st = (stmt_ty)asdl_seq_GET(stmts, 0);
INADA Naokicb41b272017-02-23 00:31:59 +09001498 /* if not -OO mode, set docstring */
Serhiy Storchaka2641ee52018-05-29 10:49:10 +03001499 if (compiler_isdocstring(st) && c->c_optimize < 2) {
1500 /* don't generate docstrings if -OO */
1501 i = 1;
1502 VISIT(c, expr, st->v.Expr.value);
1503 if (!compiler_nameop(c, __doc__, Store))
1504 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 }
Serhiy Storchaka2641ee52018-05-29 10:49:10 +03001506 for (; i < asdl_seq_LEN(stmts); i++)
1507 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509}
1510
1511static PyCodeObject *
1512compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 PyCodeObject *co;
1515 int addNone = 1;
1516 static PyObject *module;
1517 if (!module) {
1518 module = PyUnicode_InternFromString("<module>");
1519 if (!module)
1520 return NULL;
1521 }
1522 /* Use 0 for firstlineno initially, will fixup in assemble(). */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001523 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 return NULL;
1525 switch (mod->kind) {
1526 case Module_kind:
Serhiy Storchaka2641ee52018-05-29 10:49:10 +03001527 if (!compiler_body(c, mod->v.Module.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 compiler_exit_scope(c);
1529 return 0;
1530 }
1531 break;
1532 case Interactive_kind:
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001533 if (find_ann(mod->v.Interactive.body)) {
1534 ADDOP(c, SETUP_ANNOTATIONS);
1535 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 c->c_interactive = 1;
1537 VISIT_SEQ_IN_SCOPE(c, stmt,
1538 mod->v.Interactive.body);
1539 break;
1540 case Expression_kind:
1541 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1542 addNone = 0;
1543 break;
1544 case Suite_kind:
1545 PyErr_SetString(PyExc_SystemError,
1546 "suite should not be possible");
1547 return 0;
1548 default:
1549 PyErr_Format(PyExc_SystemError,
1550 "module kind %d should not be possible",
1551 mod->kind);
1552 return 0;
1553 }
1554 co = assemble(c, addNone);
1555 compiler_exit_scope(c);
1556 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001557}
1558
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559/* The test for LOCAL must come before the test for FREE in order to
1560 handle classes where name is both local and free. The local var is
1561 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001562*/
1563
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564static int
1565get_ref_type(struct compiler *c, PyObject *name)
1566{
Victor Stinner0b1bc562013-05-16 22:17:17 +02001567 int scope;
Benjamin Peterson312595c2013-05-15 15:26:42 -05001568 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001569 _PyUnicode_EqualToASCIIString(name, "__class__"))
Benjamin Peterson312595c2013-05-15 15:26:42 -05001570 return CELL;
Victor Stinner0b1bc562013-05-16 22:17:17 +02001571 scope = PyST_GetScope(c->u->u_ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 if (scope == 0) {
1573 char buf[350];
1574 PyOS_snprintf(buf, sizeof(buf),
Victor Stinner14e461d2013-08-26 22:28:21 +02001575 "unknown scope for %.100s in %.100s(%s)\n"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 "symbols: %s\nlocals: %s\nglobals: %s",
Serhiy Storchakadf4518c2014-11-18 23:34:33 +02001577 PyUnicode_AsUTF8(name),
1578 PyUnicode_AsUTF8(c->u->u_name),
1579 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)),
1580 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)),
1581 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)),
1582 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 );
1584 Py_FatalError(buf);
1585 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001588}
1589
1590static int
1591compiler_lookup_arg(PyObject *dict, PyObject *name)
1592{
1593 PyObject *k, *v;
Victor Stinnerefb24132016-01-22 12:33:12 +01001594 k = _PyCode_ConstantKey(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001595 if (k == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001596 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001597 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001598 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001599 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001600 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001601 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001602}
1603
1604static int
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001605compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001606{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001607 Py_ssize_t i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001608 if (qualname == NULL)
1609 qualname = co->co_name;
1610
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001611 if (free) {
1612 for (i = 0; i < free; ++i) {
1613 /* Bypass com_addop_varname because it will generate
1614 LOAD_DEREF but LOAD_CLOSURE is needed.
1615 */
1616 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1617 int arg, reftype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001618
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001619 /* Special case: If a class contains a method with a
1620 free variable that has the same name as a method,
1621 the name will be considered free *and* local in the
1622 class. It should be handled by the closure, as
1623 well as by the normal name loookup logic.
1624 */
1625 reftype = get_ref_type(c, name);
1626 if (reftype == CELL)
1627 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1628 else /* (reftype == FREE) */
1629 arg = compiler_lookup_arg(c->u->u_freevars, name);
1630 if (arg == -1) {
1631 fprintf(stderr,
1632 "lookup %s in %s %d %d\n"
1633 "freevars of %s: %s\n",
1634 PyUnicode_AsUTF8(PyObject_Repr(name)),
1635 PyUnicode_AsUTF8(c->u->u_name),
1636 reftype, arg,
1637 PyUnicode_AsUTF8(co->co_name),
1638 PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars)));
1639 Py_FatalError("compiler_make_closure()");
1640 }
1641 ADDOP_I(c, LOAD_CLOSURE, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001643 flags |= 0x08;
1644 ADDOP_I(c, BUILD_TUPLE, free);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001647 ADDOP_O(c, LOAD_CONST, qualname, consts);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001648 ADDOP_I(c, MAKE_FUNCTION, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001650}
1651
1652static int
1653compiler_decorators(struct compiler *c, asdl_seq* decos)
1654{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 if (!decos)
1658 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1661 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1662 }
1663 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001664}
1665
1666static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001667compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 asdl_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001669{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001670 /* Push a dict of keyword-only default values.
1671
1672 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
1673 */
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001674 int i;
1675 PyObject *keys = NULL;
1676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1678 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1679 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1680 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04001681 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001682 if (!mangled) {
1683 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001685 if (keys == NULL) {
1686 keys = PyList_New(1);
1687 if (keys == NULL) {
1688 Py_DECREF(mangled);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001689 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001690 }
1691 PyList_SET_ITEM(keys, 0, mangled);
1692 }
1693 else {
1694 int res = PyList_Append(keys, mangled);
1695 Py_DECREF(mangled);
1696 if (res == -1) {
1697 goto error;
1698 }
1699 }
1700 if (!compiler_visit_expr(c, default_)) {
1701 goto error;
1702 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 }
1704 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001705 if (keys != NULL) {
1706 Py_ssize_t default_count = PyList_GET_SIZE(keys);
1707 PyObject *keys_tuple = PyList_AsTuple(keys);
1708 Py_DECREF(keys);
1709 if (keys_tuple == NULL) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001710 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001711 }
1712 ADDOP_N(c, LOAD_CONST, keys_tuple, consts);
1713 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001714 assert(default_count > 0);
1715 return 1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001716 }
1717 else {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001718 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001719 }
1720
1721error:
1722 Py_XDECREF(keys);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001723 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001724}
1725
1726static int
Guido van Rossum95e4d582018-01-26 08:20:18 -08001727compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
1728{
1729 PyObject *ann_as_str;
Serhiy Storchakab32f8892018-05-20 18:06:08 +03001730 ann_as_str = _PyAST_ExprAsUnicode(annotation);
Guido van Rossum95e4d582018-01-26 08:20:18 -08001731 if (!ann_as_str) {
1732 return 0;
1733 }
1734 ADDOP_N(c, LOAD_CONST, ann_as_str, consts);
1735 return 1;
1736}
1737
1738static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001739compiler_visit_argannotation(struct compiler *c, identifier id,
1740 expr_ty annotation, PyObject *names)
1741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 if (annotation) {
Victor Stinner065efc32014-02-18 22:07:56 +01001743 PyObject *mangled;
Guido van Rossum95e4d582018-01-26 08:20:18 -08001744 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
1745 VISIT(c, annexpr, annotation)
1746 }
1747 else {
1748 VISIT(c, expr, annotation);
1749 }
Victor Stinner065efc32014-02-18 22:07:56 +01001750 mangled = _Py_Mangle(c->u->u_private, id);
Yury Selivanov34ce99f2014-02-18 12:49:41 -05001751 if (!mangled)
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001752 return 0;
Yury Selivanov34ce99f2014-02-18 12:49:41 -05001753 if (PyList_Append(names, mangled) < 0) {
1754 Py_DECREF(mangled);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001755 return 0;
Yury Selivanov34ce99f2014-02-18 12:49:41 -05001756 }
1757 Py_DECREF(mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001759 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001760}
1761
1762static int
1763compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1764 PyObject *names)
1765{
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001766 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001767 for (i = 0; i < asdl_seq_LEN(args); i++) {
1768 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001769 if (!compiler_visit_argannotation(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 c,
1771 arg->arg,
1772 arg->annotation,
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001773 names))
1774 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001776 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001777}
1778
1779static int
1780compiler_visit_annotations(struct compiler *c, arguments_ty args,
1781 expr_ty returns)
1782{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001783 /* Push arg annotation dict.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001784 The expressions are evaluated out-of-order wrt the source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00001785
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001786 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 */
1788 static identifier return_str;
1789 PyObject *names;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001790 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 names = PyList_New(0);
1792 if (!names)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03001793 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001794
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001795 if (!compiler_visit_argannotations(c, args->args, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001797 if (args->vararg && args->vararg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001798 !compiler_visit_argannotation(c, args->vararg->arg,
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001799 args->vararg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 goto error;
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001801 if (!compiler_visit_argannotations(c, args->kwonlyargs, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001803 if (args->kwarg && args->kwarg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001804 !compiler_visit_argannotation(c, args->kwarg->arg,
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001805 args->kwarg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 if (!return_str) {
1809 return_str = PyUnicode_InternFromString("return");
1810 if (!return_str)
1811 goto error;
1812 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001813 if (!compiler_visit_argannotation(c, return_str, returns, names)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 goto error;
1815 }
1816
1817 len = PyList_GET_SIZE(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 if (len) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001819 PyObject *keytuple = PyList_AsTuple(names);
1820 Py_DECREF(names);
1821 if (keytuple == NULL) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001822 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001824 ADDOP_N(c, LOAD_CONST, keytuple, consts);
1825 ADDOP_I(c, BUILD_CONST_KEY_MAP, len);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001826 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001828 else {
1829 Py_DECREF(names);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001830 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001831 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001832
1833error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 Py_DECREF(names);
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03001835 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001836}
1837
1838static int
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001839compiler_visit_defaults(struct compiler *c, arguments_ty args)
1840{
1841 VISIT_SEQ(c, expr, args->defaults);
1842 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
1843 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001844}
1845
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001846static Py_ssize_t
1847compiler_default_arguments(struct compiler *c, arguments_ty args)
1848{
1849 Py_ssize_t funcflags = 0;
1850 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001851 if (!compiler_visit_defaults(c, args))
1852 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001853 funcflags |= 0x01;
1854 }
1855 if (args->kwonlyargs) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001856 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001857 args->kw_defaults);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001858 if (res == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001859 return -1;
1860 }
1861 else if (res > 0) {
1862 funcflags |= 0x02;
1863 }
1864 }
1865 return funcflags;
1866}
1867
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001868static int
Yury Selivanov75445082015-05-11 22:57:16 -04001869compiler_function(struct compiler *c, stmt_ty s, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001870{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 PyCodeObject *co;
Serhiy Storchaka2641ee52018-05-29 10:49:10 +03001872 PyObject *qualname, *first_const = Py_None;
Yury Selivanov75445082015-05-11 22:57:16 -04001873 arguments_ty args;
1874 expr_ty returns;
1875 identifier name;
1876 asdl_seq* decos;
1877 asdl_seq *body;
Serhiy Storchaka2641ee52018-05-29 10:49:10 +03001878 stmt_ty st;
INADA Naokicb41b272017-02-23 00:31:59 +09001879 Py_ssize_t i, funcflags;
Serhiy Storchaka2641ee52018-05-29 10:49:10 +03001880 int docstring;
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001881 int annotations;
Yury Selivanov75445082015-05-11 22:57:16 -04001882 int scope_type;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001883
Yury Selivanov75445082015-05-11 22:57:16 -04001884 if (is_async) {
1885 assert(s->kind == AsyncFunctionDef_kind);
1886
1887 args = s->v.AsyncFunctionDef.args;
1888 returns = s->v.AsyncFunctionDef.returns;
1889 decos = s->v.AsyncFunctionDef.decorator_list;
1890 name = s->v.AsyncFunctionDef.name;
1891 body = s->v.AsyncFunctionDef.body;
1892
1893 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
1894 } else {
1895 assert(s->kind == FunctionDef_kind);
1896
1897 args = s->v.FunctionDef.args;
1898 returns = s->v.FunctionDef.returns;
1899 decos = s->v.FunctionDef.decorator_list;
1900 name = s->v.FunctionDef.name;
1901 body = s->v.FunctionDef.body;
1902
1903 scope_type = COMPILER_SCOPE_FUNCTION;
1904 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 if (!compiler_decorators(c, decos))
1907 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001908
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001909 funcflags = compiler_default_arguments(c, args);
1910 if (funcflags == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001912 }
1913
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001914 annotations = compiler_visit_annotations(c, args, returns);
1915 if (annotations == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001916 return 0;
1917 }
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001918 else if (annotations > 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001919 funcflags |= 0x04;
1920 }
1921
1922 if (!compiler_enter_scope(c, name, scope_type, (void *)s, s->lineno)) {
1923 return 0;
1924 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925
Serhiy Storchaka2641ee52018-05-29 10:49:10 +03001926 st = (stmt_ty)asdl_seq_GET(body, 0);
1927 docstring = compiler_isdocstring(st);
1928 if (docstring && c->c_optimize < 2) {
1929 if (st->v.Expr.value->kind == Constant_kind)
1930 first_const = st->v.Expr.value->v.Constant.value;
1931 else
1932 first_const = st->v.Expr.value->v.Str.s;
1933 }
1934 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 compiler_exit_scope(c);
1936 return 0;
1937 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 c->u->u_argcount = asdl_seq_LEN(args->args);
1940 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
INADA Naokicb41b272017-02-23 00:31:59 +09001941 VISIT_SEQ_IN_SCOPE(c, stmt, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001943 qualname = c->u->u_qualname;
1944 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001946 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001947 Py_XDECREF(qualname);
1948 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001950 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001951
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001952 compiler_make_closure(c, co, funcflags, qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001953 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 /* decorators */
1957 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1958 ADDOP_I(c, CALL_FUNCTION, 1);
1959 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001960
Yury Selivanov75445082015-05-11 22:57:16 -04001961 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001962}
1963
1964static int
1965compiler_class(struct compiler *c, stmt_ty s)
1966{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 PyCodeObject *co;
1968 PyObject *str;
1969 int i;
1970 asdl_seq* decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 if (!compiler_decorators(c, decos))
1973 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 /* ultimately generate code for:
1976 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1977 where:
1978 <func> is a function/closure created from the class body;
1979 it has a single argument (__locals__) where the dict
1980 (or MutableSequence) representing the locals is passed
1981 <name> is the class name
1982 <bases> is the positional arguments and *varargs argument
1983 <keywords> is the keyword arguments and **kwds argument
1984 This borrows from compiler_call.
1985 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001988 if (!compiler_enter_scope(c, s->v.ClassDef.name,
1989 COMPILER_SCOPE_CLASS, (void *)s, s->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 return 0;
1991 /* this block represents what we do in the new scope */
1992 {
1993 /* use the class name for name mangling */
1994 Py_INCREF(s->v.ClassDef.name);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001995 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 /* load (global) __name__ ... */
1997 str = PyUnicode_InternFromString("__name__");
1998 if (!str || !compiler_nameop(c, str, Load)) {
1999 Py_XDECREF(str);
2000 compiler_exit_scope(c);
2001 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002002 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 Py_DECREF(str);
2004 /* ... and store it as __module__ */
2005 str = PyUnicode_InternFromString("__module__");
2006 if (!str || !compiler_nameop(c, str, Store)) {
2007 Py_XDECREF(str);
2008 compiler_exit_scope(c);
2009 return 0;
2010 }
2011 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002012 assert(c->u->u_qualname);
2013 ADDOP_O(c, LOAD_CONST, c->u->u_qualname, consts);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002014 str = PyUnicode_InternFromString("__qualname__");
2015 if (!str || !compiler_nameop(c, str, Store)) {
2016 Py_XDECREF(str);
2017 compiler_exit_scope(c);
2018 return 0;
2019 }
2020 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 /* compile the body proper */
Serhiy Storchaka2641ee52018-05-29 10:49:10 +03002022 if (!compiler_body(c, s->v.ClassDef.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 compiler_exit_scope(c);
2024 return 0;
2025 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002026 /* Return __classcell__ if it is referenced, otherwise return None */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002027 if (c->u->u_ste->ste_needs_class_closure) {
Nick Coghlan19d24672016-12-05 16:47:55 +10002028 /* Store __classcell__ into class namespace & return it */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002029 str = PyUnicode_InternFromString("__class__");
2030 if (str == NULL) {
2031 compiler_exit_scope(c);
2032 return 0;
2033 }
2034 i = compiler_lookup_arg(c->u->u_cellvars, str);
2035 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01002036 if (i < 0) {
2037 compiler_exit_scope(c);
2038 return 0;
2039 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002040 assert(i == 0);
Nick Coghlan944368e2016-09-11 14:45:49 +10002041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 ADDOP_I(c, LOAD_CLOSURE, i);
Nick Coghlan19d24672016-12-05 16:47:55 +10002043 ADDOP(c, DUP_TOP);
Nick Coghlan944368e2016-09-11 14:45:49 +10002044 str = PyUnicode_InternFromString("__classcell__");
2045 if (!str || !compiler_nameop(c, str, Store)) {
2046 Py_XDECREF(str);
2047 compiler_exit_scope(c);
2048 return 0;
2049 }
2050 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002052 else {
Nick Coghlan19d24672016-12-05 16:47:55 +10002053 /* No methods referenced __class__, so just return None */
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002054 assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
Nick Coghlan19d24672016-12-05 16:47:55 +10002055 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Benjamin Peterson312595c2013-05-15 15:26:42 -05002056 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002057 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 /* create the code object */
2059 co = assemble(c, 1);
2060 }
2061 /* leave the new scope */
2062 compiler_exit_scope(c);
2063 if (co == NULL)
2064 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 /* 2. load the 'build_class' function */
2067 ADDOP(c, LOAD_BUILD_CLASS);
2068
2069 /* 3. load a function (or closure) made from the code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002070 compiler_make_closure(c, co, 0, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 Py_DECREF(co);
2072
2073 /* 4. load class name */
2074 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
2075
2076 /* 5. generate the rest of the code for the call */
2077 if (!compiler_call_helper(c, 2,
2078 s->v.ClassDef.bases,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002079 s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 return 0;
2081
2082 /* 6. apply decorators */
2083 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2084 ADDOP_I(c, CALL_FUNCTION, 1);
2085 }
2086
2087 /* 7. store into <name> */
2088 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2089 return 0;
2090 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002091}
2092
2093static int
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002094cmpop(cmpop_ty op)
2095{
2096 switch (op) {
2097 case Eq:
2098 return PyCmp_EQ;
2099 case NotEq:
2100 return PyCmp_NE;
2101 case Lt:
2102 return PyCmp_LT;
2103 case LtE:
2104 return PyCmp_LE;
2105 case Gt:
2106 return PyCmp_GT;
2107 case GtE:
2108 return PyCmp_GE;
2109 case Is:
2110 return PyCmp_IS;
2111 case IsNot:
2112 return PyCmp_IS_NOT;
2113 case In:
2114 return PyCmp_IN;
2115 case NotIn:
2116 return PyCmp_NOT_IN;
2117 default:
2118 return PyCmp_BAD;
2119 }
2120}
2121
2122static int
2123compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2124{
2125 switch (e->kind) {
2126 case UnaryOp_kind:
2127 if (e->v.UnaryOp.op == Not)
2128 return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2129 /* fallback to general implementation */
2130 break;
2131 case BoolOp_kind: {
2132 asdl_seq *s = e->v.BoolOp.values;
2133 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2134 assert(n >= 0);
2135 int cond2 = e->v.BoolOp.op == Or;
2136 basicblock *next2 = next;
2137 if (!cond2 != !cond) {
2138 next2 = compiler_new_block(c);
2139 if (next2 == NULL)
2140 return 0;
2141 }
2142 for (i = 0; i < n; ++i) {
2143 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2144 return 0;
2145 }
2146 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2147 return 0;
2148 if (next2 != next)
2149 compiler_use_next_block(c, next2);
2150 return 1;
2151 }
2152 case IfExp_kind: {
2153 basicblock *end, *next2;
2154 end = compiler_new_block(c);
2155 if (end == NULL)
2156 return 0;
2157 next2 = compiler_new_block(c);
2158 if (next2 == NULL)
2159 return 0;
2160 if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2161 return 0;
2162 if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2163 return 0;
2164 ADDOP_JREL(c, JUMP_FORWARD, end);
2165 compiler_use_next_block(c, next2);
2166 if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2167 return 0;
2168 compiler_use_next_block(c, end);
2169 return 1;
2170 }
2171 case Compare_kind: {
2172 Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2173 if (n > 0) {
2174 basicblock *cleanup = compiler_new_block(c);
2175 if (cleanup == NULL)
2176 return 0;
2177 VISIT(c, expr, e->v.Compare.left);
2178 for (i = 0; i < n; i++) {
2179 VISIT(c, expr,
2180 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2181 ADDOP(c, DUP_TOP);
2182 ADDOP(c, ROT_THREE);
2183 ADDOP_I(c, COMPARE_OP,
2184 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, i))));
2185 ADDOP_JABS(c, POP_JUMP_IF_FALSE, cleanup);
2186 NEXT_BLOCK(c);
2187 }
2188 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
2189 ADDOP_I(c, COMPARE_OP,
2190 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n))));
2191 ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
2192 basicblock *end = compiler_new_block(c);
2193 if (end == NULL)
2194 return 0;
2195 ADDOP_JREL(c, JUMP_FORWARD, end);
2196 compiler_use_next_block(c, cleanup);
2197 ADDOP(c, POP_TOP);
2198 if (!cond) {
2199 ADDOP_JREL(c, JUMP_FORWARD, next);
2200 }
2201 compiler_use_next_block(c, end);
2202 return 1;
2203 }
2204 /* fallback to general implementation */
2205 break;
2206 }
2207 default:
2208 /* fallback to general implementation */
2209 break;
2210 }
2211
2212 /* general implementation */
2213 VISIT(c, expr, e);
2214 ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
2215 return 1;
2216}
2217
2218static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002219compiler_ifexp(struct compiler *c, expr_ty e)
2220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 basicblock *end, *next;
2222
2223 assert(e->kind == IfExp_kind);
2224 end = compiler_new_block(c);
2225 if (end == NULL)
2226 return 0;
2227 next = compiler_new_block(c);
2228 if (next == NULL)
2229 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002230 if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2231 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 VISIT(c, expr, e->v.IfExp.body);
2233 ADDOP_JREL(c, JUMP_FORWARD, end);
2234 compiler_use_next_block(c, next);
2235 VISIT(c, expr, e->v.IfExp.orelse);
2236 compiler_use_next_block(c, end);
2237 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002238}
2239
2240static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002241compiler_lambda(struct compiler *c, expr_ty e)
2242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002244 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 static identifier name;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002246 Py_ssize_t funcflags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 arguments_ty args = e->v.Lambda.args;
2248 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 if (!name) {
2251 name = PyUnicode_InternFromString("<lambda>");
2252 if (!name)
2253 return 0;
2254 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002255
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002256 funcflags = compiler_default_arguments(c, args);
2257 if (funcflags == -1) {
2258 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002260
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002261 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002262 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 /* Make None the first constant, so the lambda can't have a
2266 docstring. */
2267 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0)
2268 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 c->u->u_argcount = asdl_seq_LEN(args->args);
2271 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2272 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2273 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002274 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 }
2276 else {
2277 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002278 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002280 qualname = c->u->u_qualname;
2281 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002283 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002285
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002286 compiler_make_closure(c, co, funcflags, qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002287 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 Py_DECREF(co);
2289
2290 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002291}
2292
2293static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002294compiler_if(struct compiler *c, stmt_ty s)
2295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 basicblock *end, *next;
2297 int constant;
2298 assert(s->kind == If_kind);
2299 end = compiler_new_block(c);
2300 if (end == NULL)
2301 return 0;
2302
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02002303 constant = expr_constant(s->v.If.test);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 /* constant = 0: "if 0"
2305 * constant = 1: "if 1", "if 2", ...
2306 * constant = -1: rest */
2307 if (constant == 0) {
2308 if (s->v.If.orelse)
2309 VISIT_SEQ(c, stmt, s->v.If.orelse);
2310 } else if (constant == 1) {
2311 VISIT_SEQ(c, stmt, s->v.If.body);
2312 } else {
Antoine Pitroue7811fc2014-09-18 03:06:50 +02002313 if (asdl_seq_LEN(s->v.If.orelse)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314 next = compiler_new_block(c);
2315 if (next == NULL)
2316 return 0;
2317 }
2318 else
2319 next = end;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002320 if (!compiler_jump_if(c, s->v.If.test, next, 0))
2321 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 VISIT_SEQ(c, stmt, s->v.If.body);
Antoine Pitroue7811fc2014-09-18 03:06:50 +02002323 if (asdl_seq_LEN(s->v.If.orelse)) {
2324 ADDOP_JREL(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002325 compiler_use_next_block(c, next);
2326 VISIT_SEQ(c, stmt, s->v.If.orelse);
2327 }
2328 }
2329 compiler_use_next_block(c, end);
2330 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002331}
2332
2333static int
2334compiler_for(struct compiler *c, stmt_ty s)
2335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 basicblock *start, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 start = compiler_new_block(c);
2339 cleanup = compiler_new_block(c);
2340 end = compiler_new_block(c);
2341 if (start == NULL || end == NULL || cleanup == NULL)
2342 return 0;
2343 ADDOP_JREL(c, SETUP_LOOP, end);
2344 if (!compiler_push_fblock(c, LOOP, start))
2345 return 0;
2346 VISIT(c, expr, s->v.For.iter);
2347 ADDOP(c, GET_ITER);
2348 compiler_use_next_block(c, start);
2349 ADDOP_JREL(c, FOR_ITER, cleanup);
2350 VISIT(c, expr, s->v.For.target);
2351 VISIT_SEQ(c, stmt, s->v.For.body);
2352 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2353 compiler_use_next_block(c, cleanup);
2354 ADDOP(c, POP_BLOCK);
2355 compiler_pop_fblock(c, LOOP, start);
2356 VISIT_SEQ(c, stmt, s->v.For.orelse);
2357 compiler_use_next_block(c, end);
2358 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002359}
2360
Yury Selivanov75445082015-05-11 22:57:16 -04002361
2362static int
2363compiler_async_for(struct compiler *c, stmt_ty s)
2364{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002365 _Py_IDENTIFIER(StopAsyncIteration);
2366
Yury Selivanov75445082015-05-11 22:57:16 -04002367 basicblock *try, *except, *end, *after_try, *try_cleanup,
Serhiy Storchaka9e94c0d2018-03-10 20:45:05 +02002368 *after_loop_else;
Yury Selivanov75445082015-05-11 22:57:16 -04002369
Zsolt Dollensteina93a6632018-04-27 15:33:37 -07002370 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
2371 return compiler_error(c, "'async for' outside async function");
2372 }
2373
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002374 PyObject *stop_aiter_error = _PyUnicode_FromId(&PyId_StopAsyncIteration);
2375 if (stop_aiter_error == NULL) {
2376 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04002377 }
2378
2379 try = compiler_new_block(c);
2380 except = compiler_new_block(c);
2381 end = compiler_new_block(c);
2382 after_try = compiler_new_block(c);
2383 try_cleanup = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002384 after_loop_else = compiler_new_block(c);
2385
2386 if (try == NULL || except == NULL || end == NULL
Serhiy Storchaka9e94c0d2018-03-10 20:45:05 +02002387 || after_try == NULL || try_cleanup == NULL
2388 || after_loop_else == NULL)
Yury Selivanov75445082015-05-11 22:57:16 -04002389 return 0;
2390
Serhiy Storchaka9e94c0d2018-03-10 20:45:05 +02002391 ADDOP_JREL(c, SETUP_LOOP, end);
Yury Selivanov75445082015-05-11 22:57:16 -04002392 if (!compiler_push_fblock(c, LOOP, try))
2393 return 0;
2394
2395 VISIT(c, expr, s->v.AsyncFor.iter);
2396 ADDOP(c, GET_AITER);
Yury Selivanov75445082015-05-11 22:57:16 -04002397
2398 compiler_use_next_block(c, try);
2399
2400
2401 ADDOP_JREL(c, SETUP_EXCEPT, except);
2402 if (!compiler_push_fblock(c, EXCEPT, try))
2403 return 0;
2404
2405 ADDOP(c, GET_ANEXT);
2406 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2407 ADDOP(c, YIELD_FROM);
2408 VISIT(c, expr, s->v.AsyncFor.target);
2409 ADDOP(c, POP_BLOCK);
2410 compiler_pop_fblock(c, EXCEPT, try);
2411 ADDOP_JREL(c, JUMP_FORWARD, after_try);
2412
2413
2414 compiler_use_next_block(c, except);
2415 ADDOP(c, DUP_TOP);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002416 ADDOP_O(c, LOAD_GLOBAL, stop_aiter_error, names);
Yury Selivanov75445082015-05-11 22:57:16 -04002417 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
Serhiy Storchakab9744e92018-03-23 14:35:33 +02002418 ADDOP_JABS(c, POP_JUMP_IF_TRUE, try_cleanup);
Yury Selivanov75445082015-05-11 22:57:16 -04002419 ADDOP(c, END_FINALLY);
2420
2421 compiler_use_next_block(c, after_try);
2422 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
2423 ADDOP_JABS(c, JUMP_ABSOLUTE, try);
2424
Serhiy Storchakab9744e92018-03-23 14:35:33 +02002425 compiler_use_next_block(c, try_cleanup);
2426 ADDOP(c, POP_TOP);
2427 ADDOP(c, POP_TOP);
2428 ADDOP(c, POP_TOP);
2429 ADDOP(c, POP_EXCEPT); /* for SETUP_EXCEPT */
2430 ADDOP(c, POP_TOP); /* for correct calculation of stack effect */
Yury Selivanov75445082015-05-11 22:57:16 -04002431 ADDOP(c, POP_BLOCK); /* for SETUP_LOOP */
2432 compiler_pop_fblock(c, LOOP, try);
2433
Yury Selivanov75445082015-05-11 22:57:16 -04002434 compiler_use_next_block(c, after_loop_else);
2435 VISIT_SEQ(c, stmt, s->v.For.orelse);
2436
2437 compiler_use_next_block(c, end);
2438
2439 return 1;
2440}
2441
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002442static int
2443compiler_while(struct compiler *c, stmt_ty s)
2444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 basicblock *loop, *orelse, *end, *anchor = NULL;
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02002446 int constant = expr_constant(s->v.While.test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448 if (constant == 0) {
2449 if (s->v.While.orelse)
2450 VISIT_SEQ(c, stmt, s->v.While.orelse);
2451 return 1;
2452 }
2453 loop = compiler_new_block(c);
2454 end = compiler_new_block(c);
2455 if (constant == -1) {
2456 anchor = compiler_new_block(c);
2457 if (anchor == NULL)
2458 return 0;
2459 }
2460 if (loop == NULL || end == NULL)
2461 return 0;
2462 if (s->v.While.orelse) {
2463 orelse = compiler_new_block(c);
2464 if (orelse == NULL)
2465 return 0;
2466 }
2467 else
2468 orelse = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 ADDOP_JREL(c, SETUP_LOOP, end);
2471 compiler_use_next_block(c, loop);
2472 if (!compiler_push_fblock(c, LOOP, loop))
2473 return 0;
2474 if (constant == -1) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002475 if (!compiler_jump_if(c, s->v.While.test, anchor, 0))
2476 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 }
2478 VISIT_SEQ(c, stmt, s->v.While.body);
2479 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 /* XXX should the two POP instructions be in a separate block
2482 if there is no else clause ?
2483 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002484
Benjamin Peterson3cda0ed2014-12-13 16:06:19 -05002485 if (constant == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 compiler_use_next_block(c, anchor);
Benjamin Peterson3cda0ed2014-12-13 16:06:19 -05002487 ADDOP(c, POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 compiler_pop_fblock(c, LOOP, loop);
2489 if (orelse != NULL) /* what if orelse is just pass? */
2490 VISIT_SEQ(c, stmt, s->v.While.orelse);
2491 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002493 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002494}
2495
2496static int
2497compiler_continue(struct compiler *c)
2498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
2500 static const char IN_FINALLY_ERROR_MSG[] =
2501 "'continue' not supported inside 'finally' clause";
2502 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002504 if (!c->u->u_nfblocks)
2505 return compiler_error(c, LOOP_ERROR_MSG);
2506 i = c->u->u_nfblocks - 1;
2507 switch (c->u->u_fblock[i].fb_type) {
2508 case LOOP:
2509 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
2510 break;
2511 case EXCEPT:
2512 case FINALLY_TRY:
2513 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
2514 /* Prevent continue anywhere under a finally
2515 even if hidden in a sub-try or except. */
2516 if (c->u->u_fblock[i].fb_type == FINALLY_END)
2517 return compiler_error(c, IN_FINALLY_ERROR_MSG);
2518 }
2519 if (i == -1)
2520 return compiler_error(c, LOOP_ERROR_MSG);
2521 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
2522 break;
2523 case FINALLY_END:
2524 return compiler_error(c, IN_FINALLY_ERROR_MSG);
2525 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002527 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002528}
2529
2530/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002531
2532 SETUP_FINALLY L
2533 <code for body>
2534 POP_BLOCK
2535 LOAD_CONST <None>
2536 L: <code for finalbody>
2537 END_FINALLY
2538
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539 The special instructions use the block stack. Each block
2540 stack entry contains the instruction that created it (here
2541 SETUP_FINALLY), the level of the value stack at the time the
2542 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002544 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002545 Pushes the current value stack level and the label
2546 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002547 POP_BLOCK:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002548 Pops en entry from the block stack, and pops the value
2549 stack until its level is the same as indicated on the
2550 block stack. (The label is ignored.)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002551 END_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 Pops a variable number of entries from the *value* stack
2553 and re-raises the exception they specify. The number of
2554 entries popped depends on the (pseudo) exception type.
2555
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556 The block stack is unwound when an exception is raised:
2557 when a SETUP_FINALLY entry is found, the exception is pushed
2558 onto the value stack (and the exception condition is cleared),
2559 and the interpreter jumps to the label gotten from the block
2560 stack.
2561*/
2562
2563static int
2564compiler_try_finally(struct compiler *c, stmt_ty s)
2565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002566 basicblock *body, *end;
2567 body = compiler_new_block(c);
2568 end = compiler_new_block(c);
2569 if (body == NULL || end == NULL)
2570 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 ADDOP_JREL(c, SETUP_FINALLY, end);
2573 compiler_use_next_block(c, body);
2574 if (!compiler_push_fblock(c, FINALLY_TRY, body))
2575 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002576 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
2577 if (!compiler_try_except(c, s))
2578 return 0;
2579 }
2580 else {
2581 VISIT_SEQ(c, stmt, s->v.Try.body);
2582 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002583 ADDOP(c, POP_BLOCK);
2584 compiler_pop_fblock(c, FINALLY_TRY, body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002586 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2587 compiler_use_next_block(c, end);
2588 if (!compiler_push_fblock(c, FINALLY_END, end))
2589 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002590 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002591 ADDOP(c, END_FINALLY);
2592 compiler_pop_fblock(c, FINALLY_END, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002594 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002595}
2596
2597/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002598 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002599 (The contents of the value stack is shown in [], with the top
2600 at the right; 'tb' is trace-back info, 'val' the exception's
2601 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002602
2603 Value stack Label Instruction Argument
2604 [] SETUP_EXCEPT L1
2605 [] <code for S>
2606 [] POP_BLOCK
2607 [] JUMP_FORWARD L0
2608
2609 [tb, val, exc] L1: DUP )
2610 [tb, val, exc, exc] <evaluate E1> )
2611 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2612 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
2613 [tb, val, exc] POP
2614 [tb, val] <assign to V1> (or POP if no V1)
2615 [tb] POP
2616 [] <code for S1>
2617 JUMP_FORWARD L0
2618
2619 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620 .............................etc.......................
2621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002622 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
2623
2624 [] L0: <next statement>
2625
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002626 Of course, parts are not generated if Vi or Ei is not present.
2627*/
2628static int
2629compiler_try_except(struct compiler *c, stmt_ty s)
2630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002631 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01002632 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002634 body = compiler_new_block(c);
2635 except = compiler_new_block(c);
2636 orelse = compiler_new_block(c);
2637 end = compiler_new_block(c);
2638 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
2639 return 0;
2640 ADDOP_JREL(c, SETUP_EXCEPT, except);
2641 compiler_use_next_block(c, body);
2642 if (!compiler_push_fblock(c, EXCEPT, body))
2643 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002644 VISIT_SEQ(c, stmt, s->v.Try.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 ADDOP(c, POP_BLOCK);
2646 compiler_pop_fblock(c, EXCEPT, body);
2647 ADDOP_JREL(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002648 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002649 compiler_use_next_block(c, except);
2650 for (i = 0; i < n; i++) {
2651 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002652 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 if (!handler->v.ExceptHandler.type && i < n-1)
2654 return compiler_error(c, "default 'except:' must be last");
2655 c->u->u_lineno_set = 0;
2656 c->u->u_lineno = handler->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00002657 c->u->u_col_offset = handler->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002658 except = compiler_new_block(c);
2659 if (except == NULL)
2660 return 0;
2661 if (handler->v.ExceptHandler.type) {
2662 ADDOP(c, DUP_TOP);
2663 VISIT(c, expr, handler->v.ExceptHandler.type);
2664 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2665 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
2666 }
2667 ADDOP(c, POP_TOP);
2668 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002669 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00002670
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002671 cleanup_end = compiler_new_block(c);
2672 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05002673 if (!(cleanup_end || cleanup_body))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002674 return 0;
Guido van Rossumb940e112007-01-10 16:19:56 +00002675
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002676 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
2677 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002678
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002679 /*
2680 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03002681 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002682 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03002683 try:
2684 # body
2685 finally:
2686 name = None
2687 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002688 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002689
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002690 /* second try: */
2691 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2692 compiler_use_next_block(c, cleanup_body);
2693 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2694 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002695
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002696 /* second # body */
2697 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2698 ADDOP(c, POP_BLOCK);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002699 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002701 /* finally: */
2702 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2703 compiler_use_next_block(c, cleanup_end);
2704 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
2705 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002706
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002707 /* name = None */
2708 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2709 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002710
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002711 /* del name */
2712 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002713
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002714 ADDOP(c, END_FINALLY);
Serhiy Storchakad4864c62018-01-09 21:54:52 +02002715 ADDOP(c, POP_EXCEPT);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002716 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002717 }
2718 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002719 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002720
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002721 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05002722 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002723 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002724
Guido van Rossumb940e112007-01-10 16:19:56 +00002725 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002726 ADDOP(c, POP_TOP);
2727 compiler_use_next_block(c, cleanup_body);
2728 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2729 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002730 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002731 ADDOP(c, POP_EXCEPT);
2732 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002733 }
2734 ADDOP_JREL(c, JUMP_FORWARD, end);
2735 compiler_use_next_block(c, except);
2736 }
2737 ADDOP(c, END_FINALLY);
2738 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002739 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 compiler_use_next_block(c, end);
2741 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002742}
2743
2744static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002745compiler_try(struct compiler *c, stmt_ty s) {
2746 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
2747 return compiler_try_finally(c, s);
2748 else
2749 return compiler_try_except(c, s);
2750}
2751
2752
2753static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002754compiler_import_as(struct compiler *c, identifier name, identifier asname)
2755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 /* The IMPORT_NAME opcode was already generated. This function
2757 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002759 If there is a dot in name, we need to split it and emit a
Serhiy Storchakaf93234b2017-05-09 22:31:05 +03002760 IMPORT_FROM for each name.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03002762 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
2763 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002764 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03002765 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002766 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 /* Consume the base module name to get the first attribute */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03002768 while (1) {
2769 Py_ssize_t pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002770 PyObject *attr;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03002771 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002772 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03002773 return 0;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03002774 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002775 if (!attr)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03002776 return 0;
Miss Islington (bot)9e96e7b2018-03-31 16:41:28 -07002777 ADDOP_N(c, IMPORT_FROM, attr, names);
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03002778 if (dot == -1) {
2779 break;
2780 }
2781 ADDOP(c, ROT_TWO);
2782 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002783 }
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03002784 if (!compiler_nameop(c, asname, Store)) {
2785 return 0;
2786 }
2787 ADDOP(c, POP_TOP);
2788 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002789 }
2790 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002791}
2792
2793static int
2794compiler_import(struct compiler *c, stmt_ty s)
2795{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 /* The Import node stores a module name like a.b.c as a single
2797 string. This is convenient for all cases except
2798 import a.b.c as d
2799 where we need to parse that string to extract the individual
2800 module names.
2801 XXX Perhaps change the representation to make this case simpler?
2802 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01002803 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 for (i = 0; i < n; i++) {
2806 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
2807 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002808
Serhiy Storchakaba85d692017-03-30 09:09:41 +03002809 ADDOP_O(c, LOAD_CONST, _PyLong_Zero, consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2811 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 if (alias->asname) {
2814 r = compiler_import_as(c, alias->name, alias->asname);
2815 if (!r)
2816 return r;
2817 }
2818 else {
2819 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002820 Py_ssize_t dot = PyUnicode_FindChar(
2821 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02002822 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002823 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02002824 if (tmp == NULL)
2825 return 0;
2826 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002828 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 Py_DECREF(tmp);
2830 }
2831 if (!r)
2832 return r;
2833 }
2834 }
2835 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002836}
2837
2838static int
2839compiler_from_import(struct compiler *c, stmt_ty s)
2840{
Victor Stinnerad9a0662013-11-19 22:23:20 +01002841 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Miss Islington (bot)471364b2018-03-24 14:27:06 -07002842 PyObject *level, *names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00002844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 if (!empty_string) {
2846 empty_string = PyUnicode_FromString("");
2847 if (!empty_string)
2848 return 0;
2849 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002851 level = PyLong_FromLong(s->v.ImportFrom.level);
2852 if (!level) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 return 0;
2854 }
Miss Islington (bot)471364b2018-03-24 14:27:06 -07002855 ADDOP_N(c, LOAD_CONST, level, consts);
2856
2857 names = PyTuple_New(n);
2858 if (!names)
2859 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 /* build up the names */
2862 for (i = 0; i < n; i++) {
2863 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2864 Py_INCREF(alias->name);
2865 PyTuple_SET_ITEM(names, i, alias->name);
2866 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02002869 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 Py_DECREF(names);
2871 return compiler_error(c, "from __future__ imports must occur "
2872 "at the beginning of the file");
2873 }
Miss Islington (bot)471364b2018-03-24 14:27:06 -07002874 ADDOP_N(c, LOAD_CONST, names, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 if (s->v.ImportFrom.module) {
2877 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2878 }
2879 else {
2880 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
2881 }
2882 for (i = 0; i < n; i++) {
2883 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2884 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002885
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002886 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 assert(n == 1);
2888 ADDOP(c, IMPORT_STAR);
2889 return 1;
2890 }
2891
2892 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2893 store_name = alias->name;
2894 if (alias->asname)
2895 store_name = alias->asname;
2896
2897 if (!compiler_nameop(c, store_name, Store)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002898 return 0;
2899 }
2900 }
2901 /* remove imported module */
2902 ADDOP(c, POP_TOP);
2903 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002904}
2905
2906static int
2907compiler_assert(struct compiler *c, stmt_ty s)
2908{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 static PyObject *assertion_error = NULL;
2910 basicblock *end;
Victor Stinner14e461d2013-08-26 22:28:21 +02002911 PyObject* msg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002912
Georg Brandl8334fd92010-12-04 10:26:46 +00002913 if (c->c_optimize)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002914 return 1;
2915 if (assertion_error == NULL) {
2916 assertion_error = PyUnicode_InternFromString("AssertionError");
2917 if (assertion_error == NULL)
2918 return 0;
2919 }
2920 if (s->v.Assert.test->kind == Tuple_kind &&
2921 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
Victor Stinner14e461d2013-08-26 22:28:21 +02002922 msg = PyUnicode_FromString("assertion is always true, "
2923 "perhaps remove parentheses?");
2924 if (msg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 return 0;
Victor Stinner14e461d2013-08-26 22:28:21 +02002926 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
2927 c->c_filename, c->u->u_lineno,
2928 NULL, NULL) == -1) {
2929 Py_DECREF(msg);
2930 return 0;
2931 }
2932 Py_DECREF(msg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002933 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 end = compiler_new_block(c);
2935 if (end == NULL)
2936 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002937 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
2938 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2940 if (s->v.Assert.msg) {
2941 VISIT(c, expr, s->v.Assert.msg);
2942 ADDOP_I(c, CALL_FUNCTION, 1);
2943 }
2944 ADDOP_I(c, RAISE_VARARGS, 1);
2945 compiler_use_next_block(c, end);
2946 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002947}
2948
2949static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01002950compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
2951{
2952 if (c->c_interactive && c->c_nestlevel <= 1) {
2953 VISIT(c, expr, value);
2954 ADDOP(c, PRINT_EXPR);
2955 return 1;
2956 }
2957
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002958 if (is_const(value)) {
Victor Stinner15a30952016-02-08 22:45:06 +01002959 /* ignore constant statement */
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01002960 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01002961 }
2962
2963 VISIT(c, expr, value);
2964 ADDOP(c, POP_TOP);
2965 return 1;
2966}
2967
2968static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002969compiler_visit_stmt(struct compiler *c, stmt_ty s)
2970{
Victor Stinnerad9a0662013-11-19 22:23:20 +01002971 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002973 /* Always assign a lineno to the next instruction for a stmt. */
2974 c->u->u_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00002975 c->u->u_col_offset = s->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002976 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 switch (s->kind) {
2979 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04002980 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 case ClassDef_kind:
2982 return compiler_class(c, s);
2983 case Return_kind:
2984 if (c->u->u_ste->ste_type != FunctionBlock)
2985 return compiler_error(c, "'return' outside function");
2986 if (s->v.Return.value) {
Yury Selivanoveb636452016-09-08 22:01:51 -07002987 if (c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2988 return compiler_error(
2989 c, "'return' with value in async generator");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 VISIT(c, expr, s->v.Return.value);
2991 }
2992 else
2993 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2994 ADDOP(c, RETURN_VALUE);
2995 break;
2996 case Delete_kind:
2997 VISIT_SEQ(c, expr, s->v.Delete.targets)
2998 break;
2999 case Assign_kind:
3000 n = asdl_seq_LEN(s->v.Assign.targets);
3001 VISIT(c, expr, s->v.Assign.value);
3002 for (i = 0; i < n; i++) {
3003 if (i < n - 1)
3004 ADDOP(c, DUP_TOP);
3005 VISIT(c, expr,
3006 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3007 }
3008 break;
3009 case AugAssign_kind:
3010 return compiler_augassign(c, s);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003011 case AnnAssign_kind:
3012 return compiler_annassign(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003013 case For_kind:
3014 return compiler_for(c, s);
3015 case While_kind:
3016 return compiler_while(c, s);
3017 case If_kind:
3018 return compiler_if(c, s);
3019 case Raise_kind:
3020 n = 0;
3021 if (s->v.Raise.exc) {
3022 VISIT(c, expr, s->v.Raise.exc);
3023 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003024 if (s->v.Raise.cause) {
3025 VISIT(c, expr, s->v.Raise.cause);
3026 n++;
3027 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003028 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01003029 ADDOP_I(c, RAISE_VARARGS, (int)n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003030 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003031 case Try_kind:
3032 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 case Assert_kind:
3034 return compiler_assert(c, s);
3035 case Import_kind:
3036 return compiler_import(c, s);
3037 case ImportFrom_kind:
3038 return compiler_from_import(c, s);
3039 case Global_kind:
3040 case Nonlocal_kind:
3041 break;
3042 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003043 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003044 case Pass_kind:
3045 break;
3046 case Break_kind:
3047 if (!compiler_in_loop(c))
3048 return compiler_error(c, "'break' outside loop");
3049 ADDOP(c, BREAK_LOOP);
3050 break;
3051 case Continue_kind:
3052 return compiler_continue(c);
3053 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003054 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04003055 case AsyncFunctionDef_kind:
3056 return compiler_function(c, s, 1);
3057 case AsyncWith_kind:
3058 return compiler_async_with(c, s, 0);
3059 case AsyncFor_kind:
3060 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 }
Yury Selivanov75445082015-05-11 22:57:16 -04003062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003064}
3065
3066static int
3067unaryop(unaryop_ty op)
3068{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003069 switch (op) {
3070 case Invert:
3071 return UNARY_INVERT;
3072 case Not:
3073 return UNARY_NOT;
3074 case UAdd:
3075 return UNARY_POSITIVE;
3076 case USub:
3077 return UNARY_NEGATIVE;
3078 default:
3079 PyErr_Format(PyExc_SystemError,
3080 "unary op %d should not be possible", op);
3081 return 0;
3082 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003083}
3084
3085static int
3086binop(struct compiler *c, operator_ty op)
3087{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 switch (op) {
3089 case Add:
3090 return BINARY_ADD;
3091 case Sub:
3092 return BINARY_SUBTRACT;
3093 case Mult:
3094 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003095 case MatMult:
3096 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003097 case Div:
3098 return BINARY_TRUE_DIVIDE;
3099 case Mod:
3100 return BINARY_MODULO;
3101 case Pow:
3102 return BINARY_POWER;
3103 case LShift:
3104 return BINARY_LSHIFT;
3105 case RShift:
3106 return BINARY_RSHIFT;
3107 case BitOr:
3108 return BINARY_OR;
3109 case BitXor:
3110 return BINARY_XOR;
3111 case BitAnd:
3112 return BINARY_AND;
3113 case FloorDiv:
3114 return BINARY_FLOOR_DIVIDE;
3115 default:
3116 PyErr_Format(PyExc_SystemError,
3117 "binary op %d should not be possible", op);
3118 return 0;
3119 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003120}
3121
3122static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003123inplace_binop(struct compiler *c, operator_ty op)
3124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003125 switch (op) {
3126 case Add:
3127 return INPLACE_ADD;
3128 case Sub:
3129 return INPLACE_SUBTRACT;
3130 case Mult:
3131 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003132 case MatMult:
3133 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 case Div:
3135 return INPLACE_TRUE_DIVIDE;
3136 case Mod:
3137 return INPLACE_MODULO;
3138 case Pow:
3139 return INPLACE_POWER;
3140 case LShift:
3141 return INPLACE_LSHIFT;
3142 case RShift:
3143 return INPLACE_RSHIFT;
3144 case BitOr:
3145 return INPLACE_OR;
3146 case BitXor:
3147 return INPLACE_XOR;
3148 case BitAnd:
3149 return INPLACE_AND;
3150 case FloorDiv:
3151 return INPLACE_FLOOR_DIVIDE;
3152 default:
3153 PyErr_Format(PyExc_SystemError,
3154 "inplace binary op %d should not be possible", op);
3155 return 0;
3156 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003157}
3158
3159static int
3160compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3161{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003162 int op, scope;
3163 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003166 PyObject *dict = c->u->u_names;
3167 PyObject *mangled;
3168 /* XXX AugStore isn't used anywhere! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003169
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003170 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3171 !_PyUnicode_EqualToASCIIString(name, "True") &&
3172 !_PyUnicode_EqualToASCIIString(name, "False"));
Benjamin Peterson70b224d2012-12-06 17:49:58 -05003173
Serhiy Storchakabd6ec4d2017-12-18 14:29:12 +02003174 mangled = _Py_Mangle(c->u->u_private, name);
3175 if (!mangled)
3176 return 0;
3177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003178 op = 0;
3179 optype = OP_NAME;
3180 scope = PyST_GetScope(c->u->u_ste, mangled);
3181 switch (scope) {
3182 case FREE:
3183 dict = c->u->u_freevars;
3184 optype = OP_DEREF;
3185 break;
3186 case CELL:
3187 dict = c->u->u_cellvars;
3188 optype = OP_DEREF;
3189 break;
3190 case LOCAL:
3191 if (c->u->u_ste->ste_type == FunctionBlock)
3192 optype = OP_FAST;
3193 break;
3194 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04003195 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003196 optype = OP_GLOBAL;
3197 break;
3198 case GLOBAL_EXPLICIT:
3199 optype = OP_GLOBAL;
3200 break;
3201 default:
3202 /* scope can be 0 */
3203 break;
3204 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003206 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003207 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003209 switch (optype) {
3210 case OP_DEREF:
3211 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003212 case Load:
3213 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3214 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003215 case Store: op = STORE_DEREF; break;
3216 case AugLoad:
3217 case AugStore:
3218 break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003219 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003220 case Param:
3221 default:
3222 PyErr_SetString(PyExc_SystemError,
3223 "param invalid for deref variable");
3224 return 0;
3225 }
3226 break;
3227 case OP_FAST:
3228 switch (ctx) {
3229 case Load: op = LOAD_FAST; break;
3230 case Store: op = STORE_FAST; break;
3231 case Del: op = DELETE_FAST; break;
3232 case AugLoad:
3233 case AugStore:
3234 break;
3235 case Param:
3236 default:
3237 PyErr_SetString(PyExc_SystemError,
3238 "param invalid for local variable");
3239 return 0;
3240 }
Miss Islington (bot)9e96e7b2018-03-31 16:41:28 -07003241 ADDOP_N(c, op, mangled, varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242 return 1;
3243 case OP_GLOBAL:
3244 switch (ctx) {
3245 case Load: op = LOAD_GLOBAL; break;
3246 case Store: op = STORE_GLOBAL; break;
3247 case Del: op = DELETE_GLOBAL; break;
3248 case AugLoad:
3249 case AugStore:
3250 break;
3251 case Param:
3252 default:
3253 PyErr_SetString(PyExc_SystemError,
3254 "param invalid for global variable");
3255 return 0;
3256 }
3257 break;
3258 case OP_NAME:
3259 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00003260 case Load: op = LOAD_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 case Store: op = STORE_NAME; break;
3262 case Del: op = DELETE_NAME; break;
3263 case AugLoad:
3264 case AugStore:
3265 break;
3266 case Param:
3267 default:
3268 PyErr_SetString(PyExc_SystemError,
3269 "param invalid for name variable");
3270 return 0;
3271 }
3272 break;
3273 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003275 assert(op);
3276 arg = compiler_add_o(c, dict, mangled);
3277 Py_DECREF(mangled);
3278 if (arg < 0)
3279 return 0;
3280 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003281}
3282
3283static int
3284compiler_boolop(struct compiler *c, expr_ty e)
3285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003286 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003287 int jumpi;
3288 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 asdl_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003291 assert(e->kind == BoolOp_kind);
3292 if (e->v.BoolOp.op == And)
3293 jumpi = JUMP_IF_FALSE_OR_POP;
3294 else
3295 jumpi = JUMP_IF_TRUE_OR_POP;
3296 end = compiler_new_block(c);
3297 if (end == NULL)
3298 return 0;
3299 s = e->v.BoolOp.values;
3300 n = asdl_seq_LEN(s) - 1;
3301 assert(n >= 0);
3302 for (i = 0; i < n; ++i) {
3303 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
3304 ADDOP_JABS(c, jumpi, end);
3305 }
3306 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3307 compiler_use_next_block(c, end);
3308 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003309}
3310
3311static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003312starunpack_helper(struct compiler *c, asdl_seq *elts,
3313 int single_op, int inner_op, int outer_op)
3314{
3315 Py_ssize_t n = asdl_seq_LEN(elts);
3316 Py_ssize_t i, nsubitems = 0, nseen = 0;
3317 for (i = 0; i < n; i++) {
3318 expr_ty elt = asdl_seq_GET(elts, i);
3319 if (elt->kind == Starred_kind) {
3320 if (nseen) {
3321 ADDOP_I(c, inner_op, nseen);
3322 nseen = 0;
3323 nsubitems++;
3324 }
3325 VISIT(c, expr, elt->v.Starred.value);
3326 nsubitems++;
3327 }
3328 else {
3329 VISIT(c, expr, elt);
3330 nseen++;
3331 }
3332 }
3333 if (nsubitems) {
3334 if (nseen) {
3335 ADDOP_I(c, inner_op, nseen);
3336 nsubitems++;
3337 }
3338 ADDOP_I(c, outer_op, nsubitems);
3339 }
3340 else
3341 ADDOP_I(c, single_op, nseen);
3342 return 1;
3343}
3344
3345static int
3346assignment_helper(struct compiler *c, asdl_seq *elts)
3347{
3348 Py_ssize_t n = asdl_seq_LEN(elts);
3349 Py_ssize_t i;
3350 int seen_star = 0;
3351 for (i = 0; i < n; i++) {
3352 expr_ty elt = asdl_seq_GET(elts, i);
3353 if (elt->kind == Starred_kind && !seen_star) {
3354 if ((i >= (1 << 8)) ||
3355 (n-i-1 >= (INT_MAX >> 8)))
3356 return compiler_error(c,
3357 "too many expressions in "
3358 "star-unpacking assignment");
3359 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3360 seen_star = 1;
3361 asdl_seq_SET(elts, i, elt->v.Starred.value);
3362 }
3363 else if (elt->kind == Starred_kind) {
3364 return compiler_error(c,
3365 "two starred expressions in assignment");
3366 }
3367 }
3368 if (!seen_star) {
3369 ADDOP_I(c, UNPACK_SEQUENCE, n);
3370 }
3371 VISIT_SEQ(c, expr, elts);
3372 return 1;
3373}
3374
3375static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003376compiler_list(struct compiler *c, expr_ty e)
3377{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003378 asdl_seq *elts = e->v.List.elts;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003379 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003380 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003381 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003382 else if (e->v.List.ctx == Load) {
3383 return starunpack_helper(c, elts,
3384 BUILD_LIST, BUILD_TUPLE, BUILD_LIST_UNPACK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003385 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003386 else
3387 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003388 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003389}
3390
3391static int
3392compiler_tuple(struct compiler *c, expr_ty e)
3393{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003394 asdl_seq *elts = e->v.Tuple.elts;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003395 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003396 return assignment_helper(c, elts);
3397 }
3398 else if (e->v.Tuple.ctx == Load) {
3399 return starunpack_helper(c, elts,
3400 BUILD_TUPLE, BUILD_TUPLE, BUILD_TUPLE_UNPACK);
3401 }
3402 else
3403 VISIT_SEQ(c, expr, elts);
3404 return 1;
3405}
3406
3407static int
3408compiler_set(struct compiler *c, expr_ty e)
3409{
3410 return starunpack_helper(c, e->v.Set.elts, BUILD_SET,
3411 BUILD_SET, BUILD_SET_UNPACK);
3412}
3413
3414static int
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003415are_all_items_const(asdl_seq *seq, Py_ssize_t begin, Py_ssize_t end)
3416{
3417 Py_ssize_t i;
3418 for (i = begin; i < end; i++) {
3419 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
3420 if (key == NULL || !is_const(key))
3421 return 0;
3422 }
3423 return 1;
3424}
3425
3426static int
3427compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3428{
3429 Py_ssize_t i, n = end - begin;
3430 PyObject *keys, *key;
3431 if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) {
3432 for (i = begin; i < end; i++) {
3433 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3434 }
3435 keys = PyTuple_New(n);
3436 if (keys == NULL) {
3437 return 0;
3438 }
3439 for (i = begin; i < end; i++) {
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02003440 key = get_const_value((expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003441 Py_INCREF(key);
3442 PyTuple_SET_ITEM(keys, i - begin, key);
3443 }
3444 ADDOP_N(c, LOAD_CONST, keys, consts);
3445 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3446 }
3447 else {
3448 for (i = begin; i < end; i++) {
3449 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3450 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3451 }
3452 ADDOP_I(c, BUILD_MAP, n);
3453 }
3454 return 1;
3455}
3456
3457static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003458compiler_dict(struct compiler *c, expr_ty e)
3459{
Victor Stinner976bb402016-03-23 11:36:19 +01003460 Py_ssize_t i, n, elements;
3461 int containers;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003462 int is_unpacking = 0;
3463 n = asdl_seq_LEN(e->v.Dict.values);
3464 containers = 0;
3465 elements = 0;
3466 for (i = 0; i < n; i++) {
3467 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
3468 if (elements == 0xFFFF || (elements && is_unpacking)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003469 if (!compiler_subdict(c, e, i - elements, i))
3470 return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003471 containers++;
3472 elements = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003473 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003474 if (is_unpacking) {
3475 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3476 containers++;
3477 }
3478 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003479 elements++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 }
3481 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003482 if (elements || containers == 0) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003483 if (!compiler_subdict(c, e, n - elements, n))
3484 return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003485 containers++;
3486 }
3487 /* If there is more than one dict, they need to be merged into a new
3488 * dict. If there is one dict and it's an unpacking, then it needs
3489 * to be copied into a new dict." */
Serhiy Storchaka3d85fae2016-11-28 20:56:37 +02003490 if (containers > 1 || is_unpacking) {
3491 ADDOP_I(c, BUILD_MAP_UNPACK, containers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003492 }
3493 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003494}
3495
3496static int
3497compiler_compare(struct compiler *c, expr_ty e)
3498{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003499 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003501 VISIT(c, expr, e->v.Compare.left);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003502 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
3503 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
3504 if (n == 0) {
3505 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
3506 ADDOP_I(c, COMPARE_OP,
3507 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, 0))));
3508 }
3509 else {
3510 basicblock *cleanup = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003511 if (cleanup == NULL)
3512 return 0;
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003513 for (i = 0; i < n; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003514 VISIT(c, expr,
3515 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003516 ADDOP(c, DUP_TOP);
3517 ADDOP(c, ROT_THREE);
3518 ADDOP_I(c, COMPARE_OP,
3519 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, i))));
3520 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
3521 NEXT_BLOCK(c);
3522 }
3523 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
3524 ADDOP_I(c, COMPARE_OP,
3525 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n))));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003526 basicblock *end = compiler_new_block(c);
3527 if (end == NULL)
3528 return 0;
3529 ADDOP_JREL(c, JUMP_FORWARD, end);
3530 compiler_use_next_block(c, cleanup);
3531 ADDOP(c, ROT_TWO);
3532 ADDOP(c, POP_TOP);
3533 compiler_use_next_block(c, end);
3534 }
3535 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003536}
3537
3538static int
Yury Selivanovf2392132016-12-13 19:03:51 -05003539maybe_optimize_method_call(struct compiler *c, expr_ty e)
3540{
3541 Py_ssize_t argsl, i;
3542 expr_ty meth = e->v.Call.func;
3543 asdl_seq *args = e->v.Call.args;
3544
3545 /* Check that the call node is an attribute access, and that
3546 the call doesn't have keyword parameters. */
3547 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
3548 asdl_seq_LEN(e->v.Call.keywords))
3549 return -1;
3550
3551 /* Check that there are no *varargs types of arguments. */
3552 argsl = asdl_seq_LEN(args);
3553 for (i = 0; i < argsl; i++) {
3554 expr_ty elt = asdl_seq_GET(args, i);
3555 if (elt->kind == Starred_kind) {
3556 return -1;
3557 }
3558 }
3559
3560 /* Alright, we can optimize the code. */
3561 VISIT(c, expr, meth->v.Attribute.value);
3562 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
3563 VISIT_SEQ(c, expr, e->v.Call.args);
3564 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
3565 return 1;
3566}
3567
3568static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003569compiler_call(struct compiler *c, expr_ty e)
3570{
Yury Selivanovf2392132016-12-13 19:03:51 -05003571 if (maybe_optimize_method_call(c, e) > 0)
3572 return 1;
3573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 VISIT(c, expr, e->v.Call.func);
3575 return compiler_call_helper(c, 0,
3576 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003577 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003578}
3579
Eric V. Smith235a6f02015-09-19 14:51:32 -04003580static int
3581compiler_joined_str(struct compiler *c, expr_ty e)
3582{
Eric V. Smith235a6f02015-09-19 14:51:32 -04003583 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02003584 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1)
3585 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
Eric V. Smith235a6f02015-09-19 14:51:32 -04003586 return 1;
3587}
3588
Eric V. Smitha78c7952015-11-03 12:45:05 -05003589/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04003590static int
3591compiler_formatted_value(struct compiler *c, expr_ty e)
3592{
Eric V. Smitha78c7952015-11-03 12:45:05 -05003593 /* Our oparg encodes 2 pieces of information: the conversion
3594 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04003595
Eric V. Smitha78c7952015-11-03 12:45:05 -05003596 Convert the conversion char to 2 bits:
3597 None: 000 0x0 FVC_NONE
3598 !s : 001 0x1 FVC_STR
3599 !r : 010 0x2 FVC_REPR
3600 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04003601
Eric V. Smitha78c7952015-11-03 12:45:05 -05003602 next bit is whether or not we have a format spec:
3603 yes : 100 0x4
3604 no : 000 0x0
3605 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04003606
Eric V. Smitha78c7952015-11-03 12:45:05 -05003607 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003608
Eric V. Smitha78c7952015-11-03 12:45:05 -05003609 /* Evaluate the expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04003610 VISIT(c, expr, e->v.FormattedValue.value);
3611
Eric V. Smitha78c7952015-11-03 12:45:05 -05003612 switch (e->v.FormattedValue.conversion) {
3613 case 's': oparg = FVC_STR; break;
3614 case 'r': oparg = FVC_REPR; break;
3615 case 'a': oparg = FVC_ASCII; break;
3616 case -1: oparg = FVC_NONE; break;
3617 default:
3618 PyErr_SetString(PyExc_SystemError,
3619 "Unrecognized conversion character");
3620 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003621 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04003622 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003623 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04003624 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003625 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003626 }
3627
Eric V. Smitha78c7952015-11-03 12:45:05 -05003628 /* And push our opcode and oparg */
3629 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith235a6f02015-09-19 14:51:32 -04003630 return 1;
3631}
3632
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003633static int
3634compiler_subkwargs(struct compiler *c, asdl_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
3635{
3636 Py_ssize_t i, n = end - begin;
3637 keyword_ty kw;
3638 PyObject *keys, *key;
3639 assert(n > 0);
3640 if (n > 1) {
3641 for (i = begin; i < end; i++) {
3642 kw = asdl_seq_GET(keywords, i);
3643 VISIT(c, expr, kw->value);
3644 }
3645 keys = PyTuple_New(n);
3646 if (keys == NULL) {
3647 return 0;
3648 }
3649 for (i = begin; i < end; i++) {
3650 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
3651 Py_INCREF(key);
3652 PyTuple_SET_ITEM(keys, i - begin, key);
3653 }
3654 ADDOP_N(c, LOAD_CONST, keys, consts);
3655 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3656 }
3657 else {
3658 /* a for loop only executes once */
3659 for (i = begin; i < end; i++) {
3660 kw = asdl_seq_GET(keywords, i);
3661 ADDOP_O(c, LOAD_CONST, kw->arg, consts);
3662 VISIT(c, expr, kw->value);
3663 }
3664 ADDOP_I(c, BUILD_MAP, n);
3665 }
3666 return 1;
3667}
3668
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003669/* shared code between compiler_call and compiler_class */
3670static int
3671compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01003672 int n, /* Args already pushed */
Victor Stinnerad9a0662013-11-19 22:23:20 +01003673 asdl_seq *args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003674 asdl_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003675{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003676 Py_ssize_t i, nseen, nelts, nkwelts;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003677 int mustdictunpack = 0;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003678
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003679 /* the number of tuples and dictionaries on the stack */
3680 Py_ssize_t nsubargs = 0, nsubkwargs = 0;
3681
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003682 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003683 nkwelts = asdl_seq_LEN(keywords);
3684
3685 for (i = 0; i < nkwelts; i++) {
3686 keyword_ty kw = asdl_seq_GET(keywords, i);
3687 if (kw->arg == NULL) {
3688 mustdictunpack = 1;
3689 break;
3690 }
3691 }
3692
3693 nseen = n; /* the number of positional arguments on the stack */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003694 for (i = 0; i < nelts; i++) {
3695 expr_ty elt = asdl_seq_GET(args, i);
3696 if (elt->kind == Starred_kind) {
3697 /* A star-arg. If we've seen positional arguments,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003698 pack the positional arguments into a tuple. */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003699 if (nseen) {
3700 ADDOP_I(c, BUILD_TUPLE, nseen);
3701 nseen = 0;
3702 nsubargs++;
3703 }
3704 VISIT(c, expr, elt->v.Starred.value);
3705 nsubargs++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003706 }
3707 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003708 VISIT(c, expr, elt);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003709 nseen++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003710 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003712
3713 /* Same dance again for keyword arguments */
Serhiy Storchakab7281052016-09-12 00:52:40 +03003714 if (nsubargs || mustdictunpack) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003715 if (nseen) {
3716 /* Pack up any trailing positional arguments. */
3717 ADDOP_I(c, BUILD_TUPLE, nseen);
3718 nsubargs++;
3719 }
Serhiy Storchakab7281052016-09-12 00:52:40 +03003720 if (nsubargs > 1) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003721 /* If we ended up with more than one stararg, we need
3722 to concatenate them into a single sequence. */
Serhiy Storchaka73442852016-10-02 10:33:46 +03003723 ADDOP_I(c, BUILD_TUPLE_UNPACK_WITH_CALL, nsubargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003724 }
3725 else if (nsubargs == 0) {
3726 ADDOP_I(c, BUILD_TUPLE, 0);
3727 }
3728 nseen = 0; /* the number of keyword arguments on the stack following */
3729 for (i = 0; i < nkwelts; i++) {
3730 keyword_ty kw = asdl_seq_GET(keywords, i);
3731 if (kw->arg == NULL) {
3732 /* A keyword argument unpacking. */
3733 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003734 if (!compiler_subkwargs(c, keywords, i - nseen, i))
3735 return 0;
3736 nsubkwargs++;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003737 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003738 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003739 VISIT(c, expr, kw->value);
3740 nsubkwargs++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003741 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003742 else {
3743 nseen++;
3744 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003745 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003746 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003747 /* Pack up any trailing keyword arguments. */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003748 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts))
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003749 return 0;
3750 nsubkwargs++;
3751 }
Serhiy Storchakab7281052016-09-12 00:52:40 +03003752 if (nsubkwargs > 1) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003753 /* Pack it all up */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003754 ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003755 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003756 ADDOP_I(c, CALL_FUNCTION_EX, nsubkwargs > 0);
3757 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003759 else if (nkwelts) {
3760 PyObject *names;
3761 VISIT_SEQ(c, keyword, keywords);
3762 names = PyTuple_New(nkwelts);
3763 if (names == NULL) {
3764 return 0;
3765 }
3766 for (i = 0; i < nkwelts; i++) {
3767 keyword_ty kw = asdl_seq_GET(keywords, i);
3768 Py_INCREF(kw->arg);
3769 PyTuple_SET_ITEM(names, i, kw->arg);
3770 }
3771 ADDOP_N(c, LOAD_CONST, names, consts);
3772 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
3773 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003774 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003775 else {
3776 ADDOP_I(c, CALL_FUNCTION, n + nelts);
3777 return 1;
3778 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003779}
3780
Nick Coghlan650f0d02007-04-15 12:05:43 +00003781
3782/* List and set comprehensions and generator expressions work by creating a
3783 nested function to perform the actual iteration. This means that the
3784 iteration variables don't leak into the current scope.
3785 The defined function is called immediately following its definition, with the
3786 result of that call being the result of the expression.
3787 The LC/SC version returns the populated container, while the GE version is
3788 flagged in symtable.c as a generator, so it returns the generator object
3789 when the function is called.
3790 This code *knows* that the loop cannot contain break, continue, or return,
3791 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
3792
3793 Possible cleanups:
3794 - iterate over the generator sequence instead of using recursion
3795*/
3796
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003797
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003798static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003799compiler_comprehension_generator(struct compiler *c,
3800 asdl_seq *generators, int gen_index,
3801 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003802{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003803 comprehension_ty gen;
3804 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
3805 if (gen->is_async) {
3806 return compiler_async_comprehension_generator(
3807 c, generators, gen_index, elt, val, type);
3808 } else {
3809 return compiler_sync_comprehension_generator(
3810 c, generators, gen_index, elt, val, type);
3811 }
3812}
3813
3814static int
3815compiler_sync_comprehension_generator(struct compiler *c,
3816 asdl_seq *generators, int gen_index,
3817 expr_ty elt, expr_ty val, int type)
3818{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003819 /* generate code for the iterator, then each of the ifs,
3820 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003822 comprehension_ty gen;
3823 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003824 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003826 start = compiler_new_block(c);
3827 skip = compiler_new_block(c);
3828 if_cleanup = compiler_new_block(c);
3829 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003831 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3832 anchor == NULL)
3833 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003835 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003837 if (gen_index == 0) {
3838 /* Receive outermost iter as an implicit argument */
3839 c->u->u_argcount = 1;
3840 ADDOP_I(c, LOAD_FAST, 0);
3841 }
3842 else {
3843 /* Sub-iter - calculate on the fly */
3844 VISIT(c, expr, gen->iter);
3845 ADDOP(c, GET_ITER);
3846 }
3847 compiler_use_next_block(c, start);
3848 ADDOP_JREL(c, FOR_ITER, anchor);
3849 NEXT_BLOCK(c);
3850 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003852 /* XXX this needs to be cleaned up...a lot! */
3853 n = asdl_seq_LEN(gen->ifs);
3854 for (i = 0; i < n; i++) {
3855 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003856 if (!compiler_jump_if(c, e, if_cleanup, 0))
3857 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003858 NEXT_BLOCK(c);
3859 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003861 if (++gen_index < asdl_seq_LEN(generators))
3862 if (!compiler_comprehension_generator(c,
3863 generators, gen_index,
3864 elt, val, type))
3865 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003867 /* only append after the last for generator */
3868 if (gen_index >= asdl_seq_LEN(generators)) {
3869 /* comprehension specific code */
3870 switch (type) {
3871 case COMP_GENEXP:
3872 VISIT(c, expr, elt);
3873 ADDOP(c, YIELD_VALUE);
3874 ADDOP(c, POP_TOP);
3875 break;
3876 case COMP_LISTCOMP:
3877 VISIT(c, expr, elt);
3878 ADDOP_I(c, LIST_APPEND, gen_index + 1);
3879 break;
3880 case COMP_SETCOMP:
3881 VISIT(c, expr, elt);
3882 ADDOP_I(c, SET_ADD, gen_index + 1);
3883 break;
3884 case COMP_DICTCOMP:
3885 /* With 'd[k] = v', v is evaluated before k, so we do
3886 the same. */
3887 VISIT(c, expr, val);
3888 VISIT(c, expr, elt);
3889 ADDOP_I(c, MAP_ADD, gen_index + 1);
3890 break;
3891 default:
3892 return 0;
3893 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003895 compiler_use_next_block(c, skip);
3896 }
3897 compiler_use_next_block(c, if_cleanup);
3898 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3899 compiler_use_next_block(c, anchor);
3900
3901 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003902}
3903
3904static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003905compiler_async_comprehension_generator(struct compiler *c,
3906 asdl_seq *generators, int gen_index,
3907 expr_ty elt, expr_ty val, int type)
3908{
3909 _Py_IDENTIFIER(StopAsyncIteration);
3910
3911 comprehension_ty gen;
Serhiy Storchakab9744e92018-03-23 14:35:33 +02003912 basicblock *if_cleanup, *try,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003913 *after_try, *except, *try_cleanup;
3914 Py_ssize_t i, n;
3915
3916 PyObject *stop_aiter_error = _PyUnicode_FromId(&PyId_StopAsyncIteration);
3917 if (stop_aiter_error == NULL) {
3918 return 0;
3919 }
3920
3921 try = compiler_new_block(c);
3922 after_try = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003923 except = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003924 if_cleanup = compiler_new_block(c);
Serhiy Storchakab9744e92018-03-23 14:35:33 +02003925 try_cleanup = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003926
Serhiy Storchakab9744e92018-03-23 14:35:33 +02003927 if (if_cleanup == NULL ||
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003928 try == NULL || after_try == NULL ||
Serhiy Storchaka9e94c0d2018-03-10 20:45:05 +02003929 except == NULL || try_cleanup == NULL) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003930 return 0;
3931 }
3932
3933 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
3934
3935 if (gen_index == 0) {
3936 /* Receive outermost iter as an implicit argument */
3937 c->u->u_argcount = 1;
3938 ADDOP_I(c, LOAD_FAST, 0);
3939 }
3940 else {
3941 /* Sub-iter - calculate on the fly */
3942 VISIT(c, expr, gen->iter);
3943 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003944 }
3945
3946 compiler_use_next_block(c, try);
3947
3948
3949 ADDOP_JREL(c, SETUP_EXCEPT, except);
3950 if (!compiler_push_fblock(c, EXCEPT, try))
3951 return 0;
3952
3953 ADDOP(c, GET_ANEXT);
3954 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3955 ADDOP(c, YIELD_FROM);
3956 VISIT(c, expr, gen->target);
3957 ADDOP(c, POP_BLOCK);
3958 compiler_pop_fblock(c, EXCEPT, try);
3959 ADDOP_JREL(c, JUMP_FORWARD, after_try);
3960
3961
3962 compiler_use_next_block(c, except);
3963 ADDOP(c, DUP_TOP);
3964 ADDOP_O(c, LOAD_GLOBAL, stop_aiter_error, names);
3965 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
Serhiy Storchakab9744e92018-03-23 14:35:33 +02003966 ADDOP_JABS(c, POP_JUMP_IF_TRUE, try_cleanup);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003967 ADDOP(c, END_FINALLY);
3968
3969 compiler_use_next_block(c, after_try);
3970
3971 n = asdl_seq_LEN(gen->ifs);
3972 for (i = 0; i < n; i++) {
3973 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003974 if (!compiler_jump_if(c, e, if_cleanup, 0))
3975 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003976 NEXT_BLOCK(c);
3977 }
3978
3979 if (++gen_index < asdl_seq_LEN(generators))
3980 if (!compiler_comprehension_generator(c,
3981 generators, gen_index,
3982 elt, val, type))
3983 return 0;
3984
3985 /* only append after the last for generator */
3986 if (gen_index >= asdl_seq_LEN(generators)) {
3987 /* comprehension specific code */
3988 switch (type) {
3989 case COMP_GENEXP:
3990 VISIT(c, expr, elt);
3991 ADDOP(c, YIELD_VALUE);
3992 ADDOP(c, POP_TOP);
3993 break;
3994 case COMP_LISTCOMP:
3995 VISIT(c, expr, elt);
3996 ADDOP_I(c, LIST_APPEND, gen_index + 1);
3997 break;
3998 case COMP_SETCOMP:
3999 VISIT(c, expr, elt);
4000 ADDOP_I(c, SET_ADD, gen_index + 1);
4001 break;
4002 case COMP_DICTCOMP:
4003 /* With 'd[k] = v', v is evaluated before k, so we do
4004 the same. */
4005 VISIT(c, expr, val);
4006 VISIT(c, expr, elt);
4007 ADDOP_I(c, MAP_ADD, gen_index + 1);
4008 break;
4009 default:
4010 return 0;
4011 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004012 }
4013 compiler_use_next_block(c, if_cleanup);
4014 ADDOP_JABS(c, JUMP_ABSOLUTE, try);
Serhiy Storchakab9744e92018-03-23 14:35:33 +02004015
4016 compiler_use_next_block(c, try_cleanup);
4017 ADDOP(c, POP_TOP);
4018 ADDOP(c, POP_TOP);
4019 ADDOP(c, POP_TOP);
4020 ADDOP(c, POP_EXCEPT); /* for SETUP_EXCEPT */
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004021 ADDOP(c, POP_TOP);
4022
4023 return 1;
4024}
4025
4026static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004027compiler_comprehension(struct compiler *c, expr_ty e, int type,
4028 identifier name, asdl_seq *generators, expr_ty elt,
4029 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004030{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004031 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004032 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004033 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004034 int is_async_function = c->u->u_ste->ste_coroutine;
4035 int is_async_generator = 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004036
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004037 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004038
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004039 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4040 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004041 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004042 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004043 }
4044
4045 is_async_generator = c->u->u_ste->ste_coroutine;
4046
Yury Selivanovb8ab9d32017-10-06 02:58:28 -04004047 if (is_async_generator && !is_async_function && type != COMP_GENEXP) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004048 if (e->lineno > c->u->u_lineno) {
4049 c->u->u_lineno = e->lineno;
4050 c->u->u_lineno_set = 0;
4051 }
4052 compiler_error(c, "asynchronous comprehension outside of "
4053 "an asynchronous function");
4054 goto error_in_scope;
4055 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004057 if (type != COMP_GENEXP) {
4058 int op;
4059 switch (type) {
4060 case COMP_LISTCOMP:
4061 op = BUILD_LIST;
4062 break;
4063 case COMP_SETCOMP:
4064 op = BUILD_SET;
4065 break;
4066 case COMP_DICTCOMP:
4067 op = BUILD_MAP;
4068 break;
4069 default:
4070 PyErr_Format(PyExc_SystemError,
4071 "unknown comprehension type %d", type);
4072 goto error_in_scope;
4073 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004075 ADDOP_I(c, op, 0);
4076 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004078 if (!compiler_comprehension_generator(c, generators, 0, elt,
4079 val, type))
4080 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004082 if (type != COMP_GENEXP) {
4083 ADDOP(c, RETURN_VALUE);
4084 }
4085
4086 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004087 qualname = c->u->u_qualname;
4088 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004089 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004090 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004091 goto error;
4092
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004093 if (!compiler_make_closure(c, co, 0, qualname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004094 goto error;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004095 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004096 Py_DECREF(co);
4097
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004098 VISIT(c, expr, outermost->iter);
4099
4100 if (outermost->is_async) {
4101 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004102 } else {
4103 ADDOP(c, GET_ITER);
4104 }
4105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004106 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004107
4108 if (is_async_generator && type != COMP_GENEXP) {
4109 ADDOP(c, GET_AWAITABLE);
4110 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4111 ADDOP(c, YIELD_FROM);
4112 }
4113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004114 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004115error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004116 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004117error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004118 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004119 Py_XDECREF(co);
4120 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004121}
4122
4123static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004124compiler_genexp(struct compiler *c, expr_ty e)
4125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004126 static identifier name;
4127 if (!name) {
4128 name = PyUnicode_FromString("<genexpr>");
4129 if (!name)
4130 return 0;
4131 }
4132 assert(e->kind == GeneratorExp_kind);
4133 return compiler_comprehension(c, e, COMP_GENEXP, name,
4134 e->v.GeneratorExp.generators,
4135 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004136}
4137
4138static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004139compiler_listcomp(struct compiler *c, expr_ty e)
4140{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004141 static identifier name;
4142 if (!name) {
4143 name = PyUnicode_FromString("<listcomp>");
4144 if (!name)
4145 return 0;
4146 }
4147 assert(e->kind == ListComp_kind);
4148 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4149 e->v.ListComp.generators,
4150 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004151}
4152
4153static int
4154compiler_setcomp(struct compiler *c, expr_ty e)
4155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004156 static identifier name;
4157 if (!name) {
4158 name = PyUnicode_FromString("<setcomp>");
4159 if (!name)
4160 return 0;
4161 }
4162 assert(e->kind == SetComp_kind);
4163 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4164 e->v.SetComp.generators,
4165 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004166}
4167
4168
4169static int
4170compiler_dictcomp(struct compiler *c, expr_ty e)
4171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004172 static identifier name;
4173 if (!name) {
4174 name = PyUnicode_FromString("<dictcomp>");
4175 if (!name)
4176 return 0;
4177 }
4178 assert(e->kind == DictComp_kind);
4179 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4180 e->v.DictComp.generators,
4181 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004182}
4183
4184
4185static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004186compiler_visit_keyword(struct compiler *c, keyword_ty k)
4187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004188 VISIT(c, expr, k->value);
4189 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004190}
4191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004192/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004193 whether they are true or false.
4194
4195 Return values: 1 for true, 0 for false, -1 for non-constant.
4196 */
4197
4198static int
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02004199expr_constant(expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004200{
Serhiy Storchaka3325a672017-12-15 12:35:48 +02004201 if (is_const(e)) {
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02004202 return PyObject_IsTrue(get_const_value(e));
Benjamin Peterson442f2092012-12-06 17:41:04 -05004203 }
Serhiy Storchaka3325a672017-12-15 12:35:48 +02004204 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004205}
4206
Yury Selivanov75445082015-05-11 22:57:16 -04004207
4208/*
4209 Implements the async with statement.
4210
4211 The semantics outlined in that PEP are as follows:
4212
4213 async with EXPR as VAR:
4214 BLOCK
4215
4216 It is implemented roughly as:
4217
4218 context = EXPR
4219 exit = context.__aexit__ # not calling it
4220 value = await context.__aenter__()
4221 try:
4222 VAR = value # if VAR present in the syntax
4223 BLOCK
4224 finally:
4225 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004226 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04004227 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004228 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04004229 if not (await exit(*exc)):
4230 raise
4231 */
4232static int
4233compiler_async_with(struct compiler *c, stmt_ty s, int pos)
4234{
4235 basicblock *block, *finally;
4236 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
4237
4238 assert(s->kind == AsyncWith_kind);
Zsolt Dollensteina93a6632018-04-27 15:33:37 -07004239 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
4240 return compiler_error(c, "'async with' outside async function");
4241 }
Yury Selivanov75445082015-05-11 22:57:16 -04004242
4243 block = compiler_new_block(c);
4244 finally = compiler_new_block(c);
4245 if (!block || !finally)
4246 return 0;
4247
4248 /* Evaluate EXPR */
4249 VISIT(c, expr, item->context_expr);
4250
4251 ADDOP(c, BEFORE_ASYNC_WITH);
4252 ADDOP(c, GET_AWAITABLE);
4253 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4254 ADDOP(c, YIELD_FROM);
4255
4256 ADDOP_JREL(c, SETUP_ASYNC_WITH, finally);
4257
4258 /* SETUP_ASYNC_WITH pushes a finally block. */
4259 compiler_use_next_block(c, block);
4260 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
4261 return 0;
4262 }
4263
4264 if (item->optional_vars) {
4265 VISIT(c, expr, item->optional_vars);
4266 }
4267 else {
4268 /* Discard result from context.__aenter__() */
4269 ADDOP(c, POP_TOP);
4270 }
4271
4272 pos++;
4273 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
4274 /* BLOCK code */
4275 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
4276 else if (!compiler_async_with(c, s, pos))
4277 return 0;
4278
4279 /* End of try block; start the finally block */
4280 ADDOP(c, POP_BLOCK);
4281 compiler_pop_fblock(c, FINALLY_TRY, block);
4282
4283 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4284 compiler_use_next_block(c, finally);
4285 if (!compiler_push_fblock(c, FINALLY_END, finally))
4286 return 0;
4287
4288 /* Finally block starts; context.__exit__ is on the stack under
4289 the exception or return information. Just issue our magic
4290 opcode. */
4291 ADDOP(c, WITH_CLEANUP_START);
4292
4293 ADDOP(c, GET_AWAITABLE);
4294 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4295 ADDOP(c, YIELD_FROM);
4296
4297 ADDOP(c, WITH_CLEANUP_FINISH);
4298
4299 /* Finally block ends. */
4300 ADDOP(c, END_FINALLY);
4301 compiler_pop_fblock(c, FINALLY_END, finally);
4302 return 1;
4303}
4304
4305
Guido van Rossumc2e20742006-02-27 22:32:47 +00004306/*
4307 Implements the with statement from PEP 343.
4308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004309 The semantics outlined in that PEP are as follows:
Guido van Rossumc2e20742006-02-27 22:32:47 +00004310
4311 with EXPR as VAR:
4312 BLOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004313
Guido van Rossumc2e20742006-02-27 22:32:47 +00004314 It is implemented roughly as:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004315
Thomas Wouters477c8d52006-05-27 19:21:47 +00004316 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00004317 exit = context.__exit__ # not calling it
4318 value = context.__enter__()
4319 try:
4320 VAR = value # if VAR present in the syntax
4321 BLOCK
4322 finally:
4323 if an exception was raised:
Serhiy Storchakad741a882015-06-11 00:06:39 +03004324 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004325 else:
Serhiy Storchakad741a882015-06-11 00:06:39 +03004326 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004327 exit(*exc)
4328 */
4329static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004330compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004331{
Guido van Rossumc2e20742006-02-27 22:32:47 +00004332 basicblock *block, *finally;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004333 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004334
4335 assert(s->kind == With_kind);
4336
Guido van Rossumc2e20742006-02-27 22:32:47 +00004337 block = compiler_new_block(c);
4338 finally = compiler_new_block(c);
4339 if (!block || !finally)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004340 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004341
Thomas Wouters477c8d52006-05-27 19:21:47 +00004342 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004343 VISIT(c, expr, item->context_expr);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004344 ADDOP_JREL(c, SETUP_WITH, finally);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004345
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004346 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00004347 compiler_use_next_block(c, block);
4348 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004349 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004350 }
4351
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004352 if (item->optional_vars) {
4353 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004354 }
4355 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004356 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004357 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004358 }
4359
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004360 pos++;
4361 if (pos == asdl_seq_LEN(s->v.With.items))
4362 /* BLOCK code */
4363 VISIT_SEQ(c, stmt, s->v.With.body)
4364 else if (!compiler_with(c, s, pos))
4365 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004366
4367 /* End of try block; start the finally block */
4368 ADDOP(c, POP_BLOCK);
4369 compiler_pop_fblock(c, FINALLY_TRY, block);
4370
4371 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4372 compiler_use_next_block(c, finally);
4373 if (!compiler_push_fblock(c, FINALLY_END, finally))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004374 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004375
Christian Heimesdd15f6c2008-03-16 00:07:10 +00004376 /* Finally block starts; context.__exit__ is on the stack under
4377 the exception or return information. Just issue our magic
4378 opcode. */
Yury Selivanov75445082015-05-11 22:57:16 -04004379 ADDOP(c, WITH_CLEANUP_START);
4380 ADDOP(c, WITH_CLEANUP_FINISH);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004381
4382 /* Finally block ends. */
4383 ADDOP(c, END_FINALLY);
4384 compiler_pop_fblock(c, FINALLY_END, finally);
4385 return 1;
4386}
4387
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004388static int
4389compiler_visit_expr(struct compiler *c, expr_ty e)
4390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004391 /* If expr e has a different line number than the last expr/stmt,
4392 set a new line number for the next instruction.
4393 */
4394 if (e->lineno > c->u->u_lineno) {
4395 c->u->u_lineno = e->lineno;
4396 c->u->u_lineno_set = 0;
4397 }
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00004398 /* Updating the column offset is always harmless. */
4399 c->u->u_col_offset = e->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004400 switch (e->kind) {
4401 case BoolOp_kind:
4402 return compiler_boolop(c, e);
4403 case BinOp_kind:
4404 VISIT(c, expr, e->v.BinOp.left);
4405 VISIT(c, expr, e->v.BinOp.right);
4406 ADDOP(c, binop(c, e->v.BinOp.op));
4407 break;
4408 case UnaryOp_kind:
4409 VISIT(c, expr, e->v.UnaryOp.operand);
4410 ADDOP(c, unaryop(e->v.UnaryOp.op));
4411 break;
4412 case Lambda_kind:
4413 return compiler_lambda(c, e);
4414 case IfExp_kind:
4415 return compiler_ifexp(c, e);
4416 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004417 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004418 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004419 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004420 case GeneratorExp_kind:
4421 return compiler_genexp(c, e);
4422 case ListComp_kind:
4423 return compiler_listcomp(c, e);
4424 case SetComp_kind:
4425 return compiler_setcomp(c, e);
4426 case DictComp_kind:
4427 return compiler_dictcomp(c, e);
4428 case Yield_kind:
4429 if (c->u->u_ste->ste_type != FunctionBlock)
4430 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00004431 if (e->v.Yield.value) {
4432 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004433 }
4434 else {
4435 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4436 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00004437 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004438 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00004439 case YieldFrom_kind:
4440 if (c->u->u_ste->ste_type != FunctionBlock)
4441 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04004442
4443 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
4444 return compiler_error(c, "'yield from' inside async function");
4445
Mark Dickinsonded35ae2012-11-25 14:36:26 +00004446 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004447 ADDOP(c, GET_YIELD_FROM_ITER);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00004448 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4449 ADDOP(c, YIELD_FROM);
4450 break;
Yury Selivanov75445082015-05-11 22:57:16 -04004451 case Await_kind:
4452 if (c->u->u_ste->ste_type != FunctionBlock)
4453 return compiler_error(c, "'await' outside function");
4454
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004455 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
4456 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION)
Yury Selivanov75445082015-05-11 22:57:16 -04004457 return compiler_error(c, "'await' outside async function");
4458
4459 VISIT(c, expr, e->v.Await.value);
4460 ADDOP(c, GET_AWAITABLE);
4461 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4462 ADDOP(c, YIELD_FROM);
4463 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004464 case Compare_kind:
4465 return compiler_compare(c, e);
4466 case Call_kind:
4467 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01004468 case Constant_kind:
4469 ADDOP_O(c, LOAD_CONST, e->v.Constant.value, consts);
4470 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004471 case Num_kind:
4472 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
4473 break;
4474 case Str_kind:
4475 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
4476 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004477 case JoinedStr_kind:
4478 return compiler_joined_str(c, e);
4479 case FormattedValue_kind:
4480 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004481 case Bytes_kind:
4482 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
4483 break;
4484 case Ellipsis_kind:
4485 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
4486 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05004487 case NameConstant_kind:
4488 ADDOP_O(c, LOAD_CONST, e->v.NameConstant.value, consts);
4489 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004490 /* The following exprs can be assignment targets. */
4491 case Attribute_kind:
4492 if (e->v.Attribute.ctx != AugStore)
4493 VISIT(c, expr, e->v.Attribute.value);
4494 switch (e->v.Attribute.ctx) {
4495 case AugLoad:
4496 ADDOP(c, DUP_TOP);
Stefan Krahf432a322017-08-21 13:09:59 +02004497 /* Fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 case Load:
4499 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
4500 break;
4501 case AugStore:
4502 ADDOP(c, ROT_TWO);
Stefan Krahf432a322017-08-21 13:09:59 +02004503 /* Fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004504 case Store:
4505 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
4506 break;
4507 case Del:
4508 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
4509 break;
4510 case Param:
4511 default:
4512 PyErr_SetString(PyExc_SystemError,
4513 "param invalid in attribute expression");
4514 return 0;
4515 }
4516 break;
4517 case Subscript_kind:
4518 switch (e->v.Subscript.ctx) {
4519 case AugLoad:
4520 VISIT(c, expr, e->v.Subscript.value);
4521 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
4522 break;
4523 case Load:
4524 VISIT(c, expr, e->v.Subscript.value);
4525 VISIT_SLICE(c, e->v.Subscript.slice, Load);
4526 break;
4527 case AugStore:
4528 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
4529 break;
4530 case Store:
4531 VISIT(c, expr, e->v.Subscript.value);
4532 VISIT_SLICE(c, e->v.Subscript.slice, Store);
4533 break;
4534 case Del:
4535 VISIT(c, expr, e->v.Subscript.value);
4536 VISIT_SLICE(c, e->v.Subscript.slice, Del);
4537 break;
4538 case Param:
4539 default:
4540 PyErr_SetString(PyExc_SystemError,
4541 "param invalid in subscript expression");
4542 return 0;
4543 }
4544 break;
4545 case Starred_kind:
4546 switch (e->v.Starred.ctx) {
4547 case Store:
4548 /* In all legitimate cases, the Starred node was already replaced
4549 * by compiler_list/compiler_tuple. XXX: is that okay? */
4550 return compiler_error(c,
4551 "starred assignment target must be in a list or tuple");
4552 default:
4553 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004554 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004555 }
4556 break;
4557 case Name_kind:
4558 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
4559 /* child nodes of List and Tuple will have expr_context set */
4560 case List_kind:
4561 return compiler_list(c, e);
4562 case Tuple_kind:
4563 return compiler_tuple(c, e);
4564 }
4565 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004566}
4567
4568static int
4569compiler_augassign(struct compiler *c, stmt_ty s)
4570{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004571 expr_ty e = s->v.AugAssign.target;
4572 expr_ty auge;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004574 assert(s->kind == AugAssign_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004576 switch (e->kind) {
4577 case Attribute_kind:
4578 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
4579 AugLoad, e->lineno, e->col_offset, c->c_arena);
4580 if (auge == NULL)
4581 return 0;
4582 VISIT(c, expr, auge);
4583 VISIT(c, expr, s->v.AugAssign.value);
4584 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
4585 auge->v.Attribute.ctx = AugStore;
4586 VISIT(c, expr, auge);
4587 break;
4588 case Subscript_kind:
4589 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
4590 AugLoad, e->lineno, e->col_offset, c->c_arena);
4591 if (auge == NULL)
4592 return 0;
4593 VISIT(c, expr, auge);
4594 VISIT(c, expr, s->v.AugAssign.value);
4595 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
4596 auge->v.Subscript.ctx = AugStore;
4597 VISIT(c, expr, auge);
4598 break;
4599 case Name_kind:
4600 if (!compiler_nameop(c, e->v.Name.id, Load))
4601 return 0;
4602 VISIT(c, expr, s->v.AugAssign.value);
4603 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
4604 return compiler_nameop(c, e->v.Name.id, Store);
4605 default:
4606 PyErr_Format(PyExc_SystemError,
4607 "invalid node type (%d) for augmented assignment",
4608 e->kind);
4609 return 0;
4610 }
4611 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004612}
4613
4614static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07004615check_ann_expr(struct compiler *c, expr_ty e)
4616{
4617 VISIT(c, expr, e);
4618 ADDOP(c, POP_TOP);
4619 return 1;
4620}
4621
4622static int
4623check_annotation(struct compiler *c, stmt_ty s)
4624{
4625 /* Annotations are only evaluated in a module or class. */
4626 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
4627 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
4628 return check_ann_expr(c, s->v.AnnAssign.annotation);
4629 }
4630 return 1;
4631}
4632
4633static int
4634check_ann_slice(struct compiler *c, slice_ty sl)
4635{
4636 switch(sl->kind) {
4637 case Index_kind:
4638 return check_ann_expr(c, sl->v.Index.value);
4639 case Slice_kind:
4640 if (sl->v.Slice.lower && !check_ann_expr(c, sl->v.Slice.lower)) {
4641 return 0;
4642 }
4643 if (sl->v.Slice.upper && !check_ann_expr(c, sl->v.Slice.upper)) {
4644 return 0;
4645 }
4646 if (sl->v.Slice.step && !check_ann_expr(c, sl->v.Slice.step)) {
4647 return 0;
4648 }
4649 break;
4650 default:
4651 PyErr_SetString(PyExc_SystemError,
4652 "unexpected slice kind");
4653 return 0;
4654 }
4655 return 1;
4656}
4657
4658static int
4659check_ann_subscr(struct compiler *c, slice_ty sl)
4660{
4661 /* We check that everything in a subscript is defined at runtime. */
4662 Py_ssize_t i, n;
4663
4664 switch (sl->kind) {
4665 case Index_kind:
4666 case Slice_kind:
4667 if (!check_ann_slice(c, sl)) {
4668 return 0;
4669 }
4670 break;
4671 case ExtSlice_kind:
4672 n = asdl_seq_LEN(sl->v.ExtSlice.dims);
4673 for (i = 0; i < n; i++) {
4674 slice_ty subsl = (slice_ty)asdl_seq_GET(sl->v.ExtSlice.dims, i);
4675 switch (subsl->kind) {
4676 case Index_kind:
4677 case Slice_kind:
4678 if (!check_ann_slice(c, subsl)) {
4679 return 0;
4680 }
4681 break;
4682 case ExtSlice_kind:
4683 default:
4684 PyErr_SetString(PyExc_SystemError,
4685 "extended slice invalid in nested slice");
4686 return 0;
4687 }
4688 }
4689 break;
4690 default:
4691 PyErr_Format(PyExc_SystemError,
4692 "invalid subscript kind %d", sl->kind);
4693 return 0;
4694 }
4695 return 1;
4696}
4697
4698static int
4699compiler_annassign(struct compiler *c, stmt_ty s)
4700{
4701 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07004702 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07004703
4704 assert(s->kind == AnnAssign_kind);
4705
4706 /* We perform the actual assignment first. */
4707 if (s->v.AnnAssign.value) {
4708 VISIT(c, expr, s->v.AnnAssign.value);
4709 VISIT(c, expr, targ);
4710 }
4711 switch (targ->kind) {
4712 case Name_kind:
4713 /* If we have a simple name in a module or class, store annotation. */
4714 if (s->v.AnnAssign.simple &&
4715 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
4716 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Guido van Rossum95e4d582018-01-26 08:20:18 -08004717 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
4718 VISIT(c, annexpr, s->v.AnnAssign.annotation)
4719 }
4720 else {
4721 VISIT(c, expr, s->v.AnnAssign.annotation);
4722 }
Mark Shannon332cd5e2018-01-30 00:41:04 +00004723 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
Miss Islington (bot)471364b2018-03-24 14:27:06 -07004724 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
4725 if (!mangled) {
4726 return 0;
4727 }
4728 ADDOP_N(c, LOAD_CONST, mangled, consts);
Mark Shannon332cd5e2018-01-30 00:41:04 +00004729 ADDOP(c, STORE_SUBSCR);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07004730 }
4731 break;
4732 case Attribute_kind:
4733 if (!s->v.AnnAssign.value &&
4734 !check_ann_expr(c, targ->v.Attribute.value)) {
4735 return 0;
4736 }
4737 break;
4738 case Subscript_kind:
4739 if (!s->v.AnnAssign.value &&
4740 (!check_ann_expr(c, targ->v.Subscript.value) ||
4741 !check_ann_subscr(c, targ->v.Subscript.slice))) {
4742 return 0;
4743 }
4744 break;
4745 default:
4746 PyErr_Format(PyExc_SystemError,
4747 "invalid node type (%d) for annotated assignment",
4748 targ->kind);
4749 return 0;
4750 }
4751 /* Annotation is evaluated last. */
4752 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
4753 return 0;
4754 }
4755 return 1;
4756}
4757
4758static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004759compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
4760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004761 struct fblockinfo *f;
4762 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
Benjamin Petersone09ed542016-07-14 22:00:03 -07004763 PyErr_SetString(PyExc_SyntaxError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004764 "too many statically nested blocks");
4765 return 0;
4766 }
4767 f = &c->u->u_fblock[c->u->u_nfblocks++];
4768 f->fb_type = t;
4769 f->fb_block = b;
4770 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004771}
4772
4773static void
4774compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
4775{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004776 struct compiler_unit *u = c->u;
4777 assert(u->u_nfblocks > 0);
4778 u->u_nfblocks--;
4779 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
4780 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004781}
4782
Thomas Wouters89f507f2006-12-13 04:49:30 +00004783static int
4784compiler_in_loop(struct compiler *c) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004785 int i;
4786 struct compiler_unit *u = c->u;
4787 for (i = 0; i < u->u_nfblocks; ++i) {
4788 if (u->u_fblock[i].fb_type == LOOP)
4789 return 1;
4790 }
4791 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004792}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004793/* Raises a SyntaxError and returns 0.
4794 If something goes wrong, a different exception may be raised.
4795*/
4796
4797static int
4798compiler_error(struct compiler *c, const char *errstr)
4799{
Benjamin Peterson43b06862011-05-27 09:08:01 -05004800 PyObject *loc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004801 PyObject *u = NULL, *v = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004802
Victor Stinner14e461d2013-08-26 22:28:21 +02004803 loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004804 if (!loc) {
4805 Py_INCREF(Py_None);
4806 loc = Py_None;
4807 }
Victor Stinner14e461d2013-08-26 22:28:21 +02004808 u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00004809 c->u->u_col_offset, loc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004810 if (!u)
4811 goto exit;
4812 v = Py_BuildValue("(zO)", errstr, u);
4813 if (!v)
4814 goto exit;
4815 PyErr_SetObject(PyExc_SyntaxError, v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004816 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004817 Py_DECREF(loc);
4818 Py_XDECREF(u);
4819 Py_XDECREF(v);
4820 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004821}
4822
4823static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004824compiler_handle_subscr(struct compiler *c, const char *kind,
4825 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004826{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004827 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004829 /* XXX this code is duplicated */
4830 switch (ctx) {
4831 case AugLoad: /* fall through to Load */
4832 case Load: op = BINARY_SUBSCR; break;
4833 case AugStore:/* fall through to Store */
4834 case Store: op = STORE_SUBSCR; break;
4835 case Del: op = DELETE_SUBSCR; break;
4836 case Param:
4837 PyErr_Format(PyExc_SystemError,
4838 "invalid %s kind %d in subscript\n",
4839 kind, ctx);
4840 return 0;
4841 }
4842 if (ctx == AugLoad) {
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00004843 ADDOP(c, DUP_TOP_TWO);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004844 }
4845 else if (ctx == AugStore) {
4846 ADDOP(c, ROT_THREE);
4847 }
4848 ADDOP(c, op);
4849 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004850}
4851
4852static int
4853compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
4854{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004855 int n = 2;
4856 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004858 /* only handles the cases where BUILD_SLICE is emitted */
4859 if (s->v.Slice.lower) {
4860 VISIT(c, expr, s->v.Slice.lower);
4861 }
4862 else {
4863 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4864 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004866 if (s->v.Slice.upper) {
4867 VISIT(c, expr, s->v.Slice.upper);
4868 }
4869 else {
4870 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4871 }
4872
4873 if (s->v.Slice.step) {
4874 n++;
4875 VISIT(c, expr, s->v.Slice.step);
4876 }
4877 ADDOP_I(c, BUILD_SLICE, n);
4878 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004879}
4880
4881static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004882compiler_visit_nested_slice(struct compiler *c, slice_ty s,
4883 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004884{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004885 switch (s->kind) {
4886 case Slice_kind:
4887 return compiler_slice(c, s, ctx);
4888 case Index_kind:
4889 VISIT(c, expr, s->v.Index.value);
4890 break;
4891 case ExtSlice_kind:
4892 default:
4893 PyErr_SetString(PyExc_SystemError,
4894 "extended slice invalid in nested slice");
4895 return 0;
4896 }
4897 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004898}
4899
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004900static int
4901compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
4902{
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02004903 const char * kindname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004904 switch (s->kind) {
4905 case Index_kind:
4906 kindname = "index";
4907 if (ctx != AugStore) {
4908 VISIT(c, expr, s->v.Index.value);
4909 }
4910 break;
4911 case Slice_kind:
4912 kindname = "slice";
4913 if (ctx != AugStore) {
4914 if (!compiler_slice(c, s, ctx))
4915 return 0;
4916 }
4917 break;
4918 case ExtSlice_kind:
4919 kindname = "extended slice";
4920 if (ctx != AugStore) {
Victor Stinnerad9a0662013-11-19 22:23:20 +01004921 Py_ssize_t i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004922 for (i = 0; i < n; i++) {
4923 slice_ty sub = (slice_ty)asdl_seq_GET(
4924 s->v.ExtSlice.dims, i);
4925 if (!compiler_visit_nested_slice(c, sub, ctx))
4926 return 0;
4927 }
4928 ADDOP_I(c, BUILD_TUPLE, n);
4929 }
4930 break;
4931 default:
4932 PyErr_Format(PyExc_SystemError,
4933 "invalid subscript kind %d", s->kind);
4934 return 0;
4935 }
4936 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004937}
4938
Thomas Wouters89f507f2006-12-13 04:49:30 +00004939/* End of the compiler section, beginning of the assembler section */
4940
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004941/* do depth-first search of basic block graph, starting with block.
4942 post records the block indices in post-order.
4943
4944 XXX must handle implicit jumps from one block to next
4945*/
4946
Thomas Wouters89f507f2006-12-13 04:49:30 +00004947struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004948 PyObject *a_bytecode; /* string containing bytecode */
4949 int a_offset; /* offset into bytecode */
4950 int a_nblocks; /* number of reachable blocks */
4951 basicblock **a_postorder; /* list of blocks in dfs postorder */
4952 PyObject *a_lnotab; /* string containing lnotab */
4953 int a_lnotab_off; /* offset into lnotab */
4954 int a_lineno; /* last lineno of emitted instruction */
4955 int a_lineno_off; /* bytecode offset of last lineno */
Thomas Wouters89f507f2006-12-13 04:49:30 +00004956};
4957
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004958static void
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02004959dfs(struct compiler *c, basicblock *b, struct assembler *a, int end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004960{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02004961 int i, j;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004962
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02004963 /* Get rid of recursion for normal control flow.
4964 Since the number of blocks is limited, unused space in a_postorder
4965 (from a_nblocks to end) can be used as a stack for still not ordered
4966 blocks. */
4967 for (j = end; b && !b->b_seen; b = b->b_next) {
4968 b->b_seen = 1;
4969 assert(a->a_nblocks < j);
4970 a->a_postorder[--j] = b;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004971 }
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02004972 while (j < end) {
4973 b = a->a_postorder[j++];
4974 for (i = 0; i < b->b_iused; i++) {
4975 struct instr *instr = &b->b_instr[i];
4976 if (instr->i_jrel || instr->i_jabs)
4977 dfs(c, instr->i_target, a, j);
4978 }
4979 assert(a->a_nblocks < j);
4980 a->a_postorder[a->a_nblocks++] = b;
4981 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004982}
4983
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02004984Py_LOCAL_INLINE(void)
4985stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004986{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02004987 /* XXX b->b_startdepth > depth only for the target of SETUP_FINALLY,
4988 * SETUP_WITH and SETUP_ASYNC_WITH. */
4989 assert(b->b_startdepth < 0 || b->b_startdepth >= depth);
4990 if (b->b_startdepth < depth) {
4991 assert(b->b_startdepth < 0);
4992 b->b_startdepth = depth;
4993 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02004994 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004995}
4996
4997/* Find the flow path that needs the largest stack. We assume that
4998 * cycles in the flow graph have no net effect on the stack depth.
4999 */
5000static int
5001stackdepth(struct compiler *c)
5002{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005003 basicblock *b, *entryblock = NULL;
5004 basicblock **stack, **sp;
5005 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005006 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005007 b->b_startdepth = INT_MIN;
5008 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005009 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005010 }
5011 if (!entryblock)
5012 return 0;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005013 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
5014 if (!stack) {
5015 PyErr_NoMemory();
5016 return -1;
5017 }
5018
5019 sp = stack;
5020 stackdepth_push(&sp, entryblock, 0);
5021 while (sp != stack) {
5022 b = *--sp;
5023 int depth = b->b_startdepth;
5024 assert(depth >= 0);
5025 basicblock *next = b->b_next;
5026 for (int i = 0; i < b->b_iused; i++) {
5027 struct instr *instr = &b->b_instr[i];
5028 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
5029 if (effect == PY_INVALID_STACK_EFFECT) {
5030 fprintf(stderr, "opcode = %d\n", instr->i_opcode);
5031 Py_FatalError("PyCompile_OpcodeStackEffect()");
5032 }
5033 int new_depth = depth + effect;
5034 if (new_depth > maxdepth) {
5035 maxdepth = new_depth;
5036 }
5037 assert(depth >= 0); /* invalid code or bug in stackdepth() */
5038 if (instr->i_jrel || instr->i_jabs) {
5039 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
5040 assert(effect != PY_INVALID_STACK_EFFECT);
5041 int target_depth = depth + effect;
5042 if (target_depth > maxdepth) {
5043 maxdepth = target_depth;
5044 }
5045 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
5046 if (instr->i_opcode == CONTINUE_LOOP) {
5047 /* Pops a variable number of values from the stack,
5048 * but the target should be already proceeding.
5049 */
5050 assert(instr->i_target->b_startdepth >= 0);
5051 assert(instr->i_target->b_startdepth <= depth);
5052 /* remaining code is dead */
5053 next = NULL;
5054 break;
5055 }
5056 stackdepth_push(&sp, instr->i_target, target_depth);
5057 }
5058 depth = new_depth;
5059 if (instr->i_opcode == JUMP_ABSOLUTE ||
5060 instr->i_opcode == JUMP_FORWARD ||
5061 instr->i_opcode == RETURN_VALUE ||
5062 instr->i_opcode == RAISE_VARARGS ||
5063 instr->i_opcode == BREAK_LOOP)
5064 {
5065 /* remaining code is dead */
5066 next = NULL;
5067 break;
5068 }
5069 }
5070 if (next != NULL) {
5071 stackdepth_push(&sp, next, depth);
5072 }
5073 }
5074 PyObject_Free(stack);
5075 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005076}
5077
5078static int
5079assemble_init(struct assembler *a, int nblocks, int firstlineno)
5080{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005081 memset(a, 0, sizeof(struct assembler));
5082 a->a_lineno = firstlineno;
5083 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
5084 if (!a->a_bytecode)
5085 return 0;
5086 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
5087 if (!a->a_lnotab)
5088 return 0;
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07005089 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005090 PyErr_NoMemory();
5091 return 0;
5092 }
5093 a->a_postorder = (basicblock **)PyObject_Malloc(
5094 sizeof(basicblock *) * nblocks);
5095 if (!a->a_postorder) {
5096 PyErr_NoMemory();
5097 return 0;
5098 }
5099 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005100}
5101
5102static void
5103assemble_free(struct assembler *a)
5104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005105 Py_XDECREF(a->a_bytecode);
5106 Py_XDECREF(a->a_lnotab);
5107 if (a->a_postorder)
5108 PyObject_Free(a->a_postorder);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005109}
5110
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005111static int
5112blocksize(basicblock *b)
5113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005114 int i;
5115 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005117 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005118 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005119 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005120}
5121
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00005122/* Appends a pair to the end of the line number table, a_lnotab, representing
5123 the instruction's bytecode offset and line number. See
5124 Objects/lnotab_notes.txt for the description of the line number table. */
Tim Peters2a7f3842001-06-09 09:26:21 +00005125
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005126static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005127assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005129 int d_bytecode, d_lineno;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005130 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005131 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005132
Serhiy Storchakaab874002016-09-11 13:48:15 +03005133 d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005134 d_lineno = i->i_lineno - a->a_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005136 assert(d_bytecode >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005138 if(d_bytecode == 0 && d_lineno == 0)
5139 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00005140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005141 if (d_bytecode > 255) {
5142 int j, nbytes, ncodes = d_bytecode / 255;
5143 nbytes = a->a_lnotab_off + 2 * ncodes;
5144 len = PyBytes_GET_SIZE(a->a_lnotab);
5145 if (nbytes >= len) {
5146 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
5147 len = nbytes;
5148 else if (len <= INT_MAX / 2)
5149 len *= 2;
5150 else {
5151 PyErr_NoMemory();
5152 return 0;
5153 }
5154 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
5155 return 0;
5156 }
5157 lnotab = (unsigned char *)
5158 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
5159 for (j = 0; j < ncodes; j++) {
5160 *lnotab++ = 255;
5161 *lnotab++ = 0;
5162 }
5163 d_bytecode -= ncodes * 255;
5164 a->a_lnotab_off += ncodes * 2;
5165 }
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005166 assert(0 <= d_bytecode && d_bytecode <= 255);
5167
5168 if (d_lineno < -128 || 127 < d_lineno) {
5169 int j, nbytes, ncodes, k;
5170 if (d_lineno < 0) {
5171 k = -128;
5172 /* use division on positive numbers */
5173 ncodes = (-d_lineno) / 128;
5174 }
5175 else {
5176 k = 127;
5177 ncodes = d_lineno / 127;
5178 }
5179 d_lineno -= ncodes * k;
5180 assert(ncodes >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005181 nbytes = a->a_lnotab_off + 2 * ncodes;
5182 len = PyBytes_GET_SIZE(a->a_lnotab);
5183 if (nbytes >= len) {
5184 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
5185 len = nbytes;
5186 else if (len <= INT_MAX / 2)
5187 len *= 2;
5188 else {
5189 PyErr_NoMemory();
5190 return 0;
5191 }
5192 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
5193 return 0;
5194 }
5195 lnotab = (unsigned char *)
5196 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
5197 *lnotab++ = d_bytecode;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005198 *lnotab++ = k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005199 d_bytecode = 0;
5200 for (j = 1; j < ncodes; j++) {
5201 *lnotab++ = 0;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005202 *lnotab++ = k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005203 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005204 a->a_lnotab_off += ncodes * 2;
5205 }
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005206 assert(-128 <= d_lineno && d_lineno <= 127);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005208 len = PyBytes_GET_SIZE(a->a_lnotab);
5209 if (a->a_lnotab_off + 2 >= len) {
5210 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
5211 return 0;
5212 }
5213 lnotab = (unsigned char *)
5214 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00005215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005216 a->a_lnotab_off += 2;
5217 if (d_bytecode) {
5218 *lnotab++ = d_bytecode;
Victor Stinner4f2dab52011-05-27 16:46:51 +02005219 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005220 }
5221 else { /* First line of a block; def stmt, etc. */
5222 *lnotab++ = 0;
Victor Stinner4f2dab52011-05-27 16:46:51 +02005223 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005224 }
5225 a->a_lineno = i->i_lineno;
5226 a->a_lineno_off = a->a_offset;
5227 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005228}
5229
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005230/* assemble_emit()
5231 Extend the bytecode with a new instruction.
5232 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00005233*/
5234
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00005235static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005236assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00005237{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005238 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005239 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03005240 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005241
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005242 arg = i->i_oparg;
5243 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005244 if (i->i_lineno && !assemble_lnotab(a, i))
5245 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005246 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005247 if (len > PY_SSIZE_T_MAX / 2)
5248 return 0;
5249 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
5250 return 0;
5251 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03005252 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005253 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005254 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005255 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00005256}
5257
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00005258static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005259assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00005260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005261 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005262 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005263 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00005264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005265 /* Compute the size of each block and fixup jump args.
5266 Replace block pointer with position in bytecode. */
5267 do {
5268 totsize = 0;
5269 for (i = a->a_nblocks - 1; i >= 0; i--) {
5270 b = a->a_postorder[i];
5271 bsize = blocksize(b);
5272 b->b_offset = totsize;
5273 totsize += bsize;
5274 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005275 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005276 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
5277 bsize = b->b_offset;
5278 for (i = 0; i < b->b_iused; i++) {
5279 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005280 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005281 /* Relative jumps are computed relative to
5282 the instruction pointer after fetching
5283 the jump instruction.
5284 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005285 bsize += isize;
5286 if (instr->i_jabs || instr->i_jrel) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005287 instr->i_oparg = instr->i_target->b_offset;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005288 if (instr->i_jrel) {
5289 instr->i_oparg -= bsize;
5290 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03005291 instr->i_oparg *= sizeof(_Py_CODEUNIT);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005292 if (instrsize(instr->i_oparg) != isize) {
5293 extended_arg_recompile = 1;
5294 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005295 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005296 }
5297 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00005298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005299 /* XXX: This is an awful hack that could hurt performance, but
5300 on the bright side it should work until we come up
5301 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00005302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005303 The issue is that in the first loop blocksize() is called
5304 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005305 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005306 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00005307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005308 So we loop until we stop seeing new EXTENDED_ARGs.
5309 The only EXTENDED_ARGs that could be popping up are
5310 ones in jump instructions. So this should converge
5311 fairly quickly.
5312 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005313 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005314}
5315
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005316static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01005317dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005319 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02005320 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005322 tuple = PyTuple_New(size);
5323 if (tuple == NULL)
5324 return NULL;
5325 while (PyDict_Next(dict, &pos, &k, &v)) {
5326 i = PyLong_AS_LONG(v);
Victor Stinnerefb24132016-01-22 12:33:12 +01005327 /* The keys of the dictionary are tuples. (see compiler_add_o
5328 * and _PyCode_ConstantKey). The object we want is always second,
5329 * though. */
5330 k = PyTuple_GET_ITEM(k, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005331 Py_INCREF(k);
5332 assert((i - offset) < size);
5333 assert((i - offset) >= 0);
5334 PyTuple_SET_ITEM(tuple, i - offset, k);
5335 }
5336 return tuple;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005337}
5338
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005339static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005340compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005342 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005343 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005344 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04005345 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005346 if (ste->ste_nested)
5347 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07005348 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005349 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07005350 if (!ste->ste_generator && ste->ste_coroutine)
5351 flags |= CO_COROUTINE;
5352 if (ste->ste_generator && ste->ste_coroutine)
5353 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005354 if (ste->ste_varargs)
5355 flags |= CO_VARARGS;
5356 if (ste->ste_varkeywords)
5357 flags |= CO_VARKEYWORDS;
5358 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00005359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005360 /* (Only) inherit compilerflags in PyCF_MASK */
5361 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00005362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005363 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005364}
5365
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005366static PyCodeObject *
5367makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005369 PyObject *tmp;
5370 PyCodeObject *co = NULL;
5371 PyObject *consts = NULL;
5372 PyObject *names = NULL;
5373 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005374 PyObject *name = NULL;
5375 PyObject *freevars = NULL;
5376 PyObject *cellvars = NULL;
5377 PyObject *bytecode = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005378 Py_ssize_t nlocals;
5379 int nlocals_int;
5380 int flags;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005381 int argcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005383 tmp = dict_keys_inorder(c->u->u_consts, 0);
5384 if (!tmp)
5385 goto error;
5386 consts = PySequence_List(tmp); /* optimize_code requires a list */
5387 Py_DECREF(tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005389 names = dict_keys_inorder(c->u->u_names, 0);
5390 varnames = dict_keys_inorder(c->u->u_varnames, 0);
5391 if (!consts || !names || !varnames)
5392 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005394 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
5395 if (!cellvars)
5396 goto error;
5397 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
5398 if (!freevars)
5399 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005400
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02005401 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01005402 assert(nlocals < INT_MAX);
5403 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
5404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005405 flags = compute_code_flags(c);
5406 if (flags < 0)
5407 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005409 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
5410 if (!bytecode)
5411 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005413 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
5414 if (!tmp)
5415 goto error;
5416 Py_DECREF(consts);
5417 consts = tmp;
5418
Victor Stinnerf8e32212013-11-19 23:56:34 +01005419 argcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
5420 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005421 maxdepth = stackdepth(c);
5422 if (maxdepth < 0) {
5423 goto error;
5424 }
Victor Stinnerf8e32212013-11-19 23:56:34 +01005425 co = PyCode_New(argcount, kwonlyargcount,
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005426 nlocals_int, maxdepth, flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005427 bytecode, consts, names, varnames,
5428 freevars, cellvars,
Victor Stinner14e461d2013-08-26 22:28:21 +02005429 c->c_filename, c->u->u_name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005430 c->u->u_firstlineno,
5431 a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005432 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005433 Py_XDECREF(consts);
5434 Py_XDECREF(names);
5435 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005436 Py_XDECREF(name);
5437 Py_XDECREF(freevars);
5438 Py_XDECREF(cellvars);
5439 Py_XDECREF(bytecode);
5440 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005441}
5442
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005443
5444/* For debugging purposes only */
5445#if 0
5446static void
5447dump_instr(const struct instr *i)
5448{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005449 const char *jrel = i->i_jrel ? "jrel " : "";
5450 const char *jabs = i->i_jabs ? "jabs " : "";
5451 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005453 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005454 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005455 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005456 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005457 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
5458 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005459}
5460
5461static void
5462dump_basicblock(const basicblock *b)
5463{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005464 const char *seen = b->b_seen ? "seen " : "";
5465 const char *b_return = b->b_return ? "return " : "";
5466 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
5467 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
5468 if (b->b_instr) {
5469 int i;
5470 for (i = 0; i < b->b_iused; i++) {
5471 fprintf(stderr, " [%02d] ", i);
5472 dump_instr(b->b_instr + i);
5473 }
5474 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005475}
5476#endif
5477
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005478static PyCodeObject *
5479assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005480{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005481 basicblock *b, *entryblock;
5482 struct assembler a;
5483 int i, j, nblocks;
5484 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005486 /* Make sure every block that falls off the end returns None.
5487 XXX NEXT_BLOCK() isn't quite right, because if the last
5488 block ends with a jump or return b_next shouldn't set.
5489 */
5490 if (!c->u->u_curblock->b_return) {
5491 NEXT_BLOCK(c);
5492 if (addNone)
5493 ADDOP_O(c, LOAD_CONST, Py_None, consts);
5494 ADDOP(c, RETURN_VALUE);
5495 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005497 nblocks = 0;
5498 entryblock = NULL;
5499 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
5500 nblocks++;
5501 entryblock = b;
5502 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005504 /* Set firstlineno if it wasn't explicitly set. */
5505 if (!c->u->u_firstlineno) {
Ned Deilydc35cda2016-08-17 17:18:33 -04005506 if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005507 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
5508 else
5509 c->u->u_firstlineno = 1;
5510 }
5511 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
5512 goto error;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005513 dfs(c, entryblock, &a, nblocks);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005515 /* Can't modify the bytecode after computing jump offsets. */
5516 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00005517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005518 /* Emit code in reverse postorder from dfs. */
5519 for (i = a.a_nblocks - 1; i >= 0; i--) {
5520 b = a.a_postorder[i];
5521 for (j = 0; j < b->b_iused; j++)
5522 if (!assemble_emit(&a, &b->b_instr[j]))
5523 goto error;
5524 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00005525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005526 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
5527 goto error;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005528 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005529 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005531 co = makecode(c, &a);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005532 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005533 assemble_free(&a);
5534 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005535}
Georg Brandl8334fd92010-12-04 10:26:46 +00005536
5537#undef PyAST_Compile
5538PyAPI_FUNC(PyCodeObject *)
5539PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
5540 PyArena *arena)
5541{
5542 return PyAST_CompileEx(mod, filename, flags, -1, arena);
5543}