blob: ea1bf6bf923afdcdddb4f3c7bb022bbb639fb572 [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);
Mark Shannonc5440932021-03-15 14:24:25 +00003400 /* Mark POP_TOP as artificial */
3401 c->u->u_lineno = -1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003402 ADDOP(c, POP_TOP);
3403 return 1;
3404}
3405
3406static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003407compiler_visit_stmt(struct compiler *c, stmt_ty s)
3408{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003409 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003411 /* Always assign a lineno to the next instruction for a stmt. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003412 SET_LOC(c, s);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003414 switch (s->kind) {
3415 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04003416 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003417 case ClassDef_kind:
3418 return compiler_class(c, s);
3419 case Return_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003420 return compiler_return(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003421 case Delete_kind:
3422 VISIT_SEQ(c, expr, s->v.Delete.targets)
3423 break;
3424 case Assign_kind:
3425 n = asdl_seq_LEN(s->v.Assign.targets);
3426 VISIT(c, expr, s->v.Assign.value);
3427 for (i = 0; i < n; i++) {
3428 if (i < n - 1)
3429 ADDOP(c, DUP_TOP);
3430 VISIT(c, expr,
3431 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3432 }
3433 break;
3434 case AugAssign_kind:
3435 return compiler_augassign(c, s);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003436 case AnnAssign_kind:
3437 return compiler_annassign(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 case For_kind:
3439 return compiler_for(c, s);
3440 case While_kind:
3441 return compiler_while(c, s);
3442 case If_kind:
3443 return compiler_if(c, s);
Brandt Bucher145bf262021-02-26 14:51:55 -08003444 case Match_kind:
3445 return compiler_match(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003446 case Raise_kind:
3447 n = 0;
3448 if (s->v.Raise.exc) {
3449 VISIT(c, expr, s->v.Raise.exc);
3450 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003451 if (s->v.Raise.cause) {
3452 VISIT(c, expr, s->v.Raise.cause);
3453 n++;
3454 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003455 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01003456 ADDOP_I(c, RAISE_VARARGS, (int)n);
Mark Shannon266b4622020-11-17 19:30:14 +00003457 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003458 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003459 case Try_kind:
3460 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003461 case Assert_kind:
3462 return compiler_assert(c, s);
3463 case Import_kind:
3464 return compiler_import(c, s);
3465 case ImportFrom_kind:
3466 return compiler_from_import(c, s);
3467 case Global_kind:
3468 case Nonlocal_kind:
3469 break;
3470 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003471 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003472 case Pass_kind:
Mark Shannon877df852020-11-12 09:43:29 +00003473 ADDOP(c, NOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 break;
3475 case Break_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003476 return compiler_break(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 case Continue_kind:
3478 return compiler_continue(c);
3479 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003480 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04003481 case AsyncFunctionDef_kind:
3482 return compiler_function(c, s, 1);
3483 case AsyncWith_kind:
3484 return compiler_async_with(c, s, 0);
3485 case AsyncFor_kind:
3486 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003487 }
Yury Selivanov75445082015-05-11 22:57:16 -04003488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003490}
3491
3492static int
3493unaryop(unaryop_ty op)
3494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003495 switch (op) {
3496 case Invert:
3497 return UNARY_INVERT;
3498 case Not:
3499 return UNARY_NOT;
3500 case UAdd:
3501 return UNARY_POSITIVE;
3502 case USub:
3503 return UNARY_NEGATIVE;
3504 default:
3505 PyErr_Format(PyExc_SystemError,
3506 "unary op %d should not be possible", op);
3507 return 0;
3508 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003509}
3510
3511static int
Andy Lester76d58772020-03-10 21:18:12 -05003512binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003514 switch (op) {
3515 case Add:
3516 return BINARY_ADD;
3517 case Sub:
3518 return BINARY_SUBTRACT;
3519 case Mult:
3520 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003521 case MatMult:
3522 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003523 case Div:
3524 return BINARY_TRUE_DIVIDE;
3525 case Mod:
3526 return BINARY_MODULO;
3527 case Pow:
3528 return BINARY_POWER;
3529 case LShift:
3530 return BINARY_LSHIFT;
3531 case RShift:
3532 return BINARY_RSHIFT;
3533 case BitOr:
3534 return BINARY_OR;
3535 case BitXor:
3536 return BINARY_XOR;
3537 case BitAnd:
3538 return BINARY_AND;
3539 case FloorDiv:
3540 return BINARY_FLOOR_DIVIDE;
3541 default:
3542 PyErr_Format(PyExc_SystemError,
3543 "binary op %d should not be possible", op);
3544 return 0;
3545 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003546}
3547
3548static int
Andy Lester76d58772020-03-10 21:18:12 -05003549inplace_binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003551 switch (op) {
3552 case Add:
3553 return INPLACE_ADD;
3554 case Sub:
3555 return INPLACE_SUBTRACT;
3556 case Mult:
3557 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003558 case MatMult:
3559 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003560 case Div:
3561 return INPLACE_TRUE_DIVIDE;
3562 case Mod:
3563 return INPLACE_MODULO;
3564 case Pow:
3565 return INPLACE_POWER;
3566 case LShift:
3567 return INPLACE_LSHIFT;
3568 case RShift:
3569 return INPLACE_RSHIFT;
3570 case BitOr:
3571 return INPLACE_OR;
3572 case BitXor:
3573 return INPLACE_XOR;
3574 case BitAnd:
3575 return INPLACE_AND;
3576 case FloorDiv:
3577 return INPLACE_FLOOR_DIVIDE;
3578 default:
3579 PyErr_Format(PyExc_SystemError,
3580 "inplace binary op %d should not be possible", op);
3581 return 0;
3582 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003583}
3584
3585static int
3586compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3587{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003588 int op, scope;
3589 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003590 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003592 PyObject *dict = c->u->u_names;
3593 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003594
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003595 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3596 !_PyUnicode_EqualToASCIIString(name, "True") &&
3597 !_PyUnicode_EqualToASCIIString(name, "False"));
Benjamin Peterson70b224d2012-12-06 17:49:58 -05003598
Pablo Galindoc5fc1562020-04-22 23:29:27 +01003599 if (forbidden_name(c, name, ctx))
3600 return 0;
3601
Serhiy Storchakabd6ec4d2017-12-18 14:29:12 +02003602 mangled = _Py_Mangle(c->u->u_private, name);
3603 if (!mangled)
3604 return 0;
3605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003606 op = 0;
3607 optype = OP_NAME;
3608 scope = PyST_GetScope(c->u->u_ste, mangled);
3609 switch (scope) {
3610 case FREE:
3611 dict = c->u->u_freevars;
3612 optype = OP_DEREF;
3613 break;
3614 case CELL:
3615 dict = c->u->u_cellvars;
3616 optype = OP_DEREF;
3617 break;
3618 case LOCAL:
3619 if (c->u->u_ste->ste_type == FunctionBlock)
3620 optype = OP_FAST;
3621 break;
3622 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04003623 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003624 optype = OP_GLOBAL;
3625 break;
3626 case GLOBAL_EXPLICIT:
3627 optype = OP_GLOBAL;
3628 break;
3629 default:
3630 /* scope can be 0 */
3631 break;
3632 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003634 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003635 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003637 switch (optype) {
3638 case OP_DEREF:
3639 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003640 case Load:
3641 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3642 break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003643 case Store: op = STORE_DEREF; break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003644 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003645 }
3646 break;
3647 case OP_FAST:
3648 switch (ctx) {
3649 case Load: op = LOAD_FAST; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003650 case Store: op = STORE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003651 case Del: op = DELETE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003652 }
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003653 ADDOP_N(c, op, mangled, varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003654 return 1;
3655 case OP_GLOBAL:
3656 switch (ctx) {
3657 case Load: op = LOAD_GLOBAL; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003658 case Store: op = STORE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003659 case Del: op = DELETE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003660 }
3661 break;
3662 case OP_NAME:
3663 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00003664 case Load: op = LOAD_NAME; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003665 case Store: op = STORE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003666 case Del: op = DELETE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003667 }
3668 break;
3669 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003671 assert(op);
Andy Lester76d58772020-03-10 21:18:12 -05003672 arg = compiler_add_o(dict, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003673 Py_DECREF(mangled);
3674 if (arg < 0)
3675 return 0;
3676 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003677}
3678
3679static int
3680compiler_boolop(struct compiler *c, expr_ty e)
3681{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003682 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003683 int jumpi;
3684 Py_ssize_t i, n;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003685 asdl_expr_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003687 assert(e->kind == BoolOp_kind);
3688 if (e->v.BoolOp.op == And)
3689 jumpi = JUMP_IF_FALSE_OR_POP;
3690 else
3691 jumpi = JUMP_IF_TRUE_OR_POP;
3692 end = compiler_new_block(c);
3693 if (end == NULL)
3694 return 0;
3695 s = e->v.BoolOp.values;
3696 n = asdl_seq_LEN(s) - 1;
3697 assert(n >= 0);
3698 for (i = 0; i < n; ++i) {
3699 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003700 ADDOP_JUMP(c, jumpi, end);
Mark Shannon6e8128f2020-07-30 10:03:00 +01003701 basicblock *next = compiler_new_block(c);
3702 if (next == NULL) {
3703 return 0;
3704 }
3705 compiler_use_next_block(c, next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003706 }
3707 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3708 compiler_use_next_block(c, end);
3709 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003710}
3711
3712static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003713starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
Mark Shannon13bc1392020-01-23 09:25:17 +00003714 int build, int add, int extend, int tuple)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003715{
3716 Py_ssize_t n = asdl_seq_LEN(elts);
Mark Shannon13bc1392020-01-23 09:25:17 +00003717 Py_ssize_t i, seen_star = 0;
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003718 if (n > 2 && are_all_items_const(elts, 0, n)) {
3719 PyObject *folded = PyTuple_New(n);
3720 if (folded == NULL) {
3721 return 0;
3722 }
3723 PyObject *val;
3724 for (i = 0; i < n; i++) {
3725 val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
3726 Py_INCREF(val);
3727 PyTuple_SET_ITEM(folded, i, val);
3728 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003729 if (tuple) {
3730 ADDOP_LOAD_CONST_NEW(c, folded);
3731 } else {
3732 if (add == SET_ADD) {
3733 Py_SETREF(folded, PyFrozenSet_New(folded));
3734 if (folded == NULL) {
3735 return 0;
3736 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003737 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003738 ADDOP_I(c, build, pushed);
3739 ADDOP_LOAD_CONST_NEW(c, folded);
3740 ADDOP_I(c, extend, 1);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003741 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003742 return 1;
3743 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003744
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003745 for (i = 0; i < n; i++) {
3746 expr_ty elt = asdl_seq_GET(elts, i);
3747 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003748 seen_star = 1;
3749 }
3750 }
3751 if (seen_star) {
3752 seen_star = 0;
3753 for (i = 0; i < n; i++) {
3754 expr_ty elt = asdl_seq_GET(elts, i);
3755 if (elt->kind == Starred_kind) {
3756 if (seen_star == 0) {
3757 ADDOP_I(c, build, i+pushed);
3758 seen_star = 1;
3759 }
3760 VISIT(c, expr, elt->v.Starred.value);
3761 ADDOP_I(c, extend, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003762 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003763 else {
3764 VISIT(c, expr, elt);
3765 if (seen_star) {
3766 ADDOP_I(c, add, 1);
3767 }
3768 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003769 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003770 assert(seen_star);
3771 if (tuple) {
3772 ADDOP(c, LIST_TO_TUPLE);
3773 }
3774 }
3775 else {
3776 for (i = 0; i < n; i++) {
3777 expr_ty elt = asdl_seq_GET(elts, i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003778 VISIT(c, expr, elt);
Mark Shannon13bc1392020-01-23 09:25:17 +00003779 }
3780 if (tuple) {
3781 ADDOP_I(c, BUILD_TUPLE, n+pushed);
3782 } else {
3783 ADDOP_I(c, build, n+pushed);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003784 }
3785 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003786 return 1;
3787}
3788
3789static int
Brandt Bucher145bf262021-02-26 14:51:55 -08003790unpack_helper(struct compiler *c, asdl_expr_seq *elts)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003791{
3792 Py_ssize_t n = asdl_seq_LEN(elts);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003793 int seen_star = 0;
Brandt Bucher145bf262021-02-26 14:51:55 -08003794 for (Py_ssize_t i = 0; i < n; i++) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003795 expr_ty elt = asdl_seq_GET(elts, i);
3796 if (elt->kind == Starred_kind && !seen_star) {
3797 if ((i >= (1 << 8)) ||
3798 (n-i-1 >= (INT_MAX >> 8)))
3799 return compiler_error(c,
3800 "too many expressions in "
3801 "star-unpacking assignment");
3802 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3803 seen_star = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003804 }
3805 else if (elt->kind == Starred_kind) {
3806 return compiler_error(c,
Furkan Öndercb6534e2020-03-26 04:54:31 +03003807 "multiple starred expressions in assignment");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003808 }
3809 }
3810 if (!seen_star) {
3811 ADDOP_I(c, UNPACK_SEQUENCE, n);
3812 }
Brandt Bucher145bf262021-02-26 14:51:55 -08003813 return 1;
3814}
3815
3816static int
3817assignment_helper(struct compiler *c, asdl_expr_seq *elts)
3818{
3819 Py_ssize_t n = asdl_seq_LEN(elts);
3820 RETURN_IF_FALSE(unpack_helper(c, elts));
3821 for (Py_ssize_t i = 0; i < n; i++) {
Brandt Bucherd5aa2e92020-03-07 19:44:18 -08003822 expr_ty elt = asdl_seq_GET(elts, i);
3823 VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value);
3824 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003825 return 1;
3826}
3827
3828static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003829compiler_list(struct compiler *c, expr_ty e)
3830{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003831 asdl_expr_seq *elts = e->v.List.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003832 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003833 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003834 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003835 else if (e->v.List.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003836 return starunpack_helper(c, elts, 0, BUILD_LIST,
3837 LIST_APPEND, LIST_EXTEND, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003838 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003839 else
3840 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003841 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003842}
3843
3844static int
3845compiler_tuple(struct compiler *c, expr_ty e)
3846{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003847 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003848 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003849 return assignment_helper(c, elts);
3850 }
3851 else if (e->v.Tuple.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003852 return starunpack_helper(c, elts, 0, BUILD_LIST,
3853 LIST_APPEND, LIST_EXTEND, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003854 }
3855 else
3856 VISIT_SEQ(c, expr, elts);
3857 return 1;
3858}
3859
3860static int
3861compiler_set(struct compiler *c, expr_ty e)
3862{
Mark Shannon13bc1392020-01-23 09:25:17 +00003863 return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET,
3864 SET_ADD, SET_UPDATE, 0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003865}
3866
3867static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003868are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003869{
3870 Py_ssize_t i;
3871 for (i = begin; i < end; i++) {
3872 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003873 if (key == NULL || key->kind != Constant_kind)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003874 return 0;
3875 }
3876 return 1;
3877}
3878
3879static int
3880compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3881{
3882 Py_ssize_t i, n = end - begin;
3883 PyObject *keys, *key;
3884 if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) {
3885 for (i = begin; i < end; i++) {
3886 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3887 }
3888 keys = PyTuple_New(n);
3889 if (keys == NULL) {
3890 return 0;
3891 }
3892 for (i = begin; i < end; i++) {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003893 key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003894 Py_INCREF(key);
3895 PyTuple_SET_ITEM(keys, i - begin, key);
3896 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003897 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003898 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3899 }
3900 else {
3901 for (i = begin; i < end; i++) {
3902 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3903 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3904 }
3905 ADDOP_I(c, BUILD_MAP, n);
3906 }
3907 return 1;
3908}
3909
3910static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003911compiler_dict(struct compiler *c, expr_ty e)
3912{
Victor Stinner976bb402016-03-23 11:36:19 +01003913 Py_ssize_t i, n, elements;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003914 int have_dict;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003915 int is_unpacking = 0;
3916 n = asdl_seq_LEN(e->v.Dict.values);
Mark Shannon8a4cd702020-01-27 09:57:45 +00003917 have_dict = 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003918 elements = 0;
3919 for (i = 0; i < n; i++) {
3920 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003921 if (is_unpacking) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003922 if (elements) {
3923 if (!compiler_subdict(c, e, i - elements, i)) {
3924 return 0;
3925 }
3926 if (have_dict) {
3927 ADDOP_I(c, DICT_UPDATE, 1);
3928 }
3929 have_dict = 1;
3930 elements = 0;
3931 }
3932 if (have_dict == 0) {
3933 ADDOP_I(c, BUILD_MAP, 0);
3934 have_dict = 1;
3935 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003936 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Mark Shannon8a4cd702020-01-27 09:57:45 +00003937 ADDOP_I(c, DICT_UPDATE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003938 }
3939 else {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003940 if (elements == 0xFFFF) {
Pablo Galindoc51db0e2020-08-13 09:48:41 +01003941 if (!compiler_subdict(c, e, i - elements, i + 1)) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003942 return 0;
3943 }
3944 if (have_dict) {
3945 ADDOP_I(c, DICT_UPDATE, 1);
3946 }
3947 have_dict = 1;
3948 elements = 0;
3949 }
3950 else {
3951 elements++;
3952 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003953 }
3954 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003955 if (elements) {
3956 if (!compiler_subdict(c, e, n - elements, n)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003957 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003958 }
3959 if (have_dict) {
3960 ADDOP_I(c, DICT_UPDATE, 1);
3961 }
3962 have_dict = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003963 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003964 if (!have_dict) {
3965 ADDOP_I(c, BUILD_MAP, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003966 }
3967 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003968}
3969
3970static int
3971compiler_compare(struct compiler *c, expr_ty e)
3972{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003973 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003974
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02003975 if (!check_compare(c, e)) {
3976 return 0;
3977 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003978 VISIT(c, expr, e->v.Compare.left);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003979 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
3980 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
3981 if (n == 0) {
3982 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Mark Shannon9af0e472020-01-14 10:12:45 +00003983 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003984 }
3985 else {
3986 basicblock *cleanup = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003987 if (cleanup == NULL)
3988 return 0;
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003989 for (i = 0; i < n; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003990 VISIT(c, expr,
3991 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003992 ADDOP(c, DUP_TOP);
3993 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00003994 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003995 ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003996 NEXT_BLOCK(c);
3997 }
3998 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00003999 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004000 basicblock *end = compiler_new_block(c);
4001 if (end == NULL)
4002 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00004003 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004004 compiler_use_next_block(c, cleanup);
4005 ADDOP(c, ROT_TWO);
4006 ADDOP(c, POP_TOP);
4007 compiler_use_next_block(c, end);
4008 }
4009 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004010}
4011
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004012static PyTypeObject *
4013infer_type(expr_ty e)
4014{
4015 switch (e->kind) {
4016 case Tuple_kind:
4017 return &PyTuple_Type;
4018 case List_kind:
4019 case ListComp_kind:
4020 return &PyList_Type;
4021 case Dict_kind:
4022 case DictComp_kind:
4023 return &PyDict_Type;
4024 case Set_kind:
4025 case SetComp_kind:
4026 return &PySet_Type;
4027 case GeneratorExp_kind:
4028 return &PyGen_Type;
4029 case Lambda_kind:
4030 return &PyFunction_Type;
4031 case JoinedStr_kind:
4032 case FormattedValue_kind:
4033 return &PyUnicode_Type;
4034 case Constant_kind:
Victor Stinnera102ed72020-02-07 02:24:48 +01004035 return Py_TYPE(e->v.Constant.value);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004036 default:
4037 return NULL;
4038 }
4039}
4040
4041static int
4042check_caller(struct compiler *c, expr_ty e)
4043{
4044 switch (e->kind) {
4045 case Constant_kind:
4046 case Tuple_kind:
4047 case List_kind:
4048 case ListComp_kind:
4049 case Dict_kind:
4050 case DictComp_kind:
4051 case Set_kind:
4052 case SetComp_kind:
4053 case GeneratorExp_kind:
4054 case JoinedStr_kind:
4055 case FormattedValue_kind:
4056 return compiler_warn(c, "'%.200s' object is not callable; "
4057 "perhaps you missed a comma?",
4058 infer_type(e)->tp_name);
4059 default:
4060 return 1;
4061 }
4062}
4063
4064static int
4065check_subscripter(struct compiler *c, expr_ty e)
4066{
4067 PyObject *v;
4068
4069 switch (e->kind) {
4070 case Constant_kind:
4071 v = e->v.Constant.value;
4072 if (!(v == Py_None || v == Py_Ellipsis ||
4073 PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
4074 PyAnySet_Check(v)))
4075 {
4076 return 1;
4077 }
4078 /* fall through */
4079 case Set_kind:
4080 case SetComp_kind:
4081 case GeneratorExp_kind:
4082 case Lambda_kind:
4083 return compiler_warn(c, "'%.200s' object is not subscriptable; "
4084 "perhaps you missed a comma?",
4085 infer_type(e)->tp_name);
4086 default:
4087 return 1;
4088 }
4089}
4090
4091static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004092check_index(struct compiler *c, expr_ty e, expr_ty s)
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004093{
4094 PyObject *v;
4095
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004096 PyTypeObject *index_type = infer_type(s);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004097 if (index_type == NULL
4098 || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
4099 || index_type == &PySlice_Type) {
4100 return 1;
4101 }
4102
4103 switch (e->kind) {
4104 case Constant_kind:
4105 v = e->v.Constant.value;
4106 if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
4107 return 1;
4108 }
4109 /* fall through */
4110 case Tuple_kind:
4111 case List_kind:
4112 case ListComp_kind:
4113 case JoinedStr_kind:
4114 case FormattedValue_kind:
4115 return compiler_warn(c, "%.200s indices must be integers or slices, "
4116 "not %.200s; "
4117 "perhaps you missed a comma?",
4118 infer_type(e)->tp_name,
4119 index_type->tp_name);
4120 default:
4121 return 1;
4122 }
4123}
4124
Zackery Spytz97f5de02019-03-22 01:30:32 -06004125// Return 1 if the method call was optimized, -1 if not, and 0 on error.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004126static int
Yury Selivanovf2392132016-12-13 19:03:51 -05004127maybe_optimize_method_call(struct compiler *c, expr_ty e)
4128{
4129 Py_ssize_t argsl, i;
4130 expr_ty meth = e->v.Call.func;
Pablo Galindoa5634c42020-09-16 19:42:00 +01004131 asdl_expr_seq *args = e->v.Call.args;
Yury Selivanovf2392132016-12-13 19:03:51 -05004132
4133 /* Check that the call node is an attribute access, and that
4134 the call doesn't have keyword parameters. */
4135 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
4136 asdl_seq_LEN(e->v.Call.keywords))
4137 return -1;
4138
4139 /* Check that there are no *varargs types of arguments. */
4140 argsl = asdl_seq_LEN(args);
4141 for (i = 0; i < argsl; i++) {
4142 expr_ty elt = asdl_seq_GET(args, i);
4143 if (elt->kind == Starred_kind) {
4144 return -1;
4145 }
4146 }
4147
4148 /* Alright, we can optimize the code. */
4149 VISIT(c, expr, meth->v.Attribute.value);
Mark Shannond48848c2021-03-14 18:01:30 +00004150 int old_lineno = c->u->u_lineno;
4151 c->u->u_lineno = meth->end_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004152 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
4153 VISIT_SEQ(c, expr, e->v.Call.args);
4154 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
Mark Shannond48848c2021-03-14 18:01:30 +00004155 c->u->u_lineno = old_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004156 return 1;
4157}
4158
4159static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004160validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
Zackery Spytz08050e92020-04-06 00:47:47 -06004161{
4162 Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
4163 for (Py_ssize_t i = 0; i < nkeywords; i++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004164 keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
4165 if (key->arg == NULL) {
4166 continue;
4167 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +01004168 if (forbidden_name(c, key->arg, Store)) {
4169 return -1;
4170 }
Zackery Spytz08050e92020-04-06 00:47:47 -06004171 for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004172 keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
4173 if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004174 c->u->u_col_offset = other->col_offset;
Brandt Bucher145bf262021-02-26 14:51:55 -08004175 compiler_error(c, "keyword argument repeated: %U", key->arg);
Pablo Galindo254ec782020-04-03 20:37:13 +01004176 return -1;
4177 }
4178 }
4179 }
4180 return 0;
4181}
4182
4183static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004184compiler_call(struct compiler *c, expr_ty e)
4185{
Zackery Spytz97f5de02019-03-22 01:30:32 -06004186 int ret = maybe_optimize_method_call(c, e);
4187 if (ret >= 0) {
4188 return ret;
4189 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004190 if (!check_caller(c, e->v.Call.func)) {
4191 return 0;
4192 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004193 VISIT(c, expr, e->v.Call.func);
4194 return compiler_call_helper(c, 0,
4195 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004196 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004197}
4198
Eric V. Smith235a6f02015-09-19 14:51:32 -04004199static int
4200compiler_joined_str(struct compiler *c, expr_ty e)
4201{
Eric V. Smith235a6f02015-09-19 14:51:32 -04004202 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004203 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1)
4204 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
Eric V. Smith235a6f02015-09-19 14:51:32 -04004205 return 1;
4206}
4207
Eric V. Smitha78c7952015-11-03 12:45:05 -05004208/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004209static int
4210compiler_formatted_value(struct compiler *c, expr_ty e)
4211{
Eric V. Smitha78c7952015-11-03 12:45:05 -05004212 /* Our oparg encodes 2 pieces of information: the conversion
4213 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04004214
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004215 Convert the conversion char to 3 bits:
4216 : 000 0x0 FVC_NONE The default if nothing specified.
Eric V. Smitha78c7952015-11-03 12:45:05 -05004217 !s : 001 0x1 FVC_STR
4218 !r : 010 0x2 FVC_REPR
4219 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04004220
Eric V. Smitha78c7952015-11-03 12:45:05 -05004221 next bit is whether or not we have a format spec:
4222 yes : 100 0x4
4223 no : 000 0x0
4224 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004225
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004226 int conversion = e->v.FormattedValue.conversion;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004227 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004228
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004229 /* The expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004230 VISIT(c, expr, e->v.FormattedValue.value);
4231
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004232 switch (conversion) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004233 case 's': oparg = FVC_STR; break;
4234 case 'r': oparg = FVC_REPR; break;
4235 case 'a': oparg = FVC_ASCII; break;
4236 case -1: oparg = FVC_NONE; break;
4237 default:
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004238 PyErr_Format(PyExc_SystemError,
4239 "Unrecognized conversion character %d", conversion);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004240 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004241 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004242 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004243 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004244 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004245 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004246 }
4247
Eric V. Smitha78c7952015-11-03 12:45:05 -05004248 /* And push our opcode and oparg */
4249 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004250
Eric V. Smith235a6f02015-09-19 14:51:32 -04004251 return 1;
4252}
4253
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004254static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004255compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004256{
4257 Py_ssize_t i, n = end - begin;
4258 keyword_ty kw;
4259 PyObject *keys, *key;
4260 assert(n > 0);
4261 if (n > 1) {
4262 for (i = begin; i < end; i++) {
4263 kw = asdl_seq_GET(keywords, i);
4264 VISIT(c, expr, kw->value);
4265 }
4266 keys = PyTuple_New(n);
4267 if (keys == NULL) {
4268 return 0;
4269 }
4270 for (i = begin; i < end; i++) {
4271 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
4272 Py_INCREF(key);
4273 PyTuple_SET_ITEM(keys, i - begin, key);
4274 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004275 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004276 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
4277 }
4278 else {
4279 /* a for loop only executes once */
4280 for (i = begin; i < end; i++) {
4281 kw = asdl_seq_GET(keywords, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004282 ADDOP_LOAD_CONST(c, kw->arg);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004283 VISIT(c, expr, kw->value);
4284 }
4285 ADDOP_I(c, BUILD_MAP, n);
4286 }
4287 return 1;
4288}
4289
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004290/* shared code between compiler_call and compiler_class */
4291static int
4292compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01004293 int n, /* Args already pushed */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004294 asdl_expr_seq *args,
4295 asdl_keyword_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004296{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004297 Py_ssize_t i, nseen, nelts, nkwelts;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004298
Pablo Galindo254ec782020-04-03 20:37:13 +01004299 if (validate_keywords(c, keywords) == -1) {
4300 return 0;
4301 }
4302
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004303 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004304 nkwelts = asdl_seq_LEN(keywords);
4305
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004306 for (i = 0; i < nelts; i++) {
4307 expr_ty elt = asdl_seq_GET(args, i);
4308 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00004309 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004310 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004311 }
4312 for (i = 0; i < nkwelts; i++) {
4313 keyword_ty kw = asdl_seq_GET(keywords, i);
4314 if (kw->arg == NULL) {
4315 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004316 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004317 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004318
Mark Shannon13bc1392020-01-23 09:25:17 +00004319 /* No * or ** args, so can use faster calling sequence */
4320 for (i = 0; i < nelts; i++) {
4321 expr_ty elt = asdl_seq_GET(args, i);
4322 assert(elt->kind != Starred_kind);
4323 VISIT(c, expr, elt);
4324 }
4325 if (nkwelts) {
4326 PyObject *names;
4327 VISIT_SEQ(c, keyword, keywords);
4328 names = PyTuple_New(nkwelts);
4329 if (names == NULL) {
4330 return 0;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004331 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004332 for (i = 0; i < nkwelts; i++) {
4333 keyword_ty kw = asdl_seq_GET(keywords, i);
4334 Py_INCREF(kw->arg);
4335 PyTuple_SET_ITEM(names, i, kw->arg);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004336 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004337 ADDOP_LOAD_CONST_NEW(c, names);
4338 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
4339 return 1;
4340 }
4341 else {
4342 ADDOP_I(c, CALL_FUNCTION, n + nelts);
4343 return 1;
4344 }
4345
4346ex_call:
4347
4348 /* Do positional arguments. */
4349 if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
4350 VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
4351 }
4352 else if (starunpack_helper(c, args, n, BUILD_LIST,
4353 LIST_APPEND, LIST_EXTEND, 1) == 0) {
4354 return 0;
4355 }
4356 /* Then keyword arguments */
4357 if (nkwelts) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004358 /* Has a new dict been pushed */
4359 int have_dict = 0;
Mark Shannon13bc1392020-01-23 09:25:17 +00004360
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004361 nseen = 0; /* the number of keyword arguments on the stack following */
4362 for (i = 0; i < nkwelts; i++) {
4363 keyword_ty kw = asdl_seq_GET(keywords, i);
4364 if (kw->arg == NULL) {
4365 /* A keyword argument unpacking. */
4366 if (nseen) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004367 if (!compiler_subkwargs(c, keywords, i - nseen, i)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004368 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004369 }
Mark Shannondb64f122020-06-01 10:42:42 +01004370 if (have_dict) {
4371 ADDOP_I(c, DICT_MERGE, 1);
4372 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004373 have_dict = 1;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004374 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004375 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004376 if (!have_dict) {
4377 ADDOP_I(c, BUILD_MAP, 0);
4378 have_dict = 1;
4379 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004380 VISIT(c, expr, kw->value);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004381 ADDOP_I(c, DICT_MERGE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004382 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004383 else {
4384 nseen++;
4385 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004386 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004387 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004388 /* Pack up any trailing keyword arguments. */
Mark Shannon8a4cd702020-01-27 09:57:45 +00004389 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004390 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004391 }
4392 if (have_dict) {
4393 ADDOP_I(c, DICT_MERGE, 1);
4394 }
4395 have_dict = 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004396 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004397 assert(have_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004398 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004399 ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0);
4400 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004401}
4402
Nick Coghlan650f0d02007-04-15 12:05:43 +00004403
4404/* List and set comprehensions and generator expressions work by creating a
4405 nested function to perform the actual iteration. This means that the
4406 iteration variables don't leak into the current scope.
4407 The defined function is called immediately following its definition, with the
4408 result of that call being the result of the expression.
4409 The LC/SC version returns the populated container, while the GE version is
4410 flagged in symtable.c as a generator, so it returns the generator object
4411 when the function is called.
Nick Coghlan650f0d02007-04-15 12:05:43 +00004412
4413 Possible cleanups:
4414 - iterate over the generator sequence instead of using recursion
4415*/
4416
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004417
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004418static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004419compiler_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004420 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004421 int depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004422 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004423{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004424 comprehension_ty gen;
4425 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4426 if (gen->is_async) {
4427 return compiler_async_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004428 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004429 } else {
4430 return compiler_sync_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004431 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004432 }
4433}
4434
4435static int
4436compiler_sync_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004437 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004438 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004439 expr_ty elt, expr_ty val, int type)
4440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004441 /* generate code for the iterator, then each of the ifs,
4442 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004444 comprehension_ty gen;
4445 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004446 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004448 start = compiler_new_block(c);
4449 skip = compiler_new_block(c);
4450 if_cleanup = compiler_new_block(c);
4451 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004453 if (start == NULL || skip == NULL || if_cleanup == NULL ||
4454 anchor == NULL)
4455 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004459 if (gen_index == 0) {
4460 /* Receive outermost iter as an implicit argument */
4461 c->u->u_argcount = 1;
4462 ADDOP_I(c, LOAD_FAST, 0);
4463 }
4464 else {
4465 /* Sub-iter - calculate on the fly */
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004466 /* Fast path for the temporary variable assignment idiom:
4467 for y in [f(x)]
4468 */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004469 asdl_expr_seq *elts;
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004470 switch (gen->iter->kind) {
4471 case List_kind:
4472 elts = gen->iter->v.List.elts;
4473 break;
4474 case Tuple_kind:
4475 elts = gen->iter->v.Tuple.elts;
4476 break;
4477 default:
4478 elts = NULL;
4479 }
4480 if (asdl_seq_LEN(elts) == 1) {
4481 expr_ty elt = asdl_seq_GET(elts, 0);
4482 if (elt->kind != Starred_kind) {
4483 VISIT(c, expr, elt);
4484 start = NULL;
4485 }
4486 }
4487 if (start) {
4488 VISIT(c, expr, gen->iter);
4489 ADDOP(c, GET_ITER);
4490 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004491 }
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004492 if (start) {
4493 depth++;
4494 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01004495 ADDOP_JUMP(c, FOR_ITER, anchor);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004496 NEXT_BLOCK(c);
4497 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004500 /* XXX this needs to be cleaned up...a lot! */
4501 n = asdl_seq_LEN(gen->ifs);
4502 for (i = 0; i < n; i++) {
4503 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004504 if (!compiler_jump_if(c, e, if_cleanup, 0))
4505 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004506 NEXT_BLOCK(c);
4507 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004509 if (++gen_index < asdl_seq_LEN(generators))
4510 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004511 generators, gen_index, depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004512 elt, val, type))
4513 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004515 /* only append after the last for generator */
4516 if (gen_index >= asdl_seq_LEN(generators)) {
4517 /* comprehension specific code */
4518 switch (type) {
4519 case COMP_GENEXP:
4520 VISIT(c, expr, elt);
4521 ADDOP(c, YIELD_VALUE);
4522 ADDOP(c, POP_TOP);
4523 break;
4524 case COMP_LISTCOMP:
4525 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004526 ADDOP_I(c, LIST_APPEND, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004527 break;
4528 case COMP_SETCOMP:
4529 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004530 ADDOP_I(c, SET_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004531 break;
4532 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004533 /* With '{k: v}', k is evaluated before v, so we do
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004534 the same. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004535 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004536 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004537 ADDOP_I(c, MAP_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 break;
4539 default:
4540 return 0;
4541 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004543 compiler_use_next_block(c, skip);
4544 }
4545 compiler_use_next_block(c, if_cleanup);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004546 if (start) {
Mark Shannon582aaf12020-08-04 17:30:11 +01004547 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004548 compiler_use_next_block(c, anchor);
4549 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004550
4551 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004552}
4553
4554static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004555compiler_async_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004556 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004557 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004558 expr_ty elt, expr_ty val, int type)
4559{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004560 comprehension_ty gen;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004561 basicblock *start, *if_cleanup, *except;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004562 Py_ssize_t i, n;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004563 start = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004564 except = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004565 if_cleanup = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004566
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004567 if (start == NULL || if_cleanup == NULL || except == NULL) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004568 return 0;
4569 }
4570
4571 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4572
4573 if (gen_index == 0) {
4574 /* Receive outermost iter as an implicit argument */
4575 c->u->u_argcount = 1;
4576 ADDOP_I(c, LOAD_FAST, 0);
4577 }
4578 else {
4579 /* Sub-iter - calculate on the fly */
4580 VISIT(c, expr, gen->iter);
4581 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004582 }
4583
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004584 compiler_use_next_block(c, start);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004585
Mark Shannon582aaf12020-08-04 17:30:11 +01004586 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004587 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004588 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004589 ADDOP(c, YIELD_FROM);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004590 ADDOP(c, POP_BLOCK);
Serhiy Storchaka24d32012018-03-10 18:22:34 +02004591 VISIT(c, expr, gen->target);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004592
4593 n = asdl_seq_LEN(gen->ifs);
4594 for (i = 0; i < n; i++) {
4595 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004596 if (!compiler_jump_if(c, e, if_cleanup, 0))
4597 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004598 NEXT_BLOCK(c);
4599 }
4600
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004601 depth++;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004602 if (++gen_index < asdl_seq_LEN(generators))
4603 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004604 generators, gen_index, depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004605 elt, val, type))
4606 return 0;
4607
4608 /* only append after the last for generator */
4609 if (gen_index >= asdl_seq_LEN(generators)) {
4610 /* comprehension specific code */
4611 switch (type) {
4612 case COMP_GENEXP:
4613 VISIT(c, expr, elt);
4614 ADDOP(c, YIELD_VALUE);
4615 ADDOP(c, POP_TOP);
4616 break;
4617 case COMP_LISTCOMP:
4618 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004619 ADDOP_I(c, LIST_APPEND, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004620 break;
4621 case COMP_SETCOMP:
4622 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004623 ADDOP_I(c, SET_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004624 break;
4625 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004626 /* With '{k: v}', k is evaluated before v, so we do
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004627 the same. */
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004628 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004629 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004630 ADDOP_I(c, MAP_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004631 break;
4632 default:
4633 return 0;
4634 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004635 }
4636 compiler_use_next_block(c, if_cleanup);
Mark Shannon582aaf12020-08-04 17:30:11 +01004637 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004638
4639 compiler_use_next_block(c, except);
4640 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004641
4642 return 1;
4643}
4644
4645static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004646compiler_comprehension(struct compiler *c, expr_ty e, int type,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004647 identifier name, asdl_comprehension_seq *generators, expr_ty elt,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004648 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004650 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004651 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004652 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004653 int is_async_generator = 0;
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004654 int top_level_await = IS_TOP_LEVEL_AWAIT(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004655
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004656
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004657 int is_async_function = c->u->u_ste->ste_coroutine;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004658
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004659 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004660 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4661 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004662 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004663 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004664 }
4665
4666 is_async_generator = c->u->u_ste->ste_coroutine;
4667
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004668 if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004669 compiler_error(c, "asynchronous comprehension outside of "
4670 "an asynchronous function");
4671 goto error_in_scope;
4672 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004674 if (type != COMP_GENEXP) {
4675 int op;
4676 switch (type) {
4677 case COMP_LISTCOMP:
4678 op = BUILD_LIST;
4679 break;
4680 case COMP_SETCOMP:
4681 op = BUILD_SET;
4682 break;
4683 case COMP_DICTCOMP:
4684 op = BUILD_MAP;
4685 break;
4686 default:
4687 PyErr_Format(PyExc_SystemError,
4688 "unknown comprehension type %d", type);
4689 goto error_in_scope;
4690 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004692 ADDOP_I(c, op, 0);
4693 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004694
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004695 if (!compiler_comprehension_generator(c, generators, 0, 0, elt,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004696 val, type))
4697 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004699 if (type != COMP_GENEXP) {
4700 ADDOP(c, RETURN_VALUE);
4701 }
4702
4703 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004704 qualname = c->u->u_qualname;
4705 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004706 compiler_exit_scope(c);
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004707 if (top_level_await && is_async_generator){
4708 c->u->u_ste->ste_coroutine = 1;
4709 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004710 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004711 goto error;
4712
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004713 if (!compiler_make_closure(c, co, 0, qualname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004714 goto error;
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004715 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004716 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004717 Py_DECREF(co);
4718
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004719 VISIT(c, expr, outermost->iter);
4720
4721 if (outermost->is_async) {
4722 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004723 } else {
4724 ADDOP(c, GET_ITER);
4725 }
4726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004727 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004728
4729 if (is_async_generator && type != COMP_GENEXP) {
4730 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004731 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004732 ADDOP(c, YIELD_FROM);
4733 }
4734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004735 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004736error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004737 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004738error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004739 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004740 Py_XDECREF(co);
4741 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004742}
4743
4744static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004745compiler_genexp(struct compiler *c, expr_ty e)
4746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004747 static identifier name;
4748 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004749 name = PyUnicode_InternFromString("<genexpr>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004750 if (!name)
4751 return 0;
4752 }
4753 assert(e->kind == GeneratorExp_kind);
4754 return compiler_comprehension(c, e, COMP_GENEXP, name,
4755 e->v.GeneratorExp.generators,
4756 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004757}
4758
4759static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004760compiler_listcomp(struct compiler *c, expr_ty e)
4761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004762 static identifier name;
4763 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004764 name = PyUnicode_InternFromString("<listcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004765 if (!name)
4766 return 0;
4767 }
4768 assert(e->kind == ListComp_kind);
4769 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4770 e->v.ListComp.generators,
4771 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004772}
4773
4774static int
4775compiler_setcomp(struct compiler *c, expr_ty e)
4776{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004777 static identifier name;
4778 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004779 name = PyUnicode_InternFromString("<setcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004780 if (!name)
4781 return 0;
4782 }
4783 assert(e->kind == SetComp_kind);
4784 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4785 e->v.SetComp.generators,
4786 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004787}
4788
4789
4790static int
4791compiler_dictcomp(struct compiler *c, expr_ty e)
4792{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004793 static identifier name;
4794 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004795 name = PyUnicode_InternFromString("<dictcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004796 if (!name)
4797 return 0;
4798 }
4799 assert(e->kind == DictComp_kind);
4800 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4801 e->v.DictComp.generators,
4802 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004803}
4804
4805
4806static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004807compiler_visit_keyword(struct compiler *c, keyword_ty k)
4808{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004809 VISIT(c, expr, k->value);
4810 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004811}
4812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004813/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004814 whether they are true or false.
4815
4816 Return values: 1 for true, 0 for false, -1 for non-constant.
4817 */
4818
4819static int
Mark Shannonfee55262019-11-21 09:11:43 +00004820compiler_with_except_finish(struct compiler *c) {
4821 basicblock *exit;
4822 exit = compiler_new_block(c);
4823 if (exit == NULL)
4824 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01004825 ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit);
Mark Shannon266b4622020-11-17 19:30:14 +00004826 NEXT_BLOCK(c);
Mark Shannonbf353f32020-12-17 13:55:28 +00004827 ADDOP_I(c, RERAISE, 1);
Mark Shannonfee55262019-11-21 09:11:43 +00004828 compiler_use_next_block(c, exit);
4829 ADDOP(c, POP_TOP);
4830 ADDOP(c, POP_TOP);
4831 ADDOP(c, POP_TOP);
4832 ADDOP(c, POP_EXCEPT);
4833 ADDOP(c, POP_TOP);
4834 return 1;
4835}
Yury Selivanov75445082015-05-11 22:57:16 -04004836
4837/*
4838 Implements the async with statement.
4839
4840 The semantics outlined in that PEP are as follows:
4841
4842 async with EXPR as VAR:
4843 BLOCK
4844
4845 It is implemented roughly as:
4846
4847 context = EXPR
4848 exit = context.__aexit__ # not calling it
4849 value = await context.__aenter__()
4850 try:
4851 VAR = value # if VAR present in the syntax
4852 BLOCK
4853 finally:
4854 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004855 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04004856 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004857 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04004858 if not (await exit(*exc)):
4859 raise
4860 */
4861static int
4862compiler_async_with(struct compiler *c, stmt_ty s, int pos)
4863{
Mark Shannonfee55262019-11-21 09:11:43 +00004864 basicblock *block, *final, *exit;
Yury Selivanov75445082015-05-11 22:57:16 -04004865 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
4866
4867 assert(s->kind == AsyncWith_kind);
Pablo Galindo90235812020-03-15 04:29:22 +00004868 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07004869 c->u->u_ste->ste_coroutine = 1;
4870 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
Zsolt Dollensteine2396502018-04-27 08:58:56 -07004871 return compiler_error(c, "'async with' outside async function");
4872 }
Yury Selivanov75445082015-05-11 22:57:16 -04004873
4874 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004875 final = compiler_new_block(c);
4876 exit = compiler_new_block(c);
4877 if (!block || !final || !exit)
Yury Selivanov75445082015-05-11 22:57:16 -04004878 return 0;
4879
4880 /* Evaluate EXPR */
4881 VISIT(c, expr, item->context_expr);
4882
4883 ADDOP(c, BEFORE_ASYNC_WITH);
4884 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004885 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004886 ADDOP(c, YIELD_FROM);
4887
Mark Shannon582aaf12020-08-04 17:30:11 +01004888 ADDOP_JUMP(c, SETUP_ASYNC_WITH, final);
Yury Selivanov75445082015-05-11 22:57:16 -04004889
4890 /* SETUP_ASYNC_WITH pushes a finally block. */
4891 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004892 if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004893 return 0;
4894 }
4895
4896 if (item->optional_vars) {
4897 VISIT(c, expr, item->optional_vars);
4898 }
4899 else {
4900 /* Discard result from context.__aenter__() */
4901 ADDOP(c, POP_TOP);
4902 }
4903
4904 pos++;
4905 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
4906 /* BLOCK code */
4907 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
4908 else if (!compiler_async_with(c, s, pos))
4909 return 0;
4910
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004911 compiler_pop_fblock(c, ASYNC_WITH, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004912 ADDOP(c, POP_BLOCK);
4913 /* End of body; start the cleanup */
Yury Selivanov75445082015-05-11 22:57:16 -04004914
Mark Shannonfee55262019-11-21 09:11:43 +00004915 /* For successful outcome:
4916 * call __exit__(None, None, None)
4917 */
4918 if(!compiler_call_exit_with_nones(c))
Yury Selivanov75445082015-05-11 22:57:16 -04004919 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00004920 ADDOP(c, GET_AWAITABLE);
4921 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4922 ADDOP(c, YIELD_FROM);
Yury Selivanov75445082015-05-11 22:57:16 -04004923
Mark Shannonfee55262019-11-21 09:11:43 +00004924 ADDOP(c, POP_TOP);
Yury Selivanov75445082015-05-11 22:57:16 -04004925
Mark Shannon582aaf12020-08-04 17:30:11 +01004926 ADDOP_JUMP(c, JUMP_ABSOLUTE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00004927
4928 /* For exceptional outcome: */
4929 compiler_use_next_block(c, final);
4930
4931 ADDOP(c, WITH_EXCEPT_START);
Yury Selivanov75445082015-05-11 22:57:16 -04004932 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004933 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004934 ADDOP(c, YIELD_FROM);
Mark Shannonfee55262019-11-21 09:11:43 +00004935 compiler_with_except_finish(c);
Yury Selivanov75445082015-05-11 22:57:16 -04004936
Mark Shannonfee55262019-11-21 09:11:43 +00004937compiler_use_next_block(c, exit);
Yury Selivanov75445082015-05-11 22:57:16 -04004938 return 1;
4939}
4940
4941
Guido van Rossumc2e20742006-02-27 22:32:47 +00004942/*
4943 Implements the with statement from PEP 343.
Guido van Rossumc2e20742006-02-27 22:32:47 +00004944 with EXPR as VAR:
4945 BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00004946 is implemented as:
4947 <code for EXPR>
4948 SETUP_WITH E
4949 <code to store to VAR> or POP_TOP
4950 <code for BLOCK>
4951 LOAD_CONST (None, None, None)
4952 CALL_FUNCTION_EX 0
4953 JUMP_FORWARD EXIT
4954 E: WITH_EXCEPT_START (calls EXPR.__exit__)
4955 POP_JUMP_IF_TRUE T:
4956 RERAISE
4957 T: POP_TOP * 3 (remove exception from stack)
4958 POP_EXCEPT
4959 POP_TOP
4960 EXIT:
Guido van Rossumc2e20742006-02-27 22:32:47 +00004961 */
Mark Shannonfee55262019-11-21 09:11:43 +00004962
Guido van Rossumc2e20742006-02-27 22:32:47 +00004963static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004964compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004965{
Mark Shannonfee55262019-11-21 09:11:43 +00004966 basicblock *block, *final, *exit;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004967 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004968
4969 assert(s->kind == With_kind);
4970
Guido van Rossumc2e20742006-02-27 22:32:47 +00004971 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004972 final = compiler_new_block(c);
4973 exit = compiler_new_block(c);
4974 if (!block || !final || !exit)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004975 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004976
Thomas Wouters477c8d52006-05-27 19:21:47 +00004977 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004978 VISIT(c, expr, item->context_expr);
Mark Shannonfee55262019-11-21 09:11:43 +00004979 /* Will push bound __exit__ */
Mark Shannon582aaf12020-08-04 17:30:11 +01004980 ADDOP_JUMP(c, SETUP_WITH, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004981
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004982 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00004983 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004984 if (!compiler_push_fblock(c, WITH, block, final, NULL)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004985 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004986 }
4987
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004988 if (item->optional_vars) {
4989 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004990 }
4991 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004992 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004993 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004994 }
4995
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004996 pos++;
4997 if (pos == asdl_seq_LEN(s->v.With.items))
4998 /* BLOCK code */
4999 VISIT_SEQ(c, stmt, s->v.With.body)
5000 else if (!compiler_with(c, s, pos))
5001 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005002
Mark Shannon3bd60352021-01-13 12:05:43 +00005003
5004 /* Mark all following code as artificial */
5005 c->u->u_lineno = -1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005006 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005007 compiler_pop_fblock(c, WITH, block);
Mark Shannon13bc1392020-01-23 09:25:17 +00005008
Mark Shannonfee55262019-11-21 09:11:43 +00005009 /* End of body; start the cleanup. */
Mark Shannon13bc1392020-01-23 09:25:17 +00005010
Mark Shannonfee55262019-11-21 09:11:43 +00005011 /* For successful outcome:
5012 * call __exit__(None, None, None)
5013 */
5014 if (!compiler_call_exit_with_nones(c))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005015 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00005016 ADDOP(c, POP_TOP);
Mark Shannon582aaf12020-08-04 17:30:11 +01005017 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005018
Mark Shannonfee55262019-11-21 09:11:43 +00005019 /* For exceptional outcome: */
5020 compiler_use_next_block(c, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005021
Mark Shannonfee55262019-11-21 09:11:43 +00005022 ADDOP(c, WITH_EXCEPT_START);
5023 compiler_with_except_finish(c);
5024
5025 compiler_use_next_block(c, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005026 return 1;
5027}
5028
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005029static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005030compiler_visit_expr1(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005031{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005032 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07005033 case NamedExpr_kind:
5034 VISIT(c, expr, e->v.NamedExpr.value);
5035 ADDOP(c, DUP_TOP);
5036 VISIT(c, expr, e->v.NamedExpr.target);
5037 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005038 case BoolOp_kind:
5039 return compiler_boolop(c, e);
5040 case BinOp_kind:
5041 VISIT(c, expr, e->v.BinOp.left);
5042 VISIT(c, expr, e->v.BinOp.right);
Andy Lester76d58772020-03-10 21:18:12 -05005043 ADDOP(c, binop(e->v.BinOp.op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005044 break;
5045 case UnaryOp_kind:
5046 VISIT(c, expr, e->v.UnaryOp.operand);
5047 ADDOP(c, unaryop(e->v.UnaryOp.op));
5048 break;
5049 case Lambda_kind:
5050 return compiler_lambda(c, e);
5051 case IfExp_kind:
5052 return compiler_ifexp(c, e);
5053 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005054 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005055 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005056 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005057 case GeneratorExp_kind:
5058 return compiler_genexp(c, e);
5059 case ListComp_kind:
5060 return compiler_listcomp(c, e);
5061 case SetComp_kind:
5062 return compiler_setcomp(c, e);
5063 case DictComp_kind:
5064 return compiler_dictcomp(c, e);
5065 case Yield_kind:
5066 if (c->u->u_ste->ste_type != FunctionBlock)
5067 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005068 if (e->v.Yield.value) {
5069 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005070 }
5071 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005072 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005073 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005074 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005075 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005076 case YieldFrom_kind:
5077 if (c->u->u_ste->ste_type != FunctionBlock)
5078 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04005079
5080 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
5081 return compiler_error(c, "'yield from' inside async function");
5082
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005083 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04005084 ADDOP(c, GET_YIELD_FROM_ITER);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005085 ADDOP_LOAD_CONST(c, Py_None);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005086 ADDOP(c, YIELD_FROM);
5087 break;
Yury Selivanov75445082015-05-11 22:57:16 -04005088 case Await_kind:
Pablo Galindo90235812020-03-15 04:29:22 +00005089 if (!IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005090 if (c->u->u_ste->ste_type != FunctionBlock){
5091 return compiler_error(c, "'await' outside function");
5092 }
Yury Selivanov75445082015-05-11 22:57:16 -04005093
Victor Stinner331a6a52019-05-27 16:39:22 +02005094 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005095 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
5096 return compiler_error(c, "'await' outside async function");
5097 }
5098 }
Yury Selivanov75445082015-05-11 22:57:16 -04005099
5100 VISIT(c, expr, e->v.Await.value);
5101 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005102 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005103 ADDOP(c, YIELD_FROM);
5104 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005105 case Compare_kind:
5106 return compiler_compare(c, e);
5107 case Call_kind:
5108 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005109 case Constant_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005110 ADDOP_LOAD_CONST(c, e->v.Constant.value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005111 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005112 case JoinedStr_kind:
5113 return compiler_joined_str(c, e);
5114 case FormattedValue_kind:
5115 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005116 /* The following exprs can be assignment targets. */
5117 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005118 VISIT(c, expr, e->v.Attribute.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005119 switch (e->v.Attribute.ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005120 case Load:
Mark Shannond48848c2021-03-14 18:01:30 +00005121 {
5122 int old_lineno = c->u->u_lineno;
5123 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005124 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005125 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005126 break;
Mark Shannond48848c2021-03-14 18:01:30 +00005127 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005128 case Store:
Mark Shannond48848c2021-03-14 18:01:30 +00005129 if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005130 return 0;
Mark Shannond48848c2021-03-14 18:01:30 +00005131 }
5132 int old_lineno = c->u->u_lineno;
5133 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005134 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005135 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005136 break;
5137 case Del:
5138 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
5139 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005140 }
5141 break;
5142 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005143 return compiler_subscript(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005144 case Starred_kind:
5145 switch (e->v.Starred.ctx) {
5146 case Store:
5147 /* In all legitimate cases, the Starred node was already replaced
5148 * by compiler_list/compiler_tuple. XXX: is that okay? */
5149 return compiler_error(c,
5150 "starred assignment target must be in a list or tuple");
5151 default:
5152 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005153 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005154 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005155 break;
5156 case Slice_kind:
5157 return compiler_slice(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005158 case Name_kind:
5159 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
5160 /* child nodes of List and Tuple will have expr_context set */
5161 case List_kind:
5162 return compiler_list(c, e);
5163 case Tuple_kind:
5164 return compiler_tuple(c, e);
Brandt Bucher145bf262021-02-26 14:51:55 -08005165 case MatchAs_kind:
5166 case MatchOr_kind:
5167 // Can only occur in patterns, which are handled elsewhere.
5168 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005169 }
5170 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005171}
5172
5173static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005174compiler_visit_expr(struct compiler *c, expr_ty e)
5175{
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005176 int old_lineno = c->u->u_lineno;
5177 int old_col_offset = c->u->u_col_offset;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005178 SET_LOC(c, e);
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005179 int res = compiler_visit_expr1(c, e);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005180 c->u->u_lineno = old_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005181 c->u->u_col_offset = old_col_offset;
5182 return res;
5183}
5184
5185static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005186compiler_augassign(struct compiler *c, stmt_ty s)
5187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005188 assert(s->kind == AugAssign_kind);
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005189 expr_ty e = s->v.AugAssign.target;
5190
5191 int old_lineno = c->u->u_lineno;
5192 int old_col_offset = c->u->u_col_offset;
5193 SET_LOC(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005195 switch (e->kind) {
5196 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005197 VISIT(c, expr, e->v.Attribute.value);
5198 ADDOP(c, DUP_TOP);
Mark Shannond48848c2021-03-14 18:01:30 +00005199 int old_lineno = c->u->u_lineno;
5200 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005201 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005202 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005203 break;
5204 case Subscript_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005205 VISIT(c, expr, e->v.Subscript.value);
5206 VISIT(c, expr, e->v.Subscript.slice);
5207 ADDOP(c, DUP_TOP_TWO);
5208 ADDOP(c, BINARY_SUBSCR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005209 break;
5210 case Name_kind:
5211 if (!compiler_nameop(c, e->v.Name.id, Load))
5212 return 0;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005213 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005214 default:
5215 PyErr_Format(PyExc_SystemError,
5216 "invalid node type (%d) for augmented assignment",
5217 e->kind);
5218 return 0;
5219 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005220
5221 c->u->u_lineno = old_lineno;
5222 c->u->u_col_offset = old_col_offset;
5223
5224 VISIT(c, expr, s->v.AugAssign.value);
5225 ADDOP(c, inplace_binop(s->v.AugAssign.op));
5226
5227 SET_LOC(c, e);
5228
5229 switch (e->kind) {
5230 case Attribute_kind:
Mark Shannond48848c2021-03-14 18:01:30 +00005231 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005232 ADDOP(c, ROT_TWO);
5233 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5234 break;
5235 case Subscript_kind:
5236 ADDOP(c, ROT_THREE);
5237 ADDOP(c, STORE_SUBSCR);
5238 break;
5239 case Name_kind:
5240 return compiler_nameop(c, e->v.Name.id, Store);
5241 default:
5242 Py_UNREACHABLE();
5243 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005244 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005245}
5246
5247static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005248check_ann_expr(struct compiler *c, expr_ty e)
5249{
5250 VISIT(c, expr, e);
5251 ADDOP(c, POP_TOP);
5252 return 1;
5253}
5254
5255static int
5256check_annotation(struct compiler *c, stmt_ty s)
5257{
5258 /* Annotations are only evaluated in a module or class. */
5259 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5260 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
5261 return check_ann_expr(c, s->v.AnnAssign.annotation);
5262 }
5263 return 1;
5264}
5265
5266static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005267check_ann_subscr(struct compiler *c, expr_ty e)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005268{
5269 /* We check that everything in a subscript is defined at runtime. */
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005270 switch (e->kind) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005271 case Slice_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005272 if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005273 return 0;
5274 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005275 if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) {
5276 return 0;
5277 }
5278 if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) {
5279 return 0;
5280 }
5281 return 1;
5282 case Tuple_kind: {
5283 /* extended slice */
Pablo Galindoa5634c42020-09-16 19:42:00 +01005284 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005285 Py_ssize_t i, n = asdl_seq_LEN(elts);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005286 for (i = 0; i < n; i++) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005287 if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005288 return 0;
5289 }
5290 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005291 return 1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005292 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005293 default:
5294 return check_ann_expr(c, e);
5295 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005296}
5297
5298static int
5299compiler_annassign(struct compiler *c, stmt_ty s)
5300{
5301 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07005302 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005303
5304 assert(s->kind == AnnAssign_kind);
5305
5306 /* We perform the actual assignment first. */
5307 if (s->v.AnnAssign.value) {
5308 VISIT(c, expr, s->v.AnnAssign.value);
5309 VISIT(c, expr, targ);
5310 }
5311 switch (targ->kind) {
5312 case Name_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005313 if (forbidden_name(c, targ->v.Name.id, Store))
5314 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005315 /* If we have a simple name in a module or class, store annotation. */
5316 if (s->v.AnnAssign.simple &&
5317 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5318 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Batuhan Taskaya044a1042020-10-06 23:03:02 +03005319 VISIT(c, annexpr, s->v.AnnAssign.annotation);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005320 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02005321 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005322 ADDOP_LOAD_CONST_NEW(c, mangled);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005323 ADDOP(c, STORE_SUBSCR);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005324 }
5325 break;
5326 case Attribute_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005327 if (forbidden_name(c, targ->v.Attribute.attr, Store))
5328 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005329 if (!s->v.AnnAssign.value &&
5330 !check_ann_expr(c, targ->v.Attribute.value)) {
5331 return 0;
5332 }
5333 break;
5334 case Subscript_kind:
5335 if (!s->v.AnnAssign.value &&
5336 (!check_ann_expr(c, targ->v.Subscript.value) ||
5337 !check_ann_subscr(c, targ->v.Subscript.slice))) {
5338 return 0;
5339 }
5340 break;
5341 default:
5342 PyErr_Format(PyExc_SystemError,
5343 "invalid node type (%d) for annotated assignment",
5344 targ->kind);
5345 return 0;
5346 }
5347 /* Annotation is evaluated last. */
5348 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
5349 return 0;
5350 }
5351 return 1;
5352}
5353
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005354/* Raises a SyntaxError and returns 0.
5355 If something goes wrong, a different exception may be raised.
5356*/
5357
5358static int
Brandt Bucher145bf262021-02-26 14:51:55 -08005359compiler_error(struct compiler *c, const char *format, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005360{
Brandt Bucher145bf262021-02-26 14:51:55 -08005361 va_list vargs;
5362#ifdef HAVE_STDARG_PROTOTYPES
5363 va_start(vargs, format);
5364#else
5365 va_start(vargs);
5366#endif
5367 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5368 va_end(vargs);
5369 if (msg == NULL) {
5370 return 0;
5371 }
5372 PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
5373 if (loc == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005374 Py_INCREF(Py_None);
5375 loc = Py_None;
5376 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005377 PyObject *args = Py_BuildValue("O(OiiO)", msg, c->c_filename,
5378 c->u->u_lineno, c->u->u_col_offset + 1, loc);
5379 Py_DECREF(msg);
5380 if (args == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005381 goto exit;
Brandt Bucher145bf262021-02-26 14:51:55 -08005382 }
5383 PyErr_SetObject(PyExc_SyntaxError, args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005384 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005385 Py_DECREF(loc);
Brandt Bucher145bf262021-02-26 14:51:55 -08005386 Py_XDECREF(args);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005387 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005388}
5389
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005390/* Emits a SyntaxWarning and returns 1 on success.
5391 If a SyntaxWarning raised as error, replaces it with a SyntaxError
5392 and returns 0.
5393*/
5394static int
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005395compiler_warn(struct compiler *c, const char *format, ...)
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005396{
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005397 va_list vargs;
5398#ifdef HAVE_STDARG_PROTOTYPES
5399 va_start(vargs, format);
5400#else
5401 va_start(vargs);
5402#endif
5403 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5404 va_end(vargs);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005405 if (msg == NULL) {
5406 return 0;
5407 }
5408 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
5409 c->u->u_lineno, NULL, NULL) < 0)
5410 {
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005411 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005412 /* Replace the SyntaxWarning exception with a SyntaxError
5413 to get a more accurate error report */
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005414 PyErr_Clear();
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005415 assert(PyUnicode_AsUTF8(msg) != NULL);
5416 compiler_error(c, PyUnicode_AsUTF8(msg));
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005417 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005418 Py_DECREF(msg);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005419 return 0;
5420 }
5421 Py_DECREF(msg);
5422 return 1;
5423}
5424
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005425static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005426compiler_subscript(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005427{
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005428 expr_context_ty ctx = e->v.Subscript.ctx;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005429 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005430
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005431 if (ctx == Load) {
5432 if (!check_subscripter(c, e->v.Subscript.value)) {
5433 return 0;
5434 }
5435 if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
5436 return 0;
5437 }
5438 }
5439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005440 switch (ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005441 case Load: op = BINARY_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005442 case Store: op = STORE_SUBSCR; break;
5443 case Del: op = DELETE_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005444 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005445 assert(op);
5446 VISIT(c, expr, e->v.Subscript.value);
5447 VISIT(c, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005448 ADDOP(c, op);
5449 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005450}
5451
5452static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005453compiler_slice(struct compiler *c, expr_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005455 int n = 2;
5456 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005458 /* only handles the cases where BUILD_SLICE is emitted */
5459 if (s->v.Slice.lower) {
5460 VISIT(c, expr, s->v.Slice.lower);
5461 }
5462 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005463 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005464 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005466 if (s->v.Slice.upper) {
5467 VISIT(c, expr, s->v.Slice.upper);
5468 }
5469 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005470 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005471 }
5472
5473 if (s->v.Slice.step) {
5474 n++;
5475 VISIT(c, expr, s->v.Slice.step);
5476 }
5477 ADDOP_I(c, BUILD_SLICE, n);
5478 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005479}
5480
Brandt Bucher145bf262021-02-26 14:51:55 -08005481
5482// PEP 634: Structural Pattern Matching
5483
5484// To keep things simple, all compiler_pattern_* routines follow the convention
5485// of replacing TOS (the subject for the given pattern) with either True (match)
5486// or False (no match). We do this even for irrefutable patterns; the idea is
5487// that it's much easier to smooth out any redundant pushing, popping, and
5488// jumping in the peephole optimizer than to detect or predict it here.
5489
5490
5491#define WILDCARD_CHECK(N) \
5492 ((N)->kind == Name_kind && \
5493 _PyUnicode_EqualToASCIIString((N)->v.Name.id, "_"))
5494
5495
5496static int
5497pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc)
5498{
5499 assert(!_PyUnicode_EqualToASCIIString(n, "_"));
5500 // Can't assign to the same name twice:
5501 if (pc->stores == NULL) {
5502 RETURN_IF_FALSE(pc->stores = PySet_New(NULL));
5503 }
5504 else {
5505 int duplicate = PySet_Contains(pc->stores, n);
5506 if (duplicate < 0) {
5507 return 0;
5508 }
5509 if (duplicate) {
5510 const char *e = "multiple assignments to name %R in pattern";
5511 return compiler_error(c, e, n);
5512 }
5513 }
5514 RETURN_IF_FALSE(!PySet_Add(pc->stores, n));
5515 RETURN_IF_FALSE(compiler_nameop(c, n, Store));
5516 return 1;
5517}
5518
5519
5520static int
5521pattern_helper_sequence_unpack(struct compiler *c, asdl_expr_seq *values,
5522 Py_ssize_t star, pattern_context *pc)
5523{
5524 RETURN_IF_FALSE(unpack_helper(c, values));
5525 // We've now got a bunch of new subjects on the stack. If any of them fail
5526 // to match, we need to pop everything else off, then finally push False.
5527 // fails is an array of blocks that correspond to the necessary amount of
5528 // popping for each element:
5529 basicblock **fails;
5530 Py_ssize_t size = asdl_seq_LEN(values);
5531 fails = (basicblock **)PyObject_Malloc(sizeof(basicblock*) * size);
5532 if (fails == NULL) {
5533 PyErr_NoMemory();
5534 return 0;
5535 }
5536 // NOTE: Can't use our nice returning macros anymore: they'll leak memory!
5537 // goto error on error.
5538 for (Py_ssize_t i = 0; i < size; i++) {
5539 fails[i] = compiler_new_block(c);
5540 if (fails[i] == NULL) {
5541 goto error;
5542 }
5543 }
5544 for (Py_ssize_t i = 0; i < size; i++) {
5545 expr_ty value = asdl_seq_GET(values, i);
5546 if (i == star) {
5547 assert(value->kind == Starred_kind);
5548 value = value->v.Starred.value;
5549 }
5550 if (!compiler_pattern_subpattern(c, value, pc) ||
5551 !compiler_addop_j(c, POP_JUMP_IF_FALSE, fails[i]) ||
5552 compiler_next_block(c) == NULL)
5553 {
5554 goto error;
5555 }
5556 }
5557 // Success!
5558 basicblock *end = compiler_new_block(c);
5559 if (end == NULL ||
5560 !compiler_addop_load_const(c, Py_True) ||
5561 !compiler_addop_j(c, JUMP_FORWARD, end))
5562 {
5563 goto error;
5564 }
5565 // This is where we handle failed sub-patterns. For a sequence pattern like
5566 // [a, b, c, d], this will look like:
5567 // fails[0]: POP_TOP
5568 // fails[1]: POP_TOP
5569 // fails[2]: POP_TOP
5570 // fails[3]: LOAD_CONST False
5571 for (Py_ssize_t i = 0; i < size - 1; i++) {
5572 compiler_use_next_block(c, fails[i]);
5573 if (!compiler_addop(c, POP_TOP)) {
5574 goto error;
5575 }
5576 }
5577 compiler_use_next_block(c, fails[size - 1]);
5578 if (!compiler_addop_load_const(c, Py_False)) {
5579 goto error;
5580 }
5581 compiler_use_next_block(c, end);
5582 PyObject_Free(fails);
5583 return 1;
5584error:
5585 PyObject_Free(fails);
5586 return 0;
5587}
5588
5589// Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of
5590// UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a
5591// starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc.
5592static int
5593pattern_helper_sequence_subscr(struct compiler *c, asdl_expr_seq *values,
5594 Py_ssize_t star, pattern_context *pc)
5595{
5596 basicblock *end, *fail_pop_1;
5597 RETURN_IF_FALSE(end = compiler_new_block(c));
5598 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5599 Py_ssize_t size = asdl_seq_LEN(values);
5600 for (Py_ssize_t i = 0; i < size; i++) {
5601 expr_ty value = asdl_seq_GET(values, i);
5602 if (WILDCARD_CHECK(value)) {
5603 continue;
5604 }
5605 if (i == star) {
5606 assert(value->kind == Starred_kind);
5607 assert(WILDCARD_CHECK(value->v.Starred.value));
5608 continue;
5609 }
5610 ADDOP(c, DUP_TOP);
5611 if (i < star) {
5612 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5613 }
5614 else {
5615 // The subject may not support negative indexing! Compute a
5616 // nonnegative index:
5617 ADDOP(c, GET_LEN);
5618 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i));
5619 ADDOP(c, BINARY_SUBTRACT);
5620 }
5621 ADDOP(c, BINARY_SUBSCR);
5622 RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc));
5623 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5624 NEXT_BLOCK(c);
5625 }
5626 ADDOP(c, POP_TOP);
5627 ADDOP_LOAD_CONST(c, Py_True);
5628 ADDOP_JUMP(c, JUMP_FORWARD, end);
5629 compiler_use_next_block(c, fail_pop_1);
5630 ADDOP(c, POP_TOP);
5631 ADDOP_LOAD_CONST(c, Py_False);
5632 compiler_use_next_block(c, end);
5633 return 1;
5634}
5635
5636
5637// Like compiler_pattern, but turn off checks for irrefutability.
5638static int
5639compiler_pattern_subpattern(struct compiler *c, expr_ty p, pattern_context *pc)
5640{
5641 int allow_irrefutable = pc->allow_irrefutable;
5642 pc->allow_irrefutable = 1;
5643 RETURN_IF_FALSE(compiler_pattern(c, p, pc));
5644 pc->allow_irrefutable = allow_irrefutable;
5645 return 1;
5646}
5647
5648
5649static int
5650compiler_pattern_as(struct compiler *c, expr_ty p, pattern_context *pc)
5651{
5652 assert(p->kind == MatchAs_kind);
5653 basicblock *end, *fail_pop_1;
5654 RETURN_IF_FALSE(end = compiler_new_block(c));
5655 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5656 // Need to make a copy for (possibly) storing later:
5657 ADDOP(c, DUP_TOP);
5658 RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc));
5659 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5660 NEXT_BLOCK(c);
5661 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc));
5662 ADDOP_LOAD_CONST(c, Py_True);
5663 ADDOP_JUMP(c, JUMP_FORWARD, end);
5664 compiler_use_next_block(c, fail_pop_1);
5665 // Need to pop that unused copy from before:
5666 ADDOP(c, POP_TOP);
5667 ADDOP_LOAD_CONST(c, Py_False);
5668 compiler_use_next_block(c, end);
5669 return 1;
5670}
5671
5672
5673static int
5674compiler_pattern_capture(struct compiler *c, expr_ty p, pattern_context *pc)
5675{
5676 assert(p->kind == Name_kind);
5677 assert(p->v.Name.ctx == Store);
5678 assert(!WILDCARD_CHECK(p));
5679 if (!pc->allow_irrefutable) {
5680 // Whoops, can't have a name capture here!
5681 const char *e = "name capture %R makes remaining patterns unreachable";
5682 return compiler_error(c, e, p->v.Name.id);
5683 }
5684 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.Name.id, pc));
5685 ADDOP_LOAD_CONST(c, Py_True);
5686 return 1;
5687}
5688
5689
5690static int
5691compiler_pattern_class(struct compiler *c, expr_ty p, pattern_context *pc)
5692{
5693 asdl_expr_seq *args = p->v.Call.args;
5694 asdl_keyword_seq *kwargs = p->v.Call.keywords;
5695 Py_ssize_t nargs = asdl_seq_LEN(args);
5696 Py_ssize_t nkwargs = asdl_seq_LEN(kwargs);
5697 if (INT_MAX < nargs || INT_MAX < nargs + nkwargs - 1) {
5698 const char *e = "too many sub-patterns in class pattern %R";
5699 return compiler_error(c, e, p->v.Call.func);
5700 }
5701 RETURN_IF_FALSE(!validate_keywords(c, kwargs));
5702 basicblock *end, *fail_pop_1;
5703 RETURN_IF_FALSE(end = compiler_new_block(c));
5704 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5705 VISIT(c, expr, p->v.Call.func);
5706 PyObject *kwnames;
5707 RETURN_IF_FALSE(kwnames = PyTuple_New(nkwargs));
5708 Py_ssize_t i;
5709 for (i = 0; i < nkwargs; i++) {
5710 PyObject *name = ((keyword_ty) asdl_seq_GET(kwargs, i))->arg;
5711 Py_INCREF(name);
5712 PyTuple_SET_ITEM(kwnames, i, name);
5713 }
5714 ADDOP_LOAD_CONST_NEW(c, kwnames);
5715 ADDOP_I(c, MATCH_CLASS, nargs);
5716 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5717 NEXT_BLOCK(c);
5718 // TOS is now a tuple of (nargs + nkwargs) attributes.
5719 for (i = 0; i < nargs + nkwargs; i++) {
5720 expr_ty arg;
5721 if (i < nargs) {
5722 // Positional:
5723 arg = asdl_seq_GET(args, i);
5724 }
5725 else {
5726 // Keyword:
5727 arg = ((keyword_ty) asdl_seq_GET(kwargs, i - nargs))->value;
5728 }
5729 if (WILDCARD_CHECK(arg)) {
5730 continue;
5731 }
5732 // Get the i-th attribute, and match it against the i-th pattern:
5733 ADDOP(c, DUP_TOP);
5734 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5735 ADDOP(c, BINARY_SUBSCR);
5736 RETURN_IF_FALSE(compiler_pattern_subpattern(c, arg, pc));
5737 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5738 NEXT_BLOCK(c);
5739 }
5740 // Success! Pop the tuple of attributes:
5741 ADDOP(c, POP_TOP);
5742 ADDOP_LOAD_CONST(c, Py_True);
5743 ADDOP_JUMP(c, JUMP_FORWARD, end);
5744 compiler_use_next_block(c, fail_pop_1);
5745 ADDOP(c, POP_TOP);
5746 ADDOP_LOAD_CONST(c, Py_False);
5747 compiler_use_next_block(c, end);
5748 return 1;
5749}
5750
5751
5752static int
5753compiler_pattern_literal(struct compiler *c, expr_ty p, pattern_context *pc)
5754{
5755 assert(p->kind == Constant_kind);
5756 PyObject *v = p->v.Constant.value;
5757 ADDOP_LOAD_CONST(c, v);
5758 // Literal True, False, and None are compared by identity. All others use
5759 // equality:
5760 ADDOP_COMPARE(c, (v == Py_None || PyBool_Check(v)) ? Is : Eq);
5761 return 1;
5762}
5763
5764
5765static int
5766compiler_pattern_mapping(struct compiler *c, expr_ty p, pattern_context *pc)
5767{
5768 basicblock *end, *fail_pop_1, *fail_pop_3;
5769 RETURN_IF_FALSE(end = compiler_new_block(c));
5770 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5771 RETURN_IF_FALSE(fail_pop_3 = compiler_new_block(c));
5772 asdl_expr_seq *keys = p->v.Dict.keys;
5773 asdl_expr_seq *values = p->v.Dict.values;
5774 Py_ssize_t size = asdl_seq_LEN(values);
Ikko Ashimine57827f82021-03-10 19:39:51 +09005775 // A starred pattern will be a keyless value. It is guaranteed to be last:
Brandt Bucher145bf262021-02-26 14:51:55 -08005776 int star = size ? !asdl_seq_GET(keys, size - 1) : 0;
5777 ADDOP(c, MATCH_MAPPING);
5778 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5779 NEXT_BLOCK(c);
5780 if (!size) {
5781 // If the pattern is just "{}", we're done!
5782 ADDOP(c, POP_TOP);
5783 ADDOP_LOAD_CONST(c, Py_True);
5784 ADDOP_JUMP(c, JUMP_FORWARD, end);
5785 compiler_use_next_block(c, fail_pop_1);
5786 ADDOP(c, POP_TOP);
5787 ADDOP_LOAD_CONST(c, Py_False);
5788 compiler_use_next_block(c, end);
5789 return 1;
5790 }
5791 if (size - star) {
5792 // If the pattern has any keys in it, perform a length check:
5793 ADDOP(c, GET_LEN);
5794 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - star));
5795 ADDOP_COMPARE(c, GtE);
5796 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5797 NEXT_BLOCK(c);
5798 }
5799 if (INT_MAX < size - star - 1) {
5800 return compiler_error(c, "too many sub-patterns in mapping pattern");
5801 }
5802 // Collect all of the keys into a tuple for MATCH_KEYS and
5803 // COPY_DICT_WITHOUT_KEYS. They can either be dotted names or literals:
5804 for (Py_ssize_t i = 0; i < size - star; i++) {
5805 expr_ty key = asdl_seq_GET(keys, i);
5806 if (key == NULL) {
5807 const char *e = "can't use starred name here "
5808 "(consider moving to end)";
5809 return compiler_error(c, e);
5810 }
5811 VISIT(c, expr, key);
5812 }
5813 ADDOP_I(c, BUILD_TUPLE, size - star);
5814 ADDOP(c, MATCH_KEYS);
5815 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3);
5816 NEXT_BLOCK(c);
5817 // So far so good. There's now a tuple of values on the stack to match
5818 // sub-patterns against:
5819 for (Py_ssize_t i = 0; i < size - star; i++) {
5820 expr_ty value = asdl_seq_GET(values, i);
5821 if (WILDCARD_CHECK(value)) {
5822 continue;
5823 }
5824 ADDOP(c, DUP_TOP);
5825 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5826 ADDOP(c, BINARY_SUBSCR);
5827 RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc));
5828 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3);
5829 NEXT_BLOCK(c);
5830 }
5831 // If we get this far, it's a match! We're done with that tuple of values.
5832 ADDOP(c, POP_TOP);
5833 if (star) {
5834 // If we had a starred name, bind a dict of remaining items to it:
5835 ADDOP(c, COPY_DICT_WITHOUT_KEYS);
5836 PyObject *id = asdl_seq_GET(values, size - 1)->v.Name.id;
5837 RETURN_IF_FALSE(pattern_helper_store_name(c, id, pc));
5838 }
5839 else {
5840 // Otherwise, we don't care about this tuple of keys anymore:
5841 ADDOP(c, POP_TOP);
5842 }
5843 // Pop the subject:
5844 ADDOP(c, POP_TOP);
5845 ADDOP_LOAD_CONST(c, Py_True);
5846 ADDOP_JUMP(c, JUMP_FORWARD, end);
5847 // The top two items are a tuple of values or None, followed by a tuple of
5848 // keys. Pop them both:
5849 compiler_use_next_block(c, fail_pop_3);
5850 ADDOP(c, POP_TOP);
5851 ADDOP(c, POP_TOP);
5852 compiler_use_next_block(c, fail_pop_1);
5853 // Pop the subject:
5854 ADDOP(c, POP_TOP);
5855 ADDOP_LOAD_CONST(c, Py_False);
5856 compiler_use_next_block(c, end);
5857 return 1;
5858}
5859
5860
5861static int
5862compiler_pattern_or(struct compiler *c, expr_ty p, pattern_context *pc)
5863{
5864 assert(p->kind == MatchOr_kind);
5865 // control is the set of names bound by the first alternative. If all of the
5866 // others bind the same names (they should), then this becomes pc->stores.
5867 PyObject *control = NULL;
5868 basicblock *end, *pass_pop_1;
5869 RETURN_IF_FALSE(end = compiler_new_block(c));
5870 RETURN_IF_FALSE(pass_pop_1 = compiler_new_block(c));
5871 Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns);
5872 assert(size > 1);
5873 // We're going to be messing with pc. Keep the original info handy:
5874 PyObject *stores_init = pc->stores;
5875 int allow_irrefutable = pc->allow_irrefutable;
5876 for (Py_ssize_t i = 0; i < size; i++) {
5877 // NOTE: Can't use our nice returning macros in here: they'll leak sets!
5878 expr_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i);
5879 pc->stores = PySet_New(stores_init);
5880 // An irrefutable sub-pattern must be last, if it is allowed at all:
5881 int is_last = i == size - 1;
5882 pc->allow_irrefutable = allow_irrefutable && is_last;
5883 SET_LOC(c, alt);
5884 if (pc->stores == NULL ||
5885 // Only copy the subject if we're *not* on the last alternative:
5886 (!is_last && !compiler_addop(c, DUP_TOP)) ||
5887 !compiler_pattern(c, alt, pc) ||
5888 // Only jump if we're *not* on the last alternative:
5889 (!is_last && !compiler_addop_j(c, POP_JUMP_IF_TRUE, pass_pop_1)) ||
5890 !compiler_next_block(c))
5891 {
5892 goto fail;
5893 }
5894 if (!i) {
5895 // If this is the first alternative, save its stores as a "control"
5896 // for the others (they can't bind a different set of names):
5897 control = pc->stores;
5898 continue;
5899 }
5900 if (PySet_GET_SIZE(pc->stores) || PySet_GET_SIZE(control)) {
5901 // Otherwise, check to see if we differ from the control set:
5902 PyObject *diff = PyNumber_InPlaceXor(pc->stores, control);
5903 if (diff == NULL) {
5904 goto fail;
5905 }
5906 if (PySet_GET_SIZE(diff)) {
5907 // The names differ! Raise.
5908 Py_DECREF(diff);
5909 compiler_error(c, "alternative patterns bind different names");
5910 goto fail;
5911 }
5912 Py_DECREF(diff);
5913 }
5914 Py_DECREF(pc->stores);
5915 }
5916 Py_XDECREF(stores_init);
5917 // Update pc->stores and restore pc->allow_irrefutable:
5918 pc->stores = control;
5919 pc->allow_irrefutable = allow_irrefutable;
5920 ADDOP_JUMP(c, JUMP_FORWARD, end);
5921 compiler_use_next_block(c, pass_pop_1);
5922 ADDOP(c, POP_TOP);
5923 ADDOP_LOAD_CONST(c, Py_True);
5924 compiler_use_next_block(c, end);
5925 return 1;
5926fail:
5927 Py_XDECREF(stores_init);
5928 Py_XDECREF(control);
5929 return 0;
5930}
5931
5932
5933static int
5934compiler_pattern_sequence(struct compiler *c, expr_ty p, pattern_context *pc)
5935{
5936 assert(p->kind == List_kind || p->kind == Tuple_kind);
5937 asdl_expr_seq *values = (p->kind == Tuple_kind) ? p->v.Tuple.elts
5938 : p->v.List.elts;
5939 Py_ssize_t size = asdl_seq_LEN(values);
5940 Py_ssize_t star = -1;
5941 int only_wildcard = 1;
5942 int star_wildcard = 0;
5943 // Find a starred name, if it exists. There may be at most one:
5944 for (Py_ssize_t i = 0; i < size; i++) {
5945 expr_ty value = asdl_seq_GET(values, i);
5946 if (value->kind == Starred_kind) {
5947 value = value->v.Starred.value;
5948 if (star >= 0) {
5949 const char *e = "multiple starred names in sequence pattern";
5950 return compiler_error(c, e);
5951 }
5952 star_wildcard = WILDCARD_CHECK(value);
5953 star = i;
5954 }
5955 only_wildcard &= WILDCARD_CHECK(value);
5956 }
5957 basicblock *end, *fail_pop_1;
5958 RETURN_IF_FALSE(end = compiler_new_block(c));
5959 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5960 ADDOP(c, MATCH_SEQUENCE);
5961 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5962 NEXT_BLOCK(c);
5963 if (star < 0) {
5964 // No star: len(subject) == size
5965 ADDOP(c, GET_LEN);
5966 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
5967 ADDOP_COMPARE(c, Eq);
5968 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5969 NEXT_BLOCK(c);
5970 }
5971 else if (size > 1) {
5972 // Star: len(subject) >= size - 1
5973 ADDOP(c, GET_LEN);
5974 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1));
5975 ADDOP_COMPARE(c, GtE);
5976 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5977 NEXT_BLOCK(c);
5978 }
5979 if (only_wildcard) {
5980 // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc.
5981 ADDOP(c, POP_TOP);
5982 ADDOP_LOAD_CONST(c, Py_True);
5983 }
5984 else if (star_wildcard) {
5985 RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, values, star, pc));
5986 }
5987 else {
5988 RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, values, star, pc));
5989 }
5990 ADDOP_JUMP(c, JUMP_FORWARD, end);
5991 compiler_use_next_block(c, fail_pop_1);
5992 ADDOP(c, POP_TOP)
5993 ADDOP_LOAD_CONST(c, Py_False);
5994 compiler_use_next_block(c, end);
5995 return 1;
5996}
5997
5998
5999static int
6000compiler_pattern_value(struct compiler *c, expr_ty p, pattern_context *pc)
6001{
6002 assert(p->kind == Attribute_kind);
6003 assert(p->v.Attribute.ctx == Load);
6004 VISIT(c, expr, p);
6005 ADDOP_COMPARE(c, Eq);
6006 return 1;
6007}
6008
6009
6010static int
6011compiler_pattern_wildcard(struct compiler *c, expr_ty p, pattern_context *pc)
6012{
6013 assert(p->kind == Name_kind);
6014 assert(p->v.Name.ctx == Store);
6015 assert(WILDCARD_CHECK(p));
6016 if (!pc->allow_irrefutable) {
6017 // Whoops, can't have a wildcard here!
6018 const char *e = "wildcard makes remaining patterns unreachable";
6019 return compiler_error(c, e);
6020 }
6021 ADDOP(c, POP_TOP);
6022 ADDOP_LOAD_CONST(c, Py_True);
6023 return 1;
6024}
6025
6026
6027static int
6028compiler_pattern(struct compiler *c, expr_ty p, pattern_context *pc)
6029{
6030 SET_LOC(c, p);
6031 switch (p->kind) {
6032 case Attribute_kind:
6033 return compiler_pattern_value(c, p, pc);
6034 case BinOp_kind:
6035 // Because we allow "2+2j", things like "2+2" make it this far:
6036 return compiler_error(c, "patterns cannot include operators");
6037 case Call_kind:
6038 return compiler_pattern_class(c, p, pc);
6039 case Constant_kind:
6040 return compiler_pattern_literal(c, p, pc);
6041 case Dict_kind:
6042 return compiler_pattern_mapping(c, p, pc);
6043 case JoinedStr_kind:
6044 // Because we allow strings, f-strings make it this far:
6045 return compiler_error(c, "patterns cannot include f-strings");
6046 case List_kind:
6047 case Tuple_kind:
6048 return compiler_pattern_sequence(c, p, pc);
6049 case MatchAs_kind:
6050 return compiler_pattern_as(c, p, pc);
6051 case MatchOr_kind:
6052 return compiler_pattern_or(c, p, pc);
6053 case Name_kind:
6054 if (WILDCARD_CHECK(p)) {
6055 return compiler_pattern_wildcard(c, p, pc);
6056 }
6057 return compiler_pattern_capture(c, p, pc);
6058 default:
6059 Py_UNREACHABLE();
6060 }
6061}
6062
6063
6064static int
6065compiler_match(struct compiler *c, stmt_ty s)
6066{
6067 VISIT(c, expr, s->v.Match.subject);
6068 basicblock *next, *end;
6069 RETURN_IF_FALSE(end = compiler_new_block(c));
6070 Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases);
6071 assert(cases);
6072 pattern_context pc;
6073 // We use pc.stores to track:
6074 // - Repeated name assignments in the same pattern.
6075 // - Different name assignments in alternatives.
6076 // It's a set of names, but we don't create it until it's needed:
6077 pc.stores = NULL;
6078 match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6079 int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases;
6080 for (Py_ssize_t i = 0; i < cases - has_default; i++) {
6081 m = asdl_seq_GET(s->v.Match.cases, i);
6082 SET_LOC(c, m->pattern);
6083 RETURN_IF_FALSE(next = compiler_new_block(c));
6084 // If pc.allow_irrefutable is 0, any name captures against our subject
6085 // will raise. Irrefutable cases must be either guarded, last, or both:
6086 pc.allow_irrefutable = m->guard != NULL || i == cases - 1;
6087 // Only copy the subject if we're *not* on the last case:
6088 if (i != cases - has_default - 1) {
6089 ADDOP(c, DUP_TOP);
6090 }
6091 int result = compiler_pattern(c, m->pattern, &pc);
6092 Py_CLEAR(pc.stores);
6093 RETURN_IF_FALSE(result);
6094 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, next);
6095 NEXT_BLOCK(c);
6096 if (m->guard) {
6097 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, next, 0));
6098 }
6099 // Success! Pop the subject off, we're done with it:
6100 if (i != cases - has_default - 1) {
6101 ADDOP(c, POP_TOP);
6102 }
6103 VISIT_SEQ(c, stmt, m->body);
6104 ADDOP_JUMP(c, JUMP_FORWARD, end);
6105 compiler_use_next_block(c, next);
6106 }
6107 if (has_default) {
6108 if (cases == 1) {
6109 // No matches. Done with the subject:
6110 ADDOP(c, POP_TOP);
6111 }
6112 // A trailing "case _" is common, and lets us save a bit of redundant
6113 // pushing and popping in the loop above:
6114 m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6115 SET_LOC(c, m->pattern);
6116 if (m->guard) {
6117 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0));
6118 }
6119 VISIT_SEQ(c, stmt, m->body);
6120 }
6121 compiler_use_next_block(c, end);
6122 return 1;
6123}
6124
6125
6126#undef WILDCARD_CHECK
6127
6128
Thomas Wouters89f507f2006-12-13 04:49:30 +00006129/* End of the compiler section, beginning of the assembler section */
6130
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006131/* do depth-first search of basic block graph, starting with block.
T. Wouters99b54d62019-09-12 07:05:33 -07006132 post records the block indices in post-order.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006133
6134 XXX must handle implicit jumps from one block to next
6135*/
6136
Thomas Wouters89f507f2006-12-13 04:49:30 +00006137struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006138 PyObject *a_bytecode; /* string containing bytecode */
6139 int a_offset; /* offset into bytecode */
6140 int a_nblocks; /* number of reachable blocks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006141 PyObject *a_lnotab; /* string containing lnotab */
6142 int a_lnotab_off; /* offset into lnotab */
Mark Shannon877df852020-11-12 09:43:29 +00006143 int a_prevlineno; /* lineno of last emitted line in line table */
6144 int a_lineno; /* lineno of last emitted instruction */
6145 int a_lineno_start; /* bytecode start offset of current lineno */
Mark Shannoncc75ab72020-11-12 19:49:33 +00006146 basicblock *a_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006147};
6148
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006149Py_LOCAL_INLINE(void)
6150stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006151{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02006152 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
Mark Shannonfee55262019-11-21 09:11:43 +00006153 if (b->b_startdepth < depth && b->b_startdepth < 100) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006154 assert(b->b_startdepth < 0);
6155 b->b_startdepth = depth;
6156 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02006157 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006158}
6159
6160/* Find the flow path that needs the largest stack. We assume that
6161 * cycles in the flow graph have no net effect on the stack depth.
6162 */
6163static int
6164stackdepth(struct compiler *c)
6165{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006166 basicblock *b, *entryblock = NULL;
6167 basicblock **stack, **sp;
6168 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006169 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006170 b->b_startdepth = INT_MIN;
6171 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006172 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006173 }
6174 if (!entryblock)
6175 return 0;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006176 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
6177 if (!stack) {
6178 PyErr_NoMemory();
6179 return -1;
6180 }
6181
6182 sp = stack;
6183 stackdepth_push(&sp, entryblock, 0);
6184 while (sp != stack) {
6185 b = *--sp;
6186 int depth = b->b_startdepth;
6187 assert(depth >= 0);
6188 basicblock *next = b->b_next;
6189 for (int i = 0; i < b->b_iused; i++) {
6190 struct instr *instr = &b->b_instr[i];
6191 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
6192 if (effect == PY_INVALID_STACK_EFFECT) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01006193 PyErr_Format(PyExc_SystemError,
6194 "compiler stack_effect(opcode=%d, arg=%i) failed",
6195 instr->i_opcode, instr->i_oparg);
6196 return -1;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006197 }
6198 int new_depth = depth + effect;
6199 if (new_depth > maxdepth) {
6200 maxdepth = new_depth;
6201 }
6202 assert(depth >= 0); /* invalid code or bug in stackdepth() */
Mark Shannon582aaf12020-08-04 17:30:11 +01006203 if (is_jump(instr)) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006204 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
6205 assert(effect != PY_INVALID_STACK_EFFECT);
6206 int target_depth = depth + effect;
6207 if (target_depth > maxdepth) {
6208 maxdepth = target_depth;
6209 }
6210 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006211 stackdepth_push(&sp, instr->i_target, target_depth);
6212 }
6213 depth = new_depth;
6214 if (instr->i_opcode == JUMP_ABSOLUTE ||
6215 instr->i_opcode == JUMP_FORWARD ||
6216 instr->i_opcode == RETURN_VALUE ||
Mark Shannonfee55262019-11-21 09:11:43 +00006217 instr->i_opcode == RAISE_VARARGS ||
6218 instr->i_opcode == RERAISE)
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006219 {
6220 /* remaining code is dead */
6221 next = NULL;
6222 break;
6223 }
6224 }
6225 if (next != NULL) {
Mark Shannon266b4622020-11-17 19:30:14 +00006226 assert(b->b_nofallthrough == 0);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006227 stackdepth_push(&sp, next, depth);
6228 }
6229 }
6230 PyObject_Free(stack);
6231 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006232}
6233
6234static int
6235assemble_init(struct assembler *a, int nblocks, int firstlineno)
6236{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006237 memset(a, 0, sizeof(struct assembler));
Mark Shannon877df852020-11-12 09:43:29 +00006238 a->a_prevlineno = a->a_lineno = firstlineno;
Mark Shannonfd009e62020-11-13 12:53:53 +00006239 a->a_lnotab = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006240 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006241 if (a->a_bytecode == NULL) {
6242 goto error;
6243 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006244 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006245 if (a->a_lnotab == NULL) {
6246 goto error;
6247 }
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07006248 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006249 PyErr_NoMemory();
Mark Shannonfd009e62020-11-13 12:53:53 +00006250 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006251 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006252 return 1;
Mark Shannonfd009e62020-11-13 12:53:53 +00006253error:
6254 Py_XDECREF(a->a_bytecode);
6255 Py_XDECREF(a->a_lnotab);
6256 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006257}
6258
6259static void
6260assemble_free(struct assembler *a)
6261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006262 Py_XDECREF(a->a_bytecode);
6263 Py_XDECREF(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006264}
6265
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006266static int
6267blocksize(basicblock *b)
6268{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006269 int i;
6270 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006272 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006273 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006274 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006275}
6276
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006277static int
Mark Shannon877df852020-11-12 09:43:29 +00006278assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006279{
Mark Shannon877df852020-11-12 09:43:29 +00006280 Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006281 if (a->a_lnotab_off + 2 >= len) {
6282 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
6283 return 0;
6284 }
Pablo Galindo86e322f2021-01-30 13:54:22 +00006285 unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab);
6286 lnotab += a->a_lnotab_off;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006287 a->a_lnotab_off += 2;
Mark Shannon877df852020-11-12 09:43:29 +00006288 *lnotab++ = bdelta;
6289 *lnotab++ = ldelta;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006290 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006291}
6292
Mark Shannon877df852020-11-12 09:43:29 +00006293/* Appends a range to the end of the line number table. See
6294 * Objects/lnotab_notes.txt for the description of the line number table. */
6295
6296static int
6297assemble_line_range(struct assembler *a)
6298{
6299 int ldelta, bdelta;
6300 bdelta = (a->a_offset - a->a_lineno_start) * 2;
6301 if (bdelta == 0) {
6302 return 1;
6303 }
6304 if (a->a_lineno < 0) {
6305 ldelta = -128;
6306 }
6307 else {
6308 ldelta = a->a_lineno - a->a_prevlineno;
6309 a->a_prevlineno = a->a_lineno;
6310 while (ldelta > 127) {
6311 if (!assemble_emit_linetable_pair(a, 0, 127)) {
6312 return 0;
6313 }
6314 ldelta -= 127;
6315 }
6316 while (ldelta < -127) {
6317 if (!assemble_emit_linetable_pair(a, 0, -127)) {
6318 return 0;
6319 }
6320 ldelta += 127;
6321 }
6322 }
6323 assert(-128 <= ldelta && ldelta < 128);
6324 while (bdelta > 254) {
6325 if (!assemble_emit_linetable_pair(a, 254, ldelta)) {
6326 return 0;
6327 }
6328 ldelta = a->a_lineno < 0 ? -128 : 0;
6329 bdelta -= 254;
6330 }
6331 if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) {
6332 return 0;
6333 }
6334 a->a_lineno_start = a->a_offset;
6335 return 1;
6336}
6337
6338static int
6339assemble_lnotab(struct assembler *a, struct instr *i)
6340{
6341 if (i->i_lineno == a->a_lineno) {
6342 return 1;
6343 }
6344 if (!assemble_line_range(a)) {
6345 return 0;
6346 }
6347 a->a_lineno = i->i_lineno;
6348 return 1;
6349}
6350
6351
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006352/* assemble_emit()
6353 Extend the bytecode with a new instruction.
6354 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00006355*/
6356
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006357static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006358assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006359{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006360 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006361 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03006362 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006363
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006364 arg = i->i_oparg;
6365 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006366 if (i->i_lineno && !assemble_lnotab(a, i))
6367 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006368 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006369 if (len > PY_SSIZE_T_MAX / 2)
6370 return 0;
6371 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
6372 return 0;
6373 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03006374 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006375 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006376 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006377 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006378}
6379
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00006380static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006381assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006383 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006384 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006385 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00006386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006387 /* Compute the size of each block and fixup jump args.
6388 Replace block pointer with position in bytecode. */
6389 do {
6390 totsize = 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006391 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006392 bsize = blocksize(b);
6393 b->b_offset = totsize;
6394 totsize += bsize;
6395 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006396 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006397 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6398 bsize = b->b_offset;
6399 for (i = 0; i < b->b_iused; i++) {
6400 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006401 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006402 /* Relative jumps are computed relative to
6403 the instruction pointer after fetching
6404 the jump instruction.
6405 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006406 bsize += isize;
Mark Shannon582aaf12020-08-04 17:30:11 +01006407 if (is_jump(instr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006408 instr->i_oparg = instr->i_target->b_offset;
Mark Shannon582aaf12020-08-04 17:30:11 +01006409 if (is_relative_jump(instr)) {
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006410 instr->i_oparg -= bsize;
6411 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03006412 instr->i_oparg *= sizeof(_Py_CODEUNIT);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006413 if (instrsize(instr->i_oparg) != isize) {
6414 extended_arg_recompile = 1;
6415 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006416 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006417 }
6418 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00006419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006420 /* XXX: This is an awful hack that could hurt performance, but
6421 on the bright side it should work until we come up
6422 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006424 The issue is that in the first loop blocksize() is called
6425 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006426 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006427 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006429 So we loop until we stop seeing new EXTENDED_ARGs.
6430 The only EXTENDED_ARGs that could be popping up are
6431 ones in jump instructions. So this should converge
6432 fairly quickly.
6433 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006434 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00006435}
6436
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006437static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01006438dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006440 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006441 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006443 tuple = PyTuple_New(size);
6444 if (tuple == NULL)
6445 return NULL;
6446 while (PyDict_Next(dict, &pos, &k, &v)) {
6447 i = PyLong_AS_LONG(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006448 Py_INCREF(k);
6449 assert((i - offset) < size);
6450 assert((i - offset) >= 0);
6451 PyTuple_SET_ITEM(tuple, i - offset, k);
6452 }
6453 return tuple;
6454}
6455
6456static PyObject *
6457consts_dict_keys_inorder(PyObject *dict)
6458{
6459 PyObject *consts, *k, *v;
6460 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
6461
6462 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
6463 if (consts == NULL)
6464 return NULL;
6465 while (PyDict_Next(dict, &pos, &k, &v)) {
6466 i = PyLong_AS_LONG(v);
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +03006467 /* The keys of the dictionary can be tuples wrapping a contant.
6468 * (see compiler_add_o and _PyCode_ConstantKey). In that case
6469 * the object we want is always second. */
6470 if (PyTuple_CheckExact(k)) {
6471 k = PyTuple_GET_ITEM(k, 1);
6472 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006473 Py_INCREF(k);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006474 assert(i < size);
6475 assert(i >= 0);
6476 PyList_SET_ITEM(consts, i, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006477 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006478 return consts;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006479}
6480
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006481static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006482compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006484 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006485 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006486 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04006487 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006488 if (ste->ste_nested)
6489 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07006490 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006491 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07006492 if (!ste->ste_generator && ste->ste_coroutine)
6493 flags |= CO_COROUTINE;
6494 if (ste->ste_generator && ste->ste_coroutine)
6495 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006496 if (ste->ste_varargs)
6497 flags |= CO_VARARGS;
6498 if (ste->ste_varkeywords)
6499 flags |= CO_VARKEYWORDS;
6500 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006502 /* (Only) inherit compilerflags in PyCF_MASK */
6503 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006504
Pablo Galindo90235812020-03-15 04:29:22 +00006505 if ((IS_TOP_LEVEL_AWAIT(c)) &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07006506 ste->ste_coroutine &&
6507 !ste->ste_generator) {
6508 flags |= CO_COROUTINE;
6509 }
6510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006511 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00006512}
6513
Inada Naokibdb941b2021-02-10 09:20:42 +09006514// Merge *obj* with constant cache.
INADA Naokic2e16072018-11-26 21:23:22 +09006515// Unlike merge_consts_recursive(), this function doesn't work recursively.
6516static int
Inada Naokibdb941b2021-02-10 09:20:42 +09006517merge_const_one(struct compiler *c, PyObject **obj)
INADA Naokic2e16072018-11-26 21:23:22 +09006518{
Inada Naokibdb941b2021-02-10 09:20:42 +09006519 PyObject *key = _PyCode_ConstantKey(*obj);
INADA Naokic2e16072018-11-26 21:23:22 +09006520 if (key == NULL) {
6521 return 0;
6522 }
6523
6524 // t is borrowed reference
6525 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
6526 Py_DECREF(key);
6527 if (t == NULL) {
6528 return 0;
6529 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006530 if (t == key) { // obj is new constant.
INADA Naokic2e16072018-11-26 21:23:22 +09006531 return 1;
6532 }
6533
Inada Naokibdb941b2021-02-10 09:20:42 +09006534 if (PyTuple_CheckExact(t)) {
6535 // t is still borrowed reference
6536 t = PyTuple_GET_ITEM(t, 1);
6537 }
6538
6539 Py_INCREF(t);
6540 Py_DECREF(*obj);
6541 *obj = t;
INADA Naokic2e16072018-11-26 21:23:22 +09006542 return 1;
6543}
6544
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006545static PyCodeObject *
Mark Shannon6e8128f2020-07-30 10:03:00 +01006546makecode(struct compiler *c, struct assembler *a, PyObject *consts)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006547{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006548 PyCodeObject *co = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006549 PyObject *names = NULL;
6550 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006551 PyObject *name = NULL;
6552 PyObject *freevars = NULL;
6553 PyObject *cellvars = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006554 Py_ssize_t nlocals;
6555 int nlocals_int;
6556 int flags;
Pablo Galindocd74e662019-06-01 18:08:04 +01006557 int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006559 names = dict_keys_inorder(c->u->u_names, 0);
6560 varnames = dict_keys_inorder(c->u->u_varnames, 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006561 if (!names || !varnames) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006562 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006563 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006564 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
6565 if (!cellvars)
6566 goto error;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006567 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006568 if (!freevars)
6569 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006570
Inada Naokibdb941b2021-02-10 09:20:42 +09006571 if (!merge_const_one(c, &names) ||
6572 !merge_const_one(c, &varnames) ||
6573 !merge_const_one(c, &cellvars) ||
6574 !merge_const_one(c, &freevars))
INADA Naokic2e16072018-11-26 21:23:22 +09006575 {
6576 goto error;
6577 }
6578
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006579 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01006580 assert(nlocals < INT_MAX);
6581 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
6582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006583 flags = compute_code_flags(c);
6584 if (flags < 0)
6585 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006586
Mark Shannon6e8128f2020-07-30 10:03:00 +01006587 consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
6588 if (consts == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006589 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006590 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006591 if (!merge_const_one(c, &consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006592 Py_DECREF(consts);
INADA Naokic2e16072018-11-26 21:23:22 +09006593 goto error;
6594 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006595
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01006596 posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int);
Pablo Galindocd74e662019-06-01 18:08:04 +01006597 posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +01006598 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006599 maxdepth = stackdepth(c);
6600 if (maxdepth < 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006601 Py_DECREF(consts);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006602 goto error;
6603 }
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006604 co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
Mark Shannon13bc1392020-01-23 09:25:17 +00006605 posonlyargcount, kwonlyargcount, nlocals_int,
Mark Shannon6e8128f2020-07-30 10:03:00 +01006606 maxdepth, flags, a->a_bytecode, consts, names,
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006607 varnames, freevars, cellvars, c->c_filename,
6608 c->u->u_name, c->u->u_firstlineno, a->a_lnotab);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006609 Py_DECREF(consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006610 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006611 Py_XDECREF(names);
6612 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006613 Py_XDECREF(name);
6614 Py_XDECREF(freevars);
6615 Py_XDECREF(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006616 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006617}
6618
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006619
6620/* For debugging purposes only */
6621#if 0
6622static void
6623dump_instr(const struct instr *i)
6624{
Mark Shannon582aaf12020-08-04 17:30:11 +01006625 const char *jrel = (is_relative_jump(instr)) ? "jrel " : "";
6626 const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : "";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006627 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006629 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006630 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006631 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006632 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006633 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
6634 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006635}
6636
6637static void
6638dump_basicblock(const basicblock *b)
6639{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006640 const char *b_return = b->b_return ? "return " : "";
Pablo Galindo60eb9f12020-06-28 01:55:47 +01006641 fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n",
6642 b->b_iused, b->b_startdepth, b->b_offset, b_return);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006643 if (b->b_instr) {
6644 int i;
6645 for (i = 0; i < b->b_iused; i++) {
6646 fprintf(stderr, " [%02d] ", i);
6647 dump_instr(b->b_instr + i);
6648 }
6649 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006650}
6651#endif
6652
Mark Shannon5977a792020-12-02 13:31:40 +00006653
6654static int
6655normalize_basic_block(basicblock *bb);
6656
Mark Shannon6e8128f2020-07-30 10:03:00 +01006657static int
6658optimize_cfg(struct assembler *a, PyObject *consts);
6659
Mark Shannon5977a792020-12-02 13:31:40 +00006660static int
6661ensure_exits_have_lineno(struct compiler *c);
6662
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006663static PyCodeObject *
6664assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006665{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006666 basicblock *b, *entryblock;
6667 struct assembler a;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006668 int j, nblocks;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006669 PyCodeObject *co = NULL;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006670 PyObject *consts = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006672 /* Make sure every block that falls off the end returns None.
6673 XXX NEXT_BLOCK() isn't quite right, because if the last
6674 block ends with a jump or return b_next shouldn't set.
6675 */
6676 if (!c->u->u_curblock->b_return) {
Mark Shannon877df852020-11-12 09:43:29 +00006677 c->u->u_lineno = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006678 if (addNone)
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006679 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006680 ADDOP(c, RETURN_VALUE);
6681 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006682
Mark Shannon5977a792020-12-02 13:31:40 +00006683 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
6684 if (normalize_basic_block(b)) {
Alex Henrie503627f2021-03-02 03:20:25 -07006685 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00006686 }
6687 }
6688
6689 if (ensure_exits_have_lineno(c)) {
Alex Henrie503627f2021-03-02 03:20:25 -07006690 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00006691 }
6692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006693 nblocks = 0;
6694 entryblock = NULL;
6695 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6696 nblocks++;
6697 entryblock = b;
6698 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006700 /* Set firstlineno if it wasn't explicitly set. */
6701 if (!c->u->u_firstlineno) {
Ned Deilydc35cda2016-08-17 17:18:33 -04006702 if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006703 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
Mark Shannon877df852020-11-12 09:43:29 +00006704 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006705 c->u->u_firstlineno = 1;
6706 }
Mark Shannon5977a792020-12-02 13:31:40 +00006707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006708 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
6709 goto error;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006710 a.a_entry = entryblock;
6711 a.a_nblocks = nblocks;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006712
Mark Shannon6e8128f2020-07-30 10:03:00 +01006713 consts = consts_dict_keys_inorder(c->u->u_consts);
6714 if (consts == NULL) {
6715 goto error;
6716 }
6717 if (optimize_cfg(&a, consts)) {
6718 goto error;
6719 }
6720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006721 /* Can't modify the bytecode after computing jump offsets. */
6722 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00006723
Mark Shannoncc75ab72020-11-12 19:49:33 +00006724 /* Emit code. */
6725 for(b = entryblock; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006726 for (j = 0; j < b->b_iused; j++)
6727 if (!assemble_emit(&a, &b->b_instr[j]))
6728 goto error;
6729 }
Mark Shannon877df852020-11-12 09:43:29 +00006730 if (!assemble_line_range(&a)) {
6731 return 0;
6732 }
6733 /* Emit sentinel at end of line number table */
6734 if (!assemble_emit_linetable_pair(&a, 255, -128)) {
6735 goto error;
6736 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00006737
Inada Naokibdb941b2021-02-10 09:20:42 +09006738 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006739 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09006740 }
6741 if (!merge_const_one(c, &a.a_lnotab)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006742 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09006743 }
6744 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) {
6745 goto error;
6746 }
6747 if (!merge_const_one(c, &a.a_bytecode)) {
6748 goto error;
6749 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006750
Mark Shannon6e8128f2020-07-30 10:03:00 +01006751 co = makecode(c, &a, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006752 error:
Mark Shannon6e8128f2020-07-30 10:03:00 +01006753 Py_XDECREF(consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006754 assemble_free(&a);
6755 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006756}
Georg Brandl8334fd92010-12-04 10:26:46 +00006757
6758#undef PyAST_Compile
Benjamin Petersone5024512018-09-12 12:06:42 -07006759PyCodeObject *
Georg Brandl8334fd92010-12-04 10:26:46 +00006760PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
6761 PyArena *arena)
6762{
6763 return PyAST_CompileEx(mod, filename, flags, -1, arena);
6764}
Mark Shannon6e8128f2020-07-30 10:03:00 +01006765
6766
6767/* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
6768 with LOAD_CONST (c1, c2, ... cn).
6769 The consts table must still be in list form so that the
6770 new constant (c1, c2, ... cn) can be appended.
6771 Called with codestr pointing to the first LOAD_CONST.
6772*/
6773static int
6774fold_tuple_on_constants(struct instr *inst,
6775 int n, PyObject *consts)
6776{
6777 /* Pre-conditions */
6778 assert(PyList_CheckExact(consts));
6779 assert(inst[n].i_opcode == BUILD_TUPLE);
6780 assert(inst[n].i_oparg == n);
6781
6782 for (int i = 0; i < n; i++) {
6783 if (inst[i].i_opcode != LOAD_CONST) {
6784 return 0;
6785 }
6786 }
6787
6788 /* Buildup new tuple of constants */
6789 PyObject *newconst = PyTuple_New(n);
6790 if (newconst == NULL) {
6791 return -1;
6792 }
6793 for (int i = 0; i < n; i++) {
6794 int arg = inst[i].i_oparg;
6795 PyObject *constant = PyList_GET_ITEM(consts, arg);
6796 Py_INCREF(constant);
6797 PyTuple_SET_ITEM(newconst, i, constant);
6798 }
6799 Py_ssize_t index = PyList_GET_SIZE(consts);
Victor Stinner71f2ff42020-09-23 14:06:55 +02006800 if ((size_t)index >= (size_t)INT_MAX - 1) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006801 Py_DECREF(newconst);
6802 PyErr_SetString(PyExc_OverflowError, "too many constants");
6803 return -1;
6804 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006805 if (PyList_Append(consts, newconst)) {
6806 Py_DECREF(newconst);
6807 return -1;
6808 }
6809 Py_DECREF(newconst);
6810 for (int i = 0; i < n; i++) {
6811 inst[i].i_opcode = NOP;
6812 }
6813 inst[n].i_opcode = LOAD_CONST;
Victor Stinner71f2ff42020-09-23 14:06:55 +02006814 inst[n].i_oparg = (int)index;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006815 return 0;
6816}
6817
Mark Shannon28b75c82020-12-23 11:43:10 +00006818
6819static int
6820eliminate_jump_to_jump(basicblock *bb, int opcode) {
6821 assert (bb->b_iused > 0);
6822 struct instr *inst = &bb->b_instr[bb->b_iused-1];
6823 assert (is_jump(inst));
6824 assert (inst->i_target->b_iused > 0);
6825 struct instr *target = &inst->i_target->b_instr[0];
6826 if (inst->i_target == target->i_target) {
6827 /* Nothing to do */
6828 return 0;
6829 }
6830 int lineno = target->i_lineno;
6831 if (add_jump_to_block(bb, opcode, lineno, target->i_target) == 0) {
6832 return -1;
6833 }
6834 assert (bb->b_iused >= 2);
6835 bb->b_instr[bb->b_iused-2].i_opcode = NOP;
6836 return 0;
6837}
6838
Mark Shannoncc75ab72020-11-12 19:49:33 +00006839/* Maximum size of basic block that should be copied in optimizer */
6840#define MAX_COPY_SIZE 4
Mark Shannon6e8128f2020-07-30 10:03:00 +01006841
6842/* Optimization */
6843static int
6844optimize_basic_block(basicblock *bb, PyObject *consts)
6845{
6846 assert(PyList_CheckExact(consts));
6847 struct instr nop;
6848 nop.i_opcode = NOP;
6849 struct instr *target;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006850 for (int i = 0; i < bb->b_iused; i++) {
6851 struct instr *inst = &bb->b_instr[i];
6852 int oparg = inst->i_oparg;
6853 int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01006854 if (is_jump(inst)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006855 /* Skip over empty basic blocks. */
6856 while (inst->i_target->b_iused == 0) {
6857 inst->i_target = inst->i_target->b_next;
6858 }
6859 target = &inst->i_target->b_instr[0];
6860 }
6861 else {
6862 target = &nop;
6863 }
6864 switch (inst->i_opcode) {
Mark Shannon266b4622020-11-17 19:30:14 +00006865 /* Remove LOAD_CONST const; conditional jump */
Mark Shannon6e8128f2020-07-30 10:03:00 +01006866 case LOAD_CONST:
Mark Shannon266b4622020-11-17 19:30:14 +00006867 {
6868 PyObject* cnt;
6869 int is_true;
6870 int jump_if_true;
6871 switch(nextop) {
6872 case POP_JUMP_IF_FALSE:
6873 case POP_JUMP_IF_TRUE:
6874 cnt = PyList_GET_ITEM(consts, oparg);
6875 is_true = PyObject_IsTrue(cnt);
6876 if (is_true == -1) {
6877 goto error;
6878 }
6879 inst->i_opcode = NOP;
6880 jump_if_true = nextop == POP_JUMP_IF_TRUE;
6881 if (is_true == jump_if_true) {
6882 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
6883 bb->b_nofallthrough = 1;
6884 }
6885 else {
6886 bb->b_instr[i+1].i_opcode = NOP;
6887 }
6888 break;
6889 case JUMP_IF_FALSE_OR_POP:
6890 case JUMP_IF_TRUE_OR_POP:
6891 cnt = PyList_GET_ITEM(consts, oparg);
6892 is_true = PyObject_IsTrue(cnt);
6893 if (is_true == -1) {
6894 goto error;
6895 }
6896 jump_if_true = nextop == JUMP_IF_TRUE_OR_POP;
6897 if (is_true == jump_if_true) {
6898 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
6899 bb->b_nofallthrough = 1;
6900 }
6901 else {
6902 inst->i_opcode = NOP;
6903 bb->b_instr[i+1].i_opcode = NOP;
6904 }
6905 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006906 }
6907 break;
Mark Shannon266b4622020-11-17 19:30:14 +00006908 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006909
6910 /* Try to fold tuples of constants.
6911 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
6912 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
6913 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
6914 case BUILD_TUPLE:
6915 if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
6916 switch(oparg) {
6917 case 1:
6918 inst->i_opcode = NOP;
6919 bb->b_instr[i+1].i_opcode = NOP;
6920 break;
6921 case 2:
6922 inst->i_opcode = ROT_TWO;
6923 bb->b_instr[i+1].i_opcode = NOP;
6924 break;
6925 case 3:
6926 inst->i_opcode = ROT_THREE;
6927 bb->b_instr[i+1].i_opcode = ROT_TWO;
6928 }
6929 break;
6930 }
6931 if (i >= oparg) {
6932 if (fold_tuple_on_constants(inst-oparg, oparg, consts)) {
6933 goto error;
6934 }
6935 }
6936 break;
6937
6938 /* Simplify conditional jump to conditional jump where the
6939 result of the first test implies the success of a similar
6940 test or the failure of the opposite test.
6941 Arises in code like:
6942 "a and b or c"
6943 "(a and b) and c"
6944 "(a or b) or c"
6945 "(a or b) and c"
6946 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
6947 --> x:JUMP_IF_FALSE_OR_POP z
6948 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
6949 --> x:POP_JUMP_IF_FALSE y+1
6950 where y+1 is the instruction following the second test.
6951 */
6952 case JUMP_IF_FALSE_OR_POP:
6953 switch(target->i_opcode) {
6954 case POP_JUMP_IF_FALSE:
Mark Shannon28b75c82020-12-23 11:43:10 +00006955 if (inst->i_lineno == target->i_lineno) {
6956 *inst = *target;
6957 i--;
6958 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006959 break;
6960 case JUMP_ABSOLUTE:
6961 case JUMP_FORWARD:
6962 case JUMP_IF_FALSE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00006963 if (inst->i_lineno == target->i_lineno &&
6964 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00006965 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00006966 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00006967 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006968 break;
6969 case JUMP_IF_TRUE_OR_POP:
6970 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00006971 if (inst->i_lineno == target->i_lineno) {
6972 inst->i_opcode = POP_JUMP_IF_FALSE;
6973 inst->i_target = inst->i_target->b_next;
6974 --i;
6975 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006976 break;
6977 }
6978 break;
6979
6980 case JUMP_IF_TRUE_OR_POP:
6981 switch(target->i_opcode) {
6982 case POP_JUMP_IF_TRUE:
Mark Shannon28b75c82020-12-23 11:43:10 +00006983 if (inst->i_lineno == target->i_lineno) {
6984 *inst = *target;
6985 i--;
6986 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006987 break;
6988 case JUMP_ABSOLUTE:
6989 case JUMP_FORWARD:
6990 case JUMP_IF_TRUE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00006991 if (inst->i_lineno == target->i_lineno &&
6992 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00006993 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00006994 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00006995 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006996 break;
6997 case JUMP_IF_FALSE_OR_POP:
6998 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00006999 if (inst->i_lineno == target->i_lineno) {
7000 inst->i_opcode = POP_JUMP_IF_TRUE;
7001 inst->i_target = inst->i_target->b_next;
7002 --i;
7003 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007004 break;
7005 }
7006 break;
7007
7008 case POP_JUMP_IF_FALSE:
7009 switch(target->i_opcode) {
7010 case JUMP_ABSOLUTE:
7011 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007012 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00007013 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007014 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007015 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007016 break;
7017 }
7018 break;
7019
7020 case POP_JUMP_IF_TRUE:
7021 switch(target->i_opcode) {
7022 case JUMP_ABSOLUTE:
7023 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007024 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00007025 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007026 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007027 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007028 break;
7029 }
7030 break;
7031
7032 case JUMP_ABSOLUTE:
7033 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007034 assert (i == bb->b_iused-1);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007035 switch(target->i_opcode) {
7036 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007037 if (eliminate_jump_to_jump(bb, inst->i_opcode)) {
7038 goto error;
Mark Shannon266b4622020-11-17 19:30:14 +00007039 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007040 break;
Mark Shannon28b75c82020-12-23 11:43:10 +00007041
Mark Shannon6e8128f2020-07-30 10:03:00 +01007042 case JUMP_ABSOLUTE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007043 if (eliminate_jump_to_jump(bb, JUMP_ABSOLUTE)) {
7044 goto error;
Mark Shannon266b4622020-11-17 19:30:14 +00007045 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007046 break;
Mark Shannon28b75c82020-12-23 11:43:10 +00007047 default:
7048 if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) {
7049 basicblock *to_copy = inst->i_target;
7050 inst->i_opcode = NOP;
7051 for (i = 0; i < to_copy->b_iused; i++) {
7052 int index = compiler_next_instr(bb);
7053 if (index < 0) {
7054 return -1;
7055 }
7056 bb->b_instr[index] = to_copy->b_instr[i];
7057 }
7058 bb->b_exit = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007059 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007060 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007061 }
7062 }
7063 return 0;
7064error:
7065 return -1;
7066}
7067
7068
7069static void
Mark Shannon1659ad12021-01-13 15:05:04 +00007070clean_basic_block(basicblock *bb, int prev_lineno) {
7071 /* Remove NOPs when legal to do so. */
Mark Shannon6e8128f2020-07-30 10:03:00 +01007072 int dest = 0;
7073 for (int src = 0; src < bb->b_iused; src++) {
Mark Shannon877df852020-11-12 09:43:29 +00007074 int lineno = bb->b_instr[src].i_lineno;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007075 if (bb->b_instr[src].i_opcode == NOP) {
Mark Shannon266b4622020-11-17 19:30:14 +00007076 /* Eliminate no-op if it doesn't have a line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007077 if (lineno < 0) {
7078 continue;
7079 }
Mark Shannon266b4622020-11-17 19:30:14 +00007080 /* or, if the previous instruction had the same line number. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007081 if (prev_lineno == lineno) {
7082 continue;
7083 }
Mark Shannon266b4622020-11-17 19:30:14 +00007084 /* or, if the next instruction has same line number or no line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007085 if (src < bb->b_iused - 1) {
7086 int next_lineno = bb->b_instr[src+1].i_lineno;
7087 if (next_lineno < 0 || next_lineno == lineno) {
7088 bb->b_instr[src+1].i_lineno = lineno;
7089 continue;
Mark Shannon877df852020-11-12 09:43:29 +00007090 }
7091 }
Mark Shannon266b4622020-11-17 19:30:14 +00007092 else {
7093 basicblock* next = bb->b_next;
7094 while (next && next->b_iused == 0) {
7095 next = next->b_next;
7096 }
7097 /* or if last instruction in BB and next BB has same line number */
7098 if (next) {
7099 if (lineno == next->b_instr[0].i_lineno) {
7100 continue;
7101 }
7102 }
7103 }
7104
Mark Shannon6e8128f2020-07-30 10:03:00 +01007105 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007106 if (dest != src) {
7107 bb->b_instr[dest] = bb->b_instr[src];
7108 }
7109 dest++;
7110 prev_lineno = lineno;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007111 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007112 assert(dest <= bb->b_iused);
7113 bb->b_iused = dest;
7114}
7115
Mark Shannon266b4622020-11-17 19:30:14 +00007116static int
7117normalize_basic_block(basicblock *bb) {
7118 /* Mark blocks as exit and/or nofallthrough.
7119 Raise SystemError if CFG is malformed. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007120 for (int i = 0; i < bb->b_iused; i++) {
7121 switch(bb->b_instr[i].i_opcode) {
7122 case RETURN_VALUE:
7123 case RAISE_VARARGS:
7124 case RERAISE:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007125 bb->b_exit = 1;
Mark Shannon5977a792020-12-02 13:31:40 +00007126 bb->b_nofallthrough = 1;
7127 break;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007128 case JUMP_ABSOLUTE:
7129 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007130 bb->b_nofallthrough = 1;
Mark Shannon266b4622020-11-17 19:30:14 +00007131 /* fall through */
7132 case POP_JUMP_IF_FALSE:
7133 case POP_JUMP_IF_TRUE:
7134 case JUMP_IF_FALSE_OR_POP:
7135 case JUMP_IF_TRUE_OR_POP:
Mark Shannon5977a792020-12-02 13:31:40 +00007136 case FOR_ITER:
Mark Shannon266b4622020-11-17 19:30:14 +00007137 if (i != bb->b_iused-1) {
7138 PyErr_SetString(PyExc_SystemError, "malformed control flow graph.");
7139 return -1;
7140 }
Mark Shannon5977a792020-12-02 13:31:40 +00007141 /* Skip over empty basic blocks. */
7142 while (bb->b_instr[i].i_target->b_iused == 0) {
7143 bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next;
7144 }
7145
Mark Shannoncc75ab72020-11-12 19:49:33 +00007146 }
7147 }
Mark Shannon266b4622020-11-17 19:30:14 +00007148 return 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007149}
7150
Mark Shannon6e8128f2020-07-30 10:03:00 +01007151static int
7152mark_reachable(struct assembler *a) {
7153 basicblock **stack, **sp;
7154 sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks);
7155 if (stack == NULL) {
7156 return -1;
7157 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007158 a->a_entry->b_predecessors = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007159 *sp++ = a->a_entry;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007160 while (sp > stack) {
7161 basicblock *b = *(--sp);
Mark Shannon3bd60352021-01-13 12:05:43 +00007162 if (b->b_next && !b->b_nofallthrough) {
7163 if (b->b_next->b_predecessors == 0) {
7164 *sp++ = b->b_next;
7165 }
7166 b->b_next->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007167 }
7168 for (int i = 0; i < b->b_iused; i++) {
7169 basicblock *target;
Mark Shannon582aaf12020-08-04 17:30:11 +01007170 if (is_jump(&b->b_instr[i])) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007171 target = b->b_instr[i].i_target;
Mark Shannon3bd60352021-01-13 12:05:43 +00007172 if (target->b_predecessors == 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007173 *sp++ = target;
7174 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007175 target->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007176 }
7177 }
7178 }
7179 PyObject_Free(stack);
7180 return 0;
7181}
7182
Mark Shannon3bd60352021-01-13 12:05:43 +00007183static void
7184eliminate_empty_basic_blocks(basicblock *entry) {
7185 /* Eliminate empty blocks */
7186 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7187 basicblock *next = b->b_next;
7188 if (next) {
7189 while (next->b_iused == 0 && next->b_next) {
7190 next = next->b_next;
7191 }
7192 b->b_next = next;
7193 }
7194 }
7195 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7196 if (b->b_iused == 0) {
7197 continue;
7198 }
7199 if (is_jump(&b->b_instr[b->b_iused-1])) {
7200 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7201 while (target->b_iused == 0) {
7202 target = target->b_next;
7203 }
7204 b->b_instr[b->b_iused-1].i_target = target;
7205 }
7206 }
7207}
7208
7209
Mark Shannon5977a792020-12-02 13:31:40 +00007210/* If an instruction has no line number, but it's predecessor in the BB does,
Mark Shannon3bd60352021-01-13 12:05:43 +00007211 * then copy the line number. If a successor block has no line number, and only
7212 * one predecessor, then inherit the line number.
7213 * This ensures that all exit blocks (with one predecessor) receive a line number.
7214 * Also reduces the size of the line number table,
Mark Shannon5977a792020-12-02 13:31:40 +00007215 * but has no impact on the generated line number events.
7216 */
7217static void
Mark Shannon3bd60352021-01-13 12:05:43 +00007218propogate_line_numbers(struct assembler *a) {
Mark Shannon5977a792020-12-02 13:31:40 +00007219 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007220 if (b->b_iused == 0) {
7221 continue;
7222 }
Mark Shannon5977a792020-12-02 13:31:40 +00007223 int prev_lineno = -1;
7224 for (int i = 0; i < b->b_iused; i++) {
7225 if (b->b_instr[i].i_lineno < 0) {
7226 b->b_instr[i].i_lineno = prev_lineno;
7227 }
7228 else {
7229 prev_lineno = b->b_instr[i].i_lineno;
7230 }
7231 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007232 if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) {
7233 assert(b->b_next->b_iused);
7234 if (b->b_next->b_instr[0].i_lineno < 0) {
7235 b->b_next->b_instr[0].i_lineno = prev_lineno;
7236 }
7237 }
7238 if (is_jump(&b->b_instr[b->b_iused-1])) {
7239 switch (b->b_instr[b->b_iused-1].i_opcode) {
7240 /* Note: Only actual jumps, not exception handlers */
7241 case SETUP_ASYNC_WITH:
7242 case SETUP_WITH:
7243 case SETUP_FINALLY:
7244 continue;
7245 }
7246 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7247 if (target->b_predecessors == 1) {
7248 if (target->b_instr[0].i_lineno < 0) {
7249 target->b_instr[0].i_lineno = prev_lineno;
7250 }
7251 }
7252 }
Mark Shannon5977a792020-12-02 13:31:40 +00007253 }
7254}
7255
7256/* Perform optimizations on a control flow graph.
Mark Shannon6e8128f2020-07-30 10:03:00 +01007257 The consts object should still be in list form to allow new constants
7258 to be appended.
7259
7260 All transformations keep the code size the same or smaller.
7261 For those that reduce size, the gaps are initially filled with
7262 NOPs. Later those NOPs are removed.
7263*/
7264
7265static int
7266optimize_cfg(struct assembler *a, PyObject *consts)
7267{
Mark Shannoncc75ab72020-11-12 19:49:33 +00007268 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannoncc75ab72020-11-12 19:49:33 +00007269 if (optimize_basic_block(b, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007270 return -1;
7271 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007272 clean_basic_block(b, -1);
Mark Shannon3bd60352021-01-13 12:05:43 +00007273 assert(b->b_predecessors == 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007274 }
7275 if (mark_reachable(a)) {
7276 return -1;
7277 }
7278 /* Delete unreachable instructions */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007279 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007280 if (b->b_predecessors == 0) {
Mark Shannoncc75ab72020-11-12 19:49:33 +00007281 b->b_iused = 0;
Om Gc71581c2020-12-16 17:48:05 +05307282 b->b_nofallthrough = 0;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007283 }
7284 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007285 basicblock *pred = NULL;
7286 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7287 int prev_lineno = -1;
7288 if (pred && pred->b_iused) {
7289 prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno;
7290 }
7291 clean_basic_block(b, prev_lineno);
7292 pred = b->b_nofallthrough ? NULL : b;
7293 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007294 eliminate_empty_basic_blocks(a->a_entry);
Om Gc71581c2020-12-16 17:48:05 +05307295 /* Delete jump instructions made redundant by previous step. If a non-empty
7296 block ends with a jump instruction, check if the next non-empty block
7297 reached through normal flow control is the target of that jump. If it
7298 is, then the jump instruction is redundant and can be deleted.
7299 */
Mark Shannon3bd60352021-01-13 12:05:43 +00007300 int maybe_empty_blocks = 0;
Om Gc71581c2020-12-16 17:48:05 +05307301 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7302 if (b->b_iused > 0) {
7303 struct instr *b_last_instr = &b->b_instr[b->b_iused - 1];
Mark Shannon802b6452021-02-02 14:59:15 +00007304 if (b_last_instr->i_opcode == JUMP_ABSOLUTE ||
Om Gc71581c2020-12-16 17:48:05 +05307305 b_last_instr->i_opcode == JUMP_FORWARD) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007306 if (b_last_instr->i_target == b->b_next) {
7307 assert(b->b_next->b_iused);
Om Gc71581c2020-12-16 17:48:05 +05307308 b->b_nofallthrough = 0;
Mark Shannon802b6452021-02-02 14:59:15 +00007309 b_last_instr->i_opcode = NOP;
7310 clean_basic_block(b, -1);
7311 maybe_empty_blocks = 1;
Om Gc71581c2020-12-16 17:48:05 +05307312 }
7313 }
7314 }
7315 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007316 if (maybe_empty_blocks) {
7317 eliminate_empty_basic_blocks(a->a_entry);
7318 }
7319 propogate_line_numbers(a);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007320 return 0;
7321}
7322
Mark Shannon5977a792020-12-02 13:31:40 +00007323static inline int
7324is_exit_without_lineno(basicblock *b) {
7325 return b->b_exit && b->b_instr[0].i_lineno < 0;
7326}
7327
7328/* PEP 626 mandates that the f_lineno of a frame is correct
7329 * after a frame terminates. It would be prohibitively expensive
7330 * to continuously update the f_lineno field at runtime,
7331 * so we make sure that all exiting instruction (raises and returns)
7332 * have a valid line number, allowing us to compute f_lineno lazily.
7333 * We can do this by duplicating the exit blocks without line number
7334 * so that none have more than one predecessor. We can then safely
7335 * copy the line number from the sole predecessor block.
7336 */
7337static int
7338ensure_exits_have_lineno(struct compiler *c)
7339{
Mark Shannoneaccc122020-12-04 15:22:12 +00007340 basicblock *entry = NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00007341 /* Copy all exit blocks without line number that are targets of a jump.
7342 */
7343 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7344 if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) {
7345 switch (b->b_instr[b->b_iused-1].i_opcode) {
7346 /* Note: Only actual jumps, not exception handlers */
7347 case SETUP_ASYNC_WITH:
7348 case SETUP_WITH:
7349 case SETUP_FINALLY:
7350 continue;
7351 }
7352 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7353 if (is_exit_without_lineno(target)) {
7354 basicblock *new_target = compiler_copy_block(c, target);
7355 if (new_target == NULL) {
7356 return -1;
7357 }
7358 new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7359 b->b_instr[b->b_iused-1].i_target = new_target;
7360 }
7361 }
Mark Shannoneaccc122020-12-04 15:22:12 +00007362 entry = b;
7363 }
7364 assert(entry != NULL);
7365 if (is_exit_without_lineno(entry)) {
7366 entry->b_instr[0].i_lineno = c->u->u_firstlineno;
Mark Shannon5977a792020-12-02 13:31:40 +00007367 }
Mark Shannonee9f98d2021-01-05 12:04:10 +00007368 /* Eliminate empty blocks */
7369 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7370 while (b->b_next && b->b_next->b_iused == 0) {
7371 b->b_next = b->b_next->b_next;
7372 }
7373 }
Mark Shannon5977a792020-12-02 13:31:40 +00007374 /* Any remaining reachable exit blocks without line number can only be reached by
7375 * fall through, and thus can only have a single predecessor */
7376 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7377 if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) {
7378 if (is_exit_without_lineno(b->b_next)) {
7379 assert(b->b_next->b_iused > 0);
7380 b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7381 }
7382 }
7383 }
7384 return 0;
7385}
7386
7387
Mark Shannon6e8128f2020-07-30 10:03:00 +01007388/* Retained for API compatibility.
7389 * Optimization is now done in optimize_cfg */
7390
7391PyObject *
7392PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
7393 PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
7394{
7395 Py_INCREF(code);
7396 return code;
7397}