blob: b007859bd2e732dd3a84df3b7537913c07729bde [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);
Mark Shannon762ef852021-08-09 10:54:48 +0100865 basicblock *result = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +0000866 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) { \
Miss Islington (bot)ebbd0ac2021-08-31 11:08:32 -07001571 assert((OP) != LOAD_CONST); /* use ADDOP_LOAD_CONST */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1573 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001574}
1575
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001576/* Same as ADDOP_O, but steals a reference. */
1577#define ADDOP_N(C, OP, O, TYPE) { \
Miss Islington (bot)ebbd0ac2021-08-31 11:08:32 -07001578 assert((OP) != LOAD_CONST); /* use ADDOP_LOAD_CONST_NEW */ \
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001579 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1580 Py_DECREF((O)); \
1581 return 0; \
1582 } \
1583 Py_DECREF((O)); \
1584}
1585
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1588 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589}
1590
1591#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 if (!compiler_addop_i((C), (OP), (O))) \
1593 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001594}
1595
Mark Shannon11e0b292021-04-15 14:28:56 +01001596#define ADDOP_I_NOLINE(C, OP, O) { \
1597 if (!compiler_addop_i_noline((C), (OP), (O))) \
1598 return 0; \
1599}
1600
Mark Shannon582aaf12020-08-04 17:30:11 +01001601#define ADDOP_JUMP(C, OP, O) { \
1602 if (!compiler_addop_j((C), (OP), (O))) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001604}
1605
Mark Shannon127dde52021-01-04 18:06:55 +00001606/* Add a jump with no line number.
1607 * Used for artificial jumps that have no corresponding
1608 * token in the source code. */
1609#define ADDOP_JUMP_NOLINE(C, OP, O) { \
1610 if (!compiler_addop_j_noline((C), (OP), (O))) \
1611 return 0; \
1612}
1613
Mark Shannon9af0e472020-01-14 10:12:45 +00001614#define ADDOP_COMPARE(C, CMP) { \
1615 if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \
1616 return 0; \
1617}
1618
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001619/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1620 the ASDL name to synthesize the name of the C type and the visit function.
1621*/
1622
1623#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 if (!compiler_visit_ ## TYPE((C), (V))) \
1625 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626}
1627
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001628#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 if (!compiler_visit_ ## TYPE((C), (V))) { \
1630 compiler_exit_scope(c); \
1631 return 0; \
1632 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001633}
1634
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001635#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 if (!compiler_visit_slice((C), (V), (CTX))) \
1637 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001638}
1639
1640#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001642 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1644 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1645 if (!compiler_visit_ ## TYPE((C), elt)) \
1646 return 0; \
1647 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001648}
1649
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001650#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001652 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1654 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1655 if (!compiler_visit_ ## TYPE((C), elt)) { \
1656 compiler_exit_scope(c); \
1657 return 0; \
1658 } \
1659 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001660}
1661
Brandt Bucher145bf262021-02-26 14:51:55 -08001662#define RETURN_IF_FALSE(X) \
1663 if (!(X)) { \
1664 return 0; \
1665 }
1666
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001667/* Search if variable annotations are present statically in a block. */
1668
1669static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001670find_ann(asdl_stmt_seq *stmts)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001671{
1672 int i, j, res = 0;
1673 stmt_ty st;
1674
1675 for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1676 st = (stmt_ty)asdl_seq_GET(stmts, i);
1677 switch (st->kind) {
1678 case AnnAssign_kind:
1679 return 1;
1680 case For_kind:
1681 res = find_ann(st->v.For.body) ||
1682 find_ann(st->v.For.orelse);
1683 break;
1684 case AsyncFor_kind:
1685 res = find_ann(st->v.AsyncFor.body) ||
1686 find_ann(st->v.AsyncFor.orelse);
1687 break;
1688 case While_kind:
1689 res = find_ann(st->v.While.body) ||
1690 find_ann(st->v.While.orelse);
1691 break;
1692 case If_kind:
1693 res = find_ann(st->v.If.body) ||
1694 find_ann(st->v.If.orelse);
1695 break;
1696 case With_kind:
1697 res = find_ann(st->v.With.body);
1698 break;
1699 case AsyncWith_kind:
1700 res = find_ann(st->v.AsyncWith.body);
1701 break;
1702 case Try_kind:
1703 for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1704 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1705 st->v.Try.handlers, j);
1706 if (find_ann(handler->v.ExceptHandler.body)) {
1707 return 1;
1708 }
1709 }
1710 res = find_ann(st->v.Try.body) ||
1711 find_ann(st->v.Try.finalbody) ||
1712 find_ann(st->v.Try.orelse);
1713 break;
1714 default:
1715 res = 0;
1716 }
1717 if (res) {
1718 break;
1719 }
1720 }
1721 return res;
1722}
1723
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001724/*
1725 * Frame block handling functions
1726 */
1727
1728static int
1729compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b,
Mark Shannonfee55262019-11-21 09:11:43 +00001730 basicblock *exit, void *datum)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001731{
1732 struct fblockinfo *f;
1733 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
Mark Shannon02d126a2020-09-25 14:04:19 +01001734 return compiler_error(c, "too many statically nested blocks");
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001735 }
1736 f = &c->u->u_fblock[c->u->u_nfblocks++];
1737 f->fb_type = t;
1738 f->fb_block = b;
1739 f->fb_exit = exit;
Mark Shannonfee55262019-11-21 09:11:43 +00001740 f->fb_datum = datum;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001741 return 1;
1742}
1743
1744static void
1745compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
1746{
1747 struct compiler_unit *u = c->u;
1748 assert(u->u_nfblocks > 0);
1749 u->u_nfblocks--;
1750 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
1751 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
1752}
1753
Mark Shannonfee55262019-11-21 09:11:43 +00001754static int
1755compiler_call_exit_with_nones(struct compiler *c) {
Miss Islington (bot)ebbd0ac2021-08-31 11:08:32 -07001756 ADDOP_LOAD_CONST(c, Py_None);
Mark Shannonfee55262019-11-21 09:11:43 +00001757 ADDOP(c, DUP_TOP);
1758 ADDOP(c, DUP_TOP);
1759 ADDOP_I(c, CALL_FUNCTION, 3);
1760 return 1;
1761}
1762
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001763/* Unwind a frame block. If preserve_tos is true, the TOS before
Mark Shannonfee55262019-11-21 09:11:43 +00001764 * popping the blocks will be restored afterwards, unless another
1765 * return, break or continue is found. In which case, the TOS will
1766 * be popped.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001767 */
1768static int
1769compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
1770 int preserve_tos)
1771{
1772 switch (info->fb_type) {
1773 case WHILE_LOOP:
Mark Shannon02d126a2020-09-25 14:04:19 +01001774 case EXCEPTION_HANDLER:
tomKPZ7a7ba3d2021-04-07 07:43:45 -07001775 case ASYNC_COMPREHENSION_GENERATOR:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001776 return 1;
1777
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001778 case FOR_LOOP:
1779 /* Pop the iterator */
1780 if (preserve_tos) {
1781 ADDOP(c, ROT_TWO);
1782 }
1783 ADDOP(c, POP_TOP);
1784 return 1;
1785
Mark Shannon02d126a2020-09-25 14:04:19 +01001786 case TRY_EXCEPT:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001787 ADDOP(c, POP_BLOCK);
1788 return 1;
1789
1790 case FINALLY_TRY:
Mark Shannon5274b682020-12-16 13:07:01 +00001791 /* This POP_BLOCK gets the line number of the unwinding statement */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001792 ADDOP(c, POP_BLOCK);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001793 if (preserve_tos) {
Mark Shannonfee55262019-11-21 09:11:43 +00001794 if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) {
1795 return 0;
1796 }
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001797 }
Mark Shannon5274b682020-12-16 13:07:01 +00001798 /* Emit the finally block */
Mark Shannonfee55262019-11-21 09:11:43 +00001799 VISIT_SEQ(c, stmt, info->fb_datum);
1800 if (preserve_tos) {
1801 compiler_pop_fblock(c, POP_VALUE, NULL);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001802 }
Mark Shannon5274b682020-12-16 13:07:01 +00001803 /* The finally block should appear to execute after the
1804 * statement causing the unwinding, so make the unwinding
1805 * instruction artificial */
1806 c->u->u_lineno = -1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001807 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001808
Mark Shannonfee55262019-11-21 09:11:43 +00001809 case FINALLY_END:
1810 if (preserve_tos) {
1811 ADDOP(c, ROT_FOUR);
1812 }
1813 ADDOP(c, POP_TOP);
1814 ADDOP(c, POP_TOP);
1815 ADDOP(c, POP_TOP);
1816 if (preserve_tos) {
1817 ADDOP(c, ROT_FOUR);
1818 }
1819 ADDOP(c, POP_EXCEPT);
1820 return 1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001821
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001822 case WITH:
1823 case ASYNC_WITH:
Mark Shannon5979e812021-04-30 14:32:47 +01001824 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 Shannoncea05852021-06-03 19:57:31 +01001838 /* The exit block should appear to execute after the
1839 * statement causing the unwinding, so make the unwinding
1840 * instruction artificial */
1841 c->u->u_lineno = -1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001842 return 1;
1843
1844 case HANDLER_CLEANUP:
Mark Shannonfee55262019-11-21 09:11:43 +00001845 if (info->fb_datum) {
1846 ADDOP(c, POP_BLOCK);
1847 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001848 if (preserve_tos) {
1849 ADDOP(c, ROT_FOUR);
1850 }
Mark Shannonfee55262019-11-21 09:11:43 +00001851 ADDOP(c, POP_EXCEPT);
1852 if (info->fb_datum) {
1853 ADDOP_LOAD_CONST(c, Py_None);
1854 compiler_nameop(c, info->fb_datum, Store);
1855 compiler_nameop(c, info->fb_datum, Del);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001856 }
Mark Shannonfee55262019-11-21 09:11:43 +00001857 return 1;
1858
1859 case POP_VALUE:
1860 if (preserve_tos) {
1861 ADDOP(c, ROT_TWO);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001862 }
Mark Shannonfee55262019-11-21 09:11:43 +00001863 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001864 return 1;
1865 }
1866 Py_UNREACHABLE();
1867}
1868
Mark Shannonfee55262019-11-21 09:11:43 +00001869/** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */
1870static int
1871compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) {
1872 if (c->u->u_nfblocks == 0) {
1873 return 1;
1874 }
1875 struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1];
1876 if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) {
1877 *loop = top;
1878 return 1;
1879 }
1880 struct fblockinfo copy = *top;
1881 c->u->u_nfblocks--;
1882 if (!compiler_unwind_fblock(c, &copy, preserve_tos)) {
1883 return 0;
1884 }
1885 if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) {
1886 return 0;
1887 }
1888 c->u->u_fblock[c->u->u_nfblocks] = copy;
1889 c->u->u_nfblocks++;
1890 return 1;
1891}
1892
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001893/* Compile a sequence of statements, checking for a docstring
1894 and for annotations. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001895
1896static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001897compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001898{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001899 int i = 0;
1900 stmt_ty st;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001901 PyObject *docstring;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001902
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001903 /* Set current line number to the line number of first statement.
1904 This way line number for SETUP_ANNOTATIONS will always
1905 coincide with the line number of first "real" statement in module.
Hansraj Das01171eb2019-10-09 07:54:02 +05301906 If body is empty, then lineno will be set later in assemble. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001907 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) {
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001908 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001909 SET_LOC(c, st);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001910 }
1911 /* Every annotated class and module should have __annotations__. */
1912 if (find_ann(stmts)) {
1913 ADDOP(c, SETUP_ANNOTATIONS);
1914 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001915 if (!asdl_seq_LEN(stmts))
1916 return 1;
INADA Naokicb41b272017-02-23 00:31:59 +09001917 /* if not -OO mode, set docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001918 if (c->c_optimize < 2) {
1919 docstring = _PyAST_GetDocString(stmts);
1920 if (docstring) {
1921 i = 1;
1922 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1923 assert(st->kind == Expr_kind);
1924 VISIT(c, expr, st->v.Expr.value);
1925 if (!compiler_nameop(c, __doc__, Store))
1926 return 0;
1927 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001929 for (; i < asdl_seq_LEN(stmts); i++)
1930 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001932}
1933
1934static PyCodeObject *
1935compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 PyCodeObject *co;
1938 int addNone = 1;
1939 static PyObject *module;
1940 if (!module) {
1941 module = PyUnicode_InternFromString("<module>");
1942 if (!module)
1943 return NULL;
1944 }
1945 /* Use 0 for firstlineno initially, will fixup in assemble(). */
Mark Shannon877df852020-11-12 09:43:29 +00001946 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 return NULL;
1948 switch (mod->kind) {
1949 case Module_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001950 if (!compiler_body(c, mod->v.Module.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 compiler_exit_scope(c);
1952 return 0;
1953 }
1954 break;
1955 case Interactive_kind:
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001956 if (find_ann(mod->v.Interactive.body)) {
1957 ADDOP(c, SETUP_ANNOTATIONS);
1958 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 c->c_interactive = 1;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001960 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 break;
1962 case Expression_kind:
1963 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1964 addNone = 0;
1965 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 default:
1967 PyErr_Format(PyExc_SystemError,
1968 "module kind %d should not be possible",
1969 mod->kind);
1970 return 0;
1971 }
1972 co = assemble(c, addNone);
1973 compiler_exit_scope(c);
1974 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001975}
1976
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001977/* The test for LOCAL must come before the test for FREE in order to
1978 handle classes where name is both local and free. The local var is
1979 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001980*/
1981
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001982static int
1983get_ref_type(struct compiler *c, PyObject *name)
1984{
Victor Stinner0b1bc562013-05-16 22:17:17 +02001985 int scope;
Benjamin Peterson312595c2013-05-15 15:26:42 -05001986 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001987 _PyUnicode_EqualToASCIIString(name, "__class__"))
Benjamin Peterson312595c2013-05-15 15:26:42 -05001988 return CELL;
Victor Stinner28ad12f2021-03-19 12:41:49 +01001989 scope = _PyST_GetScope(c->u->u_ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 if (scope == 0) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001991 PyErr_Format(PyExc_SystemError,
Victor Stinner28ad12f2021-03-19 12:41:49 +01001992 "_PyST_GetScope(name=%R) failed: "
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001993 "unknown scope in unit %S (%R); "
1994 "symbols: %R; locals: %R; globals: %R",
1995 name,
1996 c->u->u_name, c->u->u_ste->ste_id,
1997 c->u->u_ste->ste_symbols, c->u->u_varnames, c->u->u_names);
1998 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002001}
2002
2003static int
2004compiler_lookup_arg(PyObject *dict, PyObject *name)
2005{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002006 PyObject *v;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02002007 v = PyDict_GetItemWithError(dict, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002008 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00002009 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00002010 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011}
2012
2013static int
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002014compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags,
2015 PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002016{
Victor Stinnerad9a0662013-11-19 22:23:20 +01002017 Py_ssize_t i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002018 if (qualname == NULL)
2019 qualname = co->co_name;
2020
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002021 if (free) {
2022 for (i = 0; i < free; ++i) {
2023 /* Bypass com_addop_varname because it will generate
2024 LOAD_DEREF but LOAD_CLOSURE is needed.
2025 */
2026 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002027
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002028 /* Special case: If a class contains a method with a
2029 free variable that has the same name as a method,
2030 the name will be considered free *and* local in the
2031 class. It should be handled by the closure, as
Min ho Kimc4cacc82019-07-31 08:16:13 +10002032 well as by the normal name lookup logic.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002033 */
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002034 int reftype = get_ref_type(c, name);
2035 if (reftype == -1) {
2036 return 0;
2037 }
2038 int arg;
2039 if (reftype == CELL) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002040 arg = compiler_lookup_arg(c->u->u_cellvars, name);
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002041 }
2042 else {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002043 arg = compiler_lookup_arg(c->u->u_freevars, name);
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002044 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002045 if (arg == -1) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002046 PyErr_Format(PyExc_SystemError,
2047 "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; "
2048 "freevars of code %S: %R",
2049 name,
2050 reftype,
2051 c->u->u_name,
2052 co->co_name,
2053 co->co_freevars);
2054 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002055 }
2056 ADDOP_I(c, LOAD_CLOSURE, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002058 flags |= 0x08;
2059 ADDOP_I(c, BUILD_TUPLE, free);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002061 ADDOP_LOAD_CONST(c, (PyObject*)co);
2062 ADDOP_LOAD_CONST(c, qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002063 ADDOP_I(c, MAKE_FUNCTION, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002065}
2066
2067static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002068compiler_decorators(struct compiler *c, asdl_expr_seq* decos)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 if (!decos)
2073 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2076 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
2077 }
2078 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002079}
2080
2081static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002082compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs,
2083 asdl_expr_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00002084{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002085 /* Push a dict of keyword-only default values.
2086
2087 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
2088 */
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002089 int i;
2090 PyObject *keys = NULL;
2091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002092 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
2093 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
2094 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
2095 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04002096 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002097 if (!mangled) {
2098 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002100 if (keys == NULL) {
2101 keys = PyList_New(1);
2102 if (keys == NULL) {
2103 Py_DECREF(mangled);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002104 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002105 }
2106 PyList_SET_ITEM(keys, 0, mangled);
2107 }
2108 else {
2109 int res = PyList_Append(keys, mangled);
2110 Py_DECREF(mangled);
2111 if (res == -1) {
2112 goto error;
2113 }
2114 }
2115 if (!compiler_visit_expr(c, default_)) {
2116 goto error;
2117 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 }
2119 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002120 if (keys != NULL) {
2121 Py_ssize_t default_count = PyList_GET_SIZE(keys);
2122 PyObject *keys_tuple = PyList_AsTuple(keys);
2123 Py_DECREF(keys);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002124 ADDOP_LOAD_CONST_NEW(c, keys_tuple);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002125 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002126 assert(default_count > 0);
2127 return 1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002128 }
2129 else {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002130 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002131 }
2132
2133error:
2134 Py_XDECREF(keys);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002135 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002136}
2137
2138static int
Guido van Rossum95e4d582018-01-26 08:20:18 -08002139compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
2140{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +03002141 ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation));
Guido van Rossum95e4d582018-01-26 08:20:18 -08002142 return 1;
2143}
2144
2145static int
Neal Norwitzc1505362006-12-28 06:47:50 +00002146compiler_visit_argannotation(struct compiler *c, identifier id,
Yurii Karabas73019792020-11-25 12:43:18 +02002147 expr_ty annotation, Py_ssize_t *annotations_len)
Neal Norwitzc1505362006-12-28 06:47:50 +00002148{
Pablo Galindob0544ba2021-04-21 12:41:19 +01002149 if (!annotation) {
2150 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151 }
Pablo Galindob0544ba2021-04-21 12:41:19 +01002152
2153 PyObject *mangled = _Py_Mangle(c->u->u_private, id);
2154 if (!mangled) {
2155 return 0;
2156 }
2157 ADDOP_LOAD_CONST(c, mangled);
2158 Py_DECREF(mangled);
2159
2160 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
2161 VISIT(c, annexpr, annotation)
2162 }
2163 else {
2164 VISIT(c, expr, annotation);
2165 }
2166 *annotations_len += 2;
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002167 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002168}
2169
2170static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002171compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args,
Yurii Karabas73019792020-11-25 12:43:18 +02002172 Py_ssize_t *annotations_len)
Neal Norwitzc1505362006-12-28 06:47:50 +00002173{
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002174 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 for (i = 0; i < asdl_seq_LEN(args); i++) {
2176 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002177 if (!compiler_visit_argannotation(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 c,
2179 arg->arg,
2180 arg->annotation,
Yurii Karabas73019792020-11-25 12:43:18 +02002181 annotations_len))
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002182 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002184 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002185}
2186
2187static int
2188compiler_visit_annotations(struct compiler *c, arguments_ty args,
2189 expr_ty returns)
2190{
Yurii Karabas73019792020-11-25 12:43:18 +02002191 /* Push arg annotation names and values.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002192 The expressions are evaluated out-of-order wrt the source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00002193
Yurii Karabas73019792020-11-25 12:43:18 +02002194 Return 0 on error, -1 if no annotations pushed, 1 if a annotations is pushed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 */
2196 static identifier return_str;
Yurii Karabas73019792020-11-25 12:43:18 +02002197 Py_ssize_t annotations_len = 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002198
Yurii Karabas73019792020-11-25 12:43:18 +02002199 if (!compiler_visit_argannotations(c, args->args, &annotations_len))
2200 return 0;
2201 if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len))
2202 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002203 if (args->vararg && args->vararg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002204 !compiler_visit_argannotation(c, args->vararg->arg,
Yurii Karabas73019792020-11-25 12:43:18 +02002205 args->vararg->annotation, &annotations_len))
2206 return 0;
2207 if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len))
2208 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002209 if (args->kwarg && args->kwarg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002210 !compiler_visit_argannotation(c, args->kwarg->arg,
Yurii Karabas73019792020-11-25 12:43:18 +02002211 args->kwarg->annotation, &annotations_len))
2212 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 if (!return_str) {
2215 return_str = PyUnicode_InternFromString("return");
2216 if (!return_str)
Yurii Karabas73019792020-11-25 12:43:18 +02002217 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 }
Yurii Karabas73019792020-11-25 12:43:18 +02002219 if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) {
2220 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 }
2222
Yurii Karabas73019792020-11-25 12:43:18 +02002223 if (annotations_len) {
2224 ADDOP_I(c, BUILD_TUPLE, annotations_len);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002225 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002227
Yurii Karabas73019792020-11-25 12:43:18 +02002228 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002229}
2230
2231static int
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002232compiler_visit_defaults(struct compiler *c, arguments_ty args)
2233{
2234 VISIT_SEQ(c, expr, args->defaults);
2235 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
2236 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002237}
2238
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002239static Py_ssize_t
2240compiler_default_arguments(struct compiler *c, arguments_ty args)
2241{
2242 Py_ssize_t funcflags = 0;
2243 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002244 if (!compiler_visit_defaults(c, args))
2245 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002246 funcflags |= 0x01;
2247 }
2248 if (args->kwonlyargs) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002249 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002250 args->kw_defaults);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002251 if (res == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002252 return -1;
2253 }
2254 else if (res > 0) {
2255 funcflags |= 0x02;
2256 }
2257 }
2258 return funcflags;
2259}
2260
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002261static int
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002262forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx)
2263{
2264
2265 if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2266 compiler_error(c, "cannot assign to __debug__");
2267 return 1;
2268 }
Dong-hee Na32c1caa2021-08-26 09:52:21 +00002269 if (ctx == Del && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2270 compiler_error(c, "cannot delete __debug__");
2271 return 1;
2272 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002273 return 0;
2274}
2275
2276static int
2277compiler_check_debug_one_arg(struct compiler *c, arg_ty arg)
2278{
2279 if (arg != NULL) {
2280 if (forbidden_name(c, arg->arg, Store))
2281 return 0;
2282 }
2283 return 1;
2284}
2285
2286static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002287compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args)
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002288{
2289 if (args != NULL) {
Pablo Galindoee40e4b2020-04-23 03:43:08 +01002290 for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002291 if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i)))
2292 return 0;
2293 }
2294 }
2295 return 1;
2296}
2297
2298static int
2299compiler_check_debug_args(struct compiler *c, arguments_ty args)
2300{
2301 if (!compiler_check_debug_args_seq(c, args->posonlyargs))
2302 return 0;
2303 if (!compiler_check_debug_args_seq(c, args->args))
2304 return 0;
2305 if (!compiler_check_debug_one_arg(c, args->vararg))
2306 return 0;
2307 if (!compiler_check_debug_args_seq(c, args->kwonlyargs))
2308 return 0;
2309 if (!compiler_check_debug_one_arg(c, args->kwarg))
2310 return 0;
2311 return 1;
2312}
2313
2314static int
Yury Selivanov75445082015-05-11 22:57:16 -04002315compiler_function(struct compiler *c, stmt_ty s, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 PyCodeObject *co;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002318 PyObject *qualname, *docstring = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002319 arguments_ty args;
2320 expr_ty returns;
2321 identifier name;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002322 asdl_expr_seq* decos;
2323 asdl_stmt_seq *body;
INADA Naokicb41b272017-02-23 00:31:59 +09002324 Py_ssize_t i, funcflags;
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002325 int annotations;
Yury Selivanov75445082015-05-11 22:57:16 -04002326 int scope_type;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002327 int firstlineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002328
Yury Selivanov75445082015-05-11 22:57:16 -04002329 if (is_async) {
2330 assert(s->kind == AsyncFunctionDef_kind);
2331
2332 args = s->v.AsyncFunctionDef.args;
2333 returns = s->v.AsyncFunctionDef.returns;
2334 decos = s->v.AsyncFunctionDef.decorator_list;
2335 name = s->v.AsyncFunctionDef.name;
2336 body = s->v.AsyncFunctionDef.body;
2337
2338 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
2339 } else {
2340 assert(s->kind == FunctionDef_kind);
2341
2342 args = s->v.FunctionDef.args;
2343 returns = s->v.FunctionDef.returns;
2344 decos = s->v.FunctionDef.decorator_list;
2345 name = s->v.FunctionDef.name;
2346 body = s->v.FunctionDef.body;
2347
2348 scope_type = COMPILER_SCOPE_FUNCTION;
2349 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002350
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002351 if (!compiler_check_debug_args(c, args))
2352 return 0;
2353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 if (!compiler_decorators(c, decos))
2355 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002356
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002357 firstlineno = s->lineno;
2358 if (asdl_seq_LEN(decos)) {
2359 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2360 }
2361
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002362 funcflags = compiler_default_arguments(c, args);
2363 if (funcflags == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002365 }
2366
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002367 annotations = compiler_visit_annotations(c, args, returns);
2368 if (annotations == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002369 return 0;
2370 }
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002371 else if (annotations > 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002372 funcflags |= 0x04;
2373 }
2374
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002375 if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002376 return 0;
2377 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002378
INADA Naokicb41b272017-02-23 00:31:59 +09002379 /* if not -OO mode, add docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002380 if (c->c_optimize < 2) {
2381 docstring = _PyAST_GetDocString(body);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002382 }
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002383 if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002384 compiler_exit_scope(c);
2385 return 0;
2386 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002389 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Mark Shannon877df852020-11-12 09:43:29 +00002391 for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) {
Mark Shannonfd009e62020-11-13 12:53:53 +00002392 VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i));
Mark Shannon877df852020-11-12 09:43:29 +00002393 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002395 qualname = c->u->u_qualname;
2396 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002397 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002398 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002399 Py_XDECREF(qualname);
2400 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002402 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002403
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002404 if (!compiler_make_closure(c, co, funcflags, qualname)) {
2405 Py_DECREF(qualname);
2406 Py_DECREF(co);
2407 return 0;
2408 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002409 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 /* decorators */
2413 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2414 ADDOP_I(c, CALL_FUNCTION, 1);
2415 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002416
Yury Selivanov75445082015-05-11 22:57:16 -04002417 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002418}
2419
2420static int
2421compiler_class(struct compiler *c, stmt_ty s)
2422{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002423 PyCodeObject *co;
2424 PyObject *str;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002425 int i, firstlineno;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002426 asdl_expr_seq *decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 if (!compiler_decorators(c, decos))
2429 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002430
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002431 firstlineno = s->lineno;
2432 if (asdl_seq_LEN(decos)) {
2433 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2434 }
2435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 /* ultimately generate code for:
2437 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
2438 where:
2439 <func> is a function/closure created from the class body;
2440 it has a single argument (__locals__) where the dict
2441 (or MutableSequence) representing the locals is passed
2442 <name> is the class name
2443 <bases> is the positional arguments and *varargs argument
2444 <keywords> is the keyword arguments and **kwds argument
2445 This borrows from compiler_call.
2446 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002449 if (!compiler_enter_scope(c, s->v.ClassDef.name,
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002450 COMPILER_SCOPE_CLASS, (void *)s, firstlineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 return 0;
2452 /* this block represents what we do in the new scope */
2453 {
2454 /* use the class name for name mangling */
2455 Py_INCREF(s->v.ClassDef.name);
Serhiy Storchaka48842712016-04-06 09:45:48 +03002456 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002457 /* load (global) __name__ ... */
2458 str = PyUnicode_InternFromString("__name__");
2459 if (!str || !compiler_nameop(c, str, Load)) {
2460 Py_XDECREF(str);
2461 compiler_exit_scope(c);
2462 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002463 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464 Py_DECREF(str);
2465 /* ... and store it as __module__ */
2466 str = PyUnicode_InternFromString("__module__");
2467 if (!str || !compiler_nameop(c, str, Store)) {
2468 Py_XDECREF(str);
2469 compiler_exit_scope(c);
2470 return 0;
2471 }
2472 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002473 assert(c->u->u_qualname);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002474 ADDOP_LOAD_CONST(c, c->u->u_qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002475 str = PyUnicode_InternFromString("__qualname__");
2476 if (!str || !compiler_nameop(c, str, Store)) {
2477 Py_XDECREF(str);
2478 compiler_exit_scope(c);
2479 return 0;
2480 }
2481 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 /* compile the body proper */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002483 if (!compiler_body(c, s->v.ClassDef.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 compiler_exit_scope(c);
2485 return 0;
2486 }
Mark Shannone56d54e2021-01-15 13:52:00 +00002487 /* The following code is artificial */
2488 c->u->u_lineno = -1;
Nick Coghlan19d24672016-12-05 16:47:55 +10002489 /* Return __classcell__ if it is referenced, otherwise return None */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002490 if (c->u->u_ste->ste_needs_class_closure) {
Nick Coghlan19d24672016-12-05 16:47:55 +10002491 /* Store __classcell__ into class namespace & return it */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002492 str = PyUnicode_InternFromString("__class__");
2493 if (str == NULL) {
2494 compiler_exit_scope(c);
2495 return 0;
2496 }
2497 i = compiler_lookup_arg(c->u->u_cellvars, str);
2498 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01002499 if (i < 0) {
2500 compiler_exit_scope(c);
2501 return 0;
2502 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002503 assert(i == 0);
Nick Coghlan944368e2016-09-11 14:45:49 +10002504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 ADDOP_I(c, LOAD_CLOSURE, i);
Nick Coghlan19d24672016-12-05 16:47:55 +10002506 ADDOP(c, DUP_TOP);
Nick Coghlan944368e2016-09-11 14:45:49 +10002507 str = PyUnicode_InternFromString("__classcell__");
2508 if (!str || !compiler_nameop(c, str, Store)) {
2509 Py_XDECREF(str);
2510 compiler_exit_scope(c);
2511 return 0;
2512 }
2513 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002515 else {
Nick Coghlan19d24672016-12-05 16:47:55 +10002516 /* No methods referenced __class__, so just return None */
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002517 assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002518 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson312595c2013-05-15 15:26:42 -05002519 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002520 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002521 /* create the code object */
2522 co = assemble(c, 1);
2523 }
2524 /* leave the new scope */
2525 compiler_exit_scope(c);
2526 if (co == NULL)
2527 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 /* 2. load the 'build_class' function */
2530 ADDOP(c, LOAD_BUILD_CLASS);
2531
2532 /* 3. load a function (or closure) made from the code object */
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002533 if (!compiler_make_closure(c, co, 0, NULL)) {
2534 Py_DECREF(co);
2535 return 0;
2536 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002537 Py_DECREF(co);
2538
2539 /* 4. load class name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002540 ADDOP_LOAD_CONST(c, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541
2542 /* 5. generate the rest of the code for the call */
Pablo Galindoa5634c42020-09-16 19:42:00 +01002543 if (!compiler_call_helper(c, 2, s->v.ClassDef.bases, s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002544 return 0;
2545
2546 /* 6. apply decorators */
2547 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2548 ADDOP_I(c, CALL_FUNCTION, 1);
2549 }
2550
2551 /* 7. store into <name> */
2552 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2553 return 0;
2554 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002555}
2556
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02002557/* Return 0 if the expression is a constant value except named singletons.
2558 Return 1 otherwise. */
2559static int
2560check_is_arg(expr_ty e)
2561{
2562 if (e->kind != Constant_kind) {
2563 return 1;
2564 }
2565 PyObject *value = e->v.Constant.value;
2566 return (value == Py_None
2567 || value == Py_False
2568 || value == Py_True
2569 || value == Py_Ellipsis);
2570}
2571
2572/* Check operands of identity chacks ("is" and "is not").
2573 Emit a warning if any operand is a constant except named singletons.
2574 Return 0 on error.
2575 */
2576static int
2577check_compare(struct compiler *c, expr_ty e)
2578{
2579 Py_ssize_t i, n;
2580 int left = check_is_arg(e->v.Compare.left);
2581 n = asdl_seq_LEN(e->v.Compare.ops);
2582 for (i = 0; i < n; i++) {
2583 cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i);
2584 int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2585 if (op == Is || op == IsNot) {
2586 if (!right || !left) {
2587 const char *msg = (op == Is)
2588 ? "\"is\" with a literal. Did you mean \"==\"?"
2589 : "\"is not\" with a literal. Did you mean \"!=\"?";
2590 return compiler_warn(c, msg);
2591 }
2592 }
2593 left = right;
2594 }
2595 return 1;
2596}
2597
Mark Shannon9af0e472020-01-14 10:12:45 +00002598static int compiler_addcompare(struct compiler *c, cmpop_ty op)
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002599{
Mark Shannon9af0e472020-01-14 10:12:45 +00002600 int cmp;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002601 switch (op) {
2602 case Eq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002603 cmp = Py_EQ;
2604 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002605 case NotEq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002606 cmp = Py_NE;
2607 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002608 case Lt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002609 cmp = Py_LT;
2610 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002611 case LtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002612 cmp = Py_LE;
2613 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002614 case Gt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002615 cmp = Py_GT;
2616 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002617 case GtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002618 cmp = Py_GE;
2619 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002620 case Is:
Mark Shannon9af0e472020-01-14 10:12:45 +00002621 ADDOP_I(c, IS_OP, 0);
2622 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002623 case IsNot:
Mark Shannon9af0e472020-01-14 10:12:45 +00002624 ADDOP_I(c, IS_OP, 1);
2625 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002626 case In:
Mark Shannon9af0e472020-01-14 10:12:45 +00002627 ADDOP_I(c, CONTAINS_OP, 0);
2628 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002629 case NotIn:
Mark Shannon9af0e472020-01-14 10:12:45 +00002630 ADDOP_I(c, CONTAINS_OP, 1);
2631 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002632 default:
Mark Shannon9af0e472020-01-14 10:12:45 +00002633 Py_UNREACHABLE();
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002634 }
Mark Shannon9af0e472020-01-14 10:12:45 +00002635 ADDOP_I(c, COMPARE_OP, cmp);
2636 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002637}
2638
Mark Shannon9af0e472020-01-14 10:12:45 +00002639
2640
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002641static int
2642compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2643{
2644 switch (e->kind) {
2645 case UnaryOp_kind:
2646 if (e->v.UnaryOp.op == Not)
2647 return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2648 /* fallback to general implementation */
2649 break;
2650 case BoolOp_kind: {
Pablo Galindoa5634c42020-09-16 19:42:00 +01002651 asdl_expr_seq *s = e->v.BoolOp.values;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002652 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2653 assert(n >= 0);
2654 int cond2 = e->v.BoolOp.op == Or;
2655 basicblock *next2 = next;
2656 if (!cond2 != !cond) {
2657 next2 = compiler_new_block(c);
2658 if (next2 == NULL)
2659 return 0;
2660 }
2661 for (i = 0; i < n; ++i) {
2662 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2663 return 0;
2664 }
2665 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2666 return 0;
2667 if (next2 != next)
2668 compiler_use_next_block(c, next2);
2669 return 1;
2670 }
2671 case IfExp_kind: {
2672 basicblock *end, *next2;
2673 end = compiler_new_block(c);
2674 if (end == NULL)
2675 return 0;
2676 next2 = compiler_new_block(c);
2677 if (next2 == NULL)
2678 return 0;
2679 if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2680 return 0;
2681 if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2682 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002683 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002684 compiler_use_next_block(c, next2);
2685 if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2686 return 0;
2687 compiler_use_next_block(c, end);
2688 return 1;
2689 }
2690 case Compare_kind: {
2691 Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2692 if (n > 0) {
Serhiy Storchaka45835252019-02-16 08:29:46 +02002693 if (!check_compare(c, e)) {
2694 return 0;
2695 }
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002696 basicblock *cleanup = compiler_new_block(c);
2697 if (cleanup == NULL)
2698 return 0;
2699 VISIT(c, expr, e->v.Compare.left);
2700 for (i = 0; i < n; i++) {
2701 VISIT(c, expr,
2702 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2703 ADDOP(c, DUP_TOP);
2704 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00002705 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01002706 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002707 NEXT_BLOCK(c);
2708 }
2709 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00002710 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Mark Shannon582aaf12020-08-04 17:30:11 +01002711 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Mark Shannon266b4622020-11-17 19:30:14 +00002712 NEXT_BLOCK(c);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002713 basicblock *end = compiler_new_block(c);
2714 if (end == NULL)
2715 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002716 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002717 compiler_use_next_block(c, cleanup);
2718 ADDOP(c, POP_TOP);
2719 if (!cond) {
Mark Shannon127dde52021-01-04 18:06:55 +00002720 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002721 }
2722 compiler_use_next_block(c, end);
2723 return 1;
2724 }
2725 /* fallback to general implementation */
2726 break;
2727 }
2728 default:
2729 /* fallback to general implementation */
2730 break;
2731 }
2732
2733 /* general implementation */
2734 VISIT(c, expr, e);
Mark Shannon582aaf12020-08-04 17:30:11 +01002735 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Mark Shannon266b4622020-11-17 19:30:14 +00002736 NEXT_BLOCK(c);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002737 return 1;
2738}
2739
2740static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002741compiler_ifexp(struct compiler *c, expr_ty e)
2742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 basicblock *end, *next;
2744
2745 assert(e->kind == IfExp_kind);
2746 end = compiler_new_block(c);
2747 if (end == NULL)
2748 return 0;
2749 next = compiler_new_block(c);
2750 if (next == NULL)
2751 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002752 if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2753 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 VISIT(c, expr, e->v.IfExp.body);
Mark Shannon127dde52021-01-04 18:06:55 +00002755 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 compiler_use_next_block(c, next);
2757 VISIT(c, expr, e->v.IfExp.orelse);
2758 compiler_use_next_block(c, end);
2759 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002760}
2761
2762static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002763compiler_lambda(struct compiler *c, expr_ty e)
2764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002765 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002766 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 static identifier name;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002768 Py_ssize_t funcflags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002769 arguments_ty args = e->v.Lambda.args;
2770 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002771
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002772 if (!compiler_check_debug_args(c, args))
2773 return 0;
2774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002775 if (!name) {
2776 name = PyUnicode_InternFromString("<lambda>");
2777 if (!name)
2778 return 0;
2779 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002780
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002781 funcflags = compiler_default_arguments(c, args);
2782 if (funcflags == -1) {
2783 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002784 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002785
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002786 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002787 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002790 /* Make None the first constant, so the lambda can't have a
2791 docstring. */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002792 if (compiler_add_const(c, Py_None) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002795 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002796 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002797 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2798 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2799 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002800 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 }
2802 else {
2803 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002804 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002806 qualname = c->u->u_qualname;
2807 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 compiler_exit_scope(c);
Pablo Galindo7fdab832021-01-29 22:40:59 +00002809 if (co == NULL) {
2810 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 return 0;
Pablo Galindo7fdab832021-01-29 22:40:59 +00002812 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002813
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002814 if (!compiler_make_closure(c, co, funcflags, qualname)) {
2815 Py_DECREF(qualname);
2816 Py_DECREF(co);
2817 return 0;
2818 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002819 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002820 Py_DECREF(co);
2821
2822 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823}
2824
2825static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002826compiler_if(struct compiler *c, stmt_ty s)
2827{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 basicblock *end, *next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 assert(s->kind == If_kind);
2830 end = compiler_new_block(c);
Mark Shannon8473cf82020-12-15 11:07:50 +00002831 if (end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 return 0;
Mark Shannon8473cf82020-12-15 11:07:50 +00002833 }
2834 if (asdl_seq_LEN(s->v.If.orelse)) {
2835 next = compiler_new_block(c);
2836 if (next == NULL) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002837 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002838 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002839 }
2840 else {
2841 next = end;
2842 }
2843 if (!compiler_jump_if(c, s->v.If.test, next, 0)) {
2844 return 0;
2845 }
2846 VISIT_SEQ(c, stmt, s->v.If.body);
2847 if (asdl_seq_LEN(s->v.If.orelse)) {
Mark Shannon127dde52021-01-04 18:06:55 +00002848 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Mark Shannon8473cf82020-12-15 11:07:50 +00002849 compiler_use_next_block(c, next);
2850 VISIT_SEQ(c, stmt, s->v.If.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002851 }
2852 compiler_use_next_block(c, end);
2853 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002854}
2855
2856static int
2857compiler_for(struct compiler *c, stmt_ty s)
2858{
Mark Shannon5977a792020-12-02 13:31:40 +00002859 basicblock *start, *body, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 start = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002862 body = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 cleanup = compiler_new_block(c);
2864 end = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002865 if (start == NULL || body == NULL || end == NULL || cleanup == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002867 }
2868 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002870 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 VISIT(c, expr, s->v.For.iter);
2872 ADDOP(c, GET_ITER);
2873 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01002874 ADDOP_JUMP(c, FOR_ITER, cleanup);
Mark Shannon5977a792020-12-02 13:31:40 +00002875 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 VISIT(c, expr, s->v.For.target);
2877 VISIT_SEQ(c, stmt, s->v.For.body);
Mark Shannonf5e97b72020-12-14 11:28:39 +00002878 /* Mark jump as artificial */
2879 c->u->u_lineno = -1;
Mark Shannon582aaf12020-08-04 17:30:11 +01002880 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 compiler_use_next_block(c, cleanup);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002882
2883 compiler_pop_fblock(c, FOR_LOOP, start);
2884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 VISIT_SEQ(c, stmt, s->v.For.orelse);
2886 compiler_use_next_block(c, end);
2887 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002888}
2889
Yury Selivanov75445082015-05-11 22:57:16 -04002890
2891static int
2892compiler_async_for(struct compiler *c, stmt_ty s)
2893{
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002894 basicblock *start, *except, *end;
Pablo Galindo90235812020-03-15 04:29:22 +00002895 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07002896 c->u->u_ste->ste_coroutine = 1;
2897 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
Zsolt Dollensteine2396502018-04-27 08:58:56 -07002898 return compiler_error(c, "'async for' outside async function");
2899 }
2900
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002901 start = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002902 except = compiler_new_block(c);
2903 end = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002904
Mark Shannonfee55262019-11-21 09:11:43 +00002905 if (start == NULL || except == NULL || end == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002906 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002907 }
Yury Selivanov75445082015-05-11 22:57:16 -04002908 VISIT(c, expr, s->v.AsyncFor.iter);
2909 ADDOP(c, GET_AITER);
Yury Selivanov75445082015-05-11 22:57:16 -04002910
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002911 compiler_use_next_block(c, start);
Mark Shannonfee55262019-11-21 09:11:43 +00002912 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002913 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002914 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002915 /* SETUP_FINALLY to guard the __anext__ call */
Mark Shannon582aaf12020-08-04 17:30:11 +01002916 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov75445082015-05-11 22:57:16 -04002917 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002918 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04002919 ADDOP(c, YIELD_FROM);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002920 ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */
Yury Selivanov75445082015-05-11 22:57:16 -04002921
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002922 /* Success block for __anext__ */
2923 VISIT(c, expr, s->v.AsyncFor.target);
2924 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002925 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002926
2927 compiler_pop_fblock(c, FOR_LOOP, start);
Yury Selivanov75445082015-05-11 22:57:16 -04002928
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002929 /* Except block for __anext__ */
Yury Selivanov75445082015-05-11 22:57:16 -04002930 compiler_use_next_block(c, except);
Mark Shannon877df852020-11-12 09:43:29 +00002931
Mark Shannon47695e32021-07-15 15:54:38 +01002932 /* Use same line number as the iterator,
2933 * as the END_ASYNC_FOR succeeds the `for`, not the body. */
2934 SET_LOC(c, s->v.AsyncFor.iter);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002935 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov75445082015-05-11 22:57:16 -04002936
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002937 /* `else` block */
Yury Selivanov75445082015-05-11 22:57:16 -04002938 VISIT_SEQ(c, stmt, s->v.For.orelse);
2939
2940 compiler_use_next_block(c, end);
2941
2942 return 1;
2943}
2944
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002945static int
2946compiler_while(struct compiler *c, stmt_ty s)
2947{
Mark Shannon266b4622020-11-17 19:30:14 +00002948 basicblock *loop, *body, *end, *anchor = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002949 loop = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002950 body = compiler_new_block(c);
2951 anchor = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 end = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002953 if (loop == NULL || body == NULL || anchor == NULL || end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002954 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002956 compiler_use_next_block(c, loop);
Mark Shannon266b4622020-11-17 19:30:14 +00002957 if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002958 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002960 if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) {
2961 return 0;
Mark Shannon266b4622020-11-17 19:30:14 +00002962 }
2963
2964 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002965 VISIT_SEQ(c, stmt, s->v.While.body);
Mark Shannon8473cf82020-12-15 11:07:50 +00002966 SET_LOC(c, s);
2967 if (!compiler_jump_if(c, s->v.While.test, body, 1)) {
2968 return 0;
2969 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002970
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002971 compiler_pop_fblock(c, WHILE_LOOP, loop);
2972
Mark Shannon266b4622020-11-17 19:30:14 +00002973 compiler_use_next_block(c, anchor);
2974 if (s->v.While.orelse) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975 VISIT_SEQ(c, stmt, s->v.While.orelse);
Mark Shannon266b4622020-11-17 19:30:14 +00002976 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002979 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002980}
2981
2982static int
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002983compiler_return(struct compiler *c, stmt_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002984{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002985 int preserve_tos = ((s->v.Return.value != NULL) &&
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002986 (s->v.Return.value->kind != Constant_kind));
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002987 if (c->u->u_ste->ste_type != FunctionBlock)
2988 return compiler_error(c, "'return' outside function");
2989 if (s->v.Return.value != NULL &&
2990 c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2991 {
2992 return compiler_error(
2993 c, "'return' with value in async generator");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002995 if (preserve_tos) {
2996 VISIT(c, expr, s->v.Return.value);
Mark Shannon5274b682020-12-16 13:07:01 +00002997 } else {
Mark Shannoncea05852021-06-03 19:57:31 +01002998 /* Emit instruction with line number for return value */
Mark Shannon5274b682020-12-16 13:07:01 +00002999 if (s->v.Return.value != NULL) {
3000 SET_LOC(c, s->v.Return.value);
3001 ADDOP(c, NOP);
3002 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003003 }
Mark Shannoncea05852021-06-03 19:57:31 +01003004 if (s->v.Return.value == NULL || s->v.Return.value->lineno != s->lineno) {
3005 SET_LOC(c, s);
3006 ADDOP(c, NOP);
3007 }
3008
Mark Shannonfee55262019-11-21 09:11:43 +00003009 if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL))
3010 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003011 if (s->v.Return.value == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003012 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003013 }
3014 else if (!preserve_tos) {
Mark Shannon5274b682020-12-16 13:07:01 +00003015 ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003016 }
3017 ADDOP(c, RETURN_VALUE);
Mark Shannon266b4622020-11-17 19:30:14 +00003018 NEXT_BLOCK(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003021}
3022
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003023static int
3024compiler_break(struct compiler *c)
3025{
Mark Shannonfee55262019-11-21 09:11:43 +00003026 struct fblockinfo *loop = NULL;
Mark Shannoncea05852021-06-03 19:57:31 +01003027 /* Emit instruction with line number */
3028 ADDOP(c, NOP);
Mark Shannonfee55262019-11-21 09:11:43 +00003029 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
3030 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003031 }
Mark Shannonfee55262019-11-21 09:11:43 +00003032 if (loop == NULL) {
3033 return compiler_error(c, "'break' outside loop");
3034 }
3035 if (!compiler_unwind_fblock(c, loop, 0)) {
3036 return 0;
3037 }
Mark Shannon582aaf12020-08-04 17:30:11 +01003038 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit);
Mark Shannon266b4622020-11-17 19:30:14 +00003039 NEXT_BLOCK(c);
Mark Shannonfee55262019-11-21 09:11:43 +00003040 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003041}
3042
3043static int
3044compiler_continue(struct compiler *c)
3045{
Mark Shannonfee55262019-11-21 09:11:43 +00003046 struct fblockinfo *loop = NULL;
Mark Shannoncea05852021-06-03 19:57:31 +01003047 /* Emit instruction with line number */
3048 ADDOP(c, NOP);
Mark Shannonfee55262019-11-21 09:11:43 +00003049 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
3050 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003051 }
Mark Shannonfee55262019-11-21 09:11:43 +00003052 if (loop == NULL) {
3053 return compiler_error(c, "'continue' not properly in loop");
3054 }
Mark Shannon582aaf12020-08-04 17:30:11 +01003055 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block);
Mark Shannon266b4622020-11-17 19:30:14 +00003056 NEXT_BLOCK(c)
Mark Shannonfee55262019-11-21 09:11:43 +00003057 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003058}
3059
3060
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003061/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003062
3063 SETUP_FINALLY L
3064 <code for body>
3065 POP_BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00003066 <code for finalbody>
3067 JUMP E
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003068 L:
3069 <code for finalbody>
Mark Shannonfee55262019-11-21 09:11:43 +00003070 E:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003071
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003072 The special instructions use the block stack. Each block
3073 stack entry contains the instruction that created it (here
3074 SETUP_FINALLY), the level of the value stack at the time the
3075 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003076
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003077 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003078 Pushes the current value stack level and the label
3079 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003080 POP_BLOCK:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003081 Pops en entry from the block stack.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003083 The block stack is unwound when an exception is raised:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003084 when a SETUP_FINALLY entry is found, the raised and the caught
3085 exceptions are pushed onto the value stack (and the exception
3086 condition is cleared), and the interpreter jumps to the label
3087 gotten from the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003088*/
3089
3090static int
3091compiler_try_finally(struct compiler *c, stmt_ty s)
3092{
Mark Shannonfee55262019-11-21 09:11:43 +00003093 basicblock *body, *end, *exit;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003095 body = compiler_new_block(c);
3096 end = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00003097 exit = compiler_new_block(c);
3098 if (body == NULL || end == NULL || exit == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003099 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003100
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003101 /* `try` block */
Mark Shannon582aaf12020-08-04 17:30:11 +01003102 ADDOP_JUMP(c, SETUP_FINALLY, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 compiler_use_next_block(c, body);
Mark Shannonfee55262019-11-21 09:11:43 +00003104 if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003105 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003106 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
3107 if (!compiler_try_except(c, s))
3108 return 0;
3109 }
3110 else {
3111 VISIT_SEQ(c, stmt, s->v.Try.body);
3112 }
Mark Shannon3bd60352021-01-13 12:05:43 +00003113 ADDOP_NOLINE(c, POP_BLOCK);
Mark Shannonfee55262019-11-21 09:11:43 +00003114 compiler_pop_fblock(c, FINALLY_TRY, body);
3115 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Mark Shannon127dde52021-01-04 18:06:55 +00003116 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00003117 /* `finally` block */
3118 compiler_use_next_block(c, end);
3119 if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL))
3120 return 0;
3121 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3122 compiler_pop_fblock(c, FINALLY_END, end);
Mark Shannonbf353f32020-12-17 13:55:28 +00003123 ADDOP_I(c, RERAISE, 0);
Mark Shannonfee55262019-11-21 09:11:43 +00003124 compiler_use_next_block(c, exit);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003125 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003126}
3127
3128/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003129 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003130 (The contents of the value stack is shown in [], with the top
3131 at the right; 'tb' is trace-back info, 'val' the exception's
3132 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003133
3134 Value stack Label Instruction Argument
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003135 [] SETUP_FINALLY L1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 [] <code for S>
3137 [] POP_BLOCK
3138 [] JUMP_FORWARD L0
3139
3140 [tb, val, exc] L1: DUP )
3141 [tb, val, exc, exc] <evaluate E1> )
Mark Shannon9af0e472020-01-14 10:12:45 +00003142 [tb, val, exc, exc, E1] JUMP_IF_NOT_EXC_MATCH L2 ) only if E1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003143 [tb, val, exc] POP
3144 [tb, val] <assign to V1> (or POP if no V1)
3145 [tb] POP
3146 [] <code for S1>
3147 JUMP_FORWARD L0
3148
3149 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003150 .............................etc.......................
3151
Mark Shannonfee55262019-11-21 09:11:43 +00003152 [tb, val, exc] Ln+1: RERAISE # re-raise exception
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003153
3154 [] L0: <next statement>
3155
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003156 Of course, parts are not generated if Vi or Ei is not present.
3157*/
3158static int
3159compiler_try_except(struct compiler *c, stmt_ty s)
3160{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003162 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 body = compiler_new_block(c);
3165 except = compiler_new_block(c);
3166 orelse = compiler_new_block(c);
3167 end = compiler_new_block(c);
3168 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
3169 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01003170 ADDOP_JUMP(c, SETUP_FINALLY, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003171 compiler_use_next_block(c, body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003172 if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003173 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003174 VISIT_SEQ(c, stmt, s->v.Try.body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003175 compiler_pop_fblock(c, TRY_EXCEPT, body);
Mark Shannon3bd60352021-01-13 12:05:43 +00003176 ADDOP_NOLINE(c, POP_BLOCK);
3177 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003178 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003179 compiler_use_next_block(c, except);
Mark Shannon02d126a2020-09-25 14:04:19 +01003180 /* Runtime will push a block here, so we need to account for that */
3181 if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL))
3182 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003183 for (i = 0; i < n; i++) {
3184 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003185 s->v.Try.handlers, i);
Mark Shannon8d4b1842021-05-06 13:38:50 +01003186 SET_LOC(c, handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003187 if (!handler->v.ExceptHandler.type && i < n-1)
3188 return compiler_error(c, "default 'except:' must be last");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 except = compiler_new_block(c);
3190 if (except == NULL)
3191 return 0;
3192 if (handler->v.ExceptHandler.type) {
3193 ADDOP(c, DUP_TOP);
3194 VISIT(c, expr, handler->v.ExceptHandler.type);
Mark Shannon582aaf12020-08-04 17:30:11 +01003195 ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except);
Mark Shannon266b4622020-11-17 19:30:14 +00003196 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003197 }
3198 ADDOP(c, POP_TOP);
3199 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003200 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00003201
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003202 cleanup_end = compiler_new_block(c);
3203 cleanup_body = compiler_new_block(c);
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003204 if (cleanup_end == NULL || cleanup_body == NULL) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003205 return 0;
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003206 }
Guido van Rossumb940e112007-01-10 16:19:56 +00003207
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003208 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3209 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003211 /*
3212 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003213 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003214 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003215 try:
3216 # body
3217 finally:
Chris Angelicoad098b62019-05-21 23:34:19 +10003218 name = None # in case body contains "del name"
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003219 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003220 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003221
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003222 /* second try: */
Mark Shannon582aaf12020-08-04 17:30:11 +01003223 ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003224 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003225 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003226 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003228 /* second # body */
3229 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003230 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannon877df852020-11-12 09:43:29 +00003231 /* name = None; del name; # Mark as artificial */
3232 c->u->u_lineno = -1;
Mark Shannon794ff7d2021-07-14 11:43:56 +01003233 ADDOP(c, POP_BLOCK);
3234 ADDOP(c, POP_EXCEPT);
Mark Shannonfee55262019-11-21 09:11:43 +00003235 ADDOP_LOAD_CONST(c, Py_None);
3236 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3237 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Mark Shannon582aaf12020-08-04 17:30:11 +01003238 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003239
Mark Shannonfee55262019-11-21 09:11:43 +00003240 /* except: */
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003241 compiler_use_next_block(c, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242
Mark Shannon877df852020-11-12 09:43:29 +00003243 /* name = None; del name; # Mark as artificial */
3244 c->u->u_lineno = -1;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003245 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003246 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003247 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003248
Mark Shannonbf353f32020-12-17 13:55:28 +00003249 ADDOP_I(c, RERAISE, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250 }
3251 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003252 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003253
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003254 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05003255 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003256 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257
Guido van Rossumb940e112007-01-10 16:19:56 +00003258 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003259 ADDOP(c, POP_TOP);
3260 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003261 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003262 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003264 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannon127dde52021-01-04 18:06:55 +00003265 c->u->u_lineno = -1;
Mark Shannonfee55262019-11-21 09:11:43 +00003266 ADDOP(c, POP_EXCEPT);
Mark Shannon582aaf12020-08-04 17:30:11 +01003267 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003268 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003269 compiler_use_next_block(c, except);
3270 }
Mark Shannon02d126a2020-09-25 14:04:19 +01003271 compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL);
Mark Shannonf2dbfd72020-12-21 13:53:50 +00003272 /* Mark as artificial */
3273 c->u->u_lineno = -1;
Mark Shannonbf353f32020-12-17 13:55:28 +00003274 ADDOP_I(c, RERAISE, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003275 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003276 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003277 compiler_use_next_block(c, end);
3278 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003279}
3280
3281static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003282compiler_try(struct compiler *c, stmt_ty s) {
3283 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
3284 return compiler_try_finally(c, s);
3285 else
3286 return compiler_try_except(c, s);
3287}
3288
3289
3290static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003291compiler_import_as(struct compiler *c, identifier name, identifier asname)
3292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 /* The IMPORT_NAME opcode was already generated. This function
3294 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003296 If there is a dot in name, we need to split it and emit a
Serhiy Storchakaf93234b2017-05-09 22:31:05 +03003297 IMPORT_FROM for each name.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003298 */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003299 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
3300 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003301 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003302 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003303 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003304 /* Consume the base module name to get the first attribute */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003305 while (1) {
3306 Py_ssize_t pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 PyObject *attr;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003308 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003309 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003310 return 0;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003311 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003312 if (!attr)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003313 return 0;
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003314 ADDOP_N(c, IMPORT_FROM, attr, names);
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003315 if (dot == -1) {
3316 break;
3317 }
3318 ADDOP(c, ROT_TWO);
3319 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003320 }
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003321 if (!compiler_nameop(c, asname, Store)) {
3322 return 0;
3323 }
3324 ADDOP(c, POP_TOP);
3325 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 }
3327 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003328}
3329
3330static int
3331compiler_import(struct compiler *c, stmt_ty s)
3332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003333 /* The Import node stores a module name like a.b.c as a single
3334 string. This is convenient for all cases except
3335 import a.b.c as d
3336 where we need to parse that string to extract the individual
3337 module names.
3338 XXX Perhaps change the representation to make this case simpler?
3339 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01003340 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003341
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003342 PyObject *zero = _PyLong_GetZero(); // borrowed reference
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003343 for (i = 0; i < n; i++) {
3344 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
3345 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003346
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003347 ADDOP_LOAD_CONST(c, zero);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003348 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003349 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003351 if (alias->asname) {
3352 r = compiler_import_as(c, alias->name, alias->asname);
3353 if (!r)
3354 return r;
3355 }
3356 else {
3357 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003358 Py_ssize_t dot = PyUnicode_FindChar(
3359 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02003360 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003361 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02003362 if (tmp == NULL)
3363 return 0;
3364 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003365 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003366 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003367 Py_DECREF(tmp);
3368 }
3369 if (!r)
3370 return r;
3371 }
3372 }
3373 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003374}
3375
3376static int
3377compiler_from_import(struct compiler *c, stmt_ty s)
3378{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003379 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003380 PyObject *names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003381 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003383 if (!empty_string) {
3384 empty_string = PyUnicode_FromString("");
3385 if (!empty_string)
3386 return 0;
3387 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003388
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003389 ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02003390
3391 names = PyTuple_New(n);
3392 if (!names)
3393 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003395 /* build up the names */
3396 for (i = 0; i < n; i++) {
3397 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3398 Py_INCREF(alias->name);
3399 PyTuple_SET_ITEM(names, i, alias->name);
3400 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003403 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003404 Py_DECREF(names);
3405 return compiler_error(c, "from __future__ imports must occur "
3406 "at the beginning of the file");
3407 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003408 ADDOP_LOAD_CONST_NEW(c, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003410 if (s->v.ImportFrom.module) {
3411 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
3412 }
3413 else {
3414 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
3415 }
3416 for (i = 0; i < n; i++) {
3417 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3418 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003419
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003420 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003421 assert(n == 1);
3422 ADDOP(c, IMPORT_STAR);
3423 return 1;
3424 }
3425
3426 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
3427 store_name = alias->name;
3428 if (alias->asname)
3429 store_name = alias->asname;
3430
3431 if (!compiler_nameop(c, store_name, Store)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 return 0;
3433 }
3434 }
3435 /* remove imported module */
3436 ADDOP(c, POP_TOP);
3437 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003438}
3439
3440static int
3441compiler_assert(struct compiler *c, stmt_ty s)
3442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003443 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003444
tsukasa-aua8ef4572021-03-16 22:14:41 +11003445 /* Always emit a warning if the test is a non-zero length tuple */
3446 if ((s->v.Assert.test->kind == Tuple_kind &&
3447 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) ||
3448 (s->v.Assert.test->kind == Constant_kind &&
3449 PyTuple_Check(s->v.Assert.test->v.Constant.value) &&
3450 PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0))
Serhiy Storchakad31e7732018-10-21 10:09:39 +03003451 {
3452 if (!compiler_warn(c, "assertion is always true, "
3453 "perhaps remove parentheses?"))
3454 {
Victor Stinner14e461d2013-08-26 22:28:21 +02003455 return 0;
3456 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003457 }
tsukasa-aua8ef4572021-03-16 22:14:41 +11003458 if (c->c_optimize)
3459 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 end = compiler_new_block(c);
3461 if (end == NULL)
3462 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003463 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
3464 return 0;
Zackery Spytzce6a0702019-08-25 03:44:09 -06003465 ADDOP(c, LOAD_ASSERTION_ERROR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003466 if (s->v.Assert.msg) {
3467 VISIT(c, expr, s->v.Assert.msg);
3468 ADDOP_I(c, CALL_FUNCTION, 1);
3469 }
3470 ADDOP_I(c, RAISE_VARARGS, 1);
3471 compiler_use_next_block(c, end);
3472 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003473}
3474
3475static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003476compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3477{
3478 if (c->c_interactive && c->c_nestlevel <= 1) {
3479 VISIT(c, expr, value);
3480 ADDOP(c, PRINT_EXPR);
3481 return 1;
3482 }
3483
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003484 if (value->kind == Constant_kind) {
Victor Stinner15a30952016-02-08 22:45:06 +01003485 /* ignore constant statement */
Mark Shannon877df852020-11-12 09:43:29 +00003486 ADDOP(c, NOP);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003487 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003488 }
3489
3490 VISIT(c, expr, value);
Mark Shannonc5440932021-03-15 14:24:25 +00003491 /* Mark POP_TOP as artificial */
3492 c->u->u_lineno = -1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003493 ADDOP(c, POP_TOP);
3494 return 1;
3495}
3496
3497static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003498compiler_visit_stmt(struct compiler *c, stmt_ty s)
3499{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003500 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003502 /* Always assign a lineno to the next instruction for a stmt. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003503 SET_LOC(c, s);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003505 switch (s->kind) {
3506 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04003507 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 case ClassDef_kind:
3509 return compiler_class(c, s);
3510 case Return_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003511 return compiler_return(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003512 case Delete_kind:
3513 VISIT_SEQ(c, expr, s->v.Delete.targets)
3514 break;
3515 case Assign_kind:
3516 n = asdl_seq_LEN(s->v.Assign.targets);
3517 VISIT(c, expr, s->v.Assign.value);
3518 for (i = 0; i < n; i++) {
3519 if (i < n - 1)
3520 ADDOP(c, DUP_TOP);
3521 VISIT(c, expr,
3522 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3523 }
3524 break;
3525 case AugAssign_kind:
3526 return compiler_augassign(c, s);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003527 case AnnAssign_kind:
3528 return compiler_annassign(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003529 case For_kind:
3530 return compiler_for(c, s);
3531 case While_kind:
3532 return compiler_while(c, s);
3533 case If_kind:
3534 return compiler_if(c, s);
Brandt Bucher145bf262021-02-26 14:51:55 -08003535 case Match_kind:
3536 return compiler_match(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003537 case Raise_kind:
3538 n = 0;
3539 if (s->v.Raise.exc) {
3540 VISIT(c, expr, s->v.Raise.exc);
3541 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003542 if (s->v.Raise.cause) {
3543 VISIT(c, expr, s->v.Raise.cause);
3544 n++;
3545 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003546 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01003547 ADDOP_I(c, RAISE_VARARGS, (int)n);
Mark Shannon266b4622020-11-17 19:30:14 +00003548 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003549 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003550 case Try_kind:
3551 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003552 case Assert_kind:
3553 return compiler_assert(c, s);
3554 case Import_kind:
3555 return compiler_import(c, s);
3556 case ImportFrom_kind:
3557 return compiler_from_import(c, s);
3558 case Global_kind:
3559 case Nonlocal_kind:
3560 break;
3561 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003562 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003563 case Pass_kind:
Mark Shannon877df852020-11-12 09:43:29 +00003564 ADDOP(c, NOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003565 break;
3566 case Break_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003567 return compiler_break(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003568 case Continue_kind:
3569 return compiler_continue(c);
3570 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003571 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04003572 case AsyncFunctionDef_kind:
3573 return compiler_function(c, s, 1);
3574 case AsyncWith_kind:
3575 return compiler_async_with(c, s, 0);
3576 case AsyncFor_kind:
3577 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003578 }
Yury Selivanov75445082015-05-11 22:57:16 -04003579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003580 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003581}
3582
3583static int
3584unaryop(unaryop_ty op)
3585{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003586 switch (op) {
3587 case Invert:
3588 return UNARY_INVERT;
3589 case Not:
3590 return UNARY_NOT;
3591 case UAdd:
3592 return UNARY_POSITIVE;
3593 case USub:
3594 return UNARY_NEGATIVE;
3595 default:
3596 PyErr_Format(PyExc_SystemError,
3597 "unary op %d should not be possible", op);
3598 return 0;
3599 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003600}
3601
3602static int
Andy Lester76d58772020-03-10 21:18:12 -05003603binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003604{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 switch (op) {
3606 case Add:
3607 return BINARY_ADD;
3608 case Sub:
3609 return BINARY_SUBTRACT;
3610 case Mult:
3611 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003612 case MatMult:
3613 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003614 case Div:
3615 return BINARY_TRUE_DIVIDE;
3616 case Mod:
3617 return BINARY_MODULO;
3618 case Pow:
3619 return BINARY_POWER;
3620 case LShift:
3621 return BINARY_LSHIFT;
3622 case RShift:
3623 return BINARY_RSHIFT;
3624 case BitOr:
3625 return BINARY_OR;
3626 case BitXor:
3627 return BINARY_XOR;
3628 case BitAnd:
3629 return BINARY_AND;
3630 case FloorDiv:
3631 return BINARY_FLOOR_DIVIDE;
3632 default:
3633 PyErr_Format(PyExc_SystemError,
3634 "binary op %d should not be possible", op);
3635 return 0;
3636 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003637}
3638
3639static int
Andy Lester76d58772020-03-10 21:18:12 -05003640inplace_binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003642 switch (op) {
3643 case Add:
3644 return INPLACE_ADD;
3645 case Sub:
3646 return INPLACE_SUBTRACT;
3647 case Mult:
3648 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003649 case MatMult:
3650 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003651 case Div:
3652 return INPLACE_TRUE_DIVIDE;
3653 case Mod:
3654 return INPLACE_MODULO;
3655 case Pow:
3656 return INPLACE_POWER;
3657 case LShift:
3658 return INPLACE_LSHIFT;
3659 case RShift:
3660 return INPLACE_RSHIFT;
3661 case BitOr:
3662 return INPLACE_OR;
3663 case BitXor:
3664 return INPLACE_XOR;
3665 case BitAnd:
3666 return INPLACE_AND;
3667 case FloorDiv:
3668 return INPLACE_FLOOR_DIVIDE;
3669 default:
3670 PyErr_Format(PyExc_SystemError,
3671 "inplace binary op %d should not be possible", op);
3672 return 0;
3673 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003674}
3675
3676static int
3677compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3678{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003679 int op, scope;
3680 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003681 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003683 PyObject *dict = c->u->u_names;
3684 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003685
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003686 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3687 !_PyUnicode_EqualToASCIIString(name, "True") &&
3688 !_PyUnicode_EqualToASCIIString(name, "False"));
Benjamin Peterson70b224d2012-12-06 17:49:58 -05003689
Pablo Galindoc5fc1562020-04-22 23:29:27 +01003690 if (forbidden_name(c, name, ctx))
3691 return 0;
3692
Serhiy Storchakabd6ec4d2017-12-18 14:29:12 +02003693 mangled = _Py_Mangle(c->u->u_private, name);
3694 if (!mangled)
3695 return 0;
3696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003697 op = 0;
3698 optype = OP_NAME;
Victor Stinner28ad12f2021-03-19 12:41:49 +01003699 scope = _PyST_GetScope(c->u->u_ste, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003700 switch (scope) {
3701 case FREE:
3702 dict = c->u->u_freevars;
3703 optype = OP_DEREF;
3704 break;
3705 case CELL:
3706 dict = c->u->u_cellvars;
3707 optype = OP_DEREF;
3708 break;
3709 case LOCAL:
3710 if (c->u->u_ste->ste_type == FunctionBlock)
3711 optype = OP_FAST;
3712 break;
3713 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04003714 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003715 optype = OP_GLOBAL;
3716 break;
3717 case GLOBAL_EXPLICIT:
3718 optype = OP_GLOBAL;
3719 break;
3720 default:
3721 /* scope can be 0 */
3722 break;
3723 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003725 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003726 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 switch (optype) {
3729 case OP_DEREF:
3730 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003731 case Load:
3732 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3733 break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003734 case Store: op = STORE_DEREF; break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003735 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003736 }
3737 break;
3738 case OP_FAST:
3739 switch (ctx) {
3740 case Load: op = LOAD_FAST; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003741 case Store: op = STORE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003742 case Del: op = DELETE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003743 }
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003744 ADDOP_N(c, op, mangled, varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003745 return 1;
3746 case OP_GLOBAL:
3747 switch (ctx) {
3748 case Load: op = LOAD_GLOBAL; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003749 case Store: op = STORE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003750 case Del: op = DELETE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003751 }
3752 break;
3753 case OP_NAME:
3754 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00003755 case Load: op = LOAD_NAME; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003756 case Store: op = STORE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003757 case Del: op = DELETE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 }
3759 break;
3760 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003762 assert(op);
Andy Lester76d58772020-03-10 21:18:12 -05003763 arg = compiler_add_o(dict, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003764 Py_DECREF(mangled);
3765 if (arg < 0)
3766 return 0;
3767 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003768}
3769
3770static int
3771compiler_boolop(struct compiler *c, expr_ty e)
3772{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003773 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003774 int jumpi;
3775 Py_ssize_t i, n;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003776 asdl_expr_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003778 assert(e->kind == BoolOp_kind);
3779 if (e->v.BoolOp.op == And)
3780 jumpi = JUMP_IF_FALSE_OR_POP;
3781 else
3782 jumpi = JUMP_IF_TRUE_OR_POP;
3783 end = compiler_new_block(c);
3784 if (end == NULL)
3785 return 0;
3786 s = e->v.BoolOp.values;
3787 n = asdl_seq_LEN(s) - 1;
3788 assert(n >= 0);
3789 for (i = 0; i < n; ++i) {
3790 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003791 ADDOP_JUMP(c, jumpi, end);
Mark Shannon6e8128f2020-07-30 10:03:00 +01003792 basicblock *next = compiler_new_block(c);
3793 if (next == NULL) {
3794 return 0;
3795 }
3796 compiler_use_next_block(c, next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003797 }
3798 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3799 compiler_use_next_block(c, end);
3800 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003801}
3802
3803static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003804starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
Mark Shannon13bc1392020-01-23 09:25:17 +00003805 int build, int add, int extend, int tuple)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003806{
3807 Py_ssize_t n = asdl_seq_LEN(elts);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003808 if (n > 2 && are_all_items_const(elts, 0, n)) {
3809 PyObject *folded = PyTuple_New(n);
3810 if (folded == NULL) {
3811 return 0;
3812 }
3813 PyObject *val;
Mark Shannon11e0b292021-04-15 14:28:56 +01003814 for (Py_ssize_t i = 0; i < n; i++) {
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003815 val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
3816 Py_INCREF(val);
3817 PyTuple_SET_ITEM(folded, i, val);
3818 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003819 if (tuple) {
3820 ADDOP_LOAD_CONST_NEW(c, folded);
3821 } else {
3822 if (add == SET_ADD) {
3823 Py_SETREF(folded, PyFrozenSet_New(folded));
3824 if (folded == NULL) {
3825 return 0;
3826 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003827 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003828 ADDOP_I(c, build, pushed);
3829 ADDOP_LOAD_CONST_NEW(c, folded);
3830 ADDOP_I(c, extend, 1);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003831 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003832 return 1;
3833 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003834
Mark Shannon11e0b292021-04-15 14:28:56 +01003835 int big = n+pushed > STACK_USE_GUIDELINE;
3836 int seen_star = 0;
3837 for (Py_ssize_t i = 0; i < n; i++) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003838 expr_ty elt = asdl_seq_GET(elts, i);
3839 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003840 seen_star = 1;
3841 }
3842 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003843 if (!seen_star && !big) {
3844 for (Py_ssize_t i = 0; i < n; i++) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003845 expr_ty elt = asdl_seq_GET(elts, i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003846 VISIT(c, expr, elt);
Mark Shannon13bc1392020-01-23 09:25:17 +00003847 }
3848 if (tuple) {
3849 ADDOP_I(c, BUILD_TUPLE, n+pushed);
3850 } else {
3851 ADDOP_I(c, build, n+pushed);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003852 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003853 return 1;
3854 }
3855 int sequence_built = 0;
3856 if (big) {
3857 ADDOP_I(c, build, pushed);
3858 sequence_built = 1;
3859 }
3860 for (Py_ssize_t i = 0; i < n; i++) {
3861 expr_ty elt = asdl_seq_GET(elts, i);
3862 if (elt->kind == Starred_kind) {
3863 if (sequence_built == 0) {
3864 ADDOP_I(c, build, i+pushed);
3865 sequence_built = 1;
3866 }
3867 VISIT(c, expr, elt->v.Starred.value);
3868 ADDOP_I(c, extend, 1);
3869 }
3870 else {
3871 VISIT(c, expr, elt);
3872 if (sequence_built) {
3873 ADDOP_I(c, add, 1);
3874 }
3875 }
3876 }
3877 assert(sequence_built);
3878 if (tuple) {
3879 ADDOP(c, LIST_TO_TUPLE);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003880 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003881 return 1;
3882}
3883
3884static int
Brandt Bucher145bf262021-02-26 14:51:55 -08003885unpack_helper(struct compiler *c, asdl_expr_seq *elts)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003886{
3887 Py_ssize_t n = asdl_seq_LEN(elts);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003888 int seen_star = 0;
Brandt Bucher145bf262021-02-26 14:51:55 -08003889 for (Py_ssize_t i = 0; i < n; i++) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003890 expr_ty elt = asdl_seq_GET(elts, i);
3891 if (elt->kind == Starred_kind && !seen_star) {
3892 if ((i >= (1 << 8)) ||
3893 (n-i-1 >= (INT_MAX >> 8)))
3894 return compiler_error(c,
3895 "too many expressions in "
3896 "star-unpacking assignment");
3897 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3898 seen_star = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003899 }
3900 else if (elt->kind == Starred_kind) {
3901 return compiler_error(c,
Furkan Öndercb6534e2020-03-26 04:54:31 +03003902 "multiple starred expressions in assignment");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003903 }
3904 }
3905 if (!seen_star) {
3906 ADDOP_I(c, UNPACK_SEQUENCE, n);
3907 }
Brandt Bucher145bf262021-02-26 14:51:55 -08003908 return 1;
3909}
3910
3911static int
3912assignment_helper(struct compiler *c, asdl_expr_seq *elts)
3913{
3914 Py_ssize_t n = asdl_seq_LEN(elts);
3915 RETURN_IF_FALSE(unpack_helper(c, elts));
3916 for (Py_ssize_t i = 0; i < n; i++) {
Brandt Bucherd5aa2e92020-03-07 19:44:18 -08003917 expr_ty elt = asdl_seq_GET(elts, i);
3918 VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value);
3919 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003920 return 1;
3921}
3922
3923static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003924compiler_list(struct compiler *c, expr_ty e)
3925{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003926 asdl_expr_seq *elts = e->v.List.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003927 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003928 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003929 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003930 else if (e->v.List.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003931 return starunpack_helper(c, elts, 0, BUILD_LIST,
3932 LIST_APPEND, LIST_EXTEND, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003933 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003934 else
3935 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003936 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003937}
3938
3939static int
3940compiler_tuple(struct compiler *c, expr_ty e)
3941{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003942 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003943 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003944 return assignment_helper(c, elts);
3945 }
3946 else if (e->v.Tuple.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003947 return starunpack_helper(c, elts, 0, BUILD_LIST,
3948 LIST_APPEND, LIST_EXTEND, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003949 }
3950 else
3951 VISIT_SEQ(c, expr, elts);
3952 return 1;
3953}
3954
3955static int
3956compiler_set(struct compiler *c, expr_ty e)
3957{
Mark Shannon13bc1392020-01-23 09:25:17 +00003958 return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET,
3959 SET_ADD, SET_UPDATE, 0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003960}
3961
3962static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003963are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003964{
3965 Py_ssize_t i;
3966 for (i = begin; i < end; i++) {
3967 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003968 if (key == NULL || key->kind != Constant_kind)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003969 return 0;
3970 }
3971 return 1;
3972}
3973
3974static int
3975compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3976{
3977 Py_ssize_t i, n = end - begin;
3978 PyObject *keys, *key;
Mark Shannon11e0b292021-04-15 14:28:56 +01003979 int big = n*2 > STACK_USE_GUIDELINE;
3980 if (n > 1 && !big && are_all_items_const(e->v.Dict.keys, begin, end)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003981 for (i = begin; i < end; i++) {
3982 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3983 }
3984 keys = PyTuple_New(n);
3985 if (keys == NULL) {
3986 return 0;
3987 }
3988 for (i = begin; i < end; i++) {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003989 key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003990 Py_INCREF(key);
3991 PyTuple_SET_ITEM(keys, i - begin, key);
3992 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003993 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003994 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
Mark Shannon11e0b292021-04-15 14:28:56 +01003995 return 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003996 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003997 if (big) {
3998 ADDOP_I(c, BUILD_MAP, 0);
3999 }
4000 for (i = begin; i < end; i++) {
4001 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
4002 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
4003 if (big) {
4004 ADDOP_I(c, MAP_ADD, 1);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004005 }
Mark Shannon11e0b292021-04-15 14:28:56 +01004006 }
4007 if (!big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004008 ADDOP_I(c, BUILD_MAP, n);
4009 }
4010 return 1;
4011}
4012
4013static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004014compiler_dict(struct compiler *c, expr_ty e)
4015{
Victor Stinner976bb402016-03-23 11:36:19 +01004016 Py_ssize_t i, n, elements;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004017 int have_dict;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004018 int is_unpacking = 0;
4019 n = asdl_seq_LEN(e->v.Dict.values);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004020 have_dict = 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004021 elements = 0;
4022 for (i = 0; i < n; i++) {
4023 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004024 if (is_unpacking) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004025 if (elements) {
4026 if (!compiler_subdict(c, e, i - elements, i)) {
4027 return 0;
4028 }
4029 if (have_dict) {
4030 ADDOP_I(c, DICT_UPDATE, 1);
4031 }
4032 have_dict = 1;
4033 elements = 0;
4034 }
4035 if (have_dict == 0) {
4036 ADDOP_I(c, BUILD_MAP, 0);
4037 have_dict = 1;
4038 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004039 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Mark Shannon8a4cd702020-01-27 09:57:45 +00004040 ADDOP_I(c, DICT_UPDATE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004041 }
4042 else {
Mark Shannon11e0b292021-04-15 14:28:56 +01004043 if (elements*2 > STACK_USE_GUIDELINE) {
Pablo Galindoc51db0e2020-08-13 09:48:41 +01004044 if (!compiler_subdict(c, e, i - elements, i + 1)) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004045 return 0;
4046 }
4047 if (have_dict) {
4048 ADDOP_I(c, DICT_UPDATE, 1);
4049 }
4050 have_dict = 1;
4051 elements = 0;
4052 }
4053 else {
4054 elements++;
4055 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004056 }
4057 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004058 if (elements) {
4059 if (!compiler_subdict(c, e, n - elements, n)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004060 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004061 }
4062 if (have_dict) {
4063 ADDOP_I(c, DICT_UPDATE, 1);
4064 }
4065 have_dict = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004066 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004067 if (!have_dict) {
4068 ADDOP_I(c, BUILD_MAP, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004069 }
4070 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004071}
4072
4073static int
4074compiler_compare(struct compiler *c, expr_ty e)
4075{
Victor Stinnerad9a0662013-11-19 22:23:20 +01004076 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004077
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02004078 if (!check_compare(c, e)) {
4079 return 0;
4080 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004081 VISIT(c, expr, e->v.Compare.left);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004082 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
4083 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
4084 if (n == 0) {
4085 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Mark Shannon9af0e472020-01-14 10:12:45 +00004086 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004087 }
4088 else {
4089 basicblock *cleanup = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004090 if (cleanup == NULL)
4091 return 0;
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004092 for (i = 0; i < n; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004093 VISIT(c, expr,
4094 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004095 ADDOP(c, DUP_TOP);
4096 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00004097 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01004098 ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004099 NEXT_BLOCK(c);
4100 }
4101 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00004102 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004103 basicblock *end = compiler_new_block(c);
4104 if (end == NULL)
4105 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00004106 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004107 compiler_use_next_block(c, cleanup);
4108 ADDOP(c, ROT_TWO);
4109 ADDOP(c, POP_TOP);
4110 compiler_use_next_block(c, end);
4111 }
4112 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004113}
4114
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004115static PyTypeObject *
4116infer_type(expr_ty e)
4117{
4118 switch (e->kind) {
4119 case Tuple_kind:
4120 return &PyTuple_Type;
4121 case List_kind:
4122 case ListComp_kind:
4123 return &PyList_Type;
4124 case Dict_kind:
4125 case DictComp_kind:
4126 return &PyDict_Type;
4127 case Set_kind:
4128 case SetComp_kind:
4129 return &PySet_Type;
4130 case GeneratorExp_kind:
4131 return &PyGen_Type;
4132 case Lambda_kind:
4133 return &PyFunction_Type;
4134 case JoinedStr_kind:
4135 case FormattedValue_kind:
4136 return &PyUnicode_Type;
4137 case Constant_kind:
Victor Stinnera102ed72020-02-07 02:24:48 +01004138 return Py_TYPE(e->v.Constant.value);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004139 default:
4140 return NULL;
4141 }
4142}
4143
4144static int
4145check_caller(struct compiler *c, expr_ty e)
4146{
4147 switch (e->kind) {
4148 case Constant_kind:
4149 case Tuple_kind:
4150 case List_kind:
4151 case ListComp_kind:
4152 case Dict_kind:
4153 case DictComp_kind:
4154 case Set_kind:
4155 case SetComp_kind:
4156 case GeneratorExp_kind:
4157 case JoinedStr_kind:
4158 case FormattedValue_kind:
4159 return compiler_warn(c, "'%.200s' object is not callable; "
4160 "perhaps you missed a comma?",
4161 infer_type(e)->tp_name);
4162 default:
4163 return 1;
4164 }
4165}
4166
4167static int
4168check_subscripter(struct compiler *c, expr_ty e)
4169{
4170 PyObject *v;
4171
4172 switch (e->kind) {
4173 case Constant_kind:
4174 v = e->v.Constant.value;
4175 if (!(v == Py_None || v == Py_Ellipsis ||
4176 PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
4177 PyAnySet_Check(v)))
4178 {
4179 return 1;
4180 }
4181 /* fall through */
4182 case Set_kind:
4183 case SetComp_kind:
4184 case GeneratorExp_kind:
4185 case Lambda_kind:
4186 return compiler_warn(c, "'%.200s' object is not subscriptable; "
4187 "perhaps you missed a comma?",
4188 infer_type(e)->tp_name);
4189 default:
4190 return 1;
4191 }
4192}
4193
4194static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004195check_index(struct compiler *c, expr_ty e, expr_ty s)
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004196{
4197 PyObject *v;
4198
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004199 PyTypeObject *index_type = infer_type(s);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004200 if (index_type == NULL
4201 || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
4202 || index_type == &PySlice_Type) {
4203 return 1;
4204 }
4205
4206 switch (e->kind) {
4207 case Constant_kind:
4208 v = e->v.Constant.value;
4209 if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
4210 return 1;
4211 }
4212 /* fall through */
4213 case Tuple_kind:
4214 case List_kind:
4215 case ListComp_kind:
4216 case JoinedStr_kind:
4217 case FormattedValue_kind:
4218 return compiler_warn(c, "%.200s indices must be integers or slices, "
4219 "not %.200s; "
4220 "perhaps you missed a comma?",
4221 infer_type(e)->tp_name,
4222 index_type->tp_name);
4223 default:
4224 return 1;
4225 }
4226}
4227
Zackery Spytz97f5de02019-03-22 01:30:32 -06004228// Return 1 if the method call was optimized, -1 if not, and 0 on error.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004229static int
Yury Selivanovf2392132016-12-13 19:03:51 -05004230maybe_optimize_method_call(struct compiler *c, expr_ty e)
4231{
4232 Py_ssize_t argsl, i;
4233 expr_ty meth = e->v.Call.func;
Pablo Galindoa5634c42020-09-16 19:42:00 +01004234 asdl_expr_seq *args = e->v.Call.args;
Yury Selivanovf2392132016-12-13 19:03:51 -05004235
4236 /* Check that the call node is an attribute access, and that
4237 the call doesn't have keyword parameters. */
4238 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
Mark Shannon11e0b292021-04-15 14:28:56 +01004239 asdl_seq_LEN(e->v.Call.keywords)) {
Yury Selivanovf2392132016-12-13 19:03:51 -05004240 return -1;
Mark Shannon11e0b292021-04-15 14:28:56 +01004241 }
4242 /* Check that there aren't too many arguments */
Yury Selivanovf2392132016-12-13 19:03:51 -05004243 argsl = asdl_seq_LEN(args);
Mark Shannon11e0b292021-04-15 14:28:56 +01004244 if (argsl >= STACK_USE_GUIDELINE) {
4245 return -1;
4246 }
4247 /* Check that there are no *varargs types of arguments. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004248 for (i = 0; i < argsl; i++) {
4249 expr_ty elt = asdl_seq_GET(args, i);
4250 if (elt->kind == Starred_kind) {
4251 return -1;
4252 }
4253 }
4254
4255 /* Alright, we can optimize the code. */
4256 VISIT(c, expr, meth->v.Attribute.value);
Mark Shannond48848c2021-03-14 18:01:30 +00004257 int old_lineno = c->u->u_lineno;
4258 c->u->u_lineno = meth->end_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004259 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
4260 VISIT_SEQ(c, expr, e->v.Call.args);
4261 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
Mark Shannond48848c2021-03-14 18:01:30 +00004262 c->u->u_lineno = old_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004263 return 1;
4264}
4265
4266static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004267validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
Zackery Spytz08050e92020-04-06 00:47:47 -06004268{
4269 Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
4270 for (Py_ssize_t i = 0; i < nkeywords; i++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004271 keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
4272 if (key->arg == NULL) {
4273 continue;
4274 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +01004275 if (forbidden_name(c, key->arg, Store)) {
4276 return -1;
4277 }
Zackery Spytz08050e92020-04-06 00:47:47 -06004278 for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004279 keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
4280 if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07004281 SET_LOC(c, other);
Brandt Bucher145bf262021-02-26 14:51:55 -08004282 compiler_error(c, "keyword argument repeated: %U", key->arg);
Pablo Galindo254ec782020-04-03 20:37:13 +01004283 return -1;
4284 }
4285 }
4286 }
4287 return 0;
4288}
4289
4290static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004291compiler_call(struct compiler *c, expr_ty e)
4292{
Zackery Spytz97f5de02019-03-22 01:30:32 -06004293 int ret = maybe_optimize_method_call(c, e);
4294 if (ret >= 0) {
4295 return ret;
4296 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004297 if (!check_caller(c, e->v.Call.func)) {
4298 return 0;
4299 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004300 VISIT(c, expr, e->v.Call.func);
4301 return compiler_call_helper(c, 0,
4302 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004303 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004304}
4305
Eric V. Smith235a6f02015-09-19 14:51:32 -04004306static int
4307compiler_joined_str(struct compiler *c, expr_ty e)
4308{
Mark Shannon11e0b292021-04-15 14:28:56 +01004309
4310 Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values);
4311 if (value_count > STACK_USE_GUIDELINE) {
4312 ADDOP_LOAD_CONST_NEW(c, _PyUnicode_FromASCII("", 0));
4313 PyObject *join = _PyUnicode_FromASCII("join", 4);
4314 if (join == NULL) {
4315 return 0;
4316 }
4317 ADDOP_NAME(c, LOAD_METHOD, join, names);
4318 Py_DECREF(join);
4319 ADDOP_I(c, BUILD_LIST, 0);
4320 for (Py_ssize_t i = 0; i < asdl_seq_LEN(e->v.JoinedStr.values); i++) {
4321 VISIT(c, expr, asdl_seq_GET(e->v.JoinedStr.values, i));
4322 ADDOP_I(c, LIST_APPEND, 1);
4323 }
4324 ADDOP_I(c, CALL_METHOD, 1);
4325 }
4326 else {
4327 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
4328 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) {
4329 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
4330 }
4331 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004332 return 1;
4333}
4334
Eric V. Smitha78c7952015-11-03 12:45:05 -05004335/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004336static int
4337compiler_formatted_value(struct compiler *c, expr_ty e)
4338{
Eric V. Smitha78c7952015-11-03 12:45:05 -05004339 /* Our oparg encodes 2 pieces of information: the conversion
4340 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04004341
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004342 Convert the conversion char to 3 bits:
4343 : 000 0x0 FVC_NONE The default if nothing specified.
Eric V. Smitha78c7952015-11-03 12:45:05 -05004344 !s : 001 0x1 FVC_STR
4345 !r : 010 0x2 FVC_REPR
4346 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04004347
Eric V. Smitha78c7952015-11-03 12:45:05 -05004348 next bit is whether or not we have a format spec:
4349 yes : 100 0x4
4350 no : 000 0x0
4351 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004352
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004353 int conversion = e->v.FormattedValue.conversion;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004354 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004355
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004356 /* The expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004357 VISIT(c, expr, e->v.FormattedValue.value);
4358
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004359 switch (conversion) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004360 case 's': oparg = FVC_STR; break;
4361 case 'r': oparg = FVC_REPR; break;
4362 case 'a': oparg = FVC_ASCII; break;
4363 case -1: oparg = FVC_NONE; break;
4364 default:
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004365 PyErr_Format(PyExc_SystemError,
4366 "Unrecognized conversion character %d", conversion);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004367 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004368 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004369 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004370 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004371 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004372 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004373 }
4374
Eric V. Smitha78c7952015-11-03 12:45:05 -05004375 /* And push our opcode and oparg */
4376 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004377
Eric V. Smith235a6f02015-09-19 14:51:32 -04004378 return 1;
4379}
4380
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004381static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004382compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004383{
4384 Py_ssize_t i, n = end - begin;
4385 keyword_ty kw;
4386 PyObject *keys, *key;
4387 assert(n > 0);
Mark Shannon11e0b292021-04-15 14:28:56 +01004388 int big = n*2 > STACK_USE_GUIDELINE;
4389 if (n > 1 && !big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004390 for (i = begin; i < end; i++) {
4391 kw = asdl_seq_GET(keywords, i);
4392 VISIT(c, expr, kw->value);
4393 }
4394 keys = PyTuple_New(n);
4395 if (keys == NULL) {
4396 return 0;
4397 }
4398 for (i = begin; i < end; i++) {
4399 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
4400 Py_INCREF(key);
4401 PyTuple_SET_ITEM(keys, i - begin, key);
4402 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004403 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004404 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
Mark Shannon11e0b292021-04-15 14:28:56 +01004405 return 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004406 }
Mark Shannon11e0b292021-04-15 14:28:56 +01004407 if (big) {
4408 ADDOP_I_NOLINE(c, BUILD_MAP, 0);
4409 }
4410 for (i = begin; i < end; i++) {
4411 kw = asdl_seq_GET(keywords, i);
4412 ADDOP_LOAD_CONST(c, kw->arg);
4413 VISIT(c, expr, kw->value);
4414 if (big) {
4415 ADDOP_I_NOLINE(c, MAP_ADD, 1);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004416 }
Mark Shannon11e0b292021-04-15 14:28:56 +01004417 }
4418 if (!big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004419 ADDOP_I(c, BUILD_MAP, n);
4420 }
4421 return 1;
4422}
4423
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004424/* shared code between compiler_call and compiler_class */
4425static int
4426compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01004427 int n, /* Args already pushed */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004428 asdl_expr_seq *args,
4429 asdl_keyword_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004430{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004431 Py_ssize_t i, nseen, nelts, nkwelts;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004432
Pablo Galindo254ec782020-04-03 20:37:13 +01004433 if (validate_keywords(c, keywords) == -1) {
4434 return 0;
4435 }
4436
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004437 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004438 nkwelts = asdl_seq_LEN(keywords);
4439
Mark Shannon11e0b292021-04-15 14:28:56 +01004440 if (nelts + nkwelts*2 > STACK_USE_GUIDELINE) {
4441 goto ex_call;
4442 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004443 for (i = 0; i < nelts; i++) {
4444 expr_ty elt = asdl_seq_GET(args, i);
4445 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00004446 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004447 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004448 }
4449 for (i = 0; i < nkwelts; i++) {
4450 keyword_ty kw = asdl_seq_GET(keywords, i);
4451 if (kw->arg == NULL) {
4452 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004453 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004454 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004455
Mark Shannon13bc1392020-01-23 09:25:17 +00004456 /* No * or ** args, so can use faster calling sequence */
4457 for (i = 0; i < nelts; i++) {
4458 expr_ty elt = asdl_seq_GET(args, i);
4459 assert(elt->kind != Starred_kind);
4460 VISIT(c, expr, elt);
4461 }
4462 if (nkwelts) {
4463 PyObject *names;
4464 VISIT_SEQ(c, keyword, keywords);
4465 names = PyTuple_New(nkwelts);
4466 if (names == NULL) {
4467 return 0;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004468 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004469 for (i = 0; i < nkwelts; i++) {
4470 keyword_ty kw = asdl_seq_GET(keywords, i);
4471 Py_INCREF(kw->arg);
4472 PyTuple_SET_ITEM(names, i, kw->arg);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004473 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004474 ADDOP_LOAD_CONST_NEW(c, names);
4475 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
4476 return 1;
4477 }
4478 else {
4479 ADDOP_I(c, CALL_FUNCTION, n + nelts);
4480 return 1;
4481 }
4482
4483ex_call:
4484
4485 /* Do positional arguments. */
4486 if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
4487 VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
4488 }
4489 else if (starunpack_helper(c, args, n, BUILD_LIST,
4490 LIST_APPEND, LIST_EXTEND, 1) == 0) {
4491 return 0;
4492 }
4493 /* Then keyword arguments */
4494 if (nkwelts) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004495 /* Has a new dict been pushed */
4496 int have_dict = 0;
Mark Shannon13bc1392020-01-23 09:25:17 +00004497
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004498 nseen = 0; /* the number of keyword arguments on the stack following */
4499 for (i = 0; i < nkwelts; i++) {
4500 keyword_ty kw = asdl_seq_GET(keywords, i);
4501 if (kw->arg == NULL) {
4502 /* A keyword argument unpacking. */
4503 if (nseen) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004504 if (!compiler_subkwargs(c, keywords, i - nseen, i)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004505 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004506 }
Mark Shannondb64f122020-06-01 10:42:42 +01004507 if (have_dict) {
4508 ADDOP_I(c, DICT_MERGE, 1);
4509 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004510 have_dict = 1;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004511 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004512 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004513 if (!have_dict) {
4514 ADDOP_I(c, BUILD_MAP, 0);
4515 have_dict = 1;
4516 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004517 VISIT(c, expr, kw->value);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004518 ADDOP_I(c, DICT_MERGE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004519 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004520 else {
4521 nseen++;
4522 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004523 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004524 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004525 /* Pack up any trailing keyword arguments. */
Mark Shannon8a4cd702020-01-27 09:57:45 +00004526 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004527 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004528 }
4529 if (have_dict) {
4530 ADDOP_I(c, DICT_MERGE, 1);
4531 }
4532 have_dict = 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004533 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004534 assert(have_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004535 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004536 ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0);
4537 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004538}
4539
Nick Coghlan650f0d02007-04-15 12:05:43 +00004540
4541/* List and set comprehensions and generator expressions work by creating a
4542 nested function to perform the actual iteration. This means that the
4543 iteration variables don't leak into the current scope.
4544 The defined function is called immediately following its definition, with the
4545 result of that call being the result of the expression.
4546 The LC/SC version returns the populated container, while the GE version is
4547 flagged in symtable.c as a generator, so it returns the generator object
4548 when the function is called.
Nick Coghlan650f0d02007-04-15 12:05:43 +00004549
4550 Possible cleanups:
4551 - iterate over the generator sequence instead of using recursion
4552*/
4553
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004554
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004555static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004556compiler_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,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004559 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004560{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004561 comprehension_ty gen;
4562 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4563 if (gen->is_async) {
4564 return compiler_async_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004565 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004566 } else {
4567 return compiler_sync_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004568 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004569 }
4570}
4571
4572static int
4573compiler_sync_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004574 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004575 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004576 expr_ty elt, expr_ty val, int type)
4577{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004578 /* generate code for the iterator, then each of the ifs,
4579 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004581 comprehension_ty gen;
4582 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004583 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004585 start = compiler_new_block(c);
4586 skip = compiler_new_block(c);
4587 if_cleanup = compiler_new_block(c);
4588 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004590 if (start == NULL || skip == NULL || if_cleanup == NULL ||
4591 anchor == NULL)
4592 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004594 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004596 if (gen_index == 0) {
4597 /* Receive outermost iter as an implicit argument */
4598 c->u->u_argcount = 1;
4599 ADDOP_I(c, LOAD_FAST, 0);
4600 }
4601 else {
4602 /* Sub-iter - calculate on the fly */
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004603 /* Fast path for the temporary variable assignment idiom:
4604 for y in [f(x)]
4605 */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004606 asdl_expr_seq *elts;
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004607 switch (gen->iter->kind) {
4608 case List_kind:
4609 elts = gen->iter->v.List.elts;
4610 break;
4611 case Tuple_kind:
4612 elts = gen->iter->v.Tuple.elts;
4613 break;
4614 default:
4615 elts = NULL;
4616 }
4617 if (asdl_seq_LEN(elts) == 1) {
4618 expr_ty elt = asdl_seq_GET(elts, 0);
4619 if (elt->kind != Starred_kind) {
4620 VISIT(c, expr, elt);
4621 start = NULL;
4622 }
4623 }
4624 if (start) {
4625 VISIT(c, expr, gen->iter);
4626 ADDOP(c, GET_ITER);
4627 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004628 }
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004629 if (start) {
4630 depth++;
4631 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01004632 ADDOP_JUMP(c, FOR_ITER, anchor);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004633 NEXT_BLOCK(c);
4634 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004635 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004637 /* XXX this needs to be cleaned up...a lot! */
4638 n = asdl_seq_LEN(gen->ifs);
4639 for (i = 0; i < n; i++) {
4640 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004641 if (!compiler_jump_if(c, e, if_cleanup, 0))
4642 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004643 NEXT_BLOCK(c);
4644 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004646 if (++gen_index < asdl_seq_LEN(generators))
4647 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004648 generators, gen_index, depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004649 elt, val, type))
4650 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004652 /* only append after the last for generator */
4653 if (gen_index >= asdl_seq_LEN(generators)) {
4654 /* comprehension specific code */
4655 switch (type) {
4656 case COMP_GENEXP:
4657 VISIT(c, expr, elt);
4658 ADDOP(c, YIELD_VALUE);
4659 ADDOP(c, POP_TOP);
4660 break;
4661 case COMP_LISTCOMP:
4662 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004663 ADDOP_I(c, LIST_APPEND, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004664 break;
4665 case COMP_SETCOMP:
4666 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004667 ADDOP_I(c, SET_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004668 break;
4669 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004670 /* With '{k: v}', k is evaluated before v, so we do
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004671 the same. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004672 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004673 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004674 ADDOP_I(c, MAP_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004675 break;
4676 default:
4677 return 0;
4678 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004680 compiler_use_next_block(c, skip);
4681 }
4682 compiler_use_next_block(c, if_cleanup);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004683 if (start) {
Mark Shannon582aaf12020-08-04 17:30:11 +01004684 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004685 compiler_use_next_block(c, anchor);
4686 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004687
4688 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004689}
4690
4691static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004692compiler_async_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004693 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004694 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004695 expr_ty elt, expr_ty val, int type)
4696{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004697 comprehension_ty gen;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004698 basicblock *start, *if_cleanup, *except;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004699 Py_ssize_t i, n;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004700 start = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004701 except = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004702 if_cleanup = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004703
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004704 if (start == NULL || if_cleanup == NULL || except == NULL) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004705 return 0;
4706 }
4707
4708 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4709
4710 if (gen_index == 0) {
4711 /* Receive outermost iter as an implicit argument */
4712 c->u->u_argcount = 1;
4713 ADDOP_I(c, LOAD_FAST, 0);
4714 }
4715 else {
4716 /* Sub-iter - calculate on the fly */
4717 VISIT(c, expr, gen->iter);
4718 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004719 }
4720
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004721 compiler_use_next_block(c, start);
tomKPZ7a7ba3d2021-04-07 07:43:45 -07004722 /* Runtime will push a block here, so we need to account for that */
4723 if (!compiler_push_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start,
4724 NULL, NULL)) {
4725 return 0;
4726 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004727
Mark Shannon582aaf12020-08-04 17:30:11 +01004728 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004729 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004730 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004731 ADDOP(c, YIELD_FROM);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004732 ADDOP(c, POP_BLOCK);
Serhiy Storchaka24d32012018-03-10 18:22:34 +02004733 VISIT(c, expr, gen->target);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004734
4735 n = asdl_seq_LEN(gen->ifs);
4736 for (i = 0; i < n; i++) {
4737 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004738 if (!compiler_jump_if(c, e, if_cleanup, 0))
4739 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004740 NEXT_BLOCK(c);
4741 }
4742
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004743 depth++;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004744 if (++gen_index < asdl_seq_LEN(generators))
4745 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004746 generators, gen_index, depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004747 elt, val, type))
4748 return 0;
4749
4750 /* only append after the last for generator */
4751 if (gen_index >= asdl_seq_LEN(generators)) {
4752 /* comprehension specific code */
4753 switch (type) {
4754 case COMP_GENEXP:
4755 VISIT(c, expr, elt);
4756 ADDOP(c, YIELD_VALUE);
4757 ADDOP(c, POP_TOP);
4758 break;
4759 case COMP_LISTCOMP:
4760 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004761 ADDOP_I(c, LIST_APPEND, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004762 break;
4763 case COMP_SETCOMP:
4764 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004765 ADDOP_I(c, SET_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004766 break;
4767 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004768 /* With '{k: v}', k is evaluated before v, so we do
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004769 the same. */
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004770 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004771 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004772 ADDOP_I(c, MAP_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004773 break;
4774 default:
4775 return 0;
4776 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004777 }
4778 compiler_use_next_block(c, if_cleanup);
Mark Shannon582aaf12020-08-04 17:30:11 +01004779 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004780
tomKPZ7a7ba3d2021-04-07 07:43:45 -07004781 compiler_pop_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start);
4782
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004783 compiler_use_next_block(c, except);
4784 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004785
4786 return 1;
4787}
4788
4789static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004790compiler_comprehension(struct compiler *c, expr_ty e, int type,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004791 identifier name, asdl_comprehension_seq *generators, expr_ty elt,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004792 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004794 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004795 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004796 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004797 int is_async_generator = 0;
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004798 int top_level_await = IS_TOP_LEVEL_AWAIT(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004799
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004800
Batuhan Taßkaya9052f7a2020-03-19 14:35:44 +03004801 int is_async_function = c->u->u_ste->ste_coroutine;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004802
Batuhan Taßkaya9052f7a2020-03-19 14:35:44 +03004803 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004804 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4805 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004806 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004807 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004808 }
Mark Shannon7674c832021-06-21 11:47:16 +01004809 SET_LOC(c, e);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004810
4811 is_async_generator = c->u->u_ste->ste_coroutine;
4812
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004813 if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004814 compiler_error(c, "asynchronous comprehension outside of "
4815 "an asynchronous function");
4816 goto error_in_scope;
4817 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004819 if (type != COMP_GENEXP) {
4820 int op;
4821 switch (type) {
4822 case COMP_LISTCOMP:
4823 op = BUILD_LIST;
4824 break;
4825 case COMP_SETCOMP:
4826 op = BUILD_SET;
4827 break;
4828 case COMP_DICTCOMP:
4829 op = BUILD_MAP;
4830 break;
4831 default:
4832 PyErr_Format(PyExc_SystemError,
4833 "unknown comprehension type %d", type);
4834 goto error_in_scope;
4835 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004837 ADDOP_I(c, op, 0);
4838 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004839
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004840 if (!compiler_comprehension_generator(c, generators, 0, 0, elt,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004841 val, type))
4842 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004844 if (type != COMP_GENEXP) {
4845 ADDOP(c, RETURN_VALUE);
4846 }
4847
4848 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004849 qualname = c->u->u_qualname;
4850 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004851 compiler_exit_scope(c);
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004852 if (top_level_await && is_async_generator){
4853 c->u->u_ste->ste_coroutine = 1;
4854 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004855 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004856 goto error;
4857
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004858 if (!compiler_make_closure(c, co, 0, qualname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004859 goto error;
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004860 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004861 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004862 Py_DECREF(co);
4863
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004864 VISIT(c, expr, outermost->iter);
4865
4866 if (outermost->is_async) {
4867 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004868 } else {
4869 ADDOP(c, GET_ITER);
4870 }
4871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004872 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004873
4874 if (is_async_generator && type != COMP_GENEXP) {
4875 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004876 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004877 ADDOP(c, YIELD_FROM);
4878 }
4879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004880 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004881error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004882 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004883error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004884 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004885 Py_XDECREF(co);
4886 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004887}
4888
4889static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004890compiler_genexp(struct compiler *c, expr_ty e)
4891{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004892 static identifier name;
4893 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004894 name = PyUnicode_InternFromString("<genexpr>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004895 if (!name)
4896 return 0;
4897 }
4898 assert(e->kind == GeneratorExp_kind);
4899 return compiler_comprehension(c, e, COMP_GENEXP, name,
4900 e->v.GeneratorExp.generators,
4901 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004902}
4903
4904static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004905compiler_listcomp(struct compiler *c, expr_ty e)
4906{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004907 static identifier name;
4908 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004909 name = PyUnicode_InternFromString("<listcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004910 if (!name)
4911 return 0;
4912 }
4913 assert(e->kind == ListComp_kind);
4914 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4915 e->v.ListComp.generators,
4916 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004917}
4918
4919static int
4920compiler_setcomp(struct compiler *c, expr_ty e)
4921{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004922 static identifier name;
4923 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004924 name = PyUnicode_InternFromString("<setcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004925 if (!name)
4926 return 0;
4927 }
4928 assert(e->kind == SetComp_kind);
4929 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4930 e->v.SetComp.generators,
4931 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004932}
4933
4934
4935static int
4936compiler_dictcomp(struct compiler *c, expr_ty e)
4937{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004938 static identifier name;
4939 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004940 name = PyUnicode_InternFromString("<dictcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004941 if (!name)
4942 return 0;
4943 }
4944 assert(e->kind == DictComp_kind);
4945 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4946 e->v.DictComp.generators,
4947 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004948}
4949
4950
4951static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004952compiler_visit_keyword(struct compiler *c, keyword_ty k)
4953{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004954 VISIT(c, expr, k->value);
4955 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004956}
4957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004958/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004959 whether they are true or false.
4960
4961 Return values: 1 for true, 0 for false, -1 for non-constant.
4962 */
4963
4964static int
Mark Shannonfee55262019-11-21 09:11:43 +00004965compiler_with_except_finish(struct compiler *c) {
4966 basicblock *exit;
4967 exit = compiler_new_block(c);
4968 if (exit == NULL)
4969 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01004970 ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit);
Mark Shannon266b4622020-11-17 19:30:14 +00004971 NEXT_BLOCK(c);
Mark Shannonbf353f32020-12-17 13:55:28 +00004972 ADDOP_I(c, RERAISE, 1);
Mark Shannonfee55262019-11-21 09:11:43 +00004973 compiler_use_next_block(c, exit);
4974 ADDOP(c, POP_TOP);
4975 ADDOP(c, POP_TOP);
4976 ADDOP(c, POP_TOP);
4977 ADDOP(c, POP_EXCEPT);
4978 ADDOP(c, POP_TOP);
4979 return 1;
4980}
Yury Selivanov75445082015-05-11 22:57:16 -04004981
4982/*
4983 Implements the async with statement.
4984
4985 The semantics outlined in that PEP are as follows:
4986
4987 async with EXPR as VAR:
4988 BLOCK
4989
4990 It is implemented roughly as:
4991
4992 context = EXPR
4993 exit = context.__aexit__ # not calling it
4994 value = await context.__aenter__()
4995 try:
4996 VAR = value # if VAR present in the syntax
4997 BLOCK
4998 finally:
4999 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03005000 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04005001 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03005002 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04005003 if not (await exit(*exc)):
5004 raise
5005 */
5006static int
5007compiler_async_with(struct compiler *c, stmt_ty s, int pos)
5008{
Mark Shannonfee55262019-11-21 09:11:43 +00005009 basicblock *block, *final, *exit;
Yury Selivanov75445082015-05-11 22:57:16 -04005010 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
5011
5012 assert(s->kind == AsyncWith_kind);
Pablo Galindo90235812020-03-15 04:29:22 +00005013 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005014 c->u->u_ste->ste_coroutine = 1;
5015 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
Zsolt Dollensteine2396502018-04-27 08:58:56 -07005016 return compiler_error(c, "'async with' outside async function");
5017 }
Yury Selivanov75445082015-05-11 22:57:16 -04005018
5019 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00005020 final = compiler_new_block(c);
5021 exit = compiler_new_block(c);
5022 if (!block || !final || !exit)
Yury Selivanov75445082015-05-11 22:57:16 -04005023 return 0;
5024
5025 /* Evaluate EXPR */
5026 VISIT(c, expr, item->context_expr);
5027
5028 ADDOP(c, BEFORE_ASYNC_WITH);
5029 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005030 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005031 ADDOP(c, YIELD_FROM);
5032
Mark Shannon582aaf12020-08-04 17:30:11 +01005033 ADDOP_JUMP(c, SETUP_ASYNC_WITH, final);
Yury Selivanov75445082015-05-11 22:57:16 -04005034
5035 /* SETUP_ASYNC_WITH pushes a finally block. */
5036 compiler_use_next_block(c, block);
Mark Shannon5979e812021-04-30 14:32:47 +01005037 if (!compiler_push_fblock(c, ASYNC_WITH, block, final, s)) {
Yury Selivanov75445082015-05-11 22:57:16 -04005038 return 0;
5039 }
5040
5041 if (item->optional_vars) {
5042 VISIT(c, expr, item->optional_vars);
5043 }
5044 else {
5045 /* Discard result from context.__aenter__() */
5046 ADDOP(c, POP_TOP);
5047 }
5048
5049 pos++;
5050 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
5051 /* BLOCK code */
5052 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
5053 else if (!compiler_async_with(c, s, pos))
5054 return 0;
5055
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005056 compiler_pop_fblock(c, ASYNC_WITH, block);
Mark Shannonfee55262019-11-21 09:11:43 +00005057 ADDOP(c, POP_BLOCK);
5058 /* End of body; start the cleanup */
Yury Selivanov75445082015-05-11 22:57:16 -04005059
Mark Shannonfee55262019-11-21 09:11:43 +00005060 /* For successful outcome:
5061 * call __exit__(None, None, None)
5062 */
Mark Shannon5979e812021-04-30 14:32:47 +01005063 SET_LOC(c, s);
Mark Shannonfee55262019-11-21 09:11:43 +00005064 if(!compiler_call_exit_with_nones(c))
Yury Selivanov75445082015-05-11 22:57:16 -04005065 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00005066 ADDOP(c, GET_AWAITABLE);
Miss Islington (bot)ebbd0ac2021-08-31 11:08:32 -07005067 ADDOP_LOAD_CONST(c, Py_None);
Mark Shannonfee55262019-11-21 09:11:43 +00005068 ADDOP(c, YIELD_FROM);
Yury Selivanov75445082015-05-11 22:57:16 -04005069
Mark Shannonfee55262019-11-21 09:11:43 +00005070 ADDOP(c, POP_TOP);
Yury Selivanov75445082015-05-11 22:57:16 -04005071
Mark Shannon582aaf12020-08-04 17:30:11 +01005072 ADDOP_JUMP(c, JUMP_ABSOLUTE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00005073
5074 /* For exceptional outcome: */
5075 compiler_use_next_block(c, final);
Mark Shannonfee55262019-11-21 09:11:43 +00005076 ADDOP(c, WITH_EXCEPT_START);
Yury Selivanov75445082015-05-11 22:57:16 -04005077 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005078 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005079 ADDOP(c, YIELD_FROM);
Mark Shannonfee55262019-11-21 09:11:43 +00005080 compiler_with_except_finish(c);
Yury Selivanov75445082015-05-11 22:57:16 -04005081
Mark Shannonfee55262019-11-21 09:11:43 +00005082compiler_use_next_block(c, exit);
Yury Selivanov75445082015-05-11 22:57:16 -04005083 return 1;
5084}
5085
5086
Guido van Rossumc2e20742006-02-27 22:32:47 +00005087/*
5088 Implements the with statement from PEP 343.
Guido van Rossumc2e20742006-02-27 22:32:47 +00005089 with EXPR as VAR:
5090 BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00005091 is implemented as:
5092 <code for EXPR>
5093 SETUP_WITH E
5094 <code to store to VAR> or POP_TOP
5095 <code for BLOCK>
5096 LOAD_CONST (None, None, None)
5097 CALL_FUNCTION_EX 0
5098 JUMP_FORWARD EXIT
5099 E: WITH_EXCEPT_START (calls EXPR.__exit__)
5100 POP_JUMP_IF_TRUE T:
5101 RERAISE
5102 T: POP_TOP * 3 (remove exception from stack)
5103 POP_EXCEPT
5104 POP_TOP
5105 EXIT:
Guido van Rossumc2e20742006-02-27 22:32:47 +00005106 */
Mark Shannonfee55262019-11-21 09:11:43 +00005107
Guido van Rossumc2e20742006-02-27 22:32:47 +00005108static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005109compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00005110{
Mark Shannonfee55262019-11-21 09:11:43 +00005111 basicblock *block, *final, *exit;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005112 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005113
5114 assert(s->kind == With_kind);
5115
Guido van Rossumc2e20742006-02-27 22:32:47 +00005116 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00005117 final = compiler_new_block(c);
5118 exit = compiler_new_block(c);
5119 if (!block || !final || !exit)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005120 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005121
Thomas Wouters477c8d52006-05-27 19:21:47 +00005122 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005123 VISIT(c, expr, item->context_expr);
Mark Shannonfee55262019-11-21 09:11:43 +00005124 /* Will push bound __exit__ */
Mark Shannon582aaf12020-08-04 17:30:11 +01005125 ADDOP_JUMP(c, SETUP_WITH, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005126
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005127 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00005128 compiler_use_next_block(c, block);
Mark Shannon5979e812021-04-30 14:32:47 +01005129 if (!compiler_push_fblock(c, WITH, block, final, s)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005130 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005131 }
5132
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005133 if (item->optional_vars) {
5134 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005135 }
5136 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005137 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005138 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005139 }
5140
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005141 pos++;
5142 if (pos == asdl_seq_LEN(s->v.With.items))
5143 /* BLOCK code */
5144 VISIT_SEQ(c, stmt, s->v.With.body)
5145 else if (!compiler_with(c, s, pos))
5146 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005147
Mark Shannon3bd60352021-01-13 12:05:43 +00005148
5149 /* Mark all following code as artificial */
5150 c->u->u_lineno = -1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005151 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005152 compiler_pop_fblock(c, WITH, block);
Mark Shannon13bc1392020-01-23 09:25:17 +00005153
Mark Shannonfee55262019-11-21 09:11:43 +00005154 /* End of body; start the cleanup. */
Mark Shannon13bc1392020-01-23 09:25:17 +00005155
Mark Shannonfee55262019-11-21 09:11:43 +00005156 /* For successful outcome:
5157 * call __exit__(None, None, None)
5158 */
Mark Shannon5979e812021-04-30 14:32:47 +01005159 SET_LOC(c, s);
Mark Shannonfee55262019-11-21 09:11:43 +00005160 if (!compiler_call_exit_with_nones(c))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005161 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00005162 ADDOP(c, POP_TOP);
Mark Shannon582aaf12020-08-04 17:30:11 +01005163 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005164
Mark Shannonfee55262019-11-21 09:11:43 +00005165 /* For exceptional outcome: */
5166 compiler_use_next_block(c, final);
Mark Shannonfee55262019-11-21 09:11:43 +00005167 ADDOP(c, WITH_EXCEPT_START);
5168 compiler_with_except_finish(c);
5169
5170 compiler_use_next_block(c, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005171 return 1;
5172}
5173
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005174static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005175compiler_visit_expr1(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005177 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07005178 case NamedExpr_kind:
5179 VISIT(c, expr, e->v.NamedExpr.value);
5180 ADDOP(c, DUP_TOP);
5181 VISIT(c, expr, e->v.NamedExpr.target);
5182 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005183 case BoolOp_kind:
5184 return compiler_boolop(c, e);
5185 case BinOp_kind:
5186 VISIT(c, expr, e->v.BinOp.left);
5187 VISIT(c, expr, e->v.BinOp.right);
Andy Lester76d58772020-03-10 21:18:12 -05005188 ADDOP(c, binop(e->v.BinOp.op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005189 break;
5190 case UnaryOp_kind:
5191 VISIT(c, expr, e->v.UnaryOp.operand);
5192 ADDOP(c, unaryop(e->v.UnaryOp.op));
5193 break;
5194 case Lambda_kind:
5195 return compiler_lambda(c, e);
5196 case IfExp_kind:
5197 return compiler_ifexp(c, e);
5198 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005199 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005200 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005201 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005202 case GeneratorExp_kind:
5203 return compiler_genexp(c, e);
5204 case ListComp_kind:
5205 return compiler_listcomp(c, e);
5206 case SetComp_kind:
5207 return compiler_setcomp(c, e);
5208 case DictComp_kind:
5209 return compiler_dictcomp(c, e);
5210 case Yield_kind:
5211 if (c->u->u_ste->ste_type != FunctionBlock)
5212 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005213 if (e->v.Yield.value) {
5214 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005215 }
5216 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005217 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005218 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005219 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005220 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005221 case YieldFrom_kind:
5222 if (c->u->u_ste->ste_type != FunctionBlock)
5223 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04005224
5225 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
5226 return compiler_error(c, "'yield from' inside async function");
5227
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005228 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04005229 ADDOP(c, GET_YIELD_FROM_ITER);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005230 ADDOP_LOAD_CONST(c, Py_None);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005231 ADDOP(c, YIELD_FROM);
5232 break;
Yury Selivanov75445082015-05-11 22:57:16 -04005233 case Await_kind:
Pablo Galindo90235812020-03-15 04:29:22 +00005234 if (!IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005235 if (c->u->u_ste->ste_type != FunctionBlock){
5236 return compiler_error(c, "'await' outside function");
5237 }
Yury Selivanov75445082015-05-11 22:57:16 -04005238
Victor Stinner331a6a52019-05-27 16:39:22 +02005239 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005240 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
5241 return compiler_error(c, "'await' outside async function");
5242 }
5243 }
Yury Selivanov75445082015-05-11 22:57:16 -04005244
5245 VISIT(c, expr, e->v.Await.value);
5246 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005247 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005248 ADDOP(c, YIELD_FROM);
5249 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005250 case Compare_kind:
5251 return compiler_compare(c, e);
5252 case Call_kind:
5253 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005254 case Constant_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005255 ADDOP_LOAD_CONST(c, e->v.Constant.value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005256 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005257 case JoinedStr_kind:
5258 return compiler_joined_str(c, e);
5259 case FormattedValue_kind:
5260 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005261 /* The following exprs can be assignment targets. */
5262 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005263 VISIT(c, expr, e->v.Attribute.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005264 switch (e->v.Attribute.ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005265 case Load:
Mark Shannond48848c2021-03-14 18:01:30 +00005266 {
5267 int old_lineno = c->u->u_lineno;
5268 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005269 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005270 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005271 break;
Mark Shannond48848c2021-03-14 18:01:30 +00005272 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005273 case Store:
Mark Shannond48848c2021-03-14 18:01:30 +00005274 if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005275 return 0;
Mark Shannond48848c2021-03-14 18:01:30 +00005276 }
5277 int old_lineno = c->u->u_lineno;
5278 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005279 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005280 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005281 break;
5282 case Del:
5283 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
5284 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005285 }
5286 break;
5287 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005288 return compiler_subscript(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005289 case Starred_kind:
5290 switch (e->v.Starred.ctx) {
5291 case Store:
5292 /* In all legitimate cases, the Starred node was already replaced
5293 * by compiler_list/compiler_tuple. XXX: is that okay? */
5294 return compiler_error(c,
5295 "starred assignment target must be in a list or tuple");
5296 default:
5297 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005298 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005299 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005300 break;
5301 case Slice_kind:
5302 return compiler_slice(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005303 case Name_kind:
5304 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
5305 /* child nodes of List and Tuple will have expr_context set */
5306 case List_kind:
5307 return compiler_list(c, e);
5308 case Tuple_kind:
5309 return compiler_tuple(c, e);
5310 }
5311 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005312}
5313
5314static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005315compiler_visit_expr(struct compiler *c, expr_ty e)
5316{
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005317 int old_lineno = c->u->u_lineno;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005318 int old_end_lineno = c->u->u_end_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005319 int old_col_offset = c->u->u_col_offset;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005320 int old_end_col_offset = c->u->u_end_col_offset;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005321 SET_LOC(c, e);
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005322 int res = compiler_visit_expr1(c, e);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005323 c->u->u_lineno = old_lineno;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005324 c->u->u_end_lineno = old_end_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005325 c->u->u_col_offset = old_col_offset;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005326 c->u->u_end_col_offset = old_end_col_offset;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005327 return res;
5328}
5329
5330static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005331compiler_augassign(struct compiler *c, stmt_ty s)
5332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005333 assert(s->kind == AugAssign_kind);
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005334 expr_ty e = s->v.AugAssign.target;
5335
5336 int old_lineno = c->u->u_lineno;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005337 int old_end_lineno = c->u->u_end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005338 int old_col_offset = c->u->u_col_offset;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005339 int old_end_col_offset = c->u->u_end_col_offset;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005340 SET_LOC(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005342 switch (e->kind) {
5343 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005344 VISIT(c, expr, e->v.Attribute.value);
5345 ADDOP(c, DUP_TOP);
Mark Shannond48848c2021-03-14 18:01:30 +00005346 int old_lineno = c->u->u_lineno;
5347 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005348 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005349 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005350 break;
5351 case Subscript_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005352 VISIT(c, expr, e->v.Subscript.value);
5353 VISIT(c, expr, e->v.Subscript.slice);
5354 ADDOP(c, DUP_TOP_TWO);
5355 ADDOP(c, BINARY_SUBSCR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005356 break;
5357 case Name_kind:
5358 if (!compiler_nameop(c, e->v.Name.id, Load))
5359 return 0;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005360 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005361 default:
5362 PyErr_Format(PyExc_SystemError,
5363 "invalid node type (%d) for augmented assignment",
5364 e->kind);
5365 return 0;
5366 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005367
5368 c->u->u_lineno = old_lineno;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005369 c->u->u_end_lineno = old_end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005370 c->u->u_col_offset = old_col_offset;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005371 c->u->u_end_col_offset = old_end_col_offset;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005372
5373 VISIT(c, expr, s->v.AugAssign.value);
5374 ADDOP(c, inplace_binop(s->v.AugAssign.op));
5375
5376 SET_LOC(c, e);
5377
5378 switch (e->kind) {
5379 case Attribute_kind:
Mark Shannond48848c2021-03-14 18:01:30 +00005380 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005381 ADDOP(c, ROT_TWO);
5382 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5383 break;
5384 case Subscript_kind:
5385 ADDOP(c, ROT_THREE);
5386 ADDOP(c, STORE_SUBSCR);
5387 break;
5388 case Name_kind:
5389 return compiler_nameop(c, e->v.Name.id, Store);
5390 default:
5391 Py_UNREACHABLE();
5392 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005393 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005394}
5395
5396static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005397check_ann_expr(struct compiler *c, expr_ty e)
5398{
5399 VISIT(c, expr, e);
5400 ADDOP(c, POP_TOP);
5401 return 1;
5402}
5403
5404static int
5405check_annotation(struct compiler *c, stmt_ty s)
5406{
Batuhan Taskaya8cc3cfa2021-04-25 05:31:20 +03005407 /* Annotations of complex targets does not produce anything
5408 under annotations future */
5409 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
5410 return 1;
5411 }
5412
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005413 /* Annotations are only evaluated in a module or class. */
5414 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5415 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
5416 return check_ann_expr(c, s->v.AnnAssign.annotation);
5417 }
5418 return 1;
5419}
5420
5421static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005422check_ann_subscr(struct compiler *c, expr_ty e)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005423{
5424 /* We check that everything in a subscript is defined at runtime. */
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005425 switch (e->kind) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005426 case Slice_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005427 if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005428 return 0;
5429 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005430 if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) {
5431 return 0;
5432 }
5433 if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) {
5434 return 0;
5435 }
5436 return 1;
5437 case Tuple_kind: {
5438 /* extended slice */
Pablo Galindoa5634c42020-09-16 19:42:00 +01005439 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005440 Py_ssize_t i, n = asdl_seq_LEN(elts);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005441 for (i = 0; i < n; i++) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005442 if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005443 return 0;
5444 }
5445 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005446 return 1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005447 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005448 default:
5449 return check_ann_expr(c, e);
5450 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005451}
5452
5453static int
5454compiler_annassign(struct compiler *c, stmt_ty s)
5455{
5456 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07005457 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005458
5459 assert(s->kind == AnnAssign_kind);
5460
5461 /* We perform the actual assignment first. */
5462 if (s->v.AnnAssign.value) {
5463 VISIT(c, expr, s->v.AnnAssign.value);
5464 VISIT(c, expr, targ);
5465 }
5466 switch (targ->kind) {
5467 case Name_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005468 if (forbidden_name(c, targ->v.Name.id, Store))
5469 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005470 /* If we have a simple name in a module or class, store annotation. */
5471 if (s->v.AnnAssign.simple &&
5472 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5473 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Pablo Galindob0544ba2021-04-21 12:41:19 +01005474 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
5475 VISIT(c, annexpr, s->v.AnnAssign.annotation)
5476 }
5477 else {
5478 VISIT(c, expr, s->v.AnnAssign.annotation);
5479 }
Mark Shannon332cd5e2018-01-30 00:41:04 +00005480 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02005481 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005482 ADDOP_LOAD_CONST_NEW(c, mangled);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005483 ADDOP(c, STORE_SUBSCR);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005484 }
5485 break;
5486 case Attribute_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005487 if (forbidden_name(c, targ->v.Attribute.attr, Store))
5488 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005489 if (!s->v.AnnAssign.value &&
5490 !check_ann_expr(c, targ->v.Attribute.value)) {
5491 return 0;
5492 }
5493 break;
5494 case Subscript_kind:
5495 if (!s->v.AnnAssign.value &&
5496 (!check_ann_expr(c, targ->v.Subscript.value) ||
5497 !check_ann_subscr(c, targ->v.Subscript.slice))) {
5498 return 0;
5499 }
5500 break;
5501 default:
5502 PyErr_Format(PyExc_SystemError,
5503 "invalid node type (%d) for annotated assignment",
5504 targ->kind);
5505 return 0;
5506 }
5507 /* Annotation is evaluated last. */
5508 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
5509 return 0;
5510 }
5511 return 1;
5512}
5513
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005514/* Raises a SyntaxError and returns 0.
5515 If something goes wrong, a different exception may be raised.
5516*/
5517
5518static int
Brandt Bucher145bf262021-02-26 14:51:55 -08005519compiler_error(struct compiler *c, const char *format, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005520{
Brandt Bucher145bf262021-02-26 14:51:55 -08005521 va_list vargs;
5522#ifdef HAVE_STDARG_PROTOTYPES
5523 va_start(vargs, format);
5524#else
5525 va_start(vargs);
5526#endif
5527 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5528 va_end(vargs);
5529 if (msg == NULL) {
5530 return 0;
5531 }
5532 PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
5533 if (loc == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005534 Py_INCREF(Py_None);
5535 loc = Py_None;
5536 }
Pablo Galindoa77aac42021-04-23 14:27:05 +01005537 PyObject *args = Py_BuildValue("O(OiiOii)", msg, c->c_filename,
5538 c->u->u_lineno, c->u->u_col_offset + 1, loc,
5539 c->u->u_end_lineno, c->u->u_end_col_offset + 1);
Brandt Bucher145bf262021-02-26 14:51:55 -08005540 Py_DECREF(msg);
5541 if (args == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005542 goto exit;
Brandt Bucher145bf262021-02-26 14:51:55 -08005543 }
5544 PyErr_SetObject(PyExc_SyntaxError, args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005545 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005546 Py_DECREF(loc);
Brandt Bucher145bf262021-02-26 14:51:55 -08005547 Py_XDECREF(args);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005548 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005549}
5550
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005551/* Emits a SyntaxWarning and returns 1 on success.
5552 If a SyntaxWarning raised as error, replaces it with a SyntaxError
5553 and returns 0.
5554*/
5555static int
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005556compiler_warn(struct compiler *c, const char *format, ...)
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005557{
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005558 va_list vargs;
5559#ifdef HAVE_STDARG_PROTOTYPES
5560 va_start(vargs, format);
5561#else
5562 va_start(vargs);
5563#endif
5564 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5565 va_end(vargs);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005566 if (msg == NULL) {
5567 return 0;
5568 }
5569 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
5570 c->u->u_lineno, NULL, NULL) < 0)
5571 {
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005572 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005573 /* Replace the SyntaxWarning exception with a SyntaxError
5574 to get a more accurate error report */
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005575 PyErr_Clear();
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005576 assert(PyUnicode_AsUTF8(msg) != NULL);
5577 compiler_error(c, PyUnicode_AsUTF8(msg));
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005578 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005579 Py_DECREF(msg);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005580 return 0;
5581 }
5582 Py_DECREF(msg);
5583 return 1;
5584}
5585
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005586static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005587compiler_subscript(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005588{
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005589 expr_context_ty ctx = e->v.Subscript.ctx;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005590 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005591
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005592 if (ctx == Load) {
5593 if (!check_subscripter(c, e->v.Subscript.value)) {
5594 return 0;
5595 }
5596 if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
5597 return 0;
5598 }
5599 }
5600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005601 switch (ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005602 case Load: op = BINARY_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005603 case Store: op = STORE_SUBSCR; break;
5604 case Del: op = DELETE_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005605 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005606 assert(op);
5607 VISIT(c, expr, e->v.Subscript.value);
5608 VISIT(c, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005609 ADDOP(c, op);
5610 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005611}
5612
5613static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005614compiler_slice(struct compiler *c, expr_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005616 int n = 2;
5617 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005619 /* only handles the cases where BUILD_SLICE is emitted */
5620 if (s->v.Slice.lower) {
5621 VISIT(c, expr, s->v.Slice.lower);
5622 }
5623 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005624 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005625 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005627 if (s->v.Slice.upper) {
5628 VISIT(c, expr, s->v.Slice.upper);
5629 }
5630 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005631 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005632 }
5633
5634 if (s->v.Slice.step) {
5635 n++;
5636 VISIT(c, expr, s->v.Slice.step);
5637 }
5638 ADDOP_I(c, BUILD_SLICE, n);
5639 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005640}
5641
Brandt Bucher145bf262021-02-26 14:51:55 -08005642
5643// PEP 634: Structural Pattern Matching
5644
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005645// To keep things simple, all compiler_pattern_* and pattern_helper_* routines
5646// follow the convention of consuming TOS (the subject for the given pattern)
5647// and calling jump_to_fail_pop on failure (no match).
5648
5649// When calling into these routines, it's important that pc->on_top be kept
5650// updated to reflect the current number of items that we are using on the top
5651// of the stack: they will be popped on failure, and any name captures will be
5652// stored *underneath* them on success. This lets us defer all names stores
5653// until the *entire* pattern matches.
Brandt Bucher145bf262021-02-26 14:51:55 -08005654
Brandt Bucher145bf262021-02-26 14:51:55 -08005655#define WILDCARD_CHECK(N) \
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005656 ((N)->kind == MatchAs_kind && !(N)->v.MatchAs.name)
Brandt Bucher145bf262021-02-26 14:51:55 -08005657
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005658#define WILDCARD_STAR_CHECK(N) \
5659 ((N)->kind == MatchStar_kind && !(N)->v.MatchStar.name)
5660
5661// Limit permitted subexpressions, even if the parser & AST validator let them through
5662#define MATCH_VALUE_EXPR(N) \
5663 ((N)->kind == Constant_kind || (N)->kind == Attribute_kind)
Brandt Bucher145bf262021-02-26 14:51:55 -08005664
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005665// Allocate or resize pc->fail_pop to allow for n items to be popped on failure.
5666static int
5667ensure_fail_pop(struct compiler *c, pattern_context *pc, Py_ssize_t n)
5668{
5669 Py_ssize_t size = n + 1;
5670 if (size <= pc->fail_pop_size) {
5671 return 1;
5672 }
5673 Py_ssize_t needed = sizeof(basicblock*) * size;
5674 basicblock **resized = PyObject_Realloc(pc->fail_pop, needed);
5675 if (resized == NULL) {
5676 PyErr_NoMemory();
5677 return 0;
5678 }
5679 pc->fail_pop = resized;
5680 while (pc->fail_pop_size < size) {
5681 basicblock *new_block;
5682 RETURN_IF_FALSE(new_block = compiler_new_block(c));
5683 pc->fail_pop[pc->fail_pop_size++] = new_block;
5684 }
5685 return 1;
5686}
5687
5688// Use op to jump to the correct fail_pop block.
5689static int
5690jump_to_fail_pop(struct compiler *c, pattern_context *pc, int op)
5691{
5692 // Pop any items on the top of the stack, plus any objects we were going to
5693 // capture on success:
5694 Py_ssize_t pops = pc->on_top + PyList_GET_SIZE(pc->stores);
5695 RETURN_IF_FALSE(ensure_fail_pop(c, pc, pops));
5696 ADDOP_JUMP(c, op, pc->fail_pop[pops]);
5697 NEXT_BLOCK(c);
5698 return 1;
5699}
5700
5701// Build all of the fail_pop blocks and reset fail_pop.
5702static int
5703emit_and_reset_fail_pop(struct compiler *c, pattern_context *pc)
5704{
5705 if (!pc->fail_pop_size) {
5706 assert(pc->fail_pop == NULL);
5707 NEXT_BLOCK(c);
5708 return 1;
5709 }
5710 while (--pc->fail_pop_size) {
5711 compiler_use_next_block(c, pc->fail_pop[pc->fail_pop_size]);
5712 if (!compiler_addop(c, POP_TOP)) {
5713 pc->fail_pop_size = 0;
5714 PyObject_Free(pc->fail_pop);
5715 pc->fail_pop = NULL;
5716 return 0;
5717 }
5718 }
5719 compiler_use_next_block(c, pc->fail_pop[0]);
5720 PyObject_Free(pc->fail_pop);
5721 pc->fail_pop = NULL;
5722 return 1;
5723}
5724
5725static int
5726compiler_error_duplicate_store(struct compiler *c, identifier n)
5727{
5728 return compiler_error(c, "multiple assignments to name %R in pattern", n);
5729}
5730
Brandt Bucher145bf262021-02-26 14:51:55 -08005731static int
5732pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc)
5733{
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005734 if (n == NULL) {
5735 ADDOP(c, POP_TOP);
5736 return 1;
5737 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005738 if (forbidden_name(c, n, Store)) {
5739 return 0;
5740 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005741 // Can't assign to the same name twice:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005742 int duplicate = PySequence_Contains(pc->stores, n);
5743 if (duplicate < 0) {
5744 return 0;
Brandt Bucher145bf262021-02-26 14:51:55 -08005745 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005746 if (duplicate) {
5747 return compiler_error_duplicate_store(c, n);
Brandt Bucher145bf262021-02-26 14:51:55 -08005748 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005749 // Rotate this object underneath any items we need to preserve:
5750 ADDOP_I(c, ROT_N, pc->on_top + PyList_GET_SIZE(pc->stores) + 1);
5751 return !PyList_Append(pc->stores, n);
Brandt Bucher145bf262021-02-26 14:51:55 -08005752}
5753
5754
5755static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005756pattern_unpack_helper(struct compiler *c, asdl_pattern_seq *elts)
5757{
5758 Py_ssize_t n = asdl_seq_LEN(elts);
5759 int seen_star = 0;
5760 for (Py_ssize_t i = 0; i < n; i++) {
5761 pattern_ty elt = asdl_seq_GET(elts, i);
5762 if (elt->kind == MatchStar_kind && !seen_star) {
5763 if ((i >= (1 << 8)) ||
5764 (n-i-1 >= (INT_MAX >> 8)))
5765 return compiler_error(c,
5766 "too many expressions in "
5767 "star-unpacking sequence pattern");
5768 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
5769 seen_star = 1;
5770 }
5771 else if (elt->kind == MatchStar_kind) {
5772 return compiler_error(c,
5773 "multiple starred expressions in sequence pattern");
5774 }
5775 }
5776 if (!seen_star) {
5777 ADDOP_I(c, UNPACK_SEQUENCE, n);
5778 }
5779 return 1;
5780}
5781
5782static int
5783pattern_helper_sequence_unpack(struct compiler *c, asdl_pattern_seq *patterns,
Brandt Bucher145bf262021-02-26 14:51:55 -08005784 Py_ssize_t star, pattern_context *pc)
5785{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005786 RETURN_IF_FALSE(pattern_unpack_helper(c, patterns));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005787 Py_ssize_t size = asdl_seq_LEN(patterns);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005788 // We've now got a bunch of new subjects on the stack. They need to remain
5789 // there after each subpattern match:
5790 pc->on_top += size;
Brandt Bucher145bf262021-02-26 14:51:55 -08005791 for (Py_ssize_t i = 0; i < size; i++) {
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005792 // One less item to keep track of each time we loop through:
5793 pc->on_top--;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005794 pattern_ty pattern = asdl_seq_GET(patterns, i);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005795 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08005796 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005797 return 1;
Brandt Bucher145bf262021-02-26 14:51:55 -08005798}
5799
5800// Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of
5801// UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a
5802// starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc.
5803static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005804pattern_helper_sequence_subscr(struct compiler *c, asdl_pattern_seq *patterns,
Brandt Bucher145bf262021-02-26 14:51:55 -08005805 Py_ssize_t star, pattern_context *pc)
5806{
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005807 // We need to keep the subject around for extracting elements:
5808 pc->on_top++;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005809 Py_ssize_t size = asdl_seq_LEN(patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08005810 for (Py_ssize_t i = 0; i < size; i++) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005811 pattern_ty pattern = asdl_seq_GET(patterns, i);
5812 if (WILDCARD_CHECK(pattern)) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005813 continue;
5814 }
5815 if (i == star) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005816 assert(WILDCARD_STAR_CHECK(pattern));
Brandt Bucher145bf262021-02-26 14:51:55 -08005817 continue;
5818 }
5819 ADDOP(c, DUP_TOP);
5820 if (i < star) {
5821 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5822 }
5823 else {
5824 // The subject may not support negative indexing! Compute a
5825 // nonnegative index:
5826 ADDOP(c, GET_LEN);
5827 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i));
5828 ADDOP(c, BINARY_SUBTRACT);
5829 }
5830 ADDOP(c, BINARY_SUBSCR);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005831 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08005832 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005833 // Pop the subject, we're done with it:
5834 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08005835 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08005836 return 1;
5837}
5838
Brandt Bucher145bf262021-02-26 14:51:55 -08005839// Like compiler_pattern, but turn off checks for irrefutability.
5840static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005841compiler_pattern_subpattern(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005842{
5843 int allow_irrefutable = pc->allow_irrefutable;
5844 pc->allow_irrefutable = 1;
5845 RETURN_IF_FALSE(compiler_pattern(c, p, pc));
5846 pc->allow_irrefutable = allow_irrefutable;
5847 return 1;
5848}
5849
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005850static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005851compiler_pattern_as(struct compiler *c, pattern_ty p, pattern_context *pc)
5852{
5853 assert(p->kind == MatchAs_kind);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005854 if (p->v.MatchAs.pattern == NULL) {
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005855 // An irrefutable match:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005856 if (!pc->allow_irrefutable) {
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005857 if (p->v.MatchAs.name) {
5858 const char *e = "name capture %R makes remaining patterns unreachable";
5859 return compiler_error(c, e, p->v.MatchAs.name);
5860 }
5861 const char *e = "wildcard makes remaining patterns unreachable";
5862 return compiler_error(c, e);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005863 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005864 return pattern_helper_store_name(c, p->v.MatchAs.name, pc);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005865 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005866 // Need to make a copy for (possibly) storing later:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005867 pc->on_top++;
Brandt Bucher145bf262021-02-26 14:51:55 -08005868 ADDOP(c, DUP_TOP);
5869 RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc));
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005870 // Success! Store it:
5871 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08005872 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08005873 return 1;
5874}
5875
Brandt Bucher145bf262021-02-26 14:51:55 -08005876static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005877compiler_pattern_star(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005878{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005879 assert(p->kind == MatchStar_kind);
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005880 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchStar.name, pc));
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005881 return 1;
Brandt Bucher145bf262021-02-26 14:51:55 -08005882}
5883
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005884static int
5885validate_kwd_attrs(struct compiler *c, asdl_identifier_seq *attrs, asdl_pattern_seq* patterns)
5886{
5887 // Any errors will point to the pattern rather than the arg name as the
5888 // parser is only supplying identifiers rather than Name or keyword nodes
5889 Py_ssize_t nattrs = asdl_seq_LEN(attrs);
5890 for (Py_ssize_t i = 0; i < nattrs; i++) {
5891 identifier attr = ((identifier)asdl_seq_GET(attrs, i));
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005892 SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, i)));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005893 if (forbidden_name(c, attr, Store)) {
5894 return -1;
5895 }
5896 for (Py_ssize_t j = i + 1; j < nattrs; j++) {
5897 identifier other = ((identifier)asdl_seq_GET(attrs, j));
5898 if (!PyUnicode_Compare(attr, other)) {
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005899 SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, j)));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005900 compiler_error(c, "attribute name repeated in class pattern: %U", attr);
5901 return -1;
5902 }
5903 }
5904 }
5905 return 0;
5906}
Brandt Bucher145bf262021-02-26 14:51:55 -08005907
5908static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005909compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005910{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005911 assert(p->kind == MatchClass_kind);
5912 asdl_pattern_seq *patterns = p->v.MatchClass.patterns;
5913 asdl_identifier_seq *kwd_attrs = p->v.MatchClass.kwd_attrs;
5914 asdl_pattern_seq *kwd_patterns = p->v.MatchClass.kwd_patterns;
5915 Py_ssize_t nargs = asdl_seq_LEN(patterns);
5916 Py_ssize_t nattrs = asdl_seq_LEN(kwd_attrs);
5917 Py_ssize_t nkwd_patterns = asdl_seq_LEN(kwd_patterns);
5918 if (nattrs != nkwd_patterns) {
5919 // AST validator shouldn't let this happen, but if it does,
5920 // just fail, don't crash out of the interpreter
5921 const char * e = "kwd_attrs (%d) / kwd_patterns (%d) length mismatch in class pattern";
5922 return compiler_error(c, e, nattrs, nkwd_patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08005923 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005924 if (INT_MAX < nargs || INT_MAX < nargs + nattrs - 1) {
5925 const char *e = "too many sub-patterns in class pattern %R";
5926 return compiler_error(c, e, p->v.MatchClass.cls);
5927 }
5928 if (nattrs) {
5929 RETURN_IF_FALSE(!validate_kwd_attrs(c, kwd_attrs, kwd_patterns));
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005930 SET_LOC(c, p);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005931 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005932 VISIT(c, expr, p->v.MatchClass.cls);
5933 PyObject *attr_names;
5934 RETURN_IF_FALSE(attr_names = PyTuple_New(nattrs));
Brandt Bucher145bf262021-02-26 14:51:55 -08005935 Py_ssize_t i;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005936 for (i = 0; i < nattrs; i++) {
5937 PyObject *name = asdl_seq_GET(kwd_attrs, i);
Brandt Bucher145bf262021-02-26 14:51:55 -08005938 Py_INCREF(name);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005939 PyTuple_SET_ITEM(attr_names, i, name);
Brandt Bucher145bf262021-02-26 14:51:55 -08005940 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005941 ADDOP_LOAD_CONST_NEW(c, attr_names);
Brandt Bucher145bf262021-02-26 14:51:55 -08005942 ADDOP_I(c, MATCH_CLASS, nargs);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005943 // TOS is now a tuple of (nargs + nattrs) attributes. Preserve it:
5944 pc->on_top++;
5945 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005946 for (i = 0; i < nargs + nattrs; i++) {
5947 pattern_ty pattern;
Brandt Bucher145bf262021-02-26 14:51:55 -08005948 if (i < nargs) {
5949 // Positional:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005950 pattern = asdl_seq_GET(patterns, i);
Brandt Bucher145bf262021-02-26 14:51:55 -08005951 }
5952 else {
5953 // Keyword:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005954 pattern = asdl_seq_GET(kwd_patterns, i - nargs);
Brandt Bucher145bf262021-02-26 14:51:55 -08005955 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005956 if (WILDCARD_CHECK(pattern)) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005957 continue;
5958 }
5959 // Get the i-th attribute, and match it against the i-th pattern:
5960 ADDOP(c, DUP_TOP);
5961 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5962 ADDOP(c, BINARY_SUBSCR);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005963 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08005964 }
5965 // Success! Pop the tuple of attributes:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005966 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08005967 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08005968 return 1;
5969}
5970
Brandt Bucher145bf262021-02-26 14:51:55 -08005971static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005972compiler_pattern_mapping(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005973{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005974 assert(p->kind == MatchMapping_kind);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005975 asdl_expr_seq *keys = p->v.MatchMapping.keys;
5976 asdl_pattern_seq *patterns = p->v.MatchMapping.patterns;
5977 Py_ssize_t size = asdl_seq_LEN(keys);
5978 Py_ssize_t npatterns = asdl_seq_LEN(patterns);
5979 if (size != npatterns) {
5980 // AST validator shouldn't let this happen, but if it does,
5981 // just fail, don't crash out of the interpreter
5982 const char * e = "keys (%d) / patterns (%d) length mismatch in mapping pattern";
5983 return compiler_error(c, e, size, npatterns);
5984 }
5985 // We have a double-star target if "rest" is set
5986 PyObject *star_target = p->v.MatchMapping.rest;
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005987 // We need to keep the subject on top during the mapping and length checks:
5988 pc->on_top++;
Brandt Bucher145bf262021-02-26 14:51:55 -08005989 ADDOP(c, MATCH_MAPPING);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005990 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005991 if (!size && !star_target) {
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005992 // If the pattern is just "{}", we're done! Pop the subject:
5993 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08005994 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08005995 return 1;
5996 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005997 if (size) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005998 // If the pattern has any keys in it, perform a length check:
5999 ADDOP(c, GET_LEN);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006000 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
Brandt Bucher145bf262021-02-26 14:51:55 -08006001 ADDOP_COMPARE(c, GtE);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006002 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006003 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006004 if (INT_MAX < size - 1) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006005 return compiler_error(c, "too many sub-patterns in mapping pattern");
6006 }
6007 // Collect all of the keys into a tuple for MATCH_KEYS and
6008 // COPY_DICT_WITHOUT_KEYS. They can either be dotted names or literals:
Miss Islington (bot)016af142021-07-14 18:00:35 -07006009
6010 // Maintaining a set of Constant_kind kind keys allows us to raise a
6011 // SyntaxError in the case of duplicates.
6012 PyObject *seen = PySet_New(NULL);
6013 if (seen == NULL) {
6014 return 0;
6015 }
6016
6017 // NOTE: goto error on failure in the loop below to avoid leaking `seen`
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006018 for (Py_ssize_t i = 0; i < size; i++) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006019 expr_ty key = asdl_seq_GET(keys, i);
6020 if (key == NULL) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006021 const char *e = "can't use NULL keys in MatchMapping "
6022 "(set 'rest' parameter instead)";
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07006023 SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, i)));
Miss Islington (bot)016af142021-07-14 18:00:35 -07006024 compiler_error(c, e);
6025 goto error;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006026 }
Miss Islington (bot)016af142021-07-14 18:00:35 -07006027
6028 if (key->kind == Constant_kind) {
6029 int in_seen = PySet_Contains(seen, key->v.Constant.value);
6030 if (in_seen < 0) {
6031 goto error;
6032 }
6033 if (in_seen) {
6034 const char *e = "mapping pattern checks duplicate key (%R)";
6035 compiler_error(c, e, key->v.Constant.value);
6036 goto error;
6037 }
6038 if (PySet_Add(seen, key->v.Constant.value)) {
6039 goto error;
6040 }
6041 }
6042
6043 else if (key->kind != Attribute_kind) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006044 const char *e = "mapping pattern keys may only match literals and attribute lookups";
Miss Islington (bot)016af142021-07-14 18:00:35 -07006045 compiler_error(c, e);
6046 goto error;
Brandt Bucher145bf262021-02-26 14:51:55 -08006047 }
Miss Islington (bot)016af142021-07-14 18:00:35 -07006048 if (!compiler_visit_expr(c, key)) {
6049 goto error;
6050 }
Brandt Bucher145bf262021-02-26 14:51:55 -08006051 }
Miss Islington (bot)016af142021-07-14 18:00:35 -07006052
6053 // all keys have been checked; there are no duplicates
6054 Py_DECREF(seen);
6055
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006056 ADDOP_I(c, BUILD_TUPLE, size);
Brandt Bucher145bf262021-02-26 14:51:55 -08006057 ADDOP(c, MATCH_KEYS);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006058 // There's now a tuple of keys and a tuple of values on top of the subject:
6059 pc->on_top += 2;
6060 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
6061 // So far so good. Use that tuple of values on the stack to match
Brandt Bucher145bf262021-02-26 14:51:55 -08006062 // sub-patterns against:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006063 for (Py_ssize_t i = 0; i < size; i++) {
6064 pattern_ty pattern = asdl_seq_GET(patterns, i);
6065 if (WILDCARD_CHECK(pattern)) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006066 continue;
6067 }
6068 ADDOP(c, DUP_TOP);
6069 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
6070 ADDOP(c, BINARY_SUBSCR);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006071 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006072 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006073 // If we get this far, it's a match! We're done with the tuple of values,
6074 // and whatever happens next should consume the tuple of keys underneath it:
6075 pc->on_top -= 2;
Brandt Bucher145bf262021-02-26 14:51:55 -08006076 ADDOP(c, POP_TOP);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006077 if (star_target) {
6078 // If we have a starred name, bind a dict of remaining items to it:
Brandt Bucher145bf262021-02-26 14:51:55 -08006079 ADDOP(c, COPY_DICT_WITHOUT_KEYS);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006080 RETURN_IF_FALSE(pattern_helper_store_name(c, star_target, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006081 }
6082 else {
6083 // Otherwise, we don't care about this tuple of keys anymore:
6084 ADDOP(c, POP_TOP);
6085 }
6086 // Pop the subject:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006087 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08006088 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08006089 return 1;
Miss Islington (bot)016af142021-07-14 18:00:35 -07006090
6091error:
6092 Py_DECREF(seen);
6093 return 0;
Brandt Bucher145bf262021-02-26 14:51:55 -08006094}
6095
Brandt Bucher145bf262021-02-26 14:51:55 -08006096static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006097compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006098{
6099 assert(p->kind == MatchOr_kind);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006100 basicblock *end;
Brandt Bucher145bf262021-02-26 14:51:55 -08006101 RETURN_IF_FALSE(end = compiler_new_block(c));
Brandt Bucher145bf262021-02-26 14:51:55 -08006102 Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns);
6103 assert(size > 1);
6104 // We're going to be messing with pc. Keep the original info handy:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006105 pattern_context old_pc = *pc;
6106 Py_INCREF(pc->stores);
6107 // control is the list of names bound by the first alternative. It is used
6108 // for checking different name bindings in alternatives, and for correcting
6109 // the order in which extracted elements are placed on the stack.
6110 PyObject *control = NULL;
6111 // NOTE: We can't use returning macros anymore! goto error on error.
Brandt Bucher145bf262021-02-26 14:51:55 -08006112 for (Py_ssize_t i = 0; i < size; i++) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006113 pattern_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i);
Brandt Bucher145bf262021-02-26 14:51:55 -08006114 SET_LOC(c, alt);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006115 PyObject *pc_stores = PyList_New(0);
6116 if (pc_stores == NULL) {
6117 goto error;
Brandt Bucher145bf262021-02-26 14:51:55 -08006118 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006119 Py_SETREF(pc->stores, pc_stores);
6120 // An irrefutable sub-pattern must be last, if it is allowed at all:
6121 pc->allow_irrefutable = (i == size - 1) && old_pc.allow_irrefutable;
6122 pc->fail_pop = NULL;
6123 pc->fail_pop_size = 0;
6124 pc->on_top = 0;
6125 if (!compiler_addop(c, DUP_TOP) || !compiler_pattern(c, alt, pc)) {
6126 goto error;
6127 }
6128 // Success!
6129 Py_ssize_t nstores = PyList_GET_SIZE(pc->stores);
Brandt Bucher145bf262021-02-26 14:51:55 -08006130 if (!i) {
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006131 // This is the first alternative, so save its stores as a "control"
6132 // for the others (they can't bind a different set of names, and
6133 // might need to be reordered):
6134 assert(control == NULL);
Brandt Bucher145bf262021-02-26 14:51:55 -08006135 control = pc->stores;
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006136 Py_INCREF(control);
Brandt Bucher145bf262021-02-26 14:51:55 -08006137 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006138 else if (nstores != PyList_GET_SIZE(control)) {
6139 goto diff;
Brandt Bucher145bf262021-02-26 14:51:55 -08006140 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006141 else if (nstores) {
6142 // There were captures. Check to see if we differ from control:
6143 Py_ssize_t icontrol = nstores;
6144 while (icontrol--) {
6145 PyObject *name = PyList_GET_ITEM(control, icontrol);
6146 Py_ssize_t istores = PySequence_Index(pc->stores, name);
6147 if (istores < 0) {
6148 PyErr_Clear();
6149 goto diff;
6150 }
6151 if (icontrol != istores) {
6152 // Reorder the names on the stack to match the order of the
6153 // names in control. There's probably a better way of doing
6154 // this; the current solution is potentially very
6155 // inefficient when each alternative subpattern binds lots
6156 // of names in different orders. It's fine for reasonable
6157 // cases, though.
6158 assert(istores < icontrol);
6159 Py_ssize_t rotations = istores + 1;
6160 // Perfom the same rotation on pc->stores:
6161 PyObject *rotated = PyList_GetSlice(pc->stores, 0,
6162 rotations);
6163 if (rotated == NULL ||
6164 PyList_SetSlice(pc->stores, 0, rotations, NULL) ||
6165 PyList_SetSlice(pc->stores, icontrol - istores,
6166 icontrol - istores, rotated))
6167 {
6168 Py_XDECREF(rotated);
6169 goto error;
6170 }
6171 Py_DECREF(rotated);
6172 // That just did:
6173 // rotated = pc_stores[:rotations]
6174 // del pc_stores[:rotations]
6175 // pc_stores[icontrol-istores:icontrol-istores] = rotated
6176 // Do the same thing to the stack, using several ROT_Ns:
6177 while (rotations--) {
6178 if (!compiler_addop_i(c, ROT_N, icontrol + 1)) {
6179 goto error;
6180 }
6181 }
6182 }
6183 }
6184 }
6185 assert(control);
6186 if (!compiler_addop_j(c, JUMP_FORWARD, end) ||
6187 !compiler_next_block(c) ||
6188 !emit_and_reset_fail_pop(c, pc))
6189 {
6190 goto error;
6191 }
Brandt Bucher145bf262021-02-26 14:51:55 -08006192 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006193 Py_DECREF(pc->stores);
6194 *pc = old_pc;
6195 Py_INCREF(pc->stores);
6196 // Need to NULL this for the PyObject_Free call in the error block.
6197 old_pc.fail_pop = NULL;
6198 // No match. Pop the remaining copy of the subject and fail:
6199 if (!compiler_addop(c, POP_TOP) || !jump_to_fail_pop(c, pc, JUMP_FORWARD)) {
6200 goto error;
6201 }
Brandt Bucher145bf262021-02-26 14:51:55 -08006202 compiler_use_next_block(c, end);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006203 Py_ssize_t nstores = PyList_GET_SIZE(control);
6204 // There's a bunch of stuff on the stack between any where the new stores
6205 // are and where they need to be:
6206 // - The other stores.
6207 // - A copy of the subject.
6208 // - Anything else that may be on top of the stack.
6209 // - Any previous stores we've already stashed away on the stack.
Pablo Galindo39494282021-05-03 16:20:46 +01006210 Py_ssize_t nrots = nstores + 1 + pc->on_top + PyList_GET_SIZE(pc->stores);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006211 for (Py_ssize_t i = 0; i < nstores; i++) {
6212 // Rotate this capture to its proper place on the stack:
6213 if (!compiler_addop_i(c, ROT_N, nrots)) {
6214 goto error;
6215 }
6216 // Update the list of previous stores with this new name, checking for
6217 // duplicates:
6218 PyObject *name = PyList_GET_ITEM(control, i);
6219 int dupe = PySequence_Contains(pc->stores, name);
6220 if (dupe < 0) {
6221 goto error;
6222 }
6223 if (dupe) {
6224 compiler_error_duplicate_store(c, name);
6225 goto error;
6226 }
6227 if (PyList_Append(pc->stores, name)) {
6228 goto error;
6229 }
6230 }
6231 Py_DECREF(old_pc.stores);
6232 Py_DECREF(control);
6233 // NOTE: Returning macros are safe again.
6234 // Pop the copy of the subject:
6235 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08006236 return 1;
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006237diff:
6238 compiler_error(c, "alternative patterns bind different names");
6239error:
6240 PyObject_Free(old_pc.fail_pop);
6241 Py_DECREF(old_pc.stores);
Brandt Bucher145bf262021-02-26 14:51:55 -08006242 Py_XDECREF(control);
6243 return 0;
6244}
6245
6246
6247static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006248compiler_pattern_sequence(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006249{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006250 assert(p->kind == MatchSequence_kind);
6251 asdl_pattern_seq *patterns = p->v.MatchSequence.patterns;
6252 Py_ssize_t size = asdl_seq_LEN(patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08006253 Py_ssize_t star = -1;
6254 int only_wildcard = 1;
6255 int star_wildcard = 0;
6256 // Find a starred name, if it exists. There may be at most one:
6257 for (Py_ssize_t i = 0; i < size; i++) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006258 pattern_ty pattern = asdl_seq_GET(patterns, i);
6259 if (pattern->kind == MatchStar_kind) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006260 if (star >= 0) {
6261 const char *e = "multiple starred names in sequence pattern";
6262 return compiler_error(c, e);
6263 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006264 star_wildcard = WILDCARD_STAR_CHECK(pattern);
6265 only_wildcard &= star_wildcard;
Brandt Bucher145bf262021-02-26 14:51:55 -08006266 star = i;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006267 continue;
Brandt Bucher145bf262021-02-26 14:51:55 -08006268 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006269 only_wildcard &= WILDCARD_CHECK(pattern);
Brandt Bucher145bf262021-02-26 14:51:55 -08006270 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006271 // We need to keep the subject on top during the sequence and length checks:
6272 pc->on_top++;
Brandt Bucher145bf262021-02-26 14:51:55 -08006273 ADDOP(c, MATCH_SEQUENCE);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006274 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006275 if (star < 0) {
6276 // No star: len(subject) == size
6277 ADDOP(c, GET_LEN);
6278 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
6279 ADDOP_COMPARE(c, Eq);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006280 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006281 }
6282 else if (size > 1) {
6283 // Star: len(subject) >= size - 1
6284 ADDOP(c, GET_LEN);
6285 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1));
6286 ADDOP_COMPARE(c, GtE);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006287 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006288 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006289 // Whatever comes next should consume the subject:
6290 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08006291 if (only_wildcard) {
6292 // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc.
6293 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08006294 }
6295 else if (star_wildcard) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006296 RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, patterns, star, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006297 }
6298 else {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006299 RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, patterns, star, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006300 }
Brandt Bucher145bf262021-02-26 14:51:55 -08006301 return 1;
6302}
6303
Brandt Bucher145bf262021-02-26 14:51:55 -08006304static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006305compiler_pattern_value(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006306{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006307 assert(p->kind == MatchValue_kind);
6308 expr_ty value = p->v.MatchValue.value;
6309 if (!MATCH_VALUE_EXPR(value)) {
6310 const char *e = "patterns may only match literals and attribute lookups";
6311 return compiler_error(c, e);
6312 }
6313 VISIT(c, expr, value);
Brandt Bucher145bf262021-02-26 14:51:55 -08006314 ADDOP_COMPARE(c, Eq);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006315 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006316 return 1;
6317}
6318
Brandt Bucher145bf262021-02-26 14:51:55 -08006319static int
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07006320compiler_pattern_singleton(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006321{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006322 assert(p->kind == MatchSingleton_kind);
6323 ADDOP_LOAD_CONST(c, p->v.MatchSingleton.value);
6324 ADDOP_COMPARE(c, Is);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006325 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006326 return 1;
6327}
6328
Brandt Bucher145bf262021-02-26 14:51:55 -08006329static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006330compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006331{
6332 SET_LOC(c, p);
6333 switch (p->kind) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006334 case MatchValue_kind:
Brandt Bucher145bf262021-02-26 14:51:55 -08006335 return compiler_pattern_value(c, p, pc);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006336 case MatchSingleton_kind:
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07006337 return compiler_pattern_singleton(c, p, pc);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006338 case MatchSequence_kind:
Brandt Bucher145bf262021-02-26 14:51:55 -08006339 return compiler_pattern_sequence(c, p, pc);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006340 case MatchMapping_kind:
6341 return compiler_pattern_mapping(c, p, pc);
6342 case MatchClass_kind:
6343 return compiler_pattern_class(c, p, pc);
6344 case MatchStar_kind:
6345 return compiler_pattern_star(c, p, pc);
Brandt Bucher145bf262021-02-26 14:51:55 -08006346 case MatchAs_kind:
6347 return compiler_pattern_as(c, p, pc);
6348 case MatchOr_kind:
6349 return compiler_pattern_or(c, p, pc);
Brandt Bucher145bf262021-02-26 14:51:55 -08006350 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006351 // AST validator shouldn't let this happen, but if it does,
6352 // just fail, don't crash out of the interpreter
6353 const char *e = "invalid match pattern node in AST (kind=%d)";
6354 return compiler_error(c, e, p->kind);
Brandt Bucher145bf262021-02-26 14:51:55 -08006355}
6356
Brandt Bucher145bf262021-02-26 14:51:55 -08006357static int
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006358compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006359{
6360 VISIT(c, expr, s->v.Match.subject);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006361 basicblock *end;
Brandt Bucher145bf262021-02-26 14:51:55 -08006362 RETURN_IF_FALSE(end = compiler_new_block(c));
6363 Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006364 assert(cases > 0);
Brandt Bucher145bf262021-02-26 14:51:55 -08006365 match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6366 int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases;
6367 for (Py_ssize_t i = 0; i < cases - has_default; i++) {
6368 m = asdl_seq_GET(s->v.Match.cases, i);
6369 SET_LOC(c, m->pattern);
Brandt Bucher145bf262021-02-26 14:51:55 -08006370 // Only copy the subject if we're *not* on the last case:
6371 if (i != cases - has_default - 1) {
6372 ADDOP(c, DUP_TOP);
6373 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006374 RETURN_IF_FALSE(pc->stores = PyList_New(0));
6375 // Irrefutable cases must be either guarded, last, or both:
6376 pc->allow_irrefutable = m->guard != NULL || i == cases - 1;
6377 pc->fail_pop = NULL;
6378 pc->fail_pop_size = 0;
6379 pc->on_top = 0;
6380 // NOTE: Can't use returning macros here (they'll leak pc->stores)!
6381 if (!compiler_pattern(c, m->pattern, pc)) {
6382 Py_DECREF(pc->stores);
6383 return 0;
6384 }
6385 assert(!pc->on_top);
6386 // It's a match! Store all of the captured names (they're on the stack).
6387 Py_ssize_t nstores = PyList_GET_SIZE(pc->stores);
6388 for (Py_ssize_t n = 0; n < nstores; n++) {
6389 PyObject *name = PyList_GET_ITEM(pc->stores, n);
6390 if (!compiler_nameop(c, name, Store)) {
6391 Py_DECREF(pc->stores);
6392 return 0;
6393 }
6394 }
6395 Py_DECREF(pc->stores);
6396 // NOTE: Returning macros are safe again.
Brandt Bucher145bf262021-02-26 14:51:55 -08006397 if (m->guard) {
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006398 RETURN_IF_FALSE(ensure_fail_pop(c, pc, 0));
6399 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, pc->fail_pop[0], 0));
Brandt Bucher145bf262021-02-26 14:51:55 -08006400 }
6401 // Success! Pop the subject off, we're done with it:
6402 if (i != cases - has_default - 1) {
6403 ADDOP(c, POP_TOP);
6404 }
6405 VISIT_SEQ(c, stmt, m->body);
6406 ADDOP_JUMP(c, JUMP_FORWARD, end);
Miss Islington (bot)01601aa2021-07-25 17:04:06 -07006407 // If the pattern fails to match, we want the line number of the
6408 // cleanup to be associated with the failed pattern, not the last line
6409 // of the body
6410 SET_LOC(c, m->pattern);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006411 RETURN_IF_FALSE(emit_and_reset_fail_pop(c, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006412 }
6413 if (has_default) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006414 // A trailing "case _" is common, and lets us save a bit of redundant
6415 // pushing and popping in the loop above:
6416 m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6417 SET_LOC(c, m->pattern);
Miss Islington (bot)01601aa2021-07-25 17:04:06 -07006418 if (cases == 1) {
6419 // No matches. Done with the subject:
6420 ADDOP(c, POP_TOP);
6421 }
6422 else {
6423 // Show line coverage for default case (it doesn't create bytecode)
6424 ADDOP(c, NOP);
6425 }
Brandt Bucher145bf262021-02-26 14:51:55 -08006426 if (m->guard) {
6427 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0));
6428 }
6429 VISIT_SEQ(c, stmt, m->body);
6430 }
6431 compiler_use_next_block(c, end);
6432 return 1;
6433}
6434
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006435static int
6436compiler_match(struct compiler *c, stmt_ty s)
6437{
6438 pattern_context pc;
6439 pc.fail_pop = NULL;
6440 int result = compiler_match_inner(c, s, &pc);
6441 PyObject_Free(pc.fail_pop);
6442 return result;
6443}
6444
Brandt Bucher145bf262021-02-26 14:51:55 -08006445#undef WILDCARD_CHECK
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006446#undef WILDCARD_STAR_CHECK
Brandt Bucher145bf262021-02-26 14:51:55 -08006447
Thomas Wouters89f507f2006-12-13 04:49:30 +00006448/* End of the compiler section, beginning of the assembler section */
6449
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006450/* do depth-first search of basic block graph, starting with block.
T. Wouters99b54d62019-09-12 07:05:33 -07006451 post records the block indices in post-order.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006452
6453 XXX must handle implicit jumps from one block to next
6454*/
6455
Thomas Wouters89f507f2006-12-13 04:49:30 +00006456struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006457 PyObject *a_bytecode; /* string containing bytecode */
6458 int a_offset; /* offset into bytecode */
6459 int a_nblocks; /* number of reachable blocks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006460 PyObject *a_lnotab; /* string containing lnotab */
6461 int a_lnotab_off; /* offset into lnotab */
Mark Shannon877df852020-11-12 09:43:29 +00006462 int a_prevlineno; /* lineno of last emitted line in line table */
6463 int a_lineno; /* lineno of last emitted instruction */
6464 int a_lineno_start; /* bytecode start offset of current lineno */
Mark Shannoncc75ab72020-11-12 19:49:33 +00006465 basicblock *a_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006466};
6467
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006468Py_LOCAL_INLINE(void)
6469stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006470{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02006471 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
Mark Shannonfee55262019-11-21 09:11:43 +00006472 if (b->b_startdepth < depth && b->b_startdepth < 100) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006473 assert(b->b_startdepth < 0);
6474 b->b_startdepth = depth;
6475 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02006476 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006477}
6478
6479/* Find the flow path that needs the largest stack. We assume that
6480 * cycles in the flow graph have no net effect on the stack depth.
6481 */
6482static int
6483stackdepth(struct compiler *c)
6484{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006485 basicblock *b, *entryblock = NULL;
6486 basicblock **stack, **sp;
6487 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006488 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006489 b->b_startdepth = INT_MIN;
6490 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006491 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006492 }
Mark Shannon67969f52021-04-07 10:52:07 +01006493 assert(entryblock!= NULL);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006494 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
6495 if (!stack) {
6496 PyErr_NoMemory();
6497 return -1;
6498 }
6499
6500 sp = stack;
Mark Shannonb37181e2021-04-06 11:48:59 +01006501 if (c->u->u_ste->ste_generator || c->u->u_ste->ste_coroutine) {
6502 stackdepth_push(&sp, entryblock, 1);
6503 } else {
6504 stackdepth_push(&sp, entryblock, 0);
6505 }
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006506 while (sp != stack) {
6507 b = *--sp;
6508 int depth = b->b_startdepth;
6509 assert(depth >= 0);
6510 basicblock *next = b->b_next;
6511 for (int i = 0; i < b->b_iused; i++) {
6512 struct instr *instr = &b->b_instr[i];
6513 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
6514 if (effect == PY_INVALID_STACK_EFFECT) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01006515 PyErr_Format(PyExc_SystemError,
6516 "compiler stack_effect(opcode=%d, arg=%i) failed",
6517 instr->i_opcode, instr->i_oparg);
6518 return -1;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006519 }
6520 int new_depth = depth + effect;
6521 if (new_depth > maxdepth) {
6522 maxdepth = new_depth;
6523 }
6524 assert(depth >= 0); /* invalid code or bug in stackdepth() */
Mark Shannon582aaf12020-08-04 17:30:11 +01006525 if (is_jump(instr)) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006526 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
6527 assert(effect != PY_INVALID_STACK_EFFECT);
6528 int target_depth = depth + effect;
6529 if (target_depth > maxdepth) {
6530 maxdepth = target_depth;
6531 }
6532 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006533 stackdepth_push(&sp, instr->i_target, target_depth);
6534 }
6535 depth = new_depth;
6536 if (instr->i_opcode == JUMP_ABSOLUTE ||
6537 instr->i_opcode == JUMP_FORWARD ||
6538 instr->i_opcode == RETURN_VALUE ||
Mark Shannonfee55262019-11-21 09:11:43 +00006539 instr->i_opcode == RAISE_VARARGS ||
6540 instr->i_opcode == RERAISE)
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006541 {
6542 /* remaining code is dead */
6543 next = NULL;
6544 break;
6545 }
6546 }
6547 if (next != NULL) {
Mark Shannon266b4622020-11-17 19:30:14 +00006548 assert(b->b_nofallthrough == 0);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006549 stackdepth_push(&sp, next, depth);
6550 }
6551 }
6552 PyObject_Free(stack);
6553 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006554}
6555
6556static int
6557assemble_init(struct assembler *a, int nblocks, int firstlineno)
6558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006559 memset(a, 0, sizeof(struct assembler));
Mark Shannon877df852020-11-12 09:43:29 +00006560 a->a_prevlineno = a->a_lineno = firstlineno;
Mark Shannonfd009e62020-11-13 12:53:53 +00006561 a->a_lnotab = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006562 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006563 if (a->a_bytecode == NULL) {
6564 goto error;
6565 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006566 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006567 if (a->a_lnotab == NULL) {
6568 goto error;
6569 }
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07006570 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006571 PyErr_NoMemory();
Mark Shannonfd009e62020-11-13 12:53:53 +00006572 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006573 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006574 return 1;
Mark Shannonfd009e62020-11-13 12:53:53 +00006575error:
6576 Py_XDECREF(a->a_bytecode);
6577 Py_XDECREF(a->a_lnotab);
6578 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006579}
6580
6581static void
6582assemble_free(struct assembler *a)
6583{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006584 Py_XDECREF(a->a_bytecode);
6585 Py_XDECREF(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006586}
6587
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006588static int
6589blocksize(basicblock *b)
6590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006591 int i;
6592 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006594 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006595 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006596 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006597}
6598
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006599static int
Mark Shannon877df852020-11-12 09:43:29 +00006600assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006601{
Mark Shannon877df852020-11-12 09:43:29 +00006602 Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006603 if (a->a_lnotab_off + 2 >= len) {
6604 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
6605 return 0;
6606 }
Pablo Galindo86e322f2021-01-30 13:54:22 +00006607 unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab);
6608 lnotab += a->a_lnotab_off;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006609 a->a_lnotab_off += 2;
Mark Shannon877df852020-11-12 09:43:29 +00006610 *lnotab++ = bdelta;
6611 *lnotab++ = ldelta;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006612 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006613}
6614
Mark Shannon877df852020-11-12 09:43:29 +00006615/* Appends a range to the end of the line number table. See
6616 * Objects/lnotab_notes.txt for the description of the line number table. */
6617
6618static int
6619assemble_line_range(struct assembler *a)
6620{
6621 int ldelta, bdelta;
6622 bdelta = (a->a_offset - a->a_lineno_start) * 2;
6623 if (bdelta == 0) {
6624 return 1;
6625 }
6626 if (a->a_lineno < 0) {
6627 ldelta = -128;
6628 }
6629 else {
6630 ldelta = a->a_lineno - a->a_prevlineno;
6631 a->a_prevlineno = a->a_lineno;
6632 while (ldelta > 127) {
6633 if (!assemble_emit_linetable_pair(a, 0, 127)) {
6634 return 0;
6635 }
6636 ldelta -= 127;
6637 }
6638 while (ldelta < -127) {
6639 if (!assemble_emit_linetable_pair(a, 0, -127)) {
6640 return 0;
6641 }
6642 ldelta += 127;
6643 }
6644 }
6645 assert(-128 <= ldelta && ldelta < 128);
6646 while (bdelta > 254) {
6647 if (!assemble_emit_linetable_pair(a, 254, ldelta)) {
6648 return 0;
6649 }
6650 ldelta = a->a_lineno < 0 ? -128 : 0;
6651 bdelta -= 254;
6652 }
6653 if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) {
6654 return 0;
6655 }
6656 a->a_lineno_start = a->a_offset;
6657 return 1;
6658}
6659
6660static int
6661assemble_lnotab(struct assembler *a, struct instr *i)
6662{
6663 if (i->i_lineno == a->a_lineno) {
6664 return 1;
6665 }
6666 if (!assemble_line_range(a)) {
6667 return 0;
6668 }
6669 a->a_lineno = i->i_lineno;
6670 return 1;
6671}
6672
6673
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006674/* assemble_emit()
6675 Extend the bytecode with a new instruction.
6676 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00006677*/
6678
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006679static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006680assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006681{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006682 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006683 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03006684 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006685
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006686 arg = i->i_oparg;
6687 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006688 if (i->i_lineno && !assemble_lnotab(a, i))
6689 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006690 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006691 if (len > PY_SSIZE_T_MAX / 2)
6692 return 0;
6693 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
6694 return 0;
6695 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03006696 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006697 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006698 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006699 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006700}
6701
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00006702static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006703assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006705 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006706 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006707 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00006708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006709 /* Compute the size of each block and fixup jump args.
6710 Replace block pointer with position in bytecode. */
6711 do {
6712 totsize = 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006713 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006714 bsize = blocksize(b);
6715 b->b_offset = totsize;
6716 totsize += bsize;
6717 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006718 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006719 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6720 bsize = b->b_offset;
6721 for (i = 0; i < b->b_iused; i++) {
6722 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006723 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006724 /* Relative jumps are computed relative to
6725 the instruction pointer after fetching
6726 the jump instruction.
6727 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006728 bsize += isize;
Mark Shannon582aaf12020-08-04 17:30:11 +01006729 if (is_jump(instr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006730 instr->i_oparg = instr->i_target->b_offset;
Mark Shannon582aaf12020-08-04 17:30:11 +01006731 if (is_relative_jump(instr)) {
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006732 instr->i_oparg -= bsize;
6733 }
6734 if (instrsize(instr->i_oparg) != isize) {
6735 extended_arg_recompile = 1;
6736 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006737 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006738 }
6739 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00006740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006741 /* XXX: This is an awful hack that could hurt performance, but
6742 on the bright side it should work until we come up
6743 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006745 The issue is that in the first loop blocksize() is called
6746 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006747 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006748 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006750 So we loop until we stop seeing new EXTENDED_ARGs.
6751 The only EXTENDED_ARGs that could be popping up are
6752 ones in jump instructions. So this should converge
6753 fairly quickly.
6754 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006755 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00006756}
6757
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006758static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01006759dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006761 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006762 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006764 tuple = PyTuple_New(size);
6765 if (tuple == NULL)
6766 return NULL;
6767 while (PyDict_Next(dict, &pos, &k, &v)) {
6768 i = PyLong_AS_LONG(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006769 Py_INCREF(k);
6770 assert((i - offset) < size);
6771 assert((i - offset) >= 0);
6772 PyTuple_SET_ITEM(tuple, i - offset, k);
6773 }
6774 return tuple;
6775}
6776
6777static PyObject *
6778consts_dict_keys_inorder(PyObject *dict)
6779{
6780 PyObject *consts, *k, *v;
6781 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
6782
6783 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
6784 if (consts == NULL)
6785 return NULL;
6786 while (PyDict_Next(dict, &pos, &k, &v)) {
6787 i = PyLong_AS_LONG(v);
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +03006788 /* The keys of the dictionary can be tuples wrapping a contant.
6789 * (see compiler_add_o and _PyCode_ConstantKey). In that case
6790 * the object we want is always second. */
6791 if (PyTuple_CheckExact(k)) {
6792 k = PyTuple_GET_ITEM(k, 1);
6793 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006794 Py_INCREF(k);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006795 assert(i < size);
6796 assert(i >= 0);
6797 PyList_SET_ITEM(consts, i, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006798 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006799 return consts;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006800}
6801
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006802static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006803compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006804{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006805 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006806 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006807 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04006808 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006809 if (ste->ste_nested)
6810 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07006811 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006812 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07006813 if (!ste->ste_generator && ste->ste_coroutine)
6814 flags |= CO_COROUTINE;
6815 if (ste->ste_generator && ste->ste_coroutine)
6816 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006817 if (ste->ste_varargs)
6818 flags |= CO_VARARGS;
6819 if (ste->ste_varkeywords)
6820 flags |= CO_VARKEYWORDS;
6821 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006823 /* (Only) inherit compilerflags in PyCF_MASK */
6824 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006825
Pablo Galindo90235812020-03-15 04:29:22 +00006826 if ((IS_TOP_LEVEL_AWAIT(c)) &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07006827 ste->ste_coroutine &&
6828 !ste->ste_generator) {
6829 flags |= CO_COROUTINE;
6830 }
6831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006832 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00006833}
6834
Inada Naokibdb941b2021-02-10 09:20:42 +09006835// Merge *obj* with constant cache.
INADA Naokic2e16072018-11-26 21:23:22 +09006836// Unlike merge_consts_recursive(), this function doesn't work recursively.
6837static int
Inada Naokibdb941b2021-02-10 09:20:42 +09006838merge_const_one(struct compiler *c, PyObject **obj)
INADA Naokic2e16072018-11-26 21:23:22 +09006839{
Inada Naokibdb941b2021-02-10 09:20:42 +09006840 PyObject *key = _PyCode_ConstantKey(*obj);
INADA Naokic2e16072018-11-26 21:23:22 +09006841 if (key == NULL) {
6842 return 0;
6843 }
6844
6845 // t is borrowed reference
6846 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
6847 Py_DECREF(key);
6848 if (t == NULL) {
6849 return 0;
6850 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006851 if (t == key) { // obj is new constant.
INADA Naokic2e16072018-11-26 21:23:22 +09006852 return 1;
6853 }
6854
Inada Naokibdb941b2021-02-10 09:20:42 +09006855 if (PyTuple_CheckExact(t)) {
6856 // t is still borrowed reference
6857 t = PyTuple_GET_ITEM(t, 1);
6858 }
6859
6860 Py_INCREF(t);
6861 Py_DECREF(*obj);
6862 *obj = t;
INADA Naokic2e16072018-11-26 21:23:22 +09006863 return 1;
6864}
6865
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006866static PyCodeObject *
Mark Shannon6e8128f2020-07-30 10:03:00 +01006867makecode(struct compiler *c, struct assembler *a, PyObject *consts)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006868{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006869 PyCodeObject *co = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006870 PyObject *names = NULL;
6871 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006872 PyObject *name = NULL;
6873 PyObject *freevars = NULL;
6874 PyObject *cellvars = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006875 Py_ssize_t nlocals;
6876 int nlocals_int;
6877 int flags;
Pablo Galindocd74e662019-06-01 18:08:04 +01006878 int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006880 names = dict_keys_inorder(c->u->u_names, 0);
6881 varnames = dict_keys_inorder(c->u->u_varnames, 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006882 if (!names || !varnames) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006883 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006884 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006885 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
6886 if (!cellvars)
6887 goto error;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006888 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006889 if (!freevars)
6890 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006891
Inada Naokibdb941b2021-02-10 09:20:42 +09006892 if (!merge_const_one(c, &names) ||
6893 !merge_const_one(c, &varnames) ||
6894 !merge_const_one(c, &cellvars) ||
6895 !merge_const_one(c, &freevars))
INADA Naokic2e16072018-11-26 21:23:22 +09006896 {
6897 goto error;
6898 }
6899
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006900 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01006901 assert(nlocals < INT_MAX);
6902 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
6903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006904 flags = compute_code_flags(c);
6905 if (flags < 0)
6906 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006907
Mark Shannon6e8128f2020-07-30 10:03:00 +01006908 consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
6909 if (consts == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006910 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006911 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006912 if (!merge_const_one(c, &consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006913 Py_DECREF(consts);
INADA Naokic2e16072018-11-26 21:23:22 +09006914 goto error;
6915 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006916
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01006917 posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int);
Pablo Galindocd74e662019-06-01 18:08:04 +01006918 posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +01006919 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006920 maxdepth = stackdepth(c);
6921 if (maxdepth < 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006922 Py_DECREF(consts);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006923 goto error;
6924 }
Mark Shannon11e0b292021-04-15 14:28:56 +01006925 if (maxdepth > MAX_ALLOWED_STACK_USE) {
6926 PyErr_Format(PyExc_SystemError,
6927 "excessive stack use: stack is %d deep",
6928 maxdepth);
6929 Py_DECREF(consts);
6930 goto error;
6931 }
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006932 co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
Mark Shannon13bc1392020-01-23 09:25:17 +00006933 posonlyargcount, kwonlyargcount, nlocals_int,
Mark Shannon6e8128f2020-07-30 10:03:00 +01006934 maxdepth, flags, a->a_bytecode, consts, names,
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006935 varnames, freevars, cellvars, c->c_filename,
6936 c->u->u_name, c->u->u_firstlineno, a->a_lnotab);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006937 Py_DECREF(consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006938 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006939 Py_XDECREF(names);
6940 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006941 Py_XDECREF(name);
6942 Py_XDECREF(freevars);
6943 Py_XDECREF(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006944 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006945}
6946
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006947
6948/* For debugging purposes only */
6949#if 0
6950static void
Mark Shannon37686f72021-07-16 11:49:10 +01006951dump_instr(struct instr *i)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006952{
Mark Shannon37686f72021-07-16 11:49:10 +01006953 const char *jrel = (is_relative_jump(i)) ? "jrel " : "";
6954 const char *jabs = (is_jump(i) && !is_relative_jump(i))? "jabs " : "";
6955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006956 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006958 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006959 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006960 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006961 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006962 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
6963 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006964}
6965
6966static void
6967dump_basicblock(const basicblock *b)
6968{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006969 const char *b_return = b->b_return ? "return " : "";
Pablo Galindo60eb9f12020-06-28 01:55:47 +01006970 fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n",
6971 b->b_iused, b->b_startdepth, b->b_offset, b_return);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006972 if (b->b_instr) {
6973 int i;
6974 for (i = 0; i < b->b_iused; i++) {
6975 fprintf(stderr, " [%02d] ", i);
6976 dump_instr(b->b_instr + i);
6977 }
6978 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006979}
6980#endif
6981
Mark Shannon5977a792020-12-02 13:31:40 +00006982
6983static int
6984normalize_basic_block(basicblock *bb);
6985
Mark Shannon6e8128f2020-07-30 10:03:00 +01006986static int
Inada Naoki8a232c72021-04-16 14:01:04 +09006987optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006988
Ɓukasz Langad41abe82021-09-08 18:25:09 +02006989static int
6990trim_unused_consts(struct compiler *c, struct assembler *a, PyObject *consts);
6991
Mark Shannon762ef852021-08-09 10:54:48 +01006992/* Duplicates exit BBs, so that line numbers can be propagated to them */
Mark Shannon5977a792020-12-02 13:31:40 +00006993static int
Mark Shannon762ef852021-08-09 10:54:48 +01006994duplicate_exits_without_lineno(struct compiler *c);
Mark Shannon5977a792020-12-02 13:31:40 +00006995
Mark Shannonb37181e2021-04-06 11:48:59 +01006996static int
Mark Shannon37686f72021-07-16 11:49:10 +01006997extend_block(basicblock *bb);
6998
6999static int
Mark Shannonb37181e2021-04-06 11:48:59 +01007000insert_generator_prefix(struct compiler *c, basicblock *entryblock) {
7001
7002 int flags = compute_code_flags(c);
7003 if (flags < 0) {
7004 return -1;
7005 }
7006 int kind;
7007 if (flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
7008 if (flags & CO_COROUTINE) {
7009 kind = 1;
7010 }
7011 else if (flags & CO_ASYNC_GENERATOR) {
7012 kind = 2;
7013 }
7014 else {
7015 kind = 0;
7016 }
7017 }
7018 else {
7019 return 0;
7020 }
7021 if (compiler_next_instr(entryblock) < 0) {
7022 return -1;
7023 }
7024 for (int i = entryblock->b_iused-1; i > 0; i--) {
7025 entryblock->b_instr[i] = entryblock->b_instr[i-1];
7026 }
7027 entryblock->b_instr[0].i_opcode = GEN_START;
7028 entryblock->b_instr[0].i_oparg = kind;
7029 entryblock->b_instr[0].i_lineno = -1;
7030 entryblock->b_instr[0].i_target = NULL;
7031 return 0;
7032}
7033
Mark Shannon0acdf252021-05-13 14:11:41 +01007034/* Make sure that all returns have a line number, even if early passes
7035 * have failed to propagate a correct line number.
7036 * The resulting line number may not be correct according to PEP 626,
7037 * but should be "good enough", and no worse than in older versions. */
7038static void
7039guarantee_lineno_for_exits(struct assembler *a, int firstlineno) {
7040 int lineno = firstlineno;
7041 assert(lineno > 0);
7042 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7043 if (b->b_iused == 0) {
7044 continue;
7045 }
7046 struct instr *last = &b->b_instr[b->b_iused-1];
7047 if (last->i_lineno < 0) {
Mark Shannon762ef852021-08-09 10:54:48 +01007048 if (last->i_opcode == RETURN_VALUE) {
Mark Shannon0acdf252021-05-13 14:11:41 +01007049 for (int i = 0; i < b->b_iused; i++) {
7050 assert(b->b_instr[i].i_lineno < 0);
Mark Shannon762ef852021-08-09 10:54:48 +01007051
Mark Shannon0acdf252021-05-13 14:11:41 +01007052 b->b_instr[i].i_lineno = lineno;
7053 }
7054 }
7055 }
7056 else {
7057 lineno = last->i_lineno;
7058 }
7059 }
7060}
7061
Mark Shannon762ef852021-08-09 10:54:48 +01007062static void
7063propagate_line_numbers(struct assembler *a);
7064
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007065static PyCodeObject *
7066assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00007067{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007068 basicblock *b, *entryblock;
7069 struct assembler a;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007070 int j, nblocks;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007071 PyCodeObject *co = NULL;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007072 PyObject *consts = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00007073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007074 /* Make sure every block that falls off the end returns None.
7075 XXX NEXT_BLOCK() isn't quite right, because if the last
7076 block ends with a jump or return b_next shouldn't set.
7077 */
7078 if (!c->u->u_curblock->b_return) {
Mark Shannon877df852020-11-12 09:43:29 +00007079 c->u->u_lineno = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007080 if (addNone)
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03007081 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007082 ADDOP(c, RETURN_VALUE);
7083 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00007084
Mark Shannon5977a792020-12-02 13:31:40 +00007085 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7086 if (normalize_basic_block(b)) {
Alex Henrie503627f2021-03-02 03:20:25 -07007087 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00007088 }
7089 }
7090
Mark Shannon37686f72021-07-16 11:49:10 +01007091 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7092 if (extend_block(b)) {
7093 return NULL;
7094 }
7095 }
7096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007097 nblocks = 0;
7098 entryblock = NULL;
7099 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
7100 nblocks++;
7101 entryblock = b;
7102 }
Mark Shannon67969f52021-04-07 10:52:07 +01007103 assert(entryblock != NULL);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00007104
Mark Shannonb37181e2021-04-06 11:48:59 +01007105 if (insert_generator_prefix(c, entryblock)) {
7106 goto error;
7107 }
7108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007109 /* Set firstlineno if it wasn't explicitly set. */
7110 if (!c->u->u_firstlineno) {
Mark Shannon67969f52021-04-07 10:52:07 +01007111 if (entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007112 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
Mark Shannon877df852020-11-12 09:43:29 +00007113 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007114 c->u->u_firstlineno = 1;
7115 }
Mark Shannon5977a792020-12-02 13:31:40 +00007116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007117 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
7118 goto error;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007119 a.a_entry = entryblock;
7120 a.a_nblocks = nblocks;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00007121
Mark Shannon6e8128f2020-07-30 10:03:00 +01007122 consts = consts_dict_keys_inorder(c->u->u_consts);
7123 if (consts == NULL) {
7124 goto error;
7125 }
Mark Shannon37686f72021-07-16 11:49:10 +01007126
Inada Naoki8a232c72021-04-16 14:01:04 +09007127 if (optimize_cfg(c, &a, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007128 goto error;
7129 }
Mark Shannon762ef852021-08-09 10:54:48 +01007130 if (duplicate_exits_without_lineno(c)) {
7131 return NULL;
7132 }
Ɓukasz Langad41abe82021-09-08 18:25:09 +02007133 if (trim_unused_consts(c, &a, consts)) {
7134 goto error;
7135 }
Mark Shannon762ef852021-08-09 10:54:48 +01007136 propagate_line_numbers(&a);
Mark Shannon0acdf252021-05-13 14:11:41 +01007137 guarantee_lineno_for_exits(&a, c->u->u_firstlineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007138 /* Can't modify the bytecode after computing jump offsets. */
7139 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00007140
Mark Shannoncc75ab72020-11-12 19:49:33 +00007141 /* Emit code. */
7142 for(b = entryblock; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007143 for (j = 0; j < b->b_iused; j++)
7144 if (!assemble_emit(&a, &b->b_instr[j]))
7145 goto error;
7146 }
Mark Shannon877df852020-11-12 09:43:29 +00007147 if (!assemble_line_range(&a)) {
7148 return 0;
7149 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00007150
Inada Naokibdb941b2021-02-10 09:20:42 +09007151 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007152 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09007153 }
7154 if (!merge_const_one(c, &a.a_lnotab)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007155 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09007156 }
7157 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) {
7158 goto error;
7159 }
7160 if (!merge_const_one(c, &a.a_bytecode)) {
7161 goto error;
7162 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00007163
Mark Shannon6e8128f2020-07-30 10:03:00 +01007164 co = makecode(c, &a, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007165 error:
Mark Shannon6e8128f2020-07-30 10:03:00 +01007166 Py_XDECREF(consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007167 assemble_free(&a);
7168 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00007169}
Georg Brandl8334fd92010-12-04 10:26:46 +00007170
Mark Shannon6e8128f2020-07-30 10:03:00 +01007171/* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
7172 with LOAD_CONST (c1, c2, ... cn).
7173 The consts table must still be in list form so that the
7174 new constant (c1, c2, ... cn) can be appended.
7175 Called with codestr pointing to the first LOAD_CONST.
7176*/
7177static int
Inada Naoki8a232c72021-04-16 14:01:04 +09007178fold_tuple_on_constants(struct compiler *c,
7179 struct instr *inst,
Mark Shannon6e8128f2020-07-30 10:03:00 +01007180 int n, PyObject *consts)
7181{
7182 /* Pre-conditions */
7183 assert(PyList_CheckExact(consts));
7184 assert(inst[n].i_opcode == BUILD_TUPLE);
7185 assert(inst[n].i_oparg == n);
7186
7187 for (int i = 0; i < n; i++) {
7188 if (inst[i].i_opcode != LOAD_CONST) {
7189 return 0;
7190 }
7191 }
7192
7193 /* Buildup new tuple of constants */
7194 PyObject *newconst = PyTuple_New(n);
7195 if (newconst == NULL) {
7196 return -1;
7197 }
7198 for (int i = 0; i < n; i++) {
7199 int arg = inst[i].i_oparg;
7200 PyObject *constant = PyList_GET_ITEM(consts, arg);
7201 Py_INCREF(constant);
7202 PyTuple_SET_ITEM(newconst, i, constant);
7203 }
Inada Naoki8a232c72021-04-16 14:01:04 +09007204 if (merge_const_one(c, &newconst) == 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007205 Py_DECREF(newconst);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007206 return -1;
7207 }
Inada Naoki8a232c72021-04-16 14:01:04 +09007208
7209 Py_ssize_t index;
7210 for (index = 0; index < PyList_GET_SIZE(consts); index++) {
7211 if (PyList_GET_ITEM(consts, index) == newconst) {
7212 break;
7213 }
7214 }
7215 if (index == PyList_GET_SIZE(consts)) {
7216 if ((size_t)index >= (size_t)INT_MAX - 1) {
7217 Py_DECREF(newconst);
7218 PyErr_SetString(PyExc_OverflowError, "too many constants");
7219 return -1;
7220 }
7221 if (PyList_Append(consts, newconst)) {
7222 Py_DECREF(newconst);
7223 return -1;
7224 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007225 }
7226 Py_DECREF(newconst);
7227 for (int i = 0; i < n; i++) {
7228 inst[i].i_opcode = NOP;
7229 }
7230 inst[n].i_opcode = LOAD_CONST;
Victor Stinner71f2ff42020-09-23 14:06:55 +02007231 inst[n].i_oparg = (int)index;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007232 return 0;
7233}
7234
Mark Shannon28b75c82020-12-23 11:43:10 +00007235
Brandt Bucher0ad1e032021-05-02 13:02:10 -07007236// Eliminate n * ROT_N(n).
7237static void
7238fold_rotations(struct instr *inst, int n)
7239{
7240 for (int i = 0; i < n; i++) {
7241 int rot;
7242 switch (inst[i].i_opcode) {
7243 case ROT_N:
7244 rot = inst[i].i_oparg;
7245 break;
7246 case ROT_FOUR:
7247 rot = 4;
7248 break;
7249 case ROT_THREE:
7250 rot = 3;
7251 break;
7252 case ROT_TWO:
7253 rot = 2;
7254 break;
7255 default:
7256 return;
7257 }
7258 if (rot != n) {
7259 return;
7260 }
7261 }
7262 for (int i = 0; i < n; i++) {
7263 inst[i].i_opcode = NOP;
7264 }
7265}
7266
7267
Mark Shannon28b75c82020-12-23 11:43:10 +00007268static int
7269eliminate_jump_to_jump(basicblock *bb, int opcode) {
7270 assert (bb->b_iused > 0);
7271 struct instr *inst = &bb->b_instr[bb->b_iused-1];
7272 assert (is_jump(inst));
7273 assert (inst->i_target->b_iused > 0);
7274 struct instr *target = &inst->i_target->b_instr[0];
7275 if (inst->i_target == target->i_target) {
7276 /* Nothing to do */
7277 return 0;
7278 }
7279 int lineno = target->i_lineno;
7280 if (add_jump_to_block(bb, opcode, lineno, target->i_target) == 0) {
7281 return -1;
7282 }
7283 assert (bb->b_iused >= 2);
7284 bb->b_instr[bb->b_iused-2].i_opcode = NOP;
7285 return 0;
7286}
7287
Mark Shannoncc75ab72020-11-12 19:49:33 +00007288/* Maximum size of basic block that should be copied in optimizer */
7289#define MAX_COPY_SIZE 4
Mark Shannon6e8128f2020-07-30 10:03:00 +01007290
7291/* Optimization */
7292static int
Inada Naoki8a232c72021-04-16 14:01:04 +09007293optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
Mark Shannon6e8128f2020-07-30 10:03:00 +01007294{
7295 assert(PyList_CheckExact(consts));
7296 struct instr nop;
7297 nop.i_opcode = NOP;
7298 struct instr *target;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007299 for (int i = 0; i < bb->b_iused; i++) {
7300 struct instr *inst = &bb->b_instr[i];
7301 int oparg = inst->i_oparg;
7302 int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01007303 if (is_jump(inst)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007304 /* Skip over empty basic blocks. */
7305 while (inst->i_target->b_iused == 0) {
7306 inst->i_target = inst->i_target->b_next;
7307 }
7308 target = &inst->i_target->b_instr[0];
7309 }
7310 else {
7311 target = &nop;
7312 }
7313 switch (inst->i_opcode) {
Mark Shannon266b4622020-11-17 19:30:14 +00007314 /* Remove LOAD_CONST const; conditional jump */
Mark Shannon6e8128f2020-07-30 10:03:00 +01007315 case LOAD_CONST:
Mark Shannon266b4622020-11-17 19:30:14 +00007316 {
7317 PyObject* cnt;
7318 int is_true;
7319 int jump_if_true;
7320 switch(nextop) {
7321 case POP_JUMP_IF_FALSE:
7322 case POP_JUMP_IF_TRUE:
7323 cnt = PyList_GET_ITEM(consts, oparg);
7324 is_true = PyObject_IsTrue(cnt);
7325 if (is_true == -1) {
7326 goto error;
7327 }
7328 inst->i_opcode = NOP;
7329 jump_if_true = nextop == POP_JUMP_IF_TRUE;
7330 if (is_true == jump_if_true) {
7331 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
7332 bb->b_nofallthrough = 1;
7333 }
7334 else {
7335 bb->b_instr[i+1].i_opcode = NOP;
7336 }
7337 break;
7338 case JUMP_IF_FALSE_OR_POP:
7339 case JUMP_IF_TRUE_OR_POP:
7340 cnt = PyList_GET_ITEM(consts, oparg);
7341 is_true = PyObject_IsTrue(cnt);
7342 if (is_true == -1) {
7343 goto error;
7344 }
7345 jump_if_true = nextop == JUMP_IF_TRUE_OR_POP;
7346 if (is_true == jump_if_true) {
7347 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
7348 bb->b_nofallthrough = 1;
7349 }
7350 else {
7351 inst->i_opcode = NOP;
7352 bb->b_instr[i+1].i_opcode = NOP;
7353 }
7354 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007355 }
7356 break;
Mark Shannon266b4622020-11-17 19:30:14 +00007357 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007358
7359 /* Try to fold tuples of constants.
7360 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
7361 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
7362 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
7363 case BUILD_TUPLE:
7364 if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
7365 switch(oparg) {
7366 case 1:
7367 inst->i_opcode = NOP;
7368 bb->b_instr[i+1].i_opcode = NOP;
7369 break;
7370 case 2:
7371 inst->i_opcode = ROT_TWO;
7372 bb->b_instr[i+1].i_opcode = NOP;
7373 break;
7374 case 3:
7375 inst->i_opcode = ROT_THREE;
7376 bb->b_instr[i+1].i_opcode = ROT_TWO;
7377 }
7378 break;
7379 }
7380 if (i >= oparg) {
Inada Naoki8a232c72021-04-16 14:01:04 +09007381 if (fold_tuple_on_constants(c, inst-oparg, oparg, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007382 goto error;
7383 }
7384 }
7385 break;
7386
7387 /* Simplify conditional jump to conditional jump where the
7388 result of the first test implies the success of a similar
7389 test or the failure of the opposite test.
7390 Arises in code like:
7391 "a and b or c"
7392 "(a and b) and c"
7393 "(a or b) or c"
7394 "(a or b) and c"
7395 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
7396 --> x:JUMP_IF_FALSE_OR_POP z
7397 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
7398 --> x:POP_JUMP_IF_FALSE y+1
7399 where y+1 is the instruction following the second test.
7400 */
7401 case JUMP_IF_FALSE_OR_POP:
7402 switch(target->i_opcode) {
7403 case POP_JUMP_IF_FALSE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007404 if (inst->i_lineno == target->i_lineno) {
7405 *inst = *target;
7406 i--;
7407 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007408 break;
7409 case JUMP_ABSOLUTE:
7410 case JUMP_FORWARD:
7411 case JUMP_IF_FALSE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00007412 if (inst->i_lineno == target->i_lineno &&
7413 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00007414 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007415 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007416 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007417 break;
7418 case JUMP_IF_TRUE_OR_POP:
7419 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00007420 if (inst->i_lineno == target->i_lineno) {
7421 inst->i_opcode = POP_JUMP_IF_FALSE;
7422 inst->i_target = inst->i_target->b_next;
7423 --i;
7424 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007425 break;
7426 }
7427 break;
7428
7429 case JUMP_IF_TRUE_OR_POP:
7430 switch(target->i_opcode) {
7431 case POP_JUMP_IF_TRUE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007432 if (inst->i_lineno == target->i_lineno) {
7433 *inst = *target;
7434 i--;
7435 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007436 break;
7437 case JUMP_ABSOLUTE:
7438 case JUMP_FORWARD:
7439 case JUMP_IF_TRUE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00007440 if (inst->i_lineno == target->i_lineno &&
7441 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00007442 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007443 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007444 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007445 break;
7446 case JUMP_IF_FALSE_OR_POP:
7447 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00007448 if (inst->i_lineno == target->i_lineno) {
7449 inst->i_opcode = POP_JUMP_IF_TRUE;
7450 inst->i_target = inst->i_target->b_next;
7451 --i;
7452 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007453 break;
7454 }
7455 break;
7456
7457 case POP_JUMP_IF_FALSE:
7458 switch(target->i_opcode) {
7459 case JUMP_ABSOLUTE:
7460 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007461 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00007462 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007463 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007464 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007465 break;
7466 }
7467 break;
7468
7469 case POP_JUMP_IF_TRUE:
7470 switch(target->i_opcode) {
7471 case JUMP_ABSOLUTE:
7472 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007473 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00007474 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007475 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007476 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007477 break;
7478 }
7479 break;
7480
7481 case JUMP_ABSOLUTE:
7482 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007483 assert (i == bb->b_iused-1);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007484 switch(target->i_opcode) {
7485 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007486 if (eliminate_jump_to_jump(bb, inst->i_opcode)) {
7487 goto error;
Mark Shannon266b4622020-11-17 19:30:14 +00007488 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007489 break;
Mark Shannon28b75c82020-12-23 11:43:10 +00007490
Mark Shannon6e8128f2020-07-30 10:03:00 +01007491 case JUMP_ABSOLUTE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007492 if (eliminate_jump_to_jump(bb, JUMP_ABSOLUTE)) {
7493 goto error;
Mark Shannon266b4622020-11-17 19:30:14 +00007494 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007495 break;
Mark Shannon37686f72021-07-16 11:49:10 +01007496 }
7497 break;
7498 case FOR_ITER:
7499 assert (i == bb->b_iused-1);
7500 if (target->i_opcode == JUMP_FORWARD) {
7501 if (eliminate_jump_to_jump(bb, inst->i_opcode)) {
7502 goto error;
7503 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007504 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07007505 break;
7506 case ROT_N:
7507 switch (oparg) {
7508 case 0:
7509 case 1:
7510 inst->i_opcode = NOP;
7511 continue;
7512 case 2:
7513 inst->i_opcode = ROT_TWO;
7514 break;
7515 case 3:
7516 inst->i_opcode = ROT_THREE;
7517 break;
7518 case 4:
7519 inst->i_opcode = ROT_FOUR;
7520 break;
7521 }
7522 if (i >= oparg - 1) {
7523 fold_rotations(inst - oparg + 1, oparg);
7524 }
7525 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007526 }
7527 }
7528 return 0;
7529error:
7530 return -1;
7531}
7532
Mark Shannon37686f72021-07-16 11:49:10 +01007533/* If this block ends with an unconditional jump to an exit block,
7534 * then remove the jump and extend this block with the target.
7535 */
7536static int
7537extend_block(basicblock *bb) {
7538 if (bb->b_iused == 0) {
7539 return 0;
7540 }
7541 struct instr *last = &bb->b_instr[bb->b_iused-1];
7542 if (last->i_opcode != JUMP_ABSOLUTE && last->i_opcode != JUMP_FORWARD) {
7543 return 0;
7544 }
7545 if (last->i_target->b_exit && last->i_target->b_iused <= MAX_COPY_SIZE) {
7546 basicblock *to_copy = last->i_target;
7547 last->i_opcode = NOP;
7548 for (int i = 0; i < to_copy->b_iused; i++) {
7549 int index = compiler_next_instr(bb);
7550 if (index < 0) {
7551 return -1;
7552 }
7553 bb->b_instr[index] = to_copy->b_instr[i];
7554 }
7555 bb->b_exit = 1;
7556 }
7557 return 0;
7558}
Mark Shannon6e8128f2020-07-30 10:03:00 +01007559
7560static void
Mark Shannon1659ad12021-01-13 15:05:04 +00007561clean_basic_block(basicblock *bb, int prev_lineno) {
7562 /* Remove NOPs when legal to do so. */
Mark Shannon6e8128f2020-07-30 10:03:00 +01007563 int dest = 0;
7564 for (int src = 0; src < bb->b_iused; src++) {
Mark Shannon877df852020-11-12 09:43:29 +00007565 int lineno = bb->b_instr[src].i_lineno;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007566 if (bb->b_instr[src].i_opcode == NOP) {
Mark Shannon266b4622020-11-17 19:30:14 +00007567 /* Eliminate no-op if it doesn't have a line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007568 if (lineno < 0) {
7569 continue;
7570 }
Mark Shannon266b4622020-11-17 19:30:14 +00007571 /* or, if the previous instruction had the same line number. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007572 if (prev_lineno == lineno) {
7573 continue;
7574 }
Mark Shannon266b4622020-11-17 19:30:14 +00007575 /* or, if the next instruction has same line number or no line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007576 if (src < bb->b_iused - 1) {
7577 int next_lineno = bb->b_instr[src+1].i_lineno;
7578 if (next_lineno < 0 || next_lineno == lineno) {
7579 bb->b_instr[src+1].i_lineno = lineno;
7580 continue;
Mark Shannon877df852020-11-12 09:43:29 +00007581 }
7582 }
Mark Shannon266b4622020-11-17 19:30:14 +00007583 else {
7584 basicblock* next = bb->b_next;
7585 while (next && next->b_iused == 0) {
7586 next = next->b_next;
7587 }
7588 /* or if last instruction in BB and next BB has same line number */
7589 if (next) {
7590 if (lineno == next->b_instr[0].i_lineno) {
7591 continue;
7592 }
7593 }
7594 }
7595
Mark Shannon6e8128f2020-07-30 10:03:00 +01007596 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007597 if (dest != src) {
7598 bb->b_instr[dest] = bb->b_instr[src];
7599 }
7600 dest++;
7601 prev_lineno = lineno;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007602 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007603 assert(dest <= bb->b_iused);
7604 bb->b_iused = dest;
7605}
7606
Mark Shannon266b4622020-11-17 19:30:14 +00007607static int
7608normalize_basic_block(basicblock *bb) {
7609 /* Mark blocks as exit and/or nofallthrough.
7610 Raise SystemError if CFG is malformed. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007611 for (int i = 0; i < bb->b_iused; i++) {
7612 switch(bb->b_instr[i].i_opcode) {
7613 case RETURN_VALUE:
7614 case RAISE_VARARGS:
7615 case RERAISE:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007616 bb->b_exit = 1;
Mark Shannon5977a792020-12-02 13:31:40 +00007617 bb->b_nofallthrough = 1;
7618 break;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007619 case JUMP_ABSOLUTE:
7620 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007621 bb->b_nofallthrough = 1;
Mark Shannon266b4622020-11-17 19:30:14 +00007622 /* fall through */
7623 case POP_JUMP_IF_FALSE:
7624 case POP_JUMP_IF_TRUE:
7625 case JUMP_IF_FALSE_OR_POP:
7626 case JUMP_IF_TRUE_OR_POP:
Mark Shannon5977a792020-12-02 13:31:40 +00007627 case FOR_ITER:
Mark Shannon266b4622020-11-17 19:30:14 +00007628 if (i != bb->b_iused-1) {
7629 PyErr_SetString(PyExc_SystemError, "malformed control flow graph.");
7630 return -1;
7631 }
Mark Shannon5977a792020-12-02 13:31:40 +00007632 /* Skip over empty basic blocks. */
7633 while (bb->b_instr[i].i_target->b_iused == 0) {
7634 bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next;
7635 }
7636
Mark Shannoncc75ab72020-11-12 19:49:33 +00007637 }
7638 }
Mark Shannon266b4622020-11-17 19:30:14 +00007639 return 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007640}
7641
Mark Shannon6e8128f2020-07-30 10:03:00 +01007642static int
7643mark_reachable(struct assembler *a) {
7644 basicblock **stack, **sp;
7645 sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks);
7646 if (stack == NULL) {
7647 return -1;
7648 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007649 a->a_entry->b_predecessors = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007650 *sp++ = a->a_entry;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007651 while (sp > stack) {
7652 basicblock *b = *(--sp);
Mark Shannon3bd60352021-01-13 12:05:43 +00007653 if (b->b_next && !b->b_nofallthrough) {
7654 if (b->b_next->b_predecessors == 0) {
7655 *sp++ = b->b_next;
7656 }
7657 b->b_next->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007658 }
7659 for (int i = 0; i < b->b_iused; i++) {
7660 basicblock *target;
Mark Shannon582aaf12020-08-04 17:30:11 +01007661 if (is_jump(&b->b_instr[i])) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007662 target = b->b_instr[i].i_target;
Mark Shannon3bd60352021-01-13 12:05:43 +00007663 if (target->b_predecessors == 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007664 *sp++ = target;
7665 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007666 target->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007667 }
7668 }
7669 }
7670 PyObject_Free(stack);
7671 return 0;
7672}
7673
Mark Shannon3bd60352021-01-13 12:05:43 +00007674static void
7675eliminate_empty_basic_blocks(basicblock *entry) {
7676 /* Eliminate empty blocks */
7677 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7678 basicblock *next = b->b_next;
7679 if (next) {
7680 while (next->b_iused == 0 && next->b_next) {
7681 next = next->b_next;
7682 }
7683 b->b_next = next;
7684 }
7685 }
7686 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7687 if (b->b_iused == 0) {
7688 continue;
7689 }
7690 if (is_jump(&b->b_instr[b->b_iused-1])) {
7691 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7692 while (target->b_iused == 0) {
7693 target = target->b_next;
7694 }
7695 b->b_instr[b->b_iused-1].i_target = target;
7696 }
7697 }
7698}
7699
7700
Mark Shannon5977a792020-12-02 13:31:40 +00007701/* If an instruction has no line number, but it's predecessor in the BB does,
Mark Shannon3bd60352021-01-13 12:05:43 +00007702 * then copy the line number. If a successor block has no line number, and only
7703 * one predecessor, then inherit the line number.
7704 * This ensures that all exit blocks (with one predecessor) receive a line number.
7705 * Also reduces the size of the line number table,
Mark Shannon5977a792020-12-02 13:31:40 +00007706 * but has no impact on the generated line number events.
7707 */
7708static void
Mark Shannon762ef852021-08-09 10:54:48 +01007709propagate_line_numbers(struct assembler *a) {
Mark Shannon5977a792020-12-02 13:31:40 +00007710 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007711 if (b->b_iused == 0) {
7712 continue;
7713 }
Mark Shannon5977a792020-12-02 13:31:40 +00007714 int prev_lineno = -1;
7715 for (int i = 0; i < b->b_iused; i++) {
7716 if (b->b_instr[i].i_lineno < 0) {
7717 b->b_instr[i].i_lineno = prev_lineno;
7718 }
7719 else {
7720 prev_lineno = b->b_instr[i].i_lineno;
7721 }
7722 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007723 if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) {
7724 assert(b->b_next->b_iused);
7725 if (b->b_next->b_instr[0].i_lineno < 0) {
7726 b->b_next->b_instr[0].i_lineno = prev_lineno;
7727 }
7728 }
7729 if (is_jump(&b->b_instr[b->b_iused-1])) {
7730 switch (b->b_instr[b->b_iused-1].i_opcode) {
7731 /* Note: Only actual jumps, not exception handlers */
7732 case SETUP_ASYNC_WITH:
7733 case SETUP_WITH:
7734 case SETUP_FINALLY:
7735 continue;
7736 }
7737 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7738 if (target->b_predecessors == 1) {
7739 if (target->b_instr[0].i_lineno < 0) {
7740 target->b_instr[0].i_lineno = prev_lineno;
7741 }
7742 }
7743 }
Mark Shannon5977a792020-12-02 13:31:40 +00007744 }
7745}
7746
7747/* Perform optimizations on a control flow graph.
Mark Shannon6e8128f2020-07-30 10:03:00 +01007748 The consts object should still be in list form to allow new constants
7749 to be appended.
7750
7751 All transformations keep the code size the same or smaller.
7752 For those that reduce size, the gaps are initially filled with
7753 NOPs. Later those NOPs are removed.
7754*/
7755
7756static int
Inada Naoki8a232c72021-04-16 14:01:04 +09007757optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts)
Mark Shannon6e8128f2020-07-30 10:03:00 +01007758{
Mark Shannoncc75ab72020-11-12 19:49:33 +00007759 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Inada Naoki8a232c72021-04-16 14:01:04 +09007760 if (optimize_basic_block(c, b, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007761 return -1;
7762 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007763 clean_basic_block(b, -1);
Mark Shannon3bd60352021-01-13 12:05:43 +00007764 assert(b->b_predecessors == 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007765 }
Mark Shannon762ef852021-08-09 10:54:48 +01007766 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7767 if (extend_block(b)) {
7768 return -1;
7769 }
7770 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007771 if (mark_reachable(a)) {
7772 return -1;
7773 }
7774 /* Delete unreachable instructions */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007775 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007776 if (b->b_predecessors == 0) {
Mark Shannoncc75ab72020-11-12 19:49:33 +00007777 b->b_iused = 0;
Om Gc71581c2020-12-16 17:48:05 +05307778 b->b_nofallthrough = 0;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007779 }
7780 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007781 basicblock *pred = NULL;
7782 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7783 int prev_lineno = -1;
7784 if (pred && pred->b_iused) {
7785 prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno;
7786 }
7787 clean_basic_block(b, prev_lineno);
7788 pred = b->b_nofallthrough ? NULL : b;
7789 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007790 eliminate_empty_basic_blocks(a->a_entry);
Om Gc71581c2020-12-16 17:48:05 +05307791 /* Delete jump instructions made redundant by previous step. If a non-empty
7792 block ends with a jump instruction, check if the next non-empty block
7793 reached through normal flow control is the target of that jump. If it
7794 is, then the jump instruction is redundant and can be deleted.
7795 */
Mark Shannon3bd60352021-01-13 12:05:43 +00007796 int maybe_empty_blocks = 0;
Om Gc71581c2020-12-16 17:48:05 +05307797 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7798 if (b->b_iused > 0) {
7799 struct instr *b_last_instr = &b->b_instr[b->b_iused - 1];
Mark Shannon802b6452021-02-02 14:59:15 +00007800 if (b_last_instr->i_opcode == JUMP_ABSOLUTE ||
Om Gc71581c2020-12-16 17:48:05 +05307801 b_last_instr->i_opcode == JUMP_FORWARD) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007802 if (b_last_instr->i_target == b->b_next) {
7803 assert(b->b_next->b_iused);
Om Gc71581c2020-12-16 17:48:05 +05307804 b->b_nofallthrough = 0;
Mark Shannon802b6452021-02-02 14:59:15 +00007805 b_last_instr->i_opcode = NOP;
7806 clean_basic_block(b, -1);
7807 maybe_empty_blocks = 1;
Om Gc71581c2020-12-16 17:48:05 +05307808 }
7809 }
7810 }
7811 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007812 if (maybe_empty_blocks) {
7813 eliminate_empty_basic_blocks(a->a_entry);
7814 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007815 return 0;
7816}
7817
Ɓukasz Langad41abe82021-09-08 18:25:09 +02007818// Remove trailing unused constants.
7819static int
7820trim_unused_consts(struct compiler *c, struct assembler *a, PyObject *consts)
7821{
7822 assert(PyList_CheckExact(consts));
7823
7824 // The first constant may be docstring; keep it always.
7825 int max_const_index = 0;
7826 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7827 for (int i = 0; i < b->b_iused; i++) {
7828 if (b->b_instr[i].i_opcode == LOAD_CONST &&
7829 b->b_instr[i].i_oparg > max_const_index) {
7830 max_const_index = b->b_instr[i].i_oparg;
7831 }
7832 }
7833 }
7834 if (max_const_index+1 < PyList_GET_SIZE(consts)) {
7835 //fprintf(stderr, "removing trailing consts: max=%d, size=%d\n",
7836 // max_const_index, (int)PyList_GET_SIZE(consts));
7837 if (PyList_SetSlice(consts, max_const_index+1,
7838 PyList_GET_SIZE(consts), NULL) < 0) {
7839 return 1;
7840 }
7841 }
7842 return 0;
7843}
7844
Mark Shannon5977a792020-12-02 13:31:40 +00007845static inline int
7846is_exit_without_lineno(basicblock *b) {
7847 return b->b_exit && b->b_instr[0].i_lineno < 0;
7848}
7849
7850/* PEP 626 mandates that the f_lineno of a frame is correct
7851 * after a frame terminates. It would be prohibitively expensive
7852 * to continuously update the f_lineno field at runtime,
7853 * so we make sure that all exiting instruction (raises and returns)
7854 * have a valid line number, allowing us to compute f_lineno lazily.
7855 * We can do this by duplicating the exit blocks without line number
7856 * so that none have more than one predecessor. We can then safely
7857 * copy the line number from the sole predecessor block.
7858 */
7859static int
Mark Shannon762ef852021-08-09 10:54:48 +01007860duplicate_exits_without_lineno(struct compiler *c)
Mark Shannon5977a792020-12-02 13:31:40 +00007861{
7862 /* Copy all exit blocks without line number that are targets of a jump.
7863 */
7864 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7865 if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) {
7866 switch (b->b_instr[b->b_iused-1].i_opcode) {
7867 /* Note: Only actual jumps, not exception handlers */
7868 case SETUP_ASYNC_WITH:
7869 case SETUP_WITH:
7870 case SETUP_FINALLY:
7871 continue;
7872 }
7873 basicblock *target = b->b_instr[b->b_iused-1].i_target;
Mark Shannon762ef852021-08-09 10:54:48 +01007874 if (is_exit_without_lineno(target) && target->b_predecessors > 1) {
Mark Shannon5977a792020-12-02 13:31:40 +00007875 basicblock *new_target = compiler_copy_block(c, target);
7876 if (new_target == NULL) {
7877 return -1;
7878 }
7879 new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7880 b->b_instr[b->b_iused-1].i_target = new_target;
Mark Shannon762ef852021-08-09 10:54:48 +01007881 target->b_predecessors--;
7882 new_target->b_predecessors = 1;
7883 new_target->b_next = target->b_next;
7884 target->b_next = new_target;
Mark Shannon5977a792020-12-02 13:31:40 +00007885 }
7886 }
Mark Shannoneaccc122020-12-04 15:22:12 +00007887 }
Mark Shannonee9f98d2021-01-05 12:04:10 +00007888 /* Eliminate empty blocks */
7889 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7890 while (b->b_next && b->b_next->b_iused == 0) {
7891 b->b_next = b->b_next->b_next;
7892 }
7893 }
Mark Shannon5977a792020-12-02 13:31:40 +00007894 /* Any remaining reachable exit blocks without line number can only be reached by
7895 * fall through, and thus can only have a single predecessor */
7896 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7897 if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) {
7898 if (is_exit_without_lineno(b->b_next)) {
7899 assert(b->b_next->b_iused > 0);
7900 b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7901 }
7902 }
7903 }
7904 return 0;
7905}
7906
7907
Mark Shannon6e8128f2020-07-30 10:03:00 +01007908/* Retained for API compatibility.
7909 * Optimization is now done in optimize_cfg */
7910
7911PyObject *
7912PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
7913 PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
7914{
7915 Py_INCREF(code);
7916 return code;
7917}