blob: a841288520b880b5e548431a96e7005a2e904a21 [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);
4148 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
4149 VISIT_SEQ(c, expr, e->v.Call.args);
4150 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
4151 return 1;
4152}
4153
4154static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004155validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
Zackery Spytz08050e92020-04-06 00:47:47 -06004156{
4157 Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
4158 for (Py_ssize_t i = 0; i < nkeywords; i++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004159 keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
4160 if (key->arg == NULL) {
4161 continue;
4162 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +01004163 if (forbidden_name(c, key->arg, Store)) {
4164 return -1;
4165 }
Zackery Spytz08050e92020-04-06 00:47:47 -06004166 for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004167 keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
4168 if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004169 c->u->u_col_offset = other->col_offset;
Brandt Bucher145bf262021-02-26 14:51:55 -08004170 compiler_error(c, "keyword argument repeated: %U", key->arg);
Pablo Galindo254ec782020-04-03 20:37:13 +01004171 return -1;
4172 }
4173 }
4174 }
4175 return 0;
4176}
4177
4178static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004179compiler_call(struct compiler *c, expr_ty e)
4180{
Zackery Spytz97f5de02019-03-22 01:30:32 -06004181 int ret = maybe_optimize_method_call(c, e);
4182 if (ret >= 0) {
4183 return ret;
4184 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004185 if (!check_caller(c, e->v.Call.func)) {
4186 return 0;
4187 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004188 VISIT(c, expr, e->v.Call.func);
4189 return compiler_call_helper(c, 0,
4190 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004191 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004192}
4193
Eric V. Smith235a6f02015-09-19 14:51:32 -04004194static int
4195compiler_joined_str(struct compiler *c, expr_ty e)
4196{
Eric V. Smith235a6f02015-09-19 14:51:32 -04004197 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004198 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1)
4199 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
Eric V. Smith235a6f02015-09-19 14:51:32 -04004200 return 1;
4201}
4202
Eric V. Smitha78c7952015-11-03 12:45:05 -05004203/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004204static int
4205compiler_formatted_value(struct compiler *c, expr_ty e)
4206{
Eric V. Smitha78c7952015-11-03 12:45:05 -05004207 /* Our oparg encodes 2 pieces of information: the conversion
4208 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04004209
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004210 Convert the conversion char to 3 bits:
4211 : 000 0x0 FVC_NONE The default if nothing specified.
Eric V. Smitha78c7952015-11-03 12:45:05 -05004212 !s : 001 0x1 FVC_STR
4213 !r : 010 0x2 FVC_REPR
4214 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04004215
Eric V. Smitha78c7952015-11-03 12:45:05 -05004216 next bit is whether or not we have a format spec:
4217 yes : 100 0x4
4218 no : 000 0x0
4219 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004220
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004221 int conversion = e->v.FormattedValue.conversion;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004222 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004223
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004224 /* The expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004225 VISIT(c, expr, e->v.FormattedValue.value);
4226
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004227 switch (conversion) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004228 case 's': oparg = FVC_STR; break;
4229 case 'r': oparg = FVC_REPR; break;
4230 case 'a': oparg = FVC_ASCII; break;
4231 case -1: oparg = FVC_NONE; break;
4232 default:
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004233 PyErr_Format(PyExc_SystemError,
4234 "Unrecognized conversion character %d", conversion);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004235 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004236 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004237 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004238 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004239 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004240 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004241 }
4242
Eric V. Smitha78c7952015-11-03 12:45:05 -05004243 /* And push our opcode and oparg */
4244 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004245
Eric V. Smith235a6f02015-09-19 14:51:32 -04004246 return 1;
4247}
4248
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004249static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004250compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004251{
4252 Py_ssize_t i, n = end - begin;
4253 keyword_ty kw;
4254 PyObject *keys, *key;
4255 assert(n > 0);
4256 if (n > 1) {
4257 for (i = begin; i < end; i++) {
4258 kw = asdl_seq_GET(keywords, i);
4259 VISIT(c, expr, kw->value);
4260 }
4261 keys = PyTuple_New(n);
4262 if (keys == NULL) {
4263 return 0;
4264 }
4265 for (i = begin; i < end; i++) {
4266 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
4267 Py_INCREF(key);
4268 PyTuple_SET_ITEM(keys, i - begin, key);
4269 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004270 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004271 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
4272 }
4273 else {
4274 /* a for loop only executes once */
4275 for (i = begin; i < end; i++) {
4276 kw = asdl_seq_GET(keywords, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004277 ADDOP_LOAD_CONST(c, kw->arg);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004278 VISIT(c, expr, kw->value);
4279 }
4280 ADDOP_I(c, BUILD_MAP, n);
4281 }
4282 return 1;
4283}
4284
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004285/* shared code between compiler_call and compiler_class */
4286static int
4287compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01004288 int n, /* Args already pushed */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004289 asdl_expr_seq *args,
4290 asdl_keyword_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004291{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004292 Py_ssize_t i, nseen, nelts, nkwelts;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004293
Pablo Galindo254ec782020-04-03 20:37:13 +01004294 if (validate_keywords(c, keywords) == -1) {
4295 return 0;
4296 }
4297
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004298 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004299 nkwelts = asdl_seq_LEN(keywords);
4300
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004301 for (i = 0; i < nelts; i++) {
4302 expr_ty elt = asdl_seq_GET(args, i);
4303 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00004304 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004305 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004306 }
4307 for (i = 0; i < nkwelts; i++) {
4308 keyword_ty kw = asdl_seq_GET(keywords, i);
4309 if (kw->arg == NULL) {
4310 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004311 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004312 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004313
Mark Shannon13bc1392020-01-23 09:25:17 +00004314 /* No * or ** args, so can use faster calling sequence */
4315 for (i = 0; i < nelts; i++) {
4316 expr_ty elt = asdl_seq_GET(args, i);
4317 assert(elt->kind != Starred_kind);
4318 VISIT(c, expr, elt);
4319 }
4320 if (nkwelts) {
4321 PyObject *names;
4322 VISIT_SEQ(c, keyword, keywords);
4323 names = PyTuple_New(nkwelts);
4324 if (names == NULL) {
4325 return 0;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004326 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004327 for (i = 0; i < nkwelts; i++) {
4328 keyword_ty kw = asdl_seq_GET(keywords, i);
4329 Py_INCREF(kw->arg);
4330 PyTuple_SET_ITEM(names, i, kw->arg);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004331 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004332 ADDOP_LOAD_CONST_NEW(c, names);
4333 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
4334 return 1;
4335 }
4336 else {
4337 ADDOP_I(c, CALL_FUNCTION, n + nelts);
4338 return 1;
4339 }
4340
4341ex_call:
4342
4343 /* Do positional arguments. */
4344 if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
4345 VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
4346 }
4347 else if (starunpack_helper(c, args, n, BUILD_LIST,
4348 LIST_APPEND, LIST_EXTEND, 1) == 0) {
4349 return 0;
4350 }
4351 /* Then keyword arguments */
4352 if (nkwelts) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004353 /* Has a new dict been pushed */
4354 int have_dict = 0;
Mark Shannon13bc1392020-01-23 09:25:17 +00004355
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004356 nseen = 0; /* the number of keyword arguments on the stack following */
4357 for (i = 0; i < nkwelts; i++) {
4358 keyword_ty kw = asdl_seq_GET(keywords, i);
4359 if (kw->arg == NULL) {
4360 /* A keyword argument unpacking. */
4361 if (nseen) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004362 if (!compiler_subkwargs(c, keywords, i - nseen, i)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004363 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004364 }
Mark Shannondb64f122020-06-01 10:42:42 +01004365 if (have_dict) {
4366 ADDOP_I(c, DICT_MERGE, 1);
4367 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004368 have_dict = 1;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004369 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004370 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004371 if (!have_dict) {
4372 ADDOP_I(c, BUILD_MAP, 0);
4373 have_dict = 1;
4374 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004375 VISIT(c, expr, kw->value);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004376 ADDOP_I(c, DICT_MERGE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004377 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004378 else {
4379 nseen++;
4380 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004381 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004382 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004383 /* Pack up any trailing keyword arguments. */
Mark Shannon8a4cd702020-01-27 09:57:45 +00004384 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004385 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004386 }
4387 if (have_dict) {
4388 ADDOP_I(c, DICT_MERGE, 1);
4389 }
4390 have_dict = 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004391 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004392 assert(have_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004393 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004394 ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0);
4395 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004396}
4397
Nick Coghlan650f0d02007-04-15 12:05:43 +00004398
4399/* List and set comprehensions and generator expressions work by creating a
4400 nested function to perform the actual iteration. This means that the
4401 iteration variables don't leak into the current scope.
4402 The defined function is called immediately following its definition, with the
4403 result of that call being the result of the expression.
4404 The LC/SC version returns the populated container, while the GE version is
4405 flagged in symtable.c as a generator, so it returns the generator object
4406 when the function is called.
Nick Coghlan650f0d02007-04-15 12:05:43 +00004407
4408 Possible cleanups:
4409 - iterate over the generator sequence instead of using recursion
4410*/
4411
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004412
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004413static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414compiler_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004415 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004416 int depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004417 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004418{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004419 comprehension_ty gen;
4420 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4421 if (gen->is_async) {
4422 return compiler_async_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004423 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004424 } else {
4425 return compiler_sync_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 }
4428}
4429
4430static int
4431compiler_sync_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004432 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004433 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004434 expr_ty elt, expr_ty val, int type)
4435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004436 /* generate code for the iterator, then each of the ifs,
4437 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004439 comprehension_ty gen;
4440 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004441 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004443 start = compiler_new_block(c);
4444 skip = compiler_new_block(c);
4445 if_cleanup = compiler_new_block(c);
4446 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004448 if (start == NULL || skip == NULL || if_cleanup == NULL ||
4449 anchor == NULL)
4450 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004454 if (gen_index == 0) {
4455 /* Receive outermost iter as an implicit argument */
4456 c->u->u_argcount = 1;
4457 ADDOP_I(c, LOAD_FAST, 0);
4458 }
4459 else {
4460 /* Sub-iter - calculate on the fly */
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004461 /* Fast path for the temporary variable assignment idiom:
4462 for y in [f(x)]
4463 */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004464 asdl_expr_seq *elts;
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004465 switch (gen->iter->kind) {
4466 case List_kind:
4467 elts = gen->iter->v.List.elts;
4468 break;
4469 case Tuple_kind:
4470 elts = gen->iter->v.Tuple.elts;
4471 break;
4472 default:
4473 elts = NULL;
4474 }
4475 if (asdl_seq_LEN(elts) == 1) {
4476 expr_ty elt = asdl_seq_GET(elts, 0);
4477 if (elt->kind != Starred_kind) {
4478 VISIT(c, expr, elt);
4479 start = NULL;
4480 }
4481 }
4482 if (start) {
4483 VISIT(c, expr, gen->iter);
4484 ADDOP(c, GET_ITER);
4485 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004486 }
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004487 if (start) {
4488 depth++;
4489 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01004490 ADDOP_JUMP(c, FOR_ITER, anchor);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004491 NEXT_BLOCK(c);
4492 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004493 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004495 /* XXX this needs to be cleaned up...a lot! */
4496 n = asdl_seq_LEN(gen->ifs);
4497 for (i = 0; i < n; i++) {
4498 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004499 if (!compiler_jump_if(c, e, if_cleanup, 0))
4500 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 NEXT_BLOCK(c);
4502 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004504 if (++gen_index < asdl_seq_LEN(generators))
4505 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004506 generators, gen_index, depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004507 elt, val, type))
4508 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004510 /* only append after the last for generator */
4511 if (gen_index >= asdl_seq_LEN(generators)) {
4512 /* comprehension specific code */
4513 switch (type) {
4514 case COMP_GENEXP:
4515 VISIT(c, expr, elt);
4516 ADDOP(c, YIELD_VALUE);
4517 ADDOP(c, POP_TOP);
4518 break;
4519 case COMP_LISTCOMP:
4520 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004521 ADDOP_I(c, LIST_APPEND, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004522 break;
4523 case COMP_SETCOMP:
4524 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004525 ADDOP_I(c, SET_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004526 break;
4527 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004528 /* With '{k: v}', k is evaluated before v, so we do
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004529 the same. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004530 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004531 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004532 ADDOP_I(c, MAP_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004533 break;
4534 default:
4535 return 0;
4536 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 compiler_use_next_block(c, skip);
4539 }
4540 compiler_use_next_block(c, if_cleanup);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004541 if (start) {
Mark Shannon582aaf12020-08-04 17:30:11 +01004542 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004543 compiler_use_next_block(c, anchor);
4544 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004545
4546 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004547}
4548
4549static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004550compiler_async_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004551 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004552 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004553 expr_ty elt, expr_ty val, int type)
4554{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004555 comprehension_ty gen;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004556 basicblock *start, *if_cleanup, *except;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004557 Py_ssize_t i, n;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004558 start = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004559 except = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004560 if_cleanup = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004561
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004562 if (start == NULL || if_cleanup == NULL || except == NULL) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004563 return 0;
4564 }
4565
4566 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4567
4568 if (gen_index == 0) {
4569 /* Receive outermost iter as an implicit argument */
4570 c->u->u_argcount = 1;
4571 ADDOP_I(c, LOAD_FAST, 0);
4572 }
4573 else {
4574 /* Sub-iter - calculate on the fly */
4575 VISIT(c, expr, gen->iter);
4576 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004577 }
4578
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004579 compiler_use_next_block(c, start);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004580
Mark Shannon582aaf12020-08-04 17:30:11 +01004581 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004582 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004583 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004584 ADDOP(c, YIELD_FROM);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004585 ADDOP(c, POP_BLOCK);
Serhiy Storchaka24d32012018-03-10 18:22:34 +02004586 VISIT(c, expr, gen->target);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004587
4588 n = asdl_seq_LEN(gen->ifs);
4589 for (i = 0; i < n; i++) {
4590 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004591 if (!compiler_jump_if(c, e, if_cleanup, 0))
4592 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004593 NEXT_BLOCK(c);
4594 }
4595
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004596 depth++;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004597 if (++gen_index < asdl_seq_LEN(generators))
4598 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004599 generators, gen_index, depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004600 elt, val, type))
4601 return 0;
4602
4603 /* only append after the last for generator */
4604 if (gen_index >= asdl_seq_LEN(generators)) {
4605 /* comprehension specific code */
4606 switch (type) {
4607 case COMP_GENEXP:
4608 VISIT(c, expr, elt);
4609 ADDOP(c, YIELD_VALUE);
4610 ADDOP(c, POP_TOP);
4611 break;
4612 case COMP_LISTCOMP:
4613 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004614 ADDOP_I(c, LIST_APPEND, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004615 break;
4616 case COMP_SETCOMP:
4617 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004618 ADDOP_I(c, SET_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004619 break;
4620 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004621 /* With '{k: v}', k is evaluated before v, so we do
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004622 the same. */
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004623 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004624 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004625 ADDOP_I(c, MAP_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004626 break;
4627 default:
4628 return 0;
4629 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004630 }
4631 compiler_use_next_block(c, if_cleanup);
Mark Shannon582aaf12020-08-04 17:30:11 +01004632 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004633
4634 compiler_use_next_block(c, except);
4635 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004636
4637 return 1;
4638}
4639
4640static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004641compiler_comprehension(struct compiler *c, expr_ty e, int type,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004642 identifier name, asdl_comprehension_seq *generators, expr_ty elt,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004643 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004644{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004645 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004646 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004647 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004648 int is_async_generator = 0;
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004649 int top_level_await = IS_TOP_LEVEL_AWAIT(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004650
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004651
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004652 int is_async_function = c->u->u_ste->ste_coroutine;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004653
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004654 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004655 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4656 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004657 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004658 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004659 }
4660
4661 is_async_generator = c->u->u_ste->ste_coroutine;
4662
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004663 if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004664 compiler_error(c, "asynchronous comprehension outside of "
4665 "an asynchronous function");
4666 goto error_in_scope;
4667 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004669 if (type != COMP_GENEXP) {
4670 int op;
4671 switch (type) {
4672 case COMP_LISTCOMP:
4673 op = BUILD_LIST;
4674 break;
4675 case COMP_SETCOMP:
4676 op = BUILD_SET;
4677 break;
4678 case COMP_DICTCOMP:
4679 op = BUILD_MAP;
4680 break;
4681 default:
4682 PyErr_Format(PyExc_SystemError,
4683 "unknown comprehension type %d", type);
4684 goto error_in_scope;
4685 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004687 ADDOP_I(c, op, 0);
4688 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004689
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004690 if (!compiler_comprehension_generator(c, generators, 0, 0, elt,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004691 val, type))
4692 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004694 if (type != COMP_GENEXP) {
4695 ADDOP(c, RETURN_VALUE);
4696 }
4697
4698 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004699 qualname = c->u->u_qualname;
4700 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004701 compiler_exit_scope(c);
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004702 if (top_level_await && is_async_generator){
4703 c->u->u_ste->ste_coroutine = 1;
4704 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004705 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004706 goto error;
4707
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004708 if (!compiler_make_closure(c, co, 0, qualname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004709 goto error;
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004710 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004711 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004712 Py_DECREF(co);
4713
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004714 VISIT(c, expr, outermost->iter);
4715
4716 if (outermost->is_async) {
4717 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004718 } else {
4719 ADDOP(c, GET_ITER);
4720 }
4721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004722 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004723
4724 if (is_async_generator && type != COMP_GENEXP) {
4725 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004726 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004727 ADDOP(c, YIELD_FROM);
4728 }
4729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004730 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004731error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004732 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004733error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004734 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004735 Py_XDECREF(co);
4736 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004737}
4738
4739static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004740compiler_genexp(struct compiler *c, expr_ty e)
4741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004742 static identifier name;
4743 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004744 name = PyUnicode_InternFromString("<genexpr>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004745 if (!name)
4746 return 0;
4747 }
4748 assert(e->kind == GeneratorExp_kind);
4749 return compiler_comprehension(c, e, COMP_GENEXP, name,
4750 e->v.GeneratorExp.generators,
4751 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004752}
4753
4754static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004755compiler_listcomp(struct compiler *c, expr_ty e)
4756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004757 static identifier name;
4758 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004759 name = PyUnicode_InternFromString("<listcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004760 if (!name)
4761 return 0;
4762 }
4763 assert(e->kind == ListComp_kind);
4764 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4765 e->v.ListComp.generators,
4766 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004767}
4768
4769static int
4770compiler_setcomp(struct compiler *c, expr_ty e)
4771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004772 static identifier name;
4773 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004774 name = PyUnicode_InternFromString("<setcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004775 if (!name)
4776 return 0;
4777 }
4778 assert(e->kind == SetComp_kind);
4779 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4780 e->v.SetComp.generators,
4781 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004782}
4783
4784
4785static int
4786compiler_dictcomp(struct compiler *c, expr_ty e)
4787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004788 static identifier name;
4789 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004790 name = PyUnicode_InternFromString("<dictcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004791 if (!name)
4792 return 0;
4793 }
4794 assert(e->kind == DictComp_kind);
4795 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4796 e->v.DictComp.generators,
4797 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004798}
4799
4800
4801static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004802compiler_visit_keyword(struct compiler *c, keyword_ty k)
4803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004804 VISIT(c, expr, k->value);
4805 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004806}
4807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004808/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004809 whether they are true or false.
4810
4811 Return values: 1 for true, 0 for false, -1 for non-constant.
4812 */
4813
4814static int
Mark Shannonfee55262019-11-21 09:11:43 +00004815compiler_with_except_finish(struct compiler *c) {
4816 basicblock *exit;
4817 exit = compiler_new_block(c);
4818 if (exit == NULL)
4819 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01004820 ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit);
Mark Shannon266b4622020-11-17 19:30:14 +00004821 NEXT_BLOCK(c);
Mark Shannonbf353f32020-12-17 13:55:28 +00004822 ADDOP_I(c, RERAISE, 1);
Mark Shannonfee55262019-11-21 09:11:43 +00004823 compiler_use_next_block(c, exit);
4824 ADDOP(c, POP_TOP);
4825 ADDOP(c, POP_TOP);
4826 ADDOP(c, POP_TOP);
4827 ADDOP(c, POP_EXCEPT);
4828 ADDOP(c, POP_TOP);
4829 return 1;
4830}
Yury Selivanov75445082015-05-11 22:57:16 -04004831
4832/*
4833 Implements the async with statement.
4834
4835 The semantics outlined in that PEP are as follows:
4836
4837 async with EXPR as VAR:
4838 BLOCK
4839
4840 It is implemented roughly as:
4841
4842 context = EXPR
4843 exit = context.__aexit__ # not calling it
4844 value = await context.__aenter__()
4845 try:
4846 VAR = value # if VAR present in the syntax
4847 BLOCK
4848 finally:
4849 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004850 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04004851 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004852 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04004853 if not (await exit(*exc)):
4854 raise
4855 */
4856static int
4857compiler_async_with(struct compiler *c, stmt_ty s, int pos)
4858{
Mark Shannonfee55262019-11-21 09:11:43 +00004859 basicblock *block, *final, *exit;
Yury Selivanov75445082015-05-11 22:57:16 -04004860 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
4861
4862 assert(s->kind == AsyncWith_kind);
Pablo Galindo90235812020-03-15 04:29:22 +00004863 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07004864 c->u->u_ste->ste_coroutine = 1;
4865 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
Zsolt Dollensteine2396502018-04-27 08:58:56 -07004866 return compiler_error(c, "'async with' outside async function");
4867 }
Yury Selivanov75445082015-05-11 22:57:16 -04004868
4869 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004870 final = compiler_new_block(c);
4871 exit = compiler_new_block(c);
4872 if (!block || !final || !exit)
Yury Selivanov75445082015-05-11 22:57:16 -04004873 return 0;
4874
4875 /* Evaluate EXPR */
4876 VISIT(c, expr, item->context_expr);
4877
4878 ADDOP(c, BEFORE_ASYNC_WITH);
4879 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004880 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004881 ADDOP(c, YIELD_FROM);
4882
Mark Shannon582aaf12020-08-04 17:30:11 +01004883 ADDOP_JUMP(c, SETUP_ASYNC_WITH, final);
Yury Selivanov75445082015-05-11 22:57:16 -04004884
4885 /* SETUP_ASYNC_WITH pushes a finally block. */
4886 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004887 if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004888 return 0;
4889 }
4890
4891 if (item->optional_vars) {
4892 VISIT(c, expr, item->optional_vars);
4893 }
4894 else {
4895 /* Discard result from context.__aenter__() */
4896 ADDOP(c, POP_TOP);
4897 }
4898
4899 pos++;
4900 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
4901 /* BLOCK code */
4902 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
4903 else if (!compiler_async_with(c, s, pos))
4904 return 0;
4905
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004906 compiler_pop_fblock(c, ASYNC_WITH, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004907 ADDOP(c, POP_BLOCK);
4908 /* End of body; start the cleanup */
Yury Selivanov75445082015-05-11 22:57:16 -04004909
Mark Shannonfee55262019-11-21 09:11:43 +00004910 /* For successful outcome:
4911 * call __exit__(None, None, None)
4912 */
4913 if(!compiler_call_exit_with_nones(c))
Yury Selivanov75445082015-05-11 22:57:16 -04004914 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00004915 ADDOP(c, GET_AWAITABLE);
4916 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4917 ADDOP(c, YIELD_FROM);
Yury Selivanov75445082015-05-11 22:57:16 -04004918
Mark Shannonfee55262019-11-21 09:11:43 +00004919 ADDOP(c, POP_TOP);
Yury Selivanov75445082015-05-11 22:57:16 -04004920
Mark Shannon582aaf12020-08-04 17:30:11 +01004921 ADDOP_JUMP(c, JUMP_ABSOLUTE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00004922
4923 /* For exceptional outcome: */
4924 compiler_use_next_block(c, final);
4925
4926 ADDOP(c, WITH_EXCEPT_START);
Yury Selivanov75445082015-05-11 22:57:16 -04004927 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004928 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004929 ADDOP(c, YIELD_FROM);
Mark Shannonfee55262019-11-21 09:11:43 +00004930 compiler_with_except_finish(c);
Yury Selivanov75445082015-05-11 22:57:16 -04004931
Mark Shannonfee55262019-11-21 09:11:43 +00004932compiler_use_next_block(c, exit);
Yury Selivanov75445082015-05-11 22:57:16 -04004933 return 1;
4934}
4935
4936
Guido van Rossumc2e20742006-02-27 22:32:47 +00004937/*
4938 Implements the with statement from PEP 343.
Guido van Rossumc2e20742006-02-27 22:32:47 +00004939 with EXPR as VAR:
4940 BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00004941 is implemented as:
4942 <code for EXPR>
4943 SETUP_WITH E
4944 <code to store to VAR> or POP_TOP
4945 <code for BLOCK>
4946 LOAD_CONST (None, None, None)
4947 CALL_FUNCTION_EX 0
4948 JUMP_FORWARD EXIT
4949 E: WITH_EXCEPT_START (calls EXPR.__exit__)
4950 POP_JUMP_IF_TRUE T:
4951 RERAISE
4952 T: POP_TOP * 3 (remove exception from stack)
4953 POP_EXCEPT
4954 POP_TOP
4955 EXIT:
Guido van Rossumc2e20742006-02-27 22:32:47 +00004956 */
Mark Shannonfee55262019-11-21 09:11:43 +00004957
Guido van Rossumc2e20742006-02-27 22:32:47 +00004958static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004959compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004960{
Mark Shannonfee55262019-11-21 09:11:43 +00004961 basicblock *block, *final, *exit;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004962 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004963
4964 assert(s->kind == With_kind);
4965
Guido van Rossumc2e20742006-02-27 22:32:47 +00004966 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004967 final = compiler_new_block(c);
4968 exit = compiler_new_block(c);
4969 if (!block || !final || !exit)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004970 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004971
Thomas Wouters477c8d52006-05-27 19:21:47 +00004972 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004973 VISIT(c, expr, item->context_expr);
Mark Shannonfee55262019-11-21 09:11:43 +00004974 /* Will push bound __exit__ */
Mark Shannon582aaf12020-08-04 17:30:11 +01004975 ADDOP_JUMP(c, SETUP_WITH, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004976
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004977 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00004978 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004979 if (!compiler_push_fblock(c, WITH, block, final, NULL)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004980 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004981 }
4982
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004983 if (item->optional_vars) {
4984 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004985 }
4986 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004987 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004988 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004989 }
4990
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004991 pos++;
4992 if (pos == asdl_seq_LEN(s->v.With.items))
4993 /* BLOCK code */
4994 VISIT_SEQ(c, stmt, s->v.With.body)
4995 else if (!compiler_with(c, s, pos))
4996 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004997
Mark Shannon3bd60352021-01-13 12:05:43 +00004998
4999 /* Mark all following code as artificial */
5000 c->u->u_lineno = -1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005001 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005002 compiler_pop_fblock(c, WITH, block);
Mark Shannon13bc1392020-01-23 09:25:17 +00005003
Mark Shannonfee55262019-11-21 09:11:43 +00005004 /* End of body; start the cleanup. */
Mark Shannon13bc1392020-01-23 09:25:17 +00005005
Mark Shannonfee55262019-11-21 09:11:43 +00005006 /* For successful outcome:
5007 * call __exit__(None, None, None)
5008 */
5009 if (!compiler_call_exit_with_nones(c))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005010 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00005011 ADDOP(c, POP_TOP);
Mark Shannon582aaf12020-08-04 17:30:11 +01005012 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005013
Mark Shannonfee55262019-11-21 09:11:43 +00005014 /* For exceptional outcome: */
5015 compiler_use_next_block(c, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005016
Mark Shannonfee55262019-11-21 09:11:43 +00005017 ADDOP(c, WITH_EXCEPT_START);
5018 compiler_with_except_finish(c);
5019
5020 compiler_use_next_block(c, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005021 return 1;
5022}
5023
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005024static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005025compiler_visit_expr1(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005026{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005027 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07005028 case NamedExpr_kind:
5029 VISIT(c, expr, e->v.NamedExpr.value);
5030 ADDOP(c, DUP_TOP);
5031 VISIT(c, expr, e->v.NamedExpr.target);
5032 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005033 case BoolOp_kind:
5034 return compiler_boolop(c, e);
5035 case BinOp_kind:
5036 VISIT(c, expr, e->v.BinOp.left);
5037 VISIT(c, expr, e->v.BinOp.right);
Andy Lester76d58772020-03-10 21:18:12 -05005038 ADDOP(c, binop(e->v.BinOp.op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005039 break;
5040 case UnaryOp_kind:
5041 VISIT(c, expr, e->v.UnaryOp.operand);
5042 ADDOP(c, unaryop(e->v.UnaryOp.op));
5043 break;
5044 case Lambda_kind:
5045 return compiler_lambda(c, e);
5046 case IfExp_kind:
5047 return compiler_ifexp(c, e);
5048 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005049 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005050 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005051 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005052 case GeneratorExp_kind:
5053 return compiler_genexp(c, e);
5054 case ListComp_kind:
5055 return compiler_listcomp(c, e);
5056 case SetComp_kind:
5057 return compiler_setcomp(c, e);
5058 case DictComp_kind:
5059 return compiler_dictcomp(c, e);
5060 case Yield_kind:
5061 if (c->u->u_ste->ste_type != FunctionBlock)
5062 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005063 if (e->v.Yield.value) {
5064 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005065 }
5066 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005067 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005068 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005069 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005070 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005071 case YieldFrom_kind:
5072 if (c->u->u_ste->ste_type != FunctionBlock)
5073 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04005074
5075 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
5076 return compiler_error(c, "'yield from' inside async function");
5077
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005078 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04005079 ADDOP(c, GET_YIELD_FROM_ITER);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005080 ADDOP_LOAD_CONST(c, Py_None);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005081 ADDOP(c, YIELD_FROM);
5082 break;
Yury Selivanov75445082015-05-11 22:57:16 -04005083 case Await_kind:
Pablo Galindo90235812020-03-15 04:29:22 +00005084 if (!IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005085 if (c->u->u_ste->ste_type != FunctionBlock){
5086 return compiler_error(c, "'await' outside function");
5087 }
Yury Selivanov75445082015-05-11 22:57:16 -04005088
Victor Stinner331a6a52019-05-27 16:39:22 +02005089 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005090 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
5091 return compiler_error(c, "'await' outside async function");
5092 }
5093 }
Yury Selivanov75445082015-05-11 22:57:16 -04005094
5095 VISIT(c, expr, e->v.Await.value);
5096 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005097 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005098 ADDOP(c, YIELD_FROM);
5099 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005100 case Compare_kind:
5101 return compiler_compare(c, e);
5102 case Call_kind:
5103 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005104 case Constant_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005105 ADDOP_LOAD_CONST(c, e->v.Constant.value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005106 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005107 case JoinedStr_kind:
5108 return compiler_joined_str(c, e);
5109 case FormattedValue_kind:
5110 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005111 /* The following exprs can be assignment targets. */
5112 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005113 VISIT(c, expr, e->v.Attribute.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005114 switch (e->v.Attribute.ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005115 case Load:
5116 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
5117 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005118 case Store:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005119 if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx))
5120 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005121 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5122 break;
5123 case Del:
5124 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
5125 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005126 }
5127 break;
5128 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005129 return compiler_subscript(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005130 case Starred_kind:
5131 switch (e->v.Starred.ctx) {
5132 case Store:
5133 /* In all legitimate cases, the Starred node was already replaced
5134 * by compiler_list/compiler_tuple. XXX: is that okay? */
5135 return compiler_error(c,
5136 "starred assignment target must be in a list or tuple");
5137 default:
5138 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005139 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005140 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005141 break;
5142 case Slice_kind:
5143 return compiler_slice(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005144 case Name_kind:
5145 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
5146 /* child nodes of List and Tuple will have expr_context set */
5147 case List_kind:
5148 return compiler_list(c, e);
5149 case Tuple_kind:
5150 return compiler_tuple(c, e);
Brandt Bucher145bf262021-02-26 14:51:55 -08005151 case MatchAs_kind:
5152 case MatchOr_kind:
5153 // Can only occur in patterns, which are handled elsewhere.
5154 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005155 }
5156 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005157}
5158
5159static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005160compiler_visit_expr(struct compiler *c, expr_ty e)
5161{
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005162 int old_lineno = c->u->u_lineno;
5163 int old_col_offset = c->u->u_col_offset;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005164 SET_LOC(c, e);
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005165 int res = compiler_visit_expr1(c, e);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005166 c->u->u_lineno = old_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005167 c->u->u_col_offset = old_col_offset;
5168 return res;
5169}
5170
5171static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005172compiler_augassign(struct compiler *c, stmt_ty s)
5173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005174 assert(s->kind == AugAssign_kind);
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005175 expr_ty e = s->v.AugAssign.target;
5176
5177 int old_lineno = c->u->u_lineno;
5178 int old_col_offset = c->u->u_col_offset;
5179 SET_LOC(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005181 switch (e->kind) {
5182 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005183 VISIT(c, expr, e->v.Attribute.value);
5184 ADDOP(c, DUP_TOP);
5185 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005186 break;
5187 case Subscript_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005188 VISIT(c, expr, e->v.Subscript.value);
5189 VISIT(c, expr, e->v.Subscript.slice);
5190 ADDOP(c, DUP_TOP_TWO);
5191 ADDOP(c, BINARY_SUBSCR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005192 break;
5193 case Name_kind:
5194 if (!compiler_nameop(c, e->v.Name.id, Load))
5195 return 0;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005196 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005197 default:
5198 PyErr_Format(PyExc_SystemError,
5199 "invalid node type (%d) for augmented assignment",
5200 e->kind);
5201 return 0;
5202 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005203
5204 c->u->u_lineno = old_lineno;
5205 c->u->u_col_offset = old_col_offset;
5206
5207 VISIT(c, expr, s->v.AugAssign.value);
5208 ADDOP(c, inplace_binop(s->v.AugAssign.op));
5209
5210 SET_LOC(c, e);
5211
5212 switch (e->kind) {
5213 case Attribute_kind:
5214 ADDOP(c, ROT_TWO);
5215 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5216 break;
5217 case Subscript_kind:
5218 ADDOP(c, ROT_THREE);
5219 ADDOP(c, STORE_SUBSCR);
5220 break;
5221 case Name_kind:
5222 return compiler_nameop(c, e->v.Name.id, Store);
5223 default:
5224 Py_UNREACHABLE();
5225 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005226 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005227}
5228
5229static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005230check_ann_expr(struct compiler *c, expr_ty e)
5231{
5232 VISIT(c, expr, e);
5233 ADDOP(c, POP_TOP);
5234 return 1;
5235}
5236
5237static int
5238check_annotation(struct compiler *c, stmt_ty s)
5239{
5240 /* Annotations are only evaluated in a module or class. */
5241 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5242 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
5243 return check_ann_expr(c, s->v.AnnAssign.annotation);
5244 }
5245 return 1;
5246}
5247
5248static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005249check_ann_subscr(struct compiler *c, expr_ty e)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005250{
5251 /* We check that everything in a subscript is defined at runtime. */
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005252 switch (e->kind) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005253 case Slice_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005254 if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005255 return 0;
5256 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005257 if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) {
5258 return 0;
5259 }
5260 if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) {
5261 return 0;
5262 }
5263 return 1;
5264 case Tuple_kind: {
5265 /* extended slice */
Pablo Galindoa5634c42020-09-16 19:42:00 +01005266 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005267 Py_ssize_t i, n = asdl_seq_LEN(elts);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005268 for (i = 0; i < n; i++) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005269 if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005270 return 0;
5271 }
5272 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005273 return 1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005274 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005275 default:
5276 return check_ann_expr(c, e);
5277 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005278}
5279
5280static int
5281compiler_annassign(struct compiler *c, stmt_ty s)
5282{
5283 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07005284 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005285
5286 assert(s->kind == AnnAssign_kind);
5287
5288 /* We perform the actual assignment first. */
5289 if (s->v.AnnAssign.value) {
5290 VISIT(c, expr, s->v.AnnAssign.value);
5291 VISIT(c, expr, targ);
5292 }
5293 switch (targ->kind) {
5294 case Name_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005295 if (forbidden_name(c, targ->v.Name.id, Store))
5296 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005297 /* If we have a simple name in a module or class, store annotation. */
5298 if (s->v.AnnAssign.simple &&
5299 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5300 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Batuhan Taskaya044a1042020-10-06 23:03:02 +03005301 VISIT(c, annexpr, s->v.AnnAssign.annotation);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005302 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02005303 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005304 ADDOP_LOAD_CONST_NEW(c, mangled);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005305 ADDOP(c, STORE_SUBSCR);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005306 }
5307 break;
5308 case Attribute_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005309 if (forbidden_name(c, targ->v.Attribute.attr, Store))
5310 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005311 if (!s->v.AnnAssign.value &&
5312 !check_ann_expr(c, targ->v.Attribute.value)) {
5313 return 0;
5314 }
5315 break;
5316 case Subscript_kind:
5317 if (!s->v.AnnAssign.value &&
5318 (!check_ann_expr(c, targ->v.Subscript.value) ||
5319 !check_ann_subscr(c, targ->v.Subscript.slice))) {
5320 return 0;
5321 }
5322 break;
5323 default:
5324 PyErr_Format(PyExc_SystemError,
5325 "invalid node type (%d) for annotated assignment",
5326 targ->kind);
5327 return 0;
5328 }
5329 /* Annotation is evaluated last. */
5330 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
5331 return 0;
5332 }
5333 return 1;
5334}
5335
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005336/* Raises a SyntaxError and returns 0.
5337 If something goes wrong, a different exception may be raised.
5338*/
5339
5340static int
Brandt Bucher145bf262021-02-26 14:51:55 -08005341compiler_error(struct compiler *c, const char *format, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005342{
Brandt Bucher145bf262021-02-26 14:51:55 -08005343 va_list vargs;
5344#ifdef HAVE_STDARG_PROTOTYPES
5345 va_start(vargs, format);
5346#else
5347 va_start(vargs);
5348#endif
5349 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5350 va_end(vargs);
5351 if (msg == NULL) {
5352 return 0;
5353 }
5354 PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
5355 if (loc == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005356 Py_INCREF(Py_None);
5357 loc = Py_None;
5358 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005359 PyObject *args = Py_BuildValue("O(OiiO)", msg, c->c_filename,
5360 c->u->u_lineno, c->u->u_col_offset + 1, loc);
5361 Py_DECREF(msg);
5362 if (args == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005363 goto exit;
Brandt Bucher145bf262021-02-26 14:51:55 -08005364 }
5365 PyErr_SetObject(PyExc_SyntaxError, args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005366 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005367 Py_DECREF(loc);
Brandt Bucher145bf262021-02-26 14:51:55 -08005368 Py_XDECREF(args);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005369 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005370}
5371
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005372/* Emits a SyntaxWarning and returns 1 on success.
5373 If a SyntaxWarning raised as error, replaces it with a SyntaxError
5374 and returns 0.
5375*/
5376static int
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005377compiler_warn(struct compiler *c, const char *format, ...)
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005378{
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005379 va_list vargs;
5380#ifdef HAVE_STDARG_PROTOTYPES
5381 va_start(vargs, format);
5382#else
5383 va_start(vargs);
5384#endif
5385 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5386 va_end(vargs);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005387 if (msg == NULL) {
5388 return 0;
5389 }
5390 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
5391 c->u->u_lineno, NULL, NULL) < 0)
5392 {
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005393 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005394 /* Replace the SyntaxWarning exception with a SyntaxError
5395 to get a more accurate error report */
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005396 PyErr_Clear();
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005397 assert(PyUnicode_AsUTF8(msg) != NULL);
5398 compiler_error(c, PyUnicode_AsUTF8(msg));
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005399 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005400 Py_DECREF(msg);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005401 return 0;
5402 }
5403 Py_DECREF(msg);
5404 return 1;
5405}
5406
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005407static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005408compiler_subscript(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005409{
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005410 expr_context_ty ctx = e->v.Subscript.ctx;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005411 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005412
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005413 if (ctx == Load) {
5414 if (!check_subscripter(c, e->v.Subscript.value)) {
5415 return 0;
5416 }
5417 if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
5418 return 0;
5419 }
5420 }
5421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005422 switch (ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005423 case Load: op = BINARY_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005424 case Store: op = STORE_SUBSCR; break;
5425 case Del: op = DELETE_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005426 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005427 assert(op);
5428 VISIT(c, expr, e->v.Subscript.value);
5429 VISIT(c, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005430 ADDOP(c, op);
5431 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005432}
5433
5434static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005435compiler_slice(struct compiler *c, expr_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005436{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005437 int n = 2;
5438 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005440 /* only handles the cases where BUILD_SLICE is emitted */
5441 if (s->v.Slice.lower) {
5442 VISIT(c, expr, s->v.Slice.lower);
5443 }
5444 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005445 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005446 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005448 if (s->v.Slice.upper) {
5449 VISIT(c, expr, s->v.Slice.upper);
5450 }
5451 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005452 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005453 }
5454
5455 if (s->v.Slice.step) {
5456 n++;
5457 VISIT(c, expr, s->v.Slice.step);
5458 }
5459 ADDOP_I(c, BUILD_SLICE, n);
5460 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005461}
5462
Brandt Bucher145bf262021-02-26 14:51:55 -08005463
5464// PEP 634: Structural Pattern Matching
5465
5466// To keep things simple, all compiler_pattern_* routines follow the convention
5467// of replacing TOS (the subject for the given pattern) with either True (match)
5468// or False (no match). We do this even for irrefutable patterns; the idea is
5469// that it's much easier to smooth out any redundant pushing, popping, and
5470// jumping in the peephole optimizer than to detect or predict it here.
5471
5472
5473#define WILDCARD_CHECK(N) \
5474 ((N)->kind == Name_kind && \
5475 _PyUnicode_EqualToASCIIString((N)->v.Name.id, "_"))
5476
5477
5478static int
5479pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc)
5480{
5481 assert(!_PyUnicode_EqualToASCIIString(n, "_"));
5482 // Can't assign to the same name twice:
5483 if (pc->stores == NULL) {
5484 RETURN_IF_FALSE(pc->stores = PySet_New(NULL));
5485 }
5486 else {
5487 int duplicate = PySet_Contains(pc->stores, n);
5488 if (duplicate < 0) {
5489 return 0;
5490 }
5491 if (duplicate) {
5492 const char *e = "multiple assignments to name %R in pattern";
5493 return compiler_error(c, e, n);
5494 }
5495 }
5496 RETURN_IF_FALSE(!PySet_Add(pc->stores, n));
5497 RETURN_IF_FALSE(compiler_nameop(c, n, Store));
5498 return 1;
5499}
5500
5501
5502static int
5503pattern_helper_sequence_unpack(struct compiler *c, asdl_expr_seq *values,
5504 Py_ssize_t star, pattern_context *pc)
5505{
5506 RETURN_IF_FALSE(unpack_helper(c, values));
5507 // We've now got a bunch of new subjects on the stack. If any of them fail
5508 // to match, we need to pop everything else off, then finally push False.
5509 // fails is an array of blocks that correspond to the necessary amount of
5510 // popping for each element:
5511 basicblock **fails;
5512 Py_ssize_t size = asdl_seq_LEN(values);
5513 fails = (basicblock **)PyObject_Malloc(sizeof(basicblock*) * size);
5514 if (fails == NULL) {
5515 PyErr_NoMemory();
5516 return 0;
5517 }
5518 // NOTE: Can't use our nice returning macros anymore: they'll leak memory!
5519 // goto error on error.
5520 for (Py_ssize_t i = 0; i < size; i++) {
5521 fails[i] = compiler_new_block(c);
5522 if (fails[i] == NULL) {
5523 goto error;
5524 }
5525 }
5526 for (Py_ssize_t i = 0; i < size; i++) {
5527 expr_ty value = asdl_seq_GET(values, i);
5528 if (i == star) {
5529 assert(value->kind == Starred_kind);
5530 value = value->v.Starred.value;
5531 }
5532 if (!compiler_pattern_subpattern(c, value, pc) ||
5533 !compiler_addop_j(c, POP_JUMP_IF_FALSE, fails[i]) ||
5534 compiler_next_block(c) == NULL)
5535 {
5536 goto error;
5537 }
5538 }
5539 // Success!
5540 basicblock *end = compiler_new_block(c);
5541 if (end == NULL ||
5542 !compiler_addop_load_const(c, Py_True) ||
5543 !compiler_addop_j(c, JUMP_FORWARD, end))
5544 {
5545 goto error;
5546 }
5547 // This is where we handle failed sub-patterns. For a sequence pattern like
5548 // [a, b, c, d], this will look like:
5549 // fails[0]: POP_TOP
5550 // fails[1]: POP_TOP
5551 // fails[2]: POP_TOP
5552 // fails[3]: LOAD_CONST False
5553 for (Py_ssize_t i = 0; i < size - 1; i++) {
5554 compiler_use_next_block(c, fails[i]);
5555 if (!compiler_addop(c, POP_TOP)) {
5556 goto error;
5557 }
5558 }
5559 compiler_use_next_block(c, fails[size - 1]);
5560 if (!compiler_addop_load_const(c, Py_False)) {
5561 goto error;
5562 }
5563 compiler_use_next_block(c, end);
5564 PyObject_Free(fails);
5565 return 1;
5566error:
5567 PyObject_Free(fails);
5568 return 0;
5569}
5570
5571// Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of
5572// UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a
5573// starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc.
5574static int
5575pattern_helper_sequence_subscr(struct compiler *c, asdl_expr_seq *values,
5576 Py_ssize_t star, pattern_context *pc)
5577{
5578 basicblock *end, *fail_pop_1;
5579 RETURN_IF_FALSE(end = compiler_new_block(c));
5580 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5581 Py_ssize_t size = asdl_seq_LEN(values);
5582 for (Py_ssize_t i = 0; i < size; i++) {
5583 expr_ty value = asdl_seq_GET(values, i);
5584 if (WILDCARD_CHECK(value)) {
5585 continue;
5586 }
5587 if (i == star) {
5588 assert(value->kind == Starred_kind);
5589 assert(WILDCARD_CHECK(value->v.Starred.value));
5590 continue;
5591 }
5592 ADDOP(c, DUP_TOP);
5593 if (i < star) {
5594 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5595 }
5596 else {
5597 // The subject may not support negative indexing! Compute a
5598 // nonnegative index:
5599 ADDOP(c, GET_LEN);
5600 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i));
5601 ADDOP(c, BINARY_SUBTRACT);
5602 }
5603 ADDOP(c, BINARY_SUBSCR);
5604 RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc));
5605 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5606 NEXT_BLOCK(c);
5607 }
5608 ADDOP(c, POP_TOP);
5609 ADDOP_LOAD_CONST(c, Py_True);
5610 ADDOP_JUMP(c, JUMP_FORWARD, end);
5611 compiler_use_next_block(c, fail_pop_1);
5612 ADDOP(c, POP_TOP);
5613 ADDOP_LOAD_CONST(c, Py_False);
5614 compiler_use_next_block(c, end);
5615 return 1;
5616}
5617
5618
5619// Like compiler_pattern, but turn off checks for irrefutability.
5620static int
5621compiler_pattern_subpattern(struct compiler *c, expr_ty p, pattern_context *pc)
5622{
5623 int allow_irrefutable = pc->allow_irrefutable;
5624 pc->allow_irrefutable = 1;
5625 RETURN_IF_FALSE(compiler_pattern(c, p, pc));
5626 pc->allow_irrefutable = allow_irrefutable;
5627 return 1;
5628}
5629
5630
5631static int
5632compiler_pattern_as(struct compiler *c, expr_ty p, pattern_context *pc)
5633{
5634 assert(p->kind == MatchAs_kind);
5635 basicblock *end, *fail_pop_1;
5636 RETURN_IF_FALSE(end = compiler_new_block(c));
5637 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5638 // Need to make a copy for (possibly) storing later:
5639 ADDOP(c, DUP_TOP);
5640 RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc));
5641 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5642 NEXT_BLOCK(c);
5643 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc));
5644 ADDOP_LOAD_CONST(c, Py_True);
5645 ADDOP_JUMP(c, JUMP_FORWARD, end);
5646 compiler_use_next_block(c, fail_pop_1);
5647 // Need to pop that unused copy from before:
5648 ADDOP(c, POP_TOP);
5649 ADDOP_LOAD_CONST(c, Py_False);
5650 compiler_use_next_block(c, end);
5651 return 1;
5652}
5653
5654
5655static int
5656compiler_pattern_capture(struct compiler *c, expr_ty p, pattern_context *pc)
5657{
5658 assert(p->kind == Name_kind);
5659 assert(p->v.Name.ctx == Store);
5660 assert(!WILDCARD_CHECK(p));
5661 if (!pc->allow_irrefutable) {
5662 // Whoops, can't have a name capture here!
5663 const char *e = "name capture %R makes remaining patterns unreachable";
5664 return compiler_error(c, e, p->v.Name.id);
5665 }
5666 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.Name.id, pc));
5667 ADDOP_LOAD_CONST(c, Py_True);
5668 return 1;
5669}
5670
5671
5672static int
5673compiler_pattern_class(struct compiler *c, expr_ty p, pattern_context *pc)
5674{
5675 asdl_expr_seq *args = p->v.Call.args;
5676 asdl_keyword_seq *kwargs = p->v.Call.keywords;
5677 Py_ssize_t nargs = asdl_seq_LEN(args);
5678 Py_ssize_t nkwargs = asdl_seq_LEN(kwargs);
5679 if (INT_MAX < nargs || INT_MAX < nargs + nkwargs - 1) {
5680 const char *e = "too many sub-patterns in class pattern %R";
5681 return compiler_error(c, e, p->v.Call.func);
5682 }
5683 RETURN_IF_FALSE(!validate_keywords(c, kwargs));
5684 basicblock *end, *fail_pop_1;
5685 RETURN_IF_FALSE(end = compiler_new_block(c));
5686 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5687 VISIT(c, expr, p->v.Call.func);
5688 PyObject *kwnames;
5689 RETURN_IF_FALSE(kwnames = PyTuple_New(nkwargs));
5690 Py_ssize_t i;
5691 for (i = 0; i < nkwargs; i++) {
5692 PyObject *name = ((keyword_ty) asdl_seq_GET(kwargs, i))->arg;
5693 Py_INCREF(name);
5694 PyTuple_SET_ITEM(kwnames, i, name);
5695 }
5696 ADDOP_LOAD_CONST_NEW(c, kwnames);
5697 ADDOP_I(c, MATCH_CLASS, nargs);
5698 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5699 NEXT_BLOCK(c);
5700 // TOS is now a tuple of (nargs + nkwargs) attributes.
5701 for (i = 0; i < nargs + nkwargs; i++) {
5702 expr_ty arg;
5703 if (i < nargs) {
5704 // Positional:
5705 arg = asdl_seq_GET(args, i);
5706 }
5707 else {
5708 // Keyword:
5709 arg = ((keyword_ty) asdl_seq_GET(kwargs, i - nargs))->value;
5710 }
5711 if (WILDCARD_CHECK(arg)) {
5712 continue;
5713 }
5714 // Get the i-th attribute, and match it against the i-th pattern:
5715 ADDOP(c, DUP_TOP);
5716 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5717 ADDOP(c, BINARY_SUBSCR);
5718 RETURN_IF_FALSE(compiler_pattern_subpattern(c, arg, pc));
5719 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5720 NEXT_BLOCK(c);
5721 }
5722 // Success! Pop the tuple of attributes:
5723 ADDOP(c, POP_TOP);
5724 ADDOP_LOAD_CONST(c, Py_True);
5725 ADDOP_JUMP(c, JUMP_FORWARD, end);
5726 compiler_use_next_block(c, fail_pop_1);
5727 ADDOP(c, POP_TOP);
5728 ADDOP_LOAD_CONST(c, Py_False);
5729 compiler_use_next_block(c, end);
5730 return 1;
5731}
5732
5733
5734static int
5735compiler_pattern_literal(struct compiler *c, expr_ty p, pattern_context *pc)
5736{
5737 assert(p->kind == Constant_kind);
5738 PyObject *v = p->v.Constant.value;
5739 ADDOP_LOAD_CONST(c, v);
5740 // Literal True, False, and None are compared by identity. All others use
5741 // equality:
5742 ADDOP_COMPARE(c, (v == Py_None || PyBool_Check(v)) ? Is : Eq);
5743 return 1;
5744}
5745
5746
5747static int
5748compiler_pattern_mapping(struct compiler *c, expr_ty p, pattern_context *pc)
5749{
5750 basicblock *end, *fail_pop_1, *fail_pop_3;
5751 RETURN_IF_FALSE(end = compiler_new_block(c));
5752 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5753 RETURN_IF_FALSE(fail_pop_3 = compiler_new_block(c));
5754 asdl_expr_seq *keys = p->v.Dict.keys;
5755 asdl_expr_seq *values = p->v.Dict.values;
5756 Py_ssize_t size = asdl_seq_LEN(values);
Ikko Ashimine57827f82021-03-10 19:39:51 +09005757 // A starred pattern will be a keyless value. It is guaranteed to be last:
Brandt Bucher145bf262021-02-26 14:51:55 -08005758 int star = size ? !asdl_seq_GET(keys, size - 1) : 0;
5759 ADDOP(c, MATCH_MAPPING);
5760 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5761 NEXT_BLOCK(c);
5762 if (!size) {
5763 // If the pattern is just "{}", we're done!
5764 ADDOP(c, POP_TOP);
5765 ADDOP_LOAD_CONST(c, Py_True);
5766 ADDOP_JUMP(c, JUMP_FORWARD, end);
5767 compiler_use_next_block(c, fail_pop_1);
5768 ADDOP(c, POP_TOP);
5769 ADDOP_LOAD_CONST(c, Py_False);
5770 compiler_use_next_block(c, end);
5771 return 1;
5772 }
5773 if (size - star) {
5774 // If the pattern has any keys in it, perform a length check:
5775 ADDOP(c, GET_LEN);
5776 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - star));
5777 ADDOP_COMPARE(c, GtE);
5778 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5779 NEXT_BLOCK(c);
5780 }
5781 if (INT_MAX < size - star - 1) {
5782 return compiler_error(c, "too many sub-patterns in mapping pattern");
5783 }
5784 // Collect all of the keys into a tuple for MATCH_KEYS and
5785 // COPY_DICT_WITHOUT_KEYS. They can either be dotted names or literals:
5786 for (Py_ssize_t i = 0; i < size - star; i++) {
5787 expr_ty key = asdl_seq_GET(keys, i);
5788 if (key == NULL) {
5789 const char *e = "can't use starred name here "
5790 "(consider moving to end)";
5791 return compiler_error(c, e);
5792 }
5793 VISIT(c, expr, key);
5794 }
5795 ADDOP_I(c, BUILD_TUPLE, size - star);
5796 ADDOP(c, MATCH_KEYS);
5797 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3);
5798 NEXT_BLOCK(c);
5799 // So far so good. There's now a tuple of values on the stack to match
5800 // sub-patterns against:
5801 for (Py_ssize_t i = 0; i < size - star; i++) {
5802 expr_ty value = asdl_seq_GET(values, i);
5803 if (WILDCARD_CHECK(value)) {
5804 continue;
5805 }
5806 ADDOP(c, DUP_TOP);
5807 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5808 ADDOP(c, BINARY_SUBSCR);
5809 RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc));
5810 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3);
5811 NEXT_BLOCK(c);
5812 }
5813 // If we get this far, it's a match! We're done with that tuple of values.
5814 ADDOP(c, POP_TOP);
5815 if (star) {
5816 // If we had a starred name, bind a dict of remaining items to it:
5817 ADDOP(c, COPY_DICT_WITHOUT_KEYS);
5818 PyObject *id = asdl_seq_GET(values, size - 1)->v.Name.id;
5819 RETURN_IF_FALSE(pattern_helper_store_name(c, id, pc));
5820 }
5821 else {
5822 // Otherwise, we don't care about this tuple of keys anymore:
5823 ADDOP(c, POP_TOP);
5824 }
5825 // Pop the subject:
5826 ADDOP(c, POP_TOP);
5827 ADDOP_LOAD_CONST(c, Py_True);
5828 ADDOP_JUMP(c, JUMP_FORWARD, end);
5829 // The top two items are a tuple of values or None, followed by a tuple of
5830 // keys. Pop them both:
5831 compiler_use_next_block(c, fail_pop_3);
5832 ADDOP(c, POP_TOP);
5833 ADDOP(c, POP_TOP);
5834 compiler_use_next_block(c, fail_pop_1);
5835 // Pop the subject:
5836 ADDOP(c, POP_TOP);
5837 ADDOP_LOAD_CONST(c, Py_False);
5838 compiler_use_next_block(c, end);
5839 return 1;
5840}
5841
5842
5843static int
5844compiler_pattern_or(struct compiler *c, expr_ty p, pattern_context *pc)
5845{
5846 assert(p->kind == MatchOr_kind);
5847 // control is the set of names bound by the first alternative. If all of the
5848 // others bind the same names (they should), then this becomes pc->stores.
5849 PyObject *control = NULL;
5850 basicblock *end, *pass_pop_1;
5851 RETURN_IF_FALSE(end = compiler_new_block(c));
5852 RETURN_IF_FALSE(pass_pop_1 = compiler_new_block(c));
5853 Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns);
5854 assert(size > 1);
5855 // We're going to be messing with pc. Keep the original info handy:
5856 PyObject *stores_init = pc->stores;
5857 int allow_irrefutable = pc->allow_irrefutable;
5858 for (Py_ssize_t i = 0; i < size; i++) {
5859 // NOTE: Can't use our nice returning macros in here: they'll leak sets!
5860 expr_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i);
5861 pc->stores = PySet_New(stores_init);
5862 // An irrefutable sub-pattern must be last, if it is allowed at all:
5863 int is_last = i == size - 1;
5864 pc->allow_irrefutable = allow_irrefutable && is_last;
5865 SET_LOC(c, alt);
5866 if (pc->stores == NULL ||
5867 // Only copy the subject if we're *not* on the last alternative:
5868 (!is_last && !compiler_addop(c, DUP_TOP)) ||
5869 !compiler_pattern(c, alt, pc) ||
5870 // Only jump if we're *not* on the last alternative:
5871 (!is_last && !compiler_addop_j(c, POP_JUMP_IF_TRUE, pass_pop_1)) ||
5872 !compiler_next_block(c))
5873 {
5874 goto fail;
5875 }
5876 if (!i) {
5877 // If this is the first alternative, save its stores as a "control"
5878 // for the others (they can't bind a different set of names):
5879 control = pc->stores;
5880 continue;
5881 }
5882 if (PySet_GET_SIZE(pc->stores) || PySet_GET_SIZE(control)) {
5883 // Otherwise, check to see if we differ from the control set:
5884 PyObject *diff = PyNumber_InPlaceXor(pc->stores, control);
5885 if (diff == NULL) {
5886 goto fail;
5887 }
5888 if (PySet_GET_SIZE(diff)) {
5889 // The names differ! Raise.
5890 Py_DECREF(diff);
5891 compiler_error(c, "alternative patterns bind different names");
5892 goto fail;
5893 }
5894 Py_DECREF(diff);
5895 }
5896 Py_DECREF(pc->stores);
5897 }
5898 Py_XDECREF(stores_init);
5899 // Update pc->stores and restore pc->allow_irrefutable:
5900 pc->stores = control;
5901 pc->allow_irrefutable = allow_irrefutable;
5902 ADDOP_JUMP(c, JUMP_FORWARD, end);
5903 compiler_use_next_block(c, pass_pop_1);
5904 ADDOP(c, POP_TOP);
5905 ADDOP_LOAD_CONST(c, Py_True);
5906 compiler_use_next_block(c, end);
5907 return 1;
5908fail:
5909 Py_XDECREF(stores_init);
5910 Py_XDECREF(control);
5911 return 0;
5912}
5913
5914
5915static int
5916compiler_pattern_sequence(struct compiler *c, expr_ty p, pattern_context *pc)
5917{
5918 assert(p->kind == List_kind || p->kind == Tuple_kind);
5919 asdl_expr_seq *values = (p->kind == Tuple_kind) ? p->v.Tuple.elts
5920 : p->v.List.elts;
5921 Py_ssize_t size = asdl_seq_LEN(values);
5922 Py_ssize_t star = -1;
5923 int only_wildcard = 1;
5924 int star_wildcard = 0;
5925 // Find a starred name, if it exists. There may be at most one:
5926 for (Py_ssize_t i = 0; i < size; i++) {
5927 expr_ty value = asdl_seq_GET(values, i);
5928 if (value->kind == Starred_kind) {
5929 value = value->v.Starred.value;
5930 if (star >= 0) {
5931 const char *e = "multiple starred names in sequence pattern";
5932 return compiler_error(c, e);
5933 }
5934 star_wildcard = WILDCARD_CHECK(value);
5935 star = i;
5936 }
5937 only_wildcard &= WILDCARD_CHECK(value);
5938 }
5939 basicblock *end, *fail_pop_1;
5940 RETURN_IF_FALSE(end = compiler_new_block(c));
5941 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5942 ADDOP(c, MATCH_SEQUENCE);
5943 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5944 NEXT_BLOCK(c);
5945 if (star < 0) {
5946 // No star: len(subject) == size
5947 ADDOP(c, GET_LEN);
5948 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
5949 ADDOP_COMPARE(c, Eq);
5950 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5951 NEXT_BLOCK(c);
5952 }
5953 else if (size > 1) {
5954 // Star: len(subject) >= size - 1
5955 ADDOP(c, GET_LEN);
5956 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1));
5957 ADDOP_COMPARE(c, GtE);
5958 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5959 NEXT_BLOCK(c);
5960 }
5961 if (only_wildcard) {
5962 // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc.
5963 ADDOP(c, POP_TOP);
5964 ADDOP_LOAD_CONST(c, Py_True);
5965 }
5966 else if (star_wildcard) {
5967 RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, values, star, pc));
5968 }
5969 else {
5970 RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, values, star, pc));
5971 }
5972 ADDOP_JUMP(c, JUMP_FORWARD, end);
5973 compiler_use_next_block(c, fail_pop_1);
5974 ADDOP(c, POP_TOP)
5975 ADDOP_LOAD_CONST(c, Py_False);
5976 compiler_use_next_block(c, end);
5977 return 1;
5978}
5979
5980
5981static int
5982compiler_pattern_value(struct compiler *c, expr_ty p, pattern_context *pc)
5983{
5984 assert(p->kind == Attribute_kind);
5985 assert(p->v.Attribute.ctx == Load);
5986 VISIT(c, expr, p);
5987 ADDOP_COMPARE(c, Eq);
5988 return 1;
5989}
5990
5991
5992static int
5993compiler_pattern_wildcard(struct compiler *c, expr_ty p, pattern_context *pc)
5994{
5995 assert(p->kind == Name_kind);
5996 assert(p->v.Name.ctx == Store);
5997 assert(WILDCARD_CHECK(p));
5998 if (!pc->allow_irrefutable) {
5999 // Whoops, can't have a wildcard here!
6000 const char *e = "wildcard makes remaining patterns unreachable";
6001 return compiler_error(c, e);
6002 }
6003 ADDOP(c, POP_TOP);
6004 ADDOP_LOAD_CONST(c, Py_True);
6005 return 1;
6006}
6007
6008
6009static int
6010compiler_pattern(struct compiler *c, expr_ty p, pattern_context *pc)
6011{
6012 SET_LOC(c, p);
6013 switch (p->kind) {
6014 case Attribute_kind:
6015 return compiler_pattern_value(c, p, pc);
6016 case BinOp_kind:
6017 // Because we allow "2+2j", things like "2+2" make it this far:
6018 return compiler_error(c, "patterns cannot include operators");
6019 case Call_kind:
6020 return compiler_pattern_class(c, p, pc);
6021 case Constant_kind:
6022 return compiler_pattern_literal(c, p, pc);
6023 case Dict_kind:
6024 return compiler_pattern_mapping(c, p, pc);
6025 case JoinedStr_kind:
6026 // Because we allow strings, f-strings make it this far:
6027 return compiler_error(c, "patterns cannot include f-strings");
6028 case List_kind:
6029 case Tuple_kind:
6030 return compiler_pattern_sequence(c, p, pc);
6031 case MatchAs_kind:
6032 return compiler_pattern_as(c, p, pc);
6033 case MatchOr_kind:
6034 return compiler_pattern_or(c, p, pc);
6035 case Name_kind:
6036 if (WILDCARD_CHECK(p)) {
6037 return compiler_pattern_wildcard(c, p, pc);
6038 }
6039 return compiler_pattern_capture(c, p, pc);
6040 default:
6041 Py_UNREACHABLE();
6042 }
6043}
6044
6045
6046static int
6047compiler_match(struct compiler *c, stmt_ty s)
6048{
6049 VISIT(c, expr, s->v.Match.subject);
6050 basicblock *next, *end;
6051 RETURN_IF_FALSE(end = compiler_new_block(c));
6052 Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases);
6053 assert(cases);
6054 pattern_context pc;
6055 // We use pc.stores to track:
6056 // - Repeated name assignments in the same pattern.
6057 // - Different name assignments in alternatives.
6058 // It's a set of names, but we don't create it until it's needed:
6059 pc.stores = NULL;
6060 match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6061 int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases;
6062 for (Py_ssize_t i = 0; i < cases - has_default; i++) {
6063 m = asdl_seq_GET(s->v.Match.cases, i);
6064 SET_LOC(c, m->pattern);
6065 RETURN_IF_FALSE(next = compiler_new_block(c));
6066 // If pc.allow_irrefutable is 0, any name captures against our subject
6067 // will raise. Irrefutable cases must be either guarded, last, or both:
6068 pc.allow_irrefutable = m->guard != NULL || i == cases - 1;
6069 // Only copy the subject if we're *not* on the last case:
6070 if (i != cases - has_default - 1) {
6071 ADDOP(c, DUP_TOP);
6072 }
6073 int result = compiler_pattern(c, m->pattern, &pc);
6074 Py_CLEAR(pc.stores);
6075 RETURN_IF_FALSE(result);
6076 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, next);
6077 NEXT_BLOCK(c);
6078 if (m->guard) {
6079 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, next, 0));
6080 }
6081 // Success! Pop the subject off, we're done with it:
6082 if (i != cases - has_default - 1) {
6083 ADDOP(c, POP_TOP);
6084 }
6085 VISIT_SEQ(c, stmt, m->body);
6086 ADDOP_JUMP(c, JUMP_FORWARD, end);
6087 compiler_use_next_block(c, next);
6088 }
6089 if (has_default) {
6090 if (cases == 1) {
6091 // No matches. Done with the subject:
6092 ADDOP(c, POP_TOP);
6093 }
6094 // A trailing "case _" is common, and lets us save a bit of redundant
6095 // pushing and popping in the loop above:
6096 m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6097 SET_LOC(c, m->pattern);
6098 if (m->guard) {
6099 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0));
6100 }
6101 VISIT_SEQ(c, stmt, m->body);
6102 }
6103 compiler_use_next_block(c, end);
6104 return 1;
6105}
6106
6107
6108#undef WILDCARD_CHECK
6109
6110
Thomas Wouters89f507f2006-12-13 04:49:30 +00006111/* End of the compiler section, beginning of the assembler section */
6112
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006113/* do depth-first search of basic block graph, starting with block.
T. Wouters99b54d62019-09-12 07:05:33 -07006114 post records the block indices in post-order.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006115
6116 XXX must handle implicit jumps from one block to next
6117*/
6118
Thomas Wouters89f507f2006-12-13 04:49:30 +00006119struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006120 PyObject *a_bytecode; /* string containing bytecode */
6121 int a_offset; /* offset into bytecode */
6122 int a_nblocks; /* number of reachable blocks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006123 PyObject *a_lnotab; /* string containing lnotab */
6124 int a_lnotab_off; /* offset into lnotab */
Mark Shannon877df852020-11-12 09:43:29 +00006125 int a_prevlineno; /* lineno of last emitted line in line table */
6126 int a_lineno; /* lineno of last emitted instruction */
6127 int a_lineno_start; /* bytecode start offset of current lineno */
Mark Shannoncc75ab72020-11-12 19:49:33 +00006128 basicblock *a_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006129};
6130
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006131Py_LOCAL_INLINE(void)
6132stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006133{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02006134 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
Mark Shannonfee55262019-11-21 09:11:43 +00006135 if (b->b_startdepth < depth && b->b_startdepth < 100) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006136 assert(b->b_startdepth < 0);
6137 b->b_startdepth = depth;
6138 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02006139 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006140}
6141
6142/* Find the flow path that needs the largest stack. We assume that
6143 * cycles in the flow graph have no net effect on the stack depth.
6144 */
6145static int
6146stackdepth(struct compiler *c)
6147{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006148 basicblock *b, *entryblock = NULL;
6149 basicblock **stack, **sp;
6150 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006151 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006152 b->b_startdepth = INT_MIN;
6153 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006154 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006155 }
6156 if (!entryblock)
6157 return 0;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006158 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
6159 if (!stack) {
6160 PyErr_NoMemory();
6161 return -1;
6162 }
6163
6164 sp = stack;
6165 stackdepth_push(&sp, entryblock, 0);
6166 while (sp != stack) {
6167 b = *--sp;
6168 int depth = b->b_startdepth;
6169 assert(depth >= 0);
6170 basicblock *next = b->b_next;
6171 for (int i = 0; i < b->b_iused; i++) {
6172 struct instr *instr = &b->b_instr[i];
6173 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
6174 if (effect == PY_INVALID_STACK_EFFECT) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01006175 PyErr_Format(PyExc_SystemError,
6176 "compiler stack_effect(opcode=%d, arg=%i) failed",
6177 instr->i_opcode, instr->i_oparg);
6178 return -1;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006179 }
6180 int new_depth = depth + effect;
6181 if (new_depth > maxdepth) {
6182 maxdepth = new_depth;
6183 }
6184 assert(depth >= 0); /* invalid code or bug in stackdepth() */
Mark Shannon582aaf12020-08-04 17:30:11 +01006185 if (is_jump(instr)) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006186 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
6187 assert(effect != PY_INVALID_STACK_EFFECT);
6188 int target_depth = depth + effect;
6189 if (target_depth > maxdepth) {
6190 maxdepth = target_depth;
6191 }
6192 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006193 stackdepth_push(&sp, instr->i_target, target_depth);
6194 }
6195 depth = new_depth;
6196 if (instr->i_opcode == JUMP_ABSOLUTE ||
6197 instr->i_opcode == JUMP_FORWARD ||
6198 instr->i_opcode == RETURN_VALUE ||
Mark Shannonfee55262019-11-21 09:11:43 +00006199 instr->i_opcode == RAISE_VARARGS ||
6200 instr->i_opcode == RERAISE)
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006201 {
6202 /* remaining code is dead */
6203 next = NULL;
6204 break;
6205 }
6206 }
6207 if (next != NULL) {
Mark Shannon266b4622020-11-17 19:30:14 +00006208 assert(b->b_nofallthrough == 0);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006209 stackdepth_push(&sp, next, depth);
6210 }
6211 }
6212 PyObject_Free(stack);
6213 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006214}
6215
6216static int
6217assemble_init(struct assembler *a, int nblocks, int firstlineno)
6218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006219 memset(a, 0, sizeof(struct assembler));
Mark Shannon877df852020-11-12 09:43:29 +00006220 a->a_prevlineno = a->a_lineno = firstlineno;
Mark Shannonfd009e62020-11-13 12:53:53 +00006221 a->a_lnotab = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006222 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006223 if (a->a_bytecode == NULL) {
6224 goto error;
6225 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006226 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006227 if (a->a_lnotab == NULL) {
6228 goto error;
6229 }
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07006230 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006231 PyErr_NoMemory();
Mark Shannonfd009e62020-11-13 12:53:53 +00006232 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006233 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006234 return 1;
Mark Shannonfd009e62020-11-13 12:53:53 +00006235error:
6236 Py_XDECREF(a->a_bytecode);
6237 Py_XDECREF(a->a_lnotab);
6238 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006239}
6240
6241static void
6242assemble_free(struct assembler *a)
6243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006244 Py_XDECREF(a->a_bytecode);
6245 Py_XDECREF(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006246}
6247
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006248static int
6249blocksize(basicblock *b)
6250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006251 int i;
6252 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006254 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006255 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006256 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006257}
6258
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006259static int
Mark Shannon877df852020-11-12 09:43:29 +00006260assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006261{
Mark Shannon877df852020-11-12 09:43:29 +00006262 Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006263 if (a->a_lnotab_off + 2 >= len) {
6264 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
6265 return 0;
6266 }
Pablo Galindo86e322f2021-01-30 13:54:22 +00006267 unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab);
6268 lnotab += a->a_lnotab_off;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006269 a->a_lnotab_off += 2;
Mark Shannon877df852020-11-12 09:43:29 +00006270 *lnotab++ = bdelta;
6271 *lnotab++ = ldelta;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006272 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006273}
6274
Mark Shannon877df852020-11-12 09:43:29 +00006275/* Appends a range to the end of the line number table. See
6276 * Objects/lnotab_notes.txt for the description of the line number table. */
6277
6278static int
6279assemble_line_range(struct assembler *a)
6280{
6281 int ldelta, bdelta;
6282 bdelta = (a->a_offset - a->a_lineno_start) * 2;
6283 if (bdelta == 0) {
6284 return 1;
6285 }
6286 if (a->a_lineno < 0) {
6287 ldelta = -128;
6288 }
6289 else {
6290 ldelta = a->a_lineno - a->a_prevlineno;
6291 a->a_prevlineno = a->a_lineno;
6292 while (ldelta > 127) {
6293 if (!assemble_emit_linetable_pair(a, 0, 127)) {
6294 return 0;
6295 }
6296 ldelta -= 127;
6297 }
6298 while (ldelta < -127) {
6299 if (!assemble_emit_linetable_pair(a, 0, -127)) {
6300 return 0;
6301 }
6302 ldelta += 127;
6303 }
6304 }
6305 assert(-128 <= ldelta && ldelta < 128);
6306 while (bdelta > 254) {
6307 if (!assemble_emit_linetable_pair(a, 254, ldelta)) {
6308 return 0;
6309 }
6310 ldelta = a->a_lineno < 0 ? -128 : 0;
6311 bdelta -= 254;
6312 }
6313 if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) {
6314 return 0;
6315 }
6316 a->a_lineno_start = a->a_offset;
6317 return 1;
6318}
6319
6320static int
6321assemble_lnotab(struct assembler *a, struct instr *i)
6322{
6323 if (i->i_lineno == a->a_lineno) {
6324 return 1;
6325 }
6326 if (!assemble_line_range(a)) {
6327 return 0;
6328 }
6329 a->a_lineno = i->i_lineno;
6330 return 1;
6331}
6332
6333
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006334/* assemble_emit()
6335 Extend the bytecode with a new instruction.
6336 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00006337*/
6338
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006339static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006340assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006341{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006342 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006343 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03006344 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006345
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006346 arg = i->i_oparg;
6347 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006348 if (i->i_lineno && !assemble_lnotab(a, i))
6349 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006350 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006351 if (len > PY_SSIZE_T_MAX / 2)
6352 return 0;
6353 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
6354 return 0;
6355 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03006356 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006357 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006358 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006359 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006360}
6361
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00006362static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006363assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006365 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006366 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006367 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00006368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006369 /* Compute the size of each block and fixup jump args.
6370 Replace block pointer with position in bytecode. */
6371 do {
6372 totsize = 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006373 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006374 bsize = blocksize(b);
6375 b->b_offset = totsize;
6376 totsize += bsize;
6377 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006378 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006379 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6380 bsize = b->b_offset;
6381 for (i = 0; i < b->b_iused; i++) {
6382 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006383 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006384 /* Relative jumps are computed relative to
6385 the instruction pointer after fetching
6386 the jump instruction.
6387 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006388 bsize += isize;
Mark Shannon582aaf12020-08-04 17:30:11 +01006389 if (is_jump(instr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006390 instr->i_oparg = instr->i_target->b_offset;
Mark Shannon582aaf12020-08-04 17:30:11 +01006391 if (is_relative_jump(instr)) {
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006392 instr->i_oparg -= bsize;
6393 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03006394 instr->i_oparg *= sizeof(_Py_CODEUNIT);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006395 if (instrsize(instr->i_oparg) != isize) {
6396 extended_arg_recompile = 1;
6397 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006398 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006399 }
6400 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00006401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006402 /* XXX: This is an awful hack that could hurt performance, but
6403 on the bright side it should work until we come up
6404 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006406 The issue is that in the first loop blocksize() is called
6407 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006408 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006409 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006411 So we loop until we stop seeing new EXTENDED_ARGs.
6412 The only EXTENDED_ARGs that could be popping up are
6413 ones in jump instructions. So this should converge
6414 fairly quickly.
6415 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006416 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00006417}
6418
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006419static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01006420dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006422 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006423 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006425 tuple = PyTuple_New(size);
6426 if (tuple == NULL)
6427 return NULL;
6428 while (PyDict_Next(dict, &pos, &k, &v)) {
6429 i = PyLong_AS_LONG(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006430 Py_INCREF(k);
6431 assert((i - offset) < size);
6432 assert((i - offset) >= 0);
6433 PyTuple_SET_ITEM(tuple, i - offset, k);
6434 }
6435 return tuple;
6436}
6437
6438static PyObject *
6439consts_dict_keys_inorder(PyObject *dict)
6440{
6441 PyObject *consts, *k, *v;
6442 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
6443
6444 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
6445 if (consts == NULL)
6446 return NULL;
6447 while (PyDict_Next(dict, &pos, &k, &v)) {
6448 i = PyLong_AS_LONG(v);
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +03006449 /* The keys of the dictionary can be tuples wrapping a contant.
6450 * (see compiler_add_o and _PyCode_ConstantKey). In that case
6451 * the object we want is always second. */
6452 if (PyTuple_CheckExact(k)) {
6453 k = PyTuple_GET_ITEM(k, 1);
6454 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006455 Py_INCREF(k);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006456 assert(i < size);
6457 assert(i >= 0);
6458 PyList_SET_ITEM(consts, i, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006459 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006460 return consts;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006461}
6462
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006463static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006464compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006466 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006467 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006468 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04006469 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006470 if (ste->ste_nested)
6471 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07006472 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006473 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07006474 if (!ste->ste_generator && ste->ste_coroutine)
6475 flags |= CO_COROUTINE;
6476 if (ste->ste_generator && ste->ste_coroutine)
6477 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006478 if (ste->ste_varargs)
6479 flags |= CO_VARARGS;
6480 if (ste->ste_varkeywords)
6481 flags |= CO_VARKEYWORDS;
6482 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006484 /* (Only) inherit compilerflags in PyCF_MASK */
6485 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006486
Pablo Galindo90235812020-03-15 04:29:22 +00006487 if ((IS_TOP_LEVEL_AWAIT(c)) &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07006488 ste->ste_coroutine &&
6489 !ste->ste_generator) {
6490 flags |= CO_COROUTINE;
6491 }
6492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006493 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00006494}
6495
Inada Naokibdb941b2021-02-10 09:20:42 +09006496// Merge *obj* with constant cache.
INADA Naokic2e16072018-11-26 21:23:22 +09006497// Unlike merge_consts_recursive(), this function doesn't work recursively.
6498static int
Inada Naokibdb941b2021-02-10 09:20:42 +09006499merge_const_one(struct compiler *c, PyObject **obj)
INADA Naokic2e16072018-11-26 21:23:22 +09006500{
Inada Naokibdb941b2021-02-10 09:20:42 +09006501 PyObject *key = _PyCode_ConstantKey(*obj);
INADA Naokic2e16072018-11-26 21:23:22 +09006502 if (key == NULL) {
6503 return 0;
6504 }
6505
6506 // t is borrowed reference
6507 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
6508 Py_DECREF(key);
6509 if (t == NULL) {
6510 return 0;
6511 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006512 if (t == key) { // obj is new constant.
INADA Naokic2e16072018-11-26 21:23:22 +09006513 return 1;
6514 }
6515
Inada Naokibdb941b2021-02-10 09:20:42 +09006516 if (PyTuple_CheckExact(t)) {
6517 // t is still borrowed reference
6518 t = PyTuple_GET_ITEM(t, 1);
6519 }
6520
6521 Py_INCREF(t);
6522 Py_DECREF(*obj);
6523 *obj = t;
INADA Naokic2e16072018-11-26 21:23:22 +09006524 return 1;
6525}
6526
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006527static PyCodeObject *
Mark Shannon6e8128f2020-07-30 10:03:00 +01006528makecode(struct compiler *c, struct assembler *a, PyObject *consts)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006530 PyCodeObject *co = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006531 PyObject *names = NULL;
6532 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006533 PyObject *name = NULL;
6534 PyObject *freevars = NULL;
6535 PyObject *cellvars = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006536 Py_ssize_t nlocals;
6537 int nlocals_int;
6538 int flags;
Pablo Galindocd74e662019-06-01 18:08:04 +01006539 int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006541 names = dict_keys_inorder(c->u->u_names, 0);
6542 varnames = dict_keys_inorder(c->u->u_varnames, 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006543 if (!names || !varnames) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006544 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006545 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006546 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
6547 if (!cellvars)
6548 goto error;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006549 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006550 if (!freevars)
6551 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006552
Inada Naokibdb941b2021-02-10 09:20:42 +09006553 if (!merge_const_one(c, &names) ||
6554 !merge_const_one(c, &varnames) ||
6555 !merge_const_one(c, &cellvars) ||
6556 !merge_const_one(c, &freevars))
INADA Naokic2e16072018-11-26 21:23:22 +09006557 {
6558 goto error;
6559 }
6560
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006561 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01006562 assert(nlocals < INT_MAX);
6563 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
6564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006565 flags = compute_code_flags(c);
6566 if (flags < 0)
6567 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006568
Mark Shannon6e8128f2020-07-30 10:03:00 +01006569 consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
6570 if (consts == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006571 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006572 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006573 if (!merge_const_one(c, &consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006574 Py_DECREF(consts);
INADA Naokic2e16072018-11-26 21:23:22 +09006575 goto error;
6576 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006577
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01006578 posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int);
Pablo Galindocd74e662019-06-01 18:08:04 +01006579 posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +01006580 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006581 maxdepth = stackdepth(c);
6582 if (maxdepth < 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006583 Py_DECREF(consts);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006584 goto error;
6585 }
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006586 co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
Mark Shannon13bc1392020-01-23 09:25:17 +00006587 posonlyargcount, kwonlyargcount, nlocals_int,
Mark Shannon6e8128f2020-07-30 10:03:00 +01006588 maxdepth, flags, a->a_bytecode, consts, names,
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006589 varnames, freevars, cellvars, c->c_filename,
6590 c->u->u_name, c->u->u_firstlineno, a->a_lnotab);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006591 Py_DECREF(consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006592 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006593 Py_XDECREF(names);
6594 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006595 Py_XDECREF(name);
6596 Py_XDECREF(freevars);
6597 Py_XDECREF(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006598 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006599}
6600
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006601
6602/* For debugging purposes only */
6603#if 0
6604static void
6605dump_instr(const struct instr *i)
6606{
Mark Shannon582aaf12020-08-04 17:30:11 +01006607 const char *jrel = (is_relative_jump(instr)) ? "jrel " : "";
6608 const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : "";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006609 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006611 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006612 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006613 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006614 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006615 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
6616 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006617}
6618
6619static void
6620dump_basicblock(const basicblock *b)
6621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006622 const char *b_return = b->b_return ? "return " : "";
Pablo Galindo60eb9f12020-06-28 01:55:47 +01006623 fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n",
6624 b->b_iused, b->b_startdepth, b->b_offset, b_return);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006625 if (b->b_instr) {
6626 int i;
6627 for (i = 0; i < b->b_iused; i++) {
6628 fprintf(stderr, " [%02d] ", i);
6629 dump_instr(b->b_instr + i);
6630 }
6631 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006632}
6633#endif
6634
Mark Shannon5977a792020-12-02 13:31:40 +00006635
6636static int
6637normalize_basic_block(basicblock *bb);
6638
Mark Shannon6e8128f2020-07-30 10:03:00 +01006639static int
6640optimize_cfg(struct assembler *a, PyObject *consts);
6641
Mark Shannon5977a792020-12-02 13:31:40 +00006642static int
6643ensure_exits_have_lineno(struct compiler *c);
6644
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006645static PyCodeObject *
6646assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006647{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006648 basicblock *b, *entryblock;
6649 struct assembler a;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006650 int j, nblocks;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006651 PyCodeObject *co = NULL;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006652 PyObject *consts = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006654 /* Make sure every block that falls off the end returns None.
6655 XXX NEXT_BLOCK() isn't quite right, because if the last
6656 block ends with a jump or return b_next shouldn't set.
6657 */
6658 if (!c->u->u_curblock->b_return) {
Mark Shannon877df852020-11-12 09:43:29 +00006659 c->u->u_lineno = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006660 if (addNone)
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006661 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006662 ADDOP(c, RETURN_VALUE);
6663 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006664
Mark Shannon5977a792020-12-02 13:31:40 +00006665 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
6666 if (normalize_basic_block(b)) {
Alex Henrie503627f2021-03-02 03:20:25 -07006667 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00006668 }
6669 }
6670
6671 if (ensure_exits_have_lineno(c)) {
Alex Henrie503627f2021-03-02 03:20:25 -07006672 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00006673 }
6674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006675 nblocks = 0;
6676 entryblock = NULL;
6677 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6678 nblocks++;
6679 entryblock = b;
6680 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006682 /* Set firstlineno if it wasn't explicitly set. */
6683 if (!c->u->u_firstlineno) {
Ned Deilydc35cda2016-08-17 17:18:33 -04006684 if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006685 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
Mark Shannon877df852020-11-12 09:43:29 +00006686 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006687 c->u->u_firstlineno = 1;
6688 }
Mark Shannon5977a792020-12-02 13:31:40 +00006689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006690 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
6691 goto error;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006692 a.a_entry = entryblock;
6693 a.a_nblocks = nblocks;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006694
Mark Shannon6e8128f2020-07-30 10:03:00 +01006695 consts = consts_dict_keys_inorder(c->u->u_consts);
6696 if (consts == NULL) {
6697 goto error;
6698 }
6699 if (optimize_cfg(&a, consts)) {
6700 goto error;
6701 }
6702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006703 /* Can't modify the bytecode after computing jump offsets. */
6704 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00006705
Mark Shannoncc75ab72020-11-12 19:49:33 +00006706 /* Emit code. */
6707 for(b = entryblock; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006708 for (j = 0; j < b->b_iused; j++)
6709 if (!assemble_emit(&a, &b->b_instr[j]))
6710 goto error;
6711 }
Mark Shannon877df852020-11-12 09:43:29 +00006712 if (!assemble_line_range(&a)) {
6713 return 0;
6714 }
6715 /* Emit sentinel at end of line number table */
6716 if (!assemble_emit_linetable_pair(&a, 255, -128)) {
6717 goto error;
6718 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00006719
Inada Naokibdb941b2021-02-10 09:20:42 +09006720 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006721 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09006722 }
6723 if (!merge_const_one(c, &a.a_lnotab)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006724 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09006725 }
6726 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) {
6727 goto error;
6728 }
6729 if (!merge_const_one(c, &a.a_bytecode)) {
6730 goto error;
6731 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006732
Mark Shannon6e8128f2020-07-30 10:03:00 +01006733 co = makecode(c, &a, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006734 error:
Mark Shannon6e8128f2020-07-30 10:03:00 +01006735 Py_XDECREF(consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006736 assemble_free(&a);
6737 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006738}
Georg Brandl8334fd92010-12-04 10:26:46 +00006739
6740#undef PyAST_Compile
Benjamin Petersone5024512018-09-12 12:06:42 -07006741PyCodeObject *
Georg Brandl8334fd92010-12-04 10:26:46 +00006742PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
6743 PyArena *arena)
6744{
6745 return PyAST_CompileEx(mod, filename, flags, -1, arena);
6746}
Mark Shannon6e8128f2020-07-30 10:03:00 +01006747
6748
6749/* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
6750 with LOAD_CONST (c1, c2, ... cn).
6751 The consts table must still be in list form so that the
6752 new constant (c1, c2, ... cn) can be appended.
6753 Called with codestr pointing to the first LOAD_CONST.
6754*/
6755static int
6756fold_tuple_on_constants(struct instr *inst,
6757 int n, PyObject *consts)
6758{
6759 /* Pre-conditions */
6760 assert(PyList_CheckExact(consts));
6761 assert(inst[n].i_opcode == BUILD_TUPLE);
6762 assert(inst[n].i_oparg == n);
6763
6764 for (int i = 0; i < n; i++) {
6765 if (inst[i].i_opcode != LOAD_CONST) {
6766 return 0;
6767 }
6768 }
6769
6770 /* Buildup new tuple of constants */
6771 PyObject *newconst = PyTuple_New(n);
6772 if (newconst == NULL) {
6773 return -1;
6774 }
6775 for (int i = 0; i < n; i++) {
6776 int arg = inst[i].i_oparg;
6777 PyObject *constant = PyList_GET_ITEM(consts, arg);
6778 Py_INCREF(constant);
6779 PyTuple_SET_ITEM(newconst, i, constant);
6780 }
6781 Py_ssize_t index = PyList_GET_SIZE(consts);
Victor Stinner71f2ff42020-09-23 14:06:55 +02006782 if ((size_t)index >= (size_t)INT_MAX - 1) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006783 Py_DECREF(newconst);
6784 PyErr_SetString(PyExc_OverflowError, "too many constants");
6785 return -1;
6786 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006787 if (PyList_Append(consts, newconst)) {
6788 Py_DECREF(newconst);
6789 return -1;
6790 }
6791 Py_DECREF(newconst);
6792 for (int i = 0; i < n; i++) {
6793 inst[i].i_opcode = NOP;
6794 }
6795 inst[n].i_opcode = LOAD_CONST;
Victor Stinner71f2ff42020-09-23 14:06:55 +02006796 inst[n].i_oparg = (int)index;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006797 return 0;
6798}
6799
Mark Shannon28b75c82020-12-23 11:43:10 +00006800
6801static int
6802eliminate_jump_to_jump(basicblock *bb, int opcode) {
6803 assert (bb->b_iused > 0);
6804 struct instr *inst = &bb->b_instr[bb->b_iused-1];
6805 assert (is_jump(inst));
6806 assert (inst->i_target->b_iused > 0);
6807 struct instr *target = &inst->i_target->b_instr[0];
6808 if (inst->i_target == target->i_target) {
6809 /* Nothing to do */
6810 return 0;
6811 }
6812 int lineno = target->i_lineno;
6813 if (add_jump_to_block(bb, opcode, lineno, target->i_target) == 0) {
6814 return -1;
6815 }
6816 assert (bb->b_iused >= 2);
6817 bb->b_instr[bb->b_iused-2].i_opcode = NOP;
6818 return 0;
6819}
6820
Mark Shannoncc75ab72020-11-12 19:49:33 +00006821/* Maximum size of basic block that should be copied in optimizer */
6822#define MAX_COPY_SIZE 4
Mark Shannon6e8128f2020-07-30 10:03:00 +01006823
6824/* Optimization */
6825static int
6826optimize_basic_block(basicblock *bb, PyObject *consts)
6827{
6828 assert(PyList_CheckExact(consts));
6829 struct instr nop;
6830 nop.i_opcode = NOP;
6831 struct instr *target;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006832 for (int i = 0; i < bb->b_iused; i++) {
6833 struct instr *inst = &bb->b_instr[i];
6834 int oparg = inst->i_oparg;
6835 int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01006836 if (is_jump(inst)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006837 /* Skip over empty basic blocks. */
6838 while (inst->i_target->b_iused == 0) {
6839 inst->i_target = inst->i_target->b_next;
6840 }
6841 target = &inst->i_target->b_instr[0];
6842 }
6843 else {
6844 target = &nop;
6845 }
6846 switch (inst->i_opcode) {
Mark Shannon266b4622020-11-17 19:30:14 +00006847 /* Remove LOAD_CONST const; conditional jump */
Mark Shannon6e8128f2020-07-30 10:03:00 +01006848 case LOAD_CONST:
Mark Shannon266b4622020-11-17 19:30:14 +00006849 {
6850 PyObject* cnt;
6851 int is_true;
6852 int jump_if_true;
6853 switch(nextop) {
6854 case POP_JUMP_IF_FALSE:
6855 case POP_JUMP_IF_TRUE:
6856 cnt = PyList_GET_ITEM(consts, oparg);
6857 is_true = PyObject_IsTrue(cnt);
6858 if (is_true == -1) {
6859 goto error;
6860 }
6861 inst->i_opcode = NOP;
6862 jump_if_true = nextop == POP_JUMP_IF_TRUE;
6863 if (is_true == jump_if_true) {
6864 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
6865 bb->b_nofallthrough = 1;
6866 }
6867 else {
6868 bb->b_instr[i+1].i_opcode = NOP;
6869 }
6870 break;
6871 case JUMP_IF_FALSE_OR_POP:
6872 case JUMP_IF_TRUE_OR_POP:
6873 cnt = PyList_GET_ITEM(consts, oparg);
6874 is_true = PyObject_IsTrue(cnt);
6875 if (is_true == -1) {
6876 goto error;
6877 }
6878 jump_if_true = nextop == JUMP_IF_TRUE_OR_POP;
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 inst->i_opcode = NOP;
6885 bb->b_instr[i+1].i_opcode = NOP;
6886 }
6887 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006888 }
6889 break;
Mark Shannon266b4622020-11-17 19:30:14 +00006890 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006891
6892 /* Try to fold tuples of constants.
6893 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
6894 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
6895 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
6896 case BUILD_TUPLE:
6897 if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
6898 switch(oparg) {
6899 case 1:
6900 inst->i_opcode = NOP;
6901 bb->b_instr[i+1].i_opcode = NOP;
6902 break;
6903 case 2:
6904 inst->i_opcode = ROT_TWO;
6905 bb->b_instr[i+1].i_opcode = NOP;
6906 break;
6907 case 3:
6908 inst->i_opcode = ROT_THREE;
6909 bb->b_instr[i+1].i_opcode = ROT_TWO;
6910 }
6911 break;
6912 }
6913 if (i >= oparg) {
6914 if (fold_tuple_on_constants(inst-oparg, oparg, consts)) {
6915 goto error;
6916 }
6917 }
6918 break;
6919
6920 /* Simplify conditional jump to conditional jump where the
6921 result of the first test implies the success of a similar
6922 test or the failure of the opposite test.
6923 Arises in code like:
6924 "a and b or c"
6925 "(a and b) and c"
6926 "(a or b) or c"
6927 "(a or b) and c"
6928 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
6929 --> x:JUMP_IF_FALSE_OR_POP z
6930 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
6931 --> x:POP_JUMP_IF_FALSE y+1
6932 where y+1 is the instruction following the second test.
6933 */
6934 case JUMP_IF_FALSE_OR_POP:
6935 switch(target->i_opcode) {
6936 case POP_JUMP_IF_FALSE:
Mark Shannon28b75c82020-12-23 11:43:10 +00006937 if (inst->i_lineno == target->i_lineno) {
6938 *inst = *target;
6939 i--;
6940 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006941 break;
6942 case JUMP_ABSOLUTE:
6943 case JUMP_FORWARD:
6944 case JUMP_IF_FALSE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00006945 if (inst->i_lineno == target->i_lineno &&
6946 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00006947 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00006948 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00006949 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006950 break;
6951 case JUMP_IF_TRUE_OR_POP:
6952 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00006953 if (inst->i_lineno == target->i_lineno) {
6954 inst->i_opcode = POP_JUMP_IF_FALSE;
6955 inst->i_target = inst->i_target->b_next;
6956 --i;
6957 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006958 break;
6959 }
6960 break;
6961
6962 case JUMP_IF_TRUE_OR_POP:
6963 switch(target->i_opcode) {
6964 case POP_JUMP_IF_TRUE:
Mark Shannon28b75c82020-12-23 11:43:10 +00006965 if (inst->i_lineno == target->i_lineno) {
6966 *inst = *target;
6967 i--;
6968 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006969 break;
6970 case JUMP_ABSOLUTE:
6971 case JUMP_FORWARD:
6972 case JUMP_IF_TRUE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00006973 if (inst->i_lineno == target->i_lineno &&
6974 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00006975 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00006976 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00006977 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006978 break;
6979 case JUMP_IF_FALSE_OR_POP:
6980 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00006981 if (inst->i_lineno == target->i_lineno) {
6982 inst->i_opcode = POP_JUMP_IF_TRUE;
6983 inst->i_target = inst->i_target->b_next;
6984 --i;
6985 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006986 break;
6987 }
6988 break;
6989
6990 case POP_JUMP_IF_FALSE:
6991 switch(target->i_opcode) {
6992 case JUMP_ABSOLUTE:
6993 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00006994 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00006995 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00006996 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00006997 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006998 break;
6999 }
7000 break;
7001
7002 case POP_JUMP_IF_TRUE:
7003 switch(target->i_opcode) {
7004 case JUMP_ABSOLUTE:
7005 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007006 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00007007 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007008 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007009 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007010 break;
7011 }
7012 break;
7013
7014 case JUMP_ABSOLUTE:
7015 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007016 assert (i == bb->b_iused-1);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007017 switch(target->i_opcode) {
7018 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007019 if (eliminate_jump_to_jump(bb, inst->i_opcode)) {
7020 goto error;
Mark Shannon266b4622020-11-17 19:30:14 +00007021 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007022 break;
Mark Shannon28b75c82020-12-23 11:43:10 +00007023
Mark Shannon6e8128f2020-07-30 10:03:00 +01007024 case JUMP_ABSOLUTE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007025 if (eliminate_jump_to_jump(bb, JUMP_ABSOLUTE)) {
7026 goto error;
Mark Shannon266b4622020-11-17 19:30:14 +00007027 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007028 break;
Mark Shannon28b75c82020-12-23 11:43:10 +00007029 default:
7030 if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) {
7031 basicblock *to_copy = inst->i_target;
7032 inst->i_opcode = NOP;
7033 for (i = 0; i < to_copy->b_iused; i++) {
7034 int index = compiler_next_instr(bb);
7035 if (index < 0) {
7036 return -1;
7037 }
7038 bb->b_instr[index] = to_copy->b_instr[i];
7039 }
7040 bb->b_exit = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007041 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007042 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007043 }
7044 }
7045 return 0;
7046error:
7047 return -1;
7048}
7049
7050
7051static void
Mark Shannon1659ad12021-01-13 15:05:04 +00007052clean_basic_block(basicblock *bb, int prev_lineno) {
7053 /* Remove NOPs when legal to do so. */
Mark Shannon6e8128f2020-07-30 10:03:00 +01007054 int dest = 0;
7055 for (int src = 0; src < bb->b_iused; src++) {
Mark Shannon877df852020-11-12 09:43:29 +00007056 int lineno = bb->b_instr[src].i_lineno;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007057 if (bb->b_instr[src].i_opcode == NOP) {
Mark Shannon266b4622020-11-17 19:30:14 +00007058 /* Eliminate no-op if it doesn't have a line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007059 if (lineno < 0) {
7060 continue;
7061 }
Mark Shannon266b4622020-11-17 19:30:14 +00007062 /* or, if the previous instruction had the same line number. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007063 if (prev_lineno == lineno) {
7064 continue;
7065 }
Mark Shannon266b4622020-11-17 19:30:14 +00007066 /* or, if the next instruction has same line number or no line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007067 if (src < bb->b_iused - 1) {
7068 int next_lineno = bb->b_instr[src+1].i_lineno;
7069 if (next_lineno < 0 || next_lineno == lineno) {
7070 bb->b_instr[src+1].i_lineno = lineno;
7071 continue;
Mark Shannon877df852020-11-12 09:43:29 +00007072 }
7073 }
Mark Shannon266b4622020-11-17 19:30:14 +00007074 else {
7075 basicblock* next = bb->b_next;
7076 while (next && next->b_iused == 0) {
7077 next = next->b_next;
7078 }
7079 /* or if last instruction in BB and next BB has same line number */
7080 if (next) {
7081 if (lineno == next->b_instr[0].i_lineno) {
7082 continue;
7083 }
7084 }
7085 }
7086
Mark Shannon6e8128f2020-07-30 10:03:00 +01007087 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007088 if (dest != src) {
7089 bb->b_instr[dest] = bb->b_instr[src];
7090 }
7091 dest++;
7092 prev_lineno = lineno;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007093 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007094 assert(dest <= bb->b_iused);
7095 bb->b_iused = dest;
7096}
7097
Mark Shannon266b4622020-11-17 19:30:14 +00007098static int
7099normalize_basic_block(basicblock *bb) {
7100 /* Mark blocks as exit and/or nofallthrough.
7101 Raise SystemError if CFG is malformed. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007102 for (int i = 0; i < bb->b_iused; i++) {
7103 switch(bb->b_instr[i].i_opcode) {
7104 case RETURN_VALUE:
7105 case RAISE_VARARGS:
7106 case RERAISE:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007107 bb->b_exit = 1;
Mark Shannon5977a792020-12-02 13:31:40 +00007108 bb->b_nofallthrough = 1;
7109 break;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007110 case JUMP_ABSOLUTE:
7111 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007112 bb->b_nofallthrough = 1;
Mark Shannon266b4622020-11-17 19:30:14 +00007113 /* fall through */
7114 case POP_JUMP_IF_FALSE:
7115 case POP_JUMP_IF_TRUE:
7116 case JUMP_IF_FALSE_OR_POP:
7117 case JUMP_IF_TRUE_OR_POP:
Mark Shannon5977a792020-12-02 13:31:40 +00007118 case FOR_ITER:
Mark Shannon266b4622020-11-17 19:30:14 +00007119 if (i != bb->b_iused-1) {
7120 PyErr_SetString(PyExc_SystemError, "malformed control flow graph.");
7121 return -1;
7122 }
Mark Shannon5977a792020-12-02 13:31:40 +00007123 /* Skip over empty basic blocks. */
7124 while (bb->b_instr[i].i_target->b_iused == 0) {
7125 bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next;
7126 }
7127
Mark Shannoncc75ab72020-11-12 19:49:33 +00007128 }
7129 }
Mark Shannon266b4622020-11-17 19:30:14 +00007130 return 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007131}
7132
Mark Shannon6e8128f2020-07-30 10:03:00 +01007133static int
7134mark_reachable(struct assembler *a) {
7135 basicblock **stack, **sp;
7136 sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks);
7137 if (stack == NULL) {
7138 return -1;
7139 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007140 a->a_entry->b_predecessors = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007141 *sp++ = a->a_entry;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007142 while (sp > stack) {
7143 basicblock *b = *(--sp);
Mark Shannon3bd60352021-01-13 12:05:43 +00007144 if (b->b_next && !b->b_nofallthrough) {
7145 if (b->b_next->b_predecessors == 0) {
7146 *sp++ = b->b_next;
7147 }
7148 b->b_next->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007149 }
7150 for (int i = 0; i < b->b_iused; i++) {
7151 basicblock *target;
Mark Shannon582aaf12020-08-04 17:30:11 +01007152 if (is_jump(&b->b_instr[i])) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007153 target = b->b_instr[i].i_target;
Mark Shannon3bd60352021-01-13 12:05:43 +00007154 if (target->b_predecessors == 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007155 *sp++ = target;
7156 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007157 target->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007158 }
7159 }
7160 }
7161 PyObject_Free(stack);
7162 return 0;
7163}
7164
Mark Shannon3bd60352021-01-13 12:05:43 +00007165static void
7166eliminate_empty_basic_blocks(basicblock *entry) {
7167 /* Eliminate empty blocks */
7168 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7169 basicblock *next = b->b_next;
7170 if (next) {
7171 while (next->b_iused == 0 && next->b_next) {
7172 next = next->b_next;
7173 }
7174 b->b_next = next;
7175 }
7176 }
7177 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7178 if (b->b_iused == 0) {
7179 continue;
7180 }
7181 if (is_jump(&b->b_instr[b->b_iused-1])) {
7182 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7183 while (target->b_iused == 0) {
7184 target = target->b_next;
7185 }
7186 b->b_instr[b->b_iused-1].i_target = target;
7187 }
7188 }
7189}
7190
7191
Mark Shannon5977a792020-12-02 13:31:40 +00007192/* If an instruction has no line number, but it's predecessor in the BB does,
Mark Shannon3bd60352021-01-13 12:05:43 +00007193 * then copy the line number. If a successor block has no line number, and only
7194 * one predecessor, then inherit the line number.
7195 * This ensures that all exit blocks (with one predecessor) receive a line number.
7196 * Also reduces the size of the line number table,
Mark Shannon5977a792020-12-02 13:31:40 +00007197 * but has no impact on the generated line number events.
7198 */
7199static void
Mark Shannon3bd60352021-01-13 12:05:43 +00007200propogate_line_numbers(struct assembler *a) {
Mark Shannon5977a792020-12-02 13:31:40 +00007201 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007202 if (b->b_iused == 0) {
7203 continue;
7204 }
Mark Shannon5977a792020-12-02 13:31:40 +00007205 int prev_lineno = -1;
7206 for (int i = 0; i < b->b_iused; i++) {
7207 if (b->b_instr[i].i_lineno < 0) {
7208 b->b_instr[i].i_lineno = prev_lineno;
7209 }
7210 else {
7211 prev_lineno = b->b_instr[i].i_lineno;
7212 }
7213 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007214 if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) {
7215 assert(b->b_next->b_iused);
7216 if (b->b_next->b_instr[0].i_lineno < 0) {
7217 b->b_next->b_instr[0].i_lineno = prev_lineno;
7218 }
7219 }
7220 if (is_jump(&b->b_instr[b->b_iused-1])) {
7221 switch (b->b_instr[b->b_iused-1].i_opcode) {
7222 /* Note: Only actual jumps, not exception handlers */
7223 case SETUP_ASYNC_WITH:
7224 case SETUP_WITH:
7225 case SETUP_FINALLY:
7226 continue;
7227 }
7228 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7229 if (target->b_predecessors == 1) {
7230 if (target->b_instr[0].i_lineno < 0) {
7231 target->b_instr[0].i_lineno = prev_lineno;
7232 }
7233 }
7234 }
Mark Shannon5977a792020-12-02 13:31:40 +00007235 }
7236}
7237
7238/* Perform optimizations on a control flow graph.
Mark Shannon6e8128f2020-07-30 10:03:00 +01007239 The consts object should still be in list form to allow new constants
7240 to be appended.
7241
7242 All transformations keep the code size the same or smaller.
7243 For those that reduce size, the gaps are initially filled with
7244 NOPs. Later those NOPs are removed.
7245*/
7246
7247static int
7248optimize_cfg(struct assembler *a, PyObject *consts)
7249{
Mark Shannoncc75ab72020-11-12 19:49:33 +00007250 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannoncc75ab72020-11-12 19:49:33 +00007251 if (optimize_basic_block(b, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007252 return -1;
7253 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007254 clean_basic_block(b, -1);
Mark Shannon3bd60352021-01-13 12:05:43 +00007255 assert(b->b_predecessors == 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007256 }
7257 if (mark_reachable(a)) {
7258 return -1;
7259 }
7260 /* Delete unreachable instructions */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007261 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007262 if (b->b_predecessors == 0) {
Mark Shannoncc75ab72020-11-12 19:49:33 +00007263 b->b_iused = 0;
Om Gc71581c2020-12-16 17:48:05 +05307264 b->b_nofallthrough = 0;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007265 }
7266 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007267 basicblock *pred = NULL;
7268 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7269 int prev_lineno = -1;
7270 if (pred && pred->b_iused) {
7271 prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno;
7272 }
7273 clean_basic_block(b, prev_lineno);
7274 pred = b->b_nofallthrough ? NULL : b;
7275 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007276 eliminate_empty_basic_blocks(a->a_entry);
Om Gc71581c2020-12-16 17:48:05 +05307277 /* Delete jump instructions made redundant by previous step. If a non-empty
7278 block ends with a jump instruction, check if the next non-empty block
7279 reached through normal flow control is the target of that jump. If it
7280 is, then the jump instruction is redundant and can be deleted.
7281 */
Mark Shannon3bd60352021-01-13 12:05:43 +00007282 int maybe_empty_blocks = 0;
Om Gc71581c2020-12-16 17:48:05 +05307283 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7284 if (b->b_iused > 0) {
7285 struct instr *b_last_instr = &b->b_instr[b->b_iused - 1];
Mark Shannon802b6452021-02-02 14:59:15 +00007286 if (b_last_instr->i_opcode == JUMP_ABSOLUTE ||
Om Gc71581c2020-12-16 17:48:05 +05307287 b_last_instr->i_opcode == JUMP_FORWARD) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007288 if (b_last_instr->i_target == b->b_next) {
7289 assert(b->b_next->b_iused);
Om Gc71581c2020-12-16 17:48:05 +05307290 b->b_nofallthrough = 0;
Mark Shannon802b6452021-02-02 14:59:15 +00007291 b_last_instr->i_opcode = NOP;
7292 clean_basic_block(b, -1);
7293 maybe_empty_blocks = 1;
Om Gc71581c2020-12-16 17:48:05 +05307294 }
7295 }
7296 }
7297 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007298 if (maybe_empty_blocks) {
7299 eliminate_empty_basic_blocks(a->a_entry);
7300 }
7301 propogate_line_numbers(a);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007302 return 0;
7303}
7304
Mark Shannon5977a792020-12-02 13:31:40 +00007305static inline int
7306is_exit_without_lineno(basicblock *b) {
7307 return b->b_exit && b->b_instr[0].i_lineno < 0;
7308}
7309
7310/* PEP 626 mandates that the f_lineno of a frame is correct
7311 * after a frame terminates. It would be prohibitively expensive
7312 * to continuously update the f_lineno field at runtime,
7313 * so we make sure that all exiting instruction (raises and returns)
7314 * have a valid line number, allowing us to compute f_lineno lazily.
7315 * We can do this by duplicating the exit blocks without line number
7316 * so that none have more than one predecessor. We can then safely
7317 * copy the line number from the sole predecessor block.
7318 */
7319static int
7320ensure_exits_have_lineno(struct compiler *c)
7321{
Mark Shannoneaccc122020-12-04 15:22:12 +00007322 basicblock *entry = NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00007323 /* Copy all exit blocks without line number that are targets of a jump.
7324 */
7325 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7326 if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) {
7327 switch (b->b_instr[b->b_iused-1].i_opcode) {
7328 /* Note: Only actual jumps, not exception handlers */
7329 case SETUP_ASYNC_WITH:
7330 case SETUP_WITH:
7331 case SETUP_FINALLY:
7332 continue;
7333 }
7334 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7335 if (is_exit_without_lineno(target)) {
7336 basicblock *new_target = compiler_copy_block(c, target);
7337 if (new_target == NULL) {
7338 return -1;
7339 }
7340 new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7341 b->b_instr[b->b_iused-1].i_target = new_target;
7342 }
7343 }
Mark Shannoneaccc122020-12-04 15:22:12 +00007344 entry = b;
7345 }
7346 assert(entry != NULL);
7347 if (is_exit_without_lineno(entry)) {
7348 entry->b_instr[0].i_lineno = c->u->u_firstlineno;
Mark Shannon5977a792020-12-02 13:31:40 +00007349 }
Mark Shannonee9f98d2021-01-05 12:04:10 +00007350 /* Eliminate empty blocks */
7351 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7352 while (b->b_next && b->b_next->b_iused == 0) {
7353 b->b_next = b->b_next->b_next;
7354 }
7355 }
Mark Shannon5977a792020-12-02 13:31:40 +00007356 /* Any remaining reachable exit blocks without line number can only be reached by
7357 * fall through, and thus can only have a single predecessor */
7358 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7359 if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) {
7360 if (is_exit_without_lineno(b->b_next)) {
7361 assert(b->b_next->b_iused > 0);
7362 b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7363 }
7364 }
7365 }
7366 return 0;
7367}
7368
7369
Mark Shannon6e8128f2020-07-30 10:03:00 +01007370/* Retained for API compatibility.
7371 * Optimization is now done in optimize_cfg */
7372
7373PyObject *
7374PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
7375 PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
7376{
7377 Py_INCREF(code);
7378 return code;
7379}