blob: f012406c0669388945998ed9eaac40ba374cb4ee [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
Brandt Buchera89bbde2021-11-11 13:52:43 -080024#include <stdbool.h>
25
Guido van Rossum79f25d91997-04-29 20:08:16 +000026#include "Python.h"
Victor Stinner526fdeb2021-03-17 23:50:50 +010027#include "pycore_ast.h" // _PyAST_GetDocString()
Victor Stinnera81fca62021-03-24 00:51:50 +010028#include "pycore_compile.h" // _PyFuture_FromAST()
Victor Stinnerba7a99d2021-01-30 01:46:44 +010029#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
Victor Stinnerc9bc2902020-10-27 02:24:34 +010030#include "pycore_long.h" // _PyLong_GetZero()
Victor Stinner28ad12f2021-03-19 12:41:49 +010031#include "pycore_symtable.h" // PySTEntryObject
Guido van Rossum3f5da241990-12-20 15:06:42 +000032
Mark Shannon582aaf12020-08-04 17:30:11 +010033#define NEED_OPCODE_JUMP_TABLES
Victor Stinner526fdeb2021-03-17 23:50:50 +010034#include "opcode.h" // EXTENDED_ARG
35#include "wordcode_helpers.h" // instrsize()
36
Guido van Rossumb05a5c71997-05-07 17:46:13 +000037
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000038#define DEFAULT_BLOCK_SIZE 16
39#define DEFAULT_BLOCKS 8
40#define DEFAULT_CODE_SIZE 128
41#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000042
Nick Coghlan650f0d02007-04-15 12:05:43 +000043#define COMP_GENEXP 0
44#define COMP_LISTCOMP 1
45#define COMP_SETCOMP 2
Guido van Rossum992d4a32007-07-11 13:09:30 +000046#define COMP_DICTCOMP 3
Nick Coghlan650f0d02007-04-15 12:05:43 +000047
Mark Shannon11e0b292021-04-15 14:28:56 +010048/* A soft limit for stack use, to avoid excessive
49 * memory use for large constants, etc.
50 *
51 * The value 30 is plucked out of thin air.
52 * Code that could use more stack than this is
53 * rare, so the exact value is unimportant.
54 */
55#define STACK_USE_GUIDELINE 30
56
57/* If we exceed this limit, it should
58 * be considered a compiler bug.
59 * Currently it should be impossible
60 * to exceed STACK_USE_GUIDELINE * 100,
61 * as 100 is the maximum parse depth.
62 * For performance reasons we will
63 * want to reduce this to a
64 * few hundred in the future.
65 *
66 * NOTE: Whatever MAX_ALLOWED_STACK_USE is
67 * set to, it should never restrict what Python
68 * we can write, just how we compile it.
69 */
70#define MAX_ALLOWED_STACK_USE (STACK_USE_GUIDELINE * 100)
71
Pablo Galindo90235812020-03-15 04:29:22 +000072#define IS_TOP_LEVEL_AWAIT(c) ( \
73 (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \
74 && (c->u->u_ste->ste_type == ModuleBlock))
75
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000076struct instr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 unsigned char i_opcode;
78 int i_oparg;
79 struct basicblock_ *i_target; /* target block (if jump instruction) */
80 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000081};
82
Mark Shannon582aaf12020-08-04 17:30:11 +010083#define LOG_BITS_PER_INT 5
84#define MASK_LOW_LOG_BITS 31
85
86static inline int
87is_bit_set_in_table(uint32_t *table, int bitindex) {
88 /* Is the relevant bit set in the relevant word? */
89 /* 256 bits fit into 8 32-bits words.
90 * Word is indexed by (bitindex>>ln(size of int in bits)).
91 * Bit within word is the low bits of bitindex.
92 */
93 uint32_t word = table[bitindex >> LOG_BITS_PER_INT];
94 return (word >> (bitindex & MASK_LOW_LOG_BITS)) & 1;
95}
96
97static inline int
98is_relative_jump(struct instr *i)
99{
100 return is_bit_set_in_table(_PyOpcode_RelativeJump, i->i_opcode);
101}
102
103static inline int
104is_jump(struct instr *i)
105{
106 return is_bit_set_in_table(_PyOpcode_Jump, i->i_opcode);
107}
108
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000109typedef struct basicblock_ {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000110 /* Each basicblock in a compilation unit is linked via b_list in the
111 reverse order that the block are allocated. b_list points to the next
112 block, not to be confused with b_next, which is next by control flow. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 struct basicblock_ *b_list;
114 /* number of instructions used */
115 int b_iused;
116 /* length of instruction array (b_instr) */
117 int b_ialloc;
118 /* pointer to an array of instructions, initially NULL */
119 struct instr *b_instr;
120 /* If b_next is non-NULL, it is a pointer to the next
121 block reached by normal control flow. */
122 struct basicblock_ *b_next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 /* b_return is true if a RETURN_VALUE opcode is inserted. */
124 unsigned b_return : 1;
Mark Shannon3bd60352021-01-13 12:05:43 +0000125 /* Number of predecssors that a block has. */
126 int b_predecessors;
Mark Shannoncc75ab72020-11-12 19:49:33 +0000127 /* Basic block has no fall through (it ends with a return, raise or jump) */
128 unsigned b_nofallthrough : 1;
129 /* Basic block exits scope (it ends with a return or raise) */
130 unsigned b_exit : 1;
Mark Shannond4e4ef12022-02-16 11:26:02 +0000131 /* Used by compiler passes to mark whether they have visited a basic block. */
132 unsigned b_visited : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 /* depth of stack upon entry of block, computed by stackdepth() */
134 int b_startdepth;
135 /* instruction offset for block, computed by assemble_jump_offsets() */
136 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000137} basicblock;
138
139/* fblockinfo tracks the current frame block.
140
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000141A frame block is used to handle loops, try/except, and try/finally.
142It's called a frame block to distinguish it from a basic block in the
143compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000144*/
145
Mark Shannon02d126a2020-09-25 14:04:19 +0100146enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END,
tomKPZ7a7ba3d2021-04-07 07:43:45 -0700147 WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER,
148 ASYNC_COMPREHENSION_GENERATOR };
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000149
150struct fblockinfo {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 enum fblocktype fb_type;
152 basicblock *fb_block;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200153 /* (optional) type-specific exit or cleanup block */
154 basicblock *fb_exit;
Mark Shannonfee55262019-11-21 09:11:43 +0000155 /* (optional) additional information required for unwinding */
156 void *fb_datum;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000157};
158
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100159enum {
160 COMPILER_SCOPE_MODULE,
161 COMPILER_SCOPE_CLASS,
162 COMPILER_SCOPE_FUNCTION,
Yury Selivanov75445082015-05-11 22:57:16 -0400163 COMPILER_SCOPE_ASYNC_FUNCTION,
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400164 COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100165 COMPILER_SCOPE_COMPREHENSION,
166};
167
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000168/* The following items change on entry and exit of code blocks.
169 They must be saved and restored when returning to a block.
170*/
171struct compiler_unit {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 PySTEntryObject *u_ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 PyObject *u_name;
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400175 PyObject *u_qualname; /* dot-separated qualified name (lazy) */
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100176 int u_scope_type;
177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 /* The following fields are dicts that map objects to
179 the index of them in co_XXX. The index is used as
180 the argument for opcodes that refer to those collections.
181 */
182 PyObject *u_consts; /* all constants */
183 PyObject *u_names; /* all names */
184 PyObject *u_varnames; /* local variables */
185 PyObject *u_cellvars; /* cell variables */
186 PyObject *u_freevars; /* free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 PyObject *u_private; /* for private name mangling */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000189
Victor Stinnerf8e32212013-11-19 23:56:34 +0100190 Py_ssize_t u_argcount; /* number of arguments for block */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100191 Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */
Victor Stinnerf8e32212013-11-19 23:56:34 +0100192 Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 /* Pointer to the most recently allocated block. By following b_list
194 members, you can reach all early allocated blocks. */
195 basicblock *u_blocks;
196 basicblock *u_curblock; /* pointer to current block */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 int u_nfblocks;
199 struct fblockinfo u_fblock[CO_MAXBLOCKS];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 int u_firstlineno; /* the first lineno of the block */
202 int u_lineno; /* the lineno for the current stmt */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000203 int u_col_offset; /* the offset of the current stmt */
Pablo Galindoa77aac42021-04-23 14:27:05 +0100204 int u_end_lineno; /* the end line of the current stmt */
205 int u_end_col_offset; /* the end offset of the current stmt */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000206};
207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208/* This struct captures the global state of a compilation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000209
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000210The u pointer points to the current compilation unit, while units
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211for enclosing blocks are stored in c_stack. The u and c_stack are
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000212managed by compiler_enter_scope() and compiler_exit_scope().
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000213
214Note that we don't track recursion levels during compilation - the
215task of detecting and rejecting excessive levels of nesting is
216handled by the symbol analysis pass.
217
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000218*/
219
220struct compiler {
Victor Stinner14e461d2013-08-26 22:28:21 +0200221 PyObject *c_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 struct symtable *c_st;
223 PyFutureFeatures *c_future; /* pointer to module's __future__ */
224 PyCompilerFlags *c_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000225
Georg Brandl8334fd92010-12-04 10:26:46 +0000226 int c_optimize; /* optimization level */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 int c_interactive; /* true if in interactive mode */
228 int c_nestlevel;
INADA Naokic2e16072018-11-26 21:23:22 +0900229 PyObject *c_const_cache; /* Python dict holding all constants,
230 including names tuple */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 struct compiler_unit *u; /* compiler state for current block */
232 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
233 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000234};
235
Brandt Bucher145bf262021-02-26 14:51:55 -0800236typedef struct {
Brandt Bucher0ad1e032021-05-02 13:02:10 -0700237 // A list of strings corresponding to name captures. It is used to track:
238 // - Repeated name assignments in the same pattern.
239 // - Different name assignments in alternatives.
240 // - The order of name assignments in alternatives.
Brandt Bucher145bf262021-02-26 14:51:55 -0800241 PyObject *stores;
Brandt Bucher0ad1e032021-05-02 13:02:10 -0700242 // If 0, any name captures against our subject will raise.
Brandt Bucher145bf262021-02-26 14:51:55 -0800243 int allow_irrefutable;
Brandt Bucher0ad1e032021-05-02 13:02:10 -0700244 // An array of blocks to jump to on failure. Jumping to fail_pop[i] will pop
245 // i items off of the stack. The end result looks like this (with each block
246 // falling through to the next):
247 // fail_pop[4]: POP_TOP
248 // fail_pop[3]: POP_TOP
249 // fail_pop[2]: POP_TOP
250 // fail_pop[1]: POP_TOP
251 // fail_pop[0]: NOP
252 basicblock **fail_pop;
253 // The current length of fail_pop.
254 Py_ssize_t fail_pop_size;
255 // The number of items on top of the stack that need to *stay* on top of the
256 // stack. Variable captures go beneath these. All of them will be popped on
257 // failure.
258 Py_ssize_t on_top;
Brandt Bucher145bf262021-02-26 14:51:55 -0800259} pattern_context;
260
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100261static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000262static void compiler_free(struct compiler *);
263static basicblock *compiler_new_block(struct compiler *);
Andy Lester76d58772020-03-10 21:18:12 -0500264static int compiler_next_instr(basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000265static int compiler_addop(struct compiler *, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +0100266static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
Mark Shannon582aaf12020-08-04 17:30:11 +0100267static int compiler_addop_j(struct compiler *, int, basicblock *);
Mark Shannon127dde52021-01-04 18:06:55 +0000268static int compiler_addop_j_noline(struct compiler *, int, basicblock *);
Brandt Bucher145bf262021-02-26 14:51:55 -0800269static int compiler_error(struct compiler *, const char *, ...);
Serhiy Storchaka62e44812019-02-16 08:12:19 +0200270static int compiler_warn(struct compiler *, const char *, ...);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000271static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
272
273static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
274static int compiler_visit_stmt(struct compiler *, stmt_ty);
275static int compiler_visit_keyword(struct compiler *, keyword_ty);
276static int compiler_visit_expr(struct compiler *, expr_ty);
277static int compiler_augassign(struct compiler *, stmt_ty);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700278static int compiler_annassign(struct compiler *, stmt_ty);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +0200279static int compiler_subscript(struct compiler *, expr_ty);
280static int compiler_slice(struct compiler *, expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000281
Andy Lester76d58772020-03-10 21:18:12 -0500282static int inplace_binop(operator_ty);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100283static int are_all_items_const(asdl_expr_seq *, Py_ssize_t, Py_ssize_t);
Mark Shannon8473cf82020-12-15 11:07:50 +0000284
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000285
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500286static int compiler_with(struct compiler *, stmt_ty, int);
Yury Selivanov75445082015-05-11 22:57:16 -0400287static int compiler_async_with(struct compiler *, stmt_ty, int);
288static int compiler_async_for(struct compiler *, stmt_ty);
Victor Stinner976bb402016-03-23 11:36:19 +0100289static int compiler_call_helper(struct compiler *c, int n,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100290 asdl_expr_seq *args,
291 asdl_keyword_seq *keywords);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500292static int compiler_try_except(struct compiler *, stmt_ty);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400293static int compiler_set_qualname(struct compiler *);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000294
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700295static int compiler_sync_comprehension_generator(
296 struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100297 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +0200298 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700299 expr_ty elt, expr_ty val, int type);
300
301static int compiler_async_comprehension_generator(
302 struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100303 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +0200304 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700305 expr_ty elt, expr_ty val, int type);
306
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000307static int compiler_pattern(struct compiler *, pattern_ty, pattern_context *);
Brandt Bucher145bf262021-02-26 14:51:55 -0800308static int compiler_match(struct compiler *, stmt_ty);
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000309static int compiler_pattern_subpattern(struct compiler *, pattern_ty,
Brandt Bucher145bf262021-02-26 14:51:55 -0800310 pattern_context *);
311
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000312static PyCodeObject *assemble(struct compiler *, int addNone);
Mark Shannon332cd5e2018-01-30 00:41:04 +0000313static PyObject *__doc__, *__annotations__;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000314
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400315#define CAPSULE_NAME "compile.c compiler unit"
Benjamin Petersonb173f782009-05-05 22:31:58 +0000316
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000317PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000318_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 /* Name mangling: __private becomes _classname__private.
321 This is independent from how the name is used. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200322 PyObject *result;
323 size_t nlen, plen, ipriv;
324 Py_UCS4 maxchar;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200326 PyUnicode_READ_CHAR(ident, 0) != '_' ||
327 PyUnicode_READ_CHAR(ident, 1) != '_') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 Py_INCREF(ident);
329 return ident;
330 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200331 nlen = PyUnicode_GET_LENGTH(ident);
332 plen = PyUnicode_GET_LENGTH(privateobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 /* Don't mangle __id__ or names with dots.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 The only time a name with a dot can occur is when
336 we are compiling an import statement that has a
337 package name.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 TODO(jhylton): Decide whether we want to support
340 mangling of the module name, e.g. __M.X.
341 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200342 if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
343 PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
344 PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 Py_INCREF(ident);
346 return ident; /* Don't mangle __whatever__ */
347 }
348 /* Strip leading underscores from class name */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200349 ipriv = 0;
350 while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
351 ipriv++;
352 if (ipriv == plen) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 Py_INCREF(ident);
354 return ident; /* Don't mangle if class is just underscores */
355 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200356 plen -= ipriv;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000357
Antoine Pitrou55bff892013-04-06 21:21:04 +0200358 if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
359 PyErr_SetString(PyExc_OverflowError,
360 "private identifier too large to be mangled");
361 return NULL;
362 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000363
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200364 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
365 if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
366 maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
367
368 result = PyUnicode_New(1 + nlen + plen, maxchar);
369 if (!result)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200371 /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
372 PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
Victor Stinner6c7a52a2011-09-28 21:39:17 +0200373 if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
374 Py_DECREF(result);
375 return NULL;
376 }
377 if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
378 Py_DECREF(result);
379 return NULL;
380 }
Victor Stinner8f825062012-04-27 13:55:39 +0200381 assert(_PyUnicode_CheckConsistency(result, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200382 return result;
Michael W. Hudson60934622004-08-12 17:56:29 +0000383}
384
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000385static int
386compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000387{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000389
INADA Naokic2e16072018-11-26 21:23:22 +0900390 c->c_const_cache = PyDict_New();
391 if (!c->c_const_cache) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 return 0;
INADA Naokic2e16072018-11-26 21:23:22 +0900393 }
394
395 c->c_stack = PyList_New(0);
396 if (!c->c_stack) {
397 Py_CLEAR(c->c_const_cache);
398 return 0;
399 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000402}
403
404PyCodeObject *
Victor Stinnera81fca62021-03-24 00:51:50 +0100405_PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
406 int optimize, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 struct compiler c;
409 PyCodeObject *co = NULL;
Victor Stinner37d66d72019-06-13 02:16:41 +0200410 PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 if (!__doc__) {
414 __doc__ = PyUnicode_InternFromString("__doc__");
415 if (!__doc__)
416 return NULL;
417 }
Mark Shannon332cd5e2018-01-30 00:41:04 +0000418 if (!__annotations__) {
419 __annotations__ = PyUnicode_InternFromString("__annotations__");
420 if (!__annotations__)
421 return NULL;
422 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 if (!compiler_init(&c))
424 return NULL;
Victor Stinner14e461d2013-08-26 22:28:21 +0200425 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 c.c_filename = filename;
427 c.c_arena = arena;
Victor Stinnera81fca62021-03-24 00:51:50 +0100428 c.c_future = _PyFuture_FromAST(mod, filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 if (c.c_future == NULL)
430 goto finally;
431 if (!flags) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 flags = &local_flags;
433 }
434 merged = c.c_future->ff_features | flags->cf_flags;
435 c.c_future->ff_features = merged;
436 flags->cf_flags = merged;
437 c.c_flags = flags;
Victor Stinnerda7933e2020-04-13 03:04:28 +0200438 c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000440
Pablo Galindod112c602020-03-18 23:02:09 +0000441 _PyASTOptimizeState state;
442 state.optimize = c.c_optimize;
443 state.ff_features = merged;
444
445 if (!_PyAST_Optimize(mod, arena, &state)) {
INADA Naoki7ea143a2017-12-14 16:47:20 +0900446 goto finally;
447 }
448
Victor Stinner28ad12f2021-03-19 12:41:49 +0100449 c.c_st = _PySymtable_Build(mod, filename, c.c_future);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 if (c.c_st == NULL) {
451 if (!PyErr_Occurred())
452 PyErr_SetString(PyExc_SystemError, "no symtable");
453 goto finally;
454 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000457
Thomas Wouters1175c432006-02-27 22:49:54 +0000458 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 compiler_free(&c);
460 assert(co || PyErr_Occurred());
461 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000462}
463
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000464static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000465compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 if (c->c_st)
Victor Stinner28ad12f2021-03-19 12:41:49 +0100468 _PySymtable_Free(c->c_st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 if (c->c_future)
470 PyObject_Free(c->c_future);
Victor Stinner14e461d2013-08-26 22:28:21 +0200471 Py_XDECREF(c->c_filename);
INADA Naokic2e16072018-11-26 21:23:22 +0900472 Py_DECREF(c->c_const_cache);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000474}
475
Guido van Rossum79f25d91997-04-29 20:08:16 +0000476static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000477list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000478{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 Py_ssize_t i, n;
480 PyObject *v, *k;
481 PyObject *dict = PyDict_New();
482 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 n = PyList_Size(list);
485 for (i = 0; i < n; i++) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100486 v = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 if (!v) {
488 Py_DECREF(dict);
489 return NULL;
490 }
491 k = PyList_GET_ITEM(list, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300492 if (PyDict_SetItem(dict, k, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 Py_DECREF(v);
494 Py_DECREF(dict);
495 return NULL;
496 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 Py_DECREF(v);
498 }
499 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000500}
501
502/* Return new dict containing names from src that match scope(s).
503
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000504src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000506values are integers, starting at offset and increasing by one for
507each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000508*/
509
510static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +0100511dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000512{
Benjamin Peterson51ab2832012-07-18 15:12:47 -0700513 Py_ssize_t i = offset, scope, num_keys, key_i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 PyObject *k, *v, *dest = PyDict_New();
Meador Inge2ca63152012-07-18 14:20:11 -0500515 PyObject *sorted_keys;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 assert(offset >= 0);
518 if (dest == NULL)
519 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000520
Meador Inge2ca63152012-07-18 14:20:11 -0500521 /* Sort the keys so that we have a deterministic order on the indexes
522 saved in the returned dictionary. These indexes are used as indexes
523 into the free and cell var storage. Therefore if they aren't
524 deterministic, then the generated bytecode is not deterministic.
525 */
526 sorted_keys = PyDict_Keys(src);
527 if (sorted_keys == NULL)
528 return NULL;
529 if (PyList_Sort(sorted_keys) != 0) {
530 Py_DECREF(sorted_keys);
531 return NULL;
532 }
Meador Ingef69e24e2012-07-18 16:41:03 -0500533 num_keys = PyList_GET_SIZE(sorted_keys);
Meador Inge2ca63152012-07-18 14:20:11 -0500534
535 for (key_i = 0; key_i < num_keys; key_i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 /* XXX this should probably be a macro in symtable.h */
537 long vi;
Meador Inge2ca63152012-07-18 14:20:11 -0500538 k = PyList_GET_ITEM(sorted_keys, key_i);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200539 v = PyDict_GetItemWithError(src, k);
540 assert(v && PyLong_Check(v));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 vi = PyLong_AS_LONG(v);
542 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 if (scope == scope_type || vi & flag) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300545 PyObject *item = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 if (item == NULL) {
Meador Inge2ca63152012-07-18 14:20:11 -0500547 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 Py_DECREF(dest);
549 return NULL;
550 }
551 i++;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300552 if (PyDict_SetItem(dest, k, item) < 0) {
Meador Inge2ca63152012-07-18 14:20:11 -0500553 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 Py_DECREF(item);
555 Py_DECREF(dest);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 return NULL;
557 }
558 Py_DECREF(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 }
560 }
Meador Inge2ca63152012-07-18 14:20:11 -0500561 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000563}
564
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000565static void
566compiler_unit_check(struct compiler_unit *u)
567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 basicblock *block;
569 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +0100570 assert(!_PyMem_IsPtrFreed(block));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 if (block->b_instr != NULL) {
572 assert(block->b_ialloc > 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +0100573 assert(block->b_iused >= 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 assert(block->b_ialloc >= block->b_iused);
575 }
576 else {
577 assert (block->b_iused == 0);
578 assert (block->b_ialloc == 0);
579 }
580 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581}
582
583static void
584compiler_unit_free(struct compiler_unit *u)
585{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 compiler_unit_check(u);
589 b = u->u_blocks;
590 while (b != NULL) {
591 if (b->b_instr)
592 PyObject_Free((void *)b->b_instr);
593 next = b->b_list;
594 PyObject_Free((void *)b);
595 b = next;
596 }
597 Py_CLEAR(u->u_ste);
598 Py_CLEAR(u->u_name);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400599 Py_CLEAR(u->u_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 Py_CLEAR(u->u_consts);
601 Py_CLEAR(u->u_names);
602 Py_CLEAR(u->u_varnames);
603 Py_CLEAR(u->u_freevars);
604 Py_CLEAR(u->u_cellvars);
605 Py_CLEAR(u->u_private);
606 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000607}
608
609static int
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100610compiler_enter_scope(struct compiler *c, identifier name,
611 int scope_type, void *key, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 struct compiler_unit *u;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100614 basicblock *block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000615
Andy Lester7668a8b2020-03-24 23:26:44 -0500616 u = (struct compiler_unit *)PyObject_Calloc(1, sizeof(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 struct compiler_unit));
618 if (!u) {
619 PyErr_NoMemory();
620 return 0;
621 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100622 u->u_scope_type = scope_type;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 u->u_argcount = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100624 u->u_posonlyargcount = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 u->u_kwonlyargcount = 0;
626 u->u_ste = PySymtable_Lookup(c->c_st, key);
627 if (!u->u_ste) {
628 compiler_unit_free(u);
629 return 0;
630 }
631 Py_INCREF(name);
632 u->u_name = name;
633 u->u_varnames = list2dict(u->u_ste->ste_varnames);
634 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
635 if (!u->u_varnames || !u->u_cellvars) {
636 compiler_unit_free(u);
637 return 0;
638 }
Benjamin Peterson312595c2013-05-15 15:26:42 -0500639 if (u->u_ste->ste_needs_class_closure) {
Martin Panter7462b6492015-11-02 03:37:02 +0000640 /* Cook up an implicit __class__ cell. */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500641 _Py_IDENTIFIER(__class__);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300642 PyObject *name;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500643 int res;
644 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200645 assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500646 name = _PyUnicode_FromId(&PyId___class__);
647 if (!name) {
648 compiler_unit_free(u);
649 return 0;
650 }
Victor Stinnerc9bc2902020-10-27 02:24:34 +0100651 res = PyDict_SetItem(u->u_cellvars, name, _PyLong_GetZero());
Benjamin Peterson312595c2013-05-15 15:26:42 -0500652 if (res < 0) {
653 compiler_unit_free(u);
654 return 0;
655 }
656 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200659 PyDict_GET_SIZE(u->u_cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 if (!u->u_freevars) {
661 compiler_unit_free(u);
662 return 0;
663 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 u->u_blocks = NULL;
666 u->u_nfblocks = 0;
667 u->u_firstlineno = lineno;
668 u->u_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000669 u->u_col_offset = 0;
Pablo Galindoa77aac42021-04-23 14:27:05 +0100670 u->u_end_lineno = 0;
671 u->u_end_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 u->u_consts = PyDict_New();
673 if (!u->u_consts) {
674 compiler_unit_free(u);
675 return 0;
676 }
677 u->u_names = PyDict_New();
678 if (!u->u_names) {
679 compiler_unit_free(u);
680 return 0;
681 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 /* Push the old compiler_unit on the stack. */
686 if (c->u) {
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400687 PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
689 Py_XDECREF(capsule);
690 compiler_unit_free(u);
691 return 0;
692 }
693 Py_DECREF(capsule);
694 u->u_private = c->u->u_private;
695 Py_XINCREF(u->u_private);
696 }
697 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 c->c_nestlevel++;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100700
701 block = compiler_new_block(c);
702 if (block == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 return 0;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100704 c->u->u_curblock = block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000705
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400706 if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
707 if (!compiler_set_qualname(c))
708 return 0;
709 }
710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000712}
713
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000714static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000715compiler_exit_scope(struct compiler *c)
716{
Victor Stinnera6192632021-01-29 16:53:03 +0100717 // Don't call PySequence_DelItem() with an exception raised
718 PyObject *exc_type, *exc_val, *exc_tb;
719 PyErr_Fetch(&exc_type, &exc_val, &exc_tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 c->c_nestlevel--;
722 compiler_unit_free(c->u);
723 /* Restore c->u to the parent unit. */
Victor Stinnera6192632021-01-29 16:53:03 +0100724 Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 if (n >= 0) {
Victor Stinnera6192632021-01-29 16:53:03 +0100726 PyObject *capsule = PyList_GET_ITEM(c->c_stack, n);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400727 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 assert(c->u);
729 /* we are deleting from a list so this really shouldn't fail */
Victor Stinnera6192632021-01-29 16:53:03 +0100730 if (PySequence_DelItem(c->c_stack, n) < 0) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +0100731 _PyErr_WriteUnraisableMsg("on removing the last compiler "
732 "stack item", NULL);
Victor Stinnera6192632021-01-29 16:53:03 +0100733 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 compiler_unit_check(c->u);
735 }
Victor Stinnera6192632021-01-29 16:53:03 +0100736 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 c->u = NULL;
Victor Stinnera6192632021-01-29 16:53:03 +0100738 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000739
Victor Stinnera6192632021-01-29 16:53:03 +0100740 PyErr_Restore(exc_type, exc_val, exc_tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000741}
742
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400743static int
744compiler_set_qualname(struct compiler *c)
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100745{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100746 _Py_static_string(dot, ".");
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400747 _Py_static_string(dot_locals, ".<locals>");
748 Py_ssize_t stack_size;
749 struct compiler_unit *u = c->u;
750 PyObject *name, *base, *dot_str, *dot_locals_str;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100751
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400752 base = NULL;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100753 stack_size = PyList_GET_SIZE(c->c_stack);
Benjamin Petersona8a38b82013-10-19 16:14:39 -0400754 assert(stack_size >= 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400755 if (stack_size > 1) {
756 int scope, force_global = 0;
757 struct compiler_unit *parent;
758 PyObject *mangled, *capsule;
759
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400760 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400761 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400762 assert(parent);
763
Yury Selivanov75445082015-05-11 22:57:16 -0400764 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
765 || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
766 || u->u_scope_type == COMPILER_SCOPE_CLASS) {
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400767 assert(u->u_name);
768 mangled = _Py_Mangle(parent->u_private, u->u_name);
769 if (!mangled)
770 return 0;
Victor Stinner28ad12f2021-03-19 12:41:49 +0100771 scope = _PyST_GetScope(parent->u_ste, mangled);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400772 Py_DECREF(mangled);
773 assert(scope != GLOBAL_IMPLICIT);
774 if (scope == GLOBAL_EXPLICIT)
775 force_global = 1;
776 }
777
778 if (!force_global) {
779 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
Yury Selivanov75445082015-05-11 22:57:16 -0400780 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400781 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
782 dot_locals_str = _PyUnicode_FromId(&dot_locals);
783 if (dot_locals_str == NULL)
784 return 0;
785 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
786 if (base == NULL)
787 return 0;
788 }
789 else {
790 Py_INCREF(parent->u_qualname);
791 base = parent->u_qualname;
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400792 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100793 }
794 }
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400795
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400796 if (base != NULL) {
797 dot_str = _PyUnicode_FromId(&dot);
798 if (dot_str == NULL) {
799 Py_DECREF(base);
800 return 0;
801 }
802 name = PyUnicode_Concat(base, dot_str);
803 Py_DECREF(base);
804 if (name == NULL)
805 return 0;
806 PyUnicode_Append(&name, u->u_name);
807 if (name == NULL)
808 return 0;
809 }
810 else {
811 Py_INCREF(u->u_name);
812 name = u->u_name;
813 }
814 u->u_qualname = name;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100815
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400816 return 1;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100817}
818
Eric V. Smith235a6f02015-09-19 14:51:32 -0400819
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820/* Allocate a new block and return a pointer to it.
821 Returns NULL on error.
822*/
823
824static basicblock *
825compiler_new_block(struct compiler *c)
826{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 basicblock *b;
828 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 u = c->u;
Andy Lester7668a8b2020-03-24 23:26:44 -0500831 b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 if (b == NULL) {
833 PyErr_NoMemory();
834 return NULL;
835 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 /* Extend the singly linked list of blocks with new block. */
837 b->b_list = u->u_blocks;
838 u->u_blocks = b;
839 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840}
841
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842static basicblock *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000843compiler_next_block(struct compiler *c)
844{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 basicblock *block = compiler_new_block(c);
846 if (block == NULL)
847 return NULL;
848 c->u->u_curblock->b_next = block;
849 c->u->u_curblock = block;
850 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000851}
852
853static basicblock *
854compiler_use_next_block(struct compiler *c, basicblock *block)
855{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 assert(block != NULL);
857 c->u->u_curblock->b_next = block;
858 c->u->u_curblock = block;
859 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860}
861
Mark Shannon5977a792020-12-02 13:31:40 +0000862static basicblock *
863compiler_copy_block(struct compiler *c, basicblock *block)
864{
865 /* Cannot copy a block if it has a fallthrough, since
866 * a block can only have one fallthrough predecessor.
867 */
868 assert(block->b_nofallthrough);
Mark Shannon762ef852021-08-09 10:54:48 +0100869 basicblock *result = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +0000870 if (result == NULL) {
871 return NULL;
872 }
873 for (int i = 0; i < block->b_iused; i++) {
874 int n = compiler_next_instr(result);
875 if (n < 0) {
876 return NULL;
877 }
878 result->b_instr[n] = block->b_instr[i];
879 }
880 result->b_exit = block->b_exit;
Mark Shannon3bd60352021-01-13 12:05:43 +0000881 result->b_nofallthrough = 1;
Mark Shannon5977a792020-12-02 13:31:40 +0000882 return result;
883}
884
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000885/* Returns the offset of the next instruction in the current block's
886 b_instr array. Resizes the b_instr as necessary.
887 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000888*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000889
890static int
Andy Lester76d58772020-03-10 21:18:12 -0500891compiler_next_instr(basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 assert(b != NULL);
894 if (b->b_instr == NULL) {
Andy Lester7668a8b2020-03-24 23:26:44 -0500895 b->b_instr = (struct instr *)PyObject_Calloc(
896 DEFAULT_BLOCK_SIZE, sizeof(struct instr));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 if (b->b_instr == NULL) {
898 PyErr_NoMemory();
899 return -1;
900 }
901 b->b_ialloc = DEFAULT_BLOCK_SIZE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 }
903 else if (b->b_iused == b->b_ialloc) {
904 struct instr *tmp;
905 size_t oldsize, newsize;
906 oldsize = b->b_ialloc * sizeof(struct instr);
907 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000908
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -0700909 if (oldsize > (SIZE_MAX >> 1)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 PyErr_NoMemory();
911 return -1;
912 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 if (newsize == 0) {
915 PyErr_NoMemory();
916 return -1;
917 }
918 b->b_ialloc <<= 1;
919 tmp = (struct instr *)PyObject_Realloc(
920 (void *)b->b_instr, newsize);
921 if (tmp == NULL) {
922 PyErr_NoMemory();
923 return -1;
924 }
925 b->b_instr = tmp;
926 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
927 }
928 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929}
930
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200931/* Set the line number and column offset for the following instructions.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000932
Christian Heimes2202f872008-02-06 14:31:34 +0000933 The line number is reset in the following cases:
934 - when entering a new scope
935 - on each statement
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200936 - on each expression and sub-expression
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200937 - before the "except" and "finally" clauses
Thomas Wouters89f507f2006-12-13 04:49:30 +0000938*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000939
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200940#define SET_LOC(c, x) \
941 (c)->u->u_lineno = (x)->lineno; \
Pablo Galindoa77aac42021-04-23 14:27:05 +0100942 (c)->u->u_col_offset = (x)->col_offset; \
943 (c)->u->u_end_lineno = (x)->end_lineno; \
944 (c)->u->u_end_col_offset = (x)->end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000945
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200946/* Return the stack effect of opcode with argument oparg.
947
948 Some opcodes have different stack effect when jump to the target and
949 when not jump. The 'jump' parameter specifies the case:
950
951 * 0 -- when not jump
952 * 1 -- when jump
953 * -1 -- maximal
954 */
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200955static int
956stack_effect(int opcode, int oparg, int jump)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000957{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 switch (opcode) {
Serhiy Storchaka57faf342018-04-25 22:04:06 +0300959 case NOP:
960 case EXTENDED_ARG:
961 return 0;
962
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200963 /* Stack manipulation */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 case POP_TOP:
965 return -1;
966 case ROT_TWO:
967 case ROT_THREE:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200968 case ROT_FOUR:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 return 0;
970 case DUP_TOP:
971 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000972 case DUP_TOP_TWO:
973 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200975 /* Unary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 case UNARY_POSITIVE:
977 case UNARY_NEGATIVE:
978 case UNARY_NOT:
979 case UNARY_INVERT:
980 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 case SET_ADD:
983 case LIST_APPEND:
984 return -1;
985 case MAP_ADD:
986 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000987
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200988 /* Binary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 case BINARY_POWER:
990 case BINARY_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400991 case BINARY_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 case BINARY_MODULO:
993 case BINARY_ADD:
994 case BINARY_SUBTRACT:
995 case BINARY_SUBSCR:
996 case BINARY_FLOOR_DIVIDE:
997 case BINARY_TRUE_DIVIDE:
998 return -1;
999 case INPLACE_FLOOR_DIVIDE:
1000 case INPLACE_TRUE_DIVIDE:
1001 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 case INPLACE_ADD:
1004 case INPLACE_SUBTRACT:
1005 case INPLACE_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -04001006 case INPLACE_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 case INPLACE_MODULO:
1008 return -1;
1009 case STORE_SUBSCR:
1010 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 case DELETE_SUBSCR:
1012 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 case BINARY_LSHIFT:
1015 case BINARY_RSHIFT:
1016 case BINARY_AND:
1017 case BINARY_XOR:
1018 case BINARY_OR:
1019 return -1;
1020 case INPLACE_POWER:
1021 return -1;
1022 case GET_ITER:
1023 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 case PRINT_EXPR:
1026 return -1;
1027 case LOAD_BUILD_CLASS:
1028 return 1;
1029 case INPLACE_LSHIFT:
1030 case INPLACE_RSHIFT:
1031 case INPLACE_AND:
1032 case INPLACE_XOR:
1033 case INPLACE_OR:
1034 return -1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 case SETUP_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001037 /* 1 in the normal flow.
1038 * Restore the stack position and push 6 values before jumping to
1039 * the handler if an exception be raised. */
1040 return jump ? 6 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 case RETURN_VALUE:
1042 return -1;
1043 case IMPORT_STAR:
1044 return -1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001045 case SETUP_ANNOTATIONS:
1046 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 case YIELD_VALUE:
1048 return 0;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001049 case YIELD_FROM:
1050 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 case POP_BLOCK:
1052 return 0;
1053 case POP_EXCEPT:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001054 return -3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 case STORE_NAME:
1057 return -1;
1058 case DELETE_NAME:
1059 return 0;
1060 case UNPACK_SEQUENCE:
1061 return oparg-1;
1062 case UNPACK_EX:
1063 return (oparg&0xFF) + (oparg>>8);
1064 case FOR_ITER:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001065 /* -1 at end of iterator, 1 if continue iterating. */
1066 return jump > 0 ? -1 : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 case STORE_ATTR:
1069 return -2;
1070 case DELETE_ATTR:
1071 return -1;
1072 case STORE_GLOBAL:
1073 return -1;
1074 case DELETE_GLOBAL:
1075 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 case LOAD_CONST:
1077 return 1;
1078 case LOAD_NAME:
1079 return 1;
1080 case BUILD_TUPLE:
1081 case BUILD_LIST:
1082 case BUILD_SET:
Serhiy Storchakaea525a22016-09-06 22:07:53 +03001083 case BUILD_STRING:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 return 1-oparg;
1085 case BUILD_MAP:
Benjamin Petersonb6855152015-09-10 21:02:39 -07001086 return 1 - 2*oparg;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001087 case BUILD_CONST_KEY_MAP:
1088 return -oparg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 case LOAD_ATTR:
1090 return 0;
1091 case COMPARE_OP:
Mark Shannon9af0e472020-01-14 10:12:45 +00001092 case IS_OP:
1093 case CONTAINS_OP:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 return -1;
Mark Shannon9af0e472020-01-14 10:12:45 +00001095 case JUMP_IF_NOT_EXC_MATCH:
1096 return -2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 case IMPORT_NAME:
1098 return -1;
1099 case IMPORT_FROM:
1100 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001101
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001102 /* Jumps */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 case JUMP_FORWARD:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 case JUMP_ABSOLUTE:
1105 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001107 case JUMP_IF_TRUE_OR_POP:
1108 case JUMP_IF_FALSE_OR_POP:
1109 return jump ? 0 : -1;
1110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 case POP_JUMP_IF_FALSE:
1112 case POP_JUMP_IF_TRUE:
1113 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 case LOAD_GLOBAL:
1116 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001117
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001118 /* Exception handling */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 case SETUP_FINALLY:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001120 /* 0 in the normal flow.
1121 * Restore the stack position and push 6 values before jumping to
1122 * the handler if an exception be raised. */
1123 return jump ? 6 : 0;
Mark Shannonfee55262019-11-21 09:11:43 +00001124 case RERAISE:
1125 return -3;
1126
1127 case WITH_EXCEPT_START:
1128 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 case LOAD_FAST:
1131 return 1;
1132 case STORE_FAST:
1133 return -1;
1134 case DELETE_FAST:
1135 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 case RAISE_VARARGS:
1138 return -oparg;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001139
1140 /* Functions and calls */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 case CALL_FUNCTION:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001142 return -oparg;
Yury Selivanovf2392132016-12-13 19:03:51 -05001143 case CALL_METHOD:
1144 return -oparg-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 case CALL_FUNCTION_KW:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001146 return -oparg-1;
1147 case CALL_FUNCTION_EX:
Matthieu Dartiailh3a9ac822017-02-21 14:25:22 +01001148 return -1 - ((oparg & 0x01) != 0);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001149 case MAKE_FUNCTION:
1150 return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1151 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 case BUILD_SLICE:
1153 if (oparg == 3)
1154 return -2;
1155 else
1156 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001158 /* Closures */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 case LOAD_CLOSURE:
1160 return 1;
1161 case LOAD_DEREF:
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001162 case LOAD_CLASSDEREF:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 return 1;
1164 case STORE_DEREF:
1165 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001166 case DELETE_DEREF:
1167 return 0;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001168
1169 /* Iterators and generators */
Yury Selivanov75445082015-05-11 22:57:16 -04001170 case GET_AWAITABLE:
1171 return 0;
1172 case SETUP_ASYNC_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001173 /* 0 in the normal flow.
1174 * Restore the stack position to the position before the result
1175 * of __aenter__ and push 6 values before jumping to the handler
1176 * if an exception be raised. */
1177 return jump ? -1 + 6 : 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001178 case BEFORE_ASYNC_WITH:
1179 return 1;
1180 case GET_AITER:
1181 return 0;
1182 case GET_ANEXT:
1183 return 1;
Yury Selivanov5376ba92015-06-22 12:19:30 -04001184 case GET_YIELD_FROM_ITER:
1185 return 0;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02001186 case END_ASYNC_FOR:
1187 return -7;
Eric V. Smitha78c7952015-11-03 12:45:05 -05001188 case FORMAT_VALUE:
1189 /* If there's a fmt_spec on the stack, we go from 2->1,
1190 else 1->1. */
1191 return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
Yury Selivanovf2392132016-12-13 19:03:51 -05001192 case LOAD_METHOD:
1193 return 1;
Zackery Spytzce6a0702019-08-25 03:44:09 -06001194 case LOAD_ASSERTION_ERROR:
1195 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001196 case LIST_TO_TUPLE:
1197 return 0;
Mark Shannonb37181e2021-04-06 11:48:59 +01001198 case GEN_START:
1199 return -1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001200 case LIST_EXTEND:
1201 case SET_UPDATE:
Mark Shannon8a4cd702020-01-27 09:57:45 +00001202 case DICT_MERGE:
1203 case DICT_UPDATE:
Mark Shannon13bc1392020-01-23 09:25:17 +00001204 return -1;
Brandt Bucher145bf262021-02-26 14:51:55 -08001205 case COPY_DICT_WITHOUT_KEYS:
1206 return 0;
1207 case MATCH_CLASS:
1208 return -1;
1209 case GET_LEN:
1210 case MATCH_MAPPING:
1211 case MATCH_SEQUENCE:
1212 return 1;
1213 case MATCH_KEYS:
1214 return 2;
Brandt Bucher0ad1e032021-05-02 13:02:10 -07001215 case ROT_N:
1216 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 default:
Larry Hastings3a907972013-11-23 14:49:22 -08001218 return PY_INVALID_STACK_EFFECT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 }
Larry Hastings3a907972013-11-23 14:49:22 -08001220 return PY_INVALID_STACK_EFFECT; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221}
1222
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001223int
Serhiy Storchaka7bdf2822018-09-18 09:54:26 +03001224PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump)
1225{
1226 return stack_effect(opcode, oparg, jump);
1227}
1228
1229int
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001230PyCompile_OpcodeStackEffect(int opcode, int oparg)
1231{
1232 return stack_effect(opcode, oparg, -1);
1233}
1234
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235/* Add an opcode with no argument.
1236 Returns 0 on failure, 1 on success.
1237*/
1238
1239static int
Mark Shannon3bd60352021-01-13 12:05:43 +00001240compiler_addop_line(struct compiler *c, int opcode, int line)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 basicblock *b;
1243 struct instr *i;
1244 int off;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001245 assert(!HAS_ARG(opcode));
Andy Lester76d58772020-03-10 21:18:12 -05001246 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 if (off < 0)
1248 return 0;
1249 b = c->u->u_curblock;
1250 i = &b->b_instr[off];
1251 i->i_opcode = opcode;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001252 i->i_oparg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 if (opcode == RETURN_VALUE)
1254 b->b_return = 1;
Mark Shannon3bd60352021-01-13 12:05:43 +00001255 i->i_lineno = line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257}
1258
Mark Shannon3bd60352021-01-13 12:05:43 +00001259static int
1260compiler_addop(struct compiler *c, int opcode)
1261{
1262 return compiler_addop_line(c, opcode, c->u->u_lineno);
1263}
1264
1265static int
1266compiler_addop_noline(struct compiler *c, int opcode)
1267{
1268 return compiler_addop_line(c, opcode, -1);
1269}
1270
1271
Victor Stinnerf8e32212013-11-19 23:56:34 +01001272static Py_ssize_t
Andy Lester76d58772020-03-10 21:18:12 -05001273compiler_add_o(PyObject *dict, PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001275 PyObject *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001277
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001278 v = PyDict_GetItemWithError(dict, o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 if (!v) {
Stefan Krahc0cbed12015-07-27 12:56:49 +02001280 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 return -1;
Stefan Krahc0cbed12015-07-27 12:56:49 +02001282 }
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001283 arg = PyDict_GET_SIZE(dict);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001284 v = PyLong_FromSsize_t(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 if (!v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 return -1;
1287 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001288 if (PyDict_SetItem(dict, o, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 Py_DECREF(v);
1290 return -1;
1291 }
1292 Py_DECREF(v);
1293 }
1294 else
1295 arg = PyLong_AsLong(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001296 return arg;
1297}
1298
INADA Naokic2e16072018-11-26 21:23:22 +09001299// Merge const *o* recursively and return constant key object.
1300static PyObject*
1301merge_consts_recursive(struct compiler *c, PyObject *o)
1302{
1303 // None and Ellipsis are singleton, and key is the singleton.
1304 // No need to merge object and key.
1305 if (o == Py_None || o == Py_Ellipsis) {
1306 Py_INCREF(o);
1307 return o;
1308 }
1309
1310 PyObject *key = _PyCode_ConstantKey(o);
1311 if (key == NULL) {
1312 return NULL;
1313 }
1314
1315 // t is borrowed reference
1316 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
1317 if (t != key) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001318 // o is registered in c_const_cache. Just use it.
Zackery Spytz9b4a1b12019-03-20 03:16:25 -06001319 Py_XINCREF(t);
INADA Naokic2e16072018-11-26 21:23:22 +09001320 Py_DECREF(key);
1321 return t;
1322 }
1323
INADA Naokif7e4d362018-11-29 00:58:46 +09001324 // We registered o in c_const_cache.
Simeon63b5fc52019-04-09 19:36:57 -04001325 // When o is a tuple or frozenset, we want to merge its
INADA Naokif7e4d362018-11-29 00:58:46 +09001326 // items too.
INADA Naokic2e16072018-11-26 21:23:22 +09001327 if (PyTuple_CheckExact(o)) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001328 Py_ssize_t len = PyTuple_GET_SIZE(o);
1329 for (Py_ssize_t i = 0; i < len; i++) {
INADA Naokic2e16072018-11-26 21:23:22 +09001330 PyObject *item = PyTuple_GET_ITEM(o, i);
1331 PyObject *u = merge_consts_recursive(c, item);
1332 if (u == NULL) {
1333 Py_DECREF(key);
1334 return NULL;
1335 }
1336
1337 // See _PyCode_ConstantKey()
1338 PyObject *v; // borrowed
1339 if (PyTuple_CheckExact(u)) {
1340 v = PyTuple_GET_ITEM(u, 1);
1341 }
1342 else {
1343 v = u;
1344 }
1345 if (v != item) {
1346 Py_INCREF(v);
1347 PyTuple_SET_ITEM(o, i, v);
1348 Py_DECREF(item);
1349 }
1350
1351 Py_DECREF(u);
1352 }
1353 }
INADA Naokif7e4d362018-11-29 00:58:46 +09001354 else if (PyFrozenSet_CheckExact(o)) {
Simeon63b5fc52019-04-09 19:36:57 -04001355 // *key* is tuple. And its first item is frozenset of
INADA Naokif7e4d362018-11-29 00:58:46 +09001356 // constant keys.
1357 // See _PyCode_ConstantKey() for detail.
1358 assert(PyTuple_CheckExact(key));
1359 assert(PyTuple_GET_SIZE(key) == 2);
1360
1361 Py_ssize_t len = PySet_GET_SIZE(o);
1362 if (len == 0) { // empty frozenset should not be re-created.
1363 return key;
1364 }
1365 PyObject *tuple = PyTuple_New(len);
1366 if (tuple == NULL) {
1367 Py_DECREF(key);
1368 return NULL;
1369 }
1370 Py_ssize_t i = 0, pos = 0;
1371 PyObject *item;
1372 Py_hash_t hash;
1373 while (_PySet_NextEntry(o, &pos, &item, &hash)) {
1374 PyObject *k = merge_consts_recursive(c, item);
1375 if (k == NULL) {
1376 Py_DECREF(tuple);
1377 Py_DECREF(key);
1378 return NULL;
1379 }
1380 PyObject *u;
1381 if (PyTuple_CheckExact(k)) {
1382 u = PyTuple_GET_ITEM(k, 1);
1383 Py_INCREF(u);
1384 Py_DECREF(k);
1385 }
1386 else {
1387 u = k;
1388 }
1389 PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u.
1390 i++;
1391 }
1392
1393 // Instead of rewriting o, we create new frozenset and embed in the
1394 // key tuple. Caller should get merged frozenset from the key tuple.
1395 PyObject *new = PyFrozenSet_New(tuple);
1396 Py_DECREF(tuple);
1397 if (new == NULL) {
1398 Py_DECREF(key);
1399 return NULL;
1400 }
1401 assert(PyTuple_GET_ITEM(key, 1) == o);
1402 Py_DECREF(o);
1403 PyTuple_SET_ITEM(key, 1, new);
1404 }
INADA Naokic2e16072018-11-26 21:23:22 +09001405
1406 return key;
1407}
1408
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001409static Py_ssize_t
1410compiler_add_const(struct compiler *c, PyObject *o)
1411{
INADA Naokic2e16072018-11-26 21:23:22 +09001412 PyObject *key = merge_consts_recursive(c, o);
1413 if (key == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001414 return -1;
INADA Naokic2e16072018-11-26 21:23:22 +09001415 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001416
Andy Lester76d58772020-03-10 21:18:12 -05001417 Py_ssize_t arg = compiler_add_o(c->u->u_consts, key);
INADA Naokic2e16072018-11-26 21:23:22 +09001418 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420}
1421
1422static int
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001423compiler_addop_load_const(struct compiler *c, PyObject *o)
1424{
1425 Py_ssize_t arg = compiler_add_const(c, o);
1426 if (arg < 0)
1427 return 0;
1428 return compiler_addop_i(c, LOAD_CONST, arg);
1429}
1430
1431static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001434{
Andy Lester76d58772020-03-10 21:18:12 -05001435 Py_ssize_t arg = compiler_add_o(dict, o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001437 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438 return compiler_addop_i(c, opcode, arg);
1439}
1440
1441static int
1442compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001445 Py_ssize_t arg;
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001446
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1448 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001449 return 0;
Andy Lester76d58772020-03-10 21:18:12 -05001450 arg = compiler_add_o(dict, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451 Py_DECREF(mangled);
1452 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001453 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454 return compiler_addop_i(c, opcode, arg);
1455}
1456
1457/* Add an opcode with an integer argument.
1458 Returns 0 on failure, 1 on success.
1459*/
1460
1461static int
Mark Shannon11e0b292021-04-15 14:28:56 +01001462compiler_addop_i_line(struct compiler *c, int opcode, Py_ssize_t oparg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 struct instr *i;
1465 int off;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001466
Victor Stinner2ad474b2016-03-01 23:34:47 +01001467 /* oparg value is unsigned, but a signed C int is usually used to store
1468 it in the C code (like Python/ceval.c).
1469
1470 Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1471
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001472 The argument of a concrete bytecode instruction is limited to 8-bit.
1473 EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1474 assert(HAS_ARG(opcode));
Victor Stinner2ad474b2016-03-01 23:34:47 +01001475 assert(0 <= oparg && oparg <= 2147483647);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001476
Andy Lester76d58772020-03-10 21:18:12 -05001477 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 if (off < 0)
1479 return 0;
1480 i = &c->u->u_curblock->b_instr[off];
Victor Stinnerf8e32212013-11-19 23:56:34 +01001481 i->i_opcode = opcode;
1482 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
Mark Shannon11e0b292021-04-15 14:28:56 +01001483 i->i_lineno = lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485}
1486
Mark Shannon11e0b292021-04-15 14:28:56 +01001487static int
1488compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
1489{
1490 return compiler_addop_i_line(c, opcode, oparg, c->u->u_lineno);
1491}
1492
1493static int
1494compiler_addop_i_noline(struct compiler *c, int opcode, Py_ssize_t oparg)
1495{
1496 return compiler_addop_i_line(c, opcode, oparg, -1);
1497}
1498
Mark Shannon28b75c82020-12-23 11:43:10 +00001499static int add_jump_to_block(basicblock *b, int opcode, int lineno, basicblock *target)
1500{
1501 assert(HAS_ARG(opcode));
1502 assert(b != NULL);
1503 assert(target != NULL);
1504
1505 int off = compiler_next_instr(b);
1506 struct instr *i = &b->b_instr[off];
1507 if (off < 0) {
1508 return 0;
1509 }
1510 i->i_opcode = opcode;
1511 i->i_target = target;
1512 i->i_lineno = lineno;
1513 return 1;
1514}
1515
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516static int
Mark Shannon582aaf12020-08-04 17:30:11 +01001517compiler_addop_j(struct compiler *c, int opcode, basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001518{
Mark Shannon28b75c82020-12-23 11:43:10 +00001519 return add_jump_to_block(c->u->u_curblock, opcode, c->u->u_lineno, b);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520}
1521
Mark Shannon127dde52021-01-04 18:06:55 +00001522static int
1523compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b)
1524{
1525 return add_jump_to_block(c->u->u_curblock, opcode, -1, b);
1526}
1527
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +01001528/* NEXT_BLOCK() creates an implicit jump from the current block
1529 to the new block.
1530
1531 The returns inside this macro make it impossible to decref objects
1532 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001533*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 if (compiler_next_block((C)) == NULL) \
1536 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001537}
1538
1539#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 if (!compiler_addop((C), (OP))) \
1541 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001542}
1543
Mark Shannon3bd60352021-01-13 12:05:43 +00001544#define ADDOP_NOLINE(C, OP) { \
1545 if (!compiler_addop_noline((C), (OP))) \
1546 return 0; \
1547}
1548
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001549#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 if (!compiler_addop((C), (OP))) { \
1551 compiler_exit_scope(c); \
1552 return 0; \
1553 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001554}
1555
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001556#define ADDOP_LOAD_CONST(C, O) { \
1557 if (!compiler_addop_load_const((C), (O))) \
1558 return 0; \
1559}
1560
1561/* Same as ADDOP_LOAD_CONST, but steals a reference. */
1562#define ADDOP_LOAD_CONST_NEW(C, O) { \
1563 PyObject *__new_const = (O); \
1564 if (__new_const == NULL) { \
1565 return 0; \
1566 } \
1567 if (!compiler_addop_load_const((C), __new_const)) { \
1568 Py_DECREF(__new_const); \
1569 return 0; \
1570 } \
1571 Py_DECREF(__new_const); \
1572}
1573
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001574#define ADDOP_O(C, OP, O, TYPE) { \
Miss Islington (bot)ebbd0ac2021-08-31 11:08:32 -07001575 assert((OP) != LOAD_CONST); /* use ADDOP_LOAD_CONST */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1577 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001578}
1579
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001580/* Same as ADDOP_O, but steals a reference. */
1581#define ADDOP_N(C, OP, O, TYPE) { \
Miss Islington (bot)ebbd0ac2021-08-31 11:08:32 -07001582 assert((OP) != LOAD_CONST); /* use ADDOP_LOAD_CONST_NEW */ \
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001583 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1584 Py_DECREF((O)); \
1585 return 0; \
1586 } \
1587 Py_DECREF((O)); \
1588}
1589
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001590#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1592 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001593}
1594
1595#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 if (!compiler_addop_i((C), (OP), (O))) \
1597 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598}
1599
Mark Shannon11e0b292021-04-15 14:28:56 +01001600#define ADDOP_I_NOLINE(C, OP, O) { \
1601 if (!compiler_addop_i_noline((C), (OP), (O))) \
1602 return 0; \
1603}
1604
Mark Shannon582aaf12020-08-04 17:30:11 +01001605#define ADDOP_JUMP(C, OP, O) { \
1606 if (!compiler_addop_j((C), (OP), (O))) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001608}
1609
Mark Shannon127dde52021-01-04 18:06:55 +00001610/* Add a jump with no line number.
1611 * Used for artificial jumps that have no corresponding
1612 * token in the source code. */
1613#define ADDOP_JUMP_NOLINE(C, OP, O) { \
1614 if (!compiler_addop_j_noline((C), (OP), (O))) \
1615 return 0; \
1616}
1617
Mark Shannon9af0e472020-01-14 10:12:45 +00001618#define ADDOP_COMPARE(C, CMP) { \
1619 if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \
1620 return 0; \
1621}
1622
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001623/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1624 the ASDL name to synthesize the name of the C type and the visit function.
1625*/
1626
1627#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 if (!compiler_visit_ ## TYPE((C), (V))) \
1629 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001630}
1631
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001632#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 if (!compiler_visit_ ## TYPE((C), (V))) { \
1634 compiler_exit_scope(c); \
1635 return 0; \
1636 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001637}
1638
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001639#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 if (!compiler_visit_slice((C), (V), (CTX))) \
1641 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001642}
1643
1644#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001646 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1648 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1649 if (!compiler_visit_ ## TYPE((C), elt)) \
1650 return 0; \
1651 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001652}
1653
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001654#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001656 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1658 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1659 if (!compiler_visit_ ## TYPE((C), elt)) { \
1660 compiler_exit_scope(c); \
1661 return 0; \
1662 } \
1663 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001664}
1665
Brandt Bucher145bf262021-02-26 14:51:55 -08001666#define RETURN_IF_FALSE(X) \
1667 if (!(X)) { \
1668 return 0; \
1669 }
1670
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001671/* Search if variable annotations are present statically in a block. */
1672
1673static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001674find_ann(asdl_stmt_seq *stmts)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001675{
1676 int i, j, res = 0;
1677 stmt_ty st;
1678
1679 for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1680 st = (stmt_ty)asdl_seq_GET(stmts, i);
1681 switch (st->kind) {
1682 case AnnAssign_kind:
1683 return 1;
1684 case For_kind:
1685 res = find_ann(st->v.For.body) ||
1686 find_ann(st->v.For.orelse);
1687 break;
1688 case AsyncFor_kind:
1689 res = find_ann(st->v.AsyncFor.body) ||
1690 find_ann(st->v.AsyncFor.orelse);
1691 break;
1692 case While_kind:
1693 res = find_ann(st->v.While.body) ||
1694 find_ann(st->v.While.orelse);
1695 break;
1696 case If_kind:
1697 res = find_ann(st->v.If.body) ||
1698 find_ann(st->v.If.orelse);
1699 break;
1700 case With_kind:
1701 res = find_ann(st->v.With.body);
1702 break;
1703 case AsyncWith_kind:
1704 res = find_ann(st->v.AsyncWith.body);
1705 break;
1706 case Try_kind:
1707 for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1708 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1709 st->v.Try.handlers, j);
1710 if (find_ann(handler->v.ExceptHandler.body)) {
1711 return 1;
1712 }
1713 }
1714 res = find_ann(st->v.Try.body) ||
1715 find_ann(st->v.Try.finalbody) ||
1716 find_ann(st->v.Try.orelse);
1717 break;
1718 default:
1719 res = 0;
1720 }
1721 if (res) {
1722 break;
1723 }
1724 }
1725 return res;
1726}
1727
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001728/*
1729 * Frame block handling functions
1730 */
1731
1732static int
1733compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b,
Mark Shannonfee55262019-11-21 09:11:43 +00001734 basicblock *exit, void *datum)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001735{
1736 struct fblockinfo *f;
1737 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
Mark Shannon02d126a2020-09-25 14:04:19 +01001738 return compiler_error(c, "too many statically nested blocks");
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001739 }
1740 f = &c->u->u_fblock[c->u->u_nfblocks++];
1741 f->fb_type = t;
1742 f->fb_block = b;
1743 f->fb_exit = exit;
Mark Shannonfee55262019-11-21 09:11:43 +00001744 f->fb_datum = datum;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001745 return 1;
1746}
1747
1748static void
1749compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
1750{
1751 struct compiler_unit *u = c->u;
1752 assert(u->u_nfblocks > 0);
1753 u->u_nfblocks--;
1754 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
1755 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
1756}
1757
Mark Shannonfee55262019-11-21 09:11:43 +00001758static int
1759compiler_call_exit_with_nones(struct compiler *c) {
Miss Islington (bot)ebbd0ac2021-08-31 11:08:32 -07001760 ADDOP_LOAD_CONST(c, Py_None);
Mark Shannonfee55262019-11-21 09:11:43 +00001761 ADDOP(c, DUP_TOP);
1762 ADDOP(c, DUP_TOP);
1763 ADDOP_I(c, CALL_FUNCTION, 3);
1764 return 1;
1765}
1766
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001767/* Unwind a frame block. If preserve_tos is true, the TOS before
Mark Shannonfee55262019-11-21 09:11:43 +00001768 * popping the blocks will be restored afterwards, unless another
1769 * return, break or continue is found. In which case, the TOS will
1770 * be popped.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001771 */
1772static int
1773compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
1774 int preserve_tos)
1775{
1776 switch (info->fb_type) {
1777 case WHILE_LOOP:
Mark Shannon02d126a2020-09-25 14:04:19 +01001778 case EXCEPTION_HANDLER:
tomKPZ7a7ba3d2021-04-07 07:43:45 -07001779 case ASYNC_COMPREHENSION_GENERATOR:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001780 return 1;
1781
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001782 case FOR_LOOP:
1783 /* Pop the iterator */
1784 if (preserve_tos) {
1785 ADDOP(c, ROT_TWO);
1786 }
1787 ADDOP(c, POP_TOP);
1788 return 1;
1789
Mark Shannon02d126a2020-09-25 14:04:19 +01001790 case TRY_EXCEPT:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001791 ADDOP(c, POP_BLOCK);
1792 return 1;
1793
1794 case FINALLY_TRY:
Mark Shannon5274b682020-12-16 13:07:01 +00001795 /* This POP_BLOCK gets the line number of the unwinding statement */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001796 ADDOP(c, POP_BLOCK);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001797 if (preserve_tos) {
Mark Shannonfee55262019-11-21 09:11:43 +00001798 if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) {
1799 return 0;
1800 }
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001801 }
Mark Shannon5274b682020-12-16 13:07:01 +00001802 /* Emit the finally block */
Mark Shannonfee55262019-11-21 09:11:43 +00001803 VISIT_SEQ(c, stmt, info->fb_datum);
1804 if (preserve_tos) {
1805 compiler_pop_fblock(c, POP_VALUE, NULL);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001806 }
Mark Shannon5274b682020-12-16 13:07:01 +00001807 /* The finally block should appear to execute after the
1808 * statement causing the unwinding, so make the unwinding
1809 * instruction artificial */
1810 c->u->u_lineno = -1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001811 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001812
Mark Shannonfee55262019-11-21 09:11:43 +00001813 case FINALLY_END:
1814 if (preserve_tos) {
1815 ADDOP(c, ROT_FOUR);
1816 }
1817 ADDOP(c, POP_TOP);
1818 ADDOP(c, POP_TOP);
1819 ADDOP(c, POP_TOP);
1820 if (preserve_tos) {
1821 ADDOP(c, ROT_FOUR);
1822 }
1823 ADDOP(c, POP_EXCEPT);
1824 return 1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001825
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001826 case WITH:
1827 case ASYNC_WITH:
Mark Shannon5979e812021-04-30 14:32:47 +01001828 SET_LOC(c, (stmt_ty)info->fb_datum);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001829 ADDOP(c, POP_BLOCK);
1830 if (preserve_tos) {
1831 ADDOP(c, ROT_TWO);
1832 }
Mark Shannonfee55262019-11-21 09:11:43 +00001833 if(!compiler_call_exit_with_nones(c)) {
1834 return 0;
1835 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001836 if (info->fb_type == ASYNC_WITH) {
1837 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001838 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001839 ADDOP(c, YIELD_FROM);
1840 }
Mark Shannonfee55262019-11-21 09:11:43 +00001841 ADDOP(c, POP_TOP);
Mark Shannoncea05852021-06-03 19:57:31 +01001842 /* The exit block should appear to execute after the
1843 * statement causing the unwinding, so make the unwinding
1844 * instruction artificial */
1845 c->u->u_lineno = -1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001846 return 1;
1847
1848 case HANDLER_CLEANUP:
Mark Shannonfee55262019-11-21 09:11:43 +00001849 if (info->fb_datum) {
1850 ADDOP(c, POP_BLOCK);
1851 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001852 if (preserve_tos) {
1853 ADDOP(c, ROT_FOUR);
1854 }
Mark Shannonfee55262019-11-21 09:11:43 +00001855 ADDOP(c, POP_EXCEPT);
1856 if (info->fb_datum) {
1857 ADDOP_LOAD_CONST(c, Py_None);
1858 compiler_nameop(c, info->fb_datum, Store);
1859 compiler_nameop(c, info->fb_datum, Del);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001860 }
Mark Shannonfee55262019-11-21 09:11:43 +00001861 return 1;
1862
1863 case POP_VALUE:
1864 if (preserve_tos) {
1865 ADDOP(c, ROT_TWO);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001866 }
Mark Shannonfee55262019-11-21 09:11:43 +00001867 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001868 return 1;
1869 }
1870 Py_UNREACHABLE();
1871}
1872
Mark Shannonfee55262019-11-21 09:11:43 +00001873/** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */
1874static int
1875compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) {
1876 if (c->u->u_nfblocks == 0) {
1877 return 1;
1878 }
1879 struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1];
1880 if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) {
1881 *loop = top;
1882 return 1;
1883 }
1884 struct fblockinfo copy = *top;
1885 c->u->u_nfblocks--;
1886 if (!compiler_unwind_fblock(c, &copy, preserve_tos)) {
1887 return 0;
1888 }
1889 if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) {
1890 return 0;
1891 }
1892 c->u->u_fblock[c->u->u_nfblocks] = copy;
1893 c->u->u_nfblocks++;
1894 return 1;
1895}
1896
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001897/* Compile a sequence of statements, checking for a docstring
1898 and for annotations. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001899
1900static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001901compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001902{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001903 int i = 0;
1904 stmt_ty st;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001905 PyObject *docstring;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001906
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001907 /* Set current line number to the line number of first statement.
1908 This way line number for SETUP_ANNOTATIONS will always
1909 coincide with the line number of first "real" statement in module.
Hansraj Das01171eb2019-10-09 07:54:02 +05301910 If body is empty, then lineno will be set later in assemble. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001911 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) {
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001912 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001913 SET_LOC(c, st);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001914 }
1915 /* Every annotated class and module should have __annotations__. */
1916 if (find_ann(stmts)) {
1917 ADDOP(c, SETUP_ANNOTATIONS);
1918 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001919 if (!asdl_seq_LEN(stmts))
1920 return 1;
INADA Naokicb41b272017-02-23 00:31:59 +09001921 /* if not -OO mode, set docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001922 if (c->c_optimize < 2) {
1923 docstring = _PyAST_GetDocString(stmts);
1924 if (docstring) {
1925 i = 1;
1926 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1927 assert(st->kind == Expr_kind);
1928 VISIT(c, expr, st->v.Expr.value);
1929 if (!compiler_nameop(c, __doc__, Store))
1930 return 0;
1931 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001933 for (; i < asdl_seq_LEN(stmts); i++)
1934 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001936}
1937
1938static PyCodeObject *
1939compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001940{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 PyCodeObject *co;
1942 int addNone = 1;
1943 static PyObject *module;
1944 if (!module) {
1945 module = PyUnicode_InternFromString("<module>");
1946 if (!module)
1947 return NULL;
1948 }
1949 /* Use 0 for firstlineno initially, will fixup in assemble(). */
Mark Shannon877df852020-11-12 09:43:29 +00001950 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 return NULL;
1952 switch (mod->kind) {
1953 case Module_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001954 if (!compiler_body(c, mod->v.Module.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 compiler_exit_scope(c);
1956 return 0;
1957 }
1958 break;
1959 case Interactive_kind:
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001960 if (find_ann(mod->v.Interactive.body)) {
1961 ADDOP(c, SETUP_ANNOTATIONS);
1962 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 c->c_interactive = 1;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001964 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 break;
1966 case Expression_kind:
1967 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1968 addNone = 0;
1969 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 default:
1971 PyErr_Format(PyExc_SystemError,
1972 "module kind %d should not be possible",
1973 mod->kind);
1974 return 0;
1975 }
1976 co = assemble(c, addNone);
1977 compiler_exit_scope(c);
1978 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001979}
1980
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001981/* The test for LOCAL must come before the test for FREE in order to
1982 handle classes where name is both local and free. The local var is
1983 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001984*/
1985
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986static int
1987get_ref_type(struct compiler *c, PyObject *name)
1988{
Victor Stinner0b1bc562013-05-16 22:17:17 +02001989 int scope;
Benjamin Peterson312595c2013-05-15 15:26:42 -05001990 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001991 _PyUnicode_EqualToASCIIString(name, "__class__"))
Benjamin Peterson312595c2013-05-15 15:26:42 -05001992 return CELL;
Victor Stinner28ad12f2021-03-19 12:41:49 +01001993 scope = _PyST_GetScope(c->u->u_ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 if (scope == 0) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001995 PyErr_Format(PyExc_SystemError,
Victor Stinner28ad12f2021-03-19 12:41:49 +01001996 "_PyST_GetScope(name=%R) failed: "
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001997 "unknown scope in unit %S (%R); "
1998 "symbols: %R; locals: %R; globals: %R",
1999 name,
2000 c->u->u_name, c->u->u_ste->ste_id,
2001 c->u->u_ste->ste_symbols, c->u->u_varnames, c->u->u_names);
2002 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005}
2006
2007static int
2008compiler_lookup_arg(PyObject *dict, PyObject *name)
2009{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002010 PyObject *v;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02002011 v = PyDict_GetItemWithError(dict, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002012 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00002013 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00002014 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002015}
2016
2017static int
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002018compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags,
2019 PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002020{
Victor Stinnerad9a0662013-11-19 22:23:20 +01002021 Py_ssize_t i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002022 if (qualname == NULL)
2023 qualname = co->co_name;
2024
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002025 if (free) {
2026 for (i = 0; i < free; ++i) {
2027 /* Bypass com_addop_varname because it will generate
2028 LOAD_DEREF but LOAD_CLOSURE is needed.
2029 */
2030 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002031
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002032 /* Special case: If a class contains a method with a
2033 free variable that has the same name as a method,
2034 the name will be considered free *and* local in the
2035 class. It should be handled by the closure, as
Min ho Kimc4cacc82019-07-31 08:16:13 +10002036 well as by the normal name lookup logic.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002037 */
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002038 int reftype = get_ref_type(c, name);
2039 if (reftype == -1) {
2040 return 0;
2041 }
2042 int arg;
2043 if (reftype == CELL) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002044 arg = compiler_lookup_arg(c->u->u_cellvars, name);
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002045 }
2046 else {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002047 arg = compiler_lookup_arg(c->u->u_freevars, name);
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002048 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002049 if (arg == -1) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002050 PyErr_Format(PyExc_SystemError,
2051 "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; "
2052 "freevars of code %S: %R",
2053 name,
2054 reftype,
2055 c->u->u_name,
2056 co->co_name,
2057 co->co_freevars);
2058 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002059 }
2060 ADDOP_I(c, LOAD_CLOSURE, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002062 flags |= 0x08;
2063 ADDOP_I(c, BUILD_TUPLE, free);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002065 ADDOP_LOAD_CONST(c, (PyObject*)co);
2066 ADDOP_LOAD_CONST(c, qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002067 ADDOP_I(c, MAKE_FUNCTION, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069}
2070
2071static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002072compiler_decorators(struct compiler *c, asdl_expr_seq* decos)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 if (!decos)
2077 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002079 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2080 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
2081 }
2082 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002083}
2084
2085static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002086compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs,
2087 asdl_expr_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00002088{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002089 /* Push a dict of keyword-only default values.
2090
2091 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
2092 */
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002093 int i;
2094 PyObject *keys = NULL;
2095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
2097 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
2098 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
2099 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04002100 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002101 if (!mangled) {
2102 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002104 if (keys == NULL) {
2105 keys = PyList_New(1);
2106 if (keys == NULL) {
2107 Py_DECREF(mangled);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002108 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002109 }
2110 PyList_SET_ITEM(keys, 0, mangled);
2111 }
2112 else {
2113 int res = PyList_Append(keys, mangled);
2114 Py_DECREF(mangled);
2115 if (res == -1) {
2116 goto error;
2117 }
2118 }
2119 if (!compiler_visit_expr(c, default_)) {
2120 goto error;
2121 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 }
2123 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002124 if (keys != NULL) {
2125 Py_ssize_t default_count = PyList_GET_SIZE(keys);
2126 PyObject *keys_tuple = PyList_AsTuple(keys);
2127 Py_DECREF(keys);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002128 ADDOP_LOAD_CONST_NEW(c, keys_tuple);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002129 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002130 assert(default_count > 0);
2131 return 1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002132 }
2133 else {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002134 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002135 }
2136
2137error:
2138 Py_XDECREF(keys);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002139 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002140}
2141
2142static int
Guido van Rossum95e4d582018-01-26 08:20:18 -08002143compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
2144{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +03002145 ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation));
Guido van Rossum95e4d582018-01-26 08:20:18 -08002146 return 1;
2147}
2148
2149static int
Neal Norwitzc1505362006-12-28 06:47:50 +00002150compiler_visit_argannotation(struct compiler *c, identifier id,
Yurii Karabas73019792020-11-25 12:43:18 +02002151 expr_ty annotation, Py_ssize_t *annotations_len)
Neal Norwitzc1505362006-12-28 06:47:50 +00002152{
Pablo Galindob0544ba2021-04-21 12:41:19 +01002153 if (!annotation) {
2154 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 }
Pablo Galindob0544ba2021-04-21 12:41:19 +01002156
2157 PyObject *mangled = _Py_Mangle(c->u->u_private, id);
2158 if (!mangled) {
2159 return 0;
2160 }
2161 ADDOP_LOAD_CONST(c, mangled);
2162 Py_DECREF(mangled);
2163
2164 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
2165 VISIT(c, annexpr, annotation)
2166 }
2167 else {
2168 VISIT(c, expr, annotation);
2169 }
2170 *annotations_len += 2;
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002171 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002172}
2173
2174static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002175compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args,
Yurii Karabas73019792020-11-25 12:43:18 +02002176 Py_ssize_t *annotations_len)
Neal Norwitzc1505362006-12-28 06:47:50 +00002177{
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002178 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 for (i = 0; i < asdl_seq_LEN(args); i++) {
2180 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002181 if (!compiler_visit_argannotation(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 c,
2183 arg->arg,
2184 arg->annotation,
Yurii Karabas73019792020-11-25 12:43:18 +02002185 annotations_len))
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002186 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002188 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002189}
2190
2191static int
2192compiler_visit_annotations(struct compiler *c, arguments_ty args,
2193 expr_ty returns)
2194{
Yurii Karabas73019792020-11-25 12:43:18 +02002195 /* Push arg annotation names and values.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002196 The expressions are evaluated out-of-order wrt the source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00002197
Yurii Karabas73019792020-11-25 12:43:18 +02002198 Return 0 on error, -1 if no annotations pushed, 1 if a annotations is pushed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 */
2200 static identifier return_str;
Yurii Karabas73019792020-11-25 12:43:18 +02002201 Py_ssize_t annotations_len = 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002202
Yurii Karabas73019792020-11-25 12:43:18 +02002203 if (!compiler_visit_argannotations(c, args->args, &annotations_len))
2204 return 0;
2205 if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len))
2206 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002207 if (args->vararg && args->vararg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002208 !compiler_visit_argannotation(c, args->vararg->arg,
Yurii Karabas73019792020-11-25 12:43:18 +02002209 args->vararg->annotation, &annotations_len))
2210 return 0;
2211 if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len))
2212 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002213 if (args->kwarg && args->kwarg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002214 !compiler_visit_argannotation(c, args->kwarg->arg,
Yurii Karabas73019792020-11-25 12:43:18 +02002215 args->kwarg->annotation, &annotations_len))
2216 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 if (!return_str) {
2219 return_str = PyUnicode_InternFromString("return");
2220 if (!return_str)
Yurii Karabas73019792020-11-25 12:43:18 +02002221 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 }
Yurii Karabas73019792020-11-25 12:43:18 +02002223 if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) {
2224 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 }
2226
Yurii Karabas73019792020-11-25 12:43:18 +02002227 if (annotations_len) {
2228 ADDOP_I(c, BUILD_TUPLE, annotations_len);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002229 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002231
Yurii Karabas73019792020-11-25 12:43:18 +02002232 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002233}
2234
2235static int
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002236compiler_visit_defaults(struct compiler *c, arguments_ty args)
2237{
2238 VISIT_SEQ(c, expr, args->defaults);
2239 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
2240 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002241}
2242
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002243static Py_ssize_t
2244compiler_default_arguments(struct compiler *c, arguments_ty args)
2245{
2246 Py_ssize_t funcflags = 0;
2247 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002248 if (!compiler_visit_defaults(c, args))
2249 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002250 funcflags |= 0x01;
2251 }
2252 if (args->kwonlyargs) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002253 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002254 args->kw_defaults);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002255 if (res == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002256 return -1;
2257 }
2258 else if (res > 0) {
2259 funcflags |= 0x02;
2260 }
2261 }
2262 return funcflags;
2263}
2264
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002265static int
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002266forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx)
2267{
2268
2269 if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2270 compiler_error(c, "cannot assign to __debug__");
2271 return 1;
2272 }
Dong-hee Na32c1caa2021-08-26 09:52:21 +00002273 if (ctx == Del && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2274 compiler_error(c, "cannot delete __debug__");
2275 return 1;
2276 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002277 return 0;
2278}
2279
2280static int
2281compiler_check_debug_one_arg(struct compiler *c, arg_ty arg)
2282{
2283 if (arg != NULL) {
2284 if (forbidden_name(c, arg->arg, Store))
2285 return 0;
2286 }
2287 return 1;
2288}
2289
2290static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002291compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args)
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002292{
2293 if (args != NULL) {
Pablo Galindoee40e4b2020-04-23 03:43:08 +01002294 for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002295 if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i)))
2296 return 0;
2297 }
2298 }
2299 return 1;
2300}
2301
2302static int
2303compiler_check_debug_args(struct compiler *c, arguments_ty args)
2304{
2305 if (!compiler_check_debug_args_seq(c, args->posonlyargs))
2306 return 0;
2307 if (!compiler_check_debug_args_seq(c, args->args))
2308 return 0;
2309 if (!compiler_check_debug_one_arg(c, args->vararg))
2310 return 0;
2311 if (!compiler_check_debug_args_seq(c, args->kwonlyargs))
2312 return 0;
2313 if (!compiler_check_debug_one_arg(c, args->kwarg))
2314 return 0;
2315 return 1;
2316}
2317
2318static int
Yury Selivanov75445082015-05-11 22:57:16 -04002319compiler_function(struct compiler *c, stmt_ty s, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 PyCodeObject *co;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002322 PyObject *qualname, *docstring = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002323 arguments_ty args;
2324 expr_ty returns;
2325 identifier name;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002326 asdl_expr_seq* decos;
2327 asdl_stmt_seq *body;
INADA Naokicb41b272017-02-23 00:31:59 +09002328 Py_ssize_t i, funcflags;
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002329 int annotations;
Yury Selivanov75445082015-05-11 22:57:16 -04002330 int scope_type;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002331 int firstlineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002332
Yury Selivanov75445082015-05-11 22:57:16 -04002333 if (is_async) {
2334 assert(s->kind == AsyncFunctionDef_kind);
2335
2336 args = s->v.AsyncFunctionDef.args;
2337 returns = s->v.AsyncFunctionDef.returns;
2338 decos = s->v.AsyncFunctionDef.decorator_list;
2339 name = s->v.AsyncFunctionDef.name;
2340 body = s->v.AsyncFunctionDef.body;
2341
2342 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
2343 } else {
2344 assert(s->kind == FunctionDef_kind);
2345
2346 args = s->v.FunctionDef.args;
2347 returns = s->v.FunctionDef.returns;
2348 decos = s->v.FunctionDef.decorator_list;
2349 name = s->v.FunctionDef.name;
2350 body = s->v.FunctionDef.body;
2351
2352 scope_type = COMPILER_SCOPE_FUNCTION;
2353 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002354
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002355 if (!compiler_check_debug_args(c, args))
2356 return 0;
2357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 if (!compiler_decorators(c, decos))
2359 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002360
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002361 firstlineno = s->lineno;
2362 if (asdl_seq_LEN(decos)) {
2363 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2364 }
2365
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002366 funcflags = compiler_default_arguments(c, args);
2367 if (funcflags == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002369 }
2370
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002371 annotations = compiler_visit_annotations(c, args, returns);
2372 if (annotations == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002373 return 0;
2374 }
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002375 else if (annotations > 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002376 funcflags |= 0x04;
2377 }
2378
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002379 if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002380 return 0;
2381 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002382
INADA Naokicb41b272017-02-23 00:31:59 +09002383 /* if not -OO mode, add docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002384 if (c->c_optimize < 2) {
2385 docstring = _PyAST_GetDocString(body);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002386 }
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002387 if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 compiler_exit_scope(c);
2389 return 0;
2390 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002393 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Mark Shannon877df852020-11-12 09:43:29 +00002395 for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) {
Mark Shannonfd009e62020-11-13 12:53:53 +00002396 VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i));
Mark Shannon877df852020-11-12 09:43:29 +00002397 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002399 qualname = c->u->u_qualname;
2400 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002402 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002403 Py_XDECREF(qualname);
2404 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002406 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002407
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002408 if (!compiler_make_closure(c, co, funcflags, qualname)) {
2409 Py_DECREF(qualname);
2410 Py_DECREF(co);
2411 return 0;
2412 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002413 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 /* decorators */
2417 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2418 ADDOP_I(c, CALL_FUNCTION, 1);
2419 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002420
Yury Selivanov75445082015-05-11 22:57:16 -04002421 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002422}
2423
2424static int
2425compiler_class(struct compiler *c, stmt_ty s)
2426{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002427 PyCodeObject *co;
2428 PyObject *str;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002429 int i, firstlineno;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002430 asdl_expr_seq *decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 if (!compiler_decorators(c, decos))
2433 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002434
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002435 firstlineno = s->lineno;
2436 if (asdl_seq_LEN(decos)) {
2437 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2438 }
2439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 /* ultimately generate code for:
2441 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
2442 where:
Miss Islington (bot)fa8c5ed2022-03-02 22:02:59 -08002443 <func> is a zero arg function/closure created from the class body.
2444 It mutates its locals to build the class namespace.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 <name> is the class name
2446 <bases> is the positional arguments and *varargs argument
2447 <keywords> is the keyword arguments and **kwds argument
2448 This borrows from compiler_call.
2449 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002452 if (!compiler_enter_scope(c, s->v.ClassDef.name,
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002453 COMPILER_SCOPE_CLASS, (void *)s, firstlineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 return 0;
2455 /* this block represents what we do in the new scope */
2456 {
2457 /* use the class name for name mangling */
2458 Py_INCREF(s->v.ClassDef.name);
Serhiy Storchaka48842712016-04-06 09:45:48 +03002459 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 /* load (global) __name__ ... */
2461 str = PyUnicode_InternFromString("__name__");
2462 if (!str || !compiler_nameop(c, str, Load)) {
2463 Py_XDECREF(str);
2464 compiler_exit_scope(c);
2465 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002466 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002467 Py_DECREF(str);
2468 /* ... and store it as __module__ */
2469 str = PyUnicode_InternFromString("__module__");
2470 if (!str || !compiler_nameop(c, str, Store)) {
2471 Py_XDECREF(str);
2472 compiler_exit_scope(c);
2473 return 0;
2474 }
2475 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002476 assert(c->u->u_qualname);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002477 ADDOP_LOAD_CONST(c, c->u->u_qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002478 str = PyUnicode_InternFromString("__qualname__");
2479 if (!str || !compiler_nameop(c, str, Store)) {
2480 Py_XDECREF(str);
2481 compiler_exit_scope(c);
2482 return 0;
2483 }
2484 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 /* compile the body proper */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002486 if (!compiler_body(c, s->v.ClassDef.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487 compiler_exit_scope(c);
2488 return 0;
2489 }
Mark Shannone56d54e2021-01-15 13:52:00 +00002490 /* The following code is artificial */
2491 c->u->u_lineno = -1;
Nick Coghlan19d24672016-12-05 16:47:55 +10002492 /* Return __classcell__ if it is referenced, otherwise return None */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002493 if (c->u->u_ste->ste_needs_class_closure) {
Nick Coghlan19d24672016-12-05 16:47:55 +10002494 /* Store __classcell__ into class namespace & return it */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002495 str = PyUnicode_InternFromString("__class__");
2496 if (str == NULL) {
2497 compiler_exit_scope(c);
2498 return 0;
2499 }
2500 i = compiler_lookup_arg(c->u->u_cellvars, str);
2501 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01002502 if (i < 0) {
2503 compiler_exit_scope(c);
2504 return 0;
2505 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002506 assert(i == 0);
Nick Coghlan944368e2016-09-11 14:45:49 +10002507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002508 ADDOP_I(c, LOAD_CLOSURE, i);
Nick Coghlan19d24672016-12-05 16:47:55 +10002509 ADDOP(c, DUP_TOP);
Nick Coghlan944368e2016-09-11 14:45:49 +10002510 str = PyUnicode_InternFromString("__classcell__");
2511 if (!str || !compiler_nameop(c, str, Store)) {
2512 Py_XDECREF(str);
2513 compiler_exit_scope(c);
2514 return 0;
2515 }
2516 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002517 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002518 else {
Nick Coghlan19d24672016-12-05 16:47:55 +10002519 /* No methods referenced __class__, so just return None */
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002520 assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002521 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson312595c2013-05-15 15:26:42 -05002522 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002523 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002524 /* create the code object */
2525 co = assemble(c, 1);
2526 }
2527 /* leave the new scope */
2528 compiler_exit_scope(c);
2529 if (co == NULL)
2530 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002532 /* 2. load the 'build_class' function */
2533 ADDOP(c, LOAD_BUILD_CLASS);
2534
2535 /* 3. load a function (or closure) made from the code object */
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002536 if (!compiler_make_closure(c, co, 0, NULL)) {
2537 Py_DECREF(co);
2538 return 0;
2539 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 Py_DECREF(co);
2541
2542 /* 4. load class name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002543 ADDOP_LOAD_CONST(c, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002544
2545 /* 5. generate the rest of the code for the call */
Pablo Galindoa5634c42020-09-16 19:42:00 +01002546 if (!compiler_call_helper(c, 2, s->v.ClassDef.bases, s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 return 0;
2548
2549 /* 6. apply decorators */
2550 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2551 ADDOP_I(c, CALL_FUNCTION, 1);
2552 }
2553
2554 /* 7. store into <name> */
2555 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2556 return 0;
2557 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558}
2559
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02002560/* Return 0 if the expression is a constant value except named singletons.
2561 Return 1 otherwise. */
2562static int
2563check_is_arg(expr_ty e)
2564{
2565 if (e->kind != Constant_kind) {
2566 return 1;
2567 }
2568 PyObject *value = e->v.Constant.value;
2569 return (value == Py_None
2570 || value == Py_False
2571 || value == Py_True
2572 || value == Py_Ellipsis);
2573}
2574
2575/* Check operands of identity chacks ("is" and "is not").
2576 Emit a warning if any operand is a constant except named singletons.
2577 Return 0 on error.
2578 */
2579static int
2580check_compare(struct compiler *c, expr_ty e)
2581{
2582 Py_ssize_t i, n;
2583 int left = check_is_arg(e->v.Compare.left);
2584 n = asdl_seq_LEN(e->v.Compare.ops);
2585 for (i = 0; i < n; i++) {
2586 cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i);
2587 int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2588 if (op == Is || op == IsNot) {
2589 if (!right || !left) {
2590 const char *msg = (op == Is)
2591 ? "\"is\" with a literal. Did you mean \"==\"?"
2592 : "\"is not\" with a literal. Did you mean \"!=\"?";
2593 return compiler_warn(c, msg);
2594 }
2595 }
2596 left = right;
2597 }
2598 return 1;
2599}
2600
Mark Shannon9af0e472020-01-14 10:12:45 +00002601static int compiler_addcompare(struct compiler *c, cmpop_ty op)
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002602{
Mark Shannon9af0e472020-01-14 10:12:45 +00002603 int cmp;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002604 switch (op) {
2605 case Eq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002606 cmp = Py_EQ;
2607 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002608 case NotEq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002609 cmp = Py_NE;
2610 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002611 case Lt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002612 cmp = Py_LT;
2613 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002614 case LtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002615 cmp = Py_LE;
2616 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002617 case Gt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002618 cmp = Py_GT;
2619 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002620 case GtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002621 cmp = Py_GE;
2622 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002623 case Is:
Mark Shannon9af0e472020-01-14 10:12:45 +00002624 ADDOP_I(c, IS_OP, 0);
2625 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002626 case IsNot:
Mark Shannon9af0e472020-01-14 10:12:45 +00002627 ADDOP_I(c, IS_OP, 1);
2628 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002629 case In:
Mark Shannon9af0e472020-01-14 10:12:45 +00002630 ADDOP_I(c, CONTAINS_OP, 0);
2631 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002632 case NotIn:
Mark Shannon9af0e472020-01-14 10:12:45 +00002633 ADDOP_I(c, CONTAINS_OP, 1);
2634 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002635 default:
Mark Shannon9af0e472020-01-14 10:12:45 +00002636 Py_UNREACHABLE();
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002637 }
Mark Shannon9af0e472020-01-14 10:12:45 +00002638 ADDOP_I(c, COMPARE_OP, cmp);
2639 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002640}
2641
Mark Shannon9af0e472020-01-14 10:12:45 +00002642
2643
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002644static int
2645compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2646{
2647 switch (e->kind) {
2648 case UnaryOp_kind:
2649 if (e->v.UnaryOp.op == Not)
2650 return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2651 /* fallback to general implementation */
2652 break;
2653 case BoolOp_kind: {
Pablo Galindoa5634c42020-09-16 19:42:00 +01002654 asdl_expr_seq *s = e->v.BoolOp.values;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002655 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2656 assert(n >= 0);
2657 int cond2 = e->v.BoolOp.op == Or;
2658 basicblock *next2 = next;
2659 if (!cond2 != !cond) {
2660 next2 = compiler_new_block(c);
2661 if (next2 == NULL)
2662 return 0;
2663 }
2664 for (i = 0; i < n; ++i) {
2665 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2666 return 0;
2667 }
2668 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2669 return 0;
2670 if (next2 != next)
2671 compiler_use_next_block(c, next2);
2672 return 1;
2673 }
2674 case IfExp_kind: {
2675 basicblock *end, *next2;
2676 end = compiler_new_block(c);
2677 if (end == NULL)
2678 return 0;
2679 next2 = compiler_new_block(c);
2680 if (next2 == NULL)
2681 return 0;
2682 if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2683 return 0;
2684 if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2685 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002686 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002687 compiler_use_next_block(c, next2);
2688 if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2689 return 0;
2690 compiler_use_next_block(c, end);
2691 return 1;
2692 }
2693 case Compare_kind: {
2694 Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2695 if (n > 0) {
Serhiy Storchaka45835252019-02-16 08:29:46 +02002696 if (!check_compare(c, e)) {
2697 return 0;
2698 }
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002699 basicblock *cleanup = compiler_new_block(c);
2700 if (cleanup == NULL)
2701 return 0;
2702 VISIT(c, expr, e->v.Compare.left);
2703 for (i = 0; i < n; i++) {
2704 VISIT(c, expr,
2705 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2706 ADDOP(c, DUP_TOP);
2707 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00002708 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01002709 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002710 NEXT_BLOCK(c);
2711 }
2712 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00002713 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Mark Shannon582aaf12020-08-04 17:30:11 +01002714 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Mark Shannon266b4622020-11-17 19:30:14 +00002715 NEXT_BLOCK(c);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002716 basicblock *end = compiler_new_block(c);
2717 if (end == NULL)
2718 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002719 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002720 compiler_use_next_block(c, cleanup);
2721 ADDOP(c, POP_TOP);
2722 if (!cond) {
Mark Shannon127dde52021-01-04 18:06:55 +00002723 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002724 }
2725 compiler_use_next_block(c, end);
2726 return 1;
2727 }
2728 /* fallback to general implementation */
2729 break;
2730 }
2731 default:
2732 /* fallback to general implementation */
2733 break;
2734 }
2735
2736 /* general implementation */
2737 VISIT(c, expr, e);
Mark Shannon582aaf12020-08-04 17:30:11 +01002738 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Mark Shannon266b4622020-11-17 19:30:14 +00002739 NEXT_BLOCK(c);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002740 return 1;
2741}
2742
2743static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002744compiler_ifexp(struct compiler *c, expr_ty e)
2745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 basicblock *end, *next;
2747
2748 assert(e->kind == IfExp_kind);
2749 end = compiler_new_block(c);
2750 if (end == NULL)
2751 return 0;
2752 next = compiler_new_block(c);
2753 if (next == NULL)
2754 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002755 if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2756 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 VISIT(c, expr, e->v.IfExp.body);
Mark Shannon127dde52021-01-04 18:06:55 +00002758 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002759 compiler_use_next_block(c, next);
2760 VISIT(c, expr, e->v.IfExp.orelse);
2761 compiler_use_next_block(c, end);
2762 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002763}
2764
2765static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002766compiler_lambda(struct compiler *c, expr_ty e)
2767{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002768 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002769 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002770 static identifier name;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002771 Py_ssize_t funcflags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 arguments_ty args = e->v.Lambda.args;
2773 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002774
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002775 if (!compiler_check_debug_args(c, args))
2776 return 0;
2777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002778 if (!name) {
2779 name = PyUnicode_InternFromString("<lambda>");
2780 if (!name)
2781 return 0;
2782 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002783
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002784 funcflags = compiler_default_arguments(c, args);
2785 if (funcflags == -1) {
2786 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002787 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002788
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002789 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002790 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002791 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002792
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 /* Make None the first constant, so the lambda can't have a
2794 docstring. */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002795 if (compiler_add_const(c, Py_None) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002798 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002799 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002800 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2801 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2802 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002803 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 }
2805 else {
2806 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002807 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002809 qualname = c->u->u_qualname;
2810 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 compiler_exit_scope(c);
Pablo Galindo7fdab832021-01-29 22:40:59 +00002812 if (co == NULL) {
2813 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 return 0;
Pablo Galindo7fdab832021-01-29 22:40:59 +00002815 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002816
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002817 if (!compiler_make_closure(c, co, funcflags, qualname)) {
2818 Py_DECREF(qualname);
2819 Py_DECREF(co);
2820 return 0;
2821 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002822 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 Py_DECREF(co);
2824
2825 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002826}
2827
2828static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002829compiler_if(struct compiler *c, stmt_ty s)
2830{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 basicblock *end, *next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 assert(s->kind == If_kind);
2833 end = compiler_new_block(c);
Mark Shannon8473cf82020-12-15 11:07:50 +00002834 if (end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002835 return 0;
Mark Shannon8473cf82020-12-15 11:07:50 +00002836 }
2837 if (asdl_seq_LEN(s->v.If.orelse)) {
2838 next = compiler_new_block(c);
2839 if (next == NULL) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002840 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002841 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002842 }
2843 else {
2844 next = end;
2845 }
2846 if (!compiler_jump_if(c, s->v.If.test, next, 0)) {
2847 return 0;
2848 }
2849 VISIT_SEQ(c, stmt, s->v.If.body);
2850 if (asdl_seq_LEN(s->v.If.orelse)) {
Mark Shannon127dde52021-01-04 18:06:55 +00002851 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Mark Shannon8473cf82020-12-15 11:07:50 +00002852 compiler_use_next_block(c, next);
2853 VISIT_SEQ(c, stmt, s->v.If.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 }
2855 compiler_use_next_block(c, end);
2856 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002857}
2858
2859static int
2860compiler_for(struct compiler *c, stmt_ty s)
2861{
Mark Shannon5977a792020-12-02 13:31:40 +00002862 basicblock *start, *body, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 start = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002865 body = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 cleanup = compiler_new_block(c);
2867 end = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002868 if (start == NULL || body == NULL || end == NULL || cleanup == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002870 }
2871 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002873 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 VISIT(c, expr, s->v.For.iter);
2875 ADDOP(c, GET_ITER);
2876 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01002877 ADDOP_JUMP(c, FOR_ITER, cleanup);
Mark Shannon5977a792020-12-02 13:31:40 +00002878 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 VISIT(c, expr, s->v.For.target);
2880 VISIT_SEQ(c, stmt, s->v.For.body);
Mark Shannonf5e97b72020-12-14 11:28:39 +00002881 /* Mark jump as artificial */
2882 c->u->u_lineno = -1;
Mark Shannon582aaf12020-08-04 17:30:11 +01002883 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 compiler_use_next_block(c, cleanup);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002885
2886 compiler_pop_fblock(c, FOR_LOOP, start);
2887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 VISIT_SEQ(c, stmt, s->v.For.orelse);
2889 compiler_use_next_block(c, end);
2890 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002891}
2892
Yury Selivanov75445082015-05-11 22:57:16 -04002893
2894static int
2895compiler_async_for(struct compiler *c, stmt_ty s)
2896{
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002897 basicblock *start, *except, *end;
Pablo Galindo90235812020-03-15 04:29:22 +00002898 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07002899 c->u->u_ste->ste_coroutine = 1;
2900 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
Zsolt Dollensteine2396502018-04-27 08:58:56 -07002901 return compiler_error(c, "'async for' outside async function");
2902 }
2903
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002904 start = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002905 except = compiler_new_block(c);
2906 end = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002907
Mark Shannonfee55262019-11-21 09:11:43 +00002908 if (start == NULL || except == NULL || end == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002909 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002910 }
Yury Selivanov75445082015-05-11 22:57:16 -04002911 VISIT(c, expr, s->v.AsyncFor.iter);
2912 ADDOP(c, GET_AITER);
Yury Selivanov75445082015-05-11 22:57:16 -04002913
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002914 compiler_use_next_block(c, start);
Mark Shannonfee55262019-11-21 09:11:43 +00002915 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002916 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002917 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002918 /* SETUP_FINALLY to guard the __anext__ call */
Mark Shannon582aaf12020-08-04 17:30:11 +01002919 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov75445082015-05-11 22:57:16 -04002920 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002921 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04002922 ADDOP(c, YIELD_FROM);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002923 ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */
Yury Selivanov75445082015-05-11 22:57:16 -04002924
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002925 /* Success block for __anext__ */
2926 VISIT(c, expr, s->v.AsyncFor.target);
2927 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002928 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002929
2930 compiler_pop_fblock(c, FOR_LOOP, start);
Yury Selivanov75445082015-05-11 22:57:16 -04002931
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002932 /* Except block for __anext__ */
Yury Selivanov75445082015-05-11 22:57:16 -04002933 compiler_use_next_block(c, except);
Mark Shannon877df852020-11-12 09:43:29 +00002934
Mark Shannon47695e32021-07-15 15:54:38 +01002935 /* Use same line number as the iterator,
2936 * as the END_ASYNC_FOR succeeds the `for`, not the body. */
2937 SET_LOC(c, s->v.AsyncFor.iter);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002938 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov75445082015-05-11 22:57:16 -04002939
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002940 /* `else` block */
Yury Selivanov75445082015-05-11 22:57:16 -04002941 VISIT_SEQ(c, stmt, s->v.For.orelse);
2942
2943 compiler_use_next_block(c, end);
2944
2945 return 1;
2946}
2947
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002948static int
2949compiler_while(struct compiler *c, stmt_ty s)
2950{
Mark Shannon266b4622020-11-17 19:30:14 +00002951 basicblock *loop, *body, *end, *anchor = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 loop = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002953 body = compiler_new_block(c);
2954 anchor = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955 end = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002956 if (loop == NULL || body == NULL || anchor == NULL || end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002957 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002958 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 compiler_use_next_block(c, loop);
Mark Shannon266b4622020-11-17 19:30:14 +00002960 if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002962 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002963 if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) {
2964 return 0;
Mark Shannon266b4622020-11-17 19:30:14 +00002965 }
2966
2967 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968 VISIT_SEQ(c, stmt, s->v.While.body);
Mark Shannon8473cf82020-12-15 11:07:50 +00002969 SET_LOC(c, s);
2970 if (!compiler_jump_if(c, s->v.While.test, body, 1)) {
2971 return 0;
2972 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002973
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002974 compiler_pop_fblock(c, WHILE_LOOP, loop);
2975
Mark Shannon266b4622020-11-17 19:30:14 +00002976 compiler_use_next_block(c, anchor);
2977 if (s->v.While.orelse) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 VISIT_SEQ(c, stmt, s->v.While.orelse);
Mark Shannon266b4622020-11-17 19:30:14 +00002979 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002980 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002982 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002983}
2984
2985static int
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002986compiler_return(struct compiler *c, stmt_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002987{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002988 int preserve_tos = ((s->v.Return.value != NULL) &&
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002989 (s->v.Return.value->kind != Constant_kind));
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002990 if (c->u->u_ste->ste_type != FunctionBlock)
2991 return compiler_error(c, "'return' outside function");
2992 if (s->v.Return.value != NULL &&
2993 c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2994 {
2995 return compiler_error(
2996 c, "'return' with value in async generator");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002997 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002998 if (preserve_tos) {
2999 VISIT(c, expr, s->v.Return.value);
Mark Shannon5274b682020-12-16 13:07:01 +00003000 } else {
Mark Shannoncea05852021-06-03 19:57:31 +01003001 /* Emit instruction with line number for return value */
Mark Shannon5274b682020-12-16 13:07:01 +00003002 if (s->v.Return.value != NULL) {
3003 SET_LOC(c, s->v.Return.value);
3004 ADDOP(c, NOP);
3005 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003006 }
Mark Shannoncea05852021-06-03 19:57:31 +01003007 if (s->v.Return.value == NULL || s->v.Return.value->lineno != s->lineno) {
3008 SET_LOC(c, s);
3009 ADDOP(c, NOP);
3010 }
3011
Mark Shannonfee55262019-11-21 09:11:43 +00003012 if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL))
3013 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003014 if (s->v.Return.value == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003015 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003016 }
3017 else if (!preserve_tos) {
Mark Shannon5274b682020-12-16 13:07:01 +00003018 ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003019 }
3020 ADDOP(c, RETURN_VALUE);
Mark Shannon266b4622020-11-17 19:30:14 +00003021 NEXT_BLOCK(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003023 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003024}
3025
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003026static int
3027compiler_break(struct compiler *c)
3028{
Mark Shannonfee55262019-11-21 09:11:43 +00003029 struct fblockinfo *loop = NULL;
Mark Shannoncea05852021-06-03 19:57:31 +01003030 /* Emit instruction with line number */
3031 ADDOP(c, NOP);
Mark Shannonfee55262019-11-21 09:11:43 +00003032 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
3033 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003034 }
Mark Shannonfee55262019-11-21 09:11:43 +00003035 if (loop == NULL) {
3036 return compiler_error(c, "'break' outside loop");
3037 }
3038 if (!compiler_unwind_fblock(c, loop, 0)) {
3039 return 0;
3040 }
Mark Shannon582aaf12020-08-04 17:30:11 +01003041 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit);
Mark Shannon266b4622020-11-17 19:30:14 +00003042 NEXT_BLOCK(c);
Mark Shannonfee55262019-11-21 09:11:43 +00003043 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003044}
3045
3046static int
3047compiler_continue(struct compiler *c)
3048{
Mark Shannonfee55262019-11-21 09:11:43 +00003049 struct fblockinfo *loop = NULL;
Mark Shannoncea05852021-06-03 19:57:31 +01003050 /* Emit instruction with line number */
3051 ADDOP(c, NOP);
Mark Shannonfee55262019-11-21 09:11:43 +00003052 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
3053 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003054 }
Mark Shannonfee55262019-11-21 09:11:43 +00003055 if (loop == NULL) {
3056 return compiler_error(c, "'continue' not properly in loop");
3057 }
Mark Shannon582aaf12020-08-04 17:30:11 +01003058 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block);
Mark Shannon266b4622020-11-17 19:30:14 +00003059 NEXT_BLOCK(c)
Mark Shannonfee55262019-11-21 09:11:43 +00003060 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003061}
3062
3063
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003064/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065
3066 SETUP_FINALLY L
3067 <code for body>
3068 POP_BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00003069 <code for finalbody>
3070 JUMP E
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003071 L:
3072 <code for finalbody>
Mark Shannonfee55262019-11-21 09:11:43 +00003073 E:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003074
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003075 The special instructions use the block stack. Each block
3076 stack entry contains the instruction that created it (here
3077 SETUP_FINALLY), the level of the value stack at the time the
3078 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003080 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081 Pushes the current value stack level and the label
3082 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003083 POP_BLOCK:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003084 Pops en entry from the block stack.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003085
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003086 The block stack is unwound when an exception is raised:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003087 when a SETUP_FINALLY entry is found, the raised and the caught
3088 exceptions are pushed onto the value stack (and the exception
3089 condition is cleared), and the interpreter jumps to the label
3090 gotten from the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003091*/
3092
3093static int
3094compiler_try_finally(struct compiler *c, stmt_ty s)
3095{
Mark Shannonfee55262019-11-21 09:11:43 +00003096 basicblock *body, *end, *exit;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003098 body = compiler_new_block(c);
3099 end = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00003100 exit = compiler_new_block(c);
3101 if (body == NULL || end == NULL || exit == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003102 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003103
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003104 /* `try` block */
Mark Shannon582aaf12020-08-04 17:30:11 +01003105 ADDOP_JUMP(c, SETUP_FINALLY, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 compiler_use_next_block(c, body);
Mark Shannonfee55262019-11-21 09:11:43 +00003107 if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003108 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003109 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
3110 if (!compiler_try_except(c, s))
3111 return 0;
3112 }
3113 else {
3114 VISIT_SEQ(c, stmt, s->v.Try.body);
3115 }
Mark Shannon3bd60352021-01-13 12:05:43 +00003116 ADDOP_NOLINE(c, POP_BLOCK);
Mark Shannonfee55262019-11-21 09:11:43 +00003117 compiler_pop_fblock(c, FINALLY_TRY, body);
3118 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Mark Shannon127dde52021-01-04 18:06:55 +00003119 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00003120 /* `finally` block */
3121 compiler_use_next_block(c, end);
3122 if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL))
3123 return 0;
3124 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3125 compiler_pop_fblock(c, FINALLY_END, end);
Mark Shannonbf353f32020-12-17 13:55:28 +00003126 ADDOP_I(c, RERAISE, 0);
Mark Shannonfee55262019-11-21 09:11:43 +00003127 compiler_use_next_block(c, exit);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003128 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003129}
3130
3131/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003132 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133 (The contents of the value stack is shown in [], with the top
3134 at the right; 'tb' is trace-back info, 'val' the exception's
3135 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136
3137 Value stack Label Instruction Argument
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003138 [] SETUP_FINALLY L1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 [] <code for S>
3140 [] POP_BLOCK
3141 [] JUMP_FORWARD L0
3142
3143 [tb, val, exc] L1: DUP )
3144 [tb, val, exc, exc] <evaluate E1> )
Mark Shannon9af0e472020-01-14 10:12:45 +00003145 [tb, val, exc, exc, E1] JUMP_IF_NOT_EXC_MATCH L2 ) only if E1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003146 [tb, val, exc] POP
3147 [tb, val] <assign to V1> (or POP if no V1)
3148 [tb] POP
3149 [] <code for S1>
3150 JUMP_FORWARD L0
3151
3152 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003153 .............................etc.......................
3154
Mark Shannonfee55262019-11-21 09:11:43 +00003155 [tb, val, exc] Ln+1: RERAISE # re-raise exception
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003156
3157 [] L0: <next statement>
3158
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003159 Of course, parts are not generated if Vi or Ei is not present.
3160*/
3161static int
3162compiler_try_except(struct compiler *c, stmt_ty s)
3163{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003165 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003167 body = compiler_new_block(c);
3168 except = compiler_new_block(c);
3169 orelse = compiler_new_block(c);
3170 end = compiler_new_block(c);
3171 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
3172 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01003173 ADDOP_JUMP(c, SETUP_FINALLY, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003174 compiler_use_next_block(c, body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003175 if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003176 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003177 VISIT_SEQ(c, stmt, s->v.Try.body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003178 compiler_pop_fblock(c, TRY_EXCEPT, body);
Mark Shannon3bd60352021-01-13 12:05:43 +00003179 ADDOP_NOLINE(c, POP_BLOCK);
3180 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003181 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003182 compiler_use_next_block(c, except);
Mark Shannon02d126a2020-09-25 14:04:19 +01003183 /* Runtime will push a block here, so we need to account for that */
3184 if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL))
3185 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003186 for (i = 0; i < n; i++) {
3187 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003188 s->v.Try.handlers, i);
Mark Shannon8d4b1842021-05-06 13:38:50 +01003189 SET_LOC(c, handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190 if (!handler->v.ExceptHandler.type && i < n-1)
3191 return compiler_error(c, "default 'except:' must be last");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003192 except = compiler_new_block(c);
3193 if (except == NULL)
3194 return 0;
3195 if (handler->v.ExceptHandler.type) {
3196 ADDOP(c, DUP_TOP);
3197 VISIT(c, expr, handler->v.ExceptHandler.type);
Mark Shannon582aaf12020-08-04 17:30:11 +01003198 ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except);
Mark Shannon266b4622020-11-17 19:30:14 +00003199 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003200 }
3201 ADDOP(c, POP_TOP);
3202 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003203 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00003204
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003205 cleanup_end = compiler_new_block(c);
3206 cleanup_body = compiler_new_block(c);
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003207 if (cleanup_end == NULL || cleanup_body == NULL) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003208 return 0;
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003209 }
Guido van Rossumb940e112007-01-10 16:19:56 +00003210
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003211 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3212 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003213
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003214 /*
3215 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003216 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003217 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003218 try:
3219 # body
3220 finally:
Chris Angelicoad098b62019-05-21 23:34:19 +10003221 name = None # in case body contains "del name"
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003222 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003223 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003224
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003225 /* second try: */
Mark Shannon582aaf12020-08-04 17:30:11 +01003226 ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003227 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003228 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003229 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003231 /* second # body */
3232 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003233 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannon877df852020-11-12 09:43:29 +00003234 /* name = None; del name; # Mark as artificial */
3235 c->u->u_lineno = -1;
Mark Shannon794ff7d2021-07-14 11:43:56 +01003236 ADDOP(c, POP_BLOCK);
3237 ADDOP(c, POP_EXCEPT);
Mark Shannonfee55262019-11-21 09:11:43 +00003238 ADDOP_LOAD_CONST(c, Py_None);
3239 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3240 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Mark Shannon582aaf12020-08-04 17:30:11 +01003241 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242
Mark Shannonfee55262019-11-21 09:11:43 +00003243 /* except: */
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003244 compiler_use_next_block(c, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003245
Mark Shannon877df852020-11-12 09:43:29 +00003246 /* name = None; del name; # Mark as artificial */
3247 c->u->u_lineno = -1;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003248 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003249 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003250 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003251
Mark Shannonbf353f32020-12-17 13:55:28 +00003252 ADDOP_I(c, RERAISE, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003253 }
3254 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003255 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003256
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003257 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05003258 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003259 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003260
Guido van Rossumb940e112007-01-10 16:19:56 +00003261 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003262 ADDOP(c, POP_TOP);
3263 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003264 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003265 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003266 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003267 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannon127dde52021-01-04 18:06:55 +00003268 c->u->u_lineno = -1;
Mark Shannonfee55262019-11-21 09:11:43 +00003269 ADDOP(c, POP_EXCEPT);
Mark Shannon582aaf12020-08-04 17:30:11 +01003270 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003271 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 compiler_use_next_block(c, except);
3273 }
Mark Shannon02d126a2020-09-25 14:04:19 +01003274 compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL);
Mark Shannonf2dbfd72020-12-21 13:53:50 +00003275 /* Mark as artificial */
3276 c->u->u_lineno = -1;
Mark Shannonbf353f32020-12-17 13:55:28 +00003277 ADDOP_I(c, RERAISE, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003278 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003279 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003280 compiler_use_next_block(c, end);
3281 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003282}
3283
3284static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003285compiler_try(struct compiler *c, stmt_ty s) {
3286 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
3287 return compiler_try_finally(c, s);
3288 else
3289 return compiler_try_except(c, s);
3290}
3291
3292
3293static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003294compiler_import_as(struct compiler *c, identifier name, identifier asname)
3295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003296 /* The IMPORT_NAME opcode was already generated. This function
3297 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003299 If there is a dot in name, we need to split it and emit a
Serhiy Storchakaf93234b2017-05-09 22:31:05 +03003300 IMPORT_FROM for each name.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003301 */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003302 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
3303 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003304 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003305 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003306 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 /* Consume the base module name to get the first attribute */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003308 while (1) {
3309 Py_ssize_t pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003310 PyObject *attr;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003311 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003312 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003313 return 0;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003314 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 if (!attr)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003316 return 0;
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003317 ADDOP_N(c, IMPORT_FROM, attr, names);
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003318 if (dot == -1) {
3319 break;
3320 }
3321 ADDOP(c, ROT_TWO);
3322 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003323 }
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003324 if (!compiler_nameop(c, asname, Store)) {
3325 return 0;
3326 }
3327 ADDOP(c, POP_TOP);
3328 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 }
3330 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003331}
3332
3333static int
3334compiler_import(struct compiler *c, stmt_ty s)
3335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003336 /* The Import node stores a module name like a.b.c as a single
3337 string. This is convenient for all cases except
3338 import a.b.c as d
3339 where we need to parse that string to extract the individual
3340 module names.
3341 XXX Perhaps change the representation to make this case simpler?
3342 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01003343 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003344
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003345 PyObject *zero = _PyLong_GetZero(); // borrowed reference
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003346 for (i = 0; i < n; i++) {
3347 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
3348 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003349
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003350 ADDOP_LOAD_CONST(c, zero);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003351 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003352 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003354 if (alias->asname) {
3355 r = compiler_import_as(c, alias->name, alias->asname);
3356 if (!r)
3357 return r;
3358 }
3359 else {
3360 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003361 Py_ssize_t dot = PyUnicode_FindChar(
3362 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02003363 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003364 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02003365 if (tmp == NULL)
3366 return 0;
3367 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003368 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003369 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003370 Py_DECREF(tmp);
3371 }
3372 if (!r)
3373 return r;
3374 }
3375 }
3376 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003377}
3378
3379static int
3380compiler_from_import(struct compiler *c, stmt_ty s)
3381{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003382 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003383 PyObject *names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003384 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003386 if (!empty_string) {
3387 empty_string = PyUnicode_FromString("");
3388 if (!empty_string)
3389 return 0;
3390 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003391
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003392 ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02003393
3394 names = PyTuple_New(n);
3395 if (!names)
3396 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003398 /* build up the names */
3399 for (i = 0; i < n; i++) {
3400 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3401 Py_INCREF(alias->name);
3402 PyTuple_SET_ITEM(names, i, alias->name);
3403 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003405 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003406 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003407 Py_DECREF(names);
3408 return compiler_error(c, "from __future__ imports must occur "
3409 "at the beginning of the file");
3410 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003411 ADDOP_LOAD_CONST_NEW(c, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003413 if (s->v.ImportFrom.module) {
3414 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
3415 }
3416 else {
3417 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
3418 }
3419 for (i = 0; i < n; i++) {
3420 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3421 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003422
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003423 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003424 assert(n == 1);
3425 ADDOP(c, IMPORT_STAR);
3426 return 1;
3427 }
3428
3429 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
3430 store_name = alias->name;
3431 if (alias->asname)
3432 store_name = alias->asname;
3433
3434 if (!compiler_nameop(c, store_name, Store)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 return 0;
3436 }
3437 }
3438 /* remove imported module */
3439 ADDOP(c, POP_TOP);
3440 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003441}
3442
3443static int
3444compiler_assert(struct compiler *c, stmt_ty s)
3445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003446 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003447
tsukasa-aua8ef4572021-03-16 22:14:41 +11003448 /* Always emit a warning if the test is a non-zero length tuple */
3449 if ((s->v.Assert.test->kind == Tuple_kind &&
3450 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) ||
3451 (s->v.Assert.test->kind == Constant_kind &&
3452 PyTuple_Check(s->v.Assert.test->v.Constant.value) &&
3453 PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0))
Serhiy Storchakad31e7732018-10-21 10:09:39 +03003454 {
3455 if (!compiler_warn(c, "assertion is always true, "
3456 "perhaps remove parentheses?"))
3457 {
Victor Stinner14e461d2013-08-26 22:28:21 +02003458 return 0;
3459 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 }
tsukasa-aua8ef4572021-03-16 22:14:41 +11003461 if (c->c_optimize)
3462 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003463 end = compiler_new_block(c);
3464 if (end == NULL)
3465 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003466 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
3467 return 0;
Zackery Spytzce6a0702019-08-25 03:44:09 -06003468 ADDOP(c, LOAD_ASSERTION_ERROR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003469 if (s->v.Assert.msg) {
3470 VISIT(c, expr, s->v.Assert.msg);
3471 ADDOP_I(c, CALL_FUNCTION, 1);
3472 }
3473 ADDOP_I(c, RAISE_VARARGS, 1);
3474 compiler_use_next_block(c, end);
3475 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003476}
3477
3478static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003479compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3480{
3481 if (c->c_interactive && c->c_nestlevel <= 1) {
3482 VISIT(c, expr, value);
3483 ADDOP(c, PRINT_EXPR);
3484 return 1;
3485 }
3486
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003487 if (value->kind == Constant_kind) {
Victor Stinner15a30952016-02-08 22:45:06 +01003488 /* ignore constant statement */
Mark Shannon877df852020-11-12 09:43:29 +00003489 ADDOP(c, NOP);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003490 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003491 }
3492
3493 VISIT(c, expr, value);
Mark Shannonc5440932021-03-15 14:24:25 +00003494 /* Mark POP_TOP as artificial */
3495 c->u->u_lineno = -1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003496 ADDOP(c, POP_TOP);
3497 return 1;
3498}
3499
3500static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003501compiler_visit_stmt(struct compiler *c, stmt_ty s)
3502{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003503 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003505 /* Always assign a lineno to the next instruction for a stmt. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003506 SET_LOC(c, s);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 switch (s->kind) {
3509 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04003510 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003511 case ClassDef_kind:
3512 return compiler_class(c, s);
3513 case Return_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003514 return compiler_return(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003515 case Delete_kind:
3516 VISIT_SEQ(c, expr, s->v.Delete.targets)
3517 break;
3518 case Assign_kind:
3519 n = asdl_seq_LEN(s->v.Assign.targets);
3520 VISIT(c, expr, s->v.Assign.value);
3521 for (i = 0; i < n; i++) {
3522 if (i < n - 1)
3523 ADDOP(c, DUP_TOP);
3524 VISIT(c, expr,
3525 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3526 }
3527 break;
3528 case AugAssign_kind:
3529 return compiler_augassign(c, s);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003530 case AnnAssign_kind:
3531 return compiler_annassign(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003532 case For_kind:
3533 return compiler_for(c, s);
3534 case While_kind:
3535 return compiler_while(c, s);
3536 case If_kind:
3537 return compiler_if(c, s);
Brandt Bucher145bf262021-02-26 14:51:55 -08003538 case Match_kind:
3539 return compiler_match(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003540 case Raise_kind:
3541 n = 0;
3542 if (s->v.Raise.exc) {
3543 VISIT(c, expr, s->v.Raise.exc);
3544 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003545 if (s->v.Raise.cause) {
3546 VISIT(c, expr, s->v.Raise.cause);
3547 n++;
3548 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003549 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01003550 ADDOP_I(c, RAISE_VARARGS, (int)n);
Mark Shannon266b4622020-11-17 19:30:14 +00003551 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003552 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003553 case Try_kind:
3554 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003555 case Assert_kind:
3556 return compiler_assert(c, s);
3557 case Import_kind:
3558 return compiler_import(c, s);
3559 case ImportFrom_kind:
3560 return compiler_from_import(c, s);
3561 case Global_kind:
3562 case Nonlocal_kind:
3563 break;
3564 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003565 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003566 case Pass_kind:
Mark Shannon877df852020-11-12 09:43:29 +00003567 ADDOP(c, NOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003568 break;
3569 case Break_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003570 return compiler_break(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003571 case Continue_kind:
3572 return compiler_continue(c);
3573 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003574 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04003575 case AsyncFunctionDef_kind:
3576 return compiler_function(c, s, 1);
3577 case AsyncWith_kind:
3578 return compiler_async_with(c, s, 0);
3579 case AsyncFor_kind:
3580 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003581 }
Yury Selivanov75445082015-05-11 22:57:16 -04003582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003583 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003584}
3585
3586static int
3587unaryop(unaryop_ty op)
3588{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003589 switch (op) {
3590 case Invert:
3591 return UNARY_INVERT;
3592 case Not:
3593 return UNARY_NOT;
3594 case UAdd:
3595 return UNARY_POSITIVE;
3596 case USub:
3597 return UNARY_NEGATIVE;
3598 default:
3599 PyErr_Format(PyExc_SystemError,
3600 "unary op %d should not be possible", op);
3601 return 0;
3602 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003603}
3604
3605static int
Andy Lester76d58772020-03-10 21:18:12 -05003606binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 switch (op) {
3609 case Add:
3610 return BINARY_ADD;
3611 case Sub:
3612 return BINARY_SUBTRACT;
3613 case Mult:
3614 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003615 case MatMult:
3616 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003617 case Div:
3618 return BINARY_TRUE_DIVIDE;
3619 case Mod:
3620 return BINARY_MODULO;
3621 case Pow:
3622 return BINARY_POWER;
3623 case LShift:
3624 return BINARY_LSHIFT;
3625 case RShift:
3626 return BINARY_RSHIFT;
3627 case BitOr:
3628 return BINARY_OR;
3629 case BitXor:
3630 return BINARY_XOR;
3631 case BitAnd:
3632 return BINARY_AND;
3633 case FloorDiv:
3634 return BINARY_FLOOR_DIVIDE;
3635 default:
3636 PyErr_Format(PyExc_SystemError,
3637 "binary op %d should not be possible", op);
3638 return 0;
3639 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003640}
3641
3642static int
Andy Lester76d58772020-03-10 21:18:12 -05003643inplace_binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003644{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003645 switch (op) {
3646 case Add:
3647 return INPLACE_ADD;
3648 case Sub:
3649 return INPLACE_SUBTRACT;
3650 case Mult:
3651 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003652 case MatMult:
3653 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003654 case Div:
3655 return INPLACE_TRUE_DIVIDE;
3656 case Mod:
3657 return INPLACE_MODULO;
3658 case Pow:
3659 return INPLACE_POWER;
3660 case LShift:
3661 return INPLACE_LSHIFT;
3662 case RShift:
3663 return INPLACE_RSHIFT;
3664 case BitOr:
3665 return INPLACE_OR;
3666 case BitXor:
3667 return INPLACE_XOR;
3668 case BitAnd:
3669 return INPLACE_AND;
3670 case FloorDiv:
3671 return INPLACE_FLOOR_DIVIDE;
3672 default:
3673 PyErr_Format(PyExc_SystemError,
3674 "inplace binary op %d should not be possible", op);
3675 return 0;
3676 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003677}
3678
3679static int
3680compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3681{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003682 int op, scope;
3683 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003684 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003686 PyObject *dict = c->u->u_names;
3687 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003688
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003689 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3690 !_PyUnicode_EqualToASCIIString(name, "True") &&
3691 !_PyUnicode_EqualToASCIIString(name, "False"));
Benjamin Peterson70b224d2012-12-06 17:49:58 -05003692
Pablo Galindoc5fc1562020-04-22 23:29:27 +01003693 if (forbidden_name(c, name, ctx))
3694 return 0;
3695
Serhiy Storchakabd6ec4d2017-12-18 14:29:12 +02003696 mangled = _Py_Mangle(c->u->u_private, name);
3697 if (!mangled)
3698 return 0;
3699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003700 op = 0;
3701 optype = OP_NAME;
Victor Stinner28ad12f2021-03-19 12:41:49 +01003702 scope = _PyST_GetScope(c->u->u_ste, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003703 switch (scope) {
3704 case FREE:
3705 dict = c->u->u_freevars;
3706 optype = OP_DEREF;
3707 break;
3708 case CELL:
3709 dict = c->u->u_cellvars;
3710 optype = OP_DEREF;
3711 break;
3712 case LOCAL:
3713 if (c->u->u_ste->ste_type == FunctionBlock)
3714 optype = OP_FAST;
3715 break;
3716 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04003717 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003718 optype = OP_GLOBAL;
3719 break;
3720 case GLOBAL_EXPLICIT:
3721 optype = OP_GLOBAL;
3722 break;
3723 default:
3724 /* scope can be 0 */
3725 break;
3726 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003729 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003731 switch (optype) {
3732 case OP_DEREF:
3733 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003734 case Load:
3735 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3736 break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003737 case Store: op = STORE_DEREF; break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003738 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003739 }
3740 break;
3741 case OP_FAST:
3742 switch (ctx) {
3743 case Load: op = LOAD_FAST; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003744 case Store: op = STORE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003745 case Del: op = DELETE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003746 }
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003747 ADDOP_N(c, op, mangled, varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003748 return 1;
3749 case OP_GLOBAL:
3750 switch (ctx) {
3751 case Load: op = LOAD_GLOBAL; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003752 case Store: op = STORE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003753 case Del: op = DELETE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003754 }
3755 break;
3756 case OP_NAME:
3757 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00003758 case Load: op = LOAD_NAME; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003759 case Store: op = STORE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003760 case Del: op = DELETE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003761 }
3762 break;
3763 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003765 assert(op);
Andy Lester76d58772020-03-10 21:18:12 -05003766 arg = compiler_add_o(dict, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003767 Py_DECREF(mangled);
3768 if (arg < 0)
3769 return 0;
3770 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003771}
3772
3773static int
3774compiler_boolop(struct compiler *c, expr_ty e)
3775{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003776 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003777 int jumpi;
3778 Py_ssize_t i, n;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003779 asdl_expr_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003781 assert(e->kind == BoolOp_kind);
3782 if (e->v.BoolOp.op == And)
3783 jumpi = JUMP_IF_FALSE_OR_POP;
3784 else
3785 jumpi = JUMP_IF_TRUE_OR_POP;
3786 end = compiler_new_block(c);
3787 if (end == NULL)
3788 return 0;
3789 s = e->v.BoolOp.values;
3790 n = asdl_seq_LEN(s) - 1;
3791 assert(n >= 0);
3792 for (i = 0; i < n; ++i) {
3793 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003794 ADDOP_JUMP(c, jumpi, end);
Mark Shannon6e8128f2020-07-30 10:03:00 +01003795 basicblock *next = compiler_new_block(c);
3796 if (next == NULL) {
3797 return 0;
3798 }
3799 compiler_use_next_block(c, next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003800 }
3801 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3802 compiler_use_next_block(c, end);
3803 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003804}
3805
3806static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003807starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
Mark Shannon13bc1392020-01-23 09:25:17 +00003808 int build, int add, int extend, int tuple)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003809{
3810 Py_ssize_t n = asdl_seq_LEN(elts);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003811 if (n > 2 && are_all_items_const(elts, 0, n)) {
3812 PyObject *folded = PyTuple_New(n);
3813 if (folded == NULL) {
3814 return 0;
3815 }
3816 PyObject *val;
Mark Shannon11e0b292021-04-15 14:28:56 +01003817 for (Py_ssize_t i = 0; i < n; i++) {
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003818 val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
3819 Py_INCREF(val);
3820 PyTuple_SET_ITEM(folded, i, val);
3821 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003822 if (tuple) {
3823 ADDOP_LOAD_CONST_NEW(c, folded);
3824 } else {
3825 if (add == SET_ADD) {
3826 Py_SETREF(folded, PyFrozenSet_New(folded));
3827 if (folded == NULL) {
3828 return 0;
3829 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003830 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003831 ADDOP_I(c, build, pushed);
3832 ADDOP_LOAD_CONST_NEW(c, folded);
3833 ADDOP_I(c, extend, 1);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003834 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003835 return 1;
3836 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003837
Mark Shannon11e0b292021-04-15 14:28:56 +01003838 int big = n+pushed > STACK_USE_GUIDELINE;
3839 int seen_star = 0;
3840 for (Py_ssize_t i = 0; i < n; i++) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003841 expr_ty elt = asdl_seq_GET(elts, i);
3842 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003843 seen_star = 1;
3844 }
3845 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003846 if (!seen_star && !big) {
3847 for (Py_ssize_t i = 0; i < n; i++) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003848 expr_ty elt = asdl_seq_GET(elts, i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003849 VISIT(c, expr, elt);
Mark Shannon13bc1392020-01-23 09:25:17 +00003850 }
3851 if (tuple) {
3852 ADDOP_I(c, BUILD_TUPLE, n+pushed);
3853 } else {
3854 ADDOP_I(c, build, n+pushed);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003855 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003856 return 1;
3857 }
3858 int sequence_built = 0;
3859 if (big) {
3860 ADDOP_I(c, build, pushed);
3861 sequence_built = 1;
3862 }
3863 for (Py_ssize_t i = 0; i < n; i++) {
3864 expr_ty elt = asdl_seq_GET(elts, i);
3865 if (elt->kind == Starred_kind) {
3866 if (sequence_built == 0) {
3867 ADDOP_I(c, build, i+pushed);
3868 sequence_built = 1;
3869 }
3870 VISIT(c, expr, elt->v.Starred.value);
3871 ADDOP_I(c, extend, 1);
3872 }
3873 else {
3874 VISIT(c, expr, elt);
3875 if (sequence_built) {
3876 ADDOP_I(c, add, 1);
3877 }
3878 }
3879 }
3880 assert(sequence_built);
3881 if (tuple) {
3882 ADDOP(c, LIST_TO_TUPLE);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003883 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003884 return 1;
3885}
3886
3887static int
Brandt Bucher145bf262021-02-26 14:51:55 -08003888unpack_helper(struct compiler *c, asdl_expr_seq *elts)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003889{
3890 Py_ssize_t n = asdl_seq_LEN(elts);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003891 int seen_star = 0;
Brandt Bucher145bf262021-02-26 14:51:55 -08003892 for (Py_ssize_t i = 0; i < n; i++) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003893 expr_ty elt = asdl_seq_GET(elts, i);
3894 if (elt->kind == Starred_kind && !seen_star) {
3895 if ((i >= (1 << 8)) ||
3896 (n-i-1 >= (INT_MAX >> 8)))
3897 return compiler_error(c,
3898 "too many expressions in "
3899 "star-unpacking assignment");
3900 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3901 seen_star = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003902 }
3903 else if (elt->kind == Starred_kind) {
3904 return compiler_error(c,
Furkan Öndercb6534e2020-03-26 04:54:31 +03003905 "multiple starred expressions in assignment");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003906 }
3907 }
3908 if (!seen_star) {
3909 ADDOP_I(c, UNPACK_SEQUENCE, n);
3910 }
Brandt Bucher145bf262021-02-26 14:51:55 -08003911 return 1;
3912}
3913
3914static int
3915assignment_helper(struct compiler *c, asdl_expr_seq *elts)
3916{
3917 Py_ssize_t n = asdl_seq_LEN(elts);
3918 RETURN_IF_FALSE(unpack_helper(c, elts));
3919 for (Py_ssize_t i = 0; i < n; i++) {
Brandt Bucherd5aa2e92020-03-07 19:44:18 -08003920 expr_ty elt = asdl_seq_GET(elts, i);
3921 VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value);
3922 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003923 return 1;
3924}
3925
3926static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003927compiler_list(struct compiler *c, expr_ty e)
3928{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003929 asdl_expr_seq *elts = e->v.List.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003930 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003931 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003932 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003933 else if (e->v.List.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003934 return starunpack_helper(c, elts, 0, BUILD_LIST,
3935 LIST_APPEND, LIST_EXTEND, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003936 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003937 else
3938 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003939 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003940}
3941
3942static int
3943compiler_tuple(struct compiler *c, expr_ty e)
3944{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003945 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003946 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003947 return assignment_helper(c, elts);
3948 }
3949 else if (e->v.Tuple.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003950 return starunpack_helper(c, elts, 0, BUILD_LIST,
3951 LIST_APPEND, LIST_EXTEND, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003952 }
3953 else
3954 VISIT_SEQ(c, expr, elts);
3955 return 1;
3956}
3957
3958static int
3959compiler_set(struct compiler *c, expr_ty e)
3960{
Mark Shannon13bc1392020-01-23 09:25:17 +00003961 return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET,
3962 SET_ADD, SET_UPDATE, 0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003963}
3964
3965static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003966are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003967{
3968 Py_ssize_t i;
3969 for (i = begin; i < end; i++) {
3970 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003971 if (key == NULL || key->kind != Constant_kind)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003972 return 0;
3973 }
3974 return 1;
3975}
3976
3977static int
3978compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3979{
3980 Py_ssize_t i, n = end - begin;
3981 PyObject *keys, *key;
Mark Shannon11e0b292021-04-15 14:28:56 +01003982 int big = n*2 > STACK_USE_GUIDELINE;
3983 if (n > 1 && !big && are_all_items_const(e->v.Dict.keys, begin, end)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003984 for (i = begin; i < end; i++) {
3985 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3986 }
3987 keys = PyTuple_New(n);
3988 if (keys == NULL) {
3989 return 0;
3990 }
3991 for (i = begin; i < end; i++) {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003992 key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003993 Py_INCREF(key);
3994 PyTuple_SET_ITEM(keys, i - begin, key);
3995 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003996 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003997 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
Mark Shannon11e0b292021-04-15 14:28:56 +01003998 return 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003999 }
Mark Shannon11e0b292021-04-15 14:28:56 +01004000 if (big) {
4001 ADDOP_I(c, BUILD_MAP, 0);
4002 }
4003 for (i = begin; i < end; i++) {
4004 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
4005 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
4006 if (big) {
4007 ADDOP_I(c, MAP_ADD, 1);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004008 }
Mark Shannon11e0b292021-04-15 14:28:56 +01004009 }
4010 if (!big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004011 ADDOP_I(c, BUILD_MAP, n);
4012 }
4013 return 1;
4014}
4015
4016static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004017compiler_dict(struct compiler *c, expr_ty e)
4018{
Victor Stinner976bb402016-03-23 11:36:19 +01004019 Py_ssize_t i, n, elements;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004020 int have_dict;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004021 int is_unpacking = 0;
4022 n = asdl_seq_LEN(e->v.Dict.values);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004023 have_dict = 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004024 elements = 0;
4025 for (i = 0; i < n; i++) {
4026 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004027 if (is_unpacking) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004028 if (elements) {
4029 if (!compiler_subdict(c, e, i - elements, i)) {
4030 return 0;
4031 }
4032 if (have_dict) {
4033 ADDOP_I(c, DICT_UPDATE, 1);
4034 }
4035 have_dict = 1;
4036 elements = 0;
4037 }
4038 if (have_dict == 0) {
4039 ADDOP_I(c, BUILD_MAP, 0);
4040 have_dict = 1;
4041 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004042 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Mark Shannon8a4cd702020-01-27 09:57:45 +00004043 ADDOP_I(c, DICT_UPDATE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004044 }
4045 else {
Mark Shannon11e0b292021-04-15 14:28:56 +01004046 if (elements*2 > STACK_USE_GUIDELINE) {
Pablo Galindoc51db0e2020-08-13 09:48:41 +01004047 if (!compiler_subdict(c, e, i - elements, i + 1)) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004048 return 0;
4049 }
4050 if (have_dict) {
4051 ADDOP_I(c, DICT_UPDATE, 1);
4052 }
4053 have_dict = 1;
4054 elements = 0;
4055 }
4056 else {
4057 elements++;
4058 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004059 }
4060 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004061 if (elements) {
4062 if (!compiler_subdict(c, e, n - elements, n)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004063 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004064 }
4065 if (have_dict) {
4066 ADDOP_I(c, DICT_UPDATE, 1);
4067 }
4068 have_dict = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004069 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004070 if (!have_dict) {
4071 ADDOP_I(c, BUILD_MAP, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004072 }
4073 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004074}
4075
4076static int
4077compiler_compare(struct compiler *c, expr_ty e)
4078{
Victor Stinnerad9a0662013-11-19 22:23:20 +01004079 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004080
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02004081 if (!check_compare(c, e)) {
4082 return 0;
4083 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004084 VISIT(c, expr, e->v.Compare.left);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004085 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
4086 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
4087 if (n == 0) {
4088 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Mark Shannon9af0e472020-01-14 10:12:45 +00004089 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004090 }
4091 else {
4092 basicblock *cleanup = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004093 if (cleanup == NULL)
4094 return 0;
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004095 for (i = 0; i < n; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004096 VISIT(c, expr,
4097 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004098 ADDOP(c, DUP_TOP);
4099 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00004100 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01004101 ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004102 NEXT_BLOCK(c);
4103 }
4104 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00004105 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004106 basicblock *end = compiler_new_block(c);
4107 if (end == NULL)
4108 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00004109 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004110 compiler_use_next_block(c, cleanup);
4111 ADDOP(c, ROT_TWO);
4112 ADDOP(c, POP_TOP);
4113 compiler_use_next_block(c, end);
4114 }
4115 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004116}
4117
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004118static PyTypeObject *
4119infer_type(expr_ty e)
4120{
4121 switch (e->kind) {
4122 case Tuple_kind:
4123 return &PyTuple_Type;
4124 case List_kind:
4125 case ListComp_kind:
4126 return &PyList_Type;
4127 case Dict_kind:
4128 case DictComp_kind:
4129 return &PyDict_Type;
4130 case Set_kind:
4131 case SetComp_kind:
4132 return &PySet_Type;
4133 case GeneratorExp_kind:
4134 return &PyGen_Type;
4135 case Lambda_kind:
4136 return &PyFunction_Type;
4137 case JoinedStr_kind:
4138 case FormattedValue_kind:
4139 return &PyUnicode_Type;
4140 case Constant_kind:
Victor Stinnera102ed72020-02-07 02:24:48 +01004141 return Py_TYPE(e->v.Constant.value);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004142 default:
4143 return NULL;
4144 }
4145}
4146
4147static int
4148check_caller(struct compiler *c, expr_ty e)
4149{
4150 switch (e->kind) {
4151 case Constant_kind:
4152 case Tuple_kind:
4153 case List_kind:
4154 case ListComp_kind:
4155 case Dict_kind:
4156 case DictComp_kind:
4157 case Set_kind:
4158 case SetComp_kind:
4159 case GeneratorExp_kind:
4160 case JoinedStr_kind:
4161 case FormattedValue_kind:
4162 return compiler_warn(c, "'%.200s' object is not callable; "
4163 "perhaps you missed a comma?",
4164 infer_type(e)->tp_name);
4165 default:
4166 return 1;
4167 }
4168}
4169
4170static int
4171check_subscripter(struct compiler *c, expr_ty e)
4172{
4173 PyObject *v;
4174
4175 switch (e->kind) {
4176 case Constant_kind:
4177 v = e->v.Constant.value;
4178 if (!(v == Py_None || v == Py_Ellipsis ||
4179 PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
4180 PyAnySet_Check(v)))
4181 {
4182 return 1;
4183 }
4184 /* fall through */
4185 case Set_kind:
4186 case SetComp_kind:
4187 case GeneratorExp_kind:
4188 case Lambda_kind:
4189 return compiler_warn(c, "'%.200s' object is not subscriptable; "
4190 "perhaps you missed a comma?",
4191 infer_type(e)->tp_name);
4192 default:
4193 return 1;
4194 }
4195}
4196
4197static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004198check_index(struct compiler *c, expr_ty e, expr_ty s)
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004199{
4200 PyObject *v;
4201
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004202 PyTypeObject *index_type = infer_type(s);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004203 if (index_type == NULL
4204 || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
4205 || index_type == &PySlice_Type) {
4206 return 1;
4207 }
4208
4209 switch (e->kind) {
4210 case Constant_kind:
4211 v = e->v.Constant.value;
4212 if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
4213 return 1;
4214 }
4215 /* fall through */
4216 case Tuple_kind:
4217 case List_kind:
4218 case ListComp_kind:
4219 case JoinedStr_kind:
4220 case FormattedValue_kind:
4221 return compiler_warn(c, "%.200s indices must be integers or slices, "
4222 "not %.200s; "
4223 "perhaps you missed a comma?",
4224 infer_type(e)->tp_name,
4225 index_type->tp_name);
4226 default:
4227 return 1;
4228 }
4229}
4230
Zackery Spytz97f5de02019-03-22 01:30:32 -06004231// Return 1 if the method call was optimized, -1 if not, and 0 on error.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004232static int
Yury Selivanovf2392132016-12-13 19:03:51 -05004233maybe_optimize_method_call(struct compiler *c, expr_ty e)
4234{
4235 Py_ssize_t argsl, i;
4236 expr_ty meth = e->v.Call.func;
Pablo Galindoa5634c42020-09-16 19:42:00 +01004237 asdl_expr_seq *args = e->v.Call.args;
Yury Selivanovf2392132016-12-13 19:03:51 -05004238
4239 /* Check that the call node is an attribute access, and that
4240 the call doesn't have keyword parameters. */
4241 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
Mark Shannon11e0b292021-04-15 14:28:56 +01004242 asdl_seq_LEN(e->v.Call.keywords)) {
Yury Selivanovf2392132016-12-13 19:03:51 -05004243 return -1;
Mark Shannon11e0b292021-04-15 14:28:56 +01004244 }
4245 /* Check that there aren't too many arguments */
Yury Selivanovf2392132016-12-13 19:03:51 -05004246 argsl = asdl_seq_LEN(args);
Mark Shannon11e0b292021-04-15 14:28:56 +01004247 if (argsl >= STACK_USE_GUIDELINE) {
4248 return -1;
4249 }
4250 /* Check that there are no *varargs types of arguments. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004251 for (i = 0; i < argsl; i++) {
4252 expr_ty elt = asdl_seq_GET(args, i);
4253 if (elt->kind == Starred_kind) {
4254 return -1;
4255 }
4256 }
4257
4258 /* Alright, we can optimize the code. */
4259 VISIT(c, expr, meth->v.Attribute.value);
Mark Shannond48848c2021-03-14 18:01:30 +00004260 int old_lineno = c->u->u_lineno;
4261 c->u->u_lineno = meth->end_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004262 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
4263 VISIT_SEQ(c, expr, e->v.Call.args);
4264 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
Mark Shannond48848c2021-03-14 18:01:30 +00004265 c->u->u_lineno = old_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004266 return 1;
4267}
4268
4269static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004270validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
Zackery Spytz08050e92020-04-06 00:47:47 -06004271{
4272 Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
4273 for (Py_ssize_t i = 0; i < nkeywords; i++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004274 keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
4275 if (key->arg == NULL) {
4276 continue;
4277 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +01004278 if (forbidden_name(c, key->arg, Store)) {
4279 return -1;
4280 }
Zackery Spytz08050e92020-04-06 00:47:47 -06004281 for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004282 keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
4283 if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07004284 SET_LOC(c, other);
Brandt Bucher145bf262021-02-26 14:51:55 -08004285 compiler_error(c, "keyword argument repeated: %U", key->arg);
Pablo Galindo254ec782020-04-03 20:37:13 +01004286 return -1;
4287 }
4288 }
4289 }
4290 return 0;
4291}
4292
4293static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004294compiler_call(struct compiler *c, expr_ty e)
4295{
Zackery Spytz97f5de02019-03-22 01:30:32 -06004296 int ret = maybe_optimize_method_call(c, e);
4297 if (ret >= 0) {
4298 return ret;
4299 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004300 if (!check_caller(c, e->v.Call.func)) {
4301 return 0;
4302 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004303 VISIT(c, expr, e->v.Call.func);
4304 return compiler_call_helper(c, 0,
4305 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004306 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004307}
4308
Eric V. Smith235a6f02015-09-19 14:51:32 -04004309static int
4310compiler_joined_str(struct compiler *c, expr_ty e)
4311{
Mark Shannon11e0b292021-04-15 14:28:56 +01004312
4313 Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values);
4314 if (value_count > STACK_USE_GUIDELINE) {
4315 ADDOP_LOAD_CONST_NEW(c, _PyUnicode_FromASCII("", 0));
4316 PyObject *join = _PyUnicode_FromASCII("join", 4);
4317 if (join == NULL) {
4318 return 0;
4319 }
4320 ADDOP_NAME(c, LOAD_METHOD, join, names);
4321 Py_DECREF(join);
4322 ADDOP_I(c, BUILD_LIST, 0);
4323 for (Py_ssize_t i = 0; i < asdl_seq_LEN(e->v.JoinedStr.values); i++) {
4324 VISIT(c, expr, asdl_seq_GET(e->v.JoinedStr.values, i));
4325 ADDOP_I(c, LIST_APPEND, 1);
4326 }
4327 ADDOP_I(c, CALL_METHOD, 1);
4328 }
4329 else {
4330 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
4331 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) {
4332 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
4333 }
4334 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004335 return 1;
4336}
4337
Eric V. Smitha78c7952015-11-03 12:45:05 -05004338/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004339static int
4340compiler_formatted_value(struct compiler *c, expr_ty e)
4341{
Eric V. Smitha78c7952015-11-03 12:45:05 -05004342 /* Our oparg encodes 2 pieces of information: the conversion
4343 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04004344
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004345 Convert the conversion char to 3 bits:
4346 : 000 0x0 FVC_NONE The default if nothing specified.
Eric V. Smitha78c7952015-11-03 12:45:05 -05004347 !s : 001 0x1 FVC_STR
4348 !r : 010 0x2 FVC_REPR
4349 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04004350
Eric V. Smitha78c7952015-11-03 12:45:05 -05004351 next bit is whether or not we have a format spec:
4352 yes : 100 0x4
4353 no : 000 0x0
4354 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004355
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004356 int conversion = e->v.FormattedValue.conversion;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004357 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004358
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004359 /* The expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004360 VISIT(c, expr, e->v.FormattedValue.value);
4361
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004362 switch (conversion) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004363 case 's': oparg = FVC_STR; break;
4364 case 'r': oparg = FVC_REPR; break;
4365 case 'a': oparg = FVC_ASCII; break;
4366 case -1: oparg = FVC_NONE; break;
4367 default:
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004368 PyErr_Format(PyExc_SystemError,
4369 "Unrecognized conversion character %d", conversion);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004370 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004371 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004372 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004373 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004374 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004375 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004376 }
4377
Eric V. Smitha78c7952015-11-03 12:45:05 -05004378 /* And push our opcode and oparg */
4379 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004380
Eric V. Smith235a6f02015-09-19 14:51:32 -04004381 return 1;
4382}
4383
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004384static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004385compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004386{
4387 Py_ssize_t i, n = end - begin;
4388 keyword_ty kw;
4389 PyObject *keys, *key;
4390 assert(n > 0);
Mark Shannon11e0b292021-04-15 14:28:56 +01004391 int big = n*2 > STACK_USE_GUIDELINE;
4392 if (n > 1 && !big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004393 for (i = begin; i < end; i++) {
4394 kw = asdl_seq_GET(keywords, i);
4395 VISIT(c, expr, kw->value);
4396 }
4397 keys = PyTuple_New(n);
4398 if (keys == NULL) {
4399 return 0;
4400 }
4401 for (i = begin; i < end; i++) {
4402 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
4403 Py_INCREF(key);
4404 PyTuple_SET_ITEM(keys, i - begin, key);
4405 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004406 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004407 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
Mark Shannon11e0b292021-04-15 14:28:56 +01004408 return 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004409 }
Mark Shannon11e0b292021-04-15 14:28:56 +01004410 if (big) {
4411 ADDOP_I_NOLINE(c, BUILD_MAP, 0);
4412 }
4413 for (i = begin; i < end; i++) {
4414 kw = asdl_seq_GET(keywords, i);
4415 ADDOP_LOAD_CONST(c, kw->arg);
4416 VISIT(c, expr, kw->value);
4417 if (big) {
4418 ADDOP_I_NOLINE(c, MAP_ADD, 1);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004419 }
Mark Shannon11e0b292021-04-15 14:28:56 +01004420 }
4421 if (!big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004422 ADDOP_I(c, BUILD_MAP, n);
4423 }
4424 return 1;
4425}
4426
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004427/* shared code between compiler_call and compiler_class */
4428static int
4429compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01004430 int n, /* Args already pushed */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004431 asdl_expr_seq *args,
4432 asdl_keyword_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004433{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004434 Py_ssize_t i, nseen, nelts, nkwelts;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004435
Pablo Galindo254ec782020-04-03 20:37:13 +01004436 if (validate_keywords(c, keywords) == -1) {
4437 return 0;
4438 }
4439
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004440 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004441 nkwelts = asdl_seq_LEN(keywords);
4442
Mark Shannon11e0b292021-04-15 14:28:56 +01004443 if (nelts + nkwelts*2 > STACK_USE_GUIDELINE) {
4444 goto ex_call;
4445 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004446 for (i = 0; i < nelts; i++) {
4447 expr_ty elt = asdl_seq_GET(args, i);
4448 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00004449 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004450 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004451 }
4452 for (i = 0; i < nkwelts; i++) {
4453 keyword_ty kw = asdl_seq_GET(keywords, i);
4454 if (kw->arg == NULL) {
4455 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004456 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004458
Mark Shannon13bc1392020-01-23 09:25:17 +00004459 /* No * or ** args, so can use faster calling sequence */
4460 for (i = 0; i < nelts; i++) {
4461 expr_ty elt = asdl_seq_GET(args, i);
4462 assert(elt->kind != Starred_kind);
4463 VISIT(c, expr, elt);
4464 }
4465 if (nkwelts) {
4466 PyObject *names;
4467 VISIT_SEQ(c, keyword, keywords);
4468 names = PyTuple_New(nkwelts);
4469 if (names == NULL) {
4470 return 0;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004471 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004472 for (i = 0; i < nkwelts; i++) {
4473 keyword_ty kw = asdl_seq_GET(keywords, i);
4474 Py_INCREF(kw->arg);
4475 PyTuple_SET_ITEM(names, i, kw->arg);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004476 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004477 ADDOP_LOAD_CONST_NEW(c, names);
4478 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
4479 return 1;
4480 }
4481 else {
4482 ADDOP_I(c, CALL_FUNCTION, n + nelts);
4483 return 1;
4484 }
4485
4486ex_call:
4487
4488 /* Do positional arguments. */
4489 if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
4490 VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
4491 }
4492 else if (starunpack_helper(c, args, n, BUILD_LIST,
4493 LIST_APPEND, LIST_EXTEND, 1) == 0) {
4494 return 0;
4495 }
4496 /* Then keyword arguments */
4497 if (nkwelts) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004498 /* Has a new dict been pushed */
4499 int have_dict = 0;
Mark Shannon13bc1392020-01-23 09:25:17 +00004500
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004501 nseen = 0; /* the number of keyword arguments on the stack following */
4502 for (i = 0; i < nkwelts; i++) {
4503 keyword_ty kw = asdl_seq_GET(keywords, i);
4504 if (kw->arg == NULL) {
4505 /* A keyword argument unpacking. */
4506 if (nseen) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004507 if (!compiler_subkwargs(c, keywords, i - nseen, i)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004508 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004509 }
Mark Shannondb64f122020-06-01 10:42:42 +01004510 if (have_dict) {
4511 ADDOP_I(c, DICT_MERGE, 1);
4512 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004513 have_dict = 1;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004514 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004515 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004516 if (!have_dict) {
4517 ADDOP_I(c, BUILD_MAP, 0);
4518 have_dict = 1;
4519 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004520 VISIT(c, expr, kw->value);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004521 ADDOP_I(c, DICT_MERGE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004522 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004523 else {
4524 nseen++;
4525 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004526 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004527 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004528 /* Pack up any trailing keyword arguments. */
Mark Shannon8a4cd702020-01-27 09:57:45 +00004529 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004530 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004531 }
4532 if (have_dict) {
4533 ADDOP_I(c, DICT_MERGE, 1);
4534 }
4535 have_dict = 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004536 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004537 assert(have_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004539 ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0);
4540 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004541}
4542
Nick Coghlan650f0d02007-04-15 12:05:43 +00004543
4544/* List and set comprehensions and generator expressions work by creating a
4545 nested function to perform the actual iteration. This means that the
4546 iteration variables don't leak into the current scope.
4547 The defined function is called immediately following its definition, with the
4548 result of that call being the result of the expression.
4549 The LC/SC version returns the populated container, while the GE version is
4550 flagged in symtable.c as a generator, so it returns the generator object
4551 when the function is called.
Nick Coghlan650f0d02007-04-15 12:05:43 +00004552
4553 Possible cleanups:
4554 - iterate over the generator sequence instead of using recursion
4555*/
4556
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004557
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004558static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004559compiler_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004560 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004561 int depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004562 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004563{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004564 comprehension_ty gen;
4565 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4566 if (gen->is_async) {
4567 return compiler_async_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 } else {
4570 return compiler_sync_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004571 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004572 }
4573}
4574
4575static int
4576compiler_sync_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004577 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004578 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004579 expr_ty elt, expr_ty val, int type)
4580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004581 /* generate code for the iterator, then each of the ifs,
4582 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004584 comprehension_ty gen;
4585 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004586 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004588 start = compiler_new_block(c);
4589 skip = compiler_new_block(c);
4590 if_cleanup = compiler_new_block(c);
4591 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004593 if (start == NULL || skip == NULL || if_cleanup == NULL ||
4594 anchor == NULL)
4595 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004597 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004599 if (gen_index == 0) {
4600 /* Receive outermost iter as an implicit argument */
4601 c->u->u_argcount = 1;
4602 ADDOP_I(c, LOAD_FAST, 0);
4603 }
4604 else {
4605 /* Sub-iter - calculate on the fly */
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004606 /* Fast path for the temporary variable assignment idiom:
4607 for y in [f(x)]
4608 */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004609 asdl_expr_seq *elts;
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004610 switch (gen->iter->kind) {
4611 case List_kind:
4612 elts = gen->iter->v.List.elts;
4613 break;
4614 case Tuple_kind:
4615 elts = gen->iter->v.Tuple.elts;
4616 break;
4617 default:
4618 elts = NULL;
4619 }
4620 if (asdl_seq_LEN(elts) == 1) {
4621 expr_ty elt = asdl_seq_GET(elts, 0);
4622 if (elt->kind != Starred_kind) {
4623 VISIT(c, expr, elt);
4624 start = NULL;
4625 }
4626 }
4627 if (start) {
4628 VISIT(c, expr, gen->iter);
4629 ADDOP(c, GET_ITER);
4630 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004631 }
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004632 if (start) {
4633 depth++;
4634 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01004635 ADDOP_JUMP(c, FOR_ITER, anchor);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004636 NEXT_BLOCK(c);
4637 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004638 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004640 /* XXX this needs to be cleaned up...a lot! */
4641 n = asdl_seq_LEN(gen->ifs);
4642 for (i = 0; i < n; i++) {
4643 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004644 if (!compiler_jump_if(c, e, if_cleanup, 0))
4645 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004646 NEXT_BLOCK(c);
4647 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004649 if (++gen_index < asdl_seq_LEN(generators))
4650 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004651 generators, gen_index, depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004652 elt, val, type))
4653 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004655 /* only append after the last for generator */
4656 if (gen_index >= asdl_seq_LEN(generators)) {
4657 /* comprehension specific code */
4658 switch (type) {
4659 case COMP_GENEXP:
4660 VISIT(c, expr, elt);
4661 ADDOP(c, YIELD_VALUE);
4662 ADDOP(c, POP_TOP);
4663 break;
4664 case COMP_LISTCOMP:
4665 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004666 ADDOP_I(c, LIST_APPEND, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004667 break;
4668 case COMP_SETCOMP:
4669 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004670 ADDOP_I(c, SET_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004671 break;
4672 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004673 /* With '{k: v}', k is evaluated before v, so we do
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004674 the same. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004675 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004676 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004677 ADDOP_I(c, MAP_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004678 break;
4679 default:
4680 return 0;
4681 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004683 compiler_use_next_block(c, skip);
4684 }
4685 compiler_use_next_block(c, if_cleanup);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004686 if (start) {
Mark Shannon582aaf12020-08-04 17:30:11 +01004687 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004688 compiler_use_next_block(c, anchor);
4689 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004690
4691 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004692}
4693
4694static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004695compiler_async_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004696 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004697 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004698 expr_ty elt, expr_ty val, int type)
4699{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004700 comprehension_ty gen;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004701 basicblock *start, *if_cleanup, *except;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004702 Py_ssize_t i, n;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004703 start = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004704 except = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004705 if_cleanup = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004706
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004707 if (start == NULL || if_cleanup == NULL || except == NULL) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004708 return 0;
4709 }
4710
4711 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4712
4713 if (gen_index == 0) {
4714 /* Receive outermost iter as an implicit argument */
4715 c->u->u_argcount = 1;
4716 ADDOP_I(c, LOAD_FAST, 0);
4717 }
4718 else {
4719 /* Sub-iter - calculate on the fly */
4720 VISIT(c, expr, gen->iter);
4721 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004722 }
4723
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004724 compiler_use_next_block(c, start);
tomKPZ7a7ba3d2021-04-07 07:43:45 -07004725 /* Runtime will push a block here, so we need to account for that */
4726 if (!compiler_push_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start,
4727 NULL, NULL)) {
4728 return 0;
4729 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004730
Mark Shannon582aaf12020-08-04 17:30:11 +01004731 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004732 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004733 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004734 ADDOP(c, YIELD_FROM);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004735 ADDOP(c, POP_BLOCK);
Serhiy Storchaka24d32012018-03-10 18:22:34 +02004736 VISIT(c, expr, gen->target);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004737
4738 n = asdl_seq_LEN(gen->ifs);
4739 for (i = 0; i < n; i++) {
4740 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004741 if (!compiler_jump_if(c, e, if_cleanup, 0))
4742 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004743 NEXT_BLOCK(c);
4744 }
4745
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004746 depth++;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004747 if (++gen_index < asdl_seq_LEN(generators))
4748 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004749 generators, gen_index, depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004750 elt, val, type))
4751 return 0;
4752
4753 /* only append after the last for generator */
4754 if (gen_index >= asdl_seq_LEN(generators)) {
4755 /* comprehension specific code */
4756 switch (type) {
4757 case COMP_GENEXP:
4758 VISIT(c, expr, elt);
4759 ADDOP(c, YIELD_VALUE);
4760 ADDOP(c, POP_TOP);
4761 break;
4762 case COMP_LISTCOMP:
4763 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004764 ADDOP_I(c, LIST_APPEND, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004765 break;
4766 case COMP_SETCOMP:
4767 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004768 ADDOP_I(c, SET_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004769 break;
4770 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004771 /* With '{k: v}', k is evaluated before v, so we do
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004772 the same. */
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004773 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004774 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004775 ADDOP_I(c, MAP_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004776 break;
4777 default:
4778 return 0;
4779 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004780 }
4781 compiler_use_next_block(c, if_cleanup);
Mark Shannon582aaf12020-08-04 17:30:11 +01004782 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004783
tomKPZ7a7ba3d2021-04-07 07:43:45 -07004784 compiler_pop_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start);
4785
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004786 compiler_use_next_block(c, except);
4787 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004788
4789 return 1;
4790}
4791
4792static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004793compiler_comprehension(struct compiler *c, expr_ty e, int type,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004794 identifier name, asdl_comprehension_seq *generators, expr_ty elt,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004795 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004796{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004797 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004798 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004799 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004800 int is_async_generator = 0;
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004801 int top_level_await = IS_TOP_LEVEL_AWAIT(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004802
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004803
Batuhan Taßkaya9052f7a2020-03-19 14:35:44 +03004804 int is_async_function = c->u->u_ste->ste_coroutine;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004805
Batuhan Taßkaya9052f7a2020-03-19 14:35:44 +03004806 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004807 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4808 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004809 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004810 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004811 }
Mark Shannon7674c832021-06-21 11:47:16 +01004812 SET_LOC(c, e);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004813
4814 is_async_generator = c->u->u_ste->ste_coroutine;
4815
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004816 if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004817 compiler_error(c, "asynchronous comprehension outside of "
4818 "an asynchronous function");
4819 goto error_in_scope;
4820 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004822 if (type != COMP_GENEXP) {
4823 int op;
4824 switch (type) {
4825 case COMP_LISTCOMP:
4826 op = BUILD_LIST;
4827 break;
4828 case COMP_SETCOMP:
4829 op = BUILD_SET;
4830 break;
4831 case COMP_DICTCOMP:
4832 op = BUILD_MAP;
4833 break;
4834 default:
4835 PyErr_Format(PyExc_SystemError,
4836 "unknown comprehension type %d", type);
4837 goto error_in_scope;
4838 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004840 ADDOP_I(c, op, 0);
4841 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004842
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004843 if (!compiler_comprehension_generator(c, generators, 0, 0, elt,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004844 val, type))
4845 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004847 if (type != COMP_GENEXP) {
4848 ADDOP(c, RETURN_VALUE);
4849 }
4850
4851 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004852 qualname = c->u->u_qualname;
4853 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004854 compiler_exit_scope(c);
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004855 if (top_level_await && is_async_generator){
4856 c->u->u_ste->ste_coroutine = 1;
4857 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004858 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004859 goto error;
4860
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004861 if (!compiler_make_closure(c, co, 0, qualname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004862 goto error;
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004863 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004864 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004865 Py_DECREF(co);
4866
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004867 VISIT(c, expr, outermost->iter);
4868
4869 if (outermost->is_async) {
4870 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004871 } else {
4872 ADDOP(c, GET_ITER);
4873 }
4874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004875 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004876
4877 if (is_async_generator && type != COMP_GENEXP) {
4878 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004879 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004880 ADDOP(c, YIELD_FROM);
4881 }
4882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004883 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004884error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004885 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004886error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004887 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004888 Py_XDECREF(co);
4889 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004890}
4891
4892static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004893compiler_genexp(struct compiler *c, expr_ty e)
4894{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004895 static identifier name;
4896 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004897 name = PyUnicode_InternFromString("<genexpr>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004898 if (!name)
4899 return 0;
4900 }
4901 assert(e->kind == GeneratorExp_kind);
4902 return compiler_comprehension(c, e, COMP_GENEXP, name,
4903 e->v.GeneratorExp.generators,
4904 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004905}
4906
4907static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004908compiler_listcomp(struct compiler *c, expr_ty e)
4909{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004910 static identifier name;
4911 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004912 name = PyUnicode_InternFromString("<listcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004913 if (!name)
4914 return 0;
4915 }
4916 assert(e->kind == ListComp_kind);
4917 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4918 e->v.ListComp.generators,
4919 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004920}
4921
4922static int
4923compiler_setcomp(struct compiler *c, expr_ty e)
4924{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004925 static identifier name;
4926 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004927 name = PyUnicode_InternFromString("<setcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004928 if (!name)
4929 return 0;
4930 }
4931 assert(e->kind == SetComp_kind);
4932 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4933 e->v.SetComp.generators,
4934 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004935}
4936
4937
4938static int
4939compiler_dictcomp(struct compiler *c, expr_ty e)
4940{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004941 static identifier name;
4942 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004943 name = PyUnicode_InternFromString("<dictcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004944 if (!name)
4945 return 0;
4946 }
4947 assert(e->kind == DictComp_kind);
4948 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4949 e->v.DictComp.generators,
4950 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004951}
4952
4953
4954static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004955compiler_visit_keyword(struct compiler *c, keyword_ty k)
4956{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004957 VISIT(c, expr, k->value);
4958 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004959}
4960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004961/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004962 whether they are true or false.
4963
4964 Return values: 1 for true, 0 for false, -1 for non-constant.
4965 */
4966
4967static int
Mark Shannonfee55262019-11-21 09:11:43 +00004968compiler_with_except_finish(struct compiler *c) {
4969 basicblock *exit;
4970 exit = compiler_new_block(c);
4971 if (exit == NULL)
4972 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01004973 ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit);
Mark Shannon266b4622020-11-17 19:30:14 +00004974 NEXT_BLOCK(c);
Mark Shannonbf353f32020-12-17 13:55:28 +00004975 ADDOP_I(c, RERAISE, 1);
Mark Shannonfee55262019-11-21 09:11:43 +00004976 compiler_use_next_block(c, exit);
4977 ADDOP(c, POP_TOP);
4978 ADDOP(c, POP_TOP);
4979 ADDOP(c, POP_TOP);
4980 ADDOP(c, POP_EXCEPT);
4981 ADDOP(c, POP_TOP);
4982 return 1;
4983}
Yury Selivanov75445082015-05-11 22:57:16 -04004984
4985/*
4986 Implements the async with statement.
4987
4988 The semantics outlined in that PEP are as follows:
4989
4990 async with EXPR as VAR:
4991 BLOCK
4992
4993 It is implemented roughly as:
4994
4995 context = EXPR
4996 exit = context.__aexit__ # not calling it
4997 value = await context.__aenter__()
4998 try:
4999 VAR = value # if VAR present in the syntax
5000 BLOCK
5001 finally:
5002 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03005003 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04005004 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03005005 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04005006 if not (await exit(*exc)):
5007 raise
5008 */
5009static int
5010compiler_async_with(struct compiler *c, stmt_ty s, int pos)
5011{
Mark Shannonfee55262019-11-21 09:11:43 +00005012 basicblock *block, *final, *exit;
Yury Selivanov75445082015-05-11 22:57:16 -04005013 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
5014
5015 assert(s->kind == AsyncWith_kind);
Pablo Galindo90235812020-03-15 04:29:22 +00005016 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005017 c->u->u_ste->ste_coroutine = 1;
5018 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
Zsolt Dollensteine2396502018-04-27 08:58:56 -07005019 return compiler_error(c, "'async with' outside async function");
5020 }
Yury Selivanov75445082015-05-11 22:57:16 -04005021
5022 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00005023 final = compiler_new_block(c);
5024 exit = compiler_new_block(c);
5025 if (!block || !final || !exit)
Yury Selivanov75445082015-05-11 22:57:16 -04005026 return 0;
5027
5028 /* Evaluate EXPR */
5029 VISIT(c, expr, item->context_expr);
5030
5031 ADDOP(c, BEFORE_ASYNC_WITH);
5032 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005033 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005034 ADDOP(c, YIELD_FROM);
5035
Mark Shannon582aaf12020-08-04 17:30:11 +01005036 ADDOP_JUMP(c, SETUP_ASYNC_WITH, final);
Yury Selivanov75445082015-05-11 22:57:16 -04005037
5038 /* SETUP_ASYNC_WITH pushes a finally block. */
5039 compiler_use_next_block(c, block);
Mark Shannon5979e812021-04-30 14:32:47 +01005040 if (!compiler_push_fblock(c, ASYNC_WITH, block, final, s)) {
Yury Selivanov75445082015-05-11 22:57:16 -04005041 return 0;
5042 }
5043
5044 if (item->optional_vars) {
5045 VISIT(c, expr, item->optional_vars);
5046 }
5047 else {
5048 /* Discard result from context.__aenter__() */
5049 ADDOP(c, POP_TOP);
5050 }
5051
5052 pos++;
5053 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
5054 /* BLOCK code */
5055 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
5056 else if (!compiler_async_with(c, s, pos))
5057 return 0;
5058
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005059 compiler_pop_fblock(c, ASYNC_WITH, block);
Mark Shannonfee55262019-11-21 09:11:43 +00005060 ADDOP(c, POP_BLOCK);
5061 /* End of body; start the cleanup */
Yury Selivanov75445082015-05-11 22:57:16 -04005062
Mark Shannonfee55262019-11-21 09:11:43 +00005063 /* For successful outcome:
5064 * call __exit__(None, None, None)
5065 */
Mark Shannon5979e812021-04-30 14:32:47 +01005066 SET_LOC(c, s);
Mark Shannonfee55262019-11-21 09:11:43 +00005067 if(!compiler_call_exit_with_nones(c))
Yury Selivanov75445082015-05-11 22:57:16 -04005068 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00005069 ADDOP(c, GET_AWAITABLE);
Miss Islington (bot)ebbd0ac2021-08-31 11:08:32 -07005070 ADDOP_LOAD_CONST(c, Py_None);
Mark Shannonfee55262019-11-21 09:11:43 +00005071 ADDOP(c, YIELD_FROM);
Yury Selivanov75445082015-05-11 22:57:16 -04005072
Mark Shannonfee55262019-11-21 09:11:43 +00005073 ADDOP(c, POP_TOP);
Yury Selivanov75445082015-05-11 22:57:16 -04005074
Mark Shannon582aaf12020-08-04 17:30:11 +01005075 ADDOP_JUMP(c, JUMP_ABSOLUTE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00005076
5077 /* For exceptional outcome: */
5078 compiler_use_next_block(c, final);
Mark Shannonfee55262019-11-21 09:11:43 +00005079 ADDOP(c, WITH_EXCEPT_START);
Yury Selivanov75445082015-05-11 22:57:16 -04005080 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005081 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005082 ADDOP(c, YIELD_FROM);
Mark Shannonfee55262019-11-21 09:11:43 +00005083 compiler_with_except_finish(c);
Yury Selivanov75445082015-05-11 22:57:16 -04005084
Mark Shannonfee55262019-11-21 09:11:43 +00005085compiler_use_next_block(c, exit);
Yury Selivanov75445082015-05-11 22:57:16 -04005086 return 1;
5087}
5088
5089
Guido van Rossumc2e20742006-02-27 22:32:47 +00005090/*
5091 Implements the with statement from PEP 343.
Guido van Rossumc2e20742006-02-27 22:32:47 +00005092 with EXPR as VAR:
5093 BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00005094 is implemented as:
5095 <code for EXPR>
5096 SETUP_WITH E
5097 <code to store to VAR> or POP_TOP
5098 <code for BLOCK>
5099 LOAD_CONST (None, None, None)
5100 CALL_FUNCTION_EX 0
5101 JUMP_FORWARD EXIT
5102 E: WITH_EXCEPT_START (calls EXPR.__exit__)
5103 POP_JUMP_IF_TRUE T:
5104 RERAISE
5105 T: POP_TOP * 3 (remove exception from stack)
5106 POP_EXCEPT
5107 POP_TOP
5108 EXIT:
Guido van Rossumc2e20742006-02-27 22:32:47 +00005109 */
Mark Shannonfee55262019-11-21 09:11:43 +00005110
Guido van Rossumc2e20742006-02-27 22:32:47 +00005111static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005112compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00005113{
Mark Shannonfee55262019-11-21 09:11:43 +00005114 basicblock *block, *final, *exit;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005115 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005116
5117 assert(s->kind == With_kind);
5118
Guido van Rossumc2e20742006-02-27 22:32:47 +00005119 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00005120 final = compiler_new_block(c);
5121 exit = compiler_new_block(c);
5122 if (!block || !final || !exit)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005123 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005124
Thomas Wouters477c8d52006-05-27 19:21:47 +00005125 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005126 VISIT(c, expr, item->context_expr);
Mark Shannonfee55262019-11-21 09:11:43 +00005127 /* Will push bound __exit__ */
Mark Shannon582aaf12020-08-04 17:30:11 +01005128 ADDOP_JUMP(c, SETUP_WITH, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005129
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005130 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00005131 compiler_use_next_block(c, block);
Mark Shannon5979e812021-04-30 14:32:47 +01005132 if (!compiler_push_fblock(c, WITH, block, final, s)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005133 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005134 }
5135
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005136 if (item->optional_vars) {
5137 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005138 }
5139 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005140 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005141 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005142 }
5143
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005144 pos++;
5145 if (pos == asdl_seq_LEN(s->v.With.items))
5146 /* BLOCK code */
5147 VISIT_SEQ(c, stmt, s->v.With.body)
5148 else if (!compiler_with(c, s, pos))
5149 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005150
Mark Shannon3bd60352021-01-13 12:05:43 +00005151
5152 /* Mark all following code as artificial */
5153 c->u->u_lineno = -1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005154 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005155 compiler_pop_fblock(c, WITH, block);
Mark Shannon13bc1392020-01-23 09:25:17 +00005156
Mark Shannonfee55262019-11-21 09:11:43 +00005157 /* End of body; start the cleanup. */
Mark Shannon13bc1392020-01-23 09:25:17 +00005158
Mark Shannonfee55262019-11-21 09:11:43 +00005159 /* For successful outcome:
5160 * call __exit__(None, None, None)
5161 */
Mark Shannon5979e812021-04-30 14:32:47 +01005162 SET_LOC(c, s);
Mark Shannonfee55262019-11-21 09:11:43 +00005163 if (!compiler_call_exit_with_nones(c))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005164 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00005165 ADDOP(c, POP_TOP);
Mark Shannon582aaf12020-08-04 17:30:11 +01005166 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005167
Mark Shannonfee55262019-11-21 09:11:43 +00005168 /* For exceptional outcome: */
5169 compiler_use_next_block(c, final);
Mark Shannonfee55262019-11-21 09:11:43 +00005170 ADDOP(c, WITH_EXCEPT_START);
5171 compiler_with_except_finish(c);
5172
5173 compiler_use_next_block(c, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005174 return 1;
5175}
5176
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005177static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005178compiler_visit_expr1(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005180 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07005181 case NamedExpr_kind:
5182 VISIT(c, expr, e->v.NamedExpr.value);
5183 ADDOP(c, DUP_TOP);
5184 VISIT(c, expr, e->v.NamedExpr.target);
5185 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005186 case BoolOp_kind:
5187 return compiler_boolop(c, e);
5188 case BinOp_kind:
5189 VISIT(c, expr, e->v.BinOp.left);
5190 VISIT(c, expr, e->v.BinOp.right);
Andy Lester76d58772020-03-10 21:18:12 -05005191 ADDOP(c, binop(e->v.BinOp.op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005192 break;
5193 case UnaryOp_kind:
5194 VISIT(c, expr, e->v.UnaryOp.operand);
5195 ADDOP(c, unaryop(e->v.UnaryOp.op));
5196 break;
5197 case Lambda_kind:
5198 return compiler_lambda(c, e);
5199 case IfExp_kind:
5200 return compiler_ifexp(c, e);
5201 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005202 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005203 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005204 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005205 case GeneratorExp_kind:
5206 return compiler_genexp(c, e);
5207 case ListComp_kind:
5208 return compiler_listcomp(c, e);
5209 case SetComp_kind:
5210 return compiler_setcomp(c, e);
5211 case DictComp_kind:
5212 return compiler_dictcomp(c, e);
5213 case Yield_kind:
5214 if (c->u->u_ste->ste_type != FunctionBlock)
5215 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005216 if (e->v.Yield.value) {
5217 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005218 }
5219 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005220 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005221 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005222 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005223 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005224 case YieldFrom_kind:
5225 if (c->u->u_ste->ste_type != FunctionBlock)
5226 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04005227
5228 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
5229 return compiler_error(c, "'yield from' inside async function");
5230
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005231 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04005232 ADDOP(c, GET_YIELD_FROM_ITER);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005233 ADDOP_LOAD_CONST(c, Py_None);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005234 ADDOP(c, YIELD_FROM);
5235 break;
Yury Selivanov75445082015-05-11 22:57:16 -04005236 case Await_kind:
Pablo Galindo90235812020-03-15 04:29:22 +00005237 if (!IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005238 if (c->u->u_ste->ste_type != FunctionBlock){
5239 return compiler_error(c, "'await' outside function");
5240 }
Yury Selivanov75445082015-05-11 22:57:16 -04005241
Victor Stinner331a6a52019-05-27 16:39:22 +02005242 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005243 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
5244 return compiler_error(c, "'await' outside async function");
5245 }
5246 }
Yury Selivanov75445082015-05-11 22:57:16 -04005247
5248 VISIT(c, expr, e->v.Await.value);
5249 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005250 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005251 ADDOP(c, YIELD_FROM);
5252 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005253 case Compare_kind:
5254 return compiler_compare(c, e);
5255 case Call_kind:
5256 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005257 case Constant_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005258 ADDOP_LOAD_CONST(c, e->v.Constant.value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005259 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005260 case JoinedStr_kind:
5261 return compiler_joined_str(c, e);
5262 case FormattedValue_kind:
5263 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005264 /* The following exprs can be assignment targets. */
5265 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005266 VISIT(c, expr, e->v.Attribute.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005267 switch (e->v.Attribute.ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005268 case Load:
Mark Shannond48848c2021-03-14 18:01:30 +00005269 {
5270 int old_lineno = c->u->u_lineno;
5271 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005272 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005273 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005274 break;
Mark Shannond48848c2021-03-14 18:01:30 +00005275 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005276 case Store:
Mark Shannond48848c2021-03-14 18:01:30 +00005277 if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005278 return 0;
Mark Shannond48848c2021-03-14 18:01:30 +00005279 }
5280 int old_lineno = c->u->u_lineno;
5281 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005282 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005283 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005284 break;
5285 case Del:
5286 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
5287 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005288 }
5289 break;
5290 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005291 return compiler_subscript(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005292 case Starred_kind:
5293 switch (e->v.Starred.ctx) {
5294 case Store:
5295 /* In all legitimate cases, the Starred node was already replaced
5296 * by compiler_list/compiler_tuple. XXX: is that okay? */
5297 return compiler_error(c,
5298 "starred assignment target must be in a list or tuple");
5299 default:
5300 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005301 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005302 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005303 break;
5304 case Slice_kind:
5305 return compiler_slice(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005306 case Name_kind:
5307 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
5308 /* child nodes of List and Tuple will have expr_context set */
5309 case List_kind:
5310 return compiler_list(c, e);
5311 case Tuple_kind:
5312 return compiler_tuple(c, e);
5313 }
5314 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005315}
5316
5317static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005318compiler_visit_expr(struct compiler *c, expr_ty e)
5319{
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005320 int old_lineno = c->u->u_lineno;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005321 int old_end_lineno = c->u->u_end_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005322 int old_col_offset = c->u->u_col_offset;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005323 int old_end_col_offset = c->u->u_end_col_offset;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005324 SET_LOC(c, e);
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005325 int res = compiler_visit_expr1(c, e);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005326 c->u->u_lineno = old_lineno;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005327 c->u->u_end_lineno = old_end_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005328 c->u->u_col_offset = old_col_offset;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005329 c->u->u_end_col_offset = old_end_col_offset;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005330 return res;
5331}
5332
5333static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005334compiler_augassign(struct compiler *c, stmt_ty s)
5335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005336 assert(s->kind == AugAssign_kind);
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005337 expr_ty e = s->v.AugAssign.target;
5338
5339 int old_lineno = c->u->u_lineno;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005340 int old_end_lineno = c->u->u_end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005341 int old_col_offset = c->u->u_col_offset;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005342 int old_end_col_offset = c->u->u_end_col_offset;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005343 SET_LOC(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005345 switch (e->kind) {
5346 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005347 VISIT(c, expr, e->v.Attribute.value);
5348 ADDOP(c, DUP_TOP);
Mark Shannond48848c2021-03-14 18:01:30 +00005349 int old_lineno = c->u->u_lineno;
5350 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005351 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005352 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005353 break;
5354 case Subscript_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005355 VISIT(c, expr, e->v.Subscript.value);
5356 VISIT(c, expr, e->v.Subscript.slice);
5357 ADDOP(c, DUP_TOP_TWO);
5358 ADDOP(c, BINARY_SUBSCR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005359 break;
5360 case Name_kind:
5361 if (!compiler_nameop(c, e->v.Name.id, Load))
5362 return 0;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005363 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005364 default:
5365 PyErr_Format(PyExc_SystemError,
5366 "invalid node type (%d) for augmented assignment",
5367 e->kind);
5368 return 0;
5369 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005370
5371 c->u->u_lineno = old_lineno;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005372 c->u->u_end_lineno = old_end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005373 c->u->u_col_offset = old_col_offset;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005374 c->u->u_end_col_offset = old_end_col_offset;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005375
5376 VISIT(c, expr, s->v.AugAssign.value);
5377 ADDOP(c, inplace_binop(s->v.AugAssign.op));
5378
5379 SET_LOC(c, e);
5380
5381 switch (e->kind) {
5382 case Attribute_kind:
Mark Shannond48848c2021-03-14 18:01:30 +00005383 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005384 ADDOP(c, ROT_TWO);
5385 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5386 break;
5387 case Subscript_kind:
5388 ADDOP(c, ROT_THREE);
5389 ADDOP(c, STORE_SUBSCR);
5390 break;
5391 case Name_kind:
5392 return compiler_nameop(c, e->v.Name.id, Store);
5393 default:
5394 Py_UNREACHABLE();
5395 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005396 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005397}
5398
5399static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005400check_ann_expr(struct compiler *c, expr_ty e)
5401{
5402 VISIT(c, expr, e);
5403 ADDOP(c, POP_TOP);
5404 return 1;
5405}
5406
5407static int
5408check_annotation(struct compiler *c, stmt_ty s)
5409{
Batuhan Taskaya8cc3cfa2021-04-25 05:31:20 +03005410 /* Annotations of complex targets does not produce anything
5411 under annotations future */
5412 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
5413 return 1;
5414 }
5415
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005416 /* Annotations are only evaluated in a module or class. */
5417 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5418 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
5419 return check_ann_expr(c, s->v.AnnAssign.annotation);
5420 }
5421 return 1;
5422}
5423
5424static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005425check_ann_subscr(struct compiler *c, expr_ty e)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005426{
5427 /* We check that everything in a subscript is defined at runtime. */
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005428 switch (e->kind) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005429 case Slice_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005430 if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005431 return 0;
5432 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005433 if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) {
5434 return 0;
5435 }
5436 if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) {
5437 return 0;
5438 }
5439 return 1;
5440 case Tuple_kind: {
5441 /* extended slice */
Pablo Galindoa5634c42020-09-16 19:42:00 +01005442 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005443 Py_ssize_t i, n = asdl_seq_LEN(elts);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005444 for (i = 0; i < n; i++) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005445 if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005446 return 0;
5447 }
5448 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005449 return 1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005450 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005451 default:
5452 return check_ann_expr(c, e);
5453 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005454}
5455
5456static int
5457compiler_annassign(struct compiler *c, stmt_ty s)
5458{
5459 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07005460 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005461
5462 assert(s->kind == AnnAssign_kind);
5463
5464 /* We perform the actual assignment first. */
5465 if (s->v.AnnAssign.value) {
5466 VISIT(c, expr, s->v.AnnAssign.value);
5467 VISIT(c, expr, targ);
5468 }
5469 switch (targ->kind) {
5470 case Name_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005471 if (forbidden_name(c, targ->v.Name.id, Store))
5472 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005473 /* If we have a simple name in a module or class, store annotation. */
5474 if (s->v.AnnAssign.simple &&
5475 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5476 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Pablo Galindob0544ba2021-04-21 12:41:19 +01005477 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
5478 VISIT(c, annexpr, s->v.AnnAssign.annotation)
5479 }
5480 else {
5481 VISIT(c, expr, s->v.AnnAssign.annotation);
5482 }
Mark Shannon332cd5e2018-01-30 00:41:04 +00005483 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02005484 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005485 ADDOP_LOAD_CONST_NEW(c, mangled);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005486 ADDOP(c, STORE_SUBSCR);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005487 }
5488 break;
5489 case Attribute_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005490 if (forbidden_name(c, targ->v.Attribute.attr, Store))
5491 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005492 if (!s->v.AnnAssign.value &&
5493 !check_ann_expr(c, targ->v.Attribute.value)) {
5494 return 0;
5495 }
5496 break;
5497 case Subscript_kind:
5498 if (!s->v.AnnAssign.value &&
5499 (!check_ann_expr(c, targ->v.Subscript.value) ||
5500 !check_ann_subscr(c, targ->v.Subscript.slice))) {
5501 return 0;
5502 }
5503 break;
5504 default:
5505 PyErr_Format(PyExc_SystemError,
5506 "invalid node type (%d) for annotated assignment",
5507 targ->kind);
5508 return 0;
5509 }
5510 /* Annotation is evaluated last. */
5511 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
5512 return 0;
5513 }
5514 return 1;
5515}
5516
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005517/* Raises a SyntaxError and returns 0.
5518 If something goes wrong, a different exception may be raised.
5519*/
5520
5521static int
Brandt Bucher145bf262021-02-26 14:51:55 -08005522compiler_error(struct compiler *c, const char *format, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005523{
Brandt Bucher145bf262021-02-26 14:51:55 -08005524 va_list vargs;
5525#ifdef HAVE_STDARG_PROTOTYPES
5526 va_start(vargs, format);
5527#else
5528 va_start(vargs);
5529#endif
5530 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5531 va_end(vargs);
5532 if (msg == NULL) {
5533 return 0;
5534 }
5535 PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
5536 if (loc == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005537 Py_INCREF(Py_None);
5538 loc = Py_None;
5539 }
Pablo Galindoa77aac42021-04-23 14:27:05 +01005540 PyObject *args = Py_BuildValue("O(OiiOii)", msg, c->c_filename,
5541 c->u->u_lineno, c->u->u_col_offset + 1, loc,
5542 c->u->u_end_lineno, c->u->u_end_col_offset + 1);
Brandt Bucher145bf262021-02-26 14:51:55 -08005543 Py_DECREF(msg);
5544 if (args == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005545 goto exit;
Brandt Bucher145bf262021-02-26 14:51:55 -08005546 }
5547 PyErr_SetObject(PyExc_SyntaxError, args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005548 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005549 Py_DECREF(loc);
Brandt Bucher145bf262021-02-26 14:51:55 -08005550 Py_XDECREF(args);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005551 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005552}
5553
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005554/* Emits a SyntaxWarning and returns 1 on success.
5555 If a SyntaxWarning raised as error, replaces it with a SyntaxError
5556 and returns 0.
5557*/
5558static int
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005559compiler_warn(struct compiler *c, const char *format, ...)
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005560{
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005561 va_list vargs;
5562#ifdef HAVE_STDARG_PROTOTYPES
5563 va_start(vargs, format);
5564#else
5565 va_start(vargs);
5566#endif
5567 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5568 va_end(vargs);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005569 if (msg == NULL) {
5570 return 0;
5571 }
5572 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
5573 c->u->u_lineno, NULL, NULL) < 0)
5574 {
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005575 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005576 /* Replace the SyntaxWarning exception with a SyntaxError
5577 to get a more accurate error report */
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005578 PyErr_Clear();
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005579 assert(PyUnicode_AsUTF8(msg) != NULL);
5580 compiler_error(c, PyUnicode_AsUTF8(msg));
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005581 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005582 Py_DECREF(msg);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005583 return 0;
5584 }
5585 Py_DECREF(msg);
5586 return 1;
5587}
5588
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005589static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005590compiler_subscript(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005591{
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005592 expr_context_ty ctx = e->v.Subscript.ctx;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005593 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005594
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005595 if (ctx == Load) {
5596 if (!check_subscripter(c, e->v.Subscript.value)) {
5597 return 0;
5598 }
5599 if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
5600 return 0;
5601 }
5602 }
5603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005604 switch (ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005605 case Load: op = BINARY_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005606 case Store: op = STORE_SUBSCR; break;
5607 case Del: op = DELETE_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005608 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005609 assert(op);
5610 VISIT(c, expr, e->v.Subscript.value);
5611 VISIT(c, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005612 ADDOP(c, op);
5613 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005614}
5615
5616static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005617compiler_slice(struct compiler *c, expr_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005619 int n = 2;
5620 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005622 /* only handles the cases where BUILD_SLICE is emitted */
5623 if (s->v.Slice.lower) {
5624 VISIT(c, expr, s->v.Slice.lower);
5625 }
5626 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005627 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005628 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005630 if (s->v.Slice.upper) {
5631 VISIT(c, expr, s->v.Slice.upper);
5632 }
5633 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005634 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005635 }
5636
5637 if (s->v.Slice.step) {
5638 n++;
5639 VISIT(c, expr, s->v.Slice.step);
5640 }
5641 ADDOP_I(c, BUILD_SLICE, n);
5642 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005643}
5644
Brandt Bucher145bf262021-02-26 14:51:55 -08005645
5646// PEP 634: Structural Pattern Matching
5647
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005648// To keep things simple, all compiler_pattern_* and pattern_helper_* routines
5649// follow the convention of consuming TOS (the subject for the given pattern)
5650// and calling jump_to_fail_pop on failure (no match).
5651
5652// When calling into these routines, it's important that pc->on_top be kept
5653// updated to reflect the current number of items that we are using on the top
5654// of the stack: they will be popped on failure, and any name captures will be
5655// stored *underneath* them on success. This lets us defer all names stores
5656// until the *entire* pattern matches.
Brandt Bucher145bf262021-02-26 14:51:55 -08005657
Brandt Bucher145bf262021-02-26 14:51:55 -08005658#define WILDCARD_CHECK(N) \
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005659 ((N)->kind == MatchAs_kind && !(N)->v.MatchAs.name)
Brandt Bucher145bf262021-02-26 14:51:55 -08005660
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005661#define WILDCARD_STAR_CHECK(N) \
5662 ((N)->kind == MatchStar_kind && !(N)->v.MatchStar.name)
5663
5664// Limit permitted subexpressions, even if the parser & AST validator let them through
5665#define MATCH_VALUE_EXPR(N) \
5666 ((N)->kind == Constant_kind || (N)->kind == Attribute_kind)
Brandt Bucher145bf262021-02-26 14:51:55 -08005667
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005668// Allocate or resize pc->fail_pop to allow for n items to be popped on failure.
5669static int
5670ensure_fail_pop(struct compiler *c, pattern_context *pc, Py_ssize_t n)
5671{
5672 Py_ssize_t size = n + 1;
5673 if (size <= pc->fail_pop_size) {
5674 return 1;
5675 }
5676 Py_ssize_t needed = sizeof(basicblock*) * size;
5677 basicblock **resized = PyObject_Realloc(pc->fail_pop, needed);
5678 if (resized == NULL) {
5679 PyErr_NoMemory();
5680 return 0;
5681 }
5682 pc->fail_pop = resized;
5683 while (pc->fail_pop_size < size) {
5684 basicblock *new_block;
5685 RETURN_IF_FALSE(new_block = compiler_new_block(c));
5686 pc->fail_pop[pc->fail_pop_size++] = new_block;
5687 }
5688 return 1;
5689}
5690
5691// Use op to jump to the correct fail_pop block.
5692static int
5693jump_to_fail_pop(struct compiler *c, pattern_context *pc, int op)
5694{
5695 // Pop any items on the top of the stack, plus any objects we were going to
5696 // capture on success:
5697 Py_ssize_t pops = pc->on_top + PyList_GET_SIZE(pc->stores);
5698 RETURN_IF_FALSE(ensure_fail_pop(c, pc, pops));
5699 ADDOP_JUMP(c, op, pc->fail_pop[pops]);
5700 NEXT_BLOCK(c);
5701 return 1;
5702}
5703
5704// Build all of the fail_pop blocks and reset fail_pop.
5705static int
5706emit_and_reset_fail_pop(struct compiler *c, pattern_context *pc)
5707{
5708 if (!pc->fail_pop_size) {
5709 assert(pc->fail_pop == NULL);
5710 NEXT_BLOCK(c);
5711 return 1;
5712 }
5713 while (--pc->fail_pop_size) {
5714 compiler_use_next_block(c, pc->fail_pop[pc->fail_pop_size]);
5715 if (!compiler_addop(c, POP_TOP)) {
5716 pc->fail_pop_size = 0;
5717 PyObject_Free(pc->fail_pop);
5718 pc->fail_pop = NULL;
5719 return 0;
5720 }
5721 }
5722 compiler_use_next_block(c, pc->fail_pop[0]);
5723 PyObject_Free(pc->fail_pop);
5724 pc->fail_pop = NULL;
5725 return 1;
5726}
5727
5728static int
5729compiler_error_duplicate_store(struct compiler *c, identifier n)
5730{
5731 return compiler_error(c, "multiple assignments to name %R in pattern", n);
5732}
5733
Brandt Bucher145bf262021-02-26 14:51:55 -08005734static int
5735pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc)
5736{
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005737 if (n == NULL) {
5738 ADDOP(c, POP_TOP);
5739 return 1;
5740 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005741 if (forbidden_name(c, n, Store)) {
5742 return 0;
5743 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005744 // Can't assign to the same name twice:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005745 int duplicate = PySequence_Contains(pc->stores, n);
5746 if (duplicate < 0) {
5747 return 0;
Brandt Bucher145bf262021-02-26 14:51:55 -08005748 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005749 if (duplicate) {
5750 return compiler_error_duplicate_store(c, n);
Brandt Bucher145bf262021-02-26 14:51:55 -08005751 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005752 // Rotate this object underneath any items we need to preserve:
5753 ADDOP_I(c, ROT_N, pc->on_top + PyList_GET_SIZE(pc->stores) + 1);
5754 return !PyList_Append(pc->stores, n);
Brandt Bucher145bf262021-02-26 14:51:55 -08005755}
5756
5757
5758static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005759pattern_unpack_helper(struct compiler *c, asdl_pattern_seq *elts)
5760{
5761 Py_ssize_t n = asdl_seq_LEN(elts);
5762 int seen_star = 0;
5763 for (Py_ssize_t i = 0; i < n; i++) {
5764 pattern_ty elt = asdl_seq_GET(elts, i);
5765 if (elt->kind == MatchStar_kind && !seen_star) {
5766 if ((i >= (1 << 8)) ||
5767 (n-i-1 >= (INT_MAX >> 8)))
5768 return compiler_error(c,
5769 "too many expressions in "
5770 "star-unpacking sequence pattern");
5771 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
5772 seen_star = 1;
5773 }
5774 else if (elt->kind == MatchStar_kind) {
5775 return compiler_error(c,
5776 "multiple starred expressions in sequence pattern");
5777 }
5778 }
5779 if (!seen_star) {
5780 ADDOP_I(c, UNPACK_SEQUENCE, n);
5781 }
5782 return 1;
5783}
5784
5785static int
5786pattern_helper_sequence_unpack(struct compiler *c, asdl_pattern_seq *patterns,
Brandt Bucher145bf262021-02-26 14:51:55 -08005787 Py_ssize_t star, pattern_context *pc)
5788{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005789 RETURN_IF_FALSE(pattern_unpack_helper(c, patterns));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005790 Py_ssize_t size = asdl_seq_LEN(patterns);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005791 // We've now got a bunch of new subjects on the stack. They need to remain
5792 // there after each subpattern match:
5793 pc->on_top += size;
Brandt Bucher145bf262021-02-26 14:51:55 -08005794 for (Py_ssize_t i = 0; i < size; i++) {
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005795 // One less item to keep track of each time we loop through:
5796 pc->on_top--;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005797 pattern_ty pattern = asdl_seq_GET(patterns, i);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005798 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08005799 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005800 return 1;
Brandt Bucher145bf262021-02-26 14:51:55 -08005801}
5802
5803// Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of
5804// UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a
5805// starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc.
5806static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005807pattern_helper_sequence_subscr(struct compiler *c, asdl_pattern_seq *patterns,
Brandt Bucher145bf262021-02-26 14:51:55 -08005808 Py_ssize_t star, pattern_context *pc)
5809{
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005810 // We need to keep the subject around for extracting elements:
5811 pc->on_top++;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005812 Py_ssize_t size = asdl_seq_LEN(patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08005813 for (Py_ssize_t i = 0; i < size; i++) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005814 pattern_ty pattern = asdl_seq_GET(patterns, i);
5815 if (WILDCARD_CHECK(pattern)) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005816 continue;
5817 }
5818 if (i == star) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005819 assert(WILDCARD_STAR_CHECK(pattern));
Brandt Bucher145bf262021-02-26 14:51:55 -08005820 continue;
5821 }
5822 ADDOP(c, DUP_TOP);
5823 if (i < star) {
5824 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5825 }
5826 else {
5827 // The subject may not support negative indexing! Compute a
5828 // nonnegative index:
5829 ADDOP(c, GET_LEN);
5830 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i));
5831 ADDOP(c, BINARY_SUBTRACT);
5832 }
5833 ADDOP(c, BINARY_SUBSCR);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005834 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08005835 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005836 // Pop the subject, we're done with it:
5837 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08005838 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08005839 return 1;
5840}
5841
Brandt Bucher145bf262021-02-26 14:51:55 -08005842// Like compiler_pattern, but turn off checks for irrefutability.
5843static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005844compiler_pattern_subpattern(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005845{
5846 int allow_irrefutable = pc->allow_irrefutable;
5847 pc->allow_irrefutable = 1;
5848 RETURN_IF_FALSE(compiler_pattern(c, p, pc));
5849 pc->allow_irrefutable = allow_irrefutable;
5850 return 1;
5851}
5852
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005853static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005854compiler_pattern_as(struct compiler *c, pattern_ty p, pattern_context *pc)
5855{
5856 assert(p->kind == MatchAs_kind);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005857 if (p->v.MatchAs.pattern == NULL) {
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005858 // An irrefutable match:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005859 if (!pc->allow_irrefutable) {
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005860 if (p->v.MatchAs.name) {
5861 const char *e = "name capture %R makes remaining patterns unreachable";
5862 return compiler_error(c, e, p->v.MatchAs.name);
5863 }
5864 const char *e = "wildcard makes remaining patterns unreachable";
5865 return compiler_error(c, e);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005866 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005867 return pattern_helper_store_name(c, p->v.MatchAs.name, pc);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005868 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005869 // Need to make a copy for (possibly) storing later:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005870 pc->on_top++;
Brandt Bucher145bf262021-02-26 14:51:55 -08005871 ADDOP(c, DUP_TOP);
5872 RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc));
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005873 // Success! Store it:
5874 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08005875 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08005876 return 1;
5877}
5878
Brandt Bucher145bf262021-02-26 14:51:55 -08005879static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005880compiler_pattern_star(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005881{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005882 assert(p->kind == MatchStar_kind);
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005883 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchStar.name, pc));
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005884 return 1;
Brandt Bucher145bf262021-02-26 14:51:55 -08005885}
5886
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005887static int
5888validate_kwd_attrs(struct compiler *c, asdl_identifier_seq *attrs, asdl_pattern_seq* patterns)
5889{
5890 // Any errors will point to the pattern rather than the arg name as the
5891 // parser is only supplying identifiers rather than Name or keyword nodes
5892 Py_ssize_t nattrs = asdl_seq_LEN(attrs);
5893 for (Py_ssize_t i = 0; i < nattrs; i++) {
5894 identifier attr = ((identifier)asdl_seq_GET(attrs, i));
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005895 SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, i)));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005896 if (forbidden_name(c, attr, Store)) {
5897 return -1;
5898 }
5899 for (Py_ssize_t j = i + 1; j < nattrs; j++) {
5900 identifier other = ((identifier)asdl_seq_GET(attrs, j));
5901 if (!PyUnicode_Compare(attr, other)) {
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005902 SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, j)));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005903 compiler_error(c, "attribute name repeated in class pattern: %U", attr);
5904 return -1;
5905 }
5906 }
5907 }
5908 return 0;
5909}
Brandt Bucher145bf262021-02-26 14:51:55 -08005910
5911static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005912compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005913{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005914 assert(p->kind == MatchClass_kind);
5915 asdl_pattern_seq *patterns = p->v.MatchClass.patterns;
5916 asdl_identifier_seq *kwd_attrs = p->v.MatchClass.kwd_attrs;
5917 asdl_pattern_seq *kwd_patterns = p->v.MatchClass.kwd_patterns;
5918 Py_ssize_t nargs = asdl_seq_LEN(patterns);
5919 Py_ssize_t nattrs = asdl_seq_LEN(kwd_attrs);
5920 Py_ssize_t nkwd_patterns = asdl_seq_LEN(kwd_patterns);
5921 if (nattrs != nkwd_patterns) {
5922 // AST validator shouldn't let this happen, but if it does,
5923 // just fail, don't crash out of the interpreter
5924 const char * e = "kwd_attrs (%d) / kwd_patterns (%d) length mismatch in class pattern";
5925 return compiler_error(c, e, nattrs, nkwd_patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08005926 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005927 if (INT_MAX < nargs || INT_MAX < nargs + nattrs - 1) {
5928 const char *e = "too many sub-patterns in class pattern %R";
5929 return compiler_error(c, e, p->v.MatchClass.cls);
5930 }
5931 if (nattrs) {
5932 RETURN_IF_FALSE(!validate_kwd_attrs(c, kwd_attrs, kwd_patterns));
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005933 SET_LOC(c, p);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005934 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005935 VISIT(c, expr, p->v.MatchClass.cls);
5936 PyObject *attr_names;
5937 RETURN_IF_FALSE(attr_names = PyTuple_New(nattrs));
Brandt Bucher145bf262021-02-26 14:51:55 -08005938 Py_ssize_t i;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005939 for (i = 0; i < nattrs; i++) {
5940 PyObject *name = asdl_seq_GET(kwd_attrs, i);
Brandt Bucher145bf262021-02-26 14:51:55 -08005941 Py_INCREF(name);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005942 PyTuple_SET_ITEM(attr_names, i, name);
Brandt Bucher145bf262021-02-26 14:51:55 -08005943 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005944 ADDOP_LOAD_CONST_NEW(c, attr_names);
Brandt Bucher145bf262021-02-26 14:51:55 -08005945 ADDOP_I(c, MATCH_CLASS, nargs);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005946 // TOS is now a tuple of (nargs + nattrs) attributes. Preserve it:
5947 pc->on_top++;
5948 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005949 for (i = 0; i < nargs + nattrs; i++) {
5950 pattern_ty pattern;
Brandt Bucher145bf262021-02-26 14:51:55 -08005951 if (i < nargs) {
5952 // Positional:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005953 pattern = asdl_seq_GET(patterns, i);
Brandt Bucher145bf262021-02-26 14:51:55 -08005954 }
5955 else {
5956 // Keyword:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005957 pattern = asdl_seq_GET(kwd_patterns, i - nargs);
Brandt Bucher145bf262021-02-26 14:51:55 -08005958 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005959 if (WILDCARD_CHECK(pattern)) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005960 continue;
5961 }
5962 // Get the i-th attribute, and match it against the i-th pattern:
5963 ADDOP(c, DUP_TOP);
5964 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5965 ADDOP(c, BINARY_SUBSCR);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005966 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08005967 }
5968 // Success! Pop the tuple of attributes:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005969 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08005970 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08005971 return 1;
5972}
5973
Brandt Bucher145bf262021-02-26 14:51:55 -08005974static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005975compiler_pattern_mapping(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005976{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005977 assert(p->kind == MatchMapping_kind);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005978 asdl_expr_seq *keys = p->v.MatchMapping.keys;
5979 asdl_pattern_seq *patterns = p->v.MatchMapping.patterns;
5980 Py_ssize_t size = asdl_seq_LEN(keys);
5981 Py_ssize_t npatterns = asdl_seq_LEN(patterns);
5982 if (size != npatterns) {
5983 // AST validator shouldn't let this happen, but if it does,
5984 // just fail, don't crash out of the interpreter
5985 const char * e = "keys (%d) / patterns (%d) length mismatch in mapping pattern";
5986 return compiler_error(c, e, size, npatterns);
5987 }
5988 // We have a double-star target if "rest" is set
5989 PyObject *star_target = p->v.MatchMapping.rest;
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005990 // We need to keep the subject on top during the mapping and length checks:
5991 pc->on_top++;
Brandt Bucher145bf262021-02-26 14:51:55 -08005992 ADDOP(c, MATCH_MAPPING);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005993 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005994 if (!size && !star_target) {
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005995 // If the pattern is just "{}", we're done! Pop the subject:
5996 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08005997 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08005998 return 1;
5999 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006000 if (size) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006001 // If the pattern has any keys in it, perform a length check:
6002 ADDOP(c, GET_LEN);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006003 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
Brandt Bucher145bf262021-02-26 14:51:55 -08006004 ADDOP_COMPARE(c, GtE);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006005 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006006 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006007 if (INT_MAX < size - 1) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006008 return compiler_error(c, "too many sub-patterns in mapping pattern");
6009 }
6010 // Collect all of the keys into a tuple for MATCH_KEYS and
6011 // COPY_DICT_WITHOUT_KEYS. They can either be dotted names or literals:
Miss Islington (bot)016af142021-07-14 18:00:35 -07006012
6013 // Maintaining a set of Constant_kind kind keys allows us to raise a
6014 // SyntaxError in the case of duplicates.
6015 PyObject *seen = PySet_New(NULL);
6016 if (seen == NULL) {
6017 return 0;
6018 }
6019
6020 // NOTE: goto error on failure in the loop below to avoid leaking `seen`
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006021 for (Py_ssize_t i = 0; i < size; i++) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006022 expr_ty key = asdl_seq_GET(keys, i);
6023 if (key == NULL) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006024 const char *e = "can't use NULL keys in MatchMapping "
6025 "(set 'rest' parameter instead)";
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07006026 SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, i)));
Miss Islington (bot)016af142021-07-14 18:00:35 -07006027 compiler_error(c, e);
6028 goto error;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006029 }
Miss Islington (bot)016af142021-07-14 18:00:35 -07006030
6031 if (key->kind == Constant_kind) {
6032 int in_seen = PySet_Contains(seen, key->v.Constant.value);
6033 if (in_seen < 0) {
6034 goto error;
6035 }
6036 if (in_seen) {
6037 const char *e = "mapping pattern checks duplicate key (%R)";
6038 compiler_error(c, e, key->v.Constant.value);
6039 goto error;
6040 }
6041 if (PySet_Add(seen, key->v.Constant.value)) {
6042 goto error;
6043 }
6044 }
6045
6046 else if (key->kind != Attribute_kind) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006047 const char *e = "mapping pattern keys may only match literals and attribute lookups";
Miss Islington (bot)016af142021-07-14 18:00:35 -07006048 compiler_error(c, e);
6049 goto error;
Brandt Bucher145bf262021-02-26 14:51:55 -08006050 }
Miss Islington (bot)016af142021-07-14 18:00:35 -07006051 if (!compiler_visit_expr(c, key)) {
6052 goto error;
6053 }
Brandt Bucher145bf262021-02-26 14:51:55 -08006054 }
Miss Islington (bot)016af142021-07-14 18:00:35 -07006055
6056 // all keys have been checked; there are no duplicates
6057 Py_DECREF(seen);
6058
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006059 ADDOP_I(c, BUILD_TUPLE, size);
Brandt Bucher145bf262021-02-26 14:51:55 -08006060 ADDOP(c, MATCH_KEYS);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006061 // There's now a tuple of keys and a tuple of values on top of the subject:
6062 pc->on_top += 2;
6063 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
6064 // So far so good. Use that tuple of values on the stack to match
Brandt Bucher145bf262021-02-26 14:51:55 -08006065 // sub-patterns against:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006066 for (Py_ssize_t i = 0; i < size; i++) {
6067 pattern_ty pattern = asdl_seq_GET(patterns, i);
6068 if (WILDCARD_CHECK(pattern)) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006069 continue;
6070 }
6071 ADDOP(c, DUP_TOP);
6072 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
6073 ADDOP(c, BINARY_SUBSCR);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006074 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006075 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006076 // If we get this far, it's a match! We're done with the tuple of values,
6077 // and whatever happens next should consume the tuple of keys underneath it:
6078 pc->on_top -= 2;
Brandt Bucher145bf262021-02-26 14:51:55 -08006079 ADDOP(c, POP_TOP);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006080 if (star_target) {
6081 // If we have a starred name, bind a dict of remaining items to it:
Brandt Bucher145bf262021-02-26 14:51:55 -08006082 ADDOP(c, COPY_DICT_WITHOUT_KEYS);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006083 RETURN_IF_FALSE(pattern_helper_store_name(c, star_target, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006084 }
6085 else {
6086 // Otherwise, we don't care about this tuple of keys anymore:
6087 ADDOP(c, POP_TOP);
6088 }
6089 // Pop the subject:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006090 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08006091 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08006092 return 1;
Miss Islington (bot)016af142021-07-14 18:00:35 -07006093
6094error:
6095 Py_DECREF(seen);
6096 return 0;
Brandt Bucher145bf262021-02-26 14:51:55 -08006097}
6098
Brandt Bucher145bf262021-02-26 14:51:55 -08006099static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006100compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006101{
6102 assert(p->kind == MatchOr_kind);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006103 basicblock *end;
Brandt Bucher145bf262021-02-26 14:51:55 -08006104 RETURN_IF_FALSE(end = compiler_new_block(c));
Brandt Bucher145bf262021-02-26 14:51:55 -08006105 Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns);
6106 assert(size > 1);
6107 // We're going to be messing with pc. Keep the original info handy:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006108 pattern_context old_pc = *pc;
6109 Py_INCREF(pc->stores);
6110 // control is the list of names bound by the first alternative. It is used
6111 // for checking different name bindings in alternatives, and for correcting
6112 // the order in which extracted elements are placed on the stack.
6113 PyObject *control = NULL;
6114 // NOTE: We can't use returning macros anymore! goto error on error.
Brandt Bucher145bf262021-02-26 14:51:55 -08006115 for (Py_ssize_t i = 0; i < size; i++) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006116 pattern_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i);
Brandt Bucher145bf262021-02-26 14:51:55 -08006117 SET_LOC(c, alt);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006118 PyObject *pc_stores = PyList_New(0);
6119 if (pc_stores == NULL) {
6120 goto error;
Brandt Bucher145bf262021-02-26 14:51:55 -08006121 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006122 Py_SETREF(pc->stores, pc_stores);
6123 // An irrefutable sub-pattern must be last, if it is allowed at all:
6124 pc->allow_irrefutable = (i == size - 1) && old_pc.allow_irrefutable;
6125 pc->fail_pop = NULL;
6126 pc->fail_pop_size = 0;
6127 pc->on_top = 0;
6128 if (!compiler_addop(c, DUP_TOP) || !compiler_pattern(c, alt, pc)) {
6129 goto error;
6130 }
6131 // Success!
6132 Py_ssize_t nstores = PyList_GET_SIZE(pc->stores);
Brandt Bucher145bf262021-02-26 14:51:55 -08006133 if (!i) {
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006134 // This is the first alternative, so save its stores as a "control"
6135 // for the others (they can't bind a different set of names, and
6136 // might need to be reordered):
6137 assert(control == NULL);
Brandt Bucher145bf262021-02-26 14:51:55 -08006138 control = pc->stores;
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006139 Py_INCREF(control);
Brandt Bucher145bf262021-02-26 14:51:55 -08006140 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006141 else if (nstores != PyList_GET_SIZE(control)) {
6142 goto diff;
Brandt Bucher145bf262021-02-26 14:51:55 -08006143 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006144 else if (nstores) {
6145 // There were captures. Check to see if we differ from control:
6146 Py_ssize_t icontrol = nstores;
6147 while (icontrol--) {
6148 PyObject *name = PyList_GET_ITEM(control, icontrol);
6149 Py_ssize_t istores = PySequence_Index(pc->stores, name);
6150 if (istores < 0) {
6151 PyErr_Clear();
6152 goto diff;
6153 }
6154 if (icontrol != istores) {
6155 // Reorder the names on the stack to match the order of the
6156 // names in control. There's probably a better way of doing
6157 // this; the current solution is potentially very
6158 // inefficient when each alternative subpattern binds lots
6159 // of names in different orders. It's fine for reasonable
6160 // cases, though.
6161 assert(istores < icontrol);
6162 Py_ssize_t rotations = istores + 1;
Christian Claussccd82a02021-10-07 17:30:08 +02006163 // Perform the same rotation on pc->stores:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006164 PyObject *rotated = PyList_GetSlice(pc->stores, 0,
6165 rotations);
6166 if (rotated == NULL ||
6167 PyList_SetSlice(pc->stores, 0, rotations, NULL) ||
6168 PyList_SetSlice(pc->stores, icontrol - istores,
6169 icontrol - istores, rotated))
6170 {
6171 Py_XDECREF(rotated);
6172 goto error;
6173 }
6174 Py_DECREF(rotated);
6175 // That just did:
6176 // rotated = pc_stores[:rotations]
6177 // del pc_stores[:rotations]
6178 // pc_stores[icontrol-istores:icontrol-istores] = rotated
6179 // Do the same thing to the stack, using several ROT_Ns:
6180 while (rotations--) {
6181 if (!compiler_addop_i(c, ROT_N, icontrol + 1)) {
6182 goto error;
6183 }
6184 }
6185 }
6186 }
6187 }
6188 assert(control);
6189 if (!compiler_addop_j(c, JUMP_FORWARD, end) ||
6190 !compiler_next_block(c) ||
6191 !emit_and_reset_fail_pop(c, pc))
6192 {
6193 goto error;
6194 }
Brandt Bucher145bf262021-02-26 14:51:55 -08006195 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006196 Py_DECREF(pc->stores);
6197 *pc = old_pc;
6198 Py_INCREF(pc->stores);
6199 // Need to NULL this for the PyObject_Free call in the error block.
6200 old_pc.fail_pop = NULL;
6201 // No match. Pop the remaining copy of the subject and fail:
6202 if (!compiler_addop(c, POP_TOP) || !jump_to_fail_pop(c, pc, JUMP_FORWARD)) {
6203 goto error;
6204 }
Brandt Bucher145bf262021-02-26 14:51:55 -08006205 compiler_use_next_block(c, end);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006206 Py_ssize_t nstores = PyList_GET_SIZE(control);
6207 // There's a bunch of stuff on the stack between any where the new stores
6208 // are and where they need to be:
6209 // - The other stores.
6210 // - A copy of the subject.
6211 // - Anything else that may be on top of the stack.
6212 // - Any previous stores we've already stashed away on the stack.
Pablo Galindo39494282021-05-03 16:20:46 +01006213 Py_ssize_t nrots = nstores + 1 + pc->on_top + PyList_GET_SIZE(pc->stores);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006214 for (Py_ssize_t i = 0; i < nstores; i++) {
6215 // Rotate this capture to its proper place on the stack:
6216 if (!compiler_addop_i(c, ROT_N, nrots)) {
6217 goto error;
6218 }
6219 // Update the list of previous stores with this new name, checking for
6220 // duplicates:
6221 PyObject *name = PyList_GET_ITEM(control, i);
6222 int dupe = PySequence_Contains(pc->stores, name);
6223 if (dupe < 0) {
6224 goto error;
6225 }
6226 if (dupe) {
6227 compiler_error_duplicate_store(c, name);
6228 goto error;
6229 }
6230 if (PyList_Append(pc->stores, name)) {
6231 goto error;
6232 }
6233 }
6234 Py_DECREF(old_pc.stores);
6235 Py_DECREF(control);
6236 // NOTE: Returning macros are safe again.
6237 // Pop the copy of the subject:
6238 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08006239 return 1;
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006240diff:
6241 compiler_error(c, "alternative patterns bind different names");
6242error:
6243 PyObject_Free(old_pc.fail_pop);
6244 Py_DECREF(old_pc.stores);
Brandt Bucher145bf262021-02-26 14:51:55 -08006245 Py_XDECREF(control);
6246 return 0;
6247}
6248
6249
6250static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006251compiler_pattern_sequence(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006252{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006253 assert(p->kind == MatchSequence_kind);
6254 asdl_pattern_seq *patterns = p->v.MatchSequence.patterns;
6255 Py_ssize_t size = asdl_seq_LEN(patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08006256 Py_ssize_t star = -1;
6257 int only_wildcard = 1;
6258 int star_wildcard = 0;
6259 // Find a starred name, if it exists. There may be at most one:
6260 for (Py_ssize_t i = 0; i < size; i++) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006261 pattern_ty pattern = asdl_seq_GET(patterns, i);
6262 if (pattern->kind == MatchStar_kind) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006263 if (star >= 0) {
6264 const char *e = "multiple starred names in sequence pattern";
6265 return compiler_error(c, e);
6266 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006267 star_wildcard = WILDCARD_STAR_CHECK(pattern);
6268 only_wildcard &= star_wildcard;
Brandt Bucher145bf262021-02-26 14:51:55 -08006269 star = i;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006270 continue;
Brandt Bucher145bf262021-02-26 14:51:55 -08006271 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006272 only_wildcard &= WILDCARD_CHECK(pattern);
Brandt Bucher145bf262021-02-26 14:51:55 -08006273 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006274 // We need to keep the subject on top during the sequence and length checks:
6275 pc->on_top++;
Brandt Bucher145bf262021-02-26 14:51:55 -08006276 ADDOP(c, MATCH_SEQUENCE);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006277 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006278 if (star < 0) {
6279 // No star: len(subject) == size
6280 ADDOP(c, GET_LEN);
6281 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
6282 ADDOP_COMPARE(c, Eq);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006283 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006284 }
6285 else if (size > 1) {
6286 // Star: len(subject) >= size - 1
6287 ADDOP(c, GET_LEN);
6288 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1));
6289 ADDOP_COMPARE(c, GtE);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006290 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006291 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006292 // Whatever comes next should consume the subject:
6293 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08006294 if (only_wildcard) {
6295 // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc.
6296 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08006297 }
6298 else if (star_wildcard) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006299 RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, patterns, star, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006300 }
6301 else {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006302 RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, patterns, star, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006303 }
Brandt Bucher145bf262021-02-26 14:51:55 -08006304 return 1;
6305}
6306
Brandt Bucher145bf262021-02-26 14:51:55 -08006307static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006308compiler_pattern_value(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006309{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006310 assert(p->kind == MatchValue_kind);
6311 expr_ty value = p->v.MatchValue.value;
6312 if (!MATCH_VALUE_EXPR(value)) {
6313 const char *e = "patterns may only match literals and attribute lookups";
6314 return compiler_error(c, e);
6315 }
6316 VISIT(c, expr, value);
Brandt Bucher145bf262021-02-26 14:51:55 -08006317 ADDOP_COMPARE(c, Eq);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006318 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006319 return 1;
6320}
6321
Brandt Bucher145bf262021-02-26 14:51:55 -08006322static int
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07006323compiler_pattern_singleton(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006324{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006325 assert(p->kind == MatchSingleton_kind);
6326 ADDOP_LOAD_CONST(c, p->v.MatchSingleton.value);
6327 ADDOP_COMPARE(c, Is);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006328 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006329 return 1;
6330}
6331
Brandt Bucher145bf262021-02-26 14:51:55 -08006332static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006333compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006334{
6335 SET_LOC(c, p);
6336 switch (p->kind) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006337 case MatchValue_kind:
Brandt Bucher145bf262021-02-26 14:51:55 -08006338 return compiler_pattern_value(c, p, pc);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006339 case MatchSingleton_kind:
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07006340 return compiler_pattern_singleton(c, p, pc);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006341 case MatchSequence_kind:
Brandt Bucher145bf262021-02-26 14:51:55 -08006342 return compiler_pattern_sequence(c, p, pc);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006343 case MatchMapping_kind:
6344 return compiler_pattern_mapping(c, p, pc);
6345 case MatchClass_kind:
6346 return compiler_pattern_class(c, p, pc);
6347 case MatchStar_kind:
6348 return compiler_pattern_star(c, p, pc);
Brandt Bucher145bf262021-02-26 14:51:55 -08006349 case MatchAs_kind:
6350 return compiler_pattern_as(c, p, pc);
6351 case MatchOr_kind:
6352 return compiler_pattern_or(c, p, pc);
Brandt Bucher145bf262021-02-26 14:51:55 -08006353 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006354 // AST validator shouldn't let this happen, but if it does,
6355 // just fail, don't crash out of the interpreter
6356 const char *e = "invalid match pattern node in AST (kind=%d)";
6357 return compiler_error(c, e, p->kind);
Brandt Bucher145bf262021-02-26 14:51:55 -08006358}
6359
Brandt Bucher145bf262021-02-26 14:51:55 -08006360static int
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006361compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006362{
6363 VISIT(c, expr, s->v.Match.subject);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006364 basicblock *end;
Brandt Bucher145bf262021-02-26 14:51:55 -08006365 RETURN_IF_FALSE(end = compiler_new_block(c));
6366 Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006367 assert(cases > 0);
Brandt Bucher145bf262021-02-26 14:51:55 -08006368 match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6369 int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases;
6370 for (Py_ssize_t i = 0; i < cases - has_default; i++) {
6371 m = asdl_seq_GET(s->v.Match.cases, i);
6372 SET_LOC(c, m->pattern);
Brandt Bucher145bf262021-02-26 14:51:55 -08006373 // Only copy the subject if we're *not* on the last case:
6374 if (i != cases - has_default - 1) {
6375 ADDOP(c, DUP_TOP);
6376 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006377 RETURN_IF_FALSE(pc->stores = PyList_New(0));
6378 // Irrefutable cases must be either guarded, last, or both:
6379 pc->allow_irrefutable = m->guard != NULL || i == cases - 1;
6380 pc->fail_pop = NULL;
6381 pc->fail_pop_size = 0;
6382 pc->on_top = 0;
6383 // NOTE: Can't use returning macros here (they'll leak pc->stores)!
6384 if (!compiler_pattern(c, m->pattern, pc)) {
6385 Py_DECREF(pc->stores);
6386 return 0;
6387 }
6388 assert(!pc->on_top);
6389 // It's a match! Store all of the captured names (they're on the stack).
6390 Py_ssize_t nstores = PyList_GET_SIZE(pc->stores);
6391 for (Py_ssize_t n = 0; n < nstores; n++) {
6392 PyObject *name = PyList_GET_ITEM(pc->stores, n);
6393 if (!compiler_nameop(c, name, Store)) {
6394 Py_DECREF(pc->stores);
6395 return 0;
6396 }
6397 }
6398 Py_DECREF(pc->stores);
6399 // NOTE: Returning macros are safe again.
Brandt Bucher145bf262021-02-26 14:51:55 -08006400 if (m->guard) {
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006401 RETURN_IF_FALSE(ensure_fail_pop(c, pc, 0));
6402 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, pc->fail_pop[0], 0));
Brandt Bucher145bf262021-02-26 14:51:55 -08006403 }
6404 // Success! Pop the subject off, we're done with it:
6405 if (i != cases - has_default - 1) {
6406 ADDOP(c, POP_TOP);
6407 }
6408 VISIT_SEQ(c, stmt, m->body);
6409 ADDOP_JUMP(c, JUMP_FORWARD, end);
Miss Islington (bot)01601aa2021-07-25 17:04:06 -07006410 // If the pattern fails to match, we want the line number of the
6411 // cleanup to be associated with the failed pattern, not the last line
6412 // of the body
6413 SET_LOC(c, m->pattern);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006414 RETURN_IF_FALSE(emit_and_reset_fail_pop(c, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006415 }
6416 if (has_default) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006417 // A trailing "case _" is common, and lets us save a bit of redundant
6418 // pushing and popping in the loop above:
6419 m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6420 SET_LOC(c, m->pattern);
Miss Islington (bot)01601aa2021-07-25 17:04:06 -07006421 if (cases == 1) {
6422 // No matches. Done with the subject:
6423 ADDOP(c, POP_TOP);
6424 }
6425 else {
6426 // Show line coverage for default case (it doesn't create bytecode)
6427 ADDOP(c, NOP);
6428 }
Brandt Bucher145bf262021-02-26 14:51:55 -08006429 if (m->guard) {
6430 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0));
6431 }
6432 VISIT_SEQ(c, stmt, m->body);
6433 }
6434 compiler_use_next_block(c, end);
6435 return 1;
6436}
6437
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006438static int
6439compiler_match(struct compiler *c, stmt_ty s)
6440{
6441 pattern_context pc;
6442 pc.fail_pop = NULL;
6443 int result = compiler_match_inner(c, s, &pc);
6444 PyObject_Free(pc.fail_pop);
6445 return result;
6446}
6447
Brandt Bucher145bf262021-02-26 14:51:55 -08006448#undef WILDCARD_CHECK
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006449#undef WILDCARD_STAR_CHECK
Brandt Bucher145bf262021-02-26 14:51:55 -08006450
Thomas Wouters89f507f2006-12-13 04:49:30 +00006451/* End of the compiler section, beginning of the assembler section */
6452
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006453/* do depth-first search of basic block graph, starting with block.
T. Wouters99b54d62019-09-12 07:05:33 -07006454 post records the block indices in post-order.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006455
6456 XXX must handle implicit jumps from one block to next
6457*/
6458
Thomas Wouters89f507f2006-12-13 04:49:30 +00006459struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006460 PyObject *a_bytecode; /* string containing bytecode */
6461 int a_offset; /* offset into bytecode */
6462 int a_nblocks; /* number of reachable blocks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006463 PyObject *a_lnotab; /* string containing lnotab */
6464 int a_lnotab_off; /* offset into lnotab */
Mark Shannon877df852020-11-12 09:43:29 +00006465 int a_prevlineno; /* lineno of last emitted line in line table */
6466 int a_lineno; /* lineno of last emitted instruction */
6467 int a_lineno_start; /* bytecode start offset of current lineno */
Mark Shannoncc75ab72020-11-12 19:49:33 +00006468 basicblock *a_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006469};
6470
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006471Py_LOCAL_INLINE(void)
6472stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006473{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02006474 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
Mark Shannonfee55262019-11-21 09:11:43 +00006475 if (b->b_startdepth < depth && b->b_startdepth < 100) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006476 assert(b->b_startdepth < 0);
6477 b->b_startdepth = depth;
6478 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02006479 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006480}
6481
6482/* Find the flow path that needs the largest stack. We assume that
6483 * cycles in the flow graph have no net effect on the stack depth.
6484 */
6485static int
6486stackdepth(struct compiler *c)
6487{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006488 basicblock *b, *entryblock = NULL;
6489 basicblock **stack, **sp;
6490 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006491 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006492 b->b_startdepth = INT_MIN;
6493 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006494 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006495 }
Mark Shannon67969f52021-04-07 10:52:07 +01006496 assert(entryblock!= NULL);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006497 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
6498 if (!stack) {
6499 PyErr_NoMemory();
6500 return -1;
6501 }
6502
6503 sp = stack;
Mark Shannonb37181e2021-04-06 11:48:59 +01006504 if (c->u->u_ste->ste_generator || c->u->u_ste->ste_coroutine) {
6505 stackdepth_push(&sp, entryblock, 1);
6506 } else {
6507 stackdepth_push(&sp, entryblock, 0);
6508 }
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006509 while (sp != stack) {
6510 b = *--sp;
6511 int depth = b->b_startdepth;
6512 assert(depth >= 0);
6513 basicblock *next = b->b_next;
6514 for (int i = 0; i < b->b_iused; i++) {
6515 struct instr *instr = &b->b_instr[i];
6516 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
6517 if (effect == PY_INVALID_STACK_EFFECT) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01006518 PyErr_Format(PyExc_SystemError,
6519 "compiler stack_effect(opcode=%d, arg=%i) failed",
6520 instr->i_opcode, instr->i_oparg);
6521 return -1;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006522 }
6523 int new_depth = depth + effect;
6524 if (new_depth > maxdepth) {
6525 maxdepth = new_depth;
6526 }
6527 assert(depth >= 0); /* invalid code or bug in stackdepth() */
Mark Shannon582aaf12020-08-04 17:30:11 +01006528 if (is_jump(instr)) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006529 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
6530 assert(effect != PY_INVALID_STACK_EFFECT);
6531 int target_depth = depth + effect;
6532 if (target_depth > maxdepth) {
6533 maxdepth = target_depth;
6534 }
6535 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006536 stackdepth_push(&sp, instr->i_target, target_depth);
6537 }
6538 depth = new_depth;
6539 if (instr->i_opcode == JUMP_ABSOLUTE ||
6540 instr->i_opcode == JUMP_FORWARD ||
6541 instr->i_opcode == RETURN_VALUE ||
Mark Shannonfee55262019-11-21 09:11:43 +00006542 instr->i_opcode == RAISE_VARARGS ||
6543 instr->i_opcode == RERAISE)
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006544 {
6545 /* remaining code is dead */
6546 next = NULL;
6547 break;
6548 }
6549 }
6550 if (next != NULL) {
Mark Shannon266b4622020-11-17 19:30:14 +00006551 assert(b->b_nofallthrough == 0);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006552 stackdepth_push(&sp, next, depth);
6553 }
6554 }
6555 PyObject_Free(stack);
6556 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006557}
6558
6559static int
6560assemble_init(struct assembler *a, int nblocks, int firstlineno)
6561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006562 memset(a, 0, sizeof(struct assembler));
Mark Shannon877df852020-11-12 09:43:29 +00006563 a->a_prevlineno = a->a_lineno = firstlineno;
Mark Shannonfd009e62020-11-13 12:53:53 +00006564 a->a_lnotab = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006565 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006566 if (a->a_bytecode == NULL) {
6567 goto error;
6568 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006569 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006570 if (a->a_lnotab == NULL) {
6571 goto error;
6572 }
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07006573 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006574 PyErr_NoMemory();
Mark Shannonfd009e62020-11-13 12:53:53 +00006575 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006576 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006577 return 1;
Mark Shannonfd009e62020-11-13 12:53:53 +00006578error:
6579 Py_XDECREF(a->a_bytecode);
6580 Py_XDECREF(a->a_lnotab);
6581 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006582}
6583
6584static void
6585assemble_free(struct assembler *a)
6586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006587 Py_XDECREF(a->a_bytecode);
6588 Py_XDECREF(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006589}
6590
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006591static int
6592blocksize(basicblock *b)
6593{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006594 int i;
6595 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006597 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006598 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006599 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006600}
6601
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006602static int
Mark Shannon877df852020-11-12 09:43:29 +00006603assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006604{
Mark Shannon877df852020-11-12 09:43:29 +00006605 Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006606 if (a->a_lnotab_off + 2 >= len) {
6607 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
6608 return 0;
6609 }
Pablo Galindo86e322f2021-01-30 13:54:22 +00006610 unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab);
6611 lnotab += a->a_lnotab_off;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006612 a->a_lnotab_off += 2;
Mark Shannon877df852020-11-12 09:43:29 +00006613 *lnotab++ = bdelta;
6614 *lnotab++ = ldelta;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006615 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006616}
6617
Mark Shannon877df852020-11-12 09:43:29 +00006618/* Appends a range to the end of the line number table. See
6619 * Objects/lnotab_notes.txt for the description of the line number table. */
6620
6621static int
6622assemble_line_range(struct assembler *a)
6623{
6624 int ldelta, bdelta;
Serhiy Storchaka1670d592021-10-04 17:07:21 +03006625 bdelta = (a->a_offset - a->a_lineno_start) * sizeof(_Py_CODEUNIT);
Mark Shannon877df852020-11-12 09:43:29 +00006626 if (bdelta == 0) {
6627 return 1;
6628 }
6629 if (a->a_lineno < 0) {
6630 ldelta = -128;
6631 }
6632 else {
6633 ldelta = a->a_lineno - a->a_prevlineno;
6634 a->a_prevlineno = a->a_lineno;
6635 while (ldelta > 127) {
6636 if (!assemble_emit_linetable_pair(a, 0, 127)) {
6637 return 0;
6638 }
6639 ldelta -= 127;
6640 }
6641 while (ldelta < -127) {
6642 if (!assemble_emit_linetable_pair(a, 0, -127)) {
6643 return 0;
6644 }
6645 ldelta += 127;
6646 }
6647 }
6648 assert(-128 <= ldelta && ldelta < 128);
6649 while (bdelta > 254) {
6650 if (!assemble_emit_linetable_pair(a, 254, ldelta)) {
6651 return 0;
6652 }
6653 ldelta = a->a_lineno < 0 ? -128 : 0;
6654 bdelta -= 254;
6655 }
6656 if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) {
6657 return 0;
6658 }
6659 a->a_lineno_start = a->a_offset;
6660 return 1;
6661}
6662
6663static int
6664assemble_lnotab(struct assembler *a, struct instr *i)
6665{
6666 if (i->i_lineno == a->a_lineno) {
6667 return 1;
6668 }
6669 if (!assemble_line_range(a)) {
6670 return 0;
6671 }
6672 a->a_lineno = i->i_lineno;
6673 return 1;
6674}
6675
6676
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006677/* assemble_emit()
6678 Extend the bytecode with a new instruction.
6679 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00006680*/
6681
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006682static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006683assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006684{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006685 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006686 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03006687 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006688
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006689 arg = i->i_oparg;
6690 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006691 if (i->i_lineno && !assemble_lnotab(a, i))
6692 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006693 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006694 if (len > PY_SSIZE_T_MAX / 2)
6695 return 0;
6696 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
6697 return 0;
6698 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03006699 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006700 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006701 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006702 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006703}
6704
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00006705static void
Mark Shannond4e4ef12022-02-16 11:26:02 +00006706normalize_jumps(struct assembler *a)
6707{
6708 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
6709 b->b_visited = 0;
6710 }
6711 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
6712 b->b_visited = 1;
6713 if (b->b_iused == 0) {
6714 continue;
6715 }
6716 struct instr *last = &b->b_instr[b->b_iused-1];
6717 if (last->i_opcode == JUMP_ABSOLUTE) {
6718 if (last->i_target->b_visited == 0) {
6719 last->i_opcode = JUMP_FORWARD;
6720 }
6721 }
6722 if (last->i_opcode == JUMP_FORWARD) {
6723 if (last->i_target->b_visited == 1) {
6724 last->i_opcode = JUMP_ABSOLUTE;
6725 }
6726 }
6727 }
6728}
6729
6730static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006731assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006732{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006733 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006734 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006735 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00006736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006737 /* Compute the size of each block and fixup jump args.
6738 Replace block pointer with position in bytecode. */
6739 do {
6740 totsize = 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006741 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006742 bsize = blocksize(b);
6743 b->b_offset = totsize;
6744 totsize += bsize;
6745 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006746 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006747 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6748 bsize = b->b_offset;
6749 for (i = 0; i < b->b_iused; i++) {
6750 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006751 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006752 /* Relative jumps are computed relative to
6753 the instruction pointer after fetching
6754 the jump instruction.
6755 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006756 bsize += isize;
Mark Shannon582aaf12020-08-04 17:30:11 +01006757 if (is_jump(instr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006758 instr->i_oparg = instr->i_target->b_offset;
Mark Shannon582aaf12020-08-04 17:30:11 +01006759 if (is_relative_jump(instr)) {
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006760 instr->i_oparg -= bsize;
6761 }
6762 if (instrsize(instr->i_oparg) != isize) {
6763 extended_arg_recompile = 1;
6764 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006765 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006766 }
6767 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00006768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006769 /* XXX: This is an awful hack that could hurt performance, but
6770 on the bright side it should work until we come up
6771 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006773 The issue is that in the first loop blocksize() is called
6774 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006775 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006776 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006778 So we loop until we stop seeing new EXTENDED_ARGs.
6779 The only EXTENDED_ARGs that could be popping up are
6780 ones in jump instructions. So this should converge
6781 fairly quickly.
6782 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006783 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00006784}
6785
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006786static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01006787dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006788{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006789 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006790 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006792 tuple = PyTuple_New(size);
6793 if (tuple == NULL)
6794 return NULL;
6795 while (PyDict_Next(dict, &pos, &k, &v)) {
6796 i = PyLong_AS_LONG(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006797 Py_INCREF(k);
6798 assert((i - offset) < size);
6799 assert((i - offset) >= 0);
6800 PyTuple_SET_ITEM(tuple, i - offset, k);
6801 }
6802 return tuple;
6803}
6804
6805static PyObject *
6806consts_dict_keys_inorder(PyObject *dict)
6807{
6808 PyObject *consts, *k, *v;
6809 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
6810
6811 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
6812 if (consts == NULL)
6813 return NULL;
6814 while (PyDict_Next(dict, &pos, &k, &v)) {
6815 i = PyLong_AS_LONG(v);
Christian Claussccd82a02021-10-07 17:30:08 +02006816 /* The keys of the dictionary can be tuples wrapping a constant.
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +03006817 * (see compiler_add_o and _PyCode_ConstantKey). In that case
6818 * the object we want is always second. */
6819 if (PyTuple_CheckExact(k)) {
6820 k = PyTuple_GET_ITEM(k, 1);
6821 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006822 Py_INCREF(k);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006823 assert(i < size);
6824 assert(i >= 0);
6825 PyList_SET_ITEM(consts, i, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006826 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006827 return consts;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006828}
6829
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006830static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006831compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006832{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006833 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006834 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006835 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04006836 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006837 if (ste->ste_nested)
6838 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07006839 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006840 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07006841 if (!ste->ste_generator && ste->ste_coroutine)
6842 flags |= CO_COROUTINE;
6843 if (ste->ste_generator && ste->ste_coroutine)
6844 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006845 if (ste->ste_varargs)
6846 flags |= CO_VARARGS;
6847 if (ste->ste_varkeywords)
6848 flags |= CO_VARKEYWORDS;
6849 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006851 /* (Only) inherit compilerflags in PyCF_MASK */
6852 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006853
Pablo Galindo90235812020-03-15 04:29:22 +00006854 if ((IS_TOP_LEVEL_AWAIT(c)) &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07006855 ste->ste_coroutine &&
6856 !ste->ste_generator) {
6857 flags |= CO_COROUTINE;
6858 }
6859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006860 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00006861}
6862
Inada Naokibdb941b2021-02-10 09:20:42 +09006863// Merge *obj* with constant cache.
INADA Naokic2e16072018-11-26 21:23:22 +09006864// Unlike merge_consts_recursive(), this function doesn't work recursively.
6865static int
Inada Naokibdb941b2021-02-10 09:20:42 +09006866merge_const_one(struct compiler *c, PyObject **obj)
INADA Naokic2e16072018-11-26 21:23:22 +09006867{
Inada Naokibdb941b2021-02-10 09:20:42 +09006868 PyObject *key = _PyCode_ConstantKey(*obj);
INADA Naokic2e16072018-11-26 21:23:22 +09006869 if (key == NULL) {
6870 return 0;
6871 }
6872
6873 // t is borrowed reference
6874 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
6875 Py_DECREF(key);
6876 if (t == NULL) {
6877 return 0;
6878 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006879 if (t == key) { // obj is new constant.
INADA Naokic2e16072018-11-26 21:23:22 +09006880 return 1;
6881 }
6882
Inada Naokibdb941b2021-02-10 09:20:42 +09006883 if (PyTuple_CheckExact(t)) {
6884 // t is still borrowed reference
6885 t = PyTuple_GET_ITEM(t, 1);
6886 }
6887
6888 Py_INCREF(t);
6889 Py_DECREF(*obj);
6890 *obj = t;
INADA Naokic2e16072018-11-26 21:23:22 +09006891 return 1;
6892}
6893
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006894static PyCodeObject *
Mark Shannon6e8128f2020-07-30 10:03:00 +01006895makecode(struct compiler *c, struct assembler *a, PyObject *consts)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006896{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006897 PyCodeObject *co = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006898 PyObject *names = NULL;
6899 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006900 PyObject *name = NULL;
6901 PyObject *freevars = NULL;
6902 PyObject *cellvars = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006903 Py_ssize_t nlocals;
6904 int nlocals_int;
6905 int flags;
Pablo Galindocd74e662019-06-01 18:08:04 +01006906 int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006908 names = dict_keys_inorder(c->u->u_names, 0);
6909 varnames = dict_keys_inorder(c->u->u_varnames, 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006910 if (!names || !varnames) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006911 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006912 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006913 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
6914 if (!cellvars)
6915 goto error;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006916 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006917 if (!freevars)
6918 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006919
Inada Naokibdb941b2021-02-10 09:20:42 +09006920 if (!merge_const_one(c, &names) ||
6921 !merge_const_one(c, &varnames) ||
6922 !merge_const_one(c, &cellvars) ||
6923 !merge_const_one(c, &freevars))
INADA Naokic2e16072018-11-26 21:23:22 +09006924 {
6925 goto error;
6926 }
6927
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006928 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01006929 assert(nlocals < INT_MAX);
6930 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
6931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006932 flags = compute_code_flags(c);
6933 if (flags < 0)
6934 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006935
Mark Shannon6e8128f2020-07-30 10:03:00 +01006936 consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
6937 if (consts == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006938 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006939 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006940 if (!merge_const_one(c, &consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006941 Py_DECREF(consts);
INADA Naokic2e16072018-11-26 21:23:22 +09006942 goto error;
6943 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006944
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01006945 posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int);
Pablo Galindocd74e662019-06-01 18:08:04 +01006946 posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +01006947 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006948 maxdepth = stackdepth(c);
6949 if (maxdepth < 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006950 Py_DECREF(consts);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006951 goto error;
6952 }
Mark Shannon11e0b292021-04-15 14:28:56 +01006953 if (maxdepth > MAX_ALLOWED_STACK_USE) {
6954 PyErr_Format(PyExc_SystemError,
6955 "excessive stack use: stack is %d deep",
6956 maxdepth);
6957 Py_DECREF(consts);
6958 goto error;
6959 }
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006960 co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
Mark Shannon13bc1392020-01-23 09:25:17 +00006961 posonlyargcount, kwonlyargcount, nlocals_int,
Mark Shannon6e8128f2020-07-30 10:03:00 +01006962 maxdepth, flags, a->a_bytecode, consts, names,
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006963 varnames, freevars, cellvars, c->c_filename,
6964 c->u->u_name, c->u->u_firstlineno, a->a_lnotab);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006965 Py_DECREF(consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006966 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006967 Py_XDECREF(names);
6968 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006969 Py_XDECREF(name);
6970 Py_XDECREF(freevars);
6971 Py_XDECREF(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006972 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006973}
6974
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006975
6976/* For debugging purposes only */
6977#if 0
6978static void
Mark Shannon37686f72021-07-16 11:49:10 +01006979dump_instr(struct instr *i)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006980{
Mark Shannon37686f72021-07-16 11:49:10 +01006981 const char *jrel = (is_relative_jump(i)) ? "jrel " : "";
6982 const char *jabs = (is_jump(i) && !is_relative_jump(i))? "jabs " : "";
6983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006984 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006986 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006987 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006988 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006989 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006990 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
6991 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006992}
6993
6994static void
6995dump_basicblock(const basicblock *b)
6996{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006997 const char *b_return = b->b_return ? "return " : "";
Pablo Galindo60eb9f12020-06-28 01:55:47 +01006998 fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n",
6999 b->b_iused, b->b_startdepth, b->b_offset, b_return);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007000 if (b->b_instr) {
7001 int i;
7002 for (i = 0; i < b->b_iused; i++) {
7003 fprintf(stderr, " [%02d] ", i);
7004 dump_instr(b->b_instr + i);
7005 }
7006 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007007}
7008#endif
7009
Mark Shannon5977a792020-12-02 13:31:40 +00007010
7011static int
7012normalize_basic_block(basicblock *bb);
7013
Mark Shannon6e8128f2020-07-30 10:03:00 +01007014static int
Inada Naoki8a232c72021-04-16 14:01:04 +09007015optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007016
Ɓukasz Langad41abe82021-09-08 18:25:09 +02007017static int
7018trim_unused_consts(struct compiler *c, struct assembler *a, PyObject *consts);
7019
Mark Shannon762ef852021-08-09 10:54:48 +01007020/* Duplicates exit BBs, so that line numbers can be propagated to them */
Mark Shannon5977a792020-12-02 13:31:40 +00007021static int
Mark Shannon762ef852021-08-09 10:54:48 +01007022duplicate_exits_without_lineno(struct compiler *c);
Mark Shannon5977a792020-12-02 13:31:40 +00007023
Mark Shannonb37181e2021-04-06 11:48:59 +01007024static int
Mark Shannon37686f72021-07-16 11:49:10 +01007025extend_block(basicblock *bb);
7026
7027static int
Mark Shannonb37181e2021-04-06 11:48:59 +01007028insert_generator_prefix(struct compiler *c, basicblock *entryblock) {
7029
7030 int flags = compute_code_flags(c);
7031 if (flags < 0) {
7032 return -1;
7033 }
7034 int kind;
7035 if (flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
7036 if (flags & CO_COROUTINE) {
7037 kind = 1;
7038 }
7039 else if (flags & CO_ASYNC_GENERATOR) {
7040 kind = 2;
7041 }
7042 else {
7043 kind = 0;
7044 }
7045 }
7046 else {
7047 return 0;
7048 }
7049 if (compiler_next_instr(entryblock) < 0) {
7050 return -1;
7051 }
7052 for (int i = entryblock->b_iused-1; i > 0; i--) {
7053 entryblock->b_instr[i] = entryblock->b_instr[i-1];
7054 }
7055 entryblock->b_instr[0].i_opcode = GEN_START;
7056 entryblock->b_instr[0].i_oparg = kind;
7057 entryblock->b_instr[0].i_lineno = -1;
7058 entryblock->b_instr[0].i_target = NULL;
7059 return 0;
7060}
7061
Mark Shannon0acdf252021-05-13 14:11:41 +01007062/* Make sure that all returns have a line number, even if early passes
7063 * have failed to propagate a correct line number.
7064 * The resulting line number may not be correct according to PEP 626,
7065 * but should be "good enough", and no worse than in older versions. */
7066static void
7067guarantee_lineno_for_exits(struct assembler *a, int firstlineno) {
7068 int lineno = firstlineno;
7069 assert(lineno > 0);
7070 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7071 if (b->b_iused == 0) {
7072 continue;
7073 }
7074 struct instr *last = &b->b_instr[b->b_iused-1];
7075 if (last->i_lineno < 0) {
Mark Shannon762ef852021-08-09 10:54:48 +01007076 if (last->i_opcode == RETURN_VALUE) {
Mark Shannon0acdf252021-05-13 14:11:41 +01007077 for (int i = 0; i < b->b_iused; i++) {
7078 assert(b->b_instr[i].i_lineno < 0);
Mark Shannon762ef852021-08-09 10:54:48 +01007079
Mark Shannon0acdf252021-05-13 14:11:41 +01007080 b->b_instr[i].i_lineno = lineno;
7081 }
7082 }
7083 }
7084 else {
7085 lineno = last->i_lineno;
7086 }
7087 }
7088}
7089
Mark Shannon762ef852021-08-09 10:54:48 +01007090static void
7091propagate_line_numbers(struct assembler *a);
7092
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007093static PyCodeObject *
7094assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00007095{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007096 basicblock *b, *entryblock;
7097 struct assembler a;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007098 int j, nblocks;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007099 PyCodeObject *co = NULL;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007100 PyObject *consts = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00007101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007102 /* Make sure every block that falls off the end returns None.
7103 XXX NEXT_BLOCK() isn't quite right, because if the last
7104 block ends with a jump or return b_next shouldn't set.
7105 */
7106 if (!c->u->u_curblock->b_return) {
Mark Shannon877df852020-11-12 09:43:29 +00007107 c->u->u_lineno = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007108 if (addNone)
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03007109 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007110 ADDOP(c, RETURN_VALUE);
7111 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00007112
Mark Shannon5977a792020-12-02 13:31:40 +00007113 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7114 if (normalize_basic_block(b)) {
Alex Henrie503627f2021-03-02 03:20:25 -07007115 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00007116 }
7117 }
7118
Mark Shannon37686f72021-07-16 11:49:10 +01007119 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7120 if (extend_block(b)) {
7121 return NULL;
7122 }
7123 }
7124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007125 nblocks = 0;
7126 entryblock = NULL;
7127 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
7128 nblocks++;
7129 entryblock = b;
7130 }
Mark Shannon67969f52021-04-07 10:52:07 +01007131 assert(entryblock != NULL);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00007132
Mark Shannonb37181e2021-04-06 11:48:59 +01007133 if (insert_generator_prefix(c, entryblock)) {
7134 goto error;
7135 }
7136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007137 /* Set firstlineno if it wasn't explicitly set. */
7138 if (!c->u->u_firstlineno) {
Mark Shannon67969f52021-04-07 10:52:07 +01007139 if (entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007140 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
Mark Shannon877df852020-11-12 09:43:29 +00007141 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007142 c->u->u_firstlineno = 1;
7143 }
Mark Shannon5977a792020-12-02 13:31:40 +00007144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007145 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
7146 goto error;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007147 a.a_entry = entryblock;
7148 a.a_nblocks = nblocks;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00007149
Mark Shannon6e8128f2020-07-30 10:03:00 +01007150 consts = consts_dict_keys_inorder(c->u->u_consts);
7151 if (consts == NULL) {
7152 goto error;
7153 }
Mark Shannon37686f72021-07-16 11:49:10 +01007154
Inada Naoki8a232c72021-04-16 14:01:04 +09007155 if (optimize_cfg(c, &a, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007156 goto error;
7157 }
Mark Shannon762ef852021-08-09 10:54:48 +01007158 if (duplicate_exits_without_lineno(c)) {
7159 return NULL;
7160 }
Ɓukasz Langad41abe82021-09-08 18:25:09 +02007161 if (trim_unused_consts(c, &a, consts)) {
7162 goto error;
7163 }
Mark Shannon762ef852021-08-09 10:54:48 +01007164 propagate_line_numbers(&a);
Mark Shannon0acdf252021-05-13 14:11:41 +01007165 guarantee_lineno_for_exits(&a, c->u->u_firstlineno);
Mark Shannond4e4ef12022-02-16 11:26:02 +00007166
7167 /* Order of basic blocks must have been determined by now */
7168 normalize_jumps(&a);
7169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007170 /* Can't modify the bytecode after computing jump offsets. */
7171 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00007172
Mark Shannoncc75ab72020-11-12 19:49:33 +00007173 /* Emit code. */
7174 for(b = entryblock; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007175 for (j = 0; j < b->b_iused; j++)
7176 if (!assemble_emit(&a, &b->b_instr[j]))
7177 goto error;
7178 }
Mark Shannon877df852020-11-12 09:43:29 +00007179 if (!assemble_line_range(&a)) {
7180 return 0;
7181 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00007182
Inada Naokibdb941b2021-02-10 09:20:42 +09007183 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007184 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09007185 }
7186 if (!merge_const_one(c, &a.a_lnotab)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007187 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09007188 }
7189 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) {
7190 goto error;
7191 }
7192 if (!merge_const_one(c, &a.a_bytecode)) {
7193 goto error;
7194 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00007195
Mark Shannon6e8128f2020-07-30 10:03:00 +01007196 co = makecode(c, &a, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007197 error:
Mark Shannon6e8128f2020-07-30 10:03:00 +01007198 Py_XDECREF(consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007199 assemble_free(&a);
7200 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00007201}
Georg Brandl8334fd92010-12-04 10:26:46 +00007202
Mark Shannon6e8128f2020-07-30 10:03:00 +01007203/* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
7204 with LOAD_CONST (c1, c2, ... cn).
7205 The consts table must still be in list form so that the
7206 new constant (c1, c2, ... cn) can be appended.
7207 Called with codestr pointing to the first LOAD_CONST.
7208*/
7209static int
Inada Naoki8a232c72021-04-16 14:01:04 +09007210fold_tuple_on_constants(struct compiler *c,
7211 struct instr *inst,
Mark Shannon6e8128f2020-07-30 10:03:00 +01007212 int n, PyObject *consts)
7213{
7214 /* Pre-conditions */
7215 assert(PyList_CheckExact(consts));
7216 assert(inst[n].i_opcode == BUILD_TUPLE);
7217 assert(inst[n].i_oparg == n);
7218
7219 for (int i = 0; i < n; i++) {
7220 if (inst[i].i_opcode != LOAD_CONST) {
7221 return 0;
7222 }
7223 }
7224
7225 /* Buildup new tuple of constants */
7226 PyObject *newconst = PyTuple_New(n);
7227 if (newconst == NULL) {
7228 return -1;
7229 }
7230 for (int i = 0; i < n; i++) {
7231 int arg = inst[i].i_oparg;
7232 PyObject *constant = PyList_GET_ITEM(consts, arg);
7233 Py_INCREF(constant);
7234 PyTuple_SET_ITEM(newconst, i, constant);
7235 }
Inada Naoki8a232c72021-04-16 14:01:04 +09007236 if (merge_const_one(c, &newconst) == 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007237 Py_DECREF(newconst);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007238 return -1;
7239 }
Inada Naoki8a232c72021-04-16 14:01:04 +09007240
7241 Py_ssize_t index;
7242 for (index = 0; index < PyList_GET_SIZE(consts); index++) {
7243 if (PyList_GET_ITEM(consts, index) == newconst) {
7244 break;
7245 }
7246 }
7247 if (index == PyList_GET_SIZE(consts)) {
7248 if ((size_t)index >= (size_t)INT_MAX - 1) {
7249 Py_DECREF(newconst);
7250 PyErr_SetString(PyExc_OverflowError, "too many constants");
7251 return -1;
7252 }
7253 if (PyList_Append(consts, newconst)) {
7254 Py_DECREF(newconst);
7255 return -1;
7256 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007257 }
7258 Py_DECREF(newconst);
7259 for (int i = 0; i < n; i++) {
7260 inst[i].i_opcode = NOP;
7261 }
7262 inst[n].i_opcode = LOAD_CONST;
Victor Stinner71f2ff42020-09-23 14:06:55 +02007263 inst[n].i_oparg = (int)index;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007264 return 0;
7265}
7266
Mark Shannon28b75c82020-12-23 11:43:10 +00007267
Brandt Bucher0ad1e032021-05-02 13:02:10 -07007268// Eliminate n * ROT_N(n).
7269static void
7270fold_rotations(struct instr *inst, int n)
7271{
7272 for (int i = 0; i < n; i++) {
7273 int rot;
7274 switch (inst[i].i_opcode) {
7275 case ROT_N:
7276 rot = inst[i].i_oparg;
7277 break;
7278 case ROT_FOUR:
7279 rot = 4;
7280 break;
7281 case ROT_THREE:
7282 rot = 3;
7283 break;
7284 case ROT_TWO:
7285 rot = 2;
7286 break;
7287 default:
7288 return;
7289 }
7290 if (rot != n) {
7291 return;
7292 }
7293 }
7294 for (int i = 0; i < n; i++) {
7295 inst[i].i_opcode = NOP;
7296 }
7297}
7298
Brandt Buchera89bbde2021-11-11 13:52:43 -08007299// Attempt to eliminate jumps to jumps by updating inst to jump to
7300// target->i_target using the provided opcode. Return whether or not the
7301// optimization was successful.
7302static bool
7303jump_thread(struct instr *inst, struct instr *target, int opcode)
7304{
7305 assert(is_jump(inst));
7306 assert(is_jump(target));
7307 // bpo-45773: If inst->i_target == target->i_target, then nothing actually
7308 // changes (and we fall into an infinite loop):
7309 if (inst->i_lineno == target->i_lineno &&
7310 inst->i_target != target->i_target)
7311 {
7312 inst->i_target = target->i_target;
7313 inst->i_opcode = opcode;
7314 return true;
Mark Shannon28b75c82020-12-23 11:43:10 +00007315 }
Brandt Buchera89bbde2021-11-11 13:52:43 -08007316 return false;
Mark Shannon28b75c82020-12-23 11:43:10 +00007317}
7318
Mark Shannoncc75ab72020-11-12 19:49:33 +00007319/* Maximum size of basic block that should be copied in optimizer */
7320#define MAX_COPY_SIZE 4
Mark Shannon6e8128f2020-07-30 10:03:00 +01007321
7322/* Optimization */
7323static int
Inada Naoki8a232c72021-04-16 14:01:04 +09007324optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
Mark Shannon6e8128f2020-07-30 10:03:00 +01007325{
7326 assert(PyList_CheckExact(consts));
7327 struct instr nop;
7328 nop.i_opcode = NOP;
7329 struct instr *target;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007330 for (int i = 0; i < bb->b_iused; i++) {
7331 struct instr *inst = &bb->b_instr[i];
7332 int oparg = inst->i_oparg;
7333 int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01007334 if (is_jump(inst)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007335 /* Skip over empty basic blocks. */
7336 while (inst->i_target->b_iused == 0) {
7337 inst->i_target = inst->i_target->b_next;
7338 }
7339 target = &inst->i_target->b_instr[0];
7340 }
7341 else {
7342 target = &nop;
7343 }
7344 switch (inst->i_opcode) {
Mark Shannon266b4622020-11-17 19:30:14 +00007345 /* Remove LOAD_CONST const; conditional jump */
Mark Shannon6e8128f2020-07-30 10:03:00 +01007346 case LOAD_CONST:
Mark Shannon266b4622020-11-17 19:30:14 +00007347 {
7348 PyObject* cnt;
7349 int is_true;
7350 int jump_if_true;
7351 switch(nextop) {
7352 case POP_JUMP_IF_FALSE:
7353 case POP_JUMP_IF_TRUE:
7354 cnt = PyList_GET_ITEM(consts, oparg);
7355 is_true = PyObject_IsTrue(cnt);
7356 if (is_true == -1) {
7357 goto error;
7358 }
7359 inst->i_opcode = NOP;
7360 jump_if_true = nextop == POP_JUMP_IF_TRUE;
7361 if (is_true == jump_if_true) {
7362 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
7363 bb->b_nofallthrough = 1;
7364 }
7365 else {
7366 bb->b_instr[i+1].i_opcode = NOP;
7367 }
7368 break;
7369 case JUMP_IF_FALSE_OR_POP:
7370 case JUMP_IF_TRUE_OR_POP:
7371 cnt = PyList_GET_ITEM(consts, oparg);
7372 is_true = PyObject_IsTrue(cnt);
7373 if (is_true == -1) {
7374 goto error;
7375 }
7376 jump_if_true = nextop == JUMP_IF_TRUE_OR_POP;
7377 if (is_true == jump_if_true) {
7378 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
7379 bb->b_nofallthrough = 1;
7380 }
7381 else {
7382 inst->i_opcode = NOP;
7383 bb->b_instr[i+1].i_opcode = NOP;
7384 }
7385 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007386 }
7387 break;
Mark Shannon266b4622020-11-17 19:30:14 +00007388 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007389
7390 /* Try to fold tuples of constants.
7391 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
7392 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
7393 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
7394 case BUILD_TUPLE:
7395 if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
7396 switch(oparg) {
7397 case 1:
7398 inst->i_opcode = NOP;
7399 bb->b_instr[i+1].i_opcode = NOP;
7400 break;
7401 case 2:
7402 inst->i_opcode = ROT_TWO;
7403 bb->b_instr[i+1].i_opcode = NOP;
7404 break;
7405 case 3:
7406 inst->i_opcode = ROT_THREE;
7407 bb->b_instr[i+1].i_opcode = ROT_TWO;
7408 }
7409 break;
7410 }
7411 if (i >= oparg) {
Inada Naoki8a232c72021-04-16 14:01:04 +09007412 if (fold_tuple_on_constants(c, inst-oparg, oparg, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007413 goto error;
7414 }
7415 }
7416 break;
7417
7418 /* Simplify conditional jump to conditional jump where the
7419 result of the first test implies the success of a similar
7420 test or the failure of the opposite test.
7421 Arises in code like:
7422 "a and b or c"
7423 "(a and b) and c"
7424 "(a or b) or c"
7425 "(a or b) and c"
7426 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
7427 --> x:JUMP_IF_FALSE_OR_POP z
7428 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
7429 --> x:POP_JUMP_IF_FALSE y+1
7430 where y+1 is the instruction following the second test.
7431 */
7432 case JUMP_IF_FALSE_OR_POP:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007433 switch (target->i_opcode) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007434 case POP_JUMP_IF_FALSE:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007435 i -= jump_thread(inst, target, POP_JUMP_IF_FALSE);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007436 break;
7437 case JUMP_ABSOLUTE:
7438 case JUMP_FORWARD:
7439 case JUMP_IF_FALSE_OR_POP:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007440 i -= jump_thread(inst, target, JUMP_IF_FALSE_OR_POP);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007441 break;
7442 case JUMP_IF_TRUE_OR_POP:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007443 case POP_JUMP_IF_TRUE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007444 if (inst->i_lineno == target->i_lineno) {
Brandt Buchera89bbde2021-11-11 13:52:43 -08007445 // We don't need to bother checking for loops here,
7446 // since a block's b_next cannot point to itself:
7447 assert(inst->i_target != inst->i_target->b_next);
Mark Shannon28b75c82020-12-23 11:43:10 +00007448 inst->i_opcode = POP_JUMP_IF_FALSE;
7449 inst->i_target = inst->i_target->b_next;
7450 --i;
7451 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007452 break;
7453 }
7454 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007455 case JUMP_IF_TRUE_OR_POP:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007456 switch (target->i_opcode) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007457 case POP_JUMP_IF_TRUE:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007458 i -= jump_thread(inst, target, POP_JUMP_IF_TRUE);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007459 break;
7460 case JUMP_ABSOLUTE:
7461 case JUMP_FORWARD:
7462 case JUMP_IF_TRUE_OR_POP:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007463 i -= jump_thread(inst, target, JUMP_IF_TRUE_OR_POP);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007464 break;
7465 case JUMP_IF_FALSE_OR_POP:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007466 case POP_JUMP_IF_FALSE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007467 if (inst->i_lineno == target->i_lineno) {
Brandt Buchera89bbde2021-11-11 13:52:43 -08007468 // We don't need to bother checking for loops here,
7469 // since a block's b_next cannot point to itself:
7470 assert(inst->i_target != inst->i_target->b_next);
Mark Shannon28b75c82020-12-23 11:43:10 +00007471 inst->i_opcode = POP_JUMP_IF_TRUE;
7472 inst->i_target = inst->i_target->b_next;
7473 --i;
7474 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007475 break;
7476 }
7477 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007478 case POP_JUMP_IF_FALSE:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007479 switch (target->i_opcode) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007480 case JUMP_ABSOLUTE:
7481 case JUMP_FORWARD:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007482 i -= jump_thread(inst, target, POP_JUMP_IF_FALSE);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007483 }
7484 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007485 case POP_JUMP_IF_TRUE:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007486 switch (target->i_opcode) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007487 case JUMP_ABSOLUTE:
7488 case JUMP_FORWARD:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007489 i -= jump_thread(inst, target, POP_JUMP_IF_TRUE);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007490 }
7491 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007492 case JUMP_ABSOLUTE:
7493 case JUMP_FORWARD:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007494 switch (target->i_opcode) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007495 case JUMP_ABSOLUTE:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007496 case JUMP_FORWARD:
7497 i -= jump_thread(inst, target, JUMP_ABSOLUTE);
Mark Shannon37686f72021-07-16 11:49:10 +01007498 }
7499 break;
7500 case FOR_ITER:
Mark Shannon37686f72021-07-16 11:49:10 +01007501 if (target->i_opcode == JUMP_FORWARD) {
Brandt Buchera89bbde2021-11-11 13:52:43 -08007502 i -= jump_thread(inst, target, FOR_ITER);
Mark Shannoncc75ab72020-11-12 19:49:33 +00007503 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07007504 break;
7505 case ROT_N:
7506 switch (oparg) {
7507 case 0:
7508 case 1:
7509 inst->i_opcode = NOP;
7510 continue;
7511 case 2:
7512 inst->i_opcode = ROT_TWO;
7513 break;
7514 case 3:
7515 inst->i_opcode = ROT_THREE;
7516 break;
7517 case 4:
7518 inst->i_opcode = ROT_FOUR;
7519 break;
7520 }
7521 if (i >= oparg - 1) {
7522 fold_rotations(inst - oparg + 1, oparg);
7523 }
7524 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007525 }
7526 }
7527 return 0;
7528error:
7529 return -1;
7530}
7531
Mark Shannon37686f72021-07-16 11:49:10 +01007532/* If this block ends with an unconditional jump to an exit block,
7533 * then remove the jump and extend this block with the target.
7534 */
7535static int
7536extend_block(basicblock *bb) {
7537 if (bb->b_iused == 0) {
7538 return 0;
7539 }
7540 struct instr *last = &bb->b_instr[bb->b_iused-1];
7541 if (last->i_opcode != JUMP_ABSOLUTE && last->i_opcode != JUMP_FORWARD) {
7542 return 0;
7543 }
7544 if (last->i_target->b_exit && last->i_target->b_iused <= MAX_COPY_SIZE) {
7545 basicblock *to_copy = last->i_target;
7546 last->i_opcode = NOP;
7547 for (int i = 0; i < to_copy->b_iused; i++) {
7548 int index = compiler_next_instr(bb);
7549 if (index < 0) {
7550 return -1;
7551 }
7552 bb->b_instr[index] = to_copy->b_instr[i];
7553 }
7554 bb->b_exit = 1;
7555 }
7556 return 0;
7557}
Mark Shannon6e8128f2020-07-30 10:03:00 +01007558
7559static void
Mark Shannon1659ad12021-01-13 15:05:04 +00007560clean_basic_block(basicblock *bb, int prev_lineno) {
7561 /* Remove NOPs when legal to do so. */
Mark Shannon6e8128f2020-07-30 10:03:00 +01007562 int dest = 0;
7563 for (int src = 0; src < bb->b_iused; src++) {
Mark Shannon877df852020-11-12 09:43:29 +00007564 int lineno = bb->b_instr[src].i_lineno;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007565 if (bb->b_instr[src].i_opcode == NOP) {
Mark Shannon266b4622020-11-17 19:30:14 +00007566 /* Eliminate no-op if it doesn't have a line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007567 if (lineno < 0) {
7568 continue;
7569 }
Mark Shannon266b4622020-11-17 19:30:14 +00007570 /* or, if the previous instruction had the same line number. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007571 if (prev_lineno == lineno) {
7572 continue;
7573 }
Mark Shannon266b4622020-11-17 19:30:14 +00007574 /* or, if the next instruction has same line number or no line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007575 if (src < bb->b_iused - 1) {
7576 int next_lineno = bb->b_instr[src+1].i_lineno;
7577 if (next_lineno < 0 || next_lineno == lineno) {
7578 bb->b_instr[src+1].i_lineno = lineno;
7579 continue;
Mark Shannon877df852020-11-12 09:43:29 +00007580 }
7581 }
Mark Shannon266b4622020-11-17 19:30:14 +00007582 else {
7583 basicblock* next = bb->b_next;
7584 while (next && next->b_iused == 0) {
7585 next = next->b_next;
7586 }
7587 /* or if last instruction in BB and next BB has same line number */
7588 if (next) {
7589 if (lineno == next->b_instr[0].i_lineno) {
7590 continue;
7591 }
7592 }
7593 }
7594
Mark Shannon6e8128f2020-07-30 10:03:00 +01007595 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007596 if (dest != src) {
7597 bb->b_instr[dest] = bb->b_instr[src];
7598 }
7599 dest++;
7600 prev_lineno = lineno;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007601 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007602 assert(dest <= bb->b_iused);
7603 bb->b_iused = dest;
7604}
7605
Mark Shannon266b4622020-11-17 19:30:14 +00007606static int
7607normalize_basic_block(basicblock *bb) {
7608 /* Mark blocks as exit and/or nofallthrough.
7609 Raise SystemError if CFG is malformed. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007610 for (int i = 0; i < bb->b_iused; i++) {
7611 switch(bb->b_instr[i].i_opcode) {
7612 case RETURN_VALUE:
7613 case RAISE_VARARGS:
7614 case RERAISE:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007615 bb->b_exit = 1;
Mark Shannon5977a792020-12-02 13:31:40 +00007616 bb->b_nofallthrough = 1;
7617 break;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007618 case JUMP_ABSOLUTE:
7619 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007620 bb->b_nofallthrough = 1;
Mark Shannon266b4622020-11-17 19:30:14 +00007621 /* fall through */
7622 case POP_JUMP_IF_FALSE:
7623 case POP_JUMP_IF_TRUE:
7624 case JUMP_IF_FALSE_OR_POP:
7625 case JUMP_IF_TRUE_OR_POP:
Mark Shannon5977a792020-12-02 13:31:40 +00007626 case FOR_ITER:
Mark Shannon266b4622020-11-17 19:30:14 +00007627 if (i != bb->b_iused-1) {
7628 PyErr_SetString(PyExc_SystemError, "malformed control flow graph.");
7629 return -1;
7630 }
Mark Shannon5977a792020-12-02 13:31:40 +00007631 /* Skip over empty basic blocks. */
7632 while (bb->b_instr[i].i_target->b_iused == 0) {
7633 bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next;
7634 }
7635
Mark Shannoncc75ab72020-11-12 19:49:33 +00007636 }
7637 }
Mark Shannon266b4622020-11-17 19:30:14 +00007638 return 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007639}
7640
Mark Shannon6e8128f2020-07-30 10:03:00 +01007641static int
7642mark_reachable(struct assembler *a) {
7643 basicblock **stack, **sp;
7644 sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks);
7645 if (stack == NULL) {
7646 return -1;
7647 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007648 a->a_entry->b_predecessors = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007649 *sp++ = a->a_entry;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007650 while (sp > stack) {
7651 basicblock *b = *(--sp);
Mark Shannon3bd60352021-01-13 12:05:43 +00007652 if (b->b_next && !b->b_nofallthrough) {
7653 if (b->b_next->b_predecessors == 0) {
7654 *sp++ = b->b_next;
7655 }
7656 b->b_next->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007657 }
7658 for (int i = 0; i < b->b_iused; i++) {
7659 basicblock *target;
Mark Shannon582aaf12020-08-04 17:30:11 +01007660 if (is_jump(&b->b_instr[i])) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007661 target = b->b_instr[i].i_target;
Mark Shannon3bd60352021-01-13 12:05:43 +00007662 if (target->b_predecessors == 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007663 *sp++ = target;
7664 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007665 target->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007666 }
7667 }
7668 }
7669 PyObject_Free(stack);
7670 return 0;
7671}
7672
Mark Shannon3bd60352021-01-13 12:05:43 +00007673static void
7674eliminate_empty_basic_blocks(basicblock *entry) {
7675 /* Eliminate empty blocks */
7676 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7677 basicblock *next = b->b_next;
7678 if (next) {
7679 while (next->b_iused == 0 && next->b_next) {
7680 next = next->b_next;
7681 }
7682 b->b_next = next;
7683 }
7684 }
7685 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7686 if (b->b_iused == 0) {
7687 continue;
7688 }
7689 if (is_jump(&b->b_instr[b->b_iused-1])) {
7690 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7691 while (target->b_iused == 0) {
7692 target = target->b_next;
7693 }
7694 b->b_instr[b->b_iused-1].i_target = target;
7695 }
7696 }
7697}
7698
7699
Mark Shannon5977a792020-12-02 13:31:40 +00007700/* If an instruction has no line number, but it's predecessor in the BB does,
Mark Shannon3bd60352021-01-13 12:05:43 +00007701 * then copy the line number. If a successor block has no line number, and only
7702 * one predecessor, then inherit the line number.
7703 * This ensures that all exit blocks (with one predecessor) receive a line number.
7704 * Also reduces the size of the line number table,
Mark Shannon5977a792020-12-02 13:31:40 +00007705 * but has no impact on the generated line number events.
7706 */
7707static void
Mark Shannon762ef852021-08-09 10:54:48 +01007708propagate_line_numbers(struct assembler *a) {
Mark Shannon5977a792020-12-02 13:31:40 +00007709 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007710 if (b->b_iused == 0) {
7711 continue;
7712 }
Mark Shannon5977a792020-12-02 13:31:40 +00007713 int prev_lineno = -1;
7714 for (int i = 0; i < b->b_iused; i++) {
7715 if (b->b_instr[i].i_lineno < 0) {
7716 b->b_instr[i].i_lineno = prev_lineno;
7717 }
7718 else {
7719 prev_lineno = b->b_instr[i].i_lineno;
7720 }
7721 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007722 if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) {
7723 assert(b->b_next->b_iused);
7724 if (b->b_next->b_instr[0].i_lineno < 0) {
7725 b->b_next->b_instr[0].i_lineno = prev_lineno;
7726 }
7727 }
7728 if (is_jump(&b->b_instr[b->b_iused-1])) {
7729 switch (b->b_instr[b->b_iused-1].i_opcode) {
7730 /* Note: Only actual jumps, not exception handlers */
7731 case SETUP_ASYNC_WITH:
7732 case SETUP_WITH:
7733 case SETUP_FINALLY:
7734 continue;
7735 }
7736 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7737 if (target->b_predecessors == 1) {
7738 if (target->b_instr[0].i_lineno < 0) {
7739 target->b_instr[0].i_lineno = prev_lineno;
7740 }
7741 }
7742 }
Mark Shannon5977a792020-12-02 13:31:40 +00007743 }
7744}
7745
7746/* Perform optimizations on a control flow graph.
Mark Shannon6e8128f2020-07-30 10:03:00 +01007747 The consts object should still be in list form to allow new constants
7748 to be appended.
7749
7750 All transformations keep the code size the same or smaller.
7751 For those that reduce size, the gaps are initially filled with
7752 NOPs. Later those NOPs are removed.
7753*/
7754
7755static int
Inada Naoki8a232c72021-04-16 14:01:04 +09007756optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts)
Mark Shannon6e8128f2020-07-30 10:03:00 +01007757{
Mark Shannoncc75ab72020-11-12 19:49:33 +00007758 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Inada Naoki8a232c72021-04-16 14:01:04 +09007759 if (optimize_basic_block(c, b, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007760 return -1;
7761 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007762 clean_basic_block(b, -1);
Mark Shannon3bd60352021-01-13 12:05:43 +00007763 assert(b->b_predecessors == 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007764 }
Mark Shannon762ef852021-08-09 10:54:48 +01007765 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7766 if (extend_block(b)) {
7767 return -1;
7768 }
7769 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007770 if (mark_reachable(a)) {
7771 return -1;
7772 }
7773 /* Delete unreachable instructions */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007774 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007775 if (b->b_predecessors == 0) {
Mark Shannoncc75ab72020-11-12 19:49:33 +00007776 b->b_iused = 0;
Om Gc71581c2020-12-16 17:48:05 +05307777 b->b_nofallthrough = 0;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007778 }
7779 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007780 basicblock *pred = NULL;
7781 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7782 int prev_lineno = -1;
7783 if (pred && pred->b_iused) {
7784 prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno;
7785 }
7786 clean_basic_block(b, prev_lineno);
7787 pred = b->b_nofallthrough ? NULL : b;
7788 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007789 eliminate_empty_basic_blocks(a->a_entry);
Om Gc71581c2020-12-16 17:48:05 +05307790 /* Delete jump instructions made redundant by previous step. If a non-empty
7791 block ends with a jump instruction, check if the next non-empty block
7792 reached through normal flow control is the target of that jump. If it
7793 is, then the jump instruction is redundant and can be deleted.
7794 */
Mark Shannon3bd60352021-01-13 12:05:43 +00007795 int maybe_empty_blocks = 0;
Om Gc71581c2020-12-16 17:48:05 +05307796 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7797 if (b->b_iused > 0) {
7798 struct instr *b_last_instr = &b->b_instr[b->b_iused - 1];
Mark Shannon802b6452021-02-02 14:59:15 +00007799 if (b_last_instr->i_opcode == JUMP_ABSOLUTE ||
Om Gc71581c2020-12-16 17:48:05 +05307800 b_last_instr->i_opcode == JUMP_FORWARD) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007801 if (b_last_instr->i_target == b->b_next) {
7802 assert(b->b_next->b_iused);
Om Gc71581c2020-12-16 17:48:05 +05307803 b->b_nofallthrough = 0;
Mark Shannon802b6452021-02-02 14:59:15 +00007804 b_last_instr->i_opcode = NOP;
7805 clean_basic_block(b, -1);
7806 maybe_empty_blocks = 1;
Om Gc71581c2020-12-16 17:48:05 +05307807 }
7808 }
7809 }
7810 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007811 if (maybe_empty_blocks) {
7812 eliminate_empty_basic_blocks(a->a_entry);
7813 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007814 return 0;
7815}
7816
Ɓukasz Langad41abe82021-09-08 18:25:09 +02007817// Remove trailing unused constants.
7818static int
7819trim_unused_consts(struct compiler *c, struct assembler *a, PyObject *consts)
7820{
7821 assert(PyList_CheckExact(consts));
7822
7823 // The first constant may be docstring; keep it always.
7824 int max_const_index = 0;
7825 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7826 for (int i = 0; i < b->b_iused; i++) {
7827 if (b->b_instr[i].i_opcode == LOAD_CONST &&
7828 b->b_instr[i].i_oparg > max_const_index) {
7829 max_const_index = b->b_instr[i].i_oparg;
7830 }
7831 }
7832 }
7833 if (max_const_index+1 < PyList_GET_SIZE(consts)) {
7834 //fprintf(stderr, "removing trailing consts: max=%d, size=%d\n",
7835 // max_const_index, (int)PyList_GET_SIZE(consts));
7836 if (PyList_SetSlice(consts, max_const_index+1,
7837 PyList_GET_SIZE(consts), NULL) < 0) {
7838 return 1;
7839 }
7840 }
7841 return 0;
7842}
7843
Mark Shannon5977a792020-12-02 13:31:40 +00007844static inline int
7845is_exit_without_lineno(basicblock *b) {
7846 return b->b_exit && b->b_instr[0].i_lineno < 0;
7847}
7848
7849/* PEP 626 mandates that the f_lineno of a frame is correct
7850 * after a frame terminates. It would be prohibitively expensive
7851 * to continuously update the f_lineno field at runtime,
7852 * so we make sure that all exiting instruction (raises and returns)
7853 * have a valid line number, allowing us to compute f_lineno lazily.
7854 * We can do this by duplicating the exit blocks without line number
7855 * so that none have more than one predecessor. We can then safely
7856 * copy the line number from the sole predecessor block.
7857 */
7858static int
Mark Shannon762ef852021-08-09 10:54:48 +01007859duplicate_exits_without_lineno(struct compiler *c)
Mark Shannon5977a792020-12-02 13:31:40 +00007860{
7861 /* Copy all exit blocks without line number that are targets of a jump.
7862 */
7863 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7864 if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) {
7865 switch (b->b_instr[b->b_iused-1].i_opcode) {
7866 /* Note: Only actual jumps, not exception handlers */
7867 case SETUP_ASYNC_WITH:
7868 case SETUP_WITH:
7869 case SETUP_FINALLY:
7870 continue;
7871 }
7872 basicblock *target = b->b_instr[b->b_iused-1].i_target;
Mark Shannon762ef852021-08-09 10:54:48 +01007873 if (is_exit_without_lineno(target) && target->b_predecessors > 1) {
Mark Shannon5977a792020-12-02 13:31:40 +00007874 basicblock *new_target = compiler_copy_block(c, target);
7875 if (new_target == NULL) {
7876 return -1;
7877 }
7878 new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7879 b->b_instr[b->b_iused-1].i_target = new_target;
Mark Shannon762ef852021-08-09 10:54:48 +01007880 target->b_predecessors--;
7881 new_target->b_predecessors = 1;
7882 new_target->b_next = target->b_next;
7883 target->b_next = new_target;
Mark Shannon5977a792020-12-02 13:31:40 +00007884 }
7885 }
Mark Shannoneaccc122020-12-04 15:22:12 +00007886 }
Mark Shannonee9f98d2021-01-05 12:04:10 +00007887 /* Eliminate empty blocks */
7888 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7889 while (b->b_next && b->b_next->b_iused == 0) {
7890 b->b_next = b->b_next->b_next;
7891 }
7892 }
Mark Shannon5977a792020-12-02 13:31:40 +00007893 /* Any remaining reachable exit blocks without line number can only be reached by
7894 * fall through, and thus can only have a single predecessor */
7895 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7896 if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) {
7897 if (is_exit_without_lineno(b->b_next)) {
7898 assert(b->b_next->b_iused > 0);
7899 b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7900 }
7901 }
7902 }
7903 return 0;
7904}
7905
7906
Mark Shannon6e8128f2020-07-30 10:03:00 +01007907/* Retained for API compatibility.
7908 * Optimization is now done in optimize_cfg */
7909
7910PyObject *
7911PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
7912 PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
7913{
7914 Py_INCREF(code);
7915 return code;
7916}