blob: 308d6866c7dc4bf0f9adf1c6aa0ae5a119849ee6 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file compiles an abstract syntax tree (AST) into Python bytecode.
3 *
Victor Stinnera81fca62021-03-24 00:51:50 +01004 * The primary entry point is _PyAST_Compile(), which returns a
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005 * 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 Stinner526fdeb2021-03-17 23:50:50 +010025#include "pycore_ast.h" // _PyAST_GetDocString()
Victor Stinnera81fca62021-03-24 00:51:50 +010026#include "pycore_compile.h" // _PyFuture_FromAST()
Victor Stinnerba7a99d2021-01-30 01:46:44 +010027#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
Victor Stinnerc9bc2902020-10-27 02:24:34 +010028#include "pycore_long.h" // _PyLong_GetZero()
Victor Stinner28ad12f2021-03-19 12:41:49 +010029#include "pycore_symtable.h" // PySTEntryObject
Guido van Rossum3f5da241990-12-20 15:06:42 +000030
Mark Shannon582aaf12020-08-04 17:30:11 +010031#define NEED_OPCODE_JUMP_TABLES
Victor Stinner526fdeb2021-03-17 23:50:50 +010032#include "opcode.h" // EXTENDED_ARG
33#include "wordcode_helpers.h" // instrsize()
34
Guido van Rossumb05a5c71997-05-07 17:46:13 +000035
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000036#define DEFAULT_BLOCK_SIZE 16
37#define DEFAULT_BLOCKS 8
38#define DEFAULT_CODE_SIZE 128
39#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000040
Nick Coghlan650f0d02007-04-15 12:05:43 +000041#define COMP_GENEXP 0
42#define COMP_LISTCOMP 1
43#define COMP_SETCOMP 2
Guido van Rossum992d4a32007-07-11 13:09:30 +000044#define COMP_DICTCOMP 3
Nick Coghlan650f0d02007-04-15 12:05:43 +000045
Pablo Galindo90235812020-03-15 04:29:22 +000046#define IS_TOP_LEVEL_AWAIT(c) ( \
47 (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \
48 && (c->u->u_ste->ste_type == ModuleBlock))
49
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000050struct instr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 unsigned char i_opcode;
52 int i_oparg;
53 struct basicblock_ *i_target; /* target block (if jump instruction) */
54 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000055};
56
Mark Shannon582aaf12020-08-04 17:30:11 +010057#define LOG_BITS_PER_INT 5
58#define MASK_LOW_LOG_BITS 31
59
60static inline int
61is_bit_set_in_table(uint32_t *table, int bitindex) {
62 /* Is the relevant bit set in the relevant word? */
63 /* 256 bits fit into 8 32-bits words.
64 * Word is indexed by (bitindex>>ln(size of int in bits)).
65 * Bit within word is the low bits of bitindex.
66 */
67 uint32_t word = table[bitindex >> LOG_BITS_PER_INT];
68 return (word >> (bitindex & MASK_LOW_LOG_BITS)) & 1;
69}
70
71static inline int
72is_relative_jump(struct instr *i)
73{
74 return is_bit_set_in_table(_PyOpcode_RelativeJump, i->i_opcode);
75}
76
77static inline int
78is_jump(struct instr *i)
79{
80 return is_bit_set_in_table(_PyOpcode_Jump, i->i_opcode);
81}
82
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000083typedef struct basicblock_ {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000084 /* Each basicblock in a compilation unit is linked via b_list in the
85 reverse order that the block are allocated. b_list points to the next
86 block, not to be confused with b_next, which is next by control flow. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 struct basicblock_ *b_list;
88 /* number of instructions used */
89 int b_iused;
90 /* length of instruction array (b_instr) */
91 int b_ialloc;
92 /* pointer to an array of instructions, initially NULL */
93 struct instr *b_instr;
94 /* If b_next is non-NULL, it is a pointer to the next
95 block reached by normal control flow. */
96 struct basicblock_ *b_next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 /* b_return is true if a RETURN_VALUE opcode is inserted. */
98 unsigned b_return : 1;
Mark Shannon3bd60352021-01-13 12:05:43 +000099 /* Number of predecssors that a block has. */
100 int b_predecessors;
Mark Shannoncc75ab72020-11-12 19:49:33 +0000101 /* Basic block has no fall through (it ends with a return, raise or jump) */
102 unsigned b_nofallthrough : 1;
103 /* Basic block exits scope (it ends with a return or raise) */
104 unsigned b_exit : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 /* depth of stack upon entry of block, computed by stackdepth() */
106 int b_startdepth;
107 /* instruction offset for block, computed by assemble_jump_offsets() */
108 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000109} basicblock;
110
111/* fblockinfo tracks the current frame block.
112
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000113A frame block is used to handle loops, try/except, and try/finally.
114It's called a frame block to distinguish it from a basic block in the
115compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000116*/
117
Mark Shannon02d126a2020-09-25 14:04:19 +0100118enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END,
119 WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER };
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000120
121struct fblockinfo {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 enum fblocktype fb_type;
123 basicblock *fb_block;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200124 /* (optional) type-specific exit or cleanup block */
125 basicblock *fb_exit;
Mark Shannonfee55262019-11-21 09:11:43 +0000126 /* (optional) additional information required for unwinding */
127 void *fb_datum;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000128};
129
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100130enum {
131 COMPILER_SCOPE_MODULE,
132 COMPILER_SCOPE_CLASS,
133 COMPILER_SCOPE_FUNCTION,
Yury Selivanov75445082015-05-11 22:57:16 -0400134 COMPILER_SCOPE_ASYNC_FUNCTION,
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400135 COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100136 COMPILER_SCOPE_COMPREHENSION,
137};
138
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000139/* The following items change on entry and exit of code blocks.
140 They must be saved and restored when returning to a block.
141*/
142struct compiler_unit {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 PySTEntryObject *u_ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 PyObject *u_name;
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400146 PyObject *u_qualname; /* dot-separated qualified name (lazy) */
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100147 int u_scope_type;
148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 /* The following fields are dicts that map objects to
150 the index of them in co_XXX. The index is used as
151 the argument for opcodes that refer to those collections.
152 */
153 PyObject *u_consts; /* all constants */
154 PyObject *u_names; /* all names */
155 PyObject *u_varnames; /* local variables */
156 PyObject *u_cellvars; /* cell variables */
157 PyObject *u_freevars; /* free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 PyObject *u_private; /* for private name mangling */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000160
Victor Stinnerf8e32212013-11-19 23:56:34 +0100161 Py_ssize_t u_argcount; /* number of arguments for block */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100162 Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */
Victor Stinnerf8e32212013-11-19 23:56:34 +0100163 Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 /* Pointer to the most recently allocated block. By following b_list
165 members, you can reach all early allocated blocks. */
166 basicblock *u_blocks;
167 basicblock *u_curblock; /* pointer to current block */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 int u_nfblocks;
170 struct fblockinfo u_fblock[CO_MAXBLOCKS];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 int u_firstlineno; /* the first lineno of the block */
173 int u_lineno; /* the lineno for the current stmt */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000174 int u_col_offset; /* the offset of the current stmt */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000175};
176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177/* This struct captures the global state of a compilation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000178
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000179The u pointer points to the current compilation unit, while units
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180for enclosing blocks are stored in c_stack. The u and c_stack are
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000181managed by compiler_enter_scope() and compiler_exit_scope().
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000182
183Note that we don't track recursion levels during compilation - the
184task of detecting and rejecting excessive levels of nesting is
185handled by the symbol analysis pass.
186
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187*/
188
189struct compiler {
Victor Stinner14e461d2013-08-26 22:28:21 +0200190 PyObject *c_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 struct symtable *c_st;
192 PyFutureFeatures *c_future; /* pointer to module's __future__ */
193 PyCompilerFlags *c_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000194
Georg Brandl8334fd92010-12-04 10:26:46 +0000195 int c_optimize; /* optimization level */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 int c_interactive; /* true if in interactive mode */
197 int c_nestlevel;
INADA Naokic2e16072018-11-26 21:23:22 +0900198 PyObject *c_const_cache; /* Python dict holding all constants,
199 including names tuple */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 struct compiler_unit *u; /* compiler state for current block */
201 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
202 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000203};
204
Brandt Bucher145bf262021-02-26 14:51:55 -0800205typedef struct {
206 PyObject *stores;
207 int allow_irrefutable;
208} pattern_context;
209
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100210static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000211static void compiler_free(struct compiler *);
212static basicblock *compiler_new_block(struct compiler *);
Andy Lester76d58772020-03-10 21:18:12 -0500213static int compiler_next_instr(basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000214static int compiler_addop(struct compiler *, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +0100215static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
Mark Shannon582aaf12020-08-04 17:30:11 +0100216static int compiler_addop_j(struct compiler *, int, basicblock *);
Mark Shannon127dde52021-01-04 18:06:55 +0000217static int compiler_addop_j_noline(struct compiler *, int, basicblock *);
Brandt Bucher145bf262021-02-26 14:51:55 -0800218static int compiler_error(struct compiler *, const char *, ...);
Serhiy Storchaka62e44812019-02-16 08:12:19 +0200219static int compiler_warn(struct compiler *, const char *, ...);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000220static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
221
222static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
223static int compiler_visit_stmt(struct compiler *, stmt_ty);
224static int compiler_visit_keyword(struct compiler *, keyword_ty);
225static int compiler_visit_expr(struct compiler *, expr_ty);
226static int compiler_augassign(struct compiler *, stmt_ty);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700227static int compiler_annassign(struct compiler *, stmt_ty);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +0200228static int compiler_subscript(struct compiler *, expr_ty);
229static int compiler_slice(struct compiler *, expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000230
Andy Lester76d58772020-03-10 21:18:12 -0500231static int inplace_binop(operator_ty);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100232static int are_all_items_const(asdl_expr_seq *, Py_ssize_t, Py_ssize_t);
Mark Shannon8473cf82020-12-15 11:07:50 +0000233
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000234
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500235static int compiler_with(struct compiler *, stmt_ty, int);
Yury Selivanov75445082015-05-11 22:57:16 -0400236static int compiler_async_with(struct compiler *, stmt_ty, int);
237static int compiler_async_for(struct compiler *, stmt_ty);
Victor Stinner976bb402016-03-23 11:36:19 +0100238static int compiler_call_helper(struct compiler *c, int n,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100239 asdl_expr_seq *args,
240 asdl_keyword_seq *keywords);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500241static int compiler_try_except(struct compiler *, stmt_ty);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400242static int compiler_set_qualname(struct compiler *);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000243
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700244static int compiler_sync_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
250static int compiler_async_comprehension_generator(
251 struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100252 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +0200253 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700254 expr_ty elt, expr_ty val, int type);
255
Brandt Bucher145bf262021-02-26 14:51:55 -0800256static int compiler_pattern(struct compiler *, expr_ty, pattern_context *);
257static int compiler_match(struct compiler *, stmt_ty);
258static int compiler_pattern_subpattern(struct compiler *, expr_ty,
259 pattern_context *);
260
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000261static PyCodeObject *assemble(struct compiler *, int addNone);
Mark Shannon332cd5e2018-01-30 00:41:04 +0000262static PyObject *__doc__, *__annotations__;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000263
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400264#define CAPSULE_NAME "compile.c compiler unit"
Benjamin Petersonb173f782009-05-05 22:31:58 +0000265
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000266PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000267_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000268{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 /* Name mangling: __private becomes _classname__private.
270 This is independent from how the name is used. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200271 PyObject *result;
272 size_t nlen, plen, ipriv;
273 Py_UCS4 maxchar;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200275 PyUnicode_READ_CHAR(ident, 0) != '_' ||
276 PyUnicode_READ_CHAR(ident, 1) != '_') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 Py_INCREF(ident);
278 return ident;
279 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200280 nlen = PyUnicode_GET_LENGTH(ident);
281 plen = PyUnicode_GET_LENGTH(privateobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 /* Don't mangle __id__ or names with dots.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 The only time a name with a dot can occur is when
285 we are compiling an import statement that has a
286 package name.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 TODO(jhylton): Decide whether we want to support
289 mangling of the module name, e.g. __M.X.
290 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200291 if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
292 PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
293 PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 Py_INCREF(ident);
295 return ident; /* Don't mangle __whatever__ */
296 }
297 /* Strip leading underscores from class name */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200298 ipriv = 0;
299 while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
300 ipriv++;
301 if (ipriv == plen) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 Py_INCREF(ident);
303 return ident; /* Don't mangle if class is just underscores */
304 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200305 plen -= ipriv;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000306
Antoine Pitrou55bff892013-04-06 21:21:04 +0200307 if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
308 PyErr_SetString(PyExc_OverflowError,
309 "private identifier too large to be mangled");
310 return NULL;
311 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000312
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200313 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
314 if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
315 maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
316
317 result = PyUnicode_New(1 + nlen + plen, maxchar);
318 if (!result)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200320 /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
321 PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
Victor Stinner6c7a52a2011-09-28 21:39:17 +0200322 if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
323 Py_DECREF(result);
324 return NULL;
325 }
326 if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
327 Py_DECREF(result);
328 return NULL;
329 }
Victor Stinner8f825062012-04-27 13:55:39 +0200330 assert(_PyUnicode_CheckConsistency(result, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200331 return result;
Michael W. Hudson60934622004-08-12 17:56:29 +0000332}
333
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000334static int
335compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000338
INADA Naokic2e16072018-11-26 21:23:22 +0900339 c->c_const_cache = PyDict_New();
340 if (!c->c_const_cache) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 return 0;
INADA Naokic2e16072018-11-26 21:23:22 +0900342 }
343
344 c->c_stack = PyList_New(0);
345 if (!c->c_stack) {
346 Py_CLEAR(c->c_const_cache);
347 return 0;
348 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000351}
352
353PyCodeObject *
Victor Stinnera81fca62021-03-24 00:51:50 +0100354_PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
355 int optimize, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 struct compiler c;
358 PyCodeObject *co = NULL;
Victor Stinner37d66d72019-06-13 02:16:41 +0200359 PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 if (!__doc__) {
363 __doc__ = PyUnicode_InternFromString("__doc__");
364 if (!__doc__)
365 return NULL;
366 }
Mark Shannon332cd5e2018-01-30 00:41:04 +0000367 if (!__annotations__) {
368 __annotations__ = PyUnicode_InternFromString("__annotations__");
369 if (!__annotations__)
370 return NULL;
371 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 if (!compiler_init(&c))
373 return NULL;
Victor Stinner14e461d2013-08-26 22:28:21 +0200374 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 c.c_filename = filename;
376 c.c_arena = arena;
Victor Stinnera81fca62021-03-24 00:51:50 +0100377 c.c_future = _PyFuture_FromAST(mod, filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 if (c.c_future == NULL)
379 goto finally;
380 if (!flags) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 flags = &local_flags;
382 }
383 merged = c.c_future->ff_features | flags->cf_flags;
384 c.c_future->ff_features = merged;
385 flags->cf_flags = merged;
386 c.c_flags = flags;
Victor Stinnerda7933e2020-04-13 03:04:28 +0200387 c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000389
Pablo Galindod112c602020-03-18 23:02:09 +0000390 _PyASTOptimizeState state;
391 state.optimize = c.c_optimize;
392 state.ff_features = merged;
393
394 if (!_PyAST_Optimize(mod, arena, &state)) {
INADA Naoki7ea143a2017-12-14 16:47:20 +0900395 goto finally;
396 }
397
Victor Stinner28ad12f2021-03-19 12:41:49 +0100398 c.c_st = _PySymtable_Build(mod, filename, c.c_future);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 if (c.c_st == NULL) {
400 if (!PyErr_Occurred())
401 PyErr_SetString(PyExc_SystemError, "no symtable");
402 goto finally;
403 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000406
Thomas Wouters1175c432006-02-27 22:49:54 +0000407 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 compiler_free(&c);
409 assert(co || PyErr_Occurred());
410 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000411}
412
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000413static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000414compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 if (c->c_st)
Victor Stinner28ad12f2021-03-19 12:41:49 +0100417 _PySymtable_Free(c->c_st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 if (c->c_future)
419 PyObject_Free(c->c_future);
Victor Stinner14e461d2013-08-26 22:28:21 +0200420 Py_XDECREF(c->c_filename);
INADA Naokic2e16072018-11-26 21:23:22 +0900421 Py_DECREF(c->c_const_cache);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000423}
424
Guido van Rossum79f25d91997-04-29 20:08:16 +0000425static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000426list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 Py_ssize_t i, n;
429 PyObject *v, *k;
430 PyObject *dict = PyDict_New();
431 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 n = PyList_Size(list);
434 for (i = 0; i < n; i++) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100435 v = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 if (!v) {
437 Py_DECREF(dict);
438 return NULL;
439 }
440 k = PyList_GET_ITEM(list, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300441 if (PyDict_SetItem(dict, k, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 Py_DECREF(v);
443 Py_DECREF(dict);
444 return NULL;
445 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 Py_DECREF(v);
447 }
448 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000449}
450
451/* Return new dict containing names from src that match scope(s).
452
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000453src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000455values are integers, starting at offset and increasing by one for
456each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000457*/
458
459static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +0100460dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000461{
Benjamin Peterson51ab2832012-07-18 15:12:47 -0700462 Py_ssize_t i = offset, scope, num_keys, key_i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 PyObject *k, *v, *dest = PyDict_New();
Meador Inge2ca63152012-07-18 14:20:11 -0500464 PyObject *sorted_keys;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 assert(offset >= 0);
467 if (dest == NULL)
468 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000469
Meador Inge2ca63152012-07-18 14:20:11 -0500470 /* Sort the keys so that we have a deterministic order on the indexes
471 saved in the returned dictionary. These indexes are used as indexes
472 into the free and cell var storage. Therefore if they aren't
473 deterministic, then the generated bytecode is not deterministic.
474 */
475 sorted_keys = PyDict_Keys(src);
476 if (sorted_keys == NULL)
477 return NULL;
478 if (PyList_Sort(sorted_keys) != 0) {
479 Py_DECREF(sorted_keys);
480 return NULL;
481 }
Meador Ingef69e24e2012-07-18 16:41:03 -0500482 num_keys = PyList_GET_SIZE(sorted_keys);
Meador Inge2ca63152012-07-18 14:20:11 -0500483
484 for (key_i = 0; key_i < num_keys; key_i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 /* XXX this should probably be a macro in symtable.h */
486 long vi;
Meador Inge2ca63152012-07-18 14:20:11 -0500487 k = PyList_GET_ITEM(sorted_keys, key_i);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200488 v = PyDict_GetItemWithError(src, k);
489 assert(v && PyLong_Check(v));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 vi = PyLong_AS_LONG(v);
491 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 if (scope == scope_type || vi & flag) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300494 PyObject *item = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 if (item == NULL) {
Meador Inge2ca63152012-07-18 14:20:11 -0500496 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 Py_DECREF(dest);
498 return NULL;
499 }
500 i++;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300501 if (PyDict_SetItem(dest, k, item) < 0) {
Meador Inge2ca63152012-07-18 14:20:11 -0500502 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 Py_DECREF(item);
504 Py_DECREF(dest);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 return NULL;
506 }
507 Py_DECREF(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 }
509 }
Meador Inge2ca63152012-07-18 14:20:11 -0500510 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000512}
513
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000514static void
515compiler_unit_check(struct compiler_unit *u)
516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 basicblock *block;
518 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +0100519 assert(!_PyMem_IsPtrFreed(block));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 if (block->b_instr != NULL) {
521 assert(block->b_ialloc > 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +0100522 assert(block->b_iused >= 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 assert(block->b_ialloc >= block->b_iused);
524 }
525 else {
526 assert (block->b_iused == 0);
527 assert (block->b_ialloc == 0);
528 }
529 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000530}
531
532static void
533compiler_unit_free(struct compiler_unit *u)
534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 compiler_unit_check(u);
538 b = u->u_blocks;
539 while (b != NULL) {
540 if (b->b_instr)
541 PyObject_Free((void *)b->b_instr);
542 next = b->b_list;
543 PyObject_Free((void *)b);
544 b = next;
545 }
546 Py_CLEAR(u->u_ste);
547 Py_CLEAR(u->u_name);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400548 Py_CLEAR(u->u_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 Py_CLEAR(u->u_consts);
550 Py_CLEAR(u->u_names);
551 Py_CLEAR(u->u_varnames);
552 Py_CLEAR(u->u_freevars);
553 Py_CLEAR(u->u_cellvars);
554 Py_CLEAR(u->u_private);
555 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556}
557
558static int
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100559compiler_enter_scope(struct compiler *c, identifier name,
560 int scope_type, void *key, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 struct compiler_unit *u;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100563 basicblock *block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000564
Andy Lester7668a8b2020-03-24 23:26:44 -0500565 u = (struct compiler_unit *)PyObject_Calloc(1, sizeof(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 struct compiler_unit));
567 if (!u) {
568 PyErr_NoMemory();
569 return 0;
570 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100571 u->u_scope_type = scope_type;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 u->u_argcount = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100573 u->u_posonlyargcount = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 u->u_kwonlyargcount = 0;
575 u->u_ste = PySymtable_Lookup(c->c_st, key);
576 if (!u->u_ste) {
577 compiler_unit_free(u);
578 return 0;
579 }
580 Py_INCREF(name);
581 u->u_name = name;
582 u->u_varnames = list2dict(u->u_ste->ste_varnames);
583 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
584 if (!u->u_varnames || !u->u_cellvars) {
585 compiler_unit_free(u);
586 return 0;
587 }
Benjamin Peterson312595c2013-05-15 15:26:42 -0500588 if (u->u_ste->ste_needs_class_closure) {
Martin Panter7462b6492015-11-02 03:37:02 +0000589 /* Cook up an implicit __class__ cell. */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500590 _Py_IDENTIFIER(__class__);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300591 PyObject *name;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500592 int res;
593 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200594 assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500595 name = _PyUnicode_FromId(&PyId___class__);
596 if (!name) {
597 compiler_unit_free(u);
598 return 0;
599 }
Victor Stinnerc9bc2902020-10-27 02:24:34 +0100600 res = PyDict_SetItem(u->u_cellvars, name, _PyLong_GetZero());
Benjamin Peterson312595c2013-05-15 15:26:42 -0500601 if (res < 0) {
602 compiler_unit_free(u);
603 return 0;
604 }
605 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200608 PyDict_GET_SIZE(u->u_cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 if (!u->u_freevars) {
610 compiler_unit_free(u);
611 return 0;
612 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 u->u_blocks = NULL;
615 u->u_nfblocks = 0;
616 u->u_firstlineno = lineno;
617 u->u_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000618 u->u_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 u->u_consts = PyDict_New();
620 if (!u->u_consts) {
621 compiler_unit_free(u);
622 return 0;
623 }
624 u->u_names = PyDict_New();
625 if (!u->u_names) {
626 compiler_unit_free(u);
627 return 0;
628 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 /* Push the old compiler_unit on the stack. */
633 if (c->u) {
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400634 PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
636 Py_XDECREF(capsule);
637 compiler_unit_free(u);
638 return 0;
639 }
640 Py_DECREF(capsule);
641 u->u_private = c->u->u_private;
642 Py_XINCREF(u->u_private);
643 }
644 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 c->c_nestlevel++;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100647
648 block = compiler_new_block(c);
649 if (block == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 return 0;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100651 c->u->u_curblock = block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000652
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400653 if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
654 if (!compiler_set_qualname(c))
655 return 0;
656 }
657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000659}
660
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000661static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000662compiler_exit_scope(struct compiler *c)
663{
Victor Stinnera6192632021-01-29 16:53:03 +0100664 // Don't call PySequence_DelItem() with an exception raised
665 PyObject *exc_type, *exc_val, *exc_tb;
666 PyErr_Fetch(&exc_type, &exc_val, &exc_tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 c->c_nestlevel--;
669 compiler_unit_free(c->u);
670 /* Restore c->u to the parent unit. */
Victor Stinnera6192632021-01-29 16:53:03 +0100671 Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 if (n >= 0) {
Victor Stinnera6192632021-01-29 16:53:03 +0100673 PyObject *capsule = PyList_GET_ITEM(c->c_stack, n);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400674 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 assert(c->u);
676 /* we are deleting from a list so this really shouldn't fail */
Victor Stinnera6192632021-01-29 16:53:03 +0100677 if (PySequence_DelItem(c->c_stack, n) < 0) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +0100678 _PyErr_WriteUnraisableMsg("on removing the last compiler "
679 "stack item", NULL);
Victor Stinnera6192632021-01-29 16:53:03 +0100680 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 compiler_unit_check(c->u);
682 }
Victor Stinnera6192632021-01-29 16:53:03 +0100683 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 c->u = NULL;
Victor Stinnera6192632021-01-29 16:53:03 +0100685 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000686
Victor Stinnera6192632021-01-29 16:53:03 +0100687 PyErr_Restore(exc_type, exc_val, exc_tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000688}
689
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400690static int
691compiler_set_qualname(struct compiler *c)
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100692{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100693 _Py_static_string(dot, ".");
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400694 _Py_static_string(dot_locals, ".<locals>");
695 Py_ssize_t stack_size;
696 struct compiler_unit *u = c->u;
697 PyObject *name, *base, *dot_str, *dot_locals_str;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100698
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400699 base = NULL;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100700 stack_size = PyList_GET_SIZE(c->c_stack);
Benjamin Petersona8a38b82013-10-19 16:14:39 -0400701 assert(stack_size >= 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400702 if (stack_size > 1) {
703 int scope, force_global = 0;
704 struct compiler_unit *parent;
705 PyObject *mangled, *capsule;
706
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400707 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400708 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400709 assert(parent);
710
Yury Selivanov75445082015-05-11 22:57:16 -0400711 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
712 || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
713 || u->u_scope_type == COMPILER_SCOPE_CLASS) {
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400714 assert(u->u_name);
715 mangled = _Py_Mangle(parent->u_private, u->u_name);
716 if (!mangled)
717 return 0;
Victor Stinner28ad12f2021-03-19 12:41:49 +0100718 scope = _PyST_GetScope(parent->u_ste, mangled);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400719 Py_DECREF(mangled);
720 assert(scope != GLOBAL_IMPLICIT);
721 if (scope == GLOBAL_EXPLICIT)
722 force_global = 1;
723 }
724
725 if (!force_global) {
726 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
Yury Selivanov75445082015-05-11 22:57:16 -0400727 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400728 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
729 dot_locals_str = _PyUnicode_FromId(&dot_locals);
730 if (dot_locals_str == NULL)
731 return 0;
732 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
733 if (base == NULL)
734 return 0;
735 }
736 else {
737 Py_INCREF(parent->u_qualname);
738 base = parent->u_qualname;
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400739 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100740 }
741 }
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400742
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400743 if (base != NULL) {
744 dot_str = _PyUnicode_FromId(&dot);
745 if (dot_str == NULL) {
746 Py_DECREF(base);
747 return 0;
748 }
749 name = PyUnicode_Concat(base, dot_str);
750 Py_DECREF(base);
751 if (name == NULL)
752 return 0;
753 PyUnicode_Append(&name, u->u_name);
754 if (name == NULL)
755 return 0;
756 }
757 else {
758 Py_INCREF(u->u_name);
759 name = u->u_name;
760 }
761 u->u_qualname = name;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100762
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400763 return 1;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100764}
765
Eric V. Smith235a6f02015-09-19 14:51:32 -0400766
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767/* Allocate a new block and return a pointer to it.
768 Returns NULL on error.
769*/
770
771static basicblock *
772compiler_new_block(struct compiler *c)
773{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 basicblock *b;
775 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 u = c->u;
Andy Lester7668a8b2020-03-24 23:26:44 -0500778 b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 if (b == NULL) {
780 PyErr_NoMemory();
781 return NULL;
782 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 /* Extend the singly linked list of blocks with new block. */
784 b->b_list = u->u_blocks;
785 u->u_blocks = b;
786 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000787}
788
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789static basicblock *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790compiler_next_block(struct compiler *c)
791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 basicblock *block = compiler_new_block(c);
793 if (block == NULL)
794 return NULL;
795 c->u->u_curblock->b_next = block;
796 c->u->u_curblock = block;
797 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798}
799
800static basicblock *
801compiler_use_next_block(struct compiler *c, basicblock *block)
802{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 assert(block != NULL);
804 c->u->u_curblock->b_next = block;
805 c->u->u_curblock = block;
806 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807}
808
Mark Shannon5977a792020-12-02 13:31:40 +0000809static basicblock *
810compiler_copy_block(struct compiler *c, basicblock *block)
811{
812 /* Cannot copy a block if it has a fallthrough, since
813 * a block can only have one fallthrough predecessor.
814 */
815 assert(block->b_nofallthrough);
816 basicblock *result = compiler_next_block(c);
817 if (result == NULL) {
818 return NULL;
819 }
820 for (int i = 0; i < block->b_iused; i++) {
821 int n = compiler_next_instr(result);
822 if (n < 0) {
823 return NULL;
824 }
825 result->b_instr[n] = block->b_instr[i];
826 }
827 result->b_exit = block->b_exit;
Mark Shannon3bd60352021-01-13 12:05:43 +0000828 result->b_nofallthrough = 1;
Mark Shannon5977a792020-12-02 13:31:40 +0000829 return result;
830}
831
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832/* Returns the offset of the next instruction in the current block's
833 b_instr array. Resizes the b_instr as necessary.
834 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000835*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836
837static int
Andy Lester76d58772020-03-10 21:18:12 -0500838compiler_next_instr(basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 assert(b != NULL);
841 if (b->b_instr == NULL) {
Andy Lester7668a8b2020-03-24 23:26:44 -0500842 b->b_instr = (struct instr *)PyObject_Calloc(
843 DEFAULT_BLOCK_SIZE, sizeof(struct instr));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 if (b->b_instr == NULL) {
845 PyErr_NoMemory();
846 return -1;
847 }
848 b->b_ialloc = DEFAULT_BLOCK_SIZE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 }
850 else if (b->b_iused == b->b_ialloc) {
851 struct instr *tmp;
852 size_t oldsize, newsize;
853 oldsize = b->b_ialloc * sizeof(struct instr);
854 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000855
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -0700856 if (oldsize > (SIZE_MAX >> 1)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 PyErr_NoMemory();
858 return -1;
859 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 if (newsize == 0) {
862 PyErr_NoMemory();
863 return -1;
864 }
865 b->b_ialloc <<= 1;
866 tmp = (struct instr *)PyObject_Realloc(
867 (void *)b->b_instr, newsize);
868 if (tmp == NULL) {
869 PyErr_NoMemory();
870 return -1;
871 }
872 b->b_instr = tmp;
873 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
874 }
875 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000876}
877
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200878/* Set the line number and column offset for the following instructions.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000879
Christian Heimes2202f872008-02-06 14:31:34 +0000880 The line number is reset in the following cases:
881 - when entering a new scope
882 - on each statement
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200883 - on each expression and sub-expression
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200884 - before the "except" and "finally" clauses
Thomas Wouters89f507f2006-12-13 04:49:30 +0000885*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000886
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200887#define SET_LOC(c, x) \
888 (c)->u->u_lineno = (x)->lineno; \
889 (c)->u->u_col_offset = (x)->col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000890
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200891/* Return the stack effect of opcode with argument oparg.
892
893 Some opcodes have different stack effect when jump to the target and
894 when not jump. The 'jump' parameter specifies the case:
895
896 * 0 -- when not jump
897 * 1 -- when jump
898 * -1 -- maximal
899 */
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200900static int
901stack_effect(int opcode, int oparg, int jump)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000902{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 switch (opcode) {
Serhiy Storchaka57faf342018-04-25 22:04:06 +0300904 case NOP:
905 case EXTENDED_ARG:
906 return 0;
907
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200908 /* Stack manipulation */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 case POP_TOP:
910 return -1;
911 case ROT_TWO:
912 case ROT_THREE:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200913 case ROT_FOUR:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 return 0;
915 case DUP_TOP:
916 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000917 case DUP_TOP_TWO:
918 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000919
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200920 /* Unary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 case UNARY_POSITIVE:
922 case UNARY_NEGATIVE:
923 case UNARY_NOT:
924 case UNARY_INVERT:
925 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 case SET_ADD:
928 case LIST_APPEND:
929 return -1;
930 case MAP_ADD:
931 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000932
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200933 /* Binary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 case BINARY_POWER:
935 case BINARY_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400936 case BINARY_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 case BINARY_MODULO:
938 case BINARY_ADD:
939 case BINARY_SUBTRACT:
940 case BINARY_SUBSCR:
941 case BINARY_FLOOR_DIVIDE:
942 case BINARY_TRUE_DIVIDE:
943 return -1;
944 case INPLACE_FLOOR_DIVIDE:
945 case INPLACE_TRUE_DIVIDE:
946 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 case INPLACE_ADD:
949 case INPLACE_SUBTRACT:
950 case INPLACE_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400951 case INPLACE_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 case INPLACE_MODULO:
953 return -1;
954 case STORE_SUBSCR:
955 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 case DELETE_SUBSCR:
957 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 case BINARY_LSHIFT:
960 case BINARY_RSHIFT:
961 case BINARY_AND:
962 case BINARY_XOR:
963 case BINARY_OR:
964 return -1;
965 case INPLACE_POWER:
966 return -1;
967 case GET_ITER:
968 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 case PRINT_EXPR:
971 return -1;
972 case LOAD_BUILD_CLASS:
973 return 1;
974 case INPLACE_LSHIFT:
975 case INPLACE_RSHIFT:
976 case INPLACE_AND:
977 case INPLACE_XOR:
978 case INPLACE_OR:
979 return -1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 case SETUP_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200982 /* 1 in the normal flow.
983 * Restore the stack position and push 6 values before jumping to
984 * the handler if an exception be raised. */
985 return jump ? 6 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 case RETURN_VALUE:
987 return -1;
988 case IMPORT_STAR:
989 return -1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700990 case SETUP_ANNOTATIONS:
991 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 case YIELD_VALUE:
993 return 0;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500994 case YIELD_FROM:
995 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 case POP_BLOCK:
997 return 0;
998 case POP_EXCEPT:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200999 return -3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 case STORE_NAME:
1002 return -1;
1003 case DELETE_NAME:
1004 return 0;
1005 case UNPACK_SEQUENCE:
1006 return oparg-1;
1007 case UNPACK_EX:
1008 return (oparg&0xFF) + (oparg>>8);
1009 case FOR_ITER:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001010 /* -1 at end of iterator, 1 if continue iterating. */
1011 return jump > 0 ? -1 : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 case STORE_ATTR:
1014 return -2;
1015 case DELETE_ATTR:
1016 return -1;
1017 case STORE_GLOBAL:
1018 return -1;
1019 case DELETE_GLOBAL:
1020 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 case LOAD_CONST:
1022 return 1;
1023 case LOAD_NAME:
1024 return 1;
1025 case BUILD_TUPLE:
1026 case BUILD_LIST:
1027 case BUILD_SET:
Serhiy Storchakaea525a22016-09-06 22:07:53 +03001028 case BUILD_STRING:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 return 1-oparg;
1030 case BUILD_MAP:
Benjamin Petersonb6855152015-09-10 21:02:39 -07001031 return 1 - 2*oparg;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001032 case BUILD_CONST_KEY_MAP:
1033 return -oparg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 case LOAD_ATTR:
1035 return 0;
1036 case COMPARE_OP:
Mark Shannon9af0e472020-01-14 10:12:45 +00001037 case IS_OP:
1038 case CONTAINS_OP:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 return -1;
Mark Shannon9af0e472020-01-14 10:12:45 +00001040 case JUMP_IF_NOT_EXC_MATCH:
1041 return -2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 case IMPORT_NAME:
1043 return -1;
1044 case IMPORT_FROM:
1045 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001046
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001047 /* Jumps */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 case JUMP_FORWARD:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 case JUMP_ABSOLUTE:
1050 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001051
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001052 case JUMP_IF_TRUE_OR_POP:
1053 case JUMP_IF_FALSE_OR_POP:
1054 return jump ? 0 : -1;
1055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 case POP_JUMP_IF_FALSE:
1057 case POP_JUMP_IF_TRUE:
1058 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 case LOAD_GLOBAL:
1061 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001063 /* Exception handling */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 case SETUP_FINALLY:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001065 /* 0 in the normal flow.
1066 * Restore the stack position and push 6 values before jumping to
1067 * the handler if an exception be raised. */
1068 return jump ? 6 : 0;
Mark Shannonfee55262019-11-21 09:11:43 +00001069 case RERAISE:
1070 return -3;
1071
1072 case WITH_EXCEPT_START:
1073 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 case LOAD_FAST:
1076 return 1;
1077 case STORE_FAST:
1078 return -1;
1079 case DELETE_FAST:
1080 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 case RAISE_VARARGS:
1083 return -oparg;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001084
1085 /* Functions and calls */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 case CALL_FUNCTION:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001087 return -oparg;
Yury Selivanovf2392132016-12-13 19:03:51 -05001088 case CALL_METHOD:
1089 return -oparg-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 case CALL_FUNCTION_KW:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001091 return -oparg-1;
1092 case CALL_FUNCTION_EX:
Matthieu Dartiailh3a9ac822017-02-21 14:25:22 +01001093 return -1 - ((oparg & 0x01) != 0);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001094 case MAKE_FUNCTION:
1095 return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1096 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 case BUILD_SLICE:
1098 if (oparg == 3)
1099 return -2;
1100 else
1101 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001102
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001103 /* Closures */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 case LOAD_CLOSURE:
1105 return 1;
1106 case LOAD_DEREF:
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001107 case LOAD_CLASSDEREF:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 return 1;
1109 case STORE_DEREF:
1110 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001111 case DELETE_DEREF:
1112 return 0;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001113
1114 /* Iterators and generators */
Yury Selivanov75445082015-05-11 22:57:16 -04001115 case GET_AWAITABLE:
1116 return 0;
1117 case SETUP_ASYNC_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001118 /* 0 in the normal flow.
1119 * Restore the stack position to the position before the result
1120 * of __aenter__ and push 6 values before jumping to the handler
1121 * if an exception be raised. */
1122 return jump ? -1 + 6 : 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001123 case BEFORE_ASYNC_WITH:
1124 return 1;
1125 case GET_AITER:
1126 return 0;
1127 case GET_ANEXT:
1128 return 1;
Yury Selivanov5376ba92015-06-22 12:19:30 -04001129 case GET_YIELD_FROM_ITER:
1130 return 0;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02001131 case END_ASYNC_FOR:
1132 return -7;
Eric V. Smitha78c7952015-11-03 12:45:05 -05001133 case FORMAT_VALUE:
1134 /* If there's a fmt_spec on the stack, we go from 2->1,
1135 else 1->1. */
1136 return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
Yury Selivanovf2392132016-12-13 19:03:51 -05001137 case LOAD_METHOD:
1138 return 1;
Zackery Spytzce6a0702019-08-25 03:44:09 -06001139 case LOAD_ASSERTION_ERROR:
1140 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001141 case LIST_TO_TUPLE:
1142 return 0;
1143 case LIST_EXTEND:
1144 case SET_UPDATE:
Mark Shannon8a4cd702020-01-27 09:57:45 +00001145 case DICT_MERGE:
1146 case DICT_UPDATE:
Mark Shannon13bc1392020-01-23 09:25:17 +00001147 return -1;
Brandt Bucher145bf262021-02-26 14:51:55 -08001148 case COPY_DICT_WITHOUT_KEYS:
1149 return 0;
1150 case MATCH_CLASS:
1151 return -1;
1152 case GET_LEN:
1153 case MATCH_MAPPING:
1154 case MATCH_SEQUENCE:
1155 return 1;
1156 case MATCH_KEYS:
1157 return 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 default:
Larry Hastings3a907972013-11-23 14:49:22 -08001159 return PY_INVALID_STACK_EFFECT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 }
Larry Hastings3a907972013-11-23 14:49:22 -08001161 return PY_INVALID_STACK_EFFECT; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001162}
1163
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001164int
Serhiy Storchaka7bdf2822018-09-18 09:54:26 +03001165PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump)
1166{
1167 return stack_effect(opcode, oparg, jump);
1168}
1169
1170int
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001171PyCompile_OpcodeStackEffect(int opcode, int oparg)
1172{
1173 return stack_effect(opcode, oparg, -1);
1174}
1175
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176/* Add an opcode with no argument.
1177 Returns 0 on failure, 1 on success.
1178*/
1179
1180static int
Mark Shannon3bd60352021-01-13 12:05:43 +00001181compiler_addop_line(struct compiler *c, int opcode, int line)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 basicblock *b;
1184 struct instr *i;
1185 int off;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001186 assert(!HAS_ARG(opcode));
Andy Lester76d58772020-03-10 21:18:12 -05001187 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 if (off < 0)
1189 return 0;
1190 b = c->u->u_curblock;
1191 i = &b->b_instr[off];
1192 i->i_opcode = opcode;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001193 i->i_oparg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 if (opcode == RETURN_VALUE)
1195 b->b_return = 1;
Mark Shannon3bd60352021-01-13 12:05:43 +00001196 i->i_lineno = line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198}
1199
Mark Shannon3bd60352021-01-13 12:05:43 +00001200static int
1201compiler_addop(struct compiler *c, int opcode)
1202{
1203 return compiler_addop_line(c, opcode, c->u->u_lineno);
1204}
1205
1206static int
1207compiler_addop_noline(struct compiler *c, int opcode)
1208{
1209 return compiler_addop_line(c, opcode, -1);
1210}
1211
1212
Victor Stinnerf8e32212013-11-19 23:56:34 +01001213static Py_ssize_t
Andy Lester76d58772020-03-10 21:18:12 -05001214compiler_add_o(PyObject *dict, PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001216 PyObject *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001218
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001219 v = PyDict_GetItemWithError(dict, o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 if (!v) {
Stefan Krahc0cbed12015-07-27 12:56:49 +02001221 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 return -1;
Stefan Krahc0cbed12015-07-27 12:56:49 +02001223 }
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001224 arg = PyDict_GET_SIZE(dict);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001225 v = PyLong_FromSsize_t(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 if (!v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 return -1;
1228 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001229 if (PyDict_SetItem(dict, o, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 Py_DECREF(v);
1231 return -1;
1232 }
1233 Py_DECREF(v);
1234 }
1235 else
1236 arg = PyLong_AsLong(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001237 return arg;
1238}
1239
INADA Naokic2e16072018-11-26 21:23:22 +09001240// Merge const *o* recursively and return constant key object.
1241static PyObject*
1242merge_consts_recursive(struct compiler *c, PyObject *o)
1243{
1244 // None and Ellipsis are singleton, and key is the singleton.
1245 // No need to merge object and key.
1246 if (o == Py_None || o == Py_Ellipsis) {
1247 Py_INCREF(o);
1248 return o;
1249 }
1250
1251 PyObject *key = _PyCode_ConstantKey(o);
1252 if (key == NULL) {
1253 return NULL;
1254 }
1255
1256 // t is borrowed reference
1257 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
1258 if (t != key) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001259 // o is registered in c_const_cache. Just use it.
Zackery Spytz9b4a1b12019-03-20 03:16:25 -06001260 Py_XINCREF(t);
INADA Naokic2e16072018-11-26 21:23:22 +09001261 Py_DECREF(key);
1262 return t;
1263 }
1264
INADA Naokif7e4d362018-11-29 00:58:46 +09001265 // We registered o in c_const_cache.
Simeon63b5fc52019-04-09 19:36:57 -04001266 // When o is a tuple or frozenset, we want to merge its
INADA Naokif7e4d362018-11-29 00:58:46 +09001267 // items too.
INADA Naokic2e16072018-11-26 21:23:22 +09001268 if (PyTuple_CheckExact(o)) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001269 Py_ssize_t len = PyTuple_GET_SIZE(o);
1270 for (Py_ssize_t i = 0; i < len; i++) {
INADA Naokic2e16072018-11-26 21:23:22 +09001271 PyObject *item = PyTuple_GET_ITEM(o, i);
1272 PyObject *u = merge_consts_recursive(c, item);
1273 if (u == NULL) {
1274 Py_DECREF(key);
1275 return NULL;
1276 }
1277
1278 // See _PyCode_ConstantKey()
1279 PyObject *v; // borrowed
1280 if (PyTuple_CheckExact(u)) {
1281 v = PyTuple_GET_ITEM(u, 1);
1282 }
1283 else {
1284 v = u;
1285 }
1286 if (v != item) {
1287 Py_INCREF(v);
1288 PyTuple_SET_ITEM(o, i, v);
1289 Py_DECREF(item);
1290 }
1291
1292 Py_DECREF(u);
1293 }
1294 }
INADA Naokif7e4d362018-11-29 00:58:46 +09001295 else if (PyFrozenSet_CheckExact(o)) {
Simeon63b5fc52019-04-09 19:36:57 -04001296 // *key* is tuple. And its first item is frozenset of
INADA Naokif7e4d362018-11-29 00:58:46 +09001297 // constant keys.
1298 // See _PyCode_ConstantKey() for detail.
1299 assert(PyTuple_CheckExact(key));
1300 assert(PyTuple_GET_SIZE(key) == 2);
1301
1302 Py_ssize_t len = PySet_GET_SIZE(o);
1303 if (len == 0) { // empty frozenset should not be re-created.
1304 return key;
1305 }
1306 PyObject *tuple = PyTuple_New(len);
1307 if (tuple == NULL) {
1308 Py_DECREF(key);
1309 return NULL;
1310 }
1311 Py_ssize_t i = 0, pos = 0;
1312 PyObject *item;
1313 Py_hash_t hash;
1314 while (_PySet_NextEntry(o, &pos, &item, &hash)) {
1315 PyObject *k = merge_consts_recursive(c, item);
1316 if (k == NULL) {
1317 Py_DECREF(tuple);
1318 Py_DECREF(key);
1319 return NULL;
1320 }
1321 PyObject *u;
1322 if (PyTuple_CheckExact(k)) {
1323 u = PyTuple_GET_ITEM(k, 1);
1324 Py_INCREF(u);
1325 Py_DECREF(k);
1326 }
1327 else {
1328 u = k;
1329 }
1330 PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u.
1331 i++;
1332 }
1333
1334 // Instead of rewriting o, we create new frozenset and embed in the
1335 // key tuple. Caller should get merged frozenset from the key tuple.
1336 PyObject *new = PyFrozenSet_New(tuple);
1337 Py_DECREF(tuple);
1338 if (new == NULL) {
1339 Py_DECREF(key);
1340 return NULL;
1341 }
1342 assert(PyTuple_GET_ITEM(key, 1) == o);
1343 Py_DECREF(o);
1344 PyTuple_SET_ITEM(key, 1, new);
1345 }
INADA Naokic2e16072018-11-26 21:23:22 +09001346
1347 return key;
1348}
1349
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001350static Py_ssize_t
1351compiler_add_const(struct compiler *c, PyObject *o)
1352{
INADA Naokic2e16072018-11-26 21:23:22 +09001353 PyObject *key = merge_consts_recursive(c, o);
1354 if (key == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001355 return -1;
INADA Naokic2e16072018-11-26 21:23:22 +09001356 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001357
Andy Lester76d58772020-03-10 21:18:12 -05001358 Py_ssize_t arg = compiler_add_o(c->u->u_consts, key);
INADA Naokic2e16072018-11-26 21:23:22 +09001359 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001361}
1362
1363static int
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001364compiler_addop_load_const(struct compiler *c, PyObject *o)
1365{
1366 Py_ssize_t arg = compiler_add_const(c, o);
1367 if (arg < 0)
1368 return 0;
1369 return compiler_addop_i(c, LOAD_CONST, arg);
1370}
1371
1372static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001373compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375{
Andy Lester76d58772020-03-10 21:18:12 -05001376 Py_ssize_t arg = compiler_add_o(dict, o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001377 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001378 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001379 return compiler_addop_i(c, opcode, arg);
1380}
1381
1382static int
1383compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001386 Py_ssize_t arg;
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001387
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1389 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001390 return 0;
Andy Lester76d58772020-03-10 21:18:12 -05001391 arg = compiler_add_o(dict, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001392 Py_DECREF(mangled);
1393 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001394 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001395 return compiler_addop_i(c, opcode, arg);
1396}
1397
1398/* Add an opcode with an integer argument.
1399 Returns 0 on failure, 1 on success.
1400*/
1401
1402static int
Victor Stinnerf8e32212013-11-19 23:56:34 +01001403compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001404{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 struct instr *i;
1406 int off;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001407
Victor Stinner2ad474b2016-03-01 23:34:47 +01001408 /* oparg value is unsigned, but a signed C int is usually used to store
1409 it in the C code (like Python/ceval.c).
1410
1411 Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1412
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001413 The argument of a concrete bytecode instruction is limited to 8-bit.
1414 EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1415 assert(HAS_ARG(opcode));
Victor Stinner2ad474b2016-03-01 23:34:47 +01001416 assert(0 <= oparg && oparg <= 2147483647);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001417
Andy Lester76d58772020-03-10 21:18:12 -05001418 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 if (off < 0)
1420 return 0;
1421 i = &c->u->u_curblock->b_instr[off];
Victor Stinnerf8e32212013-11-19 23:56:34 +01001422 i->i_opcode = opcode;
1423 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001424 i->i_lineno = c->u->u_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426}
1427
Mark Shannon28b75c82020-12-23 11:43:10 +00001428static int add_jump_to_block(basicblock *b, int opcode, int lineno, basicblock *target)
1429{
1430 assert(HAS_ARG(opcode));
1431 assert(b != NULL);
1432 assert(target != NULL);
1433
1434 int off = compiler_next_instr(b);
1435 struct instr *i = &b->b_instr[off];
1436 if (off < 0) {
1437 return 0;
1438 }
1439 i->i_opcode = opcode;
1440 i->i_target = target;
1441 i->i_lineno = lineno;
1442 return 1;
1443}
1444
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001445static int
Mark Shannon582aaf12020-08-04 17:30:11 +01001446compiler_addop_j(struct compiler *c, int opcode, basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447{
Mark Shannon28b75c82020-12-23 11:43:10 +00001448 return add_jump_to_block(c->u->u_curblock, opcode, c->u->u_lineno, b);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449}
1450
Mark Shannon127dde52021-01-04 18:06:55 +00001451static int
1452compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b)
1453{
1454 return add_jump_to_block(c->u->u_curblock, opcode, -1, b);
1455}
1456
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +01001457/* NEXT_BLOCK() creates an implicit jump from the current block
1458 to the new block.
1459
1460 The returns inside this macro make it impossible to decref objects
1461 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 if (compiler_next_block((C)) == NULL) \
1465 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001466}
1467
1468#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 if (!compiler_addop((C), (OP))) \
1470 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471}
1472
Mark Shannon3bd60352021-01-13 12:05:43 +00001473#define ADDOP_NOLINE(C, OP) { \
1474 if (!compiler_addop_noline((C), (OP))) \
1475 return 0; \
1476}
1477
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001478#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 if (!compiler_addop((C), (OP))) { \
1480 compiler_exit_scope(c); \
1481 return 0; \
1482 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001483}
1484
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001485#define ADDOP_LOAD_CONST(C, O) { \
1486 if (!compiler_addop_load_const((C), (O))) \
1487 return 0; \
1488}
1489
1490/* Same as ADDOP_LOAD_CONST, but steals a reference. */
1491#define ADDOP_LOAD_CONST_NEW(C, O) { \
1492 PyObject *__new_const = (O); \
1493 if (__new_const == NULL) { \
1494 return 0; \
1495 } \
1496 if (!compiler_addop_load_const((C), __new_const)) { \
1497 Py_DECREF(__new_const); \
1498 return 0; \
1499 } \
1500 Py_DECREF(__new_const); \
1501}
1502
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1505 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506}
1507
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001508/* Same as ADDOP_O, but steals a reference. */
1509#define ADDOP_N(C, OP, O, TYPE) { \
1510 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1511 Py_DECREF((O)); \
1512 return 0; \
1513 } \
1514 Py_DECREF((O)); \
1515}
1516
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1519 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520}
1521
1522#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 if (!compiler_addop_i((C), (OP), (O))) \
1524 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525}
1526
Mark Shannon582aaf12020-08-04 17:30:11 +01001527#define ADDOP_JUMP(C, OP, O) { \
1528 if (!compiler_addop_j((C), (OP), (O))) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001530}
1531
Mark Shannon127dde52021-01-04 18:06:55 +00001532/* Add a jump with no line number.
1533 * Used for artificial jumps that have no corresponding
1534 * token in the source code. */
1535#define ADDOP_JUMP_NOLINE(C, OP, O) { \
1536 if (!compiler_addop_j_noline((C), (OP), (O))) \
1537 return 0; \
1538}
1539
Mark Shannon9af0e472020-01-14 10:12:45 +00001540#define ADDOP_COMPARE(C, CMP) { \
1541 if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \
1542 return 0; \
1543}
1544
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1546 the ASDL name to synthesize the name of the C type and the visit function.
1547*/
1548
1549#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 if (!compiler_visit_ ## TYPE((C), (V))) \
1551 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001552}
1553
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001554#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 if (!compiler_visit_ ## TYPE((C), (V))) { \
1556 compiler_exit_scope(c); \
1557 return 0; \
1558 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001559}
1560
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001561#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 if (!compiler_visit_slice((C), (V), (CTX))) \
1563 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564}
1565
1566#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001568 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1570 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1571 if (!compiler_visit_ ## TYPE((C), elt)) \
1572 return 0; \
1573 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001574}
1575
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001576#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001578 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1580 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1581 if (!compiler_visit_ ## TYPE((C), elt)) { \
1582 compiler_exit_scope(c); \
1583 return 0; \
1584 } \
1585 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001586}
1587
Brandt Bucher145bf262021-02-26 14:51:55 -08001588#define RETURN_IF_FALSE(X) \
1589 if (!(X)) { \
1590 return 0; \
1591 }
1592
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001593/* Search if variable annotations are present statically in a block. */
1594
1595static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001596find_ann(asdl_stmt_seq *stmts)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001597{
1598 int i, j, res = 0;
1599 stmt_ty st;
1600
1601 for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1602 st = (stmt_ty)asdl_seq_GET(stmts, i);
1603 switch (st->kind) {
1604 case AnnAssign_kind:
1605 return 1;
1606 case For_kind:
1607 res = find_ann(st->v.For.body) ||
1608 find_ann(st->v.For.orelse);
1609 break;
1610 case AsyncFor_kind:
1611 res = find_ann(st->v.AsyncFor.body) ||
1612 find_ann(st->v.AsyncFor.orelse);
1613 break;
1614 case While_kind:
1615 res = find_ann(st->v.While.body) ||
1616 find_ann(st->v.While.orelse);
1617 break;
1618 case If_kind:
1619 res = find_ann(st->v.If.body) ||
1620 find_ann(st->v.If.orelse);
1621 break;
1622 case With_kind:
1623 res = find_ann(st->v.With.body);
1624 break;
1625 case AsyncWith_kind:
1626 res = find_ann(st->v.AsyncWith.body);
1627 break;
1628 case Try_kind:
1629 for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1630 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1631 st->v.Try.handlers, j);
1632 if (find_ann(handler->v.ExceptHandler.body)) {
1633 return 1;
1634 }
1635 }
1636 res = find_ann(st->v.Try.body) ||
1637 find_ann(st->v.Try.finalbody) ||
1638 find_ann(st->v.Try.orelse);
1639 break;
1640 default:
1641 res = 0;
1642 }
1643 if (res) {
1644 break;
1645 }
1646 }
1647 return res;
1648}
1649
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001650/*
1651 * Frame block handling functions
1652 */
1653
1654static int
1655compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b,
Mark Shannonfee55262019-11-21 09:11:43 +00001656 basicblock *exit, void *datum)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001657{
1658 struct fblockinfo *f;
1659 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
Mark Shannon02d126a2020-09-25 14:04:19 +01001660 return compiler_error(c, "too many statically nested blocks");
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001661 }
1662 f = &c->u->u_fblock[c->u->u_nfblocks++];
1663 f->fb_type = t;
1664 f->fb_block = b;
1665 f->fb_exit = exit;
Mark Shannonfee55262019-11-21 09:11:43 +00001666 f->fb_datum = datum;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001667 return 1;
1668}
1669
1670static void
1671compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
1672{
1673 struct compiler_unit *u = c->u;
1674 assert(u->u_nfblocks > 0);
1675 u->u_nfblocks--;
1676 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
1677 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
1678}
1679
Mark Shannonfee55262019-11-21 09:11:43 +00001680static int
1681compiler_call_exit_with_nones(struct compiler *c) {
1682 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1683 ADDOP(c, DUP_TOP);
1684 ADDOP(c, DUP_TOP);
1685 ADDOP_I(c, CALL_FUNCTION, 3);
1686 return 1;
1687}
1688
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001689/* Unwind a frame block. If preserve_tos is true, the TOS before
Mark Shannonfee55262019-11-21 09:11:43 +00001690 * popping the blocks will be restored afterwards, unless another
1691 * return, break or continue is found. In which case, the TOS will
1692 * be popped.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001693 */
1694static int
1695compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
1696 int preserve_tos)
1697{
1698 switch (info->fb_type) {
1699 case WHILE_LOOP:
Mark Shannon02d126a2020-09-25 14:04:19 +01001700 case EXCEPTION_HANDLER:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001701 return 1;
1702
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001703 case FOR_LOOP:
1704 /* Pop the iterator */
1705 if (preserve_tos) {
1706 ADDOP(c, ROT_TWO);
1707 }
1708 ADDOP(c, POP_TOP);
1709 return 1;
1710
Mark Shannon02d126a2020-09-25 14:04:19 +01001711 case TRY_EXCEPT:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001712 ADDOP(c, POP_BLOCK);
1713 return 1;
1714
1715 case FINALLY_TRY:
Mark Shannon5274b682020-12-16 13:07:01 +00001716 /* This POP_BLOCK gets the line number of the unwinding statement */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001717 ADDOP(c, POP_BLOCK);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001718 if (preserve_tos) {
Mark Shannonfee55262019-11-21 09:11:43 +00001719 if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) {
1720 return 0;
1721 }
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001722 }
Mark Shannon5274b682020-12-16 13:07:01 +00001723 /* Emit the finally block */
Mark Shannonfee55262019-11-21 09:11:43 +00001724 VISIT_SEQ(c, stmt, info->fb_datum);
1725 if (preserve_tos) {
1726 compiler_pop_fblock(c, POP_VALUE, NULL);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001727 }
Mark Shannon5274b682020-12-16 13:07:01 +00001728 /* The finally block should appear to execute after the
1729 * statement causing the unwinding, so make the unwinding
1730 * instruction artificial */
1731 c->u->u_lineno = -1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001732 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001733
Mark Shannonfee55262019-11-21 09:11:43 +00001734 case FINALLY_END:
1735 if (preserve_tos) {
1736 ADDOP(c, ROT_FOUR);
1737 }
1738 ADDOP(c, POP_TOP);
1739 ADDOP(c, POP_TOP);
1740 ADDOP(c, POP_TOP);
1741 if (preserve_tos) {
1742 ADDOP(c, ROT_FOUR);
1743 }
1744 ADDOP(c, POP_EXCEPT);
1745 return 1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001746
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001747 case WITH:
1748 case ASYNC_WITH:
1749 ADDOP(c, POP_BLOCK);
1750 if (preserve_tos) {
1751 ADDOP(c, ROT_TWO);
1752 }
Mark Shannonfee55262019-11-21 09:11:43 +00001753 if(!compiler_call_exit_with_nones(c)) {
1754 return 0;
1755 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001756 if (info->fb_type == ASYNC_WITH) {
1757 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001758 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001759 ADDOP(c, YIELD_FROM);
1760 }
Mark Shannonfee55262019-11-21 09:11:43 +00001761 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001762 return 1;
1763
1764 case HANDLER_CLEANUP:
Mark Shannonfee55262019-11-21 09:11:43 +00001765 if (info->fb_datum) {
1766 ADDOP(c, POP_BLOCK);
1767 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001768 if (preserve_tos) {
1769 ADDOP(c, ROT_FOUR);
1770 }
Mark Shannonfee55262019-11-21 09:11:43 +00001771 ADDOP(c, POP_EXCEPT);
1772 if (info->fb_datum) {
1773 ADDOP_LOAD_CONST(c, Py_None);
1774 compiler_nameop(c, info->fb_datum, Store);
1775 compiler_nameop(c, info->fb_datum, Del);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001776 }
Mark Shannonfee55262019-11-21 09:11:43 +00001777 return 1;
1778
1779 case POP_VALUE:
1780 if (preserve_tos) {
1781 ADDOP(c, ROT_TWO);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001782 }
Mark Shannonfee55262019-11-21 09:11:43 +00001783 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001784 return 1;
1785 }
1786 Py_UNREACHABLE();
1787}
1788
Mark Shannonfee55262019-11-21 09:11:43 +00001789/** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */
1790static int
1791compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) {
1792 if (c->u->u_nfblocks == 0) {
1793 return 1;
1794 }
1795 struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1];
1796 if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) {
1797 *loop = top;
1798 return 1;
1799 }
1800 struct fblockinfo copy = *top;
1801 c->u->u_nfblocks--;
1802 if (!compiler_unwind_fblock(c, &copy, preserve_tos)) {
1803 return 0;
1804 }
1805 if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) {
1806 return 0;
1807 }
1808 c->u->u_fblock[c->u->u_nfblocks] = copy;
1809 c->u->u_nfblocks++;
1810 return 1;
1811}
1812
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001813/* Compile a sequence of statements, checking for a docstring
1814 and for annotations. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001815
1816static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001817compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001819 int i = 0;
1820 stmt_ty st;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001821 PyObject *docstring;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001822
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001823 /* Set current line number to the line number of first statement.
1824 This way line number for SETUP_ANNOTATIONS will always
1825 coincide with the line number of first "real" statement in module.
Hansraj Das01171eb2019-10-09 07:54:02 +05301826 If body is empty, then lineno will be set later in assemble. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001827 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) {
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001828 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001829 SET_LOC(c, st);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001830 }
1831 /* Every annotated class and module should have __annotations__. */
1832 if (find_ann(stmts)) {
1833 ADDOP(c, SETUP_ANNOTATIONS);
1834 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001835 if (!asdl_seq_LEN(stmts))
1836 return 1;
INADA Naokicb41b272017-02-23 00:31:59 +09001837 /* if not -OO mode, set docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001838 if (c->c_optimize < 2) {
1839 docstring = _PyAST_GetDocString(stmts);
1840 if (docstring) {
1841 i = 1;
1842 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1843 assert(st->kind == Expr_kind);
1844 VISIT(c, expr, st->v.Expr.value);
1845 if (!compiler_nameop(c, __doc__, Store))
1846 return 0;
1847 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001849 for (; i < asdl_seq_LEN(stmts); i++)
1850 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001852}
1853
1854static PyCodeObject *
1855compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001856{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 PyCodeObject *co;
1858 int addNone = 1;
1859 static PyObject *module;
1860 if (!module) {
1861 module = PyUnicode_InternFromString("<module>");
1862 if (!module)
1863 return NULL;
1864 }
1865 /* Use 0 for firstlineno initially, will fixup in assemble(). */
Mark Shannon877df852020-11-12 09:43:29 +00001866 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 return NULL;
1868 switch (mod->kind) {
1869 case Module_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001870 if (!compiler_body(c, mod->v.Module.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 compiler_exit_scope(c);
1872 return 0;
1873 }
1874 break;
1875 case Interactive_kind:
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001876 if (find_ann(mod->v.Interactive.body)) {
1877 ADDOP(c, SETUP_ANNOTATIONS);
1878 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 c->c_interactive = 1;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001880 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 break;
1882 case Expression_kind:
1883 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1884 addNone = 0;
1885 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 default:
1887 PyErr_Format(PyExc_SystemError,
1888 "module kind %d should not be possible",
1889 mod->kind);
1890 return 0;
1891 }
1892 co = assemble(c, addNone);
1893 compiler_exit_scope(c);
1894 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001895}
1896
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897/* The test for LOCAL must come before the test for FREE in order to
1898 handle classes where name is both local and free. The local var is
1899 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001900*/
1901
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001902static int
1903get_ref_type(struct compiler *c, PyObject *name)
1904{
Victor Stinner0b1bc562013-05-16 22:17:17 +02001905 int scope;
Benjamin Peterson312595c2013-05-15 15:26:42 -05001906 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001907 _PyUnicode_EqualToASCIIString(name, "__class__"))
Benjamin Peterson312595c2013-05-15 15:26:42 -05001908 return CELL;
Victor Stinner28ad12f2021-03-19 12:41:49 +01001909 scope = _PyST_GetScope(c->u->u_ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 if (scope == 0) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001911 PyErr_Format(PyExc_SystemError,
Victor Stinner28ad12f2021-03-19 12:41:49 +01001912 "_PyST_GetScope(name=%R) failed: "
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001913 "unknown scope in unit %S (%R); "
1914 "symbols: %R; locals: %R; globals: %R",
1915 name,
1916 c->u->u_name, c->u->u_ste->ste_id,
1917 c->u->u_ste->ste_symbols, c->u->u_varnames, c->u->u_names);
1918 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921}
1922
1923static int
1924compiler_lookup_arg(PyObject *dict, PyObject *name)
1925{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001926 PyObject *v;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001927 v = PyDict_GetItemWithError(dict, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001928 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001929 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001930 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001931}
1932
1933static int
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001934compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags,
1935 PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001936{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001937 Py_ssize_t i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001938 if (qualname == NULL)
1939 qualname = co->co_name;
1940
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001941 if (free) {
1942 for (i = 0; i < free; ++i) {
1943 /* Bypass com_addop_varname because it will generate
1944 LOAD_DEREF but LOAD_CLOSURE is needed.
1945 */
1946 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001947
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001948 /* Special case: If a class contains a method with a
1949 free variable that has the same name as a method,
1950 the name will be considered free *and* local in the
1951 class. It should be handled by the closure, as
Min ho Kimc4cacc82019-07-31 08:16:13 +10001952 well as by the normal name lookup logic.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001953 */
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001954 int reftype = get_ref_type(c, name);
1955 if (reftype == -1) {
1956 return 0;
1957 }
1958 int arg;
1959 if (reftype == CELL) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001960 arg = compiler_lookup_arg(c->u->u_cellvars, name);
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001961 }
1962 else {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001963 arg = compiler_lookup_arg(c->u->u_freevars, name);
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001964 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001965 if (arg == -1) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001966 PyErr_Format(PyExc_SystemError,
1967 "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; "
1968 "freevars of code %S: %R",
1969 name,
1970 reftype,
1971 c->u->u_name,
1972 co->co_name,
1973 co->co_freevars);
1974 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001975 }
1976 ADDOP_I(c, LOAD_CLOSURE, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001978 flags |= 0x08;
1979 ADDOP_I(c, BUILD_TUPLE, free);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001981 ADDOP_LOAD_CONST(c, (PyObject*)co);
1982 ADDOP_LOAD_CONST(c, qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001983 ADDOP_I(c, MAKE_FUNCTION, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001985}
1986
1987static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001988compiler_decorators(struct compiler *c, asdl_expr_seq* decos)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 if (!decos)
1993 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1996 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1997 }
1998 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999}
2000
2001static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002002compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs,
2003 asdl_expr_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00002004{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002005 /* Push a dict of keyword-only default values.
2006
2007 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
2008 */
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002009 int i;
2010 PyObject *keys = NULL;
2011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
2013 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
2014 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
2015 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04002016 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002017 if (!mangled) {
2018 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002020 if (keys == NULL) {
2021 keys = PyList_New(1);
2022 if (keys == NULL) {
2023 Py_DECREF(mangled);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002024 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002025 }
2026 PyList_SET_ITEM(keys, 0, mangled);
2027 }
2028 else {
2029 int res = PyList_Append(keys, mangled);
2030 Py_DECREF(mangled);
2031 if (res == -1) {
2032 goto error;
2033 }
2034 }
2035 if (!compiler_visit_expr(c, default_)) {
2036 goto error;
2037 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 }
2039 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002040 if (keys != NULL) {
2041 Py_ssize_t default_count = PyList_GET_SIZE(keys);
2042 PyObject *keys_tuple = PyList_AsTuple(keys);
2043 Py_DECREF(keys);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002044 ADDOP_LOAD_CONST_NEW(c, keys_tuple);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002045 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002046 assert(default_count > 0);
2047 return 1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002048 }
2049 else {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002050 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002051 }
2052
2053error:
2054 Py_XDECREF(keys);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002055 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002056}
2057
2058static int
Guido van Rossum95e4d582018-01-26 08:20:18 -08002059compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
2060{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +03002061 ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation));
Guido van Rossum95e4d582018-01-26 08:20:18 -08002062 return 1;
2063}
2064
2065static int
Neal Norwitzc1505362006-12-28 06:47:50 +00002066compiler_visit_argannotation(struct compiler *c, identifier id,
Yurii Karabas73019792020-11-25 12:43:18 +02002067 expr_ty annotation, Py_ssize_t *annotations_len)
Neal Norwitzc1505362006-12-28 06:47:50 +00002068{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 if (annotation) {
Yurii Karabas73019792020-11-25 12:43:18 +02002070 PyObject *mangled = _Py_Mangle(c->u->u_private, id);
Yury Selivanov34ce99f2014-02-18 12:49:41 -05002071 if (!mangled)
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002072 return 0;
Yurii Karabas73019792020-11-25 12:43:18 +02002073
2074 ADDOP_LOAD_CONST(c, mangled);
Yury Selivanov34ce99f2014-02-18 12:49:41 -05002075 Py_DECREF(mangled);
Yurii Karabas73019792020-11-25 12:43:18 +02002076 VISIT(c, annexpr, annotation);
2077 *annotations_len += 2;
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
Pablo Galindoa5634c42020-09-16 19:42:00 +01002083compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args,
Yurii Karabas73019792020-11-25 12:43:18 +02002084 Py_ssize_t *annotations_len)
Neal Norwitzc1505362006-12-28 06:47:50 +00002085{
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002086 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 for (i = 0; i < asdl_seq_LEN(args); i++) {
2088 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002089 if (!compiler_visit_argannotation(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 c,
2091 arg->arg,
2092 arg->annotation,
Yurii Karabas73019792020-11-25 12:43:18 +02002093 annotations_len))
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002094 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002096 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002097}
2098
2099static int
2100compiler_visit_annotations(struct compiler *c, arguments_ty args,
2101 expr_ty returns)
2102{
Yurii Karabas73019792020-11-25 12:43:18 +02002103 /* Push arg annotation names and values.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002104 The expressions are evaluated out-of-order wrt the source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00002105
Yurii Karabas73019792020-11-25 12:43:18 +02002106 Return 0 on error, -1 if no annotations pushed, 1 if a annotations is pushed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107 */
2108 static identifier return_str;
Yurii Karabas73019792020-11-25 12:43:18 +02002109 Py_ssize_t annotations_len = 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002110
Yurii Karabas73019792020-11-25 12:43:18 +02002111 if (!compiler_visit_argannotations(c, args->args, &annotations_len))
2112 return 0;
2113 if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len))
2114 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002115 if (args->vararg && args->vararg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002116 !compiler_visit_argannotation(c, args->vararg->arg,
Yurii Karabas73019792020-11-25 12:43:18 +02002117 args->vararg->annotation, &annotations_len))
2118 return 0;
2119 if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len))
2120 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002121 if (args->kwarg && args->kwarg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002122 !compiler_visit_argannotation(c, args->kwarg->arg,
Yurii Karabas73019792020-11-25 12:43:18 +02002123 args->kwarg->annotation, &annotations_len))
2124 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002126 if (!return_str) {
2127 return_str = PyUnicode_InternFromString("return");
2128 if (!return_str)
Yurii Karabas73019792020-11-25 12:43:18 +02002129 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 }
Yurii Karabas73019792020-11-25 12:43:18 +02002131 if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) {
2132 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 }
2134
Yurii Karabas73019792020-11-25 12:43:18 +02002135 if (annotations_len) {
2136 ADDOP_I(c, BUILD_TUPLE, annotations_len);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002137 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002139
Yurii Karabas73019792020-11-25 12:43:18 +02002140 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002141}
2142
2143static int
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002144compiler_visit_defaults(struct compiler *c, arguments_ty args)
2145{
2146 VISIT_SEQ(c, expr, args->defaults);
2147 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
2148 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002149}
2150
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002151static Py_ssize_t
2152compiler_default_arguments(struct compiler *c, arguments_ty args)
2153{
2154 Py_ssize_t funcflags = 0;
2155 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002156 if (!compiler_visit_defaults(c, args))
2157 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002158 funcflags |= 0x01;
2159 }
2160 if (args->kwonlyargs) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002161 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002162 args->kw_defaults);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002163 if (res == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002164 return -1;
2165 }
2166 else if (res > 0) {
2167 funcflags |= 0x02;
2168 }
2169 }
2170 return funcflags;
2171}
2172
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173static int
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002174forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx)
2175{
2176
2177 if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2178 compiler_error(c, "cannot assign to __debug__");
2179 return 1;
2180 }
2181 return 0;
2182}
2183
2184static int
2185compiler_check_debug_one_arg(struct compiler *c, arg_ty arg)
2186{
2187 if (arg != NULL) {
2188 if (forbidden_name(c, arg->arg, Store))
2189 return 0;
2190 }
2191 return 1;
2192}
2193
2194static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002195compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args)
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002196{
2197 if (args != NULL) {
Pablo Galindoee40e4b2020-04-23 03:43:08 +01002198 for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002199 if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i)))
2200 return 0;
2201 }
2202 }
2203 return 1;
2204}
2205
2206static int
2207compiler_check_debug_args(struct compiler *c, arguments_ty args)
2208{
2209 if (!compiler_check_debug_args_seq(c, args->posonlyargs))
2210 return 0;
2211 if (!compiler_check_debug_args_seq(c, args->args))
2212 return 0;
2213 if (!compiler_check_debug_one_arg(c, args->vararg))
2214 return 0;
2215 if (!compiler_check_debug_args_seq(c, args->kwonlyargs))
2216 return 0;
2217 if (!compiler_check_debug_one_arg(c, args->kwarg))
2218 return 0;
2219 return 1;
2220}
2221
2222static int
Yury Selivanov75445082015-05-11 22:57:16 -04002223compiler_function(struct compiler *c, stmt_ty s, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002224{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 PyCodeObject *co;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002226 PyObject *qualname, *docstring = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002227 arguments_ty args;
2228 expr_ty returns;
2229 identifier name;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002230 asdl_expr_seq* decos;
2231 asdl_stmt_seq *body;
INADA Naokicb41b272017-02-23 00:31:59 +09002232 Py_ssize_t i, funcflags;
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002233 int annotations;
Yury Selivanov75445082015-05-11 22:57:16 -04002234 int scope_type;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002235 int firstlineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002236
Yury Selivanov75445082015-05-11 22:57:16 -04002237 if (is_async) {
2238 assert(s->kind == AsyncFunctionDef_kind);
2239
2240 args = s->v.AsyncFunctionDef.args;
2241 returns = s->v.AsyncFunctionDef.returns;
2242 decos = s->v.AsyncFunctionDef.decorator_list;
2243 name = s->v.AsyncFunctionDef.name;
2244 body = s->v.AsyncFunctionDef.body;
2245
2246 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
2247 } else {
2248 assert(s->kind == FunctionDef_kind);
2249
2250 args = s->v.FunctionDef.args;
2251 returns = s->v.FunctionDef.returns;
2252 decos = s->v.FunctionDef.decorator_list;
2253 name = s->v.FunctionDef.name;
2254 body = s->v.FunctionDef.body;
2255
2256 scope_type = COMPILER_SCOPE_FUNCTION;
2257 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002258
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002259 if (!compiler_check_debug_args(c, args))
2260 return 0;
2261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 if (!compiler_decorators(c, decos))
2263 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002264
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002265 firstlineno = s->lineno;
2266 if (asdl_seq_LEN(decos)) {
2267 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2268 }
2269
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002270 funcflags = compiler_default_arguments(c, args);
2271 if (funcflags == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002273 }
2274
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002275 annotations = compiler_visit_annotations(c, args, returns);
2276 if (annotations == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002277 return 0;
2278 }
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002279 else if (annotations > 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002280 funcflags |= 0x04;
2281 }
2282
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002283 if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002284 return 0;
2285 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286
INADA Naokicb41b272017-02-23 00:31:59 +09002287 /* if not -OO mode, add docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002288 if (c->c_optimize < 2) {
2289 docstring = _PyAST_GetDocString(body);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002290 }
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002291 if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 compiler_exit_scope(c);
2293 return 0;
2294 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002297 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Mark Shannon877df852020-11-12 09:43:29 +00002299 for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) {
Mark Shannonfd009e62020-11-13 12:53:53 +00002300 VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i));
Mark Shannon877df852020-11-12 09:43:29 +00002301 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002303 qualname = c->u->u_qualname;
2304 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002306 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002307 Py_XDECREF(qualname);
2308 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002309 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002310 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002311
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002312 if (!compiler_make_closure(c, co, funcflags, qualname)) {
2313 Py_DECREF(qualname);
2314 Py_DECREF(co);
2315 return 0;
2316 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002317 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 /* decorators */
2321 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2322 ADDOP_I(c, CALL_FUNCTION, 1);
2323 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002324
Yury Selivanov75445082015-05-11 22:57:16 -04002325 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002326}
2327
2328static int
2329compiler_class(struct compiler *c, stmt_ty s)
2330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 PyCodeObject *co;
2332 PyObject *str;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002333 int i, firstlineno;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002334 asdl_expr_seq *decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 if (!compiler_decorators(c, decos))
2337 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002338
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002339 firstlineno = s->lineno;
2340 if (asdl_seq_LEN(decos)) {
2341 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2342 }
2343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 /* ultimately generate code for:
2345 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
2346 where:
2347 <func> is a function/closure created from the class body;
2348 it has a single argument (__locals__) where the dict
2349 (or MutableSequence) representing the locals is passed
2350 <name> is the class name
2351 <bases> is the positional arguments and *varargs argument
2352 <keywords> is the keyword arguments and **kwds argument
2353 This borrows from compiler_call.
2354 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002357 if (!compiler_enter_scope(c, s->v.ClassDef.name,
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002358 COMPILER_SCOPE_CLASS, (void *)s, firstlineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 return 0;
2360 /* this block represents what we do in the new scope */
2361 {
2362 /* use the class name for name mangling */
2363 Py_INCREF(s->v.ClassDef.name);
Serhiy Storchaka48842712016-04-06 09:45:48 +03002364 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 /* load (global) __name__ ... */
2366 str = PyUnicode_InternFromString("__name__");
2367 if (!str || !compiler_nameop(c, str, Load)) {
2368 Py_XDECREF(str);
2369 compiler_exit_scope(c);
2370 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002371 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 Py_DECREF(str);
2373 /* ... and store it as __module__ */
2374 str = PyUnicode_InternFromString("__module__");
2375 if (!str || !compiler_nameop(c, str, Store)) {
2376 Py_XDECREF(str);
2377 compiler_exit_scope(c);
2378 return 0;
2379 }
2380 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002381 assert(c->u->u_qualname);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002382 ADDOP_LOAD_CONST(c, c->u->u_qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002383 str = PyUnicode_InternFromString("__qualname__");
2384 if (!str || !compiler_nameop(c, str, Store)) {
2385 Py_XDECREF(str);
2386 compiler_exit_scope(c);
2387 return 0;
2388 }
2389 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 /* compile the body proper */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002391 if (!compiler_body(c, s->v.ClassDef.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 compiler_exit_scope(c);
2393 return 0;
2394 }
Mark Shannone56d54e2021-01-15 13:52:00 +00002395 /* The following code is artificial */
2396 c->u->u_lineno = -1;
Nick Coghlan19d24672016-12-05 16:47:55 +10002397 /* Return __classcell__ if it is referenced, otherwise return None */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002398 if (c->u->u_ste->ste_needs_class_closure) {
Nick Coghlan19d24672016-12-05 16:47:55 +10002399 /* Store __classcell__ into class namespace & return it */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002400 str = PyUnicode_InternFromString("__class__");
2401 if (str == NULL) {
2402 compiler_exit_scope(c);
2403 return 0;
2404 }
2405 i = compiler_lookup_arg(c->u->u_cellvars, str);
2406 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01002407 if (i < 0) {
2408 compiler_exit_scope(c);
2409 return 0;
2410 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002411 assert(i == 0);
Nick Coghlan944368e2016-09-11 14:45:49 +10002412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 ADDOP_I(c, LOAD_CLOSURE, i);
Nick Coghlan19d24672016-12-05 16:47:55 +10002414 ADDOP(c, DUP_TOP);
Nick Coghlan944368e2016-09-11 14:45:49 +10002415 str = PyUnicode_InternFromString("__classcell__");
2416 if (!str || !compiler_nameop(c, str, Store)) {
2417 Py_XDECREF(str);
2418 compiler_exit_scope(c);
2419 return 0;
2420 }
2421 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002423 else {
Nick Coghlan19d24672016-12-05 16:47:55 +10002424 /* No methods referenced __class__, so just return None */
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002425 assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002426 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson312595c2013-05-15 15:26:42 -05002427 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002428 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 /* create the code object */
2430 co = assemble(c, 1);
2431 }
2432 /* leave the new scope */
2433 compiler_exit_scope(c);
2434 if (co == NULL)
2435 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 /* 2. load the 'build_class' function */
2438 ADDOP(c, LOAD_BUILD_CLASS);
2439
2440 /* 3. load a function (or closure) made from the code object */
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002441 if (!compiler_make_closure(c, co, 0, NULL)) {
2442 Py_DECREF(co);
2443 return 0;
2444 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 Py_DECREF(co);
2446
2447 /* 4. load class name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002448 ADDOP_LOAD_CONST(c, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449
2450 /* 5. generate the rest of the code for the call */
Pablo Galindoa5634c42020-09-16 19:42:00 +01002451 if (!compiler_call_helper(c, 2, s->v.ClassDef.bases, s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 return 0;
2453
2454 /* 6. apply decorators */
2455 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2456 ADDOP_I(c, CALL_FUNCTION, 1);
2457 }
2458
2459 /* 7. store into <name> */
2460 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2461 return 0;
2462 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002463}
2464
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02002465/* Return 0 if the expression is a constant value except named singletons.
2466 Return 1 otherwise. */
2467static int
2468check_is_arg(expr_ty e)
2469{
2470 if (e->kind != Constant_kind) {
2471 return 1;
2472 }
2473 PyObject *value = e->v.Constant.value;
2474 return (value == Py_None
2475 || value == Py_False
2476 || value == Py_True
2477 || value == Py_Ellipsis);
2478}
2479
2480/* Check operands of identity chacks ("is" and "is not").
2481 Emit a warning if any operand is a constant except named singletons.
2482 Return 0 on error.
2483 */
2484static int
2485check_compare(struct compiler *c, expr_ty e)
2486{
2487 Py_ssize_t i, n;
2488 int left = check_is_arg(e->v.Compare.left);
2489 n = asdl_seq_LEN(e->v.Compare.ops);
2490 for (i = 0; i < n; i++) {
2491 cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i);
2492 int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2493 if (op == Is || op == IsNot) {
2494 if (!right || !left) {
2495 const char *msg = (op == Is)
2496 ? "\"is\" with a literal. Did you mean \"==\"?"
2497 : "\"is not\" with a literal. Did you mean \"!=\"?";
2498 return compiler_warn(c, msg);
2499 }
2500 }
2501 left = right;
2502 }
2503 return 1;
2504}
2505
Mark Shannon9af0e472020-01-14 10:12:45 +00002506static int compiler_addcompare(struct compiler *c, cmpop_ty op)
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002507{
Mark Shannon9af0e472020-01-14 10:12:45 +00002508 int cmp;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002509 switch (op) {
2510 case Eq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002511 cmp = Py_EQ;
2512 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002513 case NotEq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002514 cmp = Py_NE;
2515 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002516 case Lt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002517 cmp = Py_LT;
2518 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002519 case LtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002520 cmp = Py_LE;
2521 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002522 case Gt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002523 cmp = Py_GT;
2524 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002525 case GtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002526 cmp = Py_GE;
2527 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002528 case Is:
Mark Shannon9af0e472020-01-14 10:12:45 +00002529 ADDOP_I(c, IS_OP, 0);
2530 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002531 case IsNot:
Mark Shannon9af0e472020-01-14 10:12:45 +00002532 ADDOP_I(c, IS_OP, 1);
2533 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002534 case In:
Mark Shannon9af0e472020-01-14 10:12:45 +00002535 ADDOP_I(c, CONTAINS_OP, 0);
2536 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002537 case NotIn:
Mark Shannon9af0e472020-01-14 10:12:45 +00002538 ADDOP_I(c, CONTAINS_OP, 1);
2539 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002540 default:
Mark Shannon9af0e472020-01-14 10:12:45 +00002541 Py_UNREACHABLE();
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002542 }
Mark Shannon9af0e472020-01-14 10:12:45 +00002543 ADDOP_I(c, COMPARE_OP, cmp);
2544 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002545}
2546
Mark Shannon9af0e472020-01-14 10:12:45 +00002547
2548
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002549static int
2550compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2551{
2552 switch (e->kind) {
2553 case UnaryOp_kind:
2554 if (e->v.UnaryOp.op == Not)
2555 return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2556 /* fallback to general implementation */
2557 break;
2558 case BoolOp_kind: {
Pablo Galindoa5634c42020-09-16 19:42:00 +01002559 asdl_expr_seq *s = e->v.BoolOp.values;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002560 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2561 assert(n >= 0);
2562 int cond2 = e->v.BoolOp.op == Or;
2563 basicblock *next2 = next;
2564 if (!cond2 != !cond) {
2565 next2 = compiler_new_block(c);
2566 if (next2 == NULL)
2567 return 0;
2568 }
2569 for (i = 0; i < n; ++i) {
2570 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2571 return 0;
2572 }
2573 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2574 return 0;
2575 if (next2 != next)
2576 compiler_use_next_block(c, next2);
2577 return 1;
2578 }
2579 case IfExp_kind: {
2580 basicblock *end, *next2;
2581 end = compiler_new_block(c);
2582 if (end == NULL)
2583 return 0;
2584 next2 = compiler_new_block(c);
2585 if (next2 == NULL)
2586 return 0;
2587 if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2588 return 0;
2589 if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2590 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002591 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002592 compiler_use_next_block(c, next2);
2593 if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2594 return 0;
2595 compiler_use_next_block(c, end);
2596 return 1;
2597 }
2598 case Compare_kind: {
2599 Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2600 if (n > 0) {
Serhiy Storchaka45835252019-02-16 08:29:46 +02002601 if (!check_compare(c, e)) {
2602 return 0;
2603 }
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002604 basicblock *cleanup = compiler_new_block(c);
2605 if (cleanup == NULL)
2606 return 0;
2607 VISIT(c, expr, e->v.Compare.left);
2608 for (i = 0; i < n; i++) {
2609 VISIT(c, expr,
2610 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2611 ADDOP(c, DUP_TOP);
2612 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00002613 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01002614 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002615 NEXT_BLOCK(c);
2616 }
2617 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00002618 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
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 basicblock *end = compiler_new_block(c);
2622 if (end == NULL)
2623 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002624 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002625 compiler_use_next_block(c, cleanup);
2626 ADDOP(c, POP_TOP);
2627 if (!cond) {
Mark Shannon127dde52021-01-04 18:06:55 +00002628 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002629 }
2630 compiler_use_next_block(c, end);
2631 return 1;
2632 }
2633 /* fallback to general implementation */
2634 break;
2635 }
2636 default:
2637 /* fallback to general implementation */
2638 break;
2639 }
2640
2641 /* general implementation */
2642 VISIT(c, expr, e);
Mark Shannon582aaf12020-08-04 17:30:11 +01002643 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Mark Shannon266b4622020-11-17 19:30:14 +00002644 NEXT_BLOCK(c);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002645 return 1;
2646}
2647
2648static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002649compiler_ifexp(struct compiler *c, expr_ty e)
2650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651 basicblock *end, *next;
2652
2653 assert(e->kind == IfExp_kind);
2654 end = compiler_new_block(c);
2655 if (end == NULL)
2656 return 0;
2657 next = compiler_new_block(c);
2658 if (next == NULL)
2659 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002660 if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2661 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002662 VISIT(c, expr, e->v.IfExp.body);
Mark Shannon127dde52021-01-04 18:06:55 +00002663 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 compiler_use_next_block(c, next);
2665 VISIT(c, expr, e->v.IfExp.orelse);
2666 compiler_use_next_block(c, end);
2667 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002668}
2669
2670static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002671compiler_lambda(struct compiler *c, expr_ty e)
2672{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002674 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 static identifier name;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002676 Py_ssize_t funcflags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 arguments_ty args = e->v.Lambda.args;
2678 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002679
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002680 if (!compiler_check_debug_args(c, args))
2681 return 0;
2682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 if (!name) {
2684 name = PyUnicode_InternFromString("<lambda>");
2685 if (!name)
2686 return 0;
2687 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002688
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002689 funcflags = compiler_default_arguments(c, args);
2690 if (funcflags == -1) {
2691 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002692 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002693
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002694 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002695 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002696 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 /* Make None the first constant, so the lambda can't have a
2699 docstring. */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002700 if (compiler_add_const(c, Py_None) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002701 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002703 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002704 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002705 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2706 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2707 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002708 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002709 }
2710 else {
2711 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002712 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002713 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002714 qualname = c->u->u_qualname;
2715 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 compiler_exit_scope(c);
Pablo Galindo7fdab832021-01-29 22:40:59 +00002717 if (co == NULL) {
2718 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002719 return 0;
Pablo Galindo7fdab832021-01-29 22:40:59 +00002720 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002721
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002722 if (!compiler_make_closure(c, co, funcflags, qualname)) {
2723 Py_DECREF(qualname);
2724 Py_DECREF(co);
2725 return 0;
2726 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002727 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002728 Py_DECREF(co);
2729
2730 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002731}
2732
2733static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002734compiler_if(struct compiler *c, stmt_ty s)
2735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002736 basicblock *end, *next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002737 assert(s->kind == If_kind);
2738 end = compiler_new_block(c);
Mark Shannon8473cf82020-12-15 11:07:50 +00002739 if (end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 return 0;
Mark Shannon8473cf82020-12-15 11:07:50 +00002741 }
2742 if (asdl_seq_LEN(s->v.If.orelse)) {
2743 next = compiler_new_block(c);
2744 if (next == NULL) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002745 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002746 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002747 }
2748 else {
2749 next = end;
2750 }
2751 if (!compiler_jump_if(c, s->v.If.test, next, 0)) {
2752 return 0;
2753 }
2754 VISIT_SEQ(c, stmt, s->v.If.body);
2755 if (asdl_seq_LEN(s->v.If.orelse)) {
Mark Shannon127dde52021-01-04 18:06:55 +00002756 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Mark Shannon8473cf82020-12-15 11:07:50 +00002757 compiler_use_next_block(c, next);
2758 VISIT_SEQ(c, stmt, s->v.If.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002759 }
2760 compiler_use_next_block(c, end);
2761 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002762}
2763
2764static int
2765compiler_for(struct compiler *c, stmt_ty s)
2766{
Mark Shannon5977a792020-12-02 13:31:40 +00002767 basicblock *start, *body, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002769 start = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002770 body = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002771 cleanup = compiler_new_block(c);
2772 end = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002773 if (start == NULL || body == NULL || end == NULL || cleanup == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002775 }
2776 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002778 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002779 VISIT(c, expr, s->v.For.iter);
2780 ADDOP(c, GET_ITER);
2781 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01002782 ADDOP_JUMP(c, FOR_ITER, cleanup);
Mark Shannon5977a792020-12-02 13:31:40 +00002783 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002784 VISIT(c, expr, s->v.For.target);
2785 VISIT_SEQ(c, stmt, s->v.For.body);
Mark Shannonf5e97b72020-12-14 11:28:39 +00002786 /* Mark jump as artificial */
2787 c->u->u_lineno = -1;
Mark Shannon582aaf12020-08-04 17:30:11 +01002788 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002789 compiler_use_next_block(c, cleanup);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002790
2791 compiler_pop_fblock(c, FOR_LOOP, start);
2792
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 VISIT_SEQ(c, stmt, s->v.For.orelse);
2794 compiler_use_next_block(c, end);
2795 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002796}
2797
Yury Selivanov75445082015-05-11 22:57:16 -04002798
2799static int
2800compiler_async_for(struct compiler *c, stmt_ty s)
2801{
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002802 basicblock *start, *except, *end;
Pablo Galindo90235812020-03-15 04:29:22 +00002803 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07002804 c->u->u_ste->ste_coroutine = 1;
2805 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
Zsolt Dollensteine2396502018-04-27 08:58:56 -07002806 return compiler_error(c, "'async for' outside async function");
2807 }
2808
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002809 start = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002810 except = compiler_new_block(c);
2811 end = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002812
Mark Shannonfee55262019-11-21 09:11:43 +00002813 if (start == NULL || except == NULL || end == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002814 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002815 }
Yury Selivanov75445082015-05-11 22:57:16 -04002816 VISIT(c, expr, s->v.AsyncFor.iter);
2817 ADDOP(c, GET_AITER);
Yury Selivanov75445082015-05-11 22:57:16 -04002818
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002819 compiler_use_next_block(c, start);
Mark Shannonfee55262019-11-21 09:11:43 +00002820 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002821 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002822 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002823 /* SETUP_FINALLY to guard the __anext__ call */
Mark Shannon582aaf12020-08-04 17:30:11 +01002824 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov75445082015-05-11 22:57:16 -04002825 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002826 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04002827 ADDOP(c, YIELD_FROM);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002828 ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */
Yury Selivanov75445082015-05-11 22:57:16 -04002829
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002830 /* Success block for __anext__ */
2831 VISIT(c, expr, s->v.AsyncFor.target);
2832 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002833 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002834
2835 compiler_pop_fblock(c, FOR_LOOP, start);
Yury Selivanov75445082015-05-11 22:57:16 -04002836
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002837 /* Except block for __anext__ */
Yury Selivanov75445082015-05-11 22:57:16 -04002838 compiler_use_next_block(c, except);
Mark Shannon877df852020-11-12 09:43:29 +00002839
2840 c->u->u_lineno = -1;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002841 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov75445082015-05-11 22:57:16 -04002842
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002843 /* `else` block */
Yury Selivanov75445082015-05-11 22:57:16 -04002844 VISIT_SEQ(c, stmt, s->v.For.orelse);
2845
2846 compiler_use_next_block(c, end);
2847
2848 return 1;
2849}
2850
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002851static int
2852compiler_while(struct compiler *c, stmt_ty s)
2853{
Mark Shannon266b4622020-11-17 19:30:14 +00002854 basicblock *loop, *body, *end, *anchor = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 loop = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002856 body = compiler_new_block(c);
2857 anchor = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002858 end = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002859 if (loop == NULL || body == NULL || anchor == NULL || end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002860 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 compiler_use_next_block(c, loop);
Mark Shannon266b4622020-11-17 19:30:14 +00002863 if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002866 if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) {
2867 return 0;
Mark Shannon266b4622020-11-17 19:30:14 +00002868 }
2869
2870 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 VISIT_SEQ(c, stmt, s->v.While.body);
Mark Shannon8473cf82020-12-15 11:07:50 +00002872 SET_LOC(c, s);
2873 if (!compiler_jump_if(c, s->v.While.test, body, 1)) {
2874 return 0;
2875 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002876
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002877 compiler_pop_fblock(c, WHILE_LOOP, loop);
2878
Mark Shannon266b4622020-11-17 19:30:14 +00002879 compiler_use_next_block(c, anchor);
2880 if (s->v.While.orelse) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 VISIT_SEQ(c, stmt, s->v.While.orelse);
Mark Shannon266b4622020-11-17 19:30:14 +00002882 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002886}
2887
2888static int
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002889compiler_return(struct compiler *c, stmt_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002890{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002891 int preserve_tos = ((s->v.Return.value != NULL) &&
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002892 (s->v.Return.value->kind != Constant_kind));
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002893 if (c->u->u_ste->ste_type != FunctionBlock)
2894 return compiler_error(c, "'return' outside function");
2895 if (s->v.Return.value != NULL &&
2896 c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2897 {
2898 return compiler_error(
2899 c, "'return' with value in async generator");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002901 if (preserve_tos) {
2902 VISIT(c, expr, s->v.Return.value);
Mark Shannon5274b682020-12-16 13:07:01 +00002903 } else {
2904 /* Emit instruction with line number for expression */
2905 if (s->v.Return.value != NULL) {
2906 SET_LOC(c, s->v.Return.value);
2907 ADDOP(c, NOP);
2908 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002909 }
Mark Shannonfee55262019-11-21 09:11:43 +00002910 if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL))
2911 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002912 if (s->v.Return.value == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002913 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002914 }
2915 else if (!preserve_tos) {
Mark Shannon5274b682020-12-16 13:07:01 +00002916 ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002917 }
2918 ADDOP(c, RETURN_VALUE);
Mark Shannon266b4622020-11-17 19:30:14 +00002919 NEXT_BLOCK(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002922}
2923
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002924static int
2925compiler_break(struct compiler *c)
2926{
Mark Shannonfee55262019-11-21 09:11:43 +00002927 struct fblockinfo *loop = NULL;
2928 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
2929 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002930 }
Mark Shannonfee55262019-11-21 09:11:43 +00002931 if (loop == NULL) {
2932 return compiler_error(c, "'break' outside loop");
2933 }
2934 if (!compiler_unwind_fblock(c, loop, 0)) {
2935 return 0;
2936 }
Mark Shannon582aaf12020-08-04 17:30:11 +01002937 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit);
Mark Shannon266b4622020-11-17 19:30:14 +00002938 NEXT_BLOCK(c);
Mark Shannonfee55262019-11-21 09:11:43 +00002939 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002940}
2941
2942static int
2943compiler_continue(struct compiler *c)
2944{
Mark Shannonfee55262019-11-21 09:11:43 +00002945 struct fblockinfo *loop = NULL;
2946 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
2947 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002948 }
Mark Shannonfee55262019-11-21 09:11:43 +00002949 if (loop == NULL) {
2950 return compiler_error(c, "'continue' not properly in loop");
2951 }
Mark Shannon582aaf12020-08-04 17:30:11 +01002952 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block);
Mark Shannon266b4622020-11-17 19:30:14 +00002953 NEXT_BLOCK(c)
Mark Shannonfee55262019-11-21 09:11:43 +00002954 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002955}
2956
2957
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002958/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959
2960 SETUP_FINALLY L
2961 <code for body>
2962 POP_BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00002963 <code for finalbody>
2964 JUMP E
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002965 L:
2966 <code for finalbody>
Mark Shannonfee55262019-11-21 09:11:43 +00002967 E:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002969 The special instructions use the block stack. Each block
2970 stack entry contains the instruction that created it (here
2971 SETUP_FINALLY), the level of the value stack at the time the
2972 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002973
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002974 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975 Pushes the current value stack level and the label
2976 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002977 POP_BLOCK:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002978 Pops en entry from the block stack.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002979
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002980 The block stack is unwound when an exception is raised:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002981 when a SETUP_FINALLY entry is found, the raised and the caught
2982 exceptions are pushed onto the value stack (and the exception
2983 condition is cleared), and the interpreter jumps to the label
2984 gotten from the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002985*/
2986
2987static int
2988compiler_try_finally(struct compiler *c, stmt_ty s)
2989{
Mark Shannonfee55262019-11-21 09:11:43 +00002990 basicblock *body, *end, *exit;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 body = compiler_new_block(c);
2993 end = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00002994 exit = compiler_new_block(c);
2995 if (body == NULL || end == NULL || exit == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002997
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002998 /* `try` block */
Mark Shannon582aaf12020-08-04 17:30:11 +01002999 ADDOP_JUMP(c, SETUP_FINALLY, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003000 compiler_use_next_block(c, body);
Mark Shannonfee55262019-11-21 09:11:43 +00003001 if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003002 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003003 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
3004 if (!compiler_try_except(c, s))
3005 return 0;
3006 }
3007 else {
3008 VISIT_SEQ(c, stmt, s->v.Try.body);
3009 }
Mark Shannon3bd60352021-01-13 12:05:43 +00003010 ADDOP_NOLINE(c, POP_BLOCK);
Mark Shannonfee55262019-11-21 09:11:43 +00003011 compiler_pop_fblock(c, FINALLY_TRY, body);
3012 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Mark Shannon127dde52021-01-04 18:06:55 +00003013 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00003014 /* `finally` block */
3015 compiler_use_next_block(c, end);
3016 if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL))
3017 return 0;
3018 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3019 compiler_pop_fblock(c, FINALLY_END, end);
Mark Shannonbf353f32020-12-17 13:55:28 +00003020 ADDOP_I(c, RERAISE, 0);
Mark Shannonfee55262019-11-21 09:11:43 +00003021 compiler_use_next_block(c, exit);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003023}
3024
3025/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003026 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003027 (The contents of the value stack is shown in [], with the top
3028 at the right; 'tb' is trace-back info, 'val' the exception's
3029 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003030
3031 Value stack Label Instruction Argument
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003032 [] SETUP_FINALLY L1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 [] <code for S>
3034 [] POP_BLOCK
3035 [] JUMP_FORWARD L0
3036
3037 [tb, val, exc] L1: DUP )
3038 [tb, val, exc, exc] <evaluate E1> )
Mark Shannon9af0e472020-01-14 10:12:45 +00003039 [tb, val, exc, exc, E1] JUMP_IF_NOT_EXC_MATCH L2 ) only if E1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003040 [tb, val, exc] POP
3041 [tb, val] <assign to V1> (or POP if no V1)
3042 [tb] POP
3043 [] <code for S1>
3044 JUMP_FORWARD L0
3045
3046 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003047 .............................etc.......................
3048
Mark Shannonfee55262019-11-21 09:11:43 +00003049 [tb, val, exc] Ln+1: RERAISE # re-raise exception
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003050
3051 [] L0: <next statement>
3052
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003053 Of course, parts are not generated if Vi or Ei is not present.
3054*/
3055static int
3056compiler_try_except(struct compiler *c, stmt_ty s)
3057{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003059 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 body = compiler_new_block(c);
3062 except = compiler_new_block(c);
3063 orelse = compiler_new_block(c);
3064 end = compiler_new_block(c);
3065 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
3066 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01003067 ADDOP_JUMP(c, SETUP_FINALLY, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003068 compiler_use_next_block(c, body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003069 if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003071 VISIT_SEQ(c, stmt, s->v.Try.body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003072 compiler_pop_fblock(c, TRY_EXCEPT, body);
Mark Shannon3bd60352021-01-13 12:05:43 +00003073 ADDOP_NOLINE(c, POP_BLOCK);
3074 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003075 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003076 compiler_use_next_block(c, except);
Mark Shannon02d126a2020-09-25 14:04:19 +01003077 /* Runtime will push a block here, so we need to account for that */
3078 if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL))
3079 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003080 for (i = 0; i < n; i++) {
3081 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003082 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083 if (!handler->v.ExceptHandler.type && i < n-1)
3084 return compiler_error(c, "default 'except:' must be last");
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003085 SET_LOC(c, handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003086 except = compiler_new_block(c);
3087 if (except == NULL)
3088 return 0;
3089 if (handler->v.ExceptHandler.type) {
3090 ADDOP(c, DUP_TOP);
3091 VISIT(c, expr, handler->v.ExceptHandler.type);
Mark Shannon582aaf12020-08-04 17:30:11 +01003092 ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except);
Mark Shannon266b4622020-11-17 19:30:14 +00003093 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 }
3095 ADDOP(c, POP_TOP);
3096 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003097 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00003098
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003099 cleanup_end = compiler_new_block(c);
3100 cleanup_body = compiler_new_block(c);
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003101 if (cleanup_end == NULL || cleanup_body == NULL) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003102 return 0;
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003103 }
Guido van Rossumb940e112007-01-10 16:19:56 +00003104
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003105 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3106 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003107
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003108 /*
3109 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003110 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003111 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003112 try:
3113 # body
3114 finally:
Chris Angelicoad098b62019-05-21 23:34:19 +10003115 name = None # in case body contains "del name"
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003116 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003117 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003118
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003119 /* second try: */
Mark Shannon582aaf12020-08-04 17:30:11 +01003120 ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003121 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003122 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003123 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003124
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003125 /* second # body */
3126 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003127 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003128 ADDOP(c, POP_BLOCK);
3129 ADDOP(c, POP_EXCEPT);
Mark Shannon877df852020-11-12 09:43:29 +00003130 /* name = None; del name; # Mark as artificial */
3131 c->u->u_lineno = -1;
Mark Shannonfee55262019-11-21 09:11:43 +00003132 ADDOP_LOAD_CONST(c, Py_None);
3133 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3134 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Mark Shannon582aaf12020-08-04 17:30:11 +01003135 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136
Mark Shannonfee55262019-11-21 09:11:43 +00003137 /* except: */
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003138 compiler_use_next_block(c, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139
Mark Shannon877df852020-11-12 09:43:29 +00003140 /* name = None; del name; # Mark as artificial */
3141 c->u->u_lineno = -1;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003142 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003143 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003144 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145
Mark Shannonbf353f32020-12-17 13:55:28 +00003146 ADDOP_I(c, RERAISE, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003147 }
3148 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003149 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003150
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003151 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05003152 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003153 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154
Guido van Rossumb940e112007-01-10 16:19:56 +00003155 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003156 ADDOP(c, POP_TOP);
3157 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003158 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003159 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003160 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003161 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannon127dde52021-01-04 18:06:55 +00003162 /* name = None; del name; # Mark as artificial */
3163 c->u->u_lineno = -1;
Mark Shannonfee55262019-11-21 09:11:43 +00003164 ADDOP(c, POP_EXCEPT);
Mark Shannon582aaf12020-08-04 17:30:11 +01003165 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003166 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003167 compiler_use_next_block(c, except);
3168 }
Mark Shannon02d126a2020-09-25 14:04:19 +01003169 compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL);
Mark Shannonf2dbfd72020-12-21 13:53:50 +00003170 /* Mark as artificial */
3171 c->u->u_lineno = -1;
Mark Shannonbf353f32020-12-17 13:55:28 +00003172 ADDOP_I(c, RERAISE, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003173 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003174 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003175 compiler_use_next_block(c, end);
3176 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003177}
3178
3179static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003180compiler_try(struct compiler *c, stmt_ty s) {
3181 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
3182 return compiler_try_finally(c, s);
3183 else
3184 return compiler_try_except(c, s);
3185}
3186
3187
3188static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003189compiler_import_as(struct compiler *c, identifier name, identifier asname)
3190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003191 /* The IMPORT_NAME opcode was already generated. This function
3192 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003194 If there is a dot in name, we need to split it and emit a
Serhiy Storchakaf93234b2017-05-09 22:31:05 +03003195 IMPORT_FROM for each name.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003196 */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003197 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
3198 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003199 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003200 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003201 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003202 /* Consume the base module name to get the first attribute */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003203 while (1) {
3204 Py_ssize_t pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003205 PyObject *attr;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003206 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003207 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003208 return 0;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003209 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 if (!attr)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003211 return 0;
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003212 ADDOP_N(c, IMPORT_FROM, attr, names);
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003213 if (dot == -1) {
3214 break;
3215 }
3216 ADDOP(c, ROT_TWO);
3217 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003218 }
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003219 if (!compiler_nameop(c, asname, Store)) {
3220 return 0;
3221 }
3222 ADDOP(c, POP_TOP);
3223 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003224 }
3225 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003226}
3227
3228static int
3229compiler_import(struct compiler *c, stmt_ty s)
3230{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003231 /* The Import node stores a module name like a.b.c as a single
3232 string. This is convenient for all cases except
3233 import a.b.c as d
3234 where we need to parse that string to extract the individual
3235 module names.
3236 XXX Perhaps change the representation to make this case simpler?
3237 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01003238 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003239
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003240 PyObject *zero = _PyLong_GetZero(); // borrowed reference
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003241 for (i = 0; i < n; i++) {
3242 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
3243 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003244
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003245 ADDOP_LOAD_CONST(c, zero);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003246 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003247 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003249 if (alias->asname) {
3250 r = compiler_import_as(c, alias->name, alias->asname);
3251 if (!r)
3252 return r;
3253 }
3254 else {
3255 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003256 Py_ssize_t dot = PyUnicode_FindChar(
3257 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02003258 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003259 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02003260 if (tmp == NULL)
3261 return 0;
3262 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003264 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003265 Py_DECREF(tmp);
3266 }
3267 if (!r)
3268 return r;
3269 }
3270 }
3271 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003272}
3273
3274static int
3275compiler_from_import(struct compiler *c, stmt_ty s)
3276{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003277 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003278 PyObject *names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003281 if (!empty_string) {
3282 empty_string = PyUnicode_FromString("");
3283 if (!empty_string)
3284 return 0;
3285 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003286
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003287 ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02003288
3289 names = PyTuple_New(n);
3290 if (!names)
3291 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 /* build up the names */
3294 for (i = 0; i < n; i++) {
3295 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3296 Py_INCREF(alias->name);
3297 PyTuple_SET_ITEM(names, i, alias->name);
3298 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003300 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003301 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 Py_DECREF(names);
3303 return compiler_error(c, "from __future__ imports must occur "
3304 "at the beginning of the file");
3305 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003306 ADDOP_LOAD_CONST_NEW(c, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003308 if (s->v.ImportFrom.module) {
3309 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
3310 }
3311 else {
3312 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
3313 }
3314 for (i = 0; i < n; i++) {
3315 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3316 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003317
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003318 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003319 assert(n == 1);
3320 ADDOP(c, IMPORT_STAR);
3321 return 1;
3322 }
3323
3324 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
3325 store_name = alias->name;
3326 if (alias->asname)
3327 store_name = alias->asname;
3328
3329 if (!compiler_nameop(c, store_name, Store)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003330 return 0;
3331 }
3332 }
3333 /* remove imported module */
3334 ADDOP(c, POP_TOP);
3335 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003336}
3337
3338static int
3339compiler_assert(struct compiler *c, stmt_ty s)
3340{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003341 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003342
tsukasa-aua8ef4572021-03-16 22:14:41 +11003343 /* Always emit a warning if the test is a non-zero length tuple */
3344 if ((s->v.Assert.test->kind == Tuple_kind &&
3345 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) ||
3346 (s->v.Assert.test->kind == Constant_kind &&
3347 PyTuple_Check(s->v.Assert.test->v.Constant.value) &&
3348 PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0))
Serhiy Storchakad31e7732018-10-21 10:09:39 +03003349 {
3350 if (!compiler_warn(c, "assertion is always true, "
3351 "perhaps remove parentheses?"))
3352 {
Victor Stinner14e461d2013-08-26 22:28:21 +02003353 return 0;
3354 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 }
tsukasa-aua8ef4572021-03-16 22:14:41 +11003356 if (c->c_optimize)
3357 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003358 end = compiler_new_block(c);
3359 if (end == NULL)
3360 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003361 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
3362 return 0;
Zackery Spytzce6a0702019-08-25 03:44:09 -06003363 ADDOP(c, LOAD_ASSERTION_ERROR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003364 if (s->v.Assert.msg) {
3365 VISIT(c, expr, s->v.Assert.msg);
3366 ADDOP_I(c, CALL_FUNCTION, 1);
3367 }
3368 ADDOP_I(c, RAISE_VARARGS, 1);
3369 compiler_use_next_block(c, end);
3370 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003371}
3372
3373static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003374compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3375{
3376 if (c->c_interactive && c->c_nestlevel <= 1) {
3377 VISIT(c, expr, value);
3378 ADDOP(c, PRINT_EXPR);
3379 return 1;
3380 }
3381
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003382 if (value->kind == Constant_kind) {
Victor Stinner15a30952016-02-08 22:45:06 +01003383 /* ignore constant statement */
Mark Shannon877df852020-11-12 09:43:29 +00003384 ADDOP(c, NOP);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003385 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003386 }
3387
3388 VISIT(c, expr, value);
Mark Shannonc5440932021-03-15 14:24:25 +00003389 /* Mark POP_TOP as artificial */
3390 c->u->u_lineno = -1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003391 ADDOP(c, POP_TOP);
3392 return 1;
3393}
3394
3395static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003396compiler_visit_stmt(struct compiler *c, stmt_ty s)
3397{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003398 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003400 /* Always assign a lineno to the next instruction for a stmt. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003401 SET_LOC(c, s);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003403 switch (s->kind) {
3404 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04003405 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003406 case ClassDef_kind:
3407 return compiler_class(c, s);
3408 case Return_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003409 return compiler_return(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003410 case Delete_kind:
3411 VISIT_SEQ(c, expr, s->v.Delete.targets)
3412 break;
3413 case Assign_kind:
3414 n = asdl_seq_LEN(s->v.Assign.targets);
3415 VISIT(c, expr, s->v.Assign.value);
3416 for (i = 0; i < n; i++) {
3417 if (i < n - 1)
3418 ADDOP(c, DUP_TOP);
3419 VISIT(c, expr,
3420 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3421 }
3422 break;
3423 case AugAssign_kind:
3424 return compiler_augassign(c, s);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003425 case AnnAssign_kind:
3426 return compiler_annassign(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003427 case For_kind:
3428 return compiler_for(c, s);
3429 case While_kind:
3430 return compiler_while(c, s);
3431 case If_kind:
3432 return compiler_if(c, s);
Brandt Bucher145bf262021-02-26 14:51:55 -08003433 case Match_kind:
3434 return compiler_match(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 case Raise_kind:
3436 n = 0;
3437 if (s->v.Raise.exc) {
3438 VISIT(c, expr, s->v.Raise.exc);
3439 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003440 if (s->v.Raise.cause) {
3441 VISIT(c, expr, s->v.Raise.cause);
3442 n++;
3443 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003444 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01003445 ADDOP_I(c, RAISE_VARARGS, (int)n);
Mark Shannon266b4622020-11-17 19:30:14 +00003446 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003447 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003448 case Try_kind:
3449 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 case Assert_kind:
3451 return compiler_assert(c, s);
3452 case Import_kind:
3453 return compiler_import(c, s);
3454 case ImportFrom_kind:
3455 return compiler_from_import(c, s);
3456 case Global_kind:
3457 case Nonlocal_kind:
3458 break;
3459 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003460 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003461 case Pass_kind:
Mark Shannon877df852020-11-12 09:43:29 +00003462 ADDOP(c, NOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003463 break;
3464 case Break_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003465 return compiler_break(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003466 case Continue_kind:
3467 return compiler_continue(c);
3468 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003469 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04003470 case AsyncFunctionDef_kind:
3471 return compiler_function(c, s, 1);
3472 case AsyncWith_kind:
3473 return compiler_async_with(c, s, 0);
3474 case AsyncFor_kind:
3475 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003476 }
Yury Selivanov75445082015-05-11 22:57:16 -04003477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003478 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003479}
3480
3481static int
3482unaryop(unaryop_ty op)
3483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003484 switch (op) {
3485 case Invert:
3486 return UNARY_INVERT;
3487 case Not:
3488 return UNARY_NOT;
3489 case UAdd:
3490 return UNARY_POSITIVE;
3491 case USub:
3492 return UNARY_NEGATIVE;
3493 default:
3494 PyErr_Format(PyExc_SystemError,
3495 "unary op %d should not be possible", op);
3496 return 0;
3497 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003498}
3499
3500static int
Andy Lester76d58772020-03-10 21:18:12 -05003501binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 switch (op) {
3504 case Add:
3505 return BINARY_ADD;
3506 case Sub:
3507 return BINARY_SUBTRACT;
3508 case Mult:
3509 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003510 case MatMult:
3511 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003512 case Div:
3513 return BINARY_TRUE_DIVIDE;
3514 case Mod:
3515 return BINARY_MODULO;
3516 case Pow:
3517 return BINARY_POWER;
3518 case LShift:
3519 return BINARY_LSHIFT;
3520 case RShift:
3521 return BINARY_RSHIFT;
3522 case BitOr:
3523 return BINARY_OR;
3524 case BitXor:
3525 return BINARY_XOR;
3526 case BitAnd:
3527 return BINARY_AND;
3528 case FloorDiv:
3529 return BINARY_FLOOR_DIVIDE;
3530 default:
3531 PyErr_Format(PyExc_SystemError,
3532 "binary op %d should not be possible", op);
3533 return 0;
3534 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003535}
3536
3537static int
Andy Lester76d58772020-03-10 21:18:12 -05003538inplace_binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003539{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003540 switch (op) {
3541 case Add:
3542 return INPLACE_ADD;
3543 case Sub:
3544 return INPLACE_SUBTRACT;
3545 case Mult:
3546 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003547 case MatMult:
3548 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003549 case Div:
3550 return INPLACE_TRUE_DIVIDE;
3551 case Mod:
3552 return INPLACE_MODULO;
3553 case Pow:
3554 return INPLACE_POWER;
3555 case LShift:
3556 return INPLACE_LSHIFT;
3557 case RShift:
3558 return INPLACE_RSHIFT;
3559 case BitOr:
3560 return INPLACE_OR;
3561 case BitXor:
3562 return INPLACE_XOR;
3563 case BitAnd:
3564 return INPLACE_AND;
3565 case FloorDiv:
3566 return INPLACE_FLOOR_DIVIDE;
3567 default:
3568 PyErr_Format(PyExc_SystemError,
3569 "inplace binary op %d should not be possible", op);
3570 return 0;
3571 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003572}
3573
3574static int
3575compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3576{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003577 int op, scope;
3578 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003579 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003581 PyObject *dict = c->u->u_names;
3582 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003583
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003584 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3585 !_PyUnicode_EqualToASCIIString(name, "True") &&
3586 !_PyUnicode_EqualToASCIIString(name, "False"));
Benjamin Peterson70b224d2012-12-06 17:49:58 -05003587
Pablo Galindoc5fc1562020-04-22 23:29:27 +01003588 if (forbidden_name(c, name, ctx))
3589 return 0;
3590
Serhiy Storchakabd6ec4d2017-12-18 14:29:12 +02003591 mangled = _Py_Mangle(c->u->u_private, name);
3592 if (!mangled)
3593 return 0;
3594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003595 op = 0;
3596 optype = OP_NAME;
Victor Stinner28ad12f2021-03-19 12:41:49 +01003597 scope = _PyST_GetScope(c->u->u_ste, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003598 switch (scope) {
3599 case FREE:
3600 dict = c->u->u_freevars;
3601 optype = OP_DEREF;
3602 break;
3603 case CELL:
3604 dict = c->u->u_cellvars;
3605 optype = OP_DEREF;
3606 break;
3607 case LOCAL:
3608 if (c->u->u_ste->ste_type == FunctionBlock)
3609 optype = OP_FAST;
3610 break;
3611 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04003612 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003613 optype = OP_GLOBAL;
3614 break;
3615 case GLOBAL_EXPLICIT:
3616 optype = OP_GLOBAL;
3617 break;
3618 default:
3619 /* scope can be 0 */
3620 break;
3621 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003623 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003624 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003626 switch (optype) {
3627 case OP_DEREF:
3628 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003629 case Load:
3630 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3631 break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003632 case Store: op = STORE_DEREF; break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003633 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003634 }
3635 break;
3636 case OP_FAST:
3637 switch (ctx) {
3638 case Load: op = LOAD_FAST; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003639 case Store: op = STORE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003640 case Del: op = DELETE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003641 }
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003642 ADDOP_N(c, op, mangled, varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003643 return 1;
3644 case OP_GLOBAL:
3645 switch (ctx) {
3646 case Load: op = LOAD_GLOBAL; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003647 case Store: op = STORE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003648 case Del: op = DELETE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003649 }
3650 break;
3651 case OP_NAME:
3652 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00003653 case Load: op = LOAD_NAME; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003654 case Store: op = STORE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003655 case Del: op = DELETE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003656 }
3657 break;
3658 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003660 assert(op);
Andy Lester76d58772020-03-10 21:18:12 -05003661 arg = compiler_add_o(dict, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003662 Py_DECREF(mangled);
3663 if (arg < 0)
3664 return 0;
3665 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003666}
3667
3668static int
3669compiler_boolop(struct compiler *c, expr_ty e)
3670{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003671 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003672 int jumpi;
3673 Py_ssize_t i, n;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003674 asdl_expr_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003676 assert(e->kind == BoolOp_kind);
3677 if (e->v.BoolOp.op == And)
3678 jumpi = JUMP_IF_FALSE_OR_POP;
3679 else
3680 jumpi = JUMP_IF_TRUE_OR_POP;
3681 end = compiler_new_block(c);
3682 if (end == NULL)
3683 return 0;
3684 s = e->v.BoolOp.values;
3685 n = asdl_seq_LEN(s) - 1;
3686 assert(n >= 0);
3687 for (i = 0; i < n; ++i) {
3688 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003689 ADDOP_JUMP(c, jumpi, end);
Mark Shannon6e8128f2020-07-30 10:03:00 +01003690 basicblock *next = compiler_new_block(c);
3691 if (next == NULL) {
3692 return 0;
3693 }
3694 compiler_use_next_block(c, next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003695 }
3696 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3697 compiler_use_next_block(c, end);
3698 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003699}
3700
3701static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003702starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
Mark Shannon13bc1392020-01-23 09:25:17 +00003703 int build, int add, int extend, int tuple)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003704{
3705 Py_ssize_t n = asdl_seq_LEN(elts);
Mark Shannon13bc1392020-01-23 09:25:17 +00003706 Py_ssize_t i, seen_star = 0;
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003707 if (n > 2 && are_all_items_const(elts, 0, n)) {
3708 PyObject *folded = PyTuple_New(n);
3709 if (folded == NULL) {
3710 return 0;
3711 }
3712 PyObject *val;
3713 for (i = 0; i < n; i++) {
3714 val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
3715 Py_INCREF(val);
3716 PyTuple_SET_ITEM(folded, i, val);
3717 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003718 if (tuple) {
3719 ADDOP_LOAD_CONST_NEW(c, folded);
3720 } else {
3721 if (add == SET_ADD) {
3722 Py_SETREF(folded, PyFrozenSet_New(folded));
3723 if (folded == NULL) {
3724 return 0;
3725 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003726 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003727 ADDOP_I(c, build, pushed);
3728 ADDOP_LOAD_CONST_NEW(c, folded);
3729 ADDOP_I(c, extend, 1);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003730 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003731 return 1;
3732 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003733
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003734 for (i = 0; i < n; i++) {
3735 expr_ty elt = asdl_seq_GET(elts, i);
3736 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003737 seen_star = 1;
3738 }
3739 }
3740 if (seen_star) {
3741 seen_star = 0;
3742 for (i = 0; i < n; i++) {
3743 expr_ty elt = asdl_seq_GET(elts, i);
3744 if (elt->kind == Starred_kind) {
3745 if (seen_star == 0) {
3746 ADDOP_I(c, build, i+pushed);
3747 seen_star = 1;
3748 }
3749 VISIT(c, expr, elt->v.Starred.value);
3750 ADDOP_I(c, extend, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003751 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003752 else {
3753 VISIT(c, expr, elt);
3754 if (seen_star) {
3755 ADDOP_I(c, add, 1);
3756 }
3757 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003758 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003759 assert(seen_star);
3760 if (tuple) {
3761 ADDOP(c, LIST_TO_TUPLE);
3762 }
3763 }
3764 else {
3765 for (i = 0; i < n; i++) {
3766 expr_ty elt = asdl_seq_GET(elts, i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003767 VISIT(c, expr, elt);
Mark Shannon13bc1392020-01-23 09:25:17 +00003768 }
3769 if (tuple) {
3770 ADDOP_I(c, BUILD_TUPLE, n+pushed);
3771 } else {
3772 ADDOP_I(c, build, n+pushed);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003773 }
3774 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003775 return 1;
3776}
3777
3778static int
Brandt Bucher145bf262021-02-26 14:51:55 -08003779unpack_helper(struct compiler *c, asdl_expr_seq *elts)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003780{
3781 Py_ssize_t n = asdl_seq_LEN(elts);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003782 int seen_star = 0;
Brandt Bucher145bf262021-02-26 14:51:55 -08003783 for (Py_ssize_t i = 0; i < n; i++) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003784 expr_ty elt = asdl_seq_GET(elts, i);
3785 if (elt->kind == Starred_kind && !seen_star) {
3786 if ((i >= (1 << 8)) ||
3787 (n-i-1 >= (INT_MAX >> 8)))
3788 return compiler_error(c,
3789 "too many expressions in "
3790 "star-unpacking assignment");
3791 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3792 seen_star = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003793 }
3794 else if (elt->kind == Starred_kind) {
3795 return compiler_error(c,
Furkan Öndercb6534e2020-03-26 04:54:31 +03003796 "multiple starred expressions in assignment");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003797 }
3798 }
3799 if (!seen_star) {
3800 ADDOP_I(c, UNPACK_SEQUENCE, n);
3801 }
Brandt Bucher145bf262021-02-26 14:51:55 -08003802 return 1;
3803}
3804
3805static int
3806assignment_helper(struct compiler *c, asdl_expr_seq *elts)
3807{
3808 Py_ssize_t n = asdl_seq_LEN(elts);
3809 RETURN_IF_FALSE(unpack_helper(c, elts));
3810 for (Py_ssize_t i = 0; i < n; i++) {
Brandt Bucherd5aa2e92020-03-07 19:44:18 -08003811 expr_ty elt = asdl_seq_GET(elts, i);
3812 VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value);
3813 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003814 return 1;
3815}
3816
3817static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003818compiler_list(struct compiler *c, expr_ty e)
3819{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003820 asdl_expr_seq *elts = e->v.List.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003821 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003822 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003823 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003824 else if (e->v.List.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003825 return starunpack_helper(c, elts, 0, BUILD_LIST,
3826 LIST_APPEND, LIST_EXTEND, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003827 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003828 else
3829 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003830 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003831}
3832
3833static int
3834compiler_tuple(struct compiler *c, expr_ty e)
3835{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003836 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003837 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003838 return assignment_helper(c, elts);
3839 }
3840 else if (e->v.Tuple.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003841 return starunpack_helper(c, elts, 0, BUILD_LIST,
3842 LIST_APPEND, LIST_EXTEND, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003843 }
3844 else
3845 VISIT_SEQ(c, expr, elts);
3846 return 1;
3847}
3848
3849static int
3850compiler_set(struct compiler *c, expr_ty e)
3851{
Mark Shannon13bc1392020-01-23 09:25:17 +00003852 return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET,
3853 SET_ADD, SET_UPDATE, 0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003854}
3855
3856static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003857are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003858{
3859 Py_ssize_t i;
3860 for (i = begin; i < end; i++) {
3861 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003862 if (key == NULL || key->kind != Constant_kind)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003863 return 0;
3864 }
3865 return 1;
3866}
3867
3868static int
3869compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3870{
3871 Py_ssize_t i, n = end - begin;
3872 PyObject *keys, *key;
3873 if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) {
3874 for (i = begin; i < end; i++) {
3875 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3876 }
3877 keys = PyTuple_New(n);
3878 if (keys == NULL) {
3879 return 0;
3880 }
3881 for (i = begin; i < end; i++) {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003882 key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003883 Py_INCREF(key);
3884 PyTuple_SET_ITEM(keys, i - begin, key);
3885 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003886 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003887 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3888 }
3889 else {
3890 for (i = begin; i < end; i++) {
3891 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3892 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3893 }
3894 ADDOP_I(c, BUILD_MAP, n);
3895 }
3896 return 1;
3897}
3898
3899static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003900compiler_dict(struct compiler *c, expr_ty e)
3901{
Victor Stinner976bb402016-03-23 11:36:19 +01003902 Py_ssize_t i, n, elements;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003903 int have_dict;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003904 int is_unpacking = 0;
3905 n = asdl_seq_LEN(e->v.Dict.values);
Mark Shannon8a4cd702020-01-27 09:57:45 +00003906 have_dict = 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003907 elements = 0;
3908 for (i = 0; i < n; i++) {
3909 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003910 if (is_unpacking) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003911 if (elements) {
3912 if (!compiler_subdict(c, e, i - elements, i)) {
3913 return 0;
3914 }
3915 if (have_dict) {
3916 ADDOP_I(c, DICT_UPDATE, 1);
3917 }
3918 have_dict = 1;
3919 elements = 0;
3920 }
3921 if (have_dict == 0) {
3922 ADDOP_I(c, BUILD_MAP, 0);
3923 have_dict = 1;
3924 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003925 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Mark Shannon8a4cd702020-01-27 09:57:45 +00003926 ADDOP_I(c, DICT_UPDATE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003927 }
3928 else {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003929 if (elements == 0xFFFF) {
Pablo Galindoc51db0e2020-08-13 09:48:41 +01003930 if (!compiler_subdict(c, e, i - elements, i + 1)) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003931 return 0;
3932 }
3933 if (have_dict) {
3934 ADDOP_I(c, DICT_UPDATE, 1);
3935 }
3936 have_dict = 1;
3937 elements = 0;
3938 }
3939 else {
3940 elements++;
3941 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003942 }
3943 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003944 if (elements) {
3945 if (!compiler_subdict(c, e, n - elements, n)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003946 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003947 }
3948 if (have_dict) {
3949 ADDOP_I(c, DICT_UPDATE, 1);
3950 }
3951 have_dict = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003952 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003953 if (!have_dict) {
3954 ADDOP_I(c, BUILD_MAP, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003955 }
3956 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003957}
3958
3959static int
3960compiler_compare(struct compiler *c, expr_ty e)
3961{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003962 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003963
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02003964 if (!check_compare(c, e)) {
3965 return 0;
3966 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003967 VISIT(c, expr, e->v.Compare.left);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003968 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
3969 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
3970 if (n == 0) {
3971 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Mark Shannon9af0e472020-01-14 10:12:45 +00003972 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003973 }
3974 else {
3975 basicblock *cleanup = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003976 if (cleanup == NULL)
3977 return 0;
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003978 for (i = 0; i < n; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003979 VISIT(c, expr,
3980 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003981 ADDOP(c, DUP_TOP);
3982 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00003983 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003984 ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003985 NEXT_BLOCK(c);
3986 }
3987 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00003988 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003989 basicblock *end = compiler_new_block(c);
3990 if (end == NULL)
3991 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00003992 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003993 compiler_use_next_block(c, cleanup);
3994 ADDOP(c, ROT_TWO);
3995 ADDOP(c, POP_TOP);
3996 compiler_use_next_block(c, end);
3997 }
3998 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003999}
4000
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004001static PyTypeObject *
4002infer_type(expr_ty e)
4003{
4004 switch (e->kind) {
4005 case Tuple_kind:
4006 return &PyTuple_Type;
4007 case List_kind:
4008 case ListComp_kind:
4009 return &PyList_Type;
4010 case Dict_kind:
4011 case DictComp_kind:
4012 return &PyDict_Type;
4013 case Set_kind:
4014 case SetComp_kind:
4015 return &PySet_Type;
4016 case GeneratorExp_kind:
4017 return &PyGen_Type;
4018 case Lambda_kind:
4019 return &PyFunction_Type;
4020 case JoinedStr_kind:
4021 case FormattedValue_kind:
4022 return &PyUnicode_Type;
4023 case Constant_kind:
Victor Stinnera102ed72020-02-07 02:24:48 +01004024 return Py_TYPE(e->v.Constant.value);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004025 default:
4026 return NULL;
4027 }
4028}
4029
4030static int
4031check_caller(struct compiler *c, expr_ty e)
4032{
4033 switch (e->kind) {
4034 case Constant_kind:
4035 case Tuple_kind:
4036 case List_kind:
4037 case ListComp_kind:
4038 case Dict_kind:
4039 case DictComp_kind:
4040 case Set_kind:
4041 case SetComp_kind:
4042 case GeneratorExp_kind:
4043 case JoinedStr_kind:
4044 case FormattedValue_kind:
4045 return compiler_warn(c, "'%.200s' object is not callable; "
4046 "perhaps you missed a comma?",
4047 infer_type(e)->tp_name);
4048 default:
4049 return 1;
4050 }
4051}
4052
4053static int
4054check_subscripter(struct compiler *c, expr_ty e)
4055{
4056 PyObject *v;
4057
4058 switch (e->kind) {
4059 case Constant_kind:
4060 v = e->v.Constant.value;
4061 if (!(v == Py_None || v == Py_Ellipsis ||
4062 PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
4063 PyAnySet_Check(v)))
4064 {
4065 return 1;
4066 }
4067 /* fall through */
4068 case Set_kind:
4069 case SetComp_kind:
4070 case GeneratorExp_kind:
4071 case Lambda_kind:
4072 return compiler_warn(c, "'%.200s' object is not subscriptable; "
4073 "perhaps you missed a comma?",
4074 infer_type(e)->tp_name);
4075 default:
4076 return 1;
4077 }
4078}
4079
4080static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004081check_index(struct compiler *c, expr_ty e, expr_ty s)
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004082{
4083 PyObject *v;
4084
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004085 PyTypeObject *index_type = infer_type(s);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004086 if (index_type == NULL
4087 || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
4088 || index_type == &PySlice_Type) {
4089 return 1;
4090 }
4091
4092 switch (e->kind) {
4093 case Constant_kind:
4094 v = e->v.Constant.value;
4095 if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
4096 return 1;
4097 }
4098 /* fall through */
4099 case Tuple_kind:
4100 case List_kind:
4101 case ListComp_kind:
4102 case JoinedStr_kind:
4103 case FormattedValue_kind:
4104 return compiler_warn(c, "%.200s indices must be integers or slices, "
4105 "not %.200s; "
4106 "perhaps you missed a comma?",
4107 infer_type(e)->tp_name,
4108 index_type->tp_name);
4109 default:
4110 return 1;
4111 }
4112}
4113
Zackery Spytz97f5de02019-03-22 01:30:32 -06004114// Return 1 if the method call was optimized, -1 if not, and 0 on error.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004115static int
Yury Selivanovf2392132016-12-13 19:03:51 -05004116maybe_optimize_method_call(struct compiler *c, expr_ty e)
4117{
4118 Py_ssize_t argsl, i;
4119 expr_ty meth = e->v.Call.func;
Pablo Galindoa5634c42020-09-16 19:42:00 +01004120 asdl_expr_seq *args = e->v.Call.args;
Yury Selivanovf2392132016-12-13 19:03:51 -05004121
4122 /* Check that the call node is an attribute access, and that
4123 the call doesn't have keyword parameters. */
4124 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
4125 asdl_seq_LEN(e->v.Call.keywords))
4126 return -1;
4127
4128 /* Check that there are no *varargs types of arguments. */
4129 argsl = asdl_seq_LEN(args);
4130 for (i = 0; i < argsl; i++) {
4131 expr_ty elt = asdl_seq_GET(args, i);
4132 if (elt->kind == Starred_kind) {
4133 return -1;
4134 }
4135 }
4136
4137 /* Alright, we can optimize the code. */
4138 VISIT(c, expr, meth->v.Attribute.value);
Mark Shannond48848c2021-03-14 18:01:30 +00004139 int old_lineno = c->u->u_lineno;
4140 c->u->u_lineno = meth->end_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004141 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
4142 VISIT_SEQ(c, expr, e->v.Call.args);
4143 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
Mark Shannond48848c2021-03-14 18:01:30 +00004144 c->u->u_lineno = old_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004145 return 1;
4146}
4147
4148static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004149validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
Zackery Spytz08050e92020-04-06 00:47:47 -06004150{
4151 Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
4152 for (Py_ssize_t i = 0; i < nkeywords; i++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004153 keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
4154 if (key->arg == NULL) {
4155 continue;
4156 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +01004157 if (forbidden_name(c, key->arg, Store)) {
4158 return -1;
4159 }
Zackery Spytz08050e92020-04-06 00:47:47 -06004160 for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004161 keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
4162 if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004163 c->u->u_col_offset = other->col_offset;
Brandt Bucher145bf262021-02-26 14:51:55 -08004164 compiler_error(c, "keyword argument repeated: %U", key->arg);
Pablo Galindo254ec782020-04-03 20:37:13 +01004165 return -1;
4166 }
4167 }
4168 }
4169 return 0;
4170}
4171
4172static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004173compiler_call(struct compiler *c, expr_ty e)
4174{
Zackery Spytz97f5de02019-03-22 01:30:32 -06004175 int ret = maybe_optimize_method_call(c, e);
4176 if (ret >= 0) {
4177 return ret;
4178 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004179 if (!check_caller(c, e->v.Call.func)) {
4180 return 0;
4181 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004182 VISIT(c, expr, e->v.Call.func);
4183 return compiler_call_helper(c, 0,
4184 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004185 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004186}
4187
Eric V. Smith235a6f02015-09-19 14:51:32 -04004188static int
4189compiler_joined_str(struct compiler *c, expr_ty e)
4190{
Eric V. Smith235a6f02015-09-19 14:51:32 -04004191 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004192 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1)
4193 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
Eric V. Smith235a6f02015-09-19 14:51:32 -04004194 return 1;
4195}
4196
Eric V. Smitha78c7952015-11-03 12:45:05 -05004197/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004198static int
4199compiler_formatted_value(struct compiler *c, expr_ty e)
4200{
Eric V. Smitha78c7952015-11-03 12:45:05 -05004201 /* Our oparg encodes 2 pieces of information: the conversion
4202 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04004203
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004204 Convert the conversion char to 3 bits:
4205 : 000 0x0 FVC_NONE The default if nothing specified.
Eric V. Smitha78c7952015-11-03 12:45:05 -05004206 !s : 001 0x1 FVC_STR
4207 !r : 010 0x2 FVC_REPR
4208 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04004209
Eric V. Smitha78c7952015-11-03 12:45:05 -05004210 next bit is whether or not we have a format spec:
4211 yes : 100 0x4
4212 no : 000 0x0
4213 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004214
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004215 int conversion = e->v.FormattedValue.conversion;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004216 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004217
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004218 /* The expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004219 VISIT(c, expr, e->v.FormattedValue.value);
4220
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004221 switch (conversion) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004222 case 's': oparg = FVC_STR; break;
4223 case 'r': oparg = FVC_REPR; break;
4224 case 'a': oparg = FVC_ASCII; break;
4225 case -1: oparg = FVC_NONE; break;
4226 default:
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004227 PyErr_Format(PyExc_SystemError,
4228 "Unrecognized conversion character %d", conversion);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004229 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004230 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004231 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004232 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004233 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004234 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004235 }
4236
Eric V. Smitha78c7952015-11-03 12:45:05 -05004237 /* And push our opcode and oparg */
4238 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004239
Eric V. Smith235a6f02015-09-19 14:51:32 -04004240 return 1;
4241}
4242
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004243static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004244compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004245{
4246 Py_ssize_t i, n = end - begin;
4247 keyword_ty kw;
4248 PyObject *keys, *key;
4249 assert(n > 0);
4250 if (n > 1) {
4251 for (i = begin; i < end; i++) {
4252 kw = asdl_seq_GET(keywords, i);
4253 VISIT(c, expr, kw->value);
4254 }
4255 keys = PyTuple_New(n);
4256 if (keys == NULL) {
4257 return 0;
4258 }
4259 for (i = begin; i < end; i++) {
4260 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
4261 Py_INCREF(key);
4262 PyTuple_SET_ITEM(keys, i - begin, key);
4263 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004264 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004265 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
4266 }
4267 else {
4268 /* a for loop only executes once */
4269 for (i = begin; i < end; i++) {
4270 kw = asdl_seq_GET(keywords, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004271 ADDOP_LOAD_CONST(c, kw->arg);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004272 VISIT(c, expr, kw->value);
4273 }
4274 ADDOP_I(c, BUILD_MAP, n);
4275 }
4276 return 1;
4277}
4278
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004279/* shared code between compiler_call and compiler_class */
4280static int
4281compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01004282 int n, /* Args already pushed */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004283 asdl_expr_seq *args,
4284 asdl_keyword_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004285{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004286 Py_ssize_t i, nseen, nelts, nkwelts;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004287
Pablo Galindo254ec782020-04-03 20:37:13 +01004288 if (validate_keywords(c, keywords) == -1) {
4289 return 0;
4290 }
4291
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004292 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004293 nkwelts = asdl_seq_LEN(keywords);
4294
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004295 for (i = 0; i < nelts; i++) {
4296 expr_ty elt = asdl_seq_GET(args, i);
4297 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00004298 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004299 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004300 }
4301 for (i = 0; i < nkwelts; i++) {
4302 keyword_ty kw = asdl_seq_GET(keywords, i);
4303 if (kw->arg == NULL) {
4304 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004305 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004306 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004307
Mark Shannon13bc1392020-01-23 09:25:17 +00004308 /* No * or ** args, so can use faster calling sequence */
4309 for (i = 0; i < nelts; i++) {
4310 expr_ty elt = asdl_seq_GET(args, i);
4311 assert(elt->kind != Starred_kind);
4312 VISIT(c, expr, elt);
4313 }
4314 if (nkwelts) {
4315 PyObject *names;
4316 VISIT_SEQ(c, keyword, keywords);
4317 names = PyTuple_New(nkwelts);
4318 if (names == NULL) {
4319 return 0;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004320 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004321 for (i = 0; i < nkwelts; i++) {
4322 keyword_ty kw = asdl_seq_GET(keywords, i);
4323 Py_INCREF(kw->arg);
4324 PyTuple_SET_ITEM(names, i, kw->arg);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004325 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004326 ADDOP_LOAD_CONST_NEW(c, names);
4327 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
4328 return 1;
4329 }
4330 else {
4331 ADDOP_I(c, CALL_FUNCTION, n + nelts);
4332 return 1;
4333 }
4334
4335ex_call:
4336
4337 /* Do positional arguments. */
4338 if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
4339 VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
4340 }
4341 else if (starunpack_helper(c, args, n, BUILD_LIST,
4342 LIST_APPEND, LIST_EXTEND, 1) == 0) {
4343 return 0;
4344 }
4345 /* Then keyword arguments */
4346 if (nkwelts) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004347 /* Has a new dict been pushed */
4348 int have_dict = 0;
Mark Shannon13bc1392020-01-23 09:25:17 +00004349
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004350 nseen = 0; /* the number of keyword arguments on the stack following */
4351 for (i = 0; i < nkwelts; i++) {
4352 keyword_ty kw = asdl_seq_GET(keywords, i);
4353 if (kw->arg == NULL) {
4354 /* A keyword argument unpacking. */
4355 if (nseen) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004356 if (!compiler_subkwargs(c, keywords, i - nseen, i)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004357 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004358 }
Mark Shannondb64f122020-06-01 10:42:42 +01004359 if (have_dict) {
4360 ADDOP_I(c, DICT_MERGE, 1);
4361 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004362 have_dict = 1;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004363 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004364 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004365 if (!have_dict) {
4366 ADDOP_I(c, BUILD_MAP, 0);
4367 have_dict = 1;
4368 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004369 VISIT(c, expr, kw->value);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004370 ADDOP_I(c, DICT_MERGE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004371 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004372 else {
4373 nseen++;
4374 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004375 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004376 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004377 /* Pack up any trailing keyword arguments. */
Mark Shannon8a4cd702020-01-27 09:57:45 +00004378 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004379 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004380 }
4381 if (have_dict) {
4382 ADDOP_I(c, DICT_MERGE, 1);
4383 }
4384 have_dict = 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004385 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004386 assert(have_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004387 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004388 ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0);
4389 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004390}
4391
Nick Coghlan650f0d02007-04-15 12:05:43 +00004392
4393/* List and set comprehensions and generator expressions work by creating a
4394 nested function to perform the actual iteration. This means that the
4395 iteration variables don't leak into the current scope.
4396 The defined function is called immediately following its definition, with the
4397 result of that call being the result of the expression.
4398 The LC/SC version returns the populated container, while the GE version is
4399 flagged in symtable.c as a generator, so it returns the generator object
4400 when the function is called.
Nick Coghlan650f0d02007-04-15 12:05:43 +00004401
4402 Possible cleanups:
4403 - iterate over the generator sequence instead of using recursion
4404*/
4405
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004406
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004407static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004408compiler_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004409 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004410 int depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004411 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004412{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004413 comprehension_ty gen;
4414 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4415 if (gen->is_async) {
4416 return compiler_async_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004417 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004418 } else {
4419 return compiler_sync_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004420 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004421 }
4422}
4423
4424static int
4425compiler_sync_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004426 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004427 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004428 expr_ty elt, expr_ty val, int type)
4429{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004430 /* generate code for the iterator, then each of the ifs,
4431 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004433 comprehension_ty gen;
4434 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004435 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004437 start = compiler_new_block(c);
4438 skip = compiler_new_block(c);
4439 if_cleanup = compiler_new_block(c);
4440 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004442 if (start == NULL || skip == NULL || if_cleanup == NULL ||
4443 anchor == NULL)
4444 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004446 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004448 if (gen_index == 0) {
4449 /* Receive outermost iter as an implicit argument */
4450 c->u->u_argcount = 1;
4451 ADDOP_I(c, LOAD_FAST, 0);
4452 }
4453 else {
4454 /* Sub-iter - calculate on the fly */
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004455 /* Fast path for the temporary variable assignment idiom:
4456 for y in [f(x)]
4457 */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004458 asdl_expr_seq *elts;
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004459 switch (gen->iter->kind) {
4460 case List_kind:
4461 elts = gen->iter->v.List.elts;
4462 break;
4463 case Tuple_kind:
4464 elts = gen->iter->v.Tuple.elts;
4465 break;
4466 default:
4467 elts = NULL;
4468 }
4469 if (asdl_seq_LEN(elts) == 1) {
4470 expr_ty elt = asdl_seq_GET(elts, 0);
4471 if (elt->kind != Starred_kind) {
4472 VISIT(c, expr, elt);
4473 start = NULL;
4474 }
4475 }
4476 if (start) {
4477 VISIT(c, expr, gen->iter);
4478 ADDOP(c, GET_ITER);
4479 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004480 }
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004481 if (start) {
4482 depth++;
4483 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01004484 ADDOP_JUMP(c, FOR_ITER, anchor);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004485 NEXT_BLOCK(c);
4486 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004487 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 /* XXX this needs to be cleaned up...a lot! */
4490 n = asdl_seq_LEN(gen->ifs);
4491 for (i = 0; i < n; i++) {
4492 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004493 if (!compiler_jump_if(c, e, if_cleanup, 0))
4494 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004495 NEXT_BLOCK(c);
4496 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 if (++gen_index < asdl_seq_LEN(generators))
4499 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004500 generators, gen_index, depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 elt, val, type))
4502 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004504 /* only append after the last for generator */
4505 if (gen_index >= asdl_seq_LEN(generators)) {
4506 /* comprehension specific code */
4507 switch (type) {
4508 case COMP_GENEXP:
4509 VISIT(c, expr, elt);
4510 ADDOP(c, YIELD_VALUE);
4511 ADDOP(c, POP_TOP);
4512 break;
4513 case COMP_LISTCOMP:
4514 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004515 ADDOP_I(c, LIST_APPEND, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004516 break;
4517 case COMP_SETCOMP:
4518 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004519 ADDOP_I(c, SET_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004520 break;
4521 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004522 /* With '{k: v}', k is evaluated before v, so we do
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004523 the same. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004524 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004525 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004526 ADDOP_I(c, MAP_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004527 break;
4528 default:
4529 return 0;
4530 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004532 compiler_use_next_block(c, skip);
4533 }
4534 compiler_use_next_block(c, if_cleanup);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004535 if (start) {
Mark Shannon582aaf12020-08-04 17:30:11 +01004536 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004537 compiler_use_next_block(c, anchor);
4538 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004539
4540 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004541}
4542
4543static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004544compiler_async_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004545 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004546 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004547 expr_ty elt, expr_ty val, int type)
4548{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004549 comprehension_ty gen;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004550 basicblock *start, *if_cleanup, *except;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004551 Py_ssize_t i, n;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004552 start = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004553 except = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004554 if_cleanup = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004555
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004556 if (start == NULL || if_cleanup == NULL || except == NULL) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004557 return 0;
4558 }
4559
4560 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4561
4562 if (gen_index == 0) {
4563 /* Receive outermost iter as an implicit argument */
4564 c->u->u_argcount = 1;
4565 ADDOP_I(c, LOAD_FAST, 0);
4566 }
4567 else {
4568 /* Sub-iter - calculate on the fly */
4569 VISIT(c, expr, gen->iter);
4570 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004571 }
4572
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004573 compiler_use_next_block(c, start);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004574
Mark Shannon582aaf12020-08-04 17:30:11 +01004575 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004576 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004577 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004578 ADDOP(c, YIELD_FROM);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004579 ADDOP(c, POP_BLOCK);
Serhiy Storchaka24d32012018-03-10 18:22:34 +02004580 VISIT(c, expr, gen->target);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004581
4582 n = asdl_seq_LEN(gen->ifs);
4583 for (i = 0; i < n; i++) {
4584 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004585 if (!compiler_jump_if(c, e, if_cleanup, 0))
4586 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004587 NEXT_BLOCK(c);
4588 }
4589
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004590 depth++;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004591 if (++gen_index < asdl_seq_LEN(generators))
4592 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004593 generators, gen_index, depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004594 elt, val, type))
4595 return 0;
4596
4597 /* only append after the last for generator */
4598 if (gen_index >= asdl_seq_LEN(generators)) {
4599 /* comprehension specific code */
4600 switch (type) {
4601 case COMP_GENEXP:
4602 VISIT(c, expr, elt);
4603 ADDOP(c, YIELD_VALUE);
4604 ADDOP(c, POP_TOP);
4605 break;
4606 case COMP_LISTCOMP:
4607 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004608 ADDOP_I(c, LIST_APPEND, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004609 break;
4610 case COMP_SETCOMP:
4611 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004612 ADDOP_I(c, SET_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004613 break;
4614 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004615 /* With '{k: v}', k is evaluated before v, so we do
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004616 the same. */
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004617 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004618 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004619 ADDOP_I(c, MAP_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004620 break;
4621 default:
4622 return 0;
4623 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004624 }
4625 compiler_use_next_block(c, if_cleanup);
Mark Shannon582aaf12020-08-04 17:30:11 +01004626 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004627
4628 compiler_use_next_block(c, except);
4629 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004630
4631 return 1;
4632}
4633
4634static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004635compiler_comprehension(struct compiler *c, expr_ty e, int type,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004636 identifier name, asdl_comprehension_seq *generators, expr_ty elt,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004637 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004638{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004639 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004640 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004641 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004642 int is_async_generator = 0;
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004643 int top_level_await = IS_TOP_LEVEL_AWAIT(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004644
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004645
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004646 int is_async_function = c->u->u_ste->ste_coroutine;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004647
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004648 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004649 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4650 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004651 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004652 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004653 }
4654
4655 is_async_generator = c->u->u_ste->ste_coroutine;
4656
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004657 if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004658 compiler_error(c, "asynchronous comprehension outside of "
4659 "an asynchronous function");
4660 goto error_in_scope;
4661 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004663 if (type != COMP_GENEXP) {
4664 int op;
4665 switch (type) {
4666 case COMP_LISTCOMP:
4667 op = BUILD_LIST;
4668 break;
4669 case COMP_SETCOMP:
4670 op = BUILD_SET;
4671 break;
4672 case COMP_DICTCOMP:
4673 op = BUILD_MAP;
4674 break;
4675 default:
4676 PyErr_Format(PyExc_SystemError,
4677 "unknown comprehension type %d", type);
4678 goto error_in_scope;
4679 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004681 ADDOP_I(c, op, 0);
4682 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004683
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004684 if (!compiler_comprehension_generator(c, generators, 0, 0, elt,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004685 val, type))
4686 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004688 if (type != COMP_GENEXP) {
4689 ADDOP(c, RETURN_VALUE);
4690 }
4691
4692 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004693 qualname = c->u->u_qualname;
4694 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004695 compiler_exit_scope(c);
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004696 if (top_level_await && is_async_generator){
4697 c->u->u_ste->ste_coroutine = 1;
4698 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004699 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004700 goto error;
4701
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004702 if (!compiler_make_closure(c, co, 0, qualname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004703 goto error;
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004704 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004705 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004706 Py_DECREF(co);
4707
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004708 VISIT(c, expr, outermost->iter);
4709
4710 if (outermost->is_async) {
4711 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004712 } else {
4713 ADDOP(c, GET_ITER);
4714 }
4715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004716 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004717
4718 if (is_async_generator && type != COMP_GENEXP) {
4719 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004720 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004721 ADDOP(c, YIELD_FROM);
4722 }
4723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004724 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004725error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004726 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004727error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004728 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004729 Py_XDECREF(co);
4730 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004731}
4732
4733static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004734compiler_genexp(struct compiler *c, expr_ty e)
4735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004736 static identifier name;
4737 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004738 name = PyUnicode_InternFromString("<genexpr>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004739 if (!name)
4740 return 0;
4741 }
4742 assert(e->kind == GeneratorExp_kind);
4743 return compiler_comprehension(c, e, COMP_GENEXP, name,
4744 e->v.GeneratorExp.generators,
4745 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004746}
4747
4748static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004749compiler_listcomp(struct compiler *c, expr_ty e)
4750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004751 static identifier name;
4752 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004753 name = PyUnicode_InternFromString("<listcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004754 if (!name)
4755 return 0;
4756 }
4757 assert(e->kind == ListComp_kind);
4758 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4759 e->v.ListComp.generators,
4760 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004761}
4762
4763static int
4764compiler_setcomp(struct compiler *c, expr_ty e)
4765{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004766 static identifier name;
4767 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004768 name = PyUnicode_InternFromString("<setcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004769 if (!name)
4770 return 0;
4771 }
4772 assert(e->kind == SetComp_kind);
4773 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4774 e->v.SetComp.generators,
4775 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004776}
4777
4778
4779static int
4780compiler_dictcomp(struct compiler *c, expr_ty e)
4781{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004782 static identifier name;
4783 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004784 name = PyUnicode_InternFromString("<dictcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004785 if (!name)
4786 return 0;
4787 }
4788 assert(e->kind == DictComp_kind);
4789 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4790 e->v.DictComp.generators,
4791 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004792}
4793
4794
4795static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004796compiler_visit_keyword(struct compiler *c, keyword_ty k)
4797{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004798 VISIT(c, expr, k->value);
4799 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004800}
4801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004802/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004803 whether they are true or false.
4804
4805 Return values: 1 for true, 0 for false, -1 for non-constant.
4806 */
4807
4808static int
Mark Shannonfee55262019-11-21 09:11:43 +00004809compiler_with_except_finish(struct compiler *c) {
4810 basicblock *exit;
4811 exit = compiler_new_block(c);
4812 if (exit == NULL)
4813 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01004814 ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit);
Mark Shannon266b4622020-11-17 19:30:14 +00004815 NEXT_BLOCK(c);
Mark Shannonbf353f32020-12-17 13:55:28 +00004816 ADDOP_I(c, RERAISE, 1);
Mark Shannonfee55262019-11-21 09:11:43 +00004817 compiler_use_next_block(c, exit);
4818 ADDOP(c, POP_TOP);
4819 ADDOP(c, POP_TOP);
4820 ADDOP(c, POP_TOP);
4821 ADDOP(c, POP_EXCEPT);
4822 ADDOP(c, POP_TOP);
4823 return 1;
4824}
Yury Selivanov75445082015-05-11 22:57:16 -04004825
4826/*
4827 Implements the async with statement.
4828
4829 The semantics outlined in that PEP are as follows:
4830
4831 async with EXPR as VAR:
4832 BLOCK
4833
4834 It is implemented roughly as:
4835
4836 context = EXPR
4837 exit = context.__aexit__ # not calling it
4838 value = await context.__aenter__()
4839 try:
4840 VAR = value # if VAR present in the syntax
4841 BLOCK
4842 finally:
4843 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004844 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04004845 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004846 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04004847 if not (await exit(*exc)):
4848 raise
4849 */
4850static int
4851compiler_async_with(struct compiler *c, stmt_ty s, int pos)
4852{
Mark Shannonfee55262019-11-21 09:11:43 +00004853 basicblock *block, *final, *exit;
Yury Selivanov75445082015-05-11 22:57:16 -04004854 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
4855
4856 assert(s->kind == AsyncWith_kind);
Pablo Galindo90235812020-03-15 04:29:22 +00004857 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07004858 c->u->u_ste->ste_coroutine = 1;
4859 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
Zsolt Dollensteine2396502018-04-27 08:58:56 -07004860 return compiler_error(c, "'async with' outside async function");
4861 }
Yury Selivanov75445082015-05-11 22:57:16 -04004862
4863 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004864 final = compiler_new_block(c);
4865 exit = compiler_new_block(c);
4866 if (!block || !final || !exit)
Yury Selivanov75445082015-05-11 22:57:16 -04004867 return 0;
4868
4869 /* Evaluate EXPR */
4870 VISIT(c, expr, item->context_expr);
4871
4872 ADDOP(c, BEFORE_ASYNC_WITH);
4873 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004874 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004875 ADDOP(c, YIELD_FROM);
4876
Mark Shannon582aaf12020-08-04 17:30:11 +01004877 ADDOP_JUMP(c, SETUP_ASYNC_WITH, final);
Yury Selivanov75445082015-05-11 22:57:16 -04004878
4879 /* SETUP_ASYNC_WITH pushes a finally block. */
4880 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004881 if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004882 return 0;
4883 }
4884
4885 if (item->optional_vars) {
4886 VISIT(c, expr, item->optional_vars);
4887 }
4888 else {
4889 /* Discard result from context.__aenter__() */
4890 ADDOP(c, POP_TOP);
4891 }
4892
4893 pos++;
4894 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
4895 /* BLOCK code */
4896 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
4897 else if (!compiler_async_with(c, s, pos))
4898 return 0;
4899
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004900 compiler_pop_fblock(c, ASYNC_WITH, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004901 ADDOP(c, POP_BLOCK);
4902 /* End of body; start the cleanup */
Yury Selivanov75445082015-05-11 22:57:16 -04004903
Mark Shannonfee55262019-11-21 09:11:43 +00004904 /* For successful outcome:
4905 * call __exit__(None, None, None)
4906 */
4907 if(!compiler_call_exit_with_nones(c))
Yury Selivanov75445082015-05-11 22:57:16 -04004908 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00004909 ADDOP(c, GET_AWAITABLE);
4910 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4911 ADDOP(c, YIELD_FROM);
Yury Selivanov75445082015-05-11 22:57:16 -04004912
Mark Shannonfee55262019-11-21 09:11:43 +00004913 ADDOP(c, POP_TOP);
Yury Selivanov75445082015-05-11 22:57:16 -04004914
Mark Shannon582aaf12020-08-04 17:30:11 +01004915 ADDOP_JUMP(c, JUMP_ABSOLUTE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00004916
4917 /* For exceptional outcome: */
4918 compiler_use_next_block(c, final);
4919
4920 ADDOP(c, WITH_EXCEPT_START);
Yury Selivanov75445082015-05-11 22:57:16 -04004921 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004922 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004923 ADDOP(c, YIELD_FROM);
Mark Shannonfee55262019-11-21 09:11:43 +00004924 compiler_with_except_finish(c);
Yury Selivanov75445082015-05-11 22:57:16 -04004925
Mark Shannonfee55262019-11-21 09:11:43 +00004926compiler_use_next_block(c, exit);
Yury Selivanov75445082015-05-11 22:57:16 -04004927 return 1;
4928}
4929
4930
Guido van Rossumc2e20742006-02-27 22:32:47 +00004931/*
4932 Implements the with statement from PEP 343.
Guido van Rossumc2e20742006-02-27 22:32:47 +00004933 with EXPR as VAR:
4934 BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00004935 is implemented as:
4936 <code for EXPR>
4937 SETUP_WITH E
4938 <code to store to VAR> or POP_TOP
4939 <code for BLOCK>
4940 LOAD_CONST (None, None, None)
4941 CALL_FUNCTION_EX 0
4942 JUMP_FORWARD EXIT
4943 E: WITH_EXCEPT_START (calls EXPR.__exit__)
4944 POP_JUMP_IF_TRUE T:
4945 RERAISE
4946 T: POP_TOP * 3 (remove exception from stack)
4947 POP_EXCEPT
4948 POP_TOP
4949 EXIT:
Guido van Rossumc2e20742006-02-27 22:32:47 +00004950 */
Mark Shannonfee55262019-11-21 09:11:43 +00004951
Guido van Rossumc2e20742006-02-27 22:32:47 +00004952static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004953compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004954{
Mark Shannonfee55262019-11-21 09:11:43 +00004955 basicblock *block, *final, *exit;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004956 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004957
4958 assert(s->kind == With_kind);
4959
Guido van Rossumc2e20742006-02-27 22:32:47 +00004960 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004961 final = compiler_new_block(c);
4962 exit = compiler_new_block(c);
4963 if (!block || !final || !exit)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004964 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004965
Thomas Wouters477c8d52006-05-27 19:21:47 +00004966 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004967 VISIT(c, expr, item->context_expr);
Mark Shannonfee55262019-11-21 09:11:43 +00004968 /* Will push bound __exit__ */
Mark Shannon582aaf12020-08-04 17:30:11 +01004969 ADDOP_JUMP(c, SETUP_WITH, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004970
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004971 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00004972 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004973 if (!compiler_push_fblock(c, WITH, block, final, NULL)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004974 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004975 }
4976
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004977 if (item->optional_vars) {
4978 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004979 }
4980 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004981 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004982 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004983 }
4984
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004985 pos++;
4986 if (pos == asdl_seq_LEN(s->v.With.items))
4987 /* BLOCK code */
4988 VISIT_SEQ(c, stmt, s->v.With.body)
4989 else if (!compiler_with(c, s, pos))
4990 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004991
Mark Shannon3bd60352021-01-13 12:05:43 +00004992
4993 /* Mark all following code as artificial */
4994 c->u->u_lineno = -1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004995 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004996 compiler_pop_fblock(c, WITH, block);
Mark Shannon13bc1392020-01-23 09:25:17 +00004997
Mark Shannonfee55262019-11-21 09:11:43 +00004998 /* End of body; start the cleanup. */
Mark Shannon13bc1392020-01-23 09:25:17 +00004999
Mark Shannonfee55262019-11-21 09:11:43 +00005000 /* For successful outcome:
5001 * call __exit__(None, None, None)
5002 */
5003 if (!compiler_call_exit_with_nones(c))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005004 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00005005 ADDOP(c, POP_TOP);
Mark Shannon582aaf12020-08-04 17:30:11 +01005006 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005007
Mark Shannonfee55262019-11-21 09:11:43 +00005008 /* For exceptional outcome: */
5009 compiler_use_next_block(c, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005010
Mark Shannonfee55262019-11-21 09:11:43 +00005011 ADDOP(c, WITH_EXCEPT_START);
5012 compiler_with_except_finish(c);
5013
5014 compiler_use_next_block(c, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005015 return 1;
5016}
5017
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005018static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005019compiler_visit_expr1(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005020{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005021 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07005022 case NamedExpr_kind:
5023 VISIT(c, expr, e->v.NamedExpr.value);
5024 ADDOP(c, DUP_TOP);
5025 VISIT(c, expr, e->v.NamedExpr.target);
5026 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005027 case BoolOp_kind:
5028 return compiler_boolop(c, e);
5029 case BinOp_kind:
5030 VISIT(c, expr, e->v.BinOp.left);
5031 VISIT(c, expr, e->v.BinOp.right);
Andy Lester76d58772020-03-10 21:18:12 -05005032 ADDOP(c, binop(e->v.BinOp.op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005033 break;
5034 case UnaryOp_kind:
5035 VISIT(c, expr, e->v.UnaryOp.operand);
5036 ADDOP(c, unaryop(e->v.UnaryOp.op));
5037 break;
5038 case Lambda_kind:
5039 return compiler_lambda(c, e);
5040 case IfExp_kind:
5041 return compiler_ifexp(c, e);
5042 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005043 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005044 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005045 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005046 case GeneratorExp_kind:
5047 return compiler_genexp(c, e);
5048 case ListComp_kind:
5049 return compiler_listcomp(c, e);
5050 case SetComp_kind:
5051 return compiler_setcomp(c, e);
5052 case DictComp_kind:
5053 return compiler_dictcomp(c, e);
5054 case Yield_kind:
5055 if (c->u->u_ste->ste_type != FunctionBlock)
5056 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005057 if (e->v.Yield.value) {
5058 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005059 }
5060 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005061 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005062 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005063 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005064 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005065 case YieldFrom_kind:
5066 if (c->u->u_ste->ste_type != FunctionBlock)
5067 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04005068
5069 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
5070 return compiler_error(c, "'yield from' inside async function");
5071
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005072 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04005073 ADDOP(c, GET_YIELD_FROM_ITER);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005074 ADDOP_LOAD_CONST(c, Py_None);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005075 ADDOP(c, YIELD_FROM);
5076 break;
Yury Selivanov75445082015-05-11 22:57:16 -04005077 case Await_kind:
Pablo Galindo90235812020-03-15 04:29:22 +00005078 if (!IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005079 if (c->u->u_ste->ste_type != FunctionBlock){
5080 return compiler_error(c, "'await' outside function");
5081 }
Yury Selivanov75445082015-05-11 22:57:16 -04005082
Victor Stinner331a6a52019-05-27 16:39:22 +02005083 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005084 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
5085 return compiler_error(c, "'await' outside async function");
5086 }
5087 }
Yury Selivanov75445082015-05-11 22:57:16 -04005088
5089 VISIT(c, expr, e->v.Await.value);
5090 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005091 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005092 ADDOP(c, YIELD_FROM);
5093 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005094 case Compare_kind:
5095 return compiler_compare(c, e);
5096 case Call_kind:
5097 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005098 case Constant_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005099 ADDOP_LOAD_CONST(c, e->v.Constant.value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005100 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005101 case JoinedStr_kind:
5102 return compiler_joined_str(c, e);
5103 case FormattedValue_kind:
5104 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005105 /* The following exprs can be assignment targets. */
5106 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005107 VISIT(c, expr, e->v.Attribute.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005108 switch (e->v.Attribute.ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005109 case Load:
Mark Shannond48848c2021-03-14 18:01:30 +00005110 {
5111 int old_lineno = c->u->u_lineno;
5112 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005113 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005114 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005115 break;
Mark Shannond48848c2021-03-14 18:01:30 +00005116 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005117 case Store:
Mark Shannond48848c2021-03-14 18:01:30 +00005118 if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005119 return 0;
Mark Shannond48848c2021-03-14 18:01:30 +00005120 }
5121 int old_lineno = c->u->u_lineno;
5122 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005123 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005124 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005125 break;
5126 case Del:
5127 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
5128 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005129 }
5130 break;
5131 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005132 return compiler_subscript(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005133 case Starred_kind:
5134 switch (e->v.Starred.ctx) {
5135 case Store:
5136 /* In all legitimate cases, the Starred node was already replaced
5137 * by compiler_list/compiler_tuple. XXX: is that okay? */
5138 return compiler_error(c,
5139 "starred assignment target must be in a list or tuple");
5140 default:
5141 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005142 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005143 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005144 break;
5145 case Slice_kind:
5146 return compiler_slice(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005147 case Name_kind:
5148 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
5149 /* child nodes of List and Tuple will have expr_context set */
5150 case List_kind:
5151 return compiler_list(c, e);
5152 case Tuple_kind:
5153 return compiler_tuple(c, e);
Brandt Bucher145bf262021-02-26 14:51:55 -08005154 case MatchAs_kind:
5155 case MatchOr_kind:
5156 // Can only occur in patterns, which are handled elsewhere.
5157 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005158 }
5159 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005160}
5161
5162static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005163compiler_visit_expr(struct compiler *c, expr_ty e)
5164{
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005165 int old_lineno = c->u->u_lineno;
5166 int old_col_offset = c->u->u_col_offset;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005167 SET_LOC(c, e);
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005168 int res = compiler_visit_expr1(c, e);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005169 c->u->u_lineno = old_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005170 c->u->u_col_offset = old_col_offset;
5171 return res;
5172}
5173
5174static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005175compiler_augassign(struct compiler *c, stmt_ty s)
5176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005177 assert(s->kind == AugAssign_kind);
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005178 expr_ty e = s->v.AugAssign.target;
5179
5180 int old_lineno = c->u->u_lineno;
5181 int old_col_offset = c->u->u_col_offset;
5182 SET_LOC(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005184 switch (e->kind) {
5185 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005186 VISIT(c, expr, e->v.Attribute.value);
5187 ADDOP(c, DUP_TOP);
Mark Shannond48848c2021-03-14 18:01:30 +00005188 int old_lineno = c->u->u_lineno;
5189 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005190 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005191 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005192 break;
5193 case Subscript_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005194 VISIT(c, expr, e->v.Subscript.value);
5195 VISIT(c, expr, e->v.Subscript.slice);
5196 ADDOP(c, DUP_TOP_TWO);
5197 ADDOP(c, BINARY_SUBSCR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005198 break;
5199 case Name_kind:
5200 if (!compiler_nameop(c, e->v.Name.id, Load))
5201 return 0;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005202 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005203 default:
5204 PyErr_Format(PyExc_SystemError,
5205 "invalid node type (%d) for augmented assignment",
5206 e->kind);
5207 return 0;
5208 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005209
5210 c->u->u_lineno = old_lineno;
5211 c->u->u_col_offset = old_col_offset;
5212
5213 VISIT(c, expr, s->v.AugAssign.value);
5214 ADDOP(c, inplace_binop(s->v.AugAssign.op));
5215
5216 SET_LOC(c, e);
5217
5218 switch (e->kind) {
5219 case Attribute_kind:
Mark Shannond48848c2021-03-14 18:01:30 +00005220 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005221 ADDOP(c, ROT_TWO);
5222 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5223 break;
5224 case Subscript_kind:
5225 ADDOP(c, ROT_THREE);
5226 ADDOP(c, STORE_SUBSCR);
5227 break;
5228 case Name_kind:
5229 return compiler_nameop(c, e->v.Name.id, Store);
5230 default:
5231 Py_UNREACHABLE();
5232 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005233 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005234}
5235
5236static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005237check_ann_expr(struct compiler *c, expr_ty e)
5238{
5239 VISIT(c, expr, e);
5240 ADDOP(c, POP_TOP);
5241 return 1;
5242}
5243
5244static int
5245check_annotation(struct compiler *c, stmt_ty s)
5246{
5247 /* Annotations are only evaluated in a module or class. */
5248 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5249 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
5250 return check_ann_expr(c, s->v.AnnAssign.annotation);
5251 }
5252 return 1;
5253}
5254
5255static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005256check_ann_subscr(struct compiler *c, expr_ty e)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005257{
5258 /* We check that everything in a subscript is defined at runtime. */
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005259 switch (e->kind) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005260 case Slice_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005261 if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005262 return 0;
5263 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005264 if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) {
5265 return 0;
5266 }
5267 if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) {
5268 return 0;
5269 }
5270 return 1;
5271 case Tuple_kind: {
5272 /* extended slice */
Pablo Galindoa5634c42020-09-16 19:42:00 +01005273 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005274 Py_ssize_t i, n = asdl_seq_LEN(elts);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005275 for (i = 0; i < n; i++) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005276 if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005277 return 0;
5278 }
5279 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005280 return 1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005281 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005282 default:
5283 return check_ann_expr(c, e);
5284 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005285}
5286
5287static int
5288compiler_annassign(struct compiler *c, stmt_ty s)
5289{
5290 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07005291 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005292
5293 assert(s->kind == AnnAssign_kind);
5294
5295 /* We perform the actual assignment first. */
5296 if (s->v.AnnAssign.value) {
5297 VISIT(c, expr, s->v.AnnAssign.value);
5298 VISIT(c, expr, targ);
5299 }
5300 switch (targ->kind) {
5301 case Name_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005302 if (forbidden_name(c, targ->v.Name.id, Store))
5303 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005304 /* If we have a simple name in a module or class, store annotation. */
5305 if (s->v.AnnAssign.simple &&
5306 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5307 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Batuhan Taskaya044a1042020-10-06 23:03:02 +03005308 VISIT(c, annexpr, s->v.AnnAssign.annotation);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005309 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02005310 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005311 ADDOP_LOAD_CONST_NEW(c, mangled);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005312 ADDOP(c, STORE_SUBSCR);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005313 }
5314 break;
5315 case Attribute_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005316 if (forbidden_name(c, targ->v.Attribute.attr, Store))
5317 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005318 if (!s->v.AnnAssign.value &&
5319 !check_ann_expr(c, targ->v.Attribute.value)) {
5320 return 0;
5321 }
5322 break;
5323 case Subscript_kind:
5324 if (!s->v.AnnAssign.value &&
5325 (!check_ann_expr(c, targ->v.Subscript.value) ||
5326 !check_ann_subscr(c, targ->v.Subscript.slice))) {
5327 return 0;
5328 }
5329 break;
5330 default:
5331 PyErr_Format(PyExc_SystemError,
5332 "invalid node type (%d) for annotated assignment",
5333 targ->kind);
5334 return 0;
5335 }
5336 /* Annotation is evaluated last. */
5337 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
5338 return 0;
5339 }
5340 return 1;
5341}
5342
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005343/* Raises a SyntaxError and returns 0.
5344 If something goes wrong, a different exception may be raised.
5345*/
5346
5347static int
Brandt Bucher145bf262021-02-26 14:51:55 -08005348compiler_error(struct compiler *c, const char *format, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005349{
Brandt Bucher145bf262021-02-26 14:51:55 -08005350 va_list vargs;
5351#ifdef HAVE_STDARG_PROTOTYPES
5352 va_start(vargs, format);
5353#else
5354 va_start(vargs);
5355#endif
5356 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5357 va_end(vargs);
5358 if (msg == NULL) {
5359 return 0;
5360 }
5361 PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
5362 if (loc == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005363 Py_INCREF(Py_None);
5364 loc = Py_None;
5365 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005366 PyObject *args = Py_BuildValue("O(OiiO)", msg, c->c_filename,
5367 c->u->u_lineno, c->u->u_col_offset + 1, loc);
5368 Py_DECREF(msg);
5369 if (args == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005370 goto exit;
Brandt Bucher145bf262021-02-26 14:51:55 -08005371 }
5372 PyErr_SetObject(PyExc_SyntaxError, args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005373 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005374 Py_DECREF(loc);
Brandt Bucher145bf262021-02-26 14:51:55 -08005375 Py_XDECREF(args);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005376 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005377}
5378
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005379/* Emits a SyntaxWarning and returns 1 on success.
5380 If a SyntaxWarning raised as error, replaces it with a SyntaxError
5381 and returns 0.
5382*/
5383static int
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005384compiler_warn(struct compiler *c, const char *format, ...)
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005385{
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005386 va_list vargs;
5387#ifdef HAVE_STDARG_PROTOTYPES
5388 va_start(vargs, format);
5389#else
5390 va_start(vargs);
5391#endif
5392 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5393 va_end(vargs);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005394 if (msg == NULL) {
5395 return 0;
5396 }
5397 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
5398 c->u->u_lineno, NULL, NULL) < 0)
5399 {
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005400 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005401 /* Replace the SyntaxWarning exception with a SyntaxError
5402 to get a more accurate error report */
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005403 PyErr_Clear();
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005404 assert(PyUnicode_AsUTF8(msg) != NULL);
5405 compiler_error(c, PyUnicode_AsUTF8(msg));
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005406 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005407 Py_DECREF(msg);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005408 return 0;
5409 }
5410 Py_DECREF(msg);
5411 return 1;
5412}
5413
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005414static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005415compiler_subscript(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005416{
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005417 expr_context_ty ctx = e->v.Subscript.ctx;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005418 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005419
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005420 if (ctx == Load) {
5421 if (!check_subscripter(c, e->v.Subscript.value)) {
5422 return 0;
5423 }
5424 if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
5425 return 0;
5426 }
5427 }
5428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005429 switch (ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005430 case Load: op = BINARY_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005431 case Store: op = STORE_SUBSCR; break;
5432 case Del: op = DELETE_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005433 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005434 assert(op);
5435 VISIT(c, expr, e->v.Subscript.value);
5436 VISIT(c, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005437 ADDOP(c, op);
5438 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005439}
5440
5441static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005442compiler_slice(struct compiler *c, expr_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005444 int n = 2;
5445 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005447 /* only handles the cases where BUILD_SLICE is emitted */
5448 if (s->v.Slice.lower) {
5449 VISIT(c, expr, s->v.Slice.lower);
5450 }
5451 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005452 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005453 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005455 if (s->v.Slice.upper) {
5456 VISIT(c, expr, s->v.Slice.upper);
5457 }
5458 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005459 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005460 }
5461
5462 if (s->v.Slice.step) {
5463 n++;
5464 VISIT(c, expr, s->v.Slice.step);
5465 }
5466 ADDOP_I(c, BUILD_SLICE, n);
5467 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005468}
5469
Brandt Bucher145bf262021-02-26 14:51:55 -08005470
5471// PEP 634: Structural Pattern Matching
5472
5473// To keep things simple, all compiler_pattern_* routines follow the convention
5474// of replacing TOS (the subject for the given pattern) with either True (match)
5475// or False (no match). We do this even for irrefutable patterns; the idea is
5476// that it's much easier to smooth out any redundant pushing, popping, and
5477// jumping in the peephole optimizer than to detect or predict it here.
5478
5479
5480#define WILDCARD_CHECK(N) \
5481 ((N)->kind == Name_kind && \
5482 _PyUnicode_EqualToASCIIString((N)->v.Name.id, "_"))
5483
5484
5485static int
5486pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc)
5487{
5488 assert(!_PyUnicode_EqualToASCIIString(n, "_"));
5489 // Can't assign to the same name twice:
5490 if (pc->stores == NULL) {
5491 RETURN_IF_FALSE(pc->stores = PySet_New(NULL));
5492 }
5493 else {
5494 int duplicate = PySet_Contains(pc->stores, n);
5495 if (duplicate < 0) {
5496 return 0;
5497 }
5498 if (duplicate) {
5499 const char *e = "multiple assignments to name %R in pattern";
5500 return compiler_error(c, e, n);
5501 }
5502 }
5503 RETURN_IF_FALSE(!PySet_Add(pc->stores, n));
5504 RETURN_IF_FALSE(compiler_nameop(c, n, Store));
5505 return 1;
5506}
5507
5508
5509static int
5510pattern_helper_sequence_unpack(struct compiler *c, asdl_expr_seq *values,
5511 Py_ssize_t star, pattern_context *pc)
5512{
5513 RETURN_IF_FALSE(unpack_helper(c, values));
5514 // We've now got a bunch of new subjects on the stack. If any of them fail
5515 // to match, we need to pop everything else off, then finally push False.
5516 // fails is an array of blocks that correspond to the necessary amount of
5517 // popping for each element:
5518 basicblock **fails;
5519 Py_ssize_t size = asdl_seq_LEN(values);
5520 fails = (basicblock **)PyObject_Malloc(sizeof(basicblock*) * size);
5521 if (fails == NULL) {
5522 PyErr_NoMemory();
5523 return 0;
5524 }
5525 // NOTE: Can't use our nice returning macros anymore: they'll leak memory!
5526 // goto error on error.
5527 for (Py_ssize_t i = 0; i < size; i++) {
5528 fails[i] = compiler_new_block(c);
5529 if (fails[i] == NULL) {
5530 goto error;
5531 }
5532 }
5533 for (Py_ssize_t i = 0; i < size; i++) {
5534 expr_ty value = asdl_seq_GET(values, i);
5535 if (i == star) {
5536 assert(value->kind == Starred_kind);
5537 value = value->v.Starred.value;
5538 }
5539 if (!compiler_pattern_subpattern(c, value, pc) ||
5540 !compiler_addop_j(c, POP_JUMP_IF_FALSE, fails[i]) ||
5541 compiler_next_block(c) == NULL)
5542 {
5543 goto error;
5544 }
5545 }
5546 // Success!
5547 basicblock *end = compiler_new_block(c);
5548 if (end == NULL ||
5549 !compiler_addop_load_const(c, Py_True) ||
5550 !compiler_addop_j(c, JUMP_FORWARD, end))
5551 {
5552 goto error;
5553 }
5554 // This is where we handle failed sub-patterns. For a sequence pattern like
5555 // [a, b, c, d], this will look like:
5556 // fails[0]: POP_TOP
5557 // fails[1]: POP_TOP
5558 // fails[2]: POP_TOP
5559 // fails[3]: LOAD_CONST False
5560 for (Py_ssize_t i = 0; i < size - 1; i++) {
5561 compiler_use_next_block(c, fails[i]);
5562 if (!compiler_addop(c, POP_TOP)) {
5563 goto error;
5564 }
5565 }
5566 compiler_use_next_block(c, fails[size - 1]);
5567 if (!compiler_addop_load_const(c, Py_False)) {
5568 goto error;
5569 }
5570 compiler_use_next_block(c, end);
5571 PyObject_Free(fails);
5572 return 1;
5573error:
5574 PyObject_Free(fails);
5575 return 0;
5576}
5577
5578// Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of
5579// UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a
5580// starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc.
5581static int
5582pattern_helper_sequence_subscr(struct compiler *c, asdl_expr_seq *values,
5583 Py_ssize_t star, pattern_context *pc)
5584{
5585 basicblock *end, *fail_pop_1;
5586 RETURN_IF_FALSE(end = compiler_new_block(c));
5587 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5588 Py_ssize_t size = asdl_seq_LEN(values);
5589 for (Py_ssize_t i = 0; i < size; i++) {
5590 expr_ty value = asdl_seq_GET(values, i);
5591 if (WILDCARD_CHECK(value)) {
5592 continue;
5593 }
5594 if (i == star) {
5595 assert(value->kind == Starred_kind);
5596 assert(WILDCARD_CHECK(value->v.Starred.value));
5597 continue;
5598 }
5599 ADDOP(c, DUP_TOP);
5600 if (i < star) {
5601 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5602 }
5603 else {
5604 // The subject may not support negative indexing! Compute a
5605 // nonnegative index:
5606 ADDOP(c, GET_LEN);
5607 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i));
5608 ADDOP(c, BINARY_SUBTRACT);
5609 }
5610 ADDOP(c, BINARY_SUBSCR);
5611 RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc));
5612 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5613 NEXT_BLOCK(c);
5614 }
5615 ADDOP(c, POP_TOP);
5616 ADDOP_LOAD_CONST(c, Py_True);
5617 ADDOP_JUMP(c, JUMP_FORWARD, end);
5618 compiler_use_next_block(c, fail_pop_1);
5619 ADDOP(c, POP_TOP);
5620 ADDOP_LOAD_CONST(c, Py_False);
5621 compiler_use_next_block(c, end);
5622 return 1;
5623}
5624
5625
5626// Like compiler_pattern, but turn off checks for irrefutability.
5627static int
5628compiler_pattern_subpattern(struct compiler *c, expr_ty p, pattern_context *pc)
5629{
5630 int allow_irrefutable = pc->allow_irrefutable;
5631 pc->allow_irrefutable = 1;
5632 RETURN_IF_FALSE(compiler_pattern(c, p, pc));
5633 pc->allow_irrefutable = allow_irrefutable;
5634 return 1;
5635}
5636
5637
5638static int
5639compiler_pattern_as(struct compiler *c, expr_ty p, pattern_context *pc)
5640{
5641 assert(p->kind == MatchAs_kind);
5642 basicblock *end, *fail_pop_1;
5643 RETURN_IF_FALSE(end = compiler_new_block(c));
5644 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5645 // Need to make a copy for (possibly) storing later:
5646 ADDOP(c, DUP_TOP);
5647 RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc));
5648 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5649 NEXT_BLOCK(c);
5650 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc));
5651 ADDOP_LOAD_CONST(c, Py_True);
5652 ADDOP_JUMP(c, JUMP_FORWARD, end);
5653 compiler_use_next_block(c, fail_pop_1);
5654 // Need to pop that unused copy from before:
5655 ADDOP(c, POP_TOP);
5656 ADDOP_LOAD_CONST(c, Py_False);
5657 compiler_use_next_block(c, end);
5658 return 1;
5659}
5660
5661
5662static int
5663compiler_pattern_capture(struct compiler *c, expr_ty p, pattern_context *pc)
5664{
5665 assert(p->kind == Name_kind);
5666 assert(p->v.Name.ctx == Store);
5667 assert(!WILDCARD_CHECK(p));
5668 if (!pc->allow_irrefutable) {
5669 // Whoops, can't have a name capture here!
5670 const char *e = "name capture %R makes remaining patterns unreachable";
5671 return compiler_error(c, e, p->v.Name.id);
5672 }
5673 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.Name.id, pc));
5674 ADDOP_LOAD_CONST(c, Py_True);
5675 return 1;
5676}
5677
5678
5679static int
5680compiler_pattern_class(struct compiler *c, expr_ty p, pattern_context *pc)
5681{
5682 asdl_expr_seq *args = p->v.Call.args;
5683 asdl_keyword_seq *kwargs = p->v.Call.keywords;
5684 Py_ssize_t nargs = asdl_seq_LEN(args);
5685 Py_ssize_t nkwargs = asdl_seq_LEN(kwargs);
5686 if (INT_MAX < nargs || INT_MAX < nargs + nkwargs - 1) {
5687 const char *e = "too many sub-patterns in class pattern %R";
5688 return compiler_error(c, e, p->v.Call.func);
5689 }
5690 RETURN_IF_FALSE(!validate_keywords(c, kwargs));
5691 basicblock *end, *fail_pop_1;
5692 RETURN_IF_FALSE(end = compiler_new_block(c));
5693 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5694 VISIT(c, expr, p->v.Call.func);
5695 PyObject *kwnames;
5696 RETURN_IF_FALSE(kwnames = PyTuple_New(nkwargs));
5697 Py_ssize_t i;
5698 for (i = 0; i < nkwargs; i++) {
5699 PyObject *name = ((keyword_ty) asdl_seq_GET(kwargs, i))->arg;
5700 Py_INCREF(name);
5701 PyTuple_SET_ITEM(kwnames, i, name);
5702 }
5703 ADDOP_LOAD_CONST_NEW(c, kwnames);
5704 ADDOP_I(c, MATCH_CLASS, nargs);
5705 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5706 NEXT_BLOCK(c);
5707 // TOS is now a tuple of (nargs + nkwargs) attributes.
5708 for (i = 0; i < nargs + nkwargs; i++) {
5709 expr_ty arg;
5710 if (i < nargs) {
5711 // Positional:
5712 arg = asdl_seq_GET(args, i);
5713 }
5714 else {
5715 // Keyword:
5716 arg = ((keyword_ty) asdl_seq_GET(kwargs, i - nargs))->value;
5717 }
5718 if (WILDCARD_CHECK(arg)) {
5719 continue;
5720 }
5721 // Get the i-th attribute, and match it against the i-th pattern:
5722 ADDOP(c, DUP_TOP);
5723 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5724 ADDOP(c, BINARY_SUBSCR);
5725 RETURN_IF_FALSE(compiler_pattern_subpattern(c, arg, pc));
5726 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5727 NEXT_BLOCK(c);
5728 }
5729 // Success! Pop the tuple of attributes:
5730 ADDOP(c, POP_TOP);
5731 ADDOP_LOAD_CONST(c, Py_True);
5732 ADDOP_JUMP(c, JUMP_FORWARD, end);
5733 compiler_use_next_block(c, fail_pop_1);
5734 ADDOP(c, POP_TOP);
5735 ADDOP_LOAD_CONST(c, Py_False);
5736 compiler_use_next_block(c, end);
5737 return 1;
5738}
5739
5740
5741static int
5742compiler_pattern_literal(struct compiler *c, expr_ty p, pattern_context *pc)
5743{
5744 assert(p->kind == Constant_kind);
5745 PyObject *v = p->v.Constant.value;
5746 ADDOP_LOAD_CONST(c, v);
5747 // Literal True, False, and None are compared by identity. All others use
5748 // equality:
5749 ADDOP_COMPARE(c, (v == Py_None || PyBool_Check(v)) ? Is : Eq);
5750 return 1;
5751}
5752
5753
5754static int
5755compiler_pattern_mapping(struct compiler *c, expr_ty p, pattern_context *pc)
5756{
5757 basicblock *end, *fail_pop_1, *fail_pop_3;
5758 RETURN_IF_FALSE(end = compiler_new_block(c));
5759 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5760 RETURN_IF_FALSE(fail_pop_3 = compiler_new_block(c));
5761 asdl_expr_seq *keys = p->v.Dict.keys;
5762 asdl_expr_seq *values = p->v.Dict.values;
5763 Py_ssize_t size = asdl_seq_LEN(values);
Ikko Ashimine57827f82021-03-10 19:39:51 +09005764 // A starred pattern will be a keyless value. It is guaranteed to be last:
Brandt Bucher145bf262021-02-26 14:51:55 -08005765 int star = size ? !asdl_seq_GET(keys, size - 1) : 0;
5766 ADDOP(c, MATCH_MAPPING);
5767 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5768 NEXT_BLOCK(c);
5769 if (!size) {
5770 // If the pattern is just "{}", we're done!
5771 ADDOP(c, POP_TOP);
5772 ADDOP_LOAD_CONST(c, Py_True);
5773 ADDOP_JUMP(c, JUMP_FORWARD, end);
5774 compiler_use_next_block(c, fail_pop_1);
5775 ADDOP(c, POP_TOP);
5776 ADDOP_LOAD_CONST(c, Py_False);
5777 compiler_use_next_block(c, end);
5778 return 1;
5779 }
5780 if (size - star) {
5781 // If the pattern has any keys in it, perform a length check:
5782 ADDOP(c, GET_LEN);
5783 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - star));
5784 ADDOP_COMPARE(c, GtE);
5785 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5786 NEXT_BLOCK(c);
5787 }
5788 if (INT_MAX < size - star - 1) {
5789 return compiler_error(c, "too many sub-patterns in mapping pattern");
5790 }
5791 // Collect all of the keys into a tuple for MATCH_KEYS and
5792 // COPY_DICT_WITHOUT_KEYS. They can either be dotted names or literals:
5793 for (Py_ssize_t i = 0; i < size - star; i++) {
5794 expr_ty key = asdl_seq_GET(keys, i);
5795 if (key == NULL) {
5796 const char *e = "can't use starred name here "
5797 "(consider moving to end)";
5798 return compiler_error(c, e);
5799 }
5800 VISIT(c, expr, key);
5801 }
5802 ADDOP_I(c, BUILD_TUPLE, size - star);
5803 ADDOP(c, MATCH_KEYS);
5804 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3);
5805 NEXT_BLOCK(c);
5806 // So far so good. There's now a tuple of values on the stack to match
5807 // sub-patterns against:
5808 for (Py_ssize_t i = 0; i < size - star; i++) {
5809 expr_ty value = asdl_seq_GET(values, i);
5810 if (WILDCARD_CHECK(value)) {
5811 continue;
5812 }
5813 ADDOP(c, DUP_TOP);
5814 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5815 ADDOP(c, BINARY_SUBSCR);
5816 RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc));
5817 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3);
5818 NEXT_BLOCK(c);
5819 }
5820 // If we get this far, it's a match! We're done with that tuple of values.
5821 ADDOP(c, POP_TOP);
5822 if (star) {
5823 // If we had a starred name, bind a dict of remaining items to it:
5824 ADDOP(c, COPY_DICT_WITHOUT_KEYS);
5825 PyObject *id = asdl_seq_GET(values, size - 1)->v.Name.id;
5826 RETURN_IF_FALSE(pattern_helper_store_name(c, id, pc));
5827 }
5828 else {
5829 // Otherwise, we don't care about this tuple of keys anymore:
5830 ADDOP(c, POP_TOP);
5831 }
5832 // Pop the subject:
5833 ADDOP(c, POP_TOP);
5834 ADDOP_LOAD_CONST(c, Py_True);
5835 ADDOP_JUMP(c, JUMP_FORWARD, end);
5836 // The top two items are a tuple of values or None, followed by a tuple of
5837 // keys. Pop them both:
5838 compiler_use_next_block(c, fail_pop_3);
5839 ADDOP(c, POP_TOP);
5840 ADDOP(c, POP_TOP);
5841 compiler_use_next_block(c, fail_pop_1);
5842 // Pop the subject:
5843 ADDOP(c, POP_TOP);
5844 ADDOP_LOAD_CONST(c, Py_False);
5845 compiler_use_next_block(c, end);
5846 return 1;
5847}
5848
5849
5850static int
5851compiler_pattern_or(struct compiler *c, expr_ty p, pattern_context *pc)
5852{
5853 assert(p->kind == MatchOr_kind);
5854 // control is the set of names bound by the first alternative. If all of the
5855 // others bind the same names (they should), then this becomes pc->stores.
5856 PyObject *control = NULL;
5857 basicblock *end, *pass_pop_1;
5858 RETURN_IF_FALSE(end = compiler_new_block(c));
5859 RETURN_IF_FALSE(pass_pop_1 = compiler_new_block(c));
5860 Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns);
5861 assert(size > 1);
5862 // We're going to be messing with pc. Keep the original info handy:
5863 PyObject *stores_init = pc->stores;
5864 int allow_irrefutable = pc->allow_irrefutable;
5865 for (Py_ssize_t i = 0; i < size; i++) {
5866 // NOTE: Can't use our nice returning macros in here: they'll leak sets!
5867 expr_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i);
5868 pc->stores = PySet_New(stores_init);
5869 // An irrefutable sub-pattern must be last, if it is allowed at all:
5870 int is_last = i == size - 1;
5871 pc->allow_irrefutable = allow_irrefutable && is_last;
5872 SET_LOC(c, alt);
5873 if (pc->stores == NULL ||
5874 // Only copy the subject if we're *not* on the last alternative:
5875 (!is_last && !compiler_addop(c, DUP_TOP)) ||
5876 !compiler_pattern(c, alt, pc) ||
5877 // Only jump if we're *not* on the last alternative:
5878 (!is_last && !compiler_addop_j(c, POP_JUMP_IF_TRUE, pass_pop_1)) ||
5879 !compiler_next_block(c))
5880 {
5881 goto fail;
5882 }
5883 if (!i) {
5884 // If this is the first alternative, save its stores as a "control"
5885 // for the others (they can't bind a different set of names):
5886 control = pc->stores;
5887 continue;
5888 }
5889 if (PySet_GET_SIZE(pc->stores) || PySet_GET_SIZE(control)) {
5890 // Otherwise, check to see if we differ from the control set:
5891 PyObject *diff = PyNumber_InPlaceXor(pc->stores, control);
5892 if (diff == NULL) {
5893 goto fail;
5894 }
5895 if (PySet_GET_SIZE(diff)) {
5896 // The names differ! Raise.
5897 Py_DECREF(diff);
5898 compiler_error(c, "alternative patterns bind different names");
5899 goto fail;
5900 }
5901 Py_DECREF(diff);
5902 }
5903 Py_DECREF(pc->stores);
5904 }
5905 Py_XDECREF(stores_init);
5906 // Update pc->stores and restore pc->allow_irrefutable:
5907 pc->stores = control;
5908 pc->allow_irrefutable = allow_irrefutable;
5909 ADDOP_JUMP(c, JUMP_FORWARD, end);
5910 compiler_use_next_block(c, pass_pop_1);
5911 ADDOP(c, POP_TOP);
5912 ADDOP_LOAD_CONST(c, Py_True);
5913 compiler_use_next_block(c, end);
5914 return 1;
5915fail:
5916 Py_XDECREF(stores_init);
5917 Py_XDECREF(control);
5918 return 0;
5919}
5920
5921
5922static int
5923compiler_pattern_sequence(struct compiler *c, expr_ty p, pattern_context *pc)
5924{
5925 assert(p->kind == List_kind || p->kind == Tuple_kind);
5926 asdl_expr_seq *values = (p->kind == Tuple_kind) ? p->v.Tuple.elts
5927 : p->v.List.elts;
5928 Py_ssize_t size = asdl_seq_LEN(values);
5929 Py_ssize_t star = -1;
5930 int only_wildcard = 1;
5931 int star_wildcard = 0;
5932 // Find a starred name, if it exists. There may be at most one:
5933 for (Py_ssize_t i = 0; i < size; i++) {
5934 expr_ty value = asdl_seq_GET(values, i);
5935 if (value->kind == Starred_kind) {
5936 value = value->v.Starred.value;
5937 if (star >= 0) {
5938 const char *e = "multiple starred names in sequence pattern";
5939 return compiler_error(c, e);
5940 }
5941 star_wildcard = WILDCARD_CHECK(value);
5942 star = i;
5943 }
5944 only_wildcard &= WILDCARD_CHECK(value);
5945 }
5946 basicblock *end, *fail_pop_1;
5947 RETURN_IF_FALSE(end = compiler_new_block(c));
5948 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5949 ADDOP(c, MATCH_SEQUENCE);
5950 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5951 NEXT_BLOCK(c);
5952 if (star < 0) {
5953 // No star: len(subject) == size
5954 ADDOP(c, GET_LEN);
5955 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
5956 ADDOP_COMPARE(c, Eq);
5957 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5958 NEXT_BLOCK(c);
5959 }
5960 else if (size > 1) {
5961 // Star: len(subject) >= size - 1
5962 ADDOP(c, GET_LEN);
5963 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1));
5964 ADDOP_COMPARE(c, GtE);
5965 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5966 NEXT_BLOCK(c);
5967 }
5968 if (only_wildcard) {
5969 // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc.
5970 ADDOP(c, POP_TOP);
5971 ADDOP_LOAD_CONST(c, Py_True);
5972 }
5973 else if (star_wildcard) {
5974 RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, values, star, pc));
5975 }
5976 else {
5977 RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, values, star, pc));
5978 }
5979 ADDOP_JUMP(c, JUMP_FORWARD, end);
5980 compiler_use_next_block(c, fail_pop_1);
5981 ADDOP(c, POP_TOP)
5982 ADDOP_LOAD_CONST(c, Py_False);
5983 compiler_use_next_block(c, end);
5984 return 1;
5985}
5986
5987
5988static int
5989compiler_pattern_value(struct compiler *c, expr_ty p, pattern_context *pc)
5990{
5991 assert(p->kind == Attribute_kind);
5992 assert(p->v.Attribute.ctx == Load);
5993 VISIT(c, expr, p);
5994 ADDOP_COMPARE(c, Eq);
5995 return 1;
5996}
5997
5998
5999static int
6000compiler_pattern_wildcard(struct compiler *c, expr_ty p, pattern_context *pc)
6001{
6002 assert(p->kind == Name_kind);
6003 assert(p->v.Name.ctx == Store);
6004 assert(WILDCARD_CHECK(p));
6005 if (!pc->allow_irrefutable) {
6006 // Whoops, can't have a wildcard here!
6007 const char *e = "wildcard makes remaining patterns unreachable";
6008 return compiler_error(c, e);
6009 }
6010 ADDOP(c, POP_TOP);
6011 ADDOP_LOAD_CONST(c, Py_True);
6012 return 1;
6013}
6014
6015
6016static int
6017compiler_pattern(struct compiler *c, expr_ty p, pattern_context *pc)
6018{
6019 SET_LOC(c, p);
6020 switch (p->kind) {
6021 case Attribute_kind:
6022 return compiler_pattern_value(c, p, pc);
6023 case BinOp_kind:
6024 // Because we allow "2+2j", things like "2+2" make it this far:
6025 return compiler_error(c, "patterns cannot include operators");
6026 case Call_kind:
6027 return compiler_pattern_class(c, p, pc);
6028 case Constant_kind:
6029 return compiler_pattern_literal(c, p, pc);
6030 case Dict_kind:
6031 return compiler_pattern_mapping(c, p, pc);
6032 case JoinedStr_kind:
6033 // Because we allow strings, f-strings make it this far:
6034 return compiler_error(c, "patterns cannot include f-strings");
6035 case List_kind:
6036 case Tuple_kind:
6037 return compiler_pattern_sequence(c, p, pc);
6038 case MatchAs_kind:
6039 return compiler_pattern_as(c, p, pc);
6040 case MatchOr_kind:
6041 return compiler_pattern_or(c, p, pc);
6042 case Name_kind:
6043 if (WILDCARD_CHECK(p)) {
6044 return compiler_pattern_wildcard(c, p, pc);
6045 }
6046 return compiler_pattern_capture(c, p, pc);
6047 default:
6048 Py_UNREACHABLE();
6049 }
6050}
6051
6052
6053static int
6054compiler_match(struct compiler *c, stmt_ty s)
6055{
6056 VISIT(c, expr, s->v.Match.subject);
6057 basicblock *next, *end;
6058 RETURN_IF_FALSE(end = compiler_new_block(c));
6059 Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases);
6060 assert(cases);
6061 pattern_context pc;
6062 // We use pc.stores to track:
6063 // - Repeated name assignments in the same pattern.
6064 // - Different name assignments in alternatives.
6065 // It's a set of names, but we don't create it until it's needed:
6066 pc.stores = NULL;
6067 match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6068 int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases;
6069 for (Py_ssize_t i = 0; i < cases - has_default; i++) {
6070 m = asdl_seq_GET(s->v.Match.cases, i);
6071 SET_LOC(c, m->pattern);
6072 RETURN_IF_FALSE(next = compiler_new_block(c));
6073 // If pc.allow_irrefutable is 0, any name captures against our subject
6074 // will raise. Irrefutable cases must be either guarded, last, or both:
6075 pc.allow_irrefutable = m->guard != NULL || i == cases - 1;
6076 // Only copy the subject if we're *not* on the last case:
6077 if (i != cases - has_default - 1) {
6078 ADDOP(c, DUP_TOP);
6079 }
6080 int result = compiler_pattern(c, m->pattern, &pc);
6081 Py_CLEAR(pc.stores);
6082 RETURN_IF_FALSE(result);
6083 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, next);
6084 NEXT_BLOCK(c);
6085 if (m->guard) {
6086 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, next, 0));
6087 }
6088 // Success! Pop the subject off, we're done with it:
6089 if (i != cases - has_default - 1) {
6090 ADDOP(c, POP_TOP);
6091 }
6092 VISIT_SEQ(c, stmt, m->body);
6093 ADDOP_JUMP(c, JUMP_FORWARD, end);
6094 compiler_use_next_block(c, next);
6095 }
6096 if (has_default) {
6097 if (cases == 1) {
6098 // No matches. Done with the subject:
6099 ADDOP(c, POP_TOP);
6100 }
6101 // A trailing "case _" is common, and lets us save a bit of redundant
6102 // pushing and popping in the loop above:
6103 m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6104 SET_LOC(c, m->pattern);
6105 if (m->guard) {
6106 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0));
6107 }
6108 VISIT_SEQ(c, stmt, m->body);
6109 }
6110 compiler_use_next_block(c, end);
6111 return 1;
6112}
6113
6114
6115#undef WILDCARD_CHECK
6116
6117
Thomas Wouters89f507f2006-12-13 04:49:30 +00006118/* End of the compiler section, beginning of the assembler section */
6119
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006120/* do depth-first search of basic block graph, starting with block.
T. Wouters99b54d62019-09-12 07:05:33 -07006121 post records the block indices in post-order.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006122
6123 XXX must handle implicit jumps from one block to next
6124*/
6125
Thomas Wouters89f507f2006-12-13 04:49:30 +00006126struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006127 PyObject *a_bytecode; /* string containing bytecode */
6128 int a_offset; /* offset into bytecode */
6129 int a_nblocks; /* number of reachable blocks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006130 PyObject *a_lnotab; /* string containing lnotab */
6131 int a_lnotab_off; /* offset into lnotab */
Mark Shannon877df852020-11-12 09:43:29 +00006132 int a_prevlineno; /* lineno of last emitted line in line table */
6133 int a_lineno; /* lineno of last emitted instruction */
6134 int a_lineno_start; /* bytecode start offset of current lineno */
Mark Shannoncc75ab72020-11-12 19:49:33 +00006135 basicblock *a_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006136};
6137
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006138Py_LOCAL_INLINE(void)
6139stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006140{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02006141 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
Mark Shannonfee55262019-11-21 09:11:43 +00006142 if (b->b_startdepth < depth && b->b_startdepth < 100) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006143 assert(b->b_startdepth < 0);
6144 b->b_startdepth = depth;
6145 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02006146 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006147}
6148
6149/* Find the flow path that needs the largest stack. We assume that
6150 * cycles in the flow graph have no net effect on the stack depth.
6151 */
6152static int
6153stackdepth(struct compiler *c)
6154{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006155 basicblock *b, *entryblock = NULL;
6156 basicblock **stack, **sp;
6157 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006158 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006159 b->b_startdepth = INT_MIN;
6160 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006161 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006162 }
6163 if (!entryblock)
6164 return 0;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006165 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
6166 if (!stack) {
6167 PyErr_NoMemory();
6168 return -1;
6169 }
6170
6171 sp = stack;
6172 stackdepth_push(&sp, entryblock, 0);
6173 while (sp != stack) {
6174 b = *--sp;
6175 int depth = b->b_startdepth;
6176 assert(depth >= 0);
6177 basicblock *next = b->b_next;
6178 for (int i = 0; i < b->b_iused; i++) {
6179 struct instr *instr = &b->b_instr[i];
6180 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
6181 if (effect == PY_INVALID_STACK_EFFECT) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01006182 PyErr_Format(PyExc_SystemError,
6183 "compiler stack_effect(opcode=%d, arg=%i) failed",
6184 instr->i_opcode, instr->i_oparg);
6185 return -1;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006186 }
6187 int new_depth = depth + effect;
6188 if (new_depth > maxdepth) {
6189 maxdepth = new_depth;
6190 }
6191 assert(depth >= 0); /* invalid code or bug in stackdepth() */
Mark Shannon582aaf12020-08-04 17:30:11 +01006192 if (is_jump(instr)) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006193 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
6194 assert(effect != PY_INVALID_STACK_EFFECT);
6195 int target_depth = depth + effect;
6196 if (target_depth > maxdepth) {
6197 maxdepth = target_depth;
6198 }
6199 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006200 stackdepth_push(&sp, instr->i_target, target_depth);
6201 }
6202 depth = new_depth;
6203 if (instr->i_opcode == JUMP_ABSOLUTE ||
6204 instr->i_opcode == JUMP_FORWARD ||
6205 instr->i_opcode == RETURN_VALUE ||
Mark Shannonfee55262019-11-21 09:11:43 +00006206 instr->i_opcode == RAISE_VARARGS ||
6207 instr->i_opcode == RERAISE)
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006208 {
6209 /* remaining code is dead */
6210 next = NULL;
6211 break;
6212 }
6213 }
6214 if (next != NULL) {
Mark Shannon266b4622020-11-17 19:30:14 +00006215 assert(b->b_nofallthrough == 0);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006216 stackdepth_push(&sp, next, depth);
6217 }
6218 }
6219 PyObject_Free(stack);
6220 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006221}
6222
6223static int
6224assemble_init(struct assembler *a, int nblocks, int firstlineno)
6225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006226 memset(a, 0, sizeof(struct assembler));
Mark Shannon877df852020-11-12 09:43:29 +00006227 a->a_prevlineno = a->a_lineno = firstlineno;
Mark Shannonfd009e62020-11-13 12:53:53 +00006228 a->a_lnotab = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006229 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006230 if (a->a_bytecode == NULL) {
6231 goto error;
6232 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006233 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006234 if (a->a_lnotab == NULL) {
6235 goto error;
6236 }
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07006237 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006238 PyErr_NoMemory();
Mark Shannonfd009e62020-11-13 12:53:53 +00006239 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006240 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006241 return 1;
Mark Shannonfd009e62020-11-13 12:53:53 +00006242error:
6243 Py_XDECREF(a->a_bytecode);
6244 Py_XDECREF(a->a_lnotab);
6245 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006246}
6247
6248static void
6249assemble_free(struct assembler *a)
6250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006251 Py_XDECREF(a->a_bytecode);
6252 Py_XDECREF(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006253}
6254
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006255static int
6256blocksize(basicblock *b)
6257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006258 int i;
6259 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006261 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006262 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006263 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006264}
6265
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006266static int
Mark Shannon877df852020-11-12 09:43:29 +00006267assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006268{
Mark Shannon877df852020-11-12 09:43:29 +00006269 Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006270 if (a->a_lnotab_off + 2 >= len) {
6271 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
6272 return 0;
6273 }
Pablo Galindo86e322f2021-01-30 13:54:22 +00006274 unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab);
6275 lnotab += a->a_lnotab_off;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006276 a->a_lnotab_off += 2;
Mark Shannon877df852020-11-12 09:43:29 +00006277 *lnotab++ = bdelta;
6278 *lnotab++ = ldelta;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006279 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006280}
6281
Mark Shannon877df852020-11-12 09:43:29 +00006282/* Appends a range to the end of the line number table. See
6283 * Objects/lnotab_notes.txt for the description of the line number table. */
6284
6285static int
6286assemble_line_range(struct assembler *a)
6287{
6288 int ldelta, bdelta;
6289 bdelta = (a->a_offset - a->a_lineno_start) * 2;
6290 if (bdelta == 0) {
6291 return 1;
6292 }
6293 if (a->a_lineno < 0) {
6294 ldelta = -128;
6295 }
6296 else {
6297 ldelta = a->a_lineno - a->a_prevlineno;
6298 a->a_prevlineno = a->a_lineno;
6299 while (ldelta > 127) {
6300 if (!assemble_emit_linetable_pair(a, 0, 127)) {
6301 return 0;
6302 }
6303 ldelta -= 127;
6304 }
6305 while (ldelta < -127) {
6306 if (!assemble_emit_linetable_pair(a, 0, -127)) {
6307 return 0;
6308 }
6309 ldelta += 127;
6310 }
6311 }
6312 assert(-128 <= ldelta && ldelta < 128);
6313 while (bdelta > 254) {
6314 if (!assemble_emit_linetable_pair(a, 254, ldelta)) {
6315 return 0;
6316 }
6317 ldelta = a->a_lineno < 0 ? -128 : 0;
6318 bdelta -= 254;
6319 }
6320 if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) {
6321 return 0;
6322 }
6323 a->a_lineno_start = a->a_offset;
6324 return 1;
6325}
6326
6327static int
6328assemble_lnotab(struct assembler *a, struct instr *i)
6329{
6330 if (i->i_lineno == a->a_lineno) {
6331 return 1;
6332 }
6333 if (!assemble_line_range(a)) {
6334 return 0;
6335 }
6336 a->a_lineno = i->i_lineno;
6337 return 1;
6338}
6339
6340
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006341/* assemble_emit()
6342 Extend the bytecode with a new instruction.
6343 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00006344*/
6345
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006346static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006347assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006348{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006349 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006350 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03006351 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006352
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006353 arg = i->i_oparg;
6354 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006355 if (i->i_lineno && !assemble_lnotab(a, i))
6356 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006357 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006358 if (len > PY_SSIZE_T_MAX / 2)
6359 return 0;
6360 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
6361 return 0;
6362 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03006363 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006364 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006365 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006366 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006367}
6368
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00006369static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006370assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006371{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006372 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006373 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006374 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00006375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006376 /* Compute the size of each block and fixup jump args.
6377 Replace block pointer with position in bytecode. */
6378 do {
6379 totsize = 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006380 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006381 bsize = blocksize(b);
6382 b->b_offset = totsize;
6383 totsize += bsize;
6384 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006385 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006386 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6387 bsize = b->b_offset;
6388 for (i = 0; i < b->b_iused; i++) {
6389 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006390 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006391 /* Relative jumps are computed relative to
6392 the instruction pointer after fetching
6393 the jump instruction.
6394 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006395 bsize += isize;
Mark Shannon582aaf12020-08-04 17:30:11 +01006396 if (is_jump(instr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006397 instr->i_oparg = instr->i_target->b_offset;
Mark Shannon582aaf12020-08-04 17:30:11 +01006398 if (is_relative_jump(instr)) {
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006399 instr->i_oparg -= bsize;
6400 }
6401 if (instrsize(instr->i_oparg) != isize) {
6402 extended_arg_recompile = 1;
6403 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006404 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006405 }
6406 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00006407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006408 /* XXX: This is an awful hack that could hurt performance, but
6409 on the bright side it should work until we come up
6410 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006412 The issue is that in the first loop blocksize() is called
6413 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006414 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006415 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006417 So we loop until we stop seeing new EXTENDED_ARGs.
6418 The only EXTENDED_ARGs that could be popping up are
6419 ones in jump instructions. So this should converge
6420 fairly quickly.
6421 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006422 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00006423}
6424
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006425static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01006426dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006428 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006429 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006431 tuple = PyTuple_New(size);
6432 if (tuple == NULL)
6433 return NULL;
6434 while (PyDict_Next(dict, &pos, &k, &v)) {
6435 i = PyLong_AS_LONG(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006436 Py_INCREF(k);
6437 assert((i - offset) < size);
6438 assert((i - offset) >= 0);
6439 PyTuple_SET_ITEM(tuple, i - offset, k);
6440 }
6441 return tuple;
6442}
6443
6444static PyObject *
6445consts_dict_keys_inorder(PyObject *dict)
6446{
6447 PyObject *consts, *k, *v;
6448 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
6449
6450 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
6451 if (consts == NULL)
6452 return NULL;
6453 while (PyDict_Next(dict, &pos, &k, &v)) {
6454 i = PyLong_AS_LONG(v);
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +03006455 /* The keys of the dictionary can be tuples wrapping a contant.
6456 * (see compiler_add_o and _PyCode_ConstantKey). In that case
6457 * the object we want is always second. */
6458 if (PyTuple_CheckExact(k)) {
6459 k = PyTuple_GET_ITEM(k, 1);
6460 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006461 Py_INCREF(k);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006462 assert(i < size);
6463 assert(i >= 0);
6464 PyList_SET_ITEM(consts, i, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006465 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006466 return consts;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006467}
6468
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006469static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006470compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006472 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006473 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006474 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04006475 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006476 if (ste->ste_nested)
6477 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07006478 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006479 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07006480 if (!ste->ste_generator && ste->ste_coroutine)
6481 flags |= CO_COROUTINE;
6482 if (ste->ste_generator && ste->ste_coroutine)
6483 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006484 if (ste->ste_varargs)
6485 flags |= CO_VARARGS;
6486 if (ste->ste_varkeywords)
6487 flags |= CO_VARKEYWORDS;
6488 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006490 /* (Only) inherit compilerflags in PyCF_MASK */
6491 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006492
Pablo Galindo90235812020-03-15 04:29:22 +00006493 if ((IS_TOP_LEVEL_AWAIT(c)) &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07006494 ste->ste_coroutine &&
6495 !ste->ste_generator) {
6496 flags |= CO_COROUTINE;
6497 }
6498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006499 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00006500}
6501
Inada Naokibdb941b2021-02-10 09:20:42 +09006502// Merge *obj* with constant cache.
INADA Naokic2e16072018-11-26 21:23:22 +09006503// Unlike merge_consts_recursive(), this function doesn't work recursively.
6504static int
Inada Naokibdb941b2021-02-10 09:20:42 +09006505merge_const_one(struct compiler *c, PyObject **obj)
INADA Naokic2e16072018-11-26 21:23:22 +09006506{
Inada Naokibdb941b2021-02-10 09:20:42 +09006507 PyObject *key = _PyCode_ConstantKey(*obj);
INADA Naokic2e16072018-11-26 21:23:22 +09006508 if (key == NULL) {
6509 return 0;
6510 }
6511
6512 // t is borrowed reference
6513 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
6514 Py_DECREF(key);
6515 if (t == NULL) {
6516 return 0;
6517 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006518 if (t == key) { // obj is new constant.
INADA Naokic2e16072018-11-26 21:23:22 +09006519 return 1;
6520 }
6521
Inada Naokibdb941b2021-02-10 09:20:42 +09006522 if (PyTuple_CheckExact(t)) {
6523 // t is still borrowed reference
6524 t = PyTuple_GET_ITEM(t, 1);
6525 }
6526
6527 Py_INCREF(t);
6528 Py_DECREF(*obj);
6529 *obj = t;
INADA Naokic2e16072018-11-26 21:23:22 +09006530 return 1;
6531}
6532
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006533static PyCodeObject *
Mark Shannon6e8128f2020-07-30 10:03:00 +01006534makecode(struct compiler *c, struct assembler *a, PyObject *consts)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006536 PyCodeObject *co = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006537 PyObject *names = NULL;
6538 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006539 PyObject *name = NULL;
6540 PyObject *freevars = NULL;
6541 PyObject *cellvars = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006542 Py_ssize_t nlocals;
6543 int nlocals_int;
6544 int flags;
Pablo Galindocd74e662019-06-01 18:08:04 +01006545 int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006547 names = dict_keys_inorder(c->u->u_names, 0);
6548 varnames = dict_keys_inorder(c->u->u_varnames, 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006549 if (!names || !varnames) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006550 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006551 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006552 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
6553 if (!cellvars)
6554 goto error;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006555 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006556 if (!freevars)
6557 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006558
Inada Naokibdb941b2021-02-10 09:20:42 +09006559 if (!merge_const_one(c, &names) ||
6560 !merge_const_one(c, &varnames) ||
6561 !merge_const_one(c, &cellvars) ||
6562 !merge_const_one(c, &freevars))
INADA Naokic2e16072018-11-26 21:23:22 +09006563 {
6564 goto error;
6565 }
6566
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006567 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01006568 assert(nlocals < INT_MAX);
6569 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
6570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006571 flags = compute_code_flags(c);
6572 if (flags < 0)
6573 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006574
Mark Shannon6e8128f2020-07-30 10:03:00 +01006575 consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
6576 if (consts == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006577 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006578 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006579 if (!merge_const_one(c, &consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006580 Py_DECREF(consts);
INADA Naokic2e16072018-11-26 21:23:22 +09006581 goto error;
6582 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006583
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01006584 posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int);
Pablo Galindocd74e662019-06-01 18:08:04 +01006585 posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +01006586 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006587 maxdepth = stackdepth(c);
6588 if (maxdepth < 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006589 Py_DECREF(consts);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006590 goto error;
6591 }
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006592 co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
Mark Shannon13bc1392020-01-23 09:25:17 +00006593 posonlyargcount, kwonlyargcount, nlocals_int,
Mark Shannon6e8128f2020-07-30 10:03:00 +01006594 maxdepth, flags, a->a_bytecode, consts, names,
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006595 varnames, freevars, cellvars, c->c_filename,
6596 c->u->u_name, c->u->u_firstlineno, a->a_lnotab);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006597 Py_DECREF(consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006598 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006599 Py_XDECREF(names);
6600 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006601 Py_XDECREF(name);
6602 Py_XDECREF(freevars);
6603 Py_XDECREF(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006604 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006605}
6606
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006607
6608/* For debugging purposes only */
6609#if 0
6610static void
6611dump_instr(const struct instr *i)
6612{
Mark Shannon582aaf12020-08-04 17:30:11 +01006613 const char *jrel = (is_relative_jump(instr)) ? "jrel " : "";
6614 const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : "";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006615 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006617 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006618 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006619 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006620 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006621 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
6622 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006623}
6624
6625static void
6626dump_basicblock(const basicblock *b)
6627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006628 const char *b_return = b->b_return ? "return " : "";
Pablo Galindo60eb9f12020-06-28 01:55:47 +01006629 fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n",
6630 b->b_iused, b->b_startdepth, b->b_offset, b_return);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006631 if (b->b_instr) {
6632 int i;
6633 for (i = 0; i < b->b_iused; i++) {
6634 fprintf(stderr, " [%02d] ", i);
6635 dump_instr(b->b_instr + i);
6636 }
6637 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006638}
6639#endif
6640
Mark Shannon5977a792020-12-02 13:31:40 +00006641
6642static int
6643normalize_basic_block(basicblock *bb);
6644
Mark Shannon6e8128f2020-07-30 10:03:00 +01006645static int
6646optimize_cfg(struct assembler *a, PyObject *consts);
6647
Mark Shannon5977a792020-12-02 13:31:40 +00006648static int
6649ensure_exits_have_lineno(struct compiler *c);
6650
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006651static PyCodeObject *
6652assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006653{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006654 basicblock *b, *entryblock;
6655 struct assembler a;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006656 int j, nblocks;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006657 PyCodeObject *co = NULL;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006658 PyObject *consts = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006660 /* Make sure every block that falls off the end returns None.
6661 XXX NEXT_BLOCK() isn't quite right, because if the last
6662 block ends with a jump or return b_next shouldn't set.
6663 */
6664 if (!c->u->u_curblock->b_return) {
Mark Shannon877df852020-11-12 09:43:29 +00006665 c->u->u_lineno = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006666 if (addNone)
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006667 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006668 ADDOP(c, RETURN_VALUE);
6669 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006670
Mark Shannon5977a792020-12-02 13:31:40 +00006671 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
6672 if (normalize_basic_block(b)) {
Alex Henrie503627f2021-03-02 03:20:25 -07006673 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00006674 }
6675 }
6676
6677 if (ensure_exits_have_lineno(c)) {
Alex Henrie503627f2021-03-02 03:20:25 -07006678 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00006679 }
6680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006681 nblocks = 0;
6682 entryblock = NULL;
6683 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6684 nblocks++;
6685 entryblock = b;
6686 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006688 /* Set firstlineno if it wasn't explicitly set. */
6689 if (!c->u->u_firstlineno) {
Ned Deilydc35cda2016-08-17 17:18:33 -04006690 if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006691 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
Mark Shannon877df852020-11-12 09:43:29 +00006692 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006693 c->u->u_firstlineno = 1;
6694 }
Mark Shannon5977a792020-12-02 13:31:40 +00006695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006696 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
6697 goto error;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006698 a.a_entry = entryblock;
6699 a.a_nblocks = nblocks;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006700
Mark Shannon6e8128f2020-07-30 10:03:00 +01006701 consts = consts_dict_keys_inorder(c->u->u_consts);
6702 if (consts == NULL) {
6703 goto error;
6704 }
6705 if (optimize_cfg(&a, consts)) {
6706 goto error;
6707 }
6708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006709 /* Can't modify the bytecode after computing jump offsets. */
6710 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00006711
Mark Shannoncc75ab72020-11-12 19:49:33 +00006712 /* Emit code. */
6713 for(b = entryblock; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006714 for (j = 0; j < b->b_iused; j++)
6715 if (!assemble_emit(&a, &b->b_instr[j]))
6716 goto error;
6717 }
Mark Shannon877df852020-11-12 09:43:29 +00006718 if (!assemble_line_range(&a)) {
6719 return 0;
6720 }
6721 /* Emit sentinel at end of line number table */
6722 if (!assemble_emit_linetable_pair(&a, 255, -128)) {
6723 goto error;
6724 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00006725
Inada Naokibdb941b2021-02-10 09:20:42 +09006726 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006727 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09006728 }
6729 if (!merge_const_one(c, &a.a_lnotab)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006730 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09006731 }
6732 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) {
6733 goto error;
6734 }
6735 if (!merge_const_one(c, &a.a_bytecode)) {
6736 goto error;
6737 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006738
Mark Shannon6e8128f2020-07-30 10:03:00 +01006739 co = makecode(c, &a, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006740 error:
Mark Shannon6e8128f2020-07-30 10:03:00 +01006741 Py_XDECREF(consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006742 assemble_free(&a);
6743 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006744}
Georg Brandl8334fd92010-12-04 10:26:46 +00006745
Mark Shannon6e8128f2020-07-30 10:03:00 +01006746/* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
6747 with LOAD_CONST (c1, c2, ... cn).
6748 The consts table must still be in list form so that the
6749 new constant (c1, c2, ... cn) can be appended.
6750 Called with codestr pointing to the first LOAD_CONST.
6751*/
6752static int
6753fold_tuple_on_constants(struct instr *inst,
6754 int n, PyObject *consts)
6755{
6756 /* Pre-conditions */
6757 assert(PyList_CheckExact(consts));
6758 assert(inst[n].i_opcode == BUILD_TUPLE);
6759 assert(inst[n].i_oparg == n);
6760
6761 for (int i = 0; i < n; i++) {
6762 if (inst[i].i_opcode != LOAD_CONST) {
6763 return 0;
6764 }
6765 }
6766
6767 /* Buildup new tuple of constants */
6768 PyObject *newconst = PyTuple_New(n);
6769 if (newconst == NULL) {
6770 return -1;
6771 }
6772 for (int i = 0; i < n; i++) {
6773 int arg = inst[i].i_oparg;
6774 PyObject *constant = PyList_GET_ITEM(consts, arg);
6775 Py_INCREF(constant);
6776 PyTuple_SET_ITEM(newconst, i, constant);
6777 }
6778 Py_ssize_t index = PyList_GET_SIZE(consts);
Victor Stinner71f2ff42020-09-23 14:06:55 +02006779 if ((size_t)index >= (size_t)INT_MAX - 1) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006780 Py_DECREF(newconst);
6781 PyErr_SetString(PyExc_OverflowError, "too many constants");
6782 return -1;
6783 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006784 if (PyList_Append(consts, newconst)) {
6785 Py_DECREF(newconst);
6786 return -1;
6787 }
6788 Py_DECREF(newconst);
6789 for (int i = 0; i < n; i++) {
6790 inst[i].i_opcode = NOP;
6791 }
6792 inst[n].i_opcode = LOAD_CONST;
Victor Stinner71f2ff42020-09-23 14:06:55 +02006793 inst[n].i_oparg = (int)index;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006794 return 0;
6795}
6796
Mark Shannon28b75c82020-12-23 11:43:10 +00006797
6798static int
6799eliminate_jump_to_jump(basicblock *bb, int opcode) {
6800 assert (bb->b_iused > 0);
6801 struct instr *inst = &bb->b_instr[bb->b_iused-1];
6802 assert (is_jump(inst));
6803 assert (inst->i_target->b_iused > 0);
6804 struct instr *target = &inst->i_target->b_instr[0];
6805 if (inst->i_target == target->i_target) {
6806 /* Nothing to do */
6807 return 0;
6808 }
6809 int lineno = target->i_lineno;
6810 if (add_jump_to_block(bb, opcode, lineno, target->i_target) == 0) {
6811 return -1;
6812 }
6813 assert (bb->b_iused >= 2);
6814 bb->b_instr[bb->b_iused-2].i_opcode = NOP;
6815 return 0;
6816}
6817
Mark Shannoncc75ab72020-11-12 19:49:33 +00006818/* Maximum size of basic block that should be copied in optimizer */
6819#define MAX_COPY_SIZE 4
Mark Shannon6e8128f2020-07-30 10:03:00 +01006820
6821/* Optimization */
6822static int
6823optimize_basic_block(basicblock *bb, PyObject *consts)
6824{
6825 assert(PyList_CheckExact(consts));
6826 struct instr nop;
6827 nop.i_opcode = NOP;
6828 struct instr *target;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006829 for (int i = 0; i < bb->b_iused; i++) {
6830 struct instr *inst = &bb->b_instr[i];
6831 int oparg = inst->i_oparg;
6832 int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01006833 if (is_jump(inst)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006834 /* Skip over empty basic blocks. */
6835 while (inst->i_target->b_iused == 0) {
6836 inst->i_target = inst->i_target->b_next;
6837 }
6838 target = &inst->i_target->b_instr[0];
6839 }
6840 else {
6841 target = &nop;
6842 }
6843 switch (inst->i_opcode) {
Mark Shannon266b4622020-11-17 19:30:14 +00006844 /* Remove LOAD_CONST const; conditional jump */
Mark Shannon6e8128f2020-07-30 10:03:00 +01006845 case LOAD_CONST:
Mark Shannon266b4622020-11-17 19:30:14 +00006846 {
6847 PyObject* cnt;
6848 int is_true;
6849 int jump_if_true;
6850 switch(nextop) {
6851 case POP_JUMP_IF_FALSE:
6852 case POP_JUMP_IF_TRUE:
6853 cnt = PyList_GET_ITEM(consts, oparg);
6854 is_true = PyObject_IsTrue(cnt);
6855 if (is_true == -1) {
6856 goto error;
6857 }
6858 inst->i_opcode = NOP;
6859 jump_if_true = nextop == POP_JUMP_IF_TRUE;
6860 if (is_true == jump_if_true) {
6861 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
6862 bb->b_nofallthrough = 1;
6863 }
6864 else {
6865 bb->b_instr[i+1].i_opcode = NOP;
6866 }
6867 break;
6868 case JUMP_IF_FALSE_OR_POP:
6869 case JUMP_IF_TRUE_OR_POP:
6870 cnt = PyList_GET_ITEM(consts, oparg);
6871 is_true = PyObject_IsTrue(cnt);
6872 if (is_true == -1) {
6873 goto error;
6874 }
6875 jump_if_true = nextop == JUMP_IF_TRUE_OR_POP;
6876 if (is_true == jump_if_true) {
6877 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
6878 bb->b_nofallthrough = 1;
6879 }
6880 else {
6881 inst->i_opcode = NOP;
6882 bb->b_instr[i+1].i_opcode = NOP;
6883 }
6884 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006885 }
6886 break;
Mark Shannon266b4622020-11-17 19:30:14 +00006887 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006888
6889 /* Try to fold tuples of constants.
6890 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
6891 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
6892 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
6893 case BUILD_TUPLE:
6894 if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
6895 switch(oparg) {
6896 case 1:
6897 inst->i_opcode = NOP;
6898 bb->b_instr[i+1].i_opcode = NOP;
6899 break;
6900 case 2:
6901 inst->i_opcode = ROT_TWO;
6902 bb->b_instr[i+1].i_opcode = NOP;
6903 break;
6904 case 3:
6905 inst->i_opcode = ROT_THREE;
6906 bb->b_instr[i+1].i_opcode = ROT_TWO;
6907 }
6908 break;
6909 }
6910 if (i >= oparg) {
6911 if (fold_tuple_on_constants(inst-oparg, oparg, consts)) {
6912 goto error;
6913 }
6914 }
6915 break;
6916
6917 /* Simplify conditional jump to conditional jump where the
6918 result of the first test implies the success of a similar
6919 test or the failure of the opposite test.
6920 Arises in code like:
6921 "a and b or c"
6922 "(a and b) and c"
6923 "(a or b) or c"
6924 "(a or b) and c"
6925 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
6926 --> x:JUMP_IF_FALSE_OR_POP z
6927 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
6928 --> x:POP_JUMP_IF_FALSE y+1
6929 where y+1 is the instruction following the second test.
6930 */
6931 case JUMP_IF_FALSE_OR_POP:
6932 switch(target->i_opcode) {
6933 case POP_JUMP_IF_FALSE:
Mark Shannon28b75c82020-12-23 11:43:10 +00006934 if (inst->i_lineno == target->i_lineno) {
6935 *inst = *target;
6936 i--;
6937 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006938 break;
6939 case JUMP_ABSOLUTE:
6940 case JUMP_FORWARD:
6941 case JUMP_IF_FALSE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00006942 if (inst->i_lineno == target->i_lineno &&
6943 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00006944 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00006945 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00006946 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006947 break;
6948 case JUMP_IF_TRUE_OR_POP:
6949 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00006950 if (inst->i_lineno == target->i_lineno) {
6951 inst->i_opcode = POP_JUMP_IF_FALSE;
6952 inst->i_target = inst->i_target->b_next;
6953 --i;
6954 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006955 break;
6956 }
6957 break;
6958
6959 case JUMP_IF_TRUE_OR_POP:
6960 switch(target->i_opcode) {
6961 case POP_JUMP_IF_TRUE:
Mark Shannon28b75c82020-12-23 11:43:10 +00006962 if (inst->i_lineno == target->i_lineno) {
6963 *inst = *target;
6964 i--;
6965 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006966 break;
6967 case JUMP_ABSOLUTE:
6968 case JUMP_FORWARD:
6969 case JUMP_IF_TRUE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00006970 if (inst->i_lineno == target->i_lineno &&
6971 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00006972 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00006973 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00006974 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006975 break;
6976 case JUMP_IF_FALSE_OR_POP:
6977 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00006978 if (inst->i_lineno == target->i_lineno) {
6979 inst->i_opcode = POP_JUMP_IF_TRUE;
6980 inst->i_target = inst->i_target->b_next;
6981 --i;
6982 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006983 break;
6984 }
6985 break;
6986
6987 case POP_JUMP_IF_FALSE:
6988 switch(target->i_opcode) {
6989 case JUMP_ABSOLUTE:
6990 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00006991 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00006992 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00006993 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00006994 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006995 break;
6996 }
6997 break;
6998
6999 case POP_JUMP_IF_TRUE:
7000 switch(target->i_opcode) {
7001 case JUMP_ABSOLUTE:
7002 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007003 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00007004 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007005 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007006 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007007 break;
7008 }
7009 break;
7010
7011 case JUMP_ABSOLUTE:
7012 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007013 assert (i == bb->b_iused-1);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007014 switch(target->i_opcode) {
7015 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007016 if (eliminate_jump_to_jump(bb, inst->i_opcode)) {
7017 goto error;
Mark Shannon266b4622020-11-17 19:30:14 +00007018 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007019 break;
Mark Shannon28b75c82020-12-23 11:43:10 +00007020
Mark Shannon6e8128f2020-07-30 10:03:00 +01007021 case JUMP_ABSOLUTE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007022 if (eliminate_jump_to_jump(bb, JUMP_ABSOLUTE)) {
7023 goto error;
Mark Shannon266b4622020-11-17 19:30:14 +00007024 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007025 break;
Mark Shannon28b75c82020-12-23 11:43:10 +00007026 default:
7027 if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) {
7028 basicblock *to_copy = inst->i_target;
7029 inst->i_opcode = NOP;
7030 for (i = 0; i < to_copy->b_iused; i++) {
7031 int index = compiler_next_instr(bb);
7032 if (index < 0) {
7033 return -1;
7034 }
7035 bb->b_instr[index] = to_copy->b_instr[i];
7036 }
7037 bb->b_exit = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007038 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007039 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007040 }
7041 }
7042 return 0;
7043error:
7044 return -1;
7045}
7046
7047
7048static void
Mark Shannon1659ad12021-01-13 15:05:04 +00007049clean_basic_block(basicblock *bb, int prev_lineno) {
7050 /* Remove NOPs when legal to do so. */
Mark Shannon6e8128f2020-07-30 10:03:00 +01007051 int dest = 0;
7052 for (int src = 0; src < bb->b_iused; src++) {
Mark Shannon877df852020-11-12 09:43:29 +00007053 int lineno = bb->b_instr[src].i_lineno;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007054 if (bb->b_instr[src].i_opcode == NOP) {
Mark Shannon266b4622020-11-17 19:30:14 +00007055 /* Eliminate no-op if it doesn't have a line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007056 if (lineno < 0) {
7057 continue;
7058 }
Mark Shannon266b4622020-11-17 19:30:14 +00007059 /* or, if the previous instruction had the same line number. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007060 if (prev_lineno == lineno) {
7061 continue;
7062 }
Mark Shannon266b4622020-11-17 19:30:14 +00007063 /* or, if the next instruction has same line number or no line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007064 if (src < bb->b_iused - 1) {
7065 int next_lineno = bb->b_instr[src+1].i_lineno;
7066 if (next_lineno < 0 || next_lineno == lineno) {
7067 bb->b_instr[src+1].i_lineno = lineno;
7068 continue;
Mark Shannon877df852020-11-12 09:43:29 +00007069 }
7070 }
Mark Shannon266b4622020-11-17 19:30:14 +00007071 else {
7072 basicblock* next = bb->b_next;
7073 while (next && next->b_iused == 0) {
7074 next = next->b_next;
7075 }
7076 /* or if last instruction in BB and next BB has same line number */
7077 if (next) {
7078 if (lineno == next->b_instr[0].i_lineno) {
7079 continue;
7080 }
7081 }
7082 }
7083
Mark Shannon6e8128f2020-07-30 10:03:00 +01007084 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007085 if (dest != src) {
7086 bb->b_instr[dest] = bb->b_instr[src];
7087 }
7088 dest++;
7089 prev_lineno = lineno;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007090 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007091 assert(dest <= bb->b_iused);
7092 bb->b_iused = dest;
7093}
7094
Mark Shannon266b4622020-11-17 19:30:14 +00007095static int
7096normalize_basic_block(basicblock *bb) {
7097 /* Mark blocks as exit and/or nofallthrough.
7098 Raise SystemError if CFG is malformed. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007099 for (int i = 0; i < bb->b_iused; i++) {
7100 switch(bb->b_instr[i].i_opcode) {
7101 case RETURN_VALUE:
7102 case RAISE_VARARGS:
7103 case RERAISE:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007104 bb->b_exit = 1;
Mark Shannon5977a792020-12-02 13:31:40 +00007105 bb->b_nofallthrough = 1;
7106 break;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007107 case JUMP_ABSOLUTE:
7108 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007109 bb->b_nofallthrough = 1;
Mark Shannon266b4622020-11-17 19:30:14 +00007110 /* fall through */
7111 case POP_JUMP_IF_FALSE:
7112 case POP_JUMP_IF_TRUE:
7113 case JUMP_IF_FALSE_OR_POP:
7114 case JUMP_IF_TRUE_OR_POP:
Mark Shannon5977a792020-12-02 13:31:40 +00007115 case FOR_ITER:
Mark Shannon266b4622020-11-17 19:30:14 +00007116 if (i != bb->b_iused-1) {
7117 PyErr_SetString(PyExc_SystemError, "malformed control flow graph.");
7118 return -1;
7119 }
Mark Shannon5977a792020-12-02 13:31:40 +00007120 /* Skip over empty basic blocks. */
7121 while (bb->b_instr[i].i_target->b_iused == 0) {
7122 bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next;
7123 }
7124
Mark Shannoncc75ab72020-11-12 19:49:33 +00007125 }
7126 }
Mark Shannon266b4622020-11-17 19:30:14 +00007127 return 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007128}
7129
Mark Shannon6e8128f2020-07-30 10:03:00 +01007130static int
7131mark_reachable(struct assembler *a) {
7132 basicblock **stack, **sp;
7133 sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks);
7134 if (stack == NULL) {
7135 return -1;
7136 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007137 a->a_entry->b_predecessors = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007138 *sp++ = a->a_entry;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007139 while (sp > stack) {
7140 basicblock *b = *(--sp);
Mark Shannon3bd60352021-01-13 12:05:43 +00007141 if (b->b_next && !b->b_nofallthrough) {
7142 if (b->b_next->b_predecessors == 0) {
7143 *sp++ = b->b_next;
7144 }
7145 b->b_next->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007146 }
7147 for (int i = 0; i < b->b_iused; i++) {
7148 basicblock *target;
Mark Shannon582aaf12020-08-04 17:30:11 +01007149 if (is_jump(&b->b_instr[i])) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007150 target = b->b_instr[i].i_target;
Mark Shannon3bd60352021-01-13 12:05:43 +00007151 if (target->b_predecessors == 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007152 *sp++ = target;
7153 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007154 target->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007155 }
7156 }
7157 }
7158 PyObject_Free(stack);
7159 return 0;
7160}
7161
Mark Shannon3bd60352021-01-13 12:05:43 +00007162static void
7163eliminate_empty_basic_blocks(basicblock *entry) {
7164 /* Eliminate empty blocks */
7165 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7166 basicblock *next = b->b_next;
7167 if (next) {
7168 while (next->b_iused == 0 && next->b_next) {
7169 next = next->b_next;
7170 }
7171 b->b_next = next;
7172 }
7173 }
7174 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7175 if (b->b_iused == 0) {
7176 continue;
7177 }
7178 if (is_jump(&b->b_instr[b->b_iused-1])) {
7179 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7180 while (target->b_iused == 0) {
7181 target = target->b_next;
7182 }
7183 b->b_instr[b->b_iused-1].i_target = target;
7184 }
7185 }
7186}
7187
7188
Mark Shannon5977a792020-12-02 13:31:40 +00007189/* If an instruction has no line number, but it's predecessor in the BB does,
Mark Shannon3bd60352021-01-13 12:05:43 +00007190 * then copy the line number. If a successor block has no line number, and only
7191 * one predecessor, then inherit the line number.
7192 * This ensures that all exit blocks (with one predecessor) receive a line number.
7193 * Also reduces the size of the line number table,
Mark Shannon5977a792020-12-02 13:31:40 +00007194 * but has no impact on the generated line number events.
7195 */
7196static void
Mark Shannon3bd60352021-01-13 12:05:43 +00007197propogate_line_numbers(struct assembler *a) {
Mark Shannon5977a792020-12-02 13:31:40 +00007198 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007199 if (b->b_iused == 0) {
7200 continue;
7201 }
Mark Shannon5977a792020-12-02 13:31:40 +00007202 int prev_lineno = -1;
7203 for (int i = 0; i < b->b_iused; i++) {
7204 if (b->b_instr[i].i_lineno < 0) {
7205 b->b_instr[i].i_lineno = prev_lineno;
7206 }
7207 else {
7208 prev_lineno = b->b_instr[i].i_lineno;
7209 }
7210 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007211 if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) {
7212 assert(b->b_next->b_iused);
7213 if (b->b_next->b_instr[0].i_lineno < 0) {
7214 b->b_next->b_instr[0].i_lineno = prev_lineno;
7215 }
7216 }
7217 if (is_jump(&b->b_instr[b->b_iused-1])) {
7218 switch (b->b_instr[b->b_iused-1].i_opcode) {
7219 /* Note: Only actual jumps, not exception handlers */
7220 case SETUP_ASYNC_WITH:
7221 case SETUP_WITH:
7222 case SETUP_FINALLY:
7223 continue;
7224 }
7225 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7226 if (target->b_predecessors == 1) {
7227 if (target->b_instr[0].i_lineno < 0) {
7228 target->b_instr[0].i_lineno = prev_lineno;
7229 }
7230 }
7231 }
Mark Shannon5977a792020-12-02 13:31:40 +00007232 }
7233}
7234
7235/* Perform optimizations on a control flow graph.
Mark Shannon6e8128f2020-07-30 10:03:00 +01007236 The consts object should still be in list form to allow new constants
7237 to be appended.
7238
7239 All transformations keep the code size the same or smaller.
7240 For those that reduce size, the gaps are initially filled with
7241 NOPs. Later those NOPs are removed.
7242*/
7243
7244static int
7245optimize_cfg(struct assembler *a, PyObject *consts)
7246{
Mark Shannoncc75ab72020-11-12 19:49:33 +00007247 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannoncc75ab72020-11-12 19:49:33 +00007248 if (optimize_basic_block(b, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007249 return -1;
7250 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007251 clean_basic_block(b, -1);
Mark Shannon3bd60352021-01-13 12:05:43 +00007252 assert(b->b_predecessors == 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007253 }
7254 if (mark_reachable(a)) {
7255 return -1;
7256 }
7257 /* Delete unreachable instructions */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007258 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007259 if (b->b_predecessors == 0) {
Mark Shannoncc75ab72020-11-12 19:49:33 +00007260 b->b_iused = 0;
Om Gc71581c2020-12-16 17:48:05 +05307261 b->b_nofallthrough = 0;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007262 }
7263 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007264 basicblock *pred = NULL;
7265 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7266 int prev_lineno = -1;
7267 if (pred && pred->b_iused) {
7268 prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno;
7269 }
7270 clean_basic_block(b, prev_lineno);
7271 pred = b->b_nofallthrough ? NULL : b;
7272 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007273 eliminate_empty_basic_blocks(a->a_entry);
Om Gc71581c2020-12-16 17:48:05 +05307274 /* Delete jump instructions made redundant by previous step. If a non-empty
7275 block ends with a jump instruction, check if the next non-empty block
7276 reached through normal flow control is the target of that jump. If it
7277 is, then the jump instruction is redundant and can be deleted.
7278 */
Mark Shannon3bd60352021-01-13 12:05:43 +00007279 int maybe_empty_blocks = 0;
Om Gc71581c2020-12-16 17:48:05 +05307280 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7281 if (b->b_iused > 0) {
7282 struct instr *b_last_instr = &b->b_instr[b->b_iused - 1];
Mark Shannon802b6452021-02-02 14:59:15 +00007283 if (b_last_instr->i_opcode == JUMP_ABSOLUTE ||
Om Gc71581c2020-12-16 17:48:05 +05307284 b_last_instr->i_opcode == JUMP_FORWARD) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007285 if (b_last_instr->i_target == b->b_next) {
7286 assert(b->b_next->b_iused);
Om Gc71581c2020-12-16 17:48:05 +05307287 b->b_nofallthrough = 0;
Mark Shannon802b6452021-02-02 14:59:15 +00007288 b_last_instr->i_opcode = NOP;
7289 clean_basic_block(b, -1);
7290 maybe_empty_blocks = 1;
Om Gc71581c2020-12-16 17:48:05 +05307291 }
7292 }
7293 }
7294 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007295 if (maybe_empty_blocks) {
7296 eliminate_empty_basic_blocks(a->a_entry);
7297 }
7298 propogate_line_numbers(a);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007299 return 0;
7300}
7301
Mark Shannon5977a792020-12-02 13:31:40 +00007302static inline int
7303is_exit_without_lineno(basicblock *b) {
7304 return b->b_exit && b->b_instr[0].i_lineno < 0;
7305}
7306
7307/* PEP 626 mandates that the f_lineno of a frame is correct
7308 * after a frame terminates. It would be prohibitively expensive
7309 * to continuously update the f_lineno field at runtime,
7310 * so we make sure that all exiting instruction (raises and returns)
7311 * have a valid line number, allowing us to compute f_lineno lazily.
7312 * We can do this by duplicating the exit blocks without line number
7313 * so that none have more than one predecessor. We can then safely
7314 * copy the line number from the sole predecessor block.
7315 */
7316static int
7317ensure_exits_have_lineno(struct compiler *c)
7318{
Mark Shannoneaccc122020-12-04 15:22:12 +00007319 basicblock *entry = NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00007320 /* Copy all exit blocks without line number that are targets of a jump.
7321 */
7322 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7323 if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) {
7324 switch (b->b_instr[b->b_iused-1].i_opcode) {
7325 /* Note: Only actual jumps, not exception handlers */
7326 case SETUP_ASYNC_WITH:
7327 case SETUP_WITH:
7328 case SETUP_FINALLY:
7329 continue;
7330 }
7331 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7332 if (is_exit_without_lineno(target)) {
7333 basicblock *new_target = compiler_copy_block(c, target);
7334 if (new_target == NULL) {
7335 return -1;
7336 }
7337 new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7338 b->b_instr[b->b_iused-1].i_target = new_target;
7339 }
7340 }
Mark Shannoneaccc122020-12-04 15:22:12 +00007341 entry = b;
7342 }
7343 assert(entry != NULL);
7344 if (is_exit_without_lineno(entry)) {
7345 entry->b_instr[0].i_lineno = c->u->u_firstlineno;
Mark Shannon5977a792020-12-02 13:31:40 +00007346 }
Mark Shannonee9f98d2021-01-05 12:04:10 +00007347 /* Eliminate empty blocks */
7348 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7349 while (b->b_next && b->b_next->b_iused == 0) {
7350 b->b_next = b->b_next->b_next;
7351 }
7352 }
Mark Shannon5977a792020-12-02 13:31:40 +00007353 /* Any remaining reachable exit blocks without line number can only be reached by
7354 * fall through, and thus can only have a single predecessor */
7355 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7356 if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) {
7357 if (is_exit_without_lineno(b->b_next)) {
7358 assert(b->b_next->b_iused > 0);
7359 b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7360 }
7361 }
7362 }
7363 return 0;
7364}
7365
7366
Mark Shannon6e8128f2020-07-30 10:03:00 +01007367/* Retained for API compatibility.
7368 * Optimization is now done in optimize_cfg */
7369
7370PyObject *
7371PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
7372 PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
7373{
7374 Py_INCREF(code);
7375 return code;
7376}