blob: 4fc8b38d978289fea9694ba9d69429980387abe2 [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 */
Pablo Galindoa77aac42021-04-23 14:27:05 +0100200 int u_end_lineno; /* the end line of the current stmt */
201 int u_end_col_offset; /* the end offset of the current stmt */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000202};
203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204/* This struct captures the global state of a compilation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000206The u pointer points to the current compilation unit, while units
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207for enclosing blocks are stored in c_stack. The u and c_stack are
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000208managed by compiler_enter_scope() and compiler_exit_scope().
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000209
210Note that we don't track recursion levels during compilation - the
211task of detecting and rejecting excessive levels of nesting is
212handled by the symbol analysis pass.
213
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000214*/
215
216struct compiler {
Victor Stinner14e461d2013-08-26 22:28:21 +0200217 PyObject *c_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 struct symtable *c_st;
219 PyFutureFeatures *c_future; /* pointer to module's __future__ */
220 PyCompilerFlags *c_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000221
Georg Brandl8334fd92010-12-04 10:26:46 +0000222 int c_optimize; /* optimization level */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 int c_interactive; /* true if in interactive mode */
224 int c_nestlevel;
INADA Naokic2e16072018-11-26 21:23:22 +0900225 PyObject *c_const_cache; /* Python dict holding all constants,
226 including names tuple */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 struct compiler_unit *u; /* compiler state for current block */
228 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
229 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000230};
231
Brandt Bucher145bf262021-02-26 14:51:55 -0800232typedef struct {
Brandt Bucher0ad1e032021-05-02 13:02:10 -0700233 // A list of strings corresponding to name captures. It is used to track:
234 // - Repeated name assignments in the same pattern.
235 // - Different name assignments in alternatives.
236 // - The order of name assignments in alternatives.
Brandt Bucher145bf262021-02-26 14:51:55 -0800237 PyObject *stores;
Brandt Bucher0ad1e032021-05-02 13:02:10 -0700238 // If 0, any name captures against our subject will raise.
Brandt Bucher145bf262021-02-26 14:51:55 -0800239 int allow_irrefutable;
Brandt Bucher0ad1e032021-05-02 13:02:10 -0700240 // An array of blocks to jump to on failure. Jumping to fail_pop[i] will pop
241 // i items off of the stack. The end result looks like this (with each block
242 // falling through to the next):
243 // fail_pop[4]: POP_TOP
244 // fail_pop[3]: POP_TOP
245 // fail_pop[2]: POP_TOP
246 // fail_pop[1]: POP_TOP
247 // fail_pop[0]: NOP
248 basicblock **fail_pop;
249 // The current length of fail_pop.
250 Py_ssize_t fail_pop_size;
251 // The number of items on top of the stack that need to *stay* on top of the
252 // stack. Variable captures go beneath these. All of them will be popped on
253 // failure.
254 Py_ssize_t on_top;
Brandt Bucher145bf262021-02-26 14:51:55 -0800255} pattern_context;
256
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100257static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000258static void compiler_free(struct compiler *);
259static basicblock *compiler_new_block(struct compiler *);
Andy Lester76d58772020-03-10 21:18:12 -0500260static int compiler_next_instr(basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000261static int compiler_addop(struct compiler *, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +0100262static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
Mark Shannon582aaf12020-08-04 17:30:11 +0100263static int compiler_addop_j(struct compiler *, int, basicblock *);
Mark Shannon127dde52021-01-04 18:06:55 +0000264static int compiler_addop_j_noline(struct compiler *, int, basicblock *);
Brandt Bucher145bf262021-02-26 14:51:55 -0800265static int compiler_error(struct compiler *, const char *, ...);
Serhiy Storchaka62e44812019-02-16 08:12:19 +0200266static int compiler_warn(struct compiler *, const char *, ...);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000267static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
268
269static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
270static int compiler_visit_stmt(struct compiler *, stmt_ty);
271static int compiler_visit_keyword(struct compiler *, keyword_ty);
272static int compiler_visit_expr(struct compiler *, expr_ty);
273static int compiler_augassign(struct compiler *, stmt_ty);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700274static int compiler_annassign(struct compiler *, stmt_ty);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +0200275static int compiler_subscript(struct compiler *, expr_ty);
276static int compiler_slice(struct compiler *, expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000277
Andy Lester76d58772020-03-10 21:18:12 -0500278static int inplace_binop(operator_ty);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100279static int are_all_items_const(asdl_expr_seq *, Py_ssize_t, Py_ssize_t);
Mark Shannon8473cf82020-12-15 11:07:50 +0000280
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000281
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500282static int compiler_with(struct compiler *, stmt_ty, int);
Yury Selivanov75445082015-05-11 22:57:16 -0400283static int compiler_async_with(struct compiler *, stmt_ty, int);
284static int compiler_async_for(struct compiler *, stmt_ty);
Victor Stinner976bb402016-03-23 11:36:19 +0100285static int compiler_call_helper(struct compiler *c, int n,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100286 asdl_expr_seq *args,
287 asdl_keyword_seq *keywords);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500288static int compiler_try_except(struct compiler *, stmt_ty);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400289static int compiler_set_qualname(struct compiler *);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000290
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700291static int compiler_sync_comprehension_generator(
292 struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100293 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +0200294 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700295 expr_ty elt, expr_ty val, int type);
296
297static int compiler_async_comprehension_generator(
298 struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100299 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +0200300 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700301 expr_ty elt, expr_ty val, int type);
302
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000303static int compiler_pattern(struct compiler *, pattern_ty, pattern_context *);
Brandt Bucher145bf262021-02-26 14:51:55 -0800304static int compiler_match(struct compiler *, stmt_ty);
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000305static int compiler_pattern_subpattern(struct compiler *, pattern_ty,
Brandt Bucher145bf262021-02-26 14:51:55 -0800306 pattern_context *);
307
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000308static PyCodeObject *assemble(struct compiler *, int addNone);
Mark Shannon332cd5e2018-01-30 00:41:04 +0000309static PyObject *__doc__, *__annotations__;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000310
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400311#define CAPSULE_NAME "compile.c compiler unit"
Benjamin Petersonb173f782009-05-05 22:31:58 +0000312
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000313PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000314_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 /* Name mangling: __private becomes _classname__private.
317 This is independent from how the name is used. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200318 PyObject *result;
319 size_t nlen, plen, ipriv;
320 Py_UCS4 maxchar;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200322 PyUnicode_READ_CHAR(ident, 0) != '_' ||
323 PyUnicode_READ_CHAR(ident, 1) != '_') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 Py_INCREF(ident);
325 return ident;
326 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200327 nlen = PyUnicode_GET_LENGTH(ident);
328 plen = PyUnicode_GET_LENGTH(privateobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 /* Don't mangle __id__ or names with dots.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 The only time a name with a dot can occur is when
332 we are compiling an import statement that has a
333 package name.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 TODO(jhylton): Decide whether we want to support
336 mangling of the module name, e.g. __M.X.
337 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200338 if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
339 PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
340 PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 Py_INCREF(ident);
342 return ident; /* Don't mangle __whatever__ */
343 }
344 /* Strip leading underscores from class name */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200345 ipriv = 0;
346 while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
347 ipriv++;
348 if (ipriv == plen) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 Py_INCREF(ident);
350 return ident; /* Don't mangle if class is just underscores */
351 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200352 plen -= ipriv;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000353
Antoine Pitrou55bff892013-04-06 21:21:04 +0200354 if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
355 PyErr_SetString(PyExc_OverflowError,
356 "private identifier too large to be mangled");
357 return NULL;
358 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000359
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200360 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
361 if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
362 maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
363
364 result = PyUnicode_New(1 + nlen + plen, maxchar);
365 if (!result)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200367 /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
368 PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
Victor Stinner6c7a52a2011-09-28 21:39:17 +0200369 if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
370 Py_DECREF(result);
371 return NULL;
372 }
373 if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
374 Py_DECREF(result);
375 return NULL;
376 }
Victor Stinner8f825062012-04-27 13:55:39 +0200377 assert(_PyUnicode_CheckConsistency(result, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200378 return result;
Michael W. Hudson60934622004-08-12 17:56:29 +0000379}
380
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381static int
382compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000383{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000385
INADA Naokic2e16072018-11-26 21:23:22 +0900386 c->c_const_cache = PyDict_New();
387 if (!c->c_const_cache) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 return 0;
INADA Naokic2e16072018-11-26 21:23:22 +0900389 }
390
391 c->c_stack = PyList_New(0);
392 if (!c->c_stack) {
393 Py_CLEAR(c->c_const_cache);
394 return 0;
395 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000398}
399
400PyCodeObject *
Victor Stinnera81fca62021-03-24 00:51:50 +0100401_PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
402 int optimize, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 struct compiler c;
405 PyCodeObject *co = NULL;
Victor Stinner37d66d72019-06-13 02:16:41 +0200406 PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 if (!__doc__) {
410 __doc__ = PyUnicode_InternFromString("__doc__");
411 if (!__doc__)
412 return NULL;
413 }
Mark Shannon332cd5e2018-01-30 00:41:04 +0000414 if (!__annotations__) {
415 __annotations__ = PyUnicode_InternFromString("__annotations__");
416 if (!__annotations__)
417 return NULL;
418 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 if (!compiler_init(&c))
420 return NULL;
Victor Stinner14e461d2013-08-26 22:28:21 +0200421 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 c.c_filename = filename;
423 c.c_arena = arena;
Victor Stinnera81fca62021-03-24 00:51:50 +0100424 c.c_future = _PyFuture_FromAST(mod, filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 if (c.c_future == NULL)
426 goto finally;
427 if (!flags) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 flags = &local_flags;
429 }
430 merged = c.c_future->ff_features | flags->cf_flags;
431 c.c_future->ff_features = merged;
432 flags->cf_flags = merged;
433 c.c_flags = flags;
Victor Stinnerda7933e2020-04-13 03:04:28 +0200434 c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000436
Pablo Galindod112c602020-03-18 23:02:09 +0000437 _PyASTOptimizeState state;
438 state.optimize = c.c_optimize;
439 state.ff_features = merged;
440
441 if (!_PyAST_Optimize(mod, arena, &state)) {
INADA Naoki7ea143a2017-12-14 16:47:20 +0900442 goto finally;
443 }
444
Victor Stinner28ad12f2021-03-19 12:41:49 +0100445 c.c_st = _PySymtable_Build(mod, filename, c.c_future);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 if (c.c_st == NULL) {
447 if (!PyErr_Occurred())
448 PyErr_SetString(PyExc_SystemError, "no symtable");
449 goto finally;
450 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000453
Thomas Wouters1175c432006-02-27 22:49:54 +0000454 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 compiler_free(&c);
456 assert(co || PyErr_Occurred());
457 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000458}
459
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000460static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000461compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 if (c->c_st)
Victor Stinner28ad12f2021-03-19 12:41:49 +0100464 _PySymtable_Free(c->c_st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 if (c->c_future)
466 PyObject_Free(c->c_future);
Victor Stinner14e461d2013-08-26 22:28:21 +0200467 Py_XDECREF(c->c_filename);
INADA Naokic2e16072018-11-26 21:23:22 +0900468 Py_DECREF(c->c_const_cache);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000470}
471
Guido van Rossum79f25d91997-04-29 20:08:16 +0000472static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000473list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 Py_ssize_t i, n;
476 PyObject *v, *k;
477 PyObject *dict = PyDict_New();
478 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 n = PyList_Size(list);
481 for (i = 0; i < n; i++) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100482 v = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 if (!v) {
484 Py_DECREF(dict);
485 return NULL;
486 }
487 k = PyList_GET_ITEM(list, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300488 if (PyDict_SetItem(dict, k, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 Py_DECREF(v);
490 Py_DECREF(dict);
491 return NULL;
492 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 Py_DECREF(v);
494 }
495 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000496}
497
498/* Return new dict containing names from src that match scope(s).
499
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000500src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000502values are integers, starting at offset and increasing by one for
503each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000504*/
505
506static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +0100507dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000508{
Benjamin Peterson51ab2832012-07-18 15:12:47 -0700509 Py_ssize_t i = offset, scope, num_keys, key_i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 PyObject *k, *v, *dest = PyDict_New();
Meador Inge2ca63152012-07-18 14:20:11 -0500511 PyObject *sorted_keys;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 assert(offset >= 0);
514 if (dest == NULL)
515 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000516
Meador Inge2ca63152012-07-18 14:20:11 -0500517 /* Sort the keys so that we have a deterministic order on the indexes
518 saved in the returned dictionary. These indexes are used as indexes
519 into the free and cell var storage. Therefore if they aren't
520 deterministic, then the generated bytecode is not deterministic.
521 */
522 sorted_keys = PyDict_Keys(src);
523 if (sorted_keys == NULL)
524 return NULL;
525 if (PyList_Sort(sorted_keys) != 0) {
526 Py_DECREF(sorted_keys);
527 return NULL;
528 }
Meador Ingef69e24e2012-07-18 16:41:03 -0500529 num_keys = PyList_GET_SIZE(sorted_keys);
Meador Inge2ca63152012-07-18 14:20:11 -0500530
531 for (key_i = 0; key_i < num_keys; key_i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 /* XXX this should probably be a macro in symtable.h */
533 long vi;
Meador Inge2ca63152012-07-18 14:20:11 -0500534 k = PyList_GET_ITEM(sorted_keys, key_i);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200535 v = PyDict_GetItemWithError(src, k);
536 assert(v && PyLong_Check(v));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 vi = PyLong_AS_LONG(v);
538 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 if (scope == scope_type || vi & flag) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300541 PyObject *item = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 if (item == NULL) {
Meador Inge2ca63152012-07-18 14:20:11 -0500543 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 Py_DECREF(dest);
545 return NULL;
546 }
547 i++;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300548 if (PyDict_SetItem(dest, k, item) < 0) {
Meador Inge2ca63152012-07-18 14:20:11 -0500549 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 Py_DECREF(item);
551 Py_DECREF(dest);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 return NULL;
553 }
554 Py_DECREF(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 }
556 }
Meador Inge2ca63152012-07-18 14:20:11 -0500557 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000559}
560
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561static void
562compiler_unit_check(struct compiler_unit *u)
563{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 basicblock *block;
565 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +0100566 assert(!_PyMem_IsPtrFreed(block));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 if (block->b_instr != NULL) {
568 assert(block->b_ialloc > 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +0100569 assert(block->b_iused >= 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 assert(block->b_ialloc >= block->b_iused);
571 }
572 else {
573 assert (block->b_iused == 0);
574 assert (block->b_ialloc == 0);
575 }
576 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000577}
578
579static void
580compiler_unit_free(struct compiler_unit *u)
581{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 compiler_unit_check(u);
585 b = u->u_blocks;
586 while (b != NULL) {
587 if (b->b_instr)
588 PyObject_Free((void *)b->b_instr);
589 next = b->b_list;
590 PyObject_Free((void *)b);
591 b = next;
592 }
593 Py_CLEAR(u->u_ste);
594 Py_CLEAR(u->u_name);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400595 Py_CLEAR(u->u_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 Py_CLEAR(u->u_consts);
597 Py_CLEAR(u->u_names);
598 Py_CLEAR(u->u_varnames);
599 Py_CLEAR(u->u_freevars);
600 Py_CLEAR(u->u_cellvars);
601 Py_CLEAR(u->u_private);
602 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000603}
604
605static int
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100606compiler_enter_scope(struct compiler *c, identifier name,
607 int scope_type, void *key, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000608{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 struct compiler_unit *u;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100610 basicblock *block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000611
Andy Lester7668a8b2020-03-24 23:26:44 -0500612 u = (struct compiler_unit *)PyObject_Calloc(1, sizeof(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 struct compiler_unit));
614 if (!u) {
615 PyErr_NoMemory();
616 return 0;
617 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100618 u->u_scope_type = scope_type;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 u->u_argcount = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100620 u->u_posonlyargcount = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 u->u_kwonlyargcount = 0;
622 u->u_ste = PySymtable_Lookup(c->c_st, key);
623 if (!u->u_ste) {
624 compiler_unit_free(u);
625 return 0;
626 }
627 Py_INCREF(name);
628 u->u_name = name;
629 u->u_varnames = list2dict(u->u_ste->ste_varnames);
630 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
631 if (!u->u_varnames || !u->u_cellvars) {
632 compiler_unit_free(u);
633 return 0;
634 }
Benjamin Peterson312595c2013-05-15 15:26:42 -0500635 if (u->u_ste->ste_needs_class_closure) {
Martin Panter7462b6492015-11-02 03:37:02 +0000636 /* Cook up an implicit __class__ cell. */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500637 _Py_IDENTIFIER(__class__);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300638 PyObject *name;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500639 int res;
640 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200641 assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500642 name = _PyUnicode_FromId(&PyId___class__);
643 if (!name) {
644 compiler_unit_free(u);
645 return 0;
646 }
Victor Stinnerc9bc2902020-10-27 02:24:34 +0100647 res = PyDict_SetItem(u->u_cellvars, name, _PyLong_GetZero());
Benjamin Peterson312595c2013-05-15 15:26:42 -0500648 if (res < 0) {
649 compiler_unit_free(u);
650 return 0;
651 }
652 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200655 PyDict_GET_SIZE(u->u_cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 if (!u->u_freevars) {
657 compiler_unit_free(u);
658 return 0;
659 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 u->u_blocks = NULL;
662 u->u_nfblocks = 0;
663 u->u_firstlineno = lineno;
664 u->u_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000665 u->u_col_offset = 0;
Pablo Galindoa77aac42021-04-23 14:27:05 +0100666 u->u_end_lineno = 0;
667 u->u_end_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 u->u_consts = PyDict_New();
669 if (!u->u_consts) {
670 compiler_unit_free(u);
671 return 0;
672 }
673 u->u_names = PyDict_New();
674 if (!u->u_names) {
675 compiler_unit_free(u);
676 return 0;
677 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 /* Push the old compiler_unit on the stack. */
682 if (c->u) {
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400683 PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
685 Py_XDECREF(capsule);
686 compiler_unit_free(u);
687 return 0;
688 }
689 Py_DECREF(capsule);
690 u->u_private = c->u->u_private;
691 Py_XINCREF(u->u_private);
692 }
693 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 c->c_nestlevel++;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100696
697 block = compiler_new_block(c);
698 if (block == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 return 0;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100700 c->u->u_curblock = block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000701
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400702 if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
703 if (!compiler_set_qualname(c))
704 return 0;
705 }
706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000708}
709
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000710static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000711compiler_exit_scope(struct compiler *c)
712{
Victor Stinnera6192632021-01-29 16:53:03 +0100713 // Don't call PySequence_DelItem() with an exception raised
714 PyObject *exc_type, *exc_val, *exc_tb;
715 PyErr_Fetch(&exc_type, &exc_val, &exc_tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 c->c_nestlevel--;
718 compiler_unit_free(c->u);
719 /* Restore c->u to the parent unit. */
Victor Stinnera6192632021-01-29 16:53:03 +0100720 Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 if (n >= 0) {
Victor Stinnera6192632021-01-29 16:53:03 +0100722 PyObject *capsule = PyList_GET_ITEM(c->c_stack, n);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400723 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 assert(c->u);
725 /* we are deleting from a list so this really shouldn't fail */
Victor Stinnera6192632021-01-29 16:53:03 +0100726 if (PySequence_DelItem(c->c_stack, n) < 0) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +0100727 _PyErr_WriteUnraisableMsg("on removing the last compiler "
728 "stack item", NULL);
Victor Stinnera6192632021-01-29 16:53:03 +0100729 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 compiler_unit_check(c->u);
731 }
Victor Stinnera6192632021-01-29 16:53:03 +0100732 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 c->u = NULL;
Victor Stinnera6192632021-01-29 16:53:03 +0100734 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000735
Victor Stinnera6192632021-01-29 16:53:03 +0100736 PyErr_Restore(exc_type, exc_val, exc_tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737}
738
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400739static int
740compiler_set_qualname(struct compiler *c)
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100741{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100742 _Py_static_string(dot, ".");
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400743 _Py_static_string(dot_locals, ".<locals>");
744 Py_ssize_t stack_size;
745 struct compiler_unit *u = c->u;
746 PyObject *name, *base, *dot_str, *dot_locals_str;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100747
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400748 base = NULL;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100749 stack_size = PyList_GET_SIZE(c->c_stack);
Benjamin Petersona8a38b82013-10-19 16:14:39 -0400750 assert(stack_size >= 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400751 if (stack_size > 1) {
752 int scope, force_global = 0;
753 struct compiler_unit *parent;
754 PyObject *mangled, *capsule;
755
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400756 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400757 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400758 assert(parent);
759
Yury Selivanov75445082015-05-11 22:57:16 -0400760 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
761 || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
762 || u->u_scope_type == COMPILER_SCOPE_CLASS) {
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400763 assert(u->u_name);
764 mangled = _Py_Mangle(parent->u_private, u->u_name);
765 if (!mangled)
766 return 0;
Victor Stinner28ad12f2021-03-19 12:41:49 +0100767 scope = _PyST_GetScope(parent->u_ste, mangled);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400768 Py_DECREF(mangled);
769 assert(scope != GLOBAL_IMPLICIT);
770 if (scope == GLOBAL_EXPLICIT)
771 force_global = 1;
772 }
773
774 if (!force_global) {
775 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
Yury Selivanov75445082015-05-11 22:57:16 -0400776 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400777 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
778 dot_locals_str = _PyUnicode_FromId(&dot_locals);
779 if (dot_locals_str == NULL)
780 return 0;
781 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
782 if (base == NULL)
783 return 0;
784 }
785 else {
786 Py_INCREF(parent->u_qualname);
787 base = parent->u_qualname;
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400788 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100789 }
790 }
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400791
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400792 if (base != NULL) {
793 dot_str = _PyUnicode_FromId(&dot);
794 if (dot_str == NULL) {
795 Py_DECREF(base);
796 return 0;
797 }
798 name = PyUnicode_Concat(base, dot_str);
799 Py_DECREF(base);
800 if (name == NULL)
801 return 0;
802 PyUnicode_Append(&name, u->u_name);
803 if (name == NULL)
804 return 0;
805 }
806 else {
807 Py_INCREF(u->u_name);
808 name = u->u_name;
809 }
810 u->u_qualname = name;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100811
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400812 return 1;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100813}
814
Eric V. Smith235a6f02015-09-19 14:51:32 -0400815
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816/* Allocate a new block and return a pointer to it.
817 Returns NULL on error.
818*/
819
820static basicblock *
821compiler_new_block(struct compiler *c)
822{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 basicblock *b;
824 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 u = c->u;
Andy Lester7668a8b2020-03-24 23:26:44 -0500827 b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 if (b == NULL) {
829 PyErr_NoMemory();
830 return NULL;
831 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 /* Extend the singly linked list of blocks with new block. */
833 b->b_list = u->u_blocks;
834 u->u_blocks = b;
835 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836}
837
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838static basicblock *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839compiler_next_block(struct compiler *c)
840{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 basicblock *block = compiler_new_block(c);
842 if (block == NULL)
843 return NULL;
844 c->u->u_curblock->b_next = block;
845 c->u->u_curblock = block;
846 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000847}
848
849static basicblock *
850compiler_use_next_block(struct compiler *c, basicblock *block)
851{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 assert(block != NULL);
853 c->u->u_curblock->b_next = block;
854 c->u->u_curblock = block;
855 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856}
857
Mark Shannon5977a792020-12-02 13:31:40 +0000858static basicblock *
859compiler_copy_block(struct compiler *c, basicblock *block)
860{
861 /* Cannot copy a block if it has a fallthrough, since
862 * a block can only have one fallthrough predecessor.
863 */
864 assert(block->b_nofallthrough);
865 basicblock *result = compiler_next_block(c);
866 if (result == NULL) {
867 return NULL;
868 }
869 for (int i = 0; i < block->b_iused; i++) {
870 int n = compiler_next_instr(result);
871 if (n < 0) {
872 return NULL;
873 }
874 result->b_instr[n] = block->b_instr[i];
875 }
876 result->b_exit = block->b_exit;
Mark Shannon3bd60352021-01-13 12:05:43 +0000877 result->b_nofallthrough = 1;
Mark Shannon5977a792020-12-02 13:31:40 +0000878 return result;
879}
880
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000881/* Returns the offset of the next instruction in the current block's
882 b_instr array. Resizes the b_instr as necessary.
883 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000884*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000885
886static int
Andy Lester76d58772020-03-10 21:18:12 -0500887compiler_next_instr(basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000888{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 assert(b != NULL);
890 if (b->b_instr == NULL) {
Andy Lester7668a8b2020-03-24 23:26:44 -0500891 b->b_instr = (struct instr *)PyObject_Calloc(
892 DEFAULT_BLOCK_SIZE, sizeof(struct instr));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 if (b->b_instr == NULL) {
894 PyErr_NoMemory();
895 return -1;
896 }
897 b->b_ialloc = DEFAULT_BLOCK_SIZE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 }
899 else if (b->b_iused == b->b_ialloc) {
900 struct instr *tmp;
901 size_t oldsize, newsize;
902 oldsize = b->b_ialloc * sizeof(struct instr);
903 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000904
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -0700905 if (oldsize > (SIZE_MAX >> 1)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 PyErr_NoMemory();
907 return -1;
908 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 if (newsize == 0) {
911 PyErr_NoMemory();
912 return -1;
913 }
914 b->b_ialloc <<= 1;
915 tmp = (struct instr *)PyObject_Realloc(
916 (void *)b->b_instr, newsize);
917 if (tmp == NULL) {
918 PyErr_NoMemory();
919 return -1;
920 }
921 b->b_instr = tmp;
922 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
923 }
924 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000925}
926
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200927/* Set the line number and column offset for the following instructions.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000928
Christian Heimes2202f872008-02-06 14:31:34 +0000929 The line number is reset in the following cases:
930 - when entering a new scope
931 - on each statement
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200932 - on each expression and sub-expression
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200933 - before the "except" and "finally" clauses
Thomas Wouters89f507f2006-12-13 04:49:30 +0000934*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000935
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200936#define SET_LOC(c, x) \
937 (c)->u->u_lineno = (x)->lineno; \
Pablo Galindoa77aac42021-04-23 14:27:05 +0100938 (c)->u->u_col_offset = (x)->col_offset; \
939 (c)->u->u_end_lineno = (x)->end_lineno; \
940 (c)->u->u_end_col_offset = (x)->end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000941
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200942/* Return the stack effect of opcode with argument oparg.
943
944 Some opcodes have different stack effect when jump to the target and
945 when not jump. The 'jump' parameter specifies the case:
946
947 * 0 -- when not jump
948 * 1 -- when jump
949 * -1 -- maximal
950 */
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200951static int
952stack_effect(int opcode, int oparg, int jump)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000953{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 switch (opcode) {
Serhiy Storchaka57faf342018-04-25 22:04:06 +0300955 case NOP:
956 case EXTENDED_ARG:
957 return 0;
958
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200959 /* Stack manipulation */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 case POP_TOP:
961 return -1;
962 case ROT_TWO:
963 case ROT_THREE:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200964 case ROT_FOUR:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 return 0;
966 case DUP_TOP:
967 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000968 case DUP_TOP_TWO:
969 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000970
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200971 /* Unary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 case UNARY_POSITIVE:
973 case UNARY_NEGATIVE:
974 case UNARY_NOT:
975 case UNARY_INVERT:
976 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 case SET_ADD:
979 case LIST_APPEND:
980 return -1;
981 case MAP_ADD:
982 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000983
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200984 /* Binary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 case BINARY_POWER:
986 case BINARY_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400987 case BINARY_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 case BINARY_MODULO:
989 case BINARY_ADD:
990 case BINARY_SUBTRACT:
991 case BINARY_SUBSCR:
992 case BINARY_FLOOR_DIVIDE:
993 case BINARY_TRUE_DIVIDE:
994 return -1;
995 case INPLACE_FLOOR_DIVIDE:
996 case INPLACE_TRUE_DIVIDE:
997 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 case INPLACE_ADD:
1000 case INPLACE_SUBTRACT:
1001 case INPLACE_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -04001002 case INPLACE_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 case INPLACE_MODULO:
1004 return -1;
1005 case STORE_SUBSCR:
1006 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 case DELETE_SUBSCR:
1008 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 case BINARY_LSHIFT:
1011 case BINARY_RSHIFT:
1012 case BINARY_AND:
1013 case BINARY_XOR:
1014 case BINARY_OR:
1015 return -1;
1016 case INPLACE_POWER:
1017 return -1;
1018 case GET_ITER:
1019 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 case PRINT_EXPR:
1022 return -1;
1023 case LOAD_BUILD_CLASS:
1024 return 1;
1025 case INPLACE_LSHIFT:
1026 case INPLACE_RSHIFT:
1027 case INPLACE_AND:
1028 case INPLACE_XOR:
1029 case INPLACE_OR:
1030 return -1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 case SETUP_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001033 /* 1 in the normal flow.
1034 * Restore the stack position and push 6 values before jumping to
1035 * the handler if an exception be raised. */
1036 return jump ? 6 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 case RETURN_VALUE:
1038 return -1;
1039 case IMPORT_STAR:
1040 return -1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001041 case SETUP_ANNOTATIONS:
1042 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 case YIELD_VALUE:
1044 return 0;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001045 case YIELD_FROM:
1046 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 case POP_BLOCK:
1048 return 0;
1049 case POP_EXCEPT:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001050 return -3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 case STORE_NAME:
1053 return -1;
1054 case DELETE_NAME:
1055 return 0;
1056 case UNPACK_SEQUENCE:
1057 return oparg-1;
1058 case UNPACK_EX:
1059 return (oparg&0xFF) + (oparg>>8);
1060 case FOR_ITER:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001061 /* -1 at end of iterator, 1 if continue iterating. */
1062 return jump > 0 ? -1 : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 case STORE_ATTR:
1065 return -2;
1066 case DELETE_ATTR:
1067 return -1;
1068 case STORE_GLOBAL:
1069 return -1;
1070 case DELETE_GLOBAL:
1071 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 case LOAD_CONST:
1073 return 1;
1074 case LOAD_NAME:
1075 return 1;
1076 case BUILD_TUPLE:
1077 case BUILD_LIST:
1078 case BUILD_SET:
Serhiy Storchakaea525a22016-09-06 22:07:53 +03001079 case BUILD_STRING:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 return 1-oparg;
1081 case BUILD_MAP:
Benjamin Petersonb6855152015-09-10 21:02:39 -07001082 return 1 - 2*oparg;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001083 case BUILD_CONST_KEY_MAP:
1084 return -oparg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 case LOAD_ATTR:
1086 return 0;
1087 case COMPARE_OP:
Mark Shannon9af0e472020-01-14 10:12:45 +00001088 case IS_OP:
1089 case CONTAINS_OP:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 return -1;
Mark Shannon9af0e472020-01-14 10:12:45 +00001091 case JUMP_IF_NOT_EXC_MATCH:
1092 return -2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 case IMPORT_NAME:
1094 return -1;
1095 case IMPORT_FROM:
1096 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001098 /* Jumps */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 case JUMP_FORWARD:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 case JUMP_ABSOLUTE:
1101 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001102
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001103 case JUMP_IF_TRUE_OR_POP:
1104 case JUMP_IF_FALSE_OR_POP:
1105 return jump ? 0 : -1;
1106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 case POP_JUMP_IF_FALSE:
1108 case POP_JUMP_IF_TRUE:
1109 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 case LOAD_GLOBAL:
1112 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001114 /* Exception handling */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 case SETUP_FINALLY:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001116 /* 0 in the normal flow.
1117 * Restore the stack position and push 6 values before jumping to
1118 * the handler if an exception be raised. */
1119 return jump ? 6 : 0;
Mark Shannonfee55262019-11-21 09:11:43 +00001120 case RERAISE:
1121 return -3;
1122
1123 case WITH_EXCEPT_START:
1124 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 case LOAD_FAST:
1127 return 1;
1128 case STORE_FAST:
1129 return -1;
1130 case DELETE_FAST:
1131 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 case RAISE_VARARGS:
1134 return -oparg;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001135
1136 /* Functions and calls */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 case CALL_FUNCTION:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001138 return -oparg;
Yury Selivanovf2392132016-12-13 19:03:51 -05001139 case CALL_METHOD:
1140 return -oparg-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 case CALL_FUNCTION_KW:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001142 return -oparg-1;
1143 case CALL_FUNCTION_EX:
Matthieu Dartiailh3a9ac822017-02-21 14:25:22 +01001144 return -1 - ((oparg & 0x01) != 0);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001145 case MAKE_FUNCTION:
1146 return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1147 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 case BUILD_SLICE:
1149 if (oparg == 3)
1150 return -2;
1151 else
1152 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001154 /* Closures */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 case LOAD_CLOSURE:
1156 return 1;
1157 case LOAD_DEREF:
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001158 case LOAD_CLASSDEREF:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 return 1;
1160 case STORE_DEREF:
1161 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001162 case DELETE_DEREF:
1163 return 0;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001164
1165 /* Iterators and generators */
Yury Selivanov75445082015-05-11 22:57:16 -04001166 case GET_AWAITABLE:
1167 return 0;
1168 case SETUP_ASYNC_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001169 /* 0 in the normal flow.
1170 * Restore the stack position to the position before the result
1171 * of __aenter__ and push 6 values before jumping to the handler
1172 * if an exception be raised. */
1173 return jump ? -1 + 6 : 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001174 case BEFORE_ASYNC_WITH:
1175 return 1;
1176 case GET_AITER:
1177 return 0;
1178 case GET_ANEXT:
1179 return 1;
Yury Selivanov5376ba92015-06-22 12:19:30 -04001180 case GET_YIELD_FROM_ITER:
1181 return 0;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02001182 case END_ASYNC_FOR:
1183 return -7;
Eric V. Smitha78c7952015-11-03 12:45:05 -05001184 case FORMAT_VALUE:
1185 /* If there's a fmt_spec on the stack, we go from 2->1,
1186 else 1->1. */
1187 return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
Yury Selivanovf2392132016-12-13 19:03:51 -05001188 case LOAD_METHOD:
1189 return 1;
Zackery Spytzce6a0702019-08-25 03:44:09 -06001190 case LOAD_ASSERTION_ERROR:
1191 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001192 case LIST_TO_TUPLE:
1193 return 0;
Mark Shannonb37181e2021-04-06 11:48:59 +01001194 case GEN_START:
1195 return -1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001196 case LIST_EXTEND:
1197 case SET_UPDATE:
Mark Shannon8a4cd702020-01-27 09:57:45 +00001198 case DICT_MERGE:
1199 case DICT_UPDATE:
Mark Shannon13bc1392020-01-23 09:25:17 +00001200 return -1;
Brandt Bucher145bf262021-02-26 14:51:55 -08001201 case COPY_DICT_WITHOUT_KEYS:
1202 return 0;
1203 case MATCH_CLASS:
1204 return -1;
1205 case GET_LEN:
1206 case MATCH_MAPPING:
1207 case MATCH_SEQUENCE:
1208 return 1;
1209 case MATCH_KEYS:
1210 return 2;
Brandt Bucher0ad1e032021-05-02 13:02:10 -07001211 case ROT_N:
1212 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 default:
Larry Hastings3a907972013-11-23 14:49:22 -08001214 return PY_INVALID_STACK_EFFECT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 }
Larry Hastings3a907972013-11-23 14:49:22 -08001216 return PY_INVALID_STACK_EFFECT; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001217}
1218
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001219int
Serhiy Storchaka7bdf2822018-09-18 09:54:26 +03001220PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump)
1221{
1222 return stack_effect(opcode, oparg, jump);
1223}
1224
1225int
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001226PyCompile_OpcodeStackEffect(int opcode, int oparg)
1227{
1228 return stack_effect(opcode, oparg, -1);
1229}
1230
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001231/* Add an opcode with no argument.
1232 Returns 0 on failure, 1 on success.
1233*/
1234
1235static int
Mark Shannon3bd60352021-01-13 12:05:43 +00001236compiler_addop_line(struct compiler *c, int opcode, int line)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001237{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 basicblock *b;
1239 struct instr *i;
1240 int off;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001241 assert(!HAS_ARG(opcode));
Andy Lester76d58772020-03-10 21:18:12 -05001242 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 if (off < 0)
1244 return 0;
1245 b = c->u->u_curblock;
1246 i = &b->b_instr[off];
1247 i->i_opcode = opcode;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001248 i->i_oparg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 if (opcode == RETURN_VALUE)
1250 b->b_return = 1;
Mark Shannon3bd60352021-01-13 12:05:43 +00001251 i->i_lineno = line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253}
1254
Mark Shannon3bd60352021-01-13 12:05:43 +00001255static int
1256compiler_addop(struct compiler *c, int opcode)
1257{
1258 return compiler_addop_line(c, opcode, c->u->u_lineno);
1259}
1260
1261static int
1262compiler_addop_noline(struct compiler *c, int opcode)
1263{
1264 return compiler_addop_line(c, opcode, -1);
1265}
1266
1267
Victor Stinnerf8e32212013-11-19 23:56:34 +01001268static Py_ssize_t
Andy Lester76d58772020-03-10 21:18:12 -05001269compiler_add_o(PyObject *dict, PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001271 PyObject *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001274 v = PyDict_GetItemWithError(dict, o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 if (!v) {
Stefan Krahc0cbed12015-07-27 12:56:49 +02001276 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 return -1;
Stefan Krahc0cbed12015-07-27 12:56:49 +02001278 }
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001279 arg = PyDict_GET_SIZE(dict);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001280 v = PyLong_FromSsize_t(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 if (!v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 return -1;
1283 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001284 if (PyDict_SetItem(dict, o, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 Py_DECREF(v);
1286 return -1;
1287 }
1288 Py_DECREF(v);
1289 }
1290 else
1291 arg = PyLong_AsLong(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001292 return arg;
1293}
1294
INADA Naokic2e16072018-11-26 21:23:22 +09001295// Merge const *o* recursively and return constant key object.
1296static PyObject*
1297merge_consts_recursive(struct compiler *c, PyObject *o)
1298{
1299 // None and Ellipsis are singleton, and key is the singleton.
1300 // No need to merge object and key.
1301 if (o == Py_None || o == Py_Ellipsis) {
1302 Py_INCREF(o);
1303 return o;
1304 }
1305
1306 PyObject *key = _PyCode_ConstantKey(o);
1307 if (key == NULL) {
1308 return NULL;
1309 }
1310
1311 // t is borrowed reference
1312 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
1313 if (t != key) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001314 // o is registered in c_const_cache. Just use it.
Zackery Spytz9b4a1b12019-03-20 03:16:25 -06001315 Py_XINCREF(t);
INADA Naokic2e16072018-11-26 21:23:22 +09001316 Py_DECREF(key);
1317 return t;
1318 }
1319
INADA Naokif7e4d362018-11-29 00:58:46 +09001320 // We registered o in c_const_cache.
Simeon63b5fc52019-04-09 19:36:57 -04001321 // When o is a tuple or frozenset, we want to merge its
INADA Naokif7e4d362018-11-29 00:58:46 +09001322 // items too.
INADA Naokic2e16072018-11-26 21:23:22 +09001323 if (PyTuple_CheckExact(o)) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001324 Py_ssize_t len = PyTuple_GET_SIZE(o);
1325 for (Py_ssize_t i = 0; i < len; i++) {
INADA Naokic2e16072018-11-26 21:23:22 +09001326 PyObject *item = PyTuple_GET_ITEM(o, i);
1327 PyObject *u = merge_consts_recursive(c, item);
1328 if (u == NULL) {
1329 Py_DECREF(key);
1330 return NULL;
1331 }
1332
1333 // See _PyCode_ConstantKey()
1334 PyObject *v; // borrowed
1335 if (PyTuple_CheckExact(u)) {
1336 v = PyTuple_GET_ITEM(u, 1);
1337 }
1338 else {
1339 v = u;
1340 }
1341 if (v != item) {
1342 Py_INCREF(v);
1343 PyTuple_SET_ITEM(o, i, v);
1344 Py_DECREF(item);
1345 }
1346
1347 Py_DECREF(u);
1348 }
1349 }
INADA Naokif7e4d362018-11-29 00:58:46 +09001350 else if (PyFrozenSet_CheckExact(o)) {
Simeon63b5fc52019-04-09 19:36:57 -04001351 // *key* is tuple. And its first item is frozenset of
INADA Naokif7e4d362018-11-29 00:58:46 +09001352 // constant keys.
1353 // See _PyCode_ConstantKey() for detail.
1354 assert(PyTuple_CheckExact(key));
1355 assert(PyTuple_GET_SIZE(key) == 2);
1356
1357 Py_ssize_t len = PySet_GET_SIZE(o);
1358 if (len == 0) { // empty frozenset should not be re-created.
1359 return key;
1360 }
1361 PyObject *tuple = PyTuple_New(len);
1362 if (tuple == NULL) {
1363 Py_DECREF(key);
1364 return NULL;
1365 }
1366 Py_ssize_t i = 0, pos = 0;
1367 PyObject *item;
1368 Py_hash_t hash;
1369 while (_PySet_NextEntry(o, &pos, &item, &hash)) {
1370 PyObject *k = merge_consts_recursive(c, item);
1371 if (k == NULL) {
1372 Py_DECREF(tuple);
1373 Py_DECREF(key);
1374 return NULL;
1375 }
1376 PyObject *u;
1377 if (PyTuple_CheckExact(k)) {
1378 u = PyTuple_GET_ITEM(k, 1);
1379 Py_INCREF(u);
1380 Py_DECREF(k);
1381 }
1382 else {
1383 u = k;
1384 }
1385 PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u.
1386 i++;
1387 }
1388
1389 // Instead of rewriting o, we create new frozenset and embed in the
1390 // key tuple. Caller should get merged frozenset from the key tuple.
1391 PyObject *new = PyFrozenSet_New(tuple);
1392 Py_DECREF(tuple);
1393 if (new == NULL) {
1394 Py_DECREF(key);
1395 return NULL;
1396 }
1397 assert(PyTuple_GET_ITEM(key, 1) == o);
1398 Py_DECREF(o);
1399 PyTuple_SET_ITEM(key, 1, new);
1400 }
INADA Naokic2e16072018-11-26 21:23:22 +09001401
1402 return key;
1403}
1404
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001405static Py_ssize_t
1406compiler_add_const(struct compiler *c, PyObject *o)
1407{
INADA Naokic2e16072018-11-26 21:23:22 +09001408 PyObject *key = merge_consts_recursive(c, o);
1409 if (key == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001410 return -1;
INADA Naokic2e16072018-11-26 21:23:22 +09001411 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001412
Andy Lester76d58772020-03-10 21:18:12 -05001413 Py_ssize_t arg = compiler_add_o(c->u->u_consts, key);
INADA Naokic2e16072018-11-26 21:23:22 +09001414 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416}
1417
1418static int
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001419compiler_addop_load_const(struct compiler *c, PyObject *o)
1420{
1421 Py_ssize_t arg = compiler_add_const(c, o);
1422 if (arg < 0)
1423 return 0;
1424 return compiler_addop_i(c, LOAD_CONST, arg);
1425}
1426
1427static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430{
Andy Lester76d58772020-03-10 21:18:12 -05001431 Py_ssize_t arg = compiler_add_o(dict, o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001433 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001434 return compiler_addop_i(c, opcode, arg);
1435}
1436
1437static int
1438compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001441 Py_ssize_t arg;
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001442
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001443 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1444 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001445 return 0;
Andy Lester76d58772020-03-10 21:18:12 -05001446 arg = compiler_add_o(dict, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447 Py_DECREF(mangled);
1448 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001449 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450 return compiler_addop_i(c, opcode, arg);
1451}
1452
1453/* Add an opcode with an integer argument.
1454 Returns 0 on failure, 1 on success.
1455*/
1456
1457static int
Mark Shannon11e0b292021-04-15 14:28:56 +01001458compiler_addop_i_line(struct compiler *c, int opcode, Py_ssize_t oparg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 struct instr *i;
1461 int off;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001462
Victor Stinner2ad474b2016-03-01 23:34:47 +01001463 /* oparg value is unsigned, but a signed C int is usually used to store
1464 it in the C code (like Python/ceval.c).
1465
1466 Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1467
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001468 The argument of a concrete bytecode instruction is limited to 8-bit.
1469 EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1470 assert(HAS_ARG(opcode));
Victor Stinner2ad474b2016-03-01 23:34:47 +01001471 assert(0 <= oparg && oparg <= 2147483647);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001472
Andy Lester76d58772020-03-10 21:18:12 -05001473 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 if (off < 0)
1475 return 0;
1476 i = &c->u->u_curblock->b_instr[off];
Victor Stinnerf8e32212013-11-19 23:56:34 +01001477 i->i_opcode = opcode;
1478 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
Mark Shannon11e0b292021-04-15 14:28:56 +01001479 i->i_lineno = lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001481}
1482
Mark Shannon11e0b292021-04-15 14:28:56 +01001483static int
1484compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
1485{
1486 return compiler_addop_i_line(c, opcode, oparg, c->u->u_lineno);
1487}
1488
1489static int
1490compiler_addop_i_noline(struct compiler *c, int opcode, Py_ssize_t oparg)
1491{
1492 return compiler_addop_i_line(c, opcode, oparg, -1);
1493}
1494
Mark Shannon28b75c82020-12-23 11:43:10 +00001495static int add_jump_to_block(basicblock *b, int opcode, int lineno, basicblock *target)
1496{
1497 assert(HAS_ARG(opcode));
1498 assert(b != NULL);
1499 assert(target != NULL);
1500
1501 int off = compiler_next_instr(b);
1502 struct instr *i = &b->b_instr[off];
1503 if (off < 0) {
1504 return 0;
1505 }
1506 i->i_opcode = opcode;
1507 i->i_target = target;
1508 i->i_lineno = lineno;
1509 return 1;
1510}
1511
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001512static int
Mark Shannon582aaf12020-08-04 17:30:11 +01001513compiler_addop_j(struct compiler *c, int opcode, basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514{
Mark Shannon28b75c82020-12-23 11:43:10 +00001515 return add_jump_to_block(c->u->u_curblock, opcode, c->u->u_lineno, b);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516}
1517
Mark Shannon127dde52021-01-04 18:06:55 +00001518static int
1519compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b)
1520{
1521 return add_jump_to_block(c->u->u_curblock, opcode, -1, b);
1522}
1523
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +01001524/* NEXT_BLOCK() creates an implicit jump from the current block
1525 to the new block.
1526
1527 The returns inside this macro make it impossible to decref objects
1528 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001530#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 if (compiler_next_block((C)) == NULL) \
1532 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001533}
1534
1535#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 if (!compiler_addop((C), (OP))) \
1537 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538}
1539
Mark Shannon3bd60352021-01-13 12:05:43 +00001540#define ADDOP_NOLINE(C, OP) { \
1541 if (!compiler_addop_noline((C), (OP))) \
1542 return 0; \
1543}
1544
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001545#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 if (!compiler_addop((C), (OP))) { \
1547 compiler_exit_scope(c); \
1548 return 0; \
1549 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001550}
1551
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001552#define ADDOP_LOAD_CONST(C, O) { \
1553 if (!compiler_addop_load_const((C), (O))) \
1554 return 0; \
1555}
1556
1557/* Same as ADDOP_LOAD_CONST, but steals a reference. */
1558#define ADDOP_LOAD_CONST_NEW(C, O) { \
1559 PyObject *__new_const = (O); \
1560 if (__new_const == NULL) { \
1561 return 0; \
1562 } \
1563 if (!compiler_addop_load_const((C), __new_const)) { \
1564 Py_DECREF(__new_const); \
1565 return 0; \
1566 } \
1567 Py_DECREF(__new_const); \
1568}
1569
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001570#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1572 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001573}
1574
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001575/* Same as ADDOP_O, but steals a reference. */
1576#define ADDOP_N(C, OP, O, TYPE) { \
1577 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1578 Py_DECREF((O)); \
1579 return 0; \
1580 } \
1581 Py_DECREF((O)); \
1582}
1583
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001584#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1586 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001587}
1588
1589#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 if (!compiler_addop_i((C), (OP), (O))) \
1591 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001592}
1593
Mark Shannon11e0b292021-04-15 14:28:56 +01001594#define ADDOP_I_NOLINE(C, OP, O) { \
1595 if (!compiler_addop_i_noline((C), (OP), (O))) \
1596 return 0; \
1597}
1598
Mark Shannon582aaf12020-08-04 17:30:11 +01001599#define ADDOP_JUMP(C, OP, O) { \
1600 if (!compiler_addop_j((C), (OP), (O))) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001602}
1603
Mark Shannon127dde52021-01-04 18:06:55 +00001604/* Add a jump with no line number.
1605 * Used for artificial jumps that have no corresponding
1606 * token in the source code. */
1607#define ADDOP_JUMP_NOLINE(C, OP, O) { \
1608 if (!compiler_addop_j_noline((C), (OP), (O))) \
1609 return 0; \
1610}
1611
Mark Shannon9af0e472020-01-14 10:12:45 +00001612#define ADDOP_COMPARE(C, CMP) { \
1613 if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \
1614 return 0; \
1615}
1616
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1618 the ASDL name to synthesize the name of the C type and the visit function.
1619*/
1620
1621#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 if (!compiler_visit_ ## TYPE((C), (V))) \
1623 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001624}
1625
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001626#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 if (!compiler_visit_ ## TYPE((C), (V))) { \
1628 compiler_exit_scope(c); \
1629 return 0; \
1630 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001631}
1632
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001633#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 if (!compiler_visit_slice((C), (V), (CTX))) \
1635 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636}
1637
1638#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001640 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1642 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1643 if (!compiler_visit_ ## TYPE((C), elt)) \
1644 return 0; \
1645 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001646}
1647
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001648#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001650 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1652 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1653 if (!compiler_visit_ ## TYPE((C), elt)) { \
1654 compiler_exit_scope(c); \
1655 return 0; \
1656 } \
1657 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001658}
1659
Brandt Bucher145bf262021-02-26 14:51:55 -08001660#define RETURN_IF_FALSE(X) \
1661 if (!(X)) { \
1662 return 0; \
1663 }
1664
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001665/* Search if variable annotations are present statically in a block. */
1666
1667static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001668find_ann(asdl_stmt_seq *stmts)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001669{
1670 int i, j, res = 0;
1671 stmt_ty st;
1672
1673 for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1674 st = (stmt_ty)asdl_seq_GET(stmts, i);
1675 switch (st->kind) {
1676 case AnnAssign_kind:
1677 return 1;
1678 case For_kind:
1679 res = find_ann(st->v.For.body) ||
1680 find_ann(st->v.For.orelse);
1681 break;
1682 case AsyncFor_kind:
1683 res = find_ann(st->v.AsyncFor.body) ||
1684 find_ann(st->v.AsyncFor.orelse);
1685 break;
1686 case While_kind:
1687 res = find_ann(st->v.While.body) ||
1688 find_ann(st->v.While.orelse);
1689 break;
1690 case If_kind:
1691 res = find_ann(st->v.If.body) ||
1692 find_ann(st->v.If.orelse);
1693 break;
1694 case With_kind:
1695 res = find_ann(st->v.With.body);
1696 break;
1697 case AsyncWith_kind:
1698 res = find_ann(st->v.AsyncWith.body);
1699 break;
1700 case Try_kind:
1701 for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1702 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1703 st->v.Try.handlers, j);
1704 if (find_ann(handler->v.ExceptHandler.body)) {
1705 return 1;
1706 }
1707 }
1708 res = find_ann(st->v.Try.body) ||
1709 find_ann(st->v.Try.finalbody) ||
1710 find_ann(st->v.Try.orelse);
1711 break;
1712 default:
1713 res = 0;
1714 }
1715 if (res) {
1716 break;
1717 }
1718 }
1719 return res;
1720}
1721
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001722/*
1723 * Frame block handling functions
1724 */
1725
1726static int
1727compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b,
Mark Shannonfee55262019-11-21 09:11:43 +00001728 basicblock *exit, void *datum)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001729{
1730 struct fblockinfo *f;
1731 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
Mark Shannon02d126a2020-09-25 14:04:19 +01001732 return compiler_error(c, "too many statically nested blocks");
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001733 }
1734 f = &c->u->u_fblock[c->u->u_nfblocks++];
1735 f->fb_type = t;
1736 f->fb_block = b;
1737 f->fb_exit = exit;
Mark Shannonfee55262019-11-21 09:11:43 +00001738 f->fb_datum = datum;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001739 return 1;
1740}
1741
1742static void
1743compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
1744{
1745 struct compiler_unit *u = c->u;
1746 assert(u->u_nfblocks > 0);
1747 u->u_nfblocks--;
1748 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
1749 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
1750}
1751
Mark Shannonfee55262019-11-21 09:11:43 +00001752static int
1753compiler_call_exit_with_nones(struct compiler *c) {
1754 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1755 ADDOP(c, DUP_TOP);
1756 ADDOP(c, DUP_TOP);
1757 ADDOP_I(c, CALL_FUNCTION, 3);
1758 return 1;
1759}
1760
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001761/* Unwind a frame block. If preserve_tos is true, the TOS before
Mark Shannonfee55262019-11-21 09:11:43 +00001762 * popping the blocks will be restored afterwards, unless another
1763 * return, break or continue is found. In which case, the TOS will
1764 * be popped.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001765 */
1766static int
1767compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
1768 int preserve_tos)
1769{
Mark Shannon5979e812021-04-30 14:32:47 +01001770 int loc;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001771 switch (info->fb_type) {
1772 case WHILE_LOOP:
Mark Shannon02d126a2020-09-25 14:04:19 +01001773 case EXCEPTION_HANDLER:
tomKPZ7a7ba3d2021-04-07 07:43:45 -07001774 case ASYNC_COMPREHENSION_GENERATOR:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001775 return 1;
1776
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001777 case FOR_LOOP:
1778 /* Pop the iterator */
1779 if (preserve_tos) {
1780 ADDOP(c, ROT_TWO);
1781 }
1782 ADDOP(c, POP_TOP);
1783 return 1;
1784
Mark Shannon02d126a2020-09-25 14:04:19 +01001785 case TRY_EXCEPT:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001786 ADDOP(c, POP_BLOCK);
1787 return 1;
1788
1789 case FINALLY_TRY:
Mark Shannon5274b682020-12-16 13:07:01 +00001790 /* This POP_BLOCK gets the line number of the unwinding statement */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001791 ADDOP(c, POP_BLOCK);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001792 if (preserve_tos) {
Mark Shannonfee55262019-11-21 09:11:43 +00001793 if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) {
1794 return 0;
1795 }
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001796 }
Mark Shannon5274b682020-12-16 13:07:01 +00001797 /* Emit the finally block */
Mark Shannonfee55262019-11-21 09:11:43 +00001798 VISIT_SEQ(c, stmt, info->fb_datum);
1799 if (preserve_tos) {
1800 compiler_pop_fblock(c, POP_VALUE, NULL);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001801 }
Mark Shannon5274b682020-12-16 13:07:01 +00001802 /* The finally block should appear to execute after the
1803 * statement causing the unwinding, so make the unwinding
1804 * instruction artificial */
1805 c->u->u_lineno = -1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001806 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001807
Mark Shannonfee55262019-11-21 09:11:43 +00001808 case FINALLY_END:
1809 if (preserve_tos) {
1810 ADDOP(c, ROT_FOUR);
1811 }
1812 ADDOP(c, POP_TOP);
1813 ADDOP(c, POP_TOP);
1814 ADDOP(c, POP_TOP);
1815 if (preserve_tos) {
1816 ADDOP(c, ROT_FOUR);
1817 }
1818 ADDOP(c, POP_EXCEPT);
1819 return 1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001820
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001821 case WITH:
1822 case ASYNC_WITH:
Mark Shannon5979e812021-04-30 14:32:47 +01001823 loc = c->u->u_lineno;
1824 SET_LOC(c, (stmt_ty)info->fb_datum);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001825 ADDOP(c, POP_BLOCK);
1826 if (preserve_tos) {
1827 ADDOP(c, ROT_TWO);
1828 }
Mark Shannonfee55262019-11-21 09:11:43 +00001829 if(!compiler_call_exit_with_nones(c)) {
1830 return 0;
1831 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001832 if (info->fb_type == ASYNC_WITH) {
1833 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001834 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001835 ADDOP(c, YIELD_FROM);
1836 }
Mark Shannonfee55262019-11-21 09:11:43 +00001837 ADDOP(c, POP_TOP);
Mark Shannon5979e812021-04-30 14:32:47 +01001838 c->u->u_lineno = loc;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001839 return 1;
1840
1841 case HANDLER_CLEANUP:
Mark Shannonfee55262019-11-21 09:11:43 +00001842 if (info->fb_datum) {
1843 ADDOP(c, POP_BLOCK);
1844 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001845 if (preserve_tos) {
1846 ADDOP(c, ROT_FOUR);
1847 }
Mark Shannonfee55262019-11-21 09:11:43 +00001848 ADDOP(c, POP_EXCEPT);
1849 if (info->fb_datum) {
1850 ADDOP_LOAD_CONST(c, Py_None);
1851 compiler_nameop(c, info->fb_datum, Store);
1852 compiler_nameop(c, info->fb_datum, Del);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001853 }
Mark Shannonfee55262019-11-21 09:11:43 +00001854 return 1;
1855
1856 case POP_VALUE:
1857 if (preserve_tos) {
1858 ADDOP(c, ROT_TWO);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001859 }
Mark Shannonfee55262019-11-21 09:11:43 +00001860 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001861 return 1;
1862 }
1863 Py_UNREACHABLE();
1864}
1865
Mark Shannonfee55262019-11-21 09:11:43 +00001866/** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */
1867static int
1868compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) {
1869 if (c->u->u_nfblocks == 0) {
1870 return 1;
1871 }
1872 struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1];
1873 if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) {
1874 *loop = top;
1875 return 1;
1876 }
1877 struct fblockinfo copy = *top;
1878 c->u->u_nfblocks--;
1879 if (!compiler_unwind_fblock(c, &copy, preserve_tos)) {
1880 return 0;
1881 }
1882 if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) {
1883 return 0;
1884 }
1885 c->u->u_fblock[c->u->u_nfblocks] = copy;
1886 c->u->u_nfblocks++;
1887 return 1;
1888}
1889
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001890/* Compile a sequence of statements, checking for a docstring
1891 and for annotations. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001892
1893static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001894compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001895{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001896 int i = 0;
1897 stmt_ty st;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001898 PyObject *docstring;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001899
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001900 /* Set current line number to the line number of first statement.
1901 This way line number for SETUP_ANNOTATIONS will always
1902 coincide with the line number of first "real" statement in module.
Hansraj Das01171eb2019-10-09 07:54:02 +05301903 If body is empty, then lineno will be set later in assemble. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001904 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) {
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001905 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001906 SET_LOC(c, st);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001907 }
1908 /* Every annotated class and module should have __annotations__. */
1909 if (find_ann(stmts)) {
1910 ADDOP(c, SETUP_ANNOTATIONS);
1911 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001912 if (!asdl_seq_LEN(stmts))
1913 return 1;
INADA Naokicb41b272017-02-23 00:31:59 +09001914 /* if not -OO mode, set docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001915 if (c->c_optimize < 2) {
1916 docstring = _PyAST_GetDocString(stmts);
1917 if (docstring) {
1918 i = 1;
1919 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1920 assert(st->kind == Expr_kind);
1921 VISIT(c, expr, st->v.Expr.value);
1922 if (!compiler_nameop(c, __doc__, Store))
1923 return 0;
1924 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001926 for (; i < asdl_seq_LEN(stmts); i++)
1927 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001929}
1930
1931static PyCodeObject *
1932compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001933{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 PyCodeObject *co;
1935 int addNone = 1;
1936 static PyObject *module;
1937 if (!module) {
1938 module = PyUnicode_InternFromString("<module>");
1939 if (!module)
1940 return NULL;
1941 }
1942 /* Use 0 for firstlineno initially, will fixup in assemble(). */
Mark Shannon877df852020-11-12 09:43:29 +00001943 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 return NULL;
1945 switch (mod->kind) {
1946 case Module_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001947 if (!compiler_body(c, mod->v.Module.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 compiler_exit_scope(c);
1949 return 0;
1950 }
1951 break;
1952 case Interactive_kind:
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001953 if (find_ann(mod->v.Interactive.body)) {
1954 ADDOP(c, SETUP_ANNOTATIONS);
1955 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 c->c_interactive = 1;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001957 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 break;
1959 case Expression_kind:
1960 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1961 addNone = 0;
1962 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 default:
1964 PyErr_Format(PyExc_SystemError,
1965 "module kind %d should not be possible",
1966 mod->kind);
1967 return 0;
1968 }
1969 co = assemble(c, addNone);
1970 compiler_exit_scope(c);
1971 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001972}
1973
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001974/* The test for LOCAL must come before the test for FREE in order to
1975 handle classes where name is both local and free. The local var is
1976 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001977*/
1978
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001979static int
1980get_ref_type(struct compiler *c, PyObject *name)
1981{
Victor Stinner0b1bc562013-05-16 22:17:17 +02001982 int scope;
Benjamin Peterson312595c2013-05-15 15:26:42 -05001983 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001984 _PyUnicode_EqualToASCIIString(name, "__class__"))
Benjamin Peterson312595c2013-05-15 15:26:42 -05001985 return CELL;
Victor Stinner28ad12f2021-03-19 12:41:49 +01001986 scope = _PyST_GetScope(c->u->u_ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 if (scope == 0) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001988 PyErr_Format(PyExc_SystemError,
Victor Stinner28ad12f2021-03-19 12:41:49 +01001989 "_PyST_GetScope(name=%R) failed: "
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001990 "unknown scope in unit %S (%R); "
1991 "symbols: %R; locals: %R; globals: %R",
1992 name,
1993 c->u->u_name, c->u->u_ste->ste_id,
1994 c->u->u_ste->ste_symbols, c->u->u_varnames, c->u->u_names);
1995 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001998}
1999
2000static int
2001compiler_lookup_arg(PyObject *dict, PyObject *name)
2002{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002003 PyObject *v;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02002004 v = PyDict_GetItemWithError(dict, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00002006 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00002007 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002008}
2009
2010static int
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002011compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags,
2012 PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002013{
Victor Stinnerad9a0662013-11-19 22:23:20 +01002014 Py_ssize_t i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002015 if (qualname == NULL)
2016 qualname = co->co_name;
2017
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002018 if (free) {
2019 for (i = 0; i < free; ++i) {
2020 /* Bypass com_addop_varname because it will generate
2021 LOAD_DEREF but LOAD_CLOSURE is needed.
2022 */
2023 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002024
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002025 /* Special case: If a class contains a method with a
2026 free variable that has the same name as a method,
2027 the name will be considered free *and* local in the
2028 class. It should be handled by the closure, as
Min ho Kimc4cacc82019-07-31 08:16:13 +10002029 well as by the normal name lookup logic.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002030 */
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002031 int reftype = get_ref_type(c, name);
2032 if (reftype == -1) {
2033 return 0;
2034 }
2035 int arg;
2036 if (reftype == CELL) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002037 arg = compiler_lookup_arg(c->u->u_cellvars, name);
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002038 }
2039 else {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002040 arg = compiler_lookup_arg(c->u->u_freevars, name);
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002041 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002042 if (arg == -1) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002043 PyErr_Format(PyExc_SystemError,
2044 "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; "
2045 "freevars of code %S: %R",
2046 name,
2047 reftype,
2048 c->u->u_name,
2049 co->co_name,
2050 co->co_freevars);
2051 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002052 }
2053 ADDOP_I(c, LOAD_CLOSURE, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002055 flags |= 0x08;
2056 ADDOP_I(c, BUILD_TUPLE, free);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002058 ADDOP_LOAD_CONST(c, (PyObject*)co);
2059 ADDOP_LOAD_CONST(c, qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002060 ADDOP_I(c, MAKE_FUNCTION, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002062}
2063
2064static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002065compiler_decorators(struct compiler *c, asdl_expr_seq* decos)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002066{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 if (!decos)
2070 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2073 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
2074 }
2075 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002076}
2077
2078static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002079compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs,
2080 asdl_expr_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00002081{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002082 /* Push a dict of keyword-only default values.
2083
2084 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
2085 */
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002086 int i;
2087 PyObject *keys = NULL;
2088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
2090 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
2091 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
2092 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04002093 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002094 if (!mangled) {
2095 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002097 if (keys == NULL) {
2098 keys = PyList_New(1);
2099 if (keys == NULL) {
2100 Py_DECREF(mangled);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002101 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002102 }
2103 PyList_SET_ITEM(keys, 0, mangled);
2104 }
2105 else {
2106 int res = PyList_Append(keys, mangled);
2107 Py_DECREF(mangled);
2108 if (res == -1) {
2109 goto error;
2110 }
2111 }
2112 if (!compiler_visit_expr(c, default_)) {
2113 goto error;
2114 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115 }
2116 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002117 if (keys != NULL) {
2118 Py_ssize_t default_count = PyList_GET_SIZE(keys);
2119 PyObject *keys_tuple = PyList_AsTuple(keys);
2120 Py_DECREF(keys);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002121 ADDOP_LOAD_CONST_NEW(c, keys_tuple);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002122 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002123 assert(default_count > 0);
2124 return 1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002125 }
2126 else {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002127 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002128 }
2129
2130error:
2131 Py_XDECREF(keys);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002132 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002133}
2134
2135static int
Guido van Rossum95e4d582018-01-26 08:20:18 -08002136compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
2137{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +03002138 ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation));
Guido van Rossum95e4d582018-01-26 08:20:18 -08002139 return 1;
2140}
2141
2142static int
Neal Norwitzc1505362006-12-28 06:47:50 +00002143compiler_visit_argannotation(struct compiler *c, identifier id,
Yurii Karabas73019792020-11-25 12:43:18 +02002144 expr_ty annotation, Py_ssize_t *annotations_len)
Neal Norwitzc1505362006-12-28 06:47:50 +00002145{
Pablo Galindob0544ba2021-04-21 12:41:19 +01002146 if (!annotation) {
2147 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 }
Pablo Galindob0544ba2021-04-21 12:41:19 +01002149
2150 PyObject *mangled = _Py_Mangle(c->u->u_private, id);
2151 if (!mangled) {
2152 return 0;
2153 }
2154 ADDOP_LOAD_CONST(c, mangled);
2155 Py_DECREF(mangled);
2156
2157 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
2158 VISIT(c, annexpr, annotation)
2159 }
2160 else {
2161 VISIT(c, expr, annotation);
2162 }
2163 *annotations_len += 2;
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002164 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002165}
2166
2167static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002168compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args,
Yurii Karabas73019792020-11-25 12:43:18 +02002169 Py_ssize_t *annotations_len)
Neal Norwitzc1505362006-12-28 06:47:50 +00002170{
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002171 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002172 for (i = 0; i < asdl_seq_LEN(args); i++) {
2173 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002174 if (!compiler_visit_argannotation(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 c,
2176 arg->arg,
2177 arg->annotation,
Yurii Karabas73019792020-11-25 12:43:18 +02002178 annotations_len))
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002179 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002181 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002182}
2183
2184static int
2185compiler_visit_annotations(struct compiler *c, arguments_ty args,
2186 expr_ty returns)
2187{
Yurii Karabas73019792020-11-25 12:43:18 +02002188 /* Push arg annotation names and values.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002189 The expressions are evaluated out-of-order wrt the source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00002190
Yurii Karabas73019792020-11-25 12:43:18 +02002191 Return 0 on error, -1 if no annotations pushed, 1 if a annotations is pushed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 */
2193 static identifier return_str;
Yurii Karabas73019792020-11-25 12:43:18 +02002194 Py_ssize_t annotations_len = 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002195
Yurii Karabas73019792020-11-25 12:43:18 +02002196 if (!compiler_visit_argannotations(c, args->args, &annotations_len))
2197 return 0;
2198 if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len))
2199 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002200 if (args->vararg && args->vararg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002201 !compiler_visit_argannotation(c, args->vararg->arg,
Yurii Karabas73019792020-11-25 12:43:18 +02002202 args->vararg->annotation, &annotations_len))
2203 return 0;
2204 if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len))
2205 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002206 if (args->kwarg && args->kwarg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002207 !compiler_visit_argannotation(c, args->kwarg->arg,
Yurii Karabas73019792020-11-25 12:43:18 +02002208 args->kwarg->annotation, &annotations_len))
2209 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 if (!return_str) {
2212 return_str = PyUnicode_InternFromString("return");
2213 if (!return_str)
Yurii Karabas73019792020-11-25 12:43:18 +02002214 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002215 }
Yurii Karabas73019792020-11-25 12:43:18 +02002216 if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) {
2217 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 }
2219
Yurii Karabas73019792020-11-25 12:43:18 +02002220 if (annotations_len) {
2221 ADDOP_I(c, BUILD_TUPLE, annotations_len);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002222 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002224
Yurii Karabas73019792020-11-25 12:43:18 +02002225 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002226}
2227
2228static int
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002229compiler_visit_defaults(struct compiler *c, arguments_ty args)
2230{
2231 VISIT_SEQ(c, expr, args->defaults);
2232 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
2233 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002234}
2235
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002236static Py_ssize_t
2237compiler_default_arguments(struct compiler *c, arguments_ty args)
2238{
2239 Py_ssize_t funcflags = 0;
2240 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002241 if (!compiler_visit_defaults(c, args))
2242 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002243 funcflags |= 0x01;
2244 }
2245 if (args->kwonlyargs) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002246 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002247 args->kw_defaults);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002248 if (res == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002249 return -1;
2250 }
2251 else if (res > 0) {
2252 funcflags |= 0x02;
2253 }
2254 }
2255 return funcflags;
2256}
2257
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002258static int
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002259forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx)
2260{
2261
2262 if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2263 compiler_error(c, "cannot assign to __debug__");
2264 return 1;
2265 }
2266 return 0;
2267}
2268
2269static int
2270compiler_check_debug_one_arg(struct compiler *c, arg_ty arg)
2271{
2272 if (arg != NULL) {
2273 if (forbidden_name(c, arg->arg, Store))
2274 return 0;
2275 }
2276 return 1;
2277}
2278
2279static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002280compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args)
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002281{
2282 if (args != NULL) {
Pablo Galindoee40e4b2020-04-23 03:43:08 +01002283 for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002284 if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i)))
2285 return 0;
2286 }
2287 }
2288 return 1;
2289}
2290
2291static int
2292compiler_check_debug_args(struct compiler *c, arguments_ty args)
2293{
2294 if (!compiler_check_debug_args_seq(c, args->posonlyargs))
2295 return 0;
2296 if (!compiler_check_debug_args_seq(c, args->args))
2297 return 0;
2298 if (!compiler_check_debug_one_arg(c, args->vararg))
2299 return 0;
2300 if (!compiler_check_debug_args_seq(c, args->kwonlyargs))
2301 return 0;
2302 if (!compiler_check_debug_one_arg(c, args->kwarg))
2303 return 0;
2304 return 1;
2305}
2306
2307static int
Yury Selivanov75445082015-05-11 22:57:16 -04002308compiler_function(struct compiler *c, stmt_ty s, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002309{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 PyCodeObject *co;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002311 PyObject *qualname, *docstring = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002312 arguments_ty args;
2313 expr_ty returns;
2314 identifier name;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002315 asdl_expr_seq* decos;
2316 asdl_stmt_seq *body;
INADA Naokicb41b272017-02-23 00:31:59 +09002317 Py_ssize_t i, funcflags;
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002318 int annotations;
Yury Selivanov75445082015-05-11 22:57:16 -04002319 int scope_type;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002320 int firstlineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002321
Yury Selivanov75445082015-05-11 22:57:16 -04002322 if (is_async) {
2323 assert(s->kind == AsyncFunctionDef_kind);
2324
2325 args = s->v.AsyncFunctionDef.args;
2326 returns = s->v.AsyncFunctionDef.returns;
2327 decos = s->v.AsyncFunctionDef.decorator_list;
2328 name = s->v.AsyncFunctionDef.name;
2329 body = s->v.AsyncFunctionDef.body;
2330
2331 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
2332 } else {
2333 assert(s->kind == FunctionDef_kind);
2334
2335 args = s->v.FunctionDef.args;
2336 returns = s->v.FunctionDef.returns;
2337 decos = s->v.FunctionDef.decorator_list;
2338 name = s->v.FunctionDef.name;
2339 body = s->v.FunctionDef.body;
2340
2341 scope_type = COMPILER_SCOPE_FUNCTION;
2342 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002343
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002344 if (!compiler_check_debug_args(c, args))
2345 return 0;
2346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 if (!compiler_decorators(c, decos))
2348 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002349
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002350 firstlineno = s->lineno;
2351 if (asdl_seq_LEN(decos)) {
2352 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2353 }
2354
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002355 funcflags = compiler_default_arguments(c, args);
2356 if (funcflags == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002358 }
2359
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002360 annotations = compiler_visit_annotations(c, args, returns);
2361 if (annotations == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002362 return 0;
2363 }
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002364 else if (annotations > 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002365 funcflags |= 0x04;
2366 }
2367
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002368 if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002369 return 0;
2370 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002371
INADA Naokicb41b272017-02-23 00:31:59 +09002372 /* if not -OO mode, add docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002373 if (c->c_optimize < 2) {
2374 docstring = _PyAST_GetDocString(body);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002375 }
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002376 if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 compiler_exit_scope(c);
2378 return 0;
2379 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002382 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Mark Shannon877df852020-11-12 09:43:29 +00002384 for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) {
Mark Shannonfd009e62020-11-13 12:53:53 +00002385 VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i));
Mark Shannon877df852020-11-12 09:43:29 +00002386 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002388 qualname = c->u->u_qualname;
2389 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002391 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002392 Py_XDECREF(qualname);
2393 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002395 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002396
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002397 if (!compiler_make_closure(c, co, funcflags, qualname)) {
2398 Py_DECREF(qualname);
2399 Py_DECREF(co);
2400 return 0;
2401 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002402 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 /* decorators */
2406 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2407 ADDOP_I(c, CALL_FUNCTION, 1);
2408 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002409
Yury Selivanov75445082015-05-11 22:57:16 -04002410 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002411}
2412
2413static int
2414compiler_class(struct compiler *c, stmt_ty s)
2415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 PyCodeObject *co;
2417 PyObject *str;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002418 int i, firstlineno;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002419 asdl_expr_seq *decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 if (!compiler_decorators(c, decos))
2422 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002423
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002424 firstlineno = s->lineno;
2425 if (asdl_seq_LEN(decos)) {
2426 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2427 }
2428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 /* ultimately generate code for:
2430 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
2431 where:
2432 <func> is a function/closure created from the class body;
2433 it has a single argument (__locals__) where the dict
2434 (or MutableSequence) representing the locals is passed
2435 <name> is the class name
2436 <bases> is the positional arguments and *varargs argument
2437 <keywords> is the keyword arguments and **kwds argument
2438 This borrows from compiler_call.
2439 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002442 if (!compiler_enter_scope(c, s->v.ClassDef.name,
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002443 COMPILER_SCOPE_CLASS, (void *)s, firstlineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 return 0;
2445 /* this block represents what we do in the new scope */
2446 {
2447 /* use the class name for name mangling */
2448 Py_INCREF(s->v.ClassDef.name);
Serhiy Storchaka48842712016-04-06 09:45:48 +03002449 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002450 /* load (global) __name__ ... */
2451 str = PyUnicode_InternFromString("__name__");
2452 if (!str || !compiler_nameop(c, str, Load)) {
2453 Py_XDECREF(str);
2454 compiler_exit_scope(c);
2455 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002456 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002457 Py_DECREF(str);
2458 /* ... and store it as __module__ */
2459 str = PyUnicode_InternFromString("__module__");
2460 if (!str || !compiler_nameop(c, str, Store)) {
2461 Py_XDECREF(str);
2462 compiler_exit_scope(c);
2463 return 0;
2464 }
2465 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002466 assert(c->u->u_qualname);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002467 ADDOP_LOAD_CONST(c, c->u->u_qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002468 str = PyUnicode_InternFromString("__qualname__");
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 /* compile the body proper */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002476 if (!compiler_body(c, s->v.ClassDef.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 compiler_exit_scope(c);
2478 return 0;
2479 }
Mark Shannone56d54e2021-01-15 13:52:00 +00002480 /* The following code is artificial */
2481 c->u->u_lineno = -1;
Nick Coghlan19d24672016-12-05 16:47:55 +10002482 /* Return __classcell__ if it is referenced, otherwise return None */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002483 if (c->u->u_ste->ste_needs_class_closure) {
Nick Coghlan19d24672016-12-05 16:47:55 +10002484 /* Store __classcell__ into class namespace & return it */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002485 str = PyUnicode_InternFromString("__class__");
2486 if (str == NULL) {
2487 compiler_exit_scope(c);
2488 return 0;
2489 }
2490 i = compiler_lookup_arg(c->u->u_cellvars, str);
2491 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01002492 if (i < 0) {
2493 compiler_exit_scope(c);
2494 return 0;
2495 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002496 assert(i == 0);
Nick Coghlan944368e2016-09-11 14:45:49 +10002497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002498 ADDOP_I(c, LOAD_CLOSURE, i);
Nick Coghlan19d24672016-12-05 16:47:55 +10002499 ADDOP(c, DUP_TOP);
Nick Coghlan944368e2016-09-11 14:45:49 +10002500 str = PyUnicode_InternFromString("__classcell__");
2501 if (!str || !compiler_nameop(c, str, Store)) {
2502 Py_XDECREF(str);
2503 compiler_exit_scope(c);
2504 return 0;
2505 }
2506 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002508 else {
Nick Coghlan19d24672016-12-05 16:47:55 +10002509 /* No methods referenced __class__, so just return None */
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002510 assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002511 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson312595c2013-05-15 15:26:42 -05002512 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002513 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 /* create the code object */
2515 co = assemble(c, 1);
2516 }
2517 /* leave the new scope */
2518 compiler_exit_scope(c);
2519 if (co == NULL)
2520 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 /* 2. load the 'build_class' function */
2523 ADDOP(c, LOAD_BUILD_CLASS);
2524
2525 /* 3. load a function (or closure) made from the code object */
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002526 if (!compiler_make_closure(c, co, 0, NULL)) {
2527 Py_DECREF(co);
2528 return 0;
2529 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002530 Py_DECREF(co);
2531
2532 /* 4. load class name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002533 ADDOP_LOAD_CONST(c, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002534
2535 /* 5. generate the rest of the code for the call */
Pablo Galindoa5634c42020-09-16 19:42:00 +01002536 if (!compiler_call_helper(c, 2, s->v.ClassDef.bases, s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002537 return 0;
2538
2539 /* 6. apply decorators */
2540 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2541 ADDOP_I(c, CALL_FUNCTION, 1);
2542 }
2543
2544 /* 7. store into <name> */
2545 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2546 return 0;
2547 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002548}
2549
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02002550/* Return 0 if the expression is a constant value except named singletons.
2551 Return 1 otherwise. */
2552static int
2553check_is_arg(expr_ty e)
2554{
2555 if (e->kind != Constant_kind) {
2556 return 1;
2557 }
2558 PyObject *value = e->v.Constant.value;
2559 return (value == Py_None
2560 || value == Py_False
2561 || value == Py_True
2562 || value == Py_Ellipsis);
2563}
2564
2565/* Check operands of identity chacks ("is" and "is not").
2566 Emit a warning if any operand is a constant except named singletons.
2567 Return 0 on error.
2568 */
2569static int
2570check_compare(struct compiler *c, expr_ty e)
2571{
2572 Py_ssize_t i, n;
2573 int left = check_is_arg(e->v.Compare.left);
2574 n = asdl_seq_LEN(e->v.Compare.ops);
2575 for (i = 0; i < n; i++) {
2576 cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i);
2577 int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2578 if (op == Is || op == IsNot) {
2579 if (!right || !left) {
2580 const char *msg = (op == Is)
2581 ? "\"is\" with a literal. Did you mean \"==\"?"
2582 : "\"is not\" with a literal. Did you mean \"!=\"?";
2583 return compiler_warn(c, msg);
2584 }
2585 }
2586 left = right;
2587 }
2588 return 1;
2589}
2590
Mark Shannon9af0e472020-01-14 10:12:45 +00002591static int compiler_addcompare(struct compiler *c, cmpop_ty op)
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002592{
Mark Shannon9af0e472020-01-14 10:12:45 +00002593 int cmp;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002594 switch (op) {
2595 case Eq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002596 cmp = Py_EQ;
2597 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002598 case NotEq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002599 cmp = Py_NE;
2600 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002601 case Lt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002602 cmp = Py_LT;
2603 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002604 case LtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002605 cmp = Py_LE;
2606 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002607 case Gt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002608 cmp = Py_GT;
2609 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002610 case GtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002611 cmp = Py_GE;
2612 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002613 case Is:
Mark Shannon9af0e472020-01-14 10:12:45 +00002614 ADDOP_I(c, IS_OP, 0);
2615 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002616 case IsNot:
Mark Shannon9af0e472020-01-14 10:12:45 +00002617 ADDOP_I(c, IS_OP, 1);
2618 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002619 case In:
Mark Shannon9af0e472020-01-14 10:12:45 +00002620 ADDOP_I(c, CONTAINS_OP, 0);
2621 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002622 case NotIn:
Mark Shannon9af0e472020-01-14 10:12:45 +00002623 ADDOP_I(c, CONTAINS_OP, 1);
2624 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002625 default:
Mark Shannon9af0e472020-01-14 10:12:45 +00002626 Py_UNREACHABLE();
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002627 }
Mark Shannon9af0e472020-01-14 10:12:45 +00002628 ADDOP_I(c, COMPARE_OP, cmp);
2629 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002630}
2631
Mark Shannon9af0e472020-01-14 10:12:45 +00002632
2633
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002634static int
2635compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2636{
2637 switch (e->kind) {
2638 case UnaryOp_kind:
2639 if (e->v.UnaryOp.op == Not)
2640 return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2641 /* fallback to general implementation */
2642 break;
2643 case BoolOp_kind: {
Pablo Galindoa5634c42020-09-16 19:42:00 +01002644 asdl_expr_seq *s = e->v.BoolOp.values;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002645 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2646 assert(n >= 0);
2647 int cond2 = e->v.BoolOp.op == Or;
2648 basicblock *next2 = next;
2649 if (!cond2 != !cond) {
2650 next2 = compiler_new_block(c);
2651 if (next2 == NULL)
2652 return 0;
2653 }
2654 for (i = 0; i < n; ++i) {
2655 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2656 return 0;
2657 }
2658 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2659 return 0;
2660 if (next2 != next)
2661 compiler_use_next_block(c, next2);
2662 return 1;
2663 }
2664 case IfExp_kind: {
2665 basicblock *end, *next2;
2666 end = compiler_new_block(c);
2667 if (end == NULL)
2668 return 0;
2669 next2 = compiler_new_block(c);
2670 if (next2 == NULL)
2671 return 0;
2672 if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2673 return 0;
2674 if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2675 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002676 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002677 compiler_use_next_block(c, next2);
2678 if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2679 return 0;
2680 compiler_use_next_block(c, end);
2681 return 1;
2682 }
2683 case Compare_kind: {
2684 Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2685 if (n > 0) {
Serhiy Storchaka45835252019-02-16 08:29:46 +02002686 if (!check_compare(c, e)) {
2687 return 0;
2688 }
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002689 basicblock *cleanup = compiler_new_block(c);
2690 if (cleanup == NULL)
2691 return 0;
2692 VISIT(c, expr, e->v.Compare.left);
2693 for (i = 0; i < n; i++) {
2694 VISIT(c, expr,
2695 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2696 ADDOP(c, DUP_TOP);
2697 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00002698 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01002699 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002700 NEXT_BLOCK(c);
2701 }
2702 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00002703 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Mark Shannon582aaf12020-08-04 17:30:11 +01002704 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Mark Shannon266b4622020-11-17 19:30:14 +00002705 NEXT_BLOCK(c);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002706 basicblock *end = compiler_new_block(c);
2707 if (end == NULL)
2708 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002709 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002710 compiler_use_next_block(c, cleanup);
2711 ADDOP(c, POP_TOP);
2712 if (!cond) {
Mark Shannon127dde52021-01-04 18:06:55 +00002713 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002714 }
2715 compiler_use_next_block(c, end);
2716 return 1;
2717 }
2718 /* fallback to general implementation */
2719 break;
2720 }
2721 default:
2722 /* fallback to general implementation */
2723 break;
2724 }
2725
2726 /* general implementation */
2727 VISIT(c, expr, e);
Mark Shannon582aaf12020-08-04 17:30:11 +01002728 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Mark Shannon266b4622020-11-17 19:30:14 +00002729 NEXT_BLOCK(c);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002730 return 1;
2731}
2732
2733static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002734compiler_ifexp(struct compiler *c, expr_ty e)
2735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002736 basicblock *end, *next;
2737
2738 assert(e->kind == IfExp_kind);
2739 end = compiler_new_block(c);
2740 if (end == NULL)
2741 return 0;
2742 next = compiler_new_block(c);
2743 if (next == NULL)
2744 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002745 if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2746 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 VISIT(c, expr, e->v.IfExp.body);
Mark Shannon127dde52021-01-04 18:06:55 +00002748 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002749 compiler_use_next_block(c, next);
2750 VISIT(c, expr, e->v.IfExp.orelse);
2751 compiler_use_next_block(c, end);
2752 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002753}
2754
2755static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002756compiler_lambda(struct compiler *c, expr_ty e)
2757{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002758 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002759 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 static identifier name;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002761 Py_ssize_t funcflags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 arguments_ty args = e->v.Lambda.args;
2763 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002764
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002765 if (!compiler_check_debug_args(c, args))
2766 return 0;
2767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002768 if (!name) {
2769 name = PyUnicode_InternFromString("<lambda>");
2770 if (!name)
2771 return 0;
2772 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002773
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002774 funcflags = compiler_default_arguments(c, args);
2775 if (funcflags == -1) {
2776 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002778
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002779 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002780 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002783 /* Make None the first constant, so the lambda can't have a
2784 docstring. */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002785 if (compiler_add_const(c, Py_None) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002789 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002790 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2791 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2792 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002793 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002794 }
2795 else {
2796 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002797 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002798 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002799 qualname = c->u->u_qualname;
2800 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 compiler_exit_scope(c);
Pablo Galindo7fdab832021-01-29 22:40:59 +00002802 if (co == NULL) {
2803 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 return 0;
Pablo Galindo7fdab832021-01-29 22:40:59 +00002805 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002806
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002807 if (!compiler_make_closure(c, co, funcflags, qualname)) {
2808 Py_DECREF(qualname);
2809 Py_DECREF(co);
2810 return 0;
2811 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002812 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 Py_DECREF(co);
2814
2815 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002816}
2817
2818static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002819compiler_if(struct compiler *c, stmt_ty s)
2820{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 basicblock *end, *next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002822 assert(s->kind == If_kind);
2823 end = compiler_new_block(c);
Mark Shannon8473cf82020-12-15 11:07:50 +00002824 if (end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002825 return 0;
Mark Shannon8473cf82020-12-15 11:07:50 +00002826 }
2827 if (asdl_seq_LEN(s->v.If.orelse)) {
2828 next = compiler_new_block(c);
2829 if (next == NULL) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002830 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002831 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002832 }
2833 else {
2834 next = end;
2835 }
2836 if (!compiler_jump_if(c, s->v.If.test, next, 0)) {
2837 return 0;
2838 }
2839 VISIT_SEQ(c, stmt, s->v.If.body);
2840 if (asdl_seq_LEN(s->v.If.orelse)) {
Mark Shannon127dde52021-01-04 18:06:55 +00002841 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Mark Shannon8473cf82020-12-15 11:07:50 +00002842 compiler_use_next_block(c, next);
2843 VISIT_SEQ(c, stmt, s->v.If.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 }
2845 compiler_use_next_block(c, end);
2846 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002847}
2848
2849static int
2850compiler_for(struct compiler *c, stmt_ty s)
2851{
Mark Shannon5977a792020-12-02 13:31:40 +00002852 basicblock *start, *body, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 start = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002855 body = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 cleanup = compiler_new_block(c);
2857 end = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002858 if (start == NULL || body == NULL || end == NULL || cleanup == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002860 }
2861 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002863 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 VISIT(c, expr, s->v.For.iter);
2865 ADDOP(c, GET_ITER);
2866 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01002867 ADDOP_JUMP(c, FOR_ITER, cleanup);
Mark Shannon5977a792020-12-02 13:31:40 +00002868 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 VISIT(c, expr, s->v.For.target);
2870 VISIT_SEQ(c, stmt, s->v.For.body);
Mark Shannonf5e97b72020-12-14 11:28:39 +00002871 /* Mark jump as artificial */
2872 c->u->u_lineno = -1;
Mark Shannon582aaf12020-08-04 17:30:11 +01002873 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 compiler_use_next_block(c, cleanup);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002875
2876 compiler_pop_fblock(c, FOR_LOOP, start);
2877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 VISIT_SEQ(c, stmt, s->v.For.orelse);
2879 compiler_use_next_block(c, end);
2880 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002881}
2882
Yury Selivanov75445082015-05-11 22:57:16 -04002883
2884static int
2885compiler_async_for(struct compiler *c, stmt_ty s)
2886{
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002887 basicblock *start, *except, *end;
Pablo Galindo90235812020-03-15 04:29:22 +00002888 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07002889 c->u->u_ste->ste_coroutine = 1;
2890 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
Zsolt Dollensteine2396502018-04-27 08:58:56 -07002891 return compiler_error(c, "'async for' outside async function");
2892 }
2893
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002894 start = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002895 except = compiler_new_block(c);
2896 end = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002897
Mark Shannonfee55262019-11-21 09:11:43 +00002898 if (start == NULL || except == NULL || end == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002899 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002900 }
Yury Selivanov75445082015-05-11 22:57:16 -04002901 VISIT(c, expr, s->v.AsyncFor.iter);
2902 ADDOP(c, GET_AITER);
Yury Selivanov75445082015-05-11 22:57:16 -04002903
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002904 compiler_use_next_block(c, start);
Mark Shannonfee55262019-11-21 09:11:43 +00002905 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002906 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002907 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002908 /* SETUP_FINALLY to guard the __anext__ call */
Mark Shannon582aaf12020-08-04 17:30:11 +01002909 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov75445082015-05-11 22:57:16 -04002910 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002911 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04002912 ADDOP(c, YIELD_FROM);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002913 ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */
Yury Selivanov75445082015-05-11 22:57:16 -04002914
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002915 /* Success block for __anext__ */
2916 VISIT(c, expr, s->v.AsyncFor.target);
2917 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002918 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002919
2920 compiler_pop_fblock(c, FOR_LOOP, start);
Yury Selivanov75445082015-05-11 22:57:16 -04002921
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002922 /* Except block for __anext__ */
Yury Selivanov75445082015-05-11 22:57:16 -04002923 compiler_use_next_block(c, except);
Mark Shannon877df852020-11-12 09:43:29 +00002924
2925 c->u->u_lineno = -1;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002926 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov75445082015-05-11 22:57:16 -04002927
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002928 /* `else` block */
Yury Selivanov75445082015-05-11 22:57:16 -04002929 VISIT_SEQ(c, stmt, s->v.For.orelse);
2930
2931 compiler_use_next_block(c, end);
2932
2933 return 1;
2934}
2935
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002936static int
2937compiler_while(struct compiler *c, stmt_ty s)
2938{
Mark Shannon266b4622020-11-17 19:30:14 +00002939 basicblock *loop, *body, *end, *anchor = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002940 loop = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002941 body = compiler_new_block(c);
2942 anchor = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 end = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002944 if (loop == NULL || body == NULL || anchor == NULL || end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002945 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002947 compiler_use_next_block(c, loop);
Mark Shannon266b4622020-11-17 19:30:14 +00002948 if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002949 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002950 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002951 if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) {
2952 return 0;
Mark Shannon266b4622020-11-17 19:30:14 +00002953 }
2954
2955 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002956 VISIT_SEQ(c, stmt, s->v.While.body);
Mark Shannon8473cf82020-12-15 11:07:50 +00002957 SET_LOC(c, s);
2958 if (!compiler_jump_if(c, s->v.While.test, body, 1)) {
2959 return 0;
2960 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002961
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002962 compiler_pop_fblock(c, WHILE_LOOP, loop);
2963
Mark Shannon266b4622020-11-17 19:30:14 +00002964 compiler_use_next_block(c, anchor);
2965 if (s->v.While.orelse) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002966 VISIT_SEQ(c, stmt, s->v.While.orelse);
Mark Shannon266b4622020-11-17 19:30:14 +00002967 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002970 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002971}
2972
2973static int
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002974compiler_return(struct compiler *c, stmt_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002975{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002976 int preserve_tos = ((s->v.Return.value != NULL) &&
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002977 (s->v.Return.value->kind != Constant_kind));
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002978 if (c->u->u_ste->ste_type != FunctionBlock)
2979 return compiler_error(c, "'return' outside function");
2980 if (s->v.Return.value != NULL &&
2981 c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2982 {
2983 return compiler_error(
2984 c, "'return' with value in async generator");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002985 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002986 if (preserve_tos) {
2987 VISIT(c, expr, s->v.Return.value);
Mark Shannon5274b682020-12-16 13:07:01 +00002988 } else {
2989 /* Emit instruction with line number for expression */
2990 if (s->v.Return.value != NULL) {
2991 SET_LOC(c, s->v.Return.value);
2992 ADDOP(c, NOP);
2993 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002994 }
Mark Shannonfee55262019-11-21 09:11:43 +00002995 if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL))
2996 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002997 if (s->v.Return.value == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002998 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002999 }
3000 else if (!preserve_tos) {
Mark Shannon5274b682020-12-16 13:07:01 +00003001 ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003002 }
3003 ADDOP(c, RETURN_VALUE);
Mark Shannon266b4622020-11-17 19:30:14 +00003004 NEXT_BLOCK(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003007}
3008
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003009static int
3010compiler_break(struct compiler *c)
3011{
Mark Shannonfee55262019-11-21 09:11:43 +00003012 struct fblockinfo *loop = NULL;
3013 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
3014 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003015 }
Mark Shannonfee55262019-11-21 09:11:43 +00003016 if (loop == NULL) {
3017 return compiler_error(c, "'break' outside loop");
3018 }
3019 if (!compiler_unwind_fblock(c, loop, 0)) {
3020 return 0;
3021 }
Mark Shannon582aaf12020-08-04 17:30:11 +01003022 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit);
Mark Shannon266b4622020-11-17 19:30:14 +00003023 NEXT_BLOCK(c);
Mark Shannonfee55262019-11-21 09:11:43 +00003024 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003025}
3026
3027static int
3028compiler_continue(struct compiler *c)
3029{
Mark Shannonfee55262019-11-21 09:11:43 +00003030 struct fblockinfo *loop = NULL;
3031 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
3032 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003033 }
Mark Shannonfee55262019-11-21 09:11:43 +00003034 if (loop == NULL) {
3035 return compiler_error(c, "'continue' not properly in loop");
3036 }
Mark Shannon582aaf12020-08-04 17:30:11 +01003037 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block);
Mark Shannon266b4622020-11-17 19:30:14 +00003038 NEXT_BLOCK(c)
Mark Shannonfee55262019-11-21 09:11:43 +00003039 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003040}
3041
3042
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003043/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003044
3045 SETUP_FINALLY L
3046 <code for body>
3047 POP_BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00003048 <code for finalbody>
3049 JUMP E
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003050 L:
3051 <code for finalbody>
Mark Shannonfee55262019-11-21 09:11:43 +00003052 E:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003053
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003054 The special instructions use the block stack. Each block
3055 stack entry contains the instruction that created it (here
3056 SETUP_FINALLY), the level of the value stack at the time the
3057 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003059 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003060 Pushes the current value stack level and the label
3061 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003062 POP_BLOCK:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003063 Pops en entry from the block stack.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003064
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003065 The block stack is unwound when an exception is raised:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003066 when a SETUP_FINALLY entry is found, the raised and the caught
3067 exceptions are pushed onto the value stack (and the exception
3068 condition is cleared), and the interpreter jumps to the label
3069 gotten from the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003070*/
3071
3072static int
3073compiler_try_finally(struct compiler *c, stmt_ty s)
3074{
Mark Shannonfee55262019-11-21 09:11:43 +00003075 basicblock *body, *end, *exit;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003077 body = compiler_new_block(c);
3078 end = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00003079 exit = compiler_new_block(c);
3080 if (body == NULL || end == NULL || exit == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003082
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003083 /* `try` block */
Mark Shannon582aaf12020-08-04 17:30:11 +01003084 ADDOP_JUMP(c, SETUP_FINALLY, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003085 compiler_use_next_block(c, body);
Mark Shannonfee55262019-11-21 09:11:43 +00003086 if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003088 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
3089 if (!compiler_try_except(c, s))
3090 return 0;
3091 }
3092 else {
3093 VISIT_SEQ(c, stmt, s->v.Try.body);
3094 }
Mark Shannon3bd60352021-01-13 12:05:43 +00003095 ADDOP_NOLINE(c, POP_BLOCK);
Mark Shannonfee55262019-11-21 09:11:43 +00003096 compiler_pop_fblock(c, FINALLY_TRY, body);
3097 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Mark Shannon127dde52021-01-04 18:06:55 +00003098 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00003099 /* `finally` block */
3100 compiler_use_next_block(c, end);
3101 if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL))
3102 return 0;
3103 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3104 compiler_pop_fblock(c, FINALLY_END, end);
Mark Shannonbf353f32020-12-17 13:55:28 +00003105 ADDOP_I(c, RERAISE, 0);
Mark Shannonfee55262019-11-21 09:11:43 +00003106 compiler_use_next_block(c, exit);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003107 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003108}
3109
3110/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003111 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003112 (The contents of the value stack is shown in [], with the top
3113 at the right; 'tb' is trace-back info, 'val' the exception's
3114 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003115
3116 Value stack Label Instruction Argument
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003117 [] SETUP_FINALLY L1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003118 [] <code for S>
3119 [] POP_BLOCK
3120 [] JUMP_FORWARD L0
3121
3122 [tb, val, exc] L1: DUP )
3123 [tb, val, exc, exc] <evaluate E1> )
Mark Shannon9af0e472020-01-14 10:12:45 +00003124 [tb, val, exc, exc, E1] JUMP_IF_NOT_EXC_MATCH L2 ) only if E1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003125 [tb, val, exc] POP
3126 [tb, val] <assign to V1> (or POP if no V1)
3127 [tb] POP
3128 [] <code for S1>
3129 JUMP_FORWARD L0
3130
3131 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003132 .............................etc.......................
3133
Mark Shannonfee55262019-11-21 09:11:43 +00003134 [tb, val, exc] Ln+1: RERAISE # re-raise exception
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003135
3136 [] L0: <next statement>
3137
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003138 Of course, parts are not generated if Vi or Ei is not present.
3139*/
3140static int
3141compiler_try_except(struct compiler *c, stmt_ty s)
3142{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003143 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003144 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003146 body = compiler_new_block(c);
3147 except = compiler_new_block(c);
3148 orelse = compiler_new_block(c);
3149 end = compiler_new_block(c);
3150 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
3151 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01003152 ADDOP_JUMP(c, SETUP_FINALLY, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003153 compiler_use_next_block(c, body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003154 if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003155 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003156 VISIT_SEQ(c, stmt, s->v.Try.body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003157 compiler_pop_fblock(c, TRY_EXCEPT, body);
Mark Shannon3bd60352021-01-13 12:05:43 +00003158 ADDOP_NOLINE(c, POP_BLOCK);
3159 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003160 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 compiler_use_next_block(c, except);
Mark Shannon02d126a2020-09-25 14:04:19 +01003162 /* Runtime will push a block here, so we need to account for that */
3163 if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL))
3164 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003165 for (i = 0; i < n; i++) {
3166 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003167 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003168 if (!handler->v.ExceptHandler.type && i < n-1)
3169 return compiler_error(c, "default 'except:' must be last");
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003170 SET_LOC(c, handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003171 except = compiler_new_block(c);
3172 if (except == NULL)
3173 return 0;
3174 if (handler->v.ExceptHandler.type) {
3175 ADDOP(c, DUP_TOP);
3176 VISIT(c, expr, handler->v.ExceptHandler.type);
Mark Shannon582aaf12020-08-04 17:30:11 +01003177 ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except);
Mark Shannon266b4622020-11-17 19:30:14 +00003178 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003179 }
3180 ADDOP(c, POP_TOP);
3181 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003182 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00003183
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003184 cleanup_end = compiler_new_block(c);
3185 cleanup_body = compiler_new_block(c);
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003186 if (cleanup_end == NULL || cleanup_body == NULL) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003187 return 0;
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003188 }
Guido van Rossumb940e112007-01-10 16:19:56 +00003189
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003190 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3191 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003192
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003193 /*
3194 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003195 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003196 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003197 try:
3198 # body
3199 finally:
Chris Angelicoad098b62019-05-21 23:34:19 +10003200 name = None # in case body contains "del name"
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003201 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003202 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003203
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003204 /* second try: */
Mark Shannon582aaf12020-08-04 17:30:11 +01003205 ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003206 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003207 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003208 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003209
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003210 /* second # body */
3211 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003212 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003213 ADDOP(c, POP_BLOCK);
3214 ADDOP(c, POP_EXCEPT);
Mark Shannon877df852020-11-12 09:43:29 +00003215 /* name = None; del name; # Mark as artificial */
3216 c->u->u_lineno = -1;
Mark Shannonfee55262019-11-21 09:11:43 +00003217 ADDOP_LOAD_CONST(c, Py_None);
3218 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3219 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Mark Shannon582aaf12020-08-04 17:30:11 +01003220 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003221
Mark Shannonfee55262019-11-21 09:11:43 +00003222 /* except: */
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003223 compiler_use_next_block(c, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003224
Mark Shannon877df852020-11-12 09:43:29 +00003225 /* name = None; del name; # Mark as artificial */
3226 c->u->u_lineno = -1;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003227 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003228 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003229 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230
Mark Shannonbf353f32020-12-17 13:55:28 +00003231 ADDOP_I(c, RERAISE, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003232 }
3233 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003234 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003235
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003236 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05003237 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003238 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003239
Guido van Rossumb940e112007-01-10 16:19:56 +00003240 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003241 ADDOP(c, POP_TOP);
3242 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003243 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003244 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003245 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003246 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannon127dde52021-01-04 18:06:55 +00003247 /* name = None; del name; # Mark as artificial */
3248 c->u->u_lineno = -1;
Mark Shannonfee55262019-11-21 09:11:43 +00003249 ADDOP(c, POP_EXCEPT);
Mark Shannon582aaf12020-08-04 17:30:11 +01003250 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003251 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003252 compiler_use_next_block(c, except);
3253 }
Mark Shannon02d126a2020-09-25 14:04:19 +01003254 compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL);
Mark Shannonf2dbfd72020-12-21 13:53:50 +00003255 /* Mark as artificial */
3256 c->u->u_lineno = -1;
Mark Shannonbf353f32020-12-17 13:55:28 +00003257 ADDOP_I(c, RERAISE, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003258 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003259 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003260 compiler_use_next_block(c, end);
3261 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003262}
3263
3264static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003265compiler_try(struct compiler *c, stmt_ty s) {
3266 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
3267 return compiler_try_finally(c, s);
3268 else
3269 return compiler_try_except(c, s);
3270}
3271
3272
3273static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003274compiler_import_as(struct compiler *c, identifier name, identifier asname)
3275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003276 /* The IMPORT_NAME opcode was already generated. This function
3277 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 If there is a dot in name, we need to split it and emit a
Serhiy Storchakaf93234b2017-05-09 22:31:05 +03003280 IMPORT_FROM for each name.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003281 */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003282 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
3283 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003284 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003285 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003286 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003287 /* Consume the base module name to get the first attribute */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003288 while (1) {
3289 Py_ssize_t pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290 PyObject *attr;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003291 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003292 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003293 return 0;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003294 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003295 if (!attr)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003296 return 0;
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003297 ADDOP_N(c, IMPORT_FROM, attr, names);
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003298 if (dot == -1) {
3299 break;
3300 }
3301 ADDOP(c, ROT_TWO);
3302 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003303 }
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003304 if (!compiler_nameop(c, asname, Store)) {
3305 return 0;
3306 }
3307 ADDOP(c, POP_TOP);
3308 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003309 }
3310 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003311}
3312
3313static int
3314compiler_import(struct compiler *c, stmt_ty s)
3315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 /* The Import node stores a module name like a.b.c as a single
3317 string. This is convenient for all cases except
3318 import a.b.c as d
3319 where we need to parse that string to extract the individual
3320 module names.
3321 XXX Perhaps change the representation to make this case simpler?
3322 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01003323 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003324
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003325 PyObject *zero = _PyLong_GetZero(); // borrowed reference
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 for (i = 0; i < n; i++) {
3327 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
3328 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003329
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003330 ADDOP_LOAD_CONST(c, zero);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003331 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003332 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003334 if (alias->asname) {
3335 r = compiler_import_as(c, alias->name, alias->asname);
3336 if (!r)
3337 return r;
3338 }
3339 else {
3340 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003341 Py_ssize_t dot = PyUnicode_FindChar(
3342 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02003343 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003344 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02003345 if (tmp == NULL)
3346 return 0;
3347 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003348 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003349 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350 Py_DECREF(tmp);
3351 }
3352 if (!r)
3353 return r;
3354 }
3355 }
3356 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003357}
3358
3359static int
3360compiler_from_import(struct compiler *c, stmt_ty s)
3361{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003362 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003363 PyObject *names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003364 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003366 if (!empty_string) {
3367 empty_string = PyUnicode_FromString("");
3368 if (!empty_string)
3369 return 0;
3370 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003371
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003372 ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02003373
3374 names = PyTuple_New(n);
3375 if (!names)
3376 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003378 /* build up the names */
3379 for (i = 0; i < n; i++) {
3380 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3381 Py_INCREF(alias->name);
3382 PyTuple_SET_ITEM(names, i, alias->name);
3383 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003385 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003386 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003387 Py_DECREF(names);
3388 return compiler_error(c, "from __future__ imports must occur "
3389 "at the beginning of the file");
3390 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003391 ADDOP_LOAD_CONST_NEW(c, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003393 if (s->v.ImportFrom.module) {
3394 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
3395 }
3396 else {
3397 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
3398 }
3399 for (i = 0; i < n; i++) {
3400 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3401 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003402
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003403 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003404 assert(n == 1);
3405 ADDOP(c, IMPORT_STAR);
3406 return 1;
3407 }
3408
3409 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
3410 store_name = alias->name;
3411 if (alias->asname)
3412 store_name = alias->asname;
3413
3414 if (!compiler_nameop(c, store_name, Store)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003415 return 0;
3416 }
3417 }
3418 /* remove imported module */
3419 ADDOP(c, POP_TOP);
3420 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003421}
3422
3423static int
3424compiler_assert(struct compiler *c, stmt_ty s)
3425{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003427
tsukasa-aua8ef4572021-03-16 22:14:41 +11003428 /* Always emit a warning if the test is a non-zero length tuple */
3429 if ((s->v.Assert.test->kind == Tuple_kind &&
3430 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) ||
3431 (s->v.Assert.test->kind == Constant_kind &&
3432 PyTuple_Check(s->v.Assert.test->v.Constant.value) &&
3433 PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0))
Serhiy Storchakad31e7732018-10-21 10:09:39 +03003434 {
3435 if (!compiler_warn(c, "assertion is always true, "
3436 "perhaps remove parentheses?"))
3437 {
Victor Stinner14e461d2013-08-26 22:28:21 +02003438 return 0;
3439 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003440 }
tsukasa-aua8ef4572021-03-16 22:14:41 +11003441 if (c->c_optimize)
3442 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003443 end = compiler_new_block(c);
3444 if (end == NULL)
3445 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003446 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
3447 return 0;
Zackery Spytzce6a0702019-08-25 03:44:09 -06003448 ADDOP(c, LOAD_ASSERTION_ERROR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003449 if (s->v.Assert.msg) {
3450 VISIT(c, expr, s->v.Assert.msg);
3451 ADDOP_I(c, CALL_FUNCTION, 1);
3452 }
3453 ADDOP_I(c, RAISE_VARARGS, 1);
3454 compiler_use_next_block(c, end);
3455 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003456}
3457
3458static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003459compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3460{
3461 if (c->c_interactive && c->c_nestlevel <= 1) {
3462 VISIT(c, expr, value);
3463 ADDOP(c, PRINT_EXPR);
3464 return 1;
3465 }
3466
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003467 if (value->kind == Constant_kind) {
Victor Stinner15a30952016-02-08 22:45:06 +01003468 /* ignore constant statement */
Mark Shannon877df852020-11-12 09:43:29 +00003469 ADDOP(c, NOP);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003470 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003471 }
3472
3473 VISIT(c, expr, value);
Mark Shannonc5440932021-03-15 14:24:25 +00003474 /* Mark POP_TOP as artificial */
3475 c->u->u_lineno = -1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003476 ADDOP(c, POP_TOP);
3477 return 1;
3478}
3479
3480static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003481compiler_visit_stmt(struct compiler *c, stmt_ty s)
3482{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003483 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 /* Always assign a lineno to the next instruction for a stmt. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003486 SET_LOC(c, s);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 switch (s->kind) {
3489 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04003490 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003491 case ClassDef_kind:
3492 return compiler_class(c, s);
3493 case Return_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003494 return compiler_return(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003495 case Delete_kind:
3496 VISIT_SEQ(c, expr, s->v.Delete.targets)
3497 break;
3498 case Assign_kind:
3499 n = asdl_seq_LEN(s->v.Assign.targets);
3500 VISIT(c, expr, s->v.Assign.value);
3501 for (i = 0; i < n; i++) {
3502 if (i < n - 1)
3503 ADDOP(c, DUP_TOP);
3504 VISIT(c, expr,
3505 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3506 }
3507 break;
3508 case AugAssign_kind:
3509 return compiler_augassign(c, s);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003510 case AnnAssign_kind:
3511 return compiler_annassign(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003512 case For_kind:
3513 return compiler_for(c, s);
3514 case While_kind:
3515 return compiler_while(c, s);
3516 case If_kind:
3517 return compiler_if(c, s);
Brandt Bucher145bf262021-02-26 14:51:55 -08003518 case Match_kind:
3519 return compiler_match(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003520 case Raise_kind:
3521 n = 0;
3522 if (s->v.Raise.exc) {
3523 VISIT(c, expr, s->v.Raise.exc);
3524 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003525 if (s->v.Raise.cause) {
3526 VISIT(c, expr, s->v.Raise.cause);
3527 n++;
3528 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003529 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01003530 ADDOP_I(c, RAISE_VARARGS, (int)n);
Mark Shannon266b4622020-11-17 19:30:14 +00003531 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003532 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003533 case Try_kind:
3534 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003535 case Assert_kind:
3536 return compiler_assert(c, s);
3537 case Import_kind:
3538 return compiler_import(c, s);
3539 case ImportFrom_kind:
3540 return compiler_from_import(c, s);
3541 case Global_kind:
3542 case Nonlocal_kind:
3543 break;
3544 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003545 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003546 case Pass_kind:
Mark Shannon877df852020-11-12 09:43:29 +00003547 ADDOP(c, NOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 break;
3549 case Break_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003550 return compiler_break(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003551 case Continue_kind:
3552 return compiler_continue(c);
3553 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003554 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04003555 case AsyncFunctionDef_kind:
3556 return compiler_function(c, s, 1);
3557 case AsyncWith_kind:
3558 return compiler_async_with(c, s, 0);
3559 case AsyncFor_kind:
3560 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003561 }
Yury Selivanov75445082015-05-11 22:57:16 -04003562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003563 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003564}
3565
3566static int
3567unaryop(unaryop_ty op)
3568{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003569 switch (op) {
3570 case Invert:
3571 return UNARY_INVERT;
3572 case Not:
3573 return UNARY_NOT;
3574 case UAdd:
3575 return UNARY_POSITIVE;
3576 case USub:
3577 return UNARY_NEGATIVE;
3578 default:
3579 PyErr_Format(PyExc_SystemError,
3580 "unary op %d should not be possible", op);
3581 return 0;
3582 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003583}
3584
3585static int
Andy Lester76d58772020-03-10 21:18:12 -05003586binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003587{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003588 switch (op) {
3589 case Add:
3590 return BINARY_ADD;
3591 case Sub:
3592 return BINARY_SUBTRACT;
3593 case Mult:
3594 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003595 case MatMult:
3596 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003597 case Div:
3598 return BINARY_TRUE_DIVIDE;
3599 case Mod:
3600 return BINARY_MODULO;
3601 case Pow:
3602 return BINARY_POWER;
3603 case LShift:
3604 return BINARY_LSHIFT;
3605 case RShift:
3606 return BINARY_RSHIFT;
3607 case BitOr:
3608 return BINARY_OR;
3609 case BitXor:
3610 return BINARY_XOR;
3611 case BitAnd:
3612 return BINARY_AND;
3613 case FloorDiv:
3614 return BINARY_FLOOR_DIVIDE;
3615 default:
3616 PyErr_Format(PyExc_SystemError,
3617 "binary op %d should not be possible", op);
3618 return 0;
3619 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003620}
3621
3622static int
Andy Lester76d58772020-03-10 21:18:12 -05003623inplace_binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003624{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003625 switch (op) {
3626 case Add:
3627 return INPLACE_ADD;
3628 case Sub:
3629 return INPLACE_SUBTRACT;
3630 case Mult:
3631 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003632 case MatMult:
3633 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003634 case Div:
3635 return INPLACE_TRUE_DIVIDE;
3636 case Mod:
3637 return INPLACE_MODULO;
3638 case Pow:
3639 return INPLACE_POWER;
3640 case LShift:
3641 return INPLACE_LSHIFT;
3642 case RShift:
3643 return INPLACE_RSHIFT;
3644 case BitOr:
3645 return INPLACE_OR;
3646 case BitXor:
3647 return INPLACE_XOR;
3648 case BitAnd:
3649 return INPLACE_AND;
3650 case FloorDiv:
3651 return INPLACE_FLOOR_DIVIDE;
3652 default:
3653 PyErr_Format(PyExc_SystemError,
3654 "inplace binary op %d should not be possible", op);
3655 return 0;
3656 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003657}
3658
3659static int
3660compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3661{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003662 int op, scope;
3663 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003664 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003666 PyObject *dict = c->u->u_names;
3667 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003668
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003669 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3670 !_PyUnicode_EqualToASCIIString(name, "True") &&
3671 !_PyUnicode_EqualToASCIIString(name, "False"));
Benjamin Peterson70b224d2012-12-06 17:49:58 -05003672
Pablo Galindoc5fc1562020-04-22 23:29:27 +01003673 if (forbidden_name(c, name, ctx))
3674 return 0;
3675
Serhiy Storchakabd6ec4d2017-12-18 14:29:12 +02003676 mangled = _Py_Mangle(c->u->u_private, name);
3677 if (!mangled)
3678 return 0;
3679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003680 op = 0;
3681 optype = OP_NAME;
Victor Stinner28ad12f2021-03-19 12:41:49 +01003682 scope = _PyST_GetScope(c->u->u_ste, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003683 switch (scope) {
3684 case FREE:
3685 dict = c->u->u_freevars;
3686 optype = OP_DEREF;
3687 break;
3688 case CELL:
3689 dict = c->u->u_cellvars;
3690 optype = OP_DEREF;
3691 break;
3692 case LOCAL:
3693 if (c->u->u_ste->ste_type == FunctionBlock)
3694 optype = OP_FAST;
3695 break;
3696 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04003697 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003698 optype = OP_GLOBAL;
3699 break;
3700 case GLOBAL_EXPLICIT:
3701 optype = OP_GLOBAL;
3702 break;
3703 default:
3704 /* scope can be 0 */
3705 break;
3706 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003708 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003709 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 switch (optype) {
3712 case OP_DEREF:
3713 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003714 case Load:
3715 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3716 break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003717 case Store: op = STORE_DEREF; break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003718 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003719 }
3720 break;
3721 case OP_FAST:
3722 switch (ctx) {
3723 case Load: op = LOAD_FAST; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003724 case Store: op = STORE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003725 case Del: op = DELETE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003726 }
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003727 ADDOP_N(c, op, mangled, varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 return 1;
3729 case OP_GLOBAL:
3730 switch (ctx) {
3731 case Load: op = LOAD_GLOBAL; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003732 case Store: op = STORE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003733 case Del: op = DELETE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003734 }
3735 break;
3736 case OP_NAME:
3737 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00003738 case Load: op = LOAD_NAME; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003739 case Store: op = STORE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003740 case Del: op = DELETE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003741 }
3742 break;
3743 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003745 assert(op);
Andy Lester76d58772020-03-10 21:18:12 -05003746 arg = compiler_add_o(dict, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003747 Py_DECREF(mangled);
3748 if (arg < 0)
3749 return 0;
3750 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003751}
3752
3753static int
3754compiler_boolop(struct compiler *c, expr_ty e)
3755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003756 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003757 int jumpi;
3758 Py_ssize_t i, n;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003759 asdl_expr_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003761 assert(e->kind == BoolOp_kind);
3762 if (e->v.BoolOp.op == And)
3763 jumpi = JUMP_IF_FALSE_OR_POP;
3764 else
3765 jumpi = JUMP_IF_TRUE_OR_POP;
3766 end = compiler_new_block(c);
3767 if (end == NULL)
3768 return 0;
3769 s = e->v.BoolOp.values;
3770 n = asdl_seq_LEN(s) - 1;
3771 assert(n >= 0);
3772 for (i = 0; i < n; ++i) {
3773 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003774 ADDOP_JUMP(c, jumpi, end);
Mark Shannon6e8128f2020-07-30 10:03:00 +01003775 basicblock *next = compiler_new_block(c);
3776 if (next == NULL) {
3777 return 0;
3778 }
3779 compiler_use_next_block(c, next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003780 }
3781 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3782 compiler_use_next_block(c, end);
3783 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003784}
3785
3786static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003787starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
Mark Shannon13bc1392020-01-23 09:25:17 +00003788 int build, int add, int extend, int tuple)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003789{
3790 Py_ssize_t n = asdl_seq_LEN(elts);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003791 if (n > 2 && are_all_items_const(elts, 0, n)) {
3792 PyObject *folded = PyTuple_New(n);
3793 if (folded == NULL) {
3794 return 0;
3795 }
3796 PyObject *val;
Mark Shannon11e0b292021-04-15 14:28:56 +01003797 for (Py_ssize_t i = 0; i < n; i++) {
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003798 val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
3799 Py_INCREF(val);
3800 PyTuple_SET_ITEM(folded, i, val);
3801 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003802 if (tuple) {
3803 ADDOP_LOAD_CONST_NEW(c, folded);
3804 } else {
3805 if (add == SET_ADD) {
3806 Py_SETREF(folded, PyFrozenSet_New(folded));
3807 if (folded == NULL) {
3808 return 0;
3809 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003810 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003811 ADDOP_I(c, build, pushed);
3812 ADDOP_LOAD_CONST_NEW(c, folded);
3813 ADDOP_I(c, extend, 1);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003814 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003815 return 1;
3816 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003817
Mark Shannon11e0b292021-04-15 14:28:56 +01003818 int big = n+pushed > STACK_USE_GUIDELINE;
3819 int seen_star = 0;
3820 for (Py_ssize_t i = 0; i < n; i++) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003821 expr_ty elt = asdl_seq_GET(elts, i);
3822 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003823 seen_star = 1;
3824 }
3825 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003826 if (!seen_star && !big) {
3827 for (Py_ssize_t i = 0; i < n; i++) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003828 expr_ty elt = asdl_seq_GET(elts, i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003829 VISIT(c, expr, elt);
Mark Shannon13bc1392020-01-23 09:25:17 +00003830 }
3831 if (tuple) {
3832 ADDOP_I(c, BUILD_TUPLE, n+pushed);
3833 } else {
3834 ADDOP_I(c, build, n+pushed);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003835 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003836 return 1;
3837 }
3838 int sequence_built = 0;
3839 if (big) {
3840 ADDOP_I(c, build, pushed);
3841 sequence_built = 1;
3842 }
3843 for (Py_ssize_t i = 0; i < n; i++) {
3844 expr_ty elt = asdl_seq_GET(elts, i);
3845 if (elt->kind == Starred_kind) {
3846 if (sequence_built == 0) {
3847 ADDOP_I(c, build, i+pushed);
3848 sequence_built = 1;
3849 }
3850 VISIT(c, expr, elt->v.Starred.value);
3851 ADDOP_I(c, extend, 1);
3852 }
3853 else {
3854 VISIT(c, expr, elt);
3855 if (sequence_built) {
3856 ADDOP_I(c, add, 1);
3857 }
3858 }
3859 }
3860 assert(sequence_built);
3861 if (tuple) {
3862 ADDOP(c, LIST_TO_TUPLE);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003863 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003864 return 1;
3865}
3866
3867static int
Brandt Bucher145bf262021-02-26 14:51:55 -08003868unpack_helper(struct compiler *c, asdl_expr_seq *elts)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003869{
3870 Py_ssize_t n = asdl_seq_LEN(elts);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003871 int seen_star = 0;
Brandt Bucher145bf262021-02-26 14:51:55 -08003872 for (Py_ssize_t i = 0; i < n; i++) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003873 expr_ty elt = asdl_seq_GET(elts, i);
3874 if (elt->kind == Starred_kind && !seen_star) {
3875 if ((i >= (1 << 8)) ||
3876 (n-i-1 >= (INT_MAX >> 8)))
3877 return compiler_error(c,
3878 "too many expressions in "
3879 "star-unpacking assignment");
3880 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3881 seen_star = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003882 }
3883 else if (elt->kind == Starred_kind) {
3884 return compiler_error(c,
Furkan Öndercb6534e2020-03-26 04:54:31 +03003885 "multiple starred expressions in assignment");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003886 }
3887 }
3888 if (!seen_star) {
3889 ADDOP_I(c, UNPACK_SEQUENCE, n);
3890 }
Brandt Bucher145bf262021-02-26 14:51:55 -08003891 return 1;
3892}
3893
3894static int
3895assignment_helper(struct compiler *c, asdl_expr_seq *elts)
3896{
3897 Py_ssize_t n = asdl_seq_LEN(elts);
3898 RETURN_IF_FALSE(unpack_helper(c, elts));
3899 for (Py_ssize_t i = 0; i < n; i++) {
Brandt Bucherd5aa2e92020-03-07 19:44:18 -08003900 expr_ty elt = asdl_seq_GET(elts, i);
3901 VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value);
3902 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003903 return 1;
3904}
3905
3906static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003907compiler_list(struct compiler *c, expr_ty e)
3908{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003909 asdl_expr_seq *elts = e->v.List.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003910 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003911 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003912 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003913 else if (e->v.List.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003914 return starunpack_helper(c, elts, 0, BUILD_LIST,
3915 LIST_APPEND, LIST_EXTEND, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003916 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003917 else
3918 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003919 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003920}
3921
3922static int
3923compiler_tuple(struct compiler *c, expr_ty e)
3924{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003925 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003926 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003927 return assignment_helper(c, elts);
3928 }
3929 else if (e->v.Tuple.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003930 return starunpack_helper(c, elts, 0, BUILD_LIST,
3931 LIST_APPEND, LIST_EXTEND, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003932 }
3933 else
3934 VISIT_SEQ(c, expr, elts);
3935 return 1;
3936}
3937
3938static int
3939compiler_set(struct compiler *c, expr_ty e)
3940{
Mark Shannon13bc1392020-01-23 09:25:17 +00003941 return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET,
3942 SET_ADD, SET_UPDATE, 0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003943}
3944
3945static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003946are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003947{
3948 Py_ssize_t i;
3949 for (i = begin; i < end; i++) {
3950 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003951 if (key == NULL || key->kind != Constant_kind)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003952 return 0;
3953 }
3954 return 1;
3955}
3956
3957static int
3958compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3959{
3960 Py_ssize_t i, n = end - begin;
3961 PyObject *keys, *key;
Mark Shannon11e0b292021-04-15 14:28:56 +01003962 int big = n*2 > STACK_USE_GUIDELINE;
3963 if (n > 1 && !big && are_all_items_const(e->v.Dict.keys, begin, end)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003964 for (i = begin; i < end; i++) {
3965 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3966 }
3967 keys = PyTuple_New(n);
3968 if (keys == NULL) {
3969 return 0;
3970 }
3971 for (i = begin; i < end; i++) {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003972 key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003973 Py_INCREF(key);
3974 PyTuple_SET_ITEM(keys, i - begin, key);
3975 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003976 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003977 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
Mark Shannon11e0b292021-04-15 14:28:56 +01003978 return 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003979 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003980 if (big) {
3981 ADDOP_I(c, BUILD_MAP, 0);
3982 }
3983 for (i = begin; i < end; i++) {
3984 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3985 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3986 if (big) {
3987 ADDOP_I(c, MAP_ADD, 1);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003988 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003989 }
3990 if (!big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003991 ADDOP_I(c, BUILD_MAP, n);
3992 }
3993 return 1;
3994}
3995
3996static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003997compiler_dict(struct compiler *c, expr_ty e)
3998{
Victor Stinner976bb402016-03-23 11:36:19 +01003999 Py_ssize_t i, n, elements;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004000 int have_dict;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004001 int is_unpacking = 0;
4002 n = asdl_seq_LEN(e->v.Dict.values);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004003 have_dict = 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004004 elements = 0;
4005 for (i = 0; i < n; i++) {
4006 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004007 if (is_unpacking) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004008 if (elements) {
4009 if (!compiler_subdict(c, e, i - elements, i)) {
4010 return 0;
4011 }
4012 if (have_dict) {
4013 ADDOP_I(c, DICT_UPDATE, 1);
4014 }
4015 have_dict = 1;
4016 elements = 0;
4017 }
4018 if (have_dict == 0) {
4019 ADDOP_I(c, BUILD_MAP, 0);
4020 have_dict = 1;
4021 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004022 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Mark Shannon8a4cd702020-01-27 09:57:45 +00004023 ADDOP_I(c, DICT_UPDATE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004024 }
4025 else {
Mark Shannon11e0b292021-04-15 14:28:56 +01004026 if (elements*2 > STACK_USE_GUIDELINE) {
Pablo Galindoc51db0e2020-08-13 09:48:41 +01004027 if (!compiler_subdict(c, e, i - elements, i + 1)) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004028 return 0;
4029 }
4030 if (have_dict) {
4031 ADDOP_I(c, DICT_UPDATE, 1);
4032 }
4033 have_dict = 1;
4034 elements = 0;
4035 }
4036 else {
4037 elements++;
4038 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004039 }
4040 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004041 if (elements) {
4042 if (!compiler_subdict(c, e, n - elements, n)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004043 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004044 }
4045 if (have_dict) {
4046 ADDOP_I(c, DICT_UPDATE, 1);
4047 }
4048 have_dict = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004049 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004050 if (!have_dict) {
4051 ADDOP_I(c, BUILD_MAP, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004052 }
4053 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004054}
4055
4056static int
4057compiler_compare(struct compiler *c, expr_ty e)
4058{
Victor Stinnerad9a0662013-11-19 22:23:20 +01004059 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004060
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02004061 if (!check_compare(c, e)) {
4062 return 0;
4063 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004064 VISIT(c, expr, e->v.Compare.left);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004065 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
4066 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
4067 if (n == 0) {
4068 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Mark Shannon9af0e472020-01-14 10:12:45 +00004069 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004070 }
4071 else {
4072 basicblock *cleanup = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004073 if (cleanup == NULL)
4074 return 0;
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004075 for (i = 0; i < n; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004076 VISIT(c, expr,
4077 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004078 ADDOP(c, DUP_TOP);
4079 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00004080 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01004081 ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004082 NEXT_BLOCK(c);
4083 }
4084 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00004085 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004086 basicblock *end = compiler_new_block(c);
4087 if (end == NULL)
4088 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00004089 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004090 compiler_use_next_block(c, cleanup);
4091 ADDOP(c, ROT_TWO);
4092 ADDOP(c, POP_TOP);
4093 compiler_use_next_block(c, end);
4094 }
4095 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004096}
4097
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004098static PyTypeObject *
4099infer_type(expr_ty e)
4100{
4101 switch (e->kind) {
4102 case Tuple_kind:
4103 return &PyTuple_Type;
4104 case List_kind:
4105 case ListComp_kind:
4106 return &PyList_Type;
4107 case Dict_kind:
4108 case DictComp_kind:
4109 return &PyDict_Type;
4110 case Set_kind:
4111 case SetComp_kind:
4112 return &PySet_Type;
4113 case GeneratorExp_kind:
4114 return &PyGen_Type;
4115 case Lambda_kind:
4116 return &PyFunction_Type;
4117 case JoinedStr_kind:
4118 case FormattedValue_kind:
4119 return &PyUnicode_Type;
4120 case Constant_kind:
Victor Stinnera102ed72020-02-07 02:24:48 +01004121 return Py_TYPE(e->v.Constant.value);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004122 default:
4123 return NULL;
4124 }
4125}
4126
4127static int
4128check_caller(struct compiler *c, expr_ty e)
4129{
4130 switch (e->kind) {
4131 case Constant_kind:
4132 case Tuple_kind:
4133 case List_kind:
4134 case ListComp_kind:
4135 case Dict_kind:
4136 case DictComp_kind:
4137 case Set_kind:
4138 case SetComp_kind:
4139 case GeneratorExp_kind:
4140 case JoinedStr_kind:
4141 case FormattedValue_kind:
4142 return compiler_warn(c, "'%.200s' object is not callable; "
4143 "perhaps you missed a comma?",
4144 infer_type(e)->tp_name);
4145 default:
4146 return 1;
4147 }
4148}
4149
4150static int
4151check_subscripter(struct compiler *c, expr_ty e)
4152{
4153 PyObject *v;
4154
4155 switch (e->kind) {
4156 case Constant_kind:
4157 v = e->v.Constant.value;
4158 if (!(v == Py_None || v == Py_Ellipsis ||
4159 PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
4160 PyAnySet_Check(v)))
4161 {
4162 return 1;
4163 }
4164 /* fall through */
4165 case Set_kind:
4166 case SetComp_kind:
4167 case GeneratorExp_kind:
4168 case Lambda_kind:
4169 return compiler_warn(c, "'%.200s' object is not subscriptable; "
4170 "perhaps you missed a comma?",
4171 infer_type(e)->tp_name);
4172 default:
4173 return 1;
4174 }
4175}
4176
4177static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004178check_index(struct compiler *c, expr_ty e, expr_ty s)
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004179{
4180 PyObject *v;
4181
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004182 PyTypeObject *index_type = infer_type(s);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004183 if (index_type == NULL
4184 || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
4185 || index_type == &PySlice_Type) {
4186 return 1;
4187 }
4188
4189 switch (e->kind) {
4190 case Constant_kind:
4191 v = e->v.Constant.value;
4192 if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
4193 return 1;
4194 }
4195 /* fall through */
4196 case Tuple_kind:
4197 case List_kind:
4198 case ListComp_kind:
4199 case JoinedStr_kind:
4200 case FormattedValue_kind:
4201 return compiler_warn(c, "%.200s indices must be integers or slices, "
4202 "not %.200s; "
4203 "perhaps you missed a comma?",
4204 infer_type(e)->tp_name,
4205 index_type->tp_name);
4206 default:
4207 return 1;
4208 }
4209}
4210
Zackery Spytz97f5de02019-03-22 01:30:32 -06004211// Return 1 if the method call was optimized, -1 if not, and 0 on error.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004212static int
Yury Selivanovf2392132016-12-13 19:03:51 -05004213maybe_optimize_method_call(struct compiler *c, expr_ty e)
4214{
4215 Py_ssize_t argsl, i;
4216 expr_ty meth = e->v.Call.func;
Pablo Galindoa5634c42020-09-16 19:42:00 +01004217 asdl_expr_seq *args = e->v.Call.args;
Yury Selivanovf2392132016-12-13 19:03:51 -05004218
4219 /* Check that the call node is an attribute access, and that
4220 the call doesn't have keyword parameters. */
4221 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
Mark Shannon11e0b292021-04-15 14:28:56 +01004222 asdl_seq_LEN(e->v.Call.keywords)) {
Yury Selivanovf2392132016-12-13 19:03:51 -05004223 return -1;
Mark Shannon11e0b292021-04-15 14:28:56 +01004224 }
4225 /* Check that there aren't too many arguments */
Yury Selivanovf2392132016-12-13 19:03:51 -05004226 argsl = asdl_seq_LEN(args);
Mark Shannon11e0b292021-04-15 14:28:56 +01004227 if (argsl >= STACK_USE_GUIDELINE) {
4228 return -1;
4229 }
4230 /* Check that there are no *varargs types of arguments. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004231 for (i = 0; i < argsl; i++) {
4232 expr_ty elt = asdl_seq_GET(args, i);
4233 if (elt->kind == Starred_kind) {
4234 return -1;
4235 }
4236 }
4237
4238 /* Alright, we can optimize the code. */
4239 VISIT(c, expr, meth->v.Attribute.value);
Mark Shannond48848c2021-03-14 18:01:30 +00004240 int old_lineno = c->u->u_lineno;
4241 c->u->u_lineno = meth->end_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004242 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
4243 VISIT_SEQ(c, expr, e->v.Call.args);
4244 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
Mark Shannond48848c2021-03-14 18:01:30 +00004245 c->u->u_lineno = old_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004246 return 1;
4247}
4248
4249static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004250validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
Zackery Spytz08050e92020-04-06 00:47:47 -06004251{
4252 Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
4253 for (Py_ssize_t i = 0; i < nkeywords; i++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004254 keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
4255 if (key->arg == NULL) {
4256 continue;
4257 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +01004258 if (forbidden_name(c, key->arg, Store)) {
4259 return -1;
4260 }
Zackery Spytz08050e92020-04-06 00:47:47 -06004261 for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004262 keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
4263 if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004264 c->u->u_col_offset = other->col_offset;
Brandt Bucher145bf262021-02-26 14:51:55 -08004265 compiler_error(c, "keyword argument repeated: %U", key->arg);
Pablo Galindo254ec782020-04-03 20:37:13 +01004266 return -1;
4267 }
4268 }
4269 }
4270 return 0;
4271}
4272
4273static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004274compiler_call(struct compiler *c, expr_ty e)
4275{
Zackery Spytz97f5de02019-03-22 01:30:32 -06004276 int ret = maybe_optimize_method_call(c, e);
4277 if (ret >= 0) {
4278 return ret;
4279 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004280 if (!check_caller(c, e->v.Call.func)) {
4281 return 0;
4282 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004283 VISIT(c, expr, e->v.Call.func);
4284 return compiler_call_helper(c, 0,
4285 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004286 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004287}
4288
Eric V. Smith235a6f02015-09-19 14:51:32 -04004289static int
4290compiler_joined_str(struct compiler *c, expr_ty e)
4291{
Mark Shannon11e0b292021-04-15 14:28:56 +01004292
4293 Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values);
4294 if (value_count > STACK_USE_GUIDELINE) {
4295 ADDOP_LOAD_CONST_NEW(c, _PyUnicode_FromASCII("", 0));
4296 PyObject *join = _PyUnicode_FromASCII("join", 4);
4297 if (join == NULL) {
4298 return 0;
4299 }
4300 ADDOP_NAME(c, LOAD_METHOD, join, names);
4301 Py_DECREF(join);
4302 ADDOP_I(c, BUILD_LIST, 0);
4303 for (Py_ssize_t i = 0; i < asdl_seq_LEN(e->v.JoinedStr.values); i++) {
4304 VISIT(c, expr, asdl_seq_GET(e->v.JoinedStr.values, i));
4305 ADDOP_I(c, LIST_APPEND, 1);
4306 }
4307 ADDOP_I(c, CALL_METHOD, 1);
4308 }
4309 else {
4310 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
4311 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) {
4312 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
4313 }
4314 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004315 return 1;
4316}
4317
Eric V. Smitha78c7952015-11-03 12:45:05 -05004318/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004319static int
4320compiler_formatted_value(struct compiler *c, expr_ty e)
4321{
Eric V. Smitha78c7952015-11-03 12:45:05 -05004322 /* Our oparg encodes 2 pieces of information: the conversion
4323 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04004324
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004325 Convert the conversion char to 3 bits:
4326 : 000 0x0 FVC_NONE The default if nothing specified.
Eric V. Smitha78c7952015-11-03 12:45:05 -05004327 !s : 001 0x1 FVC_STR
4328 !r : 010 0x2 FVC_REPR
4329 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04004330
Eric V. Smitha78c7952015-11-03 12:45:05 -05004331 next bit is whether or not we have a format spec:
4332 yes : 100 0x4
4333 no : 000 0x0
4334 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004335
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004336 int conversion = e->v.FormattedValue.conversion;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004337 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004338
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004339 /* The expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004340 VISIT(c, expr, e->v.FormattedValue.value);
4341
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004342 switch (conversion) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004343 case 's': oparg = FVC_STR; break;
4344 case 'r': oparg = FVC_REPR; break;
4345 case 'a': oparg = FVC_ASCII; break;
4346 case -1: oparg = FVC_NONE; break;
4347 default:
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004348 PyErr_Format(PyExc_SystemError,
4349 "Unrecognized conversion character %d", conversion);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004350 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004351 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004352 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004353 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004354 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004355 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004356 }
4357
Eric V. Smitha78c7952015-11-03 12:45:05 -05004358 /* And push our opcode and oparg */
4359 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004360
Eric V. Smith235a6f02015-09-19 14:51:32 -04004361 return 1;
4362}
4363
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004364static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004365compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004366{
4367 Py_ssize_t i, n = end - begin;
4368 keyword_ty kw;
4369 PyObject *keys, *key;
4370 assert(n > 0);
Mark Shannon11e0b292021-04-15 14:28:56 +01004371 int big = n*2 > STACK_USE_GUIDELINE;
4372 if (n > 1 && !big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004373 for (i = begin; i < end; i++) {
4374 kw = asdl_seq_GET(keywords, i);
4375 VISIT(c, expr, kw->value);
4376 }
4377 keys = PyTuple_New(n);
4378 if (keys == NULL) {
4379 return 0;
4380 }
4381 for (i = begin; i < end; i++) {
4382 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
4383 Py_INCREF(key);
4384 PyTuple_SET_ITEM(keys, i - begin, key);
4385 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004386 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004387 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
Mark Shannon11e0b292021-04-15 14:28:56 +01004388 return 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004389 }
Mark Shannon11e0b292021-04-15 14:28:56 +01004390 if (big) {
4391 ADDOP_I_NOLINE(c, BUILD_MAP, 0);
4392 }
4393 for (i = begin; i < end; i++) {
4394 kw = asdl_seq_GET(keywords, i);
4395 ADDOP_LOAD_CONST(c, kw->arg);
4396 VISIT(c, expr, kw->value);
4397 if (big) {
4398 ADDOP_I_NOLINE(c, MAP_ADD, 1);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004399 }
Mark Shannon11e0b292021-04-15 14:28:56 +01004400 }
4401 if (!big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004402 ADDOP_I(c, BUILD_MAP, n);
4403 }
4404 return 1;
4405}
4406
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004407/* shared code between compiler_call and compiler_class */
4408static int
4409compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01004410 int n, /* Args already pushed */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004411 asdl_expr_seq *args,
4412 asdl_keyword_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004413{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004414 Py_ssize_t i, nseen, nelts, nkwelts;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004415
Pablo Galindo254ec782020-04-03 20:37:13 +01004416 if (validate_keywords(c, keywords) == -1) {
4417 return 0;
4418 }
4419
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004420 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004421 nkwelts = asdl_seq_LEN(keywords);
4422
Mark Shannon11e0b292021-04-15 14:28:56 +01004423 if (nelts + nkwelts*2 > STACK_USE_GUIDELINE) {
4424 goto ex_call;
4425 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004426 for (i = 0; i < nelts; i++) {
4427 expr_ty elt = asdl_seq_GET(args, i);
4428 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00004429 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004430 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004431 }
4432 for (i = 0; i < nkwelts; i++) {
4433 keyword_ty kw = asdl_seq_GET(keywords, i);
4434 if (kw->arg == NULL) {
4435 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004436 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004437 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004438
Mark Shannon13bc1392020-01-23 09:25:17 +00004439 /* No * or ** args, so can use faster calling sequence */
4440 for (i = 0; i < nelts; i++) {
4441 expr_ty elt = asdl_seq_GET(args, i);
4442 assert(elt->kind != Starred_kind);
4443 VISIT(c, expr, elt);
4444 }
4445 if (nkwelts) {
4446 PyObject *names;
4447 VISIT_SEQ(c, keyword, keywords);
4448 names = PyTuple_New(nkwelts);
4449 if (names == NULL) {
4450 return 0;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004451 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004452 for (i = 0; i < nkwelts; i++) {
4453 keyword_ty kw = asdl_seq_GET(keywords, i);
4454 Py_INCREF(kw->arg);
4455 PyTuple_SET_ITEM(names, i, kw->arg);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004456 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004457 ADDOP_LOAD_CONST_NEW(c, names);
4458 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
4459 return 1;
4460 }
4461 else {
4462 ADDOP_I(c, CALL_FUNCTION, n + nelts);
4463 return 1;
4464 }
4465
4466ex_call:
4467
4468 /* Do positional arguments. */
4469 if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
4470 VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
4471 }
4472 else if (starunpack_helper(c, args, n, BUILD_LIST,
4473 LIST_APPEND, LIST_EXTEND, 1) == 0) {
4474 return 0;
4475 }
4476 /* Then keyword arguments */
4477 if (nkwelts) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004478 /* Has a new dict been pushed */
4479 int have_dict = 0;
Mark Shannon13bc1392020-01-23 09:25:17 +00004480
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004481 nseen = 0; /* the number of keyword arguments on the stack following */
4482 for (i = 0; i < nkwelts; i++) {
4483 keyword_ty kw = asdl_seq_GET(keywords, i);
4484 if (kw->arg == NULL) {
4485 /* A keyword argument unpacking. */
4486 if (nseen) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004487 if (!compiler_subkwargs(c, keywords, i - nseen, i)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004488 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004489 }
Mark Shannondb64f122020-06-01 10:42:42 +01004490 if (have_dict) {
4491 ADDOP_I(c, DICT_MERGE, 1);
4492 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004493 have_dict = 1;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004494 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004495 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004496 if (!have_dict) {
4497 ADDOP_I(c, BUILD_MAP, 0);
4498 have_dict = 1;
4499 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004500 VISIT(c, expr, kw->value);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004501 ADDOP_I(c, DICT_MERGE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004502 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004503 else {
4504 nseen++;
4505 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004506 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004507 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004508 /* Pack up any trailing keyword arguments. */
Mark Shannon8a4cd702020-01-27 09:57:45 +00004509 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004510 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004511 }
4512 if (have_dict) {
4513 ADDOP_I(c, DICT_MERGE, 1);
4514 }
4515 have_dict = 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004516 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004517 assert(have_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004518 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004519 ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0);
4520 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004521}
4522
Nick Coghlan650f0d02007-04-15 12:05:43 +00004523
4524/* List and set comprehensions and generator expressions work by creating a
4525 nested function to perform the actual iteration. This means that the
4526 iteration variables don't leak into the current scope.
4527 The defined function is called immediately following its definition, with the
4528 result of that call being the result of the expression.
4529 The LC/SC version returns the populated container, while the GE version is
4530 flagged in symtable.c as a generator, so it returns the generator object
4531 when the function is called.
Nick Coghlan650f0d02007-04-15 12:05:43 +00004532
4533 Possible cleanups:
4534 - iterate over the generator sequence instead of using recursion
4535*/
4536
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004537
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004538static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004539compiler_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004540 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004541 int depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004542 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004543{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004544 comprehension_ty gen;
4545 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4546 if (gen->is_async) {
4547 return compiler_async_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004548 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004549 } else {
4550 return compiler_sync_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004551 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004552 }
4553}
4554
4555static int
4556compiler_sync_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004557 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004558 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004559 expr_ty elt, expr_ty val, int type)
4560{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004561 /* generate code for the iterator, then each of the ifs,
4562 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004564 comprehension_ty gen;
4565 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004566 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004568 start = compiler_new_block(c);
4569 skip = compiler_new_block(c);
4570 if_cleanup = compiler_new_block(c);
4571 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004573 if (start == NULL || skip == NULL || if_cleanup == NULL ||
4574 anchor == NULL)
4575 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004577 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004579 if (gen_index == 0) {
4580 /* Receive outermost iter as an implicit argument */
4581 c->u->u_argcount = 1;
4582 ADDOP_I(c, LOAD_FAST, 0);
4583 }
4584 else {
4585 /* Sub-iter - calculate on the fly */
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004586 /* Fast path for the temporary variable assignment idiom:
4587 for y in [f(x)]
4588 */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004589 asdl_expr_seq *elts;
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004590 switch (gen->iter->kind) {
4591 case List_kind:
4592 elts = gen->iter->v.List.elts;
4593 break;
4594 case Tuple_kind:
4595 elts = gen->iter->v.Tuple.elts;
4596 break;
4597 default:
4598 elts = NULL;
4599 }
4600 if (asdl_seq_LEN(elts) == 1) {
4601 expr_ty elt = asdl_seq_GET(elts, 0);
4602 if (elt->kind != Starred_kind) {
4603 VISIT(c, expr, elt);
4604 start = NULL;
4605 }
4606 }
4607 if (start) {
4608 VISIT(c, expr, gen->iter);
4609 ADDOP(c, GET_ITER);
4610 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004611 }
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004612 if (start) {
4613 depth++;
4614 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01004615 ADDOP_JUMP(c, FOR_ITER, anchor);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004616 NEXT_BLOCK(c);
4617 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004618 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004620 /* XXX this needs to be cleaned up...a lot! */
4621 n = asdl_seq_LEN(gen->ifs);
4622 for (i = 0; i < n; i++) {
4623 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004624 if (!compiler_jump_if(c, e, if_cleanup, 0))
4625 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004626 NEXT_BLOCK(c);
4627 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004629 if (++gen_index < asdl_seq_LEN(generators))
4630 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004631 generators, gen_index, depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004632 elt, val, type))
4633 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004635 /* only append after the last for generator */
4636 if (gen_index >= asdl_seq_LEN(generators)) {
4637 /* comprehension specific code */
4638 switch (type) {
4639 case COMP_GENEXP:
4640 VISIT(c, expr, elt);
4641 ADDOP(c, YIELD_VALUE);
4642 ADDOP(c, POP_TOP);
4643 break;
4644 case COMP_LISTCOMP:
4645 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004646 ADDOP_I(c, LIST_APPEND, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004647 break;
4648 case COMP_SETCOMP:
4649 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004650 ADDOP_I(c, SET_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004651 break;
4652 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004653 /* With '{k: v}', k is evaluated before v, so we do
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004654 the same. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004655 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004656 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004657 ADDOP_I(c, MAP_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004658 break;
4659 default:
4660 return 0;
4661 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004663 compiler_use_next_block(c, skip);
4664 }
4665 compiler_use_next_block(c, if_cleanup);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004666 if (start) {
Mark Shannon582aaf12020-08-04 17:30:11 +01004667 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004668 compiler_use_next_block(c, anchor);
4669 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004670
4671 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004672}
4673
4674static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004675compiler_async_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004676 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004677 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004678 expr_ty elt, expr_ty val, int type)
4679{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004680 comprehension_ty gen;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004681 basicblock *start, *if_cleanup, *except;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004682 Py_ssize_t i, n;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004683 start = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004684 except = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004685 if_cleanup = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004686
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004687 if (start == NULL || if_cleanup == NULL || except == NULL) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004688 return 0;
4689 }
4690
4691 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4692
4693 if (gen_index == 0) {
4694 /* Receive outermost iter as an implicit argument */
4695 c->u->u_argcount = 1;
4696 ADDOP_I(c, LOAD_FAST, 0);
4697 }
4698 else {
4699 /* Sub-iter - calculate on the fly */
4700 VISIT(c, expr, gen->iter);
4701 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004702 }
4703
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004704 compiler_use_next_block(c, start);
tomKPZ7a7ba3d2021-04-07 07:43:45 -07004705 /* Runtime will push a block here, so we need to account for that */
4706 if (!compiler_push_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start,
4707 NULL, NULL)) {
4708 return 0;
4709 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004710
Mark Shannon582aaf12020-08-04 17:30:11 +01004711 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004712 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004713 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004714 ADDOP(c, YIELD_FROM);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004715 ADDOP(c, POP_BLOCK);
Serhiy Storchaka24d32012018-03-10 18:22:34 +02004716 VISIT(c, expr, gen->target);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004717
4718 n = asdl_seq_LEN(gen->ifs);
4719 for (i = 0; i < n; i++) {
4720 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004721 if (!compiler_jump_if(c, e, if_cleanup, 0))
4722 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004723 NEXT_BLOCK(c);
4724 }
4725
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004726 depth++;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004727 if (++gen_index < asdl_seq_LEN(generators))
4728 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004729 generators, gen_index, depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004730 elt, val, type))
4731 return 0;
4732
4733 /* only append after the last for generator */
4734 if (gen_index >= asdl_seq_LEN(generators)) {
4735 /* comprehension specific code */
4736 switch (type) {
4737 case COMP_GENEXP:
4738 VISIT(c, expr, elt);
4739 ADDOP(c, YIELD_VALUE);
4740 ADDOP(c, POP_TOP);
4741 break;
4742 case COMP_LISTCOMP:
4743 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004744 ADDOP_I(c, LIST_APPEND, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004745 break;
4746 case COMP_SETCOMP:
4747 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004748 ADDOP_I(c, SET_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004749 break;
4750 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004751 /* With '{k: v}', k is evaluated before v, so we do
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004752 the same. */
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004753 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004754 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004755 ADDOP_I(c, MAP_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004756 break;
4757 default:
4758 return 0;
4759 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004760 }
4761 compiler_use_next_block(c, if_cleanup);
Mark Shannon582aaf12020-08-04 17:30:11 +01004762 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004763
tomKPZ7a7ba3d2021-04-07 07:43:45 -07004764 compiler_pop_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start);
4765
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004766 compiler_use_next_block(c, except);
4767 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004768
4769 return 1;
4770}
4771
4772static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004773compiler_comprehension(struct compiler *c, expr_ty e, int type,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004774 identifier name, asdl_comprehension_seq *generators, expr_ty elt,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004775 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004776{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004777 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004778 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004779 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004780 int is_async_generator = 0;
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004781 int top_level_await = IS_TOP_LEVEL_AWAIT(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004782
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004783
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004784 int is_async_function = c->u->u_ste->ste_coroutine;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004785
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004786 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004787 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4788 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004789 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004790 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004791 }
4792
4793 is_async_generator = c->u->u_ste->ste_coroutine;
4794
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004795 if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004796 compiler_error(c, "asynchronous comprehension outside of "
4797 "an asynchronous function");
4798 goto error_in_scope;
4799 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004801 if (type != COMP_GENEXP) {
4802 int op;
4803 switch (type) {
4804 case COMP_LISTCOMP:
4805 op = BUILD_LIST;
4806 break;
4807 case COMP_SETCOMP:
4808 op = BUILD_SET;
4809 break;
4810 case COMP_DICTCOMP:
4811 op = BUILD_MAP;
4812 break;
4813 default:
4814 PyErr_Format(PyExc_SystemError,
4815 "unknown comprehension type %d", type);
4816 goto error_in_scope;
4817 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004819 ADDOP_I(c, op, 0);
4820 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004821
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004822 if (!compiler_comprehension_generator(c, generators, 0, 0, elt,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004823 val, type))
4824 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004826 if (type != COMP_GENEXP) {
4827 ADDOP(c, RETURN_VALUE);
4828 }
4829
4830 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004831 qualname = c->u->u_qualname;
4832 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004833 compiler_exit_scope(c);
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004834 if (top_level_await && is_async_generator){
4835 c->u->u_ste->ste_coroutine = 1;
4836 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004837 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004838 goto error;
4839
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004840 if (!compiler_make_closure(c, co, 0, qualname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004841 goto error;
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004842 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004843 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004844 Py_DECREF(co);
4845
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004846 VISIT(c, expr, outermost->iter);
4847
4848 if (outermost->is_async) {
4849 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004850 } else {
4851 ADDOP(c, GET_ITER);
4852 }
4853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004854 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004855
4856 if (is_async_generator && type != COMP_GENEXP) {
4857 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004858 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004859 ADDOP(c, YIELD_FROM);
4860 }
4861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004862 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004863error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004864 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004865error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004866 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004867 Py_XDECREF(co);
4868 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004869}
4870
4871static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004872compiler_genexp(struct compiler *c, expr_ty e)
4873{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004874 static identifier name;
4875 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004876 name = PyUnicode_InternFromString("<genexpr>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004877 if (!name)
4878 return 0;
4879 }
4880 assert(e->kind == GeneratorExp_kind);
4881 return compiler_comprehension(c, e, COMP_GENEXP, name,
4882 e->v.GeneratorExp.generators,
4883 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004884}
4885
4886static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004887compiler_listcomp(struct compiler *c, expr_ty e)
4888{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004889 static identifier name;
4890 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004891 name = PyUnicode_InternFromString("<listcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004892 if (!name)
4893 return 0;
4894 }
4895 assert(e->kind == ListComp_kind);
4896 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4897 e->v.ListComp.generators,
4898 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004899}
4900
4901static int
4902compiler_setcomp(struct compiler *c, expr_ty e)
4903{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004904 static identifier name;
4905 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004906 name = PyUnicode_InternFromString("<setcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004907 if (!name)
4908 return 0;
4909 }
4910 assert(e->kind == SetComp_kind);
4911 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4912 e->v.SetComp.generators,
4913 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004914}
4915
4916
4917static int
4918compiler_dictcomp(struct compiler *c, expr_ty e)
4919{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004920 static identifier name;
4921 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004922 name = PyUnicode_InternFromString("<dictcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004923 if (!name)
4924 return 0;
4925 }
4926 assert(e->kind == DictComp_kind);
4927 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4928 e->v.DictComp.generators,
4929 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004930}
4931
4932
4933static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004934compiler_visit_keyword(struct compiler *c, keyword_ty k)
4935{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004936 VISIT(c, expr, k->value);
4937 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004938}
4939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004940/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004941 whether they are true or false.
4942
4943 Return values: 1 for true, 0 for false, -1 for non-constant.
4944 */
4945
4946static int
Mark Shannonfee55262019-11-21 09:11:43 +00004947compiler_with_except_finish(struct compiler *c) {
4948 basicblock *exit;
4949 exit = compiler_new_block(c);
4950 if (exit == NULL)
4951 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01004952 ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit);
Mark Shannon266b4622020-11-17 19:30:14 +00004953 NEXT_BLOCK(c);
Mark Shannonbf353f32020-12-17 13:55:28 +00004954 ADDOP_I(c, RERAISE, 1);
Mark Shannonfee55262019-11-21 09:11:43 +00004955 compiler_use_next_block(c, exit);
4956 ADDOP(c, POP_TOP);
4957 ADDOP(c, POP_TOP);
4958 ADDOP(c, POP_TOP);
4959 ADDOP(c, POP_EXCEPT);
4960 ADDOP(c, POP_TOP);
4961 return 1;
4962}
Yury Selivanov75445082015-05-11 22:57:16 -04004963
4964/*
4965 Implements the async with statement.
4966
4967 The semantics outlined in that PEP are as follows:
4968
4969 async with EXPR as VAR:
4970 BLOCK
4971
4972 It is implemented roughly as:
4973
4974 context = EXPR
4975 exit = context.__aexit__ # not calling it
4976 value = await context.__aenter__()
4977 try:
4978 VAR = value # if VAR present in the syntax
4979 BLOCK
4980 finally:
4981 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004982 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04004983 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004984 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04004985 if not (await exit(*exc)):
4986 raise
4987 */
4988static int
4989compiler_async_with(struct compiler *c, stmt_ty s, int pos)
4990{
Mark Shannonfee55262019-11-21 09:11:43 +00004991 basicblock *block, *final, *exit;
Yury Selivanov75445082015-05-11 22:57:16 -04004992 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
4993
4994 assert(s->kind == AsyncWith_kind);
Pablo Galindo90235812020-03-15 04:29:22 +00004995 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07004996 c->u->u_ste->ste_coroutine = 1;
4997 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
Zsolt Dollensteine2396502018-04-27 08:58:56 -07004998 return compiler_error(c, "'async with' outside async function");
4999 }
Yury Selivanov75445082015-05-11 22:57:16 -04005000
5001 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00005002 final = compiler_new_block(c);
5003 exit = compiler_new_block(c);
5004 if (!block || !final || !exit)
Yury Selivanov75445082015-05-11 22:57:16 -04005005 return 0;
5006
5007 /* Evaluate EXPR */
5008 VISIT(c, expr, item->context_expr);
5009
5010 ADDOP(c, BEFORE_ASYNC_WITH);
5011 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005012 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005013 ADDOP(c, YIELD_FROM);
5014
Mark Shannon582aaf12020-08-04 17:30:11 +01005015 ADDOP_JUMP(c, SETUP_ASYNC_WITH, final);
Yury Selivanov75445082015-05-11 22:57:16 -04005016
5017 /* SETUP_ASYNC_WITH pushes a finally block. */
5018 compiler_use_next_block(c, block);
Mark Shannon5979e812021-04-30 14:32:47 +01005019 if (!compiler_push_fblock(c, ASYNC_WITH, block, final, s)) {
Yury Selivanov75445082015-05-11 22:57:16 -04005020 return 0;
5021 }
5022
5023 if (item->optional_vars) {
5024 VISIT(c, expr, item->optional_vars);
5025 }
5026 else {
5027 /* Discard result from context.__aenter__() */
5028 ADDOP(c, POP_TOP);
5029 }
5030
5031 pos++;
5032 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
5033 /* BLOCK code */
5034 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
5035 else if (!compiler_async_with(c, s, pos))
5036 return 0;
5037
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005038 compiler_pop_fblock(c, ASYNC_WITH, block);
Mark Shannonfee55262019-11-21 09:11:43 +00005039 ADDOP(c, POP_BLOCK);
5040 /* End of body; start the cleanup */
Yury Selivanov75445082015-05-11 22:57:16 -04005041
Mark Shannonfee55262019-11-21 09:11:43 +00005042 /* For successful outcome:
5043 * call __exit__(None, None, None)
5044 */
Mark Shannon5979e812021-04-30 14:32:47 +01005045 SET_LOC(c, s);
Mark Shannonfee55262019-11-21 09:11:43 +00005046 if(!compiler_call_exit_with_nones(c))
Yury Selivanov75445082015-05-11 22:57:16 -04005047 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00005048 ADDOP(c, GET_AWAITABLE);
5049 ADDOP_O(c, LOAD_CONST, Py_None, consts);
5050 ADDOP(c, YIELD_FROM);
Yury Selivanov75445082015-05-11 22:57:16 -04005051
Mark Shannonfee55262019-11-21 09:11:43 +00005052 ADDOP(c, POP_TOP);
Yury Selivanov75445082015-05-11 22:57:16 -04005053
Mark Shannon582aaf12020-08-04 17:30:11 +01005054 ADDOP_JUMP(c, JUMP_ABSOLUTE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00005055
5056 /* For exceptional outcome: */
5057 compiler_use_next_block(c, final);
Mark Shannonfee55262019-11-21 09:11:43 +00005058 ADDOP(c, WITH_EXCEPT_START);
Yury Selivanov75445082015-05-11 22:57:16 -04005059 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005060 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005061 ADDOP(c, YIELD_FROM);
Mark Shannonfee55262019-11-21 09:11:43 +00005062 compiler_with_except_finish(c);
Yury Selivanov75445082015-05-11 22:57:16 -04005063
Mark Shannonfee55262019-11-21 09:11:43 +00005064compiler_use_next_block(c, exit);
Yury Selivanov75445082015-05-11 22:57:16 -04005065 return 1;
5066}
5067
5068
Guido van Rossumc2e20742006-02-27 22:32:47 +00005069/*
5070 Implements the with statement from PEP 343.
Guido van Rossumc2e20742006-02-27 22:32:47 +00005071 with EXPR as VAR:
5072 BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00005073 is implemented as:
5074 <code for EXPR>
5075 SETUP_WITH E
5076 <code to store to VAR> or POP_TOP
5077 <code for BLOCK>
5078 LOAD_CONST (None, None, None)
5079 CALL_FUNCTION_EX 0
5080 JUMP_FORWARD EXIT
5081 E: WITH_EXCEPT_START (calls EXPR.__exit__)
5082 POP_JUMP_IF_TRUE T:
5083 RERAISE
5084 T: POP_TOP * 3 (remove exception from stack)
5085 POP_EXCEPT
5086 POP_TOP
5087 EXIT:
Guido van Rossumc2e20742006-02-27 22:32:47 +00005088 */
Mark Shannonfee55262019-11-21 09:11:43 +00005089
Guido van Rossumc2e20742006-02-27 22:32:47 +00005090static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005091compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00005092{
Mark Shannonfee55262019-11-21 09:11:43 +00005093 basicblock *block, *final, *exit;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005094 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005095
5096 assert(s->kind == With_kind);
5097
Guido van Rossumc2e20742006-02-27 22:32:47 +00005098 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00005099 final = compiler_new_block(c);
5100 exit = compiler_new_block(c);
5101 if (!block || !final || !exit)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005102 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005103
Thomas Wouters477c8d52006-05-27 19:21:47 +00005104 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005105 VISIT(c, expr, item->context_expr);
Mark Shannonfee55262019-11-21 09:11:43 +00005106 /* Will push bound __exit__ */
Mark Shannon582aaf12020-08-04 17:30:11 +01005107 ADDOP_JUMP(c, SETUP_WITH, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005108
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005109 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00005110 compiler_use_next_block(c, block);
Mark Shannon5979e812021-04-30 14:32:47 +01005111 if (!compiler_push_fblock(c, WITH, block, final, s)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005112 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005113 }
5114
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005115 if (item->optional_vars) {
5116 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005117 }
5118 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005119 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005120 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005121 }
5122
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005123 pos++;
5124 if (pos == asdl_seq_LEN(s->v.With.items))
5125 /* BLOCK code */
5126 VISIT_SEQ(c, stmt, s->v.With.body)
5127 else if (!compiler_with(c, s, pos))
5128 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005129
Mark Shannon3bd60352021-01-13 12:05:43 +00005130
5131 /* Mark all following code as artificial */
5132 c->u->u_lineno = -1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005133 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005134 compiler_pop_fblock(c, WITH, block);
Mark Shannon13bc1392020-01-23 09:25:17 +00005135
Mark Shannonfee55262019-11-21 09:11:43 +00005136 /* End of body; start the cleanup. */
Mark Shannon13bc1392020-01-23 09:25:17 +00005137
Mark Shannonfee55262019-11-21 09:11:43 +00005138 /* For successful outcome:
5139 * call __exit__(None, None, None)
5140 */
Mark Shannon5979e812021-04-30 14:32:47 +01005141 SET_LOC(c, s);
Mark Shannonfee55262019-11-21 09:11:43 +00005142 if (!compiler_call_exit_with_nones(c))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005143 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00005144 ADDOP(c, POP_TOP);
Mark Shannon582aaf12020-08-04 17:30:11 +01005145 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005146
Mark Shannonfee55262019-11-21 09:11:43 +00005147 /* For exceptional outcome: */
5148 compiler_use_next_block(c, final);
Mark Shannonfee55262019-11-21 09:11:43 +00005149 ADDOP(c, WITH_EXCEPT_START);
5150 compiler_with_except_finish(c);
5151
5152 compiler_use_next_block(c, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005153 return 1;
5154}
5155
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005156static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005157compiler_visit_expr1(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005159 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07005160 case NamedExpr_kind:
5161 VISIT(c, expr, e->v.NamedExpr.value);
5162 ADDOP(c, DUP_TOP);
5163 VISIT(c, expr, e->v.NamedExpr.target);
5164 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005165 case BoolOp_kind:
5166 return compiler_boolop(c, e);
5167 case BinOp_kind:
5168 VISIT(c, expr, e->v.BinOp.left);
5169 VISIT(c, expr, e->v.BinOp.right);
Andy Lester76d58772020-03-10 21:18:12 -05005170 ADDOP(c, binop(e->v.BinOp.op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005171 break;
5172 case UnaryOp_kind:
5173 VISIT(c, expr, e->v.UnaryOp.operand);
5174 ADDOP(c, unaryop(e->v.UnaryOp.op));
5175 break;
5176 case Lambda_kind:
5177 return compiler_lambda(c, e);
5178 case IfExp_kind:
5179 return compiler_ifexp(c, e);
5180 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005181 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005182 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005183 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005184 case GeneratorExp_kind:
5185 return compiler_genexp(c, e);
5186 case ListComp_kind:
5187 return compiler_listcomp(c, e);
5188 case SetComp_kind:
5189 return compiler_setcomp(c, e);
5190 case DictComp_kind:
5191 return compiler_dictcomp(c, e);
5192 case Yield_kind:
5193 if (c->u->u_ste->ste_type != FunctionBlock)
5194 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005195 if (e->v.Yield.value) {
5196 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005197 }
5198 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005199 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005200 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005201 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005202 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005203 case YieldFrom_kind:
5204 if (c->u->u_ste->ste_type != FunctionBlock)
5205 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04005206
5207 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
5208 return compiler_error(c, "'yield from' inside async function");
5209
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005210 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04005211 ADDOP(c, GET_YIELD_FROM_ITER);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005212 ADDOP_LOAD_CONST(c, Py_None);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005213 ADDOP(c, YIELD_FROM);
5214 break;
Yury Selivanov75445082015-05-11 22:57:16 -04005215 case Await_kind:
Pablo Galindo90235812020-03-15 04:29:22 +00005216 if (!IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005217 if (c->u->u_ste->ste_type != FunctionBlock){
5218 return compiler_error(c, "'await' outside function");
5219 }
Yury Selivanov75445082015-05-11 22:57:16 -04005220
Victor Stinner331a6a52019-05-27 16:39:22 +02005221 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005222 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
5223 return compiler_error(c, "'await' outside async function");
5224 }
5225 }
Yury Selivanov75445082015-05-11 22:57:16 -04005226
5227 VISIT(c, expr, e->v.Await.value);
5228 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005229 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005230 ADDOP(c, YIELD_FROM);
5231 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005232 case Compare_kind:
5233 return compiler_compare(c, e);
5234 case Call_kind:
5235 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005236 case Constant_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005237 ADDOP_LOAD_CONST(c, e->v.Constant.value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005238 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005239 case JoinedStr_kind:
5240 return compiler_joined_str(c, e);
5241 case FormattedValue_kind:
5242 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005243 /* The following exprs can be assignment targets. */
5244 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005245 VISIT(c, expr, e->v.Attribute.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005246 switch (e->v.Attribute.ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005247 case Load:
Mark Shannond48848c2021-03-14 18:01:30 +00005248 {
5249 int old_lineno = c->u->u_lineno;
5250 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005251 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005252 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005253 break;
Mark Shannond48848c2021-03-14 18:01:30 +00005254 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005255 case Store:
Mark Shannond48848c2021-03-14 18:01:30 +00005256 if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005257 return 0;
Mark Shannond48848c2021-03-14 18:01:30 +00005258 }
5259 int old_lineno = c->u->u_lineno;
5260 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005261 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005262 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005263 break;
5264 case Del:
5265 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
5266 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005267 }
5268 break;
5269 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005270 return compiler_subscript(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005271 case Starred_kind:
5272 switch (e->v.Starred.ctx) {
5273 case Store:
5274 /* In all legitimate cases, the Starred node was already replaced
5275 * by compiler_list/compiler_tuple. XXX: is that okay? */
5276 return compiler_error(c,
5277 "starred assignment target must be in a list or tuple");
5278 default:
5279 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005280 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005281 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005282 break;
5283 case Slice_kind:
5284 return compiler_slice(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005285 case Name_kind:
5286 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
5287 /* child nodes of List and Tuple will have expr_context set */
5288 case List_kind:
5289 return compiler_list(c, e);
5290 case Tuple_kind:
5291 return compiler_tuple(c, e);
5292 }
5293 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005294}
5295
5296static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005297compiler_visit_expr(struct compiler *c, expr_ty e)
5298{
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005299 int old_lineno = c->u->u_lineno;
5300 int old_col_offset = c->u->u_col_offset;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005301 SET_LOC(c, e);
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005302 int res = compiler_visit_expr1(c, e);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005303 c->u->u_lineno = old_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005304 c->u->u_col_offset = old_col_offset;
5305 return res;
5306}
5307
5308static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005309compiler_augassign(struct compiler *c, stmt_ty s)
5310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005311 assert(s->kind == AugAssign_kind);
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005312 expr_ty e = s->v.AugAssign.target;
5313
5314 int old_lineno = c->u->u_lineno;
5315 int old_col_offset = c->u->u_col_offset;
5316 SET_LOC(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005318 switch (e->kind) {
5319 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005320 VISIT(c, expr, e->v.Attribute.value);
5321 ADDOP(c, DUP_TOP);
Mark Shannond48848c2021-03-14 18:01:30 +00005322 int old_lineno = c->u->u_lineno;
5323 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005324 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005325 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005326 break;
5327 case Subscript_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005328 VISIT(c, expr, e->v.Subscript.value);
5329 VISIT(c, expr, e->v.Subscript.slice);
5330 ADDOP(c, DUP_TOP_TWO);
5331 ADDOP(c, BINARY_SUBSCR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005332 break;
5333 case Name_kind:
5334 if (!compiler_nameop(c, e->v.Name.id, Load))
5335 return 0;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005336 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005337 default:
5338 PyErr_Format(PyExc_SystemError,
5339 "invalid node type (%d) for augmented assignment",
5340 e->kind);
5341 return 0;
5342 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005343
5344 c->u->u_lineno = old_lineno;
5345 c->u->u_col_offset = old_col_offset;
5346
5347 VISIT(c, expr, s->v.AugAssign.value);
5348 ADDOP(c, inplace_binop(s->v.AugAssign.op));
5349
5350 SET_LOC(c, e);
5351
5352 switch (e->kind) {
5353 case Attribute_kind:
Mark Shannond48848c2021-03-14 18:01:30 +00005354 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005355 ADDOP(c, ROT_TWO);
5356 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5357 break;
5358 case Subscript_kind:
5359 ADDOP(c, ROT_THREE);
5360 ADDOP(c, STORE_SUBSCR);
5361 break;
5362 case Name_kind:
5363 return compiler_nameop(c, e->v.Name.id, Store);
5364 default:
5365 Py_UNREACHABLE();
5366 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005367 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005368}
5369
5370static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005371check_ann_expr(struct compiler *c, expr_ty e)
5372{
5373 VISIT(c, expr, e);
5374 ADDOP(c, POP_TOP);
5375 return 1;
5376}
5377
5378static int
5379check_annotation(struct compiler *c, stmt_ty s)
5380{
Batuhan Taskaya8cc3cfa2021-04-25 05:31:20 +03005381 /* Annotations of complex targets does not produce anything
5382 under annotations future */
5383 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
5384 return 1;
5385 }
5386
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005387 /* Annotations are only evaluated in a module or class. */
5388 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5389 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
5390 return check_ann_expr(c, s->v.AnnAssign.annotation);
5391 }
5392 return 1;
5393}
5394
5395static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005396check_ann_subscr(struct compiler *c, expr_ty e)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005397{
5398 /* We check that everything in a subscript is defined at runtime. */
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005399 switch (e->kind) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005400 case Slice_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005401 if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005402 return 0;
5403 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005404 if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) {
5405 return 0;
5406 }
5407 if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) {
5408 return 0;
5409 }
5410 return 1;
5411 case Tuple_kind: {
5412 /* extended slice */
Pablo Galindoa5634c42020-09-16 19:42:00 +01005413 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005414 Py_ssize_t i, n = asdl_seq_LEN(elts);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005415 for (i = 0; i < n; i++) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005416 if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005417 return 0;
5418 }
5419 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005420 return 1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005421 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005422 default:
5423 return check_ann_expr(c, e);
5424 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005425}
5426
5427static int
5428compiler_annassign(struct compiler *c, stmt_ty s)
5429{
5430 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07005431 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005432
5433 assert(s->kind == AnnAssign_kind);
5434
5435 /* We perform the actual assignment first. */
5436 if (s->v.AnnAssign.value) {
5437 VISIT(c, expr, s->v.AnnAssign.value);
5438 VISIT(c, expr, targ);
5439 }
5440 switch (targ->kind) {
5441 case Name_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005442 if (forbidden_name(c, targ->v.Name.id, Store))
5443 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005444 /* If we have a simple name in a module or class, store annotation. */
5445 if (s->v.AnnAssign.simple &&
5446 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5447 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Pablo Galindob0544ba2021-04-21 12:41:19 +01005448 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
5449 VISIT(c, annexpr, s->v.AnnAssign.annotation)
5450 }
5451 else {
5452 VISIT(c, expr, s->v.AnnAssign.annotation);
5453 }
Mark Shannon332cd5e2018-01-30 00:41:04 +00005454 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02005455 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005456 ADDOP_LOAD_CONST_NEW(c, mangled);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005457 ADDOP(c, STORE_SUBSCR);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005458 }
5459 break;
5460 case Attribute_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005461 if (forbidden_name(c, targ->v.Attribute.attr, Store))
5462 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005463 if (!s->v.AnnAssign.value &&
5464 !check_ann_expr(c, targ->v.Attribute.value)) {
5465 return 0;
5466 }
5467 break;
5468 case Subscript_kind:
5469 if (!s->v.AnnAssign.value &&
5470 (!check_ann_expr(c, targ->v.Subscript.value) ||
5471 !check_ann_subscr(c, targ->v.Subscript.slice))) {
5472 return 0;
5473 }
5474 break;
5475 default:
5476 PyErr_Format(PyExc_SystemError,
5477 "invalid node type (%d) for annotated assignment",
5478 targ->kind);
5479 return 0;
5480 }
5481 /* Annotation is evaluated last. */
5482 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
5483 return 0;
5484 }
5485 return 1;
5486}
5487
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005488/* Raises a SyntaxError and returns 0.
5489 If something goes wrong, a different exception may be raised.
5490*/
5491
5492static int
Brandt Bucher145bf262021-02-26 14:51:55 -08005493compiler_error(struct compiler *c, const char *format, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005494{
Brandt Bucher145bf262021-02-26 14:51:55 -08005495 va_list vargs;
5496#ifdef HAVE_STDARG_PROTOTYPES
5497 va_start(vargs, format);
5498#else
5499 va_start(vargs);
5500#endif
5501 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5502 va_end(vargs);
5503 if (msg == NULL) {
5504 return 0;
5505 }
5506 PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
5507 if (loc == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005508 Py_INCREF(Py_None);
5509 loc = Py_None;
5510 }
Pablo Galindoa77aac42021-04-23 14:27:05 +01005511 PyObject *args = Py_BuildValue("O(OiiOii)", msg, c->c_filename,
5512 c->u->u_lineno, c->u->u_col_offset + 1, loc,
5513 c->u->u_end_lineno, c->u->u_end_col_offset + 1);
Brandt Bucher145bf262021-02-26 14:51:55 -08005514 Py_DECREF(msg);
5515 if (args == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005516 goto exit;
Brandt Bucher145bf262021-02-26 14:51:55 -08005517 }
5518 PyErr_SetObject(PyExc_SyntaxError, args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005519 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005520 Py_DECREF(loc);
Brandt Bucher145bf262021-02-26 14:51:55 -08005521 Py_XDECREF(args);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005522 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005523}
5524
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005525/* Emits a SyntaxWarning and returns 1 on success.
5526 If a SyntaxWarning raised as error, replaces it with a SyntaxError
5527 and returns 0.
5528*/
5529static int
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005530compiler_warn(struct compiler *c, const char *format, ...)
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005531{
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005532 va_list vargs;
5533#ifdef HAVE_STDARG_PROTOTYPES
5534 va_start(vargs, format);
5535#else
5536 va_start(vargs);
5537#endif
5538 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5539 va_end(vargs);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005540 if (msg == NULL) {
5541 return 0;
5542 }
5543 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
5544 c->u->u_lineno, NULL, NULL) < 0)
5545 {
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005546 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005547 /* Replace the SyntaxWarning exception with a SyntaxError
5548 to get a more accurate error report */
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005549 PyErr_Clear();
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005550 assert(PyUnicode_AsUTF8(msg) != NULL);
5551 compiler_error(c, PyUnicode_AsUTF8(msg));
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005552 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005553 Py_DECREF(msg);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005554 return 0;
5555 }
5556 Py_DECREF(msg);
5557 return 1;
5558}
5559
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005560static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005561compiler_subscript(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005562{
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005563 expr_context_ty ctx = e->v.Subscript.ctx;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005564 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005565
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005566 if (ctx == Load) {
5567 if (!check_subscripter(c, e->v.Subscript.value)) {
5568 return 0;
5569 }
5570 if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
5571 return 0;
5572 }
5573 }
5574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005575 switch (ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005576 case Load: op = BINARY_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005577 case Store: op = STORE_SUBSCR; break;
5578 case Del: op = DELETE_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005579 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005580 assert(op);
5581 VISIT(c, expr, e->v.Subscript.value);
5582 VISIT(c, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005583 ADDOP(c, op);
5584 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005585}
5586
5587static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005588compiler_slice(struct compiler *c, expr_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005589{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005590 int n = 2;
5591 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005593 /* only handles the cases where BUILD_SLICE is emitted */
5594 if (s->v.Slice.lower) {
5595 VISIT(c, expr, s->v.Slice.lower);
5596 }
5597 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005598 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005599 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005601 if (s->v.Slice.upper) {
5602 VISIT(c, expr, s->v.Slice.upper);
5603 }
5604 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005605 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005606 }
5607
5608 if (s->v.Slice.step) {
5609 n++;
5610 VISIT(c, expr, s->v.Slice.step);
5611 }
5612 ADDOP_I(c, BUILD_SLICE, n);
5613 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005614}
5615
Brandt Bucher145bf262021-02-26 14:51:55 -08005616
5617// PEP 634: Structural Pattern Matching
5618
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005619// To keep things simple, all compiler_pattern_* and pattern_helper_* routines
5620// follow the convention of consuming TOS (the subject for the given pattern)
5621// and calling jump_to_fail_pop on failure (no match).
5622
5623// When calling into these routines, it's important that pc->on_top be kept
5624// updated to reflect the current number of items that we are using on the top
5625// of the stack: they will be popped on failure, and any name captures will be
5626// stored *underneath* them on success. This lets us defer all names stores
5627// until the *entire* pattern matches.
Brandt Bucher145bf262021-02-26 14:51:55 -08005628
Brandt Bucher145bf262021-02-26 14:51:55 -08005629#define WILDCARD_CHECK(N) \
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005630 ((N)->kind == MatchAs_kind && !(N)->v.MatchAs.name)
Brandt Bucher145bf262021-02-26 14:51:55 -08005631
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005632#define WILDCARD_STAR_CHECK(N) \
5633 ((N)->kind == MatchStar_kind && !(N)->v.MatchStar.name)
5634
5635// Limit permitted subexpressions, even if the parser & AST validator let them through
5636#define MATCH_VALUE_EXPR(N) \
5637 ((N)->kind == Constant_kind || (N)->kind == Attribute_kind)
Brandt Bucher145bf262021-02-26 14:51:55 -08005638
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005639// Allocate or resize pc->fail_pop to allow for n items to be popped on failure.
5640static int
5641ensure_fail_pop(struct compiler *c, pattern_context *pc, Py_ssize_t n)
5642{
5643 Py_ssize_t size = n + 1;
5644 if (size <= pc->fail_pop_size) {
5645 return 1;
5646 }
5647 Py_ssize_t needed = sizeof(basicblock*) * size;
5648 basicblock **resized = PyObject_Realloc(pc->fail_pop, needed);
5649 if (resized == NULL) {
5650 PyErr_NoMemory();
5651 return 0;
5652 }
5653 pc->fail_pop = resized;
5654 while (pc->fail_pop_size < size) {
5655 basicblock *new_block;
5656 RETURN_IF_FALSE(new_block = compiler_new_block(c));
5657 pc->fail_pop[pc->fail_pop_size++] = new_block;
5658 }
5659 return 1;
5660}
5661
5662// Use op to jump to the correct fail_pop block.
5663static int
5664jump_to_fail_pop(struct compiler *c, pattern_context *pc, int op)
5665{
5666 // Pop any items on the top of the stack, plus any objects we were going to
5667 // capture on success:
5668 Py_ssize_t pops = pc->on_top + PyList_GET_SIZE(pc->stores);
5669 RETURN_IF_FALSE(ensure_fail_pop(c, pc, pops));
5670 ADDOP_JUMP(c, op, pc->fail_pop[pops]);
5671 NEXT_BLOCK(c);
5672 return 1;
5673}
5674
5675// Build all of the fail_pop blocks and reset fail_pop.
5676static int
5677emit_and_reset_fail_pop(struct compiler *c, pattern_context *pc)
5678{
5679 if (!pc->fail_pop_size) {
5680 assert(pc->fail_pop == NULL);
5681 NEXT_BLOCK(c);
5682 return 1;
5683 }
5684 while (--pc->fail_pop_size) {
5685 compiler_use_next_block(c, pc->fail_pop[pc->fail_pop_size]);
5686 if (!compiler_addop(c, POP_TOP)) {
5687 pc->fail_pop_size = 0;
5688 PyObject_Free(pc->fail_pop);
5689 pc->fail_pop = NULL;
5690 return 0;
5691 }
5692 }
5693 compiler_use_next_block(c, pc->fail_pop[0]);
5694 PyObject_Free(pc->fail_pop);
5695 pc->fail_pop = NULL;
5696 return 1;
5697}
5698
5699static int
5700compiler_error_duplicate_store(struct compiler *c, identifier n)
5701{
5702 return compiler_error(c, "multiple assignments to name %R in pattern", n);
5703}
5704
Brandt Bucher145bf262021-02-26 14:51:55 -08005705static int
5706pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc)
5707{
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005708 if (n == NULL) {
5709 ADDOP(c, POP_TOP);
5710 return 1;
5711 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005712 if (forbidden_name(c, n, Store)) {
5713 return 0;
5714 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005715 // Can't assign to the same name twice:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005716 int duplicate = PySequence_Contains(pc->stores, n);
5717 if (duplicate < 0) {
5718 return 0;
Brandt Bucher145bf262021-02-26 14:51:55 -08005719 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005720 if (duplicate) {
5721 return compiler_error_duplicate_store(c, n);
Brandt Bucher145bf262021-02-26 14:51:55 -08005722 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005723 // Rotate this object underneath any items we need to preserve:
5724 ADDOP_I(c, ROT_N, pc->on_top + PyList_GET_SIZE(pc->stores) + 1);
5725 return !PyList_Append(pc->stores, n);
Brandt Bucher145bf262021-02-26 14:51:55 -08005726}
5727
5728
5729static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005730pattern_unpack_helper(struct compiler *c, asdl_pattern_seq *elts)
5731{
5732 Py_ssize_t n = asdl_seq_LEN(elts);
5733 int seen_star = 0;
5734 for (Py_ssize_t i = 0; i < n; i++) {
5735 pattern_ty elt = asdl_seq_GET(elts, i);
5736 if (elt->kind == MatchStar_kind && !seen_star) {
5737 if ((i >= (1 << 8)) ||
5738 (n-i-1 >= (INT_MAX >> 8)))
5739 return compiler_error(c,
5740 "too many expressions in "
5741 "star-unpacking sequence pattern");
5742 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
5743 seen_star = 1;
5744 }
5745 else if (elt->kind == MatchStar_kind) {
5746 return compiler_error(c,
5747 "multiple starred expressions in sequence pattern");
5748 }
5749 }
5750 if (!seen_star) {
5751 ADDOP_I(c, UNPACK_SEQUENCE, n);
5752 }
5753 return 1;
5754}
5755
5756static int
5757pattern_helper_sequence_unpack(struct compiler *c, asdl_pattern_seq *patterns,
Brandt Bucher145bf262021-02-26 14:51:55 -08005758 Py_ssize_t star, pattern_context *pc)
5759{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005760 RETURN_IF_FALSE(pattern_unpack_helper(c, patterns));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005761 Py_ssize_t size = asdl_seq_LEN(patterns);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005762 // We've now got a bunch of new subjects on the stack. They need to remain
5763 // there after each subpattern match:
5764 pc->on_top += size;
Brandt Bucher145bf262021-02-26 14:51:55 -08005765 for (Py_ssize_t i = 0; i < size; i++) {
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005766 // One less item to keep track of each time we loop through:
5767 pc->on_top--;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005768 pattern_ty pattern = asdl_seq_GET(patterns, i);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005769 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08005770 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005771 return 1;
Brandt Bucher145bf262021-02-26 14:51:55 -08005772}
5773
5774// Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of
5775// UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a
5776// starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc.
5777static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005778pattern_helper_sequence_subscr(struct compiler *c, asdl_pattern_seq *patterns,
Brandt Bucher145bf262021-02-26 14:51:55 -08005779 Py_ssize_t star, pattern_context *pc)
5780{
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005781 // We need to keep the subject around for extracting elements:
5782 pc->on_top++;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005783 Py_ssize_t size = asdl_seq_LEN(patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08005784 for (Py_ssize_t i = 0; i < size; i++) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005785 pattern_ty pattern = asdl_seq_GET(patterns, i);
5786 if (WILDCARD_CHECK(pattern)) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005787 continue;
5788 }
5789 if (i == star) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005790 assert(WILDCARD_STAR_CHECK(pattern));
Brandt Bucher145bf262021-02-26 14:51:55 -08005791 continue;
5792 }
5793 ADDOP(c, DUP_TOP);
5794 if (i < star) {
5795 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5796 }
5797 else {
5798 // The subject may not support negative indexing! Compute a
5799 // nonnegative index:
5800 ADDOP(c, GET_LEN);
5801 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i));
5802 ADDOP(c, BINARY_SUBTRACT);
5803 }
5804 ADDOP(c, BINARY_SUBSCR);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005805 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08005806 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005807 // Pop the subject, we're done with it:
5808 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08005809 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08005810 return 1;
5811}
5812
Brandt Bucher145bf262021-02-26 14:51:55 -08005813// Like compiler_pattern, but turn off checks for irrefutability.
5814static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005815compiler_pattern_subpattern(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005816{
5817 int allow_irrefutable = pc->allow_irrefutable;
5818 pc->allow_irrefutable = 1;
5819 RETURN_IF_FALSE(compiler_pattern(c, p, pc));
5820 pc->allow_irrefutable = allow_irrefutable;
5821 return 1;
5822}
5823
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005824static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005825compiler_pattern_as(struct compiler *c, pattern_ty p, pattern_context *pc)
5826{
5827 assert(p->kind == MatchAs_kind);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005828 if (p->v.MatchAs.pattern == NULL) {
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005829 // An irrefutable match:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005830 if (!pc->allow_irrefutable) {
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005831 if (p->v.MatchAs.name) {
5832 const char *e = "name capture %R makes remaining patterns unreachable";
5833 return compiler_error(c, e, p->v.MatchAs.name);
5834 }
5835 const char *e = "wildcard makes remaining patterns unreachable";
5836 return compiler_error(c, e);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005837 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005838 return pattern_helper_store_name(c, p->v.MatchAs.name, pc);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005839 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005840 // Need to make a copy for (possibly) storing later:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005841 pc->on_top++;
Brandt Bucher145bf262021-02-26 14:51:55 -08005842 ADDOP(c, DUP_TOP);
5843 RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc));
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005844 // Success! Store it:
5845 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08005846 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08005847 return 1;
5848}
5849
Brandt Bucher145bf262021-02-26 14:51:55 -08005850static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005851compiler_pattern_star(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005852{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005853 assert(p->kind == MatchStar_kind);
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005854 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchStar.name, pc));
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005855 return 1;
Brandt Bucher145bf262021-02-26 14:51:55 -08005856}
5857
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005858static int
5859validate_kwd_attrs(struct compiler *c, asdl_identifier_seq *attrs, asdl_pattern_seq* patterns)
5860{
5861 // Any errors will point to the pattern rather than the arg name as the
5862 // parser is only supplying identifiers rather than Name or keyword nodes
5863 Py_ssize_t nattrs = asdl_seq_LEN(attrs);
5864 for (Py_ssize_t i = 0; i < nattrs; i++) {
5865 identifier attr = ((identifier)asdl_seq_GET(attrs, i));
5866 c->u->u_col_offset = ((pattern_ty) asdl_seq_GET(patterns, i))->col_offset;
5867 if (forbidden_name(c, attr, Store)) {
5868 return -1;
5869 }
5870 for (Py_ssize_t j = i + 1; j < nattrs; j++) {
5871 identifier other = ((identifier)asdl_seq_GET(attrs, j));
5872 if (!PyUnicode_Compare(attr, other)) {
5873 c->u->u_col_offset = ((pattern_ty) asdl_seq_GET(patterns, j))->col_offset;
5874 compiler_error(c, "attribute name repeated in class pattern: %U", attr);
5875 return -1;
5876 }
5877 }
5878 }
5879 return 0;
5880}
Brandt Bucher145bf262021-02-26 14:51:55 -08005881
5882static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005883compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005884{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005885 assert(p->kind == MatchClass_kind);
5886 asdl_pattern_seq *patterns = p->v.MatchClass.patterns;
5887 asdl_identifier_seq *kwd_attrs = p->v.MatchClass.kwd_attrs;
5888 asdl_pattern_seq *kwd_patterns = p->v.MatchClass.kwd_patterns;
5889 Py_ssize_t nargs = asdl_seq_LEN(patterns);
5890 Py_ssize_t nattrs = asdl_seq_LEN(kwd_attrs);
5891 Py_ssize_t nkwd_patterns = asdl_seq_LEN(kwd_patterns);
5892 if (nattrs != nkwd_patterns) {
5893 // AST validator shouldn't let this happen, but if it does,
5894 // just fail, don't crash out of the interpreter
5895 const char * e = "kwd_attrs (%d) / kwd_patterns (%d) length mismatch in class pattern";
5896 return compiler_error(c, e, nattrs, nkwd_patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08005897 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005898 if (INT_MAX < nargs || INT_MAX < nargs + nattrs - 1) {
5899 const char *e = "too many sub-patterns in class pattern %R";
5900 return compiler_error(c, e, p->v.MatchClass.cls);
5901 }
5902 if (nattrs) {
5903 RETURN_IF_FALSE(!validate_kwd_attrs(c, kwd_attrs, kwd_patterns));
5904 c->u->u_col_offset = p->col_offset; // validate_kwd_attrs moves this
5905 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005906 VISIT(c, expr, p->v.MatchClass.cls);
5907 PyObject *attr_names;
5908 RETURN_IF_FALSE(attr_names = PyTuple_New(nattrs));
Brandt Bucher145bf262021-02-26 14:51:55 -08005909 Py_ssize_t i;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005910 for (i = 0; i < nattrs; i++) {
5911 PyObject *name = asdl_seq_GET(kwd_attrs, i);
Brandt Bucher145bf262021-02-26 14:51:55 -08005912 Py_INCREF(name);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005913 PyTuple_SET_ITEM(attr_names, i, name);
Brandt Bucher145bf262021-02-26 14:51:55 -08005914 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005915 ADDOP_LOAD_CONST_NEW(c, attr_names);
Brandt Bucher145bf262021-02-26 14:51:55 -08005916 ADDOP_I(c, MATCH_CLASS, nargs);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005917 // TOS is now a tuple of (nargs + nattrs) attributes. Preserve it:
5918 pc->on_top++;
5919 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005920 for (i = 0; i < nargs + nattrs; i++) {
5921 pattern_ty pattern;
Brandt Bucher145bf262021-02-26 14:51:55 -08005922 if (i < nargs) {
5923 // Positional:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005924 pattern = asdl_seq_GET(patterns, i);
Brandt Bucher145bf262021-02-26 14:51:55 -08005925 }
5926 else {
5927 // Keyword:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005928 pattern = asdl_seq_GET(kwd_patterns, i - nargs);
Brandt Bucher145bf262021-02-26 14:51:55 -08005929 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005930 if (WILDCARD_CHECK(pattern)) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005931 continue;
5932 }
5933 // Get the i-th attribute, and match it against the i-th pattern:
5934 ADDOP(c, DUP_TOP);
5935 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5936 ADDOP(c, BINARY_SUBSCR);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005937 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08005938 }
5939 // Success! Pop the tuple of attributes:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005940 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08005941 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08005942 return 1;
5943}
5944
Brandt Bucher145bf262021-02-26 14:51:55 -08005945static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005946compiler_pattern_mapping(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005947{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005948 assert(p->kind == MatchMapping_kind);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005949 asdl_expr_seq *keys = p->v.MatchMapping.keys;
5950 asdl_pattern_seq *patterns = p->v.MatchMapping.patterns;
5951 Py_ssize_t size = asdl_seq_LEN(keys);
5952 Py_ssize_t npatterns = asdl_seq_LEN(patterns);
5953 if (size != npatterns) {
5954 // AST validator shouldn't let this happen, but if it does,
5955 // just fail, don't crash out of the interpreter
5956 const char * e = "keys (%d) / patterns (%d) length mismatch in mapping pattern";
5957 return compiler_error(c, e, size, npatterns);
5958 }
5959 // We have a double-star target if "rest" is set
5960 PyObject *star_target = p->v.MatchMapping.rest;
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005961 // We need to keep the subject on top during the mapping and length checks:
5962 pc->on_top++;
Brandt Bucher145bf262021-02-26 14:51:55 -08005963 ADDOP(c, MATCH_MAPPING);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005964 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005965 if (!size && !star_target) {
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005966 // If the pattern is just "{}", we're done! Pop the subject:
5967 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08005968 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08005969 return 1;
5970 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005971 if (size) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005972 // If the pattern has any keys in it, perform a length check:
5973 ADDOP(c, GET_LEN);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005974 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
Brandt Bucher145bf262021-02-26 14:51:55 -08005975 ADDOP_COMPARE(c, GtE);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005976 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08005977 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005978 if (INT_MAX < size - 1) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005979 return compiler_error(c, "too many sub-patterns in mapping pattern");
5980 }
5981 // Collect all of the keys into a tuple for MATCH_KEYS and
5982 // COPY_DICT_WITHOUT_KEYS. They can either be dotted names or literals:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005983 for (Py_ssize_t i = 0; i < size; i++) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005984 expr_ty key = asdl_seq_GET(keys, i);
5985 if (key == NULL) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005986 const char *e = "can't use NULL keys in MatchMapping "
5987 "(set 'rest' parameter instead)";
5988 c->u->u_col_offset = ((pattern_ty) asdl_seq_GET(patterns, i))->col_offset;
5989 return compiler_error(c, e);
5990 }
5991 if (!MATCH_VALUE_EXPR(key)) {
5992 const char *e = "mapping pattern keys may only match literals and attribute lookups";
Brandt Bucher145bf262021-02-26 14:51:55 -08005993 return compiler_error(c, e);
5994 }
5995 VISIT(c, expr, key);
5996 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005997 ADDOP_I(c, BUILD_TUPLE, size);
Brandt Bucher145bf262021-02-26 14:51:55 -08005998 ADDOP(c, MATCH_KEYS);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005999 // There's now a tuple of keys and a tuple of values on top of the subject:
6000 pc->on_top += 2;
6001 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
6002 // So far so good. Use that tuple of values on the stack to match
Brandt Bucher145bf262021-02-26 14:51:55 -08006003 // sub-patterns against:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006004 for (Py_ssize_t i = 0; i < size; i++) {
6005 pattern_ty pattern = asdl_seq_GET(patterns, i);
6006 if (WILDCARD_CHECK(pattern)) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006007 continue;
6008 }
6009 ADDOP(c, DUP_TOP);
6010 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
6011 ADDOP(c, BINARY_SUBSCR);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006012 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006013 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006014 // If we get this far, it's a match! We're done with the tuple of values,
6015 // and whatever happens next should consume the tuple of keys underneath it:
6016 pc->on_top -= 2;
Brandt Bucher145bf262021-02-26 14:51:55 -08006017 ADDOP(c, POP_TOP);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006018 if (star_target) {
6019 // If we have a starred name, bind a dict of remaining items to it:
Brandt Bucher145bf262021-02-26 14:51:55 -08006020 ADDOP(c, COPY_DICT_WITHOUT_KEYS);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006021 RETURN_IF_FALSE(pattern_helper_store_name(c, star_target, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006022 }
6023 else {
6024 // Otherwise, we don't care about this tuple of keys anymore:
6025 ADDOP(c, POP_TOP);
6026 }
6027 // Pop the subject:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006028 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08006029 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08006030 return 1;
6031}
6032
Brandt Bucher145bf262021-02-26 14:51:55 -08006033static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006034compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006035{
6036 assert(p->kind == MatchOr_kind);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006037 basicblock *end;
Brandt Bucher145bf262021-02-26 14:51:55 -08006038 RETURN_IF_FALSE(end = compiler_new_block(c));
Brandt Bucher145bf262021-02-26 14:51:55 -08006039 Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns);
6040 assert(size > 1);
6041 // We're going to be messing with pc. Keep the original info handy:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006042 pattern_context old_pc = *pc;
6043 Py_INCREF(pc->stores);
6044 // control is the list of names bound by the first alternative. It is used
6045 // for checking different name bindings in alternatives, and for correcting
6046 // the order in which extracted elements are placed on the stack.
6047 PyObject *control = NULL;
6048 // NOTE: We can't use returning macros anymore! goto error on error.
Brandt Bucher145bf262021-02-26 14:51:55 -08006049 for (Py_ssize_t i = 0; i < size; i++) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006050 pattern_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i);
Brandt Bucher145bf262021-02-26 14:51:55 -08006051 SET_LOC(c, alt);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006052 PyObject *pc_stores = PyList_New(0);
6053 if (pc_stores == NULL) {
6054 goto error;
Brandt Bucher145bf262021-02-26 14:51:55 -08006055 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006056 Py_SETREF(pc->stores, pc_stores);
6057 // An irrefutable sub-pattern must be last, if it is allowed at all:
6058 pc->allow_irrefutable = (i == size - 1) && old_pc.allow_irrefutable;
6059 pc->fail_pop = NULL;
6060 pc->fail_pop_size = 0;
6061 pc->on_top = 0;
6062 if (!compiler_addop(c, DUP_TOP) || !compiler_pattern(c, alt, pc)) {
6063 goto error;
6064 }
6065 // Success!
6066 Py_ssize_t nstores = PyList_GET_SIZE(pc->stores);
Brandt Bucher145bf262021-02-26 14:51:55 -08006067 if (!i) {
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006068 // This is the first alternative, so save its stores as a "control"
6069 // for the others (they can't bind a different set of names, and
6070 // might need to be reordered):
6071 assert(control == NULL);
Brandt Bucher145bf262021-02-26 14:51:55 -08006072 control = pc->stores;
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006073 Py_INCREF(control);
Brandt Bucher145bf262021-02-26 14:51:55 -08006074 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006075 else if (nstores != PyList_GET_SIZE(control)) {
6076 goto diff;
Brandt Bucher145bf262021-02-26 14:51:55 -08006077 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006078 else if (nstores) {
6079 // There were captures. Check to see if we differ from control:
6080 Py_ssize_t icontrol = nstores;
6081 while (icontrol--) {
6082 PyObject *name = PyList_GET_ITEM(control, icontrol);
6083 Py_ssize_t istores = PySequence_Index(pc->stores, name);
6084 if (istores < 0) {
6085 PyErr_Clear();
6086 goto diff;
6087 }
6088 if (icontrol != istores) {
6089 // Reorder the names on the stack to match the order of the
6090 // names in control. There's probably a better way of doing
6091 // this; the current solution is potentially very
6092 // inefficient when each alternative subpattern binds lots
6093 // of names in different orders. It's fine for reasonable
6094 // cases, though.
6095 assert(istores < icontrol);
6096 Py_ssize_t rotations = istores + 1;
6097 // Perfom the same rotation on pc->stores:
6098 PyObject *rotated = PyList_GetSlice(pc->stores, 0,
6099 rotations);
6100 if (rotated == NULL ||
6101 PyList_SetSlice(pc->stores, 0, rotations, NULL) ||
6102 PyList_SetSlice(pc->stores, icontrol - istores,
6103 icontrol - istores, rotated))
6104 {
6105 Py_XDECREF(rotated);
6106 goto error;
6107 }
6108 Py_DECREF(rotated);
6109 // That just did:
6110 // rotated = pc_stores[:rotations]
6111 // del pc_stores[:rotations]
6112 // pc_stores[icontrol-istores:icontrol-istores] = rotated
6113 // Do the same thing to the stack, using several ROT_Ns:
6114 while (rotations--) {
6115 if (!compiler_addop_i(c, ROT_N, icontrol + 1)) {
6116 goto error;
6117 }
6118 }
6119 }
6120 }
6121 }
6122 assert(control);
6123 if (!compiler_addop_j(c, JUMP_FORWARD, end) ||
6124 !compiler_next_block(c) ||
6125 !emit_and_reset_fail_pop(c, pc))
6126 {
6127 goto error;
6128 }
Brandt Bucher145bf262021-02-26 14:51:55 -08006129 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006130 Py_DECREF(pc->stores);
6131 *pc = old_pc;
6132 Py_INCREF(pc->stores);
6133 // Need to NULL this for the PyObject_Free call in the error block.
6134 old_pc.fail_pop = NULL;
6135 // No match. Pop the remaining copy of the subject and fail:
6136 if (!compiler_addop(c, POP_TOP) || !jump_to_fail_pop(c, pc, JUMP_FORWARD)) {
6137 goto error;
6138 }
Brandt Bucher145bf262021-02-26 14:51:55 -08006139 compiler_use_next_block(c, end);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006140 Py_ssize_t nstores = PyList_GET_SIZE(control);
6141 // There's a bunch of stuff on the stack between any where the new stores
6142 // are and where they need to be:
6143 // - The other stores.
6144 // - A copy of the subject.
6145 // - Anything else that may be on top of the stack.
6146 // - Any previous stores we've already stashed away on the stack.
Pablo Galindo39494282021-05-03 16:20:46 +01006147 Py_ssize_t nrots = nstores + 1 + pc->on_top + PyList_GET_SIZE(pc->stores);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006148 for (Py_ssize_t i = 0; i < nstores; i++) {
6149 // Rotate this capture to its proper place on the stack:
6150 if (!compiler_addop_i(c, ROT_N, nrots)) {
6151 goto error;
6152 }
6153 // Update the list of previous stores with this new name, checking for
6154 // duplicates:
6155 PyObject *name = PyList_GET_ITEM(control, i);
6156 int dupe = PySequence_Contains(pc->stores, name);
6157 if (dupe < 0) {
6158 goto error;
6159 }
6160 if (dupe) {
6161 compiler_error_duplicate_store(c, name);
6162 goto error;
6163 }
6164 if (PyList_Append(pc->stores, name)) {
6165 goto error;
6166 }
6167 }
6168 Py_DECREF(old_pc.stores);
6169 Py_DECREF(control);
6170 // NOTE: Returning macros are safe again.
6171 // Pop the copy of the subject:
6172 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08006173 return 1;
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006174diff:
6175 compiler_error(c, "alternative patterns bind different names");
6176error:
6177 PyObject_Free(old_pc.fail_pop);
6178 Py_DECREF(old_pc.stores);
Brandt Bucher145bf262021-02-26 14:51:55 -08006179 Py_XDECREF(control);
6180 return 0;
6181}
6182
6183
6184static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006185compiler_pattern_sequence(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006186{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006187 assert(p->kind == MatchSequence_kind);
6188 asdl_pattern_seq *patterns = p->v.MatchSequence.patterns;
6189 Py_ssize_t size = asdl_seq_LEN(patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08006190 Py_ssize_t star = -1;
6191 int only_wildcard = 1;
6192 int star_wildcard = 0;
6193 // Find a starred name, if it exists. There may be at most one:
6194 for (Py_ssize_t i = 0; i < size; i++) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006195 pattern_ty pattern = asdl_seq_GET(patterns, i);
6196 if (pattern->kind == MatchStar_kind) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006197 if (star >= 0) {
6198 const char *e = "multiple starred names in sequence pattern";
6199 return compiler_error(c, e);
6200 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006201 star_wildcard = WILDCARD_STAR_CHECK(pattern);
6202 only_wildcard &= star_wildcard;
Brandt Bucher145bf262021-02-26 14:51:55 -08006203 star = i;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006204 continue;
Brandt Bucher145bf262021-02-26 14:51:55 -08006205 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006206 only_wildcard &= WILDCARD_CHECK(pattern);
Brandt Bucher145bf262021-02-26 14:51:55 -08006207 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006208 // We need to keep the subject on top during the sequence and length checks:
6209 pc->on_top++;
Brandt Bucher145bf262021-02-26 14:51:55 -08006210 ADDOP(c, MATCH_SEQUENCE);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006211 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006212 if (star < 0) {
6213 // No star: len(subject) == size
6214 ADDOP(c, GET_LEN);
6215 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
6216 ADDOP_COMPARE(c, Eq);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006217 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006218 }
6219 else if (size > 1) {
6220 // Star: len(subject) >= size - 1
6221 ADDOP(c, GET_LEN);
6222 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1));
6223 ADDOP_COMPARE(c, GtE);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006224 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006225 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006226 // Whatever comes next should consume the subject:
6227 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08006228 if (only_wildcard) {
6229 // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc.
6230 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08006231 }
6232 else if (star_wildcard) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006233 RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, patterns, star, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006234 }
6235 else {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006236 RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, patterns, star, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006237 }
Brandt Bucher145bf262021-02-26 14:51:55 -08006238 return 1;
6239}
6240
Brandt Bucher145bf262021-02-26 14:51:55 -08006241static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006242compiler_pattern_value(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006243{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006244 assert(p->kind == MatchValue_kind);
6245 expr_ty value = p->v.MatchValue.value;
6246 if (!MATCH_VALUE_EXPR(value)) {
6247 const char *e = "patterns may only match literals and attribute lookups";
6248 return compiler_error(c, e);
6249 }
6250 VISIT(c, expr, value);
Brandt Bucher145bf262021-02-26 14:51:55 -08006251 ADDOP_COMPARE(c, Eq);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006252 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006253 return 1;
6254}
6255
Brandt Bucher145bf262021-02-26 14:51:55 -08006256static int
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07006257compiler_pattern_singleton(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006258{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006259 assert(p->kind == MatchSingleton_kind);
6260 ADDOP_LOAD_CONST(c, p->v.MatchSingleton.value);
6261 ADDOP_COMPARE(c, Is);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006262 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006263 return 1;
6264}
6265
Brandt Bucher145bf262021-02-26 14:51:55 -08006266static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006267compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006268{
6269 SET_LOC(c, p);
6270 switch (p->kind) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006271 case MatchValue_kind:
Brandt Bucher145bf262021-02-26 14:51:55 -08006272 return compiler_pattern_value(c, p, pc);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006273 case MatchSingleton_kind:
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07006274 return compiler_pattern_singleton(c, p, pc);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006275 case MatchSequence_kind:
Brandt Bucher145bf262021-02-26 14:51:55 -08006276 return compiler_pattern_sequence(c, p, pc);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006277 case MatchMapping_kind:
6278 return compiler_pattern_mapping(c, p, pc);
6279 case MatchClass_kind:
6280 return compiler_pattern_class(c, p, pc);
6281 case MatchStar_kind:
6282 return compiler_pattern_star(c, p, pc);
Brandt Bucher145bf262021-02-26 14:51:55 -08006283 case MatchAs_kind:
6284 return compiler_pattern_as(c, p, pc);
6285 case MatchOr_kind:
6286 return compiler_pattern_or(c, p, pc);
Brandt Bucher145bf262021-02-26 14:51:55 -08006287 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006288 // AST validator shouldn't let this happen, but if it does,
6289 // just fail, don't crash out of the interpreter
6290 const char *e = "invalid match pattern node in AST (kind=%d)";
6291 return compiler_error(c, e, p->kind);
Brandt Bucher145bf262021-02-26 14:51:55 -08006292}
6293
Brandt Bucher145bf262021-02-26 14:51:55 -08006294static int
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006295compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006296{
6297 VISIT(c, expr, s->v.Match.subject);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006298 basicblock *end;
Brandt Bucher145bf262021-02-26 14:51:55 -08006299 RETURN_IF_FALSE(end = compiler_new_block(c));
6300 Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006301 assert(cases > 0);
Brandt Bucher145bf262021-02-26 14:51:55 -08006302 match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6303 int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases;
6304 for (Py_ssize_t i = 0; i < cases - has_default; i++) {
6305 m = asdl_seq_GET(s->v.Match.cases, i);
6306 SET_LOC(c, m->pattern);
Brandt Bucher145bf262021-02-26 14:51:55 -08006307 // Only copy the subject if we're *not* on the last case:
6308 if (i != cases - has_default - 1) {
6309 ADDOP(c, DUP_TOP);
6310 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006311 RETURN_IF_FALSE(pc->stores = PyList_New(0));
6312 // Irrefutable cases must be either guarded, last, or both:
6313 pc->allow_irrefutable = m->guard != NULL || i == cases - 1;
6314 pc->fail_pop = NULL;
6315 pc->fail_pop_size = 0;
6316 pc->on_top = 0;
6317 // NOTE: Can't use returning macros here (they'll leak pc->stores)!
6318 if (!compiler_pattern(c, m->pattern, pc)) {
6319 Py_DECREF(pc->stores);
6320 return 0;
6321 }
6322 assert(!pc->on_top);
6323 // It's a match! Store all of the captured names (they're on the stack).
6324 Py_ssize_t nstores = PyList_GET_SIZE(pc->stores);
6325 for (Py_ssize_t n = 0; n < nstores; n++) {
6326 PyObject *name = PyList_GET_ITEM(pc->stores, n);
6327 if (!compiler_nameop(c, name, Store)) {
6328 Py_DECREF(pc->stores);
6329 return 0;
6330 }
6331 }
6332 Py_DECREF(pc->stores);
6333 // NOTE: Returning macros are safe again.
Brandt Bucher145bf262021-02-26 14:51:55 -08006334 if (m->guard) {
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006335 RETURN_IF_FALSE(ensure_fail_pop(c, pc, 0));
6336 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, pc->fail_pop[0], 0));
Brandt Bucher145bf262021-02-26 14:51:55 -08006337 }
6338 // Success! Pop the subject off, we're done with it:
6339 if (i != cases - has_default - 1) {
6340 ADDOP(c, POP_TOP);
6341 }
6342 VISIT_SEQ(c, stmt, m->body);
6343 ADDOP_JUMP(c, JUMP_FORWARD, end);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006344 RETURN_IF_FALSE(emit_and_reset_fail_pop(c, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006345 }
6346 if (has_default) {
6347 if (cases == 1) {
6348 // No matches. Done with the subject:
6349 ADDOP(c, POP_TOP);
6350 }
6351 // A trailing "case _" is common, and lets us save a bit of redundant
6352 // pushing and popping in the loop above:
6353 m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6354 SET_LOC(c, m->pattern);
6355 if (m->guard) {
6356 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0));
6357 }
6358 VISIT_SEQ(c, stmt, m->body);
6359 }
6360 compiler_use_next_block(c, end);
6361 return 1;
6362}
6363
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006364static int
6365compiler_match(struct compiler *c, stmt_ty s)
6366{
6367 pattern_context pc;
6368 pc.fail_pop = NULL;
6369 int result = compiler_match_inner(c, s, &pc);
6370 PyObject_Free(pc.fail_pop);
6371 return result;
6372}
6373
Brandt Bucher145bf262021-02-26 14:51:55 -08006374#undef WILDCARD_CHECK
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006375#undef WILDCARD_STAR_CHECK
Brandt Bucher145bf262021-02-26 14:51:55 -08006376
Thomas Wouters89f507f2006-12-13 04:49:30 +00006377/* End of the compiler section, beginning of the assembler section */
6378
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006379/* do depth-first search of basic block graph, starting with block.
T. Wouters99b54d62019-09-12 07:05:33 -07006380 post records the block indices in post-order.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006381
6382 XXX must handle implicit jumps from one block to next
6383*/
6384
Thomas Wouters89f507f2006-12-13 04:49:30 +00006385struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006386 PyObject *a_bytecode; /* string containing bytecode */
6387 int a_offset; /* offset into bytecode */
6388 int a_nblocks; /* number of reachable blocks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006389 PyObject *a_lnotab; /* string containing lnotab */
6390 int a_lnotab_off; /* offset into lnotab */
Mark Shannon877df852020-11-12 09:43:29 +00006391 int a_prevlineno; /* lineno of last emitted line in line table */
6392 int a_lineno; /* lineno of last emitted instruction */
6393 int a_lineno_start; /* bytecode start offset of current lineno */
Mark Shannoncc75ab72020-11-12 19:49:33 +00006394 basicblock *a_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006395};
6396
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006397Py_LOCAL_INLINE(void)
6398stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006399{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02006400 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
Mark Shannonfee55262019-11-21 09:11:43 +00006401 if (b->b_startdepth < depth && b->b_startdepth < 100) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006402 assert(b->b_startdepth < 0);
6403 b->b_startdepth = depth;
6404 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02006405 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006406}
6407
6408/* Find the flow path that needs the largest stack. We assume that
6409 * cycles in the flow graph have no net effect on the stack depth.
6410 */
6411static int
6412stackdepth(struct compiler *c)
6413{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006414 basicblock *b, *entryblock = NULL;
6415 basicblock **stack, **sp;
6416 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006417 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006418 b->b_startdepth = INT_MIN;
6419 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006420 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006421 }
Mark Shannon67969f52021-04-07 10:52:07 +01006422 assert(entryblock!= NULL);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006423 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
6424 if (!stack) {
6425 PyErr_NoMemory();
6426 return -1;
6427 }
6428
6429 sp = stack;
Mark Shannonb37181e2021-04-06 11:48:59 +01006430 if (c->u->u_ste->ste_generator || c->u->u_ste->ste_coroutine) {
6431 stackdepth_push(&sp, entryblock, 1);
6432 } else {
6433 stackdepth_push(&sp, entryblock, 0);
6434 }
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006435 while (sp != stack) {
6436 b = *--sp;
6437 int depth = b->b_startdepth;
6438 assert(depth >= 0);
6439 basicblock *next = b->b_next;
6440 for (int i = 0; i < b->b_iused; i++) {
6441 struct instr *instr = &b->b_instr[i];
6442 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
6443 if (effect == PY_INVALID_STACK_EFFECT) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01006444 PyErr_Format(PyExc_SystemError,
6445 "compiler stack_effect(opcode=%d, arg=%i) failed",
6446 instr->i_opcode, instr->i_oparg);
6447 return -1;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006448 }
6449 int new_depth = depth + effect;
6450 if (new_depth > maxdepth) {
6451 maxdepth = new_depth;
6452 }
6453 assert(depth >= 0); /* invalid code or bug in stackdepth() */
Mark Shannon582aaf12020-08-04 17:30:11 +01006454 if (is_jump(instr)) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006455 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
6456 assert(effect != PY_INVALID_STACK_EFFECT);
6457 int target_depth = depth + effect;
6458 if (target_depth > maxdepth) {
6459 maxdepth = target_depth;
6460 }
6461 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006462 stackdepth_push(&sp, instr->i_target, target_depth);
6463 }
6464 depth = new_depth;
6465 if (instr->i_opcode == JUMP_ABSOLUTE ||
6466 instr->i_opcode == JUMP_FORWARD ||
6467 instr->i_opcode == RETURN_VALUE ||
Mark Shannonfee55262019-11-21 09:11:43 +00006468 instr->i_opcode == RAISE_VARARGS ||
6469 instr->i_opcode == RERAISE)
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006470 {
6471 /* remaining code is dead */
6472 next = NULL;
6473 break;
6474 }
6475 }
6476 if (next != NULL) {
Mark Shannon266b4622020-11-17 19:30:14 +00006477 assert(b->b_nofallthrough == 0);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006478 stackdepth_push(&sp, next, depth);
6479 }
6480 }
6481 PyObject_Free(stack);
6482 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006483}
6484
6485static int
6486assemble_init(struct assembler *a, int nblocks, int firstlineno)
6487{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006488 memset(a, 0, sizeof(struct assembler));
Mark Shannon877df852020-11-12 09:43:29 +00006489 a->a_prevlineno = a->a_lineno = firstlineno;
Mark Shannonfd009e62020-11-13 12:53:53 +00006490 a->a_lnotab = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006491 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006492 if (a->a_bytecode == NULL) {
6493 goto error;
6494 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006495 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006496 if (a->a_lnotab == NULL) {
6497 goto error;
6498 }
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07006499 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006500 PyErr_NoMemory();
Mark Shannonfd009e62020-11-13 12:53:53 +00006501 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006502 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006503 return 1;
Mark Shannonfd009e62020-11-13 12:53:53 +00006504error:
6505 Py_XDECREF(a->a_bytecode);
6506 Py_XDECREF(a->a_lnotab);
6507 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006508}
6509
6510static void
6511assemble_free(struct assembler *a)
6512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006513 Py_XDECREF(a->a_bytecode);
6514 Py_XDECREF(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006515}
6516
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006517static int
6518blocksize(basicblock *b)
6519{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006520 int i;
6521 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006523 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006524 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006525 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006526}
6527
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006528static int
Mark Shannon877df852020-11-12 09:43:29 +00006529assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006530{
Mark Shannon877df852020-11-12 09:43:29 +00006531 Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006532 if (a->a_lnotab_off + 2 >= len) {
6533 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
6534 return 0;
6535 }
Pablo Galindo86e322f2021-01-30 13:54:22 +00006536 unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab);
6537 lnotab += a->a_lnotab_off;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006538 a->a_lnotab_off += 2;
Mark Shannon877df852020-11-12 09:43:29 +00006539 *lnotab++ = bdelta;
6540 *lnotab++ = ldelta;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006541 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006542}
6543
Mark Shannon877df852020-11-12 09:43:29 +00006544/* Appends a range to the end of the line number table. See
6545 * Objects/lnotab_notes.txt for the description of the line number table. */
6546
6547static int
6548assemble_line_range(struct assembler *a)
6549{
6550 int ldelta, bdelta;
6551 bdelta = (a->a_offset - a->a_lineno_start) * 2;
6552 if (bdelta == 0) {
6553 return 1;
6554 }
6555 if (a->a_lineno < 0) {
6556 ldelta = -128;
6557 }
6558 else {
6559 ldelta = a->a_lineno - a->a_prevlineno;
6560 a->a_prevlineno = a->a_lineno;
6561 while (ldelta > 127) {
6562 if (!assemble_emit_linetable_pair(a, 0, 127)) {
6563 return 0;
6564 }
6565 ldelta -= 127;
6566 }
6567 while (ldelta < -127) {
6568 if (!assemble_emit_linetable_pair(a, 0, -127)) {
6569 return 0;
6570 }
6571 ldelta += 127;
6572 }
6573 }
6574 assert(-128 <= ldelta && ldelta < 128);
6575 while (bdelta > 254) {
6576 if (!assemble_emit_linetable_pair(a, 254, ldelta)) {
6577 return 0;
6578 }
6579 ldelta = a->a_lineno < 0 ? -128 : 0;
6580 bdelta -= 254;
6581 }
6582 if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) {
6583 return 0;
6584 }
6585 a->a_lineno_start = a->a_offset;
6586 return 1;
6587}
6588
6589static int
6590assemble_lnotab(struct assembler *a, struct instr *i)
6591{
6592 if (i->i_lineno == a->a_lineno) {
6593 return 1;
6594 }
6595 if (!assemble_line_range(a)) {
6596 return 0;
6597 }
6598 a->a_lineno = i->i_lineno;
6599 return 1;
6600}
6601
6602
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006603/* assemble_emit()
6604 Extend the bytecode with a new instruction.
6605 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00006606*/
6607
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006608static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006609assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006610{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006611 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006612 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03006613 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006614
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006615 arg = i->i_oparg;
6616 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006617 if (i->i_lineno && !assemble_lnotab(a, i))
6618 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006619 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006620 if (len > PY_SSIZE_T_MAX / 2)
6621 return 0;
6622 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
6623 return 0;
6624 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03006625 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006626 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006627 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006628 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006629}
6630
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00006631static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006632assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006633{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006634 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006635 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006636 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00006637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006638 /* Compute the size of each block and fixup jump args.
6639 Replace block pointer with position in bytecode. */
6640 do {
6641 totsize = 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006642 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006643 bsize = blocksize(b);
6644 b->b_offset = totsize;
6645 totsize += bsize;
6646 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006647 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006648 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6649 bsize = b->b_offset;
6650 for (i = 0; i < b->b_iused; i++) {
6651 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006652 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006653 /* Relative jumps are computed relative to
6654 the instruction pointer after fetching
6655 the jump instruction.
6656 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006657 bsize += isize;
Mark Shannon582aaf12020-08-04 17:30:11 +01006658 if (is_jump(instr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006659 instr->i_oparg = instr->i_target->b_offset;
Mark Shannon582aaf12020-08-04 17:30:11 +01006660 if (is_relative_jump(instr)) {
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006661 instr->i_oparg -= bsize;
6662 }
6663 if (instrsize(instr->i_oparg) != isize) {
6664 extended_arg_recompile = 1;
6665 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006666 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006667 }
6668 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00006669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006670 /* XXX: This is an awful hack that could hurt performance, but
6671 on the bright side it should work until we come up
6672 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006674 The issue is that in the first loop blocksize() is called
6675 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006676 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006677 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006679 So we loop until we stop seeing new EXTENDED_ARGs.
6680 The only EXTENDED_ARGs that could be popping up are
6681 ones in jump instructions. So this should converge
6682 fairly quickly.
6683 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006684 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00006685}
6686
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006687static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01006688dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006690 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006691 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006693 tuple = PyTuple_New(size);
6694 if (tuple == NULL)
6695 return NULL;
6696 while (PyDict_Next(dict, &pos, &k, &v)) {
6697 i = PyLong_AS_LONG(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006698 Py_INCREF(k);
6699 assert((i - offset) < size);
6700 assert((i - offset) >= 0);
6701 PyTuple_SET_ITEM(tuple, i - offset, k);
6702 }
6703 return tuple;
6704}
6705
6706static PyObject *
6707consts_dict_keys_inorder(PyObject *dict)
6708{
6709 PyObject *consts, *k, *v;
6710 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
6711
6712 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
6713 if (consts == NULL)
6714 return NULL;
6715 while (PyDict_Next(dict, &pos, &k, &v)) {
6716 i = PyLong_AS_LONG(v);
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +03006717 /* The keys of the dictionary can be tuples wrapping a contant.
6718 * (see compiler_add_o and _PyCode_ConstantKey). In that case
6719 * the object we want is always second. */
6720 if (PyTuple_CheckExact(k)) {
6721 k = PyTuple_GET_ITEM(k, 1);
6722 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006723 Py_INCREF(k);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006724 assert(i < size);
6725 assert(i >= 0);
6726 PyList_SET_ITEM(consts, i, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006727 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006728 return consts;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006729}
6730
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006731static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006732compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006733{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006734 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006735 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006736 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04006737 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006738 if (ste->ste_nested)
6739 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07006740 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006741 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07006742 if (!ste->ste_generator && ste->ste_coroutine)
6743 flags |= CO_COROUTINE;
6744 if (ste->ste_generator && ste->ste_coroutine)
6745 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006746 if (ste->ste_varargs)
6747 flags |= CO_VARARGS;
6748 if (ste->ste_varkeywords)
6749 flags |= CO_VARKEYWORDS;
6750 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006752 /* (Only) inherit compilerflags in PyCF_MASK */
6753 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006754
Pablo Galindo90235812020-03-15 04:29:22 +00006755 if ((IS_TOP_LEVEL_AWAIT(c)) &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07006756 ste->ste_coroutine &&
6757 !ste->ste_generator) {
6758 flags |= CO_COROUTINE;
6759 }
6760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006761 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00006762}
6763
Inada Naokibdb941b2021-02-10 09:20:42 +09006764// Merge *obj* with constant cache.
INADA Naokic2e16072018-11-26 21:23:22 +09006765// Unlike merge_consts_recursive(), this function doesn't work recursively.
6766static int
Inada Naokibdb941b2021-02-10 09:20:42 +09006767merge_const_one(struct compiler *c, PyObject **obj)
INADA Naokic2e16072018-11-26 21:23:22 +09006768{
Inada Naokibdb941b2021-02-10 09:20:42 +09006769 PyObject *key = _PyCode_ConstantKey(*obj);
INADA Naokic2e16072018-11-26 21:23:22 +09006770 if (key == NULL) {
6771 return 0;
6772 }
6773
6774 // t is borrowed reference
6775 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
6776 Py_DECREF(key);
6777 if (t == NULL) {
6778 return 0;
6779 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006780 if (t == key) { // obj is new constant.
INADA Naokic2e16072018-11-26 21:23:22 +09006781 return 1;
6782 }
6783
Inada Naokibdb941b2021-02-10 09:20:42 +09006784 if (PyTuple_CheckExact(t)) {
6785 // t is still borrowed reference
6786 t = PyTuple_GET_ITEM(t, 1);
6787 }
6788
6789 Py_INCREF(t);
6790 Py_DECREF(*obj);
6791 *obj = t;
INADA Naokic2e16072018-11-26 21:23:22 +09006792 return 1;
6793}
6794
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006795static PyCodeObject *
Mark Shannon6e8128f2020-07-30 10:03:00 +01006796makecode(struct compiler *c, struct assembler *a, PyObject *consts)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006797{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006798 PyCodeObject *co = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006799 PyObject *names = NULL;
6800 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006801 PyObject *name = NULL;
6802 PyObject *freevars = NULL;
6803 PyObject *cellvars = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006804 Py_ssize_t nlocals;
6805 int nlocals_int;
6806 int flags;
Pablo Galindocd74e662019-06-01 18:08:04 +01006807 int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006808
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006809 names = dict_keys_inorder(c->u->u_names, 0);
6810 varnames = dict_keys_inorder(c->u->u_varnames, 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006811 if (!names || !varnames) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006812 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006813 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006814 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
6815 if (!cellvars)
6816 goto error;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006817 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006818 if (!freevars)
6819 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006820
Inada Naokibdb941b2021-02-10 09:20:42 +09006821 if (!merge_const_one(c, &names) ||
6822 !merge_const_one(c, &varnames) ||
6823 !merge_const_one(c, &cellvars) ||
6824 !merge_const_one(c, &freevars))
INADA Naokic2e16072018-11-26 21:23:22 +09006825 {
6826 goto error;
6827 }
6828
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006829 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01006830 assert(nlocals < INT_MAX);
6831 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
6832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006833 flags = compute_code_flags(c);
6834 if (flags < 0)
6835 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006836
Mark Shannon6e8128f2020-07-30 10:03:00 +01006837 consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
6838 if (consts == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006839 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006840 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006841 if (!merge_const_one(c, &consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006842 Py_DECREF(consts);
INADA Naokic2e16072018-11-26 21:23:22 +09006843 goto error;
6844 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006845
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01006846 posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int);
Pablo Galindocd74e662019-06-01 18:08:04 +01006847 posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +01006848 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006849 maxdepth = stackdepth(c);
6850 if (maxdepth < 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006851 Py_DECREF(consts);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006852 goto error;
6853 }
Mark Shannon11e0b292021-04-15 14:28:56 +01006854 if (maxdepth > MAX_ALLOWED_STACK_USE) {
6855 PyErr_Format(PyExc_SystemError,
6856 "excessive stack use: stack is %d deep",
6857 maxdepth);
6858 Py_DECREF(consts);
6859 goto error;
6860 }
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006861 co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
Mark Shannon13bc1392020-01-23 09:25:17 +00006862 posonlyargcount, kwonlyargcount, nlocals_int,
Mark Shannon6e8128f2020-07-30 10:03:00 +01006863 maxdepth, flags, a->a_bytecode, consts, names,
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006864 varnames, freevars, cellvars, c->c_filename,
6865 c->u->u_name, c->u->u_firstlineno, a->a_lnotab);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006866 Py_DECREF(consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006867 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006868 Py_XDECREF(names);
6869 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006870 Py_XDECREF(name);
6871 Py_XDECREF(freevars);
6872 Py_XDECREF(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006873 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006874}
6875
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006876
6877/* For debugging purposes only */
6878#if 0
6879static void
6880dump_instr(const struct instr *i)
6881{
Mark Shannon582aaf12020-08-04 17:30:11 +01006882 const char *jrel = (is_relative_jump(instr)) ? "jrel " : "";
6883 const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : "";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006884 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006886 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006887 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006888 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006889 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006890 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
6891 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006892}
6893
6894static void
6895dump_basicblock(const basicblock *b)
6896{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006897 const char *b_return = b->b_return ? "return " : "";
Pablo Galindo60eb9f12020-06-28 01:55:47 +01006898 fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n",
6899 b->b_iused, b->b_startdepth, b->b_offset, b_return);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006900 if (b->b_instr) {
6901 int i;
6902 for (i = 0; i < b->b_iused; i++) {
6903 fprintf(stderr, " [%02d] ", i);
6904 dump_instr(b->b_instr + i);
6905 }
6906 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006907}
6908#endif
6909
Mark Shannon5977a792020-12-02 13:31:40 +00006910
6911static int
6912normalize_basic_block(basicblock *bb);
6913
Mark Shannon6e8128f2020-07-30 10:03:00 +01006914static int
Inada Naoki8a232c72021-04-16 14:01:04 +09006915optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006916
Mark Shannon5977a792020-12-02 13:31:40 +00006917static int
6918ensure_exits_have_lineno(struct compiler *c);
6919
Mark Shannonb37181e2021-04-06 11:48:59 +01006920static int
6921insert_generator_prefix(struct compiler *c, basicblock *entryblock) {
6922
6923 int flags = compute_code_flags(c);
6924 if (flags < 0) {
6925 return -1;
6926 }
6927 int kind;
6928 if (flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
6929 if (flags & CO_COROUTINE) {
6930 kind = 1;
6931 }
6932 else if (flags & CO_ASYNC_GENERATOR) {
6933 kind = 2;
6934 }
6935 else {
6936 kind = 0;
6937 }
6938 }
6939 else {
6940 return 0;
6941 }
6942 if (compiler_next_instr(entryblock) < 0) {
6943 return -1;
6944 }
6945 for (int i = entryblock->b_iused-1; i > 0; i--) {
6946 entryblock->b_instr[i] = entryblock->b_instr[i-1];
6947 }
6948 entryblock->b_instr[0].i_opcode = GEN_START;
6949 entryblock->b_instr[0].i_oparg = kind;
6950 entryblock->b_instr[0].i_lineno = -1;
6951 entryblock->b_instr[0].i_target = NULL;
6952 return 0;
6953}
6954
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006955static PyCodeObject *
6956assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006957{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006958 basicblock *b, *entryblock;
6959 struct assembler a;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006960 int j, nblocks;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006961 PyCodeObject *co = NULL;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006962 PyObject *consts = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006964 /* Make sure every block that falls off the end returns None.
6965 XXX NEXT_BLOCK() isn't quite right, because if the last
6966 block ends with a jump or return b_next shouldn't set.
6967 */
6968 if (!c->u->u_curblock->b_return) {
Mark Shannon877df852020-11-12 09:43:29 +00006969 c->u->u_lineno = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006970 if (addNone)
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006971 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006972 ADDOP(c, RETURN_VALUE);
6973 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006974
Mark Shannon5977a792020-12-02 13:31:40 +00006975 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
6976 if (normalize_basic_block(b)) {
Alex Henrie503627f2021-03-02 03:20:25 -07006977 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00006978 }
6979 }
6980
6981 if (ensure_exits_have_lineno(c)) {
Alex Henrie503627f2021-03-02 03:20:25 -07006982 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00006983 }
6984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006985 nblocks = 0;
6986 entryblock = NULL;
6987 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6988 nblocks++;
6989 entryblock = b;
6990 }
Mark Shannon67969f52021-04-07 10:52:07 +01006991 assert(entryblock != NULL);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006992
Mark Shannonb37181e2021-04-06 11:48:59 +01006993 if (insert_generator_prefix(c, entryblock)) {
6994 goto error;
6995 }
6996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006997 /* Set firstlineno if it wasn't explicitly set. */
6998 if (!c->u->u_firstlineno) {
Mark Shannon67969f52021-04-07 10:52:07 +01006999 if (entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007000 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
Mark Shannon877df852020-11-12 09:43:29 +00007001 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007002 c->u->u_firstlineno = 1;
7003 }
Mark Shannon5977a792020-12-02 13:31:40 +00007004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007005 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
7006 goto error;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007007 a.a_entry = entryblock;
7008 a.a_nblocks = nblocks;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00007009
Mark Shannon6e8128f2020-07-30 10:03:00 +01007010 consts = consts_dict_keys_inorder(c->u->u_consts);
7011 if (consts == NULL) {
7012 goto error;
7013 }
Inada Naoki8a232c72021-04-16 14:01:04 +09007014 if (optimize_cfg(c, &a, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007015 goto error;
7016 }
7017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007018 /* Can't modify the bytecode after computing jump offsets. */
7019 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00007020
Mark Shannoncc75ab72020-11-12 19:49:33 +00007021 /* Emit code. */
7022 for(b = entryblock; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007023 for (j = 0; j < b->b_iused; j++)
7024 if (!assemble_emit(&a, &b->b_instr[j]))
7025 goto error;
7026 }
Mark Shannon877df852020-11-12 09:43:29 +00007027 if (!assemble_line_range(&a)) {
7028 return 0;
7029 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00007030
Inada Naokibdb941b2021-02-10 09:20:42 +09007031 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007032 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09007033 }
7034 if (!merge_const_one(c, &a.a_lnotab)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007035 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09007036 }
7037 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) {
7038 goto error;
7039 }
7040 if (!merge_const_one(c, &a.a_bytecode)) {
7041 goto error;
7042 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00007043
Mark Shannon6e8128f2020-07-30 10:03:00 +01007044 co = makecode(c, &a, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007045 error:
Mark Shannon6e8128f2020-07-30 10:03:00 +01007046 Py_XDECREF(consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007047 assemble_free(&a);
7048 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00007049}
Georg Brandl8334fd92010-12-04 10:26:46 +00007050
Mark Shannon6e8128f2020-07-30 10:03:00 +01007051/* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
7052 with LOAD_CONST (c1, c2, ... cn).
7053 The consts table must still be in list form so that the
7054 new constant (c1, c2, ... cn) can be appended.
7055 Called with codestr pointing to the first LOAD_CONST.
7056*/
7057static int
Inada Naoki8a232c72021-04-16 14:01:04 +09007058fold_tuple_on_constants(struct compiler *c,
7059 struct instr *inst,
Mark Shannon6e8128f2020-07-30 10:03:00 +01007060 int n, PyObject *consts)
7061{
7062 /* Pre-conditions */
7063 assert(PyList_CheckExact(consts));
7064 assert(inst[n].i_opcode == BUILD_TUPLE);
7065 assert(inst[n].i_oparg == n);
7066
7067 for (int i = 0; i < n; i++) {
7068 if (inst[i].i_opcode != LOAD_CONST) {
7069 return 0;
7070 }
7071 }
7072
7073 /* Buildup new tuple of constants */
7074 PyObject *newconst = PyTuple_New(n);
7075 if (newconst == NULL) {
7076 return -1;
7077 }
7078 for (int i = 0; i < n; i++) {
7079 int arg = inst[i].i_oparg;
7080 PyObject *constant = PyList_GET_ITEM(consts, arg);
7081 Py_INCREF(constant);
7082 PyTuple_SET_ITEM(newconst, i, constant);
7083 }
Inada Naoki8a232c72021-04-16 14:01:04 +09007084 if (merge_const_one(c, &newconst) == 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007085 Py_DECREF(newconst);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007086 return -1;
7087 }
Inada Naoki8a232c72021-04-16 14:01:04 +09007088
7089 Py_ssize_t index;
7090 for (index = 0; index < PyList_GET_SIZE(consts); index++) {
7091 if (PyList_GET_ITEM(consts, index) == newconst) {
7092 break;
7093 }
7094 }
7095 if (index == PyList_GET_SIZE(consts)) {
7096 if ((size_t)index >= (size_t)INT_MAX - 1) {
7097 Py_DECREF(newconst);
7098 PyErr_SetString(PyExc_OverflowError, "too many constants");
7099 return -1;
7100 }
7101 if (PyList_Append(consts, newconst)) {
7102 Py_DECREF(newconst);
7103 return -1;
7104 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007105 }
7106 Py_DECREF(newconst);
7107 for (int i = 0; i < n; i++) {
7108 inst[i].i_opcode = NOP;
7109 }
7110 inst[n].i_opcode = LOAD_CONST;
Victor Stinner71f2ff42020-09-23 14:06:55 +02007111 inst[n].i_oparg = (int)index;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007112 return 0;
7113}
7114
Mark Shannon28b75c82020-12-23 11:43:10 +00007115
Brandt Bucher0ad1e032021-05-02 13:02:10 -07007116// Eliminate n * ROT_N(n).
7117static void
7118fold_rotations(struct instr *inst, int n)
7119{
7120 for (int i = 0; i < n; i++) {
7121 int rot;
7122 switch (inst[i].i_opcode) {
7123 case ROT_N:
7124 rot = inst[i].i_oparg;
7125 break;
7126 case ROT_FOUR:
7127 rot = 4;
7128 break;
7129 case ROT_THREE:
7130 rot = 3;
7131 break;
7132 case ROT_TWO:
7133 rot = 2;
7134 break;
7135 default:
7136 return;
7137 }
7138 if (rot != n) {
7139 return;
7140 }
7141 }
7142 for (int i = 0; i < n; i++) {
7143 inst[i].i_opcode = NOP;
7144 }
7145}
7146
7147
Mark Shannon28b75c82020-12-23 11:43:10 +00007148static int
7149eliminate_jump_to_jump(basicblock *bb, int opcode) {
7150 assert (bb->b_iused > 0);
7151 struct instr *inst = &bb->b_instr[bb->b_iused-1];
7152 assert (is_jump(inst));
7153 assert (inst->i_target->b_iused > 0);
7154 struct instr *target = &inst->i_target->b_instr[0];
7155 if (inst->i_target == target->i_target) {
7156 /* Nothing to do */
7157 return 0;
7158 }
7159 int lineno = target->i_lineno;
7160 if (add_jump_to_block(bb, opcode, lineno, target->i_target) == 0) {
7161 return -1;
7162 }
7163 assert (bb->b_iused >= 2);
7164 bb->b_instr[bb->b_iused-2].i_opcode = NOP;
7165 return 0;
7166}
7167
Mark Shannoncc75ab72020-11-12 19:49:33 +00007168/* Maximum size of basic block that should be copied in optimizer */
7169#define MAX_COPY_SIZE 4
Mark Shannon6e8128f2020-07-30 10:03:00 +01007170
7171/* Optimization */
7172static int
Inada Naoki8a232c72021-04-16 14:01:04 +09007173optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
Mark Shannon6e8128f2020-07-30 10:03:00 +01007174{
7175 assert(PyList_CheckExact(consts));
7176 struct instr nop;
7177 nop.i_opcode = NOP;
7178 struct instr *target;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007179 for (int i = 0; i < bb->b_iused; i++) {
7180 struct instr *inst = &bb->b_instr[i];
7181 int oparg = inst->i_oparg;
7182 int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01007183 if (is_jump(inst)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007184 /* Skip over empty basic blocks. */
7185 while (inst->i_target->b_iused == 0) {
7186 inst->i_target = inst->i_target->b_next;
7187 }
7188 target = &inst->i_target->b_instr[0];
7189 }
7190 else {
7191 target = &nop;
7192 }
7193 switch (inst->i_opcode) {
Mark Shannon266b4622020-11-17 19:30:14 +00007194 /* Remove LOAD_CONST const; conditional jump */
Mark Shannon6e8128f2020-07-30 10:03:00 +01007195 case LOAD_CONST:
Mark Shannon266b4622020-11-17 19:30:14 +00007196 {
7197 PyObject* cnt;
7198 int is_true;
7199 int jump_if_true;
7200 switch(nextop) {
7201 case POP_JUMP_IF_FALSE:
7202 case POP_JUMP_IF_TRUE:
7203 cnt = PyList_GET_ITEM(consts, oparg);
7204 is_true = PyObject_IsTrue(cnt);
7205 if (is_true == -1) {
7206 goto error;
7207 }
7208 inst->i_opcode = NOP;
7209 jump_if_true = nextop == POP_JUMP_IF_TRUE;
7210 if (is_true == jump_if_true) {
7211 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
7212 bb->b_nofallthrough = 1;
7213 }
7214 else {
7215 bb->b_instr[i+1].i_opcode = NOP;
7216 }
7217 break;
7218 case JUMP_IF_FALSE_OR_POP:
7219 case JUMP_IF_TRUE_OR_POP:
7220 cnt = PyList_GET_ITEM(consts, oparg);
7221 is_true = PyObject_IsTrue(cnt);
7222 if (is_true == -1) {
7223 goto error;
7224 }
7225 jump_if_true = nextop == JUMP_IF_TRUE_OR_POP;
7226 if (is_true == jump_if_true) {
7227 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
7228 bb->b_nofallthrough = 1;
7229 }
7230 else {
7231 inst->i_opcode = NOP;
7232 bb->b_instr[i+1].i_opcode = NOP;
7233 }
7234 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007235 }
7236 break;
Mark Shannon266b4622020-11-17 19:30:14 +00007237 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007238
7239 /* Try to fold tuples of constants.
7240 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
7241 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
7242 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
7243 case BUILD_TUPLE:
7244 if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
7245 switch(oparg) {
7246 case 1:
7247 inst->i_opcode = NOP;
7248 bb->b_instr[i+1].i_opcode = NOP;
7249 break;
7250 case 2:
7251 inst->i_opcode = ROT_TWO;
7252 bb->b_instr[i+1].i_opcode = NOP;
7253 break;
7254 case 3:
7255 inst->i_opcode = ROT_THREE;
7256 bb->b_instr[i+1].i_opcode = ROT_TWO;
7257 }
7258 break;
7259 }
7260 if (i >= oparg) {
Inada Naoki8a232c72021-04-16 14:01:04 +09007261 if (fold_tuple_on_constants(c, inst-oparg, oparg, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007262 goto error;
7263 }
7264 }
7265 break;
7266
7267 /* Simplify conditional jump to conditional jump where the
7268 result of the first test implies the success of a similar
7269 test or the failure of the opposite test.
7270 Arises in code like:
7271 "a and b or c"
7272 "(a and b) and c"
7273 "(a or b) or c"
7274 "(a or b) and c"
7275 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
7276 --> x:JUMP_IF_FALSE_OR_POP z
7277 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
7278 --> x:POP_JUMP_IF_FALSE y+1
7279 where y+1 is the instruction following the second test.
7280 */
7281 case JUMP_IF_FALSE_OR_POP:
7282 switch(target->i_opcode) {
7283 case POP_JUMP_IF_FALSE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007284 if (inst->i_lineno == target->i_lineno) {
7285 *inst = *target;
7286 i--;
7287 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007288 break;
7289 case JUMP_ABSOLUTE:
7290 case JUMP_FORWARD:
7291 case JUMP_IF_FALSE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00007292 if (inst->i_lineno == target->i_lineno &&
7293 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00007294 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007295 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007296 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007297 break;
7298 case JUMP_IF_TRUE_OR_POP:
7299 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00007300 if (inst->i_lineno == target->i_lineno) {
7301 inst->i_opcode = POP_JUMP_IF_FALSE;
7302 inst->i_target = inst->i_target->b_next;
7303 --i;
7304 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007305 break;
7306 }
7307 break;
7308
7309 case JUMP_IF_TRUE_OR_POP:
7310 switch(target->i_opcode) {
7311 case POP_JUMP_IF_TRUE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007312 if (inst->i_lineno == target->i_lineno) {
7313 *inst = *target;
7314 i--;
7315 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007316 break;
7317 case JUMP_ABSOLUTE:
7318 case JUMP_FORWARD:
7319 case JUMP_IF_TRUE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00007320 if (inst->i_lineno == target->i_lineno &&
7321 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00007322 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007323 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007324 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007325 break;
7326 case JUMP_IF_FALSE_OR_POP:
7327 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00007328 if (inst->i_lineno == target->i_lineno) {
7329 inst->i_opcode = POP_JUMP_IF_TRUE;
7330 inst->i_target = inst->i_target->b_next;
7331 --i;
7332 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007333 break;
7334 }
7335 break;
7336
7337 case POP_JUMP_IF_FALSE:
7338 switch(target->i_opcode) {
7339 case JUMP_ABSOLUTE:
7340 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007341 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00007342 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007343 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007344 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007345 break;
7346 }
7347 break;
7348
7349 case POP_JUMP_IF_TRUE:
7350 switch(target->i_opcode) {
7351 case JUMP_ABSOLUTE:
7352 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007353 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00007354 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007355 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007356 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007357 break;
7358 }
7359 break;
7360
7361 case JUMP_ABSOLUTE:
7362 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007363 assert (i == bb->b_iused-1);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007364 switch(target->i_opcode) {
7365 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007366 if (eliminate_jump_to_jump(bb, inst->i_opcode)) {
7367 goto error;
Mark Shannon266b4622020-11-17 19:30:14 +00007368 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007369 break;
Mark Shannon28b75c82020-12-23 11:43:10 +00007370
Mark Shannon6e8128f2020-07-30 10:03:00 +01007371 case JUMP_ABSOLUTE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007372 if (eliminate_jump_to_jump(bb, JUMP_ABSOLUTE)) {
7373 goto error;
Mark Shannon266b4622020-11-17 19:30:14 +00007374 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007375 break;
Mark Shannon28b75c82020-12-23 11:43:10 +00007376 default:
7377 if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) {
7378 basicblock *to_copy = inst->i_target;
7379 inst->i_opcode = NOP;
7380 for (i = 0; i < to_copy->b_iused; i++) {
7381 int index = compiler_next_instr(bb);
7382 if (index < 0) {
7383 return -1;
7384 }
7385 bb->b_instr[index] = to_copy->b_instr[i];
7386 }
7387 bb->b_exit = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007388 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007389 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07007390 break;
7391 case ROT_N:
7392 switch (oparg) {
7393 case 0:
7394 case 1:
7395 inst->i_opcode = NOP;
7396 continue;
7397 case 2:
7398 inst->i_opcode = ROT_TWO;
7399 break;
7400 case 3:
7401 inst->i_opcode = ROT_THREE;
7402 break;
7403 case 4:
7404 inst->i_opcode = ROT_FOUR;
7405 break;
7406 }
7407 if (i >= oparg - 1) {
7408 fold_rotations(inst - oparg + 1, oparg);
7409 }
7410 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007411 }
7412 }
7413 return 0;
7414error:
7415 return -1;
7416}
7417
7418
7419static void
Mark Shannon1659ad12021-01-13 15:05:04 +00007420clean_basic_block(basicblock *bb, int prev_lineno) {
7421 /* Remove NOPs when legal to do so. */
Mark Shannon6e8128f2020-07-30 10:03:00 +01007422 int dest = 0;
7423 for (int src = 0; src < bb->b_iused; src++) {
Mark Shannon877df852020-11-12 09:43:29 +00007424 int lineno = bb->b_instr[src].i_lineno;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007425 if (bb->b_instr[src].i_opcode == NOP) {
Mark Shannon266b4622020-11-17 19:30:14 +00007426 /* Eliminate no-op if it doesn't have a line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007427 if (lineno < 0) {
7428 continue;
7429 }
Mark Shannon266b4622020-11-17 19:30:14 +00007430 /* or, if the previous instruction had the same line number. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007431 if (prev_lineno == lineno) {
7432 continue;
7433 }
Mark Shannon266b4622020-11-17 19:30:14 +00007434 /* or, if the next instruction has same line number or no line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007435 if (src < bb->b_iused - 1) {
7436 int next_lineno = bb->b_instr[src+1].i_lineno;
7437 if (next_lineno < 0 || next_lineno == lineno) {
7438 bb->b_instr[src+1].i_lineno = lineno;
7439 continue;
Mark Shannon877df852020-11-12 09:43:29 +00007440 }
7441 }
Mark Shannon266b4622020-11-17 19:30:14 +00007442 else {
7443 basicblock* next = bb->b_next;
7444 while (next && next->b_iused == 0) {
7445 next = next->b_next;
7446 }
7447 /* or if last instruction in BB and next BB has same line number */
7448 if (next) {
7449 if (lineno == next->b_instr[0].i_lineno) {
7450 continue;
7451 }
7452 }
7453 }
7454
Mark Shannon6e8128f2020-07-30 10:03:00 +01007455 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007456 if (dest != src) {
7457 bb->b_instr[dest] = bb->b_instr[src];
7458 }
7459 dest++;
7460 prev_lineno = lineno;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007461 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007462 assert(dest <= bb->b_iused);
7463 bb->b_iused = dest;
7464}
7465
Mark Shannon266b4622020-11-17 19:30:14 +00007466static int
7467normalize_basic_block(basicblock *bb) {
7468 /* Mark blocks as exit and/or nofallthrough.
7469 Raise SystemError if CFG is malformed. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007470 for (int i = 0; i < bb->b_iused; i++) {
7471 switch(bb->b_instr[i].i_opcode) {
7472 case RETURN_VALUE:
7473 case RAISE_VARARGS:
7474 case RERAISE:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007475 bb->b_exit = 1;
Mark Shannon5977a792020-12-02 13:31:40 +00007476 bb->b_nofallthrough = 1;
7477 break;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007478 case JUMP_ABSOLUTE:
7479 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007480 bb->b_nofallthrough = 1;
Mark Shannon266b4622020-11-17 19:30:14 +00007481 /* fall through */
7482 case POP_JUMP_IF_FALSE:
7483 case POP_JUMP_IF_TRUE:
7484 case JUMP_IF_FALSE_OR_POP:
7485 case JUMP_IF_TRUE_OR_POP:
Mark Shannon5977a792020-12-02 13:31:40 +00007486 case FOR_ITER:
Mark Shannon266b4622020-11-17 19:30:14 +00007487 if (i != bb->b_iused-1) {
7488 PyErr_SetString(PyExc_SystemError, "malformed control flow graph.");
7489 return -1;
7490 }
Mark Shannon5977a792020-12-02 13:31:40 +00007491 /* Skip over empty basic blocks. */
7492 while (bb->b_instr[i].i_target->b_iused == 0) {
7493 bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next;
7494 }
7495
Mark Shannoncc75ab72020-11-12 19:49:33 +00007496 }
7497 }
Mark Shannon266b4622020-11-17 19:30:14 +00007498 return 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007499}
7500
Mark Shannon6e8128f2020-07-30 10:03:00 +01007501static int
7502mark_reachable(struct assembler *a) {
7503 basicblock **stack, **sp;
7504 sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks);
7505 if (stack == NULL) {
7506 return -1;
7507 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007508 a->a_entry->b_predecessors = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007509 *sp++ = a->a_entry;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007510 while (sp > stack) {
7511 basicblock *b = *(--sp);
Mark Shannon3bd60352021-01-13 12:05:43 +00007512 if (b->b_next && !b->b_nofallthrough) {
7513 if (b->b_next->b_predecessors == 0) {
7514 *sp++ = b->b_next;
7515 }
7516 b->b_next->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007517 }
7518 for (int i = 0; i < b->b_iused; i++) {
7519 basicblock *target;
Mark Shannon582aaf12020-08-04 17:30:11 +01007520 if (is_jump(&b->b_instr[i])) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007521 target = b->b_instr[i].i_target;
Mark Shannon3bd60352021-01-13 12:05:43 +00007522 if (target->b_predecessors == 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007523 *sp++ = target;
7524 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007525 target->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007526 }
7527 }
7528 }
7529 PyObject_Free(stack);
7530 return 0;
7531}
7532
Mark Shannon3bd60352021-01-13 12:05:43 +00007533static void
7534eliminate_empty_basic_blocks(basicblock *entry) {
7535 /* Eliminate empty blocks */
7536 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7537 basicblock *next = b->b_next;
7538 if (next) {
7539 while (next->b_iused == 0 && next->b_next) {
7540 next = next->b_next;
7541 }
7542 b->b_next = next;
7543 }
7544 }
7545 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7546 if (b->b_iused == 0) {
7547 continue;
7548 }
7549 if (is_jump(&b->b_instr[b->b_iused-1])) {
7550 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7551 while (target->b_iused == 0) {
7552 target = target->b_next;
7553 }
7554 b->b_instr[b->b_iused-1].i_target = target;
7555 }
7556 }
7557}
7558
7559
Mark Shannon5977a792020-12-02 13:31:40 +00007560/* If an instruction has no line number, but it's predecessor in the BB does,
Mark Shannon3bd60352021-01-13 12:05:43 +00007561 * then copy the line number. If a successor block has no line number, and only
7562 * one predecessor, then inherit the line number.
7563 * This ensures that all exit blocks (with one predecessor) receive a line number.
7564 * Also reduces the size of the line number table,
Mark Shannon5977a792020-12-02 13:31:40 +00007565 * but has no impact on the generated line number events.
7566 */
7567static void
Mark Shannon3bd60352021-01-13 12:05:43 +00007568propogate_line_numbers(struct assembler *a) {
Mark Shannon5977a792020-12-02 13:31:40 +00007569 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007570 if (b->b_iused == 0) {
7571 continue;
7572 }
Mark Shannon5977a792020-12-02 13:31:40 +00007573 int prev_lineno = -1;
7574 for (int i = 0; i < b->b_iused; i++) {
7575 if (b->b_instr[i].i_lineno < 0) {
7576 b->b_instr[i].i_lineno = prev_lineno;
7577 }
7578 else {
7579 prev_lineno = b->b_instr[i].i_lineno;
7580 }
7581 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007582 if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) {
7583 assert(b->b_next->b_iused);
7584 if (b->b_next->b_instr[0].i_lineno < 0) {
7585 b->b_next->b_instr[0].i_lineno = prev_lineno;
7586 }
7587 }
7588 if (is_jump(&b->b_instr[b->b_iused-1])) {
7589 switch (b->b_instr[b->b_iused-1].i_opcode) {
7590 /* Note: Only actual jumps, not exception handlers */
7591 case SETUP_ASYNC_WITH:
7592 case SETUP_WITH:
7593 case SETUP_FINALLY:
7594 continue;
7595 }
7596 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7597 if (target->b_predecessors == 1) {
7598 if (target->b_instr[0].i_lineno < 0) {
7599 target->b_instr[0].i_lineno = prev_lineno;
7600 }
7601 }
7602 }
Mark Shannon5977a792020-12-02 13:31:40 +00007603 }
7604}
7605
7606/* Perform optimizations on a control flow graph.
Mark Shannon6e8128f2020-07-30 10:03:00 +01007607 The consts object should still be in list form to allow new constants
7608 to be appended.
7609
7610 All transformations keep the code size the same or smaller.
7611 For those that reduce size, the gaps are initially filled with
7612 NOPs. Later those NOPs are removed.
7613*/
7614
7615static int
Inada Naoki8a232c72021-04-16 14:01:04 +09007616optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts)
Mark Shannon6e8128f2020-07-30 10:03:00 +01007617{
Mark Shannoncc75ab72020-11-12 19:49:33 +00007618 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Inada Naoki8a232c72021-04-16 14:01:04 +09007619 if (optimize_basic_block(c, b, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007620 return -1;
7621 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007622 clean_basic_block(b, -1);
Mark Shannon3bd60352021-01-13 12:05:43 +00007623 assert(b->b_predecessors == 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007624 }
7625 if (mark_reachable(a)) {
7626 return -1;
7627 }
7628 /* Delete unreachable instructions */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007629 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007630 if (b->b_predecessors == 0) {
Mark Shannoncc75ab72020-11-12 19:49:33 +00007631 b->b_iused = 0;
Om Gc71581c2020-12-16 17:48:05 +05307632 b->b_nofallthrough = 0;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007633 }
7634 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007635 basicblock *pred = NULL;
7636 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7637 int prev_lineno = -1;
7638 if (pred && pred->b_iused) {
7639 prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno;
7640 }
7641 clean_basic_block(b, prev_lineno);
7642 pred = b->b_nofallthrough ? NULL : b;
7643 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007644 eliminate_empty_basic_blocks(a->a_entry);
Om Gc71581c2020-12-16 17:48:05 +05307645 /* Delete jump instructions made redundant by previous step. If a non-empty
7646 block ends with a jump instruction, check if the next non-empty block
7647 reached through normal flow control is the target of that jump. If it
7648 is, then the jump instruction is redundant and can be deleted.
7649 */
Mark Shannon3bd60352021-01-13 12:05:43 +00007650 int maybe_empty_blocks = 0;
Om Gc71581c2020-12-16 17:48:05 +05307651 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7652 if (b->b_iused > 0) {
7653 struct instr *b_last_instr = &b->b_instr[b->b_iused - 1];
Mark Shannon802b6452021-02-02 14:59:15 +00007654 if (b_last_instr->i_opcode == JUMP_ABSOLUTE ||
Om Gc71581c2020-12-16 17:48:05 +05307655 b_last_instr->i_opcode == JUMP_FORWARD) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007656 if (b_last_instr->i_target == b->b_next) {
7657 assert(b->b_next->b_iused);
Om Gc71581c2020-12-16 17:48:05 +05307658 b->b_nofallthrough = 0;
Mark Shannon802b6452021-02-02 14:59:15 +00007659 b_last_instr->i_opcode = NOP;
7660 clean_basic_block(b, -1);
7661 maybe_empty_blocks = 1;
Om Gc71581c2020-12-16 17:48:05 +05307662 }
7663 }
7664 }
7665 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007666 if (maybe_empty_blocks) {
7667 eliminate_empty_basic_blocks(a->a_entry);
7668 }
7669 propogate_line_numbers(a);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007670 return 0;
7671}
7672
Mark Shannon5977a792020-12-02 13:31:40 +00007673static inline int
7674is_exit_without_lineno(basicblock *b) {
7675 return b->b_exit && b->b_instr[0].i_lineno < 0;
7676}
7677
7678/* PEP 626 mandates that the f_lineno of a frame is correct
7679 * after a frame terminates. It would be prohibitively expensive
7680 * to continuously update the f_lineno field at runtime,
7681 * so we make sure that all exiting instruction (raises and returns)
7682 * have a valid line number, allowing us to compute f_lineno lazily.
7683 * We can do this by duplicating the exit blocks without line number
7684 * so that none have more than one predecessor. We can then safely
7685 * copy the line number from the sole predecessor block.
7686 */
7687static int
7688ensure_exits_have_lineno(struct compiler *c)
7689{
Mark Shannoneaccc122020-12-04 15:22:12 +00007690 basicblock *entry = NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00007691 /* Copy all exit blocks without line number that are targets of a jump.
7692 */
7693 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7694 if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) {
7695 switch (b->b_instr[b->b_iused-1].i_opcode) {
7696 /* Note: Only actual jumps, not exception handlers */
7697 case SETUP_ASYNC_WITH:
7698 case SETUP_WITH:
7699 case SETUP_FINALLY:
7700 continue;
7701 }
7702 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7703 if (is_exit_without_lineno(target)) {
7704 basicblock *new_target = compiler_copy_block(c, target);
7705 if (new_target == NULL) {
7706 return -1;
7707 }
7708 new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7709 b->b_instr[b->b_iused-1].i_target = new_target;
7710 }
7711 }
Mark Shannoneaccc122020-12-04 15:22:12 +00007712 entry = b;
7713 }
7714 assert(entry != NULL);
7715 if (is_exit_without_lineno(entry)) {
7716 entry->b_instr[0].i_lineno = c->u->u_firstlineno;
Mark Shannon5977a792020-12-02 13:31:40 +00007717 }
Mark Shannonee9f98d2021-01-05 12:04:10 +00007718 /* Eliminate empty blocks */
7719 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7720 while (b->b_next && b->b_next->b_iused == 0) {
7721 b->b_next = b->b_next->b_next;
7722 }
7723 }
Mark Shannon5977a792020-12-02 13:31:40 +00007724 /* Any remaining reachable exit blocks without line number can only be reached by
7725 * fall through, and thus can only have a single predecessor */
7726 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7727 if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) {
7728 if (is_exit_without_lineno(b->b_next)) {
7729 assert(b->b_next->b_iused > 0);
7730 b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7731 }
7732 }
7733 }
7734 return 0;
7735}
7736
7737
Mark Shannon6e8128f2020-07-30 10:03:00 +01007738/* Retained for API compatibility.
7739 * Optimization is now done in optimize_cfg */
7740
7741PyObject *
7742PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
7743 PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
7744{
7745 Py_INCREF(code);
7746 return code;
7747}