blob: 942614fdab8502d39cdb22783318545c563724d1 [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
Georg Brandl8334fd92010-12-04 10:26:46 +00003358 if (c->c_optimize)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003359 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003360 if (s->v.Assert.test->kind == Tuple_kind &&
Serhiy Storchakad31e7732018-10-21 10:09:39 +03003361 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0)
3362 {
3363 if (!compiler_warn(c, "assertion is always true, "
3364 "perhaps remove parentheses?"))
3365 {
Victor Stinner14e461d2013-08-26 22:28:21 +02003366 return 0;
3367 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003368 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003369 end = compiler_new_block(c);
3370 if (end == NULL)
3371 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003372 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
3373 return 0;
Zackery Spytzce6a0702019-08-25 03:44:09 -06003374 ADDOP(c, LOAD_ASSERTION_ERROR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003375 if (s->v.Assert.msg) {
3376 VISIT(c, expr, s->v.Assert.msg);
3377 ADDOP_I(c, CALL_FUNCTION, 1);
3378 }
3379 ADDOP_I(c, RAISE_VARARGS, 1);
3380 compiler_use_next_block(c, end);
3381 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003382}
3383
3384static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003385compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3386{
3387 if (c->c_interactive && c->c_nestlevel <= 1) {
3388 VISIT(c, expr, value);
3389 ADDOP(c, PRINT_EXPR);
3390 return 1;
3391 }
3392
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003393 if (value->kind == Constant_kind) {
Victor Stinner15a30952016-02-08 22:45:06 +01003394 /* ignore constant statement */
Mark Shannon877df852020-11-12 09:43:29 +00003395 ADDOP(c, NOP);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003396 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003397 }
3398
3399 VISIT(c, expr, value);
3400 ADDOP(c, POP_TOP);
3401 return 1;
3402}
3403
3404static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003405compiler_visit_stmt(struct compiler *c, stmt_ty s)
3406{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003407 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003409 /* Always assign a lineno to the next instruction for a stmt. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003410 SET_LOC(c, s);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003412 switch (s->kind) {
3413 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04003414 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003415 case ClassDef_kind:
3416 return compiler_class(c, s);
3417 case Return_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003418 return compiler_return(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003419 case Delete_kind:
3420 VISIT_SEQ(c, expr, s->v.Delete.targets)
3421 break;
3422 case Assign_kind:
3423 n = asdl_seq_LEN(s->v.Assign.targets);
3424 VISIT(c, expr, s->v.Assign.value);
3425 for (i = 0; i < n; i++) {
3426 if (i < n - 1)
3427 ADDOP(c, DUP_TOP);
3428 VISIT(c, expr,
3429 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3430 }
3431 break;
3432 case AugAssign_kind:
3433 return compiler_augassign(c, s);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003434 case AnnAssign_kind:
3435 return compiler_annassign(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436 case For_kind:
3437 return compiler_for(c, s);
3438 case While_kind:
3439 return compiler_while(c, s);
3440 case If_kind:
3441 return compiler_if(c, s);
Brandt Bucher145bf262021-02-26 14:51:55 -08003442 case Match_kind:
3443 return compiler_match(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003444 case Raise_kind:
3445 n = 0;
3446 if (s->v.Raise.exc) {
3447 VISIT(c, expr, s->v.Raise.exc);
3448 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003449 if (s->v.Raise.cause) {
3450 VISIT(c, expr, s->v.Raise.cause);
3451 n++;
3452 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003453 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01003454 ADDOP_I(c, RAISE_VARARGS, (int)n);
Mark Shannon266b4622020-11-17 19:30:14 +00003455 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003456 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003457 case Try_kind:
3458 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003459 case Assert_kind:
3460 return compiler_assert(c, s);
3461 case Import_kind:
3462 return compiler_import(c, s);
3463 case ImportFrom_kind:
3464 return compiler_from_import(c, s);
3465 case Global_kind:
3466 case Nonlocal_kind:
3467 break;
3468 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003469 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003470 case Pass_kind:
Mark Shannon877df852020-11-12 09:43:29 +00003471 ADDOP(c, NOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003472 break;
3473 case Break_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003474 return compiler_break(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003475 case Continue_kind:
3476 return compiler_continue(c);
3477 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003478 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04003479 case AsyncFunctionDef_kind:
3480 return compiler_function(c, s, 1);
3481 case AsyncWith_kind:
3482 return compiler_async_with(c, s, 0);
3483 case AsyncFor_kind:
3484 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 }
Yury Selivanov75445082015-05-11 22:57:16 -04003486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003487 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003488}
3489
3490static int
3491unaryop(unaryop_ty op)
3492{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003493 switch (op) {
3494 case Invert:
3495 return UNARY_INVERT;
3496 case Not:
3497 return UNARY_NOT;
3498 case UAdd:
3499 return UNARY_POSITIVE;
3500 case USub:
3501 return UNARY_NEGATIVE;
3502 default:
3503 PyErr_Format(PyExc_SystemError,
3504 "unary op %d should not be possible", op);
3505 return 0;
3506 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003507}
3508
3509static int
Andy Lester76d58772020-03-10 21:18:12 -05003510binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003512 switch (op) {
3513 case Add:
3514 return BINARY_ADD;
3515 case Sub:
3516 return BINARY_SUBTRACT;
3517 case Mult:
3518 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003519 case MatMult:
3520 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003521 case Div:
3522 return BINARY_TRUE_DIVIDE;
3523 case Mod:
3524 return BINARY_MODULO;
3525 case Pow:
3526 return BINARY_POWER;
3527 case LShift:
3528 return BINARY_LSHIFT;
3529 case RShift:
3530 return BINARY_RSHIFT;
3531 case BitOr:
3532 return BINARY_OR;
3533 case BitXor:
3534 return BINARY_XOR;
3535 case BitAnd:
3536 return BINARY_AND;
3537 case FloorDiv:
3538 return BINARY_FLOOR_DIVIDE;
3539 default:
3540 PyErr_Format(PyExc_SystemError,
3541 "binary op %d should not be possible", op);
3542 return 0;
3543 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003544}
3545
3546static int
Andy Lester76d58772020-03-10 21:18:12 -05003547inplace_binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003549 switch (op) {
3550 case Add:
3551 return INPLACE_ADD;
3552 case Sub:
3553 return INPLACE_SUBTRACT;
3554 case Mult:
3555 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003556 case MatMult:
3557 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003558 case Div:
3559 return INPLACE_TRUE_DIVIDE;
3560 case Mod:
3561 return INPLACE_MODULO;
3562 case Pow:
3563 return INPLACE_POWER;
3564 case LShift:
3565 return INPLACE_LSHIFT;
3566 case RShift:
3567 return INPLACE_RSHIFT;
3568 case BitOr:
3569 return INPLACE_OR;
3570 case BitXor:
3571 return INPLACE_XOR;
3572 case BitAnd:
3573 return INPLACE_AND;
3574 case FloorDiv:
3575 return INPLACE_FLOOR_DIVIDE;
3576 default:
3577 PyErr_Format(PyExc_SystemError,
3578 "inplace binary op %d should not be possible", op);
3579 return 0;
3580 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003581}
3582
3583static int
3584compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3585{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003586 int op, scope;
3587 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003588 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003590 PyObject *dict = c->u->u_names;
3591 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003592
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003593 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3594 !_PyUnicode_EqualToASCIIString(name, "True") &&
3595 !_PyUnicode_EqualToASCIIString(name, "False"));
Benjamin Peterson70b224d2012-12-06 17:49:58 -05003596
Pablo Galindoc5fc1562020-04-22 23:29:27 +01003597 if (forbidden_name(c, name, ctx))
3598 return 0;
3599
Serhiy Storchakabd6ec4d2017-12-18 14:29:12 +02003600 mangled = _Py_Mangle(c->u->u_private, name);
3601 if (!mangled)
3602 return 0;
3603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 op = 0;
3605 optype = OP_NAME;
3606 scope = PyST_GetScope(c->u->u_ste, mangled);
3607 switch (scope) {
3608 case FREE:
3609 dict = c->u->u_freevars;
3610 optype = OP_DEREF;
3611 break;
3612 case CELL:
3613 dict = c->u->u_cellvars;
3614 optype = OP_DEREF;
3615 break;
3616 case LOCAL:
3617 if (c->u->u_ste->ste_type == FunctionBlock)
3618 optype = OP_FAST;
3619 break;
3620 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04003621 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003622 optype = OP_GLOBAL;
3623 break;
3624 case GLOBAL_EXPLICIT:
3625 optype = OP_GLOBAL;
3626 break;
3627 default:
3628 /* scope can be 0 */
3629 break;
3630 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003632 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003633 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003635 switch (optype) {
3636 case OP_DEREF:
3637 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003638 case Load:
3639 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3640 break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003641 case Store: op = STORE_DEREF; break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003642 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003643 }
3644 break;
3645 case OP_FAST:
3646 switch (ctx) {
3647 case Load: op = LOAD_FAST; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003648 case Store: op = STORE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003649 case Del: op = DELETE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003650 }
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003651 ADDOP_N(c, op, mangled, varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003652 return 1;
3653 case OP_GLOBAL:
3654 switch (ctx) {
3655 case Load: op = LOAD_GLOBAL; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003656 case Store: op = STORE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003657 case Del: op = DELETE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003658 }
3659 break;
3660 case OP_NAME:
3661 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00003662 case Load: op = LOAD_NAME; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003663 case Store: op = STORE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003664 case Del: op = DELETE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003665 }
3666 break;
3667 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003669 assert(op);
Andy Lester76d58772020-03-10 21:18:12 -05003670 arg = compiler_add_o(dict, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003671 Py_DECREF(mangled);
3672 if (arg < 0)
3673 return 0;
3674 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003675}
3676
3677static int
3678compiler_boolop(struct compiler *c, expr_ty e)
3679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003680 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003681 int jumpi;
3682 Py_ssize_t i, n;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003683 asdl_expr_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003685 assert(e->kind == BoolOp_kind);
3686 if (e->v.BoolOp.op == And)
3687 jumpi = JUMP_IF_FALSE_OR_POP;
3688 else
3689 jumpi = JUMP_IF_TRUE_OR_POP;
3690 end = compiler_new_block(c);
3691 if (end == NULL)
3692 return 0;
3693 s = e->v.BoolOp.values;
3694 n = asdl_seq_LEN(s) - 1;
3695 assert(n >= 0);
3696 for (i = 0; i < n; ++i) {
3697 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003698 ADDOP_JUMP(c, jumpi, end);
Mark Shannon6e8128f2020-07-30 10:03:00 +01003699 basicblock *next = compiler_new_block(c);
3700 if (next == NULL) {
3701 return 0;
3702 }
3703 compiler_use_next_block(c, next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003704 }
3705 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3706 compiler_use_next_block(c, end);
3707 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003708}
3709
3710static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003711starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
Mark Shannon13bc1392020-01-23 09:25:17 +00003712 int build, int add, int extend, int tuple)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003713{
3714 Py_ssize_t n = asdl_seq_LEN(elts);
Mark Shannon13bc1392020-01-23 09:25:17 +00003715 Py_ssize_t i, seen_star = 0;
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003716 if (n > 2 && are_all_items_const(elts, 0, n)) {
3717 PyObject *folded = PyTuple_New(n);
3718 if (folded == NULL) {
3719 return 0;
3720 }
3721 PyObject *val;
3722 for (i = 0; i < n; i++) {
3723 val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
3724 Py_INCREF(val);
3725 PyTuple_SET_ITEM(folded, i, val);
3726 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003727 if (tuple) {
3728 ADDOP_LOAD_CONST_NEW(c, folded);
3729 } else {
3730 if (add == SET_ADD) {
3731 Py_SETREF(folded, PyFrozenSet_New(folded));
3732 if (folded == NULL) {
3733 return 0;
3734 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003735 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003736 ADDOP_I(c, build, pushed);
3737 ADDOP_LOAD_CONST_NEW(c, folded);
3738 ADDOP_I(c, extend, 1);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003739 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003740 return 1;
3741 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003742
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003743 for (i = 0; i < n; i++) {
3744 expr_ty elt = asdl_seq_GET(elts, i);
3745 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003746 seen_star = 1;
3747 }
3748 }
3749 if (seen_star) {
3750 seen_star = 0;
3751 for (i = 0; i < n; i++) {
3752 expr_ty elt = asdl_seq_GET(elts, i);
3753 if (elt->kind == Starred_kind) {
3754 if (seen_star == 0) {
3755 ADDOP_I(c, build, i+pushed);
3756 seen_star = 1;
3757 }
3758 VISIT(c, expr, elt->v.Starred.value);
3759 ADDOP_I(c, extend, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003760 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003761 else {
3762 VISIT(c, expr, elt);
3763 if (seen_star) {
3764 ADDOP_I(c, add, 1);
3765 }
3766 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003767 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003768 assert(seen_star);
3769 if (tuple) {
3770 ADDOP(c, LIST_TO_TUPLE);
3771 }
3772 }
3773 else {
3774 for (i = 0; i < n; i++) {
3775 expr_ty elt = asdl_seq_GET(elts, i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003776 VISIT(c, expr, elt);
Mark Shannon13bc1392020-01-23 09:25:17 +00003777 }
3778 if (tuple) {
3779 ADDOP_I(c, BUILD_TUPLE, n+pushed);
3780 } else {
3781 ADDOP_I(c, build, n+pushed);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003782 }
3783 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003784 return 1;
3785}
3786
3787static int
Brandt Bucher145bf262021-02-26 14:51:55 -08003788unpack_helper(struct compiler *c, asdl_expr_seq *elts)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003789{
3790 Py_ssize_t n = asdl_seq_LEN(elts);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003791 int seen_star = 0;
Brandt Bucher145bf262021-02-26 14:51:55 -08003792 for (Py_ssize_t i = 0; i < n; i++) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003793 expr_ty elt = asdl_seq_GET(elts, i);
3794 if (elt->kind == Starred_kind && !seen_star) {
3795 if ((i >= (1 << 8)) ||
3796 (n-i-1 >= (INT_MAX >> 8)))
3797 return compiler_error(c,
3798 "too many expressions in "
3799 "star-unpacking assignment");
3800 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3801 seen_star = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003802 }
3803 else if (elt->kind == Starred_kind) {
3804 return compiler_error(c,
Furkan Öndercb6534e2020-03-26 04:54:31 +03003805 "multiple starred expressions in assignment");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003806 }
3807 }
3808 if (!seen_star) {
3809 ADDOP_I(c, UNPACK_SEQUENCE, n);
3810 }
Brandt Bucher145bf262021-02-26 14:51:55 -08003811 return 1;
3812}
3813
3814static int
3815assignment_helper(struct compiler *c, asdl_expr_seq *elts)
3816{
3817 Py_ssize_t n = asdl_seq_LEN(elts);
3818 RETURN_IF_FALSE(unpack_helper(c, elts));
3819 for (Py_ssize_t i = 0; i < n; i++) {
Brandt Bucherd5aa2e92020-03-07 19:44:18 -08003820 expr_ty elt = asdl_seq_GET(elts, i);
3821 VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value);
3822 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003823 return 1;
3824}
3825
3826static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003827compiler_list(struct compiler *c, expr_ty e)
3828{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003829 asdl_expr_seq *elts = e->v.List.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003830 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003831 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003832 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003833 else if (e->v.List.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003834 return starunpack_helper(c, elts, 0, BUILD_LIST,
3835 LIST_APPEND, LIST_EXTEND, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003836 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003837 else
3838 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003839 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003840}
3841
3842static int
3843compiler_tuple(struct compiler *c, expr_ty e)
3844{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003845 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003846 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003847 return assignment_helper(c, elts);
3848 }
3849 else if (e->v.Tuple.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003850 return starunpack_helper(c, elts, 0, BUILD_LIST,
3851 LIST_APPEND, LIST_EXTEND, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003852 }
3853 else
3854 VISIT_SEQ(c, expr, elts);
3855 return 1;
3856}
3857
3858static int
3859compiler_set(struct compiler *c, expr_ty e)
3860{
Mark Shannon13bc1392020-01-23 09:25:17 +00003861 return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET,
3862 SET_ADD, SET_UPDATE, 0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003863}
3864
3865static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003866are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003867{
3868 Py_ssize_t i;
3869 for (i = begin; i < end; i++) {
3870 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003871 if (key == NULL || key->kind != Constant_kind)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003872 return 0;
3873 }
3874 return 1;
3875}
3876
3877static int
3878compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3879{
3880 Py_ssize_t i, n = end - begin;
3881 PyObject *keys, *key;
3882 if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) {
3883 for (i = begin; i < end; i++) {
3884 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3885 }
3886 keys = PyTuple_New(n);
3887 if (keys == NULL) {
3888 return 0;
3889 }
3890 for (i = begin; i < end; i++) {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003891 key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003892 Py_INCREF(key);
3893 PyTuple_SET_ITEM(keys, i - begin, key);
3894 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003895 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003896 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3897 }
3898 else {
3899 for (i = begin; i < end; i++) {
3900 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3901 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3902 }
3903 ADDOP_I(c, BUILD_MAP, n);
3904 }
3905 return 1;
3906}
3907
3908static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003909compiler_dict(struct compiler *c, expr_ty e)
3910{
Victor Stinner976bb402016-03-23 11:36:19 +01003911 Py_ssize_t i, n, elements;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003912 int have_dict;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003913 int is_unpacking = 0;
3914 n = asdl_seq_LEN(e->v.Dict.values);
Mark Shannon8a4cd702020-01-27 09:57:45 +00003915 have_dict = 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003916 elements = 0;
3917 for (i = 0; i < n; i++) {
3918 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003919 if (is_unpacking) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003920 if (elements) {
3921 if (!compiler_subdict(c, e, i - elements, i)) {
3922 return 0;
3923 }
3924 if (have_dict) {
3925 ADDOP_I(c, DICT_UPDATE, 1);
3926 }
3927 have_dict = 1;
3928 elements = 0;
3929 }
3930 if (have_dict == 0) {
3931 ADDOP_I(c, BUILD_MAP, 0);
3932 have_dict = 1;
3933 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003934 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Mark Shannon8a4cd702020-01-27 09:57:45 +00003935 ADDOP_I(c, DICT_UPDATE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003936 }
3937 else {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003938 if (elements == 0xFFFF) {
Pablo Galindoc51db0e2020-08-13 09:48:41 +01003939 if (!compiler_subdict(c, e, i - elements, i + 1)) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003940 return 0;
3941 }
3942 if (have_dict) {
3943 ADDOP_I(c, DICT_UPDATE, 1);
3944 }
3945 have_dict = 1;
3946 elements = 0;
3947 }
3948 else {
3949 elements++;
3950 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003951 }
3952 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003953 if (elements) {
3954 if (!compiler_subdict(c, e, n - elements, n)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003955 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003956 }
3957 if (have_dict) {
3958 ADDOP_I(c, DICT_UPDATE, 1);
3959 }
3960 have_dict = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003961 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003962 if (!have_dict) {
3963 ADDOP_I(c, BUILD_MAP, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003964 }
3965 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003966}
3967
3968static int
3969compiler_compare(struct compiler *c, expr_ty e)
3970{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003971 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003972
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02003973 if (!check_compare(c, e)) {
3974 return 0;
3975 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003976 VISIT(c, expr, e->v.Compare.left);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003977 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
3978 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
3979 if (n == 0) {
3980 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Mark Shannon9af0e472020-01-14 10:12:45 +00003981 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003982 }
3983 else {
3984 basicblock *cleanup = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003985 if (cleanup == NULL)
3986 return 0;
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003987 for (i = 0; i < n; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003988 VISIT(c, expr,
3989 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003990 ADDOP(c, DUP_TOP);
3991 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00003992 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003993 ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003994 NEXT_BLOCK(c);
3995 }
3996 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00003997 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003998 basicblock *end = compiler_new_block(c);
3999 if (end == NULL)
4000 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00004001 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004002 compiler_use_next_block(c, cleanup);
4003 ADDOP(c, ROT_TWO);
4004 ADDOP(c, POP_TOP);
4005 compiler_use_next_block(c, end);
4006 }
4007 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004008}
4009
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004010static PyTypeObject *
4011infer_type(expr_ty e)
4012{
4013 switch (e->kind) {
4014 case Tuple_kind:
4015 return &PyTuple_Type;
4016 case List_kind:
4017 case ListComp_kind:
4018 return &PyList_Type;
4019 case Dict_kind:
4020 case DictComp_kind:
4021 return &PyDict_Type;
4022 case Set_kind:
4023 case SetComp_kind:
4024 return &PySet_Type;
4025 case GeneratorExp_kind:
4026 return &PyGen_Type;
4027 case Lambda_kind:
4028 return &PyFunction_Type;
4029 case JoinedStr_kind:
4030 case FormattedValue_kind:
4031 return &PyUnicode_Type;
4032 case Constant_kind:
Victor Stinnera102ed72020-02-07 02:24:48 +01004033 return Py_TYPE(e->v.Constant.value);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004034 default:
4035 return NULL;
4036 }
4037}
4038
4039static int
4040check_caller(struct compiler *c, expr_ty e)
4041{
4042 switch (e->kind) {
4043 case Constant_kind:
4044 case Tuple_kind:
4045 case List_kind:
4046 case ListComp_kind:
4047 case Dict_kind:
4048 case DictComp_kind:
4049 case Set_kind:
4050 case SetComp_kind:
4051 case GeneratorExp_kind:
4052 case JoinedStr_kind:
4053 case FormattedValue_kind:
4054 return compiler_warn(c, "'%.200s' object is not callable; "
4055 "perhaps you missed a comma?",
4056 infer_type(e)->tp_name);
4057 default:
4058 return 1;
4059 }
4060}
4061
4062static int
4063check_subscripter(struct compiler *c, expr_ty e)
4064{
4065 PyObject *v;
4066
4067 switch (e->kind) {
4068 case Constant_kind:
4069 v = e->v.Constant.value;
4070 if (!(v == Py_None || v == Py_Ellipsis ||
4071 PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
4072 PyAnySet_Check(v)))
4073 {
4074 return 1;
4075 }
4076 /* fall through */
4077 case Set_kind:
4078 case SetComp_kind:
4079 case GeneratorExp_kind:
4080 case Lambda_kind:
4081 return compiler_warn(c, "'%.200s' object is not subscriptable; "
4082 "perhaps you missed a comma?",
4083 infer_type(e)->tp_name);
4084 default:
4085 return 1;
4086 }
4087}
4088
4089static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004090check_index(struct compiler *c, expr_ty e, expr_ty s)
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004091{
4092 PyObject *v;
4093
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004094 PyTypeObject *index_type = infer_type(s);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004095 if (index_type == NULL
4096 || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
4097 || index_type == &PySlice_Type) {
4098 return 1;
4099 }
4100
4101 switch (e->kind) {
4102 case Constant_kind:
4103 v = e->v.Constant.value;
4104 if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
4105 return 1;
4106 }
4107 /* fall through */
4108 case Tuple_kind:
4109 case List_kind:
4110 case ListComp_kind:
4111 case JoinedStr_kind:
4112 case FormattedValue_kind:
4113 return compiler_warn(c, "%.200s indices must be integers or slices, "
4114 "not %.200s; "
4115 "perhaps you missed a comma?",
4116 infer_type(e)->tp_name,
4117 index_type->tp_name);
4118 default:
4119 return 1;
4120 }
4121}
4122
Zackery Spytz97f5de02019-03-22 01:30:32 -06004123// Return 1 if the method call was optimized, -1 if not, and 0 on error.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004124static int
Yury Selivanovf2392132016-12-13 19:03:51 -05004125maybe_optimize_method_call(struct compiler *c, expr_ty e)
4126{
4127 Py_ssize_t argsl, i;
4128 expr_ty meth = e->v.Call.func;
Pablo Galindoa5634c42020-09-16 19:42:00 +01004129 asdl_expr_seq *args = e->v.Call.args;
Yury Selivanovf2392132016-12-13 19:03:51 -05004130
4131 /* Check that the call node is an attribute access, and that
4132 the call doesn't have keyword parameters. */
4133 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
4134 asdl_seq_LEN(e->v.Call.keywords))
4135 return -1;
4136
4137 /* Check that there are no *varargs types of arguments. */
4138 argsl = asdl_seq_LEN(args);
4139 for (i = 0; i < argsl; i++) {
4140 expr_ty elt = asdl_seq_GET(args, i);
4141 if (elt->kind == Starred_kind) {
4142 return -1;
4143 }
4144 }
4145
4146 /* Alright, we can optimize the code. */
4147 VISIT(c, expr, meth->v.Attribute.value);
Mark Shannond48848c2021-03-14 18:01:30 +00004148 int old_lineno = c->u->u_lineno;
4149 c->u->u_lineno = meth->end_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004150 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
4151 VISIT_SEQ(c, expr, e->v.Call.args);
4152 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
Mark Shannond48848c2021-03-14 18:01:30 +00004153 c->u->u_lineno = old_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004154 return 1;
4155}
4156
4157static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004158validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
Zackery Spytz08050e92020-04-06 00:47:47 -06004159{
4160 Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
4161 for (Py_ssize_t i = 0; i < nkeywords; i++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004162 keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
4163 if (key->arg == NULL) {
4164 continue;
4165 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +01004166 if (forbidden_name(c, key->arg, Store)) {
4167 return -1;
4168 }
Zackery Spytz08050e92020-04-06 00:47:47 -06004169 for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004170 keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
4171 if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004172 c->u->u_col_offset = other->col_offset;
Brandt Bucher145bf262021-02-26 14:51:55 -08004173 compiler_error(c, "keyword argument repeated: %U", key->arg);
Pablo Galindo254ec782020-04-03 20:37:13 +01004174 return -1;
4175 }
4176 }
4177 }
4178 return 0;
4179}
4180
4181static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004182compiler_call(struct compiler *c, expr_ty e)
4183{
Zackery Spytz97f5de02019-03-22 01:30:32 -06004184 int ret = maybe_optimize_method_call(c, e);
4185 if (ret >= 0) {
4186 return ret;
4187 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004188 if (!check_caller(c, e->v.Call.func)) {
4189 return 0;
4190 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004191 VISIT(c, expr, e->v.Call.func);
4192 return compiler_call_helper(c, 0,
4193 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004194 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004195}
4196
Eric V. Smith235a6f02015-09-19 14:51:32 -04004197static int
4198compiler_joined_str(struct compiler *c, expr_ty e)
4199{
Eric V. Smith235a6f02015-09-19 14:51:32 -04004200 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004201 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1)
4202 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
Eric V. Smith235a6f02015-09-19 14:51:32 -04004203 return 1;
4204}
4205
Eric V. Smitha78c7952015-11-03 12:45:05 -05004206/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004207static int
4208compiler_formatted_value(struct compiler *c, expr_ty e)
4209{
Eric V. Smitha78c7952015-11-03 12:45:05 -05004210 /* Our oparg encodes 2 pieces of information: the conversion
4211 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04004212
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004213 Convert the conversion char to 3 bits:
4214 : 000 0x0 FVC_NONE The default if nothing specified.
Eric V. Smitha78c7952015-11-03 12:45:05 -05004215 !s : 001 0x1 FVC_STR
4216 !r : 010 0x2 FVC_REPR
4217 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04004218
Eric V. Smitha78c7952015-11-03 12:45:05 -05004219 next bit is whether or not we have a format spec:
4220 yes : 100 0x4
4221 no : 000 0x0
4222 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004223
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004224 int conversion = e->v.FormattedValue.conversion;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004225 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004226
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004227 /* The expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004228 VISIT(c, expr, e->v.FormattedValue.value);
4229
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004230 switch (conversion) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004231 case 's': oparg = FVC_STR; break;
4232 case 'r': oparg = FVC_REPR; break;
4233 case 'a': oparg = FVC_ASCII; break;
4234 case -1: oparg = FVC_NONE; break;
4235 default:
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004236 PyErr_Format(PyExc_SystemError,
4237 "Unrecognized conversion character %d", conversion);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004238 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004239 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004240 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004241 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004242 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004243 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004244 }
4245
Eric V. Smitha78c7952015-11-03 12:45:05 -05004246 /* And push our opcode and oparg */
4247 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004248
Eric V. Smith235a6f02015-09-19 14:51:32 -04004249 return 1;
4250}
4251
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004252static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004253compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004254{
4255 Py_ssize_t i, n = end - begin;
4256 keyword_ty kw;
4257 PyObject *keys, *key;
4258 assert(n > 0);
4259 if (n > 1) {
4260 for (i = begin; i < end; i++) {
4261 kw = asdl_seq_GET(keywords, i);
4262 VISIT(c, expr, kw->value);
4263 }
4264 keys = PyTuple_New(n);
4265 if (keys == NULL) {
4266 return 0;
4267 }
4268 for (i = begin; i < end; i++) {
4269 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
4270 Py_INCREF(key);
4271 PyTuple_SET_ITEM(keys, i - begin, key);
4272 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004273 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004274 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
4275 }
4276 else {
4277 /* a for loop only executes once */
4278 for (i = begin; i < end; i++) {
4279 kw = asdl_seq_GET(keywords, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004280 ADDOP_LOAD_CONST(c, kw->arg);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004281 VISIT(c, expr, kw->value);
4282 }
4283 ADDOP_I(c, BUILD_MAP, n);
4284 }
4285 return 1;
4286}
4287
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004288/* shared code between compiler_call and compiler_class */
4289static int
4290compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01004291 int n, /* Args already pushed */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004292 asdl_expr_seq *args,
4293 asdl_keyword_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004294{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004295 Py_ssize_t i, nseen, nelts, nkwelts;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004296
Pablo Galindo254ec782020-04-03 20:37:13 +01004297 if (validate_keywords(c, keywords) == -1) {
4298 return 0;
4299 }
4300
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004301 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004302 nkwelts = asdl_seq_LEN(keywords);
4303
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004304 for (i = 0; i < nelts; i++) {
4305 expr_ty elt = asdl_seq_GET(args, i);
4306 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00004307 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004308 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004309 }
4310 for (i = 0; i < nkwelts; i++) {
4311 keyword_ty kw = asdl_seq_GET(keywords, i);
4312 if (kw->arg == NULL) {
4313 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004314 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004315 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004316
Mark Shannon13bc1392020-01-23 09:25:17 +00004317 /* No * or ** args, so can use faster calling sequence */
4318 for (i = 0; i < nelts; i++) {
4319 expr_ty elt = asdl_seq_GET(args, i);
4320 assert(elt->kind != Starred_kind);
4321 VISIT(c, expr, elt);
4322 }
4323 if (nkwelts) {
4324 PyObject *names;
4325 VISIT_SEQ(c, keyword, keywords);
4326 names = PyTuple_New(nkwelts);
4327 if (names == NULL) {
4328 return 0;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004329 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004330 for (i = 0; i < nkwelts; i++) {
4331 keyword_ty kw = asdl_seq_GET(keywords, i);
4332 Py_INCREF(kw->arg);
4333 PyTuple_SET_ITEM(names, i, kw->arg);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004334 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004335 ADDOP_LOAD_CONST_NEW(c, names);
4336 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
4337 return 1;
4338 }
4339 else {
4340 ADDOP_I(c, CALL_FUNCTION, n + nelts);
4341 return 1;
4342 }
4343
4344ex_call:
4345
4346 /* Do positional arguments. */
4347 if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
4348 VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
4349 }
4350 else if (starunpack_helper(c, args, n, BUILD_LIST,
4351 LIST_APPEND, LIST_EXTEND, 1) == 0) {
4352 return 0;
4353 }
4354 /* Then keyword arguments */
4355 if (nkwelts) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004356 /* Has a new dict been pushed */
4357 int have_dict = 0;
Mark Shannon13bc1392020-01-23 09:25:17 +00004358
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004359 nseen = 0; /* the number of keyword arguments on the stack following */
4360 for (i = 0; i < nkwelts; i++) {
4361 keyword_ty kw = asdl_seq_GET(keywords, i);
4362 if (kw->arg == NULL) {
4363 /* A keyword argument unpacking. */
4364 if (nseen) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004365 if (!compiler_subkwargs(c, keywords, i - nseen, i)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004366 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004367 }
Mark Shannondb64f122020-06-01 10:42:42 +01004368 if (have_dict) {
4369 ADDOP_I(c, DICT_MERGE, 1);
4370 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004371 have_dict = 1;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004372 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004373 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004374 if (!have_dict) {
4375 ADDOP_I(c, BUILD_MAP, 0);
4376 have_dict = 1;
4377 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004378 VISIT(c, expr, kw->value);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004379 ADDOP_I(c, DICT_MERGE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004380 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004381 else {
4382 nseen++;
4383 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004384 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004385 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004386 /* Pack up any trailing keyword arguments. */
Mark Shannon8a4cd702020-01-27 09:57:45 +00004387 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004388 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004389 }
4390 if (have_dict) {
4391 ADDOP_I(c, DICT_MERGE, 1);
4392 }
4393 have_dict = 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004394 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004395 assert(have_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004396 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004397 ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0);
4398 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004399}
4400
Nick Coghlan650f0d02007-04-15 12:05:43 +00004401
4402/* List and set comprehensions and generator expressions work by creating a
4403 nested function to perform the actual iteration. This means that the
4404 iteration variables don't leak into the current scope.
4405 The defined function is called immediately following its definition, with the
4406 result of that call being the result of the expression.
4407 The LC/SC version returns the populated container, while the GE version is
4408 flagged in symtable.c as a generator, so it returns the generator object
4409 when the function is called.
Nick Coghlan650f0d02007-04-15 12:05:43 +00004410
4411 Possible cleanups:
4412 - iterate over the generator sequence instead of using recursion
4413*/
4414
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004415
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004416static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004417compiler_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004418 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004419 int depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004420 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004421{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004422 comprehension_ty gen;
4423 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4424 if (gen->is_async) {
4425 return compiler_async_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004426 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004427 } else {
4428 return compiler_sync_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004429 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004430 }
4431}
4432
4433static int
4434compiler_sync_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004435 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004436 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004437 expr_ty elt, expr_ty val, int type)
4438{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004439 /* generate code for the iterator, then each of the ifs,
4440 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004442 comprehension_ty gen;
4443 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004444 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004446 start = compiler_new_block(c);
4447 skip = compiler_new_block(c);
4448 if_cleanup = compiler_new_block(c);
4449 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004451 if (start == NULL || skip == NULL || if_cleanup == NULL ||
4452 anchor == NULL)
4453 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004455 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 if (gen_index == 0) {
4458 /* Receive outermost iter as an implicit argument */
4459 c->u->u_argcount = 1;
4460 ADDOP_I(c, LOAD_FAST, 0);
4461 }
4462 else {
4463 /* Sub-iter - calculate on the fly */
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004464 /* Fast path for the temporary variable assignment idiom:
4465 for y in [f(x)]
4466 */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004467 asdl_expr_seq *elts;
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004468 switch (gen->iter->kind) {
4469 case List_kind:
4470 elts = gen->iter->v.List.elts;
4471 break;
4472 case Tuple_kind:
4473 elts = gen->iter->v.Tuple.elts;
4474 break;
4475 default:
4476 elts = NULL;
4477 }
4478 if (asdl_seq_LEN(elts) == 1) {
4479 expr_ty elt = asdl_seq_GET(elts, 0);
4480 if (elt->kind != Starred_kind) {
4481 VISIT(c, expr, elt);
4482 start = NULL;
4483 }
4484 }
4485 if (start) {
4486 VISIT(c, expr, gen->iter);
4487 ADDOP(c, GET_ITER);
4488 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 }
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004490 if (start) {
4491 depth++;
4492 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01004493 ADDOP_JUMP(c, FOR_ITER, anchor);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004494 NEXT_BLOCK(c);
4495 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004496 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 /* XXX this needs to be cleaned up...a lot! */
4499 n = asdl_seq_LEN(gen->ifs);
4500 for (i = 0; i < n; i++) {
4501 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004502 if (!compiler_jump_if(c, e, if_cleanup, 0))
4503 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004504 NEXT_BLOCK(c);
4505 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004507 if (++gen_index < asdl_seq_LEN(generators))
4508 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004509 generators, gen_index, depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004510 elt, val, type))
4511 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004513 /* only append after the last for generator */
4514 if (gen_index >= asdl_seq_LEN(generators)) {
4515 /* comprehension specific code */
4516 switch (type) {
4517 case COMP_GENEXP:
4518 VISIT(c, expr, elt);
4519 ADDOP(c, YIELD_VALUE);
4520 ADDOP(c, POP_TOP);
4521 break;
4522 case COMP_LISTCOMP:
4523 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004524 ADDOP_I(c, LIST_APPEND, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004525 break;
4526 case COMP_SETCOMP:
4527 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004528 ADDOP_I(c, SET_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004529 break;
4530 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004531 /* With '{k: v}', k is evaluated before v, so we do
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004532 the same. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004533 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004534 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004535 ADDOP_I(c, MAP_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004536 break;
4537 default:
4538 return 0;
4539 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004541 compiler_use_next_block(c, skip);
4542 }
4543 compiler_use_next_block(c, if_cleanup);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004544 if (start) {
Mark Shannon582aaf12020-08-04 17:30:11 +01004545 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004546 compiler_use_next_block(c, anchor);
4547 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004548
4549 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004550}
4551
4552static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004553compiler_async_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004554 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004555 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004556 expr_ty elt, expr_ty val, int type)
4557{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004558 comprehension_ty gen;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004559 basicblock *start, *if_cleanup, *except;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004560 Py_ssize_t i, n;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004561 start = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004562 except = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004563 if_cleanup = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004564
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004565 if (start == NULL || if_cleanup == NULL || except == NULL) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004566 return 0;
4567 }
4568
4569 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4570
4571 if (gen_index == 0) {
4572 /* Receive outermost iter as an implicit argument */
4573 c->u->u_argcount = 1;
4574 ADDOP_I(c, LOAD_FAST, 0);
4575 }
4576 else {
4577 /* Sub-iter - calculate on the fly */
4578 VISIT(c, expr, gen->iter);
4579 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004580 }
4581
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004582 compiler_use_next_block(c, start);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004583
Mark Shannon582aaf12020-08-04 17:30:11 +01004584 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004585 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004586 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004587 ADDOP(c, YIELD_FROM);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004588 ADDOP(c, POP_BLOCK);
Serhiy Storchaka24d32012018-03-10 18:22:34 +02004589 VISIT(c, expr, gen->target);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004590
4591 n = asdl_seq_LEN(gen->ifs);
4592 for (i = 0; i < n; i++) {
4593 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004594 if (!compiler_jump_if(c, e, if_cleanup, 0))
4595 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004596 NEXT_BLOCK(c);
4597 }
4598
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004599 depth++;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004600 if (++gen_index < asdl_seq_LEN(generators))
4601 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004602 generators, gen_index, depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004603 elt, val, type))
4604 return 0;
4605
4606 /* only append after the last for generator */
4607 if (gen_index >= asdl_seq_LEN(generators)) {
4608 /* comprehension specific code */
4609 switch (type) {
4610 case COMP_GENEXP:
4611 VISIT(c, expr, elt);
4612 ADDOP(c, YIELD_VALUE);
4613 ADDOP(c, POP_TOP);
4614 break;
4615 case COMP_LISTCOMP:
4616 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004617 ADDOP_I(c, LIST_APPEND, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004618 break;
4619 case COMP_SETCOMP:
4620 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004621 ADDOP_I(c, SET_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004622 break;
4623 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004624 /* With '{k: v}', k is evaluated before v, so we do
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004625 the same. */
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004626 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004627 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004628 ADDOP_I(c, MAP_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004629 break;
4630 default:
4631 return 0;
4632 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004633 }
4634 compiler_use_next_block(c, if_cleanup);
Mark Shannon582aaf12020-08-04 17:30:11 +01004635 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004636
4637 compiler_use_next_block(c, except);
4638 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004639
4640 return 1;
4641}
4642
4643static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004644compiler_comprehension(struct compiler *c, expr_ty e, int type,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004645 identifier name, asdl_comprehension_seq *generators, expr_ty elt,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004646 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004647{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004648 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004649 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004650 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004651 int is_async_generator = 0;
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004652 int top_level_await = IS_TOP_LEVEL_AWAIT(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004653
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004654
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004655 int is_async_function = c->u->u_ste->ste_coroutine;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004656
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004657 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004658 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4659 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004660 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004661 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004662 }
4663
4664 is_async_generator = c->u->u_ste->ste_coroutine;
4665
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004666 if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004667 compiler_error(c, "asynchronous comprehension outside of "
4668 "an asynchronous function");
4669 goto error_in_scope;
4670 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004672 if (type != COMP_GENEXP) {
4673 int op;
4674 switch (type) {
4675 case COMP_LISTCOMP:
4676 op = BUILD_LIST;
4677 break;
4678 case COMP_SETCOMP:
4679 op = BUILD_SET;
4680 break;
4681 case COMP_DICTCOMP:
4682 op = BUILD_MAP;
4683 break;
4684 default:
4685 PyErr_Format(PyExc_SystemError,
4686 "unknown comprehension type %d", type);
4687 goto error_in_scope;
4688 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004690 ADDOP_I(c, op, 0);
4691 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004692
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004693 if (!compiler_comprehension_generator(c, generators, 0, 0, elt,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004694 val, type))
4695 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004697 if (type != COMP_GENEXP) {
4698 ADDOP(c, RETURN_VALUE);
4699 }
4700
4701 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004702 qualname = c->u->u_qualname;
4703 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004704 compiler_exit_scope(c);
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004705 if (top_level_await && is_async_generator){
4706 c->u->u_ste->ste_coroutine = 1;
4707 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004708 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004709 goto error;
4710
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004711 if (!compiler_make_closure(c, co, 0, qualname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004712 goto error;
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004713 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004714 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004715 Py_DECREF(co);
4716
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004717 VISIT(c, expr, outermost->iter);
4718
4719 if (outermost->is_async) {
4720 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004721 } else {
4722 ADDOP(c, GET_ITER);
4723 }
4724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004725 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004726
4727 if (is_async_generator && type != COMP_GENEXP) {
4728 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004729 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004730 ADDOP(c, YIELD_FROM);
4731 }
4732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004733 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004734error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004735 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004736error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004737 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004738 Py_XDECREF(co);
4739 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004740}
4741
4742static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004743compiler_genexp(struct compiler *c, expr_ty e)
4744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004745 static identifier name;
4746 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004747 name = PyUnicode_InternFromString("<genexpr>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004748 if (!name)
4749 return 0;
4750 }
4751 assert(e->kind == GeneratorExp_kind);
4752 return compiler_comprehension(c, e, COMP_GENEXP, name,
4753 e->v.GeneratorExp.generators,
4754 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004755}
4756
4757static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004758compiler_listcomp(struct compiler *c, expr_ty e)
4759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004760 static identifier name;
4761 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004762 name = PyUnicode_InternFromString("<listcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004763 if (!name)
4764 return 0;
4765 }
4766 assert(e->kind == ListComp_kind);
4767 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4768 e->v.ListComp.generators,
4769 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004770}
4771
4772static int
4773compiler_setcomp(struct compiler *c, expr_ty e)
4774{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004775 static identifier name;
4776 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004777 name = PyUnicode_InternFromString("<setcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004778 if (!name)
4779 return 0;
4780 }
4781 assert(e->kind == SetComp_kind);
4782 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4783 e->v.SetComp.generators,
4784 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004785}
4786
4787
4788static int
4789compiler_dictcomp(struct compiler *c, expr_ty e)
4790{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004791 static identifier name;
4792 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004793 name = PyUnicode_InternFromString("<dictcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004794 if (!name)
4795 return 0;
4796 }
4797 assert(e->kind == DictComp_kind);
4798 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4799 e->v.DictComp.generators,
4800 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004801}
4802
4803
4804static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004805compiler_visit_keyword(struct compiler *c, keyword_ty k)
4806{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004807 VISIT(c, expr, k->value);
4808 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004809}
4810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004811/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004812 whether they are true or false.
4813
4814 Return values: 1 for true, 0 for false, -1 for non-constant.
4815 */
4816
4817static int
Mark Shannonfee55262019-11-21 09:11:43 +00004818compiler_with_except_finish(struct compiler *c) {
4819 basicblock *exit;
4820 exit = compiler_new_block(c);
4821 if (exit == NULL)
4822 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01004823 ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit);
Mark Shannon266b4622020-11-17 19:30:14 +00004824 NEXT_BLOCK(c);
Mark Shannonbf353f32020-12-17 13:55:28 +00004825 ADDOP_I(c, RERAISE, 1);
Mark Shannonfee55262019-11-21 09:11:43 +00004826 compiler_use_next_block(c, exit);
4827 ADDOP(c, POP_TOP);
4828 ADDOP(c, POP_TOP);
4829 ADDOP(c, POP_TOP);
4830 ADDOP(c, POP_EXCEPT);
4831 ADDOP(c, POP_TOP);
4832 return 1;
4833}
Yury Selivanov75445082015-05-11 22:57:16 -04004834
4835/*
4836 Implements the async with statement.
4837
4838 The semantics outlined in that PEP are as follows:
4839
4840 async with EXPR as VAR:
4841 BLOCK
4842
4843 It is implemented roughly as:
4844
4845 context = EXPR
4846 exit = context.__aexit__ # not calling it
4847 value = await context.__aenter__()
4848 try:
4849 VAR = value # if VAR present in the syntax
4850 BLOCK
4851 finally:
4852 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004853 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04004854 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004855 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04004856 if not (await exit(*exc)):
4857 raise
4858 */
4859static int
4860compiler_async_with(struct compiler *c, stmt_ty s, int pos)
4861{
Mark Shannonfee55262019-11-21 09:11:43 +00004862 basicblock *block, *final, *exit;
Yury Selivanov75445082015-05-11 22:57:16 -04004863 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
4864
4865 assert(s->kind == AsyncWith_kind);
Pablo Galindo90235812020-03-15 04:29:22 +00004866 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07004867 c->u->u_ste->ste_coroutine = 1;
4868 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
Zsolt Dollensteine2396502018-04-27 08:58:56 -07004869 return compiler_error(c, "'async with' outside async function");
4870 }
Yury Selivanov75445082015-05-11 22:57:16 -04004871
4872 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004873 final = compiler_new_block(c);
4874 exit = compiler_new_block(c);
4875 if (!block || !final || !exit)
Yury Selivanov75445082015-05-11 22:57:16 -04004876 return 0;
4877
4878 /* Evaluate EXPR */
4879 VISIT(c, expr, item->context_expr);
4880
4881 ADDOP(c, BEFORE_ASYNC_WITH);
4882 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004883 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004884 ADDOP(c, YIELD_FROM);
4885
Mark Shannon582aaf12020-08-04 17:30:11 +01004886 ADDOP_JUMP(c, SETUP_ASYNC_WITH, final);
Yury Selivanov75445082015-05-11 22:57:16 -04004887
4888 /* SETUP_ASYNC_WITH pushes a finally block. */
4889 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004890 if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004891 return 0;
4892 }
4893
4894 if (item->optional_vars) {
4895 VISIT(c, expr, item->optional_vars);
4896 }
4897 else {
4898 /* Discard result from context.__aenter__() */
4899 ADDOP(c, POP_TOP);
4900 }
4901
4902 pos++;
4903 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
4904 /* BLOCK code */
4905 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
4906 else if (!compiler_async_with(c, s, pos))
4907 return 0;
4908
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004909 compiler_pop_fblock(c, ASYNC_WITH, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004910 ADDOP(c, POP_BLOCK);
4911 /* End of body; start the cleanup */
Yury Selivanov75445082015-05-11 22:57:16 -04004912
Mark Shannonfee55262019-11-21 09:11:43 +00004913 /* For successful outcome:
4914 * call __exit__(None, None, None)
4915 */
4916 if(!compiler_call_exit_with_nones(c))
Yury Selivanov75445082015-05-11 22:57:16 -04004917 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00004918 ADDOP(c, GET_AWAITABLE);
4919 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4920 ADDOP(c, YIELD_FROM);
Yury Selivanov75445082015-05-11 22:57:16 -04004921
Mark Shannonfee55262019-11-21 09:11:43 +00004922 ADDOP(c, POP_TOP);
Yury Selivanov75445082015-05-11 22:57:16 -04004923
Mark Shannon582aaf12020-08-04 17:30:11 +01004924 ADDOP_JUMP(c, JUMP_ABSOLUTE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00004925
4926 /* For exceptional outcome: */
4927 compiler_use_next_block(c, final);
4928
4929 ADDOP(c, WITH_EXCEPT_START);
Yury Selivanov75445082015-05-11 22:57:16 -04004930 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004931 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004932 ADDOP(c, YIELD_FROM);
Mark Shannonfee55262019-11-21 09:11:43 +00004933 compiler_with_except_finish(c);
Yury Selivanov75445082015-05-11 22:57:16 -04004934
Mark Shannonfee55262019-11-21 09:11:43 +00004935compiler_use_next_block(c, exit);
Yury Selivanov75445082015-05-11 22:57:16 -04004936 return 1;
4937}
4938
4939
Guido van Rossumc2e20742006-02-27 22:32:47 +00004940/*
4941 Implements the with statement from PEP 343.
Guido van Rossumc2e20742006-02-27 22:32:47 +00004942 with EXPR as VAR:
4943 BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00004944 is implemented as:
4945 <code for EXPR>
4946 SETUP_WITH E
4947 <code to store to VAR> or POP_TOP
4948 <code for BLOCK>
4949 LOAD_CONST (None, None, None)
4950 CALL_FUNCTION_EX 0
4951 JUMP_FORWARD EXIT
4952 E: WITH_EXCEPT_START (calls EXPR.__exit__)
4953 POP_JUMP_IF_TRUE T:
4954 RERAISE
4955 T: POP_TOP * 3 (remove exception from stack)
4956 POP_EXCEPT
4957 POP_TOP
4958 EXIT:
Guido van Rossumc2e20742006-02-27 22:32:47 +00004959 */
Mark Shannonfee55262019-11-21 09:11:43 +00004960
Guido van Rossumc2e20742006-02-27 22:32:47 +00004961static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004962compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004963{
Mark Shannonfee55262019-11-21 09:11:43 +00004964 basicblock *block, *final, *exit;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004965 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004966
4967 assert(s->kind == With_kind);
4968
Guido van Rossumc2e20742006-02-27 22:32:47 +00004969 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004970 final = compiler_new_block(c);
4971 exit = compiler_new_block(c);
4972 if (!block || !final || !exit)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004973 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004974
Thomas Wouters477c8d52006-05-27 19:21:47 +00004975 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004976 VISIT(c, expr, item->context_expr);
Mark Shannonfee55262019-11-21 09:11:43 +00004977 /* Will push bound __exit__ */
Mark Shannon582aaf12020-08-04 17:30:11 +01004978 ADDOP_JUMP(c, SETUP_WITH, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004979
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004980 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00004981 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004982 if (!compiler_push_fblock(c, WITH, block, final, NULL)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004983 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004984 }
4985
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004986 if (item->optional_vars) {
4987 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004988 }
4989 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004990 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004991 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004992 }
4993
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004994 pos++;
4995 if (pos == asdl_seq_LEN(s->v.With.items))
4996 /* BLOCK code */
4997 VISIT_SEQ(c, stmt, s->v.With.body)
4998 else if (!compiler_with(c, s, pos))
4999 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005000
Mark Shannon3bd60352021-01-13 12:05:43 +00005001
5002 /* Mark all following code as artificial */
5003 c->u->u_lineno = -1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005004 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005005 compiler_pop_fblock(c, WITH, block);
Mark Shannon13bc1392020-01-23 09:25:17 +00005006
Mark Shannonfee55262019-11-21 09:11:43 +00005007 /* End of body; start the cleanup. */
Mark Shannon13bc1392020-01-23 09:25:17 +00005008
Mark Shannonfee55262019-11-21 09:11:43 +00005009 /* For successful outcome:
5010 * call __exit__(None, None, None)
5011 */
5012 if (!compiler_call_exit_with_nones(c))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005013 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00005014 ADDOP(c, POP_TOP);
Mark Shannon582aaf12020-08-04 17:30:11 +01005015 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005016
Mark Shannonfee55262019-11-21 09:11:43 +00005017 /* For exceptional outcome: */
5018 compiler_use_next_block(c, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005019
Mark Shannonfee55262019-11-21 09:11:43 +00005020 ADDOP(c, WITH_EXCEPT_START);
5021 compiler_with_except_finish(c);
5022
5023 compiler_use_next_block(c, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005024 return 1;
5025}
5026
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005027static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005028compiler_visit_expr1(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005030 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07005031 case NamedExpr_kind:
5032 VISIT(c, expr, e->v.NamedExpr.value);
5033 ADDOP(c, DUP_TOP);
5034 VISIT(c, expr, e->v.NamedExpr.target);
5035 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005036 case BoolOp_kind:
5037 return compiler_boolop(c, e);
5038 case BinOp_kind:
5039 VISIT(c, expr, e->v.BinOp.left);
5040 VISIT(c, expr, e->v.BinOp.right);
Andy Lester76d58772020-03-10 21:18:12 -05005041 ADDOP(c, binop(e->v.BinOp.op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005042 break;
5043 case UnaryOp_kind:
5044 VISIT(c, expr, e->v.UnaryOp.operand);
5045 ADDOP(c, unaryop(e->v.UnaryOp.op));
5046 break;
5047 case Lambda_kind:
5048 return compiler_lambda(c, e);
5049 case IfExp_kind:
5050 return compiler_ifexp(c, e);
5051 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005052 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005053 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005054 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005055 case GeneratorExp_kind:
5056 return compiler_genexp(c, e);
5057 case ListComp_kind:
5058 return compiler_listcomp(c, e);
5059 case SetComp_kind:
5060 return compiler_setcomp(c, e);
5061 case DictComp_kind:
5062 return compiler_dictcomp(c, e);
5063 case Yield_kind:
5064 if (c->u->u_ste->ste_type != FunctionBlock)
5065 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005066 if (e->v.Yield.value) {
5067 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005068 }
5069 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005070 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005071 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005072 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005073 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005074 case YieldFrom_kind:
5075 if (c->u->u_ste->ste_type != FunctionBlock)
5076 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04005077
5078 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
5079 return compiler_error(c, "'yield from' inside async function");
5080
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005081 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04005082 ADDOP(c, GET_YIELD_FROM_ITER);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005083 ADDOP_LOAD_CONST(c, Py_None);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005084 ADDOP(c, YIELD_FROM);
5085 break;
Yury Selivanov75445082015-05-11 22:57:16 -04005086 case Await_kind:
Pablo Galindo90235812020-03-15 04:29:22 +00005087 if (!IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005088 if (c->u->u_ste->ste_type != FunctionBlock){
5089 return compiler_error(c, "'await' outside function");
5090 }
Yury Selivanov75445082015-05-11 22:57:16 -04005091
Victor Stinner331a6a52019-05-27 16:39:22 +02005092 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005093 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
5094 return compiler_error(c, "'await' outside async function");
5095 }
5096 }
Yury Selivanov75445082015-05-11 22:57:16 -04005097
5098 VISIT(c, expr, e->v.Await.value);
5099 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005100 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005101 ADDOP(c, YIELD_FROM);
5102 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005103 case Compare_kind:
5104 return compiler_compare(c, e);
5105 case Call_kind:
5106 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005107 case Constant_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005108 ADDOP_LOAD_CONST(c, e->v.Constant.value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005109 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005110 case JoinedStr_kind:
5111 return compiler_joined_str(c, e);
5112 case FormattedValue_kind:
5113 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005114 /* The following exprs can be assignment targets. */
5115 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005116 VISIT(c, expr, e->v.Attribute.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005117 switch (e->v.Attribute.ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005118 case Load:
Mark Shannond48848c2021-03-14 18:01:30 +00005119 {
5120 int old_lineno = c->u->u_lineno;
5121 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005122 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005123 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005124 break;
Mark Shannond48848c2021-03-14 18:01:30 +00005125 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005126 case Store:
Mark Shannond48848c2021-03-14 18:01:30 +00005127 if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005128 return 0;
Mark Shannond48848c2021-03-14 18:01:30 +00005129 }
5130 int old_lineno = c->u->u_lineno;
5131 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005132 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005133 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005134 break;
5135 case Del:
5136 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
5137 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005138 }
5139 break;
5140 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005141 return compiler_subscript(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005142 case Starred_kind:
5143 switch (e->v.Starred.ctx) {
5144 case Store:
5145 /* In all legitimate cases, the Starred node was already replaced
5146 * by compiler_list/compiler_tuple. XXX: is that okay? */
5147 return compiler_error(c,
5148 "starred assignment target must be in a list or tuple");
5149 default:
5150 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005151 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005152 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005153 break;
5154 case Slice_kind:
5155 return compiler_slice(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005156 case Name_kind:
5157 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
5158 /* child nodes of List and Tuple will have expr_context set */
5159 case List_kind:
5160 return compiler_list(c, e);
5161 case Tuple_kind:
5162 return compiler_tuple(c, e);
Brandt Bucher145bf262021-02-26 14:51:55 -08005163 case MatchAs_kind:
5164 case MatchOr_kind:
5165 // Can only occur in patterns, which are handled elsewhere.
5166 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005167 }
5168 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005169}
5170
5171static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005172compiler_visit_expr(struct compiler *c, expr_ty e)
5173{
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005174 int old_lineno = c->u->u_lineno;
5175 int old_col_offset = c->u->u_col_offset;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005176 SET_LOC(c, e);
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005177 int res = compiler_visit_expr1(c, e);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005178 c->u->u_lineno = old_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005179 c->u->u_col_offset = old_col_offset;
5180 return res;
5181}
5182
5183static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005184compiler_augassign(struct compiler *c, stmt_ty s)
5185{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005186 assert(s->kind == AugAssign_kind);
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005187 expr_ty e = s->v.AugAssign.target;
5188
5189 int old_lineno = c->u->u_lineno;
5190 int old_col_offset = c->u->u_col_offset;
5191 SET_LOC(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005193 switch (e->kind) {
5194 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005195 VISIT(c, expr, e->v.Attribute.value);
5196 ADDOP(c, DUP_TOP);
Mark Shannond48848c2021-03-14 18:01:30 +00005197 int old_lineno = c->u->u_lineno;
5198 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005199 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005200 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005201 break;
5202 case Subscript_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005203 VISIT(c, expr, e->v.Subscript.value);
5204 VISIT(c, expr, e->v.Subscript.slice);
5205 ADDOP(c, DUP_TOP_TWO);
5206 ADDOP(c, BINARY_SUBSCR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005207 break;
5208 case Name_kind:
5209 if (!compiler_nameop(c, e->v.Name.id, Load))
5210 return 0;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005211 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005212 default:
5213 PyErr_Format(PyExc_SystemError,
5214 "invalid node type (%d) for augmented assignment",
5215 e->kind);
5216 return 0;
5217 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005218
5219 c->u->u_lineno = old_lineno;
5220 c->u->u_col_offset = old_col_offset;
5221
5222 VISIT(c, expr, s->v.AugAssign.value);
5223 ADDOP(c, inplace_binop(s->v.AugAssign.op));
5224
5225 SET_LOC(c, e);
5226
5227 switch (e->kind) {
5228 case Attribute_kind:
Mark Shannond48848c2021-03-14 18:01:30 +00005229 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005230 ADDOP(c, ROT_TWO);
5231 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5232 break;
5233 case Subscript_kind:
5234 ADDOP(c, ROT_THREE);
5235 ADDOP(c, STORE_SUBSCR);
5236 break;
5237 case Name_kind:
5238 return compiler_nameop(c, e->v.Name.id, Store);
5239 default:
5240 Py_UNREACHABLE();
5241 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005242 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005243}
5244
5245static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005246check_ann_expr(struct compiler *c, expr_ty e)
5247{
5248 VISIT(c, expr, e);
5249 ADDOP(c, POP_TOP);
5250 return 1;
5251}
5252
5253static int
5254check_annotation(struct compiler *c, stmt_ty s)
5255{
5256 /* Annotations are only evaluated in a module or class. */
5257 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5258 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
5259 return check_ann_expr(c, s->v.AnnAssign.annotation);
5260 }
5261 return 1;
5262}
5263
5264static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005265check_ann_subscr(struct compiler *c, expr_ty e)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005266{
5267 /* We check that everything in a subscript is defined at runtime. */
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005268 switch (e->kind) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005269 case Slice_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005270 if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005271 return 0;
5272 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005273 if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) {
5274 return 0;
5275 }
5276 if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) {
5277 return 0;
5278 }
5279 return 1;
5280 case Tuple_kind: {
5281 /* extended slice */
Pablo Galindoa5634c42020-09-16 19:42:00 +01005282 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005283 Py_ssize_t i, n = asdl_seq_LEN(elts);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005284 for (i = 0; i < n; i++) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005285 if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005286 return 0;
5287 }
5288 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005289 return 1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005290 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005291 default:
5292 return check_ann_expr(c, e);
5293 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005294}
5295
5296static int
5297compiler_annassign(struct compiler *c, stmt_ty s)
5298{
5299 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07005300 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005301
5302 assert(s->kind == AnnAssign_kind);
5303
5304 /* We perform the actual assignment first. */
5305 if (s->v.AnnAssign.value) {
5306 VISIT(c, expr, s->v.AnnAssign.value);
5307 VISIT(c, expr, targ);
5308 }
5309 switch (targ->kind) {
5310 case Name_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005311 if (forbidden_name(c, targ->v.Name.id, Store))
5312 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005313 /* If we have a simple name in a module or class, store annotation. */
5314 if (s->v.AnnAssign.simple &&
5315 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5316 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Batuhan Taskaya044a1042020-10-06 23:03:02 +03005317 VISIT(c, annexpr, s->v.AnnAssign.annotation);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005318 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02005319 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005320 ADDOP_LOAD_CONST_NEW(c, mangled);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005321 ADDOP(c, STORE_SUBSCR);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005322 }
5323 break;
5324 case Attribute_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005325 if (forbidden_name(c, targ->v.Attribute.attr, Store))
5326 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005327 if (!s->v.AnnAssign.value &&
5328 !check_ann_expr(c, targ->v.Attribute.value)) {
5329 return 0;
5330 }
5331 break;
5332 case Subscript_kind:
5333 if (!s->v.AnnAssign.value &&
5334 (!check_ann_expr(c, targ->v.Subscript.value) ||
5335 !check_ann_subscr(c, targ->v.Subscript.slice))) {
5336 return 0;
5337 }
5338 break;
5339 default:
5340 PyErr_Format(PyExc_SystemError,
5341 "invalid node type (%d) for annotated assignment",
5342 targ->kind);
5343 return 0;
5344 }
5345 /* Annotation is evaluated last. */
5346 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
5347 return 0;
5348 }
5349 return 1;
5350}
5351
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005352/* Raises a SyntaxError and returns 0.
5353 If something goes wrong, a different exception may be raised.
5354*/
5355
5356static int
Brandt Bucher145bf262021-02-26 14:51:55 -08005357compiler_error(struct compiler *c, const char *format, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005358{
Brandt Bucher145bf262021-02-26 14:51:55 -08005359 va_list vargs;
5360#ifdef HAVE_STDARG_PROTOTYPES
5361 va_start(vargs, format);
5362#else
5363 va_start(vargs);
5364#endif
5365 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5366 va_end(vargs);
5367 if (msg == NULL) {
5368 return 0;
5369 }
5370 PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
5371 if (loc == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005372 Py_INCREF(Py_None);
5373 loc = Py_None;
5374 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005375 PyObject *args = Py_BuildValue("O(OiiO)", msg, c->c_filename,
5376 c->u->u_lineno, c->u->u_col_offset + 1, loc);
5377 Py_DECREF(msg);
5378 if (args == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005379 goto exit;
Brandt Bucher145bf262021-02-26 14:51:55 -08005380 }
5381 PyErr_SetObject(PyExc_SyntaxError, args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005382 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005383 Py_DECREF(loc);
Brandt Bucher145bf262021-02-26 14:51:55 -08005384 Py_XDECREF(args);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005385 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005386}
5387
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005388/* Emits a SyntaxWarning and returns 1 on success.
5389 If a SyntaxWarning raised as error, replaces it with a SyntaxError
5390 and returns 0.
5391*/
5392static int
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005393compiler_warn(struct compiler *c, const char *format, ...)
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005394{
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005395 va_list vargs;
5396#ifdef HAVE_STDARG_PROTOTYPES
5397 va_start(vargs, format);
5398#else
5399 va_start(vargs);
5400#endif
5401 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5402 va_end(vargs);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005403 if (msg == NULL) {
5404 return 0;
5405 }
5406 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
5407 c->u->u_lineno, NULL, NULL) < 0)
5408 {
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005409 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005410 /* Replace the SyntaxWarning exception with a SyntaxError
5411 to get a more accurate error report */
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005412 PyErr_Clear();
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005413 assert(PyUnicode_AsUTF8(msg) != NULL);
5414 compiler_error(c, PyUnicode_AsUTF8(msg));
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005415 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005416 Py_DECREF(msg);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005417 return 0;
5418 }
5419 Py_DECREF(msg);
5420 return 1;
5421}
5422
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005423static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005424compiler_subscript(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005425{
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005426 expr_context_ty ctx = e->v.Subscript.ctx;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005427 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005428
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005429 if (ctx == Load) {
5430 if (!check_subscripter(c, e->v.Subscript.value)) {
5431 return 0;
5432 }
5433 if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
5434 return 0;
5435 }
5436 }
5437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005438 switch (ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005439 case Load: op = BINARY_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005440 case Store: op = STORE_SUBSCR; break;
5441 case Del: op = DELETE_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005442 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005443 assert(op);
5444 VISIT(c, expr, e->v.Subscript.value);
5445 VISIT(c, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005446 ADDOP(c, op);
5447 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005448}
5449
5450static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005451compiler_slice(struct compiler *c, expr_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005453 int n = 2;
5454 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005456 /* only handles the cases where BUILD_SLICE is emitted */
5457 if (s->v.Slice.lower) {
5458 VISIT(c, expr, s->v.Slice.lower);
5459 }
5460 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005461 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005462 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005464 if (s->v.Slice.upper) {
5465 VISIT(c, expr, s->v.Slice.upper);
5466 }
5467 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005468 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005469 }
5470
5471 if (s->v.Slice.step) {
5472 n++;
5473 VISIT(c, expr, s->v.Slice.step);
5474 }
5475 ADDOP_I(c, BUILD_SLICE, n);
5476 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005477}
5478
Brandt Bucher145bf262021-02-26 14:51:55 -08005479
5480// PEP 634: Structural Pattern Matching
5481
5482// To keep things simple, all compiler_pattern_* routines follow the convention
5483// of replacing TOS (the subject for the given pattern) with either True (match)
5484// or False (no match). We do this even for irrefutable patterns; the idea is
5485// that it's much easier to smooth out any redundant pushing, popping, and
5486// jumping in the peephole optimizer than to detect or predict it here.
5487
5488
5489#define WILDCARD_CHECK(N) \
5490 ((N)->kind == Name_kind && \
5491 _PyUnicode_EqualToASCIIString((N)->v.Name.id, "_"))
5492
5493
5494static int
5495pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc)
5496{
5497 assert(!_PyUnicode_EqualToASCIIString(n, "_"));
5498 // Can't assign to the same name twice:
5499 if (pc->stores == NULL) {
5500 RETURN_IF_FALSE(pc->stores = PySet_New(NULL));
5501 }
5502 else {
5503 int duplicate = PySet_Contains(pc->stores, n);
5504 if (duplicate < 0) {
5505 return 0;
5506 }
5507 if (duplicate) {
5508 const char *e = "multiple assignments to name %R in pattern";
5509 return compiler_error(c, e, n);
5510 }
5511 }
5512 RETURN_IF_FALSE(!PySet_Add(pc->stores, n));
5513 RETURN_IF_FALSE(compiler_nameop(c, n, Store));
5514 return 1;
5515}
5516
5517
5518static int
5519pattern_helper_sequence_unpack(struct compiler *c, asdl_expr_seq *values,
5520 Py_ssize_t star, pattern_context *pc)
5521{
5522 RETURN_IF_FALSE(unpack_helper(c, values));
5523 // We've now got a bunch of new subjects on the stack. If any of them fail
5524 // to match, we need to pop everything else off, then finally push False.
5525 // fails is an array of blocks that correspond to the necessary amount of
5526 // popping for each element:
5527 basicblock **fails;
5528 Py_ssize_t size = asdl_seq_LEN(values);
5529 fails = (basicblock **)PyObject_Malloc(sizeof(basicblock*) * size);
5530 if (fails == NULL) {
5531 PyErr_NoMemory();
5532 return 0;
5533 }
5534 // NOTE: Can't use our nice returning macros anymore: they'll leak memory!
5535 // goto error on error.
5536 for (Py_ssize_t i = 0; i < size; i++) {
5537 fails[i] = compiler_new_block(c);
5538 if (fails[i] == NULL) {
5539 goto error;
5540 }
5541 }
5542 for (Py_ssize_t i = 0; i < size; i++) {
5543 expr_ty value = asdl_seq_GET(values, i);
5544 if (i == star) {
5545 assert(value->kind == Starred_kind);
5546 value = value->v.Starred.value;
5547 }
5548 if (!compiler_pattern_subpattern(c, value, pc) ||
5549 !compiler_addop_j(c, POP_JUMP_IF_FALSE, fails[i]) ||
5550 compiler_next_block(c) == NULL)
5551 {
5552 goto error;
5553 }
5554 }
5555 // Success!
5556 basicblock *end = compiler_new_block(c);
5557 if (end == NULL ||
5558 !compiler_addop_load_const(c, Py_True) ||
5559 !compiler_addop_j(c, JUMP_FORWARD, end))
5560 {
5561 goto error;
5562 }
5563 // This is where we handle failed sub-patterns. For a sequence pattern like
5564 // [a, b, c, d], this will look like:
5565 // fails[0]: POP_TOP
5566 // fails[1]: POP_TOP
5567 // fails[2]: POP_TOP
5568 // fails[3]: LOAD_CONST False
5569 for (Py_ssize_t i = 0; i < size - 1; i++) {
5570 compiler_use_next_block(c, fails[i]);
5571 if (!compiler_addop(c, POP_TOP)) {
5572 goto error;
5573 }
5574 }
5575 compiler_use_next_block(c, fails[size - 1]);
5576 if (!compiler_addop_load_const(c, Py_False)) {
5577 goto error;
5578 }
5579 compiler_use_next_block(c, end);
5580 PyObject_Free(fails);
5581 return 1;
5582error:
5583 PyObject_Free(fails);
5584 return 0;
5585}
5586
5587// Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of
5588// UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a
5589// starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc.
5590static int
5591pattern_helper_sequence_subscr(struct compiler *c, asdl_expr_seq *values,
5592 Py_ssize_t star, pattern_context *pc)
5593{
5594 basicblock *end, *fail_pop_1;
5595 RETURN_IF_FALSE(end = compiler_new_block(c));
5596 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5597 Py_ssize_t size = asdl_seq_LEN(values);
5598 for (Py_ssize_t i = 0; i < size; i++) {
5599 expr_ty value = asdl_seq_GET(values, i);
5600 if (WILDCARD_CHECK(value)) {
5601 continue;
5602 }
5603 if (i == star) {
5604 assert(value->kind == Starred_kind);
5605 assert(WILDCARD_CHECK(value->v.Starred.value));
5606 continue;
5607 }
5608 ADDOP(c, DUP_TOP);
5609 if (i < star) {
5610 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5611 }
5612 else {
5613 // The subject may not support negative indexing! Compute a
5614 // nonnegative index:
5615 ADDOP(c, GET_LEN);
5616 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i));
5617 ADDOP(c, BINARY_SUBTRACT);
5618 }
5619 ADDOP(c, BINARY_SUBSCR);
5620 RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc));
5621 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5622 NEXT_BLOCK(c);
5623 }
5624 ADDOP(c, POP_TOP);
5625 ADDOP_LOAD_CONST(c, Py_True);
5626 ADDOP_JUMP(c, JUMP_FORWARD, end);
5627 compiler_use_next_block(c, fail_pop_1);
5628 ADDOP(c, POP_TOP);
5629 ADDOP_LOAD_CONST(c, Py_False);
5630 compiler_use_next_block(c, end);
5631 return 1;
5632}
5633
5634
5635// Like compiler_pattern, but turn off checks for irrefutability.
5636static int
5637compiler_pattern_subpattern(struct compiler *c, expr_ty p, pattern_context *pc)
5638{
5639 int allow_irrefutable = pc->allow_irrefutable;
5640 pc->allow_irrefutable = 1;
5641 RETURN_IF_FALSE(compiler_pattern(c, p, pc));
5642 pc->allow_irrefutable = allow_irrefutable;
5643 return 1;
5644}
5645
5646
5647static int
5648compiler_pattern_as(struct compiler *c, expr_ty p, pattern_context *pc)
5649{
5650 assert(p->kind == MatchAs_kind);
5651 basicblock *end, *fail_pop_1;
5652 RETURN_IF_FALSE(end = compiler_new_block(c));
5653 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5654 // Need to make a copy for (possibly) storing later:
5655 ADDOP(c, DUP_TOP);
5656 RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc));
5657 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5658 NEXT_BLOCK(c);
5659 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc));
5660 ADDOP_LOAD_CONST(c, Py_True);
5661 ADDOP_JUMP(c, JUMP_FORWARD, end);
5662 compiler_use_next_block(c, fail_pop_1);
5663 // Need to pop that unused copy from before:
5664 ADDOP(c, POP_TOP);
5665 ADDOP_LOAD_CONST(c, Py_False);
5666 compiler_use_next_block(c, end);
5667 return 1;
5668}
5669
5670
5671static int
5672compiler_pattern_capture(struct compiler *c, expr_ty p, pattern_context *pc)
5673{
5674 assert(p->kind == Name_kind);
5675 assert(p->v.Name.ctx == Store);
5676 assert(!WILDCARD_CHECK(p));
5677 if (!pc->allow_irrefutable) {
5678 // Whoops, can't have a name capture here!
5679 const char *e = "name capture %R makes remaining patterns unreachable";
5680 return compiler_error(c, e, p->v.Name.id);
5681 }
5682 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.Name.id, pc));
5683 ADDOP_LOAD_CONST(c, Py_True);
5684 return 1;
5685}
5686
5687
5688static int
5689compiler_pattern_class(struct compiler *c, expr_ty p, pattern_context *pc)
5690{
5691 asdl_expr_seq *args = p->v.Call.args;
5692 asdl_keyword_seq *kwargs = p->v.Call.keywords;
5693 Py_ssize_t nargs = asdl_seq_LEN(args);
5694 Py_ssize_t nkwargs = asdl_seq_LEN(kwargs);
5695 if (INT_MAX < nargs || INT_MAX < nargs + nkwargs - 1) {
5696 const char *e = "too many sub-patterns in class pattern %R";
5697 return compiler_error(c, e, p->v.Call.func);
5698 }
5699 RETURN_IF_FALSE(!validate_keywords(c, kwargs));
5700 basicblock *end, *fail_pop_1;
5701 RETURN_IF_FALSE(end = compiler_new_block(c));
5702 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5703 VISIT(c, expr, p->v.Call.func);
5704 PyObject *kwnames;
5705 RETURN_IF_FALSE(kwnames = PyTuple_New(nkwargs));
5706 Py_ssize_t i;
5707 for (i = 0; i < nkwargs; i++) {
5708 PyObject *name = ((keyword_ty) asdl_seq_GET(kwargs, i))->arg;
5709 Py_INCREF(name);
5710 PyTuple_SET_ITEM(kwnames, i, name);
5711 }
5712 ADDOP_LOAD_CONST_NEW(c, kwnames);
5713 ADDOP_I(c, MATCH_CLASS, nargs);
5714 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5715 NEXT_BLOCK(c);
5716 // TOS is now a tuple of (nargs + nkwargs) attributes.
5717 for (i = 0; i < nargs + nkwargs; i++) {
5718 expr_ty arg;
5719 if (i < nargs) {
5720 // Positional:
5721 arg = asdl_seq_GET(args, i);
5722 }
5723 else {
5724 // Keyword:
5725 arg = ((keyword_ty) asdl_seq_GET(kwargs, i - nargs))->value;
5726 }
5727 if (WILDCARD_CHECK(arg)) {
5728 continue;
5729 }
5730 // Get the i-th attribute, and match it against the i-th pattern:
5731 ADDOP(c, DUP_TOP);
5732 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5733 ADDOP(c, BINARY_SUBSCR);
5734 RETURN_IF_FALSE(compiler_pattern_subpattern(c, arg, pc));
5735 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5736 NEXT_BLOCK(c);
5737 }
5738 // Success! Pop the tuple of attributes:
5739 ADDOP(c, POP_TOP);
5740 ADDOP_LOAD_CONST(c, Py_True);
5741 ADDOP_JUMP(c, JUMP_FORWARD, end);
5742 compiler_use_next_block(c, fail_pop_1);
5743 ADDOP(c, POP_TOP);
5744 ADDOP_LOAD_CONST(c, Py_False);
5745 compiler_use_next_block(c, end);
5746 return 1;
5747}
5748
5749
5750static int
5751compiler_pattern_literal(struct compiler *c, expr_ty p, pattern_context *pc)
5752{
5753 assert(p->kind == Constant_kind);
5754 PyObject *v = p->v.Constant.value;
5755 ADDOP_LOAD_CONST(c, v);
5756 // Literal True, False, and None are compared by identity. All others use
5757 // equality:
5758 ADDOP_COMPARE(c, (v == Py_None || PyBool_Check(v)) ? Is : Eq);
5759 return 1;
5760}
5761
5762
5763static int
5764compiler_pattern_mapping(struct compiler *c, expr_ty p, pattern_context *pc)
5765{
5766 basicblock *end, *fail_pop_1, *fail_pop_3;
5767 RETURN_IF_FALSE(end = compiler_new_block(c));
5768 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5769 RETURN_IF_FALSE(fail_pop_3 = compiler_new_block(c));
5770 asdl_expr_seq *keys = p->v.Dict.keys;
5771 asdl_expr_seq *values = p->v.Dict.values;
5772 Py_ssize_t size = asdl_seq_LEN(values);
Ikko Ashimine57827f82021-03-10 19:39:51 +09005773 // A starred pattern will be a keyless value. It is guaranteed to be last:
Brandt Bucher145bf262021-02-26 14:51:55 -08005774 int star = size ? !asdl_seq_GET(keys, size - 1) : 0;
5775 ADDOP(c, MATCH_MAPPING);
5776 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5777 NEXT_BLOCK(c);
5778 if (!size) {
5779 // If the pattern is just "{}", we're done!
5780 ADDOP(c, POP_TOP);
5781 ADDOP_LOAD_CONST(c, Py_True);
5782 ADDOP_JUMP(c, JUMP_FORWARD, end);
5783 compiler_use_next_block(c, fail_pop_1);
5784 ADDOP(c, POP_TOP);
5785 ADDOP_LOAD_CONST(c, Py_False);
5786 compiler_use_next_block(c, end);
5787 return 1;
5788 }
5789 if (size - star) {
5790 // If the pattern has any keys in it, perform a length check:
5791 ADDOP(c, GET_LEN);
5792 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - star));
5793 ADDOP_COMPARE(c, GtE);
5794 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5795 NEXT_BLOCK(c);
5796 }
5797 if (INT_MAX < size - star - 1) {
5798 return compiler_error(c, "too many sub-patterns in mapping pattern");
5799 }
5800 // Collect all of the keys into a tuple for MATCH_KEYS and
5801 // COPY_DICT_WITHOUT_KEYS. They can either be dotted names or literals:
5802 for (Py_ssize_t i = 0; i < size - star; i++) {
5803 expr_ty key = asdl_seq_GET(keys, i);
5804 if (key == NULL) {
5805 const char *e = "can't use starred name here "
5806 "(consider moving to end)";
5807 return compiler_error(c, e);
5808 }
5809 VISIT(c, expr, key);
5810 }
5811 ADDOP_I(c, BUILD_TUPLE, size - star);
5812 ADDOP(c, MATCH_KEYS);
5813 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3);
5814 NEXT_BLOCK(c);
5815 // So far so good. There's now a tuple of values on the stack to match
5816 // sub-patterns against:
5817 for (Py_ssize_t i = 0; i < size - star; i++) {
5818 expr_ty value = asdl_seq_GET(values, i);
5819 if (WILDCARD_CHECK(value)) {
5820 continue;
5821 }
5822 ADDOP(c, DUP_TOP);
5823 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5824 ADDOP(c, BINARY_SUBSCR);
5825 RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc));
5826 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3);
5827 NEXT_BLOCK(c);
5828 }
5829 // If we get this far, it's a match! We're done with that tuple of values.
5830 ADDOP(c, POP_TOP);
5831 if (star) {
5832 // If we had a starred name, bind a dict of remaining items to it:
5833 ADDOP(c, COPY_DICT_WITHOUT_KEYS);
5834 PyObject *id = asdl_seq_GET(values, size - 1)->v.Name.id;
5835 RETURN_IF_FALSE(pattern_helper_store_name(c, id, pc));
5836 }
5837 else {
5838 // Otherwise, we don't care about this tuple of keys anymore:
5839 ADDOP(c, POP_TOP);
5840 }
5841 // Pop the subject:
5842 ADDOP(c, POP_TOP);
5843 ADDOP_LOAD_CONST(c, Py_True);
5844 ADDOP_JUMP(c, JUMP_FORWARD, end);
5845 // The top two items are a tuple of values or None, followed by a tuple of
5846 // keys. Pop them both:
5847 compiler_use_next_block(c, fail_pop_3);
5848 ADDOP(c, POP_TOP);
5849 ADDOP(c, POP_TOP);
5850 compiler_use_next_block(c, fail_pop_1);
5851 // Pop the subject:
5852 ADDOP(c, POP_TOP);
5853 ADDOP_LOAD_CONST(c, Py_False);
5854 compiler_use_next_block(c, end);
5855 return 1;
5856}
5857
5858
5859static int
5860compiler_pattern_or(struct compiler *c, expr_ty p, pattern_context *pc)
5861{
5862 assert(p->kind == MatchOr_kind);
5863 // control is the set of names bound by the first alternative. If all of the
5864 // others bind the same names (they should), then this becomes pc->stores.
5865 PyObject *control = NULL;
5866 basicblock *end, *pass_pop_1;
5867 RETURN_IF_FALSE(end = compiler_new_block(c));
5868 RETURN_IF_FALSE(pass_pop_1 = compiler_new_block(c));
5869 Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns);
5870 assert(size > 1);
5871 // We're going to be messing with pc. Keep the original info handy:
5872 PyObject *stores_init = pc->stores;
5873 int allow_irrefutable = pc->allow_irrefutable;
5874 for (Py_ssize_t i = 0; i < size; i++) {
5875 // NOTE: Can't use our nice returning macros in here: they'll leak sets!
5876 expr_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i);
5877 pc->stores = PySet_New(stores_init);
5878 // An irrefutable sub-pattern must be last, if it is allowed at all:
5879 int is_last = i == size - 1;
5880 pc->allow_irrefutable = allow_irrefutable && is_last;
5881 SET_LOC(c, alt);
5882 if (pc->stores == NULL ||
5883 // Only copy the subject if we're *not* on the last alternative:
5884 (!is_last && !compiler_addop(c, DUP_TOP)) ||
5885 !compiler_pattern(c, alt, pc) ||
5886 // Only jump if we're *not* on the last alternative:
5887 (!is_last && !compiler_addop_j(c, POP_JUMP_IF_TRUE, pass_pop_1)) ||
5888 !compiler_next_block(c))
5889 {
5890 goto fail;
5891 }
5892 if (!i) {
5893 // If this is the first alternative, save its stores as a "control"
5894 // for the others (they can't bind a different set of names):
5895 control = pc->stores;
5896 continue;
5897 }
5898 if (PySet_GET_SIZE(pc->stores) || PySet_GET_SIZE(control)) {
5899 // Otherwise, check to see if we differ from the control set:
5900 PyObject *diff = PyNumber_InPlaceXor(pc->stores, control);
5901 if (diff == NULL) {
5902 goto fail;
5903 }
5904 if (PySet_GET_SIZE(diff)) {
5905 // The names differ! Raise.
5906 Py_DECREF(diff);
5907 compiler_error(c, "alternative patterns bind different names");
5908 goto fail;
5909 }
5910 Py_DECREF(diff);
5911 }
5912 Py_DECREF(pc->stores);
5913 }
5914 Py_XDECREF(stores_init);
5915 // Update pc->stores and restore pc->allow_irrefutable:
5916 pc->stores = control;
5917 pc->allow_irrefutable = allow_irrefutable;
5918 ADDOP_JUMP(c, JUMP_FORWARD, end);
5919 compiler_use_next_block(c, pass_pop_1);
5920 ADDOP(c, POP_TOP);
5921 ADDOP_LOAD_CONST(c, Py_True);
5922 compiler_use_next_block(c, end);
5923 return 1;
5924fail:
5925 Py_XDECREF(stores_init);
5926 Py_XDECREF(control);
5927 return 0;
5928}
5929
5930
5931static int
5932compiler_pattern_sequence(struct compiler *c, expr_ty p, pattern_context *pc)
5933{
5934 assert(p->kind == List_kind || p->kind == Tuple_kind);
5935 asdl_expr_seq *values = (p->kind == Tuple_kind) ? p->v.Tuple.elts
5936 : p->v.List.elts;
5937 Py_ssize_t size = asdl_seq_LEN(values);
5938 Py_ssize_t star = -1;
5939 int only_wildcard = 1;
5940 int star_wildcard = 0;
5941 // Find a starred name, if it exists. There may be at most one:
5942 for (Py_ssize_t i = 0; i < size; i++) {
5943 expr_ty value = asdl_seq_GET(values, i);
5944 if (value->kind == Starred_kind) {
5945 value = value->v.Starred.value;
5946 if (star >= 0) {
5947 const char *e = "multiple starred names in sequence pattern";
5948 return compiler_error(c, e);
5949 }
5950 star_wildcard = WILDCARD_CHECK(value);
5951 star = i;
5952 }
5953 only_wildcard &= WILDCARD_CHECK(value);
5954 }
5955 basicblock *end, *fail_pop_1;
5956 RETURN_IF_FALSE(end = compiler_new_block(c));
5957 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5958 ADDOP(c, MATCH_SEQUENCE);
5959 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5960 NEXT_BLOCK(c);
5961 if (star < 0) {
5962 // No star: len(subject) == size
5963 ADDOP(c, GET_LEN);
5964 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
5965 ADDOP_COMPARE(c, Eq);
5966 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5967 NEXT_BLOCK(c);
5968 }
5969 else if (size > 1) {
5970 // Star: len(subject) >= size - 1
5971 ADDOP(c, GET_LEN);
5972 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1));
5973 ADDOP_COMPARE(c, GtE);
5974 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5975 NEXT_BLOCK(c);
5976 }
5977 if (only_wildcard) {
5978 // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc.
5979 ADDOP(c, POP_TOP);
5980 ADDOP_LOAD_CONST(c, Py_True);
5981 }
5982 else if (star_wildcard) {
5983 RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, values, star, pc));
5984 }
5985 else {
5986 RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, values, star, pc));
5987 }
5988 ADDOP_JUMP(c, JUMP_FORWARD, end);
5989 compiler_use_next_block(c, fail_pop_1);
5990 ADDOP(c, POP_TOP)
5991 ADDOP_LOAD_CONST(c, Py_False);
5992 compiler_use_next_block(c, end);
5993 return 1;
5994}
5995
5996
5997static int
5998compiler_pattern_value(struct compiler *c, expr_ty p, pattern_context *pc)
5999{
6000 assert(p->kind == Attribute_kind);
6001 assert(p->v.Attribute.ctx == Load);
6002 VISIT(c, expr, p);
6003 ADDOP_COMPARE(c, Eq);
6004 return 1;
6005}
6006
6007
6008static int
6009compiler_pattern_wildcard(struct compiler *c, expr_ty p, pattern_context *pc)
6010{
6011 assert(p->kind == Name_kind);
6012 assert(p->v.Name.ctx == Store);
6013 assert(WILDCARD_CHECK(p));
6014 if (!pc->allow_irrefutable) {
6015 // Whoops, can't have a wildcard here!
6016 const char *e = "wildcard makes remaining patterns unreachable";
6017 return compiler_error(c, e);
6018 }
6019 ADDOP(c, POP_TOP);
6020 ADDOP_LOAD_CONST(c, Py_True);
6021 return 1;
6022}
6023
6024
6025static int
6026compiler_pattern(struct compiler *c, expr_ty p, pattern_context *pc)
6027{
6028 SET_LOC(c, p);
6029 switch (p->kind) {
6030 case Attribute_kind:
6031 return compiler_pattern_value(c, p, pc);
6032 case BinOp_kind:
6033 // Because we allow "2+2j", things like "2+2" make it this far:
6034 return compiler_error(c, "patterns cannot include operators");
6035 case Call_kind:
6036 return compiler_pattern_class(c, p, pc);
6037 case Constant_kind:
6038 return compiler_pattern_literal(c, p, pc);
6039 case Dict_kind:
6040 return compiler_pattern_mapping(c, p, pc);
6041 case JoinedStr_kind:
6042 // Because we allow strings, f-strings make it this far:
6043 return compiler_error(c, "patterns cannot include f-strings");
6044 case List_kind:
6045 case Tuple_kind:
6046 return compiler_pattern_sequence(c, p, pc);
6047 case MatchAs_kind:
6048 return compiler_pattern_as(c, p, pc);
6049 case MatchOr_kind:
6050 return compiler_pattern_or(c, p, pc);
6051 case Name_kind:
6052 if (WILDCARD_CHECK(p)) {
6053 return compiler_pattern_wildcard(c, p, pc);
6054 }
6055 return compiler_pattern_capture(c, p, pc);
6056 default:
6057 Py_UNREACHABLE();
6058 }
6059}
6060
6061
6062static int
6063compiler_match(struct compiler *c, stmt_ty s)
6064{
6065 VISIT(c, expr, s->v.Match.subject);
6066 basicblock *next, *end;
6067 RETURN_IF_FALSE(end = compiler_new_block(c));
6068 Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases);
6069 assert(cases);
6070 pattern_context pc;
6071 // We use pc.stores to track:
6072 // - Repeated name assignments in the same pattern.
6073 // - Different name assignments in alternatives.
6074 // It's a set of names, but we don't create it until it's needed:
6075 pc.stores = NULL;
6076 match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6077 int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases;
6078 for (Py_ssize_t i = 0; i < cases - has_default; i++) {
6079 m = asdl_seq_GET(s->v.Match.cases, i);
6080 SET_LOC(c, m->pattern);
6081 RETURN_IF_FALSE(next = compiler_new_block(c));
6082 // If pc.allow_irrefutable is 0, any name captures against our subject
6083 // will raise. Irrefutable cases must be either guarded, last, or both:
6084 pc.allow_irrefutable = m->guard != NULL || i == cases - 1;
6085 // Only copy the subject if we're *not* on the last case:
6086 if (i != cases - has_default - 1) {
6087 ADDOP(c, DUP_TOP);
6088 }
6089 int result = compiler_pattern(c, m->pattern, &pc);
6090 Py_CLEAR(pc.stores);
6091 RETURN_IF_FALSE(result);
6092 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, next);
6093 NEXT_BLOCK(c);
6094 if (m->guard) {
6095 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, next, 0));
6096 }
6097 // Success! Pop the subject off, we're done with it:
6098 if (i != cases - has_default - 1) {
6099 ADDOP(c, POP_TOP);
6100 }
6101 VISIT_SEQ(c, stmt, m->body);
6102 ADDOP_JUMP(c, JUMP_FORWARD, end);
6103 compiler_use_next_block(c, next);
6104 }
6105 if (has_default) {
6106 if (cases == 1) {
6107 // No matches. Done with the subject:
6108 ADDOP(c, POP_TOP);
6109 }
6110 // A trailing "case _" is common, and lets us save a bit of redundant
6111 // pushing and popping in the loop above:
6112 m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6113 SET_LOC(c, m->pattern);
6114 if (m->guard) {
6115 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0));
6116 }
6117 VISIT_SEQ(c, stmt, m->body);
6118 }
6119 compiler_use_next_block(c, end);
6120 return 1;
6121}
6122
6123
6124#undef WILDCARD_CHECK
6125
6126
Thomas Wouters89f507f2006-12-13 04:49:30 +00006127/* End of the compiler section, beginning of the assembler section */
6128
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006129/* do depth-first search of basic block graph, starting with block.
T. Wouters99b54d62019-09-12 07:05:33 -07006130 post records the block indices in post-order.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006131
6132 XXX must handle implicit jumps from one block to next
6133*/
6134
Thomas Wouters89f507f2006-12-13 04:49:30 +00006135struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006136 PyObject *a_bytecode; /* string containing bytecode */
6137 int a_offset; /* offset into bytecode */
6138 int a_nblocks; /* number of reachable blocks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006139 PyObject *a_lnotab; /* string containing lnotab */
6140 int a_lnotab_off; /* offset into lnotab */
Mark Shannon877df852020-11-12 09:43:29 +00006141 int a_prevlineno; /* lineno of last emitted line in line table */
6142 int a_lineno; /* lineno of last emitted instruction */
6143 int a_lineno_start; /* bytecode start offset of current lineno */
Mark Shannoncc75ab72020-11-12 19:49:33 +00006144 basicblock *a_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006145};
6146
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006147Py_LOCAL_INLINE(void)
6148stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006149{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02006150 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
Mark Shannonfee55262019-11-21 09:11:43 +00006151 if (b->b_startdepth < depth && b->b_startdepth < 100) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006152 assert(b->b_startdepth < 0);
6153 b->b_startdepth = depth;
6154 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02006155 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006156}
6157
6158/* Find the flow path that needs the largest stack. We assume that
6159 * cycles in the flow graph have no net effect on the stack depth.
6160 */
6161static int
6162stackdepth(struct compiler *c)
6163{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006164 basicblock *b, *entryblock = NULL;
6165 basicblock **stack, **sp;
6166 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006167 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006168 b->b_startdepth = INT_MIN;
6169 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006170 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006171 }
6172 if (!entryblock)
6173 return 0;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006174 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
6175 if (!stack) {
6176 PyErr_NoMemory();
6177 return -1;
6178 }
6179
6180 sp = stack;
6181 stackdepth_push(&sp, entryblock, 0);
6182 while (sp != stack) {
6183 b = *--sp;
6184 int depth = b->b_startdepth;
6185 assert(depth >= 0);
6186 basicblock *next = b->b_next;
6187 for (int i = 0; i < b->b_iused; i++) {
6188 struct instr *instr = &b->b_instr[i];
6189 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
6190 if (effect == PY_INVALID_STACK_EFFECT) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01006191 PyErr_Format(PyExc_SystemError,
6192 "compiler stack_effect(opcode=%d, arg=%i) failed",
6193 instr->i_opcode, instr->i_oparg);
6194 return -1;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006195 }
6196 int new_depth = depth + effect;
6197 if (new_depth > maxdepth) {
6198 maxdepth = new_depth;
6199 }
6200 assert(depth >= 0); /* invalid code or bug in stackdepth() */
Mark Shannon582aaf12020-08-04 17:30:11 +01006201 if (is_jump(instr)) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006202 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
6203 assert(effect != PY_INVALID_STACK_EFFECT);
6204 int target_depth = depth + effect;
6205 if (target_depth > maxdepth) {
6206 maxdepth = target_depth;
6207 }
6208 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006209 stackdepth_push(&sp, instr->i_target, target_depth);
6210 }
6211 depth = new_depth;
6212 if (instr->i_opcode == JUMP_ABSOLUTE ||
6213 instr->i_opcode == JUMP_FORWARD ||
6214 instr->i_opcode == RETURN_VALUE ||
Mark Shannonfee55262019-11-21 09:11:43 +00006215 instr->i_opcode == RAISE_VARARGS ||
6216 instr->i_opcode == RERAISE)
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006217 {
6218 /* remaining code is dead */
6219 next = NULL;
6220 break;
6221 }
6222 }
6223 if (next != NULL) {
Mark Shannon266b4622020-11-17 19:30:14 +00006224 assert(b->b_nofallthrough == 0);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006225 stackdepth_push(&sp, next, depth);
6226 }
6227 }
6228 PyObject_Free(stack);
6229 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006230}
6231
6232static int
6233assemble_init(struct assembler *a, int nblocks, int firstlineno)
6234{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006235 memset(a, 0, sizeof(struct assembler));
Mark Shannon877df852020-11-12 09:43:29 +00006236 a->a_prevlineno = a->a_lineno = firstlineno;
Mark Shannonfd009e62020-11-13 12:53:53 +00006237 a->a_lnotab = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006238 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006239 if (a->a_bytecode == NULL) {
6240 goto error;
6241 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006242 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006243 if (a->a_lnotab == NULL) {
6244 goto error;
6245 }
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07006246 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006247 PyErr_NoMemory();
Mark Shannonfd009e62020-11-13 12:53:53 +00006248 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006249 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006250 return 1;
Mark Shannonfd009e62020-11-13 12:53:53 +00006251error:
6252 Py_XDECREF(a->a_bytecode);
6253 Py_XDECREF(a->a_lnotab);
6254 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006255}
6256
6257static void
6258assemble_free(struct assembler *a)
6259{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006260 Py_XDECREF(a->a_bytecode);
6261 Py_XDECREF(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006262}
6263
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006264static int
6265blocksize(basicblock *b)
6266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006267 int i;
6268 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006270 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006271 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006272 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006273}
6274
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006275static int
Mark Shannon877df852020-11-12 09:43:29 +00006276assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006277{
Mark Shannon877df852020-11-12 09:43:29 +00006278 Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006279 if (a->a_lnotab_off + 2 >= len) {
6280 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
6281 return 0;
6282 }
Pablo Galindo86e322f2021-01-30 13:54:22 +00006283 unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab);
6284 lnotab += a->a_lnotab_off;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006285 a->a_lnotab_off += 2;
Mark Shannon877df852020-11-12 09:43:29 +00006286 *lnotab++ = bdelta;
6287 *lnotab++ = ldelta;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006288 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006289}
6290
Mark Shannon877df852020-11-12 09:43:29 +00006291/* Appends a range to the end of the line number table. See
6292 * Objects/lnotab_notes.txt for the description of the line number table. */
6293
6294static int
6295assemble_line_range(struct assembler *a)
6296{
6297 int ldelta, bdelta;
6298 bdelta = (a->a_offset - a->a_lineno_start) * 2;
6299 if (bdelta == 0) {
6300 return 1;
6301 }
6302 if (a->a_lineno < 0) {
6303 ldelta = -128;
6304 }
6305 else {
6306 ldelta = a->a_lineno - a->a_prevlineno;
6307 a->a_prevlineno = a->a_lineno;
6308 while (ldelta > 127) {
6309 if (!assemble_emit_linetable_pair(a, 0, 127)) {
6310 return 0;
6311 }
6312 ldelta -= 127;
6313 }
6314 while (ldelta < -127) {
6315 if (!assemble_emit_linetable_pair(a, 0, -127)) {
6316 return 0;
6317 }
6318 ldelta += 127;
6319 }
6320 }
6321 assert(-128 <= ldelta && ldelta < 128);
6322 while (bdelta > 254) {
6323 if (!assemble_emit_linetable_pair(a, 254, ldelta)) {
6324 return 0;
6325 }
6326 ldelta = a->a_lineno < 0 ? -128 : 0;
6327 bdelta -= 254;
6328 }
6329 if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) {
6330 return 0;
6331 }
6332 a->a_lineno_start = a->a_offset;
6333 return 1;
6334}
6335
6336static int
6337assemble_lnotab(struct assembler *a, struct instr *i)
6338{
6339 if (i->i_lineno == a->a_lineno) {
6340 return 1;
6341 }
6342 if (!assemble_line_range(a)) {
6343 return 0;
6344 }
6345 a->a_lineno = i->i_lineno;
6346 return 1;
6347}
6348
6349
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006350/* assemble_emit()
6351 Extend the bytecode with a new instruction.
6352 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00006353*/
6354
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006355static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006356assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006357{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006358 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006359 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03006360 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006361
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006362 arg = i->i_oparg;
6363 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006364 if (i->i_lineno && !assemble_lnotab(a, i))
6365 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006366 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006367 if (len > PY_SSIZE_T_MAX / 2)
6368 return 0;
6369 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
6370 return 0;
6371 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03006372 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006373 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006374 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006375 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006376}
6377
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00006378static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006379assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006380{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006381 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006382 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006383 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00006384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006385 /* Compute the size of each block and fixup jump args.
6386 Replace block pointer with position in bytecode. */
6387 do {
6388 totsize = 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006389 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006390 bsize = blocksize(b);
6391 b->b_offset = totsize;
6392 totsize += bsize;
6393 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006394 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006395 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6396 bsize = b->b_offset;
6397 for (i = 0; i < b->b_iused; i++) {
6398 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006399 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006400 /* Relative jumps are computed relative to
6401 the instruction pointer after fetching
6402 the jump instruction.
6403 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006404 bsize += isize;
Mark Shannon582aaf12020-08-04 17:30:11 +01006405 if (is_jump(instr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006406 instr->i_oparg = instr->i_target->b_offset;
Mark Shannon582aaf12020-08-04 17:30:11 +01006407 if (is_relative_jump(instr)) {
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006408 instr->i_oparg -= bsize;
6409 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03006410 instr->i_oparg *= sizeof(_Py_CODEUNIT);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006411 if (instrsize(instr->i_oparg) != isize) {
6412 extended_arg_recompile = 1;
6413 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006414 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006415 }
6416 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00006417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006418 /* XXX: This is an awful hack that could hurt performance, but
6419 on the bright side it should work until we come up
6420 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006422 The issue is that in the first loop blocksize() is called
6423 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006424 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006425 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006427 So we loop until we stop seeing new EXTENDED_ARGs.
6428 The only EXTENDED_ARGs that could be popping up are
6429 ones in jump instructions. So this should converge
6430 fairly quickly.
6431 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006432 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00006433}
6434
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006435static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01006436dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006437{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006438 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006439 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006441 tuple = PyTuple_New(size);
6442 if (tuple == NULL)
6443 return NULL;
6444 while (PyDict_Next(dict, &pos, &k, &v)) {
6445 i = PyLong_AS_LONG(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006446 Py_INCREF(k);
6447 assert((i - offset) < size);
6448 assert((i - offset) >= 0);
6449 PyTuple_SET_ITEM(tuple, i - offset, k);
6450 }
6451 return tuple;
6452}
6453
6454static PyObject *
6455consts_dict_keys_inorder(PyObject *dict)
6456{
6457 PyObject *consts, *k, *v;
6458 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
6459
6460 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
6461 if (consts == NULL)
6462 return NULL;
6463 while (PyDict_Next(dict, &pos, &k, &v)) {
6464 i = PyLong_AS_LONG(v);
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +03006465 /* The keys of the dictionary can be tuples wrapping a contant.
6466 * (see compiler_add_o and _PyCode_ConstantKey). In that case
6467 * the object we want is always second. */
6468 if (PyTuple_CheckExact(k)) {
6469 k = PyTuple_GET_ITEM(k, 1);
6470 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006471 Py_INCREF(k);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006472 assert(i < size);
6473 assert(i >= 0);
6474 PyList_SET_ITEM(consts, i, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006475 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006476 return consts;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006477}
6478
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006479static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006480compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006481{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006482 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006483 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006484 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04006485 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006486 if (ste->ste_nested)
6487 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07006488 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006489 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07006490 if (!ste->ste_generator && ste->ste_coroutine)
6491 flags |= CO_COROUTINE;
6492 if (ste->ste_generator && ste->ste_coroutine)
6493 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006494 if (ste->ste_varargs)
6495 flags |= CO_VARARGS;
6496 if (ste->ste_varkeywords)
6497 flags |= CO_VARKEYWORDS;
6498 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006500 /* (Only) inherit compilerflags in PyCF_MASK */
6501 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006502
Pablo Galindo90235812020-03-15 04:29:22 +00006503 if ((IS_TOP_LEVEL_AWAIT(c)) &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07006504 ste->ste_coroutine &&
6505 !ste->ste_generator) {
6506 flags |= CO_COROUTINE;
6507 }
6508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006509 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00006510}
6511
Inada Naokibdb941b2021-02-10 09:20:42 +09006512// Merge *obj* with constant cache.
INADA Naokic2e16072018-11-26 21:23:22 +09006513// Unlike merge_consts_recursive(), this function doesn't work recursively.
6514static int
Inada Naokibdb941b2021-02-10 09:20:42 +09006515merge_const_one(struct compiler *c, PyObject **obj)
INADA Naokic2e16072018-11-26 21:23:22 +09006516{
Inada Naokibdb941b2021-02-10 09:20:42 +09006517 PyObject *key = _PyCode_ConstantKey(*obj);
INADA Naokic2e16072018-11-26 21:23:22 +09006518 if (key == NULL) {
6519 return 0;
6520 }
6521
6522 // t is borrowed reference
6523 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
6524 Py_DECREF(key);
6525 if (t == NULL) {
6526 return 0;
6527 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006528 if (t == key) { // obj is new constant.
INADA Naokic2e16072018-11-26 21:23:22 +09006529 return 1;
6530 }
6531
Inada Naokibdb941b2021-02-10 09:20:42 +09006532 if (PyTuple_CheckExact(t)) {
6533 // t is still borrowed reference
6534 t = PyTuple_GET_ITEM(t, 1);
6535 }
6536
6537 Py_INCREF(t);
6538 Py_DECREF(*obj);
6539 *obj = t;
INADA Naokic2e16072018-11-26 21:23:22 +09006540 return 1;
6541}
6542
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006543static PyCodeObject *
Mark Shannon6e8128f2020-07-30 10:03:00 +01006544makecode(struct compiler *c, struct assembler *a, PyObject *consts)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006546 PyCodeObject *co = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006547 PyObject *names = NULL;
6548 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006549 PyObject *name = NULL;
6550 PyObject *freevars = NULL;
6551 PyObject *cellvars = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006552 Py_ssize_t nlocals;
6553 int nlocals_int;
6554 int flags;
Pablo Galindocd74e662019-06-01 18:08:04 +01006555 int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006557 names = dict_keys_inorder(c->u->u_names, 0);
6558 varnames = dict_keys_inorder(c->u->u_varnames, 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006559 if (!names || !varnames) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006560 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006561 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006562 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
6563 if (!cellvars)
6564 goto error;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006565 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006566 if (!freevars)
6567 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006568
Inada Naokibdb941b2021-02-10 09:20:42 +09006569 if (!merge_const_one(c, &names) ||
6570 !merge_const_one(c, &varnames) ||
6571 !merge_const_one(c, &cellvars) ||
6572 !merge_const_one(c, &freevars))
INADA Naokic2e16072018-11-26 21:23:22 +09006573 {
6574 goto error;
6575 }
6576
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006577 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01006578 assert(nlocals < INT_MAX);
6579 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
6580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006581 flags = compute_code_flags(c);
6582 if (flags < 0)
6583 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006584
Mark Shannon6e8128f2020-07-30 10:03:00 +01006585 consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
6586 if (consts == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006587 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006588 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006589 if (!merge_const_one(c, &consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006590 Py_DECREF(consts);
INADA Naokic2e16072018-11-26 21:23:22 +09006591 goto error;
6592 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006593
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01006594 posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int);
Pablo Galindocd74e662019-06-01 18:08:04 +01006595 posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +01006596 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006597 maxdepth = stackdepth(c);
6598 if (maxdepth < 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006599 Py_DECREF(consts);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006600 goto error;
6601 }
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006602 co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
Mark Shannon13bc1392020-01-23 09:25:17 +00006603 posonlyargcount, kwonlyargcount, nlocals_int,
Mark Shannon6e8128f2020-07-30 10:03:00 +01006604 maxdepth, flags, a->a_bytecode, consts, names,
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006605 varnames, freevars, cellvars, c->c_filename,
6606 c->u->u_name, c->u->u_firstlineno, a->a_lnotab);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006607 Py_DECREF(consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006608 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006609 Py_XDECREF(names);
6610 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006611 Py_XDECREF(name);
6612 Py_XDECREF(freevars);
6613 Py_XDECREF(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006614 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006615}
6616
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006617
6618/* For debugging purposes only */
6619#if 0
6620static void
6621dump_instr(const struct instr *i)
6622{
Mark Shannon582aaf12020-08-04 17:30:11 +01006623 const char *jrel = (is_relative_jump(instr)) ? "jrel " : "";
6624 const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : "";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006625 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006627 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006628 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006629 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006630 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006631 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
6632 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006633}
6634
6635static void
6636dump_basicblock(const basicblock *b)
6637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006638 const char *b_return = b->b_return ? "return " : "";
Pablo Galindo60eb9f12020-06-28 01:55:47 +01006639 fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n",
6640 b->b_iused, b->b_startdepth, b->b_offset, b_return);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006641 if (b->b_instr) {
6642 int i;
6643 for (i = 0; i < b->b_iused; i++) {
6644 fprintf(stderr, " [%02d] ", i);
6645 dump_instr(b->b_instr + i);
6646 }
6647 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006648}
6649#endif
6650
Mark Shannon5977a792020-12-02 13:31:40 +00006651
6652static int
6653normalize_basic_block(basicblock *bb);
6654
Mark Shannon6e8128f2020-07-30 10:03:00 +01006655static int
6656optimize_cfg(struct assembler *a, PyObject *consts);
6657
Mark Shannon5977a792020-12-02 13:31:40 +00006658static int
6659ensure_exits_have_lineno(struct compiler *c);
6660
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006661static PyCodeObject *
6662assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006664 basicblock *b, *entryblock;
6665 struct assembler a;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006666 int j, nblocks;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006667 PyCodeObject *co = NULL;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006668 PyObject *consts = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006670 /* Make sure every block that falls off the end returns None.
6671 XXX NEXT_BLOCK() isn't quite right, because if the last
6672 block ends with a jump or return b_next shouldn't set.
6673 */
6674 if (!c->u->u_curblock->b_return) {
Mark Shannon877df852020-11-12 09:43:29 +00006675 c->u->u_lineno = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006676 if (addNone)
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006677 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006678 ADDOP(c, RETURN_VALUE);
6679 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006680
Mark Shannon5977a792020-12-02 13:31:40 +00006681 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
6682 if (normalize_basic_block(b)) {
Alex Henrie503627f2021-03-02 03:20:25 -07006683 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00006684 }
6685 }
6686
6687 if (ensure_exits_have_lineno(c)) {
Alex Henrie503627f2021-03-02 03:20:25 -07006688 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00006689 }
6690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006691 nblocks = 0;
6692 entryblock = NULL;
6693 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6694 nblocks++;
6695 entryblock = b;
6696 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006698 /* Set firstlineno if it wasn't explicitly set. */
6699 if (!c->u->u_firstlineno) {
Ned Deilydc35cda2016-08-17 17:18:33 -04006700 if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006701 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
Mark Shannon877df852020-11-12 09:43:29 +00006702 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006703 c->u->u_firstlineno = 1;
6704 }
Mark Shannon5977a792020-12-02 13:31:40 +00006705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006706 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
6707 goto error;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006708 a.a_entry = entryblock;
6709 a.a_nblocks = nblocks;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006710
Mark Shannon6e8128f2020-07-30 10:03:00 +01006711 consts = consts_dict_keys_inorder(c->u->u_consts);
6712 if (consts == NULL) {
6713 goto error;
6714 }
6715 if (optimize_cfg(&a, consts)) {
6716 goto error;
6717 }
6718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006719 /* Can't modify the bytecode after computing jump offsets. */
6720 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00006721
Mark Shannoncc75ab72020-11-12 19:49:33 +00006722 /* Emit code. */
6723 for(b = entryblock; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006724 for (j = 0; j < b->b_iused; j++)
6725 if (!assemble_emit(&a, &b->b_instr[j]))
6726 goto error;
6727 }
Mark Shannon877df852020-11-12 09:43:29 +00006728 if (!assemble_line_range(&a)) {
6729 return 0;
6730 }
6731 /* Emit sentinel at end of line number table */
6732 if (!assemble_emit_linetable_pair(&a, 255, -128)) {
6733 goto error;
6734 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00006735
Inada Naokibdb941b2021-02-10 09:20:42 +09006736 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006737 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09006738 }
6739 if (!merge_const_one(c, &a.a_lnotab)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006740 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09006741 }
6742 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) {
6743 goto error;
6744 }
6745 if (!merge_const_one(c, &a.a_bytecode)) {
6746 goto error;
6747 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006748
Mark Shannon6e8128f2020-07-30 10:03:00 +01006749 co = makecode(c, &a, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006750 error:
Mark Shannon6e8128f2020-07-30 10:03:00 +01006751 Py_XDECREF(consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006752 assemble_free(&a);
6753 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006754}
Georg Brandl8334fd92010-12-04 10:26:46 +00006755
6756#undef PyAST_Compile
Benjamin Petersone5024512018-09-12 12:06:42 -07006757PyCodeObject *
Georg Brandl8334fd92010-12-04 10:26:46 +00006758PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
6759 PyArena *arena)
6760{
6761 return PyAST_CompileEx(mod, filename, flags, -1, arena);
6762}
Mark Shannon6e8128f2020-07-30 10:03:00 +01006763
6764
6765/* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
6766 with LOAD_CONST (c1, c2, ... cn).
6767 The consts table must still be in list form so that the
6768 new constant (c1, c2, ... cn) can be appended.
6769 Called with codestr pointing to the first LOAD_CONST.
6770*/
6771static int
6772fold_tuple_on_constants(struct instr *inst,
6773 int n, PyObject *consts)
6774{
6775 /* Pre-conditions */
6776 assert(PyList_CheckExact(consts));
6777 assert(inst[n].i_opcode == BUILD_TUPLE);
6778 assert(inst[n].i_oparg == n);
6779
6780 for (int i = 0; i < n; i++) {
6781 if (inst[i].i_opcode != LOAD_CONST) {
6782 return 0;
6783 }
6784 }
6785
6786 /* Buildup new tuple of constants */
6787 PyObject *newconst = PyTuple_New(n);
6788 if (newconst == NULL) {
6789 return -1;
6790 }
6791 for (int i = 0; i < n; i++) {
6792 int arg = inst[i].i_oparg;
6793 PyObject *constant = PyList_GET_ITEM(consts, arg);
6794 Py_INCREF(constant);
6795 PyTuple_SET_ITEM(newconst, i, constant);
6796 }
6797 Py_ssize_t index = PyList_GET_SIZE(consts);
Victor Stinner71f2ff42020-09-23 14:06:55 +02006798 if ((size_t)index >= (size_t)INT_MAX - 1) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006799 Py_DECREF(newconst);
6800 PyErr_SetString(PyExc_OverflowError, "too many constants");
6801 return -1;
6802 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006803 if (PyList_Append(consts, newconst)) {
6804 Py_DECREF(newconst);
6805 return -1;
6806 }
6807 Py_DECREF(newconst);
6808 for (int i = 0; i < n; i++) {
6809 inst[i].i_opcode = NOP;
6810 }
6811 inst[n].i_opcode = LOAD_CONST;
Victor Stinner71f2ff42020-09-23 14:06:55 +02006812 inst[n].i_oparg = (int)index;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006813 return 0;
6814}
6815
Mark Shannon28b75c82020-12-23 11:43:10 +00006816
6817static int
6818eliminate_jump_to_jump(basicblock *bb, int opcode) {
6819 assert (bb->b_iused > 0);
6820 struct instr *inst = &bb->b_instr[bb->b_iused-1];
6821 assert (is_jump(inst));
6822 assert (inst->i_target->b_iused > 0);
6823 struct instr *target = &inst->i_target->b_instr[0];
6824 if (inst->i_target == target->i_target) {
6825 /* Nothing to do */
6826 return 0;
6827 }
6828 int lineno = target->i_lineno;
6829 if (add_jump_to_block(bb, opcode, lineno, target->i_target) == 0) {
6830 return -1;
6831 }
6832 assert (bb->b_iused >= 2);
6833 bb->b_instr[bb->b_iused-2].i_opcode = NOP;
6834 return 0;
6835}
6836
Mark Shannoncc75ab72020-11-12 19:49:33 +00006837/* Maximum size of basic block that should be copied in optimizer */
6838#define MAX_COPY_SIZE 4
Mark Shannon6e8128f2020-07-30 10:03:00 +01006839
6840/* Optimization */
6841static int
6842optimize_basic_block(basicblock *bb, PyObject *consts)
6843{
6844 assert(PyList_CheckExact(consts));
6845 struct instr nop;
6846 nop.i_opcode = NOP;
6847 struct instr *target;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006848 for (int i = 0; i < bb->b_iused; i++) {
6849 struct instr *inst = &bb->b_instr[i];
6850 int oparg = inst->i_oparg;
6851 int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01006852 if (is_jump(inst)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006853 /* Skip over empty basic blocks. */
6854 while (inst->i_target->b_iused == 0) {
6855 inst->i_target = inst->i_target->b_next;
6856 }
6857 target = &inst->i_target->b_instr[0];
6858 }
6859 else {
6860 target = &nop;
6861 }
6862 switch (inst->i_opcode) {
Mark Shannon266b4622020-11-17 19:30:14 +00006863 /* Remove LOAD_CONST const; conditional jump */
Mark Shannon6e8128f2020-07-30 10:03:00 +01006864 case LOAD_CONST:
Mark Shannon266b4622020-11-17 19:30:14 +00006865 {
6866 PyObject* cnt;
6867 int is_true;
6868 int jump_if_true;
6869 switch(nextop) {
6870 case POP_JUMP_IF_FALSE:
6871 case POP_JUMP_IF_TRUE:
6872 cnt = PyList_GET_ITEM(consts, oparg);
6873 is_true = PyObject_IsTrue(cnt);
6874 if (is_true == -1) {
6875 goto error;
6876 }
6877 inst->i_opcode = NOP;
6878 jump_if_true = nextop == POP_JUMP_IF_TRUE;
6879 if (is_true == jump_if_true) {
6880 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
6881 bb->b_nofallthrough = 1;
6882 }
6883 else {
6884 bb->b_instr[i+1].i_opcode = NOP;
6885 }
6886 break;
6887 case JUMP_IF_FALSE_OR_POP:
6888 case JUMP_IF_TRUE_OR_POP:
6889 cnt = PyList_GET_ITEM(consts, oparg);
6890 is_true = PyObject_IsTrue(cnt);
6891 if (is_true == -1) {
6892 goto error;
6893 }
6894 jump_if_true = nextop == JUMP_IF_TRUE_OR_POP;
6895 if (is_true == jump_if_true) {
6896 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
6897 bb->b_nofallthrough = 1;
6898 }
6899 else {
6900 inst->i_opcode = NOP;
6901 bb->b_instr[i+1].i_opcode = NOP;
6902 }
6903 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006904 }
6905 break;
Mark Shannon266b4622020-11-17 19:30:14 +00006906 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006907
6908 /* Try to fold tuples of constants.
6909 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
6910 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
6911 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
6912 case BUILD_TUPLE:
6913 if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
6914 switch(oparg) {
6915 case 1:
6916 inst->i_opcode = NOP;
6917 bb->b_instr[i+1].i_opcode = NOP;
6918 break;
6919 case 2:
6920 inst->i_opcode = ROT_TWO;
6921 bb->b_instr[i+1].i_opcode = NOP;
6922 break;
6923 case 3:
6924 inst->i_opcode = ROT_THREE;
6925 bb->b_instr[i+1].i_opcode = ROT_TWO;
6926 }
6927 break;
6928 }
6929 if (i >= oparg) {
6930 if (fold_tuple_on_constants(inst-oparg, oparg, consts)) {
6931 goto error;
6932 }
6933 }
6934 break;
6935
6936 /* Simplify conditional jump to conditional jump where the
6937 result of the first test implies the success of a similar
6938 test or the failure of the opposite test.
6939 Arises in code like:
6940 "a and b or c"
6941 "(a and b) and c"
6942 "(a or b) or c"
6943 "(a or b) and c"
6944 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
6945 --> x:JUMP_IF_FALSE_OR_POP z
6946 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
6947 --> x:POP_JUMP_IF_FALSE y+1
6948 where y+1 is the instruction following the second test.
6949 */
6950 case JUMP_IF_FALSE_OR_POP:
6951 switch(target->i_opcode) {
6952 case POP_JUMP_IF_FALSE:
Mark Shannon28b75c82020-12-23 11:43:10 +00006953 if (inst->i_lineno == target->i_lineno) {
6954 *inst = *target;
6955 i--;
6956 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006957 break;
6958 case JUMP_ABSOLUTE:
6959 case JUMP_FORWARD:
6960 case JUMP_IF_FALSE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00006961 if (inst->i_lineno == target->i_lineno &&
6962 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00006963 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00006964 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00006965 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006966 break;
6967 case JUMP_IF_TRUE_OR_POP:
6968 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00006969 if (inst->i_lineno == target->i_lineno) {
6970 inst->i_opcode = POP_JUMP_IF_FALSE;
6971 inst->i_target = inst->i_target->b_next;
6972 --i;
6973 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006974 break;
6975 }
6976 break;
6977
6978 case JUMP_IF_TRUE_OR_POP:
6979 switch(target->i_opcode) {
6980 case POP_JUMP_IF_TRUE:
Mark Shannon28b75c82020-12-23 11:43:10 +00006981 if (inst->i_lineno == target->i_lineno) {
6982 *inst = *target;
6983 i--;
6984 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006985 break;
6986 case JUMP_ABSOLUTE:
6987 case JUMP_FORWARD:
6988 case JUMP_IF_TRUE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00006989 if (inst->i_lineno == target->i_lineno &&
6990 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00006991 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00006992 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00006993 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006994 break;
6995 case JUMP_IF_FALSE_OR_POP:
6996 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00006997 if (inst->i_lineno == target->i_lineno) {
6998 inst->i_opcode = POP_JUMP_IF_TRUE;
6999 inst->i_target = inst->i_target->b_next;
7000 --i;
7001 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007002 break;
7003 }
7004 break;
7005
7006 case POP_JUMP_IF_FALSE:
7007 switch(target->i_opcode) {
7008 case JUMP_ABSOLUTE:
7009 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007010 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00007011 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007012 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007013 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007014 break;
7015 }
7016 break;
7017
7018 case POP_JUMP_IF_TRUE:
7019 switch(target->i_opcode) {
7020 case JUMP_ABSOLUTE:
7021 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007022 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00007023 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007024 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007025 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007026 break;
7027 }
7028 break;
7029
7030 case JUMP_ABSOLUTE:
7031 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007032 assert (i == bb->b_iused-1);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007033 switch(target->i_opcode) {
7034 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007035 if (eliminate_jump_to_jump(bb, inst->i_opcode)) {
7036 goto error;
Mark Shannon266b4622020-11-17 19:30:14 +00007037 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007038 break;
Mark Shannon28b75c82020-12-23 11:43:10 +00007039
Mark Shannon6e8128f2020-07-30 10:03:00 +01007040 case JUMP_ABSOLUTE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007041 if (eliminate_jump_to_jump(bb, JUMP_ABSOLUTE)) {
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 default:
7046 if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) {
7047 basicblock *to_copy = inst->i_target;
7048 inst->i_opcode = NOP;
7049 for (i = 0; i < to_copy->b_iused; i++) {
7050 int index = compiler_next_instr(bb);
7051 if (index < 0) {
7052 return -1;
7053 }
7054 bb->b_instr[index] = to_copy->b_instr[i];
7055 }
7056 bb->b_exit = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007057 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007058 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007059 }
7060 }
7061 return 0;
7062error:
7063 return -1;
7064}
7065
7066
7067static void
Mark Shannon1659ad12021-01-13 15:05:04 +00007068clean_basic_block(basicblock *bb, int prev_lineno) {
7069 /* Remove NOPs when legal to do so. */
Mark Shannon6e8128f2020-07-30 10:03:00 +01007070 int dest = 0;
7071 for (int src = 0; src < bb->b_iused; src++) {
Mark Shannon877df852020-11-12 09:43:29 +00007072 int lineno = bb->b_instr[src].i_lineno;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007073 if (bb->b_instr[src].i_opcode == NOP) {
Mark Shannon266b4622020-11-17 19:30:14 +00007074 /* Eliminate no-op if it doesn't have a line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007075 if (lineno < 0) {
7076 continue;
7077 }
Mark Shannon266b4622020-11-17 19:30:14 +00007078 /* or, if the previous instruction had the same line number. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007079 if (prev_lineno == lineno) {
7080 continue;
7081 }
Mark Shannon266b4622020-11-17 19:30:14 +00007082 /* or, if the next instruction has same line number or no line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007083 if (src < bb->b_iused - 1) {
7084 int next_lineno = bb->b_instr[src+1].i_lineno;
7085 if (next_lineno < 0 || next_lineno == lineno) {
7086 bb->b_instr[src+1].i_lineno = lineno;
7087 continue;
Mark Shannon877df852020-11-12 09:43:29 +00007088 }
7089 }
Mark Shannon266b4622020-11-17 19:30:14 +00007090 else {
7091 basicblock* next = bb->b_next;
7092 while (next && next->b_iused == 0) {
7093 next = next->b_next;
7094 }
7095 /* or if last instruction in BB and next BB has same line number */
7096 if (next) {
7097 if (lineno == next->b_instr[0].i_lineno) {
7098 continue;
7099 }
7100 }
7101 }
7102
Mark Shannon6e8128f2020-07-30 10:03:00 +01007103 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007104 if (dest != src) {
7105 bb->b_instr[dest] = bb->b_instr[src];
7106 }
7107 dest++;
7108 prev_lineno = lineno;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007109 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007110 assert(dest <= bb->b_iused);
7111 bb->b_iused = dest;
7112}
7113
Mark Shannon266b4622020-11-17 19:30:14 +00007114static int
7115normalize_basic_block(basicblock *bb) {
7116 /* Mark blocks as exit and/or nofallthrough.
7117 Raise SystemError if CFG is malformed. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007118 for (int i = 0; i < bb->b_iused; i++) {
7119 switch(bb->b_instr[i].i_opcode) {
7120 case RETURN_VALUE:
7121 case RAISE_VARARGS:
7122 case RERAISE:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007123 bb->b_exit = 1;
Mark Shannon5977a792020-12-02 13:31:40 +00007124 bb->b_nofallthrough = 1;
7125 break;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007126 case JUMP_ABSOLUTE:
7127 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007128 bb->b_nofallthrough = 1;
Mark Shannon266b4622020-11-17 19:30:14 +00007129 /* fall through */
7130 case POP_JUMP_IF_FALSE:
7131 case POP_JUMP_IF_TRUE:
7132 case JUMP_IF_FALSE_OR_POP:
7133 case JUMP_IF_TRUE_OR_POP:
Mark Shannon5977a792020-12-02 13:31:40 +00007134 case FOR_ITER:
Mark Shannon266b4622020-11-17 19:30:14 +00007135 if (i != bb->b_iused-1) {
7136 PyErr_SetString(PyExc_SystemError, "malformed control flow graph.");
7137 return -1;
7138 }
Mark Shannon5977a792020-12-02 13:31:40 +00007139 /* Skip over empty basic blocks. */
7140 while (bb->b_instr[i].i_target->b_iused == 0) {
7141 bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next;
7142 }
7143
Mark Shannoncc75ab72020-11-12 19:49:33 +00007144 }
7145 }
Mark Shannon266b4622020-11-17 19:30:14 +00007146 return 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007147}
7148
Mark Shannon6e8128f2020-07-30 10:03:00 +01007149static int
7150mark_reachable(struct assembler *a) {
7151 basicblock **stack, **sp;
7152 sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks);
7153 if (stack == NULL) {
7154 return -1;
7155 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007156 a->a_entry->b_predecessors = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007157 *sp++ = a->a_entry;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007158 while (sp > stack) {
7159 basicblock *b = *(--sp);
Mark Shannon3bd60352021-01-13 12:05:43 +00007160 if (b->b_next && !b->b_nofallthrough) {
7161 if (b->b_next->b_predecessors == 0) {
7162 *sp++ = b->b_next;
7163 }
7164 b->b_next->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007165 }
7166 for (int i = 0; i < b->b_iused; i++) {
7167 basicblock *target;
Mark Shannon582aaf12020-08-04 17:30:11 +01007168 if (is_jump(&b->b_instr[i])) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007169 target = b->b_instr[i].i_target;
Mark Shannon3bd60352021-01-13 12:05:43 +00007170 if (target->b_predecessors == 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007171 *sp++ = target;
7172 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007173 target->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007174 }
7175 }
7176 }
7177 PyObject_Free(stack);
7178 return 0;
7179}
7180
Mark Shannon3bd60352021-01-13 12:05:43 +00007181static void
7182eliminate_empty_basic_blocks(basicblock *entry) {
7183 /* Eliminate empty blocks */
7184 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7185 basicblock *next = b->b_next;
7186 if (next) {
7187 while (next->b_iused == 0 && next->b_next) {
7188 next = next->b_next;
7189 }
7190 b->b_next = next;
7191 }
7192 }
7193 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7194 if (b->b_iused == 0) {
7195 continue;
7196 }
7197 if (is_jump(&b->b_instr[b->b_iused-1])) {
7198 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7199 while (target->b_iused == 0) {
7200 target = target->b_next;
7201 }
7202 b->b_instr[b->b_iused-1].i_target = target;
7203 }
7204 }
7205}
7206
7207
Mark Shannon5977a792020-12-02 13:31:40 +00007208/* If an instruction has no line number, but it's predecessor in the BB does,
Mark Shannon3bd60352021-01-13 12:05:43 +00007209 * then copy the line number. If a successor block has no line number, and only
7210 * one predecessor, then inherit the line number.
7211 * This ensures that all exit blocks (with one predecessor) receive a line number.
7212 * Also reduces the size of the line number table,
Mark Shannon5977a792020-12-02 13:31:40 +00007213 * but has no impact on the generated line number events.
7214 */
7215static void
Mark Shannon3bd60352021-01-13 12:05:43 +00007216propogate_line_numbers(struct assembler *a) {
Mark Shannon5977a792020-12-02 13:31:40 +00007217 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007218 if (b->b_iused == 0) {
7219 continue;
7220 }
Mark Shannon5977a792020-12-02 13:31:40 +00007221 int prev_lineno = -1;
7222 for (int i = 0; i < b->b_iused; i++) {
7223 if (b->b_instr[i].i_lineno < 0) {
7224 b->b_instr[i].i_lineno = prev_lineno;
7225 }
7226 else {
7227 prev_lineno = b->b_instr[i].i_lineno;
7228 }
7229 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007230 if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) {
7231 assert(b->b_next->b_iused);
7232 if (b->b_next->b_instr[0].i_lineno < 0) {
7233 b->b_next->b_instr[0].i_lineno = prev_lineno;
7234 }
7235 }
7236 if (is_jump(&b->b_instr[b->b_iused-1])) {
7237 switch (b->b_instr[b->b_iused-1].i_opcode) {
7238 /* Note: Only actual jumps, not exception handlers */
7239 case SETUP_ASYNC_WITH:
7240 case SETUP_WITH:
7241 case SETUP_FINALLY:
7242 continue;
7243 }
7244 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7245 if (target->b_predecessors == 1) {
7246 if (target->b_instr[0].i_lineno < 0) {
7247 target->b_instr[0].i_lineno = prev_lineno;
7248 }
7249 }
7250 }
Mark Shannon5977a792020-12-02 13:31:40 +00007251 }
7252}
7253
7254/* Perform optimizations on a control flow graph.
Mark Shannon6e8128f2020-07-30 10:03:00 +01007255 The consts object should still be in list form to allow new constants
7256 to be appended.
7257
7258 All transformations keep the code size the same or smaller.
7259 For those that reduce size, the gaps are initially filled with
7260 NOPs. Later those NOPs are removed.
7261*/
7262
7263static int
7264optimize_cfg(struct assembler *a, PyObject *consts)
7265{
Mark Shannoncc75ab72020-11-12 19:49:33 +00007266 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannoncc75ab72020-11-12 19:49:33 +00007267 if (optimize_basic_block(b, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007268 return -1;
7269 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007270 clean_basic_block(b, -1);
Mark Shannon3bd60352021-01-13 12:05:43 +00007271 assert(b->b_predecessors == 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007272 }
7273 if (mark_reachable(a)) {
7274 return -1;
7275 }
7276 /* Delete unreachable instructions */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007277 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007278 if (b->b_predecessors == 0) {
Mark Shannoncc75ab72020-11-12 19:49:33 +00007279 b->b_iused = 0;
Om Gc71581c2020-12-16 17:48:05 +05307280 b->b_nofallthrough = 0;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007281 }
7282 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007283 basicblock *pred = NULL;
7284 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7285 int prev_lineno = -1;
7286 if (pred && pred->b_iused) {
7287 prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno;
7288 }
7289 clean_basic_block(b, prev_lineno);
7290 pred = b->b_nofallthrough ? NULL : b;
7291 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007292 eliminate_empty_basic_blocks(a->a_entry);
Om Gc71581c2020-12-16 17:48:05 +05307293 /* Delete jump instructions made redundant by previous step. If a non-empty
7294 block ends with a jump instruction, check if the next non-empty block
7295 reached through normal flow control is the target of that jump. If it
7296 is, then the jump instruction is redundant and can be deleted.
7297 */
Mark Shannon3bd60352021-01-13 12:05:43 +00007298 int maybe_empty_blocks = 0;
Om Gc71581c2020-12-16 17:48:05 +05307299 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7300 if (b->b_iused > 0) {
7301 struct instr *b_last_instr = &b->b_instr[b->b_iused - 1];
Mark Shannon802b6452021-02-02 14:59:15 +00007302 if (b_last_instr->i_opcode == JUMP_ABSOLUTE ||
Om Gc71581c2020-12-16 17:48:05 +05307303 b_last_instr->i_opcode == JUMP_FORWARD) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007304 if (b_last_instr->i_target == b->b_next) {
7305 assert(b->b_next->b_iused);
Om Gc71581c2020-12-16 17:48:05 +05307306 b->b_nofallthrough = 0;
Mark Shannon802b6452021-02-02 14:59:15 +00007307 b_last_instr->i_opcode = NOP;
7308 clean_basic_block(b, -1);
7309 maybe_empty_blocks = 1;
Om Gc71581c2020-12-16 17:48:05 +05307310 }
7311 }
7312 }
7313 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007314 if (maybe_empty_blocks) {
7315 eliminate_empty_basic_blocks(a->a_entry);
7316 }
7317 propogate_line_numbers(a);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007318 return 0;
7319}
7320
Mark Shannon5977a792020-12-02 13:31:40 +00007321static inline int
7322is_exit_without_lineno(basicblock *b) {
7323 return b->b_exit && b->b_instr[0].i_lineno < 0;
7324}
7325
7326/* PEP 626 mandates that the f_lineno of a frame is correct
7327 * after a frame terminates. It would be prohibitively expensive
7328 * to continuously update the f_lineno field at runtime,
7329 * so we make sure that all exiting instruction (raises and returns)
7330 * have a valid line number, allowing us to compute f_lineno lazily.
7331 * We can do this by duplicating the exit blocks without line number
7332 * so that none have more than one predecessor. We can then safely
7333 * copy the line number from the sole predecessor block.
7334 */
7335static int
7336ensure_exits_have_lineno(struct compiler *c)
7337{
Mark Shannoneaccc122020-12-04 15:22:12 +00007338 basicblock *entry = NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00007339 /* Copy all exit blocks without line number that are targets of a jump.
7340 */
7341 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7342 if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) {
7343 switch (b->b_instr[b->b_iused-1].i_opcode) {
7344 /* Note: Only actual jumps, not exception handlers */
7345 case SETUP_ASYNC_WITH:
7346 case SETUP_WITH:
7347 case SETUP_FINALLY:
7348 continue;
7349 }
7350 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7351 if (is_exit_without_lineno(target)) {
7352 basicblock *new_target = compiler_copy_block(c, target);
7353 if (new_target == NULL) {
7354 return -1;
7355 }
7356 new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7357 b->b_instr[b->b_iused-1].i_target = new_target;
7358 }
7359 }
Mark Shannoneaccc122020-12-04 15:22:12 +00007360 entry = b;
7361 }
7362 assert(entry != NULL);
7363 if (is_exit_without_lineno(entry)) {
7364 entry->b_instr[0].i_lineno = c->u->u_firstlineno;
Mark Shannon5977a792020-12-02 13:31:40 +00007365 }
Mark Shannonee9f98d2021-01-05 12:04:10 +00007366 /* Eliminate empty blocks */
7367 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7368 while (b->b_next && b->b_next->b_iused == 0) {
7369 b->b_next = b->b_next->b_next;
7370 }
7371 }
Mark Shannon5977a792020-12-02 13:31:40 +00007372 /* Any remaining reachable exit blocks without line number can only be reached by
7373 * fall through, and thus can only have a single predecessor */
7374 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7375 if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) {
7376 if (is_exit_without_lineno(b->b_next)) {
7377 assert(b->b_next->b_iused > 0);
7378 b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7379 }
7380 }
7381 }
7382 return 0;
7383}
7384
7385
Mark Shannon6e8128f2020-07-30 10:03:00 +01007386/* Retained for API compatibility.
7387 * Optimization is now done in optimize_cfg */
7388
7389PyObject *
7390PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
7391 PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
7392{
7393 Py_INCREF(code);
7394 return code;
7395}