blob: 49a713b2b0c36d964048abfa60435083715db4b3 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file compiles an abstract syntax tree (AST) into Python bytecode.
3 *
Victor Stinnera81fca62021-03-24 00:51:50 +01004 * The primary entry point is _PyAST_Compile(), which returns a
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005 * PyCodeObject. The compiler makes several passes to build the code
6 * object:
7 * 1. Checks for future statements. See future.c
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00008 * 2. Builds a symbol table. See symtable.c.
Thomas Wouters89f507f2006-12-13 04:49:30 +00009 * 3. Generate code for basic blocks. See compiler_mod() in this file.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000010 * 4. Assemble the basic blocks into final code. See assemble() in
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000011 * this file.
Mark Shannon6e8128f2020-07-30 10:03:00 +010012 * 5. Optimize the byte code (peephole optimizations).
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013 *
14 * Note that compiler_mod() suggests module, but the module ast type
15 * (mod_ty) has cases for expressions and interactive statements.
Nick Coghlan944d3eb2005-11-16 12:46:55 +000016 *
Jeremy Hyltone9357b22006-03-01 15:47:05 +000017 * CAUTION: The VISIT_* macros abort the current function when they
18 * encounter a problem. So don't invoke them when there is memory
19 * which needs to be released. Code blocks are OK, as the compiler
Thomas Wouters89f507f2006-12-13 04:49:30 +000020 * structure takes care of releasing those. Use the arena to manage
21 * objects.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022 */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000023
Guido van Rossum79f25d91997-04-29 20:08:16 +000024#include "Python.h"
Victor Stinner526fdeb2021-03-17 23:50:50 +010025#include "pycore_ast.h" // _PyAST_GetDocString()
Victor Stinnera81fca62021-03-24 00:51:50 +010026#include "pycore_compile.h" // _PyFuture_FromAST()
Victor Stinnerba7a99d2021-01-30 01:46:44 +010027#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
Victor Stinnerc9bc2902020-10-27 02:24:34 +010028#include "pycore_long.h" // _PyLong_GetZero()
Victor Stinner28ad12f2021-03-19 12:41:49 +010029#include "pycore_symtable.h" // PySTEntryObject
Guido van Rossum3f5da241990-12-20 15:06:42 +000030
Mark Shannon582aaf12020-08-04 17:30:11 +010031#define NEED_OPCODE_JUMP_TABLES
Victor Stinner526fdeb2021-03-17 23:50:50 +010032#include "opcode.h" // EXTENDED_ARG
33#include "wordcode_helpers.h" // instrsize()
34
Guido van Rossumb05a5c71997-05-07 17:46:13 +000035
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000036#define DEFAULT_BLOCK_SIZE 16
37#define DEFAULT_BLOCKS 8
38#define DEFAULT_CODE_SIZE 128
39#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000040
Nick Coghlan650f0d02007-04-15 12:05:43 +000041#define COMP_GENEXP 0
42#define COMP_LISTCOMP 1
43#define COMP_SETCOMP 2
Guido van Rossum992d4a32007-07-11 13:09:30 +000044#define COMP_DICTCOMP 3
Nick Coghlan650f0d02007-04-15 12:05:43 +000045
Mark Shannon11e0b292021-04-15 14:28:56 +010046/* A soft limit for stack use, to avoid excessive
47 * memory use for large constants, etc.
48 *
49 * The value 30 is plucked out of thin air.
50 * Code that could use more stack than this is
51 * rare, so the exact value is unimportant.
52 */
53#define STACK_USE_GUIDELINE 30
54
55/* If we exceed this limit, it should
56 * be considered a compiler bug.
57 * Currently it should be impossible
58 * to exceed STACK_USE_GUIDELINE * 100,
59 * as 100 is the maximum parse depth.
60 * For performance reasons we will
61 * want to reduce this to a
62 * few hundred in the future.
63 *
64 * NOTE: Whatever MAX_ALLOWED_STACK_USE is
65 * set to, it should never restrict what Python
66 * we can write, just how we compile it.
67 */
68#define MAX_ALLOWED_STACK_USE (STACK_USE_GUIDELINE * 100)
69
Pablo Galindo90235812020-03-15 04:29:22 +000070#define IS_TOP_LEVEL_AWAIT(c) ( \
71 (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \
72 && (c->u->u_ste->ste_type == ModuleBlock))
73
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000074struct instr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 unsigned char i_opcode;
76 int i_oparg;
77 struct basicblock_ *i_target; /* target block (if jump instruction) */
78 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000079};
80
Mark Shannon582aaf12020-08-04 17:30:11 +010081#define LOG_BITS_PER_INT 5
82#define MASK_LOW_LOG_BITS 31
83
84static inline int
85is_bit_set_in_table(uint32_t *table, int bitindex) {
86 /* Is the relevant bit set in the relevant word? */
87 /* 256 bits fit into 8 32-bits words.
88 * Word is indexed by (bitindex>>ln(size of int in bits)).
89 * Bit within word is the low bits of bitindex.
90 */
91 uint32_t word = table[bitindex >> LOG_BITS_PER_INT];
92 return (word >> (bitindex & MASK_LOW_LOG_BITS)) & 1;
93}
94
95static inline int
96is_relative_jump(struct instr *i)
97{
98 return is_bit_set_in_table(_PyOpcode_RelativeJump, i->i_opcode);
99}
100
101static inline int
102is_jump(struct instr *i)
103{
104 return is_bit_set_in_table(_PyOpcode_Jump, i->i_opcode);
105}
106
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000107typedef struct basicblock_ {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000108 /* Each basicblock in a compilation unit is linked via b_list in the
109 reverse order that the block are allocated. b_list points to the next
110 block, not to be confused with b_next, which is next by control flow. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 struct basicblock_ *b_list;
112 /* number of instructions used */
113 int b_iused;
114 /* length of instruction array (b_instr) */
115 int b_ialloc;
116 /* pointer to an array of instructions, initially NULL */
117 struct instr *b_instr;
118 /* If b_next is non-NULL, it is a pointer to the next
119 block reached by normal control flow. */
120 struct basicblock_ *b_next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 /* b_return is true if a RETURN_VALUE opcode is inserted. */
122 unsigned b_return : 1;
Mark Shannon3bd60352021-01-13 12:05:43 +0000123 /* Number of predecssors that a block has. */
124 int b_predecessors;
Mark Shannoncc75ab72020-11-12 19:49:33 +0000125 /* Basic block has no fall through (it ends with a return, raise or jump) */
126 unsigned b_nofallthrough : 1;
127 /* Basic block exits scope (it ends with a return or raise) */
128 unsigned b_exit : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 /* depth of stack upon entry of block, computed by stackdepth() */
130 int b_startdepth;
131 /* instruction offset for block, computed by assemble_jump_offsets() */
132 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000133} basicblock;
134
135/* fblockinfo tracks the current frame block.
136
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000137A frame block is used to handle loops, try/except, and try/finally.
138It's called a frame block to distinguish it from a basic block in the
139compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000140*/
141
Mark Shannon02d126a2020-09-25 14:04:19 +0100142enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END,
tomKPZ7a7ba3d2021-04-07 07:43:45 -0700143 WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER,
144 ASYNC_COMPREHENSION_GENERATOR };
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000145
146struct fblockinfo {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 enum fblocktype fb_type;
148 basicblock *fb_block;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200149 /* (optional) type-specific exit or cleanup block */
150 basicblock *fb_exit;
Mark Shannonfee55262019-11-21 09:11:43 +0000151 /* (optional) additional information required for unwinding */
152 void *fb_datum;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000153};
154
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100155enum {
156 COMPILER_SCOPE_MODULE,
157 COMPILER_SCOPE_CLASS,
158 COMPILER_SCOPE_FUNCTION,
Yury Selivanov75445082015-05-11 22:57:16 -0400159 COMPILER_SCOPE_ASYNC_FUNCTION,
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400160 COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100161 COMPILER_SCOPE_COMPREHENSION,
162};
163
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000164/* The following items change on entry and exit of code blocks.
165 They must be saved and restored when returning to a block.
166*/
167struct compiler_unit {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 PySTEntryObject *u_ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 PyObject *u_name;
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400171 PyObject *u_qualname; /* dot-separated qualified name (lazy) */
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100172 int u_scope_type;
173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 /* The following fields are dicts that map objects to
175 the index of them in co_XXX. The index is used as
176 the argument for opcodes that refer to those collections.
177 */
178 PyObject *u_consts; /* all constants */
179 PyObject *u_names; /* all names */
180 PyObject *u_varnames; /* local variables */
181 PyObject *u_cellvars; /* cell variables */
182 PyObject *u_freevars; /* free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 PyObject *u_private; /* for private name mangling */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185
Victor Stinnerf8e32212013-11-19 23:56:34 +0100186 Py_ssize_t u_argcount; /* number of arguments for block */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100187 Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */
Victor Stinnerf8e32212013-11-19 23:56:34 +0100188 Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 /* Pointer to the most recently allocated block. By following b_list
190 members, you can reach all early allocated blocks. */
191 basicblock *u_blocks;
192 basicblock *u_curblock; /* pointer to current block */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 int u_nfblocks;
195 struct fblockinfo u_fblock[CO_MAXBLOCKS];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 int u_firstlineno; /* the first lineno of the block */
198 int u_lineno; /* the lineno for the current stmt */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000199 int u_col_offset; /* the offset of the current stmt */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200};
201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202/* This struct captures the global state of a compilation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000203
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000204The u pointer points to the current compilation unit, while units
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205for enclosing blocks are stored in c_stack. The u and c_stack are
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000206managed by compiler_enter_scope() and compiler_exit_scope().
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000207
208Note that we don't track recursion levels during compilation - the
209task of detecting and rejecting excessive levels of nesting is
210handled by the symbol analysis pass.
211
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000212*/
213
214struct compiler {
Victor Stinner14e461d2013-08-26 22:28:21 +0200215 PyObject *c_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 struct symtable *c_st;
217 PyFutureFeatures *c_future; /* pointer to module's __future__ */
218 PyCompilerFlags *c_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000219
Georg Brandl8334fd92010-12-04 10:26:46 +0000220 int c_optimize; /* optimization level */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 int c_interactive; /* true if in interactive mode */
222 int c_nestlevel;
INADA Naokic2e16072018-11-26 21:23:22 +0900223 PyObject *c_const_cache; /* Python dict holding all constants,
224 including names tuple */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 struct compiler_unit *u; /* compiler state for current block */
226 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
227 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000228};
229
Brandt Bucher145bf262021-02-26 14:51:55 -0800230typedef struct {
231 PyObject *stores;
232 int allow_irrefutable;
233} pattern_context;
234
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100235static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000236static void compiler_free(struct compiler *);
237static basicblock *compiler_new_block(struct compiler *);
Andy Lester76d58772020-03-10 21:18:12 -0500238static int compiler_next_instr(basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000239static int compiler_addop(struct compiler *, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +0100240static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
Mark Shannon582aaf12020-08-04 17:30:11 +0100241static int compiler_addop_j(struct compiler *, int, basicblock *);
Mark Shannon127dde52021-01-04 18:06:55 +0000242static int compiler_addop_j_noline(struct compiler *, int, basicblock *);
Brandt Bucher145bf262021-02-26 14:51:55 -0800243static int compiler_error(struct compiler *, const char *, ...);
Serhiy Storchaka62e44812019-02-16 08:12:19 +0200244static int compiler_warn(struct compiler *, const char *, ...);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000245static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
246
247static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
248static int compiler_visit_stmt(struct compiler *, stmt_ty);
249static int compiler_visit_keyword(struct compiler *, keyword_ty);
250static int compiler_visit_expr(struct compiler *, expr_ty);
251static int compiler_augassign(struct compiler *, stmt_ty);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700252static int compiler_annassign(struct compiler *, stmt_ty);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +0200253static int compiler_subscript(struct compiler *, expr_ty);
254static int compiler_slice(struct compiler *, expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000255
Andy Lester76d58772020-03-10 21:18:12 -0500256static int inplace_binop(operator_ty);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100257static int are_all_items_const(asdl_expr_seq *, Py_ssize_t, Py_ssize_t);
Mark Shannon8473cf82020-12-15 11:07:50 +0000258
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000259
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500260static int compiler_with(struct compiler *, stmt_ty, int);
Yury Selivanov75445082015-05-11 22:57:16 -0400261static int compiler_async_with(struct compiler *, stmt_ty, int);
262static int compiler_async_for(struct compiler *, stmt_ty);
Victor Stinner976bb402016-03-23 11:36:19 +0100263static int compiler_call_helper(struct compiler *c, int n,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100264 asdl_expr_seq *args,
265 asdl_keyword_seq *keywords);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500266static int compiler_try_except(struct compiler *, stmt_ty);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400267static int compiler_set_qualname(struct compiler *);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000268
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700269static int compiler_sync_comprehension_generator(
270 struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100271 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +0200272 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700273 expr_ty elt, expr_ty val, int type);
274
275static int compiler_async_comprehension_generator(
276 struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100277 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +0200278 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700279 expr_ty elt, expr_ty val, int type);
280
Brandt Bucher145bf262021-02-26 14:51:55 -0800281static int compiler_pattern(struct compiler *, expr_ty, pattern_context *);
282static int compiler_match(struct compiler *, stmt_ty);
283static int compiler_pattern_subpattern(struct compiler *, expr_ty,
284 pattern_context *);
285
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000286static PyCodeObject *assemble(struct compiler *, int addNone);
Mark Shannon332cd5e2018-01-30 00:41:04 +0000287static PyObject *__doc__, *__annotations__;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000288
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400289#define CAPSULE_NAME "compile.c compiler unit"
Benjamin Petersonb173f782009-05-05 22:31:58 +0000290
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000291PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000292_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 /* Name mangling: __private becomes _classname__private.
295 This is independent from how the name is used. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200296 PyObject *result;
297 size_t nlen, plen, ipriv;
298 Py_UCS4 maxchar;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200300 PyUnicode_READ_CHAR(ident, 0) != '_' ||
301 PyUnicode_READ_CHAR(ident, 1) != '_') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 Py_INCREF(ident);
303 return ident;
304 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200305 nlen = PyUnicode_GET_LENGTH(ident);
306 plen = PyUnicode_GET_LENGTH(privateobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 /* Don't mangle __id__ or names with dots.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 The only time a name with a dot can occur is when
310 we are compiling an import statement that has a
311 package name.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 TODO(jhylton): Decide whether we want to support
314 mangling of the module name, e.g. __M.X.
315 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200316 if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
317 PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
318 PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 Py_INCREF(ident);
320 return ident; /* Don't mangle __whatever__ */
321 }
322 /* Strip leading underscores from class name */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200323 ipriv = 0;
324 while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
325 ipriv++;
326 if (ipriv == plen) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 Py_INCREF(ident);
328 return ident; /* Don't mangle if class is just underscores */
329 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200330 plen -= ipriv;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000331
Antoine Pitrou55bff892013-04-06 21:21:04 +0200332 if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
333 PyErr_SetString(PyExc_OverflowError,
334 "private identifier too large to be mangled");
335 return NULL;
336 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000337
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200338 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
339 if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
340 maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
341
342 result = PyUnicode_New(1 + nlen + plen, maxchar);
343 if (!result)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200345 /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
346 PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
Victor Stinner6c7a52a2011-09-28 21:39:17 +0200347 if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
348 Py_DECREF(result);
349 return NULL;
350 }
351 if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
352 Py_DECREF(result);
353 return NULL;
354 }
Victor Stinner8f825062012-04-27 13:55:39 +0200355 assert(_PyUnicode_CheckConsistency(result, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200356 return result;
Michael W. Hudson60934622004-08-12 17:56:29 +0000357}
358
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000359static int
360compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000363
INADA Naokic2e16072018-11-26 21:23:22 +0900364 c->c_const_cache = PyDict_New();
365 if (!c->c_const_cache) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 return 0;
INADA Naokic2e16072018-11-26 21:23:22 +0900367 }
368
369 c->c_stack = PyList_New(0);
370 if (!c->c_stack) {
371 Py_CLEAR(c->c_const_cache);
372 return 0;
373 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000376}
377
378PyCodeObject *
Victor Stinnera81fca62021-03-24 00:51:50 +0100379_PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
380 int optimize, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 struct compiler c;
383 PyCodeObject *co = NULL;
Victor Stinner37d66d72019-06-13 02:16:41 +0200384 PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 if (!__doc__) {
388 __doc__ = PyUnicode_InternFromString("__doc__");
389 if (!__doc__)
390 return NULL;
391 }
Mark Shannon332cd5e2018-01-30 00:41:04 +0000392 if (!__annotations__) {
393 __annotations__ = PyUnicode_InternFromString("__annotations__");
394 if (!__annotations__)
395 return NULL;
396 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 if (!compiler_init(&c))
398 return NULL;
Victor Stinner14e461d2013-08-26 22:28:21 +0200399 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 c.c_filename = filename;
401 c.c_arena = arena;
Victor Stinnera81fca62021-03-24 00:51:50 +0100402 c.c_future = _PyFuture_FromAST(mod, filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 if (c.c_future == NULL)
404 goto finally;
405 if (!flags) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 flags = &local_flags;
407 }
408 merged = c.c_future->ff_features | flags->cf_flags;
409 c.c_future->ff_features = merged;
410 flags->cf_flags = merged;
411 c.c_flags = flags;
Victor Stinnerda7933e2020-04-13 03:04:28 +0200412 c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000414
Pablo Galindod112c602020-03-18 23:02:09 +0000415 _PyASTOptimizeState state;
416 state.optimize = c.c_optimize;
417 state.ff_features = merged;
418
419 if (!_PyAST_Optimize(mod, arena, &state)) {
INADA Naoki7ea143a2017-12-14 16:47:20 +0900420 goto finally;
421 }
422
Victor Stinner28ad12f2021-03-19 12:41:49 +0100423 c.c_st = _PySymtable_Build(mod, filename, c.c_future);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 if (c.c_st == NULL) {
425 if (!PyErr_Occurred())
426 PyErr_SetString(PyExc_SystemError, "no symtable");
427 goto finally;
428 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000431
Thomas Wouters1175c432006-02-27 22:49:54 +0000432 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 compiler_free(&c);
434 assert(co || PyErr_Occurred());
435 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000436}
437
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000438static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000439compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 if (c->c_st)
Victor Stinner28ad12f2021-03-19 12:41:49 +0100442 _PySymtable_Free(c->c_st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 if (c->c_future)
444 PyObject_Free(c->c_future);
Victor Stinner14e461d2013-08-26 22:28:21 +0200445 Py_XDECREF(c->c_filename);
INADA Naokic2e16072018-11-26 21:23:22 +0900446 Py_DECREF(c->c_const_cache);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000448}
449
Guido van Rossum79f25d91997-04-29 20:08:16 +0000450static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000451list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 Py_ssize_t i, n;
454 PyObject *v, *k;
455 PyObject *dict = PyDict_New();
456 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 n = PyList_Size(list);
459 for (i = 0; i < n; i++) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100460 v = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 if (!v) {
462 Py_DECREF(dict);
463 return NULL;
464 }
465 k = PyList_GET_ITEM(list, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300466 if (PyDict_SetItem(dict, k, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 Py_DECREF(v);
468 Py_DECREF(dict);
469 return NULL;
470 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 Py_DECREF(v);
472 }
473 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000474}
475
476/* Return new dict containing names from src that match scope(s).
477
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000478src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000480values are integers, starting at offset and increasing by one for
481each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482*/
483
484static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +0100485dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000486{
Benjamin Peterson51ab2832012-07-18 15:12:47 -0700487 Py_ssize_t i = offset, scope, num_keys, key_i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 PyObject *k, *v, *dest = PyDict_New();
Meador Inge2ca63152012-07-18 14:20:11 -0500489 PyObject *sorted_keys;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 assert(offset >= 0);
492 if (dest == NULL)
493 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000494
Meador Inge2ca63152012-07-18 14:20:11 -0500495 /* Sort the keys so that we have a deterministic order on the indexes
496 saved in the returned dictionary. These indexes are used as indexes
497 into the free and cell var storage. Therefore if they aren't
498 deterministic, then the generated bytecode is not deterministic.
499 */
500 sorted_keys = PyDict_Keys(src);
501 if (sorted_keys == NULL)
502 return NULL;
503 if (PyList_Sort(sorted_keys) != 0) {
504 Py_DECREF(sorted_keys);
505 return NULL;
506 }
Meador Ingef69e24e2012-07-18 16:41:03 -0500507 num_keys = PyList_GET_SIZE(sorted_keys);
Meador Inge2ca63152012-07-18 14:20:11 -0500508
509 for (key_i = 0; key_i < num_keys; key_i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 /* XXX this should probably be a macro in symtable.h */
511 long vi;
Meador Inge2ca63152012-07-18 14:20:11 -0500512 k = PyList_GET_ITEM(sorted_keys, key_i);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200513 v = PyDict_GetItemWithError(src, k);
514 assert(v && PyLong_Check(v));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 vi = PyLong_AS_LONG(v);
516 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 if (scope == scope_type || vi & flag) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300519 PyObject *item = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 if (item == NULL) {
Meador Inge2ca63152012-07-18 14:20:11 -0500521 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 Py_DECREF(dest);
523 return NULL;
524 }
525 i++;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300526 if (PyDict_SetItem(dest, k, item) < 0) {
Meador Inge2ca63152012-07-18 14:20:11 -0500527 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 Py_DECREF(item);
529 Py_DECREF(dest);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 return NULL;
531 }
532 Py_DECREF(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 }
534 }
Meador Inge2ca63152012-07-18 14:20:11 -0500535 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000537}
538
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000539static void
540compiler_unit_check(struct compiler_unit *u)
541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 basicblock *block;
543 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +0100544 assert(!_PyMem_IsPtrFreed(block));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 if (block->b_instr != NULL) {
546 assert(block->b_ialloc > 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +0100547 assert(block->b_iused >= 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 assert(block->b_ialloc >= block->b_iused);
549 }
550 else {
551 assert (block->b_iused == 0);
552 assert (block->b_ialloc == 0);
553 }
554 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000555}
556
557static void
558compiler_unit_free(struct compiler_unit *u)
559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 compiler_unit_check(u);
563 b = u->u_blocks;
564 while (b != NULL) {
565 if (b->b_instr)
566 PyObject_Free((void *)b->b_instr);
567 next = b->b_list;
568 PyObject_Free((void *)b);
569 b = next;
570 }
571 Py_CLEAR(u->u_ste);
572 Py_CLEAR(u->u_name);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400573 Py_CLEAR(u->u_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 Py_CLEAR(u->u_consts);
575 Py_CLEAR(u->u_names);
576 Py_CLEAR(u->u_varnames);
577 Py_CLEAR(u->u_freevars);
578 Py_CLEAR(u->u_cellvars);
579 Py_CLEAR(u->u_private);
580 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581}
582
583static int
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100584compiler_enter_scope(struct compiler *c, identifier name,
585 int scope_type, void *key, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 struct compiler_unit *u;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100588 basicblock *block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000589
Andy Lester7668a8b2020-03-24 23:26:44 -0500590 u = (struct compiler_unit *)PyObject_Calloc(1, sizeof(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 struct compiler_unit));
592 if (!u) {
593 PyErr_NoMemory();
594 return 0;
595 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100596 u->u_scope_type = scope_type;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 u->u_argcount = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100598 u->u_posonlyargcount = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 u->u_kwonlyargcount = 0;
600 u->u_ste = PySymtable_Lookup(c->c_st, key);
601 if (!u->u_ste) {
602 compiler_unit_free(u);
603 return 0;
604 }
605 Py_INCREF(name);
606 u->u_name = name;
607 u->u_varnames = list2dict(u->u_ste->ste_varnames);
608 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
609 if (!u->u_varnames || !u->u_cellvars) {
610 compiler_unit_free(u);
611 return 0;
612 }
Benjamin Peterson312595c2013-05-15 15:26:42 -0500613 if (u->u_ste->ste_needs_class_closure) {
Martin Panter7462b6492015-11-02 03:37:02 +0000614 /* Cook up an implicit __class__ cell. */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500615 _Py_IDENTIFIER(__class__);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300616 PyObject *name;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500617 int res;
618 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200619 assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500620 name = _PyUnicode_FromId(&PyId___class__);
621 if (!name) {
622 compiler_unit_free(u);
623 return 0;
624 }
Victor Stinnerc9bc2902020-10-27 02:24:34 +0100625 res = PyDict_SetItem(u->u_cellvars, name, _PyLong_GetZero());
Benjamin Peterson312595c2013-05-15 15:26:42 -0500626 if (res < 0) {
627 compiler_unit_free(u);
628 return 0;
629 }
630 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200633 PyDict_GET_SIZE(u->u_cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 if (!u->u_freevars) {
635 compiler_unit_free(u);
636 return 0;
637 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 u->u_blocks = NULL;
640 u->u_nfblocks = 0;
641 u->u_firstlineno = lineno;
642 u->u_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000643 u->u_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 u->u_consts = PyDict_New();
645 if (!u->u_consts) {
646 compiler_unit_free(u);
647 return 0;
648 }
649 u->u_names = PyDict_New();
650 if (!u->u_names) {
651 compiler_unit_free(u);
652 return 0;
653 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 /* Push the old compiler_unit on the stack. */
658 if (c->u) {
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400659 PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
661 Py_XDECREF(capsule);
662 compiler_unit_free(u);
663 return 0;
664 }
665 Py_DECREF(capsule);
666 u->u_private = c->u->u_private;
667 Py_XINCREF(u->u_private);
668 }
669 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 c->c_nestlevel++;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100672
673 block = compiler_new_block(c);
674 if (block == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 return 0;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100676 c->u->u_curblock = block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400678 if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
679 if (!compiler_set_qualname(c))
680 return 0;
681 }
682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000684}
685
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000686static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000687compiler_exit_scope(struct compiler *c)
688{
Victor Stinnera6192632021-01-29 16:53:03 +0100689 // Don't call PySequence_DelItem() with an exception raised
690 PyObject *exc_type, *exc_val, *exc_tb;
691 PyErr_Fetch(&exc_type, &exc_val, &exc_tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 c->c_nestlevel--;
694 compiler_unit_free(c->u);
695 /* Restore c->u to the parent unit. */
Victor Stinnera6192632021-01-29 16:53:03 +0100696 Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 if (n >= 0) {
Victor Stinnera6192632021-01-29 16:53:03 +0100698 PyObject *capsule = PyList_GET_ITEM(c->c_stack, n);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400699 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 assert(c->u);
701 /* we are deleting from a list so this really shouldn't fail */
Victor Stinnera6192632021-01-29 16:53:03 +0100702 if (PySequence_DelItem(c->c_stack, n) < 0) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +0100703 _PyErr_WriteUnraisableMsg("on removing the last compiler "
704 "stack item", NULL);
Victor Stinnera6192632021-01-29 16:53:03 +0100705 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 compiler_unit_check(c->u);
707 }
Victor Stinnera6192632021-01-29 16:53:03 +0100708 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 c->u = NULL;
Victor Stinnera6192632021-01-29 16:53:03 +0100710 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000711
Victor Stinnera6192632021-01-29 16:53:03 +0100712 PyErr_Restore(exc_type, exc_val, exc_tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000713}
714
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400715static int
716compiler_set_qualname(struct compiler *c)
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100717{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100718 _Py_static_string(dot, ".");
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400719 _Py_static_string(dot_locals, ".<locals>");
720 Py_ssize_t stack_size;
721 struct compiler_unit *u = c->u;
722 PyObject *name, *base, *dot_str, *dot_locals_str;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100723
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400724 base = NULL;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100725 stack_size = PyList_GET_SIZE(c->c_stack);
Benjamin Petersona8a38b82013-10-19 16:14:39 -0400726 assert(stack_size >= 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400727 if (stack_size > 1) {
728 int scope, force_global = 0;
729 struct compiler_unit *parent;
730 PyObject *mangled, *capsule;
731
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400732 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400733 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400734 assert(parent);
735
Yury Selivanov75445082015-05-11 22:57:16 -0400736 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
737 || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
738 || u->u_scope_type == COMPILER_SCOPE_CLASS) {
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400739 assert(u->u_name);
740 mangled = _Py_Mangle(parent->u_private, u->u_name);
741 if (!mangled)
742 return 0;
Victor Stinner28ad12f2021-03-19 12:41:49 +0100743 scope = _PyST_GetScope(parent->u_ste, mangled);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400744 Py_DECREF(mangled);
745 assert(scope != GLOBAL_IMPLICIT);
746 if (scope == GLOBAL_EXPLICIT)
747 force_global = 1;
748 }
749
750 if (!force_global) {
751 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
Yury Selivanov75445082015-05-11 22:57:16 -0400752 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400753 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
754 dot_locals_str = _PyUnicode_FromId(&dot_locals);
755 if (dot_locals_str == NULL)
756 return 0;
757 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
758 if (base == NULL)
759 return 0;
760 }
761 else {
762 Py_INCREF(parent->u_qualname);
763 base = parent->u_qualname;
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400764 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100765 }
766 }
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400767
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400768 if (base != NULL) {
769 dot_str = _PyUnicode_FromId(&dot);
770 if (dot_str == NULL) {
771 Py_DECREF(base);
772 return 0;
773 }
774 name = PyUnicode_Concat(base, dot_str);
775 Py_DECREF(base);
776 if (name == NULL)
777 return 0;
778 PyUnicode_Append(&name, u->u_name);
779 if (name == NULL)
780 return 0;
781 }
782 else {
783 Py_INCREF(u->u_name);
784 name = u->u_name;
785 }
786 u->u_qualname = name;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100787
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400788 return 1;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100789}
790
Eric V. Smith235a6f02015-09-19 14:51:32 -0400791
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792/* Allocate a new block and return a pointer to it.
793 Returns NULL on error.
794*/
795
796static basicblock *
797compiler_new_block(struct compiler *c)
798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 basicblock *b;
800 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 u = c->u;
Andy Lester7668a8b2020-03-24 23:26:44 -0500803 b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 if (b == NULL) {
805 PyErr_NoMemory();
806 return NULL;
807 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 /* Extend the singly linked list of blocks with new block. */
809 b->b_list = u->u_blocks;
810 u->u_blocks = b;
811 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812}
813
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814static basicblock *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815compiler_next_block(struct compiler *c)
816{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 basicblock *block = compiler_new_block(c);
818 if (block == NULL)
819 return NULL;
820 c->u->u_curblock->b_next = block;
821 c->u->u_curblock = block;
822 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000823}
824
825static basicblock *
826compiler_use_next_block(struct compiler *c, basicblock *block)
827{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 assert(block != NULL);
829 c->u->u_curblock->b_next = block;
830 c->u->u_curblock = block;
831 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832}
833
Mark Shannon5977a792020-12-02 13:31:40 +0000834static basicblock *
835compiler_copy_block(struct compiler *c, basicblock *block)
836{
837 /* Cannot copy a block if it has a fallthrough, since
838 * a block can only have one fallthrough predecessor.
839 */
840 assert(block->b_nofallthrough);
841 basicblock *result = compiler_next_block(c);
842 if (result == NULL) {
843 return NULL;
844 }
845 for (int i = 0; i < block->b_iused; i++) {
846 int n = compiler_next_instr(result);
847 if (n < 0) {
848 return NULL;
849 }
850 result->b_instr[n] = block->b_instr[i];
851 }
852 result->b_exit = block->b_exit;
Mark Shannon3bd60352021-01-13 12:05:43 +0000853 result->b_nofallthrough = 1;
Mark Shannon5977a792020-12-02 13:31:40 +0000854 return result;
855}
856
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857/* Returns the offset of the next instruction in the current block's
858 b_instr array. Resizes the b_instr as necessary.
859 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000860*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861
862static int
Andy Lester76d58772020-03-10 21:18:12 -0500863compiler_next_instr(basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 assert(b != NULL);
866 if (b->b_instr == NULL) {
Andy Lester7668a8b2020-03-24 23:26:44 -0500867 b->b_instr = (struct instr *)PyObject_Calloc(
868 DEFAULT_BLOCK_SIZE, sizeof(struct instr));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 if (b->b_instr == NULL) {
870 PyErr_NoMemory();
871 return -1;
872 }
873 b->b_ialloc = DEFAULT_BLOCK_SIZE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 }
875 else if (b->b_iused == b->b_ialloc) {
876 struct instr *tmp;
877 size_t oldsize, newsize;
878 oldsize = b->b_ialloc * sizeof(struct instr);
879 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000880
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -0700881 if (oldsize > (SIZE_MAX >> 1)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 PyErr_NoMemory();
883 return -1;
884 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 if (newsize == 0) {
887 PyErr_NoMemory();
888 return -1;
889 }
890 b->b_ialloc <<= 1;
891 tmp = (struct instr *)PyObject_Realloc(
892 (void *)b->b_instr, newsize);
893 if (tmp == NULL) {
894 PyErr_NoMemory();
895 return -1;
896 }
897 b->b_instr = tmp;
898 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
899 }
900 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000901}
902
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200903/* Set the line number and column offset for the following instructions.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000904
Christian Heimes2202f872008-02-06 14:31:34 +0000905 The line number is reset in the following cases:
906 - when entering a new scope
907 - on each statement
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200908 - on each expression and sub-expression
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200909 - before the "except" and "finally" clauses
Thomas Wouters89f507f2006-12-13 04:49:30 +0000910*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000911
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200912#define SET_LOC(c, x) \
913 (c)->u->u_lineno = (x)->lineno; \
914 (c)->u->u_col_offset = (x)->col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000915
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200916/* Return the stack effect of opcode with argument oparg.
917
918 Some opcodes have different stack effect when jump to the target and
919 when not jump. The 'jump' parameter specifies the case:
920
921 * 0 -- when not jump
922 * 1 -- when jump
923 * -1 -- maximal
924 */
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200925static int
926stack_effect(int opcode, int oparg, int jump)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000927{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 switch (opcode) {
Serhiy Storchaka57faf342018-04-25 22:04:06 +0300929 case NOP:
930 case EXTENDED_ARG:
931 return 0;
932
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200933 /* Stack manipulation */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 case POP_TOP:
935 return -1;
936 case ROT_TWO:
937 case ROT_THREE:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200938 case ROT_FOUR:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 return 0;
940 case DUP_TOP:
941 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000942 case DUP_TOP_TWO:
943 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000944
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200945 /* Unary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 case UNARY_POSITIVE:
947 case UNARY_NEGATIVE:
948 case UNARY_NOT:
949 case UNARY_INVERT:
950 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 case SET_ADD:
953 case LIST_APPEND:
954 return -1;
955 case MAP_ADD:
956 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000957
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200958 /* Binary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 case BINARY_POWER:
960 case BINARY_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400961 case BINARY_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 case BINARY_MODULO:
963 case BINARY_ADD:
964 case BINARY_SUBTRACT:
965 case BINARY_SUBSCR:
966 case BINARY_FLOOR_DIVIDE:
967 case BINARY_TRUE_DIVIDE:
968 return -1;
969 case INPLACE_FLOOR_DIVIDE:
970 case INPLACE_TRUE_DIVIDE:
971 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 case INPLACE_ADD:
974 case INPLACE_SUBTRACT:
975 case INPLACE_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400976 case INPLACE_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 case INPLACE_MODULO:
978 return -1;
979 case STORE_SUBSCR:
980 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 case DELETE_SUBSCR:
982 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 case BINARY_LSHIFT:
985 case BINARY_RSHIFT:
986 case BINARY_AND:
987 case BINARY_XOR:
988 case BINARY_OR:
989 return -1;
990 case INPLACE_POWER:
991 return -1;
992 case GET_ITER:
993 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 case PRINT_EXPR:
996 return -1;
997 case LOAD_BUILD_CLASS:
998 return 1;
999 case INPLACE_LSHIFT:
1000 case INPLACE_RSHIFT:
1001 case INPLACE_AND:
1002 case INPLACE_XOR:
1003 case INPLACE_OR:
1004 return -1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 case SETUP_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001007 /* 1 in the normal flow.
1008 * Restore the stack position and push 6 values before jumping to
1009 * the handler if an exception be raised. */
1010 return jump ? 6 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 case RETURN_VALUE:
1012 return -1;
1013 case IMPORT_STAR:
1014 return -1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001015 case SETUP_ANNOTATIONS:
1016 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 case YIELD_VALUE:
1018 return 0;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001019 case YIELD_FROM:
1020 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 case POP_BLOCK:
1022 return 0;
1023 case POP_EXCEPT:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001024 return -3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 case STORE_NAME:
1027 return -1;
1028 case DELETE_NAME:
1029 return 0;
1030 case UNPACK_SEQUENCE:
1031 return oparg-1;
1032 case UNPACK_EX:
1033 return (oparg&0xFF) + (oparg>>8);
1034 case FOR_ITER:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001035 /* -1 at end of iterator, 1 if continue iterating. */
1036 return jump > 0 ? -1 : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 case STORE_ATTR:
1039 return -2;
1040 case DELETE_ATTR:
1041 return -1;
1042 case STORE_GLOBAL:
1043 return -1;
1044 case DELETE_GLOBAL:
1045 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 case LOAD_CONST:
1047 return 1;
1048 case LOAD_NAME:
1049 return 1;
1050 case BUILD_TUPLE:
1051 case BUILD_LIST:
1052 case BUILD_SET:
Serhiy Storchakaea525a22016-09-06 22:07:53 +03001053 case BUILD_STRING:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 return 1-oparg;
1055 case BUILD_MAP:
Benjamin Petersonb6855152015-09-10 21:02:39 -07001056 return 1 - 2*oparg;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001057 case BUILD_CONST_KEY_MAP:
1058 return -oparg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 case LOAD_ATTR:
1060 return 0;
1061 case COMPARE_OP:
Mark Shannon9af0e472020-01-14 10:12:45 +00001062 case IS_OP:
1063 case CONTAINS_OP:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 return -1;
Mark Shannon9af0e472020-01-14 10:12:45 +00001065 case JUMP_IF_NOT_EXC_MATCH:
1066 return -2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 case IMPORT_NAME:
1068 return -1;
1069 case IMPORT_FROM:
1070 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001072 /* Jumps */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 case JUMP_FORWARD:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 case JUMP_ABSOLUTE:
1075 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001077 case JUMP_IF_TRUE_OR_POP:
1078 case JUMP_IF_FALSE_OR_POP:
1079 return jump ? 0 : -1;
1080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 case POP_JUMP_IF_FALSE:
1082 case POP_JUMP_IF_TRUE:
1083 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 case LOAD_GLOBAL:
1086 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001088 /* Exception handling */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 case SETUP_FINALLY:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001090 /* 0 in the normal flow.
1091 * Restore the stack position and push 6 values before jumping to
1092 * the handler if an exception be raised. */
1093 return jump ? 6 : 0;
Mark Shannonfee55262019-11-21 09:11:43 +00001094 case RERAISE:
1095 return -3;
1096
1097 case WITH_EXCEPT_START:
1098 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 case LOAD_FAST:
1101 return 1;
1102 case STORE_FAST:
1103 return -1;
1104 case DELETE_FAST:
1105 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 case RAISE_VARARGS:
1108 return -oparg;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001109
1110 /* Functions and calls */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 case CALL_FUNCTION:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001112 return -oparg;
Yury Selivanovf2392132016-12-13 19:03:51 -05001113 case CALL_METHOD:
1114 return -oparg-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 case CALL_FUNCTION_KW:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001116 return -oparg-1;
1117 case CALL_FUNCTION_EX:
Matthieu Dartiailh3a9ac822017-02-21 14:25:22 +01001118 return -1 - ((oparg & 0x01) != 0);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001119 case MAKE_FUNCTION:
1120 return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1121 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 case BUILD_SLICE:
1123 if (oparg == 3)
1124 return -2;
1125 else
1126 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001128 /* Closures */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 case LOAD_CLOSURE:
1130 return 1;
1131 case LOAD_DEREF:
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001132 case LOAD_CLASSDEREF:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 return 1;
1134 case STORE_DEREF:
1135 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001136 case DELETE_DEREF:
1137 return 0;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001138
1139 /* Iterators and generators */
Yury Selivanov75445082015-05-11 22:57:16 -04001140 case GET_AWAITABLE:
1141 return 0;
1142 case SETUP_ASYNC_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001143 /* 0 in the normal flow.
1144 * Restore the stack position to the position before the result
1145 * of __aenter__ and push 6 values before jumping to the handler
1146 * if an exception be raised. */
1147 return jump ? -1 + 6 : 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001148 case BEFORE_ASYNC_WITH:
1149 return 1;
1150 case GET_AITER:
1151 return 0;
1152 case GET_ANEXT:
1153 return 1;
Yury Selivanov5376ba92015-06-22 12:19:30 -04001154 case GET_YIELD_FROM_ITER:
1155 return 0;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02001156 case END_ASYNC_FOR:
1157 return -7;
Eric V. Smitha78c7952015-11-03 12:45:05 -05001158 case FORMAT_VALUE:
1159 /* If there's a fmt_spec on the stack, we go from 2->1,
1160 else 1->1. */
1161 return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
Yury Selivanovf2392132016-12-13 19:03:51 -05001162 case LOAD_METHOD:
1163 return 1;
Zackery Spytzce6a0702019-08-25 03:44:09 -06001164 case LOAD_ASSERTION_ERROR:
1165 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001166 case LIST_TO_TUPLE:
1167 return 0;
Mark Shannonb37181e2021-04-06 11:48:59 +01001168 case GEN_START:
1169 return -1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001170 case LIST_EXTEND:
1171 case SET_UPDATE:
Mark Shannon8a4cd702020-01-27 09:57:45 +00001172 case DICT_MERGE:
1173 case DICT_UPDATE:
Mark Shannon13bc1392020-01-23 09:25:17 +00001174 return -1;
Brandt Bucher145bf262021-02-26 14:51:55 -08001175 case COPY_DICT_WITHOUT_KEYS:
1176 return 0;
1177 case MATCH_CLASS:
1178 return -1;
1179 case GET_LEN:
1180 case MATCH_MAPPING:
1181 case MATCH_SEQUENCE:
1182 return 1;
1183 case MATCH_KEYS:
1184 return 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 default:
Larry Hastings3a907972013-11-23 14:49:22 -08001186 return PY_INVALID_STACK_EFFECT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 }
Larry Hastings3a907972013-11-23 14:49:22 -08001188 return PY_INVALID_STACK_EFFECT; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189}
1190
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001191int
Serhiy Storchaka7bdf2822018-09-18 09:54:26 +03001192PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump)
1193{
1194 return stack_effect(opcode, oparg, jump);
1195}
1196
1197int
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001198PyCompile_OpcodeStackEffect(int opcode, int oparg)
1199{
1200 return stack_effect(opcode, oparg, -1);
1201}
1202
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203/* Add an opcode with no argument.
1204 Returns 0 on failure, 1 on success.
1205*/
1206
1207static int
Mark Shannon3bd60352021-01-13 12:05:43 +00001208compiler_addop_line(struct compiler *c, int opcode, int line)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 basicblock *b;
1211 struct instr *i;
1212 int off;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001213 assert(!HAS_ARG(opcode));
Andy Lester76d58772020-03-10 21:18:12 -05001214 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 if (off < 0)
1216 return 0;
1217 b = c->u->u_curblock;
1218 i = &b->b_instr[off];
1219 i->i_opcode = opcode;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001220 i->i_oparg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 if (opcode == RETURN_VALUE)
1222 b->b_return = 1;
Mark Shannon3bd60352021-01-13 12:05:43 +00001223 i->i_lineno = line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225}
1226
Mark Shannon3bd60352021-01-13 12:05:43 +00001227static int
1228compiler_addop(struct compiler *c, int opcode)
1229{
1230 return compiler_addop_line(c, opcode, c->u->u_lineno);
1231}
1232
1233static int
1234compiler_addop_noline(struct compiler *c, int opcode)
1235{
1236 return compiler_addop_line(c, opcode, -1);
1237}
1238
1239
Victor Stinnerf8e32212013-11-19 23:56:34 +01001240static Py_ssize_t
Andy Lester76d58772020-03-10 21:18:12 -05001241compiler_add_o(PyObject *dict, PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001243 PyObject *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001245
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001246 v = PyDict_GetItemWithError(dict, o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 if (!v) {
Stefan Krahc0cbed12015-07-27 12:56:49 +02001248 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 return -1;
Stefan Krahc0cbed12015-07-27 12:56:49 +02001250 }
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001251 arg = PyDict_GET_SIZE(dict);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001252 v = PyLong_FromSsize_t(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 if (!v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 return -1;
1255 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001256 if (PyDict_SetItem(dict, o, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 Py_DECREF(v);
1258 return -1;
1259 }
1260 Py_DECREF(v);
1261 }
1262 else
1263 arg = PyLong_AsLong(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001264 return arg;
1265}
1266
INADA Naokic2e16072018-11-26 21:23:22 +09001267// Merge const *o* recursively and return constant key object.
1268static PyObject*
1269merge_consts_recursive(struct compiler *c, PyObject *o)
1270{
1271 // None and Ellipsis are singleton, and key is the singleton.
1272 // No need to merge object and key.
1273 if (o == Py_None || o == Py_Ellipsis) {
1274 Py_INCREF(o);
1275 return o;
1276 }
1277
1278 PyObject *key = _PyCode_ConstantKey(o);
1279 if (key == NULL) {
1280 return NULL;
1281 }
1282
1283 // t is borrowed reference
1284 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
1285 if (t != key) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001286 // o is registered in c_const_cache. Just use it.
Zackery Spytz9b4a1b12019-03-20 03:16:25 -06001287 Py_XINCREF(t);
INADA Naokic2e16072018-11-26 21:23:22 +09001288 Py_DECREF(key);
1289 return t;
1290 }
1291
INADA Naokif7e4d362018-11-29 00:58:46 +09001292 // We registered o in c_const_cache.
Simeon63b5fc52019-04-09 19:36:57 -04001293 // When o is a tuple or frozenset, we want to merge its
INADA Naokif7e4d362018-11-29 00:58:46 +09001294 // items too.
INADA Naokic2e16072018-11-26 21:23:22 +09001295 if (PyTuple_CheckExact(o)) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001296 Py_ssize_t len = PyTuple_GET_SIZE(o);
1297 for (Py_ssize_t i = 0; i < len; i++) {
INADA Naokic2e16072018-11-26 21:23:22 +09001298 PyObject *item = PyTuple_GET_ITEM(o, i);
1299 PyObject *u = merge_consts_recursive(c, item);
1300 if (u == NULL) {
1301 Py_DECREF(key);
1302 return NULL;
1303 }
1304
1305 // See _PyCode_ConstantKey()
1306 PyObject *v; // borrowed
1307 if (PyTuple_CheckExact(u)) {
1308 v = PyTuple_GET_ITEM(u, 1);
1309 }
1310 else {
1311 v = u;
1312 }
1313 if (v != item) {
1314 Py_INCREF(v);
1315 PyTuple_SET_ITEM(o, i, v);
1316 Py_DECREF(item);
1317 }
1318
1319 Py_DECREF(u);
1320 }
1321 }
INADA Naokif7e4d362018-11-29 00:58:46 +09001322 else if (PyFrozenSet_CheckExact(o)) {
Simeon63b5fc52019-04-09 19:36:57 -04001323 // *key* is tuple. And its first item is frozenset of
INADA Naokif7e4d362018-11-29 00:58:46 +09001324 // constant keys.
1325 // See _PyCode_ConstantKey() for detail.
1326 assert(PyTuple_CheckExact(key));
1327 assert(PyTuple_GET_SIZE(key) == 2);
1328
1329 Py_ssize_t len = PySet_GET_SIZE(o);
1330 if (len == 0) { // empty frozenset should not be re-created.
1331 return key;
1332 }
1333 PyObject *tuple = PyTuple_New(len);
1334 if (tuple == NULL) {
1335 Py_DECREF(key);
1336 return NULL;
1337 }
1338 Py_ssize_t i = 0, pos = 0;
1339 PyObject *item;
1340 Py_hash_t hash;
1341 while (_PySet_NextEntry(o, &pos, &item, &hash)) {
1342 PyObject *k = merge_consts_recursive(c, item);
1343 if (k == NULL) {
1344 Py_DECREF(tuple);
1345 Py_DECREF(key);
1346 return NULL;
1347 }
1348 PyObject *u;
1349 if (PyTuple_CheckExact(k)) {
1350 u = PyTuple_GET_ITEM(k, 1);
1351 Py_INCREF(u);
1352 Py_DECREF(k);
1353 }
1354 else {
1355 u = k;
1356 }
1357 PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u.
1358 i++;
1359 }
1360
1361 // Instead of rewriting o, we create new frozenset and embed in the
1362 // key tuple. Caller should get merged frozenset from the key tuple.
1363 PyObject *new = PyFrozenSet_New(tuple);
1364 Py_DECREF(tuple);
1365 if (new == NULL) {
1366 Py_DECREF(key);
1367 return NULL;
1368 }
1369 assert(PyTuple_GET_ITEM(key, 1) == o);
1370 Py_DECREF(o);
1371 PyTuple_SET_ITEM(key, 1, new);
1372 }
INADA Naokic2e16072018-11-26 21:23:22 +09001373
1374 return key;
1375}
1376
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001377static Py_ssize_t
1378compiler_add_const(struct compiler *c, PyObject *o)
1379{
INADA Naokic2e16072018-11-26 21:23:22 +09001380 PyObject *key = merge_consts_recursive(c, o);
1381 if (key == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001382 return -1;
INADA Naokic2e16072018-11-26 21:23:22 +09001383 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001384
Andy Lester76d58772020-03-10 21:18:12 -05001385 Py_ssize_t arg = compiler_add_o(c->u->u_consts, key);
INADA Naokic2e16072018-11-26 21:23:22 +09001386 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388}
1389
1390static int
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001391compiler_addop_load_const(struct compiler *c, PyObject *o)
1392{
1393 Py_ssize_t arg = compiler_add_const(c, o);
1394 if (arg < 0)
1395 return 0;
1396 return compiler_addop_i(c, LOAD_CONST, arg);
1397}
1398
1399static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001402{
Andy Lester76d58772020-03-10 21:18:12 -05001403 Py_ssize_t arg = compiler_add_o(dict, o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001404 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001405 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001406 return compiler_addop_i(c, opcode, arg);
1407}
1408
1409static int
1410compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001413 Py_ssize_t arg;
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001414
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001415 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1416 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001417 return 0;
Andy Lester76d58772020-03-10 21:18:12 -05001418 arg = compiler_add_o(dict, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419 Py_DECREF(mangled);
1420 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001421 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422 return compiler_addop_i(c, opcode, arg);
1423}
1424
1425/* Add an opcode with an integer argument.
1426 Returns 0 on failure, 1 on success.
1427*/
1428
1429static int
Mark Shannon11e0b292021-04-15 14:28:56 +01001430compiler_addop_i_line(struct compiler *c, int opcode, Py_ssize_t oparg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 struct instr *i;
1433 int off;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001434
Victor Stinner2ad474b2016-03-01 23:34:47 +01001435 /* oparg value is unsigned, but a signed C int is usually used to store
1436 it in the C code (like Python/ceval.c).
1437
1438 Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1439
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001440 The argument of a concrete bytecode instruction is limited to 8-bit.
1441 EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1442 assert(HAS_ARG(opcode));
Victor Stinner2ad474b2016-03-01 23:34:47 +01001443 assert(0 <= oparg && oparg <= 2147483647);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001444
Andy Lester76d58772020-03-10 21:18:12 -05001445 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 if (off < 0)
1447 return 0;
1448 i = &c->u->u_curblock->b_instr[off];
Victor Stinnerf8e32212013-11-19 23:56:34 +01001449 i->i_opcode = opcode;
1450 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
Mark Shannon11e0b292021-04-15 14:28:56 +01001451 i->i_lineno = lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453}
1454
Mark Shannon11e0b292021-04-15 14:28:56 +01001455static int
1456compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
1457{
1458 return compiler_addop_i_line(c, opcode, oparg, c->u->u_lineno);
1459}
1460
1461static int
1462compiler_addop_i_noline(struct compiler *c, int opcode, Py_ssize_t oparg)
1463{
1464 return compiler_addop_i_line(c, opcode, oparg, -1);
1465}
1466
Mark Shannon28b75c82020-12-23 11:43:10 +00001467static int add_jump_to_block(basicblock *b, int opcode, int lineno, basicblock *target)
1468{
1469 assert(HAS_ARG(opcode));
1470 assert(b != NULL);
1471 assert(target != NULL);
1472
1473 int off = compiler_next_instr(b);
1474 struct instr *i = &b->b_instr[off];
1475 if (off < 0) {
1476 return 0;
1477 }
1478 i->i_opcode = opcode;
1479 i->i_target = target;
1480 i->i_lineno = lineno;
1481 return 1;
1482}
1483
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001484static int
Mark Shannon582aaf12020-08-04 17:30:11 +01001485compiler_addop_j(struct compiler *c, int opcode, basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001486{
Mark Shannon28b75c82020-12-23 11:43:10 +00001487 return add_jump_to_block(c->u->u_curblock, opcode, c->u->u_lineno, b);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488}
1489
Mark Shannon127dde52021-01-04 18:06:55 +00001490static int
1491compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b)
1492{
1493 return add_jump_to_block(c->u->u_curblock, opcode, -1, b);
1494}
1495
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +01001496/* NEXT_BLOCK() creates an implicit jump from the current block
1497 to the new block.
1498
1499 The returns inside this macro make it impossible to decref objects
1500 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001501*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 if (compiler_next_block((C)) == NULL) \
1504 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505}
1506
1507#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 if (!compiler_addop((C), (OP))) \
1509 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510}
1511
Mark Shannon3bd60352021-01-13 12:05:43 +00001512#define ADDOP_NOLINE(C, OP) { \
1513 if (!compiler_addop_noline((C), (OP))) \
1514 return 0; \
1515}
1516
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001517#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 if (!compiler_addop((C), (OP))) { \
1519 compiler_exit_scope(c); \
1520 return 0; \
1521 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001522}
1523
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001524#define ADDOP_LOAD_CONST(C, O) { \
1525 if (!compiler_addop_load_const((C), (O))) \
1526 return 0; \
1527}
1528
1529/* Same as ADDOP_LOAD_CONST, but steals a reference. */
1530#define ADDOP_LOAD_CONST_NEW(C, O) { \
1531 PyObject *__new_const = (O); \
1532 if (__new_const == NULL) { \
1533 return 0; \
1534 } \
1535 if (!compiler_addop_load_const((C), __new_const)) { \
1536 Py_DECREF(__new_const); \
1537 return 0; \
1538 } \
1539 Py_DECREF(__new_const); \
1540}
1541
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001542#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1544 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545}
1546
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001547/* Same as ADDOP_O, but steals a reference. */
1548#define ADDOP_N(C, OP, O, TYPE) { \
1549 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1550 Py_DECREF((O)); \
1551 return 0; \
1552 } \
1553 Py_DECREF((O)); \
1554}
1555
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001556#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1558 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559}
1560
1561#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 if (!compiler_addop_i((C), (OP), (O))) \
1563 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564}
1565
Mark Shannon11e0b292021-04-15 14:28:56 +01001566#define ADDOP_I_NOLINE(C, OP, O) { \
1567 if (!compiler_addop_i_noline((C), (OP), (O))) \
1568 return 0; \
1569}
1570
Mark Shannon582aaf12020-08-04 17:30:11 +01001571#define ADDOP_JUMP(C, OP, O) { \
1572 if (!compiler_addop_j((C), (OP), (O))) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001574}
1575
Mark Shannon127dde52021-01-04 18:06:55 +00001576/* Add a jump with no line number.
1577 * Used for artificial jumps that have no corresponding
1578 * token in the source code. */
1579#define ADDOP_JUMP_NOLINE(C, OP, O) { \
1580 if (!compiler_addop_j_noline((C), (OP), (O))) \
1581 return 0; \
1582}
1583
Mark Shannon9af0e472020-01-14 10:12:45 +00001584#define ADDOP_COMPARE(C, CMP) { \
1585 if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \
1586 return 0; \
1587}
1588
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1590 the ASDL name to synthesize the name of the C type and the visit function.
1591*/
1592
1593#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 if (!compiler_visit_ ## TYPE((C), (V))) \
1595 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596}
1597
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001598#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 if (!compiler_visit_ ## TYPE((C), (V))) { \
1600 compiler_exit_scope(c); \
1601 return 0; \
1602 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001603}
1604
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001605#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 if (!compiler_visit_slice((C), (V), (CTX))) \
1607 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001608}
1609
1610#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001612 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1614 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1615 if (!compiler_visit_ ## TYPE((C), elt)) \
1616 return 0; \
1617 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001618}
1619
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001620#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001622 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1624 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1625 if (!compiler_visit_ ## TYPE((C), elt)) { \
1626 compiler_exit_scope(c); \
1627 return 0; \
1628 } \
1629 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001630}
1631
Brandt Bucher145bf262021-02-26 14:51:55 -08001632#define RETURN_IF_FALSE(X) \
1633 if (!(X)) { \
1634 return 0; \
1635 }
1636
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001637/* Search if variable annotations are present statically in a block. */
1638
1639static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001640find_ann(asdl_stmt_seq *stmts)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001641{
1642 int i, j, res = 0;
1643 stmt_ty st;
1644
1645 for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1646 st = (stmt_ty)asdl_seq_GET(stmts, i);
1647 switch (st->kind) {
1648 case AnnAssign_kind:
1649 return 1;
1650 case For_kind:
1651 res = find_ann(st->v.For.body) ||
1652 find_ann(st->v.For.orelse);
1653 break;
1654 case AsyncFor_kind:
1655 res = find_ann(st->v.AsyncFor.body) ||
1656 find_ann(st->v.AsyncFor.orelse);
1657 break;
1658 case While_kind:
1659 res = find_ann(st->v.While.body) ||
1660 find_ann(st->v.While.orelse);
1661 break;
1662 case If_kind:
1663 res = find_ann(st->v.If.body) ||
1664 find_ann(st->v.If.orelse);
1665 break;
1666 case With_kind:
1667 res = find_ann(st->v.With.body);
1668 break;
1669 case AsyncWith_kind:
1670 res = find_ann(st->v.AsyncWith.body);
1671 break;
1672 case Try_kind:
1673 for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1674 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1675 st->v.Try.handlers, j);
1676 if (find_ann(handler->v.ExceptHandler.body)) {
1677 return 1;
1678 }
1679 }
1680 res = find_ann(st->v.Try.body) ||
1681 find_ann(st->v.Try.finalbody) ||
1682 find_ann(st->v.Try.orelse);
1683 break;
1684 default:
1685 res = 0;
1686 }
1687 if (res) {
1688 break;
1689 }
1690 }
1691 return res;
1692}
1693
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001694/*
1695 * Frame block handling functions
1696 */
1697
1698static int
1699compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b,
Mark Shannonfee55262019-11-21 09:11:43 +00001700 basicblock *exit, void *datum)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001701{
1702 struct fblockinfo *f;
1703 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
Mark Shannon02d126a2020-09-25 14:04:19 +01001704 return compiler_error(c, "too many statically nested blocks");
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001705 }
1706 f = &c->u->u_fblock[c->u->u_nfblocks++];
1707 f->fb_type = t;
1708 f->fb_block = b;
1709 f->fb_exit = exit;
Mark Shannonfee55262019-11-21 09:11:43 +00001710 f->fb_datum = datum;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001711 return 1;
1712}
1713
1714static void
1715compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
1716{
1717 struct compiler_unit *u = c->u;
1718 assert(u->u_nfblocks > 0);
1719 u->u_nfblocks--;
1720 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
1721 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
1722}
1723
Mark Shannonfee55262019-11-21 09:11:43 +00001724static int
1725compiler_call_exit_with_nones(struct compiler *c) {
1726 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1727 ADDOP(c, DUP_TOP);
1728 ADDOP(c, DUP_TOP);
1729 ADDOP_I(c, CALL_FUNCTION, 3);
1730 return 1;
1731}
1732
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001733/* Unwind a frame block. If preserve_tos is true, the TOS before
Mark Shannonfee55262019-11-21 09:11:43 +00001734 * popping the blocks will be restored afterwards, unless another
1735 * return, break or continue is found. In which case, the TOS will
1736 * be popped.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001737 */
1738static int
1739compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
1740 int preserve_tos)
1741{
1742 switch (info->fb_type) {
1743 case WHILE_LOOP:
Mark Shannon02d126a2020-09-25 14:04:19 +01001744 case EXCEPTION_HANDLER:
tomKPZ7a7ba3d2021-04-07 07:43:45 -07001745 case ASYNC_COMPREHENSION_GENERATOR:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001746 return 1;
1747
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001748 case FOR_LOOP:
1749 /* Pop the iterator */
1750 if (preserve_tos) {
1751 ADDOP(c, ROT_TWO);
1752 }
1753 ADDOP(c, POP_TOP);
1754 return 1;
1755
Mark Shannon02d126a2020-09-25 14:04:19 +01001756 case TRY_EXCEPT:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001757 ADDOP(c, POP_BLOCK);
1758 return 1;
1759
1760 case FINALLY_TRY:
Mark Shannon5274b682020-12-16 13:07:01 +00001761 /* This POP_BLOCK gets the line number of the unwinding statement */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001762 ADDOP(c, POP_BLOCK);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001763 if (preserve_tos) {
Mark Shannonfee55262019-11-21 09:11:43 +00001764 if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) {
1765 return 0;
1766 }
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001767 }
Mark Shannon5274b682020-12-16 13:07:01 +00001768 /* Emit the finally block */
Mark Shannonfee55262019-11-21 09:11:43 +00001769 VISIT_SEQ(c, stmt, info->fb_datum);
1770 if (preserve_tos) {
1771 compiler_pop_fblock(c, POP_VALUE, NULL);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001772 }
Mark Shannon5274b682020-12-16 13:07:01 +00001773 /* The finally block should appear to execute after the
1774 * statement causing the unwinding, so make the unwinding
1775 * instruction artificial */
1776 c->u->u_lineno = -1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001777 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001778
Mark Shannonfee55262019-11-21 09:11:43 +00001779 case FINALLY_END:
1780 if (preserve_tos) {
1781 ADDOP(c, ROT_FOUR);
1782 }
1783 ADDOP(c, POP_TOP);
1784 ADDOP(c, POP_TOP);
1785 ADDOP(c, POP_TOP);
1786 if (preserve_tos) {
1787 ADDOP(c, ROT_FOUR);
1788 }
1789 ADDOP(c, POP_EXCEPT);
1790 return 1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001791
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001792 case WITH:
1793 case ASYNC_WITH:
1794 ADDOP(c, POP_BLOCK);
1795 if (preserve_tos) {
1796 ADDOP(c, ROT_TWO);
1797 }
Mark Shannonfee55262019-11-21 09:11:43 +00001798 if(!compiler_call_exit_with_nones(c)) {
1799 return 0;
1800 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001801 if (info->fb_type == ASYNC_WITH) {
1802 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001803 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001804 ADDOP(c, YIELD_FROM);
1805 }
Mark Shannonfee55262019-11-21 09:11:43 +00001806 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001807 return 1;
1808
1809 case HANDLER_CLEANUP:
Mark Shannonfee55262019-11-21 09:11:43 +00001810 if (info->fb_datum) {
1811 ADDOP(c, POP_BLOCK);
1812 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001813 if (preserve_tos) {
1814 ADDOP(c, ROT_FOUR);
1815 }
Mark Shannonfee55262019-11-21 09:11:43 +00001816 ADDOP(c, POP_EXCEPT);
1817 if (info->fb_datum) {
1818 ADDOP_LOAD_CONST(c, Py_None);
1819 compiler_nameop(c, info->fb_datum, Store);
1820 compiler_nameop(c, info->fb_datum, Del);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001821 }
Mark Shannonfee55262019-11-21 09:11:43 +00001822 return 1;
1823
1824 case POP_VALUE:
1825 if (preserve_tos) {
1826 ADDOP(c, ROT_TWO);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001827 }
Mark Shannonfee55262019-11-21 09:11:43 +00001828 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001829 return 1;
1830 }
1831 Py_UNREACHABLE();
1832}
1833
Mark Shannonfee55262019-11-21 09:11:43 +00001834/** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */
1835static int
1836compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) {
1837 if (c->u->u_nfblocks == 0) {
1838 return 1;
1839 }
1840 struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1];
1841 if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) {
1842 *loop = top;
1843 return 1;
1844 }
1845 struct fblockinfo copy = *top;
1846 c->u->u_nfblocks--;
1847 if (!compiler_unwind_fblock(c, &copy, preserve_tos)) {
1848 return 0;
1849 }
1850 if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) {
1851 return 0;
1852 }
1853 c->u->u_fblock[c->u->u_nfblocks] = copy;
1854 c->u->u_nfblocks++;
1855 return 1;
1856}
1857
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001858/* Compile a sequence of statements, checking for a docstring
1859 and for annotations. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860
1861static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001862compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001863{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001864 int i = 0;
1865 stmt_ty st;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001866 PyObject *docstring;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001867
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001868 /* Set current line number to the line number of first statement.
1869 This way line number for SETUP_ANNOTATIONS will always
1870 coincide with the line number of first "real" statement in module.
Hansraj Das01171eb2019-10-09 07:54:02 +05301871 If body is empty, then lineno will be set later in assemble. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001872 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) {
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001873 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001874 SET_LOC(c, st);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001875 }
1876 /* Every annotated class and module should have __annotations__. */
1877 if (find_ann(stmts)) {
1878 ADDOP(c, SETUP_ANNOTATIONS);
1879 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001880 if (!asdl_seq_LEN(stmts))
1881 return 1;
INADA Naokicb41b272017-02-23 00:31:59 +09001882 /* if not -OO mode, set docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001883 if (c->c_optimize < 2) {
1884 docstring = _PyAST_GetDocString(stmts);
1885 if (docstring) {
1886 i = 1;
1887 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1888 assert(st->kind == Expr_kind);
1889 VISIT(c, expr, st->v.Expr.value);
1890 if (!compiler_nameop(c, __doc__, Store))
1891 return 0;
1892 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001894 for (; i < asdl_seq_LEN(stmts); i++)
1895 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897}
1898
1899static PyCodeObject *
1900compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001901{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 PyCodeObject *co;
1903 int addNone = 1;
1904 static PyObject *module;
1905 if (!module) {
1906 module = PyUnicode_InternFromString("<module>");
1907 if (!module)
1908 return NULL;
1909 }
1910 /* Use 0 for firstlineno initially, will fixup in assemble(). */
Mark Shannon877df852020-11-12 09:43:29 +00001911 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 return NULL;
1913 switch (mod->kind) {
1914 case Module_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001915 if (!compiler_body(c, mod->v.Module.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 compiler_exit_scope(c);
1917 return 0;
1918 }
1919 break;
1920 case Interactive_kind:
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001921 if (find_ann(mod->v.Interactive.body)) {
1922 ADDOP(c, SETUP_ANNOTATIONS);
1923 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 c->c_interactive = 1;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001925 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 break;
1927 case Expression_kind:
1928 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1929 addNone = 0;
1930 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 default:
1932 PyErr_Format(PyExc_SystemError,
1933 "module kind %d should not be possible",
1934 mod->kind);
1935 return 0;
1936 }
1937 co = assemble(c, addNone);
1938 compiler_exit_scope(c);
1939 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001940}
1941
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942/* The test for LOCAL must come before the test for FREE in order to
1943 handle classes where name is both local and free. The local var is
1944 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001945*/
1946
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001947static int
1948get_ref_type(struct compiler *c, PyObject *name)
1949{
Victor Stinner0b1bc562013-05-16 22:17:17 +02001950 int scope;
Benjamin Peterson312595c2013-05-15 15:26:42 -05001951 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001952 _PyUnicode_EqualToASCIIString(name, "__class__"))
Benjamin Peterson312595c2013-05-15 15:26:42 -05001953 return CELL;
Victor Stinner28ad12f2021-03-19 12:41:49 +01001954 scope = _PyST_GetScope(c->u->u_ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 if (scope == 0) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001956 PyErr_Format(PyExc_SystemError,
Victor Stinner28ad12f2021-03-19 12:41:49 +01001957 "_PyST_GetScope(name=%R) failed: "
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001958 "unknown scope in unit %S (%R); "
1959 "symbols: %R; locals: %R; globals: %R",
1960 name,
1961 c->u->u_name, c->u->u_ste->ste_id,
1962 c->u->u_ste->ste_symbols, c->u->u_varnames, c->u->u_names);
1963 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001966}
1967
1968static int
1969compiler_lookup_arg(PyObject *dict, PyObject *name)
1970{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001971 PyObject *v;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001972 v = PyDict_GetItemWithError(dict, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001973 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001974 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001975 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001976}
1977
1978static int
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001979compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags,
1980 PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001981{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001982 Py_ssize_t i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001983 if (qualname == NULL)
1984 qualname = co->co_name;
1985
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001986 if (free) {
1987 for (i = 0; i < free; ++i) {
1988 /* Bypass com_addop_varname because it will generate
1989 LOAD_DEREF but LOAD_CLOSURE is needed.
1990 */
1991 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001993 /* Special case: If a class contains a method with a
1994 free variable that has the same name as a method,
1995 the name will be considered free *and* local in the
1996 class. It should be handled by the closure, as
Min ho Kimc4cacc82019-07-31 08:16:13 +10001997 well as by the normal name lookup logic.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001998 */
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001999 int reftype = get_ref_type(c, name);
2000 if (reftype == -1) {
2001 return 0;
2002 }
2003 int arg;
2004 if (reftype == CELL) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002005 arg = compiler_lookup_arg(c->u->u_cellvars, name);
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002006 }
2007 else {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002008 arg = compiler_lookup_arg(c->u->u_freevars, name);
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002009 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002010 if (arg == -1) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002011 PyErr_Format(PyExc_SystemError,
2012 "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; "
2013 "freevars of code %S: %R",
2014 name,
2015 reftype,
2016 c->u->u_name,
2017 co->co_name,
2018 co->co_freevars);
2019 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002020 }
2021 ADDOP_I(c, LOAD_CLOSURE, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002023 flags |= 0x08;
2024 ADDOP_I(c, BUILD_TUPLE, free);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002026 ADDOP_LOAD_CONST(c, (PyObject*)co);
2027 ADDOP_LOAD_CONST(c, qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002028 ADDOP_I(c, MAKE_FUNCTION, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002030}
2031
2032static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002033compiler_decorators(struct compiler *c, asdl_expr_seq* decos)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 if (!decos)
2038 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002040 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2041 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
2042 }
2043 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002044}
2045
2046static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002047compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs,
2048 asdl_expr_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00002049{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002050 /* Push a dict of keyword-only default values.
2051
2052 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
2053 */
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002054 int i;
2055 PyObject *keys = NULL;
2056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
2058 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
2059 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
2060 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04002061 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002062 if (!mangled) {
2063 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002065 if (keys == NULL) {
2066 keys = PyList_New(1);
2067 if (keys == NULL) {
2068 Py_DECREF(mangled);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002069 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002070 }
2071 PyList_SET_ITEM(keys, 0, mangled);
2072 }
2073 else {
2074 int res = PyList_Append(keys, mangled);
2075 Py_DECREF(mangled);
2076 if (res == -1) {
2077 goto error;
2078 }
2079 }
2080 if (!compiler_visit_expr(c, default_)) {
2081 goto error;
2082 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 }
2084 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002085 if (keys != NULL) {
2086 Py_ssize_t default_count = PyList_GET_SIZE(keys);
2087 PyObject *keys_tuple = PyList_AsTuple(keys);
2088 Py_DECREF(keys);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002089 ADDOP_LOAD_CONST_NEW(c, keys_tuple);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002090 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002091 assert(default_count > 0);
2092 return 1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002093 }
2094 else {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002095 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002096 }
2097
2098error:
2099 Py_XDECREF(keys);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002100 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002101}
2102
2103static int
Guido van Rossum95e4d582018-01-26 08:20:18 -08002104compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
2105{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +03002106 ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation));
Guido van Rossum95e4d582018-01-26 08:20:18 -08002107 return 1;
2108}
2109
2110static int
Neal Norwitzc1505362006-12-28 06:47:50 +00002111compiler_visit_argannotation(struct compiler *c, identifier id,
Yurii Karabas73019792020-11-25 12:43:18 +02002112 expr_ty annotation, Py_ssize_t *annotations_len)
Neal Norwitzc1505362006-12-28 06:47:50 +00002113{
Pablo Galindob0544ba2021-04-21 12:41:19 +01002114 if (!annotation) {
2115 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116 }
Pablo Galindob0544ba2021-04-21 12:41:19 +01002117
2118 PyObject *mangled = _Py_Mangle(c->u->u_private, id);
2119 if (!mangled) {
2120 return 0;
2121 }
2122 ADDOP_LOAD_CONST(c, mangled);
2123 Py_DECREF(mangled);
2124
2125 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
2126 VISIT(c, annexpr, annotation)
2127 }
2128 else {
2129 VISIT(c, expr, annotation);
2130 }
2131 *annotations_len += 2;
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002132 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002133}
2134
2135static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002136compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args,
Yurii Karabas73019792020-11-25 12:43:18 +02002137 Py_ssize_t *annotations_len)
Neal Norwitzc1505362006-12-28 06:47:50 +00002138{
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002139 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 for (i = 0; i < asdl_seq_LEN(args); i++) {
2141 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002142 if (!compiler_visit_argannotation(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143 c,
2144 arg->arg,
2145 arg->annotation,
Yurii Karabas73019792020-11-25 12:43:18 +02002146 annotations_len))
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002147 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002149 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002150}
2151
2152static int
2153compiler_visit_annotations(struct compiler *c, arguments_ty args,
2154 expr_ty returns)
2155{
Yurii Karabas73019792020-11-25 12:43:18 +02002156 /* Push arg annotation names and values.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002157 The expressions are evaluated out-of-order wrt the source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00002158
Yurii Karabas73019792020-11-25 12:43:18 +02002159 Return 0 on error, -1 if no annotations pushed, 1 if a annotations is pushed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002160 */
2161 static identifier return_str;
Yurii Karabas73019792020-11-25 12:43:18 +02002162 Py_ssize_t annotations_len = 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002163
Yurii Karabas73019792020-11-25 12:43:18 +02002164 if (!compiler_visit_argannotations(c, args->args, &annotations_len))
2165 return 0;
2166 if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len))
2167 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002168 if (args->vararg && args->vararg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002169 !compiler_visit_argannotation(c, args->vararg->arg,
Yurii Karabas73019792020-11-25 12:43:18 +02002170 args->vararg->annotation, &annotations_len))
2171 return 0;
2172 if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len))
2173 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002174 if (args->kwarg && args->kwarg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002175 !compiler_visit_argannotation(c, args->kwarg->arg,
Yurii Karabas73019792020-11-25 12:43:18 +02002176 args->kwarg->annotation, &annotations_len))
2177 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 if (!return_str) {
2180 return_str = PyUnicode_InternFromString("return");
2181 if (!return_str)
Yurii Karabas73019792020-11-25 12:43:18 +02002182 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 }
Yurii Karabas73019792020-11-25 12:43:18 +02002184 if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) {
2185 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 }
2187
Yurii Karabas73019792020-11-25 12:43:18 +02002188 if (annotations_len) {
2189 ADDOP_I(c, BUILD_TUPLE, annotations_len);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002190 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002192
Yurii Karabas73019792020-11-25 12:43:18 +02002193 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002194}
2195
2196static int
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002197compiler_visit_defaults(struct compiler *c, arguments_ty args)
2198{
2199 VISIT_SEQ(c, expr, args->defaults);
2200 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
2201 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002202}
2203
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002204static Py_ssize_t
2205compiler_default_arguments(struct compiler *c, arguments_ty args)
2206{
2207 Py_ssize_t funcflags = 0;
2208 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002209 if (!compiler_visit_defaults(c, args))
2210 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002211 funcflags |= 0x01;
2212 }
2213 if (args->kwonlyargs) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002214 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002215 args->kw_defaults);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002216 if (res == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002217 return -1;
2218 }
2219 else if (res > 0) {
2220 funcflags |= 0x02;
2221 }
2222 }
2223 return funcflags;
2224}
2225
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002226static int
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002227forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx)
2228{
2229
2230 if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2231 compiler_error(c, "cannot assign to __debug__");
2232 return 1;
2233 }
2234 return 0;
2235}
2236
2237static int
2238compiler_check_debug_one_arg(struct compiler *c, arg_ty arg)
2239{
2240 if (arg != NULL) {
2241 if (forbidden_name(c, arg->arg, Store))
2242 return 0;
2243 }
2244 return 1;
2245}
2246
2247static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002248compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args)
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002249{
2250 if (args != NULL) {
Pablo Galindoee40e4b2020-04-23 03:43:08 +01002251 for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002252 if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i)))
2253 return 0;
2254 }
2255 }
2256 return 1;
2257}
2258
2259static int
2260compiler_check_debug_args(struct compiler *c, arguments_ty args)
2261{
2262 if (!compiler_check_debug_args_seq(c, args->posonlyargs))
2263 return 0;
2264 if (!compiler_check_debug_args_seq(c, args->args))
2265 return 0;
2266 if (!compiler_check_debug_one_arg(c, args->vararg))
2267 return 0;
2268 if (!compiler_check_debug_args_seq(c, args->kwonlyargs))
2269 return 0;
2270 if (!compiler_check_debug_one_arg(c, args->kwarg))
2271 return 0;
2272 return 1;
2273}
2274
2275static int
Yury Selivanov75445082015-05-11 22:57:16 -04002276compiler_function(struct compiler *c, stmt_ty s, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002277{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 PyCodeObject *co;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002279 PyObject *qualname, *docstring = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002280 arguments_ty args;
2281 expr_ty returns;
2282 identifier name;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002283 asdl_expr_seq* decos;
2284 asdl_stmt_seq *body;
INADA Naokicb41b272017-02-23 00:31:59 +09002285 Py_ssize_t i, funcflags;
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002286 int annotations;
Yury Selivanov75445082015-05-11 22:57:16 -04002287 int scope_type;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002288 int firstlineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002289
Yury Selivanov75445082015-05-11 22:57:16 -04002290 if (is_async) {
2291 assert(s->kind == AsyncFunctionDef_kind);
2292
2293 args = s->v.AsyncFunctionDef.args;
2294 returns = s->v.AsyncFunctionDef.returns;
2295 decos = s->v.AsyncFunctionDef.decorator_list;
2296 name = s->v.AsyncFunctionDef.name;
2297 body = s->v.AsyncFunctionDef.body;
2298
2299 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
2300 } else {
2301 assert(s->kind == FunctionDef_kind);
2302
2303 args = s->v.FunctionDef.args;
2304 returns = s->v.FunctionDef.returns;
2305 decos = s->v.FunctionDef.decorator_list;
2306 name = s->v.FunctionDef.name;
2307 body = s->v.FunctionDef.body;
2308
2309 scope_type = COMPILER_SCOPE_FUNCTION;
2310 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002311
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002312 if (!compiler_check_debug_args(c, args))
2313 return 0;
2314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 if (!compiler_decorators(c, decos))
2316 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002317
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002318 firstlineno = s->lineno;
2319 if (asdl_seq_LEN(decos)) {
2320 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2321 }
2322
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002323 funcflags = compiler_default_arguments(c, args);
2324 if (funcflags == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002325 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002326 }
2327
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002328 annotations = compiler_visit_annotations(c, args, returns);
2329 if (annotations == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002330 return 0;
2331 }
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002332 else if (annotations > 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002333 funcflags |= 0x04;
2334 }
2335
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002336 if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002337 return 0;
2338 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002339
INADA Naokicb41b272017-02-23 00:31:59 +09002340 /* if not -OO mode, add docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002341 if (c->c_optimize < 2) {
2342 docstring = _PyAST_GetDocString(body);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002343 }
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002344 if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 compiler_exit_scope(c);
2346 return 0;
2347 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002350 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Mark Shannon877df852020-11-12 09:43:29 +00002352 for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) {
Mark Shannonfd009e62020-11-13 12:53:53 +00002353 VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i));
Mark Shannon877df852020-11-12 09:43:29 +00002354 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002356 qualname = c->u->u_qualname;
2357 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002359 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002360 Py_XDECREF(qualname);
2361 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002363 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002364
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002365 if (!compiler_make_closure(c, co, funcflags, qualname)) {
2366 Py_DECREF(qualname);
2367 Py_DECREF(co);
2368 return 0;
2369 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002370 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 /* decorators */
2374 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2375 ADDOP_I(c, CALL_FUNCTION, 1);
2376 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002377
Yury Selivanov75445082015-05-11 22:57:16 -04002378 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002379}
2380
2381static int
2382compiler_class(struct compiler *c, stmt_ty s)
2383{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002384 PyCodeObject *co;
2385 PyObject *str;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002386 int i, firstlineno;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002387 asdl_expr_seq *decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 if (!compiler_decorators(c, decos))
2390 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002391
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002392 firstlineno = s->lineno;
2393 if (asdl_seq_LEN(decos)) {
2394 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2395 }
2396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002397 /* ultimately generate code for:
2398 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
2399 where:
2400 <func> is a function/closure created from the class body;
2401 it has a single argument (__locals__) where the dict
2402 (or MutableSequence) representing the locals is passed
2403 <name> is the class name
2404 <bases> is the positional arguments and *varargs argument
2405 <keywords> is the keyword arguments and **kwds argument
2406 This borrows from compiler_call.
2407 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002410 if (!compiler_enter_scope(c, s->v.ClassDef.name,
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002411 COMPILER_SCOPE_CLASS, (void *)s, firstlineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 return 0;
2413 /* this block represents what we do in the new scope */
2414 {
2415 /* use the class name for name mangling */
2416 Py_INCREF(s->v.ClassDef.name);
Serhiy Storchaka48842712016-04-06 09:45:48 +03002417 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 /* load (global) __name__ ... */
2419 str = PyUnicode_InternFromString("__name__");
2420 if (!str || !compiler_nameop(c, str, Load)) {
2421 Py_XDECREF(str);
2422 compiler_exit_scope(c);
2423 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002424 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 Py_DECREF(str);
2426 /* ... and store it as __module__ */
2427 str = PyUnicode_InternFromString("__module__");
2428 if (!str || !compiler_nameop(c, str, Store)) {
2429 Py_XDECREF(str);
2430 compiler_exit_scope(c);
2431 return 0;
2432 }
2433 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002434 assert(c->u->u_qualname);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002435 ADDOP_LOAD_CONST(c, c->u->u_qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002436 str = PyUnicode_InternFromString("__qualname__");
2437 if (!str || !compiler_nameop(c, str, Store)) {
2438 Py_XDECREF(str);
2439 compiler_exit_scope(c);
2440 return 0;
2441 }
2442 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 /* compile the body proper */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002444 if (!compiler_body(c, s->v.ClassDef.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 compiler_exit_scope(c);
2446 return 0;
2447 }
Mark Shannone56d54e2021-01-15 13:52:00 +00002448 /* The following code is artificial */
2449 c->u->u_lineno = -1;
Nick Coghlan19d24672016-12-05 16:47:55 +10002450 /* Return __classcell__ if it is referenced, otherwise return None */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002451 if (c->u->u_ste->ste_needs_class_closure) {
Nick Coghlan19d24672016-12-05 16:47:55 +10002452 /* Store __classcell__ into class namespace & return it */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002453 str = PyUnicode_InternFromString("__class__");
2454 if (str == NULL) {
2455 compiler_exit_scope(c);
2456 return 0;
2457 }
2458 i = compiler_lookup_arg(c->u->u_cellvars, str);
2459 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01002460 if (i < 0) {
2461 compiler_exit_scope(c);
2462 return 0;
2463 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002464 assert(i == 0);
Nick Coghlan944368e2016-09-11 14:45:49 +10002465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 ADDOP_I(c, LOAD_CLOSURE, i);
Nick Coghlan19d24672016-12-05 16:47:55 +10002467 ADDOP(c, DUP_TOP);
Nick Coghlan944368e2016-09-11 14:45:49 +10002468 str = PyUnicode_InternFromString("__classcell__");
2469 if (!str || !compiler_nameop(c, str, Store)) {
2470 Py_XDECREF(str);
2471 compiler_exit_scope(c);
2472 return 0;
2473 }
2474 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002476 else {
Nick Coghlan19d24672016-12-05 16:47:55 +10002477 /* No methods referenced __class__, so just return None */
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002478 assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002479 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson312595c2013-05-15 15:26:42 -05002480 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002481 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 /* create the code object */
2483 co = assemble(c, 1);
2484 }
2485 /* leave the new scope */
2486 compiler_exit_scope(c);
2487 if (co == NULL)
2488 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 /* 2. load the 'build_class' function */
2491 ADDOP(c, LOAD_BUILD_CLASS);
2492
2493 /* 3. load a function (or closure) made from the code object */
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002494 if (!compiler_make_closure(c, co, 0, NULL)) {
2495 Py_DECREF(co);
2496 return 0;
2497 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002498 Py_DECREF(co);
2499
2500 /* 4. load class name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002501 ADDOP_LOAD_CONST(c, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502
2503 /* 5. generate the rest of the code for the call */
Pablo Galindoa5634c42020-09-16 19:42:00 +01002504 if (!compiler_call_helper(c, 2, s->v.ClassDef.bases, s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 return 0;
2506
2507 /* 6. apply decorators */
2508 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2509 ADDOP_I(c, CALL_FUNCTION, 1);
2510 }
2511
2512 /* 7. store into <name> */
2513 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2514 return 0;
2515 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002516}
2517
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02002518/* Return 0 if the expression is a constant value except named singletons.
2519 Return 1 otherwise. */
2520static int
2521check_is_arg(expr_ty e)
2522{
2523 if (e->kind != Constant_kind) {
2524 return 1;
2525 }
2526 PyObject *value = e->v.Constant.value;
2527 return (value == Py_None
2528 || value == Py_False
2529 || value == Py_True
2530 || value == Py_Ellipsis);
2531}
2532
2533/* Check operands of identity chacks ("is" and "is not").
2534 Emit a warning if any operand is a constant except named singletons.
2535 Return 0 on error.
2536 */
2537static int
2538check_compare(struct compiler *c, expr_ty e)
2539{
2540 Py_ssize_t i, n;
2541 int left = check_is_arg(e->v.Compare.left);
2542 n = asdl_seq_LEN(e->v.Compare.ops);
2543 for (i = 0; i < n; i++) {
2544 cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i);
2545 int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2546 if (op == Is || op == IsNot) {
2547 if (!right || !left) {
2548 const char *msg = (op == Is)
2549 ? "\"is\" with a literal. Did you mean \"==\"?"
2550 : "\"is not\" with a literal. Did you mean \"!=\"?";
2551 return compiler_warn(c, msg);
2552 }
2553 }
2554 left = right;
2555 }
2556 return 1;
2557}
2558
Mark Shannon9af0e472020-01-14 10:12:45 +00002559static int compiler_addcompare(struct compiler *c, cmpop_ty op)
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002560{
Mark Shannon9af0e472020-01-14 10:12:45 +00002561 int cmp;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002562 switch (op) {
2563 case Eq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002564 cmp = Py_EQ;
2565 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002566 case NotEq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002567 cmp = Py_NE;
2568 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002569 case Lt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002570 cmp = Py_LT;
2571 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002572 case LtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002573 cmp = Py_LE;
2574 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002575 case Gt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002576 cmp = Py_GT;
2577 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002578 case GtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002579 cmp = Py_GE;
2580 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002581 case Is:
Mark Shannon9af0e472020-01-14 10:12:45 +00002582 ADDOP_I(c, IS_OP, 0);
2583 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002584 case IsNot:
Mark Shannon9af0e472020-01-14 10:12:45 +00002585 ADDOP_I(c, IS_OP, 1);
2586 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002587 case In:
Mark Shannon9af0e472020-01-14 10:12:45 +00002588 ADDOP_I(c, CONTAINS_OP, 0);
2589 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002590 case NotIn:
Mark Shannon9af0e472020-01-14 10:12:45 +00002591 ADDOP_I(c, CONTAINS_OP, 1);
2592 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002593 default:
Mark Shannon9af0e472020-01-14 10:12:45 +00002594 Py_UNREACHABLE();
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002595 }
Mark Shannon9af0e472020-01-14 10:12:45 +00002596 ADDOP_I(c, COMPARE_OP, cmp);
2597 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002598}
2599
Mark Shannon9af0e472020-01-14 10:12:45 +00002600
2601
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002602static int
2603compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2604{
2605 switch (e->kind) {
2606 case UnaryOp_kind:
2607 if (e->v.UnaryOp.op == Not)
2608 return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2609 /* fallback to general implementation */
2610 break;
2611 case BoolOp_kind: {
Pablo Galindoa5634c42020-09-16 19:42:00 +01002612 asdl_expr_seq *s = e->v.BoolOp.values;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002613 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2614 assert(n >= 0);
2615 int cond2 = e->v.BoolOp.op == Or;
2616 basicblock *next2 = next;
2617 if (!cond2 != !cond) {
2618 next2 = compiler_new_block(c);
2619 if (next2 == NULL)
2620 return 0;
2621 }
2622 for (i = 0; i < n; ++i) {
2623 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2624 return 0;
2625 }
2626 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2627 return 0;
2628 if (next2 != next)
2629 compiler_use_next_block(c, next2);
2630 return 1;
2631 }
2632 case IfExp_kind: {
2633 basicblock *end, *next2;
2634 end = compiler_new_block(c);
2635 if (end == NULL)
2636 return 0;
2637 next2 = compiler_new_block(c);
2638 if (next2 == NULL)
2639 return 0;
2640 if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2641 return 0;
2642 if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2643 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002644 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002645 compiler_use_next_block(c, next2);
2646 if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2647 return 0;
2648 compiler_use_next_block(c, end);
2649 return 1;
2650 }
2651 case Compare_kind: {
2652 Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2653 if (n > 0) {
Serhiy Storchaka45835252019-02-16 08:29:46 +02002654 if (!check_compare(c, e)) {
2655 return 0;
2656 }
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002657 basicblock *cleanup = compiler_new_block(c);
2658 if (cleanup == NULL)
2659 return 0;
2660 VISIT(c, expr, e->v.Compare.left);
2661 for (i = 0; i < n; i++) {
2662 VISIT(c, expr,
2663 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2664 ADDOP(c, DUP_TOP);
2665 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00002666 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01002667 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002668 NEXT_BLOCK(c);
2669 }
2670 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00002671 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Mark Shannon582aaf12020-08-04 17:30:11 +01002672 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Mark Shannon266b4622020-11-17 19:30:14 +00002673 NEXT_BLOCK(c);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002674 basicblock *end = compiler_new_block(c);
2675 if (end == NULL)
2676 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002677 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002678 compiler_use_next_block(c, cleanup);
2679 ADDOP(c, POP_TOP);
2680 if (!cond) {
Mark Shannon127dde52021-01-04 18:06:55 +00002681 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002682 }
2683 compiler_use_next_block(c, end);
2684 return 1;
2685 }
2686 /* fallback to general implementation */
2687 break;
2688 }
2689 default:
2690 /* fallback to general implementation */
2691 break;
2692 }
2693
2694 /* general implementation */
2695 VISIT(c, expr, e);
Mark Shannon582aaf12020-08-04 17:30:11 +01002696 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Mark Shannon266b4622020-11-17 19:30:14 +00002697 NEXT_BLOCK(c);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002698 return 1;
2699}
2700
2701static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002702compiler_ifexp(struct compiler *c, expr_ty e)
2703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002704 basicblock *end, *next;
2705
2706 assert(e->kind == IfExp_kind);
2707 end = compiler_new_block(c);
2708 if (end == NULL)
2709 return 0;
2710 next = compiler_new_block(c);
2711 if (next == NULL)
2712 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002713 if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2714 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002715 VISIT(c, expr, e->v.IfExp.body);
Mark Shannon127dde52021-01-04 18:06:55 +00002716 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002717 compiler_use_next_block(c, next);
2718 VISIT(c, expr, e->v.IfExp.orelse);
2719 compiler_use_next_block(c, end);
2720 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002721}
2722
2723static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002724compiler_lambda(struct compiler *c, expr_ty e)
2725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002726 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002727 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002728 static identifier name;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002729 Py_ssize_t funcflags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002730 arguments_ty args = e->v.Lambda.args;
2731 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002732
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002733 if (!compiler_check_debug_args(c, args))
2734 return 0;
2735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002736 if (!name) {
2737 name = PyUnicode_InternFromString("<lambda>");
2738 if (!name)
2739 return 0;
2740 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002741
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002742 funcflags = compiler_default_arguments(c, args);
2743 if (funcflags == -1) {
2744 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002745 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002746
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002747 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002748 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002749 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 /* Make None the first constant, so the lambda can't have a
2752 docstring. */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002753 if (compiler_add_const(c, Py_None) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002757 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002758 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2759 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2760 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002761 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 }
2763 else {
2764 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002765 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002767 qualname = c->u->u_qualname;
2768 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002769 compiler_exit_scope(c);
Pablo Galindo7fdab832021-01-29 22:40:59 +00002770 if (co == NULL) {
2771 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 return 0;
Pablo Galindo7fdab832021-01-29 22:40:59 +00002773 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002774
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002775 if (!compiler_make_closure(c, co, funcflags, qualname)) {
2776 Py_DECREF(qualname);
2777 Py_DECREF(co);
2778 return 0;
2779 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002780 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 Py_DECREF(co);
2782
2783 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002784}
2785
2786static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002787compiler_if(struct compiler *c, stmt_ty s)
2788{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002789 basicblock *end, *next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002790 assert(s->kind == If_kind);
2791 end = compiler_new_block(c);
Mark Shannon8473cf82020-12-15 11:07:50 +00002792 if (end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 return 0;
Mark Shannon8473cf82020-12-15 11:07:50 +00002794 }
2795 if (asdl_seq_LEN(s->v.If.orelse)) {
2796 next = compiler_new_block(c);
2797 if (next == NULL) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002798 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002799 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002800 }
2801 else {
2802 next = end;
2803 }
2804 if (!compiler_jump_if(c, s->v.If.test, next, 0)) {
2805 return 0;
2806 }
2807 VISIT_SEQ(c, stmt, s->v.If.body);
2808 if (asdl_seq_LEN(s->v.If.orelse)) {
Mark Shannon127dde52021-01-04 18:06:55 +00002809 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Mark Shannon8473cf82020-12-15 11:07:50 +00002810 compiler_use_next_block(c, next);
2811 VISIT_SEQ(c, stmt, s->v.If.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 }
2813 compiler_use_next_block(c, end);
2814 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002815}
2816
2817static int
2818compiler_for(struct compiler *c, stmt_ty s)
2819{
Mark Shannon5977a792020-12-02 13:31:40 +00002820 basicblock *start, *body, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002822 start = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002823 body = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 cleanup = compiler_new_block(c);
2825 end = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002826 if (start == NULL || body == NULL || end == NULL || cleanup == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002828 }
2829 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002831 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 VISIT(c, expr, s->v.For.iter);
2833 ADDOP(c, GET_ITER);
2834 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01002835 ADDOP_JUMP(c, FOR_ITER, cleanup);
Mark Shannon5977a792020-12-02 13:31:40 +00002836 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 VISIT(c, expr, s->v.For.target);
2838 VISIT_SEQ(c, stmt, s->v.For.body);
Mark Shannonf5e97b72020-12-14 11:28:39 +00002839 /* Mark jump as artificial */
2840 c->u->u_lineno = -1;
Mark Shannon582aaf12020-08-04 17:30:11 +01002841 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 compiler_use_next_block(c, cleanup);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002843
2844 compiler_pop_fblock(c, FOR_LOOP, start);
2845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 VISIT_SEQ(c, stmt, s->v.For.orelse);
2847 compiler_use_next_block(c, end);
2848 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002849}
2850
Yury Selivanov75445082015-05-11 22:57:16 -04002851
2852static int
2853compiler_async_for(struct compiler *c, stmt_ty s)
2854{
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002855 basicblock *start, *except, *end;
Pablo Galindo90235812020-03-15 04:29:22 +00002856 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07002857 c->u->u_ste->ste_coroutine = 1;
2858 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
Zsolt Dollensteine2396502018-04-27 08:58:56 -07002859 return compiler_error(c, "'async for' outside async function");
2860 }
2861
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002862 start = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002863 except = compiler_new_block(c);
2864 end = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002865
Mark Shannonfee55262019-11-21 09:11:43 +00002866 if (start == NULL || except == NULL || end == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002867 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002868 }
Yury Selivanov75445082015-05-11 22:57:16 -04002869 VISIT(c, expr, s->v.AsyncFor.iter);
2870 ADDOP(c, GET_AITER);
Yury Selivanov75445082015-05-11 22:57:16 -04002871
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002872 compiler_use_next_block(c, start);
Mark Shannonfee55262019-11-21 09:11:43 +00002873 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002874 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002875 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002876 /* SETUP_FINALLY to guard the __anext__ call */
Mark Shannon582aaf12020-08-04 17:30:11 +01002877 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov75445082015-05-11 22:57:16 -04002878 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002879 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04002880 ADDOP(c, YIELD_FROM);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002881 ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */
Yury Selivanov75445082015-05-11 22:57:16 -04002882
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002883 /* Success block for __anext__ */
2884 VISIT(c, expr, s->v.AsyncFor.target);
2885 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002886 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002887
2888 compiler_pop_fblock(c, FOR_LOOP, start);
Yury Selivanov75445082015-05-11 22:57:16 -04002889
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002890 /* Except block for __anext__ */
Yury Selivanov75445082015-05-11 22:57:16 -04002891 compiler_use_next_block(c, except);
Mark Shannon877df852020-11-12 09:43:29 +00002892
2893 c->u->u_lineno = -1;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002894 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov75445082015-05-11 22:57:16 -04002895
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002896 /* `else` block */
Yury Selivanov75445082015-05-11 22:57:16 -04002897 VISIT_SEQ(c, stmt, s->v.For.orelse);
2898
2899 compiler_use_next_block(c, end);
2900
2901 return 1;
2902}
2903
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002904static int
2905compiler_while(struct compiler *c, stmt_ty s)
2906{
Mark Shannon266b4622020-11-17 19:30:14 +00002907 basicblock *loop, *body, *end, *anchor = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 loop = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002909 body = compiler_new_block(c);
2910 anchor = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 end = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002912 if (loop == NULL || body == NULL || anchor == NULL || end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002914 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 compiler_use_next_block(c, loop);
Mark Shannon266b4622020-11-17 19:30:14 +00002916 if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002919 if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) {
2920 return 0;
Mark Shannon266b4622020-11-17 19:30:14 +00002921 }
2922
2923 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 VISIT_SEQ(c, stmt, s->v.While.body);
Mark Shannon8473cf82020-12-15 11:07:50 +00002925 SET_LOC(c, s);
2926 if (!compiler_jump_if(c, s->v.While.test, body, 1)) {
2927 return 0;
2928 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002929
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002930 compiler_pop_fblock(c, WHILE_LOOP, loop);
2931
Mark Shannon266b4622020-11-17 19:30:14 +00002932 compiler_use_next_block(c, anchor);
2933 if (s->v.While.orelse) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 VISIT_SEQ(c, stmt, s->v.While.orelse);
Mark Shannon266b4622020-11-17 19:30:14 +00002935 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002938 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002939}
2940
2941static int
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002942compiler_return(struct compiler *c, stmt_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002943{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002944 int preserve_tos = ((s->v.Return.value != NULL) &&
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002945 (s->v.Return.value->kind != Constant_kind));
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002946 if (c->u->u_ste->ste_type != FunctionBlock)
2947 return compiler_error(c, "'return' outside function");
2948 if (s->v.Return.value != NULL &&
2949 c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2950 {
2951 return compiler_error(
2952 c, "'return' with value in async generator");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002953 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002954 if (preserve_tos) {
2955 VISIT(c, expr, s->v.Return.value);
Mark Shannon5274b682020-12-16 13:07:01 +00002956 } else {
2957 /* Emit instruction with line number for expression */
2958 if (s->v.Return.value != NULL) {
2959 SET_LOC(c, s->v.Return.value);
2960 ADDOP(c, NOP);
2961 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002962 }
Mark Shannonfee55262019-11-21 09:11:43 +00002963 if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL))
2964 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002965 if (s->v.Return.value == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002966 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002967 }
2968 else if (!preserve_tos) {
Mark Shannon5274b682020-12-16 13:07:01 +00002969 ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002970 }
2971 ADDOP(c, RETURN_VALUE);
Mark Shannon266b4622020-11-17 19:30:14 +00002972 NEXT_BLOCK(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002974 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002975}
2976
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002977static int
2978compiler_break(struct compiler *c)
2979{
Mark Shannonfee55262019-11-21 09:11:43 +00002980 struct fblockinfo *loop = NULL;
2981 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
2982 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002983 }
Mark Shannonfee55262019-11-21 09:11:43 +00002984 if (loop == NULL) {
2985 return compiler_error(c, "'break' outside loop");
2986 }
2987 if (!compiler_unwind_fblock(c, loop, 0)) {
2988 return 0;
2989 }
Mark Shannon582aaf12020-08-04 17:30:11 +01002990 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit);
Mark Shannon266b4622020-11-17 19:30:14 +00002991 NEXT_BLOCK(c);
Mark Shannonfee55262019-11-21 09:11:43 +00002992 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002993}
2994
2995static int
2996compiler_continue(struct compiler *c)
2997{
Mark Shannonfee55262019-11-21 09:11:43 +00002998 struct fblockinfo *loop = NULL;
2999 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
3000 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003001 }
Mark Shannonfee55262019-11-21 09:11:43 +00003002 if (loop == NULL) {
3003 return compiler_error(c, "'continue' not properly in loop");
3004 }
Mark Shannon582aaf12020-08-04 17:30:11 +01003005 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block);
Mark Shannon266b4622020-11-17 19:30:14 +00003006 NEXT_BLOCK(c)
Mark Shannonfee55262019-11-21 09:11:43 +00003007 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003008}
3009
3010
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003011/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003012
3013 SETUP_FINALLY L
3014 <code for body>
3015 POP_BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00003016 <code for finalbody>
3017 JUMP E
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003018 L:
3019 <code for finalbody>
Mark Shannonfee55262019-11-21 09:11:43 +00003020 E:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003021
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003022 The special instructions use the block stack. Each block
3023 stack entry contains the instruction that created it (here
3024 SETUP_FINALLY), the level of the value stack at the time the
3025 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003026
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003027 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003028 Pushes the current value stack level and the label
3029 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003030 POP_BLOCK:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003031 Pops en entry from the block stack.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003033 The block stack is unwound when an exception is raised:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003034 when a SETUP_FINALLY entry is found, the raised and the caught
3035 exceptions are pushed onto the value stack (and the exception
3036 condition is cleared), and the interpreter jumps to the label
3037 gotten from the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003038*/
3039
3040static int
3041compiler_try_finally(struct compiler *c, stmt_ty s)
3042{
Mark Shannonfee55262019-11-21 09:11:43 +00003043 basicblock *body, *end, *exit;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003045 body = compiler_new_block(c);
3046 end = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00003047 exit = compiler_new_block(c);
3048 if (body == NULL || end == NULL || exit == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003050
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003051 /* `try` block */
Mark Shannon582aaf12020-08-04 17:30:11 +01003052 ADDOP_JUMP(c, SETUP_FINALLY, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003053 compiler_use_next_block(c, body);
Mark Shannonfee55262019-11-21 09:11:43 +00003054 if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003056 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
3057 if (!compiler_try_except(c, s))
3058 return 0;
3059 }
3060 else {
3061 VISIT_SEQ(c, stmt, s->v.Try.body);
3062 }
Mark Shannon3bd60352021-01-13 12:05:43 +00003063 ADDOP_NOLINE(c, POP_BLOCK);
Mark Shannonfee55262019-11-21 09:11:43 +00003064 compiler_pop_fblock(c, FINALLY_TRY, body);
3065 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Mark Shannon127dde52021-01-04 18:06:55 +00003066 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00003067 /* `finally` block */
3068 compiler_use_next_block(c, end);
3069 if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL))
3070 return 0;
3071 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3072 compiler_pop_fblock(c, FINALLY_END, end);
Mark Shannonbf353f32020-12-17 13:55:28 +00003073 ADDOP_I(c, RERAISE, 0);
Mark Shannonfee55262019-11-21 09:11:43 +00003074 compiler_use_next_block(c, exit);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003075 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003076}
3077
3078/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003079 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003080 (The contents of the value stack is shown in [], with the top
3081 at the right; 'tb' is trace-back info, 'val' the exception's
3082 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083
3084 Value stack Label Instruction Argument
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003085 [] SETUP_FINALLY L1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003086 [] <code for S>
3087 [] POP_BLOCK
3088 [] JUMP_FORWARD L0
3089
3090 [tb, val, exc] L1: DUP )
3091 [tb, val, exc, exc] <evaluate E1> )
Mark Shannon9af0e472020-01-14 10:12:45 +00003092 [tb, val, exc, exc, E1] JUMP_IF_NOT_EXC_MATCH L2 ) only if E1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093 [tb, val, exc] POP
3094 [tb, val] <assign to V1> (or POP if no V1)
3095 [tb] POP
3096 [] <code for S1>
3097 JUMP_FORWARD L0
3098
3099 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003100 .............................etc.......................
3101
Mark Shannonfee55262019-11-21 09:11:43 +00003102 [tb, val, exc] Ln+1: RERAISE # re-raise exception
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103
3104 [] L0: <next statement>
3105
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003106 Of course, parts are not generated if Vi or Ei is not present.
3107*/
3108static int
3109compiler_try_except(struct compiler *c, stmt_ty s)
3110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003111 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003112 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003114 body = compiler_new_block(c);
3115 except = compiler_new_block(c);
3116 orelse = compiler_new_block(c);
3117 end = compiler_new_block(c);
3118 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
3119 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01003120 ADDOP_JUMP(c, SETUP_FINALLY, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 compiler_use_next_block(c, body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003122 if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003123 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003124 VISIT_SEQ(c, stmt, s->v.Try.body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003125 compiler_pop_fblock(c, TRY_EXCEPT, body);
Mark Shannon3bd60352021-01-13 12:05:43 +00003126 ADDOP_NOLINE(c, POP_BLOCK);
3127 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003128 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 compiler_use_next_block(c, except);
Mark Shannon02d126a2020-09-25 14:04:19 +01003130 /* Runtime will push a block here, so we need to account for that */
3131 if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL))
3132 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003133 for (i = 0; i < n; i++) {
3134 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003135 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 if (!handler->v.ExceptHandler.type && i < n-1)
3137 return compiler_error(c, "default 'except:' must be last");
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003138 SET_LOC(c, handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 except = compiler_new_block(c);
3140 if (except == NULL)
3141 return 0;
3142 if (handler->v.ExceptHandler.type) {
3143 ADDOP(c, DUP_TOP);
3144 VISIT(c, expr, handler->v.ExceptHandler.type);
Mark Shannon582aaf12020-08-04 17:30:11 +01003145 ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except);
Mark Shannon266b4622020-11-17 19:30:14 +00003146 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003147 }
3148 ADDOP(c, POP_TOP);
3149 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003150 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00003151
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003152 cleanup_end = compiler_new_block(c);
3153 cleanup_body = compiler_new_block(c);
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003154 if (cleanup_end == NULL || cleanup_body == NULL) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003155 return 0;
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003156 }
Guido van Rossumb940e112007-01-10 16:19:56 +00003157
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003158 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3159 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003160
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003161 /*
3162 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003163 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003164 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003165 try:
3166 # body
3167 finally:
Chris Angelicoad098b62019-05-21 23:34:19 +10003168 name = None # in case body contains "del name"
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003169 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003170 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003171
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003172 /* second try: */
Mark Shannon582aaf12020-08-04 17:30:11 +01003173 ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003174 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003175 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003176 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003177
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003178 /* second # body */
3179 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003180 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003181 ADDOP(c, POP_BLOCK);
3182 ADDOP(c, POP_EXCEPT);
Mark Shannon877df852020-11-12 09:43:29 +00003183 /* name = None; del name; # Mark as artificial */
3184 c->u->u_lineno = -1;
Mark Shannonfee55262019-11-21 09:11:43 +00003185 ADDOP_LOAD_CONST(c, Py_None);
3186 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3187 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Mark Shannon582aaf12020-08-04 17:30:11 +01003188 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189
Mark Shannonfee55262019-11-21 09:11:43 +00003190 /* except: */
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003191 compiler_use_next_block(c, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003192
Mark Shannon877df852020-11-12 09:43:29 +00003193 /* name = None; del name; # Mark as artificial */
3194 c->u->u_lineno = -1;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003195 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003196 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003197 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198
Mark Shannonbf353f32020-12-17 13:55:28 +00003199 ADDOP_I(c, RERAISE, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003200 }
3201 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003202 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003203
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003204 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05003205 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003206 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207
Guido van Rossumb940e112007-01-10 16:19:56 +00003208 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003209 ADDOP(c, POP_TOP);
3210 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003211 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003212 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003213 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003214 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannon127dde52021-01-04 18:06:55 +00003215 /* name = None; del name; # Mark as artificial */
3216 c->u->u_lineno = -1;
Mark Shannonfee55262019-11-21 09:11:43 +00003217 ADDOP(c, POP_EXCEPT);
Mark Shannon582aaf12020-08-04 17:30:11 +01003218 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003220 compiler_use_next_block(c, except);
3221 }
Mark Shannon02d126a2020-09-25 14:04:19 +01003222 compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL);
Mark Shannonf2dbfd72020-12-21 13:53:50 +00003223 /* Mark as artificial */
3224 c->u->u_lineno = -1;
Mark Shannonbf353f32020-12-17 13:55:28 +00003225 ADDOP_I(c, RERAISE, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003226 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003227 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003228 compiler_use_next_block(c, end);
3229 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003230}
3231
3232static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003233compiler_try(struct compiler *c, stmt_ty s) {
3234 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
3235 return compiler_try_finally(c, s);
3236 else
3237 return compiler_try_except(c, s);
3238}
3239
3240
3241static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003242compiler_import_as(struct compiler *c, identifier name, identifier asname)
3243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003244 /* The IMPORT_NAME opcode was already generated. This function
3245 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003247 If there is a dot in name, we need to split it and emit a
Serhiy Storchakaf93234b2017-05-09 22:31:05 +03003248 IMPORT_FROM for each name.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003249 */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003250 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
3251 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003252 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003253 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003254 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003255 /* Consume the base module name to get the first attribute */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003256 while (1) {
3257 Py_ssize_t pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003258 PyObject *attr;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003259 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003260 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003261 return 0;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003262 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 if (!attr)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003264 return 0;
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003265 ADDOP_N(c, IMPORT_FROM, attr, names);
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003266 if (dot == -1) {
3267 break;
3268 }
3269 ADDOP(c, ROT_TWO);
3270 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003271 }
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003272 if (!compiler_nameop(c, asname, Store)) {
3273 return 0;
3274 }
3275 ADDOP(c, POP_TOP);
3276 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003277 }
3278 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003279}
3280
3281static int
3282compiler_import(struct compiler *c, stmt_ty s)
3283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003284 /* The Import node stores a module name like a.b.c as a single
3285 string. This is convenient for all cases except
3286 import a.b.c as d
3287 where we need to parse that string to extract the individual
3288 module names.
3289 XXX Perhaps change the representation to make this case simpler?
3290 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01003291 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003292
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003293 PyObject *zero = _PyLong_GetZero(); // borrowed reference
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003294 for (i = 0; i < n; i++) {
3295 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
3296 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003297
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003298 ADDOP_LOAD_CONST(c, zero);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003299 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003300 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 if (alias->asname) {
3303 r = compiler_import_as(c, alias->name, alias->asname);
3304 if (!r)
3305 return r;
3306 }
3307 else {
3308 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003309 Py_ssize_t dot = PyUnicode_FindChar(
3310 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02003311 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003312 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02003313 if (tmp == NULL)
3314 return 0;
3315 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003317 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 Py_DECREF(tmp);
3319 }
3320 if (!r)
3321 return r;
3322 }
3323 }
3324 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003325}
3326
3327static int
3328compiler_from_import(struct compiler *c, stmt_ty s)
3329{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003330 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003331 PyObject *names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003332 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003334 if (!empty_string) {
3335 empty_string = PyUnicode_FromString("");
3336 if (!empty_string)
3337 return 0;
3338 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003339
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003340 ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02003341
3342 names = PyTuple_New(n);
3343 if (!names)
3344 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003346 /* build up the names */
3347 for (i = 0; i < n; i++) {
3348 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3349 Py_INCREF(alias->name);
3350 PyTuple_SET_ITEM(names, i, alias->name);
3351 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003353 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003354 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 Py_DECREF(names);
3356 return compiler_error(c, "from __future__ imports must occur "
3357 "at the beginning of the file");
3358 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003359 ADDOP_LOAD_CONST_NEW(c, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003361 if (s->v.ImportFrom.module) {
3362 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
3363 }
3364 else {
3365 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
3366 }
3367 for (i = 0; i < n; i++) {
3368 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3369 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003370
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003371 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003372 assert(n == 1);
3373 ADDOP(c, IMPORT_STAR);
3374 return 1;
3375 }
3376
3377 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
3378 store_name = alias->name;
3379 if (alias->asname)
3380 store_name = alias->asname;
3381
3382 if (!compiler_nameop(c, store_name, Store)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003383 return 0;
3384 }
3385 }
3386 /* remove imported module */
3387 ADDOP(c, POP_TOP);
3388 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003389}
3390
3391static int
3392compiler_assert(struct compiler *c, stmt_ty s)
3393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003394 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003395
tsukasa-aua8ef4572021-03-16 22:14:41 +11003396 /* Always emit a warning if the test is a non-zero length tuple */
3397 if ((s->v.Assert.test->kind == Tuple_kind &&
3398 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) ||
3399 (s->v.Assert.test->kind == Constant_kind &&
3400 PyTuple_Check(s->v.Assert.test->v.Constant.value) &&
3401 PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0))
Serhiy Storchakad31e7732018-10-21 10:09:39 +03003402 {
3403 if (!compiler_warn(c, "assertion is always true, "
3404 "perhaps remove parentheses?"))
3405 {
Victor Stinner14e461d2013-08-26 22:28:21 +02003406 return 0;
3407 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003408 }
tsukasa-aua8ef4572021-03-16 22:14:41 +11003409 if (c->c_optimize)
3410 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003411 end = compiler_new_block(c);
3412 if (end == NULL)
3413 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003414 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
3415 return 0;
Zackery Spytzce6a0702019-08-25 03:44:09 -06003416 ADDOP(c, LOAD_ASSERTION_ERROR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003417 if (s->v.Assert.msg) {
3418 VISIT(c, expr, s->v.Assert.msg);
3419 ADDOP_I(c, CALL_FUNCTION, 1);
3420 }
3421 ADDOP_I(c, RAISE_VARARGS, 1);
3422 compiler_use_next_block(c, end);
3423 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003424}
3425
3426static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003427compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3428{
3429 if (c->c_interactive && c->c_nestlevel <= 1) {
3430 VISIT(c, expr, value);
3431 ADDOP(c, PRINT_EXPR);
3432 return 1;
3433 }
3434
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003435 if (value->kind == Constant_kind) {
Victor Stinner15a30952016-02-08 22:45:06 +01003436 /* ignore constant statement */
Mark Shannon877df852020-11-12 09:43:29 +00003437 ADDOP(c, NOP);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003438 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003439 }
3440
3441 VISIT(c, expr, value);
Mark Shannonc5440932021-03-15 14:24:25 +00003442 /* Mark POP_TOP as artificial */
3443 c->u->u_lineno = -1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003444 ADDOP(c, POP_TOP);
3445 return 1;
3446}
3447
3448static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003449compiler_visit_stmt(struct compiler *c, stmt_ty s)
3450{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003451 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003453 /* Always assign a lineno to the next instruction for a stmt. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003454 SET_LOC(c, s);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003456 switch (s->kind) {
3457 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04003458 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003459 case ClassDef_kind:
3460 return compiler_class(c, s);
3461 case Return_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003462 return compiler_return(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003463 case Delete_kind:
3464 VISIT_SEQ(c, expr, s->v.Delete.targets)
3465 break;
3466 case Assign_kind:
3467 n = asdl_seq_LEN(s->v.Assign.targets);
3468 VISIT(c, expr, s->v.Assign.value);
3469 for (i = 0; i < n; i++) {
3470 if (i < n - 1)
3471 ADDOP(c, DUP_TOP);
3472 VISIT(c, expr,
3473 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3474 }
3475 break;
3476 case AugAssign_kind:
3477 return compiler_augassign(c, s);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003478 case AnnAssign_kind:
3479 return compiler_annassign(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 case For_kind:
3481 return compiler_for(c, s);
3482 case While_kind:
3483 return compiler_while(c, s);
3484 case If_kind:
3485 return compiler_if(c, s);
Brandt Bucher145bf262021-02-26 14:51:55 -08003486 case Match_kind:
3487 return compiler_match(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 case Raise_kind:
3489 n = 0;
3490 if (s->v.Raise.exc) {
3491 VISIT(c, expr, s->v.Raise.exc);
3492 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003493 if (s->v.Raise.cause) {
3494 VISIT(c, expr, s->v.Raise.cause);
3495 n++;
3496 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003497 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01003498 ADDOP_I(c, RAISE_VARARGS, (int)n);
Mark Shannon266b4622020-11-17 19:30:14 +00003499 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003500 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003501 case Try_kind:
3502 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 case Assert_kind:
3504 return compiler_assert(c, s);
3505 case Import_kind:
3506 return compiler_import(c, s);
3507 case ImportFrom_kind:
3508 return compiler_from_import(c, s);
3509 case Global_kind:
3510 case Nonlocal_kind:
3511 break;
3512 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003513 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003514 case Pass_kind:
Mark Shannon877df852020-11-12 09:43:29 +00003515 ADDOP(c, NOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003516 break;
3517 case Break_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003518 return compiler_break(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 case Continue_kind:
3520 return compiler_continue(c);
3521 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003522 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04003523 case AsyncFunctionDef_kind:
3524 return compiler_function(c, s, 1);
3525 case AsyncWith_kind:
3526 return compiler_async_with(c, s, 0);
3527 case AsyncFor_kind:
3528 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003529 }
Yury Selivanov75445082015-05-11 22:57:16 -04003530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003531 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003532}
3533
3534static int
3535unaryop(unaryop_ty op)
3536{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003537 switch (op) {
3538 case Invert:
3539 return UNARY_INVERT;
3540 case Not:
3541 return UNARY_NOT;
3542 case UAdd:
3543 return UNARY_POSITIVE;
3544 case USub:
3545 return UNARY_NEGATIVE;
3546 default:
3547 PyErr_Format(PyExc_SystemError,
3548 "unary op %d should not be possible", op);
3549 return 0;
3550 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003551}
3552
3553static int
Andy Lester76d58772020-03-10 21:18:12 -05003554binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003555{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003556 switch (op) {
3557 case Add:
3558 return BINARY_ADD;
3559 case Sub:
3560 return BINARY_SUBTRACT;
3561 case Mult:
3562 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003563 case MatMult:
3564 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003565 case Div:
3566 return BINARY_TRUE_DIVIDE;
3567 case Mod:
3568 return BINARY_MODULO;
3569 case Pow:
3570 return BINARY_POWER;
3571 case LShift:
3572 return BINARY_LSHIFT;
3573 case RShift:
3574 return BINARY_RSHIFT;
3575 case BitOr:
3576 return BINARY_OR;
3577 case BitXor:
3578 return BINARY_XOR;
3579 case BitAnd:
3580 return BINARY_AND;
3581 case FloorDiv:
3582 return BINARY_FLOOR_DIVIDE;
3583 default:
3584 PyErr_Format(PyExc_SystemError,
3585 "binary op %d should not be possible", op);
3586 return 0;
3587 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003588}
3589
3590static int
Andy Lester76d58772020-03-10 21:18:12 -05003591inplace_binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003593 switch (op) {
3594 case Add:
3595 return INPLACE_ADD;
3596 case Sub:
3597 return INPLACE_SUBTRACT;
3598 case Mult:
3599 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003600 case MatMult:
3601 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003602 case Div:
3603 return INPLACE_TRUE_DIVIDE;
3604 case Mod:
3605 return INPLACE_MODULO;
3606 case Pow:
3607 return INPLACE_POWER;
3608 case LShift:
3609 return INPLACE_LSHIFT;
3610 case RShift:
3611 return INPLACE_RSHIFT;
3612 case BitOr:
3613 return INPLACE_OR;
3614 case BitXor:
3615 return INPLACE_XOR;
3616 case BitAnd:
3617 return INPLACE_AND;
3618 case FloorDiv:
3619 return INPLACE_FLOOR_DIVIDE;
3620 default:
3621 PyErr_Format(PyExc_SystemError,
3622 "inplace binary op %d should not be possible", op);
3623 return 0;
3624 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003625}
3626
3627static int
3628compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3629{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003630 int op, scope;
3631 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003632 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003634 PyObject *dict = c->u->u_names;
3635 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003636
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003637 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3638 !_PyUnicode_EqualToASCIIString(name, "True") &&
3639 !_PyUnicode_EqualToASCIIString(name, "False"));
Benjamin Peterson70b224d2012-12-06 17:49:58 -05003640
Pablo Galindoc5fc1562020-04-22 23:29:27 +01003641 if (forbidden_name(c, name, ctx))
3642 return 0;
3643
Serhiy Storchakabd6ec4d2017-12-18 14:29:12 +02003644 mangled = _Py_Mangle(c->u->u_private, name);
3645 if (!mangled)
3646 return 0;
3647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003648 op = 0;
3649 optype = OP_NAME;
Victor Stinner28ad12f2021-03-19 12:41:49 +01003650 scope = _PyST_GetScope(c->u->u_ste, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003651 switch (scope) {
3652 case FREE:
3653 dict = c->u->u_freevars;
3654 optype = OP_DEREF;
3655 break;
3656 case CELL:
3657 dict = c->u->u_cellvars;
3658 optype = OP_DEREF;
3659 break;
3660 case LOCAL:
3661 if (c->u->u_ste->ste_type == FunctionBlock)
3662 optype = OP_FAST;
3663 break;
3664 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04003665 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003666 optype = OP_GLOBAL;
3667 break;
3668 case GLOBAL_EXPLICIT:
3669 optype = OP_GLOBAL;
3670 break;
3671 default:
3672 /* scope can be 0 */
3673 break;
3674 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003676 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003677 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003679 switch (optype) {
3680 case OP_DEREF:
3681 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003682 case Load:
3683 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3684 break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003685 case Store: op = STORE_DEREF; break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003686 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003687 }
3688 break;
3689 case OP_FAST:
3690 switch (ctx) {
3691 case Load: op = LOAD_FAST; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003692 case Store: op = STORE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003693 case Del: op = DELETE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003694 }
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003695 ADDOP_N(c, op, mangled, varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003696 return 1;
3697 case OP_GLOBAL:
3698 switch (ctx) {
3699 case Load: op = LOAD_GLOBAL; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003700 case Store: op = STORE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003701 case Del: op = DELETE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003702 }
3703 break;
3704 case OP_NAME:
3705 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00003706 case Load: op = LOAD_NAME; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003707 case Store: op = STORE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003708 case Del: op = DELETE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003709 }
3710 break;
3711 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003713 assert(op);
Andy Lester76d58772020-03-10 21:18:12 -05003714 arg = compiler_add_o(dict, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003715 Py_DECREF(mangled);
3716 if (arg < 0)
3717 return 0;
3718 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003719}
3720
3721static int
3722compiler_boolop(struct compiler *c, expr_ty e)
3723{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003724 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003725 int jumpi;
3726 Py_ssize_t i, n;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003727 asdl_expr_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003729 assert(e->kind == BoolOp_kind);
3730 if (e->v.BoolOp.op == And)
3731 jumpi = JUMP_IF_FALSE_OR_POP;
3732 else
3733 jumpi = JUMP_IF_TRUE_OR_POP;
3734 end = compiler_new_block(c);
3735 if (end == NULL)
3736 return 0;
3737 s = e->v.BoolOp.values;
3738 n = asdl_seq_LEN(s) - 1;
3739 assert(n >= 0);
3740 for (i = 0; i < n; ++i) {
3741 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003742 ADDOP_JUMP(c, jumpi, end);
Mark Shannon6e8128f2020-07-30 10:03:00 +01003743 basicblock *next = compiler_new_block(c);
3744 if (next == NULL) {
3745 return 0;
3746 }
3747 compiler_use_next_block(c, next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003748 }
3749 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3750 compiler_use_next_block(c, end);
3751 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003752}
3753
3754static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003755starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
Mark Shannon13bc1392020-01-23 09:25:17 +00003756 int build, int add, int extend, int tuple)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003757{
3758 Py_ssize_t n = asdl_seq_LEN(elts);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003759 if (n > 2 && are_all_items_const(elts, 0, n)) {
3760 PyObject *folded = PyTuple_New(n);
3761 if (folded == NULL) {
3762 return 0;
3763 }
3764 PyObject *val;
Mark Shannon11e0b292021-04-15 14:28:56 +01003765 for (Py_ssize_t i = 0; i < n; i++) {
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003766 val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
3767 Py_INCREF(val);
3768 PyTuple_SET_ITEM(folded, i, val);
3769 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003770 if (tuple) {
3771 ADDOP_LOAD_CONST_NEW(c, folded);
3772 } else {
3773 if (add == SET_ADD) {
3774 Py_SETREF(folded, PyFrozenSet_New(folded));
3775 if (folded == NULL) {
3776 return 0;
3777 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003778 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003779 ADDOP_I(c, build, pushed);
3780 ADDOP_LOAD_CONST_NEW(c, folded);
3781 ADDOP_I(c, extend, 1);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003782 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003783 return 1;
3784 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003785
Mark Shannon11e0b292021-04-15 14:28:56 +01003786 int big = n+pushed > STACK_USE_GUIDELINE;
3787 int seen_star = 0;
3788 for (Py_ssize_t i = 0; i < n; i++) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003789 expr_ty elt = asdl_seq_GET(elts, i);
3790 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003791 seen_star = 1;
3792 }
3793 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003794 if (!seen_star && !big) {
3795 for (Py_ssize_t i = 0; i < n; i++) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003796 expr_ty elt = asdl_seq_GET(elts, i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003797 VISIT(c, expr, elt);
Mark Shannon13bc1392020-01-23 09:25:17 +00003798 }
3799 if (tuple) {
3800 ADDOP_I(c, BUILD_TUPLE, n+pushed);
3801 } else {
3802 ADDOP_I(c, build, n+pushed);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003803 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003804 return 1;
3805 }
3806 int sequence_built = 0;
3807 if (big) {
3808 ADDOP_I(c, build, pushed);
3809 sequence_built = 1;
3810 }
3811 for (Py_ssize_t i = 0; i < n; i++) {
3812 expr_ty elt = asdl_seq_GET(elts, i);
3813 if (elt->kind == Starred_kind) {
3814 if (sequence_built == 0) {
3815 ADDOP_I(c, build, i+pushed);
3816 sequence_built = 1;
3817 }
3818 VISIT(c, expr, elt->v.Starred.value);
3819 ADDOP_I(c, extend, 1);
3820 }
3821 else {
3822 VISIT(c, expr, elt);
3823 if (sequence_built) {
3824 ADDOP_I(c, add, 1);
3825 }
3826 }
3827 }
3828 assert(sequence_built);
3829 if (tuple) {
3830 ADDOP(c, LIST_TO_TUPLE);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003831 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003832 return 1;
3833}
3834
3835static int
Brandt Bucher145bf262021-02-26 14:51:55 -08003836unpack_helper(struct compiler *c, asdl_expr_seq *elts)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003837{
3838 Py_ssize_t n = asdl_seq_LEN(elts);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003839 int seen_star = 0;
Brandt Bucher145bf262021-02-26 14:51:55 -08003840 for (Py_ssize_t i = 0; i < n; i++) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003841 expr_ty elt = asdl_seq_GET(elts, i);
3842 if (elt->kind == Starred_kind && !seen_star) {
3843 if ((i >= (1 << 8)) ||
3844 (n-i-1 >= (INT_MAX >> 8)))
3845 return compiler_error(c,
3846 "too many expressions in "
3847 "star-unpacking assignment");
3848 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3849 seen_star = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003850 }
3851 else if (elt->kind == Starred_kind) {
3852 return compiler_error(c,
Furkan Öndercb6534e2020-03-26 04:54:31 +03003853 "multiple starred expressions in assignment");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003854 }
3855 }
3856 if (!seen_star) {
3857 ADDOP_I(c, UNPACK_SEQUENCE, n);
3858 }
Brandt Bucher145bf262021-02-26 14:51:55 -08003859 return 1;
3860}
3861
3862static int
3863assignment_helper(struct compiler *c, asdl_expr_seq *elts)
3864{
3865 Py_ssize_t n = asdl_seq_LEN(elts);
3866 RETURN_IF_FALSE(unpack_helper(c, elts));
3867 for (Py_ssize_t i = 0; i < n; i++) {
Brandt Bucherd5aa2e92020-03-07 19:44:18 -08003868 expr_ty elt = asdl_seq_GET(elts, i);
3869 VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value);
3870 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003871 return 1;
3872}
3873
3874static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003875compiler_list(struct compiler *c, expr_ty e)
3876{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003877 asdl_expr_seq *elts = e->v.List.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003878 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003879 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003880 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003881 else if (e->v.List.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003882 return starunpack_helper(c, elts, 0, BUILD_LIST,
3883 LIST_APPEND, LIST_EXTEND, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003884 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003885 else
3886 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003887 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003888}
3889
3890static int
3891compiler_tuple(struct compiler *c, expr_ty e)
3892{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003893 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003894 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003895 return assignment_helper(c, elts);
3896 }
3897 else if (e->v.Tuple.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003898 return starunpack_helper(c, elts, 0, BUILD_LIST,
3899 LIST_APPEND, LIST_EXTEND, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003900 }
3901 else
3902 VISIT_SEQ(c, expr, elts);
3903 return 1;
3904}
3905
3906static int
3907compiler_set(struct compiler *c, expr_ty e)
3908{
Mark Shannon13bc1392020-01-23 09:25:17 +00003909 return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET,
3910 SET_ADD, SET_UPDATE, 0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003911}
3912
3913static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003914are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003915{
3916 Py_ssize_t i;
3917 for (i = begin; i < end; i++) {
3918 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003919 if (key == NULL || key->kind != Constant_kind)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003920 return 0;
3921 }
3922 return 1;
3923}
3924
3925static int
3926compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3927{
3928 Py_ssize_t i, n = end - begin;
3929 PyObject *keys, *key;
Mark Shannon11e0b292021-04-15 14:28:56 +01003930 int big = n*2 > STACK_USE_GUIDELINE;
3931 if (n > 1 && !big && are_all_items_const(e->v.Dict.keys, begin, end)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003932 for (i = begin; i < end; i++) {
3933 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3934 }
3935 keys = PyTuple_New(n);
3936 if (keys == NULL) {
3937 return 0;
3938 }
3939 for (i = begin; i < end; i++) {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003940 key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003941 Py_INCREF(key);
3942 PyTuple_SET_ITEM(keys, i - begin, key);
3943 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003944 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003945 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
Mark Shannon11e0b292021-04-15 14:28:56 +01003946 return 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003947 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003948 if (big) {
3949 ADDOP_I(c, BUILD_MAP, 0);
3950 }
3951 for (i = begin; i < end; i++) {
3952 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3953 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3954 if (big) {
3955 ADDOP_I(c, MAP_ADD, 1);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003956 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003957 }
3958 if (!big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003959 ADDOP_I(c, BUILD_MAP, n);
3960 }
3961 return 1;
3962}
3963
3964static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003965compiler_dict(struct compiler *c, expr_ty e)
3966{
Victor Stinner976bb402016-03-23 11:36:19 +01003967 Py_ssize_t i, n, elements;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003968 int have_dict;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003969 int is_unpacking = 0;
3970 n = asdl_seq_LEN(e->v.Dict.values);
Mark Shannon8a4cd702020-01-27 09:57:45 +00003971 have_dict = 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003972 elements = 0;
3973 for (i = 0; i < n; i++) {
3974 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003975 if (is_unpacking) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003976 if (elements) {
3977 if (!compiler_subdict(c, e, i - elements, i)) {
3978 return 0;
3979 }
3980 if (have_dict) {
3981 ADDOP_I(c, DICT_UPDATE, 1);
3982 }
3983 have_dict = 1;
3984 elements = 0;
3985 }
3986 if (have_dict == 0) {
3987 ADDOP_I(c, BUILD_MAP, 0);
3988 have_dict = 1;
3989 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003990 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Mark Shannon8a4cd702020-01-27 09:57:45 +00003991 ADDOP_I(c, DICT_UPDATE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003992 }
3993 else {
Mark Shannon11e0b292021-04-15 14:28:56 +01003994 if (elements*2 > STACK_USE_GUIDELINE) {
Pablo Galindoc51db0e2020-08-13 09:48:41 +01003995 if (!compiler_subdict(c, e, i - elements, i + 1)) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003996 return 0;
3997 }
3998 if (have_dict) {
3999 ADDOP_I(c, DICT_UPDATE, 1);
4000 }
4001 have_dict = 1;
4002 elements = 0;
4003 }
4004 else {
4005 elements++;
4006 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004007 }
4008 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004009 if (elements) {
4010 if (!compiler_subdict(c, e, n - elements, n)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004011 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004012 }
4013 if (have_dict) {
4014 ADDOP_I(c, DICT_UPDATE, 1);
4015 }
4016 have_dict = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004017 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004018 if (!have_dict) {
4019 ADDOP_I(c, BUILD_MAP, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004020 }
4021 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004022}
4023
4024static int
4025compiler_compare(struct compiler *c, expr_ty e)
4026{
Victor Stinnerad9a0662013-11-19 22:23:20 +01004027 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004028
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02004029 if (!check_compare(c, e)) {
4030 return 0;
4031 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004032 VISIT(c, expr, e->v.Compare.left);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004033 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
4034 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
4035 if (n == 0) {
4036 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Mark Shannon9af0e472020-01-14 10:12:45 +00004037 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004038 }
4039 else {
4040 basicblock *cleanup = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004041 if (cleanup == NULL)
4042 return 0;
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004043 for (i = 0; i < n; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004044 VISIT(c, expr,
4045 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004046 ADDOP(c, DUP_TOP);
4047 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00004048 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01004049 ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004050 NEXT_BLOCK(c);
4051 }
4052 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00004053 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004054 basicblock *end = compiler_new_block(c);
4055 if (end == NULL)
4056 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00004057 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004058 compiler_use_next_block(c, cleanup);
4059 ADDOP(c, ROT_TWO);
4060 ADDOP(c, POP_TOP);
4061 compiler_use_next_block(c, end);
4062 }
4063 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004064}
4065
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004066static PyTypeObject *
4067infer_type(expr_ty e)
4068{
4069 switch (e->kind) {
4070 case Tuple_kind:
4071 return &PyTuple_Type;
4072 case List_kind:
4073 case ListComp_kind:
4074 return &PyList_Type;
4075 case Dict_kind:
4076 case DictComp_kind:
4077 return &PyDict_Type;
4078 case Set_kind:
4079 case SetComp_kind:
4080 return &PySet_Type;
4081 case GeneratorExp_kind:
4082 return &PyGen_Type;
4083 case Lambda_kind:
4084 return &PyFunction_Type;
4085 case JoinedStr_kind:
4086 case FormattedValue_kind:
4087 return &PyUnicode_Type;
4088 case Constant_kind:
Victor Stinnera102ed72020-02-07 02:24:48 +01004089 return Py_TYPE(e->v.Constant.value);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004090 default:
4091 return NULL;
4092 }
4093}
4094
4095static int
4096check_caller(struct compiler *c, expr_ty e)
4097{
4098 switch (e->kind) {
4099 case Constant_kind:
4100 case Tuple_kind:
4101 case List_kind:
4102 case ListComp_kind:
4103 case Dict_kind:
4104 case DictComp_kind:
4105 case Set_kind:
4106 case SetComp_kind:
4107 case GeneratorExp_kind:
4108 case JoinedStr_kind:
4109 case FormattedValue_kind:
4110 return compiler_warn(c, "'%.200s' object is not callable; "
4111 "perhaps you missed a comma?",
4112 infer_type(e)->tp_name);
4113 default:
4114 return 1;
4115 }
4116}
4117
4118static int
4119check_subscripter(struct compiler *c, expr_ty e)
4120{
4121 PyObject *v;
4122
4123 switch (e->kind) {
4124 case Constant_kind:
4125 v = e->v.Constant.value;
4126 if (!(v == Py_None || v == Py_Ellipsis ||
4127 PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
4128 PyAnySet_Check(v)))
4129 {
4130 return 1;
4131 }
4132 /* fall through */
4133 case Set_kind:
4134 case SetComp_kind:
4135 case GeneratorExp_kind:
4136 case Lambda_kind:
4137 return compiler_warn(c, "'%.200s' object is not subscriptable; "
4138 "perhaps you missed a comma?",
4139 infer_type(e)->tp_name);
4140 default:
4141 return 1;
4142 }
4143}
4144
4145static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004146check_index(struct compiler *c, expr_ty e, expr_ty s)
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004147{
4148 PyObject *v;
4149
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004150 PyTypeObject *index_type = infer_type(s);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004151 if (index_type == NULL
4152 || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
4153 || index_type == &PySlice_Type) {
4154 return 1;
4155 }
4156
4157 switch (e->kind) {
4158 case Constant_kind:
4159 v = e->v.Constant.value;
4160 if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
4161 return 1;
4162 }
4163 /* fall through */
4164 case Tuple_kind:
4165 case List_kind:
4166 case ListComp_kind:
4167 case JoinedStr_kind:
4168 case FormattedValue_kind:
4169 return compiler_warn(c, "%.200s indices must be integers or slices, "
4170 "not %.200s; "
4171 "perhaps you missed a comma?",
4172 infer_type(e)->tp_name,
4173 index_type->tp_name);
4174 default:
4175 return 1;
4176 }
4177}
4178
Zackery Spytz97f5de02019-03-22 01:30:32 -06004179// Return 1 if the method call was optimized, -1 if not, and 0 on error.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004180static int
Yury Selivanovf2392132016-12-13 19:03:51 -05004181maybe_optimize_method_call(struct compiler *c, expr_ty e)
4182{
4183 Py_ssize_t argsl, i;
4184 expr_ty meth = e->v.Call.func;
Pablo Galindoa5634c42020-09-16 19:42:00 +01004185 asdl_expr_seq *args = e->v.Call.args;
Yury Selivanovf2392132016-12-13 19:03:51 -05004186
4187 /* Check that the call node is an attribute access, and that
4188 the call doesn't have keyword parameters. */
4189 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
Mark Shannon11e0b292021-04-15 14:28:56 +01004190 asdl_seq_LEN(e->v.Call.keywords)) {
Yury Selivanovf2392132016-12-13 19:03:51 -05004191 return -1;
Mark Shannon11e0b292021-04-15 14:28:56 +01004192 }
4193 /* Check that there aren't too many arguments */
Yury Selivanovf2392132016-12-13 19:03:51 -05004194 argsl = asdl_seq_LEN(args);
Mark Shannon11e0b292021-04-15 14:28:56 +01004195 if (argsl >= STACK_USE_GUIDELINE) {
4196 return -1;
4197 }
4198 /* Check that there are no *varargs types of arguments. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004199 for (i = 0; i < argsl; i++) {
4200 expr_ty elt = asdl_seq_GET(args, i);
4201 if (elt->kind == Starred_kind) {
4202 return -1;
4203 }
4204 }
4205
4206 /* Alright, we can optimize the code. */
4207 VISIT(c, expr, meth->v.Attribute.value);
Mark Shannond48848c2021-03-14 18:01:30 +00004208 int old_lineno = c->u->u_lineno;
4209 c->u->u_lineno = meth->end_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004210 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
4211 VISIT_SEQ(c, expr, e->v.Call.args);
4212 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
Mark Shannond48848c2021-03-14 18:01:30 +00004213 c->u->u_lineno = old_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004214 return 1;
4215}
4216
4217static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004218validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
Zackery Spytz08050e92020-04-06 00:47:47 -06004219{
4220 Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
4221 for (Py_ssize_t i = 0; i < nkeywords; i++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004222 keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
4223 if (key->arg == NULL) {
4224 continue;
4225 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +01004226 if (forbidden_name(c, key->arg, Store)) {
4227 return -1;
4228 }
Zackery Spytz08050e92020-04-06 00:47:47 -06004229 for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004230 keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
4231 if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004232 c->u->u_col_offset = other->col_offset;
Brandt Bucher145bf262021-02-26 14:51:55 -08004233 compiler_error(c, "keyword argument repeated: %U", key->arg);
Pablo Galindo254ec782020-04-03 20:37:13 +01004234 return -1;
4235 }
4236 }
4237 }
4238 return 0;
4239}
4240
4241static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004242compiler_call(struct compiler *c, expr_ty e)
4243{
Zackery Spytz97f5de02019-03-22 01:30:32 -06004244 int ret = maybe_optimize_method_call(c, e);
4245 if (ret >= 0) {
4246 return ret;
4247 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004248 if (!check_caller(c, e->v.Call.func)) {
4249 return 0;
4250 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004251 VISIT(c, expr, e->v.Call.func);
4252 return compiler_call_helper(c, 0,
4253 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004254 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004255}
4256
Eric V. Smith235a6f02015-09-19 14:51:32 -04004257static int
4258compiler_joined_str(struct compiler *c, expr_ty e)
4259{
Mark Shannon11e0b292021-04-15 14:28:56 +01004260
4261 Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values);
4262 if (value_count > STACK_USE_GUIDELINE) {
4263 ADDOP_LOAD_CONST_NEW(c, _PyUnicode_FromASCII("", 0));
4264 PyObject *join = _PyUnicode_FromASCII("join", 4);
4265 if (join == NULL) {
4266 return 0;
4267 }
4268 ADDOP_NAME(c, LOAD_METHOD, join, names);
4269 Py_DECREF(join);
4270 ADDOP_I(c, BUILD_LIST, 0);
4271 for (Py_ssize_t i = 0; i < asdl_seq_LEN(e->v.JoinedStr.values); i++) {
4272 VISIT(c, expr, asdl_seq_GET(e->v.JoinedStr.values, i));
4273 ADDOP_I(c, LIST_APPEND, 1);
4274 }
4275 ADDOP_I(c, CALL_METHOD, 1);
4276 }
4277 else {
4278 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
4279 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) {
4280 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
4281 }
4282 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004283 return 1;
4284}
4285
Eric V. Smitha78c7952015-11-03 12:45:05 -05004286/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004287static int
4288compiler_formatted_value(struct compiler *c, expr_ty e)
4289{
Eric V. Smitha78c7952015-11-03 12:45:05 -05004290 /* Our oparg encodes 2 pieces of information: the conversion
4291 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04004292
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004293 Convert the conversion char to 3 bits:
4294 : 000 0x0 FVC_NONE The default if nothing specified.
Eric V. Smitha78c7952015-11-03 12:45:05 -05004295 !s : 001 0x1 FVC_STR
4296 !r : 010 0x2 FVC_REPR
4297 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04004298
Eric V. Smitha78c7952015-11-03 12:45:05 -05004299 next bit is whether or not we have a format spec:
4300 yes : 100 0x4
4301 no : 000 0x0
4302 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004303
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004304 int conversion = e->v.FormattedValue.conversion;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004305 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004306
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004307 /* The expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004308 VISIT(c, expr, e->v.FormattedValue.value);
4309
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004310 switch (conversion) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004311 case 's': oparg = FVC_STR; break;
4312 case 'r': oparg = FVC_REPR; break;
4313 case 'a': oparg = FVC_ASCII; break;
4314 case -1: oparg = FVC_NONE; break;
4315 default:
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004316 PyErr_Format(PyExc_SystemError,
4317 "Unrecognized conversion character %d", conversion);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004318 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004319 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004320 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004321 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004322 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004323 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004324 }
4325
Eric V. Smitha78c7952015-11-03 12:45:05 -05004326 /* And push our opcode and oparg */
4327 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004328
Eric V. Smith235a6f02015-09-19 14:51:32 -04004329 return 1;
4330}
4331
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004332static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004333compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004334{
4335 Py_ssize_t i, n = end - begin;
4336 keyword_ty kw;
4337 PyObject *keys, *key;
4338 assert(n > 0);
Mark Shannon11e0b292021-04-15 14:28:56 +01004339 int big = n*2 > STACK_USE_GUIDELINE;
4340 if (n > 1 && !big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004341 for (i = begin; i < end; i++) {
4342 kw = asdl_seq_GET(keywords, i);
4343 VISIT(c, expr, kw->value);
4344 }
4345 keys = PyTuple_New(n);
4346 if (keys == NULL) {
4347 return 0;
4348 }
4349 for (i = begin; i < end; i++) {
4350 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
4351 Py_INCREF(key);
4352 PyTuple_SET_ITEM(keys, i - begin, key);
4353 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004354 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004355 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
Mark Shannon11e0b292021-04-15 14:28:56 +01004356 return 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004357 }
Mark Shannon11e0b292021-04-15 14:28:56 +01004358 if (big) {
4359 ADDOP_I_NOLINE(c, BUILD_MAP, 0);
4360 }
4361 for (i = begin; i < end; i++) {
4362 kw = asdl_seq_GET(keywords, i);
4363 ADDOP_LOAD_CONST(c, kw->arg);
4364 VISIT(c, expr, kw->value);
4365 if (big) {
4366 ADDOP_I_NOLINE(c, MAP_ADD, 1);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004367 }
Mark Shannon11e0b292021-04-15 14:28:56 +01004368 }
4369 if (!big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004370 ADDOP_I(c, BUILD_MAP, n);
4371 }
4372 return 1;
4373}
4374
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004375/* shared code between compiler_call and compiler_class */
4376static int
4377compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01004378 int n, /* Args already pushed */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004379 asdl_expr_seq *args,
4380 asdl_keyword_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004381{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004382 Py_ssize_t i, nseen, nelts, nkwelts;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004383
Pablo Galindo254ec782020-04-03 20:37:13 +01004384 if (validate_keywords(c, keywords) == -1) {
4385 return 0;
4386 }
4387
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004388 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004389 nkwelts = asdl_seq_LEN(keywords);
4390
Mark Shannon11e0b292021-04-15 14:28:56 +01004391 if (nelts + nkwelts*2 > STACK_USE_GUIDELINE) {
4392 goto ex_call;
4393 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004394 for (i = 0; i < nelts; i++) {
4395 expr_ty elt = asdl_seq_GET(args, i);
4396 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00004397 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004398 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004399 }
4400 for (i = 0; i < nkwelts; i++) {
4401 keyword_ty kw = asdl_seq_GET(keywords, i);
4402 if (kw->arg == NULL) {
4403 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004404 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004405 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004406
Mark Shannon13bc1392020-01-23 09:25:17 +00004407 /* No * or ** args, so can use faster calling sequence */
4408 for (i = 0; i < nelts; i++) {
4409 expr_ty elt = asdl_seq_GET(args, i);
4410 assert(elt->kind != Starred_kind);
4411 VISIT(c, expr, elt);
4412 }
4413 if (nkwelts) {
4414 PyObject *names;
4415 VISIT_SEQ(c, keyword, keywords);
4416 names = PyTuple_New(nkwelts);
4417 if (names == NULL) {
4418 return 0;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004419 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004420 for (i = 0; i < nkwelts; i++) {
4421 keyword_ty kw = asdl_seq_GET(keywords, i);
4422 Py_INCREF(kw->arg);
4423 PyTuple_SET_ITEM(names, i, kw->arg);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004424 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004425 ADDOP_LOAD_CONST_NEW(c, names);
4426 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
4427 return 1;
4428 }
4429 else {
4430 ADDOP_I(c, CALL_FUNCTION, n + nelts);
4431 return 1;
4432 }
4433
4434ex_call:
4435
4436 /* Do positional arguments. */
4437 if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
4438 VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
4439 }
4440 else if (starunpack_helper(c, args, n, BUILD_LIST,
4441 LIST_APPEND, LIST_EXTEND, 1) == 0) {
4442 return 0;
4443 }
4444 /* Then keyword arguments */
4445 if (nkwelts) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004446 /* Has a new dict been pushed */
4447 int have_dict = 0;
Mark Shannon13bc1392020-01-23 09:25:17 +00004448
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004449 nseen = 0; /* the number of keyword arguments on the stack following */
4450 for (i = 0; i < nkwelts; i++) {
4451 keyword_ty kw = asdl_seq_GET(keywords, i);
4452 if (kw->arg == NULL) {
4453 /* A keyword argument unpacking. */
4454 if (nseen) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004455 if (!compiler_subkwargs(c, keywords, i - nseen, i)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004456 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004457 }
Mark Shannondb64f122020-06-01 10:42:42 +01004458 if (have_dict) {
4459 ADDOP_I(c, DICT_MERGE, 1);
4460 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004461 have_dict = 1;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004462 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004463 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004464 if (!have_dict) {
4465 ADDOP_I(c, BUILD_MAP, 0);
4466 have_dict = 1;
4467 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004468 VISIT(c, expr, kw->value);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004469 ADDOP_I(c, DICT_MERGE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004470 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004471 else {
4472 nseen++;
4473 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004474 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004475 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004476 /* Pack up any trailing keyword arguments. */
Mark Shannon8a4cd702020-01-27 09:57:45 +00004477 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004478 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004479 }
4480 if (have_dict) {
4481 ADDOP_I(c, DICT_MERGE, 1);
4482 }
4483 have_dict = 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004484 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004485 assert(have_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004486 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004487 ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0);
4488 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004489}
4490
Nick Coghlan650f0d02007-04-15 12:05:43 +00004491
4492/* List and set comprehensions and generator expressions work by creating a
4493 nested function to perform the actual iteration. This means that the
4494 iteration variables don't leak into the current scope.
4495 The defined function is called immediately following its definition, with the
4496 result of that call being the result of the expression.
4497 The LC/SC version returns the populated container, while the GE version is
4498 flagged in symtable.c as a generator, so it returns the generator object
4499 when the function is called.
Nick Coghlan650f0d02007-04-15 12:05:43 +00004500
4501 Possible cleanups:
4502 - iterate over the generator sequence instead of using recursion
4503*/
4504
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004505
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004506static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004507compiler_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004508 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004509 int depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004510 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004511{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004512 comprehension_ty gen;
4513 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4514 if (gen->is_async) {
4515 return compiler_async_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004516 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004517 } else {
4518 return compiler_sync_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004519 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004520 }
4521}
4522
4523static int
4524compiler_sync_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004525 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004526 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004527 expr_ty elt, expr_ty val, int type)
4528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004529 /* generate code for the iterator, then each of the ifs,
4530 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004532 comprehension_ty gen;
4533 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004534 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004536 start = compiler_new_block(c);
4537 skip = compiler_new_block(c);
4538 if_cleanup = compiler_new_block(c);
4539 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004541 if (start == NULL || skip == NULL || if_cleanup == NULL ||
4542 anchor == NULL)
4543 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004545 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004547 if (gen_index == 0) {
4548 /* Receive outermost iter as an implicit argument */
4549 c->u->u_argcount = 1;
4550 ADDOP_I(c, LOAD_FAST, 0);
4551 }
4552 else {
4553 /* Sub-iter - calculate on the fly */
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004554 /* Fast path for the temporary variable assignment idiom:
4555 for y in [f(x)]
4556 */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004557 asdl_expr_seq *elts;
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004558 switch (gen->iter->kind) {
4559 case List_kind:
4560 elts = gen->iter->v.List.elts;
4561 break;
4562 case Tuple_kind:
4563 elts = gen->iter->v.Tuple.elts;
4564 break;
4565 default:
4566 elts = NULL;
4567 }
4568 if (asdl_seq_LEN(elts) == 1) {
4569 expr_ty elt = asdl_seq_GET(elts, 0);
4570 if (elt->kind != Starred_kind) {
4571 VISIT(c, expr, elt);
4572 start = NULL;
4573 }
4574 }
4575 if (start) {
4576 VISIT(c, expr, gen->iter);
4577 ADDOP(c, GET_ITER);
4578 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004579 }
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004580 if (start) {
4581 depth++;
4582 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01004583 ADDOP_JUMP(c, FOR_ITER, anchor);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004584 NEXT_BLOCK(c);
4585 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004586 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004588 /* XXX this needs to be cleaned up...a lot! */
4589 n = asdl_seq_LEN(gen->ifs);
4590 for (i = 0; i < n; i++) {
4591 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004592 if (!compiler_jump_if(c, e, if_cleanup, 0))
4593 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004594 NEXT_BLOCK(c);
4595 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004597 if (++gen_index < asdl_seq_LEN(generators))
4598 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004599 generators, gen_index, depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004600 elt, val, type))
4601 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004603 /* only append after the last for generator */
4604 if (gen_index >= asdl_seq_LEN(generators)) {
4605 /* comprehension specific code */
4606 switch (type) {
4607 case COMP_GENEXP:
4608 VISIT(c, expr, elt);
4609 ADDOP(c, YIELD_VALUE);
4610 ADDOP(c, POP_TOP);
4611 break;
4612 case COMP_LISTCOMP:
4613 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004614 ADDOP_I(c, LIST_APPEND, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004615 break;
4616 case COMP_SETCOMP:
4617 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004618 ADDOP_I(c, SET_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004619 break;
4620 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004621 /* With '{k: v}', k is evaluated before v, so we do
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004622 the same. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004623 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004624 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004625 ADDOP_I(c, MAP_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004626 break;
4627 default:
4628 return 0;
4629 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004631 compiler_use_next_block(c, skip);
4632 }
4633 compiler_use_next_block(c, if_cleanup);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004634 if (start) {
Mark Shannon582aaf12020-08-04 17:30:11 +01004635 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004636 compiler_use_next_block(c, anchor);
4637 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004638
4639 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004640}
4641
4642static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004643compiler_async_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004644 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004645 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004646 expr_ty elt, expr_ty val, int type)
4647{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004648 comprehension_ty gen;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004649 basicblock *start, *if_cleanup, *except;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004650 Py_ssize_t i, n;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004651 start = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004652 except = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004653 if_cleanup = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004654
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004655 if (start == NULL || if_cleanup == NULL || except == NULL) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004656 return 0;
4657 }
4658
4659 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4660
4661 if (gen_index == 0) {
4662 /* Receive outermost iter as an implicit argument */
4663 c->u->u_argcount = 1;
4664 ADDOP_I(c, LOAD_FAST, 0);
4665 }
4666 else {
4667 /* Sub-iter - calculate on the fly */
4668 VISIT(c, expr, gen->iter);
4669 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004670 }
4671
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004672 compiler_use_next_block(c, start);
tomKPZ7a7ba3d2021-04-07 07:43:45 -07004673 /* Runtime will push a block here, so we need to account for that */
4674 if (!compiler_push_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start,
4675 NULL, NULL)) {
4676 return 0;
4677 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004678
Mark Shannon582aaf12020-08-04 17:30:11 +01004679 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004680 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004681 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004682 ADDOP(c, YIELD_FROM);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004683 ADDOP(c, POP_BLOCK);
Serhiy Storchaka24d32012018-03-10 18:22:34 +02004684 VISIT(c, expr, gen->target);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004685
4686 n = asdl_seq_LEN(gen->ifs);
4687 for (i = 0; i < n; i++) {
4688 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004689 if (!compiler_jump_if(c, e, if_cleanup, 0))
4690 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004691 NEXT_BLOCK(c);
4692 }
4693
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004694 depth++;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004695 if (++gen_index < asdl_seq_LEN(generators))
4696 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004697 generators, gen_index, depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004698 elt, val, type))
4699 return 0;
4700
4701 /* only append after the last for generator */
4702 if (gen_index >= asdl_seq_LEN(generators)) {
4703 /* comprehension specific code */
4704 switch (type) {
4705 case COMP_GENEXP:
4706 VISIT(c, expr, elt);
4707 ADDOP(c, YIELD_VALUE);
4708 ADDOP(c, POP_TOP);
4709 break;
4710 case COMP_LISTCOMP:
4711 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004712 ADDOP_I(c, LIST_APPEND, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004713 break;
4714 case COMP_SETCOMP:
4715 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004716 ADDOP_I(c, SET_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004717 break;
4718 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004719 /* With '{k: v}', k is evaluated before v, so we do
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004720 the same. */
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004721 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004722 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004723 ADDOP_I(c, MAP_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004724 break;
4725 default:
4726 return 0;
4727 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004728 }
4729 compiler_use_next_block(c, if_cleanup);
Mark Shannon582aaf12020-08-04 17:30:11 +01004730 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004731
tomKPZ7a7ba3d2021-04-07 07:43:45 -07004732 compiler_pop_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start);
4733
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004734 compiler_use_next_block(c, except);
4735 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004736
4737 return 1;
4738}
4739
4740static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004741compiler_comprehension(struct compiler *c, expr_ty e, int type,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004742 identifier name, asdl_comprehension_seq *generators, expr_ty elt,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004743 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004745 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004746 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004747 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004748 int is_async_generator = 0;
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004749 int top_level_await = IS_TOP_LEVEL_AWAIT(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004750
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004751
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004752 int is_async_function = c->u->u_ste->ste_coroutine;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004753
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004754 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004755 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4756 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004757 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004758 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004759 }
4760
4761 is_async_generator = c->u->u_ste->ste_coroutine;
4762
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004763 if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004764 compiler_error(c, "asynchronous comprehension outside of "
4765 "an asynchronous function");
4766 goto error_in_scope;
4767 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004769 if (type != COMP_GENEXP) {
4770 int op;
4771 switch (type) {
4772 case COMP_LISTCOMP:
4773 op = BUILD_LIST;
4774 break;
4775 case COMP_SETCOMP:
4776 op = BUILD_SET;
4777 break;
4778 case COMP_DICTCOMP:
4779 op = BUILD_MAP;
4780 break;
4781 default:
4782 PyErr_Format(PyExc_SystemError,
4783 "unknown comprehension type %d", type);
4784 goto error_in_scope;
4785 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004787 ADDOP_I(c, op, 0);
4788 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004789
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004790 if (!compiler_comprehension_generator(c, generators, 0, 0, elt,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004791 val, type))
4792 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004794 if (type != COMP_GENEXP) {
4795 ADDOP(c, RETURN_VALUE);
4796 }
4797
4798 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004799 qualname = c->u->u_qualname;
4800 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004801 compiler_exit_scope(c);
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004802 if (top_level_await && is_async_generator){
4803 c->u->u_ste->ste_coroutine = 1;
4804 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004805 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004806 goto error;
4807
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004808 if (!compiler_make_closure(c, co, 0, qualname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004809 goto error;
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004810 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004811 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004812 Py_DECREF(co);
4813
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004814 VISIT(c, expr, outermost->iter);
4815
4816 if (outermost->is_async) {
4817 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004818 } else {
4819 ADDOP(c, GET_ITER);
4820 }
4821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004822 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004823
4824 if (is_async_generator && type != COMP_GENEXP) {
4825 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004826 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004827 ADDOP(c, YIELD_FROM);
4828 }
4829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004830 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004831error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004832 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004833error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004834 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004835 Py_XDECREF(co);
4836 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004837}
4838
4839static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004840compiler_genexp(struct compiler *c, expr_ty e)
4841{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004842 static identifier name;
4843 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004844 name = PyUnicode_InternFromString("<genexpr>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004845 if (!name)
4846 return 0;
4847 }
4848 assert(e->kind == GeneratorExp_kind);
4849 return compiler_comprehension(c, e, COMP_GENEXP, name,
4850 e->v.GeneratorExp.generators,
4851 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004852}
4853
4854static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004855compiler_listcomp(struct compiler *c, expr_ty e)
4856{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004857 static identifier name;
4858 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004859 name = PyUnicode_InternFromString("<listcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004860 if (!name)
4861 return 0;
4862 }
4863 assert(e->kind == ListComp_kind);
4864 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4865 e->v.ListComp.generators,
4866 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004867}
4868
4869static int
4870compiler_setcomp(struct compiler *c, expr_ty e)
4871{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004872 static identifier name;
4873 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004874 name = PyUnicode_InternFromString("<setcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004875 if (!name)
4876 return 0;
4877 }
4878 assert(e->kind == SetComp_kind);
4879 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4880 e->v.SetComp.generators,
4881 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004882}
4883
4884
4885static int
4886compiler_dictcomp(struct compiler *c, expr_ty e)
4887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004888 static identifier name;
4889 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004890 name = PyUnicode_InternFromString("<dictcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004891 if (!name)
4892 return 0;
4893 }
4894 assert(e->kind == DictComp_kind);
4895 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4896 e->v.DictComp.generators,
4897 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004898}
4899
4900
4901static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004902compiler_visit_keyword(struct compiler *c, keyword_ty k)
4903{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004904 VISIT(c, expr, k->value);
4905 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004906}
4907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004908/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004909 whether they are true or false.
4910
4911 Return values: 1 for true, 0 for false, -1 for non-constant.
4912 */
4913
4914static int
Mark Shannonfee55262019-11-21 09:11:43 +00004915compiler_with_except_finish(struct compiler *c) {
4916 basicblock *exit;
4917 exit = compiler_new_block(c);
4918 if (exit == NULL)
4919 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01004920 ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit);
Mark Shannon266b4622020-11-17 19:30:14 +00004921 NEXT_BLOCK(c);
Mark Shannonbf353f32020-12-17 13:55:28 +00004922 ADDOP_I(c, RERAISE, 1);
Mark Shannonfee55262019-11-21 09:11:43 +00004923 compiler_use_next_block(c, exit);
4924 ADDOP(c, POP_TOP);
4925 ADDOP(c, POP_TOP);
4926 ADDOP(c, POP_TOP);
4927 ADDOP(c, POP_EXCEPT);
4928 ADDOP(c, POP_TOP);
4929 return 1;
4930}
Yury Selivanov75445082015-05-11 22:57:16 -04004931
4932/*
4933 Implements the async with statement.
4934
4935 The semantics outlined in that PEP are as follows:
4936
4937 async with EXPR as VAR:
4938 BLOCK
4939
4940 It is implemented roughly as:
4941
4942 context = EXPR
4943 exit = context.__aexit__ # not calling it
4944 value = await context.__aenter__()
4945 try:
4946 VAR = value # if VAR present in the syntax
4947 BLOCK
4948 finally:
4949 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004950 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04004951 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004952 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04004953 if not (await exit(*exc)):
4954 raise
4955 */
4956static int
4957compiler_async_with(struct compiler *c, stmt_ty s, int pos)
4958{
Mark Shannonfee55262019-11-21 09:11:43 +00004959 basicblock *block, *final, *exit;
Yury Selivanov75445082015-05-11 22:57:16 -04004960 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
4961
4962 assert(s->kind == AsyncWith_kind);
Pablo Galindo90235812020-03-15 04:29:22 +00004963 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07004964 c->u->u_ste->ste_coroutine = 1;
4965 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
Zsolt Dollensteine2396502018-04-27 08:58:56 -07004966 return compiler_error(c, "'async with' outside async function");
4967 }
Yury Selivanov75445082015-05-11 22:57:16 -04004968
4969 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004970 final = compiler_new_block(c);
4971 exit = compiler_new_block(c);
4972 if (!block || !final || !exit)
Yury Selivanov75445082015-05-11 22:57:16 -04004973 return 0;
4974
4975 /* Evaluate EXPR */
4976 VISIT(c, expr, item->context_expr);
4977
4978 ADDOP(c, BEFORE_ASYNC_WITH);
4979 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004980 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004981 ADDOP(c, YIELD_FROM);
4982
Mark Shannon582aaf12020-08-04 17:30:11 +01004983 ADDOP_JUMP(c, SETUP_ASYNC_WITH, final);
Yury Selivanov75445082015-05-11 22:57:16 -04004984
4985 /* SETUP_ASYNC_WITH pushes a finally block. */
4986 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004987 if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004988 return 0;
4989 }
4990
4991 if (item->optional_vars) {
4992 VISIT(c, expr, item->optional_vars);
4993 }
4994 else {
4995 /* Discard result from context.__aenter__() */
4996 ADDOP(c, POP_TOP);
4997 }
4998
4999 pos++;
5000 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
5001 /* BLOCK code */
5002 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
5003 else if (!compiler_async_with(c, s, pos))
5004 return 0;
5005
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005006 compiler_pop_fblock(c, ASYNC_WITH, block);
Mark Shannonfee55262019-11-21 09:11:43 +00005007 ADDOP(c, POP_BLOCK);
5008 /* End of body; start the cleanup */
Yury Selivanov75445082015-05-11 22:57:16 -04005009
Mark Shannonfee55262019-11-21 09:11:43 +00005010 /* For successful outcome:
5011 * call __exit__(None, None, None)
5012 */
5013 if(!compiler_call_exit_with_nones(c))
Yury Selivanov75445082015-05-11 22:57:16 -04005014 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00005015 ADDOP(c, GET_AWAITABLE);
5016 ADDOP_O(c, LOAD_CONST, Py_None, consts);
5017 ADDOP(c, YIELD_FROM);
Yury Selivanov75445082015-05-11 22:57:16 -04005018
Mark Shannonfee55262019-11-21 09:11:43 +00005019 ADDOP(c, POP_TOP);
Yury Selivanov75445082015-05-11 22:57:16 -04005020
Mark Shannon582aaf12020-08-04 17:30:11 +01005021 ADDOP_JUMP(c, JUMP_ABSOLUTE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00005022
5023 /* For exceptional outcome: */
5024 compiler_use_next_block(c, final);
5025
5026 ADDOP(c, WITH_EXCEPT_START);
Yury Selivanov75445082015-05-11 22:57:16 -04005027 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005028 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005029 ADDOP(c, YIELD_FROM);
Mark Shannonfee55262019-11-21 09:11:43 +00005030 compiler_with_except_finish(c);
Yury Selivanov75445082015-05-11 22:57:16 -04005031
Mark Shannonfee55262019-11-21 09:11:43 +00005032compiler_use_next_block(c, exit);
Yury Selivanov75445082015-05-11 22:57:16 -04005033 return 1;
5034}
5035
5036
Guido van Rossumc2e20742006-02-27 22:32:47 +00005037/*
5038 Implements the with statement from PEP 343.
Guido van Rossumc2e20742006-02-27 22:32:47 +00005039 with EXPR as VAR:
5040 BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00005041 is implemented as:
5042 <code for EXPR>
5043 SETUP_WITH E
5044 <code to store to VAR> or POP_TOP
5045 <code for BLOCK>
5046 LOAD_CONST (None, None, None)
5047 CALL_FUNCTION_EX 0
5048 JUMP_FORWARD EXIT
5049 E: WITH_EXCEPT_START (calls EXPR.__exit__)
5050 POP_JUMP_IF_TRUE T:
5051 RERAISE
5052 T: POP_TOP * 3 (remove exception from stack)
5053 POP_EXCEPT
5054 POP_TOP
5055 EXIT:
Guido van Rossumc2e20742006-02-27 22:32:47 +00005056 */
Mark Shannonfee55262019-11-21 09:11:43 +00005057
Guido van Rossumc2e20742006-02-27 22:32:47 +00005058static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005059compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00005060{
Mark Shannonfee55262019-11-21 09:11:43 +00005061 basicblock *block, *final, *exit;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005062 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005063
5064 assert(s->kind == With_kind);
5065
Guido van Rossumc2e20742006-02-27 22:32:47 +00005066 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00005067 final = compiler_new_block(c);
5068 exit = compiler_new_block(c);
5069 if (!block || !final || !exit)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005070 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005071
Thomas Wouters477c8d52006-05-27 19:21:47 +00005072 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005073 VISIT(c, expr, item->context_expr);
Mark Shannonfee55262019-11-21 09:11:43 +00005074 /* Will push bound __exit__ */
Mark Shannon582aaf12020-08-04 17:30:11 +01005075 ADDOP_JUMP(c, SETUP_WITH, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005076
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005077 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00005078 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00005079 if (!compiler_push_fblock(c, WITH, block, final, NULL)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005080 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005081 }
5082
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005083 if (item->optional_vars) {
5084 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005085 }
5086 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005087 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005088 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005089 }
5090
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005091 pos++;
5092 if (pos == asdl_seq_LEN(s->v.With.items))
5093 /* BLOCK code */
5094 VISIT_SEQ(c, stmt, s->v.With.body)
5095 else if (!compiler_with(c, s, pos))
5096 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005097
Mark Shannon3bd60352021-01-13 12:05:43 +00005098
5099 /* Mark all following code as artificial */
5100 c->u->u_lineno = -1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005101 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005102 compiler_pop_fblock(c, WITH, block);
Mark Shannon13bc1392020-01-23 09:25:17 +00005103
Mark Shannonfee55262019-11-21 09:11:43 +00005104 /* End of body; start the cleanup. */
Mark Shannon13bc1392020-01-23 09:25:17 +00005105
Mark Shannonfee55262019-11-21 09:11:43 +00005106 /* For successful outcome:
5107 * call __exit__(None, None, None)
5108 */
5109 if (!compiler_call_exit_with_nones(c))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005110 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00005111 ADDOP(c, POP_TOP);
Mark Shannon582aaf12020-08-04 17:30:11 +01005112 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005113
Mark Shannonfee55262019-11-21 09:11:43 +00005114 /* For exceptional outcome: */
5115 compiler_use_next_block(c, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005116
Mark Shannonfee55262019-11-21 09:11:43 +00005117 ADDOP(c, WITH_EXCEPT_START);
5118 compiler_with_except_finish(c);
5119
5120 compiler_use_next_block(c, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005121 return 1;
5122}
5123
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005124static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005125compiler_visit_expr1(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005127 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07005128 case NamedExpr_kind:
5129 VISIT(c, expr, e->v.NamedExpr.value);
5130 ADDOP(c, DUP_TOP);
5131 VISIT(c, expr, e->v.NamedExpr.target);
5132 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005133 case BoolOp_kind:
5134 return compiler_boolop(c, e);
5135 case BinOp_kind:
5136 VISIT(c, expr, e->v.BinOp.left);
5137 VISIT(c, expr, e->v.BinOp.right);
Andy Lester76d58772020-03-10 21:18:12 -05005138 ADDOP(c, binop(e->v.BinOp.op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005139 break;
5140 case UnaryOp_kind:
5141 VISIT(c, expr, e->v.UnaryOp.operand);
5142 ADDOP(c, unaryop(e->v.UnaryOp.op));
5143 break;
5144 case Lambda_kind:
5145 return compiler_lambda(c, e);
5146 case IfExp_kind:
5147 return compiler_ifexp(c, e);
5148 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005149 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005150 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005151 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005152 case GeneratorExp_kind:
5153 return compiler_genexp(c, e);
5154 case ListComp_kind:
5155 return compiler_listcomp(c, e);
5156 case SetComp_kind:
5157 return compiler_setcomp(c, e);
5158 case DictComp_kind:
5159 return compiler_dictcomp(c, e);
5160 case Yield_kind:
5161 if (c->u->u_ste->ste_type != FunctionBlock)
5162 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005163 if (e->v.Yield.value) {
5164 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005165 }
5166 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005167 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005168 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005169 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005170 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005171 case YieldFrom_kind:
5172 if (c->u->u_ste->ste_type != FunctionBlock)
5173 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04005174
5175 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
5176 return compiler_error(c, "'yield from' inside async function");
5177
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005178 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04005179 ADDOP(c, GET_YIELD_FROM_ITER);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005180 ADDOP_LOAD_CONST(c, Py_None);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005181 ADDOP(c, YIELD_FROM);
5182 break;
Yury Selivanov75445082015-05-11 22:57:16 -04005183 case Await_kind:
Pablo Galindo90235812020-03-15 04:29:22 +00005184 if (!IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005185 if (c->u->u_ste->ste_type != FunctionBlock){
5186 return compiler_error(c, "'await' outside function");
5187 }
Yury Selivanov75445082015-05-11 22:57:16 -04005188
Victor Stinner331a6a52019-05-27 16:39:22 +02005189 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005190 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
5191 return compiler_error(c, "'await' outside async function");
5192 }
5193 }
Yury Selivanov75445082015-05-11 22:57:16 -04005194
5195 VISIT(c, expr, e->v.Await.value);
5196 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005197 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005198 ADDOP(c, YIELD_FROM);
5199 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005200 case Compare_kind:
5201 return compiler_compare(c, e);
5202 case Call_kind:
5203 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005204 case Constant_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005205 ADDOP_LOAD_CONST(c, e->v.Constant.value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005206 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005207 case JoinedStr_kind:
5208 return compiler_joined_str(c, e);
5209 case FormattedValue_kind:
5210 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005211 /* The following exprs can be assignment targets. */
5212 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005213 VISIT(c, expr, e->v.Attribute.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005214 switch (e->v.Attribute.ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005215 case Load:
Mark Shannond48848c2021-03-14 18:01:30 +00005216 {
5217 int old_lineno = c->u->u_lineno;
5218 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005219 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005220 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005221 break;
Mark Shannond48848c2021-03-14 18:01:30 +00005222 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005223 case Store:
Mark Shannond48848c2021-03-14 18:01:30 +00005224 if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005225 return 0;
Mark Shannond48848c2021-03-14 18:01:30 +00005226 }
5227 int old_lineno = c->u->u_lineno;
5228 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005229 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005230 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005231 break;
5232 case Del:
5233 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
5234 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005235 }
5236 break;
5237 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005238 return compiler_subscript(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005239 case Starred_kind:
5240 switch (e->v.Starred.ctx) {
5241 case Store:
5242 /* In all legitimate cases, the Starred node was already replaced
5243 * by compiler_list/compiler_tuple. XXX: is that okay? */
5244 return compiler_error(c,
5245 "starred assignment target must be in a list or tuple");
5246 default:
5247 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005248 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005249 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005250 break;
5251 case Slice_kind:
5252 return compiler_slice(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005253 case Name_kind:
5254 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
5255 /* child nodes of List and Tuple will have expr_context set */
5256 case List_kind:
5257 return compiler_list(c, e);
5258 case Tuple_kind:
5259 return compiler_tuple(c, e);
Brandt Bucher145bf262021-02-26 14:51:55 -08005260 case MatchAs_kind:
5261 case MatchOr_kind:
5262 // Can only occur in patterns, which are handled elsewhere.
5263 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005264 }
5265 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005266}
5267
5268static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005269compiler_visit_expr(struct compiler *c, expr_ty e)
5270{
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005271 int old_lineno = c->u->u_lineno;
5272 int old_col_offset = c->u->u_col_offset;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005273 SET_LOC(c, e);
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005274 int res = compiler_visit_expr1(c, e);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005275 c->u->u_lineno = old_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005276 c->u->u_col_offset = old_col_offset;
5277 return res;
5278}
5279
5280static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005281compiler_augassign(struct compiler *c, stmt_ty s)
5282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005283 assert(s->kind == AugAssign_kind);
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005284 expr_ty e = s->v.AugAssign.target;
5285
5286 int old_lineno = c->u->u_lineno;
5287 int old_col_offset = c->u->u_col_offset;
5288 SET_LOC(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005290 switch (e->kind) {
5291 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005292 VISIT(c, expr, e->v.Attribute.value);
5293 ADDOP(c, DUP_TOP);
Mark Shannond48848c2021-03-14 18:01:30 +00005294 int old_lineno = c->u->u_lineno;
5295 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005296 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005297 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005298 break;
5299 case Subscript_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005300 VISIT(c, expr, e->v.Subscript.value);
5301 VISIT(c, expr, e->v.Subscript.slice);
5302 ADDOP(c, DUP_TOP_TWO);
5303 ADDOP(c, BINARY_SUBSCR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005304 break;
5305 case Name_kind:
5306 if (!compiler_nameop(c, e->v.Name.id, Load))
5307 return 0;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005308 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005309 default:
5310 PyErr_Format(PyExc_SystemError,
5311 "invalid node type (%d) for augmented assignment",
5312 e->kind);
5313 return 0;
5314 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005315
5316 c->u->u_lineno = old_lineno;
5317 c->u->u_col_offset = old_col_offset;
5318
5319 VISIT(c, expr, s->v.AugAssign.value);
5320 ADDOP(c, inplace_binop(s->v.AugAssign.op));
5321
5322 SET_LOC(c, e);
5323
5324 switch (e->kind) {
5325 case Attribute_kind:
Mark Shannond48848c2021-03-14 18:01:30 +00005326 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005327 ADDOP(c, ROT_TWO);
5328 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5329 break;
5330 case Subscript_kind:
5331 ADDOP(c, ROT_THREE);
5332 ADDOP(c, STORE_SUBSCR);
5333 break;
5334 case Name_kind:
5335 return compiler_nameop(c, e->v.Name.id, Store);
5336 default:
5337 Py_UNREACHABLE();
5338 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005339 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005340}
5341
5342static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005343check_ann_expr(struct compiler *c, expr_ty e)
5344{
5345 VISIT(c, expr, e);
5346 ADDOP(c, POP_TOP);
5347 return 1;
5348}
5349
5350static int
5351check_annotation(struct compiler *c, stmt_ty s)
5352{
5353 /* Annotations are only evaluated in a module or class. */
5354 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5355 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
5356 return check_ann_expr(c, s->v.AnnAssign.annotation);
5357 }
5358 return 1;
5359}
5360
5361static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005362check_ann_subscr(struct compiler *c, expr_ty e)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005363{
5364 /* We check that everything in a subscript is defined at runtime. */
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005365 switch (e->kind) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005366 case Slice_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005367 if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005368 return 0;
5369 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005370 if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) {
5371 return 0;
5372 }
5373 if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) {
5374 return 0;
5375 }
5376 return 1;
5377 case Tuple_kind: {
5378 /* extended slice */
Pablo Galindoa5634c42020-09-16 19:42:00 +01005379 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005380 Py_ssize_t i, n = asdl_seq_LEN(elts);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005381 for (i = 0; i < n; i++) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005382 if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005383 return 0;
5384 }
5385 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005386 return 1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005387 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005388 default:
5389 return check_ann_expr(c, e);
5390 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005391}
5392
5393static int
5394compiler_annassign(struct compiler *c, stmt_ty s)
5395{
5396 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07005397 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005398
5399 assert(s->kind == AnnAssign_kind);
5400
5401 /* We perform the actual assignment first. */
5402 if (s->v.AnnAssign.value) {
5403 VISIT(c, expr, s->v.AnnAssign.value);
5404 VISIT(c, expr, targ);
5405 }
5406 switch (targ->kind) {
5407 case Name_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005408 if (forbidden_name(c, targ->v.Name.id, Store))
5409 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005410 /* If we have a simple name in a module or class, store annotation. */
5411 if (s->v.AnnAssign.simple &&
5412 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5413 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Pablo Galindob0544ba2021-04-21 12:41:19 +01005414 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
5415 VISIT(c, annexpr, s->v.AnnAssign.annotation)
5416 }
5417 else {
5418 VISIT(c, expr, s->v.AnnAssign.annotation);
5419 }
Mark Shannon332cd5e2018-01-30 00:41:04 +00005420 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02005421 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005422 ADDOP_LOAD_CONST_NEW(c, mangled);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005423 ADDOP(c, STORE_SUBSCR);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005424 }
5425 break;
5426 case Attribute_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005427 if (forbidden_name(c, targ->v.Attribute.attr, Store))
5428 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005429 if (!s->v.AnnAssign.value &&
5430 !check_ann_expr(c, targ->v.Attribute.value)) {
5431 return 0;
5432 }
5433 break;
5434 case Subscript_kind:
5435 if (!s->v.AnnAssign.value &&
5436 (!check_ann_expr(c, targ->v.Subscript.value) ||
5437 !check_ann_subscr(c, targ->v.Subscript.slice))) {
5438 return 0;
5439 }
5440 break;
5441 default:
5442 PyErr_Format(PyExc_SystemError,
5443 "invalid node type (%d) for annotated assignment",
5444 targ->kind);
5445 return 0;
5446 }
5447 /* Annotation is evaluated last. */
5448 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
5449 return 0;
5450 }
5451 return 1;
5452}
5453
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005454/* Raises a SyntaxError and returns 0.
5455 If something goes wrong, a different exception may be raised.
5456*/
5457
5458static int
Brandt Bucher145bf262021-02-26 14:51:55 -08005459compiler_error(struct compiler *c, const char *format, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005460{
Brandt Bucher145bf262021-02-26 14:51:55 -08005461 va_list vargs;
5462#ifdef HAVE_STDARG_PROTOTYPES
5463 va_start(vargs, format);
5464#else
5465 va_start(vargs);
5466#endif
5467 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5468 va_end(vargs);
5469 if (msg == NULL) {
5470 return 0;
5471 }
5472 PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
5473 if (loc == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005474 Py_INCREF(Py_None);
5475 loc = Py_None;
5476 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005477 PyObject *args = Py_BuildValue("O(OiiO)", msg, c->c_filename,
5478 c->u->u_lineno, c->u->u_col_offset + 1, loc);
5479 Py_DECREF(msg);
5480 if (args == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005481 goto exit;
Brandt Bucher145bf262021-02-26 14:51:55 -08005482 }
5483 PyErr_SetObject(PyExc_SyntaxError, args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005484 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005485 Py_DECREF(loc);
Brandt Bucher145bf262021-02-26 14:51:55 -08005486 Py_XDECREF(args);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005487 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005488}
5489
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005490/* Emits a SyntaxWarning and returns 1 on success.
5491 If a SyntaxWarning raised as error, replaces it with a SyntaxError
5492 and returns 0.
5493*/
5494static int
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005495compiler_warn(struct compiler *c, const char *format, ...)
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005496{
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005497 va_list vargs;
5498#ifdef HAVE_STDARG_PROTOTYPES
5499 va_start(vargs, format);
5500#else
5501 va_start(vargs);
5502#endif
5503 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5504 va_end(vargs);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005505 if (msg == NULL) {
5506 return 0;
5507 }
5508 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
5509 c->u->u_lineno, NULL, NULL) < 0)
5510 {
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005511 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005512 /* Replace the SyntaxWarning exception with a SyntaxError
5513 to get a more accurate error report */
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005514 PyErr_Clear();
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005515 assert(PyUnicode_AsUTF8(msg) != NULL);
5516 compiler_error(c, PyUnicode_AsUTF8(msg));
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005517 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005518 Py_DECREF(msg);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005519 return 0;
5520 }
5521 Py_DECREF(msg);
5522 return 1;
5523}
5524
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005525static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005526compiler_subscript(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005527{
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005528 expr_context_ty ctx = e->v.Subscript.ctx;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005529 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005530
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005531 if (ctx == Load) {
5532 if (!check_subscripter(c, e->v.Subscript.value)) {
5533 return 0;
5534 }
5535 if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
5536 return 0;
5537 }
5538 }
5539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005540 switch (ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005541 case Load: op = BINARY_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005542 case Store: op = STORE_SUBSCR; break;
5543 case Del: op = DELETE_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005544 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005545 assert(op);
5546 VISIT(c, expr, e->v.Subscript.value);
5547 VISIT(c, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005548 ADDOP(c, op);
5549 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005550}
5551
5552static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005553compiler_slice(struct compiler *c, expr_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005555 int n = 2;
5556 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005558 /* only handles the cases where BUILD_SLICE is emitted */
5559 if (s->v.Slice.lower) {
5560 VISIT(c, expr, s->v.Slice.lower);
5561 }
5562 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005563 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005564 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005566 if (s->v.Slice.upper) {
5567 VISIT(c, expr, s->v.Slice.upper);
5568 }
5569 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005570 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005571 }
5572
5573 if (s->v.Slice.step) {
5574 n++;
5575 VISIT(c, expr, s->v.Slice.step);
5576 }
5577 ADDOP_I(c, BUILD_SLICE, n);
5578 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005579}
5580
Brandt Bucher145bf262021-02-26 14:51:55 -08005581
5582// PEP 634: Structural Pattern Matching
5583
5584// To keep things simple, all compiler_pattern_* routines follow the convention
5585// of replacing TOS (the subject for the given pattern) with either True (match)
5586// or False (no match). We do this even for irrefutable patterns; the idea is
5587// that it's much easier to smooth out any redundant pushing, popping, and
5588// jumping in the peephole optimizer than to detect or predict it here.
5589
5590
5591#define WILDCARD_CHECK(N) \
5592 ((N)->kind == Name_kind && \
5593 _PyUnicode_EqualToASCIIString((N)->v.Name.id, "_"))
5594
5595
5596static int
5597pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc)
5598{
5599 assert(!_PyUnicode_EqualToASCIIString(n, "_"));
5600 // Can't assign to the same name twice:
5601 if (pc->stores == NULL) {
5602 RETURN_IF_FALSE(pc->stores = PySet_New(NULL));
5603 }
5604 else {
5605 int duplicate = PySet_Contains(pc->stores, n);
5606 if (duplicate < 0) {
5607 return 0;
5608 }
5609 if (duplicate) {
5610 const char *e = "multiple assignments to name %R in pattern";
5611 return compiler_error(c, e, n);
5612 }
5613 }
5614 RETURN_IF_FALSE(!PySet_Add(pc->stores, n));
5615 RETURN_IF_FALSE(compiler_nameop(c, n, Store));
5616 return 1;
5617}
5618
5619
5620static int
5621pattern_helper_sequence_unpack(struct compiler *c, asdl_expr_seq *values,
5622 Py_ssize_t star, pattern_context *pc)
5623{
5624 RETURN_IF_FALSE(unpack_helper(c, values));
5625 // We've now got a bunch of new subjects on the stack. If any of them fail
5626 // to match, we need to pop everything else off, then finally push False.
5627 // fails is an array of blocks that correspond to the necessary amount of
5628 // popping for each element:
5629 basicblock **fails;
5630 Py_ssize_t size = asdl_seq_LEN(values);
5631 fails = (basicblock **)PyObject_Malloc(sizeof(basicblock*) * size);
5632 if (fails == NULL) {
5633 PyErr_NoMemory();
5634 return 0;
5635 }
5636 // NOTE: Can't use our nice returning macros anymore: they'll leak memory!
5637 // goto error on error.
5638 for (Py_ssize_t i = 0; i < size; i++) {
5639 fails[i] = compiler_new_block(c);
5640 if (fails[i] == NULL) {
5641 goto error;
5642 }
5643 }
5644 for (Py_ssize_t i = 0; i < size; i++) {
5645 expr_ty value = asdl_seq_GET(values, i);
5646 if (i == star) {
5647 assert(value->kind == Starred_kind);
5648 value = value->v.Starred.value;
5649 }
5650 if (!compiler_pattern_subpattern(c, value, pc) ||
5651 !compiler_addop_j(c, POP_JUMP_IF_FALSE, fails[i]) ||
5652 compiler_next_block(c) == NULL)
5653 {
5654 goto error;
5655 }
5656 }
5657 // Success!
5658 basicblock *end = compiler_new_block(c);
5659 if (end == NULL ||
5660 !compiler_addop_load_const(c, Py_True) ||
5661 !compiler_addop_j(c, JUMP_FORWARD, end))
5662 {
5663 goto error;
5664 }
5665 // This is where we handle failed sub-patterns. For a sequence pattern like
5666 // [a, b, c, d], this will look like:
5667 // fails[0]: POP_TOP
5668 // fails[1]: POP_TOP
5669 // fails[2]: POP_TOP
5670 // fails[3]: LOAD_CONST False
5671 for (Py_ssize_t i = 0; i < size - 1; i++) {
5672 compiler_use_next_block(c, fails[i]);
5673 if (!compiler_addop(c, POP_TOP)) {
5674 goto error;
5675 }
5676 }
5677 compiler_use_next_block(c, fails[size - 1]);
5678 if (!compiler_addop_load_const(c, Py_False)) {
5679 goto error;
5680 }
5681 compiler_use_next_block(c, end);
5682 PyObject_Free(fails);
5683 return 1;
5684error:
5685 PyObject_Free(fails);
5686 return 0;
5687}
5688
5689// Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of
5690// UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a
5691// starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc.
5692static int
5693pattern_helper_sequence_subscr(struct compiler *c, asdl_expr_seq *values,
5694 Py_ssize_t star, pattern_context *pc)
5695{
5696 basicblock *end, *fail_pop_1;
5697 RETURN_IF_FALSE(end = compiler_new_block(c));
5698 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5699 Py_ssize_t size = asdl_seq_LEN(values);
5700 for (Py_ssize_t i = 0; i < size; i++) {
5701 expr_ty value = asdl_seq_GET(values, i);
5702 if (WILDCARD_CHECK(value)) {
5703 continue;
5704 }
5705 if (i == star) {
5706 assert(value->kind == Starred_kind);
5707 assert(WILDCARD_CHECK(value->v.Starred.value));
5708 continue;
5709 }
5710 ADDOP(c, DUP_TOP);
5711 if (i < star) {
5712 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5713 }
5714 else {
5715 // The subject may not support negative indexing! Compute a
5716 // nonnegative index:
5717 ADDOP(c, GET_LEN);
5718 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i));
5719 ADDOP(c, BINARY_SUBTRACT);
5720 }
5721 ADDOP(c, BINARY_SUBSCR);
5722 RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc));
5723 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5724 NEXT_BLOCK(c);
5725 }
5726 ADDOP(c, POP_TOP);
5727 ADDOP_LOAD_CONST(c, Py_True);
5728 ADDOP_JUMP(c, JUMP_FORWARD, end);
5729 compiler_use_next_block(c, fail_pop_1);
5730 ADDOP(c, POP_TOP);
5731 ADDOP_LOAD_CONST(c, Py_False);
5732 compiler_use_next_block(c, end);
5733 return 1;
5734}
5735
5736
5737// Like compiler_pattern, but turn off checks for irrefutability.
5738static int
5739compiler_pattern_subpattern(struct compiler *c, expr_ty p, pattern_context *pc)
5740{
5741 int allow_irrefutable = pc->allow_irrefutable;
5742 pc->allow_irrefutable = 1;
5743 RETURN_IF_FALSE(compiler_pattern(c, p, pc));
5744 pc->allow_irrefutable = allow_irrefutable;
5745 return 1;
5746}
5747
5748
5749static int
5750compiler_pattern_as(struct compiler *c, expr_ty p, pattern_context *pc)
5751{
5752 assert(p->kind == MatchAs_kind);
5753 basicblock *end, *fail_pop_1;
5754 RETURN_IF_FALSE(end = compiler_new_block(c));
5755 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5756 // Need to make a copy for (possibly) storing later:
5757 ADDOP(c, DUP_TOP);
5758 RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc));
5759 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5760 NEXT_BLOCK(c);
5761 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc));
5762 ADDOP_LOAD_CONST(c, Py_True);
5763 ADDOP_JUMP(c, JUMP_FORWARD, end);
5764 compiler_use_next_block(c, fail_pop_1);
5765 // Need to pop that unused copy from before:
5766 ADDOP(c, POP_TOP);
5767 ADDOP_LOAD_CONST(c, Py_False);
5768 compiler_use_next_block(c, end);
5769 return 1;
5770}
5771
5772
5773static int
5774compiler_pattern_capture(struct compiler *c, expr_ty p, pattern_context *pc)
5775{
5776 assert(p->kind == Name_kind);
5777 assert(p->v.Name.ctx == Store);
5778 assert(!WILDCARD_CHECK(p));
5779 if (!pc->allow_irrefutable) {
5780 // Whoops, can't have a name capture here!
5781 const char *e = "name capture %R makes remaining patterns unreachable";
5782 return compiler_error(c, e, p->v.Name.id);
5783 }
5784 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.Name.id, pc));
5785 ADDOP_LOAD_CONST(c, Py_True);
5786 return 1;
5787}
5788
5789
5790static int
5791compiler_pattern_class(struct compiler *c, expr_ty p, pattern_context *pc)
5792{
5793 asdl_expr_seq *args = p->v.Call.args;
5794 asdl_keyword_seq *kwargs = p->v.Call.keywords;
5795 Py_ssize_t nargs = asdl_seq_LEN(args);
5796 Py_ssize_t nkwargs = asdl_seq_LEN(kwargs);
5797 if (INT_MAX < nargs || INT_MAX < nargs + nkwargs - 1) {
5798 const char *e = "too many sub-patterns in class pattern %R";
5799 return compiler_error(c, e, p->v.Call.func);
5800 }
5801 RETURN_IF_FALSE(!validate_keywords(c, kwargs));
5802 basicblock *end, *fail_pop_1;
5803 RETURN_IF_FALSE(end = compiler_new_block(c));
5804 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5805 VISIT(c, expr, p->v.Call.func);
5806 PyObject *kwnames;
5807 RETURN_IF_FALSE(kwnames = PyTuple_New(nkwargs));
5808 Py_ssize_t i;
5809 for (i = 0; i < nkwargs; i++) {
5810 PyObject *name = ((keyword_ty) asdl_seq_GET(kwargs, i))->arg;
5811 Py_INCREF(name);
5812 PyTuple_SET_ITEM(kwnames, i, name);
5813 }
5814 ADDOP_LOAD_CONST_NEW(c, kwnames);
5815 ADDOP_I(c, MATCH_CLASS, nargs);
5816 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5817 NEXT_BLOCK(c);
5818 // TOS is now a tuple of (nargs + nkwargs) attributes.
5819 for (i = 0; i < nargs + nkwargs; i++) {
5820 expr_ty arg;
5821 if (i < nargs) {
5822 // Positional:
5823 arg = asdl_seq_GET(args, i);
5824 }
5825 else {
5826 // Keyword:
5827 arg = ((keyword_ty) asdl_seq_GET(kwargs, i - nargs))->value;
5828 }
5829 if (WILDCARD_CHECK(arg)) {
5830 continue;
5831 }
5832 // Get the i-th attribute, and match it against the i-th pattern:
5833 ADDOP(c, DUP_TOP);
5834 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5835 ADDOP(c, BINARY_SUBSCR);
5836 RETURN_IF_FALSE(compiler_pattern_subpattern(c, arg, pc));
5837 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5838 NEXT_BLOCK(c);
5839 }
5840 // Success! Pop the tuple of attributes:
5841 ADDOP(c, POP_TOP);
5842 ADDOP_LOAD_CONST(c, Py_True);
5843 ADDOP_JUMP(c, JUMP_FORWARD, end);
5844 compiler_use_next_block(c, fail_pop_1);
5845 ADDOP(c, POP_TOP);
5846 ADDOP_LOAD_CONST(c, Py_False);
5847 compiler_use_next_block(c, end);
5848 return 1;
5849}
5850
5851
5852static int
5853compiler_pattern_literal(struct compiler *c, expr_ty p, pattern_context *pc)
5854{
5855 assert(p->kind == Constant_kind);
5856 PyObject *v = p->v.Constant.value;
5857 ADDOP_LOAD_CONST(c, v);
5858 // Literal True, False, and None are compared by identity. All others use
5859 // equality:
5860 ADDOP_COMPARE(c, (v == Py_None || PyBool_Check(v)) ? Is : Eq);
5861 return 1;
5862}
5863
5864
5865static int
5866compiler_pattern_mapping(struct compiler *c, expr_ty p, pattern_context *pc)
5867{
5868 basicblock *end, *fail_pop_1, *fail_pop_3;
5869 RETURN_IF_FALSE(end = compiler_new_block(c));
5870 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5871 RETURN_IF_FALSE(fail_pop_3 = compiler_new_block(c));
5872 asdl_expr_seq *keys = p->v.Dict.keys;
5873 asdl_expr_seq *values = p->v.Dict.values;
5874 Py_ssize_t size = asdl_seq_LEN(values);
Ikko Ashimine57827f82021-03-10 19:39:51 +09005875 // A starred pattern will be a keyless value. It is guaranteed to be last:
Brandt Bucher145bf262021-02-26 14:51:55 -08005876 int star = size ? !asdl_seq_GET(keys, size - 1) : 0;
5877 ADDOP(c, MATCH_MAPPING);
5878 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5879 NEXT_BLOCK(c);
5880 if (!size) {
5881 // If the pattern is just "{}", we're done!
5882 ADDOP(c, POP_TOP);
5883 ADDOP_LOAD_CONST(c, Py_True);
5884 ADDOP_JUMP(c, JUMP_FORWARD, end);
5885 compiler_use_next_block(c, fail_pop_1);
5886 ADDOP(c, POP_TOP);
5887 ADDOP_LOAD_CONST(c, Py_False);
5888 compiler_use_next_block(c, end);
5889 return 1;
5890 }
5891 if (size - star) {
5892 // If the pattern has any keys in it, perform a length check:
5893 ADDOP(c, GET_LEN);
5894 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - star));
5895 ADDOP_COMPARE(c, GtE);
5896 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5897 NEXT_BLOCK(c);
5898 }
5899 if (INT_MAX < size - star - 1) {
5900 return compiler_error(c, "too many sub-patterns in mapping pattern");
5901 }
5902 // Collect all of the keys into a tuple for MATCH_KEYS and
5903 // COPY_DICT_WITHOUT_KEYS. They can either be dotted names or literals:
5904 for (Py_ssize_t i = 0; i < size - star; i++) {
5905 expr_ty key = asdl_seq_GET(keys, i);
5906 if (key == NULL) {
5907 const char *e = "can't use starred name here "
5908 "(consider moving to end)";
5909 return compiler_error(c, e);
5910 }
5911 VISIT(c, expr, key);
5912 }
5913 ADDOP_I(c, BUILD_TUPLE, size - star);
5914 ADDOP(c, MATCH_KEYS);
5915 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3);
5916 NEXT_BLOCK(c);
5917 // So far so good. There's now a tuple of values on the stack to match
5918 // sub-patterns against:
5919 for (Py_ssize_t i = 0; i < size - star; i++) {
5920 expr_ty value = asdl_seq_GET(values, i);
5921 if (WILDCARD_CHECK(value)) {
5922 continue;
5923 }
5924 ADDOP(c, DUP_TOP);
5925 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5926 ADDOP(c, BINARY_SUBSCR);
5927 RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc));
5928 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3);
5929 NEXT_BLOCK(c);
5930 }
5931 // If we get this far, it's a match! We're done with that tuple of values.
5932 ADDOP(c, POP_TOP);
5933 if (star) {
5934 // If we had a starred name, bind a dict of remaining items to it:
5935 ADDOP(c, COPY_DICT_WITHOUT_KEYS);
5936 PyObject *id = asdl_seq_GET(values, size - 1)->v.Name.id;
5937 RETURN_IF_FALSE(pattern_helper_store_name(c, id, pc));
5938 }
5939 else {
5940 // Otherwise, we don't care about this tuple of keys anymore:
5941 ADDOP(c, POP_TOP);
5942 }
5943 // Pop the subject:
5944 ADDOP(c, POP_TOP);
5945 ADDOP_LOAD_CONST(c, Py_True);
5946 ADDOP_JUMP(c, JUMP_FORWARD, end);
5947 // The top two items are a tuple of values or None, followed by a tuple of
5948 // keys. Pop them both:
5949 compiler_use_next_block(c, fail_pop_3);
5950 ADDOP(c, POP_TOP);
5951 ADDOP(c, POP_TOP);
5952 compiler_use_next_block(c, fail_pop_1);
5953 // Pop the subject:
5954 ADDOP(c, POP_TOP);
5955 ADDOP_LOAD_CONST(c, Py_False);
5956 compiler_use_next_block(c, end);
5957 return 1;
5958}
5959
5960
5961static int
5962compiler_pattern_or(struct compiler *c, expr_ty p, pattern_context *pc)
5963{
5964 assert(p->kind == MatchOr_kind);
5965 // control is the set of names bound by the first alternative. If all of the
5966 // others bind the same names (they should), then this becomes pc->stores.
5967 PyObject *control = NULL;
5968 basicblock *end, *pass_pop_1;
5969 RETURN_IF_FALSE(end = compiler_new_block(c));
5970 RETURN_IF_FALSE(pass_pop_1 = compiler_new_block(c));
5971 Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns);
5972 assert(size > 1);
5973 // We're going to be messing with pc. Keep the original info handy:
5974 PyObject *stores_init = pc->stores;
5975 int allow_irrefutable = pc->allow_irrefutable;
5976 for (Py_ssize_t i = 0; i < size; i++) {
5977 // NOTE: Can't use our nice returning macros in here: they'll leak sets!
5978 expr_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i);
5979 pc->stores = PySet_New(stores_init);
5980 // An irrefutable sub-pattern must be last, if it is allowed at all:
5981 int is_last = i == size - 1;
5982 pc->allow_irrefutable = allow_irrefutable && is_last;
5983 SET_LOC(c, alt);
5984 if (pc->stores == NULL ||
5985 // Only copy the subject if we're *not* on the last alternative:
5986 (!is_last && !compiler_addop(c, DUP_TOP)) ||
5987 !compiler_pattern(c, alt, pc) ||
5988 // Only jump if we're *not* on the last alternative:
5989 (!is_last && !compiler_addop_j(c, POP_JUMP_IF_TRUE, pass_pop_1)) ||
5990 !compiler_next_block(c))
5991 {
5992 goto fail;
5993 }
5994 if (!i) {
5995 // If this is the first alternative, save its stores as a "control"
5996 // for the others (they can't bind a different set of names):
5997 control = pc->stores;
5998 continue;
5999 }
6000 if (PySet_GET_SIZE(pc->stores) || PySet_GET_SIZE(control)) {
6001 // Otherwise, check to see if we differ from the control set:
6002 PyObject *diff = PyNumber_InPlaceXor(pc->stores, control);
6003 if (diff == NULL) {
6004 goto fail;
6005 }
6006 if (PySet_GET_SIZE(diff)) {
6007 // The names differ! Raise.
6008 Py_DECREF(diff);
6009 compiler_error(c, "alternative patterns bind different names");
6010 goto fail;
6011 }
6012 Py_DECREF(diff);
6013 }
6014 Py_DECREF(pc->stores);
6015 }
6016 Py_XDECREF(stores_init);
6017 // Update pc->stores and restore pc->allow_irrefutable:
6018 pc->stores = control;
6019 pc->allow_irrefutable = allow_irrefutable;
6020 ADDOP_JUMP(c, JUMP_FORWARD, end);
6021 compiler_use_next_block(c, pass_pop_1);
6022 ADDOP(c, POP_TOP);
6023 ADDOP_LOAD_CONST(c, Py_True);
6024 compiler_use_next_block(c, end);
6025 return 1;
6026fail:
6027 Py_XDECREF(stores_init);
6028 Py_XDECREF(control);
6029 return 0;
6030}
6031
6032
6033static int
6034compiler_pattern_sequence(struct compiler *c, expr_ty p, pattern_context *pc)
6035{
6036 assert(p->kind == List_kind || p->kind == Tuple_kind);
6037 asdl_expr_seq *values = (p->kind == Tuple_kind) ? p->v.Tuple.elts
6038 : p->v.List.elts;
6039 Py_ssize_t size = asdl_seq_LEN(values);
6040 Py_ssize_t star = -1;
6041 int only_wildcard = 1;
6042 int star_wildcard = 0;
6043 // Find a starred name, if it exists. There may be at most one:
6044 for (Py_ssize_t i = 0; i < size; i++) {
6045 expr_ty value = asdl_seq_GET(values, i);
6046 if (value->kind == Starred_kind) {
6047 value = value->v.Starred.value;
6048 if (star >= 0) {
6049 const char *e = "multiple starred names in sequence pattern";
6050 return compiler_error(c, e);
6051 }
6052 star_wildcard = WILDCARD_CHECK(value);
6053 star = i;
6054 }
6055 only_wildcard &= WILDCARD_CHECK(value);
6056 }
6057 basicblock *end, *fail_pop_1;
6058 RETURN_IF_FALSE(end = compiler_new_block(c));
6059 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
6060 ADDOP(c, MATCH_SEQUENCE);
6061 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
6062 NEXT_BLOCK(c);
6063 if (star < 0) {
6064 // No star: len(subject) == size
6065 ADDOP(c, GET_LEN);
6066 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
6067 ADDOP_COMPARE(c, Eq);
6068 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
6069 NEXT_BLOCK(c);
6070 }
6071 else if (size > 1) {
6072 // Star: len(subject) >= size - 1
6073 ADDOP(c, GET_LEN);
6074 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1));
6075 ADDOP_COMPARE(c, GtE);
6076 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
6077 NEXT_BLOCK(c);
6078 }
6079 if (only_wildcard) {
6080 // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc.
6081 ADDOP(c, POP_TOP);
6082 ADDOP_LOAD_CONST(c, Py_True);
6083 }
6084 else if (star_wildcard) {
6085 RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, values, star, pc));
6086 }
6087 else {
6088 RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, values, star, pc));
6089 }
6090 ADDOP_JUMP(c, JUMP_FORWARD, end);
6091 compiler_use_next_block(c, fail_pop_1);
6092 ADDOP(c, POP_TOP)
6093 ADDOP_LOAD_CONST(c, Py_False);
6094 compiler_use_next_block(c, end);
6095 return 1;
6096}
6097
6098
6099static int
6100compiler_pattern_value(struct compiler *c, expr_ty p, pattern_context *pc)
6101{
6102 assert(p->kind == Attribute_kind);
6103 assert(p->v.Attribute.ctx == Load);
6104 VISIT(c, expr, p);
6105 ADDOP_COMPARE(c, Eq);
6106 return 1;
6107}
6108
6109
6110static int
6111compiler_pattern_wildcard(struct compiler *c, expr_ty p, pattern_context *pc)
6112{
6113 assert(p->kind == Name_kind);
6114 assert(p->v.Name.ctx == Store);
6115 assert(WILDCARD_CHECK(p));
6116 if (!pc->allow_irrefutable) {
6117 // Whoops, can't have a wildcard here!
6118 const char *e = "wildcard makes remaining patterns unreachable";
6119 return compiler_error(c, e);
6120 }
6121 ADDOP(c, POP_TOP);
6122 ADDOP_LOAD_CONST(c, Py_True);
6123 return 1;
6124}
6125
6126
6127static int
6128compiler_pattern(struct compiler *c, expr_ty p, pattern_context *pc)
6129{
6130 SET_LOC(c, p);
6131 switch (p->kind) {
6132 case Attribute_kind:
6133 return compiler_pattern_value(c, p, pc);
6134 case BinOp_kind:
6135 // Because we allow "2+2j", things like "2+2" make it this far:
6136 return compiler_error(c, "patterns cannot include operators");
6137 case Call_kind:
6138 return compiler_pattern_class(c, p, pc);
6139 case Constant_kind:
6140 return compiler_pattern_literal(c, p, pc);
6141 case Dict_kind:
6142 return compiler_pattern_mapping(c, p, pc);
6143 case JoinedStr_kind:
6144 // Because we allow strings, f-strings make it this far:
6145 return compiler_error(c, "patterns cannot include f-strings");
6146 case List_kind:
6147 case Tuple_kind:
6148 return compiler_pattern_sequence(c, p, pc);
6149 case MatchAs_kind:
6150 return compiler_pattern_as(c, p, pc);
6151 case MatchOr_kind:
6152 return compiler_pattern_or(c, p, pc);
6153 case Name_kind:
6154 if (WILDCARD_CHECK(p)) {
6155 return compiler_pattern_wildcard(c, p, pc);
6156 }
6157 return compiler_pattern_capture(c, p, pc);
6158 default:
6159 Py_UNREACHABLE();
6160 }
6161}
6162
6163
6164static int
6165compiler_match(struct compiler *c, stmt_ty s)
6166{
6167 VISIT(c, expr, s->v.Match.subject);
6168 basicblock *next, *end;
6169 RETURN_IF_FALSE(end = compiler_new_block(c));
6170 Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases);
6171 assert(cases);
6172 pattern_context pc;
6173 // We use pc.stores to track:
6174 // - Repeated name assignments in the same pattern.
6175 // - Different name assignments in alternatives.
6176 // It's a set of names, but we don't create it until it's needed:
6177 pc.stores = NULL;
6178 match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6179 int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases;
6180 for (Py_ssize_t i = 0; i < cases - has_default; i++) {
6181 m = asdl_seq_GET(s->v.Match.cases, i);
6182 SET_LOC(c, m->pattern);
6183 RETURN_IF_FALSE(next = compiler_new_block(c));
6184 // If pc.allow_irrefutable is 0, any name captures against our subject
6185 // will raise. Irrefutable cases must be either guarded, last, or both:
6186 pc.allow_irrefutable = m->guard != NULL || i == cases - 1;
6187 // Only copy the subject if we're *not* on the last case:
6188 if (i != cases - has_default - 1) {
6189 ADDOP(c, DUP_TOP);
6190 }
6191 int result = compiler_pattern(c, m->pattern, &pc);
6192 Py_CLEAR(pc.stores);
6193 RETURN_IF_FALSE(result);
6194 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, next);
6195 NEXT_BLOCK(c);
6196 if (m->guard) {
6197 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, next, 0));
6198 }
6199 // Success! Pop the subject off, we're done with it:
6200 if (i != cases - has_default - 1) {
6201 ADDOP(c, POP_TOP);
6202 }
6203 VISIT_SEQ(c, stmt, m->body);
6204 ADDOP_JUMP(c, JUMP_FORWARD, end);
6205 compiler_use_next_block(c, next);
6206 }
6207 if (has_default) {
6208 if (cases == 1) {
6209 // No matches. Done with the subject:
6210 ADDOP(c, POP_TOP);
6211 }
6212 // A trailing "case _" is common, and lets us save a bit of redundant
6213 // pushing and popping in the loop above:
6214 m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6215 SET_LOC(c, m->pattern);
6216 if (m->guard) {
6217 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0));
6218 }
6219 VISIT_SEQ(c, stmt, m->body);
6220 }
6221 compiler_use_next_block(c, end);
6222 return 1;
6223}
6224
6225
6226#undef WILDCARD_CHECK
6227
6228
Thomas Wouters89f507f2006-12-13 04:49:30 +00006229/* End of the compiler section, beginning of the assembler section */
6230
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006231/* do depth-first search of basic block graph, starting with block.
T. Wouters99b54d62019-09-12 07:05:33 -07006232 post records the block indices in post-order.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006233
6234 XXX must handle implicit jumps from one block to next
6235*/
6236
Thomas Wouters89f507f2006-12-13 04:49:30 +00006237struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006238 PyObject *a_bytecode; /* string containing bytecode */
6239 int a_offset; /* offset into bytecode */
6240 int a_nblocks; /* number of reachable blocks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006241 PyObject *a_lnotab; /* string containing lnotab */
6242 int a_lnotab_off; /* offset into lnotab */
Mark Shannon877df852020-11-12 09:43:29 +00006243 int a_prevlineno; /* lineno of last emitted line in line table */
6244 int a_lineno; /* lineno of last emitted instruction */
6245 int a_lineno_start; /* bytecode start offset of current lineno */
Mark Shannoncc75ab72020-11-12 19:49:33 +00006246 basicblock *a_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006247};
6248
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006249Py_LOCAL_INLINE(void)
6250stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006251{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02006252 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
Mark Shannonfee55262019-11-21 09:11:43 +00006253 if (b->b_startdepth < depth && b->b_startdepth < 100) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006254 assert(b->b_startdepth < 0);
6255 b->b_startdepth = depth;
6256 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02006257 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006258}
6259
6260/* Find the flow path that needs the largest stack. We assume that
6261 * cycles in the flow graph have no net effect on the stack depth.
6262 */
6263static int
6264stackdepth(struct compiler *c)
6265{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006266 basicblock *b, *entryblock = NULL;
6267 basicblock **stack, **sp;
6268 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006269 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006270 b->b_startdepth = INT_MIN;
6271 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006272 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006273 }
Mark Shannon67969f52021-04-07 10:52:07 +01006274 assert(entryblock!= NULL);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006275 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
6276 if (!stack) {
6277 PyErr_NoMemory();
6278 return -1;
6279 }
6280
6281 sp = stack;
Mark Shannonb37181e2021-04-06 11:48:59 +01006282 if (c->u->u_ste->ste_generator || c->u->u_ste->ste_coroutine) {
6283 stackdepth_push(&sp, entryblock, 1);
6284 } else {
6285 stackdepth_push(&sp, entryblock, 0);
6286 }
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006287 while (sp != stack) {
6288 b = *--sp;
6289 int depth = b->b_startdepth;
6290 assert(depth >= 0);
6291 basicblock *next = b->b_next;
6292 for (int i = 0; i < b->b_iused; i++) {
6293 struct instr *instr = &b->b_instr[i];
6294 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
6295 if (effect == PY_INVALID_STACK_EFFECT) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01006296 PyErr_Format(PyExc_SystemError,
6297 "compiler stack_effect(opcode=%d, arg=%i) failed",
6298 instr->i_opcode, instr->i_oparg);
6299 return -1;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006300 }
6301 int new_depth = depth + effect;
6302 if (new_depth > maxdepth) {
6303 maxdepth = new_depth;
6304 }
6305 assert(depth >= 0); /* invalid code or bug in stackdepth() */
Mark Shannon582aaf12020-08-04 17:30:11 +01006306 if (is_jump(instr)) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006307 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
6308 assert(effect != PY_INVALID_STACK_EFFECT);
6309 int target_depth = depth + effect;
6310 if (target_depth > maxdepth) {
6311 maxdepth = target_depth;
6312 }
6313 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006314 stackdepth_push(&sp, instr->i_target, target_depth);
6315 }
6316 depth = new_depth;
6317 if (instr->i_opcode == JUMP_ABSOLUTE ||
6318 instr->i_opcode == JUMP_FORWARD ||
6319 instr->i_opcode == RETURN_VALUE ||
Mark Shannonfee55262019-11-21 09:11:43 +00006320 instr->i_opcode == RAISE_VARARGS ||
6321 instr->i_opcode == RERAISE)
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006322 {
6323 /* remaining code is dead */
6324 next = NULL;
6325 break;
6326 }
6327 }
6328 if (next != NULL) {
Mark Shannon266b4622020-11-17 19:30:14 +00006329 assert(b->b_nofallthrough == 0);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006330 stackdepth_push(&sp, next, depth);
6331 }
6332 }
6333 PyObject_Free(stack);
6334 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006335}
6336
6337static int
6338assemble_init(struct assembler *a, int nblocks, int firstlineno)
6339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006340 memset(a, 0, sizeof(struct assembler));
Mark Shannon877df852020-11-12 09:43:29 +00006341 a->a_prevlineno = a->a_lineno = firstlineno;
Mark Shannonfd009e62020-11-13 12:53:53 +00006342 a->a_lnotab = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006343 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006344 if (a->a_bytecode == NULL) {
6345 goto error;
6346 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006347 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006348 if (a->a_lnotab == NULL) {
6349 goto error;
6350 }
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07006351 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006352 PyErr_NoMemory();
Mark Shannonfd009e62020-11-13 12:53:53 +00006353 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006354 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006355 return 1;
Mark Shannonfd009e62020-11-13 12:53:53 +00006356error:
6357 Py_XDECREF(a->a_bytecode);
6358 Py_XDECREF(a->a_lnotab);
6359 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006360}
6361
6362static void
6363assemble_free(struct assembler *a)
6364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006365 Py_XDECREF(a->a_bytecode);
6366 Py_XDECREF(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006367}
6368
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006369static int
6370blocksize(basicblock *b)
6371{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006372 int i;
6373 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006375 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006376 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006377 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006378}
6379
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006380static int
Mark Shannon877df852020-11-12 09:43:29 +00006381assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006382{
Mark Shannon877df852020-11-12 09:43:29 +00006383 Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006384 if (a->a_lnotab_off + 2 >= len) {
6385 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
6386 return 0;
6387 }
Pablo Galindo86e322f2021-01-30 13:54:22 +00006388 unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab);
6389 lnotab += a->a_lnotab_off;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006390 a->a_lnotab_off += 2;
Mark Shannon877df852020-11-12 09:43:29 +00006391 *lnotab++ = bdelta;
6392 *lnotab++ = ldelta;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006393 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006394}
6395
Mark Shannon877df852020-11-12 09:43:29 +00006396/* Appends a range to the end of the line number table. See
6397 * Objects/lnotab_notes.txt for the description of the line number table. */
6398
6399static int
6400assemble_line_range(struct assembler *a)
6401{
6402 int ldelta, bdelta;
6403 bdelta = (a->a_offset - a->a_lineno_start) * 2;
6404 if (bdelta == 0) {
6405 return 1;
6406 }
6407 if (a->a_lineno < 0) {
6408 ldelta = -128;
6409 }
6410 else {
6411 ldelta = a->a_lineno - a->a_prevlineno;
6412 a->a_prevlineno = a->a_lineno;
6413 while (ldelta > 127) {
6414 if (!assemble_emit_linetable_pair(a, 0, 127)) {
6415 return 0;
6416 }
6417 ldelta -= 127;
6418 }
6419 while (ldelta < -127) {
6420 if (!assemble_emit_linetable_pair(a, 0, -127)) {
6421 return 0;
6422 }
6423 ldelta += 127;
6424 }
6425 }
6426 assert(-128 <= ldelta && ldelta < 128);
6427 while (bdelta > 254) {
6428 if (!assemble_emit_linetable_pair(a, 254, ldelta)) {
6429 return 0;
6430 }
6431 ldelta = a->a_lineno < 0 ? -128 : 0;
6432 bdelta -= 254;
6433 }
6434 if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) {
6435 return 0;
6436 }
6437 a->a_lineno_start = a->a_offset;
6438 return 1;
6439}
6440
6441static int
6442assemble_lnotab(struct assembler *a, struct instr *i)
6443{
6444 if (i->i_lineno == a->a_lineno) {
6445 return 1;
6446 }
6447 if (!assemble_line_range(a)) {
6448 return 0;
6449 }
6450 a->a_lineno = i->i_lineno;
6451 return 1;
6452}
6453
6454
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006455/* assemble_emit()
6456 Extend the bytecode with a new instruction.
6457 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00006458*/
6459
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006460static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006461assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006462{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006463 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006464 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03006465 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006466
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006467 arg = i->i_oparg;
6468 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006469 if (i->i_lineno && !assemble_lnotab(a, i))
6470 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006471 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006472 if (len > PY_SSIZE_T_MAX / 2)
6473 return 0;
6474 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
6475 return 0;
6476 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03006477 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006478 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006479 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006480 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006481}
6482
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00006483static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006484assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006486 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006487 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006488 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00006489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006490 /* Compute the size of each block and fixup jump args.
6491 Replace block pointer with position in bytecode. */
6492 do {
6493 totsize = 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006494 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006495 bsize = blocksize(b);
6496 b->b_offset = totsize;
6497 totsize += bsize;
6498 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006499 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006500 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6501 bsize = b->b_offset;
6502 for (i = 0; i < b->b_iused; i++) {
6503 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006504 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006505 /* Relative jumps are computed relative to
6506 the instruction pointer after fetching
6507 the jump instruction.
6508 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006509 bsize += isize;
Mark Shannon582aaf12020-08-04 17:30:11 +01006510 if (is_jump(instr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006511 instr->i_oparg = instr->i_target->b_offset;
Mark Shannon582aaf12020-08-04 17:30:11 +01006512 if (is_relative_jump(instr)) {
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006513 instr->i_oparg -= bsize;
6514 }
6515 if (instrsize(instr->i_oparg) != isize) {
6516 extended_arg_recompile = 1;
6517 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006518 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006519 }
6520 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00006521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006522 /* XXX: This is an awful hack that could hurt performance, but
6523 on the bright side it should work until we come up
6524 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006526 The issue is that in the first loop blocksize() is called
6527 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006528 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006529 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006531 So we loop until we stop seeing new EXTENDED_ARGs.
6532 The only EXTENDED_ARGs that could be popping up are
6533 ones in jump instructions. So this should converge
6534 fairly quickly.
6535 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006536 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00006537}
6538
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006539static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01006540dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006542 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006543 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006545 tuple = PyTuple_New(size);
6546 if (tuple == NULL)
6547 return NULL;
6548 while (PyDict_Next(dict, &pos, &k, &v)) {
6549 i = PyLong_AS_LONG(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006550 Py_INCREF(k);
6551 assert((i - offset) < size);
6552 assert((i - offset) >= 0);
6553 PyTuple_SET_ITEM(tuple, i - offset, k);
6554 }
6555 return tuple;
6556}
6557
6558static PyObject *
6559consts_dict_keys_inorder(PyObject *dict)
6560{
6561 PyObject *consts, *k, *v;
6562 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
6563
6564 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
6565 if (consts == NULL)
6566 return NULL;
6567 while (PyDict_Next(dict, &pos, &k, &v)) {
6568 i = PyLong_AS_LONG(v);
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +03006569 /* The keys of the dictionary can be tuples wrapping a contant.
6570 * (see compiler_add_o and _PyCode_ConstantKey). In that case
6571 * the object we want is always second. */
6572 if (PyTuple_CheckExact(k)) {
6573 k = PyTuple_GET_ITEM(k, 1);
6574 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006575 Py_INCREF(k);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006576 assert(i < size);
6577 assert(i >= 0);
6578 PyList_SET_ITEM(consts, i, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006579 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006580 return consts;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006581}
6582
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006583static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006584compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006585{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006586 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006587 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006588 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04006589 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006590 if (ste->ste_nested)
6591 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07006592 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006593 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07006594 if (!ste->ste_generator && ste->ste_coroutine)
6595 flags |= CO_COROUTINE;
6596 if (ste->ste_generator && ste->ste_coroutine)
6597 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006598 if (ste->ste_varargs)
6599 flags |= CO_VARARGS;
6600 if (ste->ste_varkeywords)
6601 flags |= CO_VARKEYWORDS;
6602 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006604 /* (Only) inherit compilerflags in PyCF_MASK */
6605 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006606
Pablo Galindo90235812020-03-15 04:29:22 +00006607 if ((IS_TOP_LEVEL_AWAIT(c)) &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07006608 ste->ste_coroutine &&
6609 !ste->ste_generator) {
6610 flags |= CO_COROUTINE;
6611 }
6612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006613 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00006614}
6615
Inada Naokibdb941b2021-02-10 09:20:42 +09006616// Merge *obj* with constant cache.
INADA Naokic2e16072018-11-26 21:23:22 +09006617// Unlike merge_consts_recursive(), this function doesn't work recursively.
6618static int
Inada Naokibdb941b2021-02-10 09:20:42 +09006619merge_const_one(struct compiler *c, PyObject **obj)
INADA Naokic2e16072018-11-26 21:23:22 +09006620{
Inada Naokibdb941b2021-02-10 09:20:42 +09006621 PyObject *key = _PyCode_ConstantKey(*obj);
INADA Naokic2e16072018-11-26 21:23:22 +09006622 if (key == NULL) {
6623 return 0;
6624 }
6625
6626 // t is borrowed reference
6627 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
6628 Py_DECREF(key);
6629 if (t == NULL) {
6630 return 0;
6631 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006632 if (t == key) { // obj is new constant.
INADA Naokic2e16072018-11-26 21:23:22 +09006633 return 1;
6634 }
6635
Inada Naokibdb941b2021-02-10 09:20:42 +09006636 if (PyTuple_CheckExact(t)) {
6637 // t is still borrowed reference
6638 t = PyTuple_GET_ITEM(t, 1);
6639 }
6640
6641 Py_INCREF(t);
6642 Py_DECREF(*obj);
6643 *obj = t;
INADA Naokic2e16072018-11-26 21:23:22 +09006644 return 1;
6645}
6646
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006647static PyCodeObject *
Mark Shannon6e8128f2020-07-30 10:03:00 +01006648makecode(struct compiler *c, struct assembler *a, PyObject *consts)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006650 PyCodeObject *co = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006651 PyObject *names = NULL;
6652 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006653 PyObject *name = NULL;
6654 PyObject *freevars = NULL;
6655 PyObject *cellvars = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006656 Py_ssize_t nlocals;
6657 int nlocals_int;
6658 int flags;
Pablo Galindocd74e662019-06-01 18:08:04 +01006659 int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006661 names = dict_keys_inorder(c->u->u_names, 0);
6662 varnames = dict_keys_inorder(c->u->u_varnames, 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006663 if (!names || !varnames) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006664 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006665 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006666 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
6667 if (!cellvars)
6668 goto error;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006669 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006670 if (!freevars)
6671 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006672
Inada Naokibdb941b2021-02-10 09:20:42 +09006673 if (!merge_const_one(c, &names) ||
6674 !merge_const_one(c, &varnames) ||
6675 !merge_const_one(c, &cellvars) ||
6676 !merge_const_one(c, &freevars))
INADA Naokic2e16072018-11-26 21:23:22 +09006677 {
6678 goto error;
6679 }
6680
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006681 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01006682 assert(nlocals < INT_MAX);
6683 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
6684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006685 flags = compute_code_flags(c);
6686 if (flags < 0)
6687 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006688
Mark Shannon6e8128f2020-07-30 10:03:00 +01006689 consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
6690 if (consts == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006691 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006692 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006693 if (!merge_const_one(c, &consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006694 Py_DECREF(consts);
INADA Naokic2e16072018-11-26 21:23:22 +09006695 goto error;
6696 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006697
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01006698 posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int);
Pablo Galindocd74e662019-06-01 18:08:04 +01006699 posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +01006700 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006701 maxdepth = stackdepth(c);
6702 if (maxdepth < 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006703 Py_DECREF(consts);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006704 goto error;
6705 }
Mark Shannon11e0b292021-04-15 14:28:56 +01006706 if (maxdepth > MAX_ALLOWED_STACK_USE) {
6707 PyErr_Format(PyExc_SystemError,
6708 "excessive stack use: stack is %d deep",
6709 maxdepth);
6710 Py_DECREF(consts);
6711 goto error;
6712 }
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006713 co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
Mark Shannon13bc1392020-01-23 09:25:17 +00006714 posonlyargcount, kwonlyargcount, nlocals_int,
Mark Shannon6e8128f2020-07-30 10:03:00 +01006715 maxdepth, flags, a->a_bytecode, consts, names,
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006716 varnames, freevars, cellvars, c->c_filename,
6717 c->u->u_name, c->u->u_firstlineno, a->a_lnotab);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006718 Py_DECREF(consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006719 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006720 Py_XDECREF(names);
6721 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006722 Py_XDECREF(name);
6723 Py_XDECREF(freevars);
6724 Py_XDECREF(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006725 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006726}
6727
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006728
6729/* For debugging purposes only */
6730#if 0
6731static void
6732dump_instr(const struct instr *i)
6733{
Mark Shannon582aaf12020-08-04 17:30:11 +01006734 const char *jrel = (is_relative_jump(instr)) ? "jrel " : "";
6735 const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : "";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006736 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006738 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006739 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006740 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006741 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006742 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
6743 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006744}
6745
6746static void
6747dump_basicblock(const basicblock *b)
6748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006749 const char *b_return = b->b_return ? "return " : "";
Pablo Galindo60eb9f12020-06-28 01:55:47 +01006750 fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n",
6751 b->b_iused, b->b_startdepth, b->b_offset, b_return);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006752 if (b->b_instr) {
6753 int i;
6754 for (i = 0; i < b->b_iused; i++) {
6755 fprintf(stderr, " [%02d] ", i);
6756 dump_instr(b->b_instr + i);
6757 }
6758 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006759}
6760#endif
6761
Mark Shannon5977a792020-12-02 13:31:40 +00006762
6763static int
6764normalize_basic_block(basicblock *bb);
6765
Mark Shannon6e8128f2020-07-30 10:03:00 +01006766static int
Inada Naoki8a232c72021-04-16 14:01:04 +09006767optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006768
Mark Shannon5977a792020-12-02 13:31:40 +00006769static int
6770ensure_exits_have_lineno(struct compiler *c);
6771
Mark Shannonb37181e2021-04-06 11:48:59 +01006772static int
6773insert_generator_prefix(struct compiler *c, basicblock *entryblock) {
6774
6775 int flags = compute_code_flags(c);
6776 if (flags < 0) {
6777 return -1;
6778 }
6779 int kind;
6780 if (flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
6781 if (flags & CO_COROUTINE) {
6782 kind = 1;
6783 }
6784 else if (flags & CO_ASYNC_GENERATOR) {
6785 kind = 2;
6786 }
6787 else {
6788 kind = 0;
6789 }
6790 }
6791 else {
6792 return 0;
6793 }
6794 if (compiler_next_instr(entryblock) < 0) {
6795 return -1;
6796 }
6797 for (int i = entryblock->b_iused-1; i > 0; i--) {
6798 entryblock->b_instr[i] = entryblock->b_instr[i-1];
6799 }
6800 entryblock->b_instr[0].i_opcode = GEN_START;
6801 entryblock->b_instr[0].i_oparg = kind;
6802 entryblock->b_instr[0].i_lineno = -1;
6803 entryblock->b_instr[0].i_target = NULL;
6804 return 0;
6805}
6806
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006807static PyCodeObject *
6808assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006809{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006810 basicblock *b, *entryblock;
6811 struct assembler a;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006812 int j, nblocks;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006813 PyCodeObject *co = NULL;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006814 PyObject *consts = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006816 /* Make sure every block that falls off the end returns None.
6817 XXX NEXT_BLOCK() isn't quite right, because if the last
6818 block ends with a jump or return b_next shouldn't set.
6819 */
6820 if (!c->u->u_curblock->b_return) {
Mark Shannon877df852020-11-12 09:43:29 +00006821 c->u->u_lineno = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006822 if (addNone)
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006823 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006824 ADDOP(c, RETURN_VALUE);
6825 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006826
Mark Shannon5977a792020-12-02 13:31:40 +00006827 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
6828 if (normalize_basic_block(b)) {
Alex Henrie503627f2021-03-02 03:20:25 -07006829 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00006830 }
6831 }
6832
6833 if (ensure_exits_have_lineno(c)) {
Alex Henrie503627f2021-03-02 03:20:25 -07006834 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00006835 }
6836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006837 nblocks = 0;
6838 entryblock = NULL;
6839 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6840 nblocks++;
6841 entryblock = b;
6842 }
Mark Shannon67969f52021-04-07 10:52:07 +01006843 assert(entryblock != NULL);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006844
Mark Shannonb37181e2021-04-06 11:48:59 +01006845 if (insert_generator_prefix(c, entryblock)) {
6846 goto error;
6847 }
6848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006849 /* Set firstlineno if it wasn't explicitly set. */
6850 if (!c->u->u_firstlineno) {
Mark Shannon67969f52021-04-07 10:52:07 +01006851 if (entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006852 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
Mark Shannon877df852020-11-12 09:43:29 +00006853 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006854 c->u->u_firstlineno = 1;
6855 }
Mark Shannon5977a792020-12-02 13:31:40 +00006856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006857 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
6858 goto error;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006859 a.a_entry = entryblock;
6860 a.a_nblocks = nblocks;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006861
Mark Shannon6e8128f2020-07-30 10:03:00 +01006862 consts = consts_dict_keys_inorder(c->u->u_consts);
6863 if (consts == NULL) {
6864 goto error;
6865 }
Inada Naoki8a232c72021-04-16 14:01:04 +09006866 if (optimize_cfg(c, &a, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006867 goto error;
6868 }
6869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006870 /* Can't modify the bytecode after computing jump offsets. */
6871 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00006872
Mark Shannoncc75ab72020-11-12 19:49:33 +00006873 /* Emit code. */
6874 for(b = entryblock; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006875 for (j = 0; j < b->b_iused; j++)
6876 if (!assemble_emit(&a, &b->b_instr[j]))
6877 goto error;
6878 }
Mark Shannon877df852020-11-12 09:43:29 +00006879 if (!assemble_line_range(&a)) {
6880 return 0;
6881 }
6882 /* Emit sentinel at end of line number table */
6883 if (!assemble_emit_linetable_pair(&a, 255, -128)) {
6884 goto error;
6885 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00006886
Inada Naokibdb941b2021-02-10 09:20:42 +09006887 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006888 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09006889 }
6890 if (!merge_const_one(c, &a.a_lnotab)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006891 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09006892 }
6893 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) {
6894 goto error;
6895 }
6896 if (!merge_const_one(c, &a.a_bytecode)) {
6897 goto error;
6898 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006899
Mark Shannon6e8128f2020-07-30 10:03:00 +01006900 co = makecode(c, &a, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006901 error:
Mark Shannon6e8128f2020-07-30 10:03:00 +01006902 Py_XDECREF(consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006903 assemble_free(&a);
6904 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006905}
Georg Brandl8334fd92010-12-04 10:26:46 +00006906
Mark Shannon6e8128f2020-07-30 10:03:00 +01006907/* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
6908 with LOAD_CONST (c1, c2, ... cn).
6909 The consts table must still be in list form so that the
6910 new constant (c1, c2, ... cn) can be appended.
6911 Called with codestr pointing to the first LOAD_CONST.
6912*/
6913static int
Inada Naoki8a232c72021-04-16 14:01:04 +09006914fold_tuple_on_constants(struct compiler *c,
6915 struct instr *inst,
Mark Shannon6e8128f2020-07-30 10:03:00 +01006916 int n, PyObject *consts)
6917{
6918 /* Pre-conditions */
6919 assert(PyList_CheckExact(consts));
6920 assert(inst[n].i_opcode == BUILD_TUPLE);
6921 assert(inst[n].i_oparg == n);
6922
6923 for (int i = 0; i < n; i++) {
6924 if (inst[i].i_opcode != LOAD_CONST) {
6925 return 0;
6926 }
6927 }
6928
6929 /* Buildup new tuple of constants */
6930 PyObject *newconst = PyTuple_New(n);
6931 if (newconst == NULL) {
6932 return -1;
6933 }
6934 for (int i = 0; i < n; i++) {
6935 int arg = inst[i].i_oparg;
6936 PyObject *constant = PyList_GET_ITEM(consts, arg);
6937 Py_INCREF(constant);
6938 PyTuple_SET_ITEM(newconst, i, constant);
6939 }
Inada Naoki8a232c72021-04-16 14:01:04 +09006940 if (merge_const_one(c, &newconst) == 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006941 Py_DECREF(newconst);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006942 return -1;
6943 }
Inada Naoki8a232c72021-04-16 14:01:04 +09006944
6945 Py_ssize_t index;
6946 for (index = 0; index < PyList_GET_SIZE(consts); index++) {
6947 if (PyList_GET_ITEM(consts, index) == newconst) {
6948 break;
6949 }
6950 }
6951 if (index == PyList_GET_SIZE(consts)) {
6952 if ((size_t)index >= (size_t)INT_MAX - 1) {
6953 Py_DECREF(newconst);
6954 PyErr_SetString(PyExc_OverflowError, "too many constants");
6955 return -1;
6956 }
6957 if (PyList_Append(consts, newconst)) {
6958 Py_DECREF(newconst);
6959 return -1;
6960 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006961 }
6962 Py_DECREF(newconst);
6963 for (int i = 0; i < n; i++) {
6964 inst[i].i_opcode = NOP;
6965 }
6966 inst[n].i_opcode = LOAD_CONST;
Victor Stinner71f2ff42020-09-23 14:06:55 +02006967 inst[n].i_oparg = (int)index;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006968 return 0;
6969}
6970
Mark Shannon28b75c82020-12-23 11:43:10 +00006971
6972static int
6973eliminate_jump_to_jump(basicblock *bb, int opcode) {
6974 assert (bb->b_iused > 0);
6975 struct instr *inst = &bb->b_instr[bb->b_iused-1];
6976 assert (is_jump(inst));
6977 assert (inst->i_target->b_iused > 0);
6978 struct instr *target = &inst->i_target->b_instr[0];
6979 if (inst->i_target == target->i_target) {
6980 /* Nothing to do */
6981 return 0;
6982 }
6983 int lineno = target->i_lineno;
6984 if (add_jump_to_block(bb, opcode, lineno, target->i_target) == 0) {
6985 return -1;
6986 }
6987 assert (bb->b_iused >= 2);
6988 bb->b_instr[bb->b_iused-2].i_opcode = NOP;
6989 return 0;
6990}
6991
Mark Shannoncc75ab72020-11-12 19:49:33 +00006992/* Maximum size of basic block that should be copied in optimizer */
6993#define MAX_COPY_SIZE 4
Mark Shannon6e8128f2020-07-30 10:03:00 +01006994
6995/* Optimization */
6996static int
Inada Naoki8a232c72021-04-16 14:01:04 +09006997optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
Mark Shannon6e8128f2020-07-30 10:03:00 +01006998{
6999 assert(PyList_CheckExact(consts));
7000 struct instr nop;
7001 nop.i_opcode = NOP;
7002 struct instr *target;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007003 for (int i = 0; i < bb->b_iused; i++) {
7004 struct instr *inst = &bb->b_instr[i];
7005 int oparg = inst->i_oparg;
7006 int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01007007 if (is_jump(inst)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007008 /* Skip over empty basic blocks. */
7009 while (inst->i_target->b_iused == 0) {
7010 inst->i_target = inst->i_target->b_next;
7011 }
7012 target = &inst->i_target->b_instr[0];
7013 }
7014 else {
7015 target = &nop;
7016 }
7017 switch (inst->i_opcode) {
Mark Shannon266b4622020-11-17 19:30:14 +00007018 /* Remove LOAD_CONST const; conditional jump */
Mark Shannon6e8128f2020-07-30 10:03:00 +01007019 case LOAD_CONST:
Mark Shannon266b4622020-11-17 19:30:14 +00007020 {
7021 PyObject* cnt;
7022 int is_true;
7023 int jump_if_true;
7024 switch(nextop) {
7025 case POP_JUMP_IF_FALSE:
7026 case POP_JUMP_IF_TRUE:
7027 cnt = PyList_GET_ITEM(consts, oparg);
7028 is_true = PyObject_IsTrue(cnt);
7029 if (is_true == -1) {
7030 goto error;
7031 }
7032 inst->i_opcode = NOP;
7033 jump_if_true = nextop == POP_JUMP_IF_TRUE;
7034 if (is_true == jump_if_true) {
7035 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
7036 bb->b_nofallthrough = 1;
7037 }
7038 else {
7039 bb->b_instr[i+1].i_opcode = NOP;
7040 }
7041 break;
7042 case JUMP_IF_FALSE_OR_POP:
7043 case JUMP_IF_TRUE_OR_POP:
7044 cnt = PyList_GET_ITEM(consts, oparg);
7045 is_true = PyObject_IsTrue(cnt);
7046 if (is_true == -1) {
7047 goto error;
7048 }
7049 jump_if_true = nextop == JUMP_IF_TRUE_OR_POP;
7050 if (is_true == jump_if_true) {
7051 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
7052 bb->b_nofallthrough = 1;
7053 }
7054 else {
7055 inst->i_opcode = NOP;
7056 bb->b_instr[i+1].i_opcode = NOP;
7057 }
7058 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007059 }
7060 break;
Mark Shannon266b4622020-11-17 19:30:14 +00007061 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007062
7063 /* Try to fold tuples of constants.
7064 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
7065 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
7066 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
7067 case BUILD_TUPLE:
7068 if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
7069 switch(oparg) {
7070 case 1:
7071 inst->i_opcode = NOP;
7072 bb->b_instr[i+1].i_opcode = NOP;
7073 break;
7074 case 2:
7075 inst->i_opcode = ROT_TWO;
7076 bb->b_instr[i+1].i_opcode = NOP;
7077 break;
7078 case 3:
7079 inst->i_opcode = ROT_THREE;
7080 bb->b_instr[i+1].i_opcode = ROT_TWO;
7081 }
7082 break;
7083 }
7084 if (i >= oparg) {
Inada Naoki8a232c72021-04-16 14:01:04 +09007085 if (fold_tuple_on_constants(c, inst-oparg, oparg, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007086 goto error;
7087 }
7088 }
7089 break;
7090
7091 /* Simplify conditional jump to conditional jump where the
7092 result of the first test implies the success of a similar
7093 test or the failure of the opposite test.
7094 Arises in code like:
7095 "a and b or c"
7096 "(a and b) and c"
7097 "(a or b) or c"
7098 "(a or b) and c"
7099 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
7100 --> x:JUMP_IF_FALSE_OR_POP z
7101 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
7102 --> x:POP_JUMP_IF_FALSE y+1
7103 where y+1 is the instruction following the second test.
7104 */
7105 case JUMP_IF_FALSE_OR_POP:
7106 switch(target->i_opcode) {
7107 case POP_JUMP_IF_FALSE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007108 if (inst->i_lineno == target->i_lineno) {
7109 *inst = *target;
7110 i--;
7111 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007112 break;
7113 case JUMP_ABSOLUTE:
7114 case JUMP_FORWARD:
7115 case JUMP_IF_FALSE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00007116 if (inst->i_lineno == target->i_lineno &&
7117 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00007118 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007119 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007120 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007121 break;
7122 case JUMP_IF_TRUE_OR_POP:
7123 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00007124 if (inst->i_lineno == target->i_lineno) {
7125 inst->i_opcode = POP_JUMP_IF_FALSE;
7126 inst->i_target = inst->i_target->b_next;
7127 --i;
7128 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007129 break;
7130 }
7131 break;
7132
7133 case JUMP_IF_TRUE_OR_POP:
7134 switch(target->i_opcode) {
7135 case POP_JUMP_IF_TRUE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007136 if (inst->i_lineno == target->i_lineno) {
7137 *inst = *target;
7138 i--;
7139 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007140 break;
7141 case JUMP_ABSOLUTE:
7142 case JUMP_FORWARD:
7143 case JUMP_IF_TRUE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00007144 if (inst->i_lineno == target->i_lineno &&
7145 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00007146 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007147 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007148 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007149 break;
7150 case JUMP_IF_FALSE_OR_POP:
7151 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00007152 if (inst->i_lineno == target->i_lineno) {
7153 inst->i_opcode = POP_JUMP_IF_TRUE;
7154 inst->i_target = inst->i_target->b_next;
7155 --i;
7156 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007157 break;
7158 }
7159 break;
7160
7161 case POP_JUMP_IF_FALSE:
7162 switch(target->i_opcode) {
7163 case JUMP_ABSOLUTE:
7164 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007165 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00007166 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007167 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007168 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007169 break;
7170 }
7171 break;
7172
7173 case POP_JUMP_IF_TRUE:
7174 switch(target->i_opcode) {
7175 case JUMP_ABSOLUTE:
7176 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007177 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00007178 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007179 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007180 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007181 break;
7182 }
7183 break;
7184
7185 case JUMP_ABSOLUTE:
7186 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007187 assert (i == bb->b_iused-1);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007188 switch(target->i_opcode) {
7189 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007190 if (eliminate_jump_to_jump(bb, inst->i_opcode)) {
7191 goto error;
Mark Shannon266b4622020-11-17 19:30:14 +00007192 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007193 break;
Mark Shannon28b75c82020-12-23 11:43:10 +00007194
Mark Shannon6e8128f2020-07-30 10:03:00 +01007195 case JUMP_ABSOLUTE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007196 if (eliminate_jump_to_jump(bb, JUMP_ABSOLUTE)) {
7197 goto error;
Mark Shannon266b4622020-11-17 19:30:14 +00007198 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007199 break;
Mark Shannon28b75c82020-12-23 11:43:10 +00007200 default:
7201 if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) {
7202 basicblock *to_copy = inst->i_target;
7203 inst->i_opcode = NOP;
7204 for (i = 0; i < to_copy->b_iused; i++) {
7205 int index = compiler_next_instr(bb);
7206 if (index < 0) {
7207 return -1;
7208 }
7209 bb->b_instr[index] = to_copy->b_instr[i];
7210 }
7211 bb->b_exit = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007212 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007213 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007214 }
7215 }
7216 return 0;
7217error:
7218 return -1;
7219}
7220
7221
7222static void
Mark Shannon1659ad12021-01-13 15:05:04 +00007223clean_basic_block(basicblock *bb, int prev_lineno) {
7224 /* Remove NOPs when legal to do so. */
Mark Shannon6e8128f2020-07-30 10:03:00 +01007225 int dest = 0;
7226 for (int src = 0; src < bb->b_iused; src++) {
Mark Shannon877df852020-11-12 09:43:29 +00007227 int lineno = bb->b_instr[src].i_lineno;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007228 if (bb->b_instr[src].i_opcode == NOP) {
Mark Shannon266b4622020-11-17 19:30:14 +00007229 /* Eliminate no-op if it doesn't have a line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007230 if (lineno < 0) {
7231 continue;
7232 }
Mark Shannon266b4622020-11-17 19:30:14 +00007233 /* or, if the previous instruction had the same line number. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007234 if (prev_lineno == lineno) {
7235 continue;
7236 }
Mark Shannon266b4622020-11-17 19:30:14 +00007237 /* or, if the next instruction has same line number or no line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007238 if (src < bb->b_iused - 1) {
7239 int next_lineno = bb->b_instr[src+1].i_lineno;
7240 if (next_lineno < 0 || next_lineno == lineno) {
7241 bb->b_instr[src+1].i_lineno = lineno;
7242 continue;
Mark Shannon877df852020-11-12 09:43:29 +00007243 }
7244 }
Mark Shannon266b4622020-11-17 19:30:14 +00007245 else {
7246 basicblock* next = bb->b_next;
7247 while (next && next->b_iused == 0) {
7248 next = next->b_next;
7249 }
7250 /* or if last instruction in BB and next BB has same line number */
7251 if (next) {
7252 if (lineno == next->b_instr[0].i_lineno) {
7253 continue;
7254 }
7255 }
7256 }
7257
Mark Shannon6e8128f2020-07-30 10:03:00 +01007258 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007259 if (dest != src) {
7260 bb->b_instr[dest] = bb->b_instr[src];
7261 }
7262 dest++;
7263 prev_lineno = lineno;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007264 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007265 assert(dest <= bb->b_iused);
7266 bb->b_iused = dest;
7267}
7268
Mark Shannon266b4622020-11-17 19:30:14 +00007269static int
7270normalize_basic_block(basicblock *bb) {
7271 /* Mark blocks as exit and/or nofallthrough.
7272 Raise SystemError if CFG is malformed. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007273 for (int i = 0; i < bb->b_iused; i++) {
7274 switch(bb->b_instr[i].i_opcode) {
7275 case RETURN_VALUE:
7276 case RAISE_VARARGS:
7277 case RERAISE:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007278 bb->b_exit = 1;
Mark Shannon5977a792020-12-02 13:31:40 +00007279 bb->b_nofallthrough = 1;
7280 break;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007281 case JUMP_ABSOLUTE:
7282 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007283 bb->b_nofallthrough = 1;
Mark Shannon266b4622020-11-17 19:30:14 +00007284 /* fall through */
7285 case POP_JUMP_IF_FALSE:
7286 case POP_JUMP_IF_TRUE:
7287 case JUMP_IF_FALSE_OR_POP:
7288 case JUMP_IF_TRUE_OR_POP:
Mark Shannon5977a792020-12-02 13:31:40 +00007289 case FOR_ITER:
Mark Shannon266b4622020-11-17 19:30:14 +00007290 if (i != bb->b_iused-1) {
7291 PyErr_SetString(PyExc_SystemError, "malformed control flow graph.");
7292 return -1;
7293 }
Mark Shannon5977a792020-12-02 13:31:40 +00007294 /* Skip over empty basic blocks. */
7295 while (bb->b_instr[i].i_target->b_iused == 0) {
7296 bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next;
7297 }
7298
Mark Shannoncc75ab72020-11-12 19:49:33 +00007299 }
7300 }
Mark Shannon266b4622020-11-17 19:30:14 +00007301 return 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007302}
7303
Mark Shannon6e8128f2020-07-30 10:03:00 +01007304static int
7305mark_reachable(struct assembler *a) {
7306 basicblock **stack, **sp;
7307 sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks);
7308 if (stack == NULL) {
7309 return -1;
7310 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007311 a->a_entry->b_predecessors = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007312 *sp++ = a->a_entry;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007313 while (sp > stack) {
7314 basicblock *b = *(--sp);
Mark Shannon3bd60352021-01-13 12:05:43 +00007315 if (b->b_next && !b->b_nofallthrough) {
7316 if (b->b_next->b_predecessors == 0) {
7317 *sp++ = b->b_next;
7318 }
7319 b->b_next->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007320 }
7321 for (int i = 0; i < b->b_iused; i++) {
7322 basicblock *target;
Mark Shannon582aaf12020-08-04 17:30:11 +01007323 if (is_jump(&b->b_instr[i])) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007324 target = b->b_instr[i].i_target;
Mark Shannon3bd60352021-01-13 12:05:43 +00007325 if (target->b_predecessors == 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007326 *sp++ = target;
7327 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007328 target->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007329 }
7330 }
7331 }
7332 PyObject_Free(stack);
7333 return 0;
7334}
7335
Mark Shannon3bd60352021-01-13 12:05:43 +00007336static void
7337eliminate_empty_basic_blocks(basicblock *entry) {
7338 /* Eliminate empty blocks */
7339 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7340 basicblock *next = b->b_next;
7341 if (next) {
7342 while (next->b_iused == 0 && next->b_next) {
7343 next = next->b_next;
7344 }
7345 b->b_next = next;
7346 }
7347 }
7348 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7349 if (b->b_iused == 0) {
7350 continue;
7351 }
7352 if (is_jump(&b->b_instr[b->b_iused-1])) {
7353 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7354 while (target->b_iused == 0) {
7355 target = target->b_next;
7356 }
7357 b->b_instr[b->b_iused-1].i_target = target;
7358 }
7359 }
7360}
7361
7362
Mark Shannon5977a792020-12-02 13:31:40 +00007363/* If an instruction has no line number, but it's predecessor in the BB does,
Mark Shannon3bd60352021-01-13 12:05:43 +00007364 * then copy the line number. If a successor block has no line number, and only
7365 * one predecessor, then inherit the line number.
7366 * This ensures that all exit blocks (with one predecessor) receive a line number.
7367 * Also reduces the size of the line number table,
Mark Shannon5977a792020-12-02 13:31:40 +00007368 * but has no impact on the generated line number events.
7369 */
7370static void
Mark Shannon3bd60352021-01-13 12:05:43 +00007371propogate_line_numbers(struct assembler *a) {
Mark Shannon5977a792020-12-02 13:31:40 +00007372 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007373 if (b->b_iused == 0) {
7374 continue;
7375 }
Mark Shannon5977a792020-12-02 13:31:40 +00007376 int prev_lineno = -1;
7377 for (int i = 0; i < b->b_iused; i++) {
7378 if (b->b_instr[i].i_lineno < 0) {
7379 b->b_instr[i].i_lineno = prev_lineno;
7380 }
7381 else {
7382 prev_lineno = b->b_instr[i].i_lineno;
7383 }
7384 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007385 if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) {
7386 assert(b->b_next->b_iused);
7387 if (b->b_next->b_instr[0].i_lineno < 0) {
7388 b->b_next->b_instr[0].i_lineno = prev_lineno;
7389 }
7390 }
7391 if (is_jump(&b->b_instr[b->b_iused-1])) {
7392 switch (b->b_instr[b->b_iused-1].i_opcode) {
7393 /* Note: Only actual jumps, not exception handlers */
7394 case SETUP_ASYNC_WITH:
7395 case SETUP_WITH:
7396 case SETUP_FINALLY:
7397 continue;
7398 }
7399 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7400 if (target->b_predecessors == 1) {
7401 if (target->b_instr[0].i_lineno < 0) {
7402 target->b_instr[0].i_lineno = prev_lineno;
7403 }
7404 }
7405 }
Mark Shannon5977a792020-12-02 13:31:40 +00007406 }
7407}
7408
7409/* Perform optimizations on a control flow graph.
Mark Shannon6e8128f2020-07-30 10:03:00 +01007410 The consts object should still be in list form to allow new constants
7411 to be appended.
7412
7413 All transformations keep the code size the same or smaller.
7414 For those that reduce size, the gaps are initially filled with
7415 NOPs. Later those NOPs are removed.
7416*/
7417
7418static int
Inada Naoki8a232c72021-04-16 14:01:04 +09007419optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts)
Mark Shannon6e8128f2020-07-30 10:03:00 +01007420{
Mark Shannoncc75ab72020-11-12 19:49:33 +00007421 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Inada Naoki8a232c72021-04-16 14:01:04 +09007422 if (optimize_basic_block(c, b, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007423 return -1;
7424 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007425 clean_basic_block(b, -1);
Mark Shannon3bd60352021-01-13 12:05:43 +00007426 assert(b->b_predecessors == 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007427 }
7428 if (mark_reachable(a)) {
7429 return -1;
7430 }
7431 /* Delete unreachable instructions */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007432 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007433 if (b->b_predecessors == 0) {
Mark Shannoncc75ab72020-11-12 19:49:33 +00007434 b->b_iused = 0;
Om Gc71581c2020-12-16 17:48:05 +05307435 b->b_nofallthrough = 0;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007436 }
7437 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007438 basicblock *pred = NULL;
7439 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7440 int prev_lineno = -1;
7441 if (pred && pred->b_iused) {
7442 prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno;
7443 }
7444 clean_basic_block(b, prev_lineno);
7445 pred = b->b_nofallthrough ? NULL : b;
7446 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007447 eliminate_empty_basic_blocks(a->a_entry);
Om Gc71581c2020-12-16 17:48:05 +05307448 /* Delete jump instructions made redundant by previous step. If a non-empty
7449 block ends with a jump instruction, check if the next non-empty block
7450 reached through normal flow control is the target of that jump. If it
7451 is, then the jump instruction is redundant and can be deleted.
7452 */
Mark Shannon3bd60352021-01-13 12:05:43 +00007453 int maybe_empty_blocks = 0;
Om Gc71581c2020-12-16 17:48:05 +05307454 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7455 if (b->b_iused > 0) {
7456 struct instr *b_last_instr = &b->b_instr[b->b_iused - 1];
Mark Shannon802b6452021-02-02 14:59:15 +00007457 if (b_last_instr->i_opcode == JUMP_ABSOLUTE ||
Om Gc71581c2020-12-16 17:48:05 +05307458 b_last_instr->i_opcode == JUMP_FORWARD) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007459 if (b_last_instr->i_target == b->b_next) {
7460 assert(b->b_next->b_iused);
Om Gc71581c2020-12-16 17:48:05 +05307461 b->b_nofallthrough = 0;
Mark Shannon802b6452021-02-02 14:59:15 +00007462 b_last_instr->i_opcode = NOP;
7463 clean_basic_block(b, -1);
7464 maybe_empty_blocks = 1;
Om Gc71581c2020-12-16 17:48:05 +05307465 }
7466 }
7467 }
7468 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007469 if (maybe_empty_blocks) {
7470 eliminate_empty_basic_blocks(a->a_entry);
7471 }
7472 propogate_line_numbers(a);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007473 return 0;
7474}
7475
Mark Shannon5977a792020-12-02 13:31:40 +00007476static inline int
7477is_exit_without_lineno(basicblock *b) {
7478 return b->b_exit && b->b_instr[0].i_lineno < 0;
7479}
7480
7481/* PEP 626 mandates that the f_lineno of a frame is correct
7482 * after a frame terminates. It would be prohibitively expensive
7483 * to continuously update the f_lineno field at runtime,
7484 * so we make sure that all exiting instruction (raises and returns)
7485 * have a valid line number, allowing us to compute f_lineno lazily.
7486 * We can do this by duplicating the exit blocks without line number
7487 * so that none have more than one predecessor. We can then safely
7488 * copy the line number from the sole predecessor block.
7489 */
7490static int
7491ensure_exits_have_lineno(struct compiler *c)
7492{
Mark Shannoneaccc122020-12-04 15:22:12 +00007493 basicblock *entry = NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00007494 /* Copy all exit blocks without line number that are targets of a jump.
7495 */
7496 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7497 if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) {
7498 switch (b->b_instr[b->b_iused-1].i_opcode) {
7499 /* Note: Only actual jumps, not exception handlers */
7500 case SETUP_ASYNC_WITH:
7501 case SETUP_WITH:
7502 case SETUP_FINALLY:
7503 continue;
7504 }
7505 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7506 if (is_exit_without_lineno(target)) {
7507 basicblock *new_target = compiler_copy_block(c, target);
7508 if (new_target == NULL) {
7509 return -1;
7510 }
7511 new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7512 b->b_instr[b->b_iused-1].i_target = new_target;
7513 }
7514 }
Mark Shannoneaccc122020-12-04 15:22:12 +00007515 entry = b;
7516 }
7517 assert(entry != NULL);
7518 if (is_exit_without_lineno(entry)) {
7519 entry->b_instr[0].i_lineno = c->u->u_firstlineno;
Mark Shannon5977a792020-12-02 13:31:40 +00007520 }
Mark Shannonee9f98d2021-01-05 12:04:10 +00007521 /* Eliminate empty blocks */
7522 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7523 while (b->b_next && b->b_next->b_iused == 0) {
7524 b->b_next = b->b_next->b_next;
7525 }
7526 }
Mark Shannon5977a792020-12-02 13:31:40 +00007527 /* Any remaining reachable exit blocks without line number can only be reached by
7528 * fall through, and thus can only have a single predecessor */
7529 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7530 if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) {
7531 if (is_exit_without_lineno(b->b_next)) {
7532 assert(b->b_next->b_iused > 0);
7533 b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7534 }
7535 }
7536 }
7537 return 0;
7538}
7539
7540
Mark Shannon6e8128f2020-07-30 10:03:00 +01007541/* Retained for API compatibility.
7542 * Optimization is now done in optimize_cfg */
7543
7544PyObject *
7545PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
7546 PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
7547{
7548 Py_INCREF(code);
7549 return code;
7550}