blob: a1260aadd62e421b8c6e48a38fe8de009e2ecbc2 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file compiles an abstract syntax tree (AST) into Python bytecode.
3 *
4 * The primary entry point is PyAST_Compile(), which returns a
5 * PyCodeObject. The compiler makes several passes to build the code
6 * object:
7 * 1. Checks for future statements. See future.c
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00008 * 2. Builds a symbol table. See symtable.c.
Thomas Wouters89f507f2006-12-13 04:49:30 +00009 * 3. Generate code for basic blocks. See compiler_mod() in this file.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000010 * 4. Assemble the basic blocks into final code. See assemble() in
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000011 * this file.
Mark Shannon6e8128f2020-07-30 10:03:00 +010012 * 5. Optimize the byte code (peephole optimizations).
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013 *
14 * Note that compiler_mod() suggests module, but the module ast type
15 * (mod_ty) has cases for expressions and interactive statements.
Nick Coghlan944d3eb2005-11-16 12:46:55 +000016 *
Jeremy Hyltone9357b22006-03-01 15:47:05 +000017 * CAUTION: The VISIT_* macros abort the current function when they
18 * encounter a problem. So don't invoke them when there is memory
19 * which needs to be released. Code blocks are OK, as the compiler
Thomas Wouters89f507f2006-12-13 04:49:30 +000020 * structure takes care of releasing those. Use the arena to manage
21 * objects.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022 */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000023
Guido van Rossum79f25d91997-04-29 20:08:16 +000024#include "Python.h"
Victor Stinnerba7a99d2021-01-30 01:46:44 +010025#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
Victor Stinnerc9bc2902020-10-27 02:24:34 +010026#include "pycore_long.h" // _PyLong_GetZero()
Guido van Rossum3f5da241990-12-20 15:06:42 +000027
Ammar Askare92d3932020-01-15 11:48:40 -050028#include "Python-ast.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000029#include "ast.h"
30#include "code.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000031#include "symtable.h"
Mark Shannon582aaf12020-08-04 17:30:11 +010032#define NEED_OPCODE_JUMP_TABLES
Guido van Rossum10dc2e81990-11-18 17:27:39 +000033#include "opcode.h"
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030034#include "wordcode_helpers.h"
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 Stinner14e461d2013-08-26 22:28:21 +0200354PyAST_CompileObject(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 Stinner14e461d2013-08-26 22:28:21 +0200377 c.c_future = PyFuture_FromASTObject(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 Stinner14e461d2013-08-26 22:28:21 +0200398 c.c_st = PySymtable_BuildObject(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
413PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200414PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags,
415 int optimize, PyArena *arena)
416{
417 PyObject *filename;
418 PyCodeObject *co;
419 filename = PyUnicode_DecodeFSDefault(filename_str);
420 if (filename == NULL)
421 return NULL;
422 co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
423 Py_DECREF(filename);
424 return co;
425
426}
427
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000428static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000429compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 if (c->c_st)
432 PySymtable_Free(c->c_st);
433 if (c->c_future)
434 PyObject_Free(c->c_future);
Victor Stinner14e461d2013-08-26 22:28:21 +0200435 Py_XDECREF(c->c_filename);
INADA Naokic2e16072018-11-26 21:23:22 +0900436 Py_DECREF(c->c_const_cache);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000438}
439
Guido van Rossum79f25d91997-04-29 20:08:16 +0000440static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000441list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 Py_ssize_t i, n;
444 PyObject *v, *k;
445 PyObject *dict = PyDict_New();
446 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 n = PyList_Size(list);
449 for (i = 0; i < n; i++) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100450 v = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 if (!v) {
452 Py_DECREF(dict);
453 return NULL;
454 }
455 k = PyList_GET_ITEM(list, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300456 if (PyDict_SetItem(dict, k, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 Py_DECREF(v);
458 Py_DECREF(dict);
459 return NULL;
460 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 Py_DECREF(v);
462 }
463 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000464}
465
466/* Return new dict containing names from src that match scope(s).
467
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000468src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000470values are integers, starting at offset and increasing by one for
471each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000472*/
473
474static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +0100475dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000476{
Benjamin Peterson51ab2832012-07-18 15:12:47 -0700477 Py_ssize_t i = offset, scope, num_keys, key_i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 PyObject *k, *v, *dest = PyDict_New();
Meador Inge2ca63152012-07-18 14:20:11 -0500479 PyObject *sorted_keys;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 assert(offset >= 0);
482 if (dest == NULL)
483 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000484
Meador Inge2ca63152012-07-18 14:20:11 -0500485 /* Sort the keys so that we have a deterministic order on the indexes
486 saved in the returned dictionary. These indexes are used as indexes
487 into the free and cell var storage. Therefore if they aren't
488 deterministic, then the generated bytecode is not deterministic.
489 */
490 sorted_keys = PyDict_Keys(src);
491 if (sorted_keys == NULL)
492 return NULL;
493 if (PyList_Sort(sorted_keys) != 0) {
494 Py_DECREF(sorted_keys);
495 return NULL;
496 }
Meador Ingef69e24e2012-07-18 16:41:03 -0500497 num_keys = PyList_GET_SIZE(sorted_keys);
Meador Inge2ca63152012-07-18 14:20:11 -0500498
499 for (key_i = 0; key_i < num_keys; key_i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 /* XXX this should probably be a macro in symtable.h */
501 long vi;
Meador Inge2ca63152012-07-18 14:20:11 -0500502 k = PyList_GET_ITEM(sorted_keys, key_i);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200503 v = PyDict_GetItemWithError(src, k);
504 assert(v && PyLong_Check(v));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 vi = PyLong_AS_LONG(v);
506 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 if (scope == scope_type || vi & flag) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300509 PyObject *item = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 if (item == NULL) {
Meador Inge2ca63152012-07-18 14:20:11 -0500511 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 Py_DECREF(dest);
513 return NULL;
514 }
515 i++;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300516 if (PyDict_SetItem(dest, k, item) < 0) {
Meador Inge2ca63152012-07-18 14:20:11 -0500517 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 Py_DECREF(item);
519 Py_DECREF(dest);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 return NULL;
521 }
522 Py_DECREF(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 }
524 }
Meador Inge2ca63152012-07-18 14:20:11 -0500525 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000527}
528
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529static void
530compiler_unit_check(struct compiler_unit *u)
531{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 basicblock *block;
533 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +0100534 assert(!_PyMem_IsPtrFreed(block));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 if (block->b_instr != NULL) {
536 assert(block->b_ialloc > 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +0100537 assert(block->b_iused >= 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 assert(block->b_ialloc >= block->b_iused);
539 }
540 else {
541 assert (block->b_iused == 0);
542 assert (block->b_ialloc == 0);
543 }
544 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545}
546
547static void
548compiler_unit_free(struct compiler_unit *u)
549{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 compiler_unit_check(u);
553 b = u->u_blocks;
554 while (b != NULL) {
555 if (b->b_instr)
556 PyObject_Free((void *)b->b_instr);
557 next = b->b_list;
558 PyObject_Free((void *)b);
559 b = next;
560 }
561 Py_CLEAR(u->u_ste);
562 Py_CLEAR(u->u_name);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400563 Py_CLEAR(u->u_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 Py_CLEAR(u->u_consts);
565 Py_CLEAR(u->u_names);
566 Py_CLEAR(u->u_varnames);
567 Py_CLEAR(u->u_freevars);
568 Py_CLEAR(u->u_cellvars);
569 Py_CLEAR(u->u_private);
570 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000571}
572
573static int
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100574compiler_enter_scope(struct compiler *c, identifier name,
575 int scope_type, void *key, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000576{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 struct compiler_unit *u;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100578 basicblock *block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579
Andy Lester7668a8b2020-03-24 23:26:44 -0500580 u = (struct compiler_unit *)PyObject_Calloc(1, sizeof(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 struct compiler_unit));
582 if (!u) {
583 PyErr_NoMemory();
584 return 0;
585 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100586 u->u_scope_type = scope_type;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 u->u_argcount = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100588 u->u_posonlyargcount = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 u->u_kwonlyargcount = 0;
590 u->u_ste = PySymtable_Lookup(c->c_st, key);
591 if (!u->u_ste) {
592 compiler_unit_free(u);
593 return 0;
594 }
595 Py_INCREF(name);
596 u->u_name = name;
597 u->u_varnames = list2dict(u->u_ste->ste_varnames);
598 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
599 if (!u->u_varnames || !u->u_cellvars) {
600 compiler_unit_free(u);
601 return 0;
602 }
Benjamin Peterson312595c2013-05-15 15:26:42 -0500603 if (u->u_ste->ste_needs_class_closure) {
Martin Panter7462b6492015-11-02 03:37:02 +0000604 /* Cook up an implicit __class__ cell. */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500605 _Py_IDENTIFIER(__class__);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300606 PyObject *name;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500607 int res;
608 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200609 assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500610 name = _PyUnicode_FromId(&PyId___class__);
611 if (!name) {
612 compiler_unit_free(u);
613 return 0;
614 }
Victor Stinnerc9bc2902020-10-27 02:24:34 +0100615 res = PyDict_SetItem(u->u_cellvars, name, _PyLong_GetZero());
Benjamin Peterson312595c2013-05-15 15:26:42 -0500616 if (res < 0) {
617 compiler_unit_free(u);
618 return 0;
619 }
620 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200623 PyDict_GET_SIZE(u->u_cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 if (!u->u_freevars) {
625 compiler_unit_free(u);
626 return 0;
627 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 u->u_blocks = NULL;
630 u->u_nfblocks = 0;
631 u->u_firstlineno = lineno;
632 u->u_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000633 u->u_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 u->u_consts = PyDict_New();
635 if (!u->u_consts) {
636 compiler_unit_free(u);
637 return 0;
638 }
639 u->u_names = PyDict_New();
640 if (!u->u_names) {
641 compiler_unit_free(u);
642 return 0;
643 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 /* Push the old compiler_unit on the stack. */
648 if (c->u) {
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400649 PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
651 Py_XDECREF(capsule);
652 compiler_unit_free(u);
653 return 0;
654 }
655 Py_DECREF(capsule);
656 u->u_private = c->u->u_private;
657 Py_XINCREF(u->u_private);
658 }
659 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 c->c_nestlevel++;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100662
663 block = compiler_new_block(c);
664 if (block == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 return 0;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100666 c->u->u_curblock = block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000667
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400668 if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
669 if (!compiler_set_qualname(c))
670 return 0;
671 }
672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000674}
675
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000676static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677compiler_exit_scope(struct compiler *c)
678{
Victor Stinnera6192632021-01-29 16:53:03 +0100679 // Don't call PySequence_DelItem() with an exception raised
680 PyObject *exc_type, *exc_val, *exc_tb;
681 PyErr_Fetch(&exc_type, &exc_val, &exc_tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 c->c_nestlevel--;
684 compiler_unit_free(c->u);
685 /* Restore c->u to the parent unit. */
Victor Stinnera6192632021-01-29 16:53:03 +0100686 Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 if (n >= 0) {
Victor Stinnera6192632021-01-29 16:53:03 +0100688 PyObject *capsule = PyList_GET_ITEM(c->c_stack, n);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400689 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 assert(c->u);
691 /* we are deleting from a list so this really shouldn't fail */
Victor Stinnera6192632021-01-29 16:53:03 +0100692 if (PySequence_DelItem(c->c_stack, n) < 0) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +0100693 _PyErr_WriteUnraisableMsg("on removing the last compiler "
694 "stack item", NULL);
Victor Stinnera6192632021-01-29 16:53:03 +0100695 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 compiler_unit_check(c->u);
697 }
Victor Stinnera6192632021-01-29 16:53:03 +0100698 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 c->u = NULL;
Victor Stinnera6192632021-01-29 16:53:03 +0100700 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000701
Victor Stinnera6192632021-01-29 16:53:03 +0100702 PyErr_Restore(exc_type, exc_val, exc_tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703}
704
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400705static int
706compiler_set_qualname(struct compiler *c)
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100707{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100708 _Py_static_string(dot, ".");
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400709 _Py_static_string(dot_locals, ".<locals>");
710 Py_ssize_t stack_size;
711 struct compiler_unit *u = c->u;
712 PyObject *name, *base, *dot_str, *dot_locals_str;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100713
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400714 base = NULL;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100715 stack_size = PyList_GET_SIZE(c->c_stack);
Benjamin Petersona8a38b82013-10-19 16:14:39 -0400716 assert(stack_size >= 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400717 if (stack_size > 1) {
718 int scope, force_global = 0;
719 struct compiler_unit *parent;
720 PyObject *mangled, *capsule;
721
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400722 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400723 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400724 assert(parent);
725
Yury Selivanov75445082015-05-11 22:57:16 -0400726 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
727 || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
728 || u->u_scope_type == COMPILER_SCOPE_CLASS) {
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400729 assert(u->u_name);
730 mangled = _Py_Mangle(parent->u_private, u->u_name);
731 if (!mangled)
732 return 0;
733 scope = PyST_GetScope(parent->u_ste, mangled);
734 Py_DECREF(mangled);
735 assert(scope != GLOBAL_IMPLICIT);
736 if (scope == GLOBAL_EXPLICIT)
737 force_global = 1;
738 }
739
740 if (!force_global) {
741 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
Yury Selivanov75445082015-05-11 22:57:16 -0400742 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400743 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
744 dot_locals_str = _PyUnicode_FromId(&dot_locals);
745 if (dot_locals_str == NULL)
746 return 0;
747 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
748 if (base == NULL)
749 return 0;
750 }
751 else {
752 Py_INCREF(parent->u_qualname);
753 base = parent->u_qualname;
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400754 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100755 }
756 }
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400757
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400758 if (base != NULL) {
759 dot_str = _PyUnicode_FromId(&dot);
760 if (dot_str == NULL) {
761 Py_DECREF(base);
762 return 0;
763 }
764 name = PyUnicode_Concat(base, dot_str);
765 Py_DECREF(base);
766 if (name == NULL)
767 return 0;
768 PyUnicode_Append(&name, u->u_name);
769 if (name == NULL)
770 return 0;
771 }
772 else {
773 Py_INCREF(u->u_name);
774 name = u->u_name;
775 }
776 u->u_qualname = name;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100777
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400778 return 1;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100779}
780
Eric V. Smith235a6f02015-09-19 14:51:32 -0400781
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782/* Allocate a new block and return a pointer to it.
783 Returns NULL on error.
784*/
785
786static basicblock *
787compiler_new_block(struct compiler *c)
788{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 basicblock *b;
790 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 u = c->u;
Andy Lester7668a8b2020-03-24 23:26:44 -0500793 b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 if (b == NULL) {
795 PyErr_NoMemory();
796 return NULL;
797 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 /* Extend the singly linked list of blocks with new block. */
799 b->b_list = u->u_blocks;
800 u->u_blocks = b;
801 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000802}
803
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804static basicblock *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000805compiler_next_block(struct compiler *c)
806{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 basicblock *block = compiler_new_block(c);
808 if (block == NULL)
809 return NULL;
810 c->u->u_curblock->b_next = block;
811 c->u->u_curblock = block;
812 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813}
814
815static basicblock *
816compiler_use_next_block(struct compiler *c, basicblock *block)
817{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 assert(block != NULL);
819 c->u->u_curblock->b_next = block;
820 c->u->u_curblock = block;
821 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822}
823
Mark Shannon5977a792020-12-02 13:31:40 +0000824static basicblock *
825compiler_copy_block(struct compiler *c, basicblock *block)
826{
827 /* Cannot copy a block if it has a fallthrough, since
828 * a block can only have one fallthrough predecessor.
829 */
830 assert(block->b_nofallthrough);
831 basicblock *result = compiler_next_block(c);
832 if (result == NULL) {
833 return NULL;
834 }
835 for (int i = 0; i < block->b_iused; i++) {
836 int n = compiler_next_instr(result);
837 if (n < 0) {
838 return NULL;
839 }
840 result->b_instr[n] = block->b_instr[i];
841 }
842 result->b_exit = block->b_exit;
Mark Shannon3bd60352021-01-13 12:05:43 +0000843 result->b_nofallthrough = 1;
Mark Shannon5977a792020-12-02 13:31:40 +0000844 return result;
845}
846
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000847/* Returns the offset of the next instruction in the current block's
848 b_instr array. Resizes the b_instr as necessary.
849 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000850*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000851
852static int
Andy Lester76d58772020-03-10 21:18:12 -0500853compiler_next_instr(basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 assert(b != NULL);
856 if (b->b_instr == NULL) {
Andy Lester7668a8b2020-03-24 23:26:44 -0500857 b->b_instr = (struct instr *)PyObject_Calloc(
858 DEFAULT_BLOCK_SIZE, sizeof(struct instr));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 if (b->b_instr == NULL) {
860 PyErr_NoMemory();
861 return -1;
862 }
863 b->b_ialloc = DEFAULT_BLOCK_SIZE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 }
865 else if (b->b_iused == b->b_ialloc) {
866 struct instr *tmp;
867 size_t oldsize, newsize;
868 oldsize = b->b_ialloc * sizeof(struct instr);
869 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000870
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -0700871 if (oldsize > (SIZE_MAX >> 1)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 PyErr_NoMemory();
873 return -1;
874 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 if (newsize == 0) {
877 PyErr_NoMemory();
878 return -1;
879 }
880 b->b_ialloc <<= 1;
881 tmp = (struct instr *)PyObject_Realloc(
882 (void *)b->b_instr, newsize);
883 if (tmp == NULL) {
884 PyErr_NoMemory();
885 return -1;
886 }
887 b->b_instr = tmp;
888 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
889 }
890 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000891}
892
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200893/* Set the line number and column offset for the following instructions.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000894
Christian Heimes2202f872008-02-06 14:31:34 +0000895 The line number is reset in the following cases:
896 - when entering a new scope
897 - on each statement
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200898 - on each expression and sub-expression
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200899 - before the "except" and "finally" clauses
Thomas Wouters89f507f2006-12-13 04:49:30 +0000900*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000901
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200902#define SET_LOC(c, x) \
903 (c)->u->u_lineno = (x)->lineno; \
904 (c)->u->u_col_offset = (x)->col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000905
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200906/* Return the stack effect of opcode with argument oparg.
907
908 Some opcodes have different stack effect when jump to the target and
909 when not jump. The 'jump' parameter specifies the case:
910
911 * 0 -- when not jump
912 * 1 -- when jump
913 * -1 -- maximal
914 */
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200915static int
916stack_effect(int opcode, int oparg, int jump)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000917{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 switch (opcode) {
Serhiy Storchaka57faf342018-04-25 22:04:06 +0300919 case NOP:
920 case EXTENDED_ARG:
921 return 0;
922
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200923 /* Stack manipulation */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 case POP_TOP:
925 return -1;
926 case ROT_TWO:
927 case ROT_THREE:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200928 case ROT_FOUR:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 return 0;
930 case DUP_TOP:
931 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000932 case DUP_TOP_TWO:
933 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000934
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200935 /* Unary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 case UNARY_POSITIVE:
937 case UNARY_NEGATIVE:
938 case UNARY_NOT:
939 case UNARY_INVERT:
940 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 case SET_ADD:
943 case LIST_APPEND:
944 return -1;
945 case MAP_ADD:
946 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000947
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200948 /* Binary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 case BINARY_POWER:
950 case BINARY_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400951 case BINARY_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 case BINARY_MODULO:
953 case BINARY_ADD:
954 case BINARY_SUBTRACT:
955 case BINARY_SUBSCR:
956 case BINARY_FLOOR_DIVIDE:
957 case BINARY_TRUE_DIVIDE:
958 return -1;
959 case INPLACE_FLOOR_DIVIDE:
960 case INPLACE_TRUE_DIVIDE:
961 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 case INPLACE_ADD:
964 case INPLACE_SUBTRACT:
965 case INPLACE_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400966 case INPLACE_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 case INPLACE_MODULO:
968 return -1;
969 case STORE_SUBSCR:
970 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 case DELETE_SUBSCR:
972 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 case BINARY_LSHIFT:
975 case BINARY_RSHIFT:
976 case BINARY_AND:
977 case BINARY_XOR:
978 case BINARY_OR:
979 return -1;
980 case INPLACE_POWER:
981 return -1;
982 case GET_ITER:
983 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 case PRINT_EXPR:
986 return -1;
987 case LOAD_BUILD_CLASS:
988 return 1;
989 case INPLACE_LSHIFT:
990 case INPLACE_RSHIFT:
991 case INPLACE_AND:
992 case INPLACE_XOR:
993 case INPLACE_OR:
994 return -1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 case SETUP_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200997 /* 1 in the normal flow.
998 * Restore the stack position and push 6 values before jumping to
999 * the handler if an exception be raised. */
1000 return jump ? 6 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 case RETURN_VALUE:
1002 return -1;
1003 case IMPORT_STAR:
1004 return -1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001005 case SETUP_ANNOTATIONS:
1006 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 case YIELD_VALUE:
1008 return 0;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001009 case YIELD_FROM:
1010 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 case POP_BLOCK:
1012 return 0;
1013 case POP_EXCEPT:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001014 return -3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 case STORE_NAME:
1017 return -1;
1018 case DELETE_NAME:
1019 return 0;
1020 case UNPACK_SEQUENCE:
1021 return oparg-1;
1022 case UNPACK_EX:
1023 return (oparg&0xFF) + (oparg>>8);
1024 case FOR_ITER:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001025 /* -1 at end of iterator, 1 if continue iterating. */
1026 return jump > 0 ? -1 : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 case STORE_ATTR:
1029 return -2;
1030 case DELETE_ATTR:
1031 return -1;
1032 case STORE_GLOBAL:
1033 return -1;
1034 case DELETE_GLOBAL:
1035 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 case LOAD_CONST:
1037 return 1;
1038 case LOAD_NAME:
1039 return 1;
1040 case BUILD_TUPLE:
1041 case BUILD_LIST:
1042 case BUILD_SET:
Serhiy Storchakaea525a22016-09-06 22:07:53 +03001043 case BUILD_STRING:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 return 1-oparg;
1045 case BUILD_MAP:
Benjamin Petersonb6855152015-09-10 21:02:39 -07001046 return 1 - 2*oparg;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001047 case BUILD_CONST_KEY_MAP:
1048 return -oparg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 case LOAD_ATTR:
1050 return 0;
1051 case COMPARE_OP:
Mark Shannon9af0e472020-01-14 10:12:45 +00001052 case IS_OP:
1053 case CONTAINS_OP:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 return -1;
Mark Shannon9af0e472020-01-14 10:12:45 +00001055 case JUMP_IF_NOT_EXC_MATCH:
1056 return -2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 case IMPORT_NAME:
1058 return -1;
1059 case IMPORT_FROM:
1060 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001062 /* Jumps */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 case JUMP_FORWARD:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 case JUMP_ABSOLUTE:
1065 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001066
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001067 case JUMP_IF_TRUE_OR_POP:
1068 case JUMP_IF_FALSE_OR_POP:
1069 return jump ? 0 : -1;
1070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 case POP_JUMP_IF_FALSE:
1072 case POP_JUMP_IF_TRUE:
1073 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 case LOAD_GLOBAL:
1076 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001078 /* Exception handling */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 case SETUP_FINALLY:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001080 /* 0 in the normal flow.
1081 * Restore the stack position and push 6 values before jumping to
1082 * the handler if an exception be raised. */
1083 return jump ? 6 : 0;
Mark Shannonfee55262019-11-21 09:11:43 +00001084 case RERAISE:
1085 return -3;
1086
1087 case WITH_EXCEPT_START:
1088 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 case LOAD_FAST:
1091 return 1;
1092 case STORE_FAST:
1093 return -1;
1094 case DELETE_FAST:
1095 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 case RAISE_VARARGS:
1098 return -oparg;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001099
1100 /* Functions and calls */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 case CALL_FUNCTION:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001102 return -oparg;
Yury Selivanovf2392132016-12-13 19:03:51 -05001103 case CALL_METHOD:
1104 return -oparg-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 case CALL_FUNCTION_KW:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001106 return -oparg-1;
1107 case CALL_FUNCTION_EX:
Matthieu Dartiailh3a9ac822017-02-21 14:25:22 +01001108 return -1 - ((oparg & 0x01) != 0);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001109 case MAKE_FUNCTION:
1110 return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1111 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 case BUILD_SLICE:
1113 if (oparg == 3)
1114 return -2;
1115 else
1116 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001117
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001118 /* Closures */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 case LOAD_CLOSURE:
1120 return 1;
1121 case LOAD_DEREF:
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001122 case LOAD_CLASSDEREF:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 return 1;
1124 case STORE_DEREF:
1125 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001126 case DELETE_DEREF:
1127 return 0;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001128
1129 /* Iterators and generators */
Yury Selivanov75445082015-05-11 22:57:16 -04001130 case GET_AWAITABLE:
1131 return 0;
1132 case SETUP_ASYNC_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001133 /* 0 in the normal flow.
1134 * Restore the stack position to the position before the result
1135 * of __aenter__ and push 6 values before jumping to the handler
1136 * if an exception be raised. */
1137 return jump ? -1 + 6 : 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001138 case BEFORE_ASYNC_WITH:
1139 return 1;
1140 case GET_AITER:
1141 return 0;
1142 case GET_ANEXT:
1143 return 1;
Yury Selivanov5376ba92015-06-22 12:19:30 -04001144 case GET_YIELD_FROM_ITER:
1145 return 0;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02001146 case END_ASYNC_FOR:
1147 return -7;
Eric V. Smitha78c7952015-11-03 12:45:05 -05001148 case FORMAT_VALUE:
1149 /* If there's a fmt_spec on the stack, we go from 2->1,
1150 else 1->1. */
1151 return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
Yury Selivanovf2392132016-12-13 19:03:51 -05001152 case LOAD_METHOD:
1153 return 1;
Zackery Spytzce6a0702019-08-25 03:44:09 -06001154 case LOAD_ASSERTION_ERROR:
1155 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001156 case LIST_TO_TUPLE:
1157 return 0;
1158 case LIST_EXTEND:
1159 case SET_UPDATE:
Mark Shannon8a4cd702020-01-27 09:57:45 +00001160 case DICT_MERGE:
1161 case DICT_UPDATE:
Mark Shannon13bc1392020-01-23 09:25:17 +00001162 return -1;
Brandt Bucher145bf262021-02-26 14:51:55 -08001163 case COPY_DICT_WITHOUT_KEYS:
1164 return 0;
1165 case MATCH_CLASS:
1166 return -1;
1167 case GET_LEN:
1168 case MATCH_MAPPING:
1169 case MATCH_SEQUENCE:
1170 return 1;
1171 case MATCH_KEYS:
1172 return 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 default:
Larry Hastings3a907972013-11-23 14:49:22 -08001174 return PY_INVALID_STACK_EFFECT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 }
Larry Hastings3a907972013-11-23 14:49:22 -08001176 return PY_INVALID_STACK_EFFECT; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001177}
1178
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001179int
Serhiy Storchaka7bdf2822018-09-18 09:54:26 +03001180PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump)
1181{
1182 return stack_effect(opcode, oparg, jump);
1183}
1184
1185int
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001186PyCompile_OpcodeStackEffect(int opcode, int oparg)
1187{
1188 return stack_effect(opcode, oparg, -1);
1189}
1190
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001191/* Add an opcode with no argument.
1192 Returns 0 on failure, 1 on success.
1193*/
1194
1195static int
Mark Shannon3bd60352021-01-13 12:05:43 +00001196compiler_addop_line(struct compiler *c, int opcode, int line)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 basicblock *b;
1199 struct instr *i;
1200 int off;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001201 assert(!HAS_ARG(opcode));
Andy Lester76d58772020-03-10 21:18:12 -05001202 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 if (off < 0)
1204 return 0;
1205 b = c->u->u_curblock;
1206 i = &b->b_instr[off];
1207 i->i_opcode = opcode;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001208 i->i_oparg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 if (opcode == RETURN_VALUE)
1210 b->b_return = 1;
Mark Shannon3bd60352021-01-13 12:05:43 +00001211 i->i_lineno = line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213}
1214
Mark Shannon3bd60352021-01-13 12:05:43 +00001215static int
1216compiler_addop(struct compiler *c, int opcode)
1217{
1218 return compiler_addop_line(c, opcode, c->u->u_lineno);
1219}
1220
1221static int
1222compiler_addop_noline(struct compiler *c, int opcode)
1223{
1224 return compiler_addop_line(c, opcode, -1);
1225}
1226
1227
Victor Stinnerf8e32212013-11-19 23:56:34 +01001228static Py_ssize_t
Andy Lester76d58772020-03-10 21:18:12 -05001229compiler_add_o(PyObject *dict, PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001231 PyObject *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001234 v = PyDict_GetItemWithError(dict, o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 if (!v) {
Stefan Krahc0cbed12015-07-27 12:56:49 +02001236 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 return -1;
Stefan Krahc0cbed12015-07-27 12:56:49 +02001238 }
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001239 arg = PyDict_GET_SIZE(dict);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001240 v = PyLong_FromSsize_t(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 if (!v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 return -1;
1243 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001244 if (PyDict_SetItem(dict, o, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 Py_DECREF(v);
1246 return -1;
1247 }
1248 Py_DECREF(v);
1249 }
1250 else
1251 arg = PyLong_AsLong(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001252 return arg;
1253}
1254
INADA Naokic2e16072018-11-26 21:23:22 +09001255// Merge const *o* recursively and return constant key object.
1256static PyObject*
1257merge_consts_recursive(struct compiler *c, PyObject *o)
1258{
1259 // None and Ellipsis are singleton, and key is the singleton.
1260 // No need to merge object and key.
1261 if (o == Py_None || o == Py_Ellipsis) {
1262 Py_INCREF(o);
1263 return o;
1264 }
1265
1266 PyObject *key = _PyCode_ConstantKey(o);
1267 if (key == NULL) {
1268 return NULL;
1269 }
1270
1271 // t is borrowed reference
1272 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
1273 if (t != key) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001274 // o is registered in c_const_cache. Just use it.
Zackery Spytz9b4a1b12019-03-20 03:16:25 -06001275 Py_XINCREF(t);
INADA Naokic2e16072018-11-26 21:23:22 +09001276 Py_DECREF(key);
1277 return t;
1278 }
1279
INADA Naokif7e4d362018-11-29 00:58:46 +09001280 // We registered o in c_const_cache.
Simeon63b5fc52019-04-09 19:36:57 -04001281 // When o is a tuple or frozenset, we want to merge its
INADA Naokif7e4d362018-11-29 00:58:46 +09001282 // items too.
INADA Naokic2e16072018-11-26 21:23:22 +09001283 if (PyTuple_CheckExact(o)) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001284 Py_ssize_t len = PyTuple_GET_SIZE(o);
1285 for (Py_ssize_t i = 0; i < len; i++) {
INADA Naokic2e16072018-11-26 21:23:22 +09001286 PyObject *item = PyTuple_GET_ITEM(o, i);
1287 PyObject *u = merge_consts_recursive(c, item);
1288 if (u == NULL) {
1289 Py_DECREF(key);
1290 return NULL;
1291 }
1292
1293 // See _PyCode_ConstantKey()
1294 PyObject *v; // borrowed
1295 if (PyTuple_CheckExact(u)) {
1296 v = PyTuple_GET_ITEM(u, 1);
1297 }
1298 else {
1299 v = u;
1300 }
1301 if (v != item) {
1302 Py_INCREF(v);
1303 PyTuple_SET_ITEM(o, i, v);
1304 Py_DECREF(item);
1305 }
1306
1307 Py_DECREF(u);
1308 }
1309 }
INADA Naokif7e4d362018-11-29 00:58:46 +09001310 else if (PyFrozenSet_CheckExact(o)) {
Simeon63b5fc52019-04-09 19:36:57 -04001311 // *key* is tuple. And its first item is frozenset of
INADA Naokif7e4d362018-11-29 00:58:46 +09001312 // constant keys.
1313 // See _PyCode_ConstantKey() for detail.
1314 assert(PyTuple_CheckExact(key));
1315 assert(PyTuple_GET_SIZE(key) == 2);
1316
1317 Py_ssize_t len = PySet_GET_SIZE(o);
1318 if (len == 0) { // empty frozenset should not be re-created.
1319 return key;
1320 }
1321 PyObject *tuple = PyTuple_New(len);
1322 if (tuple == NULL) {
1323 Py_DECREF(key);
1324 return NULL;
1325 }
1326 Py_ssize_t i = 0, pos = 0;
1327 PyObject *item;
1328 Py_hash_t hash;
1329 while (_PySet_NextEntry(o, &pos, &item, &hash)) {
1330 PyObject *k = merge_consts_recursive(c, item);
1331 if (k == NULL) {
1332 Py_DECREF(tuple);
1333 Py_DECREF(key);
1334 return NULL;
1335 }
1336 PyObject *u;
1337 if (PyTuple_CheckExact(k)) {
1338 u = PyTuple_GET_ITEM(k, 1);
1339 Py_INCREF(u);
1340 Py_DECREF(k);
1341 }
1342 else {
1343 u = k;
1344 }
1345 PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u.
1346 i++;
1347 }
1348
1349 // Instead of rewriting o, we create new frozenset and embed in the
1350 // key tuple. Caller should get merged frozenset from the key tuple.
1351 PyObject *new = PyFrozenSet_New(tuple);
1352 Py_DECREF(tuple);
1353 if (new == NULL) {
1354 Py_DECREF(key);
1355 return NULL;
1356 }
1357 assert(PyTuple_GET_ITEM(key, 1) == o);
1358 Py_DECREF(o);
1359 PyTuple_SET_ITEM(key, 1, new);
1360 }
INADA Naokic2e16072018-11-26 21:23:22 +09001361
1362 return key;
1363}
1364
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001365static Py_ssize_t
1366compiler_add_const(struct compiler *c, PyObject *o)
1367{
INADA Naokic2e16072018-11-26 21:23:22 +09001368 PyObject *key = merge_consts_recursive(c, o);
1369 if (key == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001370 return -1;
INADA Naokic2e16072018-11-26 21:23:22 +09001371 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001372
Andy Lester76d58772020-03-10 21:18:12 -05001373 Py_ssize_t arg = compiler_add_o(c->u->u_consts, key);
INADA Naokic2e16072018-11-26 21:23:22 +09001374 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001376}
1377
1378static int
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001379compiler_addop_load_const(struct compiler *c, PyObject *o)
1380{
1381 Py_ssize_t arg = compiler_add_const(c, o);
1382 if (arg < 0)
1383 return 0;
1384 return compiler_addop_i(c, LOAD_CONST, arg);
1385}
1386
1387static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001390{
Andy Lester76d58772020-03-10 21:18:12 -05001391 Py_ssize_t arg = compiler_add_o(dict, o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001392 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001393 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001394 return compiler_addop_i(c, opcode, arg);
1395}
1396
1397static int
1398compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001401 Py_ssize_t arg;
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001402
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001403 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1404 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001405 return 0;
Andy Lester76d58772020-03-10 21:18:12 -05001406 arg = compiler_add_o(dict, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407 Py_DECREF(mangled);
1408 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001409 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001410 return compiler_addop_i(c, opcode, arg);
1411}
1412
1413/* Add an opcode with an integer argument.
1414 Returns 0 on failure, 1 on success.
1415*/
1416
1417static int
Victor Stinnerf8e32212013-11-19 23:56:34 +01001418compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 struct instr *i;
1421 int off;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001422
Victor Stinner2ad474b2016-03-01 23:34:47 +01001423 /* oparg value is unsigned, but a signed C int is usually used to store
1424 it in the C code (like Python/ceval.c).
1425
1426 Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1427
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001428 The argument of a concrete bytecode instruction is limited to 8-bit.
1429 EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1430 assert(HAS_ARG(opcode));
Victor Stinner2ad474b2016-03-01 23:34:47 +01001431 assert(0 <= oparg && oparg <= 2147483647);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001432
Andy Lester76d58772020-03-10 21:18:12 -05001433 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 if (off < 0)
1435 return 0;
1436 i = &c->u->u_curblock->b_instr[off];
Victor Stinnerf8e32212013-11-19 23:56:34 +01001437 i->i_opcode = opcode;
1438 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001439 i->i_lineno = c->u->u_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001441}
1442
Mark Shannon28b75c82020-12-23 11:43:10 +00001443static int add_jump_to_block(basicblock *b, int opcode, int lineno, basicblock *target)
1444{
1445 assert(HAS_ARG(opcode));
1446 assert(b != NULL);
1447 assert(target != NULL);
1448
1449 int off = compiler_next_instr(b);
1450 struct instr *i = &b->b_instr[off];
1451 if (off < 0) {
1452 return 0;
1453 }
1454 i->i_opcode = opcode;
1455 i->i_target = target;
1456 i->i_lineno = lineno;
1457 return 1;
1458}
1459
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001460static int
Mark Shannon582aaf12020-08-04 17:30:11 +01001461compiler_addop_j(struct compiler *c, int opcode, basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462{
Mark Shannon28b75c82020-12-23 11:43:10 +00001463 return add_jump_to_block(c->u->u_curblock, opcode, c->u->u_lineno, b);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001464}
1465
Mark Shannon127dde52021-01-04 18:06:55 +00001466static int
1467compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b)
1468{
1469 return add_jump_to_block(c->u->u_curblock, opcode, -1, b);
1470}
1471
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +01001472/* NEXT_BLOCK() creates an implicit jump from the current block
1473 to the new block.
1474
1475 The returns inside this macro make it impossible to decref objects
1476 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001477*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001478#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 if (compiler_next_block((C)) == NULL) \
1480 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001481}
1482
1483#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 if (!compiler_addop((C), (OP))) \
1485 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001486}
1487
Mark Shannon3bd60352021-01-13 12:05:43 +00001488#define ADDOP_NOLINE(C, OP) { \
1489 if (!compiler_addop_noline((C), (OP))) \
1490 return 0; \
1491}
1492
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001493#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 if (!compiler_addop((C), (OP))) { \
1495 compiler_exit_scope(c); \
1496 return 0; \
1497 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001498}
1499
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001500#define ADDOP_LOAD_CONST(C, O) { \
1501 if (!compiler_addop_load_const((C), (O))) \
1502 return 0; \
1503}
1504
1505/* Same as ADDOP_LOAD_CONST, but steals a reference. */
1506#define ADDOP_LOAD_CONST_NEW(C, O) { \
1507 PyObject *__new_const = (O); \
1508 if (__new_const == NULL) { \
1509 return 0; \
1510 } \
1511 if (!compiler_addop_load_const((C), __new_const)) { \
1512 Py_DECREF(__new_const); \
1513 return 0; \
1514 } \
1515 Py_DECREF(__new_const); \
1516}
1517
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001518#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1520 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521}
1522
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001523/* Same as ADDOP_O, but steals a reference. */
1524#define ADDOP_N(C, OP, O, TYPE) { \
1525 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1526 Py_DECREF((O)); \
1527 return 0; \
1528 } \
1529 Py_DECREF((O)); \
1530}
1531
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1534 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535}
1536
1537#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 if (!compiler_addop_i((C), (OP), (O))) \
1539 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001540}
1541
Mark Shannon582aaf12020-08-04 17:30:11 +01001542#define ADDOP_JUMP(C, OP, O) { \
1543 if (!compiler_addop_j((C), (OP), (O))) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545}
1546
Mark Shannon127dde52021-01-04 18:06:55 +00001547/* Add a jump with no line number.
1548 * Used for artificial jumps that have no corresponding
1549 * token in the source code. */
1550#define ADDOP_JUMP_NOLINE(C, OP, O) { \
1551 if (!compiler_addop_j_noline((C), (OP), (O))) \
1552 return 0; \
1553}
1554
Mark Shannon9af0e472020-01-14 10:12:45 +00001555#define ADDOP_COMPARE(C, CMP) { \
1556 if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \
1557 return 0; \
1558}
1559
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001560/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1561 the ASDL name to synthesize the name of the C type and the visit function.
1562*/
1563
1564#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 if (!compiler_visit_ ## TYPE((C), (V))) \
1566 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001567}
1568
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001569#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 if (!compiler_visit_ ## TYPE((C), (V))) { \
1571 compiler_exit_scope(c); \
1572 return 0; \
1573 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001574}
1575
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001576#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 if (!compiler_visit_slice((C), (V), (CTX))) \
1578 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001579}
1580
1581#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001583 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1585 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1586 if (!compiler_visit_ ## TYPE((C), elt)) \
1587 return 0; \
1588 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589}
1590
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001591#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001593 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1595 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1596 if (!compiler_visit_ ## TYPE((C), elt)) { \
1597 compiler_exit_scope(c); \
1598 return 0; \
1599 } \
1600 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001601}
1602
Brandt Bucher145bf262021-02-26 14:51:55 -08001603#define RETURN_IF_FALSE(X) \
1604 if (!(X)) { \
1605 return 0; \
1606 }
1607
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001608/* Search if variable annotations are present statically in a block. */
1609
1610static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001611find_ann(asdl_stmt_seq *stmts)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001612{
1613 int i, j, res = 0;
1614 stmt_ty st;
1615
1616 for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1617 st = (stmt_ty)asdl_seq_GET(stmts, i);
1618 switch (st->kind) {
1619 case AnnAssign_kind:
1620 return 1;
1621 case For_kind:
1622 res = find_ann(st->v.For.body) ||
1623 find_ann(st->v.For.orelse);
1624 break;
1625 case AsyncFor_kind:
1626 res = find_ann(st->v.AsyncFor.body) ||
1627 find_ann(st->v.AsyncFor.orelse);
1628 break;
1629 case While_kind:
1630 res = find_ann(st->v.While.body) ||
1631 find_ann(st->v.While.orelse);
1632 break;
1633 case If_kind:
1634 res = find_ann(st->v.If.body) ||
1635 find_ann(st->v.If.orelse);
1636 break;
1637 case With_kind:
1638 res = find_ann(st->v.With.body);
1639 break;
1640 case AsyncWith_kind:
1641 res = find_ann(st->v.AsyncWith.body);
1642 break;
1643 case Try_kind:
1644 for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1645 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1646 st->v.Try.handlers, j);
1647 if (find_ann(handler->v.ExceptHandler.body)) {
1648 return 1;
1649 }
1650 }
1651 res = find_ann(st->v.Try.body) ||
1652 find_ann(st->v.Try.finalbody) ||
1653 find_ann(st->v.Try.orelse);
1654 break;
1655 default:
1656 res = 0;
1657 }
1658 if (res) {
1659 break;
1660 }
1661 }
1662 return res;
1663}
1664
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001665/*
1666 * Frame block handling functions
1667 */
1668
1669static int
1670compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b,
Mark Shannonfee55262019-11-21 09:11:43 +00001671 basicblock *exit, void *datum)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001672{
1673 struct fblockinfo *f;
1674 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
Mark Shannon02d126a2020-09-25 14:04:19 +01001675 return compiler_error(c, "too many statically nested blocks");
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001676 }
1677 f = &c->u->u_fblock[c->u->u_nfblocks++];
1678 f->fb_type = t;
1679 f->fb_block = b;
1680 f->fb_exit = exit;
Mark Shannonfee55262019-11-21 09:11:43 +00001681 f->fb_datum = datum;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001682 return 1;
1683}
1684
1685static void
1686compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
1687{
1688 struct compiler_unit *u = c->u;
1689 assert(u->u_nfblocks > 0);
1690 u->u_nfblocks--;
1691 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
1692 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
1693}
1694
Mark Shannonfee55262019-11-21 09:11:43 +00001695static int
1696compiler_call_exit_with_nones(struct compiler *c) {
1697 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1698 ADDOP(c, DUP_TOP);
1699 ADDOP(c, DUP_TOP);
1700 ADDOP_I(c, CALL_FUNCTION, 3);
1701 return 1;
1702}
1703
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001704/* Unwind a frame block. If preserve_tos is true, the TOS before
Mark Shannonfee55262019-11-21 09:11:43 +00001705 * popping the blocks will be restored afterwards, unless another
1706 * return, break or continue is found. In which case, the TOS will
1707 * be popped.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001708 */
1709static int
1710compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
1711 int preserve_tos)
1712{
1713 switch (info->fb_type) {
1714 case WHILE_LOOP:
Mark Shannon02d126a2020-09-25 14:04:19 +01001715 case EXCEPTION_HANDLER:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001716 return 1;
1717
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001718 case FOR_LOOP:
1719 /* Pop the iterator */
1720 if (preserve_tos) {
1721 ADDOP(c, ROT_TWO);
1722 }
1723 ADDOP(c, POP_TOP);
1724 return 1;
1725
Mark Shannon02d126a2020-09-25 14:04:19 +01001726 case TRY_EXCEPT:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001727 ADDOP(c, POP_BLOCK);
1728 return 1;
1729
1730 case FINALLY_TRY:
Mark Shannon5274b682020-12-16 13:07:01 +00001731 /* This POP_BLOCK gets the line number of the unwinding statement */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001732 ADDOP(c, POP_BLOCK);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001733 if (preserve_tos) {
Mark Shannonfee55262019-11-21 09:11:43 +00001734 if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) {
1735 return 0;
1736 }
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001737 }
Mark Shannon5274b682020-12-16 13:07:01 +00001738 /* Emit the finally block */
Mark Shannonfee55262019-11-21 09:11:43 +00001739 VISIT_SEQ(c, stmt, info->fb_datum);
1740 if (preserve_tos) {
1741 compiler_pop_fblock(c, POP_VALUE, NULL);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001742 }
Mark Shannon5274b682020-12-16 13:07:01 +00001743 /* The finally block should appear to execute after the
1744 * statement causing the unwinding, so make the unwinding
1745 * instruction artificial */
1746 c->u->u_lineno = -1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001747 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001748
Mark Shannonfee55262019-11-21 09:11:43 +00001749 case FINALLY_END:
1750 if (preserve_tos) {
1751 ADDOP(c, ROT_FOUR);
1752 }
1753 ADDOP(c, POP_TOP);
1754 ADDOP(c, POP_TOP);
1755 ADDOP(c, POP_TOP);
1756 if (preserve_tos) {
1757 ADDOP(c, ROT_FOUR);
1758 }
1759 ADDOP(c, POP_EXCEPT);
1760 return 1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001761
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001762 case WITH:
1763 case ASYNC_WITH:
1764 ADDOP(c, POP_BLOCK);
1765 if (preserve_tos) {
1766 ADDOP(c, ROT_TWO);
1767 }
Mark Shannonfee55262019-11-21 09:11:43 +00001768 if(!compiler_call_exit_with_nones(c)) {
1769 return 0;
1770 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001771 if (info->fb_type == ASYNC_WITH) {
1772 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001773 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001774 ADDOP(c, YIELD_FROM);
1775 }
Mark Shannonfee55262019-11-21 09:11:43 +00001776 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001777 return 1;
1778
1779 case HANDLER_CLEANUP:
Mark Shannonfee55262019-11-21 09:11:43 +00001780 if (info->fb_datum) {
1781 ADDOP(c, POP_BLOCK);
1782 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001783 if (preserve_tos) {
1784 ADDOP(c, ROT_FOUR);
1785 }
Mark Shannonfee55262019-11-21 09:11:43 +00001786 ADDOP(c, POP_EXCEPT);
1787 if (info->fb_datum) {
1788 ADDOP_LOAD_CONST(c, Py_None);
1789 compiler_nameop(c, info->fb_datum, Store);
1790 compiler_nameop(c, info->fb_datum, Del);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001791 }
Mark Shannonfee55262019-11-21 09:11:43 +00001792 return 1;
1793
1794 case POP_VALUE:
1795 if (preserve_tos) {
1796 ADDOP(c, ROT_TWO);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001797 }
Mark Shannonfee55262019-11-21 09:11:43 +00001798 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001799 return 1;
1800 }
1801 Py_UNREACHABLE();
1802}
1803
Mark Shannonfee55262019-11-21 09:11:43 +00001804/** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */
1805static int
1806compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) {
1807 if (c->u->u_nfblocks == 0) {
1808 return 1;
1809 }
1810 struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1];
1811 if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) {
1812 *loop = top;
1813 return 1;
1814 }
1815 struct fblockinfo copy = *top;
1816 c->u->u_nfblocks--;
1817 if (!compiler_unwind_fblock(c, &copy, preserve_tos)) {
1818 return 0;
1819 }
1820 if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) {
1821 return 0;
1822 }
1823 c->u->u_fblock[c->u->u_nfblocks] = copy;
1824 c->u->u_nfblocks++;
1825 return 1;
1826}
1827
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001828/* Compile a sequence of statements, checking for a docstring
1829 and for annotations. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830
1831static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001832compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001833{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001834 int i = 0;
1835 stmt_ty st;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001836 PyObject *docstring;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001837
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001838 /* Set current line number to the line number of first statement.
1839 This way line number for SETUP_ANNOTATIONS will always
1840 coincide with the line number of first "real" statement in module.
Hansraj Das01171eb2019-10-09 07:54:02 +05301841 If body is empty, then lineno will be set later in assemble. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001842 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) {
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001843 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001844 SET_LOC(c, st);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001845 }
1846 /* Every annotated class and module should have __annotations__. */
1847 if (find_ann(stmts)) {
1848 ADDOP(c, SETUP_ANNOTATIONS);
1849 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001850 if (!asdl_seq_LEN(stmts))
1851 return 1;
INADA Naokicb41b272017-02-23 00:31:59 +09001852 /* if not -OO mode, set docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001853 if (c->c_optimize < 2) {
1854 docstring = _PyAST_GetDocString(stmts);
1855 if (docstring) {
1856 i = 1;
1857 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1858 assert(st->kind == Expr_kind);
1859 VISIT(c, expr, st->v.Expr.value);
1860 if (!compiler_nameop(c, __doc__, Store))
1861 return 0;
1862 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001864 for (; i < asdl_seq_LEN(stmts); i++)
1865 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001867}
1868
1869static PyCodeObject *
1870compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001871{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 PyCodeObject *co;
1873 int addNone = 1;
1874 static PyObject *module;
1875 if (!module) {
1876 module = PyUnicode_InternFromString("<module>");
1877 if (!module)
1878 return NULL;
1879 }
1880 /* Use 0 for firstlineno initially, will fixup in assemble(). */
Mark Shannon877df852020-11-12 09:43:29 +00001881 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 return NULL;
1883 switch (mod->kind) {
1884 case Module_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001885 if (!compiler_body(c, mod->v.Module.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 compiler_exit_scope(c);
1887 return 0;
1888 }
1889 break;
1890 case Interactive_kind:
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001891 if (find_ann(mod->v.Interactive.body)) {
1892 ADDOP(c, SETUP_ANNOTATIONS);
1893 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 c->c_interactive = 1;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001895 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 break;
1897 case Expression_kind:
1898 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1899 addNone = 0;
1900 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 default:
1902 PyErr_Format(PyExc_SystemError,
1903 "module kind %d should not be possible",
1904 mod->kind);
1905 return 0;
1906 }
1907 co = assemble(c, addNone);
1908 compiler_exit_scope(c);
1909 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001910}
1911
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001912/* The test for LOCAL must come before the test for FREE in order to
1913 handle classes where name is both local and free. The local var is
1914 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001915*/
1916
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001917static int
1918get_ref_type(struct compiler *c, PyObject *name)
1919{
Victor Stinner0b1bc562013-05-16 22:17:17 +02001920 int scope;
Benjamin Peterson312595c2013-05-15 15:26:42 -05001921 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001922 _PyUnicode_EqualToASCIIString(name, "__class__"))
Benjamin Peterson312595c2013-05-15 15:26:42 -05001923 return CELL;
Victor Stinner0b1bc562013-05-16 22:17:17 +02001924 scope = PyST_GetScope(c->u->u_ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 if (scope == 0) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001926 PyErr_Format(PyExc_SystemError,
1927 "PyST_GetScope(name=%R) failed: "
1928 "unknown scope in unit %S (%R); "
1929 "symbols: %R; locals: %R; globals: %R",
1930 name,
1931 c->u->u_name, c->u->u_ste->ste_id,
1932 c->u->u_ste->ste_symbols, c->u->u_varnames, c->u->u_names);
1933 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001936}
1937
1938static int
1939compiler_lookup_arg(PyObject *dict, PyObject *name)
1940{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001941 PyObject *v;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001942 v = PyDict_GetItemWithError(dict, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001943 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001944 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001945 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001946}
1947
1948static int
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001949compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags,
1950 PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001951{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001952 Py_ssize_t i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001953 if (qualname == NULL)
1954 qualname = co->co_name;
1955
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001956 if (free) {
1957 for (i = 0; i < free; ++i) {
1958 /* Bypass com_addop_varname because it will generate
1959 LOAD_DEREF but LOAD_CLOSURE is needed.
1960 */
1961 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001962
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001963 /* Special case: If a class contains a method with a
1964 free variable that has the same name as a method,
1965 the name will be considered free *and* local in the
1966 class. It should be handled by the closure, as
Min ho Kimc4cacc82019-07-31 08:16:13 +10001967 well as by the normal name lookup logic.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001968 */
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001969 int reftype = get_ref_type(c, name);
1970 if (reftype == -1) {
1971 return 0;
1972 }
1973 int arg;
1974 if (reftype == CELL) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001975 arg = compiler_lookup_arg(c->u->u_cellvars, name);
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001976 }
1977 else {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001978 arg = compiler_lookup_arg(c->u->u_freevars, name);
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001979 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001980 if (arg == -1) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001981 PyErr_Format(PyExc_SystemError,
1982 "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; "
1983 "freevars of code %S: %R",
1984 name,
1985 reftype,
1986 c->u->u_name,
1987 co->co_name,
1988 co->co_freevars);
1989 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001990 }
1991 ADDOP_I(c, LOAD_CLOSURE, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001993 flags |= 0x08;
1994 ADDOP_I(c, BUILD_TUPLE, free);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001996 ADDOP_LOAD_CONST(c, (PyObject*)co);
1997 ADDOP_LOAD_CONST(c, qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001998 ADDOP_I(c, MAKE_FUNCTION, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002000}
2001
2002static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002003compiler_decorators(struct compiler *c, asdl_expr_seq* decos)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 if (!decos)
2008 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2011 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
2012 }
2013 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002014}
2015
2016static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002017compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs,
2018 asdl_expr_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00002019{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002020 /* Push a dict of keyword-only default values.
2021
2022 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
2023 */
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002024 int i;
2025 PyObject *keys = NULL;
2026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
2028 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
2029 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
2030 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04002031 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002032 if (!mangled) {
2033 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002035 if (keys == NULL) {
2036 keys = PyList_New(1);
2037 if (keys == NULL) {
2038 Py_DECREF(mangled);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002039 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002040 }
2041 PyList_SET_ITEM(keys, 0, mangled);
2042 }
2043 else {
2044 int res = PyList_Append(keys, mangled);
2045 Py_DECREF(mangled);
2046 if (res == -1) {
2047 goto error;
2048 }
2049 }
2050 if (!compiler_visit_expr(c, default_)) {
2051 goto error;
2052 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 }
2054 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002055 if (keys != NULL) {
2056 Py_ssize_t default_count = PyList_GET_SIZE(keys);
2057 PyObject *keys_tuple = PyList_AsTuple(keys);
2058 Py_DECREF(keys);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002059 ADDOP_LOAD_CONST_NEW(c, keys_tuple);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002060 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002061 assert(default_count > 0);
2062 return 1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002063 }
2064 else {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002065 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002066 }
2067
2068error:
2069 Py_XDECREF(keys);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002070 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002071}
2072
2073static int
Guido van Rossum95e4d582018-01-26 08:20:18 -08002074compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
2075{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +03002076 ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation));
Guido van Rossum95e4d582018-01-26 08:20:18 -08002077 return 1;
2078}
2079
2080static int
Neal Norwitzc1505362006-12-28 06:47:50 +00002081compiler_visit_argannotation(struct compiler *c, identifier id,
Yurii Karabas73019792020-11-25 12:43:18 +02002082 expr_ty annotation, Py_ssize_t *annotations_len)
Neal Norwitzc1505362006-12-28 06:47:50 +00002083{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 if (annotation) {
Yurii Karabas73019792020-11-25 12:43:18 +02002085 PyObject *mangled = _Py_Mangle(c->u->u_private, id);
Yury Selivanov34ce99f2014-02-18 12:49:41 -05002086 if (!mangled)
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002087 return 0;
Yurii Karabas73019792020-11-25 12:43:18 +02002088
2089 ADDOP_LOAD_CONST(c, mangled);
Yury Selivanov34ce99f2014-02-18 12:49:41 -05002090 Py_DECREF(mangled);
Yurii Karabas73019792020-11-25 12:43:18 +02002091 VISIT(c, annexpr, annotation);
2092 *annotations_len += 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002094 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002095}
2096
2097static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002098compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args,
Yurii Karabas73019792020-11-25 12:43:18 +02002099 Py_ssize_t *annotations_len)
Neal Norwitzc1505362006-12-28 06:47:50 +00002100{
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002101 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002102 for (i = 0; i < asdl_seq_LEN(args); i++) {
2103 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002104 if (!compiler_visit_argannotation(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 c,
2106 arg->arg,
2107 arg->annotation,
Yurii Karabas73019792020-11-25 12:43:18 +02002108 annotations_len))
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002109 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002111 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002112}
2113
2114static int
2115compiler_visit_annotations(struct compiler *c, arguments_ty args,
2116 expr_ty returns)
2117{
Yurii Karabas73019792020-11-25 12:43:18 +02002118 /* Push arg annotation names and values.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002119 The expressions are evaluated out-of-order wrt the source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00002120
Yurii Karabas73019792020-11-25 12:43:18 +02002121 Return 0 on error, -1 if no annotations pushed, 1 if a annotations is pushed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 */
2123 static identifier return_str;
Yurii Karabas73019792020-11-25 12:43:18 +02002124 Py_ssize_t annotations_len = 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002125
Yurii Karabas73019792020-11-25 12:43:18 +02002126 if (!compiler_visit_argannotations(c, args->args, &annotations_len))
2127 return 0;
2128 if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len))
2129 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002130 if (args->vararg && args->vararg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002131 !compiler_visit_argannotation(c, args->vararg->arg,
Yurii Karabas73019792020-11-25 12:43:18 +02002132 args->vararg->annotation, &annotations_len))
2133 return 0;
2134 if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len))
2135 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002136 if (args->kwarg && args->kwarg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002137 !compiler_visit_argannotation(c, args->kwarg->arg,
Yurii Karabas73019792020-11-25 12:43:18 +02002138 args->kwarg->annotation, &annotations_len))
2139 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002141 if (!return_str) {
2142 return_str = PyUnicode_InternFromString("return");
2143 if (!return_str)
Yurii Karabas73019792020-11-25 12:43:18 +02002144 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002145 }
Yurii Karabas73019792020-11-25 12:43:18 +02002146 if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) {
2147 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 }
2149
Yurii Karabas73019792020-11-25 12:43:18 +02002150 if (annotations_len) {
2151 ADDOP_I(c, BUILD_TUPLE, annotations_len);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002152 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002154
Yurii Karabas73019792020-11-25 12:43:18 +02002155 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002156}
2157
2158static int
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002159compiler_visit_defaults(struct compiler *c, arguments_ty args)
2160{
2161 VISIT_SEQ(c, expr, args->defaults);
2162 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
2163 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002164}
2165
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002166static Py_ssize_t
2167compiler_default_arguments(struct compiler *c, arguments_ty args)
2168{
2169 Py_ssize_t funcflags = 0;
2170 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002171 if (!compiler_visit_defaults(c, args))
2172 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002173 funcflags |= 0x01;
2174 }
2175 if (args->kwonlyargs) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002176 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002177 args->kw_defaults);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002178 if (res == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002179 return -1;
2180 }
2181 else if (res > 0) {
2182 funcflags |= 0x02;
2183 }
2184 }
2185 return funcflags;
2186}
2187
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002188static int
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002189forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx)
2190{
2191
2192 if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2193 compiler_error(c, "cannot assign to __debug__");
2194 return 1;
2195 }
2196 return 0;
2197}
2198
2199static int
2200compiler_check_debug_one_arg(struct compiler *c, arg_ty arg)
2201{
2202 if (arg != NULL) {
2203 if (forbidden_name(c, arg->arg, Store))
2204 return 0;
2205 }
2206 return 1;
2207}
2208
2209static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002210compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args)
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002211{
2212 if (args != NULL) {
Pablo Galindoee40e4b2020-04-23 03:43:08 +01002213 for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002214 if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i)))
2215 return 0;
2216 }
2217 }
2218 return 1;
2219}
2220
2221static int
2222compiler_check_debug_args(struct compiler *c, arguments_ty args)
2223{
2224 if (!compiler_check_debug_args_seq(c, args->posonlyargs))
2225 return 0;
2226 if (!compiler_check_debug_args_seq(c, args->args))
2227 return 0;
2228 if (!compiler_check_debug_one_arg(c, args->vararg))
2229 return 0;
2230 if (!compiler_check_debug_args_seq(c, args->kwonlyargs))
2231 return 0;
2232 if (!compiler_check_debug_one_arg(c, args->kwarg))
2233 return 0;
2234 return 1;
2235}
2236
2237static int
Yury Selivanov75445082015-05-11 22:57:16 -04002238compiler_function(struct compiler *c, stmt_ty s, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002240 PyCodeObject *co;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002241 PyObject *qualname, *docstring = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002242 arguments_ty args;
2243 expr_ty returns;
2244 identifier name;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002245 asdl_expr_seq* decos;
2246 asdl_stmt_seq *body;
INADA Naokicb41b272017-02-23 00:31:59 +09002247 Py_ssize_t i, funcflags;
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002248 int annotations;
Yury Selivanov75445082015-05-11 22:57:16 -04002249 int scope_type;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002250 int firstlineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251
Yury Selivanov75445082015-05-11 22:57:16 -04002252 if (is_async) {
2253 assert(s->kind == AsyncFunctionDef_kind);
2254
2255 args = s->v.AsyncFunctionDef.args;
2256 returns = s->v.AsyncFunctionDef.returns;
2257 decos = s->v.AsyncFunctionDef.decorator_list;
2258 name = s->v.AsyncFunctionDef.name;
2259 body = s->v.AsyncFunctionDef.body;
2260
2261 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
2262 } else {
2263 assert(s->kind == FunctionDef_kind);
2264
2265 args = s->v.FunctionDef.args;
2266 returns = s->v.FunctionDef.returns;
2267 decos = s->v.FunctionDef.decorator_list;
2268 name = s->v.FunctionDef.name;
2269 body = s->v.FunctionDef.body;
2270
2271 scope_type = COMPILER_SCOPE_FUNCTION;
2272 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002273
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002274 if (!compiler_check_debug_args(c, args))
2275 return 0;
2276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 if (!compiler_decorators(c, decos))
2278 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002279
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002280 firstlineno = s->lineno;
2281 if (asdl_seq_LEN(decos)) {
2282 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2283 }
2284
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002285 funcflags = compiler_default_arguments(c, args);
2286 if (funcflags == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002288 }
2289
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002290 annotations = compiler_visit_annotations(c, args, returns);
2291 if (annotations == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002292 return 0;
2293 }
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002294 else if (annotations > 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002295 funcflags |= 0x04;
2296 }
2297
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002298 if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002299 return 0;
2300 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301
INADA Naokicb41b272017-02-23 00:31:59 +09002302 /* if not -OO mode, add docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002303 if (c->c_optimize < 2) {
2304 docstring = _PyAST_GetDocString(body);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002305 }
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002306 if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 compiler_exit_scope(c);
2308 return 0;
2309 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002312 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Mark Shannon877df852020-11-12 09:43:29 +00002314 for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) {
Mark Shannonfd009e62020-11-13 12:53:53 +00002315 VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i));
Mark Shannon877df852020-11-12 09:43:29 +00002316 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002318 qualname = c->u->u_qualname;
2319 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002321 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002322 Py_XDECREF(qualname);
2323 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002325 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002326
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002327 if (!compiler_make_closure(c, co, funcflags, qualname)) {
2328 Py_DECREF(qualname);
2329 Py_DECREF(co);
2330 return 0;
2331 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002332 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002333 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 /* decorators */
2336 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2337 ADDOP_I(c, CALL_FUNCTION, 1);
2338 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002339
Yury Selivanov75445082015-05-11 22:57:16 -04002340 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002341}
2342
2343static int
2344compiler_class(struct compiler *c, stmt_ty s)
2345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 PyCodeObject *co;
2347 PyObject *str;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002348 int i, firstlineno;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002349 asdl_expr_seq *decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 if (!compiler_decorators(c, decos))
2352 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002353
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002354 firstlineno = s->lineno;
2355 if (asdl_seq_LEN(decos)) {
2356 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2357 }
2358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 /* ultimately generate code for:
2360 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
2361 where:
2362 <func> is a function/closure created from the class body;
2363 it has a single argument (__locals__) where the dict
2364 (or MutableSequence) representing the locals is passed
2365 <name> is the class name
2366 <bases> is the positional arguments and *varargs argument
2367 <keywords> is the keyword arguments and **kwds argument
2368 This borrows from compiler_call.
2369 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002372 if (!compiler_enter_scope(c, s->v.ClassDef.name,
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002373 COMPILER_SCOPE_CLASS, (void *)s, firstlineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 return 0;
2375 /* this block represents what we do in the new scope */
2376 {
2377 /* use the class name for name mangling */
2378 Py_INCREF(s->v.ClassDef.name);
Serhiy Storchaka48842712016-04-06 09:45:48 +03002379 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 /* load (global) __name__ ... */
2381 str = PyUnicode_InternFromString("__name__");
2382 if (!str || !compiler_nameop(c, str, Load)) {
2383 Py_XDECREF(str);
2384 compiler_exit_scope(c);
2385 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002386 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 Py_DECREF(str);
2388 /* ... and store it as __module__ */
2389 str = PyUnicode_InternFromString("__module__");
2390 if (!str || !compiler_nameop(c, str, Store)) {
2391 Py_XDECREF(str);
2392 compiler_exit_scope(c);
2393 return 0;
2394 }
2395 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002396 assert(c->u->u_qualname);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002397 ADDOP_LOAD_CONST(c, c->u->u_qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002398 str = PyUnicode_InternFromString("__qualname__");
2399 if (!str || !compiler_nameop(c, str, Store)) {
2400 Py_XDECREF(str);
2401 compiler_exit_scope(c);
2402 return 0;
2403 }
2404 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 /* compile the body proper */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002406 if (!compiler_body(c, s->v.ClassDef.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 compiler_exit_scope(c);
2408 return 0;
2409 }
Mark Shannone56d54e2021-01-15 13:52:00 +00002410 /* The following code is artificial */
2411 c->u->u_lineno = -1;
Nick Coghlan19d24672016-12-05 16:47:55 +10002412 /* Return __classcell__ if it is referenced, otherwise return None */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002413 if (c->u->u_ste->ste_needs_class_closure) {
Nick Coghlan19d24672016-12-05 16:47:55 +10002414 /* Store __classcell__ into class namespace & return it */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002415 str = PyUnicode_InternFromString("__class__");
2416 if (str == NULL) {
2417 compiler_exit_scope(c);
2418 return 0;
2419 }
2420 i = compiler_lookup_arg(c->u->u_cellvars, str);
2421 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01002422 if (i < 0) {
2423 compiler_exit_scope(c);
2424 return 0;
2425 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002426 assert(i == 0);
Nick Coghlan944368e2016-09-11 14:45:49 +10002427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 ADDOP_I(c, LOAD_CLOSURE, i);
Nick Coghlan19d24672016-12-05 16:47:55 +10002429 ADDOP(c, DUP_TOP);
Nick Coghlan944368e2016-09-11 14:45:49 +10002430 str = PyUnicode_InternFromString("__classcell__");
2431 if (!str || !compiler_nameop(c, str, Store)) {
2432 Py_XDECREF(str);
2433 compiler_exit_scope(c);
2434 return 0;
2435 }
2436 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002438 else {
Nick Coghlan19d24672016-12-05 16:47:55 +10002439 /* No methods referenced __class__, so just return None */
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002440 assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002441 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson312595c2013-05-15 15:26:42 -05002442 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002443 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 /* create the code object */
2445 co = assemble(c, 1);
2446 }
2447 /* leave the new scope */
2448 compiler_exit_scope(c);
2449 if (co == NULL)
2450 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 /* 2. load the 'build_class' function */
2453 ADDOP(c, LOAD_BUILD_CLASS);
2454
2455 /* 3. load a function (or closure) made from the code object */
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002456 if (!compiler_make_closure(c, co, 0, NULL)) {
2457 Py_DECREF(co);
2458 return 0;
2459 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 Py_DECREF(co);
2461
2462 /* 4. load class name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002463 ADDOP_LOAD_CONST(c, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464
2465 /* 5. generate the rest of the code for the call */
Pablo Galindoa5634c42020-09-16 19:42:00 +01002466 if (!compiler_call_helper(c, 2, s->v.ClassDef.bases, s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002467 return 0;
2468
2469 /* 6. apply decorators */
2470 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2471 ADDOP_I(c, CALL_FUNCTION, 1);
2472 }
2473
2474 /* 7. store into <name> */
2475 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2476 return 0;
2477 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002478}
2479
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02002480/* Return 0 if the expression is a constant value except named singletons.
2481 Return 1 otherwise. */
2482static int
2483check_is_arg(expr_ty e)
2484{
2485 if (e->kind != Constant_kind) {
2486 return 1;
2487 }
2488 PyObject *value = e->v.Constant.value;
2489 return (value == Py_None
2490 || value == Py_False
2491 || value == Py_True
2492 || value == Py_Ellipsis);
2493}
2494
2495/* Check operands of identity chacks ("is" and "is not").
2496 Emit a warning if any operand is a constant except named singletons.
2497 Return 0 on error.
2498 */
2499static int
2500check_compare(struct compiler *c, expr_ty e)
2501{
2502 Py_ssize_t i, n;
2503 int left = check_is_arg(e->v.Compare.left);
2504 n = asdl_seq_LEN(e->v.Compare.ops);
2505 for (i = 0; i < n; i++) {
2506 cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i);
2507 int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2508 if (op == Is || op == IsNot) {
2509 if (!right || !left) {
2510 const char *msg = (op == Is)
2511 ? "\"is\" with a literal. Did you mean \"==\"?"
2512 : "\"is not\" with a literal. Did you mean \"!=\"?";
2513 return compiler_warn(c, msg);
2514 }
2515 }
2516 left = right;
2517 }
2518 return 1;
2519}
2520
Mark Shannon9af0e472020-01-14 10:12:45 +00002521static int compiler_addcompare(struct compiler *c, cmpop_ty op)
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002522{
Mark Shannon9af0e472020-01-14 10:12:45 +00002523 int cmp;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002524 switch (op) {
2525 case Eq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002526 cmp = Py_EQ;
2527 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002528 case NotEq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002529 cmp = Py_NE;
2530 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002531 case Lt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002532 cmp = Py_LT;
2533 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002534 case LtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002535 cmp = Py_LE;
2536 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002537 case Gt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002538 cmp = Py_GT;
2539 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002540 case GtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002541 cmp = Py_GE;
2542 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002543 case Is:
Mark Shannon9af0e472020-01-14 10:12:45 +00002544 ADDOP_I(c, IS_OP, 0);
2545 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002546 case IsNot:
Mark Shannon9af0e472020-01-14 10:12:45 +00002547 ADDOP_I(c, IS_OP, 1);
2548 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002549 case In:
Mark Shannon9af0e472020-01-14 10:12:45 +00002550 ADDOP_I(c, CONTAINS_OP, 0);
2551 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002552 case NotIn:
Mark Shannon9af0e472020-01-14 10:12:45 +00002553 ADDOP_I(c, CONTAINS_OP, 1);
2554 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002555 default:
Mark Shannon9af0e472020-01-14 10:12:45 +00002556 Py_UNREACHABLE();
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002557 }
Mark Shannon9af0e472020-01-14 10:12:45 +00002558 ADDOP_I(c, COMPARE_OP, cmp);
2559 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002560}
2561
Mark Shannon9af0e472020-01-14 10:12:45 +00002562
2563
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002564static int
2565compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2566{
2567 switch (e->kind) {
2568 case UnaryOp_kind:
2569 if (e->v.UnaryOp.op == Not)
2570 return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2571 /* fallback to general implementation */
2572 break;
2573 case BoolOp_kind: {
Pablo Galindoa5634c42020-09-16 19:42:00 +01002574 asdl_expr_seq *s = e->v.BoolOp.values;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002575 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2576 assert(n >= 0);
2577 int cond2 = e->v.BoolOp.op == Or;
2578 basicblock *next2 = next;
2579 if (!cond2 != !cond) {
2580 next2 = compiler_new_block(c);
2581 if (next2 == NULL)
2582 return 0;
2583 }
2584 for (i = 0; i < n; ++i) {
2585 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2586 return 0;
2587 }
2588 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2589 return 0;
2590 if (next2 != next)
2591 compiler_use_next_block(c, next2);
2592 return 1;
2593 }
2594 case IfExp_kind: {
2595 basicblock *end, *next2;
2596 end = compiler_new_block(c);
2597 if (end == NULL)
2598 return 0;
2599 next2 = compiler_new_block(c);
2600 if (next2 == NULL)
2601 return 0;
2602 if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2603 return 0;
2604 if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2605 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002606 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002607 compiler_use_next_block(c, next2);
2608 if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2609 return 0;
2610 compiler_use_next_block(c, end);
2611 return 1;
2612 }
2613 case Compare_kind: {
2614 Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2615 if (n > 0) {
Serhiy Storchaka45835252019-02-16 08:29:46 +02002616 if (!check_compare(c, e)) {
2617 return 0;
2618 }
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002619 basicblock *cleanup = compiler_new_block(c);
2620 if (cleanup == NULL)
2621 return 0;
2622 VISIT(c, expr, e->v.Compare.left);
2623 for (i = 0; i < n; i++) {
2624 VISIT(c, expr,
2625 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2626 ADDOP(c, DUP_TOP);
2627 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00002628 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01002629 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002630 NEXT_BLOCK(c);
2631 }
2632 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00002633 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Mark Shannon582aaf12020-08-04 17:30:11 +01002634 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Mark Shannon266b4622020-11-17 19:30:14 +00002635 NEXT_BLOCK(c);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002636 basicblock *end = compiler_new_block(c);
2637 if (end == NULL)
2638 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002639 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002640 compiler_use_next_block(c, cleanup);
2641 ADDOP(c, POP_TOP);
2642 if (!cond) {
Mark Shannon127dde52021-01-04 18:06:55 +00002643 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002644 }
2645 compiler_use_next_block(c, end);
2646 return 1;
2647 }
2648 /* fallback to general implementation */
2649 break;
2650 }
2651 default:
2652 /* fallback to general implementation */
2653 break;
2654 }
2655
2656 /* general implementation */
2657 VISIT(c, expr, e);
Mark Shannon582aaf12020-08-04 17:30:11 +01002658 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Mark Shannon266b4622020-11-17 19:30:14 +00002659 NEXT_BLOCK(c);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002660 return 1;
2661}
2662
2663static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002664compiler_ifexp(struct compiler *c, expr_ty e)
2665{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 basicblock *end, *next;
2667
2668 assert(e->kind == IfExp_kind);
2669 end = compiler_new_block(c);
2670 if (end == NULL)
2671 return 0;
2672 next = compiler_new_block(c);
2673 if (next == NULL)
2674 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002675 if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2676 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 VISIT(c, expr, e->v.IfExp.body);
Mark Shannon127dde52021-01-04 18:06:55 +00002678 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 compiler_use_next_block(c, next);
2680 VISIT(c, expr, e->v.IfExp.orelse);
2681 compiler_use_next_block(c, end);
2682 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002683}
2684
2685static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002686compiler_lambda(struct compiler *c, expr_ty e)
2687{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002689 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002690 static identifier name;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002691 Py_ssize_t funcflags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002692 arguments_ty args = e->v.Lambda.args;
2693 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002694
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002695 if (!compiler_check_debug_args(c, args))
2696 return 0;
2697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 if (!name) {
2699 name = PyUnicode_InternFromString("<lambda>");
2700 if (!name)
2701 return 0;
2702 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002703
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002704 funcflags = compiler_default_arguments(c, args);
2705 if (funcflags == -1) {
2706 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002707 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002708
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002709 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002710 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002711 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002713 /* Make None the first constant, so the lambda can't have a
2714 docstring. */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002715 if (compiler_add_const(c, Py_None) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002718 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002719 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002720 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2721 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2722 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002723 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002724 }
2725 else {
2726 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002727 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002728 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002729 qualname = c->u->u_qualname;
2730 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002731 compiler_exit_scope(c);
Pablo Galindo7fdab832021-01-29 22:40:59 +00002732 if (co == NULL) {
2733 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002734 return 0;
Pablo Galindo7fdab832021-01-29 22:40:59 +00002735 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002736
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002737 if (!compiler_make_closure(c, co, funcflags, qualname)) {
2738 Py_DECREF(qualname);
2739 Py_DECREF(co);
2740 return 0;
2741 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002742 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 Py_DECREF(co);
2744
2745 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002746}
2747
2748static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002749compiler_if(struct compiler *c, stmt_ty s)
2750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 basicblock *end, *next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002752 assert(s->kind == If_kind);
2753 end = compiler_new_block(c);
Mark Shannon8473cf82020-12-15 11:07:50 +00002754 if (end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 return 0;
Mark Shannon8473cf82020-12-15 11:07:50 +00002756 }
2757 if (asdl_seq_LEN(s->v.If.orelse)) {
2758 next = compiler_new_block(c);
2759 if (next == NULL) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002760 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002761 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002762 }
2763 else {
2764 next = end;
2765 }
2766 if (!compiler_jump_if(c, s->v.If.test, next, 0)) {
2767 return 0;
2768 }
2769 VISIT_SEQ(c, stmt, s->v.If.body);
2770 if (asdl_seq_LEN(s->v.If.orelse)) {
Mark Shannon127dde52021-01-04 18:06:55 +00002771 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Mark Shannon8473cf82020-12-15 11:07:50 +00002772 compiler_use_next_block(c, next);
2773 VISIT_SEQ(c, stmt, s->v.If.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 }
2775 compiler_use_next_block(c, end);
2776 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002777}
2778
2779static int
2780compiler_for(struct compiler *c, stmt_ty s)
2781{
Mark Shannon5977a792020-12-02 13:31:40 +00002782 basicblock *start, *body, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002784 start = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002785 body = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 cleanup = compiler_new_block(c);
2787 end = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002788 if (start == NULL || body == NULL || end == NULL || cleanup == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002789 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002790 }
2791 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002793 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002794 VISIT(c, expr, s->v.For.iter);
2795 ADDOP(c, GET_ITER);
2796 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01002797 ADDOP_JUMP(c, FOR_ITER, cleanup);
Mark Shannon5977a792020-12-02 13:31:40 +00002798 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 VISIT(c, expr, s->v.For.target);
2800 VISIT_SEQ(c, stmt, s->v.For.body);
Mark Shannonf5e97b72020-12-14 11:28:39 +00002801 /* Mark jump as artificial */
2802 c->u->u_lineno = -1;
Mark Shannon582aaf12020-08-04 17:30:11 +01002803 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 compiler_use_next_block(c, cleanup);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002805
2806 compiler_pop_fblock(c, FOR_LOOP, start);
2807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 VISIT_SEQ(c, stmt, s->v.For.orelse);
2809 compiler_use_next_block(c, end);
2810 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002811}
2812
Yury Selivanov75445082015-05-11 22:57:16 -04002813
2814static int
2815compiler_async_for(struct compiler *c, stmt_ty s)
2816{
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002817 basicblock *start, *except, *end;
Pablo Galindo90235812020-03-15 04:29:22 +00002818 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07002819 c->u->u_ste->ste_coroutine = 1;
2820 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
Zsolt Dollensteine2396502018-04-27 08:58:56 -07002821 return compiler_error(c, "'async for' outside async function");
2822 }
2823
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002824 start = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002825 except = compiler_new_block(c);
2826 end = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002827
Mark Shannonfee55262019-11-21 09:11:43 +00002828 if (start == NULL || except == NULL || end == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002829 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002830 }
Yury Selivanov75445082015-05-11 22:57:16 -04002831 VISIT(c, expr, s->v.AsyncFor.iter);
2832 ADDOP(c, GET_AITER);
Yury Selivanov75445082015-05-11 22:57:16 -04002833
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002834 compiler_use_next_block(c, start);
Mark Shannonfee55262019-11-21 09:11:43 +00002835 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002836 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002837 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002838 /* SETUP_FINALLY to guard the __anext__ call */
Mark Shannon582aaf12020-08-04 17:30:11 +01002839 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov75445082015-05-11 22:57:16 -04002840 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002841 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04002842 ADDOP(c, YIELD_FROM);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002843 ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */
Yury Selivanov75445082015-05-11 22:57:16 -04002844
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002845 /* Success block for __anext__ */
2846 VISIT(c, expr, s->v.AsyncFor.target);
2847 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002848 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002849
2850 compiler_pop_fblock(c, FOR_LOOP, start);
Yury Selivanov75445082015-05-11 22:57:16 -04002851
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002852 /* Except block for __anext__ */
Yury Selivanov75445082015-05-11 22:57:16 -04002853 compiler_use_next_block(c, except);
Mark Shannon877df852020-11-12 09:43:29 +00002854
2855 c->u->u_lineno = -1;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002856 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov75445082015-05-11 22:57:16 -04002857
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002858 /* `else` block */
Yury Selivanov75445082015-05-11 22:57:16 -04002859 VISIT_SEQ(c, stmt, s->v.For.orelse);
2860
2861 compiler_use_next_block(c, end);
2862
2863 return 1;
2864}
2865
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002866static int
2867compiler_while(struct compiler *c, stmt_ty s)
2868{
Mark Shannon266b4622020-11-17 19:30:14 +00002869 basicblock *loop, *body, *end, *anchor = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 loop = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002871 body = compiler_new_block(c);
2872 anchor = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 end = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002874 if (loop == NULL || body == NULL || anchor == NULL || end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 compiler_use_next_block(c, loop);
Mark Shannon266b4622020-11-17 19:30:14 +00002878 if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002881 if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) {
2882 return 0;
Mark Shannon266b4622020-11-17 19:30:14 +00002883 }
2884
2885 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 VISIT_SEQ(c, stmt, s->v.While.body);
Mark Shannon8473cf82020-12-15 11:07:50 +00002887 SET_LOC(c, s);
2888 if (!compiler_jump_if(c, s->v.While.test, body, 1)) {
2889 return 0;
2890 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002891
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002892 compiler_pop_fblock(c, WHILE_LOOP, loop);
2893
Mark Shannon266b4622020-11-17 19:30:14 +00002894 compiler_use_next_block(c, anchor);
2895 if (s->v.While.orelse) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002896 VISIT_SEQ(c, stmt, s->v.While.orelse);
Mark Shannon266b4622020-11-17 19:30:14 +00002897 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002898 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002901}
2902
2903static int
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002904compiler_return(struct compiler *c, stmt_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002905{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002906 int preserve_tos = ((s->v.Return.value != NULL) &&
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002907 (s->v.Return.value->kind != Constant_kind));
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002908 if (c->u->u_ste->ste_type != FunctionBlock)
2909 return compiler_error(c, "'return' outside function");
2910 if (s->v.Return.value != NULL &&
2911 c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2912 {
2913 return compiler_error(
2914 c, "'return' with value in async generator");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002916 if (preserve_tos) {
2917 VISIT(c, expr, s->v.Return.value);
Mark Shannon5274b682020-12-16 13:07:01 +00002918 } else {
2919 /* Emit instruction with line number for expression */
2920 if (s->v.Return.value != NULL) {
2921 SET_LOC(c, s->v.Return.value);
2922 ADDOP(c, NOP);
2923 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002924 }
Mark Shannonfee55262019-11-21 09:11:43 +00002925 if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL))
2926 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002927 if (s->v.Return.value == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002928 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002929 }
2930 else if (!preserve_tos) {
Mark Shannon5274b682020-12-16 13:07:01 +00002931 ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002932 }
2933 ADDOP(c, RETURN_VALUE);
Mark Shannon266b4622020-11-17 19:30:14 +00002934 NEXT_BLOCK(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002937}
2938
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002939static int
2940compiler_break(struct compiler *c)
2941{
Mark Shannonfee55262019-11-21 09:11:43 +00002942 struct fblockinfo *loop = NULL;
2943 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
2944 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002945 }
Mark Shannonfee55262019-11-21 09:11:43 +00002946 if (loop == NULL) {
2947 return compiler_error(c, "'break' outside loop");
2948 }
2949 if (!compiler_unwind_fblock(c, loop, 0)) {
2950 return 0;
2951 }
Mark Shannon582aaf12020-08-04 17:30:11 +01002952 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit);
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
2957static int
2958compiler_continue(struct compiler *c)
2959{
Mark Shannonfee55262019-11-21 09:11:43 +00002960 struct fblockinfo *loop = NULL;
2961 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
2962 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002963 }
Mark Shannonfee55262019-11-21 09:11:43 +00002964 if (loop == NULL) {
2965 return compiler_error(c, "'continue' not properly in loop");
2966 }
Mark Shannon582aaf12020-08-04 17:30:11 +01002967 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block);
Mark Shannon266b4622020-11-17 19:30:14 +00002968 NEXT_BLOCK(c)
Mark Shannonfee55262019-11-21 09:11:43 +00002969 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002970}
2971
2972
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002973/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002974
2975 SETUP_FINALLY L
2976 <code for body>
2977 POP_BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00002978 <code for finalbody>
2979 JUMP E
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002980 L:
2981 <code for finalbody>
Mark Shannonfee55262019-11-21 09:11:43 +00002982 E:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002983
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002984 The special instructions use the block stack. Each block
2985 stack entry contains the instruction that created it (here
2986 SETUP_FINALLY), the level of the value stack at the time the
2987 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002988
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002989 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 Pushes the current value stack level and the label
2991 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002992 POP_BLOCK:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002993 Pops en entry from the block stack.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002995 The block stack is unwound when an exception is raised:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002996 when a SETUP_FINALLY entry is found, the raised and the caught
2997 exceptions are pushed onto the value stack (and the exception
2998 condition is cleared), and the interpreter jumps to the label
2999 gotten from the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003000*/
3001
3002static int
3003compiler_try_finally(struct compiler *c, stmt_ty s)
3004{
Mark Shannonfee55262019-11-21 09:11:43 +00003005 basicblock *body, *end, *exit;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003007 body = compiler_new_block(c);
3008 end = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00003009 exit = compiler_new_block(c);
3010 if (body == NULL || end == NULL || exit == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003011 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003012
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003013 /* `try` block */
Mark Shannon582aaf12020-08-04 17:30:11 +01003014 ADDOP_JUMP(c, SETUP_FINALLY, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003015 compiler_use_next_block(c, body);
Mark Shannonfee55262019-11-21 09:11:43 +00003016 if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003018 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
3019 if (!compiler_try_except(c, s))
3020 return 0;
3021 }
3022 else {
3023 VISIT_SEQ(c, stmt, s->v.Try.body);
3024 }
Mark Shannon3bd60352021-01-13 12:05:43 +00003025 ADDOP_NOLINE(c, POP_BLOCK);
Mark Shannonfee55262019-11-21 09:11:43 +00003026 compiler_pop_fblock(c, FINALLY_TRY, body);
3027 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Mark Shannon127dde52021-01-04 18:06:55 +00003028 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00003029 /* `finally` block */
3030 compiler_use_next_block(c, end);
3031 if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL))
3032 return 0;
3033 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3034 compiler_pop_fblock(c, FINALLY_END, end);
Mark Shannonbf353f32020-12-17 13:55:28 +00003035 ADDOP_I(c, RERAISE, 0);
Mark Shannonfee55262019-11-21 09:11:43 +00003036 compiler_use_next_block(c, exit);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003038}
3039
3040/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003041 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003042 (The contents of the value stack is shown in [], with the top
3043 at the right; 'tb' is trace-back info, 'val' the exception's
3044 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003045
3046 Value stack Label Instruction Argument
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003047 [] SETUP_FINALLY L1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048 [] <code for S>
3049 [] POP_BLOCK
3050 [] JUMP_FORWARD L0
3051
3052 [tb, val, exc] L1: DUP )
3053 [tb, val, exc, exc] <evaluate E1> )
Mark Shannon9af0e472020-01-14 10:12:45 +00003054 [tb, val, exc, exc, E1] JUMP_IF_NOT_EXC_MATCH L2 ) only if E1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 [tb, val, exc] POP
3056 [tb, val] <assign to V1> (or POP if no V1)
3057 [tb] POP
3058 [] <code for S1>
3059 JUMP_FORWARD L0
3060
3061 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003062 .............................etc.......................
3063
Mark Shannonfee55262019-11-21 09:11:43 +00003064 [tb, val, exc] Ln+1: RERAISE # re-raise exception
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065
3066 [] L0: <next statement>
3067
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003068 Of course, parts are not generated if Vi or Ei is not present.
3069*/
3070static int
3071compiler_try_except(struct compiler *c, stmt_ty s)
3072{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003074 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003076 body = compiler_new_block(c);
3077 except = compiler_new_block(c);
3078 orelse = compiler_new_block(c);
3079 end = compiler_new_block(c);
3080 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
3081 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01003082 ADDOP_JUMP(c, SETUP_FINALLY, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083 compiler_use_next_block(c, body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003084 if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003085 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003086 VISIT_SEQ(c, stmt, s->v.Try.body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003087 compiler_pop_fblock(c, TRY_EXCEPT, body);
Mark Shannon3bd60352021-01-13 12:05:43 +00003088 ADDOP_NOLINE(c, POP_BLOCK);
3089 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003090 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003091 compiler_use_next_block(c, except);
Mark Shannon02d126a2020-09-25 14:04:19 +01003092 /* Runtime will push a block here, so we need to account for that */
3093 if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL))
3094 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003095 for (i = 0; i < n; i++) {
3096 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003097 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003098 if (!handler->v.ExceptHandler.type && i < n-1)
3099 return compiler_error(c, "default 'except:' must be last");
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003100 SET_LOC(c, handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003101 except = compiler_new_block(c);
3102 if (except == NULL)
3103 return 0;
3104 if (handler->v.ExceptHandler.type) {
3105 ADDOP(c, DUP_TOP);
3106 VISIT(c, expr, handler->v.ExceptHandler.type);
Mark Shannon582aaf12020-08-04 17:30:11 +01003107 ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except);
Mark Shannon266b4622020-11-17 19:30:14 +00003108 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003109 }
3110 ADDOP(c, POP_TOP);
3111 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003112 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00003113
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003114 cleanup_end = compiler_new_block(c);
3115 cleanup_body = compiler_new_block(c);
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003116 if (cleanup_end == NULL || cleanup_body == NULL) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003117 return 0;
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003118 }
Guido van Rossumb940e112007-01-10 16:19:56 +00003119
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003120 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3121 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003123 /*
3124 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003125 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003126 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003127 try:
3128 # body
3129 finally:
Chris Angelicoad098b62019-05-21 23:34:19 +10003130 name = None # in case body contains "del name"
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003131 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003132 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003133
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003134 /* second try: */
Mark Shannon582aaf12020-08-04 17:30:11 +01003135 ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003136 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003137 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003138 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003140 /* second # body */
3141 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003142 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003143 ADDOP(c, POP_BLOCK);
3144 ADDOP(c, POP_EXCEPT);
Mark Shannon877df852020-11-12 09:43:29 +00003145 /* name = None; del name; # Mark as artificial */
3146 c->u->u_lineno = -1;
Mark Shannonfee55262019-11-21 09:11:43 +00003147 ADDOP_LOAD_CONST(c, Py_None);
3148 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3149 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Mark Shannon582aaf12020-08-04 17:30:11 +01003150 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151
Mark Shannonfee55262019-11-21 09:11:43 +00003152 /* except: */
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003153 compiler_use_next_block(c, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154
Mark Shannon877df852020-11-12 09:43:29 +00003155 /* name = None; del name; # Mark as artificial */
3156 c->u->u_lineno = -1;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003157 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003158 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003159 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003160
Mark Shannonbf353f32020-12-17 13:55:28 +00003161 ADDOP_I(c, RERAISE, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003162 }
3163 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003164 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003165
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003166 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05003167 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003168 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003169
Guido van Rossumb940e112007-01-10 16:19:56 +00003170 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003171 ADDOP(c, POP_TOP);
3172 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003173 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003174 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003175 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003176 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannon127dde52021-01-04 18:06:55 +00003177 /* name = None; del name; # Mark as artificial */
3178 c->u->u_lineno = -1;
Mark Shannonfee55262019-11-21 09:11:43 +00003179 ADDOP(c, POP_EXCEPT);
Mark Shannon582aaf12020-08-04 17:30:11 +01003180 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003182 compiler_use_next_block(c, except);
3183 }
Mark Shannon02d126a2020-09-25 14:04:19 +01003184 compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL);
Mark Shannonf2dbfd72020-12-21 13:53:50 +00003185 /* Mark as artificial */
3186 c->u->u_lineno = -1;
Mark Shannonbf353f32020-12-17 13:55:28 +00003187 ADDOP_I(c, RERAISE, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003189 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190 compiler_use_next_block(c, end);
3191 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003192}
3193
3194static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003195compiler_try(struct compiler *c, stmt_ty s) {
3196 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
3197 return compiler_try_finally(c, s);
3198 else
3199 return compiler_try_except(c, s);
3200}
3201
3202
3203static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003204compiler_import_as(struct compiler *c, identifier name, identifier asname)
3205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003206 /* The IMPORT_NAME opcode was already generated. This function
3207 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003209 If there is a dot in name, we need to split it and emit a
Serhiy Storchakaf93234b2017-05-09 22:31:05 +03003210 IMPORT_FROM for each name.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003212 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
3213 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003214 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003215 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003216 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217 /* Consume the base module name to get the first attribute */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003218 while (1) {
3219 Py_ssize_t pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003220 PyObject *attr;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003221 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003222 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003223 return 0;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003224 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 if (!attr)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003226 return 0;
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003227 ADDOP_N(c, IMPORT_FROM, attr, names);
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003228 if (dot == -1) {
3229 break;
3230 }
3231 ADDOP(c, ROT_TWO);
3232 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003233 }
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003234 if (!compiler_nameop(c, asname, Store)) {
3235 return 0;
3236 }
3237 ADDOP(c, POP_TOP);
3238 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003239 }
3240 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003241}
3242
3243static int
3244compiler_import(struct compiler *c, stmt_ty s)
3245{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003246 /* The Import node stores a module name like a.b.c as a single
3247 string. This is convenient for all cases except
3248 import a.b.c as d
3249 where we need to parse that string to extract the individual
3250 module names.
3251 XXX Perhaps change the representation to make this case simpler?
3252 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01003253 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003254
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003255 PyObject *zero = _PyLong_GetZero(); // borrowed reference
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003256 for (i = 0; i < n; i++) {
3257 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
3258 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003259
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003260 ADDOP_LOAD_CONST(c, zero);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003261 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003262 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003264 if (alias->asname) {
3265 r = compiler_import_as(c, alias->name, alias->asname);
3266 if (!r)
3267 return r;
3268 }
3269 else {
3270 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003271 Py_ssize_t dot = PyUnicode_FindChar(
3272 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02003273 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003274 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02003275 if (tmp == NULL)
3276 return 0;
3277 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003278 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003279 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003280 Py_DECREF(tmp);
3281 }
3282 if (!r)
3283 return r;
3284 }
3285 }
3286 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003287}
3288
3289static int
3290compiler_from_import(struct compiler *c, stmt_ty s)
3291{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003292 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003293 PyObject *names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003294 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003296 if (!empty_string) {
3297 empty_string = PyUnicode_FromString("");
3298 if (!empty_string)
3299 return 0;
3300 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003301
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003302 ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02003303
3304 names = PyTuple_New(n);
3305 if (!names)
3306 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003308 /* build up the names */
3309 for (i = 0; i < n; i++) {
3310 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3311 Py_INCREF(alias->name);
3312 PyTuple_SET_ITEM(names, i, alias->name);
3313 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003316 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003317 Py_DECREF(names);
3318 return compiler_error(c, "from __future__ imports must occur "
3319 "at the beginning of the file");
3320 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003321 ADDOP_LOAD_CONST_NEW(c, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003323 if (s->v.ImportFrom.module) {
3324 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
3325 }
3326 else {
3327 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
3328 }
3329 for (i = 0; i < n; i++) {
3330 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3331 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003332
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003333 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003334 assert(n == 1);
3335 ADDOP(c, IMPORT_STAR);
3336 return 1;
3337 }
3338
3339 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
3340 store_name = alias->name;
3341 if (alias->asname)
3342 store_name = alias->asname;
3343
3344 if (!compiler_nameop(c, store_name, Store)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003345 return 0;
3346 }
3347 }
3348 /* remove imported module */
3349 ADDOP(c, POP_TOP);
3350 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003351}
3352
3353static int
3354compiler_assert(struct compiler *c, stmt_ty s)
3355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003356 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003357
tsukasa-aua8ef4572021-03-16 22:14:41 +11003358 /* Always emit a warning if the test is a non-zero length tuple */
3359 if ((s->v.Assert.test->kind == Tuple_kind &&
3360 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) ||
3361 (s->v.Assert.test->kind == Constant_kind &&
3362 PyTuple_Check(s->v.Assert.test->v.Constant.value) &&
3363 PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0))
Serhiy Storchakad31e7732018-10-21 10:09:39 +03003364 {
3365 if (!compiler_warn(c, "assertion is always true, "
3366 "perhaps remove parentheses?"))
3367 {
Victor Stinner14e461d2013-08-26 22:28:21 +02003368 return 0;
3369 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003370 }
tsukasa-aua8ef4572021-03-16 22:14:41 +11003371 if (c->c_optimize)
3372 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003373 end = compiler_new_block(c);
3374 if (end == NULL)
3375 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003376 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
3377 return 0;
Zackery Spytzce6a0702019-08-25 03:44:09 -06003378 ADDOP(c, LOAD_ASSERTION_ERROR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003379 if (s->v.Assert.msg) {
3380 VISIT(c, expr, s->v.Assert.msg);
3381 ADDOP_I(c, CALL_FUNCTION, 1);
3382 }
3383 ADDOP_I(c, RAISE_VARARGS, 1);
3384 compiler_use_next_block(c, end);
3385 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003386}
3387
3388static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003389compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3390{
3391 if (c->c_interactive && c->c_nestlevel <= 1) {
3392 VISIT(c, expr, value);
3393 ADDOP(c, PRINT_EXPR);
3394 return 1;
3395 }
3396
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003397 if (value->kind == Constant_kind) {
Victor Stinner15a30952016-02-08 22:45:06 +01003398 /* ignore constant statement */
Mark Shannon877df852020-11-12 09:43:29 +00003399 ADDOP(c, NOP);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003400 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003401 }
3402
3403 VISIT(c, expr, value);
Mark Shannonc5440932021-03-15 14:24:25 +00003404 /* Mark POP_TOP as artificial */
3405 c->u->u_lineno = -1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003406 ADDOP(c, POP_TOP);
3407 return 1;
3408}
3409
3410static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003411compiler_visit_stmt(struct compiler *c, stmt_ty s)
3412{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003413 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003415 /* Always assign a lineno to the next instruction for a stmt. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003416 SET_LOC(c, s);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003418 switch (s->kind) {
3419 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04003420 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003421 case ClassDef_kind:
3422 return compiler_class(c, s);
3423 case Return_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003424 return compiler_return(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 case Delete_kind:
3426 VISIT_SEQ(c, expr, s->v.Delete.targets)
3427 break;
3428 case Assign_kind:
3429 n = asdl_seq_LEN(s->v.Assign.targets);
3430 VISIT(c, expr, s->v.Assign.value);
3431 for (i = 0; i < n; i++) {
3432 if (i < n - 1)
3433 ADDOP(c, DUP_TOP);
3434 VISIT(c, expr,
3435 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3436 }
3437 break;
3438 case AugAssign_kind:
3439 return compiler_augassign(c, s);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003440 case AnnAssign_kind:
3441 return compiler_annassign(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003442 case For_kind:
3443 return compiler_for(c, s);
3444 case While_kind:
3445 return compiler_while(c, s);
3446 case If_kind:
3447 return compiler_if(c, s);
Brandt Bucher145bf262021-02-26 14:51:55 -08003448 case Match_kind:
3449 return compiler_match(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 case Raise_kind:
3451 n = 0;
3452 if (s->v.Raise.exc) {
3453 VISIT(c, expr, s->v.Raise.exc);
3454 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003455 if (s->v.Raise.cause) {
3456 VISIT(c, expr, s->v.Raise.cause);
3457 n++;
3458 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003459 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01003460 ADDOP_I(c, RAISE_VARARGS, (int)n);
Mark Shannon266b4622020-11-17 19:30:14 +00003461 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003462 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003463 case Try_kind:
3464 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003465 case Assert_kind:
3466 return compiler_assert(c, s);
3467 case Import_kind:
3468 return compiler_import(c, s);
3469 case ImportFrom_kind:
3470 return compiler_from_import(c, s);
3471 case Global_kind:
3472 case Nonlocal_kind:
3473 break;
3474 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003475 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003476 case Pass_kind:
Mark Shannon877df852020-11-12 09:43:29 +00003477 ADDOP(c, NOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003478 break;
3479 case Break_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003480 return compiler_break(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 case Continue_kind:
3482 return compiler_continue(c);
3483 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003484 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04003485 case AsyncFunctionDef_kind:
3486 return compiler_function(c, s, 1);
3487 case AsyncWith_kind:
3488 return compiler_async_with(c, s, 0);
3489 case AsyncFor_kind:
3490 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003491 }
Yury Selivanov75445082015-05-11 22:57:16 -04003492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003493 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003494}
3495
3496static int
3497unaryop(unaryop_ty op)
3498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003499 switch (op) {
3500 case Invert:
3501 return UNARY_INVERT;
3502 case Not:
3503 return UNARY_NOT;
3504 case UAdd:
3505 return UNARY_POSITIVE;
3506 case USub:
3507 return UNARY_NEGATIVE;
3508 default:
3509 PyErr_Format(PyExc_SystemError,
3510 "unary op %d should not be possible", op);
3511 return 0;
3512 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003513}
3514
3515static int
Andy Lester76d58772020-03-10 21:18:12 -05003516binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003517{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003518 switch (op) {
3519 case Add:
3520 return BINARY_ADD;
3521 case Sub:
3522 return BINARY_SUBTRACT;
3523 case Mult:
3524 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003525 case MatMult:
3526 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003527 case Div:
3528 return BINARY_TRUE_DIVIDE;
3529 case Mod:
3530 return BINARY_MODULO;
3531 case Pow:
3532 return BINARY_POWER;
3533 case LShift:
3534 return BINARY_LSHIFT;
3535 case RShift:
3536 return BINARY_RSHIFT;
3537 case BitOr:
3538 return BINARY_OR;
3539 case BitXor:
3540 return BINARY_XOR;
3541 case BitAnd:
3542 return BINARY_AND;
3543 case FloorDiv:
3544 return BINARY_FLOOR_DIVIDE;
3545 default:
3546 PyErr_Format(PyExc_SystemError,
3547 "binary op %d should not be possible", op);
3548 return 0;
3549 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003550}
3551
3552static int
Andy Lester76d58772020-03-10 21:18:12 -05003553inplace_binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003555 switch (op) {
3556 case Add:
3557 return INPLACE_ADD;
3558 case Sub:
3559 return INPLACE_SUBTRACT;
3560 case Mult:
3561 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003562 case MatMult:
3563 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003564 case Div:
3565 return INPLACE_TRUE_DIVIDE;
3566 case Mod:
3567 return INPLACE_MODULO;
3568 case Pow:
3569 return INPLACE_POWER;
3570 case LShift:
3571 return INPLACE_LSHIFT;
3572 case RShift:
3573 return INPLACE_RSHIFT;
3574 case BitOr:
3575 return INPLACE_OR;
3576 case BitXor:
3577 return INPLACE_XOR;
3578 case BitAnd:
3579 return INPLACE_AND;
3580 case FloorDiv:
3581 return INPLACE_FLOOR_DIVIDE;
3582 default:
3583 PyErr_Format(PyExc_SystemError,
3584 "inplace binary op %d should not be possible", op);
3585 return 0;
3586 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003587}
3588
3589static int
3590compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3591{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003592 int op, scope;
3593 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003594 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003596 PyObject *dict = c->u->u_names;
3597 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003598
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003599 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3600 !_PyUnicode_EqualToASCIIString(name, "True") &&
3601 !_PyUnicode_EqualToASCIIString(name, "False"));
Benjamin Peterson70b224d2012-12-06 17:49:58 -05003602
Pablo Galindoc5fc1562020-04-22 23:29:27 +01003603 if (forbidden_name(c, name, ctx))
3604 return 0;
3605
Serhiy Storchakabd6ec4d2017-12-18 14:29:12 +02003606 mangled = _Py_Mangle(c->u->u_private, name);
3607 if (!mangled)
3608 return 0;
3609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 op = 0;
3611 optype = OP_NAME;
3612 scope = PyST_GetScope(c->u->u_ste, mangled);
3613 switch (scope) {
3614 case FREE:
3615 dict = c->u->u_freevars;
3616 optype = OP_DEREF;
3617 break;
3618 case CELL:
3619 dict = c->u->u_cellvars;
3620 optype = OP_DEREF;
3621 break;
3622 case LOCAL:
3623 if (c->u->u_ste->ste_type == FunctionBlock)
3624 optype = OP_FAST;
3625 break;
3626 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04003627 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003628 optype = OP_GLOBAL;
3629 break;
3630 case GLOBAL_EXPLICIT:
3631 optype = OP_GLOBAL;
3632 break;
3633 default:
3634 /* scope can be 0 */
3635 break;
3636 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003638 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003639 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003641 switch (optype) {
3642 case OP_DEREF:
3643 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003644 case Load:
3645 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3646 break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003647 case Store: op = STORE_DEREF; break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003648 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003649 }
3650 break;
3651 case OP_FAST:
3652 switch (ctx) {
3653 case Load: op = LOAD_FAST; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003654 case Store: op = STORE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003655 case Del: op = DELETE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003656 }
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003657 ADDOP_N(c, op, mangled, varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003658 return 1;
3659 case OP_GLOBAL:
3660 switch (ctx) {
3661 case Load: op = LOAD_GLOBAL; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003662 case Store: op = STORE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003663 case Del: op = DELETE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003664 }
3665 break;
3666 case OP_NAME:
3667 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00003668 case Load: op = LOAD_NAME; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003669 case Store: op = STORE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003670 case Del: op = DELETE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003671 }
3672 break;
3673 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 assert(op);
Andy Lester76d58772020-03-10 21:18:12 -05003676 arg = compiler_add_o(dict, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003677 Py_DECREF(mangled);
3678 if (arg < 0)
3679 return 0;
3680 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003681}
3682
3683static int
3684compiler_boolop(struct compiler *c, expr_ty e)
3685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003686 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003687 int jumpi;
3688 Py_ssize_t i, n;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003689 asdl_expr_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003691 assert(e->kind == BoolOp_kind);
3692 if (e->v.BoolOp.op == And)
3693 jumpi = JUMP_IF_FALSE_OR_POP;
3694 else
3695 jumpi = JUMP_IF_TRUE_OR_POP;
3696 end = compiler_new_block(c);
3697 if (end == NULL)
3698 return 0;
3699 s = e->v.BoolOp.values;
3700 n = asdl_seq_LEN(s) - 1;
3701 assert(n >= 0);
3702 for (i = 0; i < n; ++i) {
3703 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003704 ADDOP_JUMP(c, jumpi, end);
Mark Shannon6e8128f2020-07-30 10:03:00 +01003705 basicblock *next = compiler_new_block(c);
3706 if (next == NULL) {
3707 return 0;
3708 }
3709 compiler_use_next_block(c, next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003710 }
3711 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3712 compiler_use_next_block(c, end);
3713 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003714}
3715
3716static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003717starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
Mark Shannon13bc1392020-01-23 09:25:17 +00003718 int build, int add, int extend, int tuple)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003719{
3720 Py_ssize_t n = asdl_seq_LEN(elts);
Mark Shannon13bc1392020-01-23 09:25:17 +00003721 Py_ssize_t i, seen_star = 0;
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003722 if (n > 2 && are_all_items_const(elts, 0, n)) {
3723 PyObject *folded = PyTuple_New(n);
3724 if (folded == NULL) {
3725 return 0;
3726 }
3727 PyObject *val;
3728 for (i = 0; i < n; i++) {
3729 val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
3730 Py_INCREF(val);
3731 PyTuple_SET_ITEM(folded, i, val);
3732 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003733 if (tuple) {
3734 ADDOP_LOAD_CONST_NEW(c, folded);
3735 } else {
3736 if (add == SET_ADD) {
3737 Py_SETREF(folded, PyFrozenSet_New(folded));
3738 if (folded == NULL) {
3739 return 0;
3740 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003741 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003742 ADDOP_I(c, build, pushed);
3743 ADDOP_LOAD_CONST_NEW(c, folded);
3744 ADDOP_I(c, extend, 1);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003745 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003746 return 1;
3747 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003748
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003749 for (i = 0; i < n; i++) {
3750 expr_ty elt = asdl_seq_GET(elts, i);
3751 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003752 seen_star = 1;
3753 }
3754 }
3755 if (seen_star) {
3756 seen_star = 0;
3757 for (i = 0; i < n; i++) {
3758 expr_ty elt = asdl_seq_GET(elts, i);
3759 if (elt->kind == Starred_kind) {
3760 if (seen_star == 0) {
3761 ADDOP_I(c, build, i+pushed);
3762 seen_star = 1;
3763 }
3764 VISIT(c, expr, elt->v.Starred.value);
3765 ADDOP_I(c, extend, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003766 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003767 else {
3768 VISIT(c, expr, elt);
3769 if (seen_star) {
3770 ADDOP_I(c, add, 1);
3771 }
3772 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003773 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003774 assert(seen_star);
3775 if (tuple) {
3776 ADDOP(c, LIST_TO_TUPLE);
3777 }
3778 }
3779 else {
3780 for (i = 0; i < n; i++) {
3781 expr_ty elt = asdl_seq_GET(elts, i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003782 VISIT(c, expr, elt);
Mark Shannon13bc1392020-01-23 09:25:17 +00003783 }
3784 if (tuple) {
3785 ADDOP_I(c, BUILD_TUPLE, n+pushed);
3786 } else {
3787 ADDOP_I(c, build, n+pushed);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003788 }
3789 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003790 return 1;
3791}
3792
3793static int
Brandt Bucher145bf262021-02-26 14:51:55 -08003794unpack_helper(struct compiler *c, asdl_expr_seq *elts)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003795{
3796 Py_ssize_t n = asdl_seq_LEN(elts);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003797 int seen_star = 0;
Brandt Bucher145bf262021-02-26 14:51:55 -08003798 for (Py_ssize_t i = 0; i < n; i++) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003799 expr_ty elt = asdl_seq_GET(elts, i);
3800 if (elt->kind == Starred_kind && !seen_star) {
3801 if ((i >= (1 << 8)) ||
3802 (n-i-1 >= (INT_MAX >> 8)))
3803 return compiler_error(c,
3804 "too many expressions in "
3805 "star-unpacking assignment");
3806 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3807 seen_star = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003808 }
3809 else if (elt->kind == Starred_kind) {
3810 return compiler_error(c,
Furkan Öndercb6534e2020-03-26 04:54:31 +03003811 "multiple starred expressions in assignment");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003812 }
3813 }
3814 if (!seen_star) {
3815 ADDOP_I(c, UNPACK_SEQUENCE, n);
3816 }
Brandt Bucher145bf262021-02-26 14:51:55 -08003817 return 1;
3818}
3819
3820static int
3821assignment_helper(struct compiler *c, asdl_expr_seq *elts)
3822{
3823 Py_ssize_t n = asdl_seq_LEN(elts);
3824 RETURN_IF_FALSE(unpack_helper(c, elts));
3825 for (Py_ssize_t i = 0; i < n; i++) {
Brandt Bucherd5aa2e92020-03-07 19:44:18 -08003826 expr_ty elt = asdl_seq_GET(elts, i);
3827 VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value);
3828 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003829 return 1;
3830}
3831
3832static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003833compiler_list(struct compiler *c, expr_ty e)
3834{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003835 asdl_expr_seq *elts = e->v.List.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003836 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003837 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003838 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003839 else if (e->v.List.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003840 return starunpack_helper(c, elts, 0, BUILD_LIST,
3841 LIST_APPEND, LIST_EXTEND, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003842 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003843 else
3844 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003845 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003846}
3847
3848static int
3849compiler_tuple(struct compiler *c, expr_ty e)
3850{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003851 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003852 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003853 return assignment_helper(c, elts);
3854 }
3855 else if (e->v.Tuple.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003856 return starunpack_helper(c, elts, 0, BUILD_LIST,
3857 LIST_APPEND, LIST_EXTEND, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003858 }
3859 else
3860 VISIT_SEQ(c, expr, elts);
3861 return 1;
3862}
3863
3864static int
3865compiler_set(struct compiler *c, expr_ty e)
3866{
Mark Shannon13bc1392020-01-23 09:25:17 +00003867 return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET,
3868 SET_ADD, SET_UPDATE, 0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003869}
3870
3871static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003872are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003873{
3874 Py_ssize_t i;
3875 for (i = begin; i < end; i++) {
3876 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003877 if (key == NULL || key->kind != Constant_kind)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003878 return 0;
3879 }
3880 return 1;
3881}
3882
3883static int
3884compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3885{
3886 Py_ssize_t i, n = end - begin;
3887 PyObject *keys, *key;
3888 if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) {
3889 for (i = begin; i < end; i++) {
3890 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3891 }
3892 keys = PyTuple_New(n);
3893 if (keys == NULL) {
3894 return 0;
3895 }
3896 for (i = begin; i < end; i++) {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003897 key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003898 Py_INCREF(key);
3899 PyTuple_SET_ITEM(keys, i - begin, key);
3900 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003901 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003902 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3903 }
3904 else {
3905 for (i = begin; i < end; i++) {
3906 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3907 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3908 }
3909 ADDOP_I(c, BUILD_MAP, n);
3910 }
3911 return 1;
3912}
3913
3914static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003915compiler_dict(struct compiler *c, expr_ty e)
3916{
Victor Stinner976bb402016-03-23 11:36:19 +01003917 Py_ssize_t i, n, elements;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003918 int have_dict;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003919 int is_unpacking = 0;
3920 n = asdl_seq_LEN(e->v.Dict.values);
Mark Shannon8a4cd702020-01-27 09:57:45 +00003921 have_dict = 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003922 elements = 0;
3923 for (i = 0; i < n; i++) {
3924 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003925 if (is_unpacking) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003926 if (elements) {
3927 if (!compiler_subdict(c, e, i - elements, i)) {
3928 return 0;
3929 }
3930 if (have_dict) {
3931 ADDOP_I(c, DICT_UPDATE, 1);
3932 }
3933 have_dict = 1;
3934 elements = 0;
3935 }
3936 if (have_dict == 0) {
3937 ADDOP_I(c, BUILD_MAP, 0);
3938 have_dict = 1;
3939 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003940 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Mark Shannon8a4cd702020-01-27 09:57:45 +00003941 ADDOP_I(c, DICT_UPDATE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003942 }
3943 else {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003944 if (elements == 0xFFFF) {
Pablo Galindoc51db0e2020-08-13 09:48:41 +01003945 if (!compiler_subdict(c, e, i - elements, i + 1)) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003946 return 0;
3947 }
3948 if (have_dict) {
3949 ADDOP_I(c, DICT_UPDATE, 1);
3950 }
3951 have_dict = 1;
3952 elements = 0;
3953 }
3954 else {
3955 elements++;
3956 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003957 }
3958 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003959 if (elements) {
3960 if (!compiler_subdict(c, e, n - elements, n)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003961 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003962 }
3963 if (have_dict) {
3964 ADDOP_I(c, DICT_UPDATE, 1);
3965 }
3966 have_dict = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003967 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003968 if (!have_dict) {
3969 ADDOP_I(c, BUILD_MAP, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003970 }
3971 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003972}
3973
3974static int
3975compiler_compare(struct compiler *c, expr_ty e)
3976{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003977 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003978
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02003979 if (!check_compare(c, e)) {
3980 return 0;
3981 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003982 VISIT(c, expr, e->v.Compare.left);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003983 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
3984 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
3985 if (n == 0) {
3986 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Mark Shannon9af0e472020-01-14 10:12:45 +00003987 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003988 }
3989 else {
3990 basicblock *cleanup = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003991 if (cleanup == NULL)
3992 return 0;
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003993 for (i = 0; i < n; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003994 VISIT(c, expr,
3995 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003996 ADDOP(c, DUP_TOP);
3997 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00003998 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003999 ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004000 NEXT_BLOCK(c);
4001 }
4002 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00004003 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004004 basicblock *end = compiler_new_block(c);
4005 if (end == NULL)
4006 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00004007 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004008 compiler_use_next_block(c, cleanup);
4009 ADDOP(c, ROT_TWO);
4010 ADDOP(c, POP_TOP);
4011 compiler_use_next_block(c, end);
4012 }
4013 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004014}
4015
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004016static PyTypeObject *
4017infer_type(expr_ty e)
4018{
4019 switch (e->kind) {
4020 case Tuple_kind:
4021 return &PyTuple_Type;
4022 case List_kind:
4023 case ListComp_kind:
4024 return &PyList_Type;
4025 case Dict_kind:
4026 case DictComp_kind:
4027 return &PyDict_Type;
4028 case Set_kind:
4029 case SetComp_kind:
4030 return &PySet_Type;
4031 case GeneratorExp_kind:
4032 return &PyGen_Type;
4033 case Lambda_kind:
4034 return &PyFunction_Type;
4035 case JoinedStr_kind:
4036 case FormattedValue_kind:
4037 return &PyUnicode_Type;
4038 case Constant_kind:
Victor Stinnera102ed72020-02-07 02:24:48 +01004039 return Py_TYPE(e->v.Constant.value);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004040 default:
4041 return NULL;
4042 }
4043}
4044
4045static int
4046check_caller(struct compiler *c, expr_ty e)
4047{
4048 switch (e->kind) {
4049 case Constant_kind:
4050 case Tuple_kind:
4051 case List_kind:
4052 case ListComp_kind:
4053 case Dict_kind:
4054 case DictComp_kind:
4055 case Set_kind:
4056 case SetComp_kind:
4057 case GeneratorExp_kind:
4058 case JoinedStr_kind:
4059 case FormattedValue_kind:
4060 return compiler_warn(c, "'%.200s' object is not callable; "
4061 "perhaps you missed a comma?",
4062 infer_type(e)->tp_name);
4063 default:
4064 return 1;
4065 }
4066}
4067
4068static int
4069check_subscripter(struct compiler *c, expr_ty e)
4070{
4071 PyObject *v;
4072
4073 switch (e->kind) {
4074 case Constant_kind:
4075 v = e->v.Constant.value;
4076 if (!(v == Py_None || v == Py_Ellipsis ||
4077 PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
4078 PyAnySet_Check(v)))
4079 {
4080 return 1;
4081 }
4082 /* fall through */
4083 case Set_kind:
4084 case SetComp_kind:
4085 case GeneratorExp_kind:
4086 case Lambda_kind:
4087 return compiler_warn(c, "'%.200s' object is not subscriptable; "
4088 "perhaps you missed a comma?",
4089 infer_type(e)->tp_name);
4090 default:
4091 return 1;
4092 }
4093}
4094
4095static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004096check_index(struct compiler *c, expr_ty e, expr_ty s)
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004097{
4098 PyObject *v;
4099
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004100 PyTypeObject *index_type = infer_type(s);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004101 if (index_type == NULL
4102 || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
4103 || index_type == &PySlice_Type) {
4104 return 1;
4105 }
4106
4107 switch (e->kind) {
4108 case Constant_kind:
4109 v = e->v.Constant.value;
4110 if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
4111 return 1;
4112 }
4113 /* fall through */
4114 case Tuple_kind:
4115 case List_kind:
4116 case ListComp_kind:
4117 case JoinedStr_kind:
4118 case FormattedValue_kind:
4119 return compiler_warn(c, "%.200s indices must be integers or slices, "
4120 "not %.200s; "
4121 "perhaps you missed a comma?",
4122 infer_type(e)->tp_name,
4123 index_type->tp_name);
4124 default:
4125 return 1;
4126 }
4127}
4128
Zackery Spytz97f5de02019-03-22 01:30:32 -06004129// Return 1 if the method call was optimized, -1 if not, and 0 on error.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004130static int
Yury Selivanovf2392132016-12-13 19:03:51 -05004131maybe_optimize_method_call(struct compiler *c, expr_ty e)
4132{
4133 Py_ssize_t argsl, i;
4134 expr_ty meth = e->v.Call.func;
Pablo Galindoa5634c42020-09-16 19:42:00 +01004135 asdl_expr_seq *args = e->v.Call.args;
Yury Selivanovf2392132016-12-13 19:03:51 -05004136
4137 /* Check that the call node is an attribute access, and that
4138 the call doesn't have keyword parameters. */
4139 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
4140 asdl_seq_LEN(e->v.Call.keywords))
4141 return -1;
4142
4143 /* Check that there are no *varargs types of arguments. */
4144 argsl = asdl_seq_LEN(args);
4145 for (i = 0; i < argsl; i++) {
4146 expr_ty elt = asdl_seq_GET(args, i);
4147 if (elt->kind == Starred_kind) {
4148 return -1;
4149 }
4150 }
4151
4152 /* Alright, we can optimize the code. */
4153 VISIT(c, expr, meth->v.Attribute.value);
Mark Shannond48848c2021-03-14 18:01:30 +00004154 int old_lineno = c->u->u_lineno;
4155 c->u->u_lineno = meth->end_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004156 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
4157 VISIT_SEQ(c, expr, e->v.Call.args);
4158 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
Mark Shannond48848c2021-03-14 18:01:30 +00004159 c->u->u_lineno = old_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004160 return 1;
4161}
4162
4163static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004164validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
Zackery Spytz08050e92020-04-06 00:47:47 -06004165{
4166 Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
4167 for (Py_ssize_t i = 0; i < nkeywords; i++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004168 keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
4169 if (key->arg == NULL) {
4170 continue;
4171 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +01004172 if (forbidden_name(c, key->arg, Store)) {
4173 return -1;
4174 }
Zackery Spytz08050e92020-04-06 00:47:47 -06004175 for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004176 keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
4177 if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004178 c->u->u_col_offset = other->col_offset;
Brandt Bucher145bf262021-02-26 14:51:55 -08004179 compiler_error(c, "keyword argument repeated: %U", key->arg);
Pablo Galindo254ec782020-04-03 20:37:13 +01004180 return -1;
4181 }
4182 }
4183 }
4184 return 0;
4185}
4186
4187static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004188compiler_call(struct compiler *c, expr_ty e)
4189{
Zackery Spytz97f5de02019-03-22 01:30:32 -06004190 int ret = maybe_optimize_method_call(c, e);
4191 if (ret >= 0) {
4192 return ret;
4193 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004194 if (!check_caller(c, e->v.Call.func)) {
4195 return 0;
4196 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004197 VISIT(c, expr, e->v.Call.func);
4198 return compiler_call_helper(c, 0,
4199 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004200 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004201}
4202
Eric V. Smith235a6f02015-09-19 14:51:32 -04004203static int
4204compiler_joined_str(struct compiler *c, expr_ty e)
4205{
Eric V. Smith235a6f02015-09-19 14:51:32 -04004206 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004207 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1)
4208 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
Eric V. Smith235a6f02015-09-19 14:51:32 -04004209 return 1;
4210}
4211
Eric V. Smitha78c7952015-11-03 12:45:05 -05004212/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004213static int
4214compiler_formatted_value(struct compiler *c, expr_ty e)
4215{
Eric V. Smitha78c7952015-11-03 12:45:05 -05004216 /* Our oparg encodes 2 pieces of information: the conversion
4217 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04004218
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004219 Convert the conversion char to 3 bits:
4220 : 000 0x0 FVC_NONE The default if nothing specified.
Eric V. Smitha78c7952015-11-03 12:45:05 -05004221 !s : 001 0x1 FVC_STR
4222 !r : 010 0x2 FVC_REPR
4223 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04004224
Eric V. Smitha78c7952015-11-03 12:45:05 -05004225 next bit is whether or not we have a format spec:
4226 yes : 100 0x4
4227 no : 000 0x0
4228 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004229
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004230 int conversion = e->v.FormattedValue.conversion;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004231 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004232
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004233 /* The expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004234 VISIT(c, expr, e->v.FormattedValue.value);
4235
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004236 switch (conversion) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004237 case 's': oparg = FVC_STR; break;
4238 case 'r': oparg = FVC_REPR; break;
4239 case 'a': oparg = FVC_ASCII; break;
4240 case -1: oparg = FVC_NONE; break;
4241 default:
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004242 PyErr_Format(PyExc_SystemError,
4243 "Unrecognized conversion character %d", conversion);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004244 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004245 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004246 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004247 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004248 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004249 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004250 }
4251
Eric V. Smitha78c7952015-11-03 12:45:05 -05004252 /* And push our opcode and oparg */
4253 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004254
Eric V. Smith235a6f02015-09-19 14:51:32 -04004255 return 1;
4256}
4257
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004258static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004259compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004260{
4261 Py_ssize_t i, n = end - begin;
4262 keyword_ty kw;
4263 PyObject *keys, *key;
4264 assert(n > 0);
4265 if (n > 1) {
4266 for (i = begin; i < end; i++) {
4267 kw = asdl_seq_GET(keywords, i);
4268 VISIT(c, expr, kw->value);
4269 }
4270 keys = PyTuple_New(n);
4271 if (keys == NULL) {
4272 return 0;
4273 }
4274 for (i = begin; i < end; i++) {
4275 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
4276 Py_INCREF(key);
4277 PyTuple_SET_ITEM(keys, i - begin, key);
4278 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004279 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004280 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
4281 }
4282 else {
4283 /* a for loop only executes once */
4284 for (i = begin; i < end; i++) {
4285 kw = asdl_seq_GET(keywords, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004286 ADDOP_LOAD_CONST(c, kw->arg);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004287 VISIT(c, expr, kw->value);
4288 }
4289 ADDOP_I(c, BUILD_MAP, n);
4290 }
4291 return 1;
4292}
4293
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004294/* shared code between compiler_call and compiler_class */
4295static int
4296compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01004297 int n, /* Args already pushed */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004298 asdl_expr_seq *args,
4299 asdl_keyword_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004300{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004301 Py_ssize_t i, nseen, nelts, nkwelts;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004302
Pablo Galindo254ec782020-04-03 20:37:13 +01004303 if (validate_keywords(c, keywords) == -1) {
4304 return 0;
4305 }
4306
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004307 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004308 nkwelts = asdl_seq_LEN(keywords);
4309
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004310 for (i = 0; i < nelts; i++) {
4311 expr_ty elt = asdl_seq_GET(args, i);
4312 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00004313 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004314 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004315 }
4316 for (i = 0; i < nkwelts; i++) {
4317 keyword_ty kw = asdl_seq_GET(keywords, i);
4318 if (kw->arg == NULL) {
4319 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004320 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004321 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004322
Mark Shannon13bc1392020-01-23 09:25:17 +00004323 /* No * or ** args, so can use faster calling sequence */
4324 for (i = 0; i < nelts; i++) {
4325 expr_ty elt = asdl_seq_GET(args, i);
4326 assert(elt->kind != Starred_kind);
4327 VISIT(c, expr, elt);
4328 }
4329 if (nkwelts) {
4330 PyObject *names;
4331 VISIT_SEQ(c, keyword, keywords);
4332 names = PyTuple_New(nkwelts);
4333 if (names == NULL) {
4334 return 0;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004335 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004336 for (i = 0; i < nkwelts; i++) {
4337 keyword_ty kw = asdl_seq_GET(keywords, i);
4338 Py_INCREF(kw->arg);
4339 PyTuple_SET_ITEM(names, i, kw->arg);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004340 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004341 ADDOP_LOAD_CONST_NEW(c, names);
4342 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
4343 return 1;
4344 }
4345 else {
4346 ADDOP_I(c, CALL_FUNCTION, n + nelts);
4347 return 1;
4348 }
4349
4350ex_call:
4351
4352 /* Do positional arguments. */
4353 if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
4354 VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
4355 }
4356 else if (starunpack_helper(c, args, n, BUILD_LIST,
4357 LIST_APPEND, LIST_EXTEND, 1) == 0) {
4358 return 0;
4359 }
4360 /* Then keyword arguments */
4361 if (nkwelts) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004362 /* Has a new dict been pushed */
4363 int have_dict = 0;
Mark Shannon13bc1392020-01-23 09:25:17 +00004364
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004365 nseen = 0; /* the number of keyword arguments on the stack following */
4366 for (i = 0; i < nkwelts; i++) {
4367 keyword_ty kw = asdl_seq_GET(keywords, i);
4368 if (kw->arg == NULL) {
4369 /* A keyword argument unpacking. */
4370 if (nseen) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004371 if (!compiler_subkwargs(c, keywords, i - nseen, i)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004372 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004373 }
Mark Shannondb64f122020-06-01 10:42:42 +01004374 if (have_dict) {
4375 ADDOP_I(c, DICT_MERGE, 1);
4376 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004377 have_dict = 1;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004378 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004379 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004380 if (!have_dict) {
4381 ADDOP_I(c, BUILD_MAP, 0);
4382 have_dict = 1;
4383 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004384 VISIT(c, expr, kw->value);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004385 ADDOP_I(c, DICT_MERGE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004386 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004387 else {
4388 nseen++;
4389 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004390 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004391 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004392 /* Pack up any trailing keyword arguments. */
Mark Shannon8a4cd702020-01-27 09:57:45 +00004393 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004394 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004395 }
4396 if (have_dict) {
4397 ADDOP_I(c, DICT_MERGE, 1);
4398 }
4399 have_dict = 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004400 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004401 assert(have_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004402 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004403 ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0);
4404 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004405}
4406
Nick Coghlan650f0d02007-04-15 12:05:43 +00004407
4408/* List and set comprehensions and generator expressions work by creating a
4409 nested function to perform the actual iteration. This means that the
4410 iteration variables don't leak into the current scope.
4411 The defined function is called immediately following its definition, with the
4412 result of that call being the result of the expression.
4413 The LC/SC version returns the populated container, while the GE version is
4414 flagged in symtable.c as a generator, so it returns the generator object
4415 when the function is called.
Nick Coghlan650f0d02007-04-15 12:05:43 +00004416
4417 Possible cleanups:
4418 - iterate over the generator sequence instead of using recursion
4419*/
4420
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004421
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004422static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004423compiler_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004424 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004425 int depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004426 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004427{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004428 comprehension_ty gen;
4429 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4430 if (gen->is_async) {
4431 return compiler_async_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004432 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004433 } else {
4434 return compiler_sync_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004435 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004436 }
4437}
4438
4439static int
4440compiler_sync_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004441 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004442 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004443 expr_ty elt, expr_ty val, int type)
4444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004445 /* generate code for the iterator, then each of the ifs,
4446 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004448 comprehension_ty gen;
4449 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004450 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 start = compiler_new_block(c);
4453 skip = compiler_new_block(c);
4454 if_cleanup = compiler_new_block(c);
4455 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 if (start == NULL || skip == NULL || if_cleanup == NULL ||
4458 anchor == NULL)
4459 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004461 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004463 if (gen_index == 0) {
4464 /* Receive outermost iter as an implicit argument */
4465 c->u->u_argcount = 1;
4466 ADDOP_I(c, LOAD_FAST, 0);
4467 }
4468 else {
4469 /* Sub-iter - calculate on the fly */
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004470 /* Fast path for the temporary variable assignment idiom:
4471 for y in [f(x)]
4472 */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004473 asdl_expr_seq *elts;
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004474 switch (gen->iter->kind) {
4475 case List_kind:
4476 elts = gen->iter->v.List.elts;
4477 break;
4478 case Tuple_kind:
4479 elts = gen->iter->v.Tuple.elts;
4480 break;
4481 default:
4482 elts = NULL;
4483 }
4484 if (asdl_seq_LEN(elts) == 1) {
4485 expr_ty elt = asdl_seq_GET(elts, 0);
4486 if (elt->kind != Starred_kind) {
4487 VISIT(c, expr, elt);
4488 start = NULL;
4489 }
4490 }
4491 if (start) {
4492 VISIT(c, expr, gen->iter);
4493 ADDOP(c, GET_ITER);
4494 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004495 }
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004496 if (start) {
4497 depth++;
4498 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01004499 ADDOP_JUMP(c, FOR_ITER, anchor);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004500 NEXT_BLOCK(c);
4501 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004502 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004504 /* XXX this needs to be cleaned up...a lot! */
4505 n = asdl_seq_LEN(gen->ifs);
4506 for (i = 0; i < n; i++) {
4507 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004508 if (!compiler_jump_if(c, e, if_cleanup, 0))
4509 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004510 NEXT_BLOCK(c);
4511 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004513 if (++gen_index < asdl_seq_LEN(generators))
4514 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004515 generators, gen_index, depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004516 elt, val, type))
4517 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004519 /* only append after the last for generator */
4520 if (gen_index >= asdl_seq_LEN(generators)) {
4521 /* comprehension specific code */
4522 switch (type) {
4523 case COMP_GENEXP:
4524 VISIT(c, expr, elt);
4525 ADDOP(c, YIELD_VALUE);
4526 ADDOP(c, POP_TOP);
4527 break;
4528 case COMP_LISTCOMP:
4529 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004530 ADDOP_I(c, LIST_APPEND, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004531 break;
4532 case COMP_SETCOMP:
4533 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004534 ADDOP_I(c, SET_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004535 break;
4536 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004537 /* With '{k: v}', k is evaluated before v, so we do
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 the same. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004539 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004540 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004541 ADDOP_I(c, MAP_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004542 break;
4543 default:
4544 return 0;
4545 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004547 compiler_use_next_block(c, skip);
4548 }
4549 compiler_use_next_block(c, if_cleanup);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004550 if (start) {
Mark Shannon582aaf12020-08-04 17:30:11 +01004551 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004552 compiler_use_next_block(c, anchor);
4553 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004554
4555 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004556}
4557
4558static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004559compiler_async_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004560 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004561 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004562 expr_ty elt, expr_ty val, int type)
4563{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004564 comprehension_ty gen;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004565 basicblock *start, *if_cleanup, *except;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004566 Py_ssize_t i, n;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004567 start = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004568 except = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004569 if_cleanup = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004570
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004571 if (start == NULL || if_cleanup == NULL || except == NULL) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004572 return 0;
4573 }
4574
4575 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4576
4577 if (gen_index == 0) {
4578 /* Receive outermost iter as an implicit argument */
4579 c->u->u_argcount = 1;
4580 ADDOP_I(c, LOAD_FAST, 0);
4581 }
4582 else {
4583 /* Sub-iter - calculate on the fly */
4584 VISIT(c, expr, gen->iter);
4585 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004586 }
4587
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004588 compiler_use_next_block(c, start);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004589
Mark Shannon582aaf12020-08-04 17:30:11 +01004590 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004591 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004592 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004593 ADDOP(c, YIELD_FROM);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004594 ADDOP(c, POP_BLOCK);
Serhiy Storchaka24d32012018-03-10 18:22:34 +02004595 VISIT(c, expr, gen->target);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004596
4597 n = asdl_seq_LEN(gen->ifs);
4598 for (i = 0; i < n; i++) {
4599 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004600 if (!compiler_jump_if(c, e, if_cleanup, 0))
4601 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004602 NEXT_BLOCK(c);
4603 }
4604
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004605 depth++;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004606 if (++gen_index < asdl_seq_LEN(generators))
4607 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004608 generators, gen_index, depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004609 elt, val, type))
4610 return 0;
4611
4612 /* only append after the last for generator */
4613 if (gen_index >= asdl_seq_LEN(generators)) {
4614 /* comprehension specific code */
4615 switch (type) {
4616 case COMP_GENEXP:
4617 VISIT(c, expr, elt);
4618 ADDOP(c, YIELD_VALUE);
4619 ADDOP(c, POP_TOP);
4620 break;
4621 case COMP_LISTCOMP:
4622 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004623 ADDOP_I(c, LIST_APPEND, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004624 break;
4625 case COMP_SETCOMP:
4626 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004627 ADDOP_I(c, SET_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004628 break;
4629 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004630 /* With '{k: v}', k is evaluated before v, so we do
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004631 the same. */
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004632 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004633 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004634 ADDOP_I(c, MAP_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004635 break;
4636 default:
4637 return 0;
4638 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004639 }
4640 compiler_use_next_block(c, if_cleanup);
Mark Shannon582aaf12020-08-04 17:30:11 +01004641 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004642
4643 compiler_use_next_block(c, except);
4644 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004645
4646 return 1;
4647}
4648
4649static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004650compiler_comprehension(struct compiler *c, expr_ty e, int type,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004651 identifier name, asdl_comprehension_seq *generators, expr_ty elt,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004652 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004653{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004654 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004655 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004656 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004657 int is_async_generator = 0;
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004658 int top_level_await = IS_TOP_LEVEL_AWAIT(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004659
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004660
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004661 int is_async_function = c->u->u_ste->ste_coroutine;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004662
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004663 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004664 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4665 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004666 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004667 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004668 }
4669
4670 is_async_generator = c->u->u_ste->ste_coroutine;
4671
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004672 if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004673 compiler_error(c, "asynchronous comprehension outside of "
4674 "an asynchronous function");
4675 goto error_in_scope;
4676 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004678 if (type != COMP_GENEXP) {
4679 int op;
4680 switch (type) {
4681 case COMP_LISTCOMP:
4682 op = BUILD_LIST;
4683 break;
4684 case COMP_SETCOMP:
4685 op = BUILD_SET;
4686 break;
4687 case COMP_DICTCOMP:
4688 op = BUILD_MAP;
4689 break;
4690 default:
4691 PyErr_Format(PyExc_SystemError,
4692 "unknown comprehension type %d", type);
4693 goto error_in_scope;
4694 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004696 ADDOP_I(c, op, 0);
4697 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004698
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004699 if (!compiler_comprehension_generator(c, generators, 0, 0, elt,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004700 val, type))
4701 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004703 if (type != COMP_GENEXP) {
4704 ADDOP(c, RETURN_VALUE);
4705 }
4706
4707 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004708 qualname = c->u->u_qualname;
4709 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004710 compiler_exit_scope(c);
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004711 if (top_level_await && is_async_generator){
4712 c->u->u_ste->ste_coroutine = 1;
4713 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004714 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004715 goto error;
4716
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004717 if (!compiler_make_closure(c, co, 0, qualname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004718 goto error;
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004719 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004720 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004721 Py_DECREF(co);
4722
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004723 VISIT(c, expr, outermost->iter);
4724
4725 if (outermost->is_async) {
4726 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004727 } else {
4728 ADDOP(c, GET_ITER);
4729 }
4730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004731 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004732
4733 if (is_async_generator && type != COMP_GENEXP) {
4734 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004735 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004736 ADDOP(c, YIELD_FROM);
4737 }
4738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004739 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004740error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004741 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004742error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004743 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004744 Py_XDECREF(co);
4745 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004746}
4747
4748static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004749compiler_genexp(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("<genexpr>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004754 if (!name)
4755 return 0;
4756 }
4757 assert(e->kind == GeneratorExp_kind);
4758 return compiler_comprehension(c, e, COMP_GENEXP, name,
4759 e->v.GeneratorExp.generators,
4760 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004761}
4762
4763static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004764compiler_listcomp(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("<listcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004769 if (!name)
4770 return 0;
4771 }
4772 assert(e->kind == ListComp_kind);
4773 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4774 e->v.ListComp.generators,
4775 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004776}
4777
4778static int
4779compiler_setcomp(struct compiler *c, expr_ty e)
4780{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004781 static identifier name;
4782 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004783 name = PyUnicode_InternFromString("<setcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004784 if (!name)
4785 return 0;
4786 }
4787 assert(e->kind == SetComp_kind);
4788 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4789 e->v.SetComp.generators,
4790 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004791}
4792
4793
4794static int
4795compiler_dictcomp(struct compiler *c, expr_ty e)
4796{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004797 static identifier name;
4798 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004799 name = PyUnicode_InternFromString("<dictcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004800 if (!name)
4801 return 0;
4802 }
4803 assert(e->kind == DictComp_kind);
4804 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4805 e->v.DictComp.generators,
4806 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004807}
4808
4809
4810static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004811compiler_visit_keyword(struct compiler *c, keyword_ty k)
4812{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004813 VISIT(c, expr, k->value);
4814 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004815}
4816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004817/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004818 whether they are true or false.
4819
4820 Return values: 1 for true, 0 for false, -1 for non-constant.
4821 */
4822
4823static int
Mark Shannonfee55262019-11-21 09:11:43 +00004824compiler_with_except_finish(struct compiler *c) {
4825 basicblock *exit;
4826 exit = compiler_new_block(c);
4827 if (exit == NULL)
4828 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01004829 ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit);
Mark Shannon266b4622020-11-17 19:30:14 +00004830 NEXT_BLOCK(c);
Mark Shannonbf353f32020-12-17 13:55:28 +00004831 ADDOP_I(c, RERAISE, 1);
Mark Shannonfee55262019-11-21 09:11:43 +00004832 compiler_use_next_block(c, exit);
4833 ADDOP(c, POP_TOP);
4834 ADDOP(c, POP_TOP);
4835 ADDOP(c, POP_TOP);
4836 ADDOP(c, POP_EXCEPT);
4837 ADDOP(c, POP_TOP);
4838 return 1;
4839}
Yury Selivanov75445082015-05-11 22:57:16 -04004840
4841/*
4842 Implements the async with statement.
4843
4844 The semantics outlined in that PEP are as follows:
4845
4846 async with EXPR as VAR:
4847 BLOCK
4848
4849 It is implemented roughly as:
4850
4851 context = EXPR
4852 exit = context.__aexit__ # not calling it
4853 value = await context.__aenter__()
4854 try:
4855 VAR = value # if VAR present in the syntax
4856 BLOCK
4857 finally:
4858 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004859 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04004860 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004861 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04004862 if not (await exit(*exc)):
4863 raise
4864 */
4865static int
4866compiler_async_with(struct compiler *c, stmt_ty s, int pos)
4867{
Mark Shannonfee55262019-11-21 09:11:43 +00004868 basicblock *block, *final, *exit;
Yury Selivanov75445082015-05-11 22:57:16 -04004869 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
4870
4871 assert(s->kind == AsyncWith_kind);
Pablo Galindo90235812020-03-15 04:29:22 +00004872 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07004873 c->u->u_ste->ste_coroutine = 1;
4874 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
Zsolt Dollensteine2396502018-04-27 08:58:56 -07004875 return compiler_error(c, "'async with' outside async function");
4876 }
Yury Selivanov75445082015-05-11 22:57:16 -04004877
4878 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004879 final = compiler_new_block(c);
4880 exit = compiler_new_block(c);
4881 if (!block || !final || !exit)
Yury Selivanov75445082015-05-11 22:57:16 -04004882 return 0;
4883
4884 /* Evaluate EXPR */
4885 VISIT(c, expr, item->context_expr);
4886
4887 ADDOP(c, BEFORE_ASYNC_WITH);
4888 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004889 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004890 ADDOP(c, YIELD_FROM);
4891
Mark Shannon582aaf12020-08-04 17:30:11 +01004892 ADDOP_JUMP(c, SETUP_ASYNC_WITH, final);
Yury Selivanov75445082015-05-11 22:57:16 -04004893
4894 /* SETUP_ASYNC_WITH pushes a finally block. */
4895 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004896 if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004897 return 0;
4898 }
4899
4900 if (item->optional_vars) {
4901 VISIT(c, expr, item->optional_vars);
4902 }
4903 else {
4904 /* Discard result from context.__aenter__() */
4905 ADDOP(c, POP_TOP);
4906 }
4907
4908 pos++;
4909 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
4910 /* BLOCK code */
4911 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
4912 else if (!compiler_async_with(c, s, pos))
4913 return 0;
4914
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004915 compiler_pop_fblock(c, ASYNC_WITH, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004916 ADDOP(c, POP_BLOCK);
4917 /* End of body; start the cleanup */
Yury Selivanov75445082015-05-11 22:57:16 -04004918
Mark Shannonfee55262019-11-21 09:11:43 +00004919 /* For successful outcome:
4920 * call __exit__(None, None, None)
4921 */
4922 if(!compiler_call_exit_with_nones(c))
Yury Selivanov75445082015-05-11 22:57:16 -04004923 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00004924 ADDOP(c, GET_AWAITABLE);
4925 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4926 ADDOP(c, YIELD_FROM);
Yury Selivanov75445082015-05-11 22:57:16 -04004927
Mark Shannonfee55262019-11-21 09:11:43 +00004928 ADDOP(c, POP_TOP);
Yury Selivanov75445082015-05-11 22:57:16 -04004929
Mark Shannon582aaf12020-08-04 17:30:11 +01004930 ADDOP_JUMP(c, JUMP_ABSOLUTE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00004931
4932 /* For exceptional outcome: */
4933 compiler_use_next_block(c, final);
4934
4935 ADDOP(c, WITH_EXCEPT_START);
Yury Selivanov75445082015-05-11 22:57:16 -04004936 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004937 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004938 ADDOP(c, YIELD_FROM);
Mark Shannonfee55262019-11-21 09:11:43 +00004939 compiler_with_except_finish(c);
Yury Selivanov75445082015-05-11 22:57:16 -04004940
Mark Shannonfee55262019-11-21 09:11:43 +00004941compiler_use_next_block(c, exit);
Yury Selivanov75445082015-05-11 22:57:16 -04004942 return 1;
4943}
4944
4945
Guido van Rossumc2e20742006-02-27 22:32:47 +00004946/*
4947 Implements the with statement from PEP 343.
Guido van Rossumc2e20742006-02-27 22:32:47 +00004948 with EXPR as VAR:
4949 BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00004950 is implemented as:
4951 <code for EXPR>
4952 SETUP_WITH E
4953 <code to store to VAR> or POP_TOP
4954 <code for BLOCK>
4955 LOAD_CONST (None, None, None)
4956 CALL_FUNCTION_EX 0
4957 JUMP_FORWARD EXIT
4958 E: WITH_EXCEPT_START (calls EXPR.__exit__)
4959 POP_JUMP_IF_TRUE T:
4960 RERAISE
4961 T: POP_TOP * 3 (remove exception from stack)
4962 POP_EXCEPT
4963 POP_TOP
4964 EXIT:
Guido van Rossumc2e20742006-02-27 22:32:47 +00004965 */
Mark Shannonfee55262019-11-21 09:11:43 +00004966
Guido van Rossumc2e20742006-02-27 22:32:47 +00004967static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004968compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004969{
Mark Shannonfee55262019-11-21 09:11:43 +00004970 basicblock *block, *final, *exit;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004971 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004972
4973 assert(s->kind == With_kind);
4974
Guido van Rossumc2e20742006-02-27 22:32:47 +00004975 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004976 final = compiler_new_block(c);
4977 exit = compiler_new_block(c);
4978 if (!block || !final || !exit)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004979 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004980
Thomas Wouters477c8d52006-05-27 19:21:47 +00004981 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004982 VISIT(c, expr, item->context_expr);
Mark Shannonfee55262019-11-21 09:11:43 +00004983 /* Will push bound __exit__ */
Mark Shannon582aaf12020-08-04 17:30:11 +01004984 ADDOP_JUMP(c, SETUP_WITH, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004985
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004986 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00004987 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004988 if (!compiler_push_fblock(c, WITH, block, final, NULL)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004989 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004990 }
4991
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004992 if (item->optional_vars) {
4993 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004994 }
4995 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004996 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004997 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004998 }
4999
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005000 pos++;
5001 if (pos == asdl_seq_LEN(s->v.With.items))
5002 /* BLOCK code */
5003 VISIT_SEQ(c, stmt, s->v.With.body)
5004 else if (!compiler_with(c, s, pos))
5005 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005006
Mark Shannon3bd60352021-01-13 12:05:43 +00005007
5008 /* Mark all following code as artificial */
5009 c->u->u_lineno = -1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005010 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005011 compiler_pop_fblock(c, WITH, block);
Mark Shannon13bc1392020-01-23 09:25:17 +00005012
Mark Shannonfee55262019-11-21 09:11:43 +00005013 /* End of body; start the cleanup. */
Mark Shannon13bc1392020-01-23 09:25:17 +00005014
Mark Shannonfee55262019-11-21 09:11:43 +00005015 /* For successful outcome:
5016 * call __exit__(None, None, None)
5017 */
5018 if (!compiler_call_exit_with_nones(c))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005019 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00005020 ADDOP(c, POP_TOP);
Mark Shannon582aaf12020-08-04 17:30:11 +01005021 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005022
Mark Shannonfee55262019-11-21 09:11:43 +00005023 /* For exceptional outcome: */
5024 compiler_use_next_block(c, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005025
Mark Shannonfee55262019-11-21 09:11:43 +00005026 ADDOP(c, WITH_EXCEPT_START);
5027 compiler_with_except_finish(c);
5028
5029 compiler_use_next_block(c, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005030 return 1;
5031}
5032
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005033static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005034compiler_visit_expr1(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005035{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005036 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07005037 case NamedExpr_kind:
5038 VISIT(c, expr, e->v.NamedExpr.value);
5039 ADDOP(c, DUP_TOP);
5040 VISIT(c, expr, e->v.NamedExpr.target);
5041 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005042 case BoolOp_kind:
5043 return compiler_boolop(c, e);
5044 case BinOp_kind:
5045 VISIT(c, expr, e->v.BinOp.left);
5046 VISIT(c, expr, e->v.BinOp.right);
Andy Lester76d58772020-03-10 21:18:12 -05005047 ADDOP(c, binop(e->v.BinOp.op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005048 break;
5049 case UnaryOp_kind:
5050 VISIT(c, expr, e->v.UnaryOp.operand);
5051 ADDOP(c, unaryop(e->v.UnaryOp.op));
5052 break;
5053 case Lambda_kind:
5054 return compiler_lambda(c, e);
5055 case IfExp_kind:
5056 return compiler_ifexp(c, e);
5057 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005058 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005059 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005060 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005061 case GeneratorExp_kind:
5062 return compiler_genexp(c, e);
5063 case ListComp_kind:
5064 return compiler_listcomp(c, e);
5065 case SetComp_kind:
5066 return compiler_setcomp(c, e);
5067 case DictComp_kind:
5068 return compiler_dictcomp(c, e);
5069 case Yield_kind:
5070 if (c->u->u_ste->ste_type != FunctionBlock)
5071 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005072 if (e->v.Yield.value) {
5073 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005074 }
5075 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005076 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005077 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005078 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005079 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005080 case YieldFrom_kind:
5081 if (c->u->u_ste->ste_type != FunctionBlock)
5082 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04005083
5084 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
5085 return compiler_error(c, "'yield from' inside async function");
5086
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005087 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04005088 ADDOP(c, GET_YIELD_FROM_ITER);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005089 ADDOP_LOAD_CONST(c, Py_None);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005090 ADDOP(c, YIELD_FROM);
5091 break;
Yury Selivanov75445082015-05-11 22:57:16 -04005092 case Await_kind:
Pablo Galindo90235812020-03-15 04:29:22 +00005093 if (!IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005094 if (c->u->u_ste->ste_type != FunctionBlock){
5095 return compiler_error(c, "'await' outside function");
5096 }
Yury Selivanov75445082015-05-11 22:57:16 -04005097
Victor Stinner331a6a52019-05-27 16:39:22 +02005098 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005099 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
5100 return compiler_error(c, "'await' outside async function");
5101 }
5102 }
Yury Selivanov75445082015-05-11 22:57:16 -04005103
5104 VISIT(c, expr, e->v.Await.value);
5105 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005106 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005107 ADDOP(c, YIELD_FROM);
5108 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005109 case Compare_kind:
5110 return compiler_compare(c, e);
5111 case Call_kind:
5112 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005113 case Constant_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005114 ADDOP_LOAD_CONST(c, e->v.Constant.value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005115 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005116 case JoinedStr_kind:
5117 return compiler_joined_str(c, e);
5118 case FormattedValue_kind:
5119 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005120 /* The following exprs can be assignment targets. */
5121 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005122 VISIT(c, expr, e->v.Attribute.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005123 switch (e->v.Attribute.ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005124 case Load:
Mark Shannond48848c2021-03-14 18:01:30 +00005125 {
5126 int old_lineno = c->u->u_lineno;
5127 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005128 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005129 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005130 break;
Mark Shannond48848c2021-03-14 18:01:30 +00005131 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005132 case Store:
Mark Shannond48848c2021-03-14 18:01:30 +00005133 if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005134 return 0;
Mark Shannond48848c2021-03-14 18:01:30 +00005135 }
5136 int old_lineno = c->u->u_lineno;
5137 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005138 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005139 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005140 break;
5141 case Del:
5142 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
5143 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005144 }
5145 break;
5146 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005147 return compiler_subscript(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005148 case Starred_kind:
5149 switch (e->v.Starred.ctx) {
5150 case Store:
5151 /* In all legitimate cases, the Starred node was already replaced
5152 * by compiler_list/compiler_tuple. XXX: is that okay? */
5153 return compiler_error(c,
5154 "starred assignment target must be in a list or tuple");
5155 default:
5156 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005157 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005158 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005159 break;
5160 case Slice_kind:
5161 return compiler_slice(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005162 case Name_kind:
5163 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
5164 /* child nodes of List and Tuple will have expr_context set */
5165 case List_kind:
5166 return compiler_list(c, e);
5167 case Tuple_kind:
5168 return compiler_tuple(c, e);
Brandt Bucher145bf262021-02-26 14:51:55 -08005169 case MatchAs_kind:
5170 case MatchOr_kind:
5171 // Can only occur in patterns, which are handled elsewhere.
5172 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005173 }
5174 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005175}
5176
5177static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005178compiler_visit_expr(struct compiler *c, expr_ty e)
5179{
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005180 int old_lineno = c->u->u_lineno;
5181 int old_col_offset = c->u->u_col_offset;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005182 SET_LOC(c, e);
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005183 int res = compiler_visit_expr1(c, e);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005184 c->u->u_lineno = old_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005185 c->u->u_col_offset = old_col_offset;
5186 return res;
5187}
5188
5189static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005190compiler_augassign(struct compiler *c, stmt_ty s)
5191{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005192 assert(s->kind == AugAssign_kind);
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005193 expr_ty e = s->v.AugAssign.target;
5194
5195 int old_lineno = c->u->u_lineno;
5196 int old_col_offset = c->u->u_col_offset;
5197 SET_LOC(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005199 switch (e->kind) {
5200 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005201 VISIT(c, expr, e->v.Attribute.value);
5202 ADDOP(c, DUP_TOP);
Mark Shannond48848c2021-03-14 18:01:30 +00005203 int old_lineno = c->u->u_lineno;
5204 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005205 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005206 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005207 break;
5208 case Subscript_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005209 VISIT(c, expr, e->v.Subscript.value);
5210 VISIT(c, expr, e->v.Subscript.slice);
5211 ADDOP(c, DUP_TOP_TWO);
5212 ADDOP(c, BINARY_SUBSCR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005213 break;
5214 case Name_kind:
5215 if (!compiler_nameop(c, e->v.Name.id, Load))
5216 return 0;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005217 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005218 default:
5219 PyErr_Format(PyExc_SystemError,
5220 "invalid node type (%d) for augmented assignment",
5221 e->kind);
5222 return 0;
5223 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005224
5225 c->u->u_lineno = old_lineno;
5226 c->u->u_col_offset = old_col_offset;
5227
5228 VISIT(c, expr, s->v.AugAssign.value);
5229 ADDOP(c, inplace_binop(s->v.AugAssign.op));
5230
5231 SET_LOC(c, e);
5232
5233 switch (e->kind) {
5234 case Attribute_kind:
Mark Shannond48848c2021-03-14 18:01:30 +00005235 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005236 ADDOP(c, ROT_TWO);
5237 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5238 break;
5239 case Subscript_kind:
5240 ADDOP(c, ROT_THREE);
5241 ADDOP(c, STORE_SUBSCR);
5242 break;
5243 case Name_kind:
5244 return compiler_nameop(c, e->v.Name.id, Store);
5245 default:
5246 Py_UNREACHABLE();
5247 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005248 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005249}
5250
5251static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005252check_ann_expr(struct compiler *c, expr_ty e)
5253{
5254 VISIT(c, expr, e);
5255 ADDOP(c, POP_TOP);
5256 return 1;
5257}
5258
5259static int
5260check_annotation(struct compiler *c, stmt_ty s)
5261{
5262 /* Annotations are only evaluated in a module or class. */
5263 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5264 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
5265 return check_ann_expr(c, s->v.AnnAssign.annotation);
5266 }
5267 return 1;
5268}
5269
5270static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005271check_ann_subscr(struct compiler *c, expr_ty e)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005272{
5273 /* We check that everything in a subscript is defined at runtime. */
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005274 switch (e->kind) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005275 case Slice_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005276 if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005277 return 0;
5278 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005279 if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) {
5280 return 0;
5281 }
5282 if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) {
5283 return 0;
5284 }
5285 return 1;
5286 case Tuple_kind: {
5287 /* extended slice */
Pablo Galindoa5634c42020-09-16 19:42:00 +01005288 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005289 Py_ssize_t i, n = asdl_seq_LEN(elts);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005290 for (i = 0; i < n; i++) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005291 if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005292 return 0;
5293 }
5294 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005295 return 1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005296 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005297 default:
5298 return check_ann_expr(c, e);
5299 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005300}
5301
5302static int
5303compiler_annassign(struct compiler *c, stmt_ty s)
5304{
5305 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07005306 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005307
5308 assert(s->kind == AnnAssign_kind);
5309
5310 /* We perform the actual assignment first. */
5311 if (s->v.AnnAssign.value) {
5312 VISIT(c, expr, s->v.AnnAssign.value);
5313 VISIT(c, expr, targ);
5314 }
5315 switch (targ->kind) {
5316 case Name_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005317 if (forbidden_name(c, targ->v.Name.id, Store))
5318 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005319 /* If we have a simple name in a module or class, store annotation. */
5320 if (s->v.AnnAssign.simple &&
5321 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5322 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Batuhan Taskaya044a1042020-10-06 23:03:02 +03005323 VISIT(c, annexpr, s->v.AnnAssign.annotation);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005324 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02005325 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005326 ADDOP_LOAD_CONST_NEW(c, mangled);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005327 ADDOP(c, STORE_SUBSCR);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005328 }
5329 break;
5330 case Attribute_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005331 if (forbidden_name(c, targ->v.Attribute.attr, Store))
5332 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005333 if (!s->v.AnnAssign.value &&
5334 !check_ann_expr(c, targ->v.Attribute.value)) {
5335 return 0;
5336 }
5337 break;
5338 case Subscript_kind:
5339 if (!s->v.AnnAssign.value &&
5340 (!check_ann_expr(c, targ->v.Subscript.value) ||
5341 !check_ann_subscr(c, targ->v.Subscript.slice))) {
5342 return 0;
5343 }
5344 break;
5345 default:
5346 PyErr_Format(PyExc_SystemError,
5347 "invalid node type (%d) for annotated assignment",
5348 targ->kind);
5349 return 0;
5350 }
5351 /* Annotation is evaluated last. */
5352 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
5353 return 0;
5354 }
5355 return 1;
5356}
5357
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005358/* Raises a SyntaxError and returns 0.
5359 If something goes wrong, a different exception may be raised.
5360*/
5361
5362static int
Brandt Bucher145bf262021-02-26 14:51:55 -08005363compiler_error(struct compiler *c, const char *format, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005364{
Brandt Bucher145bf262021-02-26 14:51:55 -08005365 va_list vargs;
5366#ifdef HAVE_STDARG_PROTOTYPES
5367 va_start(vargs, format);
5368#else
5369 va_start(vargs);
5370#endif
5371 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5372 va_end(vargs);
5373 if (msg == NULL) {
5374 return 0;
5375 }
5376 PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
5377 if (loc == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005378 Py_INCREF(Py_None);
5379 loc = Py_None;
5380 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005381 PyObject *args = Py_BuildValue("O(OiiO)", msg, c->c_filename,
5382 c->u->u_lineno, c->u->u_col_offset + 1, loc);
5383 Py_DECREF(msg);
5384 if (args == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005385 goto exit;
Brandt Bucher145bf262021-02-26 14:51:55 -08005386 }
5387 PyErr_SetObject(PyExc_SyntaxError, args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005388 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005389 Py_DECREF(loc);
Brandt Bucher145bf262021-02-26 14:51:55 -08005390 Py_XDECREF(args);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005391 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005392}
5393
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005394/* Emits a SyntaxWarning and returns 1 on success.
5395 If a SyntaxWarning raised as error, replaces it with a SyntaxError
5396 and returns 0.
5397*/
5398static int
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005399compiler_warn(struct compiler *c, const char *format, ...)
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005400{
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005401 va_list vargs;
5402#ifdef HAVE_STDARG_PROTOTYPES
5403 va_start(vargs, format);
5404#else
5405 va_start(vargs);
5406#endif
5407 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5408 va_end(vargs);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005409 if (msg == NULL) {
5410 return 0;
5411 }
5412 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
5413 c->u->u_lineno, NULL, NULL) < 0)
5414 {
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005415 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005416 /* Replace the SyntaxWarning exception with a SyntaxError
5417 to get a more accurate error report */
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005418 PyErr_Clear();
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005419 assert(PyUnicode_AsUTF8(msg) != NULL);
5420 compiler_error(c, PyUnicode_AsUTF8(msg));
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005421 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005422 Py_DECREF(msg);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005423 return 0;
5424 }
5425 Py_DECREF(msg);
5426 return 1;
5427}
5428
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005429static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005430compiler_subscript(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005431{
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005432 expr_context_ty ctx = e->v.Subscript.ctx;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005433 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005434
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005435 if (ctx == Load) {
5436 if (!check_subscripter(c, e->v.Subscript.value)) {
5437 return 0;
5438 }
5439 if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
5440 return 0;
5441 }
5442 }
5443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005444 switch (ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005445 case Load: op = BINARY_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005446 case Store: op = STORE_SUBSCR; break;
5447 case Del: op = DELETE_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005448 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005449 assert(op);
5450 VISIT(c, expr, e->v.Subscript.value);
5451 VISIT(c, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005452 ADDOP(c, op);
5453 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005454}
5455
5456static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005457compiler_slice(struct compiler *c, expr_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005459 int n = 2;
5460 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005462 /* only handles the cases where BUILD_SLICE is emitted */
5463 if (s->v.Slice.lower) {
5464 VISIT(c, expr, s->v.Slice.lower);
5465 }
5466 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005467 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005468 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005470 if (s->v.Slice.upper) {
5471 VISIT(c, expr, s->v.Slice.upper);
5472 }
5473 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005474 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005475 }
5476
5477 if (s->v.Slice.step) {
5478 n++;
5479 VISIT(c, expr, s->v.Slice.step);
5480 }
5481 ADDOP_I(c, BUILD_SLICE, n);
5482 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005483}
5484
Brandt Bucher145bf262021-02-26 14:51:55 -08005485
5486// PEP 634: Structural Pattern Matching
5487
5488// To keep things simple, all compiler_pattern_* routines follow the convention
5489// of replacing TOS (the subject for the given pattern) with either True (match)
5490// or False (no match). We do this even for irrefutable patterns; the idea is
5491// that it's much easier to smooth out any redundant pushing, popping, and
5492// jumping in the peephole optimizer than to detect or predict it here.
5493
5494
5495#define WILDCARD_CHECK(N) \
5496 ((N)->kind == Name_kind && \
5497 _PyUnicode_EqualToASCIIString((N)->v.Name.id, "_"))
5498
5499
5500static int
5501pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc)
5502{
5503 assert(!_PyUnicode_EqualToASCIIString(n, "_"));
5504 // Can't assign to the same name twice:
5505 if (pc->stores == NULL) {
5506 RETURN_IF_FALSE(pc->stores = PySet_New(NULL));
5507 }
5508 else {
5509 int duplicate = PySet_Contains(pc->stores, n);
5510 if (duplicate < 0) {
5511 return 0;
5512 }
5513 if (duplicate) {
5514 const char *e = "multiple assignments to name %R in pattern";
5515 return compiler_error(c, e, n);
5516 }
5517 }
5518 RETURN_IF_FALSE(!PySet_Add(pc->stores, n));
5519 RETURN_IF_FALSE(compiler_nameop(c, n, Store));
5520 return 1;
5521}
5522
5523
5524static int
5525pattern_helper_sequence_unpack(struct compiler *c, asdl_expr_seq *values,
5526 Py_ssize_t star, pattern_context *pc)
5527{
5528 RETURN_IF_FALSE(unpack_helper(c, values));
5529 // We've now got a bunch of new subjects on the stack. If any of them fail
5530 // to match, we need to pop everything else off, then finally push False.
5531 // fails is an array of blocks that correspond to the necessary amount of
5532 // popping for each element:
5533 basicblock **fails;
5534 Py_ssize_t size = asdl_seq_LEN(values);
5535 fails = (basicblock **)PyObject_Malloc(sizeof(basicblock*) * size);
5536 if (fails == NULL) {
5537 PyErr_NoMemory();
5538 return 0;
5539 }
5540 // NOTE: Can't use our nice returning macros anymore: they'll leak memory!
5541 // goto error on error.
5542 for (Py_ssize_t i = 0; i < size; i++) {
5543 fails[i] = compiler_new_block(c);
5544 if (fails[i] == NULL) {
5545 goto error;
5546 }
5547 }
5548 for (Py_ssize_t i = 0; i < size; i++) {
5549 expr_ty value = asdl_seq_GET(values, i);
5550 if (i == star) {
5551 assert(value->kind == Starred_kind);
5552 value = value->v.Starred.value;
5553 }
5554 if (!compiler_pattern_subpattern(c, value, pc) ||
5555 !compiler_addop_j(c, POP_JUMP_IF_FALSE, fails[i]) ||
5556 compiler_next_block(c) == NULL)
5557 {
5558 goto error;
5559 }
5560 }
5561 // Success!
5562 basicblock *end = compiler_new_block(c);
5563 if (end == NULL ||
5564 !compiler_addop_load_const(c, Py_True) ||
5565 !compiler_addop_j(c, JUMP_FORWARD, end))
5566 {
5567 goto error;
5568 }
5569 // This is where we handle failed sub-patterns. For a sequence pattern like
5570 // [a, b, c, d], this will look like:
5571 // fails[0]: POP_TOP
5572 // fails[1]: POP_TOP
5573 // fails[2]: POP_TOP
5574 // fails[3]: LOAD_CONST False
5575 for (Py_ssize_t i = 0; i < size - 1; i++) {
5576 compiler_use_next_block(c, fails[i]);
5577 if (!compiler_addop(c, POP_TOP)) {
5578 goto error;
5579 }
5580 }
5581 compiler_use_next_block(c, fails[size - 1]);
5582 if (!compiler_addop_load_const(c, Py_False)) {
5583 goto error;
5584 }
5585 compiler_use_next_block(c, end);
5586 PyObject_Free(fails);
5587 return 1;
5588error:
5589 PyObject_Free(fails);
5590 return 0;
5591}
5592
5593// Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of
5594// UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a
5595// starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc.
5596static int
5597pattern_helper_sequence_subscr(struct compiler *c, asdl_expr_seq *values,
5598 Py_ssize_t star, pattern_context *pc)
5599{
5600 basicblock *end, *fail_pop_1;
5601 RETURN_IF_FALSE(end = compiler_new_block(c));
5602 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5603 Py_ssize_t size = asdl_seq_LEN(values);
5604 for (Py_ssize_t i = 0; i < size; i++) {
5605 expr_ty value = asdl_seq_GET(values, i);
5606 if (WILDCARD_CHECK(value)) {
5607 continue;
5608 }
5609 if (i == star) {
5610 assert(value->kind == Starred_kind);
5611 assert(WILDCARD_CHECK(value->v.Starred.value));
5612 continue;
5613 }
5614 ADDOP(c, DUP_TOP);
5615 if (i < star) {
5616 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5617 }
5618 else {
5619 // The subject may not support negative indexing! Compute a
5620 // nonnegative index:
5621 ADDOP(c, GET_LEN);
5622 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i));
5623 ADDOP(c, BINARY_SUBTRACT);
5624 }
5625 ADDOP(c, BINARY_SUBSCR);
5626 RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc));
5627 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5628 NEXT_BLOCK(c);
5629 }
5630 ADDOP(c, POP_TOP);
5631 ADDOP_LOAD_CONST(c, Py_True);
5632 ADDOP_JUMP(c, JUMP_FORWARD, end);
5633 compiler_use_next_block(c, fail_pop_1);
5634 ADDOP(c, POP_TOP);
5635 ADDOP_LOAD_CONST(c, Py_False);
5636 compiler_use_next_block(c, end);
5637 return 1;
5638}
5639
5640
5641// Like compiler_pattern, but turn off checks for irrefutability.
5642static int
5643compiler_pattern_subpattern(struct compiler *c, expr_ty p, pattern_context *pc)
5644{
5645 int allow_irrefutable = pc->allow_irrefutable;
5646 pc->allow_irrefutable = 1;
5647 RETURN_IF_FALSE(compiler_pattern(c, p, pc));
5648 pc->allow_irrefutable = allow_irrefutable;
5649 return 1;
5650}
5651
5652
5653static int
5654compiler_pattern_as(struct compiler *c, expr_ty p, pattern_context *pc)
5655{
5656 assert(p->kind == MatchAs_kind);
5657 basicblock *end, *fail_pop_1;
5658 RETURN_IF_FALSE(end = compiler_new_block(c));
5659 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5660 // Need to make a copy for (possibly) storing later:
5661 ADDOP(c, DUP_TOP);
5662 RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc));
5663 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5664 NEXT_BLOCK(c);
5665 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc));
5666 ADDOP_LOAD_CONST(c, Py_True);
5667 ADDOP_JUMP(c, JUMP_FORWARD, end);
5668 compiler_use_next_block(c, fail_pop_1);
5669 // Need to pop that unused copy from before:
5670 ADDOP(c, POP_TOP);
5671 ADDOP_LOAD_CONST(c, Py_False);
5672 compiler_use_next_block(c, end);
5673 return 1;
5674}
5675
5676
5677static int
5678compiler_pattern_capture(struct compiler *c, expr_ty p, pattern_context *pc)
5679{
5680 assert(p->kind == Name_kind);
5681 assert(p->v.Name.ctx == Store);
5682 assert(!WILDCARD_CHECK(p));
5683 if (!pc->allow_irrefutable) {
5684 // Whoops, can't have a name capture here!
5685 const char *e = "name capture %R makes remaining patterns unreachable";
5686 return compiler_error(c, e, p->v.Name.id);
5687 }
5688 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.Name.id, pc));
5689 ADDOP_LOAD_CONST(c, Py_True);
5690 return 1;
5691}
5692
5693
5694static int
5695compiler_pattern_class(struct compiler *c, expr_ty p, pattern_context *pc)
5696{
5697 asdl_expr_seq *args = p->v.Call.args;
5698 asdl_keyword_seq *kwargs = p->v.Call.keywords;
5699 Py_ssize_t nargs = asdl_seq_LEN(args);
5700 Py_ssize_t nkwargs = asdl_seq_LEN(kwargs);
5701 if (INT_MAX < nargs || INT_MAX < nargs + nkwargs - 1) {
5702 const char *e = "too many sub-patterns in class pattern %R";
5703 return compiler_error(c, e, p->v.Call.func);
5704 }
5705 RETURN_IF_FALSE(!validate_keywords(c, kwargs));
5706 basicblock *end, *fail_pop_1;
5707 RETURN_IF_FALSE(end = compiler_new_block(c));
5708 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5709 VISIT(c, expr, p->v.Call.func);
5710 PyObject *kwnames;
5711 RETURN_IF_FALSE(kwnames = PyTuple_New(nkwargs));
5712 Py_ssize_t i;
5713 for (i = 0; i < nkwargs; i++) {
5714 PyObject *name = ((keyword_ty) asdl_seq_GET(kwargs, i))->arg;
5715 Py_INCREF(name);
5716 PyTuple_SET_ITEM(kwnames, i, name);
5717 }
5718 ADDOP_LOAD_CONST_NEW(c, kwnames);
5719 ADDOP_I(c, MATCH_CLASS, nargs);
5720 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5721 NEXT_BLOCK(c);
5722 // TOS is now a tuple of (nargs + nkwargs) attributes.
5723 for (i = 0; i < nargs + nkwargs; i++) {
5724 expr_ty arg;
5725 if (i < nargs) {
5726 // Positional:
5727 arg = asdl_seq_GET(args, i);
5728 }
5729 else {
5730 // Keyword:
5731 arg = ((keyword_ty) asdl_seq_GET(kwargs, i - nargs))->value;
5732 }
5733 if (WILDCARD_CHECK(arg)) {
5734 continue;
5735 }
5736 // Get the i-th attribute, and match it against the i-th pattern:
5737 ADDOP(c, DUP_TOP);
5738 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5739 ADDOP(c, BINARY_SUBSCR);
5740 RETURN_IF_FALSE(compiler_pattern_subpattern(c, arg, pc));
5741 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5742 NEXT_BLOCK(c);
5743 }
5744 // Success! Pop the tuple of attributes:
5745 ADDOP(c, POP_TOP);
5746 ADDOP_LOAD_CONST(c, Py_True);
5747 ADDOP_JUMP(c, JUMP_FORWARD, end);
5748 compiler_use_next_block(c, fail_pop_1);
5749 ADDOP(c, POP_TOP);
5750 ADDOP_LOAD_CONST(c, Py_False);
5751 compiler_use_next_block(c, end);
5752 return 1;
5753}
5754
5755
5756static int
5757compiler_pattern_literal(struct compiler *c, expr_ty p, pattern_context *pc)
5758{
5759 assert(p->kind == Constant_kind);
5760 PyObject *v = p->v.Constant.value;
5761 ADDOP_LOAD_CONST(c, v);
5762 // Literal True, False, and None are compared by identity. All others use
5763 // equality:
5764 ADDOP_COMPARE(c, (v == Py_None || PyBool_Check(v)) ? Is : Eq);
5765 return 1;
5766}
5767
5768
5769static int
5770compiler_pattern_mapping(struct compiler *c, expr_ty p, pattern_context *pc)
5771{
5772 basicblock *end, *fail_pop_1, *fail_pop_3;
5773 RETURN_IF_FALSE(end = compiler_new_block(c));
5774 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5775 RETURN_IF_FALSE(fail_pop_3 = compiler_new_block(c));
5776 asdl_expr_seq *keys = p->v.Dict.keys;
5777 asdl_expr_seq *values = p->v.Dict.values;
5778 Py_ssize_t size = asdl_seq_LEN(values);
Ikko Ashimine57827f82021-03-10 19:39:51 +09005779 // A starred pattern will be a keyless value. It is guaranteed to be last:
Brandt Bucher145bf262021-02-26 14:51:55 -08005780 int star = size ? !asdl_seq_GET(keys, size - 1) : 0;
5781 ADDOP(c, MATCH_MAPPING);
5782 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5783 NEXT_BLOCK(c);
5784 if (!size) {
5785 // If the pattern is just "{}", we're done!
5786 ADDOP(c, POP_TOP);
5787 ADDOP_LOAD_CONST(c, Py_True);
5788 ADDOP_JUMP(c, JUMP_FORWARD, end);
5789 compiler_use_next_block(c, fail_pop_1);
5790 ADDOP(c, POP_TOP);
5791 ADDOP_LOAD_CONST(c, Py_False);
5792 compiler_use_next_block(c, end);
5793 return 1;
5794 }
5795 if (size - star) {
5796 // If the pattern has any keys in it, perform a length check:
5797 ADDOP(c, GET_LEN);
5798 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - star));
5799 ADDOP_COMPARE(c, GtE);
5800 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5801 NEXT_BLOCK(c);
5802 }
5803 if (INT_MAX < size - star - 1) {
5804 return compiler_error(c, "too many sub-patterns in mapping pattern");
5805 }
5806 // Collect all of the keys into a tuple for MATCH_KEYS and
5807 // COPY_DICT_WITHOUT_KEYS. They can either be dotted names or literals:
5808 for (Py_ssize_t i = 0; i < size - star; i++) {
5809 expr_ty key = asdl_seq_GET(keys, i);
5810 if (key == NULL) {
5811 const char *e = "can't use starred name here "
5812 "(consider moving to end)";
5813 return compiler_error(c, e);
5814 }
5815 VISIT(c, expr, key);
5816 }
5817 ADDOP_I(c, BUILD_TUPLE, size - star);
5818 ADDOP(c, MATCH_KEYS);
5819 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3);
5820 NEXT_BLOCK(c);
5821 // So far so good. There's now a tuple of values on the stack to match
5822 // sub-patterns against:
5823 for (Py_ssize_t i = 0; i < size - star; i++) {
5824 expr_ty value = asdl_seq_GET(values, i);
5825 if (WILDCARD_CHECK(value)) {
5826 continue;
5827 }
5828 ADDOP(c, DUP_TOP);
5829 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5830 ADDOP(c, BINARY_SUBSCR);
5831 RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc));
5832 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3);
5833 NEXT_BLOCK(c);
5834 }
5835 // If we get this far, it's a match! We're done with that tuple of values.
5836 ADDOP(c, POP_TOP);
5837 if (star) {
5838 // If we had a starred name, bind a dict of remaining items to it:
5839 ADDOP(c, COPY_DICT_WITHOUT_KEYS);
5840 PyObject *id = asdl_seq_GET(values, size - 1)->v.Name.id;
5841 RETURN_IF_FALSE(pattern_helper_store_name(c, id, pc));
5842 }
5843 else {
5844 // Otherwise, we don't care about this tuple of keys anymore:
5845 ADDOP(c, POP_TOP);
5846 }
5847 // Pop the subject:
5848 ADDOP(c, POP_TOP);
5849 ADDOP_LOAD_CONST(c, Py_True);
5850 ADDOP_JUMP(c, JUMP_FORWARD, end);
5851 // The top two items are a tuple of values or None, followed by a tuple of
5852 // keys. Pop them both:
5853 compiler_use_next_block(c, fail_pop_3);
5854 ADDOP(c, POP_TOP);
5855 ADDOP(c, POP_TOP);
5856 compiler_use_next_block(c, fail_pop_1);
5857 // Pop the subject:
5858 ADDOP(c, POP_TOP);
5859 ADDOP_LOAD_CONST(c, Py_False);
5860 compiler_use_next_block(c, end);
5861 return 1;
5862}
5863
5864
5865static int
5866compiler_pattern_or(struct compiler *c, expr_ty p, pattern_context *pc)
5867{
5868 assert(p->kind == MatchOr_kind);
5869 // control is the set of names bound by the first alternative. If all of the
5870 // others bind the same names (they should), then this becomes pc->stores.
5871 PyObject *control = NULL;
5872 basicblock *end, *pass_pop_1;
5873 RETURN_IF_FALSE(end = compiler_new_block(c));
5874 RETURN_IF_FALSE(pass_pop_1 = compiler_new_block(c));
5875 Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns);
5876 assert(size > 1);
5877 // We're going to be messing with pc. Keep the original info handy:
5878 PyObject *stores_init = pc->stores;
5879 int allow_irrefutable = pc->allow_irrefutable;
5880 for (Py_ssize_t i = 0; i < size; i++) {
5881 // NOTE: Can't use our nice returning macros in here: they'll leak sets!
5882 expr_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i);
5883 pc->stores = PySet_New(stores_init);
5884 // An irrefutable sub-pattern must be last, if it is allowed at all:
5885 int is_last = i == size - 1;
5886 pc->allow_irrefutable = allow_irrefutable && is_last;
5887 SET_LOC(c, alt);
5888 if (pc->stores == NULL ||
5889 // Only copy the subject if we're *not* on the last alternative:
5890 (!is_last && !compiler_addop(c, DUP_TOP)) ||
5891 !compiler_pattern(c, alt, pc) ||
5892 // Only jump if we're *not* on the last alternative:
5893 (!is_last && !compiler_addop_j(c, POP_JUMP_IF_TRUE, pass_pop_1)) ||
5894 !compiler_next_block(c))
5895 {
5896 goto fail;
5897 }
5898 if (!i) {
5899 // If this is the first alternative, save its stores as a "control"
5900 // for the others (they can't bind a different set of names):
5901 control = pc->stores;
5902 continue;
5903 }
5904 if (PySet_GET_SIZE(pc->stores) || PySet_GET_SIZE(control)) {
5905 // Otherwise, check to see if we differ from the control set:
5906 PyObject *diff = PyNumber_InPlaceXor(pc->stores, control);
5907 if (diff == NULL) {
5908 goto fail;
5909 }
5910 if (PySet_GET_SIZE(diff)) {
5911 // The names differ! Raise.
5912 Py_DECREF(diff);
5913 compiler_error(c, "alternative patterns bind different names");
5914 goto fail;
5915 }
5916 Py_DECREF(diff);
5917 }
5918 Py_DECREF(pc->stores);
5919 }
5920 Py_XDECREF(stores_init);
5921 // Update pc->stores and restore pc->allow_irrefutable:
5922 pc->stores = control;
5923 pc->allow_irrefutable = allow_irrefutable;
5924 ADDOP_JUMP(c, JUMP_FORWARD, end);
5925 compiler_use_next_block(c, pass_pop_1);
5926 ADDOP(c, POP_TOP);
5927 ADDOP_LOAD_CONST(c, Py_True);
5928 compiler_use_next_block(c, end);
5929 return 1;
5930fail:
5931 Py_XDECREF(stores_init);
5932 Py_XDECREF(control);
5933 return 0;
5934}
5935
5936
5937static int
5938compiler_pattern_sequence(struct compiler *c, expr_ty p, pattern_context *pc)
5939{
5940 assert(p->kind == List_kind || p->kind == Tuple_kind);
5941 asdl_expr_seq *values = (p->kind == Tuple_kind) ? p->v.Tuple.elts
5942 : p->v.List.elts;
5943 Py_ssize_t size = asdl_seq_LEN(values);
5944 Py_ssize_t star = -1;
5945 int only_wildcard = 1;
5946 int star_wildcard = 0;
5947 // Find a starred name, if it exists. There may be at most one:
5948 for (Py_ssize_t i = 0; i < size; i++) {
5949 expr_ty value = asdl_seq_GET(values, i);
5950 if (value->kind == Starred_kind) {
5951 value = value->v.Starred.value;
5952 if (star >= 0) {
5953 const char *e = "multiple starred names in sequence pattern";
5954 return compiler_error(c, e);
5955 }
5956 star_wildcard = WILDCARD_CHECK(value);
5957 star = i;
5958 }
5959 only_wildcard &= WILDCARD_CHECK(value);
5960 }
5961 basicblock *end, *fail_pop_1;
5962 RETURN_IF_FALSE(end = compiler_new_block(c));
5963 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5964 ADDOP(c, MATCH_SEQUENCE);
5965 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5966 NEXT_BLOCK(c);
5967 if (star < 0) {
5968 // No star: len(subject) == size
5969 ADDOP(c, GET_LEN);
5970 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
5971 ADDOP_COMPARE(c, Eq);
5972 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5973 NEXT_BLOCK(c);
5974 }
5975 else if (size > 1) {
5976 // Star: len(subject) >= size - 1
5977 ADDOP(c, GET_LEN);
5978 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1));
5979 ADDOP_COMPARE(c, GtE);
5980 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5981 NEXT_BLOCK(c);
5982 }
5983 if (only_wildcard) {
5984 // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc.
5985 ADDOP(c, POP_TOP);
5986 ADDOP_LOAD_CONST(c, Py_True);
5987 }
5988 else if (star_wildcard) {
5989 RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, values, star, pc));
5990 }
5991 else {
5992 RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, values, star, pc));
5993 }
5994 ADDOP_JUMP(c, JUMP_FORWARD, end);
5995 compiler_use_next_block(c, fail_pop_1);
5996 ADDOP(c, POP_TOP)
5997 ADDOP_LOAD_CONST(c, Py_False);
5998 compiler_use_next_block(c, end);
5999 return 1;
6000}
6001
6002
6003static int
6004compiler_pattern_value(struct compiler *c, expr_ty p, pattern_context *pc)
6005{
6006 assert(p->kind == Attribute_kind);
6007 assert(p->v.Attribute.ctx == Load);
6008 VISIT(c, expr, p);
6009 ADDOP_COMPARE(c, Eq);
6010 return 1;
6011}
6012
6013
6014static int
6015compiler_pattern_wildcard(struct compiler *c, expr_ty p, pattern_context *pc)
6016{
6017 assert(p->kind == Name_kind);
6018 assert(p->v.Name.ctx == Store);
6019 assert(WILDCARD_CHECK(p));
6020 if (!pc->allow_irrefutable) {
6021 // Whoops, can't have a wildcard here!
6022 const char *e = "wildcard makes remaining patterns unreachable";
6023 return compiler_error(c, e);
6024 }
6025 ADDOP(c, POP_TOP);
6026 ADDOP_LOAD_CONST(c, Py_True);
6027 return 1;
6028}
6029
6030
6031static int
6032compiler_pattern(struct compiler *c, expr_ty p, pattern_context *pc)
6033{
6034 SET_LOC(c, p);
6035 switch (p->kind) {
6036 case Attribute_kind:
6037 return compiler_pattern_value(c, p, pc);
6038 case BinOp_kind:
6039 // Because we allow "2+2j", things like "2+2" make it this far:
6040 return compiler_error(c, "patterns cannot include operators");
6041 case Call_kind:
6042 return compiler_pattern_class(c, p, pc);
6043 case Constant_kind:
6044 return compiler_pattern_literal(c, p, pc);
6045 case Dict_kind:
6046 return compiler_pattern_mapping(c, p, pc);
6047 case JoinedStr_kind:
6048 // Because we allow strings, f-strings make it this far:
6049 return compiler_error(c, "patterns cannot include f-strings");
6050 case List_kind:
6051 case Tuple_kind:
6052 return compiler_pattern_sequence(c, p, pc);
6053 case MatchAs_kind:
6054 return compiler_pattern_as(c, p, pc);
6055 case MatchOr_kind:
6056 return compiler_pattern_or(c, p, pc);
6057 case Name_kind:
6058 if (WILDCARD_CHECK(p)) {
6059 return compiler_pattern_wildcard(c, p, pc);
6060 }
6061 return compiler_pattern_capture(c, p, pc);
6062 default:
6063 Py_UNREACHABLE();
6064 }
6065}
6066
6067
6068static int
6069compiler_match(struct compiler *c, stmt_ty s)
6070{
6071 VISIT(c, expr, s->v.Match.subject);
6072 basicblock *next, *end;
6073 RETURN_IF_FALSE(end = compiler_new_block(c));
6074 Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases);
6075 assert(cases);
6076 pattern_context pc;
6077 // We use pc.stores to track:
6078 // - Repeated name assignments in the same pattern.
6079 // - Different name assignments in alternatives.
6080 // It's a set of names, but we don't create it until it's needed:
6081 pc.stores = NULL;
6082 match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6083 int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases;
6084 for (Py_ssize_t i = 0; i < cases - has_default; i++) {
6085 m = asdl_seq_GET(s->v.Match.cases, i);
6086 SET_LOC(c, m->pattern);
6087 RETURN_IF_FALSE(next = compiler_new_block(c));
6088 // If pc.allow_irrefutable is 0, any name captures against our subject
6089 // will raise. Irrefutable cases must be either guarded, last, or both:
6090 pc.allow_irrefutable = m->guard != NULL || i == cases - 1;
6091 // Only copy the subject if we're *not* on the last case:
6092 if (i != cases - has_default - 1) {
6093 ADDOP(c, DUP_TOP);
6094 }
6095 int result = compiler_pattern(c, m->pattern, &pc);
6096 Py_CLEAR(pc.stores);
6097 RETURN_IF_FALSE(result);
6098 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, next);
6099 NEXT_BLOCK(c);
6100 if (m->guard) {
6101 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, next, 0));
6102 }
6103 // Success! Pop the subject off, we're done with it:
6104 if (i != cases - has_default - 1) {
6105 ADDOP(c, POP_TOP);
6106 }
6107 VISIT_SEQ(c, stmt, m->body);
6108 ADDOP_JUMP(c, JUMP_FORWARD, end);
6109 compiler_use_next_block(c, next);
6110 }
6111 if (has_default) {
6112 if (cases == 1) {
6113 // No matches. Done with the subject:
6114 ADDOP(c, POP_TOP);
6115 }
6116 // A trailing "case _" is common, and lets us save a bit of redundant
6117 // pushing and popping in the loop above:
6118 m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6119 SET_LOC(c, m->pattern);
6120 if (m->guard) {
6121 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0));
6122 }
6123 VISIT_SEQ(c, stmt, m->body);
6124 }
6125 compiler_use_next_block(c, end);
6126 return 1;
6127}
6128
6129
6130#undef WILDCARD_CHECK
6131
6132
Thomas Wouters89f507f2006-12-13 04:49:30 +00006133/* End of the compiler section, beginning of the assembler section */
6134
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006135/* do depth-first search of basic block graph, starting with block.
T. Wouters99b54d62019-09-12 07:05:33 -07006136 post records the block indices in post-order.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006137
6138 XXX must handle implicit jumps from one block to next
6139*/
6140
Thomas Wouters89f507f2006-12-13 04:49:30 +00006141struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006142 PyObject *a_bytecode; /* string containing bytecode */
6143 int a_offset; /* offset into bytecode */
6144 int a_nblocks; /* number of reachable blocks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006145 PyObject *a_lnotab; /* string containing lnotab */
6146 int a_lnotab_off; /* offset into lnotab */
Mark Shannon877df852020-11-12 09:43:29 +00006147 int a_prevlineno; /* lineno of last emitted line in line table */
6148 int a_lineno; /* lineno of last emitted instruction */
6149 int a_lineno_start; /* bytecode start offset of current lineno */
Mark Shannoncc75ab72020-11-12 19:49:33 +00006150 basicblock *a_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006151};
6152
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006153Py_LOCAL_INLINE(void)
6154stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006155{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02006156 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
Mark Shannonfee55262019-11-21 09:11:43 +00006157 if (b->b_startdepth < depth && b->b_startdepth < 100) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006158 assert(b->b_startdepth < 0);
6159 b->b_startdepth = depth;
6160 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02006161 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006162}
6163
6164/* Find the flow path that needs the largest stack. We assume that
6165 * cycles in the flow graph have no net effect on the stack depth.
6166 */
6167static int
6168stackdepth(struct compiler *c)
6169{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006170 basicblock *b, *entryblock = NULL;
6171 basicblock **stack, **sp;
6172 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006173 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006174 b->b_startdepth = INT_MIN;
6175 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006176 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006177 }
6178 if (!entryblock)
6179 return 0;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006180 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
6181 if (!stack) {
6182 PyErr_NoMemory();
6183 return -1;
6184 }
6185
6186 sp = stack;
6187 stackdepth_push(&sp, entryblock, 0);
6188 while (sp != stack) {
6189 b = *--sp;
6190 int depth = b->b_startdepth;
6191 assert(depth >= 0);
6192 basicblock *next = b->b_next;
6193 for (int i = 0; i < b->b_iused; i++) {
6194 struct instr *instr = &b->b_instr[i];
6195 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
6196 if (effect == PY_INVALID_STACK_EFFECT) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01006197 PyErr_Format(PyExc_SystemError,
6198 "compiler stack_effect(opcode=%d, arg=%i) failed",
6199 instr->i_opcode, instr->i_oparg);
6200 return -1;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006201 }
6202 int new_depth = depth + effect;
6203 if (new_depth > maxdepth) {
6204 maxdepth = new_depth;
6205 }
6206 assert(depth >= 0); /* invalid code or bug in stackdepth() */
Mark Shannon582aaf12020-08-04 17:30:11 +01006207 if (is_jump(instr)) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006208 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
6209 assert(effect != PY_INVALID_STACK_EFFECT);
6210 int target_depth = depth + effect;
6211 if (target_depth > maxdepth) {
6212 maxdepth = target_depth;
6213 }
6214 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006215 stackdepth_push(&sp, instr->i_target, target_depth);
6216 }
6217 depth = new_depth;
6218 if (instr->i_opcode == JUMP_ABSOLUTE ||
6219 instr->i_opcode == JUMP_FORWARD ||
6220 instr->i_opcode == RETURN_VALUE ||
Mark Shannonfee55262019-11-21 09:11:43 +00006221 instr->i_opcode == RAISE_VARARGS ||
6222 instr->i_opcode == RERAISE)
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006223 {
6224 /* remaining code is dead */
6225 next = NULL;
6226 break;
6227 }
6228 }
6229 if (next != NULL) {
Mark Shannon266b4622020-11-17 19:30:14 +00006230 assert(b->b_nofallthrough == 0);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006231 stackdepth_push(&sp, next, depth);
6232 }
6233 }
6234 PyObject_Free(stack);
6235 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006236}
6237
6238static int
6239assemble_init(struct assembler *a, int nblocks, int firstlineno)
6240{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006241 memset(a, 0, sizeof(struct assembler));
Mark Shannon877df852020-11-12 09:43:29 +00006242 a->a_prevlineno = a->a_lineno = firstlineno;
Mark Shannonfd009e62020-11-13 12:53:53 +00006243 a->a_lnotab = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006244 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006245 if (a->a_bytecode == NULL) {
6246 goto error;
6247 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006248 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006249 if (a->a_lnotab == NULL) {
6250 goto error;
6251 }
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07006252 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006253 PyErr_NoMemory();
Mark Shannonfd009e62020-11-13 12:53:53 +00006254 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006255 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006256 return 1;
Mark Shannonfd009e62020-11-13 12:53:53 +00006257error:
6258 Py_XDECREF(a->a_bytecode);
6259 Py_XDECREF(a->a_lnotab);
6260 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006261}
6262
6263static void
6264assemble_free(struct assembler *a)
6265{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006266 Py_XDECREF(a->a_bytecode);
6267 Py_XDECREF(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006268}
6269
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006270static int
6271blocksize(basicblock *b)
6272{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006273 int i;
6274 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006276 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006277 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006278 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006279}
6280
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006281static int
Mark Shannon877df852020-11-12 09:43:29 +00006282assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006283{
Mark Shannon877df852020-11-12 09:43:29 +00006284 Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006285 if (a->a_lnotab_off + 2 >= len) {
6286 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
6287 return 0;
6288 }
Pablo Galindo86e322f2021-01-30 13:54:22 +00006289 unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab);
6290 lnotab += a->a_lnotab_off;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006291 a->a_lnotab_off += 2;
Mark Shannon877df852020-11-12 09:43:29 +00006292 *lnotab++ = bdelta;
6293 *lnotab++ = ldelta;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006294 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006295}
6296
Mark Shannon877df852020-11-12 09:43:29 +00006297/* Appends a range to the end of the line number table. See
6298 * Objects/lnotab_notes.txt for the description of the line number table. */
6299
6300static int
6301assemble_line_range(struct assembler *a)
6302{
6303 int ldelta, bdelta;
6304 bdelta = (a->a_offset - a->a_lineno_start) * 2;
6305 if (bdelta == 0) {
6306 return 1;
6307 }
6308 if (a->a_lineno < 0) {
6309 ldelta = -128;
6310 }
6311 else {
6312 ldelta = a->a_lineno - a->a_prevlineno;
6313 a->a_prevlineno = a->a_lineno;
6314 while (ldelta > 127) {
6315 if (!assemble_emit_linetable_pair(a, 0, 127)) {
6316 return 0;
6317 }
6318 ldelta -= 127;
6319 }
6320 while (ldelta < -127) {
6321 if (!assemble_emit_linetable_pair(a, 0, -127)) {
6322 return 0;
6323 }
6324 ldelta += 127;
6325 }
6326 }
6327 assert(-128 <= ldelta && ldelta < 128);
6328 while (bdelta > 254) {
6329 if (!assemble_emit_linetable_pair(a, 254, ldelta)) {
6330 return 0;
6331 }
6332 ldelta = a->a_lineno < 0 ? -128 : 0;
6333 bdelta -= 254;
6334 }
6335 if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) {
6336 return 0;
6337 }
6338 a->a_lineno_start = a->a_offset;
6339 return 1;
6340}
6341
6342static int
6343assemble_lnotab(struct assembler *a, struct instr *i)
6344{
6345 if (i->i_lineno == a->a_lineno) {
6346 return 1;
6347 }
6348 if (!assemble_line_range(a)) {
6349 return 0;
6350 }
6351 a->a_lineno = i->i_lineno;
6352 return 1;
6353}
6354
6355
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006356/* assemble_emit()
6357 Extend the bytecode with a new instruction.
6358 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00006359*/
6360
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006361static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006362assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006363{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006364 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006365 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03006366 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006367
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006368 arg = i->i_oparg;
6369 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006370 if (i->i_lineno && !assemble_lnotab(a, i))
6371 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006372 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006373 if (len > PY_SSIZE_T_MAX / 2)
6374 return 0;
6375 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
6376 return 0;
6377 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03006378 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006379 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006380 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006381 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006382}
6383
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00006384static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006385assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006386{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006387 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006388 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006389 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00006390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006391 /* Compute the size of each block and fixup jump args.
6392 Replace block pointer with position in bytecode. */
6393 do {
6394 totsize = 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006395 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006396 bsize = blocksize(b);
6397 b->b_offset = totsize;
6398 totsize += bsize;
6399 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006400 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006401 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6402 bsize = b->b_offset;
6403 for (i = 0; i < b->b_iused; i++) {
6404 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006405 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006406 /* Relative jumps are computed relative to
6407 the instruction pointer after fetching
6408 the jump instruction.
6409 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006410 bsize += isize;
Mark Shannon582aaf12020-08-04 17:30:11 +01006411 if (is_jump(instr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006412 instr->i_oparg = instr->i_target->b_offset;
Mark Shannon582aaf12020-08-04 17:30:11 +01006413 if (is_relative_jump(instr)) {
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006414 instr->i_oparg -= bsize;
6415 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03006416 instr->i_oparg *= sizeof(_Py_CODEUNIT);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006417 if (instrsize(instr->i_oparg) != isize) {
6418 extended_arg_recompile = 1;
6419 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006420 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006421 }
6422 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00006423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006424 /* XXX: This is an awful hack that could hurt performance, but
6425 on the bright side it should work until we come up
6426 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006428 The issue is that in the first loop blocksize() is called
6429 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006430 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006431 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006433 So we loop until we stop seeing new EXTENDED_ARGs.
6434 The only EXTENDED_ARGs that could be popping up are
6435 ones in jump instructions. So this should converge
6436 fairly quickly.
6437 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006438 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00006439}
6440
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006441static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01006442dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006444 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006445 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006447 tuple = PyTuple_New(size);
6448 if (tuple == NULL)
6449 return NULL;
6450 while (PyDict_Next(dict, &pos, &k, &v)) {
6451 i = PyLong_AS_LONG(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006452 Py_INCREF(k);
6453 assert((i - offset) < size);
6454 assert((i - offset) >= 0);
6455 PyTuple_SET_ITEM(tuple, i - offset, k);
6456 }
6457 return tuple;
6458}
6459
6460static PyObject *
6461consts_dict_keys_inorder(PyObject *dict)
6462{
6463 PyObject *consts, *k, *v;
6464 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
6465
6466 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
6467 if (consts == NULL)
6468 return NULL;
6469 while (PyDict_Next(dict, &pos, &k, &v)) {
6470 i = PyLong_AS_LONG(v);
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +03006471 /* The keys of the dictionary can be tuples wrapping a contant.
6472 * (see compiler_add_o and _PyCode_ConstantKey). In that case
6473 * the object we want is always second. */
6474 if (PyTuple_CheckExact(k)) {
6475 k = PyTuple_GET_ITEM(k, 1);
6476 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006477 Py_INCREF(k);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006478 assert(i < size);
6479 assert(i >= 0);
6480 PyList_SET_ITEM(consts, i, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006481 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006482 return consts;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006483}
6484
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006485static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006486compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006487{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006488 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006489 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006490 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04006491 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006492 if (ste->ste_nested)
6493 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07006494 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006495 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07006496 if (!ste->ste_generator && ste->ste_coroutine)
6497 flags |= CO_COROUTINE;
6498 if (ste->ste_generator && ste->ste_coroutine)
6499 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006500 if (ste->ste_varargs)
6501 flags |= CO_VARARGS;
6502 if (ste->ste_varkeywords)
6503 flags |= CO_VARKEYWORDS;
6504 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006506 /* (Only) inherit compilerflags in PyCF_MASK */
6507 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006508
Pablo Galindo90235812020-03-15 04:29:22 +00006509 if ((IS_TOP_LEVEL_AWAIT(c)) &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07006510 ste->ste_coroutine &&
6511 !ste->ste_generator) {
6512 flags |= CO_COROUTINE;
6513 }
6514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006515 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00006516}
6517
Inada Naokibdb941b2021-02-10 09:20:42 +09006518// Merge *obj* with constant cache.
INADA Naokic2e16072018-11-26 21:23:22 +09006519// Unlike merge_consts_recursive(), this function doesn't work recursively.
6520static int
Inada Naokibdb941b2021-02-10 09:20:42 +09006521merge_const_one(struct compiler *c, PyObject **obj)
INADA Naokic2e16072018-11-26 21:23:22 +09006522{
Inada Naokibdb941b2021-02-10 09:20:42 +09006523 PyObject *key = _PyCode_ConstantKey(*obj);
INADA Naokic2e16072018-11-26 21:23:22 +09006524 if (key == NULL) {
6525 return 0;
6526 }
6527
6528 // t is borrowed reference
6529 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
6530 Py_DECREF(key);
6531 if (t == NULL) {
6532 return 0;
6533 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006534 if (t == key) { // obj is new constant.
INADA Naokic2e16072018-11-26 21:23:22 +09006535 return 1;
6536 }
6537
Inada Naokibdb941b2021-02-10 09:20:42 +09006538 if (PyTuple_CheckExact(t)) {
6539 // t is still borrowed reference
6540 t = PyTuple_GET_ITEM(t, 1);
6541 }
6542
6543 Py_INCREF(t);
6544 Py_DECREF(*obj);
6545 *obj = t;
INADA Naokic2e16072018-11-26 21:23:22 +09006546 return 1;
6547}
6548
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006549static PyCodeObject *
Mark Shannon6e8128f2020-07-30 10:03:00 +01006550makecode(struct compiler *c, struct assembler *a, PyObject *consts)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006551{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006552 PyCodeObject *co = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006553 PyObject *names = NULL;
6554 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006555 PyObject *name = NULL;
6556 PyObject *freevars = NULL;
6557 PyObject *cellvars = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006558 Py_ssize_t nlocals;
6559 int nlocals_int;
6560 int flags;
Pablo Galindocd74e662019-06-01 18:08:04 +01006561 int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006563 names = dict_keys_inorder(c->u->u_names, 0);
6564 varnames = dict_keys_inorder(c->u->u_varnames, 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006565 if (!names || !varnames) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006566 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006567 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006568 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
6569 if (!cellvars)
6570 goto error;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006571 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006572 if (!freevars)
6573 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006574
Inada Naokibdb941b2021-02-10 09:20:42 +09006575 if (!merge_const_one(c, &names) ||
6576 !merge_const_one(c, &varnames) ||
6577 !merge_const_one(c, &cellvars) ||
6578 !merge_const_one(c, &freevars))
INADA Naokic2e16072018-11-26 21:23:22 +09006579 {
6580 goto error;
6581 }
6582
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006583 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01006584 assert(nlocals < INT_MAX);
6585 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
6586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006587 flags = compute_code_flags(c);
6588 if (flags < 0)
6589 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006590
Mark Shannon6e8128f2020-07-30 10:03:00 +01006591 consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
6592 if (consts == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006593 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006594 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006595 if (!merge_const_one(c, &consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006596 Py_DECREF(consts);
INADA Naokic2e16072018-11-26 21:23:22 +09006597 goto error;
6598 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006599
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01006600 posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int);
Pablo Galindocd74e662019-06-01 18:08:04 +01006601 posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +01006602 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006603 maxdepth = stackdepth(c);
6604 if (maxdepth < 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006605 Py_DECREF(consts);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006606 goto error;
6607 }
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006608 co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
Mark Shannon13bc1392020-01-23 09:25:17 +00006609 posonlyargcount, kwonlyargcount, nlocals_int,
Mark Shannon6e8128f2020-07-30 10:03:00 +01006610 maxdepth, flags, a->a_bytecode, consts, names,
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006611 varnames, freevars, cellvars, c->c_filename,
6612 c->u->u_name, c->u->u_firstlineno, a->a_lnotab);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006613 Py_DECREF(consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006614 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006615 Py_XDECREF(names);
6616 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006617 Py_XDECREF(name);
6618 Py_XDECREF(freevars);
6619 Py_XDECREF(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006620 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006621}
6622
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006623
6624/* For debugging purposes only */
6625#if 0
6626static void
6627dump_instr(const struct instr *i)
6628{
Mark Shannon582aaf12020-08-04 17:30:11 +01006629 const char *jrel = (is_relative_jump(instr)) ? "jrel " : "";
6630 const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : "";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006631 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006633 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006634 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006635 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006636 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006637 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
6638 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006639}
6640
6641static void
6642dump_basicblock(const basicblock *b)
6643{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006644 const char *b_return = b->b_return ? "return " : "";
Pablo Galindo60eb9f12020-06-28 01:55:47 +01006645 fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n",
6646 b->b_iused, b->b_startdepth, b->b_offset, b_return);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006647 if (b->b_instr) {
6648 int i;
6649 for (i = 0; i < b->b_iused; i++) {
6650 fprintf(stderr, " [%02d] ", i);
6651 dump_instr(b->b_instr + i);
6652 }
6653 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006654}
6655#endif
6656
Mark Shannon5977a792020-12-02 13:31:40 +00006657
6658static int
6659normalize_basic_block(basicblock *bb);
6660
Mark Shannon6e8128f2020-07-30 10:03:00 +01006661static int
6662optimize_cfg(struct assembler *a, PyObject *consts);
6663
Mark Shannon5977a792020-12-02 13:31:40 +00006664static int
6665ensure_exits_have_lineno(struct compiler *c);
6666
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006667static PyCodeObject *
6668assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006670 basicblock *b, *entryblock;
6671 struct assembler a;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006672 int j, nblocks;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006673 PyCodeObject *co = NULL;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006674 PyObject *consts = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006676 /* Make sure every block that falls off the end returns None.
6677 XXX NEXT_BLOCK() isn't quite right, because if the last
6678 block ends with a jump or return b_next shouldn't set.
6679 */
6680 if (!c->u->u_curblock->b_return) {
Mark Shannon877df852020-11-12 09:43:29 +00006681 c->u->u_lineno = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006682 if (addNone)
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006683 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006684 ADDOP(c, RETURN_VALUE);
6685 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006686
Mark Shannon5977a792020-12-02 13:31:40 +00006687 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
6688 if (normalize_basic_block(b)) {
Alex Henrie503627f2021-03-02 03:20:25 -07006689 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00006690 }
6691 }
6692
6693 if (ensure_exits_have_lineno(c)) {
Alex Henrie503627f2021-03-02 03:20:25 -07006694 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00006695 }
6696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006697 nblocks = 0;
6698 entryblock = NULL;
6699 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6700 nblocks++;
6701 entryblock = b;
6702 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006704 /* Set firstlineno if it wasn't explicitly set. */
6705 if (!c->u->u_firstlineno) {
Ned Deilydc35cda2016-08-17 17:18:33 -04006706 if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006707 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
Mark Shannon877df852020-11-12 09:43:29 +00006708 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006709 c->u->u_firstlineno = 1;
6710 }
Mark Shannon5977a792020-12-02 13:31:40 +00006711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006712 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
6713 goto error;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006714 a.a_entry = entryblock;
6715 a.a_nblocks = nblocks;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006716
Mark Shannon6e8128f2020-07-30 10:03:00 +01006717 consts = consts_dict_keys_inorder(c->u->u_consts);
6718 if (consts == NULL) {
6719 goto error;
6720 }
6721 if (optimize_cfg(&a, consts)) {
6722 goto error;
6723 }
6724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006725 /* Can't modify the bytecode after computing jump offsets. */
6726 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00006727
Mark Shannoncc75ab72020-11-12 19:49:33 +00006728 /* Emit code. */
6729 for(b = entryblock; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006730 for (j = 0; j < b->b_iused; j++)
6731 if (!assemble_emit(&a, &b->b_instr[j]))
6732 goto error;
6733 }
Mark Shannon877df852020-11-12 09:43:29 +00006734 if (!assemble_line_range(&a)) {
6735 return 0;
6736 }
6737 /* Emit sentinel at end of line number table */
6738 if (!assemble_emit_linetable_pair(&a, 255, -128)) {
6739 goto error;
6740 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00006741
Inada Naokibdb941b2021-02-10 09:20:42 +09006742 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006743 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09006744 }
6745 if (!merge_const_one(c, &a.a_lnotab)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006746 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09006747 }
6748 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) {
6749 goto error;
6750 }
6751 if (!merge_const_one(c, &a.a_bytecode)) {
6752 goto error;
6753 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006754
Mark Shannon6e8128f2020-07-30 10:03:00 +01006755 co = makecode(c, &a, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006756 error:
Mark Shannon6e8128f2020-07-30 10:03:00 +01006757 Py_XDECREF(consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006758 assemble_free(&a);
6759 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006760}
Georg Brandl8334fd92010-12-04 10:26:46 +00006761
6762#undef PyAST_Compile
Benjamin Petersone5024512018-09-12 12:06:42 -07006763PyCodeObject *
Georg Brandl8334fd92010-12-04 10:26:46 +00006764PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
6765 PyArena *arena)
6766{
6767 return PyAST_CompileEx(mod, filename, flags, -1, arena);
6768}
Mark Shannon6e8128f2020-07-30 10:03:00 +01006769
6770
6771/* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
6772 with LOAD_CONST (c1, c2, ... cn).
6773 The consts table must still be in list form so that the
6774 new constant (c1, c2, ... cn) can be appended.
6775 Called with codestr pointing to the first LOAD_CONST.
6776*/
6777static int
6778fold_tuple_on_constants(struct instr *inst,
6779 int n, PyObject *consts)
6780{
6781 /* Pre-conditions */
6782 assert(PyList_CheckExact(consts));
6783 assert(inst[n].i_opcode == BUILD_TUPLE);
6784 assert(inst[n].i_oparg == n);
6785
6786 for (int i = 0; i < n; i++) {
6787 if (inst[i].i_opcode != LOAD_CONST) {
6788 return 0;
6789 }
6790 }
6791
6792 /* Buildup new tuple of constants */
6793 PyObject *newconst = PyTuple_New(n);
6794 if (newconst == NULL) {
6795 return -1;
6796 }
6797 for (int i = 0; i < n; i++) {
6798 int arg = inst[i].i_oparg;
6799 PyObject *constant = PyList_GET_ITEM(consts, arg);
6800 Py_INCREF(constant);
6801 PyTuple_SET_ITEM(newconst, i, constant);
6802 }
6803 Py_ssize_t index = PyList_GET_SIZE(consts);
Victor Stinner71f2ff42020-09-23 14:06:55 +02006804 if ((size_t)index >= (size_t)INT_MAX - 1) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006805 Py_DECREF(newconst);
6806 PyErr_SetString(PyExc_OverflowError, "too many constants");
6807 return -1;
6808 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006809 if (PyList_Append(consts, newconst)) {
6810 Py_DECREF(newconst);
6811 return -1;
6812 }
6813 Py_DECREF(newconst);
6814 for (int i = 0; i < n; i++) {
6815 inst[i].i_opcode = NOP;
6816 }
6817 inst[n].i_opcode = LOAD_CONST;
Victor Stinner71f2ff42020-09-23 14:06:55 +02006818 inst[n].i_oparg = (int)index;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006819 return 0;
6820}
6821
Mark Shannon28b75c82020-12-23 11:43:10 +00006822
6823static int
6824eliminate_jump_to_jump(basicblock *bb, int opcode) {
6825 assert (bb->b_iused > 0);
6826 struct instr *inst = &bb->b_instr[bb->b_iused-1];
6827 assert (is_jump(inst));
6828 assert (inst->i_target->b_iused > 0);
6829 struct instr *target = &inst->i_target->b_instr[0];
6830 if (inst->i_target == target->i_target) {
6831 /* Nothing to do */
6832 return 0;
6833 }
6834 int lineno = target->i_lineno;
6835 if (add_jump_to_block(bb, opcode, lineno, target->i_target) == 0) {
6836 return -1;
6837 }
6838 assert (bb->b_iused >= 2);
6839 bb->b_instr[bb->b_iused-2].i_opcode = NOP;
6840 return 0;
6841}
6842
Mark Shannoncc75ab72020-11-12 19:49:33 +00006843/* Maximum size of basic block that should be copied in optimizer */
6844#define MAX_COPY_SIZE 4
Mark Shannon6e8128f2020-07-30 10:03:00 +01006845
6846/* Optimization */
6847static int
6848optimize_basic_block(basicblock *bb, PyObject *consts)
6849{
6850 assert(PyList_CheckExact(consts));
6851 struct instr nop;
6852 nop.i_opcode = NOP;
6853 struct instr *target;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006854 for (int i = 0; i < bb->b_iused; i++) {
6855 struct instr *inst = &bb->b_instr[i];
6856 int oparg = inst->i_oparg;
6857 int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01006858 if (is_jump(inst)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006859 /* Skip over empty basic blocks. */
6860 while (inst->i_target->b_iused == 0) {
6861 inst->i_target = inst->i_target->b_next;
6862 }
6863 target = &inst->i_target->b_instr[0];
6864 }
6865 else {
6866 target = &nop;
6867 }
6868 switch (inst->i_opcode) {
Mark Shannon266b4622020-11-17 19:30:14 +00006869 /* Remove LOAD_CONST const; conditional jump */
Mark Shannon6e8128f2020-07-30 10:03:00 +01006870 case LOAD_CONST:
Mark Shannon266b4622020-11-17 19:30:14 +00006871 {
6872 PyObject* cnt;
6873 int is_true;
6874 int jump_if_true;
6875 switch(nextop) {
6876 case POP_JUMP_IF_FALSE:
6877 case POP_JUMP_IF_TRUE:
6878 cnt = PyList_GET_ITEM(consts, oparg);
6879 is_true = PyObject_IsTrue(cnt);
6880 if (is_true == -1) {
6881 goto error;
6882 }
6883 inst->i_opcode = NOP;
6884 jump_if_true = nextop == POP_JUMP_IF_TRUE;
6885 if (is_true == jump_if_true) {
6886 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
6887 bb->b_nofallthrough = 1;
6888 }
6889 else {
6890 bb->b_instr[i+1].i_opcode = NOP;
6891 }
6892 break;
6893 case JUMP_IF_FALSE_OR_POP:
6894 case JUMP_IF_TRUE_OR_POP:
6895 cnt = PyList_GET_ITEM(consts, oparg);
6896 is_true = PyObject_IsTrue(cnt);
6897 if (is_true == -1) {
6898 goto error;
6899 }
6900 jump_if_true = nextop == JUMP_IF_TRUE_OR_POP;
6901 if (is_true == jump_if_true) {
6902 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
6903 bb->b_nofallthrough = 1;
6904 }
6905 else {
6906 inst->i_opcode = NOP;
6907 bb->b_instr[i+1].i_opcode = NOP;
6908 }
6909 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006910 }
6911 break;
Mark Shannon266b4622020-11-17 19:30:14 +00006912 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006913
6914 /* Try to fold tuples of constants.
6915 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
6916 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
6917 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
6918 case BUILD_TUPLE:
6919 if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
6920 switch(oparg) {
6921 case 1:
6922 inst->i_opcode = NOP;
6923 bb->b_instr[i+1].i_opcode = NOP;
6924 break;
6925 case 2:
6926 inst->i_opcode = ROT_TWO;
6927 bb->b_instr[i+1].i_opcode = NOP;
6928 break;
6929 case 3:
6930 inst->i_opcode = ROT_THREE;
6931 bb->b_instr[i+1].i_opcode = ROT_TWO;
6932 }
6933 break;
6934 }
6935 if (i >= oparg) {
6936 if (fold_tuple_on_constants(inst-oparg, oparg, consts)) {
6937 goto error;
6938 }
6939 }
6940 break;
6941
6942 /* Simplify conditional jump to conditional jump where the
6943 result of the first test implies the success of a similar
6944 test or the failure of the opposite test.
6945 Arises in code like:
6946 "a and b or c"
6947 "(a and b) and c"
6948 "(a or b) or c"
6949 "(a or b) and c"
6950 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
6951 --> x:JUMP_IF_FALSE_OR_POP z
6952 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
6953 --> x:POP_JUMP_IF_FALSE y+1
6954 where y+1 is the instruction following the second test.
6955 */
6956 case JUMP_IF_FALSE_OR_POP:
6957 switch(target->i_opcode) {
6958 case POP_JUMP_IF_FALSE:
Mark Shannon28b75c82020-12-23 11:43:10 +00006959 if (inst->i_lineno == target->i_lineno) {
6960 *inst = *target;
6961 i--;
6962 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006963 break;
6964 case JUMP_ABSOLUTE:
6965 case JUMP_FORWARD:
6966 case JUMP_IF_FALSE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00006967 if (inst->i_lineno == target->i_lineno &&
6968 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00006969 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00006970 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00006971 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006972 break;
6973 case JUMP_IF_TRUE_OR_POP:
6974 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00006975 if (inst->i_lineno == target->i_lineno) {
6976 inst->i_opcode = POP_JUMP_IF_FALSE;
6977 inst->i_target = inst->i_target->b_next;
6978 --i;
6979 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006980 break;
6981 }
6982 break;
6983
6984 case JUMP_IF_TRUE_OR_POP:
6985 switch(target->i_opcode) {
6986 case POP_JUMP_IF_TRUE:
Mark Shannon28b75c82020-12-23 11:43:10 +00006987 if (inst->i_lineno == target->i_lineno) {
6988 *inst = *target;
6989 i--;
6990 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006991 break;
6992 case JUMP_ABSOLUTE:
6993 case JUMP_FORWARD:
6994 case JUMP_IF_TRUE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00006995 if (inst->i_lineno == target->i_lineno &&
6996 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00006997 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00006998 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00006999 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007000 break;
7001 case JUMP_IF_FALSE_OR_POP:
7002 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00007003 if (inst->i_lineno == target->i_lineno) {
7004 inst->i_opcode = POP_JUMP_IF_TRUE;
7005 inst->i_target = inst->i_target->b_next;
7006 --i;
7007 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007008 break;
7009 }
7010 break;
7011
7012 case POP_JUMP_IF_FALSE:
7013 switch(target->i_opcode) {
7014 case JUMP_ABSOLUTE:
7015 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007016 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00007017 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007018 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007019 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007020 break;
7021 }
7022 break;
7023
7024 case POP_JUMP_IF_TRUE:
7025 switch(target->i_opcode) {
7026 case JUMP_ABSOLUTE:
7027 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007028 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00007029 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007030 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007031 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007032 break;
7033 }
7034 break;
7035
7036 case JUMP_ABSOLUTE:
7037 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007038 assert (i == bb->b_iused-1);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007039 switch(target->i_opcode) {
7040 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007041 if (eliminate_jump_to_jump(bb, inst->i_opcode)) {
7042 goto error;
Mark Shannon266b4622020-11-17 19:30:14 +00007043 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007044 break;
Mark Shannon28b75c82020-12-23 11:43:10 +00007045
Mark Shannon6e8128f2020-07-30 10:03:00 +01007046 case JUMP_ABSOLUTE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007047 if (eliminate_jump_to_jump(bb, JUMP_ABSOLUTE)) {
7048 goto error;
Mark Shannon266b4622020-11-17 19:30:14 +00007049 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007050 break;
Mark Shannon28b75c82020-12-23 11:43:10 +00007051 default:
7052 if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) {
7053 basicblock *to_copy = inst->i_target;
7054 inst->i_opcode = NOP;
7055 for (i = 0; i < to_copy->b_iused; i++) {
7056 int index = compiler_next_instr(bb);
7057 if (index < 0) {
7058 return -1;
7059 }
7060 bb->b_instr[index] = to_copy->b_instr[i];
7061 }
7062 bb->b_exit = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007063 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007064 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007065 }
7066 }
7067 return 0;
7068error:
7069 return -1;
7070}
7071
7072
7073static void
Mark Shannon1659ad12021-01-13 15:05:04 +00007074clean_basic_block(basicblock *bb, int prev_lineno) {
7075 /* Remove NOPs when legal to do so. */
Mark Shannon6e8128f2020-07-30 10:03:00 +01007076 int dest = 0;
7077 for (int src = 0; src < bb->b_iused; src++) {
Mark Shannon877df852020-11-12 09:43:29 +00007078 int lineno = bb->b_instr[src].i_lineno;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007079 if (bb->b_instr[src].i_opcode == NOP) {
Mark Shannon266b4622020-11-17 19:30:14 +00007080 /* Eliminate no-op if it doesn't have a line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007081 if (lineno < 0) {
7082 continue;
7083 }
Mark Shannon266b4622020-11-17 19:30:14 +00007084 /* or, if the previous instruction had the same line number. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007085 if (prev_lineno == lineno) {
7086 continue;
7087 }
Mark Shannon266b4622020-11-17 19:30:14 +00007088 /* or, if the next instruction has same line number or no line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007089 if (src < bb->b_iused - 1) {
7090 int next_lineno = bb->b_instr[src+1].i_lineno;
7091 if (next_lineno < 0 || next_lineno == lineno) {
7092 bb->b_instr[src+1].i_lineno = lineno;
7093 continue;
Mark Shannon877df852020-11-12 09:43:29 +00007094 }
7095 }
Mark Shannon266b4622020-11-17 19:30:14 +00007096 else {
7097 basicblock* next = bb->b_next;
7098 while (next && next->b_iused == 0) {
7099 next = next->b_next;
7100 }
7101 /* or if last instruction in BB and next BB has same line number */
7102 if (next) {
7103 if (lineno == next->b_instr[0].i_lineno) {
7104 continue;
7105 }
7106 }
7107 }
7108
Mark Shannon6e8128f2020-07-30 10:03:00 +01007109 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007110 if (dest != src) {
7111 bb->b_instr[dest] = bb->b_instr[src];
7112 }
7113 dest++;
7114 prev_lineno = lineno;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007115 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007116 assert(dest <= bb->b_iused);
7117 bb->b_iused = dest;
7118}
7119
Mark Shannon266b4622020-11-17 19:30:14 +00007120static int
7121normalize_basic_block(basicblock *bb) {
7122 /* Mark blocks as exit and/or nofallthrough.
7123 Raise SystemError if CFG is malformed. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007124 for (int i = 0; i < bb->b_iused; i++) {
7125 switch(bb->b_instr[i].i_opcode) {
7126 case RETURN_VALUE:
7127 case RAISE_VARARGS:
7128 case RERAISE:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007129 bb->b_exit = 1;
Mark Shannon5977a792020-12-02 13:31:40 +00007130 bb->b_nofallthrough = 1;
7131 break;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007132 case JUMP_ABSOLUTE:
7133 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007134 bb->b_nofallthrough = 1;
Mark Shannon266b4622020-11-17 19:30:14 +00007135 /* fall through */
7136 case POP_JUMP_IF_FALSE:
7137 case POP_JUMP_IF_TRUE:
7138 case JUMP_IF_FALSE_OR_POP:
7139 case JUMP_IF_TRUE_OR_POP:
Mark Shannon5977a792020-12-02 13:31:40 +00007140 case FOR_ITER:
Mark Shannon266b4622020-11-17 19:30:14 +00007141 if (i != bb->b_iused-1) {
7142 PyErr_SetString(PyExc_SystemError, "malformed control flow graph.");
7143 return -1;
7144 }
Mark Shannon5977a792020-12-02 13:31:40 +00007145 /* Skip over empty basic blocks. */
7146 while (bb->b_instr[i].i_target->b_iused == 0) {
7147 bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next;
7148 }
7149
Mark Shannoncc75ab72020-11-12 19:49:33 +00007150 }
7151 }
Mark Shannon266b4622020-11-17 19:30:14 +00007152 return 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007153}
7154
Mark Shannon6e8128f2020-07-30 10:03:00 +01007155static int
7156mark_reachable(struct assembler *a) {
7157 basicblock **stack, **sp;
7158 sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks);
7159 if (stack == NULL) {
7160 return -1;
7161 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007162 a->a_entry->b_predecessors = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007163 *sp++ = a->a_entry;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007164 while (sp > stack) {
7165 basicblock *b = *(--sp);
Mark Shannon3bd60352021-01-13 12:05:43 +00007166 if (b->b_next && !b->b_nofallthrough) {
7167 if (b->b_next->b_predecessors == 0) {
7168 *sp++ = b->b_next;
7169 }
7170 b->b_next->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007171 }
7172 for (int i = 0; i < b->b_iused; i++) {
7173 basicblock *target;
Mark Shannon582aaf12020-08-04 17:30:11 +01007174 if (is_jump(&b->b_instr[i])) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007175 target = b->b_instr[i].i_target;
Mark Shannon3bd60352021-01-13 12:05:43 +00007176 if (target->b_predecessors == 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007177 *sp++ = target;
7178 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007179 target->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007180 }
7181 }
7182 }
7183 PyObject_Free(stack);
7184 return 0;
7185}
7186
Mark Shannon3bd60352021-01-13 12:05:43 +00007187static void
7188eliminate_empty_basic_blocks(basicblock *entry) {
7189 /* Eliminate empty blocks */
7190 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7191 basicblock *next = b->b_next;
7192 if (next) {
7193 while (next->b_iused == 0 && next->b_next) {
7194 next = next->b_next;
7195 }
7196 b->b_next = next;
7197 }
7198 }
7199 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7200 if (b->b_iused == 0) {
7201 continue;
7202 }
7203 if (is_jump(&b->b_instr[b->b_iused-1])) {
7204 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7205 while (target->b_iused == 0) {
7206 target = target->b_next;
7207 }
7208 b->b_instr[b->b_iused-1].i_target = target;
7209 }
7210 }
7211}
7212
7213
Mark Shannon5977a792020-12-02 13:31:40 +00007214/* If an instruction has no line number, but it's predecessor in the BB does,
Mark Shannon3bd60352021-01-13 12:05:43 +00007215 * then copy the line number. If a successor block has no line number, and only
7216 * one predecessor, then inherit the line number.
7217 * This ensures that all exit blocks (with one predecessor) receive a line number.
7218 * Also reduces the size of the line number table,
Mark Shannon5977a792020-12-02 13:31:40 +00007219 * but has no impact on the generated line number events.
7220 */
7221static void
Mark Shannon3bd60352021-01-13 12:05:43 +00007222propogate_line_numbers(struct assembler *a) {
Mark Shannon5977a792020-12-02 13:31:40 +00007223 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007224 if (b->b_iused == 0) {
7225 continue;
7226 }
Mark Shannon5977a792020-12-02 13:31:40 +00007227 int prev_lineno = -1;
7228 for (int i = 0; i < b->b_iused; i++) {
7229 if (b->b_instr[i].i_lineno < 0) {
7230 b->b_instr[i].i_lineno = prev_lineno;
7231 }
7232 else {
7233 prev_lineno = b->b_instr[i].i_lineno;
7234 }
7235 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007236 if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) {
7237 assert(b->b_next->b_iused);
7238 if (b->b_next->b_instr[0].i_lineno < 0) {
7239 b->b_next->b_instr[0].i_lineno = prev_lineno;
7240 }
7241 }
7242 if (is_jump(&b->b_instr[b->b_iused-1])) {
7243 switch (b->b_instr[b->b_iused-1].i_opcode) {
7244 /* Note: Only actual jumps, not exception handlers */
7245 case SETUP_ASYNC_WITH:
7246 case SETUP_WITH:
7247 case SETUP_FINALLY:
7248 continue;
7249 }
7250 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7251 if (target->b_predecessors == 1) {
7252 if (target->b_instr[0].i_lineno < 0) {
7253 target->b_instr[0].i_lineno = prev_lineno;
7254 }
7255 }
7256 }
Mark Shannon5977a792020-12-02 13:31:40 +00007257 }
7258}
7259
7260/* Perform optimizations on a control flow graph.
Mark Shannon6e8128f2020-07-30 10:03:00 +01007261 The consts object should still be in list form to allow new constants
7262 to be appended.
7263
7264 All transformations keep the code size the same or smaller.
7265 For those that reduce size, the gaps are initially filled with
7266 NOPs. Later those NOPs are removed.
7267*/
7268
7269static int
7270optimize_cfg(struct assembler *a, PyObject *consts)
7271{
Mark Shannoncc75ab72020-11-12 19:49:33 +00007272 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannoncc75ab72020-11-12 19:49:33 +00007273 if (optimize_basic_block(b, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007274 return -1;
7275 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007276 clean_basic_block(b, -1);
Mark Shannon3bd60352021-01-13 12:05:43 +00007277 assert(b->b_predecessors == 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007278 }
7279 if (mark_reachable(a)) {
7280 return -1;
7281 }
7282 /* Delete unreachable instructions */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007283 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007284 if (b->b_predecessors == 0) {
Mark Shannoncc75ab72020-11-12 19:49:33 +00007285 b->b_iused = 0;
Om Gc71581c2020-12-16 17:48:05 +05307286 b->b_nofallthrough = 0;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007287 }
7288 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007289 basicblock *pred = NULL;
7290 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7291 int prev_lineno = -1;
7292 if (pred && pred->b_iused) {
7293 prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno;
7294 }
7295 clean_basic_block(b, prev_lineno);
7296 pred = b->b_nofallthrough ? NULL : b;
7297 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007298 eliminate_empty_basic_blocks(a->a_entry);
Om Gc71581c2020-12-16 17:48:05 +05307299 /* Delete jump instructions made redundant by previous step. If a non-empty
7300 block ends with a jump instruction, check if the next non-empty block
7301 reached through normal flow control is the target of that jump. If it
7302 is, then the jump instruction is redundant and can be deleted.
7303 */
Mark Shannon3bd60352021-01-13 12:05:43 +00007304 int maybe_empty_blocks = 0;
Om Gc71581c2020-12-16 17:48:05 +05307305 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7306 if (b->b_iused > 0) {
7307 struct instr *b_last_instr = &b->b_instr[b->b_iused - 1];
Mark Shannon802b6452021-02-02 14:59:15 +00007308 if (b_last_instr->i_opcode == JUMP_ABSOLUTE ||
Om Gc71581c2020-12-16 17:48:05 +05307309 b_last_instr->i_opcode == JUMP_FORWARD) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007310 if (b_last_instr->i_target == b->b_next) {
7311 assert(b->b_next->b_iused);
Om Gc71581c2020-12-16 17:48:05 +05307312 b->b_nofallthrough = 0;
Mark Shannon802b6452021-02-02 14:59:15 +00007313 b_last_instr->i_opcode = NOP;
7314 clean_basic_block(b, -1);
7315 maybe_empty_blocks = 1;
Om Gc71581c2020-12-16 17:48:05 +05307316 }
7317 }
7318 }
7319 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007320 if (maybe_empty_blocks) {
7321 eliminate_empty_basic_blocks(a->a_entry);
7322 }
7323 propogate_line_numbers(a);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007324 return 0;
7325}
7326
Mark Shannon5977a792020-12-02 13:31:40 +00007327static inline int
7328is_exit_without_lineno(basicblock *b) {
7329 return b->b_exit && b->b_instr[0].i_lineno < 0;
7330}
7331
7332/* PEP 626 mandates that the f_lineno of a frame is correct
7333 * after a frame terminates. It would be prohibitively expensive
7334 * to continuously update the f_lineno field at runtime,
7335 * so we make sure that all exiting instruction (raises and returns)
7336 * have a valid line number, allowing us to compute f_lineno lazily.
7337 * We can do this by duplicating the exit blocks without line number
7338 * so that none have more than one predecessor. We can then safely
7339 * copy the line number from the sole predecessor block.
7340 */
7341static int
7342ensure_exits_have_lineno(struct compiler *c)
7343{
Mark Shannoneaccc122020-12-04 15:22:12 +00007344 basicblock *entry = NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00007345 /* Copy all exit blocks without line number that are targets of a jump.
7346 */
7347 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7348 if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) {
7349 switch (b->b_instr[b->b_iused-1].i_opcode) {
7350 /* Note: Only actual jumps, not exception handlers */
7351 case SETUP_ASYNC_WITH:
7352 case SETUP_WITH:
7353 case SETUP_FINALLY:
7354 continue;
7355 }
7356 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7357 if (is_exit_without_lineno(target)) {
7358 basicblock *new_target = compiler_copy_block(c, target);
7359 if (new_target == NULL) {
7360 return -1;
7361 }
7362 new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7363 b->b_instr[b->b_iused-1].i_target = new_target;
7364 }
7365 }
Mark Shannoneaccc122020-12-04 15:22:12 +00007366 entry = b;
7367 }
7368 assert(entry != NULL);
7369 if (is_exit_without_lineno(entry)) {
7370 entry->b_instr[0].i_lineno = c->u->u_firstlineno;
Mark Shannon5977a792020-12-02 13:31:40 +00007371 }
Mark Shannonee9f98d2021-01-05 12:04:10 +00007372 /* Eliminate empty blocks */
7373 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7374 while (b->b_next && b->b_next->b_iused == 0) {
7375 b->b_next = b->b_next->b_next;
7376 }
7377 }
Mark Shannon5977a792020-12-02 13:31:40 +00007378 /* Any remaining reachable exit blocks without line number can only be reached by
7379 * fall through, and thus can only have a single predecessor */
7380 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7381 if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) {
7382 if (is_exit_without_lineno(b->b_next)) {
7383 assert(b->b_next->b_iused > 0);
7384 b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7385 }
7386 }
7387 }
7388 return 0;
7389}
7390
7391
Mark Shannon6e8128f2020-07-30 10:03:00 +01007392/* Retained for API compatibility.
7393 * Optimization is now done in optimize_cfg */
7394
7395PyObject *
7396PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
7397 PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
7398{
7399 Py_INCREF(code);
7400 return code;
7401}