blob: 1416eb119c9e7678bd50605343f304630350fc7c [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:
2443 <func> is a function/closure created from the class body;
2444 it has a single argument (__locals__) where the dict
2445 (or MutableSequence) representing the locals is passed
2446 <name> is the class name
2447 <bases> is the positional arguments and *varargs argument
2448 <keywords> is the keyword arguments and **kwds argument
2449 This borrows from compiler_call.
2450 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002453 if (!compiler_enter_scope(c, s->v.ClassDef.name,
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002454 COMPILER_SCOPE_CLASS, (void *)s, firstlineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 return 0;
2456 /* this block represents what we do in the new scope */
2457 {
2458 /* use the class name for name mangling */
2459 Py_INCREF(s->v.ClassDef.name);
Serhiy Storchaka48842712016-04-06 09:45:48 +03002460 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 /* load (global) __name__ ... */
2462 str = PyUnicode_InternFromString("__name__");
2463 if (!str || !compiler_nameop(c, str, Load)) {
2464 Py_XDECREF(str);
2465 compiler_exit_scope(c);
2466 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002467 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002468 Py_DECREF(str);
2469 /* ... and store it as __module__ */
2470 str = PyUnicode_InternFromString("__module__");
2471 if (!str || !compiler_nameop(c, str, Store)) {
2472 Py_XDECREF(str);
2473 compiler_exit_scope(c);
2474 return 0;
2475 }
2476 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002477 assert(c->u->u_qualname);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002478 ADDOP_LOAD_CONST(c, c->u->u_qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002479 str = PyUnicode_InternFromString("__qualname__");
2480 if (!str || !compiler_nameop(c, str, Store)) {
2481 Py_XDECREF(str);
2482 compiler_exit_scope(c);
2483 return 0;
2484 }
2485 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 /* compile the body proper */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002487 if (!compiler_body(c, s->v.ClassDef.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 compiler_exit_scope(c);
2489 return 0;
2490 }
Mark Shannone56d54e2021-01-15 13:52:00 +00002491 /* The following code is artificial */
2492 c->u->u_lineno = -1;
Nick Coghlan19d24672016-12-05 16:47:55 +10002493 /* Return __classcell__ if it is referenced, otherwise return None */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002494 if (c->u->u_ste->ste_needs_class_closure) {
Nick Coghlan19d24672016-12-05 16:47:55 +10002495 /* Store __classcell__ into class namespace & return it */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002496 str = PyUnicode_InternFromString("__class__");
2497 if (str == NULL) {
2498 compiler_exit_scope(c);
2499 return 0;
2500 }
2501 i = compiler_lookup_arg(c->u->u_cellvars, str);
2502 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01002503 if (i < 0) {
2504 compiler_exit_scope(c);
2505 return 0;
2506 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002507 assert(i == 0);
Nick Coghlan944368e2016-09-11 14:45:49 +10002508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 ADDOP_I(c, LOAD_CLOSURE, i);
Nick Coghlan19d24672016-12-05 16:47:55 +10002510 ADDOP(c, DUP_TOP);
Nick Coghlan944368e2016-09-11 14:45:49 +10002511 str = PyUnicode_InternFromString("__classcell__");
2512 if (!str || !compiler_nameop(c, str, Store)) {
2513 Py_XDECREF(str);
2514 compiler_exit_scope(c);
2515 return 0;
2516 }
2517 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002518 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002519 else {
Nick Coghlan19d24672016-12-05 16:47:55 +10002520 /* No methods referenced __class__, so just return None */
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002521 assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002522 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson312595c2013-05-15 15:26:42 -05002523 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002524 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002525 /* create the code object */
2526 co = assemble(c, 1);
2527 }
2528 /* leave the new scope */
2529 compiler_exit_scope(c);
2530 if (co == NULL)
2531 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533 /* 2. load the 'build_class' function */
2534 ADDOP(c, LOAD_BUILD_CLASS);
2535
2536 /* 3. load a function (or closure) made from the code object */
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002537 if (!compiler_make_closure(c, co, 0, NULL)) {
2538 Py_DECREF(co);
2539 return 0;
2540 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541 Py_DECREF(co);
2542
2543 /* 4. load class name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002544 ADDOP_LOAD_CONST(c, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002545
2546 /* 5. generate the rest of the code for the call */
Pablo Galindoa5634c42020-09-16 19:42:00 +01002547 if (!compiler_call_helper(c, 2, s->v.ClassDef.bases, s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002548 return 0;
2549
2550 /* 6. apply decorators */
2551 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2552 ADDOP_I(c, CALL_FUNCTION, 1);
2553 }
2554
2555 /* 7. store into <name> */
2556 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2557 return 0;
2558 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002559}
2560
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02002561/* Return 0 if the expression is a constant value except named singletons.
2562 Return 1 otherwise. */
2563static int
2564check_is_arg(expr_ty e)
2565{
2566 if (e->kind != Constant_kind) {
2567 return 1;
2568 }
2569 PyObject *value = e->v.Constant.value;
2570 return (value == Py_None
2571 || value == Py_False
2572 || value == Py_True
2573 || value == Py_Ellipsis);
2574}
2575
2576/* Check operands of identity chacks ("is" and "is not").
2577 Emit a warning if any operand is a constant except named singletons.
2578 Return 0 on error.
2579 */
2580static int
2581check_compare(struct compiler *c, expr_ty e)
2582{
2583 Py_ssize_t i, n;
2584 int left = check_is_arg(e->v.Compare.left);
2585 n = asdl_seq_LEN(e->v.Compare.ops);
2586 for (i = 0; i < n; i++) {
2587 cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i);
2588 int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2589 if (op == Is || op == IsNot) {
2590 if (!right || !left) {
2591 const char *msg = (op == Is)
2592 ? "\"is\" with a literal. Did you mean \"==\"?"
2593 : "\"is not\" with a literal. Did you mean \"!=\"?";
2594 return compiler_warn(c, msg);
2595 }
2596 }
2597 left = right;
2598 }
2599 return 1;
2600}
2601
Mark Shannon9af0e472020-01-14 10:12:45 +00002602static int compiler_addcompare(struct compiler *c, cmpop_ty op)
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002603{
Mark Shannon9af0e472020-01-14 10:12:45 +00002604 int cmp;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002605 switch (op) {
2606 case Eq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002607 cmp = Py_EQ;
2608 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002609 case NotEq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002610 cmp = Py_NE;
2611 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002612 case Lt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002613 cmp = Py_LT;
2614 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002615 case LtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002616 cmp = Py_LE;
2617 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002618 case Gt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002619 cmp = Py_GT;
2620 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002621 case GtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002622 cmp = Py_GE;
2623 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002624 case Is:
Mark Shannon9af0e472020-01-14 10:12:45 +00002625 ADDOP_I(c, IS_OP, 0);
2626 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002627 case IsNot:
Mark Shannon9af0e472020-01-14 10:12:45 +00002628 ADDOP_I(c, IS_OP, 1);
2629 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002630 case In:
Mark Shannon9af0e472020-01-14 10:12:45 +00002631 ADDOP_I(c, CONTAINS_OP, 0);
2632 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002633 case NotIn:
Mark Shannon9af0e472020-01-14 10:12:45 +00002634 ADDOP_I(c, CONTAINS_OP, 1);
2635 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002636 default:
Mark Shannon9af0e472020-01-14 10:12:45 +00002637 Py_UNREACHABLE();
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002638 }
Mark Shannon9af0e472020-01-14 10:12:45 +00002639 ADDOP_I(c, COMPARE_OP, cmp);
2640 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002641}
2642
Mark Shannon9af0e472020-01-14 10:12:45 +00002643
2644
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002645static int
2646compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2647{
2648 switch (e->kind) {
2649 case UnaryOp_kind:
2650 if (e->v.UnaryOp.op == Not)
2651 return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2652 /* fallback to general implementation */
2653 break;
2654 case BoolOp_kind: {
Pablo Galindoa5634c42020-09-16 19:42:00 +01002655 asdl_expr_seq *s = e->v.BoolOp.values;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002656 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2657 assert(n >= 0);
2658 int cond2 = e->v.BoolOp.op == Or;
2659 basicblock *next2 = next;
2660 if (!cond2 != !cond) {
2661 next2 = compiler_new_block(c);
2662 if (next2 == NULL)
2663 return 0;
2664 }
2665 for (i = 0; i < n; ++i) {
2666 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2667 return 0;
2668 }
2669 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2670 return 0;
2671 if (next2 != next)
2672 compiler_use_next_block(c, next2);
2673 return 1;
2674 }
2675 case IfExp_kind: {
2676 basicblock *end, *next2;
2677 end = compiler_new_block(c);
2678 if (end == NULL)
2679 return 0;
2680 next2 = compiler_new_block(c);
2681 if (next2 == NULL)
2682 return 0;
2683 if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2684 return 0;
2685 if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2686 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002687 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002688 compiler_use_next_block(c, next2);
2689 if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2690 return 0;
2691 compiler_use_next_block(c, end);
2692 return 1;
2693 }
2694 case Compare_kind: {
2695 Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2696 if (n > 0) {
Serhiy Storchaka45835252019-02-16 08:29:46 +02002697 if (!check_compare(c, e)) {
2698 return 0;
2699 }
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002700 basicblock *cleanup = compiler_new_block(c);
2701 if (cleanup == NULL)
2702 return 0;
2703 VISIT(c, expr, e->v.Compare.left);
2704 for (i = 0; i < n; i++) {
2705 VISIT(c, expr,
2706 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2707 ADDOP(c, DUP_TOP);
2708 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00002709 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01002710 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002711 NEXT_BLOCK(c);
2712 }
2713 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00002714 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Mark Shannon582aaf12020-08-04 17:30:11 +01002715 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Mark Shannon266b4622020-11-17 19:30:14 +00002716 NEXT_BLOCK(c);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002717 basicblock *end = compiler_new_block(c);
2718 if (end == NULL)
2719 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002720 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002721 compiler_use_next_block(c, cleanup);
2722 ADDOP(c, POP_TOP);
2723 if (!cond) {
Mark Shannon127dde52021-01-04 18:06:55 +00002724 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002725 }
2726 compiler_use_next_block(c, end);
2727 return 1;
2728 }
2729 /* fallback to general implementation */
2730 break;
2731 }
2732 default:
2733 /* fallback to general implementation */
2734 break;
2735 }
2736
2737 /* general implementation */
2738 VISIT(c, expr, e);
Mark Shannon582aaf12020-08-04 17:30:11 +01002739 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Mark Shannon266b4622020-11-17 19:30:14 +00002740 NEXT_BLOCK(c);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002741 return 1;
2742}
2743
2744static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002745compiler_ifexp(struct compiler *c, expr_ty e)
2746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 basicblock *end, *next;
2748
2749 assert(e->kind == IfExp_kind);
2750 end = compiler_new_block(c);
2751 if (end == NULL)
2752 return 0;
2753 next = compiler_new_block(c);
2754 if (next == NULL)
2755 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002756 if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2757 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002758 VISIT(c, expr, e->v.IfExp.body);
Mark Shannon127dde52021-01-04 18:06:55 +00002759 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 compiler_use_next_block(c, next);
2761 VISIT(c, expr, e->v.IfExp.orelse);
2762 compiler_use_next_block(c, end);
2763 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002764}
2765
2766static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002767compiler_lambda(struct compiler *c, expr_ty e)
2768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002769 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002770 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002771 static identifier name;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002772 Py_ssize_t funcflags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 arguments_ty args = e->v.Lambda.args;
2774 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002775
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002776 if (!compiler_check_debug_args(c, args))
2777 return 0;
2778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002779 if (!name) {
2780 name = PyUnicode_InternFromString("<lambda>");
2781 if (!name)
2782 return 0;
2783 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002784
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002785 funcflags = compiler_default_arguments(c, args);
2786 if (funcflags == -1) {
2787 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002789
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002790 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002791 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002794 /* Make None the first constant, so the lambda can't have a
2795 docstring. */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002796 if (compiler_add_const(c, Py_None) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002797 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002800 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2802 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2803 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002804 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 }
2806 else {
2807 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002808 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002810 qualname = c->u->u_qualname;
2811 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 compiler_exit_scope(c);
Pablo Galindo7fdab832021-01-29 22:40:59 +00002813 if (co == NULL) {
2814 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002815 return 0;
Pablo Galindo7fdab832021-01-29 22:40:59 +00002816 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002818 if (!compiler_make_closure(c, co, funcflags, qualname)) {
2819 Py_DECREF(qualname);
2820 Py_DECREF(co);
2821 return 0;
2822 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002823 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 Py_DECREF(co);
2825
2826 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827}
2828
2829static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002830compiler_if(struct compiler *c, stmt_ty s)
2831{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 basicblock *end, *next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 assert(s->kind == If_kind);
2834 end = compiler_new_block(c);
Mark Shannon8473cf82020-12-15 11:07:50 +00002835 if (end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 return 0;
Mark Shannon8473cf82020-12-15 11:07:50 +00002837 }
2838 if (asdl_seq_LEN(s->v.If.orelse)) {
2839 next = compiler_new_block(c);
2840 if (next == NULL) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002841 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002842 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002843 }
2844 else {
2845 next = end;
2846 }
2847 if (!compiler_jump_if(c, s->v.If.test, next, 0)) {
2848 return 0;
2849 }
2850 VISIT_SEQ(c, stmt, s->v.If.body);
2851 if (asdl_seq_LEN(s->v.If.orelse)) {
Mark Shannon127dde52021-01-04 18:06:55 +00002852 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Mark Shannon8473cf82020-12-15 11:07:50 +00002853 compiler_use_next_block(c, next);
2854 VISIT_SEQ(c, stmt, s->v.If.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 }
2856 compiler_use_next_block(c, end);
2857 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002858}
2859
2860static int
2861compiler_for(struct compiler *c, stmt_ty s)
2862{
Mark Shannon5977a792020-12-02 13:31:40 +00002863 basicblock *start, *body, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 start = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002866 body = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 cleanup = compiler_new_block(c);
2868 end = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002869 if (start == NULL || body == NULL || end == NULL || cleanup == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002871 }
2872 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002874 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 VISIT(c, expr, s->v.For.iter);
2876 ADDOP(c, GET_ITER);
2877 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01002878 ADDOP_JUMP(c, FOR_ITER, cleanup);
Mark Shannon5977a792020-12-02 13:31:40 +00002879 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 VISIT(c, expr, s->v.For.target);
2881 VISIT_SEQ(c, stmt, s->v.For.body);
Mark Shannonf5e97b72020-12-14 11:28:39 +00002882 /* Mark jump as artificial */
2883 c->u->u_lineno = -1;
Mark Shannon582aaf12020-08-04 17:30:11 +01002884 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 compiler_use_next_block(c, cleanup);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002886
2887 compiler_pop_fblock(c, FOR_LOOP, start);
2888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002889 VISIT_SEQ(c, stmt, s->v.For.orelse);
2890 compiler_use_next_block(c, end);
2891 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002892}
2893
Yury Selivanov75445082015-05-11 22:57:16 -04002894
2895static int
2896compiler_async_for(struct compiler *c, stmt_ty s)
2897{
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002898 basicblock *start, *except, *end;
Pablo Galindo90235812020-03-15 04:29:22 +00002899 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07002900 c->u->u_ste->ste_coroutine = 1;
2901 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
Zsolt Dollensteine2396502018-04-27 08:58:56 -07002902 return compiler_error(c, "'async for' outside async function");
2903 }
2904
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002905 start = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002906 except = compiler_new_block(c);
2907 end = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002908
Mark Shannonfee55262019-11-21 09:11:43 +00002909 if (start == NULL || except == NULL || end == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002910 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002911 }
Yury Selivanov75445082015-05-11 22:57:16 -04002912 VISIT(c, expr, s->v.AsyncFor.iter);
2913 ADDOP(c, GET_AITER);
Yury Selivanov75445082015-05-11 22:57:16 -04002914
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002915 compiler_use_next_block(c, start);
Mark Shannonfee55262019-11-21 09:11:43 +00002916 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002917 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002918 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002919 /* SETUP_FINALLY to guard the __anext__ call */
Mark Shannon582aaf12020-08-04 17:30:11 +01002920 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov75445082015-05-11 22:57:16 -04002921 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002922 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04002923 ADDOP(c, YIELD_FROM);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002924 ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */
Yury Selivanov75445082015-05-11 22:57:16 -04002925
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002926 /* Success block for __anext__ */
2927 VISIT(c, expr, s->v.AsyncFor.target);
2928 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002929 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002930
2931 compiler_pop_fblock(c, FOR_LOOP, start);
Yury Selivanov75445082015-05-11 22:57:16 -04002932
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002933 /* Except block for __anext__ */
Yury Selivanov75445082015-05-11 22:57:16 -04002934 compiler_use_next_block(c, except);
Mark Shannon877df852020-11-12 09:43:29 +00002935
Mark Shannon47695e32021-07-15 15:54:38 +01002936 /* Use same line number as the iterator,
2937 * as the END_ASYNC_FOR succeeds the `for`, not the body. */
2938 SET_LOC(c, s->v.AsyncFor.iter);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002939 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov75445082015-05-11 22:57:16 -04002940
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002941 /* `else` block */
Yury Selivanov75445082015-05-11 22:57:16 -04002942 VISIT_SEQ(c, stmt, s->v.For.orelse);
2943
2944 compiler_use_next_block(c, end);
2945
2946 return 1;
2947}
2948
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002949static int
2950compiler_while(struct compiler *c, stmt_ty s)
2951{
Mark Shannon266b4622020-11-17 19:30:14 +00002952 basicblock *loop, *body, *end, *anchor = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002953 loop = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002954 body = compiler_new_block(c);
2955 anchor = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002956 end = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002957 if (loop == NULL || body == NULL || anchor == NULL || end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002958 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002960 compiler_use_next_block(c, loop);
Mark Shannon266b4622020-11-17 19:30:14 +00002961 if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002962 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002963 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002964 if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) {
2965 return 0;
Mark Shannon266b4622020-11-17 19:30:14 +00002966 }
2967
2968 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002969 VISIT_SEQ(c, stmt, s->v.While.body);
Mark Shannon8473cf82020-12-15 11:07:50 +00002970 SET_LOC(c, s);
2971 if (!compiler_jump_if(c, s->v.While.test, body, 1)) {
2972 return 0;
2973 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002974
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002975 compiler_pop_fblock(c, WHILE_LOOP, loop);
2976
Mark Shannon266b4622020-11-17 19:30:14 +00002977 compiler_use_next_block(c, anchor);
2978 if (s->v.While.orelse) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002979 VISIT_SEQ(c, stmt, s->v.While.orelse);
Mark Shannon266b4622020-11-17 19:30:14 +00002980 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002983 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002984}
2985
2986static int
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002987compiler_return(struct compiler *c, stmt_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002988{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002989 int preserve_tos = ((s->v.Return.value != NULL) &&
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002990 (s->v.Return.value->kind != Constant_kind));
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002991 if (c->u->u_ste->ste_type != FunctionBlock)
2992 return compiler_error(c, "'return' outside function");
2993 if (s->v.Return.value != NULL &&
2994 c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2995 {
2996 return compiler_error(
2997 c, "'return' with value in async generator");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002999 if (preserve_tos) {
3000 VISIT(c, expr, s->v.Return.value);
Mark Shannon5274b682020-12-16 13:07:01 +00003001 } else {
Mark Shannoncea05852021-06-03 19:57:31 +01003002 /* Emit instruction with line number for return value */
Mark Shannon5274b682020-12-16 13:07:01 +00003003 if (s->v.Return.value != NULL) {
3004 SET_LOC(c, s->v.Return.value);
3005 ADDOP(c, NOP);
3006 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003007 }
Mark Shannoncea05852021-06-03 19:57:31 +01003008 if (s->v.Return.value == NULL || s->v.Return.value->lineno != s->lineno) {
3009 SET_LOC(c, s);
3010 ADDOP(c, NOP);
3011 }
3012
Mark Shannonfee55262019-11-21 09:11:43 +00003013 if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL))
3014 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003015 if (s->v.Return.value == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003016 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003017 }
3018 else if (!preserve_tos) {
Mark Shannon5274b682020-12-16 13:07:01 +00003019 ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003020 }
3021 ADDOP(c, RETURN_VALUE);
Mark Shannon266b4622020-11-17 19:30:14 +00003022 NEXT_BLOCK(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003025}
3026
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003027static int
3028compiler_break(struct compiler *c)
3029{
Mark Shannonfee55262019-11-21 09:11:43 +00003030 struct fblockinfo *loop = NULL;
Mark Shannoncea05852021-06-03 19:57:31 +01003031 /* Emit instruction with line number */
3032 ADDOP(c, NOP);
Mark Shannonfee55262019-11-21 09:11:43 +00003033 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
3034 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003035 }
Mark Shannonfee55262019-11-21 09:11:43 +00003036 if (loop == NULL) {
3037 return compiler_error(c, "'break' outside loop");
3038 }
3039 if (!compiler_unwind_fblock(c, loop, 0)) {
3040 return 0;
3041 }
Mark Shannon582aaf12020-08-04 17:30:11 +01003042 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit);
Mark Shannon266b4622020-11-17 19:30:14 +00003043 NEXT_BLOCK(c);
Mark Shannonfee55262019-11-21 09:11:43 +00003044 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003045}
3046
3047static int
3048compiler_continue(struct compiler *c)
3049{
Mark Shannonfee55262019-11-21 09:11:43 +00003050 struct fblockinfo *loop = NULL;
Mark Shannoncea05852021-06-03 19:57:31 +01003051 /* Emit instruction with line number */
3052 ADDOP(c, NOP);
Mark Shannonfee55262019-11-21 09:11:43 +00003053 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
3054 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003055 }
Mark Shannonfee55262019-11-21 09:11:43 +00003056 if (loop == NULL) {
3057 return compiler_error(c, "'continue' not properly in loop");
3058 }
Mark Shannon582aaf12020-08-04 17:30:11 +01003059 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block);
Mark Shannon266b4622020-11-17 19:30:14 +00003060 NEXT_BLOCK(c)
Mark Shannonfee55262019-11-21 09:11:43 +00003061 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003062}
3063
3064
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003065/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066
3067 SETUP_FINALLY L
3068 <code for body>
3069 POP_BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00003070 <code for finalbody>
3071 JUMP E
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003072 L:
3073 <code for finalbody>
Mark Shannonfee55262019-11-21 09:11:43 +00003074 E:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003075
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003076 The special instructions use the block stack. Each block
3077 stack entry contains the instruction that created it (here
3078 SETUP_FINALLY), the level of the value stack at the time the
3079 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003080
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003081 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082 Pushes the current value stack level and the label
3083 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003084 POP_BLOCK:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003085 Pops en entry from the block stack.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003086
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003087 The block stack is unwound when an exception is raised:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003088 when a SETUP_FINALLY entry is found, the raised and the caught
3089 exceptions are pushed onto the value stack (and the exception
3090 condition is cleared), and the interpreter jumps to the label
3091 gotten from the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003092*/
3093
3094static int
3095compiler_try_finally(struct compiler *c, stmt_ty s)
3096{
Mark Shannonfee55262019-11-21 09:11:43 +00003097 basicblock *body, *end, *exit;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003099 body = compiler_new_block(c);
3100 end = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00003101 exit = compiler_new_block(c);
3102 if (body == NULL || end == NULL || exit == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003104
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003105 /* `try` block */
Mark Shannon582aaf12020-08-04 17:30:11 +01003106 ADDOP_JUMP(c, SETUP_FINALLY, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003107 compiler_use_next_block(c, body);
Mark Shannonfee55262019-11-21 09:11:43 +00003108 if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003109 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003110 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
3111 if (!compiler_try_except(c, s))
3112 return 0;
3113 }
3114 else {
3115 VISIT_SEQ(c, stmt, s->v.Try.body);
3116 }
Mark Shannon3bd60352021-01-13 12:05:43 +00003117 ADDOP_NOLINE(c, POP_BLOCK);
Mark Shannonfee55262019-11-21 09:11:43 +00003118 compiler_pop_fblock(c, FINALLY_TRY, body);
3119 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Mark Shannon127dde52021-01-04 18:06:55 +00003120 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00003121 /* `finally` block */
3122 compiler_use_next_block(c, end);
3123 if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL))
3124 return 0;
3125 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3126 compiler_pop_fblock(c, FINALLY_END, end);
Mark Shannonbf353f32020-12-17 13:55:28 +00003127 ADDOP_I(c, RERAISE, 0);
Mark Shannonfee55262019-11-21 09:11:43 +00003128 compiler_use_next_block(c, exit);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003130}
3131
3132/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003133 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003134 (The contents of the value stack is shown in [], with the top
3135 at the right; 'tb' is trace-back info, 'val' the exception's
3136 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003137
3138 Value stack Label Instruction Argument
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003139 [] SETUP_FINALLY L1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003140 [] <code for S>
3141 [] POP_BLOCK
3142 [] JUMP_FORWARD L0
3143
3144 [tb, val, exc] L1: DUP )
3145 [tb, val, exc, exc] <evaluate E1> )
Mark Shannon9af0e472020-01-14 10:12:45 +00003146 [tb, val, exc, exc, E1] JUMP_IF_NOT_EXC_MATCH L2 ) only if E1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003147 [tb, val, exc] POP
3148 [tb, val] <assign to V1> (or POP if no V1)
3149 [tb] POP
3150 [] <code for S1>
3151 JUMP_FORWARD L0
3152
3153 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003154 .............................etc.......................
3155
Mark Shannonfee55262019-11-21 09:11:43 +00003156 [tb, val, exc] Ln+1: RERAISE # re-raise exception
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003157
3158 [] L0: <next statement>
3159
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003160 Of course, parts are not generated if Vi or Ei is not present.
3161*/
3162static int
3163compiler_try_except(struct compiler *c, stmt_ty s)
3164{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003165 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003166 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003168 body = compiler_new_block(c);
3169 except = compiler_new_block(c);
3170 orelse = compiler_new_block(c);
3171 end = compiler_new_block(c);
3172 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
3173 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01003174 ADDOP_JUMP(c, SETUP_FINALLY, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003175 compiler_use_next_block(c, body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003176 if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003177 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003178 VISIT_SEQ(c, stmt, s->v.Try.body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003179 compiler_pop_fblock(c, TRY_EXCEPT, body);
Mark Shannon3bd60352021-01-13 12:05:43 +00003180 ADDOP_NOLINE(c, POP_BLOCK);
3181 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003182 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003183 compiler_use_next_block(c, except);
Mark Shannon02d126a2020-09-25 14:04:19 +01003184 /* Runtime will push a block here, so we need to account for that */
3185 if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL))
3186 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003187 for (i = 0; i < n; i++) {
3188 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003189 s->v.Try.handlers, i);
Mark Shannon8d4b1842021-05-06 13:38:50 +01003190 SET_LOC(c, handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003191 if (!handler->v.ExceptHandler.type && i < n-1)
3192 return compiler_error(c, "default 'except:' must be last");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003193 except = compiler_new_block(c);
3194 if (except == NULL)
3195 return 0;
3196 if (handler->v.ExceptHandler.type) {
3197 ADDOP(c, DUP_TOP);
3198 VISIT(c, expr, handler->v.ExceptHandler.type);
Mark Shannon582aaf12020-08-04 17:30:11 +01003199 ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except);
Mark Shannon266b4622020-11-17 19:30:14 +00003200 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 }
3202 ADDOP(c, POP_TOP);
3203 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003204 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00003205
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003206 cleanup_end = compiler_new_block(c);
3207 cleanup_body = compiler_new_block(c);
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003208 if (cleanup_end == NULL || cleanup_body == NULL) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003209 return 0;
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003210 }
Guido van Rossumb940e112007-01-10 16:19:56 +00003211
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003212 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3213 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003215 /*
3216 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003217 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003218 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003219 try:
3220 # body
3221 finally:
Chris Angelicoad098b62019-05-21 23:34:19 +10003222 name = None # in case body contains "del name"
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003223 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003224 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003226 /* second try: */
Mark Shannon582aaf12020-08-04 17:30:11 +01003227 ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003228 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003229 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003230 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003231
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003232 /* second # body */
3233 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003234 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannon877df852020-11-12 09:43:29 +00003235 /* name = None; del name; # Mark as artificial */
3236 c->u->u_lineno = -1;
Mark Shannon794ff7d2021-07-14 11:43:56 +01003237 ADDOP(c, POP_BLOCK);
3238 ADDOP(c, POP_EXCEPT);
Mark Shannonfee55262019-11-21 09:11:43 +00003239 ADDOP_LOAD_CONST(c, Py_None);
3240 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3241 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Mark Shannon582aaf12020-08-04 17:30:11 +01003242 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003243
Mark Shannonfee55262019-11-21 09:11:43 +00003244 /* except: */
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003245 compiler_use_next_block(c, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003246
Mark Shannon877df852020-11-12 09:43:29 +00003247 /* name = None; del name; # Mark as artificial */
3248 c->u->u_lineno = -1;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003249 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003250 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003251 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003252
Mark Shannonbf353f32020-12-17 13:55:28 +00003253 ADDOP_I(c, RERAISE, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 }
3255 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003256 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003258 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05003259 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003260 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261
Guido van Rossumb940e112007-01-10 16:19:56 +00003262 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003263 ADDOP(c, POP_TOP);
3264 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003265 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003266 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003267 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003268 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannon127dde52021-01-04 18:06:55 +00003269 c->u->u_lineno = -1;
Mark Shannonfee55262019-11-21 09:11:43 +00003270 ADDOP(c, POP_EXCEPT);
Mark Shannon582aaf12020-08-04 17:30:11 +01003271 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003273 compiler_use_next_block(c, except);
3274 }
Mark Shannon02d126a2020-09-25 14:04:19 +01003275 compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL);
Mark Shannonf2dbfd72020-12-21 13:53:50 +00003276 /* Mark as artificial */
3277 c->u->u_lineno = -1;
Mark Shannonbf353f32020-12-17 13:55:28 +00003278 ADDOP_I(c, RERAISE, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003280 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003281 compiler_use_next_block(c, end);
3282 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003283}
3284
3285static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003286compiler_try(struct compiler *c, stmt_ty s) {
3287 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
3288 return compiler_try_finally(c, s);
3289 else
3290 return compiler_try_except(c, s);
3291}
3292
3293
3294static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003295compiler_import_as(struct compiler *c, identifier name, identifier asname)
3296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003297 /* The IMPORT_NAME opcode was already generated. This function
3298 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003300 If there is a dot in name, we need to split it and emit a
Serhiy Storchakaf93234b2017-05-09 22:31:05 +03003301 IMPORT_FROM for each name.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003303 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
3304 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003305 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003306 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003307 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003308 /* Consume the base module name to get the first attribute */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003309 while (1) {
3310 Py_ssize_t pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 PyObject *attr;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003312 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003313 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003314 return 0;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003315 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 if (!attr)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003317 return 0;
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003318 ADDOP_N(c, IMPORT_FROM, attr, names);
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003319 if (dot == -1) {
3320 break;
3321 }
3322 ADDOP(c, ROT_TWO);
3323 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 }
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003325 if (!compiler_nameop(c, asname, Store)) {
3326 return 0;
3327 }
3328 ADDOP(c, POP_TOP);
3329 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003330 }
3331 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003332}
3333
3334static int
3335compiler_import(struct compiler *c, stmt_ty s)
3336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003337 /* The Import node stores a module name like a.b.c as a single
3338 string. This is convenient for all cases except
3339 import a.b.c as d
3340 where we need to parse that string to extract the individual
3341 module names.
3342 XXX Perhaps change the representation to make this case simpler?
3343 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01003344 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003345
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003346 PyObject *zero = _PyLong_GetZero(); // borrowed reference
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003347 for (i = 0; i < n; i++) {
3348 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
3349 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003350
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003351 ADDOP_LOAD_CONST(c, zero);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003352 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003353 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 if (alias->asname) {
3356 r = compiler_import_as(c, alias->name, alias->asname);
3357 if (!r)
3358 return r;
3359 }
3360 else {
3361 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003362 Py_ssize_t dot = PyUnicode_FindChar(
3363 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02003364 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003365 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02003366 if (tmp == NULL)
3367 return 0;
3368 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003369 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003370 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371 Py_DECREF(tmp);
3372 }
3373 if (!r)
3374 return r;
3375 }
3376 }
3377 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003378}
3379
3380static int
3381compiler_from_import(struct compiler *c, stmt_ty s)
3382{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003383 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003384 PyObject *names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003385 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003387 if (!empty_string) {
3388 empty_string = PyUnicode_FromString("");
3389 if (!empty_string)
3390 return 0;
3391 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003392
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003393 ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02003394
3395 names = PyTuple_New(n);
3396 if (!names)
3397 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399 /* build up the names */
3400 for (i = 0; i < n; i++) {
3401 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3402 Py_INCREF(alias->name);
3403 PyTuple_SET_ITEM(names, i, alias->name);
3404 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003406 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003407 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003408 Py_DECREF(names);
3409 return compiler_error(c, "from __future__ imports must occur "
3410 "at the beginning of the file");
3411 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003412 ADDOP_LOAD_CONST_NEW(c, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003414 if (s->v.ImportFrom.module) {
3415 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
3416 }
3417 else {
3418 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
3419 }
3420 for (i = 0; i < n; i++) {
3421 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3422 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003423
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003424 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 assert(n == 1);
3426 ADDOP(c, IMPORT_STAR);
3427 return 1;
3428 }
3429
3430 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
3431 store_name = alias->name;
3432 if (alias->asname)
3433 store_name = alias->asname;
3434
3435 if (!compiler_nameop(c, store_name, Store)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436 return 0;
3437 }
3438 }
3439 /* remove imported module */
3440 ADDOP(c, POP_TOP);
3441 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003442}
3443
3444static int
3445compiler_assert(struct compiler *c, stmt_ty s)
3446{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003447 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003448
tsukasa-aua8ef4572021-03-16 22:14:41 +11003449 /* Always emit a warning if the test is a non-zero length tuple */
3450 if ((s->v.Assert.test->kind == Tuple_kind &&
3451 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) ||
3452 (s->v.Assert.test->kind == Constant_kind &&
3453 PyTuple_Check(s->v.Assert.test->v.Constant.value) &&
3454 PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0))
Serhiy Storchakad31e7732018-10-21 10:09:39 +03003455 {
3456 if (!compiler_warn(c, "assertion is always true, "
3457 "perhaps remove parentheses?"))
3458 {
Victor Stinner14e461d2013-08-26 22:28:21 +02003459 return 0;
3460 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003461 }
tsukasa-aua8ef4572021-03-16 22:14:41 +11003462 if (c->c_optimize)
3463 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003464 end = compiler_new_block(c);
3465 if (end == NULL)
3466 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003467 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
3468 return 0;
Zackery Spytzce6a0702019-08-25 03:44:09 -06003469 ADDOP(c, LOAD_ASSERTION_ERROR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003470 if (s->v.Assert.msg) {
3471 VISIT(c, expr, s->v.Assert.msg);
3472 ADDOP_I(c, CALL_FUNCTION, 1);
3473 }
3474 ADDOP_I(c, RAISE_VARARGS, 1);
3475 compiler_use_next_block(c, end);
3476 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003477}
3478
3479static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003480compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3481{
3482 if (c->c_interactive && c->c_nestlevel <= 1) {
3483 VISIT(c, expr, value);
3484 ADDOP(c, PRINT_EXPR);
3485 return 1;
3486 }
3487
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003488 if (value->kind == Constant_kind) {
Victor Stinner15a30952016-02-08 22:45:06 +01003489 /* ignore constant statement */
Mark Shannon877df852020-11-12 09:43:29 +00003490 ADDOP(c, NOP);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003491 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003492 }
3493
3494 VISIT(c, expr, value);
Mark Shannonc5440932021-03-15 14:24:25 +00003495 /* Mark POP_TOP as artificial */
3496 c->u->u_lineno = -1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003497 ADDOP(c, POP_TOP);
3498 return 1;
3499}
3500
3501static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003502compiler_visit_stmt(struct compiler *c, stmt_ty s)
3503{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003504 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003506 /* Always assign a lineno to the next instruction for a stmt. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003507 SET_LOC(c, s);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003509 switch (s->kind) {
3510 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04003511 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003512 case ClassDef_kind:
3513 return compiler_class(c, s);
3514 case Return_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003515 return compiler_return(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003516 case Delete_kind:
3517 VISIT_SEQ(c, expr, s->v.Delete.targets)
3518 break;
3519 case Assign_kind:
3520 n = asdl_seq_LEN(s->v.Assign.targets);
3521 VISIT(c, expr, s->v.Assign.value);
3522 for (i = 0; i < n; i++) {
3523 if (i < n - 1)
3524 ADDOP(c, DUP_TOP);
3525 VISIT(c, expr,
3526 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3527 }
3528 break;
3529 case AugAssign_kind:
3530 return compiler_augassign(c, s);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003531 case AnnAssign_kind:
3532 return compiler_annassign(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003533 case For_kind:
3534 return compiler_for(c, s);
3535 case While_kind:
3536 return compiler_while(c, s);
3537 case If_kind:
3538 return compiler_if(c, s);
Brandt Bucher145bf262021-02-26 14:51:55 -08003539 case Match_kind:
3540 return compiler_match(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003541 case Raise_kind:
3542 n = 0;
3543 if (s->v.Raise.exc) {
3544 VISIT(c, expr, s->v.Raise.exc);
3545 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003546 if (s->v.Raise.cause) {
3547 VISIT(c, expr, s->v.Raise.cause);
3548 n++;
3549 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003550 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01003551 ADDOP_I(c, RAISE_VARARGS, (int)n);
Mark Shannon266b4622020-11-17 19:30:14 +00003552 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003553 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003554 case Try_kind:
3555 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003556 case Assert_kind:
3557 return compiler_assert(c, s);
3558 case Import_kind:
3559 return compiler_import(c, s);
3560 case ImportFrom_kind:
3561 return compiler_from_import(c, s);
3562 case Global_kind:
3563 case Nonlocal_kind:
3564 break;
3565 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003566 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003567 case Pass_kind:
Mark Shannon877df852020-11-12 09:43:29 +00003568 ADDOP(c, NOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003569 break;
3570 case Break_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003571 return compiler_break(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003572 case Continue_kind:
3573 return compiler_continue(c);
3574 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003575 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04003576 case AsyncFunctionDef_kind:
3577 return compiler_function(c, s, 1);
3578 case AsyncWith_kind:
3579 return compiler_async_with(c, s, 0);
3580 case AsyncFor_kind:
3581 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003582 }
Yury Selivanov75445082015-05-11 22:57:16 -04003583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003585}
3586
3587static int
3588unaryop(unaryop_ty op)
3589{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003590 switch (op) {
3591 case Invert:
3592 return UNARY_INVERT;
3593 case Not:
3594 return UNARY_NOT;
3595 case UAdd:
3596 return UNARY_POSITIVE;
3597 case USub:
3598 return UNARY_NEGATIVE;
3599 default:
3600 PyErr_Format(PyExc_SystemError,
3601 "unary op %d should not be possible", op);
3602 return 0;
3603 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003604}
3605
3606static int
Andy Lester76d58772020-03-10 21:18:12 -05003607binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003608{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003609 switch (op) {
3610 case Add:
3611 return BINARY_ADD;
3612 case Sub:
3613 return BINARY_SUBTRACT;
3614 case Mult:
3615 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003616 case MatMult:
3617 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003618 case Div:
3619 return BINARY_TRUE_DIVIDE;
3620 case Mod:
3621 return BINARY_MODULO;
3622 case Pow:
3623 return BINARY_POWER;
3624 case LShift:
3625 return BINARY_LSHIFT;
3626 case RShift:
3627 return BINARY_RSHIFT;
3628 case BitOr:
3629 return BINARY_OR;
3630 case BitXor:
3631 return BINARY_XOR;
3632 case BitAnd:
3633 return BINARY_AND;
3634 case FloorDiv:
3635 return BINARY_FLOOR_DIVIDE;
3636 default:
3637 PyErr_Format(PyExc_SystemError,
3638 "binary op %d should not be possible", op);
3639 return 0;
3640 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003641}
3642
3643static int
Andy Lester76d58772020-03-10 21:18:12 -05003644inplace_binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003646 switch (op) {
3647 case Add:
3648 return INPLACE_ADD;
3649 case Sub:
3650 return INPLACE_SUBTRACT;
3651 case Mult:
3652 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003653 case MatMult:
3654 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003655 case Div:
3656 return INPLACE_TRUE_DIVIDE;
3657 case Mod:
3658 return INPLACE_MODULO;
3659 case Pow:
3660 return INPLACE_POWER;
3661 case LShift:
3662 return INPLACE_LSHIFT;
3663 case RShift:
3664 return INPLACE_RSHIFT;
3665 case BitOr:
3666 return INPLACE_OR;
3667 case BitXor:
3668 return INPLACE_XOR;
3669 case BitAnd:
3670 return INPLACE_AND;
3671 case FloorDiv:
3672 return INPLACE_FLOOR_DIVIDE;
3673 default:
3674 PyErr_Format(PyExc_SystemError,
3675 "inplace binary op %d should not be possible", op);
3676 return 0;
3677 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003678}
3679
3680static int
3681compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3682{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003683 int op, scope;
3684 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003685 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003687 PyObject *dict = c->u->u_names;
3688 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003689
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003690 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3691 !_PyUnicode_EqualToASCIIString(name, "True") &&
3692 !_PyUnicode_EqualToASCIIString(name, "False"));
Benjamin Peterson70b224d2012-12-06 17:49:58 -05003693
Pablo Galindoc5fc1562020-04-22 23:29:27 +01003694 if (forbidden_name(c, name, ctx))
3695 return 0;
3696
Serhiy Storchakabd6ec4d2017-12-18 14:29:12 +02003697 mangled = _Py_Mangle(c->u->u_private, name);
3698 if (!mangled)
3699 return 0;
3700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003701 op = 0;
3702 optype = OP_NAME;
Victor Stinner28ad12f2021-03-19 12:41:49 +01003703 scope = _PyST_GetScope(c->u->u_ste, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003704 switch (scope) {
3705 case FREE:
3706 dict = c->u->u_freevars;
3707 optype = OP_DEREF;
3708 break;
3709 case CELL:
3710 dict = c->u->u_cellvars;
3711 optype = OP_DEREF;
3712 break;
3713 case LOCAL:
3714 if (c->u->u_ste->ste_type == FunctionBlock)
3715 optype = OP_FAST;
3716 break;
3717 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04003718 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003719 optype = OP_GLOBAL;
3720 break;
3721 case GLOBAL_EXPLICIT:
3722 optype = OP_GLOBAL;
3723 break;
3724 default:
3725 /* scope can be 0 */
3726 break;
3727 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003729 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003730 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003732 switch (optype) {
3733 case OP_DEREF:
3734 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003735 case Load:
3736 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3737 break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003738 case Store: op = STORE_DEREF; break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003739 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003740 }
3741 break;
3742 case OP_FAST:
3743 switch (ctx) {
3744 case Load: op = LOAD_FAST; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003745 case Store: op = STORE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003746 case Del: op = DELETE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003747 }
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003748 ADDOP_N(c, op, mangled, varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003749 return 1;
3750 case OP_GLOBAL:
3751 switch (ctx) {
3752 case Load: op = LOAD_GLOBAL; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003753 case Store: op = STORE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003754 case Del: op = DELETE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003755 }
3756 break;
3757 case OP_NAME:
3758 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00003759 case Load: op = LOAD_NAME; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003760 case Store: op = STORE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003761 case Del: op = DELETE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003762 }
3763 break;
3764 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003766 assert(op);
Andy Lester76d58772020-03-10 21:18:12 -05003767 arg = compiler_add_o(dict, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003768 Py_DECREF(mangled);
3769 if (arg < 0)
3770 return 0;
3771 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003772}
3773
3774static int
3775compiler_boolop(struct compiler *c, expr_ty e)
3776{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003777 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003778 int jumpi;
3779 Py_ssize_t i, n;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003780 asdl_expr_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003782 assert(e->kind == BoolOp_kind);
3783 if (e->v.BoolOp.op == And)
3784 jumpi = JUMP_IF_FALSE_OR_POP;
3785 else
3786 jumpi = JUMP_IF_TRUE_OR_POP;
3787 end = compiler_new_block(c);
3788 if (end == NULL)
3789 return 0;
3790 s = e->v.BoolOp.values;
3791 n = asdl_seq_LEN(s) - 1;
3792 assert(n >= 0);
3793 for (i = 0; i < n; ++i) {
3794 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003795 ADDOP_JUMP(c, jumpi, end);
Mark Shannon6e8128f2020-07-30 10:03:00 +01003796 basicblock *next = compiler_new_block(c);
3797 if (next == NULL) {
3798 return 0;
3799 }
3800 compiler_use_next_block(c, next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003801 }
3802 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3803 compiler_use_next_block(c, end);
3804 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003805}
3806
3807static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003808starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
Mark Shannon13bc1392020-01-23 09:25:17 +00003809 int build, int add, int extend, int tuple)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003810{
3811 Py_ssize_t n = asdl_seq_LEN(elts);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003812 if (n > 2 && are_all_items_const(elts, 0, n)) {
3813 PyObject *folded = PyTuple_New(n);
3814 if (folded == NULL) {
3815 return 0;
3816 }
3817 PyObject *val;
Mark Shannon11e0b292021-04-15 14:28:56 +01003818 for (Py_ssize_t i = 0; i < n; i++) {
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003819 val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
3820 Py_INCREF(val);
3821 PyTuple_SET_ITEM(folded, i, val);
3822 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003823 if (tuple) {
3824 ADDOP_LOAD_CONST_NEW(c, folded);
3825 } else {
3826 if (add == SET_ADD) {
3827 Py_SETREF(folded, PyFrozenSet_New(folded));
3828 if (folded == NULL) {
3829 return 0;
3830 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003831 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003832 ADDOP_I(c, build, pushed);
3833 ADDOP_LOAD_CONST_NEW(c, folded);
3834 ADDOP_I(c, extend, 1);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003835 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003836 return 1;
3837 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003838
Mark Shannon11e0b292021-04-15 14:28:56 +01003839 int big = n+pushed > STACK_USE_GUIDELINE;
3840 int seen_star = 0;
3841 for (Py_ssize_t i = 0; i < n; i++) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003842 expr_ty elt = asdl_seq_GET(elts, i);
3843 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003844 seen_star = 1;
3845 }
3846 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003847 if (!seen_star && !big) {
3848 for (Py_ssize_t i = 0; i < n; i++) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003849 expr_ty elt = asdl_seq_GET(elts, i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003850 VISIT(c, expr, elt);
Mark Shannon13bc1392020-01-23 09:25:17 +00003851 }
3852 if (tuple) {
3853 ADDOP_I(c, BUILD_TUPLE, n+pushed);
3854 } else {
3855 ADDOP_I(c, build, n+pushed);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003856 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003857 return 1;
3858 }
3859 int sequence_built = 0;
3860 if (big) {
3861 ADDOP_I(c, build, pushed);
3862 sequence_built = 1;
3863 }
3864 for (Py_ssize_t i = 0; i < n; i++) {
3865 expr_ty elt = asdl_seq_GET(elts, i);
3866 if (elt->kind == Starred_kind) {
3867 if (sequence_built == 0) {
3868 ADDOP_I(c, build, i+pushed);
3869 sequence_built = 1;
3870 }
3871 VISIT(c, expr, elt->v.Starred.value);
3872 ADDOP_I(c, extend, 1);
3873 }
3874 else {
3875 VISIT(c, expr, elt);
3876 if (sequence_built) {
3877 ADDOP_I(c, add, 1);
3878 }
3879 }
3880 }
3881 assert(sequence_built);
3882 if (tuple) {
3883 ADDOP(c, LIST_TO_TUPLE);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003884 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003885 return 1;
3886}
3887
3888static int
Brandt Bucher145bf262021-02-26 14:51:55 -08003889unpack_helper(struct compiler *c, asdl_expr_seq *elts)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003890{
3891 Py_ssize_t n = asdl_seq_LEN(elts);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003892 int seen_star = 0;
Brandt Bucher145bf262021-02-26 14:51:55 -08003893 for (Py_ssize_t i = 0; i < n; i++) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003894 expr_ty elt = asdl_seq_GET(elts, i);
3895 if (elt->kind == Starred_kind && !seen_star) {
3896 if ((i >= (1 << 8)) ||
3897 (n-i-1 >= (INT_MAX >> 8)))
3898 return compiler_error(c,
3899 "too many expressions in "
3900 "star-unpacking assignment");
3901 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3902 seen_star = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003903 }
3904 else if (elt->kind == Starred_kind) {
3905 return compiler_error(c,
Furkan Öndercb6534e2020-03-26 04:54:31 +03003906 "multiple starred expressions in assignment");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003907 }
3908 }
3909 if (!seen_star) {
3910 ADDOP_I(c, UNPACK_SEQUENCE, n);
3911 }
Brandt Bucher145bf262021-02-26 14:51:55 -08003912 return 1;
3913}
3914
3915static int
3916assignment_helper(struct compiler *c, asdl_expr_seq *elts)
3917{
3918 Py_ssize_t n = asdl_seq_LEN(elts);
3919 RETURN_IF_FALSE(unpack_helper(c, elts));
3920 for (Py_ssize_t i = 0; i < n; i++) {
Brandt Bucherd5aa2e92020-03-07 19:44:18 -08003921 expr_ty elt = asdl_seq_GET(elts, i);
3922 VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value);
3923 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003924 return 1;
3925}
3926
3927static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003928compiler_list(struct compiler *c, expr_ty e)
3929{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003930 asdl_expr_seq *elts = e->v.List.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003931 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003932 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003933 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003934 else if (e->v.List.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003935 return starunpack_helper(c, elts, 0, BUILD_LIST,
3936 LIST_APPEND, LIST_EXTEND, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003937 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003938 else
3939 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003940 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003941}
3942
3943static int
3944compiler_tuple(struct compiler *c, expr_ty e)
3945{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003946 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003947 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003948 return assignment_helper(c, elts);
3949 }
3950 else if (e->v.Tuple.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003951 return starunpack_helper(c, elts, 0, BUILD_LIST,
3952 LIST_APPEND, LIST_EXTEND, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003953 }
3954 else
3955 VISIT_SEQ(c, expr, elts);
3956 return 1;
3957}
3958
3959static int
3960compiler_set(struct compiler *c, expr_ty e)
3961{
Mark Shannon13bc1392020-01-23 09:25:17 +00003962 return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET,
3963 SET_ADD, SET_UPDATE, 0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003964}
3965
3966static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003967are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003968{
3969 Py_ssize_t i;
3970 for (i = begin; i < end; i++) {
3971 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003972 if (key == NULL || key->kind != Constant_kind)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003973 return 0;
3974 }
3975 return 1;
3976}
3977
3978static int
3979compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3980{
3981 Py_ssize_t i, n = end - begin;
3982 PyObject *keys, *key;
Mark Shannon11e0b292021-04-15 14:28:56 +01003983 int big = n*2 > STACK_USE_GUIDELINE;
3984 if (n > 1 && !big && are_all_items_const(e->v.Dict.keys, begin, end)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003985 for (i = begin; i < end; i++) {
3986 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3987 }
3988 keys = PyTuple_New(n);
3989 if (keys == NULL) {
3990 return 0;
3991 }
3992 for (i = begin; i < end; i++) {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003993 key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003994 Py_INCREF(key);
3995 PyTuple_SET_ITEM(keys, i - begin, key);
3996 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003997 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003998 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
Mark Shannon11e0b292021-04-15 14:28:56 +01003999 return 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004000 }
Mark Shannon11e0b292021-04-15 14:28:56 +01004001 if (big) {
4002 ADDOP_I(c, BUILD_MAP, 0);
4003 }
4004 for (i = begin; i < end; i++) {
4005 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
4006 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
4007 if (big) {
4008 ADDOP_I(c, MAP_ADD, 1);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004009 }
Mark Shannon11e0b292021-04-15 14:28:56 +01004010 }
4011 if (!big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004012 ADDOP_I(c, BUILD_MAP, n);
4013 }
4014 return 1;
4015}
4016
4017static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004018compiler_dict(struct compiler *c, expr_ty e)
4019{
Victor Stinner976bb402016-03-23 11:36:19 +01004020 Py_ssize_t i, n, elements;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004021 int have_dict;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004022 int is_unpacking = 0;
4023 n = asdl_seq_LEN(e->v.Dict.values);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004024 have_dict = 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004025 elements = 0;
4026 for (i = 0; i < n; i++) {
4027 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004028 if (is_unpacking) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004029 if (elements) {
4030 if (!compiler_subdict(c, e, i - elements, i)) {
4031 return 0;
4032 }
4033 if (have_dict) {
4034 ADDOP_I(c, DICT_UPDATE, 1);
4035 }
4036 have_dict = 1;
4037 elements = 0;
4038 }
4039 if (have_dict == 0) {
4040 ADDOP_I(c, BUILD_MAP, 0);
4041 have_dict = 1;
4042 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004043 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Mark Shannon8a4cd702020-01-27 09:57:45 +00004044 ADDOP_I(c, DICT_UPDATE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004045 }
4046 else {
Mark Shannon11e0b292021-04-15 14:28:56 +01004047 if (elements*2 > STACK_USE_GUIDELINE) {
Pablo Galindoc51db0e2020-08-13 09:48:41 +01004048 if (!compiler_subdict(c, e, i - elements, i + 1)) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004049 return 0;
4050 }
4051 if (have_dict) {
4052 ADDOP_I(c, DICT_UPDATE, 1);
4053 }
4054 have_dict = 1;
4055 elements = 0;
4056 }
4057 else {
4058 elements++;
4059 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004060 }
4061 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004062 if (elements) {
4063 if (!compiler_subdict(c, e, n - elements, n)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004064 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004065 }
4066 if (have_dict) {
4067 ADDOP_I(c, DICT_UPDATE, 1);
4068 }
4069 have_dict = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004070 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004071 if (!have_dict) {
4072 ADDOP_I(c, BUILD_MAP, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004073 }
4074 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004075}
4076
4077static int
4078compiler_compare(struct compiler *c, expr_ty e)
4079{
Victor Stinnerad9a0662013-11-19 22:23:20 +01004080 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004081
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02004082 if (!check_compare(c, e)) {
4083 return 0;
4084 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004085 VISIT(c, expr, e->v.Compare.left);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004086 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
4087 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
4088 if (n == 0) {
4089 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Mark Shannon9af0e472020-01-14 10:12:45 +00004090 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004091 }
4092 else {
4093 basicblock *cleanup = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004094 if (cleanup == NULL)
4095 return 0;
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004096 for (i = 0; i < n; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004097 VISIT(c, expr,
4098 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004099 ADDOP(c, DUP_TOP);
4100 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00004101 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01004102 ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004103 NEXT_BLOCK(c);
4104 }
4105 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00004106 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004107 basicblock *end = compiler_new_block(c);
4108 if (end == NULL)
4109 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00004110 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004111 compiler_use_next_block(c, cleanup);
4112 ADDOP(c, ROT_TWO);
4113 ADDOP(c, POP_TOP);
4114 compiler_use_next_block(c, end);
4115 }
4116 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004117}
4118
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004119static PyTypeObject *
4120infer_type(expr_ty e)
4121{
4122 switch (e->kind) {
4123 case Tuple_kind:
4124 return &PyTuple_Type;
4125 case List_kind:
4126 case ListComp_kind:
4127 return &PyList_Type;
4128 case Dict_kind:
4129 case DictComp_kind:
4130 return &PyDict_Type;
4131 case Set_kind:
4132 case SetComp_kind:
4133 return &PySet_Type;
4134 case GeneratorExp_kind:
4135 return &PyGen_Type;
4136 case Lambda_kind:
4137 return &PyFunction_Type;
4138 case JoinedStr_kind:
4139 case FormattedValue_kind:
4140 return &PyUnicode_Type;
4141 case Constant_kind:
Victor Stinnera102ed72020-02-07 02:24:48 +01004142 return Py_TYPE(e->v.Constant.value);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004143 default:
4144 return NULL;
4145 }
4146}
4147
4148static int
4149check_caller(struct compiler *c, expr_ty e)
4150{
4151 switch (e->kind) {
4152 case Constant_kind:
4153 case Tuple_kind:
4154 case List_kind:
4155 case ListComp_kind:
4156 case Dict_kind:
4157 case DictComp_kind:
4158 case Set_kind:
4159 case SetComp_kind:
4160 case GeneratorExp_kind:
4161 case JoinedStr_kind:
4162 case FormattedValue_kind:
4163 return compiler_warn(c, "'%.200s' object is not callable; "
4164 "perhaps you missed a comma?",
4165 infer_type(e)->tp_name);
4166 default:
4167 return 1;
4168 }
4169}
4170
4171static int
4172check_subscripter(struct compiler *c, expr_ty e)
4173{
4174 PyObject *v;
4175
4176 switch (e->kind) {
4177 case Constant_kind:
4178 v = e->v.Constant.value;
4179 if (!(v == Py_None || v == Py_Ellipsis ||
4180 PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
4181 PyAnySet_Check(v)))
4182 {
4183 return 1;
4184 }
4185 /* fall through */
4186 case Set_kind:
4187 case SetComp_kind:
4188 case GeneratorExp_kind:
4189 case Lambda_kind:
4190 return compiler_warn(c, "'%.200s' object is not subscriptable; "
4191 "perhaps you missed a comma?",
4192 infer_type(e)->tp_name);
4193 default:
4194 return 1;
4195 }
4196}
4197
4198static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004199check_index(struct compiler *c, expr_ty e, expr_ty s)
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004200{
4201 PyObject *v;
4202
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004203 PyTypeObject *index_type = infer_type(s);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004204 if (index_type == NULL
4205 || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
4206 || index_type == &PySlice_Type) {
4207 return 1;
4208 }
4209
4210 switch (e->kind) {
4211 case Constant_kind:
4212 v = e->v.Constant.value;
4213 if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
4214 return 1;
4215 }
4216 /* fall through */
4217 case Tuple_kind:
4218 case List_kind:
4219 case ListComp_kind:
4220 case JoinedStr_kind:
4221 case FormattedValue_kind:
4222 return compiler_warn(c, "%.200s indices must be integers or slices, "
4223 "not %.200s; "
4224 "perhaps you missed a comma?",
4225 infer_type(e)->tp_name,
4226 index_type->tp_name);
4227 default:
4228 return 1;
4229 }
4230}
4231
Zackery Spytz97f5de02019-03-22 01:30:32 -06004232// Return 1 if the method call was optimized, -1 if not, and 0 on error.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004233static int
Yury Selivanovf2392132016-12-13 19:03:51 -05004234maybe_optimize_method_call(struct compiler *c, expr_ty e)
4235{
4236 Py_ssize_t argsl, i;
4237 expr_ty meth = e->v.Call.func;
Pablo Galindoa5634c42020-09-16 19:42:00 +01004238 asdl_expr_seq *args = e->v.Call.args;
Yury Selivanovf2392132016-12-13 19:03:51 -05004239
4240 /* Check that the call node is an attribute access, and that
4241 the call doesn't have keyword parameters. */
4242 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
Mark Shannon11e0b292021-04-15 14:28:56 +01004243 asdl_seq_LEN(e->v.Call.keywords)) {
Yury Selivanovf2392132016-12-13 19:03:51 -05004244 return -1;
Mark Shannon11e0b292021-04-15 14:28:56 +01004245 }
4246 /* Check that there aren't too many arguments */
Yury Selivanovf2392132016-12-13 19:03:51 -05004247 argsl = asdl_seq_LEN(args);
Mark Shannon11e0b292021-04-15 14:28:56 +01004248 if (argsl >= STACK_USE_GUIDELINE) {
4249 return -1;
4250 }
4251 /* Check that there are no *varargs types of arguments. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004252 for (i = 0; i < argsl; i++) {
4253 expr_ty elt = asdl_seq_GET(args, i);
4254 if (elt->kind == Starred_kind) {
4255 return -1;
4256 }
4257 }
4258
4259 /* Alright, we can optimize the code. */
4260 VISIT(c, expr, meth->v.Attribute.value);
Mark Shannond48848c2021-03-14 18:01:30 +00004261 int old_lineno = c->u->u_lineno;
4262 c->u->u_lineno = meth->end_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004263 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
4264 VISIT_SEQ(c, expr, e->v.Call.args);
4265 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
Mark Shannond48848c2021-03-14 18:01:30 +00004266 c->u->u_lineno = old_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004267 return 1;
4268}
4269
4270static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004271validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
Zackery Spytz08050e92020-04-06 00:47:47 -06004272{
4273 Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
4274 for (Py_ssize_t i = 0; i < nkeywords; i++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004275 keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
4276 if (key->arg == NULL) {
4277 continue;
4278 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +01004279 if (forbidden_name(c, key->arg, Store)) {
4280 return -1;
4281 }
Zackery Spytz08050e92020-04-06 00:47:47 -06004282 for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004283 keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
4284 if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07004285 SET_LOC(c, other);
Brandt Bucher145bf262021-02-26 14:51:55 -08004286 compiler_error(c, "keyword argument repeated: %U", key->arg);
Pablo Galindo254ec782020-04-03 20:37:13 +01004287 return -1;
4288 }
4289 }
4290 }
4291 return 0;
4292}
4293
4294static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004295compiler_call(struct compiler *c, expr_ty e)
4296{
Zackery Spytz97f5de02019-03-22 01:30:32 -06004297 int ret = maybe_optimize_method_call(c, e);
4298 if (ret >= 0) {
4299 return ret;
4300 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004301 if (!check_caller(c, e->v.Call.func)) {
4302 return 0;
4303 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004304 VISIT(c, expr, e->v.Call.func);
4305 return compiler_call_helper(c, 0,
4306 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004307 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004308}
4309
Eric V. Smith235a6f02015-09-19 14:51:32 -04004310static int
4311compiler_joined_str(struct compiler *c, expr_ty e)
4312{
Mark Shannon11e0b292021-04-15 14:28:56 +01004313
4314 Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values);
4315 if (value_count > STACK_USE_GUIDELINE) {
4316 ADDOP_LOAD_CONST_NEW(c, _PyUnicode_FromASCII("", 0));
4317 PyObject *join = _PyUnicode_FromASCII("join", 4);
4318 if (join == NULL) {
4319 return 0;
4320 }
4321 ADDOP_NAME(c, LOAD_METHOD, join, names);
4322 Py_DECREF(join);
4323 ADDOP_I(c, BUILD_LIST, 0);
4324 for (Py_ssize_t i = 0; i < asdl_seq_LEN(e->v.JoinedStr.values); i++) {
4325 VISIT(c, expr, asdl_seq_GET(e->v.JoinedStr.values, i));
4326 ADDOP_I(c, LIST_APPEND, 1);
4327 }
4328 ADDOP_I(c, CALL_METHOD, 1);
4329 }
4330 else {
4331 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
4332 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) {
4333 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
4334 }
4335 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004336 return 1;
4337}
4338
Eric V. Smitha78c7952015-11-03 12:45:05 -05004339/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004340static int
4341compiler_formatted_value(struct compiler *c, expr_ty e)
4342{
Eric V. Smitha78c7952015-11-03 12:45:05 -05004343 /* Our oparg encodes 2 pieces of information: the conversion
4344 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04004345
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004346 Convert the conversion char to 3 bits:
4347 : 000 0x0 FVC_NONE The default if nothing specified.
Eric V. Smitha78c7952015-11-03 12:45:05 -05004348 !s : 001 0x1 FVC_STR
4349 !r : 010 0x2 FVC_REPR
4350 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04004351
Eric V. Smitha78c7952015-11-03 12:45:05 -05004352 next bit is whether or not we have a format spec:
4353 yes : 100 0x4
4354 no : 000 0x0
4355 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004356
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004357 int conversion = e->v.FormattedValue.conversion;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004358 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004359
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004360 /* The expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004361 VISIT(c, expr, e->v.FormattedValue.value);
4362
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004363 switch (conversion) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004364 case 's': oparg = FVC_STR; break;
4365 case 'r': oparg = FVC_REPR; break;
4366 case 'a': oparg = FVC_ASCII; break;
4367 case -1: oparg = FVC_NONE; break;
4368 default:
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004369 PyErr_Format(PyExc_SystemError,
4370 "Unrecognized conversion character %d", conversion);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004371 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004372 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004373 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004374 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004375 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004376 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004377 }
4378
Eric V. Smitha78c7952015-11-03 12:45:05 -05004379 /* And push our opcode and oparg */
4380 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004381
Eric V. Smith235a6f02015-09-19 14:51:32 -04004382 return 1;
4383}
4384
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004385static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004386compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004387{
4388 Py_ssize_t i, n = end - begin;
4389 keyword_ty kw;
4390 PyObject *keys, *key;
4391 assert(n > 0);
Mark Shannon11e0b292021-04-15 14:28:56 +01004392 int big = n*2 > STACK_USE_GUIDELINE;
4393 if (n > 1 && !big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004394 for (i = begin; i < end; i++) {
4395 kw = asdl_seq_GET(keywords, i);
4396 VISIT(c, expr, kw->value);
4397 }
4398 keys = PyTuple_New(n);
4399 if (keys == NULL) {
4400 return 0;
4401 }
4402 for (i = begin; i < end; i++) {
4403 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
4404 Py_INCREF(key);
4405 PyTuple_SET_ITEM(keys, i - begin, key);
4406 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004407 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004408 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
Mark Shannon11e0b292021-04-15 14:28:56 +01004409 return 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004410 }
Mark Shannon11e0b292021-04-15 14:28:56 +01004411 if (big) {
4412 ADDOP_I_NOLINE(c, BUILD_MAP, 0);
4413 }
4414 for (i = begin; i < end; i++) {
4415 kw = asdl_seq_GET(keywords, i);
4416 ADDOP_LOAD_CONST(c, kw->arg);
4417 VISIT(c, expr, kw->value);
4418 if (big) {
4419 ADDOP_I_NOLINE(c, MAP_ADD, 1);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004420 }
Mark Shannon11e0b292021-04-15 14:28:56 +01004421 }
4422 if (!big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004423 ADDOP_I(c, BUILD_MAP, n);
4424 }
4425 return 1;
4426}
4427
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004428/* shared code between compiler_call and compiler_class */
4429static int
4430compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01004431 int n, /* Args already pushed */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004432 asdl_expr_seq *args,
4433 asdl_keyword_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004434{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004435 Py_ssize_t i, nseen, nelts, nkwelts;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004436
Pablo Galindo254ec782020-04-03 20:37:13 +01004437 if (validate_keywords(c, keywords) == -1) {
4438 return 0;
4439 }
4440
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004441 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004442 nkwelts = asdl_seq_LEN(keywords);
4443
Mark Shannon11e0b292021-04-15 14:28:56 +01004444 if (nelts + nkwelts*2 > STACK_USE_GUIDELINE) {
4445 goto ex_call;
4446 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004447 for (i = 0; i < nelts; i++) {
4448 expr_ty elt = asdl_seq_GET(args, i);
4449 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00004450 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004451 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004452 }
4453 for (i = 0; i < nkwelts; i++) {
4454 keyword_ty kw = asdl_seq_GET(keywords, i);
4455 if (kw->arg == NULL) {
4456 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004457 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004458 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004459
Mark Shannon13bc1392020-01-23 09:25:17 +00004460 /* No * or ** args, so can use faster calling sequence */
4461 for (i = 0; i < nelts; i++) {
4462 expr_ty elt = asdl_seq_GET(args, i);
4463 assert(elt->kind != Starred_kind);
4464 VISIT(c, expr, elt);
4465 }
4466 if (nkwelts) {
4467 PyObject *names;
4468 VISIT_SEQ(c, keyword, keywords);
4469 names = PyTuple_New(nkwelts);
4470 if (names == NULL) {
4471 return 0;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004472 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004473 for (i = 0; i < nkwelts; i++) {
4474 keyword_ty kw = asdl_seq_GET(keywords, i);
4475 Py_INCREF(kw->arg);
4476 PyTuple_SET_ITEM(names, i, kw->arg);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004477 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004478 ADDOP_LOAD_CONST_NEW(c, names);
4479 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
4480 return 1;
4481 }
4482 else {
4483 ADDOP_I(c, CALL_FUNCTION, n + nelts);
4484 return 1;
4485 }
4486
4487ex_call:
4488
4489 /* Do positional arguments. */
4490 if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
4491 VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
4492 }
4493 else if (starunpack_helper(c, args, n, BUILD_LIST,
4494 LIST_APPEND, LIST_EXTEND, 1) == 0) {
4495 return 0;
4496 }
4497 /* Then keyword arguments */
4498 if (nkwelts) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004499 /* Has a new dict been pushed */
4500 int have_dict = 0;
Mark Shannon13bc1392020-01-23 09:25:17 +00004501
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004502 nseen = 0; /* the number of keyword arguments on the stack following */
4503 for (i = 0; i < nkwelts; i++) {
4504 keyword_ty kw = asdl_seq_GET(keywords, i);
4505 if (kw->arg == NULL) {
4506 /* A keyword argument unpacking. */
4507 if (nseen) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004508 if (!compiler_subkwargs(c, keywords, i - nseen, i)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004509 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004510 }
Mark Shannondb64f122020-06-01 10:42:42 +01004511 if (have_dict) {
4512 ADDOP_I(c, DICT_MERGE, 1);
4513 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004514 have_dict = 1;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004515 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004516 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004517 if (!have_dict) {
4518 ADDOP_I(c, BUILD_MAP, 0);
4519 have_dict = 1;
4520 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004521 VISIT(c, expr, kw->value);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004522 ADDOP_I(c, DICT_MERGE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004523 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004524 else {
4525 nseen++;
4526 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004527 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004528 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004529 /* Pack up any trailing keyword arguments. */
Mark Shannon8a4cd702020-01-27 09:57:45 +00004530 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004531 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004532 }
4533 if (have_dict) {
4534 ADDOP_I(c, DICT_MERGE, 1);
4535 }
4536 have_dict = 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004537 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004538 assert(have_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004539 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004540 ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0);
4541 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004542}
4543
Nick Coghlan650f0d02007-04-15 12:05:43 +00004544
4545/* List and set comprehensions and generator expressions work by creating a
4546 nested function to perform the actual iteration. This means that the
4547 iteration variables don't leak into the current scope.
4548 The defined function is called immediately following its definition, with the
4549 result of that call being the result of the expression.
4550 The LC/SC version returns the populated container, while the GE version is
4551 flagged in symtable.c as a generator, so it returns the generator object
4552 when the function is called.
Nick Coghlan650f0d02007-04-15 12:05:43 +00004553
4554 Possible cleanups:
4555 - iterate over the generator sequence instead of using recursion
4556*/
4557
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004558
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004559static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004560compiler_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004561 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004562 int depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004563 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004564{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004565 comprehension_ty gen;
4566 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4567 if (gen->is_async) {
4568 return compiler_async_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004569 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004570 } else {
4571 return compiler_sync_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004572 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004573 }
4574}
4575
4576static int
4577compiler_sync_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004578 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004579 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004580 expr_ty elt, expr_ty val, int type)
4581{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004582 /* generate code for the iterator, then each of the ifs,
4583 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004585 comprehension_ty gen;
4586 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004587 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004589 start = compiler_new_block(c);
4590 skip = compiler_new_block(c);
4591 if_cleanup = compiler_new_block(c);
4592 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004594 if (start == NULL || skip == NULL || if_cleanup == NULL ||
4595 anchor == NULL)
4596 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004598 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004600 if (gen_index == 0) {
4601 /* Receive outermost iter as an implicit argument */
4602 c->u->u_argcount = 1;
4603 ADDOP_I(c, LOAD_FAST, 0);
4604 }
4605 else {
4606 /* Sub-iter - calculate on the fly */
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004607 /* Fast path for the temporary variable assignment idiom:
4608 for y in [f(x)]
4609 */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004610 asdl_expr_seq *elts;
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004611 switch (gen->iter->kind) {
4612 case List_kind:
4613 elts = gen->iter->v.List.elts;
4614 break;
4615 case Tuple_kind:
4616 elts = gen->iter->v.Tuple.elts;
4617 break;
4618 default:
4619 elts = NULL;
4620 }
4621 if (asdl_seq_LEN(elts) == 1) {
4622 expr_ty elt = asdl_seq_GET(elts, 0);
4623 if (elt->kind != Starred_kind) {
4624 VISIT(c, expr, elt);
4625 start = NULL;
4626 }
4627 }
4628 if (start) {
4629 VISIT(c, expr, gen->iter);
4630 ADDOP(c, GET_ITER);
4631 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004632 }
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004633 if (start) {
4634 depth++;
4635 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01004636 ADDOP_JUMP(c, FOR_ITER, anchor);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004637 NEXT_BLOCK(c);
4638 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004639 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004641 /* XXX this needs to be cleaned up...a lot! */
4642 n = asdl_seq_LEN(gen->ifs);
4643 for (i = 0; i < n; i++) {
4644 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004645 if (!compiler_jump_if(c, e, if_cleanup, 0))
4646 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004647 NEXT_BLOCK(c);
4648 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004650 if (++gen_index < asdl_seq_LEN(generators))
4651 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004652 generators, gen_index, depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004653 elt, val, type))
4654 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004656 /* only append after the last for generator */
4657 if (gen_index >= asdl_seq_LEN(generators)) {
4658 /* comprehension specific code */
4659 switch (type) {
4660 case COMP_GENEXP:
4661 VISIT(c, expr, elt);
4662 ADDOP(c, YIELD_VALUE);
4663 ADDOP(c, POP_TOP);
4664 break;
4665 case COMP_LISTCOMP:
4666 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004667 ADDOP_I(c, LIST_APPEND, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004668 break;
4669 case COMP_SETCOMP:
4670 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004671 ADDOP_I(c, SET_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004672 break;
4673 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004674 /* With '{k: v}', k is evaluated before v, so we do
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004675 the same. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004676 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004677 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004678 ADDOP_I(c, MAP_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004679 break;
4680 default:
4681 return 0;
4682 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004684 compiler_use_next_block(c, skip);
4685 }
4686 compiler_use_next_block(c, if_cleanup);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004687 if (start) {
Mark Shannon582aaf12020-08-04 17:30:11 +01004688 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004689 compiler_use_next_block(c, anchor);
4690 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004691
4692 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004693}
4694
4695static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004696compiler_async_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004697 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004698 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004699 expr_ty elt, expr_ty val, int type)
4700{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004701 comprehension_ty gen;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004702 basicblock *start, *if_cleanup, *except;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004703 Py_ssize_t i, n;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004704 start = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004705 except = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004706 if_cleanup = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004707
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004708 if (start == NULL || if_cleanup == NULL || except == NULL) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004709 return 0;
4710 }
4711
4712 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4713
4714 if (gen_index == 0) {
4715 /* Receive outermost iter as an implicit argument */
4716 c->u->u_argcount = 1;
4717 ADDOP_I(c, LOAD_FAST, 0);
4718 }
4719 else {
4720 /* Sub-iter - calculate on the fly */
4721 VISIT(c, expr, gen->iter);
4722 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004723 }
4724
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004725 compiler_use_next_block(c, start);
tomKPZ7a7ba3d2021-04-07 07:43:45 -07004726 /* Runtime will push a block here, so we need to account for that */
4727 if (!compiler_push_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start,
4728 NULL, NULL)) {
4729 return 0;
4730 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004731
Mark Shannon582aaf12020-08-04 17:30:11 +01004732 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004733 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004734 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004735 ADDOP(c, YIELD_FROM);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004736 ADDOP(c, POP_BLOCK);
Serhiy Storchaka24d32012018-03-10 18:22:34 +02004737 VISIT(c, expr, gen->target);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004738
4739 n = asdl_seq_LEN(gen->ifs);
4740 for (i = 0; i < n; i++) {
4741 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004742 if (!compiler_jump_if(c, e, if_cleanup, 0))
4743 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004744 NEXT_BLOCK(c);
4745 }
4746
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004747 depth++;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004748 if (++gen_index < asdl_seq_LEN(generators))
4749 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004750 generators, gen_index, depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004751 elt, val, type))
4752 return 0;
4753
4754 /* only append after the last for generator */
4755 if (gen_index >= asdl_seq_LEN(generators)) {
4756 /* comprehension specific code */
4757 switch (type) {
4758 case COMP_GENEXP:
4759 VISIT(c, expr, elt);
4760 ADDOP(c, YIELD_VALUE);
4761 ADDOP(c, POP_TOP);
4762 break;
4763 case COMP_LISTCOMP:
4764 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004765 ADDOP_I(c, LIST_APPEND, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004766 break;
4767 case COMP_SETCOMP:
4768 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004769 ADDOP_I(c, SET_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004770 break;
4771 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004772 /* With '{k: v}', k is evaluated before v, so we do
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004773 the same. */
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004774 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004775 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004776 ADDOP_I(c, MAP_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004777 break;
4778 default:
4779 return 0;
4780 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004781 }
4782 compiler_use_next_block(c, if_cleanup);
Mark Shannon582aaf12020-08-04 17:30:11 +01004783 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004784
tomKPZ7a7ba3d2021-04-07 07:43:45 -07004785 compiler_pop_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start);
4786
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004787 compiler_use_next_block(c, except);
4788 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004789
4790 return 1;
4791}
4792
4793static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004794compiler_comprehension(struct compiler *c, expr_ty e, int type,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004795 identifier name, asdl_comprehension_seq *generators, expr_ty elt,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004796 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004797{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004798 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004799 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004800 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004801 int is_async_generator = 0;
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004802 int top_level_await = IS_TOP_LEVEL_AWAIT(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004803
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004804
Batuhan Taßkaya9052f7a2020-03-19 14:35:44 +03004805 int is_async_function = c->u->u_ste->ste_coroutine;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004806
Batuhan Taßkaya9052f7a2020-03-19 14:35:44 +03004807 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004808 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4809 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004810 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004811 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004812 }
Mark Shannon7674c832021-06-21 11:47:16 +01004813 SET_LOC(c, e);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004814
4815 is_async_generator = c->u->u_ste->ste_coroutine;
4816
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004817 if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004818 compiler_error(c, "asynchronous comprehension outside of "
4819 "an asynchronous function");
4820 goto error_in_scope;
4821 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004823 if (type != COMP_GENEXP) {
4824 int op;
4825 switch (type) {
4826 case COMP_LISTCOMP:
4827 op = BUILD_LIST;
4828 break;
4829 case COMP_SETCOMP:
4830 op = BUILD_SET;
4831 break;
4832 case COMP_DICTCOMP:
4833 op = BUILD_MAP;
4834 break;
4835 default:
4836 PyErr_Format(PyExc_SystemError,
4837 "unknown comprehension type %d", type);
4838 goto error_in_scope;
4839 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004841 ADDOP_I(c, op, 0);
4842 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004843
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004844 if (!compiler_comprehension_generator(c, generators, 0, 0, elt,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004845 val, type))
4846 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004848 if (type != COMP_GENEXP) {
4849 ADDOP(c, RETURN_VALUE);
4850 }
4851
4852 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004853 qualname = c->u->u_qualname;
4854 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004855 compiler_exit_scope(c);
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004856 if (top_level_await && is_async_generator){
4857 c->u->u_ste->ste_coroutine = 1;
4858 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004859 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004860 goto error;
4861
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004862 if (!compiler_make_closure(c, co, 0, qualname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004863 goto error;
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004864 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004865 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004866 Py_DECREF(co);
4867
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004868 VISIT(c, expr, outermost->iter);
4869
4870 if (outermost->is_async) {
4871 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004872 } else {
4873 ADDOP(c, GET_ITER);
4874 }
4875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004876 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004877
4878 if (is_async_generator && type != COMP_GENEXP) {
4879 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004880 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004881 ADDOP(c, YIELD_FROM);
4882 }
4883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004884 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004885error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004886 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004887error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004888 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004889 Py_XDECREF(co);
4890 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004891}
4892
4893static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004894compiler_genexp(struct compiler *c, expr_ty e)
4895{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004896 static identifier name;
4897 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004898 name = PyUnicode_InternFromString("<genexpr>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004899 if (!name)
4900 return 0;
4901 }
4902 assert(e->kind == GeneratorExp_kind);
4903 return compiler_comprehension(c, e, COMP_GENEXP, name,
4904 e->v.GeneratorExp.generators,
4905 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004906}
4907
4908static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004909compiler_listcomp(struct compiler *c, expr_ty e)
4910{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004911 static identifier name;
4912 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004913 name = PyUnicode_InternFromString("<listcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004914 if (!name)
4915 return 0;
4916 }
4917 assert(e->kind == ListComp_kind);
4918 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4919 e->v.ListComp.generators,
4920 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004921}
4922
4923static int
4924compiler_setcomp(struct compiler *c, expr_ty e)
4925{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004926 static identifier name;
4927 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004928 name = PyUnicode_InternFromString("<setcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004929 if (!name)
4930 return 0;
4931 }
4932 assert(e->kind == SetComp_kind);
4933 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4934 e->v.SetComp.generators,
4935 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004936}
4937
4938
4939static int
4940compiler_dictcomp(struct compiler *c, expr_ty e)
4941{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004942 static identifier name;
4943 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004944 name = PyUnicode_InternFromString("<dictcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004945 if (!name)
4946 return 0;
4947 }
4948 assert(e->kind == DictComp_kind);
4949 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4950 e->v.DictComp.generators,
4951 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004952}
4953
4954
4955static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004956compiler_visit_keyword(struct compiler *c, keyword_ty k)
4957{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004958 VISIT(c, expr, k->value);
4959 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004960}
4961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004962/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004963 whether they are true or false.
4964
4965 Return values: 1 for true, 0 for false, -1 for non-constant.
4966 */
4967
4968static int
Mark Shannonfee55262019-11-21 09:11:43 +00004969compiler_with_except_finish(struct compiler *c) {
4970 basicblock *exit;
4971 exit = compiler_new_block(c);
4972 if (exit == NULL)
4973 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01004974 ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit);
Mark Shannon266b4622020-11-17 19:30:14 +00004975 NEXT_BLOCK(c);
Mark Shannonbf353f32020-12-17 13:55:28 +00004976 ADDOP_I(c, RERAISE, 1);
Mark Shannonfee55262019-11-21 09:11:43 +00004977 compiler_use_next_block(c, exit);
4978 ADDOP(c, POP_TOP);
4979 ADDOP(c, POP_TOP);
4980 ADDOP(c, POP_TOP);
4981 ADDOP(c, POP_EXCEPT);
4982 ADDOP(c, POP_TOP);
4983 return 1;
4984}
Yury Selivanov75445082015-05-11 22:57:16 -04004985
4986/*
4987 Implements the async with statement.
4988
4989 The semantics outlined in that PEP are as follows:
4990
4991 async with EXPR as VAR:
4992 BLOCK
4993
4994 It is implemented roughly as:
4995
4996 context = EXPR
4997 exit = context.__aexit__ # not calling it
4998 value = await context.__aenter__()
4999 try:
5000 VAR = value # if VAR present in the syntax
5001 BLOCK
5002 finally:
5003 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03005004 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04005005 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03005006 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04005007 if not (await exit(*exc)):
5008 raise
5009 */
5010static int
5011compiler_async_with(struct compiler *c, stmt_ty s, int pos)
5012{
Mark Shannonfee55262019-11-21 09:11:43 +00005013 basicblock *block, *final, *exit;
Yury Selivanov75445082015-05-11 22:57:16 -04005014 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
5015
5016 assert(s->kind == AsyncWith_kind);
Pablo Galindo90235812020-03-15 04:29:22 +00005017 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005018 c->u->u_ste->ste_coroutine = 1;
5019 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
Zsolt Dollensteine2396502018-04-27 08:58:56 -07005020 return compiler_error(c, "'async with' outside async function");
5021 }
Yury Selivanov75445082015-05-11 22:57:16 -04005022
5023 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00005024 final = compiler_new_block(c);
5025 exit = compiler_new_block(c);
5026 if (!block || !final || !exit)
Yury Selivanov75445082015-05-11 22:57:16 -04005027 return 0;
5028
5029 /* Evaluate EXPR */
5030 VISIT(c, expr, item->context_expr);
5031
5032 ADDOP(c, BEFORE_ASYNC_WITH);
5033 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005034 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005035 ADDOP(c, YIELD_FROM);
5036
Mark Shannon582aaf12020-08-04 17:30:11 +01005037 ADDOP_JUMP(c, SETUP_ASYNC_WITH, final);
Yury Selivanov75445082015-05-11 22:57:16 -04005038
5039 /* SETUP_ASYNC_WITH pushes a finally block. */
5040 compiler_use_next_block(c, block);
Mark Shannon5979e812021-04-30 14:32:47 +01005041 if (!compiler_push_fblock(c, ASYNC_WITH, block, final, s)) {
Yury Selivanov75445082015-05-11 22:57:16 -04005042 return 0;
5043 }
5044
5045 if (item->optional_vars) {
5046 VISIT(c, expr, item->optional_vars);
5047 }
5048 else {
5049 /* Discard result from context.__aenter__() */
5050 ADDOP(c, POP_TOP);
5051 }
5052
5053 pos++;
5054 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
5055 /* BLOCK code */
5056 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
5057 else if (!compiler_async_with(c, s, pos))
5058 return 0;
5059
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005060 compiler_pop_fblock(c, ASYNC_WITH, block);
Mark Shannonfee55262019-11-21 09:11:43 +00005061 ADDOP(c, POP_BLOCK);
5062 /* End of body; start the cleanup */
Yury Selivanov75445082015-05-11 22:57:16 -04005063
Mark Shannonfee55262019-11-21 09:11:43 +00005064 /* For successful outcome:
5065 * call __exit__(None, None, None)
5066 */
Mark Shannon5979e812021-04-30 14:32:47 +01005067 SET_LOC(c, s);
Mark Shannonfee55262019-11-21 09:11:43 +00005068 if(!compiler_call_exit_with_nones(c))
Yury Selivanov75445082015-05-11 22:57:16 -04005069 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00005070 ADDOP(c, GET_AWAITABLE);
Miss Islington (bot)ebbd0ac2021-08-31 11:08:32 -07005071 ADDOP_LOAD_CONST(c, Py_None);
Mark Shannonfee55262019-11-21 09:11:43 +00005072 ADDOP(c, YIELD_FROM);
Yury Selivanov75445082015-05-11 22:57:16 -04005073
Mark Shannonfee55262019-11-21 09:11:43 +00005074 ADDOP(c, POP_TOP);
Yury Selivanov75445082015-05-11 22:57:16 -04005075
Mark Shannon582aaf12020-08-04 17:30:11 +01005076 ADDOP_JUMP(c, JUMP_ABSOLUTE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00005077
5078 /* For exceptional outcome: */
5079 compiler_use_next_block(c, final);
Mark Shannonfee55262019-11-21 09:11:43 +00005080 ADDOP(c, WITH_EXCEPT_START);
Yury Selivanov75445082015-05-11 22:57:16 -04005081 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005082 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005083 ADDOP(c, YIELD_FROM);
Mark Shannonfee55262019-11-21 09:11:43 +00005084 compiler_with_except_finish(c);
Yury Selivanov75445082015-05-11 22:57:16 -04005085
Mark Shannonfee55262019-11-21 09:11:43 +00005086compiler_use_next_block(c, exit);
Yury Selivanov75445082015-05-11 22:57:16 -04005087 return 1;
5088}
5089
5090
Guido van Rossumc2e20742006-02-27 22:32:47 +00005091/*
5092 Implements the with statement from PEP 343.
Guido van Rossumc2e20742006-02-27 22:32:47 +00005093 with EXPR as VAR:
5094 BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00005095 is implemented as:
5096 <code for EXPR>
5097 SETUP_WITH E
5098 <code to store to VAR> or POP_TOP
5099 <code for BLOCK>
5100 LOAD_CONST (None, None, None)
5101 CALL_FUNCTION_EX 0
5102 JUMP_FORWARD EXIT
5103 E: WITH_EXCEPT_START (calls EXPR.__exit__)
5104 POP_JUMP_IF_TRUE T:
5105 RERAISE
5106 T: POP_TOP * 3 (remove exception from stack)
5107 POP_EXCEPT
5108 POP_TOP
5109 EXIT:
Guido van Rossumc2e20742006-02-27 22:32:47 +00005110 */
Mark Shannonfee55262019-11-21 09:11:43 +00005111
Guido van Rossumc2e20742006-02-27 22:32:47 +00005112static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005113compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00005114{
Mark Shannonfee55262019-11-21 09:11:43 +00005115 basicblock *block, *final, *exit;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005116 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005117
5118 assert(s->kind == With_kind);
5119
Guido van Rossumc2e20742006-02-27 22:32:47 +00005120 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00005121 final = compiler_new_block(c);
5122 exit = compiler_new_block(c);
5123 if (!block || !final || !exit)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005124 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005125
Thomas Wouters477c8d52006-05-27 19:21:47 +00005126 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005127 VISIT(c, expr, item->context_expr);
Mark Shannonfee55262019-11-21 09:11:43 +00005128 /* Will push bound __exit__ */
Mark Shannon582aaf12020-08-04 17:30:11 +01005129 ADDOP_JUMP(c, SETUP_WITH, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005130
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005131 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00005132 compiler_use_next_block(c, block);
Mark Shannon5979e812021-04-30 14:32:47 +01005133 if (!compiler_push_fblock(c, WITH, block, final, s)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005134 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005135 }
5136
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005137 if (item->optional_vars) {
5138 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005139 }
5140 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005141 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005142 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005143 }
5144
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005145 pos++;
5146 if (pos == asdl_seq_LEN(s->v.With.items))
5147 /* BLOCK code */
5148 VISIT_SEQ(c, stmt, s->v.With.body)
5149 else if (!compiler_with(c, s, pos))
5150 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005151
Mark Shannon3bd60352021-01-13 12:05:43 +00005152
5153 /* Mark all following code as artificial */
5154 c->u->u_lineno = -1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005155 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005156 compiler_pop_fblock(c, WITH, block);
Mark Shannon13bc1392020-01-23 09:25:17 +00005157
Mark Shannonfee55262019-11-21 09:11:43 +00005158 /* End of body; start the cleanup. */
Mark Shannon13bc1392020-01-23 09:25:17 +00005159
Mark Shannonfee55262019-11-21 09:11:43 +00005160 /* For successful outcome:
5161 * call __exit__(None, None, None)
5162 */
Mark Shannon5979e812021-04-30 14:32:47 +01005163 SET_LOC(c, s);
Mark Shannonfee55262019-11-21 09:11:43 +00005164 if (!compiler_call_exit_with_nones(c))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005165 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00005166 ADDOP(c, POP_TOP);
Mark Shannon582aaf12020-08-04 17:30:11 +01005167 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005168
Mark Shannonfee55262019-11-21 09:11:43 +00005169 /* For exceptional outcome: */
5170 compiler_use_next_block(c, final);
Mark Shannonfee55262019-11-21 09:11:43 +00005171 ADDOP(c, WITH_EXCEPT_START);
5172 compiler_with_except_finish(c);
5173
5174 compiler_use_next_block(c, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005175 return 1;
5176}
5177
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005178static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005179compiler_visit_expr1(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005181 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07005182 case NamedExpr_kind:
5183 VISIT(c, expr, e->v.NamedExpr.value);
5184 ADDOP(c, DUP_TOP);
5185 VISIT(c, expr, e->v.NamedExpr.target);
5186 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005187 case BoolOp_kind:
5188 return compiler_boolop(c, e);
5189 case BinOp_kind:
5190 VISIT(c, expr, e->v.BinOp.left);
5191 VISIT(c, expr, e->v.BinOp.right);
Andy Lester76d58772020-03-10 21:18:12 -05005192 ADDOP(c, binop(e->v.BinOp.op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005193 break;
5194 case UnaryOp_kind:
5195 VISIT(c, expr, e->v.UnaryOp.operand);
5196 ADDOP(c, unaryop(e->v.UnaryOp.op));
5197 break;
5198 case Lambda_kind:
5199 return compiler_lambda(c, e);
5200 case IfExp_kind:
5201 return compiler_ifexp(c, e);
5202 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005203 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005204 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005205 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005206 case GeneratorExp_kind:
5207 return compiler_genexp(c, e);
5208 case ListComp_kind:
5209 return compiler_listcomp(c, e);
5210 case SetComp_kind:
5211 return compiler_setcomp(c, e);
5212 case DictComp_kind:
5213 return compiler_dictcomp(c, e);
5214 case Yield_kind:
5215 if (c->u->u_ste->ste_type != FunctionBlock)
5216 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005217 if (e->v.Yield.value) {
5218 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005219 }
5220 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005221 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005222 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005223 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005224 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005225 case YieldFrom_kind:
5226 if (c->u->u_ste->ste_type != FunctionBlock)
5227 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04005228
5229 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
5230 return compiler_error(c, "'yield from' inside async function");
5231
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005232 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04005233 ADDOP(c, GET_YIELD_FROM_ITER);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005234 ADDOP_LOAD_CONST(c, Py_None);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005235 ADDOP(c, YIELD_FROM);
5236 break;
Yury Selivanov75445082015-05-11 22:57:16 -04005237 case Await_kind:
Pablo Galindo90235812020-03-15 04:29:22 +00005238 if (!IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005239 if (c->u->u_ste->ste_type != FunctionBlock){
5240 return compiler_error(c, "'await' outside function");
5241 }
Yury Selivanov75445082015-05-11 22:57:16 -04005242
Victor Stinner331a6a52019-05-27 16:39:22 +02005243 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005244 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
5245 return compiler_error(c, "'await' outside async function");
5246 }
5247 }
Yury Selivanov75445082015-05-11 22:57:16 -04005248
5249 VISIT(c, expr, e->v.Await.value);
5250 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005251 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005252 ADDOP(c, YIELD_FROM);
5253 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005254 case Compare_kind:
5255 return compiler_compare(c, e);
5256 case Call_kind:
5257 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005258 case Constant_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005259 ADDOP_LOAD_CONST(c, e->v.Constant.value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005260 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005261 case JoinedStr_kind:
5262 return compiler_joined_str(c, e);
5263 case FormattedValue_kind:
5264 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005265 /* The following exprs can be assignment targets. */
5266 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005267 VISIT(c, expr, e->v.Attribute.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005268 switch (e->v.Attribute.ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005269 case Load:
Mark Shannond48848c2021-03-14 18:01:30 +00005270 {
5271 int old_lineno = c->u->u_lineno;
5272 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005273 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005274 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005275 break;
Mark Shannond48848c2021-03-14 18:01:30 +00005276 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005277 case Store:
Mark Shannond48848c2021-03-14 18:01:30 +00005278 if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005279 return 0;
Mark Shannond48848c2021-03-14 18:01:30 +00005280 }
5281 int old_lineno = c->u->u_lineno;
5282 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005283 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005284 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005285 break;
5286 case Del:
5287 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
5288 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005289 }
5290 break;
5291 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005292 return compiler_subscript(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005293 case Starred_kind:
5294 switch (e->v.Starred.ctx) {
5295 case Store:
5296 /* In all legitimate cases, the Starred node was already replaced
5297 * by compiler_list/compiler_tuple. XXX: is that okay? */
5298 return compiler_error(c,
5299 "starred assignment target must be in a list or tuple");
5300 default:
5301 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005302 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005303 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005304 break;
5305 case Slice_kind:
5306 return compiler_slice(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005307 case Name_kind:
5308 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
5309 /* child nodes of List and Tuple will have expr_context set */
5310 case List_kind:
5311 return compiler_list(c, e);
5312 case Tuple_kind:
5313 return compiler_tuple(c, e);
5314 }
5315 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005316}
5317
5318static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005319compiler_visit_expr(struct compiler *c, expr_ty e)
5320{
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005321 int old_lineno = c->u->u_lineno;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005322 int old_end_lineno = c->u->u_end_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005323 int old_col_offset = c->u->u_col_offset;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005324 int old_end_col_offset = c->u->u_end_col_offset;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005325 SET_LOC(c, e);
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005326 int res = compiler_visit_expr1(c, e);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005327 c->u->u_lineno = old_lineno;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005328 c->u->u_end_lineno = old_end_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005329 c->u->u_col_offset = old_col_offset;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005330 c->u->u_end_col_offset = old_end_col_offset;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005331 return res;
5332}
5333
5334static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005335compiler_augassign(struct compiler *c, stmt_ty s)
5336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005337 assert(s->kind == AugAssign_kind);
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005338 expr_ty e = s->v.AugAssign.target;
5339
5340 int old_lineno = c->u->u_lineno;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005341 int old_end_lineno = c->u->u_end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005342 int old_col_offset = c->u->u_col_offset;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005343 int old_end_col_offset = c->u->u_end_col_offset;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005344 SET_LOC(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005346 switch (e->kind) {
5347 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005348 VISIT(c, expr, e->v.Attribute.value);
5349 ADDOP(c, DUP_TOP);
Mark Shannond48848c2021-03-14 18:01:30 +00005350 int old_lineno = c->u->u_lineno;
5351 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005352 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005353 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005354 break;
5355 case Subscript_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005356 VISIT(c, expr, e->v.Subscript.value);
5357 VISIT(c, expr, e->v.Subscript.slice);
5358 ADDOP(c, DUP_TOP_TWO);
5359 ADDOP(c, BINARY_SUBSCR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005360 break;
5361 case Name_kind:
5362 if (!compiler_nameop(c, e->v.Name.id, Load))
5363 return 0;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005364 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005365 default:
5366 PyErr_Format(PyExc_SystemError,
5367 "invalid node type (%d) for augmented assignment",
5368 e->kind);
5369 return 0;
5370 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005371
5372 c->u->u_lineno = old_lineno;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005373 c->u->u_end_lineno = old_end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005374 c->u->u_col_offset = old_col_offset;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005375 c->u->u_end_col_offset = old_end_col_offset;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005376
5377 VISIT(c, expr, s->v.AugAssign.value);
5378 ADDOP(c, inplace_binop(s->v.AugAssign.op));
5379
5380 SET_LOC(c, e);
5381
5382 switch (e->kind) {
5383 case Attribute_kind:
Mark Shannond48848c2021-03-14 18:01:30 +00005384 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005385 ADDOP(c, ROT_TWO);
5386 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5387 break;
5388 case Subscript_kind:
5389 ADDOP(c, ROT_THREE);
5390 ADDOP(c, STORE_SUBSCR);
5391 break;
5392 case Name_kind:
5393 return compiler_nameop(c, e->v.Name.id, Store);
5394 default:
5395 Py_UNREACHABLE();
5396 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005397 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005398}
5399
5400static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005401check_ann_expr(struct compiler *c, expr_ty e)
5402{
5403 VISIT(c, expr, e);
5404 ADDOP(c, POP_TOP);
5405 return 1;
5406}
5407
5408static int
5409check_annotation(struct compiler *c, stmt_ty s)
5410{
Batuhan Taskaya8cc3cfa2021-04-25 05:31:20 +03005411 /* Annotations of complex targets does not produce anything
5412 under annotations future */
5413 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
5414 return 1;
5415 }
5416
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005417 /* Annotations are only evaluated in a module or class. */
5418 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5419 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
5420 return check_ann_expr(c, s->v.AnnAssign.annotation);
5421 }
5422 return 1;
5423}
5424
5425static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005426check_ann_subscr(struct compiler *c, expr_ty e)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005427{
5428 /* We check that everything in a subscript is defined at runtime. */
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005429 switch (e->kind) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005430 case Slice_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005431 if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005432 return 0;
5433 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005434 if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) {
5435 return 0;
5436 }
5437 if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) {
5438 return 0;
5439 }
5440 return 1;
5441 case Tuple_kind: {
5442 /* extended slice */
Pablo Galindoa5634c42020-09-16 19:42:00 +01005443 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005444 Py_ssize_t i, n = asdl_seq_LEN(elts);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005445 for (i = 0; i < n; i++) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005446 if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005447 return 0;
5448 }
5449 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005450 return 1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005451 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005452 default:
5453 return check_ann_expr(c, e);
5454 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005455}
5456
5457static int
5458compiler_annassign(struct compiler *c, stmt_ty s)
5459{
5460 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07005461 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005462
5463 assert(s->kind == AnnAssign_kind);
5464
5465 /* We perform the actual assignment first. */
5466 if (s->v.AnnAssign.value) {
5467 VISIT(c, expr, s->v.AnnAssign.value);
5468 VISIT(c, expr, targ);
5469 }
5470 switch (targ->kind) {
5471 case Name_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005472 if (forbidden_name(c, targ->v.Name.id, Store))
5473 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005474 /* If we have a simple name in a module or class, store annotation. */
5475 if (s->v.AnnAssign.simple &&
5476 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5477 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Pablo Galindob0544ba2021-04-21 12:41:19 +01005478 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
5479 VISIT(c, annexpr, s->v.AnnAssign.annotation)
5480 }
5481 else {
5482 VISIT(c, expr, s->v.AnnAssign.annotation);
5483 }
Mark Shannon332cd5e2018-01-30 00:41:04 +00005484 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02005485 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005486 ADDOP_LOAD_CONST_NEW(c, mangled);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005487 ADDOP(c, STORE_SUBSCR);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005488 }
5489 break;
5490 case Attribute_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005491 if (forbidden_name(c, targ->v.Attribute.attr, Store))
5492 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005493 if (!s->v.AnnAssign.value &&
5494 !check_ann_expr(c, targ->v.Attribute.value)) {
5495 return 0;
5496 }
5497 break;
5498 case Subscript_kind:
5499 if (!s->v.AnnAssign.value &&
5500 (!check_ann_expr(c, targ->v.Subscript.value) ||
5501 !check_ann_subscr(c, targ->v.Subscript.slice))) {
5502 return 0;
5503 }
5504 break;
5505 default:
5506 PyErr_Format(PyExc_SystemError,
5507 "invalid node type (%d) for annotated assignment",
5508 targ->kind);
5509 return 0;
5510 }
5511 /* Annotation is evaluated last. */
5512 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
5513 return 0;
5514 }
5515 return 1;
5516}
5517
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005518/* Raises a SyntaxError and returns 0.
5519 If something goes wrong, a different exception may be raised.
5520*/
5521
5522static int
Brandt Bucher145bf262021-02-26 14:51:55 -08005523compiler_error(struct compiler *c, const char *format, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005524{
Brandt Bucher145bf262021-02-26 14:51:55 -08005525 va_list vargs;
5526#ifdef HAVE_STDARG_PROTOTYPES
5527 va_start(vargs, format);
5528#else
5529 va_start(vargs);
5530#endif
5531 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5532 va_end(vargs);
5533 if (msg == NULL) {
5534 return 0;
5535 }
5536 PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
5537 if (loc == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005538 Py_INCREF(Py_None);
5539 loc = Py_None;
5540 }
Pablo Galindoa77aac42021-04-23 14:27:05 +01005541 PyObject *args = Py_BuildValue("O(OiiOii)", msg, c->c_filename,
5542 c->u->u_lineno, c->u->u_col_offset + 1, loc,
5543 c->u->u_end_lineno, c->u->u_end_col_offset + 1);
Brandt Bucher145bf262021-02-26 14:51:55 -08005544 Py_DECREF(msg);
5545 if (args == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005546 goto exit;
Brandt Bucher145bf262021-02-26 14:51:55 -08005547 }
5548 PyErr_SetObject(PyExc_SyntaxError, args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005549 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005550 Py_DECREF(loc);
Brandt Bucher145bf262021-02-26 14:51:55 -08005551 Py_XDECREF(args);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005552 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005553}
5554
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005555/* Emits a SyntaxWarning and returns 1 on success.
5556 If a SyntaxWarning raised as error, replaces it with a SyntaxError
5557 and returns 0.
5558*/
5559static int
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005560compiler_warn(struct compiler *c, const char *format, ...)
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005561{
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005562 va_list vargs;
5563#ifdef HAVE_STDARG_PROTOTYPES
5564 va_start(vargs, format);
5565#else
5566 va_start(vargs);
5567#endif
5568 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5569 va_end(vargs);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005570 if (msg == NULL) {
5571 return 0;
5572 }
5573 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
5574 c->u->u_lineno, NULL, NULL) < 0)
5575 {
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005576 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005577 /* Replace the SyntaxWarning exception with a SyntaxError
5578 to get a more accurate error report */
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005579 PyErr_Clear();
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005580 assert(PyUnicode_AsUTF8(msg) != NULL);
5581 compiler_error(c, PyUnicode_AsUTF8(msg));
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005582 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005583 Py_DECREF(msg);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005584 return 0;
5585 }
5586 Py_DECREF(msg);
5587 return 1;
5588}
5589
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005590static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005591compiler_subscript(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005592{
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005593 expr_context_ty ctx = e->v.Subscript.ctx;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005594 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005595
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005596 if (ctx == Load) {
5597 if (!check_subscripter(c, e->v.Subscript.value)) {
5598 return 0;
5599 }
5600 if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
5601 return 0;
5602 }
5603 }
5604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005605 switch (ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005606 case Load: op = BINARY_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005607 case Store: op = STORE_SUBSCR; break;
5608 case Del: op = DELETE_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005609 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005610 assert(op);
5611 VISIT(c, expr, e->v.Subscript.value);
5612 VISIT(c, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005613 ADDOP(c, op);
5614 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005615}
5616
5617static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005618compiler_slice(struct compiler *c, expr_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005619{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005620 int n = 2;
5621 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005623 /* only handles the cases where BUILD_SLICE is emitted */
5624 if (s->v.Slice.lower) {
5625 VISIT(c, expr, s->v.Slice.lower);
5626 }
5627 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005628 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005629 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005631 if (s->v.Slice.upper) {
5632 VISIT(c, expr, s->v.Slice.upper);
5633 }
5634 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005635 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005636 }
5637
5638 if (s->v.Slice.step) {
5639 n++;
5640 VISIT(c, expr, s->v.Slice.step);
5641 }
5642 ADDOP_I(c, BUILD_SLICE, n);
5643 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005644}
5645
Brandt Bucher145bf262021-02-26 14:51:55 -08005646
5647// PEP 634: Structural Pattern Matching
5648
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005649// To keep things simple, all compiler_pattern_* and pattern_helper_* routines
5650// follow the convention of consuming TOS (the subject for the given pattern)
5651// and calling jump_to_fail_pop on failure (no match).
5652
5653// When calling into these routines, it's important that pc->on_top be kept
5654// updated to reflect the current number of items that we are using on the top
5655// of the stack: they will be popped on failure, and any name captures will be
5656// stored *underneath* them on success. This lets us defer all names stores
5657// until the *entire* pattern matches.
Brandt Bucher145bf262021-02-26 14:51:55 -08005658
Brandt Bucher145bf262021-02-26 14:51:55 -08005659#define WILDCARD_CHECK(N) \
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005660 ((N)->kind == MatchAs_kind && !(N)->v.MatchAs.name)
Brandt Bucher145bf262021-02-26 14:51:55 -08005661
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005662#define WILDCARD_STAR_CHECK(N) \
5663 ((N)->kind == MatchStar_kind && !(N)->v.MatchStar.name)
5664
5665// Limit permitted subexpressions, even if the parser & AST validator let them through
5666#define MATCH_VALUE_EXPR(N) \
5667 ((N)->kind == Constant_kind || (N)->kind == Attribute_kind)
Brandt Bucher145bf262021-02-26 14:51:55 -08005668
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005669// Allocate or resize pc->fail_pop to allow for n items to be popped on failure.
5670static int
5671ensure_fail_pop(struct compiler *c, pattern_context *pc, Py_ssize_t n)
5672{
5673 Py_ssize_t size = n + 1;
5674 if (size <= pc->fail_pop_size) {
5675 return 1;
5676 }
5677 Py_ssize_t needed = sizeof(basicblock*) * size;
5678 basicblock **resized = PyObject_Realloc(pc->fail_pop, needed);
5679 if (resized == NULL) {
5680 PyErr_NoMemory();
5681 return 0;
5682 }
5683 pc->fail_pop = resized;
5684 while (pc->fail_pop_size < size) {
5685 basicblock *new_block;
5686 RETURN_IF_FALSE(new_block = compiler_new_block(c));
5687 pc->fail_pop[pc->fail_pop_size++] = new_block;
5688 }
5689 return 1;
5690}
5691
5692// Use op to jump to the correct fail_pop block.
5693static int
5694jump_to_fail_pop(struct compiler *c, pattern_context *pc, int op)
5695{
5696 // Pop any items on the top of the stack, plus any objects we were going to
5697 // capture on success:
5698 Py_ssize_t pops = pc->on_top + PyList_GET_SIZE(pc->stores);
5699 RETURN_IF_FALSE(ensure_fail_pop(c, pc, pops));
5700 ADDOP_JUMP(c, op, pc->fail_pop[pops]);
5701 NEXT_BLOCK(c);
5702 return 1;
5703}
5704
5705// Build all of the fail_pop blocks and reset fail_pop.
5706static int
5707emit_and_reset_fail_pop(struct compiler *c, pattern_context *pc)
5708{
5709 if (!pc->fail_pop_size) {
5710 assert(pc->fail_pop == NULL);
5711 NEXT_BLOCK(c);
5712 return 1;
5713 }
5714 while (--pc->fail_pop_size) {
5715 compiler_use_next_block(c, pc->fail_pop[pc->fail_pop_size]);
5716 if (!compiler_addop(c, POP_TOP)) {
5717 pc->fail_pop_size = 0;
5718 PyObject_Free(pc->fail_pop);
5719 pc->fail_pop = NULL;
5720 return 0;
5721 }
5722 }
5723 compiler_use_next_block(c, pc->fail_pop[0]);
5724 PyObject_Free(pc->fail_pop);
5725 pc->fail_pop = NULL;
5726 return 1;
5727}
5728
5729static int
5730compiler_error_duplicate_store(struct compiler *c, identifier n)
5731{
5732 return compiler_error(c, "multiple assignments to name %R in pattern", n);
5733}
5734
Brandt Bucher145bf262021-02-26 14:51:55 -08005735static int
5736pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc)
5737{
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005738 if (n == NULL) {
5739 ADDOP(c, POP_TOP);
5740 return 1;
5741 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005742 if (forbidden_name(c, n, Store)) {
5743 return 0;
5744 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005745 // Can't assign to the same name twice:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005746 int duplicate = PySequence_Contains(pc->stores, n);
5747 if (duplicate < 0) {
5748 return 0;
Brandt Bucher145bf262021-02-26 14:51:55 -08005749 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005750 if (duplicate) {
5751 return compiler_error_duplicate_store(c, n);
Brandt Bucher145bf262021-02-26 14:51:55 -08005752 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005753 // Rotate this object underneath any items we need to preserve:
5754 ADDOP_I(c, ROT_N, pc->on_top + PyList_GET_SIZE(pc->stores) + 1);
5755 return !PyList_Append(pc->stores, n);
Brandt Bucher145bf262021-02-26 14:51:55 -08005756}
5757
5758
5759static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005760pattern_unpack_helper(struct compiler *c, asdl_pattern_seq *elts)
5761{
5762 Py_ssize_t n = asdl_seq_LEN(elts);
5763 int seen_star = 0;
5764 for (Py_ssize_t i = 0; i < n; i++) {
5765 pattern_ty elt = asdl_seq_GET(elts, i);
5766 if (elt->kind == MatchStar_kind && !seen_star) {
5767 if ((i >= (1 << 8)) ||
5768 (n-i-1 >= (INT_MAX >> 8)))
5769 return compiler_error(c,
5770 "too many expressions in "
5771 "star-unpacking sequence pattern");
5772 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
5773 seen_star = 1;
5774 }
5775 else if (elt->kind == MatchStar_kind) {
5776 return compiler_error(c,
5777 "multiple starred expressions in sequence pattern");
5778 }
5779 }
5780 if (!seen_star) {
5781 ADDOP_I(c, UNPACK_SEQUENCE, n);
5782 }
5783 return 1;
5784}
5785
5786static int
5787pattern_helper_sequence_unpack(struct compiler *c, asdl_pattern_seq *patterns,
Brandt Bucher145bf262021-02-26 14:51:55 -08005788 Py_ssize_t star, pattern_context *pc)
5789{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005790 RETURN_IF_FALSE(pattern_unpack_helper(c, patterns));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005791 Py_ssize_t size = asdl_seq_LEN(patterns);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005792 // We've now got a bunch of new subjects on the stack. They need to remain
5793 // there after each subpattern match:
5794 pc->on_top += size;
Brandt Bucher145bf262021-02-26 14:51:55 -08005795 for (Py_ssize_t i = 0; i < size; i++) {
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005796 // One less item to keep track of each time we loop through:
5797 pc->on_top--;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005798 pattern_ty pattern = asdl_seq_GET(patterns, i);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005799 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08005800 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005801 return 1;
Brandt Bucher145bf262021-02-26 14:51:55 -08005802}
5803
5804// Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of
5805// UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a
5806// starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc.
5807static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005808pattern_helper_sequence_subscr(struct compiler *c, asdl_pattern_seq *patterns,
Brandt Bucher145bf262021-02-26 14:51:55 -08005809 Py_ssize_t star, pattern_context *pc)
5810{
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005811 // We need to keep the subject around for extracting elements:
5812 pc->on_top++;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005813 Py_ssize_t size = asdl_seq_LEN(patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08005814 for (Py_ssize_t i = 0; i < size; i++) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005815 pattern_ty pattern = asdl_seq_GET(patterns, i);
5816 if (WILDCARD_CHECK(pattern)) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005817 continue;
5818 }
5819 if (i == star) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005820 assert(WILDCARD_STAR_CHECK(pattern));
Brandt Bucher145bf262021-02-26 14:51:55 -08005821 continue;
5822 }
5823 ADDOP(c, DUP_TOP);
5824 if (i < star) {
5825 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5826 }
5827 else {
5828 // The subject may not support negative indexing! Compute a
5829 // nonnegative index:
5830 ADDOP(c, GET_LEN);
5831 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i));
5832 ADDOP(c, BINARY_SUBTRACT);
5833 }
5834 ADDOP(c, BINARY_SUBSCR);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005835 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08005836 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005837 // Pop the subject, we're done with it:
5838 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08005839 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08005840 return 1;
5841}
5842
Brandt Bucher145bf262021-02-26 14:51:55 -08005843// Like compiler_pattern, but turn off checks for irrefutability.
5844static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005845compiler_pattern_subpattern(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005846{
5847 int allow_irrefutable = pc->allow_irrefutable;
5848 pc->allow_irrefutable = 1;
5849 RETURN_IF_FALSE(compiler_pattern(c, p, pc));
5850 pc->allow_irrefutable = allow_irrefutable;
5851 return 1;
5852}
5853
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005854static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005855compiler_pattern_as(struct compiler *c, pattern_ty p, pattern_context *pc)
5856{
5857 assert(p->kind == MatchAs_kind);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005858 if (p->v.MatchAs.pattern == NULL) {
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005859 // An irrefutable match:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005860 if (!pc->allow_irrefutable) {
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005861 if (p->v.MatchAs.name) {
5862 const char *e = "name capture %R makes remaining patterns unreachable";
5863 return compiler_error(c, e, p->v.MatchAs.name);
5864 }
5865 const char *e = "wildcard makes remaining patterns unreachable";
5866 return compiler_error(c, e);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005867 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005868 return pattern_helper_store_name(c, p->v.MatchAs.name, pc);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005869 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005870 // Need to make a copy for (possibly) storing later:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005871 pc->on_top++;
Brandt Bucher145bf262021-02-26 14:51:55 -08005872 ADDOP(c, DUP_TOP);
5873 RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc));
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005874 // Success! Store it:
5875 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08005876 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08005877 return 1;
5878}
5879
Brandt Bucher145bf262021-02-26 14:51:55 -08005880static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005881compiler_pattern_star(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005882{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005883 assert(p->kind == MatchStar_kind);
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005884 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchStar.name, pc));
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005885 return 1;
Brandt Bucher145bf262021-02-26 14:51:55 -08005886}
5887
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005888static int
5889validate_kwd_attrs(struct compiler *c, asdl_identifier_seq *attrs, asdl_pattern_seq* patterns)
5890{
5891 // Any errors will point to the pattern rather than the arg name as the
5892 // parser is only supplying identifiers rather than Name or keyword nodes
5893 Py_ssize_t nattrs = asdl_seq_LEN(attrs);
5894 for (Py_ssize_t i = 0; i < nattrs; i++) {
5895 identifier attr = ((identifier)asdl_seq_GET(attrs, i));
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005896 SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, i)));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005897 if (forbidden_name(c, attr, Store)) {
5898 return -1;
5899 }
5900 for (Py_ssize_t j = i + 1; j < nattrs; j++) {
5901 identifier other = ((identifier)asdl_seq_GET(attrs, j));
5902 if (!PyUnicode_Compare(attr, other)) {
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005903 SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, j)));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005904 compiler_error(c, "attribute name repeated in class pattern: %U", attr);
5905 return -1;
5906 }
5907 }
5908 }
5909 return 0;
5910}
Brandt Bucher145bf262021-02-26 14:51:55 -08005911
5912static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005913compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005914{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005915 assert(p->kind == MatchClass_kind);
5916 asdl_pattern_seq *patterns = p->v.MatchClass.patterns;
5917 asdl_identifier_seq *kwd_attrs = p->v.MatchClass.kwd_attrs;
5918 asdl_pattern_seq *kwd_patterns = p->v.MatchClass.kwd_patterns;
5919 Py_ssize_t nargs = asdl_seq_LEN(patterns);
5920 Py_ssize_t nattrs = asdl_seq_LEN(kwd_attrs);
5921 Py_ssize_t nkwd_patterns = asdl_seq_LEN(kwd_patterns);
5922 if (nattrs != nkwd_patterns) {
5923 // AST validator shouldn't let this happen, but if it does,
5924 // just fail, don't crash out of the interpreter
5925 const char * e = "kwd_attrs (%d) / kwd_patterns (%d) length mismatch in class pattern";
5926 return compiler_error(c, e, nattrs, nkwd_patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08005927 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005928 if (INT_MAX < nargs || INT_MAX < nargs + nattrs - 1) {
5929 const char *e = "too many sub-patterns in class pattern %R";
5930 return compiler_error(c, e, p->v.MatchClass.cls);
5931 }
5932 if (nattrs) {
5933 RETURN_IF_FALSE(!validate_kwd_attrs(c, kwd_attrs, kwd_patterns));
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005934 SET_LOC(c, p);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005935 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005936 VISIT(c, expr, p->v.MatchClass.cls);
5937 PyObject *attr_names;
5938 RETURN_IF_FALSE(attr_names = PyTuple_New(nattrs));
Brandt Bucher145bf262021-02-26 14:51:55 -08005939 Py_ssize_t i;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005940 for (i = 0; i < nattrs; i++) {
5941 PyObject *name = asdl_seq_GET(kwd_attrs, i);
Brandt Bucher145bf262021-02-26 14:51:55 -08005942 Py_INCREF(name);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005943 PyTuple_SET_ITEM(attr_names, i, name);
Brandt Bucher145bf262021-02-26 14:51:55 -08005944 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005945 ADDOP_LOAD_CONST_NEW(c, attr_names);
Brandt Bucher145bf262021-02-26 14:51:55 -08005946 ADDOP_I(c, MATCH_CLASS, nargs);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005947 // TOS is now a tuple of (nargs + nattrs) attributes. Preserve it:
5948 pc->on_top++;
5949 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005950 for (i = 0; i < nargs + nattrs; i++) {
5951 pattern_ty pattern;
Brandt Bucher145bf262021-02-26 14:51:55 -08005952 if (i < nargs) {
5953 // Positional:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005954 pattern = asdl_seq_GET(patterns, i);
Brandt Bucher145bf262021-02-26 14:51:55 -08005955 }
5956 else {
5957 // Keyword:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005958 pattern = asdl_seq_GET(kwd_patterns, i - nargs);
Brandt Bucher145bf262021-02-26 14:51:55 -08005959 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005960 if (WILDCARD_CHECK(pattern)) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005961 continue;
5962 }
5963 // Get the i-th attribute, and match it against the i-th pattern:
5964 ADDOP(c, DUP_TOP);
5965 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5966 ADDOP(c, BINARY_SUBSCR);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005967 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08005968 }
5969 // Success! Pop the tuple of attributes:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005970 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08005971 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08005972 return 1;
5973}
5974
Brandt Bucher145bf262021-02-26 14:51:55 -08005975static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005976compiler_pattern_mapping(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005977{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005978 assert(p->kind == MatchMapping_kind);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005979 asdl_expr_seq *keys = p->v.MatchMapping.keys;
5980 asdl_pattern_seq *patterns = p->v.MatchMapping.patterns;
5981 Py_ssize_t size = asdl_seq_LEN(keys);
5982 Py_ssize_t npatterns = asdl_seq_LEN(patterns);
5983 if (size != npatterns) {
5984 // AST validator shouldn't let this happen, but if it does,
5985 // just fail, don't crash out of the interpreter
5986 const char * e = "keys (%d) / patterns (%d) length mismatch in mapping pattern";
5987 return compiler_error(c, e, size, npatterns);
5988 }
5989 // We have a double-star target if "rest" is set
5990 PyObject *star_target = p->v.MatchMapping.rest;
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005991 // We need to keep the subject on top during the mapping and length checks:
5992 pc->on_top++;
Brandt Bucher145bf262021-02-26 14:51:55 -08005993 ADDOP(c, MATCH_MAPPING);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005994 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005995 if (!size && !star_target) {
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005996 // If the pattern is just "{}", we're done! Pop the subject:
5997 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08005998 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08005999 return 1;
6000 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006001 if (size) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006002 // If the pattern has any keys in it, perform a length check:
6003 ADDOP(c, GET_LEN);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006004 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
Brandt Bucher145bf262021-02-26 14:51:55 -08006005 ADDOP_COMPARE(c, GtE);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006006 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006007 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006008 if (INT_MAX < size - 1) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006009 return compiler_error(c, "too many sub-patterns in mapping pattern");
6010 }
6011 // Collect all of the keys into a tuple for MATCH_KEYS and
6012 // COPY_DICT_WITHOUT_KEYS. They can either be dotted names or literals:
Miss Islington (bot)016af142021-07-14 18:00:35 -07006013
6014 // Maintaining a set of Constant_kind kind keys allows us to raise a
6015 // SyntaxError in the case of duplicates.
6016 PyObject *seen = PySet_New(NULL);
6017 if (seen == NULL) {
6018 return 0;
6019 }
6020
6021 // NOTE: goto error on failure in the loop below to avoid leaking `seen`
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006022 for (Py_ssize_t i = 0; i < size; i++) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006023 expr_ty key = asdl_seq_GET(keys, i);
6024 if (key == NULL) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006025 const char *e = "can't use NULL keys in MatchMapping "
6026 "(set 'rest' parameter instead)";
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07006027 SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, i)));
Miss Islington (bot)016af142021-07-14 18:00:35 -07006028 compiler_error(c, e);
6029 goto error;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006030 }
Miss Islington (bot)016af142021-07-14 18:00:35 -07006031
6032 if (key->kind == Constant_kind) {
6033 int in_seen = PySet_Contains(seen, key->v.Constant.value);
6034 if (in_seen < 0) {
6035 goto error;
6036 }
6037 if (in_seen) {
6038 const char *e = "mapping pattern checks duplicate key (%R)";
6039 compiler_error(c, e, key->v.Constant.value);
6040 goto error;
6041 }
6042 if (PySet_Add(seen, key->v.Constant.value)) {
6043 goto error;
6044 }
6045 }
6046
6047 else if (key->kind != Attribute_kind) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006048 const char *e = "mapping pattern keys may only match literals and attribute lookups";
Miss Islington (bot)016af142021-07-14 18:00:35 -07006049 compiler_error(c, e);
6050 goto error;
Brandt Bucher145bf262021-02-26 14:51:55 -08006051 }
Miss Islington (bot)016af142021-07-14 18:00:35 -07006052 if (!compiler_visit_expr(c, key)) {
6053 goto error;
6054 }
Brandt Bucher145bf262021-02-26 14:51:55 -08006055 }
Miss Islington (bot)016af142021-07-14 18:00:35 -07006056
6057 // all keys have been checked; there are no duplicates
6058 Py_DECREF(seen);
6059
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006060 ADDOP_I(c, BUILD_TUPLE, size);
Brandt Bucher145bf262021-02-26 14:51:55 -08006061 ADDOP(c, MATCH_KEYS);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006062 // There's now a tuple of keys and a tuple of values on top of the subject:
6063 pc->on_top += 2;
6064 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
6065 // So far so good. Use that tuple of values on the stack to match
Brandt Bucher145bf262021-02-26 14:51:55 -08006066 // sub-patterns against:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006067 for (Py_ssize_t i = 0; i < size; i++) {
6068 pattern_ty pattern = asdl_seq_GET(patterns, i);
6069 if (WILDCARD_CHECK(pattern)) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006070 continue;
6071 }
6072 ADDOP(c, DUP_TOP);
6073 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
6074 ADDOP(c, BINARY_SUBSCR);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006075 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006076 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006077 // If we get this far, it's a match! We're done with the tuple of values,
6078 // and whatever happens next should consume the tuple of keys underneath it:
6079 pc->on_top -= 2;
Brandt Bucher145bf262021-02-26 14:51:55 -08006080 ADDOP(c, POP_TOP);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006081 if (star_target) {
6082 // If we have a starred name, bind a dict of remaining items to it:
Brandt Bucher145bf262021-02-26 14:51:55 -08006083 ADDOP(c, COPY_DICT_WITHOUT_KEYS);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006084 RETURN_IF_FALSE(pattern_helper_store_name(c, star_target, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006085 }
6086 else {
6087 // Otherwise, we don't care about this tuple of keys anymore:
6088 ADDOP(c, POP_TOP);
6089 }
6090 // Pop the subject:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006091 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08006092 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08006093 return 1;
Miss Islington (bot)016af142021-07-14 18:00:35 -07006094
6095error:
6096 Py_DECREF(seen);
6097 return 0;
Brandt Bucher145bf262021-02-26 14:51:55 -08006098}
6099
Brandt Bucher145bf262021-02-26 14:51:55 -08006100static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006101compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006102{
6103 assert(p->kind == MatchOr_kind);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006104 basicblock *end;
Brandt Bucher145bf262021-02-26 14:51:55 -08006105 RETURN_IF_FALSE(end = compiler_new_block(c));
Brandt Bucher145bf262021-02-26 14:51:55 -08006106 Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns);
6107 assert(size > 1);
6108 // We're going to be messing with pc. Keep the original info handy:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006109 pattern_context old_pc = *pc;
6110 Py_INCREF(pc->stores);
6111 // control is the list of names bound by the first alternative. It is used
6112 // for checking different name bindings in alternatives, and for correcting
6113 // the order in which extracted elements are placed on the stack.
6114 PyObject *control = NULL;
6115 // NOTE: We can't use returning macros anymore! goto error on error.
Brandt Bucher145bf262021-02-26 14:51:55 -08006116 for (Py_ssize_t i = 0; i < size; i++) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006117 pattern_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i);
Brandt Bucher145bf262021-02-26 14:51:55 -08006118 SET_LOC(c, alt);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006119 PyObject *pc_stores = PyList_New(0);
6120 if (pc_stores == NULL) {
6121 goto error;
Brandt Bucher145bf262021-02-26 14:51:55 -08006122 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006123 Py_SETREF(pc->stores, pc_stores);
6124 // An irrefutable sub-pattern must be last, if it is allowed at all:
6125 pc->allow_irrefutable = (i == size - 1) && old_pc.allow_irrefutable;
6126 pc->fail_pop = NULL;
6127 pc->fail_pop_size = 0;
6128 pc->on_top = 0;
6129 if (!compiler_addop(c, DUP_TOP) || !compiler_pattern(c, alt, pc)) {
6130 goto error;
6131 }
6132 // Success!
6133 Py_ssize_t nstores = PyList_GET_SIZE(pc->stores);
Brandt Bucher145bf262021-02-26 14:51:55 -08006134 if (!i) {
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006135 // This is the first alternative, so save its stores as a "control"
6136 // for the others (they can't bind a different set of names, and
6137 // might need to be reordered):
6138 assert(control == NULL);
Brandt Bucher145bf262021-02-26 14:51:55 -08006139 control = pc->stores;
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006140 Py_INCREF(control);
Brandt Bucher145bf262021-02-26 14:51:55 -08006141 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006142 else if (nstores != PyList_GET_SIZE(control)) {
6143 goto diff;
Brandt Bucher145bf262021-02-26 14:51:55 -08006144 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006145 else if (nstores) {
6146 // There were captures. Check to see if we differ from control:
6147 Py_ssize_t icontrol = nstores;
6148 while (icontrol--) {
6149 PyObject *name = PyList_GET_ITEM(control, icontrol);
6150 Py_ssize_t istores = PySequence_Index(pc->stores, name);
6151 if (istores < 0) {
6152 PyErr_Clear();
6153 goto diff;
6154 }
6155 if (icontrol != istores) {
6156 // Reorder the names on the stack to match the order of the
6157 // names in control. There's probably a better way of doing
6158 // this; the current solution is potentially very
6159 // inefficient when each alternative subpattern binds lots
6160 // of names in different orders. It's fine for reasonable
6161 // cases, though.
6162 assert(istores < icontrol);
6163 Py_ssize_t rotations = istores + 1;
Christian Claussccd82a02021-10-07 17:30:08 +02006164 // Perform the same rotation on pc->stores:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006165 PyObject *rotated = PyList_GetSlice(pc->stores, 0,
6166 rotations);
6167 if (rotated == NULL ||
6168 PyList_SetSlice(pc->stores, 0, rotations, NULL) ||
6169 PyList_SetSlice(pc->stores, icontrol - istores,
6170 icontrol - istores, rotated))
6171 {
6172 Py_XDECREF(rotated);
6173 goto error;
6174 }
6175 Py_DECREF(rotated);
6176 // That just did:
6177 // rotated = pc_stores[:rotations]
6178 // del pc_stores[:rotations]
6179 // pc_stores[icontrol-istores:icontrol-istores] = rotated
6180 // Do the same thing to the stack, using several ROT_Ns:
6181 while (rotations--) {
6182 if (!compiler_addop_i(c, ROT_N, icontrol + 1)) {
6183 goto error;
6184 }
6185 }
6186 }
6187 }
6188 }
6189 assert(control);
6190 if (!compiler_addop_j(c, JUMP_FORWARD, end) ||
6191 !compiler_next_block(c) ||
6192 !emit_and_reset_fail_pop(c, pc))
6193 {
6194 goto error;
6195 }
Brandt Bucher145bf262021-02-26 14:51:55 -08006196 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006197 Py_DECREF(pc->stores);
6198 *pc = old_pc;
6199 Py_INCREF(pc->stores);
6200 // Need to NULL this for the PyObject_Free call in the error block.
6201 old_pc.fail_pop = NULL;
6202 // No match. Pop the remaining copy of the subject and fail:
6203 if (!compiler_addop(c, POP_TOP) || !jump_to_fail_pop(c, pc, JUMP_FORWARD)) {
6204 goto error;
6205 }
Brandt Bucher145bf262021-02-26 14:51:55 -08006206 compiler_use_next_block(c, end);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006207 Py_ssize_t nstores = PyList_GET_SIZE(control);
6208 // There's a bunch of stuff on the stack between any where the new stores
6209 // are and where they need to be:
6210 // - The other stores.
6211 // - A copy of the subject.
6212 // - Anything else that may be on top of the stack.
6213 // - Any previous stores we've already stashed away on the stack.
Pablo Galindo39494282021-05-03 16:20:46 +01006214 Py_ssize_t nrots = nstores + 1 + pc->on_top + PyList_GET_SIZE(pc->stores);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006215 for (Py_ssize_t i = 0; i < nstores; i++) {
6216 // Rotate this capture to its proper place on the stack:
6217 if (!compiler_addop_i(c, ROT_N, nrots)) {
6218 goto error;
6219 }
6220 // Update the list of previous stores with this new name, checking for
6221 // duplicates:
6222 PyObject *name = PyList_GET_ITEM(control, i);
6223 int dupe = PySequence_Contains(pc->stores, name);
6224 if (dupe < 0) {
6225 goto error;
6226 }
6227 if (dupe) {
6228 compiler_error_duplicate_store(c, name);
6229 goto error;
6230 }
6231 if (PyList_Append(pc->stores, name)) {
6232 goto error;
6233 }
6234 }
6235 Py_DECREF(old_pc.stores);
6236 Py_DECREF(control);
6237 // NOTE: Returning macros are safe again.
6238 // Pop the copy of the subject:
6239 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08006240 return 1;
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006241diff:
6242 compiler_error(c, "alternative patterns bind different names");
6243error:
6244 PyObject_Free(old_pc.fail_pop);
6245 Py_DECREF(old_pc.stores);
Brandt Bucher145bf262021-02-26 14:51:55 -08006246 Py_XDECREF(control);
6247 return 0;
6248}
6249
6250
6251static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006252compiler_pattern_sequence(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006253{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006254 assert(p->kind == MatchSequence_kind);
6255 asdl_pattern_seq *patterns = p->v.MatchSequence.patterns;
6256 Py_ssize_t size = asdl_seq_LEN(patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08006257 Py_ssize_t star = -1;
6258 int only_wildcard = 1;
6259 int star_wildcard = 0;
6260 // Find a starred name, if it exists. There may be at most one:
6261 for (Py_ssize_t i = 0; i < size; i++) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006262 pattern_ty pattern = asdl_seq_GET(patterns, i);
6263 if (pattern->kind == MatchStar_kind) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006264 if (star >= 0) {
6265 const char *e = "multiple starred names in sequence pattern";
6266 return compiler_error(c, e);
6267 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006268 star_wildcard = WILDCARD_STAR_CHECK(pattern);
6269 only_wildcard &= star_wildcard;
Brandt Bucher145bf262021-02-26 14:51:55 -08006270 star = i;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006271 continue;
Brandt Bucher145bf262021-02-26 14:51:55 -08006272 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006273 only_wildcard &= WILDCARD_CHECK(pattern);
Brandt Bucher145bf262021-02-26 14:51:55 -08006274 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006275 // We need to keep the subject on top during the sequence and length checks:
6276 pc->on_top++;
Brandt Bucher145bf262021-02-26 14:51:55 -08006277 ADDOP(c, MATCH_SEQUENCE);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006278 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006279 if (star < 0) {
6280 // No star: len(subject) == size
6281 ADDOP(c, GET_LEN);
6282 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
6283 ADDOP_COMPARE(c, Eq);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006284 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006285 }
6286 else if (size > 1) {
6287 // Star: len(subject) >= size - 1
6288 ADDOP(c, GET_LEN);
6289 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1));
6290 ADDOP_COMPARE(c, GtE);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006291 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006292 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006293 // Whatever comes next should consume the subject:
6294 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08006295 if (only_wildcard) {
6296 // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc.
6297 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08006298 }
6299 else if (star_wildcard) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006300 RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, patterns, star, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006301 }
6302 else {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006303 RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, patterns, star, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006304 }
Brandt Bucher145bf262021-02-26 14:51:55 -08006305 return 1;
6306}
6307
Brandt Bucher145bf262021-02-26 14:51:55 -08006308static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006309compiler_pattern_value(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006310{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006311 assert(p->kind == MatchValue_kind);
6312 expr_ty value = p->v.MatchValue.value;
6313 if (!MATCH_VALUE_EXPR(value)) {
6314 const char *e = "patterns may only match literals and attribute lookups";
6315 return compiler_error(c, e);
6316 }
6317 VISIT(c, expr, value);
Brandt Bucher145bf262021-02-26 14:51:55 -08006318 ADDOP_COMPARE(c, Eq);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006319 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006320 return 1;
6321}
6322
Brandt Bucher145bf262021-02-26 14:51:55 -08006323static int
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07006324compiler_pattern_singleton(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006325{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006326 assert(p->kind == MatchSingleton_kind);
6327 ADDOP_LOAD_CONST(c, p->v.MatchSingleton.value);
6328 ADDOP_COMPARE(c, Is);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006329 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006330 return 1;
6331}
6332
Brandt Bucher145bf262021-02-26 14:51:55 -08006333static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006334compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006335{
6336 SET_LOC(c, p);
6337 switch (p->kind) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006338 case MatchValue_kind:
Brandt Bucher145bf262021-02-26 14:51:55 -08006339 return compiler_pattern_value(c, p, pc);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006340 case MatchSingleton_kind:
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07006341 return compiler_pattern_singleton(c, p, pc);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006342 case MatchSequence_kind:
Brandt Bucher145bf262021-02-26 14:51:55 -08006343 return compiler_pattern_sequence(c, p, pc);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006344 case MatchMapping_kind:
6345 return compiler_pattern_mapping(c, p, pc);
6346 case MatchClass_kind:
6347 return compiler_pattern_class(c, p, pc);
6348 case MatchStar_kind:
6349 return compiler_pattern_star(c, p, pc);
Brandt Bucher145bf262021-02-26 14:51:55 -08006350 case MatchAs_kind:
6351 return compiler_pattern_as(c, p, pc);
6352 case MatchOr_kind:
6353 return compiler_pattern_or(c, p, pc);
Brandt Bucher145bf262021-02-26 14:51:55 -08006354 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006355 // AST validator shouldn't let this happen, but if it does,
6356 // just fail, don't crash out of the interpreter
6357 const char *e = "invalid match pattern node in AST (kind=%d)";
6358 return compiler_error(c, e, p->kind);
Brandt Bucher145bf262021-02-26 14:51:55 -08006359}
6360
Brandt Bucher145bf262021-02-26 14:51:55 -08006361static int
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006362compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006363{
6364 VISIT(c, expr, s->v.Match.subject);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006365 basicblock *end;
Brandt Bucher145bf262021-02-26 14:51:55 -08006366 RETURN_IF_FALSE(end = compiler_new_block(c));
6367 Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006368 assert(cases > 0);
Brandt Bucher145bf262021-02-26 14:51:55 -08006369 match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6370 int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases;
6371 for (Py_ssize_t i = 0; i < cases - has_default; i++) {
6372 m = asdl_seq_GET(s->v.Match.cases, i);
6373 SET_LOC(c, m->pattern);
Brandt Bucher145bf262021-02-26 14:51:55 -08006374 // Only copy the subject if we're *not* on the last case:
6375 if (i != cases - has_default - 1) {
6376 ADDOP(c, DUP_TOP);
6377 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006378 RETURN_IF_FALSE(pc->stores = PyList_New(0));
6379 // Irrefutable cases must be either guarded, last, or both:
6380 pc->allow_irrefutable = m->guard != NULL || i == cases - 1;
6381 pc->fail_pop = NULL;
6382 pc->fail_pop_size = 0;
6383 pc->on_top = 0;
6384 // NOTE: Can't use returning macros here (they'll leak pc->stores)!
6385 if (!compiler_pattern(c, m->pattern, pc)) {
6386 Py_DECREF(pc->stores);
6387 return 0;
6388 }
6389 assert(!pc->on_top);
6390 // It's a match! Store all of the captured names (they're on the stack).
6391 Py_ssize_t nstores = PyList_GET_SIZE(pc->stores);
6392 for (Py_ssize_t n = 0; n < nstores; n++) {
6393 PyObject *name = PyList_GET_ITEM(pc->stores, n);
6394 if (!compiler_nameop(c, name, Store)) {
6395 Py_DECREF(pc->stores);
6396 return 0;
6397 }
6398 }
6399 Py_DECREF(pc->stores);
6400 // NOTE: Returning macros are safe again.
Brandt Bucher145bf262021-02-26 14:51:55 -08006401 if (m->guard) {
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006402 RETURN_IF_FALSE(ensure_fail_pop(c, pc, 0));
6403 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, pc->fail_pop[0], 0));
Brandt Bucher145bf262021-02-26 14:51:55 -08006404 }
6405 // Success! Pop the subject off, we're done with it:
6406 if (i != cases - has_default - 1) {
6407 ADDOP(c, POP_TOP);
6408 }
6409 VISIT_SEQ(c, stmt, m->body);
6410 ADDOP_JUMP(c, JUMP_FORWARD, end);
Miss Islington (bot)01601aa2021-07-25 17:04:06 -07006411 // If the pattern fails to match, we want the line number of the
6412 // cleanup to be associated with the failed pattern, not the last line
6413 // of the body
6414 SET_LOC(c, m->pattern);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006415 RETURN_IF_FALSE(emit_and_reset_fail_pop(c, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006416 }
6417 if (has_default) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006418 // A trailing "case _" is common, and lets us save a bit of redundant
6419 // pushing and popping in the loop above:
6420 m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6421 SET_LOC(c, m->pattern);
Miss Islington (bot)01601aa2021-07-25 17:04:06 -07006422 if (cases == 1) {
6423 // No matches. Done with the subject:
6424 ADDOP(c, POP_TOP);
6425 }
6426 else {
6427 // Show line coverage for default case (it doesn't create bytecode)
6428 ADDOP(c, NOP);
6429 }
Brandt Bucher145bf262021-02-26 14:51:55 -08006430 if (m->guard) {
6431 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0));
6432 }
6433 VISIT_SEQ(c, stmt, m->body);
6434 }
6435 compiler_use_next_block(c, end);
6436 return 1;
6437}
6438
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006439static int
6440compiler_match(struct compiler *c, stmt_ty s)
6441{
6442 pattern_context pc;
6443 pc.fail_pop = NULL;
6444 int result = compiler_match_inner(c, s, &pc);
6445 PyObject_Free(pc.fail_pop);
6446 return result;
6447}
6448
Brandt Bucher145bf262021-02-26 14:51:55 -08006449#undef WILDCARD_CHECK
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006450#undef WILDCARD_STAR_CHECK
Brandt Bucher145bf262021-02-26 14:51:55 -08006451
Thomas Wouters89f507f2006-12-13 04:49:30 +00006452/* End of the compiler section, beginning of the assembler section */
6453
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006454/* do depth-first search of basic block graph, starting with block.
T. Wouters99b54d62019-09-12 07:05:33 -07006455 post records the block indices in post-order.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006456
6457 XXX must handle implicit jumps from one block to next
6458*/
6459
Thomas Wouters89f507f2006-12-13 04:49:30 +00006460struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006461 PyObject *a_bytecode; /* string containing bytecode */
6462 int a_offset; /* offset into bytecode */
6463 int a_nblocks; /* number of reachable blocks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006464 PyObject *a_lnotab; /* string containing lnotab */
6465 int a_lnotab_off; /* offset into lnotab */
Mark Shannon877df852020-11-12 09:43:29 +00006466 int a_prevlineno; /* lineno of last emitted line in line table */
6467 int a_lineno; /* lineno of last emitted instruction */
6468 int a_lineno_start; /* bytecode start offset of current lineno */
Mark Shannoncc75ab72020-11-12 19:49:33 +00006469 basicblock *a_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006470};
6471
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006472Py_LOCAL_INLINE(void)
6473stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006474{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02006475 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
Mark Shannonfee55262019-11-21 09:11:43 +00006476 if (b->b_startdepth < depth && b->b_startdepth < 100) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006477 assert(b->b_startdepth < 0);
6478 b->b_startdepth = depth;
6479 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02006480 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006481}
6482
6483/* Find the flow path that needs the largest stack. We assume that
6484 * cycles in the flow graph have no net effect on the stack depth.
6485 */
6486static int
6487stackdepth(struct compiler *c)
6488{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006489 basicblock *b, *entryblock = NULL;
6490 basicblock **stack, **sp;
6491 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006492 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006493 b->b_startdepth = INT_MIN;
6494 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006495 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006496 }
Mark Shannon67969f52021-04-07 10:52:07 +01006497 assert(entryblock!= NULL);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006498 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
6499 if (!stack) {
6500 PyErr_NoMemory();
6501 return -1;
6502 }
6503
6504 sp = stack;
Mark Shannonb37181e2021-04-06 11:48:59 +01006505 if (c->u->u_ste->ste_generator || c->u->u_ste->ste_coroutine) {
6506 stackdepth_push(&sp, entryblock, 1);
6507 } else {
6508 stackdepth_push(&sp, entryblock, 0);
6509 }
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006510 while (sp != stack) {
6511 b = *--sp;
6512 int depth = b->b_startdepth;
6513 assert(depth >= 0);
6514 basicblock *next = b->b_next;
6515 for (int i = 0; i < b->b_iused; i++) {
6516 struct instr *instr = &b->b_instr[i];
6517 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
6518 if (effect == PY_INVALID_STACK_EFFECT) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01006519 PyErr_Format(PyExc_SystemError,
6520 "compiler stack_effect(opcode=%d, arg=%i) failed",
6521 instr->i_opcode, instr->i_oparg);
6522 return -1;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006523 }
6524 int new_depth = depth + effect;
6525 if (new_depth > maxdepth) {
6526 maxdepth = new_depth;
6527 }
6528 assert(depth >= 0); /* invalid code or bug in stackdepth() */
Mark Shannon582aaf12020-08-04 17:30:11 +01006529 if (is_jump(instr)) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006530 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
6531 assert(effect != PY_INVALID_STACK_EFFECT);
6532 int target_depth = depth + effect;
6533 if (target_depth > maxdepth) {
6534 maxdepth = target_depth;
6535 }
6536 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006537 stackdepth_push(&sp, instr->i_target, target_depth);
6538 }
6539 depth = new_depth;
6540 if (instr->i_opcode == JUMP_ABSOLUTE ||
6541 instr->i_opcode == JUMP_FORWARD ||
6542 instr->i_opcode == RETURN_VALUE ||
Mark Shannonfee55262019-11-21 09:11:43 +00006543 instr->i_opcode == RAISE_VARARGS ||
6544 instr->i_opcode == RERAISE)
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006545 {
6546 /* remaining code is dead */
6547 next = NULL;
6548 break;
6549 }
6550 }
6551 if (next != NULL) {
Mark Shannon266b4622020-11-17 19:30:14 +00006552 assert(b->b_nofallthrough == 0);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006553 stackdepth_push(&sp, next, depth);
6554 }
6555 }
6556 PyObject_Free(stack);
6557 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006558}
6559
6560static int
6561assemble_init(struct assembler *a, int nblocks, int firstlineno)
6562{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006563 memset(a, 0, sizeof(struct assembler));
Mark Shannon877df852020-11-12 09:43:29 +00006564 a->a_prevlineno = a->a_lineno = firstlineno;
Mark Shannonfd009e62020-11-13 12:53:53 +00006565 a->a_lnotab = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006566 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006567 if (a->a_bytecode == NULL) {
6568 goto error;
6569 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006570 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006571 if (a->a_lnotab == NULL) {
6572 goto error;
6573 }
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07006574 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006575 PyErr_NoMemory();
Mark Shannonfd009e62020-11-13 12:53:53 +00006576 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006577 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006578 return 1;
Mark Shannonfd009e62020-11-13 12:53:53 +00006579error:
6580 Py_XDECREF(a->a_bytecode);
6581 Py_XDECREF(a->a_lnotab);
6582 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006583}
6584
6585static void
6586assemble_free(struct assembler *a)
6587{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006588 Py_XDECREF(a->a_bytecode);
6589 Py_XDECREF(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006590}
6591
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006592static int
6593blocksize(basicblock *b)
6594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006595 int i;
6596 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006598 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006599 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006600 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006601}
6602
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006603static int
Mark Shannon877df852020-11-12 09:43:29 +00006604assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006605{
Mark Shannon877df852020-11-12 09:43:29 +00006606 Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006607 if (a->a_lnotab_off + 2 >= len) {
6608 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
6609 return 0;
6610 }
Pablo Galindo86e322f2021-01-30 13:54:22 +00006611 unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab);
6612 lnotab += a->a_lnotab_off;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006613 a->a_lnotab_off += 2;
Mark Shannon877df852020-11-12 09:43:29 +00006614 *lnotab++ = bdelta;
6615 *lnotab++ = ldelta;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006616 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006617}
6618
Mark Shannon877df852020-11-12 09:43:29 +00006619/* Appends a range to the end of the line number table. See
6620 * Objects/lnotab_notes.txt for the description of the line number table. */
6621
6622static int
6623assemble_line_range(struct assembler *a)
6624{
6625 int ldelta, bdelta;
Serhiy Storchaka1670d592021-10-04 17:07:21 +03006626 bdelta = (a->a_offset - a->a_lineno_start) * sizeof(_Py_CODEUNIT);
Mark Shannon877df852020-11-12 09:43:29 +00006627 if (bdelta == 0) {
6628 return 1;
6629 }
6630 if (a->a_lineno < 0) {
6631 ldelta = -128;
6632 }
6633 else {
6634 ldelta = a->a_lineno - a->a_prevlineno;
6635 a->a_prevlineno = a->a_lineno;
6636 while (ldelta > 127) {
6637 if (!assemble_emit_linetable_pair(a, 0, 127)) {
6638 return 0;
6639 }
6640 ldelta -= 127;
6641 }
6642 while (ldelta < -127) {
6643 if (!assemble_emit_linetable_pair(a, 0, -127)) {
6644 return 0;
6645 }
6646 ldelta += 127;
6647 }
6648 }
6649 assert(-128 <= ldelta && ldelta < 128);
6650 while (bdelta > 254) {
6651 if (!assemble_emit_linetable_pair(a, 254, ldelta)) {
6652 return 0;
6653 }
6654 ldelta = a->a_lineno < 0 ? -128 : 0;
6655 bdelta -= 254;
6656 }
6657 if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) {
6658 return 0;
6659 }
6660 a->a_lineno_start = a->a_offset;
6661 return 1;
6662}
6663
6664static int
6665assemble_lnotab(struct assembler *a, struct instr *i)
6666{
6667 if (i->i_lineno == a->a_lineno) {
6668 return 1;
6669 }
6670 if (!assemble_line_range(a)) {
6671 return 0;
6672 }
6673 a->a_lineno = i->i_lineno;
6674 return 1;
6675}
6676
6677
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006678/* assemble_emit()
6679 Extend the bytecode with a new instruction.
6680 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00006681*/
6682
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006683static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006684assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006685{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006686 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006687 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03006688 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006689
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006690 arg = i->i_oparg;
6691 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006692 if (i->i_lineno && !assemble_lnotab(a, i))
6693 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006694 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006695 if (len > PY_SSIZE_T_MAX / 2)
6696 return 0;
6697 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
6698 return 0;
6699 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03006700 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006701 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006702 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006703 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006704}
6705
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00006706static void
Mark Shannond4e4ef12022-02-16 11:26:02 +00006707normalize_jumps(struct assembler *a)
6708{
6709 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
6710 b->b_visited = 0;
6711 }
6712 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
6713 b->b_visited = 1;
6714 if (b->b_iused == 0) {
6715 continue;
6716 }
6717 struct instr *last = &b->b_instr[b->b_iused-1];
6718 if (last->i_opcode == JUMP_ABSOLUTE) {
6719 if (last->i_target->b_visited == 0) {
6720 last->i_opcode = JUMP_FORWARD;
6721 }
6722 }
6723 if (last->i_opcode == JUMP_FORWARD) {
6724 if (last->i_target->b_visited == 1) {
6725 last->i_opcode = JUMP_ABSOLUTE;
6726 }
6727 }
6728 }
6729}
6730
6731static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006732assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006733{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006734 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006735 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006736 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00006737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006738 /* Compute the size of each block and fixup jump args.
6739 Replace block pointer with position in bytecode. */
6740 do {
6741 totsize = 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006742 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006743 bsize = blocksize(b);
6744 b->b_offset = totsize;
6745 totsize += bsize;
6746 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006747 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006748 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6749 bsize = b->b_offset;
6750 for (i = 0; i < b->b_iused; i++) {
6751 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006752 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006753 /* Relative jumps are computed relative to
6754 the instruction pointer after fetching
6755 the jump instruction.
6756 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006757 bsize += isize;
Mark Shannon582aaf12020-08-04 17:30:11 +01006758 if (is_jump(instr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006759 instr->i_oparg = instr->i_target->b_offset;
Mark Shannon582aaf12020-08-04 17:30:11 +01006760 if (is_relative_jump(instr)) {
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006761 instr->i_oparg -= bsize;
6762 }
6763 if (instrsize(instr->i_oparg) != isize) {
6764 extended_arg_recompile = 1;
6765 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006766 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006767 }
6768 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00006769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006770 /* XXX: This is an awful hack that could hurt performance, but
6771 on the bright side it should work until we come up
6772 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006774 The issue is that in the first loop blocksize() is called
6775 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006776 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006777 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006779 So we loop until we stop seeing new EXTENDED_ARGs.
6780 The only EXTENDED_ARGs that could be popping up are
6781 ones in jump instructions. So this should converge
6782 fairly quickly.
6783 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006784 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00006785}
6786
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006787static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01006788dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006789{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006790 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006791 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006792
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006793 tuple = PyTuple_New(size);
6794 if (tuple == NULL)
6795 return NULL;
6796 while (PyDict_Next(dict, &pos, &k, &v)) {
6797 i = PyLong_AS_LONG(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006798 Py_INCREF(k);
6799 assert((i - offset) < size);
6800 assert((i - offset) >= 0);
6801 PyTuple_SET_ITEM(tuple, i - offset, k);
6802 }
6803 return tuple;
6804}
6805
6806static PyObject *
6807consts_dict_keys_inorder(PyObject *dict)
6808{
6809 PyObject *consts, *k, *v;
6810 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
6811
6812 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
6813 if (consts == NULL)
6814 return NULL;
6815 while (PyDict_Next(dict, &pos, &k, &v)) {
6816 i = PyLong_AS_LONG(v);
Christian Claussccd82a02021-10-07 17:30:08 +02006817 /* The keys of the dictionary can be tuples wrapping a constant.
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +03006818 * (see compiler_add_o and _PyCode_ConstantKey). In that case
6819 * the object we want is always second. */
6820 if (PyTuple_CheckExact(k)) {
6821 k = PyTuple_GET_ITEM(k, 1);
6822 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006823 Py_INCREF(k);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006824 assert(i < size);
6825 assert(i >= 0);
6826 PyList_SET_ITEM(consts, i, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006827 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006828 return consts;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006829}
6830
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006831static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006832compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006833{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006834 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006835 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006836 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04006837 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006838 if (ste->ste_nested)
6839 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07006840 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006841 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07006842 if (!ste->ste_generator && ste->ste_coroutine)
6843 flags |= CO_COROUTINE;
6844 if (ste->ste_generator && ste->ste_coroutine)
6845 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006846 if (ste->ste_varargs)
6847 flags |= CO_VARARGS;
6848 if (ste->ste_varkeywords)
6849 flags |= CO_VARKEYWORDS;
6850 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006852 /* (Only) inherit compilerflags in PyCF_MASK */
6853 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006854
Pablo Galindo90235812020-03-15 04:29:22 +00006855 if ((IS_TOP_LEVEL_AWAIT(c)) &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07006856 ste->ste_coroutine &&
6857 !ste->ste_generator) {
6858 flags |= CO_COROUTINE;
6859 }
6860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006861 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00006862}
6863
Inada Naokibdb941b2021-02-10 09:20:42 +09006864// Merge *obj* with constant cache.
INADA Naokic2e16072018-11-26 21:23:22 +09006865// Unlike merge_consts_recursive(), this function doesn't work recursively.
6866static int
Inada Naokibdb941b2021-02-10 09:20:42 +09006867merge_const_one(struct compiler *c, PyObject **obj)
INADA Naokic2e16072018-11-26 21:23:22 +09006868{
Inada Naokibdb941b2021-02-10 09:20:42 +09006869 PyObject *key = _PyCode_ConstantKey(*obj);
INADA Naokic2e16072018-11-26 21:23:22 +09006870 if (key == NULL) {
6871 return 0;
6872 }
6873
6874 // t is borrowed reference
6875 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
6876 Py_DECREF(key);
6877 if (t == NULL) {
6878 return 0;
6879 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006880 if (t == key) { // obj is new constant.
INADA Naokic2e16072018-11-26 21:23:22 +09006881 return 1;
6882 }
6883
Inada Naokibdb941b2021-02-10 09:20:42 +09006884 if (PyTuple_CheckExact(t)) {
6885 // t is still borrowed reference
6886 t = PyTuple_GET_ITEM(t, 1);
6887 }
6888
6889 Py_INCREF(t);
6890 Py_DECREF(*obj);
6891 *obj = t;
INADA Naokic2e16072018-11-26 21:23:22 +09006892 return 1;
6893}
6894
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006895static PyCodeObject *
Mark Shannon6e8128f2020-07-30 10:03:00 +01006896makecode(struct compiler *c, struct assembler *a, PyObject *consts)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006897{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006898 PyCodeObject *co = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006899 PyObject *names = NULL;
6900 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006901 PyObject *name = NULL;
6902 PyObject *freevars = NULL;
6903 PyObject *cellvars = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006904 Py_ssize_t nlocals;
6905 int nlocals_int;
6906 int flags;
Pablo Galindocd74e662019-06-01 18:08:04 +01006907 int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006909 names = dict_keys_inorder(c->u->u_names, 0);
6910 varnames = dict_keys_inorder(c->u->u_varnames, 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006911 if (!names || !varnames) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006912 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006913 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006914 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
6915 if (!cellvars)
6916 goto error;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006917 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006918 if (!freevars)
6919 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006920
Inada Naokibdb941b2021-02-10 09:20:42 +09006921 if (!merge_const_one(c, &names) ||
6922 !merge_const_one(c, &varnames) ||
6923 !merge_const_one(c, &cellvars) ||
6924 !merge_const_one(c, &freevars))
INADA Naokic2e16072018-11-26 21:23:22 +09006925 {
6926 goto error;
6927 }
6928
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006929 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01006930 assert(nlocals < INT_MAX);
6931 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
6932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006933 flags = compute_code_flags(c);
6934 if (flags < 0)
6935 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006936
Mark Shannon6e8128f2020-07-30 10:03:00 +01006937 consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
6938 if (consts == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006939 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006940 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006941 if (!merge_const_one(c, &consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006942 Py_DECREF(consts);
INADA Naokic2e16072018-11-26 21:23:22 +09006943 goto error;
6944 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006945
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01006946 posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int);
Pablo Galindocd74e662019-06-01 18:08:04 +01006947 posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +01006948 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006949 maxdepth = stackdepth(c);
6950 if (maxdepth < 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006951 Py_DECREF(consts);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006952 goto error;
6953 }
Mark Shannon11e0b292021-04-15 14:28:56 +01006954 if (maxdepth > MAX_ALLOWED_STACK_USE) {
6955 PyErr_Format(PyExc_SystemError,
6956 "excessive stack use: stack is %d deep",
6957 maxdepth);
6958 Py_DECREF(consts);
6959 goto error;
6960 }
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006961 co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
Mark Shannon13bc1392020-01-23 09:25:17 +00006962 posonlyargcount, kwonlyargcount, nlocals_int,
Mark Shannon6e8128f2020-07-30 10:03:00 +01006963 maxdepth, flags, a->a_bytecode, consts, names,
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006964 varnames, freevars, cellvars, c->c_filename,
6965 c->u->u_name, c->u->u_firstlineno, a->a_lnotab);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006966 Py_DECREF(consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006967 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006968 Py_XDECREF(names);
6969 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006970 Py_XDECREF(name);
6971 Py_XDECREF(freevars);
6972 Py_XDECREF(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006973 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006974}
6975
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006976
6977/* For debugging purposes only */
6978#if 0
6979static void
Mark Shannon37686f72021-07-16 11:49:10 +01006980dump_instr(struct instr *i)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006981{
Mark Shannon37686f72021-07-16 11:49:10 +01006982 const char *jrel = (is_relative_jump(i)) ? "jrel " : "";
6983 const char *jabs = (is_jump(i) && !is_relative_jump(i))? "jabs " : "";
6984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006985 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006987 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006988 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006989 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006990 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006991 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
6992 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006993}
6994
6995static void
6996dump_basicblock(const basicblock *b)
6997{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006998 const char *b_return = b->b_return ? "return " : "";
Pablo Galindo60eb9f12020-06-28 01:55:47 +01006999 fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n",
7000 b->b_iused, b->b_startdepth, b->b_offset, b_return);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007001 if (b->b_instr) {
7002 int i;
7003 for (i = 0; i < b->b_iused; i++) {
7004 fprintf(stderr, " [%02d] ", i);
7005 dump_instr(b->b_instr + i);
7006 }
7007 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007008}
7009#endif
7010
Mark Shannon5977a792020-12-02 13:31:40 +00007011
7012static int
7013normalize_basic_block(basicblock *bb);
7014
Mark Shannon6e8128f2020-07-30 10:03:00 +01007015static int
Inada Naoki8a232c72021-04-16 14:01:04 +09007016optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007017
Ɓukasz Langad41abe82021-09-08 18:25:09 +02007018static int
7019trim_unused_consts(struct compiler *c, struct assembler *a, PyObject *consts);
7020
Mark Shannon762ef852021-08-09 10:54:48 +01007021/* Duplicates exit BBs, so that line numbers can be propagated to them */
Mark Shannon5977a792020-12-02 13:31:40 +00007022static int
Mark Shannon762ef852021-08-09 10:54:48 +01007023duplicate_exits_without_lineno(struct compiler *c);
Mark Shannon5977a792020-12-02 13:31:40 +00007024
Mark Shannonb37181e2021-04-06 11:48:59 +01007025static int
Mark Shannon37686f72021-07-16 11:49:10 +01007026extend_block(basicblock *bb);
7027
7028static int
Mark Shannonb37181e2021-04-06 11:48:59 +01007029insert_generator_prefix(struct compiler *c, basicblock *entryblock) {
7030
7031 int flags = compute_code_flags(c);
7032 if (flags < 0) {
7033 return -1;
7034 }
7035 int kind;
7036 if (flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
7037 if (flags & CO_COROUTINE) {
7038 kind = 1;
7039 }
7040 else if (flags & CO_ASYNC_GENERATOR) {
7041 kind = 2;
7042 }
7043 else {
7044 kind = 0;
7045 }
7046 }
7047 else {
7048 return 0;
7049 }
7050 if (compiler_next_instr(entryblock) < 0) {
7051 return -1;
7052 }
7053 for (int i = entryblock->b_iused-1; i > 0; i--) {
7054 entryblock->b_instr[i] = entryblock->b_instr[i-1];
7055 }
7056 entryblock->b_instr[0].i_opcode = GEN_START;
7057 entryblock->b_instr[0].i_oparg = kind;
7058 entryblock->b_instr[0].i_lineno = -1;
7059 entryblock->b_instr[0].i_target = NULL;
7060 return 0;
7061}
7062
Mark Shannon0acdf252021-05-13 14:11:41 +01007063/* Make sure that all returns have a line number, even if early passes
7064 * have failed to propagate a correct line number.
7065 * The resulting line number may not be correct according to PEP 626,
7066 * but should be "good enough", and no worse than in older versions. */
7067static void
7068guarantee_lineno_for_exits(struct assembler *a, int firstlineno) {
7069 int lineno = firstlineno;
7070 assert(lineno > 0);
7071 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7072 if (b->b_iused == 0) {
7073 continue;
7074 }
7075 struct instr *last = &b->b_instr[b->b_iused-1];
7076 if (last->i_lineno < 0) {
Mark Shannon762ef852021-08-09 10:54:48 +01007077 if (last->i_opcode == RETURN_VALUE) {
Mark Shannon0acdf252021-05-13 14:11:41 +01007078 for (int i = 0; i < b->b_iused; i++) {
7079 assert(b->b_instr[i].i_lineno < 0);
Mark Shannon762ef852021-08-09 10:54:48 +01007080
Mark Shannon0acdf252021-05-13 14:11:41 +01007081 b->b_instr[i].i_lineno = lineno;
7082 }
7083 }
7084 }
7085 else {
7086 lineno = last->i_lineno;
7087 }
7088 }
7089}
7090
Mark Shannon762ef852021-08-09 10:54:48 +01007091static void
7092propagate_line_numbers(struct assembler *a);
7093
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007094static PyCodeObject *
7095assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00007096{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007097 basicblock *b, *entryblock;
7098 struct assembler a;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007099 int j, nblocks;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007100 PyCodeObject *co = NULL;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007101 PyObject *consts = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00007102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007103 /* Make sure every block that falls off the end returns None.
7104 XXX NEXT_BLOCK() isn't quite right, because if the last
7105 block ends with a jump or return b_next shouldn't set.
7106 */
7107 if (!c->u->u_curblock->b_return) {
Mark Shannon877df852020-11-12 09:43:29 +00007108 c->u->u_lineno = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007109 if (addNone)
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03007110 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007111 ADDOP(c, RETURN_VALUE);
7112 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00007113
Mark Shannon5977a792020-12-02 13:31:40 +00007114 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7115 if (normalize_basic_block(b)) {
Alex Henrie503627f2021-03-02 03:20:25 -07007116 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00007117 }
7118 }
7119
Mark Shannon37686f72021-07-16 11:49:10 +01007120 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7121 if (extend_block(b)) {
7122 return NULL;
7123 }
7124 }
7125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007126 nblocks = 0;
7127 entryblock = NULL;
7128 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
7129 nblocks++;
7130 entryblock = b;
7131 }
Mark Shannon67969f52021-04-07 10:52:07 +01007132 assert(entryblock != NULL);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00007133
Mark Shannonb37181e2021-04-06 11:48:59 +01007134 if (insert_generator_prefix(c, entryblock)) {
7135 goto error;
7136 }
7137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007138 /* Set firstlineno if it wasn't explicitly set. */
7139 if (!c->u->u_firstlineno) {
Mark Shannon67969f52021-04-07 10:52:07 +01007140 if (entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007141 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
Mark Shannon877df852020-11-12 09:43:29 +00007142 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007143 c->u->u_firstlineno = 1;
7144 }
Mark Shannon5977a792020-12-02 13:31:40 +00007145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007146 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
7147 goto error;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007148 a.a_entry = entryblock;
7149 a.a_nblocks = nblocks;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00007150
Mark Shannon6e8128f2020-07-30 10:03:00 +01007151 consts = consts_dict_keys_inorder(c->u->u_consts);
7152 if (consts == NULL) {
7153 goto error;
7154 }
Mark Shannon37686f72021-07-16 11:49:10 +01007155
Inada Naoki8a232c72021-04-16 14:01:04 +09007156 if (optimize_cfg(c, &a, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007157 goto error;
7158 }
Mark Shannon762ef852021-08-09 10:54:48 +01007159 if (duplicate_exits_without_lineno(c)) {
7160 return NULL;
7161 }
Ɓukasz Langad41abe82021-09-08 18:25:09 +02007162 if (trim_unused_consts(c, &a, consts)) {
7163 goto error;
7164 }
Mark Shannon762ef852021-08-09 10:54:48 +01007165 propagate_line_numbers(&a);
Mark Shannon0acdf252021-05-13 14:11:41 +01007166 guarantee_lineno_for_exits(&a, c->u->u_firstlineno);
Mark Shannond4e4ef12022-02-16 11:26:02 +00007167
7168 /* Order of basic blocks must have been determined by now */
7169 normalize_jumps(&a);
7170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007171 /* Can't modify the bytecode after computing jump offsets. */
7172 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00007173
Mark Shannoncc75ab72020-11-12 19:49:33 +00007174 /* Emit code. */
7175 for(b = entryblock; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007176 for (j = 0; j < b->b_iused; j++)
7177 if (!assemble_emit(&a, &b->b_instr[j]))
7178 goto error;
7179 }
Mark Shannon877df852020-11-12 09:43:29 +00007180 if (!assemble_line_range(&a)) {
7181 return 0;
7182 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00007183
Inada Naokibdb941b2021-02-10 09:20:42 +09007184 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007185 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09007186 }
7187 if (!merge_const_one(c, &a.a_lnotab)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007188 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09007189 }
7190 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) {
7191 goto error;
7192 }
7193 if (!merge_const_one(c, &a.a_bytecode)) {
7194 goto error;
7195 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00007196
Mark Shannon6e8128f2020-07-30 10:03:00 +01007197 co = makecode(c, &a, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007198 error:
Mark Shannon6e8128f2020-07-30 10:03:00 +01007199 Py_XDECREF(consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007200 assemble_free(&a);
7201 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00007202}
Georg Brandl8334fd92010-12-04 10:26:46 +00007203
Mark Shannon6e8128f2020-07-30 10:03:00 +01007204/* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
7205 with LOAD_CONST (c1, c2, ... cn).
7206 The consts table must still be in list form so that the
7207 new constant (c1, c2, ... cn) can be appended.
7208 Called with codestr pointing to the first LOAD_CONST.
7209*/
7210static int
Inada Naoki8a232c72021-04-16 14:01:04 +09007211fold_tuple_on_constants(struct compiler *c,
7212 struct instr *inst,
Mark Shannon6e8128f2020-07-30 10:03:00 +01007213 int n, PyObject *consts)
7214{
7215 /* Pre-conditions */
7216 assert(PyList_CheckExact(consts));
7217 assert(inst[n].i_opcode == BUILD_TUPLE);
7218 assert(inst[n].i_oparg == n);
7219
7220 for (int i = 0; i < n; i++) {
7221 if (inst[i].i_opcode != LOAD_CONST) {
7222 return 0;
7223 }
7224 }
7225
7226 /* Buildup new tuple of constants */
7227 PyObject *newconst = PyTuple_New(n);
7228 if (newconst == NULL) {
7229 return -1;
7230 }
7231 for (int i = 0; i < n; i++) {
7232 int arg = inst[i].i_oparg;
7233 PyObject *constant = PyList_GET_ITEM(consts, arg);
7234 Py_INCREF(constant);
7235 PyTuple_SET_ITEM(newconst, i, constant);
7236 }
Inada Naoki8a232c72021-04-16 14:01:04 +09007237 if (merge_const_one(c, &newconst) == 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007238 Py_DECREF(newconst);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007239 return -1;
7240 }
Inada Naoki8a232c72021-04-16 14:01:04 +09007241
7242 Py_ssize_t index;
7243 for (index = 0; index < PyList_GET_SIZE(consts); index++) {
7244 if (PyList_GET_ITEM(consts, index) == newconst) {
7245 break;
7246 }
7247 }
7248 if (index == PyList_GET_SIZE(consts)) {
7249 if ((size_t)index >= (size_t)INT_MAX - 1) {
7250 Py_DECREF(newconst);
7251 PyErr_SetString(PyExc_OverflowError, "too many constants");
7252 return -1;
7253 }
7254 if (PyList_Append(consts, newconst)) {
7255 Py_DECREF(newconst);
7256 return -1;
7257 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007258 }
7259 Py_DECREF(newconst);
7260 for (int i = 0; i < n; i++) {
7261 inst[i].i_opcode = NOP;
7262 }
7263 inst[n].i_opcode = LOAD_CONST;
Victor Stinner71f2ff42020-09-23 14:06:55 +02007264 inst[n].i_oparg = (int)index;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007265 return 0;
7266}
7267
Mark Shannon28b75c82020-12-23 11:43:10 +00007268
Brandt Bucher0ad1e032021-05-02 13:02:10 -07007269// Eliminate n * ROT_N(n).
7270static void
7271fold_rotations(struct instr *inst, int n)
7272{
7273 for (int i = 0; i < n; i++) {
7274 int rot;
7275 switch (inst[i].i_opcode) {
7276 case ROT_N:
7277 rot = inst[i].i_oparg;
7278 break;
7279 case ROT_FOUR:
7280 rot = 4;
7281 break;
7282 case ROT_THREE:
7283 rot = 3;
7284 break;
7285 case ROT_TWO:
7286 rot = 2;
7287 break;
7288 default:
7289 return;
7290 }
7291 if (rot != n) {
7292 return;
7293 }
7294 }
7295 for (int i = 0; i < n; i++) {
7296 inst[i].i_opcode = NOP;
7297 }
7298}
7299
Brandt Buchera89bbde2021-11-11 13:52:43 -08007300// Attempt to eliminate jumps to jumps by updating inst to jump to
7301// target->i_target using the provided opcode. Return whether or not the
7302// optimization was successful.
7303static bool
7304jump_thread(struct instr *inst, struct instr *target, int opcode)
7305{
7306 assert(is_jump(inst));
7307 assert(is_jump(target));
7308 // bpo-45773: If inst->i_target == target->i_target, then nothing actually
7309 // changes (and we fall into an infinite loop):
7310 if (inst->i_lineno == target->i_lineno &&
7311 inst->i_target != target->i_target)
7312 {
7313 inst->i_target = target->i_target;
7314 inst->i_opcode = opcode;
7315 return true;
Mark Shannon28b75c82020-12-23 11:43:10 +00007316 }
Brandt Buchera89bbde2021-11-11 13:52:43 -08007317 return false;
Mark Shannon28b75c82020-12-23 11:43:10 +00007318}
7319
Mark Shannoncc75ab72020-11-12 19:49:33 +00007320/* Maximum size of basic block that should be copied in optimizer */
7321#define MAX_COPY_SIZE 4
Mark Shannon6e8128f2020-07-30 10:03:00 +01007322
7323/* Optimization */
7324static int
Inada Naoki8a232c72021-04-16 14:01:04 +09007325optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
Mark Shannon6e8128f2020-07-30 10:03:00 +01007326{
7327 assert(PyList_CheckExact(consts));
7328 struct instr nop;
7329 nop.i_opcode = NOP;
7330 struct instr *target;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007331 for (int i = 0; i < bb->b_iused; i++) {
7332 struct instr *inst = &bb->b_instr[i];
7333 int oparg = inst->i_oparg;
7334 int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01007335 if (is_jump(inst)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007336 /* Skip over empty basic blocks. */
7337 while (inst->i_target->b_iused == 0) {
7338 inst->i_target = inst->i_target->b_next;
7339 }
7340 target = &inst->i_target->b_instr[0];
7341 }
7342 else {
7343 target = &nop;
7344 }
7345 switch (inst->i_opcode) {
Mark Shannon266b4622020-11-17 19:30:14 +00007346 /* Remove LOAD_CONST const; conditional jump */
Mark Shannon6e8128f2020-07-30 10:03:00 +01007347 case LOAD_CONST:
Mark Shannon266b4622020-11-17 19:30:14 +00007348 {
7349 PyObject* cnt;
7350 int is_true;
7351 int jump_if_true;
7352 switch(nextop) {
7353 case POP_JUMP_IF_FALSE:
7354 case POP_JUMP_IF_TRUE:
7355 cnt = PyList_GET_ITEM(consts, oparg);
7356 is_true = PyObject_IsTrue(cnt);
7357 if (is_true == -1) {
7358 goto error;
7359 }
7360 inst->i_opcode = NOP;
7361 jump_if_true = nextop == POP_JUMP_IF_TRUE;
7362 if (is_true == jump_if_true) {
7363 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
7364 bb->b_nofallthrough = 1;
7365 }
7366 else {
7367 bb->b_instr[i+1].i_opcode = NOP;
7368 }
7369 break;
7370 case JUMP_IF_FALSE_OR_POP:
7371 case JUMP_IF_TRUE_OR_POP:
7372 cnt = PyList_GET_ITEM(consts, oparg);
7373 is_true = PyObject_IsTrue(cnt);
7374 if (is_true == -1) {
7375 goto error;
7376 }
7377 jump_if_true = nextop == JUMP_IF_TRUE_OR_POP;
7378 if (is_true == jump_if_true) {
7379 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
7380 bb->b_nofallthrough = 1;
7381 }
7382 else {
7383 inst->i_opcode = NOP;
7384 bb->b_instr[i+1].i_opcode = NOP;
7385 }
7386 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007387 }
7388 break;
Mark Shannon266b4622020-11-17 19:30:14 +00007389 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007390
7391 /* Try to fold tuples of constants.
7392 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
7393 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
7394 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
7395 case BUILD_TUPLE:
7396 if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
7397 switch(oparg) {
7398 case 1:
7399 inst->i_opcode = NOP;
7400 bb->b_instr[i+1].i_opcode = NOP;
7401 break;
7402 case 2:
7403 inst->i_opcode = ROT_TWO;
7404 bb->b_instr[i+1].i_opcode = NOP;
7405 break;
7406 case 3:
7407 inst->i_opcode = ROT_THREE;
7408 bb->b_instr[i+1].i_opcode = ROT_TWO;
7409 }
7410 break;
7411 }
7412 if (i >= oparg) {
Inada Naoki8a232c72021-04-16 14:01:04 +09007413 if (fold_tuple_on_constants(c, inst-oparg, oparg, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007414 goto error;
7415 }
7416 }
7417 break;
7418
7419 /* Simplify conditional jump to conditional jump where the
7420 result of the first test implies the success of a similar
7421 test or the failure of the opposite test.
7422 Arises in code like:
7423 "a and b or c"
7424 "(a and b) and c"
7425 "(a or b) or c"
7426 "(a or b) and c"
7427 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
7428 --> x:JUMP_IF_FALSE_OR_POP z
7429 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
7430 --> x:POP_JUMP_IF_FALSE y+1
7431 where y+1 is the instruction following the second test.
7432 */
7433 case JUMP_IF_FALSE_OR_POP:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007434 switch (target->i_opcode) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007435 case POP_JUMP_IF_FALSE:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007436 i -= jump_thread(inst, target, POP_JUMP_IF_FALSE);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007437 break;
7438 case JUMP_ABSOLUTE:
7439 case JUMP_FORWARD:
7440 case JUMP_IF_FALSE_OR_POP:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007441 i -= jump_thread(inst, target, JUMP_IF_FALSE_OR_POP);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007442 break;
7443 case JUMP_IF_TRUE_OR_POP:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007444 case POP_JUMP_IF_TRUE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007445 if (inst->i_lineno == target->i_lineno) {
Brandt Buchera89bbde2021-11-11 13:52:43 -08007446 // We don't need to bother checking for loops here,
7447 // since a block's b_next cannot point to itself:
7448 assert(inst->i_target != inst->i_target->b_next);
Mark Shannon28b75c82020-12-23 11:43:10 +00007449 inst->i_opcode = POP_JUMP_IF_FALSE;
7450 inst->i_target = inst->i_target->b_next;
7451 --i;
7452 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007453 break;
7454 }
7455 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007456 case JUMP_IF_TRUE_OR_POP:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007457 switch (target->i_opcode) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007458 case POP_JUMP_IF_TRUE:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007459 i -= jump_thread(inst, target, POP_JUMP_IF_TRUE);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007460 break;
7461 case JUMP_ABSOLUTE:
7462 case JUMP_FORWARD:
7463 case JUMP_IF_TRUE_OR_POP:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007464 i -= jump_thread(inst, target, JUMP_IF_TRUE_OR_POP);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007465 break;
7466 case JUMP_IF_FALSE_OR_POP:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007467 case POP_JUMP_IF_FALSE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007468 if (inst->i_lineno == target->i_lineno) {
Brandt Buchera89bbde2021-11-11 13:52:43 -08007469 // We don't need to bother checking for loops here,
7470 // since a block's b_next cannot point to itself:
7471 assert(inst->i_target != inst->i_target->b_next);
Mark Shannon28b75c82020-12-23 11:43:10 +00007472 inst->i_opcode = POP_JUMP_IF_TRUE;
7473 inst->i_target = inst->i_target->b_next;
7474 --i;
7475 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007476 break;
7477 }
7478 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007479 case POP_JUMP_IF_FALSE:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007480 switch (target->i_opcode) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007481 case JUMP_ABSOLUTE:
7482 case JUMP_FORWARD:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007483 i -= jump_thread(inst, target, POP_JUMP_IF_FALSE);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007484 }
7485 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007486 case POP_JUMP_IF_TRUE:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007487 switch (target->i_opcode) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007488 case JUMP_ABSOLUTE:
7489 case JUMP_FORWARD:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007490 i -= jump_thread(inst, target, POP_JUMP_IF_TRUE);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007491 }
7492 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007493 case JUMP_ABSOLUTE:
7494 case JUMP_FORWARD:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007495 switch (target->i_opcode) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007496 case JUMP_ABSOLUTE:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007497 case JUMP_FORWARD:
7498 i -= jump_thread(inst, target, JUMP_ABSOLUTE);
Mark Shannon37686f72021-07-16 11:49:10 +01007499 }
7500 break;
7501 case FOR_ITER:
Mark Shannon37686f72021-07-16 11:49:10 +01007502 if (target->i_opcode == JUMP_FORWARD) {
Brandt Buchera89bbde2021-11-11 13:52:43 -08007503 i -= jump_thread(inst, target, FOR_ITER);
Mark Shannoncc75ab72020-11-12 19:49:33 +00007504 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07007505 break;
7506 case ROT_N:
7507 switch (oparg) {
7508 case 0:
7509 case 1:
7510 inst->i_opcode = NOP;
7511 continue;
7512 case 2:
7513 inst->i_opcode = ROT_TWO;
7514 break;
7515 case 3:
7516 inst->i_opcode = ROT_THREE;
7517 break;
7518 case 4:
7519 inst->i_opcode = ROT_FOUR;
7520 break;
7521 }
7522 if (i >= oparg - 1) {
7523 fold_rotations(inst - oparg + 1, oparg);
7524 }
7525 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007526 }
7527 }
7528 return 0;
7529error:
7530 return -1;
7531}
7532
Mark Shannon37686f72021-07-16 11:49:10 +01007533/* If this block ends with an unconditional jump to an exit block,
7534 * then remove the jump and extend this block with the target.
7535 */
7536static int
7537extend_block(basicblock *bb) {
7538 if (bb->b_iused == 0) {
7539 return 0;
7540 }
7541 struct instr *last = &bb->b_instr[bb->b_iused-1];
7542 if (last->i_opcode != JUMP_ABSOLUTE && last->i_opcode != JUMP_FORWARD) {
7543 return 0;
7544 }
7545 if (last->i_target->b_exit && last->i_target->b_iused <= MAX_COPY_SIZE) {
7546 basicblock *to_copy = last->i_target;
7547 last->i_opcode = NOP;
7548 for (int i = 0; i < to_copy->b_iused; i++) {
7549 int index = compiler_next_instr(bb);
7550 if (index < 0) {
7551 return -1;
7552 }
7553 bb->b_instr[index] = to_copy->b_instr[i];
7554 }
7555 bb->b_exit = 1;
7556 }
7557 return 0;
7558}
Mark Shannon6e8128f2020-07-30 10:03:00 +01007559
7560static void
Mark Shannon1659ad12021-01-13 15:05:04 +00007561clean_basic_block(basicblock *bb, int prev_lineno) {
7562 /* Remove NOPs when legal to do so. */
Mark Shannon6e8128f2020-07-30 10:03:00 +01007563 int dest = 0;
7564 for (int src = 0; src < bb->b_iused; src++) {
Mark Shannon877df852020-11-12 09:43:29 +00007565 int lineno = bb->b_instr[src].i_lineno;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007566 if (bb->b_instr[src].i_opcode == NOP) {
Mark Shannon266b4622020-11-17 19:30:14 +00007567 /* Eliminate no-op if it doesn't have a line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007568 if (lineno < 0) {
7569 continue;
7570 }
Mark Shannon266b4622020-11-17 19:30:14 +00007571 /* or, if the previous instruction had the same line number. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007572 if (prev_lineno == lineno) {
7573 continue;
7574 }
Mark Shannon266b4622020-11-17 19:30:14 +00007575 /* or, if the next instruction has same line number or no line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007576 if (src < bb->b_iused - 1) {
7577 int next_lineno = bb->b_instr[src+1].i_lineno;
7578 if (next_lineno < 0 || next_lineno == lineno) {
7579 bb->b_instr[src+1].i_lineno = lineno;
7580 continue;
Mark Shannon877df852020-11-12 09:43:29 +00007581 }
7582 }
Mark Shannon266b4622020-11-17 19:30:14 +00007583 else {
7584 basicblock* next = bb->b_next;
7585 while (next && next->b_iused == 0) {
7586 next = next->b_next;
7587 }
7588 /* or if last instruction in BB and next BB has same line number */
7589 if (next) {
7590 if (lineno == next->b_instr[0].i_lineno) {
7591 continue;
7592 }
7593 }
7594 }
7595
Mark Shannon6e8128f2020-07-30 10:03:00 +01007596 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007597 if (dest != src) {
7598 bb->b_instr[dest] = bb->b_instr[src];
7599 }
7600 dest++;
7601 prev_lineno = lineno;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007602 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007603 assert(dest <= bb->b_iused);
7604 bb->b_iused = dest;
7605}
7606
Mark Shannon266b4622020-11-17 19:30:14 +00007607static int
7608normalize_basic_block(basicblock *bb) {
7609 /* Mark blocks as exit and/or nofallthrough.
7610 Raise SystemError if CFG is malformed. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007611 for (int i = 0; i < bb->b_iused; i++) {
7612 switch(bb->b_instr[i].i_opcode) {
7613 case RETURN_VALUE:
7614 case RAISE_VARARGS:
7615 case RERAISE:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007616 bb->b_exit = 1;
Mark Shannon5977a792020-12-02 13:31:40 +00007617 bb->b_nofallthrough = 1;
7618 break;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007619 case JUMP_ABSOLUTE:
7620 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007621 bb->b_nofallthrough = 1;
Mark Shannon266b4622020-11-17 19:30:14 +00007622 /* fall through */
7623 case POP_JUMP_IF_FALSE:
7624 case POP_JUMP_IF_TRUE:
7625 case JUMP_IF_FALSE_OR_POP:
7626 case JUMP_IF_TRUE_OR_POP:
Mark Shannon5977a792020-12-02 13:31:40 +00007627 case FOR_ITER:
Mark Shannon266b4622020-11-17 19:30:14 +00007628 if (i != bb->b_iused-1) {
7629 PyErr_SetString(PyExc_SystemError, "malformed control flow graph.");
7630 return -1;
7631 }
Mark Shannon5977a792020-12-02 13:31:40 +00007632 /* Skip over empty basic blocks. */
7633 while (bb->b_instr[i].i_target->b_iused == 0) {
7634 bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next;
7635 }
7636
Mark Shannoncc75ab72020-11-12 19:49:33 +00007637 }
7638 }
Mark Shannon266b4622020-11-17 19:30:14 +00007639 return 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007640}
7641
Mark Shannon6e8128f2020-07-30 10:03:00 +01007642static int
7643mark_reachable(struct assembler *a) {
7644 basicblock **stack, **sp;
7645 sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks);
7646 if (stack == NULL) {
7647 return -1;
7648 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007649 a->a_entry->b_predecessors = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007650 *sp++ = a->a_entry;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007651 while (sp > stack) {
7652 basicblock *b = *(--sp);
Mark Shannon3bd60352021-01-13 12:05:43 +00007653 if (b->b_next && !b->b_nofallthrough) {
7654 if (b->b_next->b_predecessors == 0) {
7655 *sp++ = b->b_next;
7656 }
7657 b->b_next->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007658 }
7659 for (int i = 0; i < b->b_iused; i++) {
7660 basicblock *target;
Mark Shannon582aaf12020-08-04 17:30:11 +01007661 if (is_jump(&b->b_instr[i])) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007662 target = b->b_instr[i].i_target;
Mark Shannon3bd60352021-01-13 12:05:43 +00007663 if (target->b_predecessors == 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007664 *sp++ = target;
7665 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007666 target->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007667 }
7668 }
7669 }
7670 PyObject_Free(stack);
7671 return 0;
7672}
7673
Mark Shannon3bd60352021-01-13 12:05:43 +00007674static void
7675eliminate_empty_basic_blocks(basicblock *entry) {
7676 /* Eliminate empty blocks */
7677 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7678 basicblock *next = b->b_next;
7679 if (next) {
7680 while (next->b_iused == 0 && next->b_next) {
7681 next = next->b_next;
7682 }
7683 b->b_next = next;
7684 }
7685 }
7686 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7687 if (b->b_iused == 0) {
7688 continue;
7689 }
7690 if (is_jump(&b->b_instr[b->b_iused-1])) {
7691 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7692 while (target->b_iused == 0) {
7693 target = target->b_next;
7694 }
7695 b->b_instr[b->b_iused-1].i_target = target;
7696 }
7697 }
7698}
7699
7700
Mark Shannon5977a792020-12-02 13:31:40 +00007701/* If an instruction has no line number, but it's predecessor in the BB does,
Mark Shannon3bd60352021-01-13 12:05:43 +00007702 * then copy the line number. If a successor block has no line number, and only
7703 * one predecessor, then inherit the line number.
7704 * This ensures that all exit blocks (with one predecessor) receive a line number.
7705 * Also reduces the size of the line number table,
Mark Shannon5977a792020-12-02 13:31:40 +00007706 * but has no impact on the generated line number events.
7707 */
7708static void
Mark Shannon762ef852021-08-09 10:54:48 +01007709propagate_line_numbers(struct assembler *a) {
Mark Shannon5977a792020-12-02 13:31:40 +00007710 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007711 if (b->b_iused == 0) {
7712 continue;
7713 }
Mark Shannon5977a792020-12-02 13:31:40 +00007714 int prev_lineno = -1;
7715 for (int i = 0; i < b->b_iused; i++) {
7716 if (b->b_instr[i].i_lineno < 0) {
7717 b->b_instr[i].i_lineno = prev_lineno;
7718 }
7719 else {
7720 prev_lineno = b->b_instr[i].i_lineno;
7721 }
7722 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007723 if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) {
7724 assert(b->b_next->b_iused);
7725 if (b->b_next->b_instr[0].i_lineno < 0) {
7726 b->b_next->b_instr[0].i_lineno = prev_lineno;
7727 }
7728 }
7729 if (is_jump(&b->b_instr[b->b_iused-1])) {
7730 switch (b->b_instr[b->b_iused-1].i_opcode) {
7731 /* Note: Only actual jumps, not exception handlers */
7732 case SETUP_ASYNC_WITH:
7733 case SETUP_WITH:
7734 case SETUP_FINALLY:
7735 continue;
7736 }
7737 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7738 if (target->b_predecessors == 1) {
7739 if (target->b_instr[0].i_lineno < 0) {
7740 target->b_instr[0].i_lineno = prev_lineno;
7741 }
7742 }
7743 }
Mark Shannon5977a792020-12-02 13:31:40 +00007744 }
7745}
7746
7747/* Perform optimizations on a control flow graph.
Mark Shannon6e8128f2020-07-30 10:03:00 +01007748 The consts object should still be in list form to allow new constants
7749 to be appended.
7750
7751 All transformations keep the code size the same or smaller.
7752 For those that reduce size, the gaps are initially filled with
7753 NOPs. Later those NOPs are removed.
7754*/
7755
7756static int
Inada Naoki8a232c72021-04-16 14:01:04 +09007757optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts)
Mark Shannon6e8128f2020-07-30 10:03:00 +01007758{
Mark Shannoncc75ab72020-11-12 19:49:33 +00007759 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Inada Naoki8a232c72021-04-16 14:01:04 +09007760 if (optimize_basic_block(c, b, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007761 return -1;
7762 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007763 clean_basic_block(b, -1);
Mark Shannon3bd60352021-01-13 12:05:43 +00007764 assert(b->b_predecessors == 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007765 }
Mark Shannon762ef852021-08-09 10:54:48 +01007766 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7767 if (extend_block(b)) {
7768 return -1;
7769 }
7770 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007771 if (mark_reachable(a)) {
7772 return -1;
7773 }
7774 /* Delete unreachable instructions */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007775 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007776 if (b->b_predecessors == 0) {
Mark Shannoncc75ab72020-11-12 19:49:33 +00007777 b->b_iused = 0;
Om Gc71581c2020-12-16 17:48:05 +05307778 b->b_nofallthrough = 0;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007779 }
7780 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007781 basicblock *pred = NULL;
7782 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7783 int prev_lineno = -1;
7784 if (pred && pred->b_iused) {
7785 prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno;
7786 }
7787 clean_basic_block(b, prev_lineno);
7788 pred = b->b_nofallthrough ? NULL : b;
7789 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007790 eliminate_empty_basic_blocks(a->a_entry);
Om Gc71581c2020-12-16 17:48:05 +05307791 /* Delete jump instructions made redundant by previous step. If a non-empty
7792 block ends with a jump instruction, check if the next non-empty block
7793 reached through normal flow control is the target of that jump. If it
7794 is, then the jump instruction is redundant and can be deleted.
7795 */
Mark Shannon3bd60352021-01-13 12:05:43 +00007796 int maybe_empty_blocks = 0;
Om Gc71581c2020-12-16 17:48:05 +05307797 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7798 if (b->b_iused > 0) {
7799 struct instr *b_last_instr = &b->b_instr[b->b_iused - 1];
Mark Shannon802b6452021-02-02 14:59:15 +00007800 if (b_last_instr->i_opcode == JUMP_ABSOLUTE ||
Om Gc71581c2020-12-16 17:48:05 +05307801 b_last_instr->i_opcode == JUMP_FORWARD) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007802 if (b_last_instr->i_target == b->b_next) {
7803 assert(b->b_next->b_iused);
Om Gc71581c2020-12-16 17:48:05 +05307804 b->b_nofallthrough = 0;
Mark Shannon802b6452021-02-02 14:59:15 +00007805 b_last_instr->i_opcode = NOP;
7806 clean_basic_block(b, -1);
7807 maybe_empty_blocks = 1;
Om Gc71581c2020-12-16 17:48:05 +05307808 }
7809 }
7810 }
7811 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007812 if (maybe_empty_blocks) {
7813 eliminate_empty_basic_blocks(a->a_entry);
7814 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007815 return 0;
7816}
7817
Ɓukasz Langad41abe82021-09-08 18:25:09 +02007818// Remove trailing unused constants.
7819static int
7820trim_unused_consts(struct compiler *c, struct assembler *a, PyObject *consts)
7821{
7822 assert(PyList_CheckExact(consts));
7823
7824 // The first constant may be docstring; keep it always.
7825 int max_const_index = 0;
7826 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7827 for (int i = 0; i < b->b_iused; i++) {
7828 if (b->b_instr[i].i_opcode == LOAD_CONST &&
7829 b->b_instr[i].i_oparg > max_const_index) {
7830 max_const_index = b->b_instr[i].i_oparg;
7831 }
7832 }
7833 }
7834 if (max_const_index+1 < PyList_GET_SIZE(consts)) {
7835 //fprintf(stderr, "removing trailing consts: max=%d, size=%d\n",
7836 // max_const_index, (int)PyList_GET_SIZE(consts));
7837 if (PyList_SetSlice(consts, max_const_index+1,
7838 PyList_GET_SIZE(consts), NULL) < 0) {
7839 return 1;
7840 }
7841 }
7842 return 0;
7843}
7844
Mark Shannon5977a792020-12-02 13:31:40 +00007845static inline int
7846is_exit_without_lineno(basicblock *b) {
7847 return b->b_exit && b->b_instr[0].i_lineno < 0;
7848}
7849
7850/* PEP 626 mandates that the f_lineno of a frame is correct
7851 * after a frame terminates. It would be prohibitively expensive
7852 * to continuously update the f_lineno field at runtime,
7853 * so we make sure that all exiting instruction (raises and returns)
7854 * have a valid line number, allowing us to compute f_lineno lazily.
7855 * We can do this by duplicating the exit blocks without line number
7856 * so that none have more than one predecessor. We can then safely
7857 * copy the line number from the sole predecessor block.
7858 */
7859static int
Mark Shannon762ef852021-08-09 10:54:48 +01007860duplicate_exits_without_lineno(struct compiler *c)
Mark Shannon5977a792020-12-02 13:31:40 +00007861{
7862 /* Copy all exit blocks without line number that are targets of a jump.
7863 */
7864 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7865 if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) {
7866 switch (b->b_instr[b->b_iused-1].i_opcode) {
7867 /* Note: Only actual jumps, not exception handlers */
7868 case SETUP_ASYNC_WITH:
7869 case SETUP_WITH:
7870 case SETUP_FINALLY:
7871 continue;
7872 }
7873 basicblock *target = b->b_instr[b->b_iused-1].i_target;
Mark Shannon762ef852021-08-09 10:54:48 +01007874 if (is_exit_without_lineno(target) && target->b_predecessors > 1) {
Mark Shannon5977a792020-12-02 13:31:40 +00007875 basicblock *new_target = compiler_copy_block(c, target);
7876 if (new_target == NULL) {
7877 return -1;
7878 }
7879 new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7880 b->b_instr[b->b_iused-1].i_target = new_target;
Mark Shannon762ef852021-08-09 10:54:48 +01007881 target->b_predecessors--;
7882 new_target->b_predecessors = 1;
7883 new_target->b_next = target->b_next;
7884 target->b_next = new_target;
Mark Shannon5977a792020-12-02 13:31:40 +00007885 }
7886 }
Mark Shannoneaccc122020-12-04 15:22:12 +00007887 }
Mark Shannonee9f98d2021-01-05 12:04:10 +00007888 /* Eliminate empty blocks */
7889 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7890 while (b->b_next && b->b_next->b_iused == 0) {
7891 b->b_next = b->b_next->b_next;
7892 }
7893 }
Mark Shannon5977a792020-12-02 13:31:40 +00007894 /* Any remaining reachable exit blocks without line number can only be reached by
7895 * fall through, and thus can only have a single predecessor */
7896 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7897 if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) {
7898 if (is_exit_without_lineno(b->b_next)) {
7899 assert(b->b_next->b_iused > 0);
7900 b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7901 }
7902 }
7903 }
7904 return 0;
7905}
7906
7907
Mark Shannon6e8128f2020-07-30 10:03:00 +01007908/* Retained for API compatibility.
7909 * Optimization is now done in optimize_cfg */
7910
7911PyObject *
7912PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
7913 PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
7914{
7915 Py_INCREF(code);
7916 return code;
7917}