blob: 6aa74cc2d67e42c32e4654ffd456e894a31fe353 [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.
Mark Shannon6e8128f2020-07-30 10:03:00 +010012 * 5. Optimize the byte code (peephole optimizations).
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"
Victor Stinnerc9bc2902020-10-27 02:24:34 +010025#include "pycore_long.h" // _PyLong_GetZero()
Guido van Rossum3f5da241990-12-20 15:06:42 +000026
Ammar Askare92d3932020-01-15 11:48:40 -050027#include "Python-ast.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"
Mark Shannon582aaf12020-08-04 17:30:11 +010031#define NEED_OPCODE_JUMP_TABLES
Guido van Rossum10dc2e81990-11-18 17:27:39 +000032#include "opcode.h"
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030033#include "wordcode_helpers.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000034
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000035#define DEFAULT_BLOCK_SIZE 16
36#define DEFAULT_BLOCKS 8
37#define DEFAULT_CODE_SIZE 128
38#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000039
Nick Coghlan650f0d02007-04-15 12:05:43 +000040#define COMP_GENEXP 0
41#define COMP_LISTCOMP 1
42#define COMP_SETCOMP 2
Guido van Rossum992d4a32007-07-11 13:09:30 +000043#define COMP_DICTCOMP 3
Nick Coghlan650f0d02007-04-15 12:05:43 +000044
Pablo Galindo90235812020-03-15 04:29:22 +000045#define IS_TOP_LEVEL_AWAIT(c) ( \
46 (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \
47 && (c->u->u_ste->ste_type == ModuleBlock))
48
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000049struct instr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 unsigned char i_opcode;
51 int i_oparg;
52 struct basicblock_ *i_target; /* target block (if jump instruction) */
53 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000054};
55
Mark Shannon582aaf12020-08-04 17:30:11 +010056#define LOG_BITS_PER_INT 5
57#define MASK_LOW_LOG_BITS 31
58
59static inline int
60is_bit_set_in_table(uint32_t *table, int bitindex) {
61 /* Is the relevant bit set in the relevant word? */
62 /* 256 bits fit into 8 32-bits words.
63 * Word is indexed by (bitindex>>ln(size of int in bits)).
64 * Bit within word is the low bits of bitindex.
65 */
66 uint32_t word = table[bitindex >> LOG_BITS_PER_INT];
67 return (word >> (bitindex & MASK_LOW_LOG_BITS)) & 1;
68}
69
70static inline int
71is_relative_jump(struct instr *i)
72{
73 return is_bit_set_in_table(_PyOpcode_RelativeJump, i->i_opcode);
74}
75
76static inline int
77is_jump(struct instr *i)
78{
79 return is_bit_set_in_table(_PyOpcode_Jump, i->i_opcode);
80}
81
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000082typedef struct basicblock_ {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000083 /* Each basicblock in a compilation unit is linked via b_list in the
84 reverse order that the block are allocated. b_list points to the next
85 block, not to be confused with b_next, which is next by control flow. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 struct basicblock_ *b_list;
87 /* number of instructions used */
88 int b_iused;
89 /* length of instruction array (b_instr) */
90 int b_ialloc;
91 /* pointer to an array of instructions, initially NULL */
92 struct instr *b_instr;
93 /* If b_next is non-NULL, it is a pointer to the next
94 block reached by normal control flow. */
95 struct basicblock_ *b_next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 /* b_return is true if a RETURN_VALUE opcode is inserted. */
97 unsigned b_return : 1;
Mark Shannon3bd60352021-01-13 12:05:43 +000098 /* Number of predecssors that a block has. */
99 int b_predecessors;
Mark Shannoncc75ab72020-11-12 19:49:33 +0000100 /* Basic block has no fall through (it ends with a return, raise or jump) */
101 unsigned b_nofallthrough : 1;
102 /* Basic block exits scope (it ends with a return or raise) */
103 unsigned b_exit : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 /* depth of stack upon entry of block, computed by stackdepth() */
105 int b_startdepth;
106 /* instruction offset for block, computed by assemble_jump_offsets() */
107 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000108} basicblock;
109
110/* fblockinfo tracks the current frame block.
111
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000112A frame block is used to handle loops, try/except, and try/finally.
113It's called a frame block to distinguish it from a basic block in the
114compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000115*/
116
Mark Shannon02d126a2020-09-25 14:04:19 +0100117enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END,
118 WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER };
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000119
120struct fblockinfo {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 enum fblocktype fb_type;
122 basicblock *fb_block;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200123 /* (optional) type-specific exit or cleanup block */
124 basicblock *fb_exit;
Mark Shannonfee55262019-11-21 09:11:43 +0000125 /* (optional) additional information required for unwinding */
126 void *fb_datum;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000127};
128
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100129enum {
130 COMPILER_SCOPE_MODULE,
131 COMPILER_SCOPE_CLASS,
132 COMPILER_SCOPE_FUNCTION,
Yury Selivanov75445082015-05-11 22:57:16 -0400133 COMPILER_SCOPE_ASYNC_FUNCTION,
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400134 COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100135 COMPILER_SCOPE_COMPREHENSION,
136};
137
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000138/* The following items change on entry and exit of code blocks.
139 They must be saved and restored when returning to a block.
140*/
141struct compiler_unit {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 PySTEntryObject *u_ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 PyObject *u_name;
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400145 PyObject *u_qualname; /* dot-separated qualified name (lazy) */
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100146 int u_scope_type;
147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 /* The following fields are dicts that map objects to
149 the index of them in co_XXX. The index is used as
150 the argument for opcodes that refer to those collections.
151 */
152 PyObject *u_consts; /* all constants */
153 PyObject *u_names; /* all names */
154 PyObject *u_varnames; /* local variables */
155 PyObject *u_cellvars; /* cell variables */
156 PyObject *u_freevars; /* free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 PyObject *u_private; /* for private name mangling */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000159
Victor Stinnerf8e32212013-11-19 23:56:34 +0100160 Py_ssize_t u_argcount; /* number of arguments for block */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100161 Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */
Victor Stinnerf8e32212013-11-19 23:56:34 +0100162 Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 /* Pointer to the most recently allocated block. By following b_list
164 members, you can reach all early allocated blocks. */
165 basicblock *u_blocks;
166 basicblock *u_curblock; /* pointer to current block */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 int u_nfblocks;
169 struct fblockinfo u_fblock[CO_MAXBLOCKS];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 int u_firstlineno; /* the first lineno of the block */
172 int u_lineno; /* the lineno for the current stmt */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000173 int u_col_offset; /* the offset of the current stmt */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000174};
175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176/* This struct captures the global state of a compilation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000177
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000178The u pointer points to the current compilation unit, while units
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179for enclosing blocks are stored in c_stack. The u and c_stack are
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000180managed by compiler_enter_scope() and compiler_exit_scope().
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000181
182Note that we don't track recursion levels during compilation - the
183task of detecting and rejecting excessive levels of nesting is
184handled by the symbol analysis pass.
185
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000186*/
187
188struct compiler {
Victor Stinner14e461d2013-08-26 22:28:21 +0200189 PyObject *c_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 struct symtable *c_st;
191 PyFutureFeatures *c_future; /* pointer to module's __future__ */
192 PyCompilerFlags *c_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193
Georg Brandl8334fd92010-12-04 10:26:46 +0000194 int c_optimize; /* optimization level */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 int c_interactive; /* true if in interactive mode */
196 int c_nestlevel;
INADA Naokic2e16072018-11-26 21:23:22 +0900197 PyObject *c_const_cache; /* Python dict holding all constants,
198 including names tuple */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 struct compiler_unit *u; /* compiler state for current block */
200 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
201 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000202};
203
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100204static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205static void compiler_free(struct compiler *);
206static basicblock *compiler_new_block(struct compiler *);
Andy Lester76d58772020-03-10 21:18:12 -0500207static int compiler_next_instr(basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000208static int compiler_addop(struct compiler *, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +0100209static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
Mark Shannon582aaf12020-08-04 17:30:11 +0100210static int compiler_addop_j(struct compiler *, int, basicblock *);
Mark Shannon127dde52021-01-04 18:06:55 +0000211static int compiler_addop_j_noline(struct compiler *, int, basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000212static int compiler_error(struct compiler *, const char *);
Serhiy Storchaka62e44812019-02-16 08:12:19 +0200213static int compiler_warn(struct compiler *, const char *, ...);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000214static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
215
216static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
217static int compiler_visit_stmt(struct compiler *, stmt_ty);
218static int compiler_visit_keyword(struct compiler *, keyword_ty);
219static int compiler_visit_expr(struct compiler *, expr_ty);
220static int compiler_augassign(struct compiler *, stmt_ty);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700221static int compiler_annassign(struct compiler *, stmt_ty);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +0200222static int compiler_subscript(struct compiler *, expr_ty);
223static int compiler_slice(struct compiler *, expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000224
Andy Lester76d58772020-03-10 21:18:12 -0500225static int inplace_binop(operator_ty);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100226static int are_all_items_const(asdl_expr_seq *, Py_ssize_t, Py_ssize_t);
Mark Shannon8473cf82020-12-15 11:07:50 +0000227
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000228
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500229static int compiler_with(struct compiler *, stmt_ty, int);
Yury Selivanov75445082015-05-11 22:57:16 -0400230static int compiler_async_with(struct compiler *, stmt_ty, int);
231static int compiler_async_for(struct compiler *, stmt_ty);
Victor Stinner976bb402016-03-23 11:36:19 +0100232static int compiler_call_helper(struct compiler *c, int n,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100233 asdl_expr_seq *args,
234 asdl_keyword_seq *keywords);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500235static int compiler_try_except(struct compiler *, stmt_ty);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400236static int compiler_set_qualname(struct compiler *);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000237
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700238static int compiler_sync_comprehension_generator(
239 struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100240 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +0200241 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700242 expr_ty elt, expr_ty val, int type);
243
244static int compiler_async_comprehension_generator(
245 struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100246 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +0200247 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700248 expr_ty elt, expr_ty val, int type);
249
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000250static PyCodeObject *assemble(struct compiler *, int addNone);
Mark Shannon332cd5e2018-01-30 00:41:04 +0000251static PyObject *__doc__, *__annotations__;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000252
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400253#define CAPSULE_NAME "compile.c compiler unit"
Benjamin Petersonb173f782009-05-05 22:31:58 +0000254
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000255PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000256_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 /* Name mangling: __private becomes _classname__private.
259 This is independent from how the name is used. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200260 PyObject *result;
261 size_t nlen, plen, ipriv;
262 Py_UCS4 maxchar;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200264 PyUnicode_READ_CHAR(ident, 0) != '_' ||
265 PyUnicode_READ_CHAR(ident, 1) != '_') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 Py_INCREF(ident);
267 return ident;
268 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200269 nlen = PyUnicode_GET_LENGTH(ident);
270 plen = PyUnicode_GET_LENGTH(privateobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 /* Don't mangle __id__ or names with dots.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 The only time a name with a dot can occur is when
274 we are compiling an import statement that has a
275 package name.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 TODO(jhylton): Decide whether we want to support
278 mangling of the module name, e.g. __M.X.
279 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200280 if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
281 PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
282 PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 Py_INCREF(ident);
284 return ident; /* Don't mangle __whatever__ */
285 }
286 /* Strip leading underscores from class name */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200287 ipriv = 0;
288 while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
289 ipriv++;
290 if (ipriv == plen) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 Py_INCREF(ident);
292 return ident; /* Don't mangle if class is just underscores */
293 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200294 plen -= ipriv;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000295
Antoine Pitrou55bff892013-04-06 21:21:04 +0200296 if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
297 PyErr_SetString(PyExc_OverflowError,
298 "private identifier too large to be mangled");
299 return NULL;
300 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000301
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200302 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
303 if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
304 maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
305
306 result = PyUnicode_New(1 + nlen + plen, maxchar);
307 if (!result)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200309 /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
310 PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
Victor Stinner6c7a52a2011-09-28 21:39:17 +0200311 if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
312 Py_DECREF(result);
313 return NULL;
314 }
315 if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
316 Py_DECREF(result);
317 return NULL;
318 }
Victor Stinner8f825062012-04-27 13:55:39 +0200319 assert(_PyUnicode_CheckConsistency(result, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200320 return result;
Michael W. Hudson60934622004-08-12 17:56:29 +0000321}
322
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000323static int
324compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000327
INADA Naokic2e16072018-11-26 21:23:22 +0900328 c->c_const_cache = PyDict_New();
329 if (!c->c_const_cache) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 return 0;
INADA Naokic2e16072018-11-26 21:23:22 +0900331 }
332
333 c->c_stack = PyList_New(0);
334 if (!c->c_stack) {
335 Py_CLEAR(c->c_const_cache);
336 return 0;
337 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000340}
341
342PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200343PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
344 int optimize, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 struct compiler c;
347 PyCodeObject *co = NULL;
Victor Stinner37d66d72019-06-13 02:16:41 +0200348 PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 if (!__doc__) {
352 __doc__ = PyUnicode_InternFromString("__doc__");
353 if (!__doc__)
354 return NULL;
355 }
Mark Shannon332cd5e2018-01-30 00:41:04 +0000356 if (!__annotations__) {
357 __annotations__ = PyUnicode_InternFromString("__annotations__");
358 if (!__annotations__)
359 return NULL;
360 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 if (!compiler_init(&c))
362 return NULL;
Victor Stinner14e461d2013-08-26 22:28:21 +0200363 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 c.c_filename = filename;
365 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200366 c.c_future = PyFuture_FromASTObject(mod, filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 if (c.c_future == NULL)
368 goto finally;
369 if (!flags) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 flags = &local_flags;
371 }
372 merged = c.c_future->ff_features | flags->cf_flags;
373 c.c_future->ff_features = merged;
374 flags->cf_flags = merged;
375 c.c_flags = flags;
Victor Stinnerda7933e2020-04-13 03:04:28 +0200376 c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000378
Pablo Galindod112c602020-03-18 23:02:09 +0000379 _PyASTOptimizeState state;
380 state.optimize = c.c_optimize;
381 state.ff_features = merged;
382
383 if (!_PyAST_Optimize(mod, arena, &state)) {
INADA Naoki7ea143a2017-12-14 16:47:20 +0900384 goto finally;
385 }
386
Victor Stinner14e461d2013-08-26 22:28:21 +0200387 c.c_st = PySymtable_BuildObject(mod, filename, c.c_future);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 if (c.c_st == NULL) {
389 if (!PyErr_Occurred())
390 PyErr_SetString(PyExc_SystemError, "no symtable");
391 goto finally;
392 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000395
Thomas Wouters1175c432006-02-27 22:49:54 +0000396 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 compiler_free(&c);
398 assert(co || PyErr_Occurred());
399 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000400}
401
402PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200403PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags,
404 int optimize, PyArena *arena)
405{
406 PyObject *filename;
407 PyCodeObject *co;
408 filename = PyUnicode_DecodeFSDefault(filename_str);
409 if (filename == NULL)
410 return NULL;
411 co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
412 Py_DECREF(filename);
413 return co;
414
415}
416
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000417static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000418compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 if (c->c_st)
421 PySymtable_Free(c->c_st);
422 if (c->c_future)
423 PyObject_Free(c->c_future);
Victor Stinner14e461d2013-08-26 22:28:21 +0200424 Py_XDECREF(c->c_filename);
INADA Naokic2e16072018-11-26 21:23:22 +0900425 Py_DECREF(c->c_const_cache);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000427}
428
Guido van Rossum79f25d91997-04-29 20:08:16 +0000429static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000430list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 Py_ssize_t i, n;
433 PyObject *v, *k;
434 PyObject *dict = PyDict_New();
435 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 n = PyList_Size(list);
438 for (i = 0; i < n; i++) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100439 v = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 if (!v) {
441 Py_DECREF(dict);
442 return NULL;
443 }
444 k = PyList_GET_ITEM(list, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300445 if (PyDict_SetItem(dict, k, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 Py_DECREF(v);
447 Py_DECREF(dict);
448 return NULL;
449 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 Py_DECREF(v);
451 }
452 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000453}
454
455/* Return new dict containing names from src that match scope(s).
456
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000457src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000459values are integers, starting at offset and increasing by one for
460each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000461*/
462
463static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +0100464dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000465{
Benjamin Peterson51ab2832012-07-18 15:12:47 -0700466 Py_ssize_t i = offset, scope, num_keys, key_i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 PyObject *k, *v, *dest = PyDict_New();
Meador Inge2ca63152012-07-18 14:20:11 -0500468 PyObject *sorted_keys;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 assert(offset >= 0);
471 if (dest == NULL)
472 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000473
Meador Inge2ca63152012-07-18 14:20:11 -0500474 /* Sort the keys so that we have a deterministic order on the indexes
475 saved in the returned dictionary. These indexes are used as indexes
476 into the free and cell var storage. Therefore if they aren't
477 deterministic, then the generated bytecode is not deterministic.
478 */
479 sorted_keys = PyDict_Keys(src);
480 if (sorted_keys == NULL)
481 return NULL;
482 if (PyList_Sort(sorted_keys) != 0) {
483 Py_DECREF(sorted_keys);
484 return NULL;
485 }
Meador Ingef69e24e2012-07-18 16:41:03 -0500486 num_keys = PyList_GET_SIZE(sorted_keys);
Meador Inge2ca63152012-07-18 14:20:11 -0500487
488 for (key_i = 0; key_i < num_keys; key_i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 /* XXX this should probably be a macro in symtable.h */
490 long vi;
Meador Inge2ca63152012-07-18 14:20:11 -0500491 k = PyList_GET_ITEM(sorted_keys, key_i);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200492 v = PyDict_GetItemWithError(src, k);
493 assert(v && PyLong_Check(v));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 vi = PyLong_AS_LONG(v);
495 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 if (scope == scope_type || vi & flag) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300498 PyObject *item = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 if (item == NULL) {
Meador Inge2ca63152012-07-18 14:20:11 -0500500 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 Py_DECREF(dest);
502 return NULL;
503 }
504 i++;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300505 if (PyDict_SetItem(dest, k, item) < 0) {
Meador Inge2ca63152012-07-18 14:20:11 -0500506 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 Py_DECREF(item);
508 Py_DECREF(dest);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 return NULL;
510 }
511 Py_DECREF(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 }
513 }
Meador Inge2ca63152012-07-18 14:20:11 -0500514 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000516}
517
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000518static void
519compiler_unit_check(struct compiler_unit *u)
520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 basicblock *block;
522 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Benjamin Petersonca470632016-09-06 13:47:26 -0700523 assert((uintptr_t)block != 0xcbcbcbcbU);
524 assert((uintptr_t)block != 0xfbfbfbfbU);
525 assert((uintptr_t)block != 0xdbdbdbdbU);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 if (block->b_instr != NULL) {
527 assert(block->b_ialloc > 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +0100528 assert(block->b_iused >= 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 assert(block->b_ialloc >= block->b_iused);
530 }
531 else {
532 assert (block->b_iused == 0);
533 assert (block->b_ialloc == 0);
534 }
535 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000536}
537
538static void
539compiler_unit_free(struct compiler_unit *u)
540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 compiler_unit_check(u);
544 b = u->u_blocks;
545 while (b != NULL) {
546 if (b->b_instr)
547 PyObject_Free((void *)b->b_instr);
548 next = b->b_list;
549 PyObject_Free((void *)b);
550 b = next;
551 }
552 Py_CLEAR(u->u_ste);
553 Py_CLEAR(u->u_name);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400554 Py_CLEAR(u->u_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 Py_CLEAR(u->u_consts);
556 Py_CLEAR(u->u_names);
557 Py_CLEAR(u->u_varnames);
558 Py_CLEAR(u->u_freevars);
559 Py_CLEAR(u->u_cellvars);
560 Py_CLEAR(u->u_private);
561 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000562}
563
564static int
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100565compiler_enter_scope(struct compiler *c, identifier name,
566 int scope_type, void *key, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 struct compiler_unit *u;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100569 basicblock *block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570
Andy Lester7668a8b2020-03-24 23:26:44 -0500571 u = (struct compiler_unit *)PyObject_Calloc(1, sizeof(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 struct compiler_unit));
573 if (!u) {
574 PyErr_NoMemory();
575 return 0;
576 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100577 u->u_scope_type = scope_type;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 u->u_argcount = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100579 u->u_posonlyargcount = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 u->u_kwonlyargcount = 0;
581 u->u_ste = PySymtable_Lookup(c->c_st, key);
582 if (!u->u_ste) {
583 compiler_unit_free(u);
584 return 0;
585 }
586 Py_INCREF(name);
587 u->u_name = name;
588 u->u_varnames = list2dict(u->u_ste->ste_varnames);
589 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
590 if (!u->u_varnames || !u->u_cellvars) {
591 compiler_unit_free(u);
592 return 0;
593 }
Benjamin Peterson312595c2013-05-15 15:26:42 -0500594 if (u->u_ste->ste_needs_class_closure) {
Martin Panter7462b6492015-11-02 03:37:02 +0000595 /* Cook up an implicit __class__ cell. */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500596 _Py_IDENTIFIER(__class__);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300597 PyObject *name;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500598 int res;
599 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200600 assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500601 name = _PyUnicode_FromId(&PyId___class__);
602 if (!name) {
603 compiler_unit_free(u);
604 return 0;
605 }
Victor Stinnerc9bc2902020-10-27 02:24:34 +0100606 res = PyDict_SetItem(u->u_cellvars, name, _PyLong_GetZero());
Benjamin Peterson312595c2013-05-15 15:26:42 -0500607 if (res < 0) {
608 compiler_unit_free(u);
609 return 0;
610 }
611 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200614 PyDict_GET_SIZE(u->u_cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 if (!u->u_freevars) {
616 compiler_unit_free(u);
617 return 0;
618 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 u->u_blocks = NULL;
621 u->u_nfblocks = 0;
622 u->u_firstlineno = lineno;
623 u->u_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000624 u->u_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 u->u_consts = PyDict_New();
626 if (!u->u_consts) {
627 compiler_unit_free(u);
628 return 0;
629 }
630 u->u_names = PyDict_New();
631 if (!u->u_names) {
632 compiler_unit_free(u);
633 return 0;
634 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 /* Push the old compiler_unit on the stack. */
639 if (c->u) {
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400640 PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
642 Py_XDECREF(capsule);
643 compiler_unit_free(u);
644 return 0;
645 }
646 Py_DECREF(capsule);
647 u->u_private = c->u->u_private;
648 Py_XINCREF(u->u_private);
649 }
650 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 c->c_nestlevel++;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100653
654 block = compiler_new_block(c);
655 if (block == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 return 0;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100657 c->u->u_curblock = block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400659 if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
660 if (!compiler_set_qualname(c))
661 return 0;
662 }
663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000665}
666
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000667static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668compiler_exit_scope(struct compiler *c)
669{
Victor Stinnerad9a0662013-11-19 22:23:20 +0100670 Py_ssize_t n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 PyObject *capsule;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 c->c_nestlevel--;
674 compiler_unit_free(c->u);
675 /* Restore c->u to the parent unit. */
676 n = PyList_GET_SIZE(c->c_stack) - 1;
677 if (n >= 0) {
678 capsule = PyList_GET_ITEM(c->c_stack, n);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400679 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 assert(c->u);
681 /* we are deleting from a list so this really shouldn't fail */
682 if (PySequence_DelItem(c->c_stack, n) < 0)
683 Py_FatalError("compiler_exit_scope()");
684 compiler_unit_check(c->u);
685 }
686 else
687 c->u = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000688
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000689}
690
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400691static int
692compiler_set_qualname(struct compiler *c)
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100693{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100694 _Py_static_string(dot, ".");
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400695 _Py_static_string(dot_locals, ".<locals>");
696 Py_ssize_t stack_size;
697 struct compiler_unit *u = c->u;
698 PyObject *name, *base, *dot_str, *dot_locals_str;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100699
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400700 base = NULL;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100701 stack_size = PyList_GET_SIZE(c->c_stack);
Benjamin Petersona8a38b82013-10-19 16:14:39 -0400702 assert(stack_size >= 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400703 if (stack_size > 1) {
704 int scope, force_global = 0;
705 struct compiler_unit *parent;
706 PyObject *mangled, *capsule;
707
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400708 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400709 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400710 assert(parent);
711
Yury Selivanov75445082015-05-11 22:57:16 -0400712 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
713 || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
714 || u->u_scope_type == COMPILER_SCOPE_CLASS) {
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400715 assert(u->u_name);
716 mangled = _Py_Mangle(parent->u_private, u->u_name);
717 if (!mangled)
718 return 0;
719 scope = PyST_GetScope(parent->u_ste, mangled);
720 Py_DECREF(mangled);
721 assert(scope != GLOBAL_IMPLICIT);
722 if (scope == GLOBAL_EXPLICIT)
723 force_global = 1;
724 }
725
726 if (!force_global) {
727 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
Yury Selivanov75445082015-05-11 22:57:16 -0400728 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400729 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
730 dot_locals_str = _PyUnicode_FromId(&dot_locals);
731 if (dot_locals_str == NULL)
732 return 0;
733 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
734 if (base == NULL)
735 return 0;
736 }
737 else {
738 Py_INCREF(parent->u_qualname);
739 base = parent->u_qualname;
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400740 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100741 }
742 }
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400743
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400744 if (base != NULL) {
745 dot_str = _PyUnicode_FromId(&dot);
746 if (dot_str == NULL) {
747 Py_DECREF(base);
748 return 0;
749 }
750 name = PyUnicode_Concat(base, dot_str);
751 Py_DECREF(base);
752 if (name == NULL)
753 return 0;
754 PyUnicode_Append(&name, u->u_name);
755 if (name == NULL)
756 return 0;
757 }
758 else {
759 Py_INCREF(u->u_name);
760 name = u->u_name;
761 }
762 u->u_qualname = name;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100763
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400764 return 1;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100765}
766
Eric V. Smith235a6f02015-09-19 14:51:32 -0400767
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000768/* Allocate a new block and return a pointer to it.
769 Returns NULL on error.
770*/
771
772static basicblock *
773compiler_new_block(struct compiler *c)
774{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 basicblock *b;
776 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 u = c->u;
Andy Lester7668a8b2020-03-24 23:26:44 -0500779 b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 if (b == NULL) {
781 PyErr_NoMemory();
782 return NULL;
783 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 /* Extend the singly linked list of blocks with new block. */
785 b->b_list = u->u_blocks;
786 u->u_blocks = b;
787 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788}
789
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790static basicblock *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791compiler_next_block(struct compiler *c)
792{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 basicblock *block = compiler_new_block(c);
794 if (block == NULL)
795 return NULL;
796 c->u->u_curblock->b_next = block;
797 c->u->u_curblock = block;
798 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799}
800
801static basicblock *
802compiler_use_next_block(struct compiler *c, basicblock *block)
803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 assert(block != NULL);
805 c->u->u_curblock->b_next = block;
806 c->u->u_curblock = block;
807 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808}
809
Mark Shannon5977a792020-12-02 13:31:40 +0000810static basicblock *
811compiler_copy_block(struct compiler *c, basicblock *block)
812{
813 /* Cannot copy a block if it has a fallthrough, since
814 * a block can only have one fallthrough predecessor.
815 */
816 assert(block->b_nofallthrough);
817 basicblock *result = compiler_next_block(c);
818 if (result == NULL) {
819 return NULL;
820 }
821 for (int i = 0; i < block->b_iused; i++) {
822 int n = compiler_next_instr(result);
823 if (n < 0) {
824 return NULL;
825 }
826 result->b_instr[n] = block->b_instr[i];
827 }
828 result->b_exit = block->b_exit;
Mark Shannon3bd60352021-01-13 12:05:43 +0000829 result->b_nofallthrough = 1;
Mark Shannon5977a792020-12-02 13:31:40 +0000830 return result;
831}
832
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833/* Returns the offset of the next instruction in the current block's
834 b_instr array. Resizes the b_instr as necessary.
835 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000836*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837
838static int
Andy Lester76d58772020-03-10 21:18:12 -0500839compiler_next_instr(basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 assert(b != NULL);
842 if (b->b_instr == NULL) {
Andy Lester7668a8b2020-03-24 23:26:44 -0500843 b->b_instr = (struct instr *)PyObject_Calloc(
844 DEFAULT_BLOCK_SIZE, sizeof(struct instr));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 if (b->b_instr == NULL) {
846 PyErr_NoMemory();
847 return -1;
848 }
849 b->b_ialloc = DEFAULT_BLOCK_SIZE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 }
851 else if (b->b_iused == b->b_ialloc) {
852 struct instr *tmp;
853 size_t oldsize, newsize;
854 oldsize = b->b_ialloc * sizeof(struct instr);
855 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000856
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -0700857 if (oldsize > (SIZE_MAX >> 1)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 PyErr_NoMemory();
859 return -1;
860 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 if (newsize == 0) {
863 PyErr_NoMemory();
864 return -1;
865 }
866 b->b_ialloc <<= 1;
867 tmp = (struct instr *)PyObject_Realloc(
868 (void *)b->b_instr, newsize);
869 if (tmp == NULL) {
870 PyErr_NoMemory();
871 return -1;
872 }
873 b->b_instr = tmp;
874 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
875 }
876 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000877}
878
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200879/* Set the line number and column offset for the following instructions.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000880
Christian Heimes2202f872008-02-06 14:31:34 +0000881 The line number is reset in the following cases:
882 - when entering a new scope
883 - on each statement
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200884 - on each expression and sub-expression
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200885 - before the "except" and "finally" clauses
Thomas Wouters89f507f2006-12-13 04:49:30 +0000886*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000887
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200888#define SET_LOC(c, x) \
889 (c)->u->u_lineno = (x)->lineno; \
890 (c)->u->u_col_offset = (x)->col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000891
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200892/* Return the stack effect of opcode with argument oparg.
893
894 Some opcodes have different stack effect when jump to the target and
895 when not jump. The 'jump' parameter specifies the case:
896
897 * 0 -- when not jump
898 * 1 -- when jump
899 * -1 -- maximal
900 */
901/* XXX Make the stack effect of WITH_CLEANUP_START and
902 WITH_CLEANUP_FINISH deterministic. */
903static int
904stack_effect(int opcode, int oparg, int jump)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000905{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 switch (opcode) {
Serhiy Storchaka57faf342018-04-25 22:04:06 +0300907 case NOP:
908 case EXTENDED_ARG:
909 return 0;
910
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200911 /* Stack manipulation */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 case POP_TOP:
913 return -1;
914 case ROT_TWO:
915 case ROT_THREE:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200916 case ROT_FOUR:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 return 0;
918 case DUP_TOP:
919 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000920 case DUP_TOP_TWO:
921 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000922
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200923 /* Unary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 case UNARY_POSITIVE:
925 case UNARY_NEGATIVE:
926 case UNARY_NOT:
927 case UNARY_INVERT:
928 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 case SET_ADD:
931 case LIST_APPEND:
932 return -1;
933 case MAP_ADD:
934 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000935
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200936 /* Binary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 case BINARY_POWER:
938 case BINARY_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400939 case BINARY_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 case BINARY_MODULO:
941 case BINARY_ADD:
942 case BINARY_SUBTRACT:
943 case BINARY_SUBSCR:
944 case BINARY_FLOOR_DIVIDE:
945 case BINARY_TRUE_DIVIDE:
946 return -1;
947 case INPLACE_FLOOR_DIVIDE:
948 case INPLACE_TRUE_DIVIDE:
949 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 case INPLACE_ADD:
952 case INPLACE_SUBTRACT:
953 case INPLACE_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400954 case INPLACE_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 case INPLACE_MODULO:
956 return -1;
957 case STORE_SUBSCR:
958 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 case DELETE_SUBSCR:
960 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 case BINARY_LSHIFT:
963 case BINARY_RSHIFT:
964 case BINARY_AND:
965 case BINARY_XOR:
966 case BINARY_OR:
967 return -1;
968 case INPLACE_POWER:
969 return -1;
970 case GET_ITER:
971 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 case PRINT_EXPR:
974 return -1;
975 case LOAD_BUILD_CLASS:
976 return 1;
977 case INPLACE_LSHIFT:
978 case INPLACE_RSHIFT:
979 case INPLACE_AND:
980 case INPLACE_XOR:
981 case INPLACE_OR:
982 return -1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 case SETUP_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200985 /* 1 in the normal flow.
986 * Restore the stack position and push 6 values before jumping to
987 * the handler if an exception be raised. */
988 return jump ? 6 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 case RETURN_VALUE:
990 return -1;
991 case IMPORT_STAR:
992 return -1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700993 case SETUP_ANNOTATIONS:
994 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 case YIELD_VALUE:
996 return 0;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500997 case YIELD_FROM:
998 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 case POP_BLOCK:
1000 return 0;
1001 case POP_EXCEPT:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001002 return -3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 case STORE_NAME:
1005 return -1;
1006 case DELETE_NAME:
1007 return 0;
1008 case UNPACK_SEQUENCE:
1009 return oparg-1;
1010 case UNPACK_EX:
1011 return (oparg&0xFF) + (oparg>>8);
1012 case FOR_ITER:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001013 /* -1 at end of iterator, 1 if continue iterating. */
1014 return jump > 0 ? -1 : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 case STORE_ATTR:
1017 return -2;
1018 case DELETE_ATTR:
1019 return -1;
1020 case STORE_GLOBAL:
1021 return -1;
1022 case DELETE_GLOBAL:
1023 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 case LOAD_CONST:
1025 return 1;
1026 case LOAD_NAME:
1027 return 1;
1028 case BUILD_TUPLE:
1029 case BUILD_LIST:
1030 case BUILD_SET:
Serhiy Storchakaea525a22016-09-06 22:07:53 +03001031 case BUILD_STRING:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 return 1-oparg;
1033 case BUILD_MAP:
Benjamin Petersonb6855152015-09-10 21:02:39 -07001034 return 1 - 2*oparg;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001035 case BUILD_CONST_KEY_MAP:
1036 return -oparg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 case LOAD_ATTR:
1038 return 0;
1039 case COMPARE_OP:
Mark Shannon9af0e472020-01-14 10:12:45 +00001040 case IS_OP:
1041 case CONTAINS_OP:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 return -1;
Mark Shannon9af0e472020-01-14 10:12:45 +00001043 case JUMP_IF_NOT_EXC_MATCH:
1044 return -2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 case IMPORT_NAME:
1046 return -1;
1047 case IMPORT_FROM:
1048 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001049
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001050 /* Jumps */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 case JUMP_FORWARD:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 case JUMP_ABSOLUTE:
1053 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001054
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001055 case JUMP_IF_TRUE_OR_POP:
1056 case JUMP_IF_FALSE_OR_POP:
1057 return jump ? 0 : -1;
1058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 case POP_JUMP_IF_FALSE:
1060 case POP_JUMP_IF_TRUE:
1061 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 case LOAD_GLOBAL:
1064 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001065
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001066 /* Exception handling */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 case SETUP_FINALLY:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001068 /* 0 in the normal flow.
1069 * Restore the stack position and push 6 values before jumping to
1070 * the handler if an exception be raised. */
1071 return jump ? 6 : 0;
Mark Shannonfee55262019-11-21 09:11:43 +00001072 case RERAISE:
1073 return -3;
1074
1075 case WITH_EXCEPT_START:
1076 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 case LOAD_FAST:
1079 return 1;
1080 case STORE_FAST:
1081 return -1;
1082 case DELETE_FAST:
1083 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 case RAISE_VARARGS:
1086 return -oparg;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001087
1088 /* Functions and calls */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 case CALL_FUNCTION:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001090 return -oparg;
Yury Selivanovf2392132016-12-13 19:03:51 -05001091 case CALL_METHOD:
1092 return -oparg-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 case CALL_FUNCTION_KW:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001094 return -oparg-1;
1095 case CALL_FUNCTION_EX:
Matthieu Dartiailh3a9ac822017-02-21 14:25:22 +01001096 return -1 - ((oparg & 0x01) != 0);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001097 case MAKE_FUNCTION:
1098 return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1099 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 case BUILD_SLICE:
1101 if (oparg == 3)
1102 return -2;
1103 else
1104 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001105
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001106 /* Closures */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 case LOAD_CLOSURE:
1108 return 1;
1109 case LOAD_DEREF:
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001110 case LOAD_CLASSDEREF:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 return 1;
1112 case STORE_DEREF:
1113 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001114 case DELETE_DEREF:
1115 return 0;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001116
1117 /* Iterators and generators */
Yury Selivanov75445082015-05-11 22:57:16 -04001118 case GET_AWAITABLE:
1119 return 0;
1120 case SETUP_ASYNC_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001121 /* 0 in the normal flow.
1122 * Restore the stack position to the position before the result
1123 * of __aenter__ and push 6 values before jumping to the handler
1124 * if an exception be raised. */
1125 return jump ? -1 + 6 : 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001126 case BEFORE_ASYNC_WITH:
1127 return 1;
1128 case GET_AITER:
1129 return 0;
1130 case GET_ANEXT:
1131 return 1;
Yury Selivanov5376ba92015-06-22 12:19:30 -04001132 case GET_YIELD_FROM_ITER:
1133 return 0;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02001134 case END_ASYNC_FOR:
1135 return -7;
Eric V. Smitha78c7952015-11-03 12:45:05 -05001136 case FORMAT_VALUE:
1137 /* If there's a fmt_spec on the stack, we go from 2->1,
1138 else 1->1. */
1139 return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
Yury Selivanovf2392132016-12-13 19:03:51 -05001140 case LOAD_METHOD:
1141 return 1;
Zackery Spytzce6a0702019-08-25 03:44:09 -06001142 case LOAD_ASSERTION_ERROR:
1143 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001144 case LIST_TO_TUPLE:
1145 return 0;
1146 case LIST_EXTEND:
1147 case SET_UPDATE:
Mark Shannon8a4cd702020-01-27 09:57:45 +00001148 case DICT_MERGE:
1149 case DICT_UPDATE:
Mark Shannon13bc1392020-01-23 09:25:17 +00001150 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 default:
Larry Hastings3a907972013-11-23 14:49:22 -08001152 return PY_INVALID_STACK_EFFECT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 }
Larry Hastings3a907972013-11-23 14:49:22 -08001154 return PY_INVALID_STACK_EFFECT; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155}
1156
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001157int
Serhiy Storchaka7bdf2822018-09-18 09:54:26 +03001158PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump)
1159{
1160 return stack_effect(opcode, oparg, jump);
1161}
1162
1163int
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001164PyCompile_OpcodeStackEffect(int opcode, int oparg)
1165{
1166 return stack_effect(opcode, oparg, -1);
1167}
1168
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169/* Add an opcode with no argument.
1170 Returns 0 on failure, 1 on success.
1171*/
1172
1173static int
Mark Shannon3bd60352021-01-13 12:05:43 +00001174compiler_addop_line(struct compiler *c, int opcode, int line)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 basicblock *b;
1177 struct instr *i;
1178 int off;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001179 assert(!HAS_ARG(opcode));
Andy Lester76d58772020-03-10 21:18:12 -05001180 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 if (off < 0)
1182 return 0;
1183 b = c->u->u_curblock;
1184 i = &b->b_instr[off];
1185 i->i_opcode = opcode;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001186 i->i_oparg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 if (opcode == RETURN_VALUE)
1188 b->b_return = 1;
Mark Shannon3bd60352021-01-13 12:05:43 +00001189 i->i_lineno = line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001191}
1192
Mark Shannon3bd60352021-01-13 12:05:43 +00001193static int
1194compiler_addop(struct compiler *c, int opcode)
1195{
1196 return compiler_addop_line(c, opcode, c->u->u_lineno);
1197}
1198
1199static int
1200compiler_addop_noline(struct compiler *c, int opcode)
1201{
1202 return compiler_addop_line(c, opcode, -1);
1203}
1204
1205
Victor Stinnerf8e32212013-11-19 23:56:34 +01001206static Py_ssize_t
Andy Lester76d58772020-03-10 21:18:12 -05001207compiler_add_o(PyObject *dict, PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001209 PyObject *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001211
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001212 v = PyDict_GetItemWithError(dict, o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 if (!v) {
Stefan Krahc0cbed12015-07-27 12:56:49 +02001214 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 return -1;
Stefan Krahc0cbed12015-07-27 12:56:49 +02001216 }
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001217 arg = PyDict_GET_SIZE(dict);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001218 v = PyLong_FromSsize_t(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 if (!v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 return -1;
1221 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001222 if (PyDict_SetItem(dict, o, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 Py_DECREF(v);
1224 return -1;
1225 }
1226 Py_DECREF(v);
1227 }
1228 else
1229 arg = PyLong_AsLong(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001230 return arg;
1231}
1232
INADA Naokic2e16072018-11-26 21:23:22 +09001233// Merge const *o* recursively and return constant key object.
1234static PyObject*
1235merge_consts_recursive(struct compiler *c, PyObject *o)
1236{
1237 // None and Ellipsis are singleton, and key is the singleton.
1238 // No need to merge object and key.
1239 if (o == Py_None || o == Py_Ellipsis) {
1240 Py_INCREF(o);
1241 return o;
1242 }
1243
1244 PyObject *key = _PyCode_ConstantKey(o);
1245 if (key == NULL) {
1246 return NULL;
1247 }
1248
1249 // t is borrowed reference
1250 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
1251 if (t != key) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001252 // o is registered in c_const_cache. Just use it.
Zackery Spytz9b4a1b12019-03-20 03:16:25 -06001253 Py_XINCREF(t);
INADA Naokic2e16072018-11-26 21:23:22 +09001254 Py_DECREF(key);
1255 return t;
1256 }
1257
INADA Naokif7e4d362018-11-29 00:58:46 +09001258 // We registered o in c_const_cache.
Simeon63b5fc52019-04-09 19:36:57 -04001259 // When o is a tuple or frozenset, we want to merge its
INADA Naokif7e4d362018-11-29 00:58:46 +09001260 // items too.
INADA Naokic2e16072018-11-26 21:23:22 +09001261 if (PyTuple_CheckExact(o)) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001262 Py_ssize_t len = PyTuple_GET_SIZE(o);
1263 for (Py_ssize_t i = 0; i < len; i++) {
INADA Naokic2e16072018-11-26 21:23:22 +09001264 PyObject *item = PyTuple_GET_ITEM(o, i);
1265 PyObject *u = merge_consts_recursive(c, item);
1266 if (u == NULL) {
1267 Py_DECREF(key);
1268 return NULL;
1269 }
1270
1271 // See _PyCode_ConstantKey()
1272 PyObject *v; // borrowed
1273 if (PyTuple_CheckExact(u)) {
1274 v = PyTuple_GET_ITEM(u, 1);
1275 }
1276 else {
1277 v = u;
1278 }
1279 if (v != item) {
1280 Py_INCREF(v);
1281 PyTuple_SET_ITEM(o, i, v);
1282 Py_DECREF(item);
1283 }
1284
1285 Py_DECREF(u);
1286 }
1287 }
INADA Naokif7e4d362018-11-29 00:58:46 +09001288 else if (PyFrozenSet_CheckExact(o)) {
Simeon63b5fc52019-04-09 19:36:57 -04001289 // *key* is tuple. And its first item is frozenset of
INADA Naokif7e4d362018-11-29 00:58:46 +09001290 // constant keys.
1291 // See _PyCode_ConstantKey() for detail.
1292 assert(PyTuple_CheckExact(key));
1293 assert(PyTuple_GET_SIZE(key) == 2);
1294
1295 Py_ssize_t len = PySet_GET_SIZE(o);
1296 if (len == 0) { // empty frozenset should not be re-created.
1297 return key;
1298 }
1299 PyObject *tuple = PyTuple_New(len);
1300 if (tuple == NULL) {
1301 Py_DECREF(key);
1302 return NULL;
1303 }
1304 Py_ssize_t i = 0, pos = 0;
1305 PyObject *item;
1306 Py_hash_t hash;
1307 while (_PySet_NextEntry(o, &pos, &item, &hash)) {
1308 PyObject *k = merge_consts_recursive(c, item);
1309 if (k == NULL) {
1310 Py_DECREF(tuple);
1311 Py_DECREF(key);
1312 return NULL;
1313 }
1314 PyObject *u;
1315 if (PyTuple_CheckExact(k)) {
1316 u = PyTuple_GET_ITEM(k, 1);
1317 Py_INCREF(u);
1318 Py_DECREF(k);
1319 }
1320 else {
1321 u = k;
1322 }
1323 PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u.
1324 i++;
1325 }
1326
1327 // Instead of rewriting o, we create new frozenset and embed in the
1328 // key tuple. Caller should get merged frozenset from the key tuple.
1329 PyObject *new = PyFrozenSet_New(tuple);
1330 Py_DECREF(tuple);
1331 if (new == NULL) {
1332 Py_DECREF(key);
1333 return NULL;
1334 }
1335 assert(PyTuple_GET_ITEM(key, 1) == o);
1336 Py_DECREF(o);
1337 PyTuple_SET_ITEM(key, 1, new);
1338 }
INADA Naokic2e16072018-11-26 21:23:22 +09001339
1340 return key;
1341}
1342
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001343static Py_ssize_t
1344compiler_add_const(struct compiler *c, PyObject *o)
1345{
INADA Naokic2e16072018-11-26 21:23:22 +09001346 PyObject *key = merge_consts_recursive(c, o);
1347 if (key == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001348 return -1;
INADA Naokic2e16072018-11-26 21:23:22 +09001349 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001350
Andy Lester76d58772020-03-10 21:18:12 -05001351 Py_ssize_t arg = compiler_add_o(c->u->u_consts, key);
INADA Naokic2e16072018-11-26 21:23:22 +09001352 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001354}
1355
1356static int
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001357compiler_addop_load_const(struct compiler *c, PyObject *o)
1358{
1359 Py_ssize_t arg = compiler_add_const(c, o);
1360 if (arg < 0)
1361 return 0;
1362 return compiler_addop_i(c, LOAD_CONST, arg);
1363}
1364
1365static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001366compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368{
Andy Lester76d58772020-03-10 21:18:12 -05001369 Py_ssize_t arg = compiler_add_o(dict, o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001371 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001372 return compiler_addop_i(c, opcode, arg);
1373}
1374
1375static int
1376compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001378{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001379 Py_ssize_t arg;
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001380
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001381 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1382 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001383 return 0;
Andy Lester76d58772020-03-10 21:18:12 -05001384 arg = compiler_add_o(dict, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385 Py_DECREF(mangled);
1386 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001387 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388 return compiler_addop_i(c, opcode, arg);
1389}
1390
1391/* Add an opcode with an integer argument.
1392 Returns 0 on failure, 1 on success.
1393*/
1394
1395static int
Victor Stinnerf8e32212013-11-19 23:56:34 +01001396compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 struct instr *i;
1399 int off;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001400
Victor Stinner2ad474b2016-03-01 23:34:47 +01001401 /* oparg value is unsigned, but a signed C int is usually used to store
1402 it in the C code (like Python/ceval.c).
1403
1404 Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1405
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001406 The argument of a concrete bytecode instruction is limited to 8-bit.
1407 EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1408 assert(HAS_ARG(opcode));
Victor Stinner2ad474b2016-03-01 23:34:47 +01001409 assert(0 <= oparg && oparg <= 2147483647);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001410
Andy Lester76d58772020-03-10 21:18:12 -05001411 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 if (off < 0)
1413 return 0;
1414 i = &c->u->u_curblock->b_instr[off];
Victor Stinnerf8e32212013-11-19 23:56:34 +01001415 i->i_opcode = opcode;
1416 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001417 i->i_lineno = c->u->u_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419}
1420
Mark Shannon28b75c82020-12-23 11:43:10 +00001421static int add_jump_to_block(basicblock *b, int opcode, int lineno, basicblock *target)
1422{
1423 assert(HAS_ARG(opcode));
1424 assert(b != NULL);
1425 assert(target != NULL);
1426
1427 int off = compiler_next_instr(b);
1428 struct instr *i = &b->b_instr[off];
1429 if (off < 0) {
1430 return 0;
1431 }
1432 i->i_opcode = opcode;
1433 i->i_target = target;
1434 i->i_lineno = lineno;
1435 return 1;
1436}
1437
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438static int
Mark Shannon582aaf12020-08-04 17:30:11 +01001439compiler_addop_j(struct compiler *c, int opcode, basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440{
Mark Shannon28b75c82020-12-23 11:43:10 +00001441 return add_jump_to_block(c->u->u_curblock, opcode, c->u->u_lineno, b);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442}
1443
Mark Shannon127dde52021-01-04 18:06:55 +00001444static int
1445compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b)
1446{
1447 return add_jump_to_block(c->u->u_curblock, opcode, -1, b);
1448}
1449
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +01001450/* NEXT_BLOCK() creates an implicit jump from the current block
1451 to the new block.
1452
1453 The returns inside this macro make it impossible to decref objects
1454 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001456#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 if (compiler_next_block((C)) == NULL) \
1458 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459}
1460
1461#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 if (!compiler_addop((C), (OP))) \
1463 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001464}
1465
Mark Shannon3bd60352021-01-13 12:05:43 +00001466#define ADDOP_NOLINE(C, OP) { \
1467 if (!compiler_addop_noline((C), (OP))) \
1468 return 0; \
1469}
1470
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001471#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 if (!compiler_addop((C), (OP))) { \
1473 compiler_exit_scope(c); \
1474 return 0; \
1475 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001476}
1477
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001478#define ADDOP_LOAD_CONST(C, O) { \
1479 if (!compiler_addop_load_const((C), (O))) \
1480 return 0; \
1481}
1482
1483/* Same as ADDOP_LOAD_CONST, but steals a reference. */
1484#define ADDOP_LOAD_CONST_NEW(C, O) { \
1485 PyObject *__new_const = (O); \
1486 if (__new_const == NULL) { \
1487 return 0; \
1488 } \
1489 if (!compiler_addop_load_const((C), __new_const)) { \
1490 Py_DECREF(__new_const); \
1491 return 0; \
1492 } \
1493 Py_DECREF(__new_const); \
1494}
1495
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001496#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1498 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001499}
1500
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001501/* Same as ADDOP_O, but steals a reference. */
1502#define ADDOP_N(C, OP, O, TYPE) { \
1503 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1504 Py_DECREF((O)); \
1505 return 0; \
1506 } \
1507 Py_DECREF((O)); \
1508}
1509
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1512 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001513}
1514
1515#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 if (!compiler_addop_i((C), (OP), (O))) \
1517 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001518}
1519
Mark Shannon582aaf12020-08-04 17:30:11 +01001520#define ADDOP_JUMP(C, OP, O) { \
1521 if (!compiler_addop_j((C), (OP), (O))) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523}
1524
Mark Shannon127dde52021-01-04 18:06:55 +00001525/* Add a jump with no line number.
1526 * Used for artificial jumps that have no corresponding
1527 * token in the source code. */
1528#define ADDOP_JUMP_NOLINE(C, OP, O) { \
1529 if (!compiler_addop_j_noline((C), (OP), (O))) \
1530 return 0; \
1531}
1532
Mark Shannon9af0e472020-01-14 10:12:45 +00001533#define ADDOP_COMPARE(C, CMP) { \
1534 if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \
1535 return 0; \
1536}
1537
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1539 the ASDL name to synthesize the name of the C type and the visit function.
1540*/
1541
1542#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 if (!compiler_visit_ ## TYPE((C), (V))) \
1544 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545}
1546
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001547#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 if (!compiler_visit_ ## TYPE((C), (V))) { \
1549 compiler_exit_scope(c); \
1550 return 0; \
1551 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001552}
1553
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001554#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 if (!compiler_visit_slice((C), (V), (CTX))) \
1556 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001557}
1558
1559#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001561 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1563 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1564 if (!compiler_visit_ ## TYPE((C), elt)) \
1565 return 0; \
1566 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001567}
1568
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001569#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001571 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1573 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1574 if (!compiler_visit_ ## TYPE((C), elt)) { \
1575 compiler_exit_scope(c); \
1576 return 0; \
1577 } \
1578 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001579}
1580
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001581/* Search if variable annotations are present statically in a block. */
1582
1583static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001584find_ann(asdl_stmt_seq *stmts)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001585{
1586 int i, j, res = 0;
1587 stmt_ty st;
1588
1589 for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1590 st = (stmt_ty)asdl_seq_GET(stmts, i);
1591 switch (st->kind) {
1592 case AnnAssign_kind:
1593 return 1;
1594 case For_kind:
1595 res = find_ann(st->v.For.body) ||
1596 find_ann(st->v.For.orelse);
1597 break;
1598 case AsyncFor_kind:
1599 res = find_ann(st->v.AsyncFor.body) ||
1600 find_ann(st->v.AsyncFor.orelse);
1601 break;
1602 case While_kind:
1603 res = find_ann(st->v.While.body) ||
1604 find_ann(st->v.While.orelse);
1605 break;
1606 case If_kind:
1607 res = find_ann(st->v.If.body) ||
1608 find_ann(st->v.If.orelse);
1609 break;
1610 case With_kind:
1611 res = find_ann(st->v.With.body);
1612 break;
1613 case AsyncWith_kind:
1614 res = find_ann(st->v.AsyncWith.body);
1615 break;
1616 case Try_kind:
1617 for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1618 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1619 st->v.Try.handlers, j);
1620 if (find_ann(handler->v.ExceptHandler.body)) {
1621 return 1;
1622 }
1623 }
1624 res = find_ann(st->v.Try.body) ||
1625 find_ann(st->v.Try.finalbody) ||
1626 find_ann(st->v.Try.orelse);
1627 break;
1628 default:
1629 res = 0;
1630 }
1631 if (res) {
1632 break;
1633 }
1634 }
1635 return res;
1636}
1637
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001638/*
1639 * Frame block handling functions
1640 */
1641
1642static int
1643compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b,
Mark Shannonfee55262019-11-21 09:11:43 +00001644 basicblock *exit, void *datum)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001645{
1646 struct fblockinfo *f;
1647 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
Mark Shannon02d126a2020-09-25 14:04:19 +01001648 return compiler_error(c, "too many statically nested blocks");
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001649 }
1650 f = &c->u->u_fblock[c->u->u_nfblocks++];
1651 f->fb_type = t;
1652 f->fb_block = b;
1653 f->fb_exit = exit;
Mark Shannonfee55262019-11-21 09:11:43 +00001654 f->fb_datum = datum;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001655 return 1;
1656}
1657
1658static void
1659compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
1660{
1661 struct compiler_unit *u = c->u;
1662 assert(u->u_nfblocks > 0);
1663 u->u_nfblocks--;
1664 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
1665 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
1666}
1667
Mark Shannonfee55262019-11-21 09:11:43 +00001668static int
1669compiler_call_exit_with_nones(struct compiler *c) {
1670 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1671 ADDOP(c, DUP_TOP);
1672 ADDOP(c, DUP_TOP);
1673 ADDOP_I(c, CALL_FUNCTION, 3);
1674 return 1;
1675}
1676
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001677/* Unwind a frame block. If preserve_tos is true, the TOS before
Mark Shannonfee55262019-11-21 09:11:43 +00001678 * popping the blocks will be restored afterwards, unless another
1679 * return, break or continue is found. In which case, the TOS will
1680 * be popped.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001681 */
1682static int
1683compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
1684 int preserve_tos)
1685{
1686 switch (info->fb_type) {
1687 case WHILE_LOOP:
Mark Shannon02d126a2020-09-25 14:04:19 +01001688 case EXCEPTION_HANDLER:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001689 return 1;
1690
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001691 case FOR_LOOP:
1692 /* Pop the iterator */
1693 if (preserve_tos) {
1694 ADDOP(c, ROT_TWO);
1695 }
1696 ADDOP(c, POP_TOP);
1697 return 1;
1698
Mark Shannon02d126a2020-09-25 14:04:19 +01001699 case TRY_EXCEPT:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001700 ADDOP(c, POP_BLOCK);
1701 return 1;
1702
1703 case FINALLY_TRY:
Mark Shannon5274b682020-12-16 13:07:01 +00001704 /* This POP_BLOCK gets the line number of the unwinding statement */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001705 ADDOP(c, POP_BLOCK);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001706 if (preserve_tos) {
Mark Shannonfee55262019-11-21 09:11:43 +00001707 if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) {
1708 return 0;
1709 }
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001710 }
Mark Shannon5274b682020-12-16 13:07:01 +00001711 /* Emit the finally block */
Mark Shannonfee55262019-11-21 09:11:43 +00001712 VISIT_SEQ(c, stmt, info->fb_datum);
1713 if (preserve_tos) {
1714 compiler_pop_fblock(c, POP_VALUE, NULL);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001715 }
Mark Shannon5274b682020-12-16 13:07:01 +00001716 /* The finally block should appear to execute after the
1717 * statement causing the unwinding, so make the unwinding
1718 * instruction artificial */
1719 c->u->u_lineno = -1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001720 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001721
Mark Shannonfee55262019-11-21 09:11:43 +00001722 case FINALLY_END:
1723 if (preserve_tos) {
1724 ADDOP(c, ROT_FOUR);
1725 }
1726 ADDOP(c, POP_TOP);
1727 ADDOP(c, POP_TOP);
1728 ADDOP(c, POP_TOP);
1729 if (preserve_tos) {
1730 ADDOP(c, ROT_FOUR);
1731 }
1732 ADDOP(c, POP_EXCEPT);
1733 return 1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001734
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001735 case WITH:
1736 case ASYNC_WITH:
1737 ADDOP(c, POP_BLOCK);
1738 if (preserve_tos) {
1739 ADDOP(c, ROT_TWO);
1740 }
Mark Shannonfee55262019-11-21 09:11:43 +00001741 if(!compiler_call_exit_with_nones(c)) {
1742 return 0;
1743 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001744 if (info->fb_type == ASYNC_WITH) {
1745 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001746 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001747 ADDOP(c, YIELD_FROM);
1748 }
Mark Shannonfee55262019-11-21 09:11:43 +00001749 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001750 return 1;
1751
1752 case HANDLER_CLEANUP:
Mark Shannonfee55262019-11-21 09:11:43 +00001753 if (info->fb_datum) {
1754 ADDOP(c, POP_BLOCK);
1755 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001756 if (preserve_tos) {
1757 ADDOP(c, ROT_FOUR);
1758 }
Mark Shannonfee55262019-11-21 09:11:43 +00001759 ADDOP(c, POP_EXCEPT);
1760 if (info->fb_datum) {
1761 ADDOP_LOAD_CONST(c, Py_None);
1762 compiler_nameop(c, info->fb_datum, Store);
1763 compiler_nameop(c, info->fb_datum, Del);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001764 }
Mark Shannonfee55262019-11-21 09:11:43 +00001765 return 1;
1766
1767 case POP_VALUE:
1768 if (preserve_tos) {
1769 ADDOP(c, ROT_TWO);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001770 }
Mark Shannonfee55262019-11-21 09:11:43 +00001771 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001772 return 1;
1773 }
1774 Py_UNREACHABLE();
1775}
1776
Mark Shannonfee55262019-11-21 09:11:43 +00001777/** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */
1778static int
1779compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) {
1780 if (c->u->u_nfblocks == 0) {
1781 return 1;
1782 }
1783 struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1];
1784 if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) {
1785 *loop = top;
1786 return 1;
1787 }
1788 struct fblockinfo copy = *top;
1789 c->u->u_nfblocks--;
1790 if (!compiler_unwind_fblock(c, &copy, preserve_tos)) {
1791 return 0;
1792 }
1793 if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) {
1794 return 0;
1795 }
1796 c->u->u_fblock[c->u->u_nfblocks] = copy;
1797 c->u->u_nfblocks++;
1798 return 1;
1799}
1800
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001801/* Compile a sequence of statements, checking for a docstring
1802 and for annotations. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001803
1804static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001805compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001807 int i = 0;
1808 stmt_ty st;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001809 PyObject *docstring;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001810
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001811 /* Set current line number to the line number of first statement.
1812 This way line number for SETUP_ANNOTATIONS will always
1813 coincide with the line number of first "real" statement in module.
Hansraj Das01171eb2019-10-09 07:54:02 +05301814 If body is empty, then lineno will be set later in assemble. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001815 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) {
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001816 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001817 SET_LOC(c, st);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001818 }
1819 /* Every annotated class and module should have __annotations__. */
1820 if (find_ann(stmts)) {
1821 ADDOP(c, SETUP_ANNOTATIONS);
1822 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001823 if (!asdl_seq_LEN(stmts))
1824 return 1;
INADA Naokicb41b272017-02-23 00:31:59 +09001825 /* if not -OO mode, set docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001826 if (c->c_optimize < 2) {
1827 docstring = _PyAST_GetDocString(stmts);
1828 if (docstring) {
1829 i = 1;
1830 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1831 assert(st->kind == Expr_kind);
1832 VISIT(c, expr, st->v.Expr.value);
1833 if (!compiler_nameop(c, __doc__, Store))
1834 return 0;
1835 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001837 for (; i < asdl_seq_LEN(stmts); i++)
1838 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001840}
1841
1842static PyCodeObject *
1843compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001844{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 PyCodeObject *co;
1846 int addNone = 1;
1847 static PyObject *module;
1848 if (!module) {
1849 module = PyUnicode_InternFromString("<module>");
1850 if (!module)
1851 return NULL;
1852 }
1853 /* Use 0 for firstlineno initially, will fixup in assemble(). */
Mark Shannon877df852020-11-12 09:43:29 +00001854 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 return NULL;
1856 switch (mod->kind) {
1857 case Module_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001858 if (!compiler_body(c, mod->v.Module.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 compiler_exit_scope(c);
1860 return 0;
1861 }
1862 break;
1863 case Interactive_kind:
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001864 if (find_ann(mod->v.Interactive.body)) {
1865 ADDOP(c, SETUP_ANNOTATIONS);
1866 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 c->c_interactive = 1;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001868 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 break;
1870 case Expression_kind:
1871 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1872 addNone = 0;
1873 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 default:
1875 PyErr_Format(PyExc_SystemError,
1876 "module kind %d should not be possible",
1877 mod->kind);
1878 return 0;
1879 }
1880 co = assemble(c, addNone);
1881 compiler_exit_scope(c);
1882 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001883}
1884
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885/* The test for LOCAL must come before the test for FREE in order to
1886 handle classes where name is both local and free. The local var is
1887 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001888*/
1889
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001890static int
1891get_ref_type(struct compiler *c, PyObject *name)
1892{
Victor Stinner0b1bc562013-05-16 22:17:17 +02001893 int scope;
Benjamin Peterson312595c2013-05-15 15:26:42 -05001894 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001895 _PyUnicode_EqualToASCIIString(name, "__class__"))
Benjamin Peterson312595c2013-05-15 15:26:42 -05001896 return CELL;
Victor Stinner0b1bc562013-05-16 22:17:17 +02001897 scope = PyST_GetScope(c->u->u_ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 if (scope == 0) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01001899 _Py_FatalErrorFormat(__func__,
1900 "unknown scope for %.100s in %.100s(%s)\n"
1901 "symbols: %s\nlocals: %s\nglobals: %s",
1902 PyUnicode_AsUTF8(name),
1903 PyUnicode_AsUTF8(c->u->u_name),
1904 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)),
1905 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)),
1906 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)),
1907 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001911}
1912
1913static int
1914compiler_lookup_arg(PyObject *dict, PyObject *name)
1915{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001916 PyObject *v;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001917 v = PyDict_GetItemWithError(dict, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001918 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001919 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001920 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921}
1922
1923static int
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001924compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001926 Py_ssize_t i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001927 if (qualname == NULL)
1928 qualname = co->co_name;
1929
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001930 if (free) {
1931 for (i = 0; i < free; ++i) {
1932 /* Bypass com_addop_varname because it will generate
1933 LOAD_DEREF but LOAD_CLOSURE is needed.
1934 */
1935 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1936 int arg, reftype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001937
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001938 /* Special case: If a class contains a method with a
1939 free variable that has the same name as a method,
1940 the name will be considered free *and* local in the
1941 class. It should be handled by the closure, as
Min ho Kimc4cacc82019-07-31 08:16:13 +10001942 well as by the normal name lookup logic.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001943 */
1944 reftype = get_ref_type(c, name);
1945 if (reftype == CELL)
1946 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1947 else /* (reftype == FREE) */
1948 arg = compiler_lookup_arg(c->u->u_freevars, name);
1949 if (arg == -1) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01001950 _Py_FatalErrorFormat(__func__,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001951 "lookup %s in %s %d %d\n"
1952 "freevars of %s: %s\n",
1953 PyUnicode_AsUTF8(PyObject_Repr(name)),
1954 PyUnicode_AsUTF8(c->u->u_name),
1955 reftype, arg,
1956 PyUnicode_AsUTF8(co->co_name),
1957 PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars)));
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001958 }
1959 ADDOP_I(c, LOAD_CLOSURE, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001961 flags |= 0x08;
1962 ADDOP_I(c, BUILD_TUPLE, free);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001964 ADDOP_LOAD_CONST(c, (PyObject*)co);
1965 ADDOP_LOAD_CONST(c, qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001966 ADDOP_I(c, MAKE_FUNCTION, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001968}
1969
1970static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001971compiler_decorators(struct compiler *c, asdl_expr_seq* decos)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001972{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 if (!decos)
1976 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1979 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1980 }
1981 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001982}
1983
1984static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001985compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs,
1986 asdl_expr_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001987{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001988 /* Push a dict of keyword-only default values.
1989
1990 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
1991 */
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001992 int i;
1993 PyObject *keys = NULL;
1994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1996 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1997 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1998 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04001999 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002000 if (!mangled) {
2001 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002002 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002003 if (keys == NULL) {
2004 keys = PyList_New(1);
2005 if (keys == NULL) {
2006 Py_DECREF(mangled);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002007 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002008 }
2009 PyList_SET_ITEM(keys, 0, mangled);
2010 }
2011 else {
2012 int res = PyList_Append(keys, mangled);
2013 Py_DECREF(mangled);
2014 if (res == -1) {
2015 goto error;
2016 }
2017 }
2018 if (!compiler_visit_expr(c, default_)) {
2019 goto error;
2020 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 }
2022 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002023 if (keys != NULL) {
2024 Py_ssize_t default_count = PyList_GET_SIZE(keys);
2025 PyObject *keys_tuple = PyList_AsTuple(keys);
2026 Py_DECREF(keys);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002027 ADDOP_LOAD_CONST_NEW(c, keys_tuple);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002028 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002029 assert(default_count > 0);
2030 return 1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002031 }
2032 else {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002033 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002034 }
2035
2036error:
2037 Py_XDECREF(keys);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002038 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002039}
2040
2041static int
Guido van Rossum95e4d582018-01-26 08:20:18 -08002042compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
2043{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +03002044 ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation));
Guido van Rossum95e4d582018-01-26 08:20:18 -08002045 return 1;
2046}
2047
2048static int
Neal Norwitzc1505362006-12-28 06:47:50 +00002049compiler_visit_argannotation(struct compiler *c, identifier id,
Yurii Karabas73019792020-11-25 12:43:18 +02002050 expr_ty annotation, Py_ssize_t *annotations_len)
Neal Norwitzc1505362006-12-28 06:47:50 +00002051{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 if (annotation) {
Yurii Karabas73019792020-11-25 12:43:18 +02002053 PyObject *mangled = _Py_Mangle(c->u->u_private, id);
Yury Selivanov34ce99f2014-02-18 12:49:41 -05002054 if (!mangled)
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002055 return 0;
Yurii Karabas73019792020-11-25 12:43:18 +02002056
2057 ADDOP_LOAD_CONST(c, mangled);
Yury Selivanov34ce99f2014-02-18 12:49:41 -05002058 Py_DECREF(mangled);
Yurii Karabas73019792020-11-25 12:43:18 +02002059 VISIT(c, annexpr, annotation);
2060 *annotations_len += 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002062 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002063}
2064
2065static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002066compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args,
Yurii Karabas73019792020-11-25 12:43:18 +02002067 Py_ssize_t *annotations_len)
Neal Norwitzc1505362006-12-28 06:47:50 +00002068{
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002069 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070 for (i = 0; i < asdl_seq_LEN(args); i++) {
2071 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002072 if (!compiler_visit_argannotation(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 c,
2074 arg->arg,
2075 arg->annotation,
Yurii Karabas73019792020-11-25 12:43:18 +02002076 annotations_len))
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002077 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002078 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002079 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002080}
2081
2082static int
2083compiler_visit_annotations(struct compiler *c, arguments_ty args,
2084 expr_ty returns)
2085{
Yurii Karabas73019792020-11-25 12:43:18 +02002086 /* Push arg annotation names and values.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002087 The expressions are evaluated out-of-order wrt the source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00002088
Yurii Karabas73019792020-11-25 12:43:18 +02002089 Return 0 on error, -1 if no annotations pushed, 1 if a annotations is pushed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 */
2091 static identifier return_str;
Yurii Karabas73019792020-11-25 12:43:18 +02002092 Py_ssize_t annotations_len = 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002093
Yurii Karabas73019792020-11-25 12:43:18 +02002094 if (!compiler_visit_argannotations(c, args->args, &annotations_len))
2095 return 0;
2096 if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len))
2097 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002098 if (args->vararg && args->vararg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002099 !compiler_visit_argannotation(c, args->vararg->arg,
Yurii Karabas73019792020-11-25 12:43:18 +02002100 args->vararg->annotation, &annotations_len))
2101 return 0;
2102 if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len))
2103 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002104 if (args->kwarg && args->kwarg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002105 !compiler_visit_argannotation(c, args->kwarg->arg,
Yurii Karabas73019792020-11-25 12:43:18 +02002106 args->kwarg->annotation, &annotations_len))
2107 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 if (!return_str) {
2110 return_str = PyUnicode_InternFromString("return");
2111 if (!return_str)
Yurii Karabas73019792020-11-25 12:43:18 +02002112 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113 }
Yurii Karabas73019792020-11-25 12:43:18 +02002114 if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) {
2115 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116 }
2117
Yurii Karabas73019792020-11-25 12:43:18 +02002118 if (annotations_len) {
2119 ADDOP_I(c, BUILD_TUPLE, annotations_len);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002120 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002122
Yurii Karabas73019792020-11-25 12:43:18 +02002123 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002124}
2125
2126static int
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002127compiler_visit_defaults(struct compiler *c, arguments_ty args)
2128{
2129 VISIT_SEQ(c, expr, args->defaults);
2130 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
2131 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132}
2133
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002134static Py_ssize_t
2135compiler_default_arguments(struct compiler *c, arguments_ty args)
2136{
2137 Py_ssize_t funcflags = 0;
2138 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002139 if (!compiler_visit_defaults(c, args))
2140 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002141 funcflags |= 0x01;
2142 }
2143 if (args->kwonlyargs) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002144 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002145 args->kw_defaults);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002146 if (res == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002147 return -1;
2148 }
2149 else if (res > 0) {
2150 funcflags |= 0x02;
2151 }
2152 }
2153 return funcflags;
2154}
2155
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002156static int
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002157forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx)
2158{
2159
2160 if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2161 compiler_error(c, "cannot assign to __debug__");
2162 return 1;
2163 }
2164 return 0;
2165}
2166
2167static int
2168compiler_check_debug_one_arg(struct compiler *c, arg_ty arg)
2169{
2170 if (arg != NULL) {
2171 if (forbidden_name(c, arg->arg, Store))
2172 return 0;
2173 }
2174 return 1;
2175}
2176
2177static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002178compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args)
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002179{
2180 if (args != NULL) {
Pablo Galindoee40e4b2020-04-23 03:43:08 +01002181 for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002182 if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i)))
2183 return 0;
2184 }
2185 }
2186 return 1;
2187}
2188
2189static int
2190compiler_check_debug_args(struct compiler *c, arguments_ty args)
2191{
2192 if (!compiler_check_debug_args_seq(c, args->posonlyargs))
2193 return 0;
2194 if (!compiler_check_debug_args_seq(c, args->args))
2195 return 0;
2196 if (!compiler_check_debug_one_arg(c, args->vararg))
2197 return 0;
2198 if (!compiler_check_debug_args_seq(c, args->kwonlyargs))
2199 return 0;
2200 if (!compiler_check_debug_one_arg(c, args->kwarg))
2201 return 0;
2202 return 1;
2203}
2204
2205static int
Yury Selivanov75445082015-05-11 22:57:16 -04002206compiler_function(struct compiler *c, stmt_ty s, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 PyCodeObject *co;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002209 PyObject *qualname, *docstring = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002210 arguments_ty args;
2211 expr_ty returns;
2212 identifier name;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002213 asdl_expr_seq* decos;
2214 asdl_stmt_seq *body;
INADA Naokicb41b272017-02-23 00:31:59 +09002215 Py_ssize_t i, funcflags;
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002216 int annotations;
Yury Selivanov75445082015-05-11 22:57:16 -04002217 int scope_type;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002218 int firstlineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002219
Yury Selivanov75445082015-05-11 22:57:16 -04002220 if (is_async) {
2221 assert(s->kind == AsyncFunctionDef_kind);
2222
2223 args = s->v.AsyncFunctionDef.args;
2224 returns = s->v.AsyncFunctionDef.returns;
2225 decos = s->v.AsyncFunctionDef.decorator_list;
2226 name = s->v.AsyncFunctionDef.name;
2227 body = s->v.AsyncFunctionDef.body;
2228
2229 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
2230 } else {
2231 assert(s->kind == FunctionDef_kind);
2232
2233 args = s->v.FunctionDef.args;
2234 returns = s->v.FunctionDef.returns;
2235 decos = s->v.FunctionDef.decorator_list;
2236 name = s->v.FunctionDef.name;
2237 body = s->v.FunctionDef.body;
2238
2239 scope_type = COMPILER_SCOPE_FUNCTION;
2240 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002241
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002242 if (!compiler_check_debug_args(c, args))
2243 return 0;
2244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 if (!compiler_decorators(c, decos))
2246 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002247
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002248 firstlineno = s->lineno;
2249 if (asdl_seq_LEN(decos)) {
2250 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2251 }
2252
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002253 funcflags = compiler_default_arguments(c, args);
2254 if (funcflags == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002256 }
2257
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002258 annotations = compiler_visit_annotations(c, args, returns);
2259 if (annotations == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002260 return 0;
2261 }
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002262 else if (annotations > 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002263 funcflags |= 0x04;
2264 }
2265
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002266 if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002267 return 0;
2268 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002269
INADA Naokicb41b272017-02-23 00:31:59 +09002270 /* if not -OO mode, add docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002271 if (c->c_optimize < 2) {
2272 docstring = _PyAST_GetDocString(body);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002273 }
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002274 if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 compiler_exit_scope(c);
2276 return 0;
2277 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002280 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Mark Shannon877df852020-11-12 09:43:29 +00002282 for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) {
Mark Shannonfd009e62020-11-13 12:53:53 +00002283 VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i));
Mark Shannon877df852020-11-12 09:43:29 +00002284 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002285 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002286 qualname = c->u->u_qualname;
2287 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002289 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002290 Py_XDECREF(qualname);
2291 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002293 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002294
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002295 compiler_make_closure(c, co, funcflags, qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002296 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 /* decorators */
2300 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2301 ADDOP_I(c, CALL_FUNCTION, 1);
2302 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002303
Yury Selivanov75445082015-05-11 22:57:16 -04002304 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002305}
2306
2307static int
2308compiler_class(struct compiler *c, stmt_ty s)
2309{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 PyCodeObject *co;
2311 PyObject *str;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002312 int i, firstlineno;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002313 asdl_expr_seq *decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 if (!compiler_decorators(c, decos))
2316 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002317
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002318 firstlineno = s->lineno;
2319 if (asdl_seq_LEN(decos)) {
2320 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2321 }
2322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 /* ultimately generate code for:
2324 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
2325 where:
2326 <func> is a function/closure created from the class body;
2327 it has a single argument (__locals__) where the dict
2328 (or MutableSequence) representing the locals is passed
2329 <name> is the class name
2330 <bases> is the positional arguments and *varargs argument
2331 <keywords> is the keyword arguments and **kwds argument
2332 This borrows from compiler_call.
2333 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002336 if (!compiler_enter_scope(c, s->v.ClassDef.name,
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002337 COMPILER_SCOPE_CLASS, (void *)s, firstlineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 return 0;
2339 /* this block represents what we do in the new scope */
2340 {
2341 /* use the class name for name mangling */
2342 Py_INCREF(s->v.ClassDef.name);
Serhiy Storchaka48842712016-04-06 09:45:48 +03002343 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 /* load (global) __name__ ... */
2345 str = PyUnicode_InternFromString("__name__");
2346 if (!str || !compiler_nameop(c, str, Load)) {
2347 Py_XDECREF(str);
2348 compiler_exit_scope(c);
2349 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002350 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 Py_DECREF(str);
2352 /* ... and store it as __module__ */
2353 str = PyUnicode_InternFromString("__module__");
2354 if (!str || !compiler_nameop(c, str, Store)) {
2355 Py_XDECREF(str);
2356 compiler_exit_scope(c);
2357 return 0;
2358 }
2359 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002360 assert(c->u->u_qualname);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002361 ADDOP_LOAD_CONST(c, c->u->u_qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002362 str = PyUnicode_InternFromString("__qualname__");
2363 if (!str || !compiler_nameop(c, str, Store)) {
2364 Py_XDECREF(str);
2365 compiler_exit_scope(c);
2366 return 0;
2367 }
2368 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 /* compile the body proper */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002370 if (!compiler_body(c, s->v.ClassDef.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 compiler_exit_scope(c);
2372 return 0;
2373 }
Mark Shannone56d54e2021-01-15 13:52:00 +00002374 /* The following code is artificial */
2375 c->u->u_lineno = -1;
Nick Coghlan19d24672016-12-05 16:47:55 +10002376 /* Return __classcell__ if it is referenced, otherwise return None */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002377 if (c->u->u_ste->ste_needs_class_closure) {
Nick Coghlan19d24672016-12-05 16:47:55 +10002378 /* Store __classcell__ into class namespace & return it */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002379 str = PyUnicode_InternFromString("__class__");
2380 if (str == NULL) {
2381 compiler_exit_scope(c);
2382 return 0;
2383 }
2384 i = compiler_lookup_arg(c->u->u_cellvars, str);
2385 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01002386 if (i < 0) {
2387 compiler_exit_scope(c);
2388 return 0;
2389 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002390 assert(i == 0);
Nick Coghlan944368e2016-09-11 14:45:49 +10002391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 ADDOP_I(c, LOAD_CLOSURE, i);
Nick Coghlan19d24672016-12-05 16:47:55 +10002393 ADDOP(c, DUP_TOP);
Nick Coghlan944368e2016-09-11 14:45:49 +10002394 str = PyUnicode_InternFromString("__classcell__");
2395 if (!str || !compiler_nameop(c, str, Store)) {
2396 Py_XDECREF(str);
2397 compiler_exit_scope(c);
2398 return 0;
2399 }
2400 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002402 else {
Nick Coghlan19d24672016-12-05 16:47:55 +10002403 /* No methods referenced __class__, so just return None */
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002404 assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002405 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson312595c2013-05-15 15:26:42 -05002406 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002407 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 /* create the code object */
2409 co = assemble(c, 1);
2410 }
2411 /* leave the new scope */
2412 compiler_exit_scope(c);
2413 if (co == NULL)
2414 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 /* 2. load the 'build_class' function */
2417 ADDOP(c, LOAD_BUILD_CLASS);
2418
2419 /* 3. load a function (or closure) made from the code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002420 compiler_make_closure(c, co, 0, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 Py_DECREF(co);
2422
2423 /* 4. load class name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002424 ADDOP_LOAD_CONST(c, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425
2426 /* 5. generate the rest of the code for the call */
Pablo Galindoa5634c42020-09-16 19:42:00 +01002427 if (!compiler_call_helper(c, 2, s->v.ClassDef.bases, s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 return 0;
2429
2430 /* 6. apply decorators */
2431 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2432 ADDOP_I(c, CALL_FUNCTION, 1);
2433 }
2434
2435 /* 7. store into <name> */
2436 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2437 return 0;
2438 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002439}
2440
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02002441/* Return 0 if the expression is a constant value except named singletons.
2442 Return 1 otherwise. */
2443static int
2444check_is_arg(expr_ty e)
2445{
2446 if (e->kind != Constant_kind) {
2447 return 1;
2448 }
2449 PyObject *value = e->v.Constant.value;
2450 return (value == Py_None
2451 || value == Py_False
2452 || value == Py_True
2453 || value == Py_Ellipsis);
2454}
2455
2456/* Check operands of identity chacks ("is" and "is not").
2457 Emit a warning if any operand is a constant except named singletons.
2458 Return 0 on error.
2459 */
2460static int
2461check_compare(struct compiler *c, expr_ty e)
2462{
2463 Py_ssize_t i, n;
2464 int left = check_is_arg(e->v.Compare.left);
2465 n = asdl_seq_LEN(e->v.Compare.ops);
2466 for (i = 0; i < n; i++) {
2467 cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i);
2468 int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2469 if (op == Is || op == IsNot) {
2470 if (!right || !left) {
2471 const char *msg = (op == Is)
2472 ? "\"is\" with a literal. Did you mean \"==\"?"
2473 : "\"is not\" with a literal. Did you mean \"!=\"?";
2474 return compiler_warn(c, msg);
2475 }
2476 }
2477 left = right;
2478 }
2479 return 1;
2480}
2481
Mark Shannon9af0e472020-01-14 10:12:45 +00002482static int compiler_addcompare(struct compiler *c, cmpop_ty op)
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002483{
Mark Shannon9af0e472020-01-14 10:12:45 +00002484 int cmp;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002485 switch (op) {
2486 case Eq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002487 cmp = Py_EQ;
2488 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002489 case NotEq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002490 cmp = Py_NE;
2491 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002492 case Lt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002493 cmp = Py_LT;
2494 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002495 case LtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002496 cmp = Py_LE;
2497 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002498 case Gt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002499 cmp = Py_GT;
2500 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002501 case GtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002502 cmp = Py_GE;
2503 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002504 case Is:
Mark Shannon9af0e472020-01-14 10:12:45 +00002505 ADDOP_I(c, IS_OP, 0);
2506 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002507 case IsNot:
Mark Shannon9af0e472020-01-14 10:12:45 +00002508 ADDOP_I(c, IS_OP, 1);
2509 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002510 case In:
Mark Shannon9af0e472020-01-14 10:12:45 +00002511 ADDOP_I(c, CONTAINS_OP, 0);
2512 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002513 case NotIn:
Mark Shannon9af0e472020-01-14 10:12:45 +00002514 ADDOP_I(c, CONTAINS_OP, 1);
2515 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002516 default:
Mark Shannon9af0e472020-01-14 10:12:45 +00002517 Py_UNREACHABLE();
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002518 }
Mark Shannon9af0e472020-01-14 10:12:45 +00002519 ADDOP_I(c, COMPARE_OP, cmp);
2520 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002521}
2522
Mark Shannon9af0e472020-01-14 10:12:45 +00002523
2524
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002525static int
2526compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2527{
2528 switch (e->kind) {
2529 case UnaryOp_kind:
2530 if (e->v.UnaryOp.op == Not)
2531 return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2532 /* fallback to general implementation */
2533 break;
2534 case BoolOp_kind: {
Pablo Galindoa5634c42020-09-16 19:42:00 +01002535 asdl_expr_seq *s = e->v.BoolOp.values;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002536 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2537 assert(n >= 0);
2538 int cond2 = e->v.BoolOp.op == Or;
2539 basicblock *next2 = next;
2540 if (!cond2 != !cond) {
2541 next2 = compiler_new_block(c);
2542 if (next2 == NULL)
2543 return 0;
2544 }
2545 for (i = 0; i < n; ++i) {
2546 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2547 return 0;
2548 }
2549 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2550 return 0;
2551 if (next2 != next)
2552 compiler_use_next_block(c, next2);
2553 return 1;
2554 }
2555 case IfExp_kind: {
2556 basicblock *end, *next2;
2557 end = compiler_new_block(c);
2558 if (end == NULL)
2559 return 0;
2560 next2 = compiler_new_block(c);
2561 if (next2 == NULL)
2562 return 0;
2563 if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2564 return 0;
2565 if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2566 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002567 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002568 compiler_use_next_block(c, next2);
2569 if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2570 return 0;
2571 compiler_use_next_block(c, end);
2572 return 1;
2573 }
2574 case Compare_kind: {
2575 Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2576 if (n > 0) {
Serhiy Storchaka45835252019-02-16 08:29:46 +02002577 if (!check_compare(c, e)) {
2578 return 0;
2579 }
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002580 basicblock *cleanup = compiler_new_block(c);
2581 if (cleanup == NULL)
2582 return 0;
2583 VISIT(c, expr, e->v.Compare.left);
2584 for (i = 0; i < n; i++) {
2585 VISIT(c, expr,
2586 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2587 ADDOP(c, DUP_TOP);
2588 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00002589 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01002590 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002591 NEXT_BLOCK(c);
2592 }
2593 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00002594 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Mark Shannon582aaf12020-08-04 17:30:11 +01002595 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Mark Shannon266b4622020-11-17 19:30:14 +00002596 NEXT_BLOCK(c);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002597 basicblock *end = compiler_new_block(c);
2598 if (end == NULL)
2599 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002600 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002601 compiler_use_next_block(c, cleanup);
2602 ADDOP(c, POP_TOP);
2603 if (!cond) {
Mark Shannon127dde52021-01-04 18:06:55 +00002604 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002605 }
2606 compiler_use_next_block(c, end);
2607 return 1;
2608 }
2609 /* fallback to general implementation */
2610 break;
2611 }
2612 default:
2613 /* fallback to general implementation */
2614 break;
2615 }
2616
2617 /* general implementation */
2618 VISIT(c, expr, e);
Mark Shannon582aaf12020-08-04 17:30:11 +01002619 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Mark Shannon266b4622020-11-17 19:30:14 +00002620 NEXT_BLOCK(c);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002621 return 1;
2622}
2623
2624static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002625compiler_ifexp(struct compiler *c, expr_ty e)
2626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002627 basicblock *end, *next;
2628
2629 assert(e->kind == IfExp_kind);
2630 end = compiler_new_block(c);
2631 if (end == NULL)
2632 return 0;
2633 next = compiler_new_block(c);
2634 if (next == NULL)
2635 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002636 if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2637 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002638 VISIT(c, expr, e->v.IfExp.body);
Mark Shannon127dde52021-01-04 18:06:55 +00002639 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002640 compiler_use_next_block(c, next);
2641 VISIT(c, expr, e->v.IfExp.orelse);
2642 compiler_use_next_block(c, end);
2643 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002644}
2645
2646static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002647compiler_lambda(struct compiler *c, expr_ty e)
2648{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002649 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002650 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651 static identifier name;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002652 Py_ssize_t funcflags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 arguments_ty args = e->v.Lambda.args;
2654 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002655
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002656 if (!compiler_check_debug_args(c, args))
2657 return 0;
2658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002659 if (!name) {
2660 name = PyUnicode_InternFromString("<lambda>");
2661 if (!name)
2662 return 0;
2663 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002664
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002665 funcflags = compiler_default_arguments(c, args);
2666 if (funcflags == -1) {
2667 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002669
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002670 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002671 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002672 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 /* Make None the first constant, so the lambda can't have a
2675 docstring. */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002676 if (compiler_add_const(c, Py_None) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002680 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002681 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2682 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2683 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002684 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002685 }
2686 else {
2687 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002688 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002689 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002690 qualname = c->u->u_qualname;
2691 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002692 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002693 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002694 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002695
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002696 compiler_make_closure(c, co, funcflags, qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002697 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 Py_DECREF(co);
2699
2700 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002701}
2702
2703static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002704compiler_if(struct compiler *c, stmt_ty s)
2705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002706 basicblock *end, *next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002707 assert(s->kind == If_kind);
2708 end = compiler_new_block(c);
Mark Shannon8473cf82020-12-15 11:07:50 +00002709 if (end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002710 return 0;
Mark Shannon8473cf82020-12-15 11:07:50 +00002711 }
2712 if (asdl_seq_LEN(s->v.If.orelse)) {
2713 next = compiler_new_block(c);
2714 if (next == NULL) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002715 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002716 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002717 }
2718 else {
2719 next = end;
2720 }
2721 if (!compiler_jump_if(c, s->v.If.test, next, 0)) {
2722 return 0;
2723 }
2724 VISIT_SEQ(c, stmt, s->v.If.body);
2725 if (asdl_seq_LEN(s->v.If.orelse)) {
Mark Shannon127dde52021-01-04 18:06:55 +00002726 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Mark Shannon8473cf82020-12-15 11:07:50 +00002727 compiler_use_next_block(c, next);
2728 VISIT_SEQ(c, stmt, s->v.If.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002729 }
2730 compiler_use_next_block(c, end);
2731 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002732}
2733
2734static int
2735compiler_for(struct compiler *c, stmt_ty s)
2736{
Mark Shannon5977a792020-12-02 13:31:40 +00002737 basicblock *start, *body, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002739 start = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002740 body = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002741 cleanup = compiler_new_block(c);
2742 end = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002743 if (start == NULL || body == NULL || end == NULL || cleanup == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002745 }
2746 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002748 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002749 VISIT(c, expr, s->v.For.iter);
2750 ADDOP(c, GET_ITER);
2751 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01002752 ADDOP_JUMP(c, FOR_ITER, cleanup);
Mark Shannon5977a792020-12-02 13:31:40 +00002753 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 VISIT(c, expr, s->v.For.target);
2755 VISIT_SEQ(c, stmt, s->v.For.body);
Mark Shannonf5e97b72020-12-14 11:28:39 +00002756 /* Mark jump as artificial */
2757 c->u->u_lineno = -1;
Mark Shannon582aaf12020-08-04 17:30:11 +01002758 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002759 compiler_use_next_block(c, cleanup);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002760
2761 compiler_pop_fblock(c, FOR_LOOP, start);
2762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 VISIT_SEQ(c, stmt, s->v.For.orelse);
2764 compiler_use_next_block(c, end);
2765 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002766}
2767
Yury Selivanov75445082015-05-11 22:57:16 -04002768
2769static int
2770compiler_async_for(struct compiler *c, stmt_ty s)
2771{
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002772 basicblock *start, *except, *end;
Pablo Galindo90235812020-03-15 04:29:22 +00002773 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07002774 c->u->u_ste->ste_coroutine = 1;
2775 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
Zsolt Dollensteine2396502018-04-27 08:58:56 -07002776 return compiler_error(c, "'async for' outside async function");
2777 }
2778
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002779 start = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002780 except = compiler_new_block(c);
2781 end = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002782
Mark Shannonfee55262019-11-21 09:11:43 +00002783 if (start == NULL || except == NULL || end == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002784 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002785 }
Yury Selivanov75445082015-05-11 22:57:16 -04002786 VISIT(c, expr, s->v.AsyncFor.iter);
2787 ADDOP(c, GET_AITER);
Yury Selivanov75445082015-05-11 22:57:16 -04002788
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002789 compiler_use_next_block(c, start);
Mark Shannonfee55262019-11-21 09:11:43 +00002790 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002791 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002792 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002793 /* SETUP_FINALLY to guard the __anext__ call */
Mark Shannon582aaf12020-08-04 17:30:11 +01002794 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov75445082015-05-11 22:57:16 -04002795 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002796 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04002797 ADDOP(c, YIELD_FROM);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002798 ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */
Yury Selivanov75445082015-05-11 22:57:16 -04002799
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002800 /* Success block for __anext__ */
2801 VISIT(c, expr, s->v.AsyncFor.target);
2802 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002803 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002804
2805 compiler_pop_fblock(c, FOR_LOOP, start);
Yury Selivanov75445082015-05-11 22:57:16 -04002806
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002807 /* Except block for __anext__ */
Yury Selivanov75445082015-05-11 22:57:16 -04002808 compiler_use_next_block(c, except);
Mark Shannon877df852020-11-12 09:43:29 +00002809
2810 c->u->u_lineno = -1;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002811 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov75445082015-05-11 22:57:16 -04002812
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002813 /* `else` block */
Yury Selivanov75445082015-05-11 22:57:16 -04002814 VISIT_SEQ(c, stmt, s->v.For.orelse);
2815
2816 compiler_use_next_block(c, end);
2817
2818 return 1;
2819}
2820
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002821static int
2822compiler_while(struct compiler *c, stmt_ty s)
2823{
Mark Shannon266b4622020-11-17 19:30:14 +00002824 basicblock *loop, *body, *end, *anchor = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002825 loop = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002826 body = compiler_new_block(c);
2827 anchor = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 end = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002829 if (loop == NULL || body == NULL || anchor == NULL || end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 compiler_use_next_block(c, loop);
Mark Shannon266b4622020-11-17 19:30:14 +00002833 if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002835 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002836 if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) {
2837 return 0;
Mark Shannon266b4622020-11-17 19:30:14 +00002838 }
2839
2840 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002841 VISIT_SEQ(c, stmt, s->v.While.body);
Mark Shannon8473cf82020-12-15 11:07:50 +00002842 SET_LOC(c, s);
2843 if (!compiler_jump_if(c, s->v.While.test, body, 1)) {
2844 return 0;
2845 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002846
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002847 compiler_pop_fblock(c, WHILE_LOOP, loop);
2848
Mark Shannon266b4622020-11-17 19:30:14 +00002849 compiler_use_next_block(c, anchor);
2850 if (s->v.While.orelse) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002851 VISIT_SEQ(c, stmt, s->v.While.orelse);
Mark Shannon266b4622020-11-17 19:30:14 +00002852 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002856}
2857
2858static int
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002859compiler_return(struct compiler *c, stmt_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002860{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002861 int preserve_tos = ((s->v.Return.value != NULL) &&
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002862 (s->v.Return.value->kind != Constant_kind));
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002863 if (c->u->u_ste->ste_type != FunctionBlock)
2864 return compiler_error(c, "'return' outside function");
2865 if (s->v.Return.value != NULL &&
2866 c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2867 {
2868 return compiler_error(
2869 c, "'return' with value in async generator");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002871 if (preserve_tos) {
2872 VISIT(c, expr, s->v.Return.value);
Mark Shannon5274b682020-12-16 13:07:01 +00002873 } else {
2874 /* Emit instruction with line number for expression */
2875 if (s->v.Return.value != NULL) {
2876 SET_LOC(c, s->v.Return.value);
2877 ADDOP(c, NOP);
2878 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002879 }
Mark Shannonfee55262019-11-21 09:11:43 +00002880 if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL))
2881 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002882 if (s->v.Return.value == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002883 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002884 }
2885 else if (!preserve_tos) {
Mark Shannon5274b682020-12-16 13:07:01 +00002886 ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002887 }
2888 ADDOP(c, RETURN_VALUE);
Mark Shannon266b4622020-11-17 19:30:14 +00002889 NEXT_BLOCK(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002892}
2893
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002894static int
2895compiler_break(struct compiler *c)
2896{
Mark Shannonfee55262019-11-21 09:11:43 +00002897 struct fblockinfo *loop = NULL;
2898 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
2899 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002900 }
Mark Shannonfee55262019-11-21 09:11:43 +00002901 if (loop == NULL) {
2902 return compiler_error(c, "'break' outside loop");
2903 }
2904 if (!compiler_unwind_fblock(c, loop, 0)) {
2905 return 0;
2906 }
Mark Shannon582aaf12020-08-04 17:30:11 +01002907 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit);
Mark Shannon266b4622020-11-17 19:30:14 +00002908 NEXT_BLOCK(c);
Mark Shannonfee55262019-11-21 09:11:43 +00002909 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002910}
2911
2912static int
2913compiler_continue(struct compiler *c)
2914{
Mark Shannonfee55262019-11-21 09:11:43 +00002915 struct fblockinfo *loop = NULL;
2916 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
2917 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002918 }
Mark Shannonfee55262019-11-21 09:11:43 +00002919 if (loop == NULL) {
2920 return compiler_error(c, "'continue' not properly in loop");
2921 }
Mark Shannon582aaf12020-08-04 17:30:11 +01002922 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block);
Mark Shannon266b4622020-11-17 19:30:14 +00002923 NEXT_BLOCK(c)
Mark Shannonfee55262019-11-21 09:11:43 +00002924 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002925}
2926
2927
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002928/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929
2930 SETUP_FINALLY L
2931 <code for body>
2932 POP_BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00002933 <code for finalbody>
2934 JUMP E
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002935 L:
2936 <code for finalbody>
Mark Shannonfee55262019-11-21 09:11:43 +00002937 E:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002938
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002939 The special instructions use the block stack. Each block
2940 stack entry contains the instruction that created it (here
2941 SETUP_FINALLY), the level of the value stack at the time the
2942 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002944 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002945 Pushes the current value stack level and the label
2946 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002947 POP_BLOCK:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002948 Pops en entry from the block stack.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002949
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002950 The block stack is unwound when an exception is raised:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002951 when a SETUP_FINALLY entry is found, the raised and the caught
2952 exceptions are pushed onto the value stack (and the exception
2953 condition is cleared), and the interpreter jumps to the label
2954 gotten from the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002955*/
2956
2957static int
2958compiler_try_finally(struct compiler *c, stmt_ty s)
2959{
Mark Shannonfee55262019-11-21 09:11:43 +00002960 basicblock *body, *end, *exit;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002962 body = compiler_new_block(c);
2963 end = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00002964 exit = compiler_new_block(c);
2965 if (body == NULL || end == NULL || exit == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002966 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002967
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002968 /* `try` block */
Mark Shannon582aaf12020-08-04 17:30:11 +01002969 ADDOP_JUMP(c, SETUP_FINALLY, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002970 compiler_use_next_block(c, body);
Mark Shannonfee55262019-11-21 09:11:43 +00002971 if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002972 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002973 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
2974 if (!compiler_try_except(c, s))
2975 return 0;
2976 }
2977 else {
2978 VISIT_SEQ(c, stmt, s->v.Try.body);
2979 }
Mark Shannon3bd60352021-01-13 12:05:43 +00002980 ADDOP_NOLINE(c, POP_BLOCK);
Mark Shannonfee55262019-11-21 09:11:43 +00002981 compiler_pop_fblock(c, FINALLY_TRY, body);
2982 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Mark Shannon127dde52021-01-04 18:06:55 +00002983 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00002984 /* `finally` block */
2985 compiler_use_next_block(c, end);
2986 if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL))
2987 return 0;
2988 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
2989 compiler_pop_fblock(c, FINALLY_END, end);
Mark Shannonbf353f32020-12-17 13:55:28 +00002990 ADDOP_I(c, RERAISE, 0);
Mark Shannonfee55262019-11-21 09:11:43 +00002991 compiler_use_next_block(c, exit);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002993}
2994
2995/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002996 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002997 (The contents of the value stack is shown in [], with the top
2998 at the right; 'tb' is trace-back info, 'val' the exception's
2999 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003000
3001 Value stack Label Instruction Argument
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003002 [] SETUP_FINALLY L1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 [] <code for S>
3004 [] POP_BLOCK
3005 [] JUMP_FORWARD L0
3006
3007 [tb, val, exc] L1: DUP )
3008 [tb, val, exc, exc] <evaluate E1> )
Mark Shannon9af0e472020-01-14 10:12:45 +00003009 [tb, val, exc, exc, E1] JUMP_IF_NOT_EXC_MATCH L2 ) only if E1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 [tb, val, exc] POP
3011 [tb, val] <assign to V1> (or POP if no V1)
3012 [tb] POP
3013 [] <code for S1>
3014 JUMP_FORWARD L0
3015
3016 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003017 .............................etc.......................
3018
Mark Shannonfee55262019-11-21 09:11:43 +00003019 [tb, val, exc] Ln+1: RERAISE # re-raise exception
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020
3021 [] L0: <next statement>
3022
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003023 Of course, parts are not generated if Vi or Ei is not present.
3024*/
3025static int
3026compiler_try_except(struct compiler *c, stmt_ty s)
3027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003028 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003029 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 body = compiler_new_block(c);
3032 except = compiler_new_block(c);
3033 orelse = compiler_new_block(c);
3034 end = compiler_new_block(c);
3035 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
3036 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01003037 ADDOP_JUMP(c, SETUP_FINALLY, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003038 compiler_use_next_block(c, body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003039 if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003040 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003041 VISIT_SEQ(c, stmt, s->v.Try.body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003042 compiler_pop_fblock(c, TRY_EXCEPT, body);
Mark Shannon3bd60352021-01-13 12:05:43 +00003043 ADDOP_NOLINE(c, POP_BLOCK);
3044 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003045 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003046 compiler_use_next_block(c, except);
Mark Shannon02d126a2020-09-25 14:04:19 +01003047 /* Runtime will push a block here, so we need to account for that */
3048 if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL))
3049 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003050 for (i = 0; i < n; i++) {
3051 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003052 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003053 if (!handler->v.ExceptHandler.type && i < n-1)
3054 return compiler_error(c, "default 'except:' must be last");
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003055 SET_LOC(c, handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003056 except = compiler_new_block(c);
3057 if (except == NULL)
3058 return 0;
3059 if (handler->v.ExceptHandler.type) {
3060 ADDOP(c, DUP_TOP);
3061 VISIT(c, expr, handler->v.ExceptHandler.type);
Mark Shannon582aaf12020-08-04 17:30:11 +01003062 ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except);
Mark Shannon266b4622020-11-17 19:30:14 +00003063 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003064 }
3065 ADDOP(c, POP_TOP);
3066 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003067 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00003068
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003069 cleanup_end = compiler_new_block(c);
3070 cleanup_body = compiler_new_block(c);
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003071 if (cleanup_end == NULL || cleanup_body == NULL) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003072 return 0;
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003073 }
Guido van Rossumb940e112007-01-10 16:19:56 +00003074
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003075 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3076 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003077
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003078 /*
3079 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003080 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003081 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003082 try:
3083 # body
3084 finally:
Chris Angelicoad098b62019-05-21 23:34:19 +10003085 name = None # in case body contains "del name"
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003086 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003087 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003089 /* second try: */
Mark Shannon582aaf12020-08-04 17:30:11 +01003090 ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003091 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003092 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003093 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003095 /* second # body */
3096 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003097 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003098 ADDOP(c, POP_BLOCK);
3099 ADDOP(c, POP_EXCEPT);
Mark Shannon877df852020-11-12 09:43:29 +00003100 /* name = None; del name; # Mark as artificial */
3101 c->u->u_lineno = -1;
Mark Shannonfee55262019-11-21 09:11:43 +00003102 ADDOP_LOAD_CONST(c, Py_None);
3103 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3104 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Mark Shannon582aaf12020-08-04 17:30:11 +01003105 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106
Mark Shannonfee55262019-11-21 09:11:43 +00003107 /* except: */
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003108 compiler_use_next_block(c, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003109
Mark Shannon877df852020-11-12 09:43:29 +00003110 /* name = None; del name; # Mark as artificial */
3111 c->u->u_lineno = -1;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003112 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003113 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003114 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003115
Mark Shannonbf353f32020-12-17 13:55:28 +00003116 ADDOP_I(c, RERAISE, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003117 }
3118 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003119 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003120
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003121 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05003122 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003123 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003124
Guido van Rossumb940e112007-01-10 16:19:56 +00003125 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003126 ADDOP(c, POP_TOP);
3127 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003128 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003129 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003130 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003131 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannon127dde52021-01-04 18:06:55 +00003132 /* name = None; del name; # Mark as artificial */
3133 c->u->u_lineno = -1;
Mark Shannonfee55262019-11-21 09:11:43 +00003134 ADDOP(c, POP_EXCEPT);
Mark Shannon582aaf12020-08-04 17:30:11 +01003135 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003137 compiler_use_next_block(c, except);
3138 }
Mark Shannon02d126a2020-09-25 14:04:19 +01003139 compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL);
Mark Shannonf2dbfd72020-12-21 13:53:50 +00003140 /* Mark as artificial */
3141 c->u->u_lineno = -1;
Mark Shannonbf353f32020-12-17 13:55:28 +00003142 ADDOP_I(c, RERAISE, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003143 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003144 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145 compiler_use_next_block(c, end);
3146 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003147}
3148
3149static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003150compiler_try(struct compiler *c, stmt_ty s) {
3151 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
3152 return compiler_try_finally(c, s);
3153 else
3154 return compiler_try_except(c, s);
3155}
3156
3157
3158static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003159compiler_import_as(struct compiler *c, identifier name, identifier asname)
3160{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 /* The IMPORT_NAME opcode was already generated. This function
3162 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 If there is a dot in name, we need to split it and emit a
Serhiy Storchakaf93234b2017-05-09 22:31:05 +03003165 IMPORT_FROM for each name.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003166 */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003167 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
3168 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003169 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003170 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003171 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003172 /* Consume the base module name to get the first attribute */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003173 while (1) {
3174 Py_ssize_t pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003175 PyObject *attr;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003176 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003177 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003178 return 0;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003179 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003180 if (!attr)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003181 return 0;
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003182 ADDOP_N(c, IMPORT_FROM, attr, names);
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003183 if (dot == -1) {
3184 break;
3185 }
3186 ADDOP(c, ROT_TWO);
3187 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 }
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003189 if (!compiler_nameop(c, asname, Store)) {
3190 return 0;
3191 }
3192 ADDOP(c, POP_TOP);
3193 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003194 }
3195 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003196}
3197
3198static int
3199compiler_import(struct compiler *c, stmt_ty s)
3200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 /* The Import node stores a module name like a.b.c as a single
3202 string. This is convenient for all cases except
3203 import a.b.c as d
3204 where we need to parse that string to extract the individual
3205 module names.
3206 XXX Perhaps change the representation to make this case simpler?
3207 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01003208 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003209
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003210 PyObject *zero = _PyLong_GetZero(); // borrowed reference
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 for (i = 0; i < n; i++) {
3212 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
3213 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003214
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003215 ADDOP_LOAD_CONST(c, zero);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003216 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 if (alias->asname) {
3220 r = compiler_import_as(c, alias->name, alias->asname);
3221 if (!r)
3222 return r;
3223 }
3224 else {
3225 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003226 Py_ssize_t dot = PyUnicode_FindChar(
3227 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02003228 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003229 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02003230 if (tmp == NULL)
3231 return 0;
3232 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003233 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003234 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003235 Py_DECREF(tmp);
3236 }
3237 if (!r)
3238 return r;
3239 }
3240 }
3241 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003242}
3243
3244static int
3245compiler_from_import(struct compiler *c, stmt_ty s)
3246{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003247 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003248 PyObject *names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003249 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003251 if (!empty_string) {
3252 empty_string = PyUnicode_FromString("");
3253 if (!empty_string)
3254 return 0;
3255 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003256
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003257 ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02003258
3259 names = PyTuple_New(n);
3260 if (!names)
3261 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 /* build up the names */
3264 for (i = 0; i < n; i++) {
3265 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3266 Py_INCREF(alias->name);
3267 PyTuple_SET_ITEM(names, i, alias->name);
3268 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003270 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003271 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 Py_DECREF(names);
3273 return compiler_error(c, "from __future__ imports must occur "
3274 "at the beginning of the file");
3275 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003276 ADDOP_LOAD_CONST_NEW(c, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003278 if (s->v.ImportFrom.module) {
3279 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
3280 }
3281 else {
3282 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
3283 }
3284 for (i = 0; i < n; i++) {
3285 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3286 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003287
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003288 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 assert(n == 1);
3290 ADDOP(c, IMPORT_STAR);
3291 return 1;
3292 }
3293
3294 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
3295 store_name = alias->name;
3296 if (alias->asname)
3297 store_name = alias->asname;
3298
3299 if (!compiler_nameop(c, store_name, Store)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003300 return 0;
3301 }
3302 }
3303 /* remove imported module */
3304 ADDOP(c, POP_TOP);
3305 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003306}
3307
3308static int
3309compiler_assert(struct compiler *c, stmt_ty s)
3310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003312
Georg Brandl8334fd92010-12-04 10:26:46 +00003313 if (c->c_optimize)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 if (s->v.Assert.test->kind == Tuple_kind &&
Serhiy Storchakad31e7732018-10-21 10:09:39 +03003316 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0)
3317 {
3318 if (!compiler_warn(c, "assertion is always true, "
3319 "perhaps remove parentheses?"))
3320 {
Victor Stinner14e461d2013-08-26 22:28:21 +02003321 return 0;
3322 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003323 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 end = compiler_new_block(c);
3325 if (end == NULL)
3326 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003327 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
3328 return 0;
Zackery Spytzce6a0702019-08-25 03:44:09 -06003329 ADDOP(c, LOAD_ASSERTION_ERROR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003330 if (s->v.Assert.msg) {
3331 VISIT(c, expr, s->v.Assert.msg);
3332 ADDOP_I(c, CALL_FUNCTION, 1);
3333 }
3334 ADDOP_I(c, RAISE_VARARGS, 1);
3335 compiler_use_next_block(c, end);
3336 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003337}
3338
3339static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003340compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3341{
3342 if (c->c_interactive && c->c_nestlevel <= 1) {
3343 VISIT(c, expr, value);
3344 ADDOP(c, PRINT_EXPR);
3345 return 1;
3346 }
3347
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003348 if (value->kind == Constant_kind) {
Victor Stinner15a30952016-02-08 22:45:06 +01003349 /* ignore constant statement */
Mark Shannon877df852020-11-12 09:43:29 +00003350 ADDOP(c, NOP);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003351 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003352 }
3353
3354 VISIT(c, expr, value);
3355 ADDOP(c, POP_TOP);
3356 return 1;
3357}
3358
3359static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003360compiler_visit_stmt(struct compiler *c, stmt_ty s)
3361{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003362 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003364 /* Always assign a lineno to the next instruction for a stmt. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003365 SET_LOC(c, s);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003367 switch (s->kind) {
3368 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04003369 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003370 case ClassDef_kind:
3371 return compiler_class(c, s);
3372 case Return_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003373 return compiler_return(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003374 case Delete_kind:
3375 VISIT_SEQ(c, expr, s->v.Delete.targets)
3376 break;
3377 case Assign_kind:
3378 n = asdl_seq_LEN(s->v.Assign.targets);
3379 VISIT(c, expr, s->v.Assign.value);
3380 for (i = 0; i < n; i++) {
3381 if (i < n - 1)
3382 ADDOP(c, DUP_TOP);
3383 VISIT(c, expr,
3384 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3385 }
3386 break;
3387 case AugAssign_kind:
3388 return compiler_augassign(c, s);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003389 case AnnAssign_kind:
3390 return compiler_annassign(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003391 case For_kind:
3392 return compiler_for(c, s);
3393 case While_kind:
3394 return compiler_while(c, s);
3395 case If_kind:
3396 return compiler_if(c, s);
3397 case Raise_kind:
3398 n = 0;
3399 if (s->v.Raise.exc) {
3400 VISIT(c, expr, s->v.Raise.exc);
3401 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003402 if (s->v.Raise.cause) {
3403 VISIT(c, expr, s->v.Raise.cause);
3404 n++;
3405 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003406 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01003407 ADDOP_I(c, RAISE_VARARGS, (int)n);
Mark Shannon266b4622020-11-17 19:30:14 +00003408 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003409 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003410 case Try_kind:
3411 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003412 case Assert_kind:
3413 return compiler_assert(c, s);
3414 case Import_kind:
3415 return compiler_import(c, s);
3416 case ImportFrom_kind:
3417 return compiler_from_import(c, s);
3418 case Global_kind:
3419 case Nonlocal_kind:
3420 break;
3421 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003422 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003423 case Pass_kind:
Mark Shannon877df852020-11-12 09:43:29 +00003424 ADDOP(c, NOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 break;
3426 case Break_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003427 return compiler_break(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 case Continue_kind:
3429 return compiler_continue(c);
3430 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003431 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04003432 case AsyncFunctionDef_kind:
3433 return compiler_function(c, s, 1);
3434 case AsyncWith_kind:
3435 return compiler_async_with(c, s, 0);
3436 case AsyncFor_kind:
3437 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 }
Yury Selivanov75445082015-05-11 22:57:16 -04003439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003440 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003441}
3442
3443static int
3444unaryop(unaryop_ty op)
3445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003446 switch (op) {
3447 case Invert:
3448 return UNARY_INVERT;
3449 case Not:
3450 return UNARY_NOT;
3451 case UAdd:
3452 return UNARY_POSITIVE;
3453 case USub:
3454 return UNARY_NEGATIVE;
3455 default:
3456 PyErr_Format(PyExc_SystemError,
3457 "unary op %d should not be possible", op);
3458 return 0;
3459 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003460}
3461
3462static int
Andy Lester76d58772020-03-10 21:18:12 -05003463binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003464{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003465 switch (op) {
3466 case Add:
3467 return BINARY_ADD;
3468 case Sub:
3469 return BINARY_SUBTRACT;
3470 case Mult:
3471 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003472 case MatMult:
3473 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 case Div:
3475 return BINARY_TRUE_DIVIDE;
3476 case Mod:
3477 return BINARY_MODULO;
3478 case Pow:
3479 return BINARY_POWER;
3480 case LShift:
3481 return BINARY_LSHIFT;
3482 case RShift:
3483 return BINARY_RSHIFT;
3484 case BitOr:
3485 return BINARY_OR;
3486 case BitXor:
3487 return BINARY_XOR;
3488 case BitAnd:
3489 return BINARY_AND;
3490 case FloorDiv:
3491 return BINARY_FLOOR_DIVIDE;
3492 default:
3493 PyErr_Format(PyExc_SystemError,
3494 "binary op %d should not be possible", op);
3495 return 0;
3496 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003497}
3498
3499static int
Andy Lester76d58772020-03-10 21:18:12 -05003500inplace_binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003502 switch (op) {
3503 case Add:
3504 return INPLACE_ADD;
3505 case Sub:
3506 return INPLACE_SUBTRACT;
3507 case Mult:
3508 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003509 case MatMult:
3510 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003511 case Div:
3512 return INPLACE_TRUE_DIVIDE;
3513 case Mod:
3514 return INPLACE_MODULO;
3515 case Pow:
3516 return INPLACE_POWER;
3517 case LShift:
3518 return INPLACE_LSHIFT;
3519 case RShift:
3520 return INPLACE_RSHIFT;
3521 case BitOr:
3522 return INPLACE_OR;
3523 case BitXor:
3524 return INPLACE_XOR;
3525 case BitAnd:
3526 return INPLACE_AND;
3527 case FloorDiv:
3528 return INPLACE_FLOOR_DIVIDE;
3529 default:
3530 PyErr_Format(PyExc_SystemError,
3531 "inplace binary op %d should not be possible", op);
3532 return 0;
3533 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003534}
3535
3536static int
3537compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3538{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003539 int op, scope;
3540 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003541 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003543 PyObject *dict = c->u->u_names;
3544 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003545
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003546 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3547 !_PyUnicode_EqualToASCIIString(name, "True") &&
3548 !_PyUnicode_EqualToASCIIString(name, "False"));
Benjamin Peterson70b224d2012-12-06 17:49:58 -05003549
Pablo Galindoc5fc1562020-04-22 23:29:27 +01003550 if (forbidden_name(c, name, ctx))
3551 return 0;
3552
Serhiy Storchakabd6ec4d2017-12-18 14:29:12 +02003553 mangled = _Py_Mangle(c->u->u_private, name);
3554 if (!mangled)
3555 return 0;
3556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003557 op = 0;
3558 optype = OP_NAME;
3559 scope = PyST_GetScope(c->u->u_ste, mangled);
3560 switch (scope) {
3561 case FREE:
3562 dict = c->u->u_freevars;
3563 optype = OP_DEREF;
3564 break;
3565 case CELL:
3566 dict = c->u->u_cellvars;
3567 optype = OP_DEREF;
3568 break;
3569 case LOCAL:
3570 if (c->u->u_ste->ste_type == FunctionBlock)
3571 optype = OP_FAST;
3572 break;
3573 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04003574 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003575 optype = OP_GLOBAL;
3576 break;
3577 case GLOBAL_EXPLICIT:
3578 optype = OP_GLOBAL;
3579 break;
3580 default:
3581 /* scope can be 0 */
3582 break;
3583 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003585 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003586 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003588 switch (optype) {
3589 case OP_DEREF:
3590 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003591 case Load:
3592 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3593 break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003594 case Store: op = STORE_DEREF; break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003595 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003596 }
3597 break;
3598 case OP_FAST:
3599 switch (ctx) {
3600 case Load: op = LOAD_FAST; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003601 case Store: op = STORE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003602 case Del: op = DELETE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003603 }
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003604 ADDOP_N(c, op, mangled, varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 return 1;
3606 case OP_GLOBAL:
3607 switch (ctx) {
3608 case Load: op = LOAD_GLOBAL; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003609 case Store: op = STORE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 case Del: op = DELETE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003611 }
3612 break;
3613 case OP_NAME:
3614 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00003615 case Load: op = LOAD_NAME; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003616 case Store: op = STORE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003617 case Del: op = DELETE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003618 }
3619 break;
3620 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003622 assert(op);
Andy Lester76d58772020-03-10 21:18:12 -05003623 arg = compiler_add_o(dict, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003624 Py_DECREF(mangled);
3625 if (arg < 0)
3626 return 0;
3627 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003628}
3629
3630static int
3631compiler_boolop(struct compiler *c, expr_ty e)
3632{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003633 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003634 int jumpi;
3635 Py_ssize_t i, n;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003636 asdl_expr_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003638 assert(e->kind == BoolOp_kind);
3639 if (e->v.BoolOp.op == And)
3640 jumpi = JUMP_IF_FALSE_OR_POP;
3641 else
3642 jumpi = JUMP_IF_TRUE_OR_POP;
3643 end = compiler_new_block(c);
3644 if (end == NULL)
3645 return 0;
3646 s = e->v.BoolOp.values;
3647 n = asdl_seq_LEN(s) - 1;
3648 assert(n >= 0);
3649 for (i = 0; i < n; ++i) {
3650 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003651 ADDOP_JUMP(c, jumpi, end);
Mark Shannon6e8128f2020-07-30 10:03:00 +01003652 basicblock *next = compiler_new_block(c);
3653 if (next == NULL) {
3654 return 0;
3655 }
3656 compiler_use_next_block(c, next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003657 }
3658 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3659 compiler_use_next_block(c, end);
3660 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003661}
3662
3663static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003664starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
Mark Shannon13bc1392020-01-23 09:25:17 +00003665 int build, int add, int extend, int tuple)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003666{
3667 Py_ssize_t n = asdl_seq_LEN(elts);
Mark Shannon13bc1392020-01-23 09:25:17 +00003668 Py_ssize_t i, seen_star = 0;
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003669 if (n > 2 && are_all_items_const(elts, 0, n)) {
3670 PyObject *folded = PyTuple_New(n);
3671 if (folded == NULL) {
3672 return 0;
3673 }
3674 PyObject *val;
3675 for (i = 0; i < n; i++) {
3676 val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
3677 Py_INCREF(val);
3678 PyTuple_SET_ITEM(folded, i, val);
3679 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003680 if (tuple) {
3681 ADDOP_LOAD_CONST_NEW(c, folded);
3682 } else {
3683 if (add == SET_ADD) {
3684 Py_SETREF(folded, PyFrozenSet_New(folded));
3685 if (folded == NULL) {
3686 return 0;
3687 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003688 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003689 ADDOP_I(c, build, pushed);
3690 ADDOP_LOAD_CONST_NEW(c, folded);
3691 ADDOP_I(c, extend, 1);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003692 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003693 return 1;
3694 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003695
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003696 for (i = 0; i < n; i++) {
3697 expr_ty elt = asdl_seq_GET(elts, i);
3698 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003699 seen_star = 1;
3700 }
3701 }
3702 if (seen_star) {
3703 seen_star = 0;
3704 for (i = 0; i < n; i++) {
3705 expr_ty elt = asdl_seq_GET(elts, i);
3706 if (elt->kind == Starred_kind) {
3707 if (seen_star == 0) {
3708 ADDOP_I(c, build, i+pushed);
3709 seen_star = 1;
3710 }
3711 VISIT(c, expr, elt->v.Starred.value);
3712 ADDOP_I(c, extend, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003713 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003714 else {
3715 VISIT(c, expr, elt);
3716 if (seen_star) {
3717 ADDOP_I(c, add, 1);
3718 }
3719 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003720 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003721 assert(seen_star);
3722 if (tuple) {
3723 ADDOP(c, LIST_TO_TUPLE);
3724 }
3725 }
3726 else {
3727 for (i = 0; i < n; i++) {
3728 expr_ty elt = asdl_seq_GET(elts, i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003729 VISIT(c, expr, elt);
Mark Shannon13bc1392020-01-23 09:25:17 +00003730 }
3731 if (tuple) {
3732 ADDOP_I(c, BUILD_TUPLE, n+pushed);
3733 } else {
3734 ADDOP_I(c, build, n+pushed);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003735 }
3736 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003737 return 1;
3738}
3739
3740static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003741assignment_helper(struct compiler *c, asdl_expr_seq *elts)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003742{
3743 Py_ssize_t n = asdl_seq_LEN(elts);
3744 Py_ssize_t i;
3745 int seen_star = 0;
3746 for (i = 0; i < n; i++) {
3747 expr_ty elt = asdl_seq_GET(elts, i);
3748 if (elt->kind == Starred_kind && !seen_star) {
3749 if ((i >= (1 << 8)) ||
3750 (n-i-1 >= (INT_MAX >> 8)))
3751 return compiler_error(c,
3752 "too many expressions in "
3753 "star-unpacking assignment");
3754 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3755 seen_star = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003756 }
3757 else if (elt->kind == Starred_kind) {
3758 return compiler_error(c,
Furkan Öndercb6534e2020-03-26 04:54:31 +03003759 "multiple starred expressions in assignment");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003760 }
3761 }
3762 if (!seen_star) {
3763 ADDOP_I(c, UNPACK_SEQUENCE, n);
3764 }
Brandt Bucherd5aa2e92020-03-07 19:44:18 -08003765 for (i = 0; i < n; i++) {
3766 expr_ty elt = asdl_seq_GET(elts, i);
3767 VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value);
3768 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003769 return 1;
3770}
3771
3772static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003773compiler_list(struct compiler *c, expr_ty e)
3774{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003775 asdl_expr_seq *elts = e->v.List.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003776 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003777 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003778 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003779 else if (e->v.List.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003780 return starunpack_helper(c, elts, 0, BUILD_LIST,
3781 LIST_APPEND, LIST_EXTEND, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003782 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003783 else
3784 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003785 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003786}
3787
3788static int
3789compiler_tuple(struct compiler *c, expr_ty e)
3790{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003791 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003792 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003793 return assignment_helper(c, elts);
3794 }
3795 else if (e->v.Tuple.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003796 return starunpack_helper(c, elts, 0, BUILD_LIST,
3797 LIST_APPEND, LIST_EXTEND, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003798 }
3799 else
3800 VISIT_SEQ(c, expr, elts);
3801 return 1;
3802}
3803
3804static int
3805compiler_set(struct compiler *c, expr_ty e)
3806{
Mark Shannon13bc1392020-01-23 09:25:17 +00003807 return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET,
3808 SET_ADD, SET_UPDATE, 0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003809}
3810
3811static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003812are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003813{
3814 Py_ssize_t i;
3815 for (i = begin; i < end; i++) {
3816 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003817 if (key == NULL || key->kind != Constant_kind)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003818 return 0;
3819 }
3820 return 1;
3821}
3822
3823static int
3824compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3825{
3826 Py_ssize_t i, n = end - begin;
3827 PyObject *keys, *key;
3828 if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) {
3829 for (i = begin; i < end; i++) {
3830 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3831 }
3832 keys = PyTuple_New(n);
3833 if (keys == NULL) {
3834 return 0;
3835 }
3836 for (i = begin; i < end; i++) {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003837 key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003838 Py_INCREF(key);
3839 PyTuple_SET_ITEM(keys, i - begin, key);
3840 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003841 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003842 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3843 }
3844 else {
3845 for (i = begin; i < end; i++) {
3846 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3847 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3848 }
3849 ADDOP_I(c, BUILD_MAP, n);
3850 }
3851 return 1;
3852}
3853
3854static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003855compiler_dict(struct compiler *c, expr_ty e)
3856{
Victor Stinner976bb402016-03-23 11:36:19 +01003857 Py_ssize_t i, n, elements;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003858 int have_dict;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003859 int is_unpacking = 0;
3860 n = asdl_seq_LEN(e->v.Dict.values);
Mark Shannon8a4cd702020-01-27 09:57:45 +00003861 have_dict = 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003862 elements = 0;
3863 for (i = 0; i < n; i++) {
3864 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003865 if (is_unpacking) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003866 if (elements) {
3867 if (!compiler_subdict(c, e, i - elements, i)) {
3868 return 0;
3869 }
3870 if (have_dict) {
3871 ADDOP_I(c, DICT_UPDATE, 1);
3872 }
3873 have_dict = 1;
3874 elements = 0;
3875 }
3876 if (have_dict == 0) {
3877 ADDOP_I(c, BUILD_MAP, 0);
3878 have_dict = 1;
3879 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003880 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Mark Shannon8a4cd702020-01-27 09:57:45 +00003881 ADDOP_I(c, DICT_UPDATE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003882 }
3883 else {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003884 if (elements == 0xFFFF) {
Pablo Galindoc51db0e2020-08-13 09:48:41 +01003885 if (!compiler_subdict(c, e, i - elements, i + 1)) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003886 return 0;
3887 }
3888 if (have_dict) {
3889 ADDOP_I(c, DICT_UPDATE, 1);
3890 }
3891 have_dict = 1;
3892 elements = 0;
3893 }
3894 else {
3895 elements++;
3896 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003897 }
3898 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003899 if (elements) {
3900 if (!compiler_subdict(c, e, n - elements, n)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003901 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003902 }
3903 if (have_dict) {
3904 ADDOP_I(c, DICT_UPDATE, 1);
3905 }
3906 have_dict = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003907 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003908 if (!have_dict) {
3909 ADDOP_I(c, BUILD_MAP, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003910 }
3911 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003912}
3913
3914static int
3915compiler_compare(struct compiler *c, expr_ty e)
3916{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003917 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003918
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02003919 if (!check_compare(c, e)) {
3920 return 0;
3921 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003922 VISIT(c, expr, e->v.Compare.left);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003923 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
3924 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
3925 if (n == 0) {
3926 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Mark Shannon9af0e472020-01-14 10:12:45 +00003927 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003928 }
3929 else {
3930 basicblock *cleanup = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003931 if (cleanup == NULL)
3932 return 0;
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003933 for (i = 0; i < n; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003934 VISIT(c, expr,
3935 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003936 ADDOP(c, DUP_TOP);
3937 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00003938 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003939 ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003940 NEXT_BLOCK(c);
3941 }
3942 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00003943 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003944 basicblock *end = compiler_new_block(c);
3945 if (end == NULL)
3946 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00003947 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003948 compiler_use_next_block(c, cleanup);
3949 ADDOP(c, ROT_TWO);
3950 ADDOP(c, POP_TOP);
3951 compiler_use_next_block(c, end);
3952 }
3953 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003954}
3955
Serhiy Storchaka62e44812019-02-16 08:12:19 +02003956static PyTypeObject *
3957infer_type(expr_ty e)
3958{
3959 switch (e->kind) {
3960 case Tuple_kind:
3961 return &PyTuple_Type;
3962 case List_kind:
3963 case ListComp_kind:
3964 return &PyList_Type;
3965 case Dict_kind:
3966 case DictComp_kind:
3967 return &PyDict_Type;
3968 case Set_kind:
3969 case SetComp_kind:
3970 return &PySet_Type;
3971 case GeneratorExp_kind:
3972 return &PyGen_Type;
3973 case Lambda_kind:
3974 return &PyFunction_Type;
3975 case JoinedStr_kind:
3976 case FormattedValue_kind:
3977 return &PyUnicode_Type;
3978 case Constant_kind:
Victor Stinnera102ed72020-02-07 02:24:48 +01003979 return Py_TYPE(e->v.Constant.value);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02003980 default:
3981 return NULL;
3982 }
3983}
3984
3985static int
3986check_caller(struct compiler *c, expr_ty e)
3987{
3988 switch (e->kind) {
3989 case Constant_kind:
3990 case Tuple_kind:
3991 case List_kind:
3992 case ListComp_kind:
3993 case Dict_kind:
3994 case DictComp_kind:
3995 case Set_kind:
3996 case SetComp_kind:
3997 case GeneratorExp_kind:
3998 case JoinedStr_kind:
3999 case FormattedValue_kind:
4000 return compiler_warn(c, "'%.200s' object is not callable; "
4001 "perhaps you missed a comma?",
4002 infer_type(e)->tp_name);
4003 default:
4004 return 1;
4005 }
4006}
4007
4008static int
4009check_subscripter(struct compiler *c, expr_ty e)
4010{
4011 PyObject *v;
4012
4013 switch (e->kind) {
4014 case Constant_kind:
4015 v = e->v.Constant.value;
4016 if (!(v == Py_None || v == Py_Ellipsis ||
4017 PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
4018 PyAnySet_Check(v)))
4019 {
4020 return 1;
4021 }
4022 /* fall through */
4023 case Set_kind:
4024 case SetComp_kind:
4025 case GeneratorExp_kind:
4026 case Lambda_kind:
4027 return compiler_warn(c, "'%.200s' object is not subscriptable; "
4028 "perhaps you missed a comma?",
4029 infer_type(e)->tp_name);
4030 default:
4031 return 1;
4032 }
4033}
4034
4035static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004036check_index(struct compiler *c, expr_ty e, expr_ty s)
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004037{
4038 PyObject *v;
4039
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004040 PyTypeObject *index_type = infer_type(s);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004041 if (index_type == NULL
4042 || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
4043 || index_type == &PySlice_Type) {
4044 return 1;
4045 }
4046
4047 switch (e->kind) {
4048 case Constant_kind:
4049 v = e->v.Constant.value;
4050 if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
4051 return 1;
4052 }
4053 /* fall through */
4054 case Tuple_kind:
4055 case List_kind:
4056 case ListComp_kind:
4057 case JoinedStr_kind:
4058 case FormattedValue_kind:
4059 return compiler_warn(c, "%.200s indices must be integers or slices, "
4060 "not %.200s; "
4061 "perhaps you missed a comma?",
4062 infer_type(e)->tp_name,
4063 index_type->tp_name);
4064 default:
4065 return 1;
4066 }
4067}
4068
Zackery Spytz97f5de02019-03-22 01:30:32 -06004069// Return 1 if the method call was optimized, -1 if not, and 0 on error.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004070static int
Yury Selivanovf2392132016-12-13 19:03:51 -05004071maybe_optimize_method_call(struct compiler *c, expr_ty e)
4072{
4073 Py_ssize_t argsl, i;
4074 expr_ty meth = e->v.Call.func;
Pablo Galindoa5634c42020-09-16 19:42:00 +01004075 asdl_expr_seq *args = e->v.Call.args;
Yury Selivanovf2392132016-12-13 19:03:51 -05004076
4077 /* Check that the call node is an attribute access, and that
4078 the call doesn't have keyword parameters. */
4079 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
4080 asdl_seq_LEN(e->v.Call.keywords))
4081 return -1;
4082
4083 /* Check that there are no *varargs types of arguments. */
4084 argsl = asdl_seq_LEN(args);
4085 for (i = 0; i < argsl; i++) {
4086 expr_ty elt = asdl_seq_GET(args, i);
4087 if (elt->kind == Starred_kind) {
4088 return -1;
4089 }
4090 }
4091
4092 /* Alright, we can optimize the code. */
4093 VISIT(c, expr, meth->v.Attribute.value);
4094 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
4095 VISIT_SEQ(c, expr, e->v.Call.args);
4096 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
4097 return 1;
4098}
4099
4100static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004101validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
Zackery Spytz08050e92020-04-06 00:47:47 -06004102{
4103 Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
4104 for (Py_ssize_t i = 0; i < nkeywords; i++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004105 keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
4106 if (key->arg == NULL) {
4107 continue;
4108 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +01004109 if (forbidden_name(c, key->arg, Store)) {
4110 return -1;
4111 }
Zackery Spytz08050e92020-04-06 00:47:47 -06004112 for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004113 keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
4114 if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
4115 PyObject *msg = PyUnicode_FromFormat("keyword argument repeated: %U", key->arg);
4116 if (msg == NULL) {
4117 return -1;
4118 }
4119 c->u->u_col_offset = other->col_offset;
4120 compiler_error(c, PyUnicode_AsUTF8(msg));
4121 Py_DECREF(msg);
4122 return -1;
4123 }
4124 }
4125 }
4126 return 0;
4127}
4128
4129static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004130compiler_call(struct compiler *c, expr_ty e)
4131{
Zackery Spytz97f5de02019-03-22 01:30:32 -06004132 int ret = maybe_optimize_method_call(c, e);
4133 if (ret >= 0) {
4134 return ret;
4135 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004136 if (!check_caller(c, e->v.Call.func)) {
4137 return 0;
4138 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004139 VISIT(c, expr, e->v.Call.func);
4140 return compiler_call_helper(c, 0,
4141 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004142 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004143}
4144
Eric V. Smith235a6f02015-09-19 14:51:32 -04004145static int
4146compiler_joined_str(struct compiler *c, expr_ty e)
4147{
Eric V. Smith235a6f02015-09-19 14:51:32 -04004148 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004149 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1)
4150 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
Eric V. Smith235a6f02015-09-19 14:51:32 -04004151 return 1;
4152}
4153
Eric V. Smitha78c7952015-11-03 12:45:05 -05004154/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004155static int
4156compiler_formatted_value(struct compiler *c, expr_ty e)
4157{
Eric V. Smitha78c7952015-11-03 12:45:05 -05004158 /* Our oparg encodes 2 pieces of information: the conversion
4159 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04004160
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004161 Convert the conversion char to 3 bits:
4162 : 000 0x0 FVC_NONE The default if nothing specified.
Eric V. Smitha78c7952015-11-03 12:45:05 -05004163 !s : 001 0x1 FVC_STR
4164 !r : 010 0x2 FVC_REPR
4165 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04004166
Eric V. Smitha78c7952015-11-03 12:45:05 -05004167 next bit is whether or not we have a format spec:
4168 yes : 100 0x4
4169 no : 000 0x0
4170 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004171
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004172 int conversion = e->v.FormattedValue.conversion;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004173 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004174
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004175 /* The expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004176 VISIT(c, expr, e->v.FormattedValue.value);
4177
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004178 switch (conversion) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004179 case 's': oparg = FVC_STR; break;
4180 case 'r': oparg = FVC_REPR; break;
4181 case 'a': oparg = FVC_ASCII; break;
4182 case -1: oparg = FVC_NONE; break;
4183 default:
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004184 PyErr_Format(PyExc_SystemError,
4185 "Unrecognized conversion character %d", conversion);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004186 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004187 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004188 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004189 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004190 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004191 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004192 }
4193
Eric V. Smitha78c7952015-11-03 12:45:05 -05004194 /* And push our opcode and oparg */
4195 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004196
Eric V. Smith235a6f02015-09-19 14:51:32 -04004197 return 1;
4198}
4199
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004200static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004201compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004202{
4203 Py_ssize_t i, n = end - begin;
4204 keyword_ty kw;
4205 PyObject *keys, *key;
4206 assert(n > 0);
4207 if (n > 1) {
4208 for (i = begin; i < end; i++) {
4209 kw = asdl_seq_GET(keywords, i);
4210 VISIT(c, expr, kw->value);
4211 }
4212 keys = PyTuple_New(n);
4213 if (keys == NULL) {
4214 return 0;
4215 }
4216 for (i = begin; i < end; i++) {
4217 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
4218 Py_INCREF(key);
4219 PyTuple_SET_ITEM(keys, i - begin, key);
4220 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004221 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004222 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
4223 }
4224 else {
4225 /* a for loop only executes once */
4226 for (i = begin; i < end; i++) {
4227 kw = asdl_seq_GET(keywords, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004228 ADDOP_LOAD_CONST(c, kw->arg);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004229 VISIT(c, expr, kw->value);
4230 }
4231 ADDOP_I(c, BUILD_MAP, n);
4232 }
4233 return 1;
4234}
4235
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004236/* shared code between compiler_call and compiler_class */
4237static int
4238compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01004239 int n, /* Args already pushed */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004240 asdl_expr_seq *args,
4241 asdl_keyword_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004242{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004243 Py_ssize_t i, nseen, nelts, nkwelts;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004244
Pablo Galindo254ec782020-04-03 20:37:13 +01004245 if (validate_keywords(c, keywords) == -1) {
4246 return 0;
4247 }
4248
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004249 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004250 nkwelts = asdl_seq_LEN(keywords);
4251
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004252 for (i = 0; i < nelts; i++) {
4253 expr_ty elt = asdl_seq_GET(args, i);
4254 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00004255 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004256 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004257 }
4258 for (i = 0; i < nkwelts; i++) {
4259 keyword_ty kw = asdl_seq_GET(keywords, i);
4260 if (kw->arg == NULL) {
4261 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004262 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004263 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004264
Mark Shannon13bc1392020-01-23 09:25:17 +00004265 /* No * or ** args, so can use faster calling sequence */
4266 for (i = 0; i < nelts; i++) {
4267 expr_ty elt = asdl_seq_GET(args, i);
4268 assert(elt->kind != Starred_kind);
4269 VISIT(c, expr, elt);
4270 }
4271 if (nkwelts) {
4272 PyObject *names;
4273 VISIT_SEQ(c, keyword, keywords);
4274 names = PyTuple_New(nkwelts);
4275 if (names == NULL) {
4276 return 0;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004277 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004278 for (i = 0; i < nkwelts; i++) {
4279 keyword_ty kw = asdl_seq_GET(keywords, i);
4280 Py_INCREF(kw->arg);
4281 PyTuple_SET_ITEM(names, i, kw->arg);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004282 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004283 ADDOP_LOAD_CONST_NEW(c, names);
4284 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
4285 return 1;
4286 }
4287 else {
4288 ADDOP_I(c, CALL_FUNCTION, n + nelts);
4289 return 1;
4290 }
4291
4292ex_call:
4293
4294 /* Do positional arguments. */
4295 if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
4296 VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
4297 }
4298 else if (starunpack_helper(c, args, n, BUILD_LIST,
4299 LIST_APPEND, LIST_EXTEND, 1) == 0) {
4300 return 0;
4301 }
4302 /* Then keyword arguments */
4303 if (nkwelts) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004304 /* Has a new dict been pushed */
4305 int have_dict = 0;
Mark Shannon13bc1392020-01-23 09:25:17 +00004306
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004307 nseen = 0; /* the number of keyword arguments on the stack following */
4308 for (i = 0; i < nkwelts; i++) {
4309 keyword_ty kw = asdl_seq_GET(keywords, i);
4310 if (kw->arg == NULL) {
4311 /* A keyword argument unpacking. */
4312 if (nseen) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004313 if (!compiler_subkwargs(c, keywords, i - nseen, i)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004314 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004315 }
Mark Shannondb64f122020-06-01 10:42:42 +01004316 if (have_dict) {
4317 ADDOP_I(c, DICT_MERGE, 1);
4318 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004319 have_dict = 1;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004320 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004321 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004322 if (!have_dict) {
4323 ADDOP_I(c, BUILD_MAP, 0);
4324 have_dict = 1;
4325 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004326 VISIT(c, expr, kw->value);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004327 ADDOP_I(c, DICT_MERGE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004328 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004329 else {
4330 nseen++;
4331 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004332 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004333 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004334 /* Pack up any trailing keyword arguments. */
Mark Shannon8a4cd702020-01-27 09:57:45 +00004335 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004336 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004337 }
4338 if (have_dict) {
4339 ADDOP_I(c, DICT_MERGE, 1);
4340 }
4341 have_dict = 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004342 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004343 assert(have_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004344 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004345 ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0);
4346 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004347}
4348
Nick Coghlan650f0d02007-04-15 12:05:43 +00004349
4350/* List and set comprehensions and generator expressions work by creating a
4351 nested function to perform the actual iteration. This means that the
4352 iteration variables don't leak into the current scope.
4353 The defined function is called immediately following its definition, with the
4354 result of that call being the result of the expression.
4355 The LC/SC version returns the populated container, while the GE version is
4356 flagged in symtable.c as a generator, so it returns the generator object
4357 when the function is called.
Nick Coghlan650f0d02007-04-15 12:05:43 +00004358
4359 Possible cleanups:
4360 - iterate over the generator sequence instead of using recursion
4361*/
4362
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004363
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004364static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004365compiler_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004366 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004367 int depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004368 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004369{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004370 comprehension_ty gen;
4371 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4372 if (gen->is_async) {
4373 return compiler_async_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004374 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004375 } else {
4376 return compiler_sync_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004377 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004378 }
4379}
4380
4381static int
4382compiler_sync_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004383 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004384 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004385 expr_ty elt, expr_ty val, int type)
4386{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004387 /* generate code for the iterator, then each of the ifs,
4388 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004390 comprehension_ty gen;
4391 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004392 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004394 start = compiler_new_block(c);
4395 skip = compiler_new_block(c);
4396 if_cleanup = compiler_new_block(c);
4397 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004399 if (start == NULL || skip == NULL || if_cleanup == NULL ||
4400 anchor == NULL)
4401 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004403 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004405 if (gen_index == 0) {
4406 /* Receive outermost iter as an implicit argument */
4407 c->u->u_argcount = 1;
4408 ADDOP_I(c, LOAD_FAST, 0);
4409 }
4410 else {
4411 /* Sub-iter - calculate on the fly */
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004412 /* Fast path for the temporary variable assignment idiom:
4413 for y in [f(x)]
4414 */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004415 asdl_expr_seq *elts;
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004416 switch (gen->iter->kind) {
4417 case List_kind:
4418 elts = gen->iter->v.List.elts;
4419 break;
4420 case Tuple_kind:
4421 elts = gen->iter->v.Tuple.elts;
4422 break;
4423 default:
4424 elts = NULL;
4425 }
4426 if (asdl_seq_LEN(elts) == 1) {
4427 expr_ty elt = asdl_seq_GET(elts, 0);
4428 if (elt->kind != Starred_kind) {
4429 VISIT(c, expr, elt);
4430 start = NULL;
4431 }
4432 }
4433 if (start) {
4434 VISIT(c, expr, gen->iter);
4435 ADDOP(c, GET_ITER);
4436 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004437 }
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004438 if (start) {
4439 depth++;
4440 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01004441 ADDOP_JUMP(c, FOR_ITER, anchor);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004442 NEXT_BLOCK(c);
4443 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004444 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004446 /* XXX this needs to be cleaned up...a lot! */
4447 n = asdl_seq_LEN(gen->ifs);
4448 for (i = 0; i < n; i++) {
4449 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004450 if (!compiler_jump_if(c, e, if_cleanup, 0))
4451 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 NEXT_BLOCK(c);
4453 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004455 if (++gen_index < asdl_seq_LEN(generators))
4456 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004457 generators, gen_index, depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004458 elt, val, type))
4459 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004461 /* only append after the last for generator */
4462 if (gen_index >= asdl_seq_LEN(generators)) {
4463 /* comprehension specific code */
4464 switch (type) {
4465 case COMP_GENEXP:
4466 VISIT(c, expr, elt);
4467 ADDOP(c, YIELD_VALUE);
4468 ADDOP(c, POP_TOP);
4469 break;
4470 case COMP_LISTCOMP:
4471 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004472 ADDOP_I(c, LIST_APPEND, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004473 break;
4474 case COMP_SETCOMP:
4475 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004476 ADDOP_I(c, SET_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004477 break;
4478 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004479 /* With '{k: v}', k is evaluated before v, so we do
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004480 the same. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004481 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004482 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004483 ADDOP_I(c, MAP_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004484 break;
4485 default:
4486 return 0;
4487 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 compiler_use_next_block(c, skip);
4490 }
4491 compiler_use_next_block(c, if_cleanup);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004492 if (start) {
Mark Shannon582aaf12020-08-04 17:30:11 +01004493 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004494 compiler_use_next_block(c, anchor);
4495 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004496
4497 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004498}
4499
4500static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004501compiler_async_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004502 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004503 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004504 expr_ty elt, expr_ty val, int type)
4505{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004506 comprehension_ty gen;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004507 basicblock *start, *if_cleanup, *except;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004508 Py_ssize_t i, n;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004509 start = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004510 except = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004511 if_cleanup = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004512
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004513 if (start == NULL || if_cleanup == NULL || except == NULL) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004514 return 0;
4515 }
4516
4517 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4518
4519 if (gen_index == 0) {
4520 /* Receive outermost iter as an implicit argument */
4521 c->u->u_argcount = 1;
4522 ADDOP_I(c, LOAD_FAST, 0);
4523 }
4524 else {
4525 /* Sub-iter - calculate on the fly */
4526 VISIT(c, expr, gen->iter);
4527 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004528 }
4529
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004530 compiler_use_next_block(c, start);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004531
Mark Shannon582aaf12020-08-04 17:30:11 +01004532 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004533 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004534 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004535 ADDOP(c, YIELD_FROM);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004536 ADDOP(c, POP_BLOCK);
Serhiy Storchaka24d32012018-03-10 18:22:34 +02004537 VISIT(c, expr, gen->target);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004538
4539 n = asdl_seq_LEN(gen->ifs);
4540 for (i = 0; i < n; i++) {
4541 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004542 if (!compiler_jump_if(c, e, if_cleanup, 0))
4543 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004544 NEXT_BLOCK(c);
4545 }
4546
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004547 depth++;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004548 if (++gen_index < asdl_seq_LEN(generators))
4549 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004550 generators, gen_index, depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004551 elt, val, type))
4552 return 0;
4553
4554 /* only append after the last for generator */
4555 if (gen_index >= asdl_seq_LEN(generators)) {
4556 /* comprehension specific code */
4557 switch (type) {
4558 case COMP_GENEXP:
4559 VISIT(c, expr, elt);
4560 ADDOP(c, YIELD_VALUE);
4561 ADDOP(c, POP_TOP);
4562 break;
4563 case COMP_LISTCOMP:
4564 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004565 ADDOP_I(c, LIST_APPEND, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004566 break;
4567 case COMP_SETCOMP:
4568 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004569 ADDOP_I(c, SET_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004570 break;
4571 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004572 /* With '{k: v}', k is evaluated before v, so we do
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004573 the same. */
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004574 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004575 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004576 ADDOP_I(c, MAP_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004577 break;
4578 default:
4579 return 0;
4580 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004581 }
4582 compiler_use_next_block(c, if_cleanup);
Mark Shannon582aaf12020-08-04 17:30:11 +01004583 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004584
4585 compiler_use_next_block(c, except);
4586 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004587
4588 return 1;
4589}
4590
4591static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004592compiler_comprehension(struct compiler *c, expr_ty e, int type,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004593 identifier name, asdl_comprehension_seq *generators, expr_ty elt,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004594 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004595{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004596 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004597 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004598 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004599 int is_async_generator = 0;
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004600 int top_level_await = IS_TOP_LEVEL_AWAIT(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004601
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004602
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004603 int is_async_function = c->u->u_ste->ste_coroutine;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004604
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004605 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004606 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4607 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004608 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004609 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004610 }
4611
4612 is_async_generator = c->u->u_ste->ste_coroutine;
4613
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004614 if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004615 compiler_error(c, "asynchronous comprehension outside of "
4616 "an asynchronous function");
4617 goto error_in_scope;
4618 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004620 if (type != COMP_GENEXP) {
4621 int op;
4622 switch (type) {
4623 case COMP_LISTCOMP:
4624 op = BUILD_LIST;
4625 break;
4626 case COMP_SETCOMP:
4627 op = BUILD_SET;
4628 break;
4629 case COMP_DICTCOMP:
4630 op = BUILD_MAP;
4631 break;
4632 default:
4633 PyErr_Format(PyExc_SystemError,
4634 "unknown comprehension type %d", type);
4635 goto error_in_scope;
4636 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004638 ADDOP_I(c, op, 0);
4639 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004640
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004641 if (!compiler_comprehension_generator(c, generators, 0, 0, elt,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004642 val, type))
4643 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004645 if (type != COMP_GENEXP) {
4646 ADDOP(c, RETURN_VALUE);
4647 }
4648
4649 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004650 qualname = c->u->u_qualname;
4651 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004652 compiler_exit_scope(c);
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004653 if (top_level_await && is_async_generator){
4654 c->u->u_ste->ste_coroutine = 1;
4655 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004656 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004657 goto error;
4658
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004659 if (!compiler_make_closure(c, co, 0, qualname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004660 goto error;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004661 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004662 Py_DECREF(co);
4663
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004664 VISIT(c, expr, outermost->iter);
4665
4666 if (outermost->is_async) {
4667 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004668 } else {
4669 ADDOP(c, GET_ITER);
4670 }
4671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004672 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004673
4674 if (is_async_generator && type != COMP_GENEXP) {
4675 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004676 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004677 ADDOP(c, YIELD_FROM);
4678 }
4679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004680 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004681error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004682 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004683error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004684 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004685 Py_XDECREF(co);
4686 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004687}
4688
4689static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004690compiler_genexp(struct compiler *c, expr_ty e)
4691{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004692 static identifier name;
4693 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004694 name = PyUnicode_InternFromString("<genexpr>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004695 if (!name)
4696 return 0;
4697 }
4698 assert(e->kind == GeneratorExp_kind);
4699 return compiler_comprehension(c, e, COMP_GENEXP, name,
4700 e->v.GeneratorExp.generators,
4701 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004702}
4703
4704static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004705compiler_listcomp(struct compiler *c, expr_ty e)
4706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004707 static identifier name;
4708 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004709 name = PyUnicode_InternFromString("<listcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004710 if (!name)
4711 return 0;
4712 }
4713 assert(e->kind == ListComp_kind);
4714 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4715 e->v.ListComp.generators,
4716 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004717}
4718
4719static int
4720compiler_setcomp(struct compiler *c, expr_ty e)
4721{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004722 static identifier name;
4723 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004724 name = PyUnicode_InternFromString("<setcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004725 if (!name)
4726 return 0;
4727 }
4728 assert(e->kind == SetComp_kind);
4729 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4730 e->v.SetComp.generators,
4731 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004732}
4733
4734
4735static int
4736compiler_dictcomp(struct compiler *c, expr_ty e)
4737{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004738 static identifier name;
4739 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004740 name = PyUnicode_InternFromString("<dictcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004741 if (!name)
4742 return 0;
4743 }
4744 assert(e->kind == DictComp_kind);
4745 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4746 e->v.DictComp.generators,
4747 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004748}
4749
4750
4751static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004752compiler_visit_keyword(struct compiler *c, keyword_ty k)
4753{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004754 VISIT(c, expr, k->value);
4755 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004756}
4757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004758/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004759 whether they are true or false.
4760
4761 Return values: 1 for true, 0 for false, -1 for non-constant.
4762 */
4763
4764static int
Mark Shannonfee55262019-11-21 09:11:43 +00004765compiler_with_except_finish(struct compiler *c) {
4766 basicblock *exit;
4767 exit = compiler_new_block(c);
4768 if (exit == NULL)
4769 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01004770 ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit);
Mark Shannon266b4622020-11-17 19:30:14 +00004771 NEXT_BLOCK(c);
Mark Shannonbf353f32020-12-17 13:55:28 +00004772 ADDOP_I(c, RERAISE, 1);
Mark Shannonfee55262019-11-21 09:11:43 +00004773 compiler_use_next_block(c, exit);
4774 ADDOP(c, POP_TOP);
4775 ADDOP(c, POP_TOP);
4776 ADDOP(c, POP_TOP);
4777 ADDOP(c, POP_EXCEPT);
4778 ADDOP(c, POP_TOP);
4779 return 1;
4780}
Yury Selivanov75445082015-05-11 22:57:16 -04004781
4782/*
4783 Implements the async with statement.
4784
4785 The semantics outlined in that PEP are as follows:
4786
4787 async with EXPR as VAR:
4788 BLOCK
4789
4790 It is implemented roughly as:
4791
4792 context = EXPR
4793 exit = context.__aexit__ # not calling it
4794 value = await context.__aenter__()
4795 try:
4796 VAR = value # if VAR present in the syntax
4797 BLOCK
4798 finally:
4799 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004800 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04004801 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004802 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04004803 if not (await exit(*exc)):
4804 raise
4805 */
4806static int
4807compiler_async_with(struct compiler *c, stmt_ty s, int pos)
4808{
Mark Shannonfee55262019-11-21 09:11:43 +00004809 basicblock *block, *final, *exit;
Yury Selivanov75445082015-05-11 22:57:16 -04004810 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
4811
4812 assert(s->kind == AsyncWith_kind);
Pablo Galindo90235812020-03-15 04:29:22 +00004813 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07004814 c->u->u_ste->ste_coroutine = 1;
4815 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
Zsolt Dollensteine2396502018-04-27 08:58:56 -07004816 return compiler_error(c, "'async with' outside async function");
4817 }
Yury Selivanov75445082015-05-11 22:57:16 -04004818
4819 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004820 final = compiler_new_block(c);
4821 exit = compiler_new_block(c);
4822 if (!block || !final || !exit)
Yury Selivanov75445082015-05-11 22:57:16 -04004823 return 0;
4824
4825 /* Evaluate EXPR */
4826 VISIT(c, expr, item->context_expr);
4827
4828 ADDOP(c, BEFORE_ASYNC_WITH);
4829 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004830 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004831 ADDOP(c, YIELD_FROM);
4832
Mark Shannon582aaf12020-08-04 17:30:11 +01004833 ADDOP_JUMP(c, SETUP_ASYNC_WITH, final);
Yury Selivanov75445082015-05-11 22:57:16 -04004834
4835 /* SETUP_ASYNC_WITH pushes a finally block. */
4836 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004837 if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004838 return 0;
4839 }
4840
4841 if (item->optional_vars) {
4842 VISIT(c, expr, item->optional_vars);
4843 }
4844 else {
4845 /* Discard result from context.__aenter__() */
4846 ADDOP(c, POP_TOP);
4847 }
4848
4849 pos++;
4850 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
4851 /* BLOCK code */
4852 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
4853 else if (!compiler_async_with(c, s, pos))
4854 return 0;
4855
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004856 compiler_pop_fblock(c, ASYNC_WITH, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004857 ADDOP(c, POP_BLOCK);
4858 /* End of body; start the cleanup */
Yury Selivanov75445082015-05-11 22:57:16 -04004859
Mark Shannonfee55262019-11-21 09:11:43 +00004860 /* For successful outcome:
4861 * call __exit__(None, None, None)
4862 */
4863 if(!compiler_call_exit_with_nones(c))
Yury Selivanov75445082015-05-11 22:57:16 -04004864 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00004865 ADDOP(c, GET_AWAITABLE);
4866 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4867 ADDOP(c, YIELD_FROM);
Yury Selivanov75445082015-05-11 22:57:16 -04004868
Mark Shannonfee55262019-11-21 09:11:43 +00004869 ADDOP(c, POP_TOP);
Yury Selivanov75445082015-05-11 22:57:16 -04004870
Mark Shannon582aaf12020-08-04 17:30:11 +01004871 ADDOP_JUMP(c, JUMP_ABSOLUTE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00004872
4873 /* For exceptional outcome: */
4874 compiler_use_next_block(c, final);
4875
4876 ADDOP(c, WITH_EXCEPT_START);
Yury Selivanov75445082015-05-11 22:57:16 -04004877 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004878 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004879 ADDOP(c, YIELD_FROM);
Mark Shannonfee55262019-11-21 09:11:43 +00004880 compiler_with_except_finish(c);
Yury Selivanov75445082015-05-11 22:57:16 -04004881
Mark Shannonfee55262019-11-21 09:11:43 +00004882compiler_use_next_block(c, exit);
Yury Selivanov75445082015-05-11 22:57:16 -04004883 return 1;
4884}
4885
4886
Guido van Rossumc2e20742006-02-27 22:32:47 +00004887/*
4888 Implements the with statement from PEP 343.
Guido van Rossumc2e20742006-02-27 22:32:47 +00004889 with EXPR as VAR:
4890 BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00004891 is implemented as:
4892 <code for EXPR>
4893 SETUP_WITH E
4894 <code to store to VAR> or POP_TOP
4895 <code for BLOCK>
4896 LOAD_CONST (None, None, None)
4897 CALL_FUNCTION_EX 0
4898 JUMP_FORWARD EXIT
4899 E: WITH_EXCEPT_START (calls EXPR.__exit__)
4900 POP_JUMP_IF_TRUE T:
4901 RERAISE
4902 T: POP_TOP * 3 (remove exception from stack)
4903 POP_EXCEPT
4904 POP_TOP
4905 EXIT:
Guido van Rossumc2e20742006-02-27 22:32:47 +00004906 */
Mark Shannonfee55262019-11-21 09:11:43 +00004907
Guido van Rossumc2e20742006-02-27 22:32:47 +00004908static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004909compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004910{
Mark Shannonfee55262019-11-21 09:11:43 +00004911 basicblock *block, *final, *exit;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004912 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004913
4914 assert(s->kind == With_kind);
4915
Guido van Rossumc2e20742006-02-27 22:32:47 +00004916 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004917 final = compiler_new_block(c);
4918 exit = compiler_new_block(c);
4919 if (!block || !final || !exit)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004920 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004921
Thomas Wouters477c8d52006-05-27 19:21:47 +00004922 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004923 VISIT(c, expr, item->context_expr);
Mark Shannonfee55262019-11-21 09:11:43 +00004924 /* Will push bound __exit__ */
Mark Shannon582aaf12020-08-04 17:30:11 +01004925 ADDOP_JUMP(c, SETUP_WITH, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004926
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004927 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00004928 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004929 if (!compiler_push_fblock(c, WITH, block, final, NULL)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004930 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004931 }
4932
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004933 if (item->optional_vars) {
4934 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004935 }
4936 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004937 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004938 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004939 }
4940
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004941 pos++;
4942 if (pos == asdl_seq_LEN(s->v.With.items))
4943 /* BLOCK code */
4944 VISIT_SEQ(c, stmt, s->v.With.body)
4945 else if (!compiler_with(c, s, pos))
4946 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004947
Mark Shannon3bd60352021-01-13 12:05:43 +00004948
4949 /* Mark all following code as artificial */
4950 c->u->u_lineno = -1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004951 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004952 compiler_pop_fblock(c, WITH, block);
Mark Shannon13bc1392020-01-23 09:25:17 +00004953
Mark Shannonfee55262019-11-21 09:11:43 +00004954 /* End of body; start the cleanup. */
Mark Shannon13bc1392020-01-23 09:25:17 +00004955
Mark Shannonfee55262019-11-21 09:11:43 +00004956 /* For successful outcome:
4957 * call __exit__(None, None, None)
4958 */
4959 if (!compiler_call_exit_with_nones(c))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004960 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00004961 ADDOP(c, POP_TOP);
Mark Shannon582aaf12020-08-04 17:30:11 +01004962 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004963
Mark Shannonfee55262019-11-21 09:11:43 +00004964 /* For exceptional outcome: */
4965 compiler_use_next_block(c, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004966
Mark Shannonfee55262019-11-21 09:11:43 +00004967 ADDOP(c, WITH_EXCEPT_START);
4968 compiler_with_except_finish(c);
4969
4970 compiler_use_next_block(c, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004971 return 1;
4972}
4973
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004974static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03004975compiler_visit_expr1(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004976{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004977 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07004978 case NamedExpr_kind:
4979 VISIT(c, expr, e->v.NamedExpr.value);
4980 ADDOP(c, DUP_TOP);
4981 VISIT(c, expr, e->v.NamedExpr.target);
4982 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004983 case BoolOp_kind:
4984 return compiler_boolop(c, e);
4985 case BinOp_kind:
4986 VISIT(c, expr, e->v.BinOp.left);
4987 VISIT(c, expr, e->v.BinOp.right);
Andy Lester76d58772020-03-10 21:18:12 -05004988 ADDOP(c, binop(e->v.BinOp.op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004989 break;
4990 case UnaryOp_kind:
4991 VISIT(c, expr, e->v.UnaryOp.operand);
4992 ADDOP(c, unaryop(e->v.UnaryOp.op));
4993 break;
4994 case Lambda_kind:
4995 return compiler_lambda(c, e);
4996 case IfExp_kind:
4997 return compiler_ifexp(c, e);
4998 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004999 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005000 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005001 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005002 case GeneratorExp_kind:
5003 return compiler_genexp(c, e);
5004 case ListComp_kind:
5005 return compiler_listcomp(c, e);
5006 case SetComp_kind:
5007 return compiler_setcomp(c, e);
5008 case DictComp_kind:
5009 return compiler_dictcomp(c, e);
5010 case Yield_kind:
5011 if (c->u->u_ste->ste_type != FunctionBlock)
5012 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005013 if (e->v.Yield.value) {
5014 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005015 }
5016 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005017 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005018 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005019 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005020 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005021 case YieldFrom_kind:
5022 if (c->u->u_ste->ste_type != FunctionBlock)
5023 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04005024
5025 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
5026 return compiler_error(c, "'yield from' inside async function");
5027
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005028 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04005029 ADDOP(c, GET_YIELD_FROM_ITER);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005030 ADDOP_LOAD_CONST(c, Py_None);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005031 ADDOP(c, YIELD_FROM);
5032 break;
Yury Selivanov75445082015-05-11 22:57:16 -04005033 case Await_kind:
Pablo Galindo90235812020-03-15 04:29:22 +00005034 if (!IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005035 if (c->u->u_ste->ste_type != FunctionBlock){
5036 return compiler_error(c, "'await' outside function");
5037 }
Yury Selivanov75445082015-05-11 22:57:16 -04005038
Victor Stinner331a6a52019-05-27 16:39:22 +02005039 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005040 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
5041 return compiler_error(c, "'await' outside async function");
5042 }
5043 }
Yury Selivanov75445082015-05-11 22:57:16 -04005044
5045 VISIT(c, expr, e->v.Await.value);
5046 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005047 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005048 ADDOP(c, YIELD_FROM);
5049 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005050 case Compare_kind:
5051 return compiler_compare(c, e);
5052 case Call_kind:
5053 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005054 case Constant_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005055 ADDOP_LOAD_CONST(c, e->v.Constant.value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005056 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005057 case JoinedStr_kind:
5058 return compiler_joined_str(c, e);
5059 case FormattedValue_kind:
5060 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005061 /* The following exprs can be assignment targets. */
5062 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005063 VISIT(c, expr, e->v.Attribute.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005064 switch (e->v.Attribute.ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005065 case Load:
5066 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
5067 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005068 case Store:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005069 if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx))
5070 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005071 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5072 break;
5073 case Del:
5074 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
5075 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005076 }
5077 break;
5078 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005079 return compiler_subscript(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005080 case Starred_kind:
5081 switch (e->v.Starred.ctx) {
5082 case Store:
5083 /* In all legitimate cases, the Starred node was already replaced
5084 * by compiler_list/compiler_tuple. XXX: is that okay? */
5085 return compiler_error(c,
5086 "starred assignment target must be in a list or tuple");
5087 default:
5088 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005089 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005090 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005091 break;
5092 case Slice_kind:
5093 return compiler_slice(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005094 case Name_kind:
5095 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
5096 /* child nodes of List and Tuple will have expr_context set */
5097 case List_kind:
5098 return compiler_list(c, e);
5099 case Tuple_kind:
5100 return compiler_tuple(c, e);
5101 }
5102 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005103}
5104
5105static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005106compiler_visit_expr(struct compiler *c, expr_ty e)
5107{
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005108 int old_lineno = c->u->u_lineno;
5109 int old_col_offset = c->u->u_col_offset;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005110 SET_LOC(c, e);
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005111 int res = compiler_visit_expr1(c, e);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005112 c->u->u_lineno = old_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005113 c->u->u_col_offset = old_col_offset;
5114 return res;
5115}
5116
5117static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005118compiler_augassign(struct compiler *c, stmt_ty s)
5119{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005120 assert(s->kind == AugAssign_kind);
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005121 expr_ty e = s->v.AugAssign.target;
5122
5123 int old_lineno = c->u->u_lineno;
5124 int old_col_offset = c->u->u_col_offset;
5125 SET_LOC(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005127 switch (e->kind) {
5128 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005129 VISIT(c, expr, e->v.Attribute.value);
5130 ADDOP(c, DUP_TOP);
5131 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005132 break;
5133 case Subscript_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005134 VISIT(c, expr, e->v.Subscript.value);
5135 VISIT(c, expr, e->v.Subscript.slice);
5136 ADDOP(c, DUP_TOP_TWO);
5137 ADDOP(c, BINARY_SUBSCR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005138 break;
5139 case Name_kind:
5140 if (!compiler_nameop(c, e->v.Name.id, Load))
5141 return 0;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005142 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005143 default:
5144 PyErr_Format(PyExc_SystemError,
5145 "invalid node type (%d) for augmented assignment",
5146 e->kind);
5147 return 0;
5148 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005149
5150 c->u->u_lineno = old_lineno;
5151 c->u->u_col_offset = old_col_offset;
5152
5153 VISIT(c, expr, s->v.AugAssign.value);
5154 ADDOP(c, inplace_binop(s->v.AugAssign.op));
5155
5156 SET_LOC(c, e);
5157
5158 switch (e->kind) {
5159 case Attribute_kind:
5160 ADDOP(c, ROT_TWO);
5161 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5162 break;
5163 case Subscript_kind:
5164 ADDOP(c, ROT_THREE);
5165 ADDOP(c, STORE_SUBSCR);
5166 break;
5167 case Name_kind:
5168 return compiler_nameop(c, e->v.Name.id, Store);
5169 default:
5170 Py_UNREACHABLE();
5171 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005172 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005173}
5174
5175static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005176check_ann_expr(struct compiler *c, expr_ty e)
5177{
5178 VISIT(c, expr, e);
5179 ADDOP(c, POP_TOP);
5180 return 1;
5181}
5182
5183static int
5184check_annotation(struct compiler *c, stmt_ty s)
5185{
5186 /* Annotations are only evaluated in a module or class. */
5187 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5188 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
5189 return check_ann_expr(c, s->v.AnnAssign.annotation);
5190 }
5191 return 1;
5192}
5193
5194static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005195check_ann_subscr(struct compiler *c, expr_ty e)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005196{
5197 /* We check that everything in a subscript is defined at runtime. */
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005198 switch (e->kind) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005199 case Slice_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005200 if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005201 return 0;
5202 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005203 if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) {
5204 return 0;
5205 }
5206 if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) {
5207 return 0;
5208 }
5209 return 1;
5210 case Tuple_kind: {
5211 /* extended slice */
Pablo Galindoa5634c42020-09-16 19:42:00 +01005212 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005213 Py_ssize_t i, n = asdl_seq_LEN(elts);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005214 for (i = 0; i < n; i++) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005215 if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005216 return 0;
5217 }
5218 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005219 return 1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005220 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005221 default:
5222 return check_ann_expr(c, e);
5223 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005224}
5225
5226static int
5227compiler_annassign(struct compiler *c, stmt_ty s)
5228{
5229 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07005230 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005231
5232 assert(s->kind == AnnAssign_kind);
5233
5234 /* We perform the actual assignment first. */
5235 if (s->v.AnnAssign.value) {
5236 VISIT(c, expr, s->v.AnnAssign.value);
5237 VISIT(c, expr, targ);
5238 }
5239 switch (targ->kind) {
5240 case Name_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005241 if (forbidden_name(c, targ->v.Name.id, Store))
5242 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005243 /* If we have a simple name in a module or class, store annotation. */
5244 if (s->v.AnnAssign.simple &&
5245 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5246 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Batuhan Taskaya044a1042020-10-06 23:03:02 +03005247 VISIT(c, annexpr, s->v.AnnAssign.annotation);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005248 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02005249 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005250 ADDOP_LOAD_CONST_NEW(c, mangled);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005251 ADDOP(c, STORE_SUBSCR);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005252 }
5253 break;
5254 case Attribute_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005255 if (forbidden_name(c, targ->v.Attribute.attr, Store))
5256 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005257 if (!s->v.AnnAssign.value &&
5258 !check_ann_expr(c, targ->v.Attribute.value)) {
5259 return 0;
5260 }
5261 break;
5262 case Subscript_kind:
5263 if (!s->v.AnnAssign.value &&
5264 (!check_ann_expr(c, targ->v.Subscript.value) ||
5265 !check_ann_subscr(c, targ->v.Subscript.slice))) {
5266 return 0;
5267 }
5268 break;
5269 default:
5270 PyErr_Format(PyExc_SystemError,
5271 "invalid node type (%d) for annotated assignment",
5272 targ->kind);
5273 return 0;
5274 }
5275 /* Annotation is evaluated last. */
5276 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
5277 return 0;
5278 }
5279 return 1;
5280}
5281
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005282/* Raises a SyntaxError and returns 0.
5283 If something goes wrong, a different exception may be raised.
5284*/
5285
5286static int
5287compiler_error(struct compiler *c, const char *errstr)
5288{
Benjamin Peterson43b06862011-05-27 09:08:01 -05005289 PyObject *loc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005290 PyObject *u = NULL, *v = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005291
Victor Stinner14e461d2013-08-26 22:28:21 +02005292 loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005293 if (!loc) {
5294 Py_INCREF(Py_None);
5295 loc = Py_None;
5296 }
Victor Stinner14e461d2013-08-26 22:28:21 +02005297 u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04005298 c->u->u_col_offset + 1, loc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005299 if (!u)
5300 goto exit;
5301 v = Py_BuildValue("(zO)", errstr, u);
5302 if (!v)
5303 goto exit;
5304 PyErr_SetObject(PyExc_SyntaxError, v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005305 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005306 Py_DECREF(loc);
5307 Py_XDECREF(u);
5308 Py_XDECREF(v);
5309 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005310}
5311
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005312/* Emits a SyntaxWarning and returns 1 on success.
5313 If a SyntaxWarning raised as error, replaces it with a SyntaxError
5314 and returns 0.
5315*/
5316static int
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005317compiler_warn(struct compiler *c, const char *format, ...)
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005318{
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005319 va_list vargs;
5320#ifdef HAVE_STDARG_PROTOTYPES
5321 va_start(vargs, format);
5322#else
5323 va_start(vargs);
5324#endif
5325 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5326 va_end(vargs);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005327 if (msg == NULL) {
5328 return 0;
5329 }
5330 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
5331 c->u->u_lineno, NULL, NULL) < 0)
5332 {
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005333 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005334 /* Replace the SyntaxWarning exception with a SyntaxError
5335 to get a more accurate error report */
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005336 PyErr_Clear();
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005337 assert(PyUnicode_AsUTF8(msg) != NULL);
5338 compiler_error(c, PyUnicode_AsUTF8(msg));
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005339 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005340 Py_DECREF(msg);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005341 return 0;
5342 }
5343 Py_DECREF(msg);
5344 return 1;
5345}
5346
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005347static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005348compiler_subscript(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005349{
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005350 expr_context_ty ctx = e->v.Subscript.ctx;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005351 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005352
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005353 if (ctx == Load) {
5354 if (!check_subscripter(c, e->v.Subscript.value)) {
5355 return 0;
5356 }
5357 if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
5358 return 0;
5359 }
5360 }
5361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005362 switch (ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005363 case Load: op = BINARY_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005364 case Store: op = STORE_SUBSCR; break;
5365 case Del: op = DELETE_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005366 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005367 assert(op);
5368 VISIT(c, expr, e->v.Subscript.value);
5369 VISIT(c, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005370 ADDOP(c, op);
5371 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005372}
5373
5374static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005375compiler_slice(struct compiler *c, expr_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005377 int n = 2;
5378 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005380 /* only handles the cases where BUILD_SLICE is emitted */
5381 if (s->v.Slice.lower) {
5382 VISIT(c, expr, s->v.Slice.lower);
5383 }
5384 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005385 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005386 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005388 if (s->v.Slice.upper) {
5389 VISIT(c, expr, s->v.Slice.upper);
5390 }
5391 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005392 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005393 }
5394
5395 if (s->v.Slice.step) {
5396 n++;
5397 VISIT(c, expr, s->v.Slice.step);
5398 }
5399 ADDOP_I(c, BUILD_SLICE, n);
5400 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005401}
5402
Thomas Wouters89f507f2006-12-13 04:49:30 +00005403/* End of the compiler section, beginning of the assembler section */
5404
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005405/* do depth-first search of basic block graph, starting with block.
T. Wouters99b54d62019-09-12 07:05:33 -07005406 post records the block indices in post-order.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005407
5408 XXX must handle implicit jumps from one block to next
5409*/
5410
Thomas Wouters89f507f2006-12-13 04:49:30 +00005411struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005412 PyObject *a_bytecode; /* string containing bytecode */
5413 int a_offset; /* offset into bytecode */
5414 int a_nblocks; /* number of reachable blocks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005415 PyObject *a_lnotab; /* string containing lnotab */
5416 int a_lnotab_off; /* offset into lnotab */
Mark Shannon877df852020-11-12 09:43:29 +00005417 int a_prevlineno; /* lineno of last emitted line in line table */
5418 int a_lineno; /* lineno of last emitted instruction */
5419 int a_lineno_start; /* bytecode start offset of current lineno */
Mark Shannoncc75ab72020-11-12 19:49:33 +00005420 basicblock *a_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005421};
5422
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005423Py_LOCAL_INLINE(void)
5424stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005425{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005426 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
Mark Shannonfee55262019-11-21 09:11:43 +00005427 if (b->b_startdepth < depth && b->b_startdepth < 100) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005428 assert(b->b_startdepth < 0);
5429 b->b_startdepth = depth;
5430 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02005431 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005432}
5433
5434/* Find the flow path that needs the largest stack. We assume that
5435 * cycles in the flow graph have no net effect on the stack depth.
5436 */
5437static int
5438stackdepth(struct compiler *c)
5439{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005440 basicblock *b, *entryblock = NULL;
5441 basicblock **stack, **sp;
5442 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005443 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005444 b->b_startdepth = INT_MIN;
5445 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005446 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005447 }
5448 if (!entryblock)
5449 return 0;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005450 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
5451 if (!stack) {
5452 PyErr_NoMemory();
5453 return -1;
5454 }
5455
5456 sp = stack;
5457 stackdepth_push(&sp, entryblock, 0);
5458 while (sp != stack) {
5459 b = *--sp;
5460 int depth = b->b_startdepth;
5461 assert(depth >= 0);
5462 basicblock *next = b->b_next;
5463 for (int i = 0; i < b->b_iused; i++) {
5464 struct instr *instr = &b->b_instr[i];
5465 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
5466 if (effect == PY_INVALID_STACK_EFFECT) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01005467 _Py_FatalErrorFormat(__func__,
5468 "opcode = %d", instr->i_opcode);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005469 }
5470 int new_depth = depth + effect;
5471 if (new_depth > maxdepth) {
5472 maxdepth = new_depth;
5473 }
5474 assert(depth >= 0); /* invalid code or bug in stackdepth() */
Mark Shannon582aaf12020-08-04 17:30:11 +01005475 if (is_jump(instr)) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005476 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
5477 assert(effect != PY_INVALID_STACK_EFFECT);
5478 int target_depth = depth + effect;
5479 if (target_depth > maxdepth) {
5480 maxdepth = target_depth;
5481 }
5482 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005483 stackdepth_push(&sp, instr->i_target, target_depth);
5484 }
5485 depth = new_depth;
5486 if (instr->i_opcode == JUMP_ABSOLUTE ||
5487 instr->i_opcode == JUMP_FORWARD ||
5488 instr->i_opcode == RETURN_VALUE ||
Mark Shannonfee55262019-11-21 09:11:43 +00005489 instr->i_opcode == RAISE_VARARGS ||
5490 instr->i_opcode == RERAISE)
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005491 {
5492 /* remaining code is dead */
5493 next = NULL;
5494 break;
5495 }
5496 }
5497 if (next != NULL) {
Mark Shannon266b4622020-11-17 19:30:14 +00005498 assert(b->b_nofallthrough == 0);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005499 stackdepth_push(&sp, next, depth);
5500 }
5501 }
5502 PyObject_Free(stack);
5503 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005504}
5505
5506static int
5507assemble_init(struct assembler *a, int nblocks, int firstlineno)
5508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005509 memset(a, 0, sizeof(struct assembler));
Mark Shannon877df852020-11-12 09:43:29 +00005510 a->a_prevlineno = a->a_lineno = firstlineno;
Mark Shannonfd009e62020-11-13 12:53:53 +00005511 a->a_lnotab = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005512 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00005513 if (a->a_bytecode == NULL) {
5514 goto error;
5515 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005516 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00005517 if (a->a_lnotab == NULL) {
5518 goto error;
5519 }
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07005520 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005521 PyErr_NoMemory();
Mark Shannonfd009e62020-11-13 12:53:53 +00005522 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005523 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005524 return 1;
Mark Shannonfd009e62020-11-13 12:53:53 +00005525error:
5526 Py_XDECREF(a->a_bytecode);
5527 Py_XDECREF(a->a_lnotab);
5528 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005529}
5530
5531static void
5532assemble_free(struct assembler *a)
5533{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005534 Py_XDECREF(a->a_bytecode);
5535 Py_XDECREF(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005536}
5537
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005538static int
5539blocksize(basicblock *b)
5540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005541 int i;
5542 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005544 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005545 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005546 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005547}
5548
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005549static int
Mark Shannon877df852020-11-12 09:43:29 +00005550assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005551{
Mark Shannon877df852020-11-12 09:43:29 +00005552 Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005553 if (a->a_lnotab_off + 2 >= len) {
5554 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
5555 return 0;
5556 }
Mark Shannon877df852020-11-12 09:43:29 +00005557 unsigned char *lnotab = (unsigned char *)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005558 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00005559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005560 a->a_lnotab_off += 2;
Mark Shannon877df852020-11-12 09:43:29 +00005561 *lnotab++ = bdelta;
5562 *lnotab++ = ldelta;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005563 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005564}
5565
Mark Shannon877df852020-11-12 09:43:29 +00005566/* Appends a range to the end of the line number table. See
5567 * Objects/lnotab_notes.txt for the description of the line number table. */
5568
5569static int
5570assemble_line_range(struct assembler *a)
5571{
5572 int ldelta, bdelta;
5573 bdelta = (a->a_offset - a->a_lineno_start) * 2;
5574 if (bdelta == 0) {
5575 return 1;
5576 }
5577 if (a->a_lineno < 0) {
5578 ldelta = -128;
5579 }
5580 else {
5581 ldelta = a->a_lineno - a->a_prevlineno;
5582 a->a_prevlineno = a->a_lineno;
5583 while (ldelta > 127) {
5584 if (!assemble_emit_linetable_pair(a, 0, 127)) {
5585 return 0;
5586 }
5587 ldelta -= 127;
5588 }
5589 while (ldelta < -127) {
5590 if (!assemble_emit_linetable_pair(a, 0, -127)) {
5591 return 0;
5592 }
5593 ldelta += 127;
5594 }
5595 }
5596 assert(-128 <= ldelta && ldelta < 128);
5597 while (bdelta > 254) {
5598 if (!assemble_emit_linetable_pair(a, 254, ldelta)) {
5599 return 0;
5600 }
5601 ldelta = a->a_lineno < 0 ? -128 : 0;
5602 bdelta -= 254;
5603 }
5604 if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) {
5605 return 0;
5606 }
5607 a->a_lineno_start = a->a_offset;
5608 return 1;
5609}
5610
5611static int
5612assemble_lnotab(struct assembler *a, struct instr *i)
5613{
5614 if (i->i_lineno == a->a_lineno) {
5615 return 1;
5616 }
5617 if (!assemble_line_range(a)) {
5618 return 0;
5619 }
5620 a->a_lineno = i->i_lineno;
5621 return 1;
5622}
5623
5624
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005625/* assemble_emit()
5626 Extend the bytecode with a new instruction.
5627 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00005628*/
5629
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00005630static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005631assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00005632{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005633 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005634 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03005635 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005636
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005637 arg = i->i_oparg;
5638 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005639 if (i->i_lineno && !assemble_lnotab(a, i))
5640 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005641 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005642 if (len > PY_SSIZE_T_MAX / 2)
5643 return 0;
5644 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
5645 return 0;
5646 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03005647 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005648 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005649 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005650 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00005651}
5652
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00005653static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005654assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00005655{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005656 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005657 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005658 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00005659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005660 /* Compute the size of each block and fixup jump args.
5661 Replace block pointer with position in bytecode. */
5662 do {
5663 totsize = 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00005664 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005665 bsize = blocksize(b);
5666 b->b_offset = totsize;
5667 totsize += bsize;
5668 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005669 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005670 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
5671 bsize = b->b_offset;
5672 for (i = 0; i < b->b_iused; i++) {
5673 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005674 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005675 /* Relative jumps are computed relative to
5676 the instruction pointer after fetching
5677 the jump instruction.
5678 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005679 bsize += isize;
Mark Shannon582aaf12020-08-04 17:30:11 +01005680 if (is_jump(instr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005681 instr->i_oparg = instr->i_target->b_offset;
Mark Shannon582aaf12020-08-04 17:30:11 +01005682 if (is_relative_jump(instr)) {
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005683 instr->i_oparg -= bsize;
5684 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03005685 instr->i_oparg *= sizeof(_Py_CODEUNIT);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005686 if (instrsize(instr->i_oparg) != isize) {
5687 extended_arg_recompile = 1;
5688 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005689 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005690 }
5691 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00005692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005693 /* XXX: This is an awful hack that could hurt performance, but
5694 on the bright side it should work until we come up
5695 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00005696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005697 The issue is that in the first loop blocksize() is called
5698 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005699 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005700 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00005701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005702 So we loop until we stop seeing new EXTENDED_ARGs.
5703 The only EXTENDED_ARGs that could be popping up are
5704 ones in jump instructions. So this should converge
5705 fairly quickly.
5706 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005707 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005708}
5709
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005710static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01005711dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005713 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02005714 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005716 tuple = PyTuple_New(size);
5717 if (tuple == NULL)
5718 return NULL;
5719 while (PyDict_Next(dict, &pos, &k, &v)) {
5720 i = PyLong_AS_LONG(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005721 Py_INCREF(k);
5722 assert((i - offset) < size);
5723 assert((i - offset) >= 0);
5724 PyTuple_SET_ITEM(tuple, i - offset, k);
5725 }
5726 return tuple;
5727}
5728
5729static PyObject *
5730consts_dict_keys_inorder(PyObject *dict)
5731{
5732 PyObject *consts, *k, *v;
5733 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
5734
5735 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
5736 if (consts == NULL)
5737 return NULL;
5738 while (PyDict_Next(dict, &pos, &k, &v)) {
5739 i = PyLong_AS_LONG(v);
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +03005740 /* The keys of the dictionary can be tuples wrapping a contant.
5741 * (see compiler_add_o and _PyCode_ConstantKey). In that case
5742 * the object we want is always second. */
5743 if (PyTuple_CheckExact(k)) {
5744 k = PyTuple_GET_ITEM(k, 1);
5745 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005746 Py_INCREF(k);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005747 assert(i < size);
5748 assert(i >= 0);
5749 PyList_SET_ITEM(consts, i, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005750 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005751 return consts;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005752}
5753
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005754static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005755compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005757 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005758 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005759 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04005760 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005761 if (ste->ste_nested)
5762 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07005763 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005764 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07005765 if (!ste->ste_generator && ste->ste_coroutine)
5766 flags |= CO_COROUTINE;
5767 if (ste->ste_generator && ste->ste_coroutine)
5768 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005769 if (ste->ste_varargs)
5770 flags |= CO_VARARGS;
5771 if (ste->ste_varkeywords)
5772 flags |= CO_VARKEYWORDS;
5773 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00005774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005775 /* (Only) inherit compilerflags in PyCF_MASK */
5776 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00005777
Pablo Galindo90235812020-03-15 04:29:22 +00005778 if ((IS_TOP_LEVEL_AWAIT(c)) &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005779 ste->ste_coroutine &&
5780 !ste->ste_generator) {
5781 flags |= CO_COROUTINE;
5782 }
5783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005784 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005785}
5786
INADA Naokic2e16072018-11-26 21:23:22 +09005787// Merge *tuple* with constant cache.
5788// Unlike merge_consts_recursive(), this function doesn't work recursively.
5789static int
5790merge_const_tuple(struct compiler *c, PyObject **tuple)
5791{
5792 assert(PyTuple_CheckExact(*tuple));
5793
5794 PyObject *key = _PyCode_ConstantKey(*tuple);
5795 if (key == NULL) {
5796 return 0;
5797 }
5798
5799 // t is borrowed reference
5800 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
5801 Py_DECREF(key);
5802 if (t == NULL) {
5803 return 0;
5804 }
5805 if (t == key) { // tuple is new constant.
5806 return 1;
5807 }
5808
5809 PyObject *u = PyTuple_GET_ITEM(t, 1);
5810 Py_INCREF(u);
5811 Py_DECREF(*tuple);
5812 *tuple = u;
5813 return 1;
5814}
5815
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005816static PyCodeObject *
Mark Shannon6e8128f2020-07-30 10:03:00 +01005817makecode(struct compiler *c, struct assembler *a, PyObject *consts)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005818{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005819 PyCodeObject *co = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005820 PyObject *names = NULL;
5821 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005822 PyObject *name = NULL;
5823 PyObject *freevars = NULL;
5824 PyObject *cellvars = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005825 Py_ssize_t nlocals;
5826 int nlocals_int;
5827 int flags;
Pablo Galindocd74e662019-06-01 18:08:04 +01005828 int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005830 names = dict_keys_inorder(c->u->u_names, 0);
5831 varnames = dict_keys_inorder(c->u->u_varnames, 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01005832 if (!names || !varnames) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005833 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01005834 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005835 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
5836 if (!cellvars)
5837 goto error;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005838 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005839 if (!freevars)
5840 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005841
INADA Naokic2e16072018-11-26 21:23:22 +09005842 if (!merge_const_tuple(c, &names) ||
5843 !merge_const_tuple(c, &varnames) ||
5844 !merge_const_tuple(c, &cellvars) ||
5845 !merge_const_tuple(c, &freevars))
5846 {
5847 goto error;
5848 }
5849
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02005850 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01005851 assert(nlocals < INT_MAX);
5852 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
5853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005854 flags = compute_code_flags(c);
5855 if (flags < 0)
5856 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005857
Mark Shannon6e8128f2020-07-30 10:03:00 +01005858 consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
5859 if (consts == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005860 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01005861 }
INADA Naokic2e16072018-11-26 21:23:22 +09005862 if (!merge_const_tuple(c, &consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01005863 Py_DECREF(consts);
INADA Naokic2e16072018-11-26 21:23:22 +09005864 goto error;
5865 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005866
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01005867 posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int);
Pablo Galindocd74e662019-06-01 18:08:04 +01005868 posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +01005869 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005870 maxdepth = stackdepth(c);
5871 if (maxdepth < 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01005872 Py_DECREF(consts);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005873 goto error;
5874 }
Pablo Galindo4a2edc32019-07-01 11:35:05 +01005875 co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
Mark Shannon13bc1392020-01-23 09:25:17 +00005876 posonlyargcount, kwonlyargcount, nlocals_int,
Mark Shannon6e8128f2020-07-30 10:03:00 +01005877 maxdepth, flags, a->a_bytecode, consts, names,
Pablo Galindo4a2edc32019-07-01 11:35:05 +01005878 varnames, freevars, cellvars, c->c_filename,
5879 c->u->u_name, c->u->u_firstlineno, a->a_lnotab);
Mark Shannon6e8128f2020-07-30 10:03:00 +01005880 Py_DECREF(consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005881 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005882 Py_XDECREF(names);
5883 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005884 Py_XDECREF(name);
5885 Py_XDECREF(freevars);
5886 Py_XDECREF(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005887 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005888}
5889
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005890
5891/* For debugging purposes only */
5892#if 0
5893static void
5894dump_instr(const struct instr *i)
5895{
Mark Shannon582aaf12020-08-04 17:30:11 +01005896 const char *jrel = (is_relative_jump(instr)) ? "jrel " : "";
5897 const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : "";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005898 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005900 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005901 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005902 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005903 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005904 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
5905 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005906}
5907
5908static void
5909dump_basicblock(const basicblock *b)
5910{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005911 const char *b_return = b->b_return ? "return " : "";
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005912 fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n",
5913 b->b_iused, b->b_startdepth, b->b_offset, b_return);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005914 if (b->b_instr) {
5915 int i;
5916 for (i = 0; i < b->b_iused; i++) {
5917 fprintf(stderr, " [%02d] ", i);
5918 dump_instr(b->b_instr + i);
5919 }
5920 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005921}
5922#endif
5923
Mark Shannon5977a792020-12-02 13:31:40 +00005924
5925static int
5926normalize_basic_block(basicblock *bb);
5927
Mark Shannon6e8128f2020-07-30 10:03:00 +01005928static int
5929optimize_cfg(struct assembler *a, PyObject *consts);
5930
Mark Shannon5977a792020-12-02 13:31:40 +00005931static int
5932ensure_exits_have_lineno(struct compiler *c);
5933
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005934static PyCodeObject *
5935assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005937 basicblock *b, *entryblock;
5938 struct assembler a;
Mark Shannoncc75ab72020-11-12 19:49:33 +00005939 int j, nblocks;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005940 PyCodeObject *co = NULL;
Mark Shannon6e8128f2020-07-30 10:03:00 +01005941 PyObject *consts = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005943 /* Make sure every block that falls off the end returns None.
5944 XXX NEXT_BLOCK() isn't quite right, because if the last
5945 block ends with a jump or return b_next shouldn't set.
5946 */
5947 if (!c->u->u_curblock->b_return) {
Mark Shannon877df852020-11-12 09:43:29 +00005948 c->u->u_lineno = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005949 if (addNone)
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005950 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005951 ADDOP(c, RETURN_VALUE);
5952 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005953
Mark Shannon5977a792020-12-02 13:31:40 +00005954 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
5955 if (normalize_basic_block(b)) {
5956 goto error;
5957 }
5958 }
5959
5960 if (ensure_exits_have_lineno(c)) {
5961 goto error;
5962 }
5963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005964 nblocks = 0;
5965 entryblock = NULL;
5966 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
5967 nblocks++;
5968 entryblock = b;
5969 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005971 /* Set firstlineno if it wasn't explicitly set. */
5972 if (!c->u->u_firstlineno) {
Ned Deilydc35cda2016-08-17 17:18:33 -04005973 if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005974 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
Mark Shannon877df852020-11-12 09:43:29 +00005975 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005976 c->u->u_firstlineno = 1;
5977 }
Mark Shannon5977a792020-12-02 13:31:40 +00005978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005979 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
5980 goto error;
Mark Shannoncc75ab72020-11-12 19:49:33 +00005981 a.a_entry = entryblock;
5982 a.a_nblocks = nblocks;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005983
Mark Shannon6e8128f2020-07-30 10:03:00 +01005984 consts = consts_dict_keys_inorder(c->u->u_consts);
5985 if (consts == NULL) {
5986 goto error;
5987 }
5988 if (optimize_cfg(&a, consts)) {
5989 goto error;
5990 }
5991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005992 /* Can't modify the bytecode after computing jump offsets. */
5993 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00005994
Mark Shannoncc75ab72020-11-12 19:49:33 +00005995 /* Emit code. */
5996 for(b = entryblock; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005997 for (j = 0; j < b->b_iused; j++)
5998 if (!assemble_emit(&a, &b->b_instr[j]))
5999 goto error;
6000 }
Mark Shannon877df852020-11-12 09:43:29 +00006001 if (!assemble_line_range(&a)) {
6002 return 0;
6003 }
6004 /* Emit sentinel at end of line number table */
6005 if (!assemble_emit_linetable_pair(&a, 255, -128)) {
6006 goto error;
6007 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00006008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006009 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
6010 goto error;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006011 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006012 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006013
Mark Shannon6e8128f2020-07-30 10:03:00 +01006014 co = makecode(c, &a, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006015 error:
Mark Shannon6e8128f2020-07-30 10:03:00 +01006016 Py_XDECREF(consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006017 assemble_free(&a);
6018 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006019}
Georg Brandl8334fd92010-12-04 10:26:46 +00006020
6021#undef PyAST_Compile
Benjamin Petersone5024512018-09-12 12:06:42 -07006022PyCodeObject *
Georg Brandl8334fd92010-12-04 10:26:46 +00006023PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
6024 PyArena *arena)
6025{
6026 return PyAST_CompileEx(mod, filename, flags, -1, arena);
6027}
Mark Shannon6e8128f2020-07-30 10:03:00 +01006028
6029
6030/* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
6031 with LOAD_CONST (c1, c2, ... cn).
6032 The consts table must still be in list form so that the
6033 new constant (c1, c2, ... cn) can be appended.
6034 Called with codestr pointing to the first LOAD_CONST.
6035*/
6036static int
6037fold_tuple_on_constants(struct instr *inst,
6038 int n, PyObject *consts)
6039{
6040 /* Pre-conditions */
6041 assert(PyList_CheckExact(consts));
6042 assert(inst[n].i_opcode == BUILD_TUPLE);
6043 assert(inst[n].i_oparg == n);
6044
6045 for (int i = 0; i < n; i++) {
6046 if (inst[i].i_opcode != LOAD_CONST) {
6047 return 0;
6048 }
6049 }
6050
6051 /* Buildup new tuple of constants */
6052 PyObject *newconst = PyTuple_New(n);
6053 if (newconst == NULL) {
6054 return -1;
6055 }
6056 for (int i = 0; i < n; i++) {
6057 int arg = inst[i].i_oparg;
6058 PyObject *constant = PyList_GET_ITEM(consts, arg);
6059 Py_INCREF(constant);
6060 PyTuple_SET_ITEM(newconst, i, constant);
6061 }
6062 Py_ssize_t index = PyList_GET_SIZE(consts);
Victor Stinner71f2ff42020-09-23 14:06:55 +02006063 if ((size_t)index >= (size_t)INT_MAX - 1) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006064 Py_DECREF(newconst);
6065 PyErr_SetString(PyExc_OverflowError, "too many constants");
6066 return -1;
6067 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006068 if (PyList_Append(consts, newconst)) {
6069 Py_DECREF(newconst);
6070 return -1;
6071 }
6072 Py_DECREF(newconst);
6073 for (int i = 0; i < n; i++) {
6074 inst[i].i_opcode = NOP;
6075 }
6076 inst[n].i_opcode = LOAD_CONST;
Victor Stinner71f2ff42020-09-23 14:06:55 +02006077 inst[n].i_oparg = (int)index;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006078 return 0;
6079}
6080
Mark Shannon28b75c82020-12-23 11:43:10 +00006081
6082static int
6083eliminate_jump_to_jump(basicblock *bb, int opcode) {
6084 assert (bb->b_iused > 0);
6085 struct instr *inst = &bb->b_instr[bb->b_iused-1];
6086 assert (is_jump(inst));
6087 assert (inst->i_target->b_iused > 0);
6088 struct instr *target = &inst->i_target->b_instr[0];
6089 if (inst->i_target == target->i_target) {
6090 /* Nothing to do */
6091 return 0;
6092 }
6093 int lineno = target->i_lineno;
6094 if (add_jump_to_block(bb, opcode, lineno, target->i_target) == 0) {
6095 return -1;
6096 }
6097 assert (bb->b_iused >= 2);
6098 bb->b_instr[bb->b_iused-2].i_opcode = NOP;
6099 return 0;
6100}
6101
Mark Shannoncc75ab72020-11-12 19:49:33 +00006102/* Maximum size of basic block that should be copied in optimizer */
6103#define MAX_COPY_SIZE 4
Mark Shannon6e8128f2020-07-30 10:03:00 +01006104
6105/* Optimization */
6106static int
6107optimize_basic_block(basicblock *bb, PyObject *consts)
6108{
6109 assert(PyList_CheckExact(consts));
6110 struct instr nop;
6111 nop.i_opcode = NOP;
6112 struct instr *target;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006113 for (int i = 0; i < bb->b_iused; i++) {
6114 struct instr *inst = &bb->b_instr[i];
6115 int oparg = inst->i_oparg;
6116 int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01006117 if (is_jump(inst)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006118 /* Skip over empty basic blocks. */
6119 while (inst->i_target->b_iused == 0) {
6120 inst->i_target = inst->i_target->b_next;
6121 }
6122 target = &inst->i_target->b_instr[0];
6123 }
6124 else {
6125 target = &nop;
6126 }
6127 switch (inst->i_opcode) {
Mark Shannon266b4622020-11-17 19:30:14 +00006128 /* Remove LOAD_CONST const; conditional jump */
Mark Shannon6e8128f2020-07-30 10:03:00 +01006129 case LOAD_CONST:
Mark Shannon266b4622020-11-17 19:30:14 +00006130 {
6131 PyObject* cnt;
6132 int is_true;
6133 int jump_if_true;
6134 switch(nextop) {
6135 case POP_JUMP_IF_FALSE:
6136 case POP_JUMP_IF_TRUE:
6137 cnt = PyList_GET_ITEM(consts, oparg);
6138 is_true = PyObject_IsTrue(cnt);
6139 if (is_true == -1) {
6140 goto error;
6141 }
6142 inst->i_opcode = NOP;
6143 jump_if_true = nextop == POP_JUMP_IF_TRUE;
6144 if (is_true == jump_if_true) {
6145 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
6146 bb->b_nofallthrough = 1;
6147 }
6148 else {
6149 bb->b_instr[i+1].i_opcode = NOP;
6150 }
6151 break;
6152 case JUMP_IF_FALSE_OR_POP:
6153 case JUMP_IF_TRUE_OR_POP:
6154 cnt = PyList_GET_ITEM(consts, oparg);
6155 is_true = PyObject_IsTrue(cnt);
6156 if (is_true == -1) {
6157 goto error;
6158 }
6159 jump_if_true = nextop == JUMP_IF_TRUE_OR_POP;
6160 if (is_true == jump_if_true) {
6161 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
6162 bb->b_nofallthrough = 1;
6163 }
6164 else {
6165 inst->i_opcode = NOP;
6166 bb->b_instr[i+1].i_opcode = NOP;
6167 }
6168 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006169 }
6170 break;
Mark Shannon266b4622020-11-17 19:30:14 +00006171 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006172
6173 /* Try to fold tuples of constants.
6174 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
6175 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
6176 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
6177 case BUILD_TUPLE:
6178 if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
6179 switch(oparg) {
6180 case 1:
6181 inst->i_opcode = NOP;
6182 bb->b_instr[i+1].i_opcode = NOP;
6183 break;
6184 case 2:
6185 inst->i_opcode = ROT_TWO;
6186 bb->b_instr[i+1].i_opcode = NOP;
6187 break;
6188 case 3:
6189 inst->i_opcode = ROT_THREE;
6190 bb->b_instr[i+1].i_opcode = ROT_TWO;
6191 }
6192 break;
6193 }
6194 if (i >= oparg) {
6195 if (fold_tuple_on_constants(inst-oparg, oparg, consts)) {
6196 goto error;
6197 }
6198 }
6199 break;
6200
6201 /* Simplify conditional jump to conditional jump where the
6202 result of the first test implies the success of a similar
6203 test or the failure of the opposite test.
6204 Arises in code like:
6205 "a and b or c"
6206 "(a and b) and c"
6207 "(a or b) or c"
6208 "(a or b) and c"
6209 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
6210 --> x:JUMP_IF_FALSE_OR_POP z
6211 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
6212 --> x:POP_JUMP_IF_FALSE y+1
6213 where y+1 is the instruction following the second test.
6214 */
6215 case JUMP_IF_FALSE_OR_POP:
6216 switch(target->i_opcode) {
6217 case POP_JUMP_IF_FALSE:
Mark Shannon28b75c82020-12-23 11:43:10 +00006218 if (inst->i_lineno == target->i_lineno) {
6219 *inst = *target;
6220 i--;
6221 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006222 break;
6223 case JUMP_ABSOLUTE:
6224 case JUMP_FORWARD:
6225 case JUMP_IF_FALSE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00006226 if (inst->i_lineno == target->i_lineno &&
6227 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00006228 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00006229 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00006230 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006231 break;
6232 case JUMP_IF_TRUE_OR_POP:
6233 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00006234 if (inst->i_lineno == target->i_lineno) {
6235 inst->i_opcode = POP_JUMP_IF_FALSE;
6236 inst->i_target = inst->i_target->b_next;
6237 --i;
6238 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006239 break;
6240 }
6241 break;
6242
6243 case JUMP_IF_TRUE_OR_POP:
6244 switch(target->i_opcode) {
6245 case POP_JUMP_IF_TRUE:
Mark Shannon28b75c82020-12-23 11:43:10 +00006246 if (inst->i_lineno == target->i_lineno) {
6247 *inst = *target;
6248 i--;
6249 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006250 break;
6251 case JUMP_ABSOLUTE:
6252 case JUMP_FORWARD:
6253 case JUMP_IF_TRUE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00006254 if (inst->i_lineno == target->i_lineno &&
6255 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00006256 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00006257 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00006258 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006259 break;
6260 case JUMP_IF_FALSE_OR_POP:
6261 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00006262 if (inst->i_lineno == target->i_lineno) {
6263 inst->i_opcode = POP_JUMP_IF_TRUE;
6264 inst->i_target = inst->i_target->b_next;
6265 --i;
6266 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006267 break;
6268 }
6269 break;
6270
6271 case POP_JUMP_IF_FALSE:
6272 switch(target->i_opcode) {
6273 case JUMP_ABSOLUTE:
6274 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00006275 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00006276 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00006277 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00006278 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006279 break;
6280 }
6281 break;
6282
6283 case POP_JUMP_IF_TRUE:
6284 switch(target->i_opcode) {
6285 case JUMP_ABSOLUTE:
6286 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00006287 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00006288 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00006289 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00006290 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006291 break;
6292 }
6293 break;
6294
6295 case JUMP_ABSOLUTE:
6296 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00006297 assert (i == bb->b_iused-1);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006298 switch(target->i_opcode) {
6299 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00006300 if (eliminate_jump_to_jump(bb, inst->i_opcode)) {
6301 goto error;
Mark Shannon266b4622020-11-17 19:30:14 +00006302 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006303 break;
Mark Shannon28b75c82020-12-23 11:43:10 +00006304
Mark Shannon6e8128f2020-07-30 10:03:00 +01006305 case JUMP_ABSOLUTE:
Mark Shannon28b75c82020-12-23 11:43:10 +00006306 if (eliminate_jump_to_jump(bb, JUMP_ABSOLUTE)) {
6307 goto error;
Mark Shannon266b4622020-11-17 19:30:14 +00006308 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006309 break;
Mark Shannon28b75c82020-12-23 11:43:10 +00006310 default:
6311 if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) {
6312 basicblock *to_copy = inst->i_target;
6313 inst->i_opcode = NOP;
6314 for (i = 0; i < to_copy->b_iused; i++) {
6315 int index = compiler_next_instr(bb);
6316 if (index < 0) {
6317 return -1;
6318 }
6319 bb->b_instr[index] = to_copy->b_instr[i];
6320 }
6321 bb->b_exit = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006322 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00006323 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006324 }
6325 }
6326 return 0;
6327error:
6328 return -1;
6329}
6330
6331
6332static void
Mark Shannon1659ad12021-01-13 15:05:04 +00006333clean_basic_block(basicblock *bb, int prev_lineno) {
6334 /* Remove NOPs when legal to do so. */
Mark Shannon6e8128f2020-07-30 10:03:00 +01006335 int dest = 0;
6336 for (int src = 0; src < bb->b_iused; src++) {
Mark Shannon877df852020-11-12 09:43:29 +00006337 int lineno = bb->b_instr[src].i_lineno;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006338 if (bb->b_instr[src].i_opcode == NOP) {
Mark Shannon266b4622020-11-17 19:30:14 +00006339 /* Eliminate no-op if it doesn't have a line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00006340 if (lineno < 0) {
6341 continue;
6342 }
Mark Shannon266b4622020-11-17 19:30:14 +00006343 /* or, if the previous instruction had the same line number. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00006344 if (prev_lineno == lineno) {
6345 continue;
6346 }
Mark Shannon266b4622020-11-17 19:30:14 +00006347 /* or, if the next instruction has same line number or no line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00006348 if (src < bb->b_iused - 1) {
6349 int next_lineno = bb->b_instr[src+1].i_lineno;
6350 if (next_lineno < 0 || next_lineno == lineno) {
6351 bb->b_instr[src+1].i_lineno = lineno;
6352 continue;
Mark Shannon877df852020-11-12 09:43:29 +00006353 }
6354 }
Mark Shannon266b4622020-11-17 19:30:14 +00006355 else {
6356 basicblock* next = bb->b_next;
6357 while (next && next->b_iused == 0) {
6358 next = next->b_next;
6359 }
6360 /* or if last instruction in BB and next BB has same line number */
6361 if (next) {
6362 if (lineno == next->b_instr[0].i_lineno) {
6363 continue;
6364 }
6365 }
6366 }
6367
Mark Shannon6e8128f2020-07-30 10:03:00 +01006368 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00006369 if (dest != src) {
6370 bb->b_instr[dest] = bb->b_instr[src];
6371 }
6372 dest++;
6373 prev_lineno = lineno;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006374 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006375 assert(dest <= bb->b_iused);
6376 bb->b_iused = dest;
6377}
6378
Mark Shannon266b4622020-11-17 19:30:14 +00006379static int
6380normalize_basic_block(basicblock *bb) {
6381 /* Mark blocks as exit and/or nofallthrough.
6382 Raise SystemError if CFG is malformed. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00006383 for (int i = 0; i < bb->b_iused; i++) {
6384 switch(bb->b_instr[i].i_opcode) {
6385 case RETURN_VALUE:
6386 case RAISE_VARARGS:
6387 case RERAISE:
Mark Shannoncc75ab72020-11-12 19:49:33 +00006388 bb->b_exit = 1;
Mark Shannon5977a792020-12-02 13:31:40 +00006389 bb->b_nofallthrough = 1;
6390 break;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006391 case JUMP_ABSOLUTE:
6392 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00006393 bb->b_nofallthrough = 1;
Mark Shannon266b4622020-11-17 19:30:14 +00006394 /* fall through */
6395 case POP_JUMP_IF_FALSE:
6396 case POP_JUMP_IF_TRUE:
6397 case JUMP_IF_FALSE_OR_POP:
6398 case JUMP_IF_TRUE_OR_POP:
Mark Shannon5977a792020-12-02 13:31:40 +00006399 case FOR_ITER:
Mark Shannon266b4622020-11-17 19:30:14 +00006400 if (i != bb->b_iused-1) {
6401 PyErr_SetString(PyExc_SystemError, "malformed control flow graph.");
6402 return -1;
6403 }
Mark Shannon5977a792020-12-02 13:31:40 +00006404 /* Skip over empty basic blocks. */
6405 while (bb->b_instr[i].i_target->b_iused == 0) {
6406 bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next;
6407 }
6408
Mark Shannoncc75ab72020-11-12 19:49:33 +00006409 }
6410 }
Mark Shannon266b4622020-11-17 19:30:14 +00006411 return 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006412}
6413
Mark Shannon6e8128f2020-07-30 10:03:00 +01006414static int
6415mark_reachable(struct assembler *a) {
6416 basicblock **stack, **sp;
6417 sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks);
6418 if (stack == NULL) {
6419 return -1;
6420 }
Mark Shannon3bd60352021-01-13 12:05:43 +00006421 a->a_entry->b_predecessors = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006422 *sp++ = a->a_entry;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006423 while (sp > stack) {
6424 basicblock *b = *(--sp);
Mark Shannon3bd60352021-01-13 12:05:43 +00006425 if (b->b_next && !b->b_nofallthrough) {
6426 if (b->b_next->b_predecessors == 0) {
6427 *sp++ = b->b_next;
6428 }
6429 b->b_next->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006430 }
6431 for (int i = 0; i < b->b_iused; i++) {
6432 basicblock *target;
Mark Shannon582aaf12020-08-04 17:30:11 +01006433 if (is_jump(&b->b_instr[i])) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006434 target = b->b_instr[i].i_target;
Mark Shannon3bd60352021-01-13 12:05:43 +00006435 if (target->b_predecessors == 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006436 *sp++ = target;
6437 }
Mark Shannon3bd60352021-01-13 12:05:43 +00006438 target->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006439 }
6440 }
6441 }
6442 PyObject_Free(stack);
6443 return 0;
6444}
6445
Mark Shannon3bd60352021-01-13 12:05:43 +00006446static void
6447eliminate_empty_basic_blocks(basicblock *entry) {
6448 /* Eliminate empty blocks */
6449 for (basicblock *b = entry; b != NULL; b = b->b_next) {
6450 basicblock *next = b->b_next;
6451 if (next) {
6452 while (next->b_iused == 0 && next->b_next) {
6453 next = next->b_next;
6454 }
6455 b->b_next = next;
6456 }
6457 }
6458 for (basicblock *b = entry; b != NULL; b = b->b_next) {
6459 if (b->b_iused == 0) {
6460 continue;
6461 }
6462 if (is_jump(&b->b_instr[b->b_iused-1])) {
6463 basicblock *target = b->b_instr[b->b_iused-1].i_target;
6464 while (target->b_iused == 0) {
6465 target = target->b_next;
6466 }
6467 b->b_instr[b->b_iused-1].i_target = target;
6468 }
6469 }
6470}
6471
6472
Mark Shannon5977a792020-12-02 13:31:40 +00006473/* If an instruction has no line number, but it's predecessor in the BB does,
Mark Shannon3bd60352021-01-13 12:05:43 +00006474 * then copy the line number. If a successor block has no line number, and only
6475 * one predecessor, then inherit the line number.
6476 * This ensures that all exit blocks (with one predecessor) receive a line number.
6477 * Also reduces the size of the line number table,
Mark Shannon5977a792020-12-02 13:31:40 +00006478 * but has no impact on the generated line number events.
6479 */
6480static void
Mark Shannon3bd60352021-01-13 12:05:43 +00006481propogate_line_numbers(struct assembler *a) {
Mark Shannon5977a792020-12-02 13:31:40 +00006482 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00006483 if (b->b_iused == 0) {
6484 continue;
6485 }
Mark Shannon5977a792020-12-02 13:31:40 +00006486 int prev_lineno = -1;
6487 for (int i = 0; i < b->b_iused; i++) {
6488 if (b->b_instr[i].i_lineno < 0) {
6489 b->b_instr[i].i_lineno = prev_lineno;
6490 }
6491 else {
6492 prev_lineno = b->b_instr[i].i_lineno;
6493 }
6494 }
Mark Shannon3bd60352021-01-13 12:05:43 +00006495 if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) {
6496 assert(b->b_next->b_iused);
6497 if (b->b_next->b_instr[0].i_lineno < 0) {
6498 b->b_next->b_instr[0].i_lineno = prev_lineno;
6499 }
6500 }
6501 if (is_jump(&b->b_instr[b->b_iused-1])) {
6502 switch (b->b_instr[b->b_iused-1].i_opcode) {
6503 /* Note: Only actual jumps, not exception handlers */
6504 case SETUP_ASYNC_WITH:
6505 case SETUP_WITH:
6506 case SETUP_FINALLY:
6507 continue;
6508 }
6509 basicblock *target = b->b_instr[b->b_iused-1].i_target;
6510 if (target->b_predecessors == 1) {
6511 if (target->b_instr[0].i_lineno < 0) {
6512 target->b_instr[0].i_lineno = prev_lineno;
6513 }
6514 }
6515 }
Mark Shannon5977a792020-12-02 13:31:40 +00006516 }
6517}
6518
6519/* Perform optimizations on a control flow graph.
Mark Shannon6e8128f2020-07-30 10:03:00 +01006520 The consts object should still be in list form to allow new constants
6521 to be appended.
6522
6523 All transformations keep the code size the same or smaller.
6524 For those that reduce size, the gaps are initially filled with
6525 NOPs. Later those NOPs are removed.
6526*/
6527
6528static int
6529optimize_cfg(struct assembler *a, PyObject *consts)
6530{
Mark Shannoncc75ab72020-11-12 19:49:33 +00006531 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannoncc75ab72020-11-12 19:49:33 +00006532 if (optimize_basic_block(b, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006533 return -1;
6534 }
Mark Shannon1659ad12021-01-13 15:05:04 +00006535 clean_basic_block(b, -1);
Mark Shannon3bd60352021-01-13 12:05:43 +00006536 assert(b->b_predecessors == 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006537 }
6538 if (mark_reachable(a)) {
6539 return -1;
6540 }
6541 /* Delete unreachable instructions */
Mark Shannoncc75ab72020-11-12 19:49:33 +00006542 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00006543 if (b->b_predecessors == 0) {
Mark Shannoncc75ab72020-11-12 19:49:33 +00006544 b->b_iused = 0;
Om Gc71581c2020-12-16 17:48:05 +05306545 b->b_nofallthrough = 0;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006546 }
6547 }
Mark Shannon1659ad12021-01-13 15:05:04 +00006548 basicblock *pred = NULL;
6549 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
6550 int prev_lineno = -1;
6551 if (pred && pred->b_iused) {
6552 prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno;
6553 }
6554 clean_basic_block(b, prev_lineno);
6555 pred = b->b_nofallthrough ? NULL : b;
6556 }
Mark Shannon3bd60352021-01-13 12:05:43 +00006557 eliminate_empty_basic_blocks(a->a_entry);
Om Gc71581c2020-12-16 17:48:05 +05306558 /* Delete jump instructions made redundant by previous step. If a non-empty
6559 block ends with a jump instruction, check if the next non-empty block
6560 reached through normal flow control is the target of that jump. If it
6561 is, then the jump instruction is redundant and can be deleted.
6562 */
Mark Shannon3bd60352021-01-13 12:05:43 +00006563 int maybe_empty_blocks = 0;
Om Gc71581c2020-12-16 17:48:05 +05306564 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
6565 if (b->b_iused > 0) {
6566 struct instr *b_last_instr = &b->b_instr[b->b_iused - 1];
6567 if (b_last_instr->i_opcode == POP_JUMP_IF_FALSE ||
6568 b_last_instr->i_opcode == POP_JUMP_IF_TRUE ||
6569 b_last_instr->i_opcode == JUMP_ABSOLUTE ||
6570 b_last_instr->i_opcode == JUMP_FORWARD) {
Mark Shannon3bd60352021-01-13 12:05:43 +00006571 if (b_last_instr->i_target == b->b_next) {
6572 assert(b->b_next->b_iused);
Om Gc71581c2020-12-16 17:48:05 +05306573 b->b_nofallthrough = 0;
6574 switch(b_last_instr->i_opcode) {
6575 case POP_JUMP_IF_FALSE:
6576 case POP_JUMP_IF_TRUE:
6577 b_last_instr->i_opcode = POP_TOP;
6578 b_last_instr->i_target = NULL;
6579 b_last_instr->i_oparg = 0;
6580 break;
6581 case JUMP_ABSOLUTE:
6582 case JUMP_FORWARD:
6583 b_last_instr->i_opcode = NOP;
Mark Shannon1659ad12021-01-13 15:05:04 +00006584 clean_basic_block(b, -1);
Mark Shannon3bd60352021-01-13 12:05:43 +00006585 maybe_empty_blocks = 1;
Om Gc71581c2020-12-16 17:48:05 +05306586 break;
6587 }
Om Gc71581c2020-12-16 17:48:05 +05306588 }
6589 }
6590 }
6591 }
Mark Shannon3bd60352021-01-13 12:05:43 +00006592 if (maybe_empty_blocks) {
6593 eliminate_empty_basic_blocks(a->a_entry);
6594 }
6595 propogate_line_numbers(a);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006596 return 0;
6597}
6598
Mark Shannon5977a792020-12-02 13:31:40 +00006599static inline int
6600is_exit_without_lineno(basicblock *b) {
6601 return b->b_exit && b->b_instr[0].i_lineno < 0;
6602}
6603
6604/* PEP 626 mandates that the f_lineno of a frame is correct
6605 * after a frame terminates. It would be prohibitively expensive
6606 * to continuously update the f_lineno field at runtime,
6607 * so we make sure that all exiting instruction (raises and returns)
6608 * have a valid line number, allowing us to compute f_lineno lazily.
6609 * We can do this by duplicating the exit blocks without line number
6610 * so that none have more than one predecessor. We can then safely
6611 * copy the line number from the sole predecessor block.
6612 */
6613static int
6614ensure_exits_have_lineno(struct compiler *c)
6615{
Mark Shannoneaccc122020-12-04 15:22:12 +00006616 basicblock *entry = NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00006617 /* Copy all exit blocks without line number that are targets of a jump.
6618 */
6619 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
6620 if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) {
6621 switch (b->b_instr[b->b_iused-1].i_opcode) {
6622 /* Note: Only actual jumps, not exception handlers */
6623 case SETUP_ASYNC_WITH:
6624 case SETUP_WITH:
6625 case SETUP_FINALLY:
6626 continue;
6627 }
6628 basicblock *target = b->b_instr[b->b_iused-1].i_target;
6629 if (is_exit_without_lineno(target)) {
6630 basicblock *new_target = compiler_copy_block(c, target);
6631 if (new_target == NULL) {
6632 return -1;
6633 }
6634 new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
6635 b->b_instr[b->b_iused-1].i_target = new_target;
6636 }
6637 }
Mark Shannoneaccc122020-12-04 15:22:12 +00006638 entry = b;
6639 }
6640 assert(entry != NULL);
6641 if (is_exit_without_lineno(entry)) {
6642 entry->b_instr[0].i_lineno = c->u->u_firstlineno;
Mark Shannon5977a792020-12-02 13:31:40 +00006643 }
Mark Shannonee9f98d2021-01-05 12:04:10 +00006644 /* Eliminate empty blocks */
6645 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
6646 while (b->b_next && b->b_next->b_iused == 0) {
6647 b->b_next = b->b_next->b_next;
6648 }
6649 }
Mark Shannon5977a792020-12-02 13:31:40 +00006650 /* Any remaining reachable exit blocks without line number can only be reached by
6651 * fall through, and thus can only have a single predecessor */
6652 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
6653 if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) {
6654 if (is_exit_without_lineno(b->b_next)) {
6655 assert(b->b_next->b_iused > 0);
6656 b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
6657 }
6658 }
6659 }
6660 return 0;
6661}
6662
6663
Mark Shannon6e8128f2020-07-30 10:03:00 +01006664/* Retained for API compatibility.
6665 * Optimization is now done in optimize_cfg */
6666
6667PyObject *
6668PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
6669 PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
6670{
6671 Py_INCREF(code);
6672 return code;
6673}
6674