blob: a7f62bbcbb33e441932dd70df080b185d49087e8 [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;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 /* depth of stack upon entry of block, computed by stackdepth() */
132 int b_startdepth;
133 /* instruction offset for block, computed by assemble_jump_offsets() */
134 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000135} basicblock;
136
137/* fblockinfo tracks the current frame block.
138
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000139A frame block is used to handle loops, try/except, and try/finally.
140It's called a frame block to distinguish it from a basic block in the
141compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000142*/
143
Mark Shannon02d126a2020-09-25 14:04:19 +0100144enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END,
tomKPZ7a7ba3d2021-04-07 07:43:45 -0700145 WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER,
146 ASYNC_COMPREHENSION_GENERATOR };
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000147
148struct fblockinfo {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 enum fblocktype fb_type;
150 basicblock *fb_block;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200151 /* (optional) type-specific exit or cleanup block */
152 basicblock *fb_exit;
Mark Shannonfee55262019-11-21 09:11:43 +0000153 /* (optional) additional information required for unwinding */
154 void *fb_datum;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000155};
156
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100157enum {
158 COMPILER_SCOPE_MODULE,
159 COMPILER_SCOPE_CLASS,
160 COMPILER_SCOPE_FUNCTION,
Yury Selivanov75445082015-05-11 22:57:16 -0400161 COMPILER_SCOPE_ASYNC_FUNCTION,
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400162 COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100163 COMPILER_SCOPE_COMPREHENSION,
164};
165
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000166/* The following items change on entry and exit of code blocks.
167 They must be saved and restored when returning to a block.
168*/
169struct compiler_unit {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 PySTEntryObject *u_ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 PyObject *u_name;
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400173 PyObject *u_qualname; /* dot-separated qualified name (lazy) */
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100174 int u_scope_type;
175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 /* The following fields are dicts that map objects to
177 the index of them in co_XXX. The index is used as
178 the argument for opcodes that refer to those collections.
179 */
180 PyObject *u_consts; /* all constants */
181 PyObject *u_names; /* all names */
182 PyObject *u_varnames; /* local variables */
183 PyObject *u_cellvars; /* cell variables */
184 PyObject *u_freevars; /* free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 PyObject *u_private; /* for private name mangling */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187
Victor Stinnerf8e32212013-11-19 23:56:34 +0100188 Py_ssize_t u_argcount; /* number of arguments for block */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100189 Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */
Victor Stinnerf8e32212013-11-19 23:56:34 +0100190 Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 /* Pointer to the most recently allocated block. By following b_list
192 members, you can reach all early allocated blocks. */
193 basicblock *u_blocks;
194 basicblock *u_curblock; /* pointer to current block */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 int u_nfblocks;
197 struct fblockinfo u_fblock[CO_MAXBLOCKS];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 int u_firstlineno; /* the first lineno of the block */
200 int u_lineno; /* the lineno for the current stmt */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000201 int u_col_offset; /* the offset of the current stmt */
Pablo Galindoa77aac42021-04-23 14:27:05 +0100202 int u_end_lineno; /* the end line of the current stmt */
203 int u_end_col_offset; /* the end offset of the current stmt */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000204};
205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206/* This struct captures the global state of a compilation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000208The u pointer points to the current compilation unit, while units
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209for enclosing blocks are stored in c_stack. The u and c_stack are
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000210managed by compiler_enter_scope() and compiler_exit_scope().
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000211
212Note that we don't track recursion levels during compilation - the
213task of detecting and rejecting excessive levels of nesting is
214handled by the symbol analysis pass.
215
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000216*/
217
218struct compiler {
Victor Stinner14e461d2013-08-26 22:28:21 +0200219 PyObject *c_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 struct symtable *c_st;
221 PyFutureFeatures *c_future; /* pointer to module's __future__ */
222 PyCompilerFlags *c_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000223
Georg Brandl8334fd92010-12-04 10:26:46 +0000224 int c_optimize; /* optimization level */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 int c_interactive; /* true if in interactive mode */
226 int c_nestlevel;
INADA Naokic2e16072018-11-26 21:23:22 +0900227 PyObject *c_const_cache; /* Python dict holding all constants,
228 including names tuple */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 struct compiler_unit *u; /* compiler state for current block */
230 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
231 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232};
233
Brandt Bucher145bf262021-02-26 14:51:55 -0800234typedef struct {
Brandt Bucher0ad1e032021-05-02 13:02:10 -0700235 // A list of strings corresponding to name captures. It is used to track:
236 // - Repeated name assignments in the same pattern.
237 // - Different name assignments in alternatives.
238 // - The order of name assignments in alternatives.
Brandt Bucher145bf262021-02-26 14:51:55 -0800239 PyObject *stores;
Brandt Bucher0ad1e032021-05-02 13:02:10 -0700240 // If 0, any name captures against our subject will raise.
Brandt Bucher145bf262021-02-26 14:51:55 -0800241 int allow_irrefutable;
Brandt Bucher0ad1e032021-05-02 13:02:10 -0700242 // An array of blocks to jump to on failure. Jumping to fail_pop[i] will pop
243 // i items off of the stack. The end result looks like this (with each block
244 // falling through to the next):
245 // fail_pop[4]: POP_TOP
246 // fail_pop[3]: POP_TOP
247 // fail_pop[2]: POP_TOP
248 // fail_pop[1]: POP_TOP
249 // fail_pop[0]: NOP
250 basicblock **fail_pop;
251 // The current length of fail_pop.
252 Py_ssize_t fail_pop_size;
253 // The number of items on top of the stack that need to *stay* on top of the
254 // stack. Variable captures go beneath these. All of them will be popped on
255 // failure.
256 Py_ssize_t on_top;
Brandt Bucher145bf262021-02-26 14:51:55 -0800257} pattern_context;
258
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100259static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000260static void compiler_free(struct compiler *);
261static basicblock *compiler_new_block(struct compiler *);
Andy Lester76d58772020-03-10 21:18:12 -0500262static int compiler_next_instr(basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000263static int compiler_addop(struct compiler *, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +0100264static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
Mark Shannon582aaf12020-08-04 17:30:11 +0100265static int compiler_addop_j(struct compiler *, int, basicblock *);
Mark Shannon127dde52021-01-04 18:06:55 +0000266static int compiler_addop_j_noline(struct compiler *, int, basicblock *);
Brandt Bucher145bf262021-02-26 14:51:55 -0800267static int compiler_error(struct compiler *, const char *, ...);
Serhiy Storchaka62e44812019-02-16 08:12:19 +0200268static int compiler_warn(struct compiler *, const char *, ...);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000269static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
270
271static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
272static int compiler_visit_stmt(struct compiler *, stmt_ty);
273static int compiler_visit_keyword(struct compiler *, keyword_ty);
274static int compiler_visit_expr(struct compiler *, expr_ty);
275static int compiler_augassign(struct compiler *, stmt_ty);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700276static int compiler_annassign(struct compiler *, stmt_ty);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +0200277static int compiler_subscript(struct compiler *, expr_ty);
278static int compiler_slice(struct compiler *, expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000279
Andy Lester76d58772020-03-10 21:18:12 -0500280static int inplace_binop(operator_ty);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100281static int are_all_items_const(asdl_expr_seq *, Py_ssize_t, Py_ssize_t);
Mark Shannon8473cf82020-12-15 11:07:50 +0000282
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000283
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500284static int compiler_with(struct compiler *, stmt_ty, int);
Yury Selivanov75445082015-05-11 22:57:16 -0400285static int compiler_async_with(struct compiler *, stmt_ty, int);
286static int compiler_async_for(struct compiler *, stmt_ty);
Victor Stinner976bb402016-03-23 11:36:19 +0100287static int compiler_call_helper(struct compiler *c, int n,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100288 asdl_expr_seq *args,
289 asdl_keyword_seq *keywords);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500290static int compiler_try_except(struct compiler *, stmt_ty);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400291static int compiler_set_qualname(struct compiler *);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000292
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700293static int compiler_sync_comprehension_generator(
294 struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100295 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +0200296 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700297 expr_ty elt, expr_ty val, int type);
298
299static int compiler_async_comprehension_generator(
300 struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100301 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +0200302 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700303 expr_ty elt, expr_ty val, int type);
304
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000305static int compiler_pattern(struct compiler *, pattern_ty, pattern_context *);
Brandt Bucher145bf262021-02-26 14:51:55 -0800306static int compiler_match(struct compiler *, stmt_ty);
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000307static int compiler_pattern_subpattern(struct compiler *, pattern_ty,
Brandt Bucher145bf262021-02-26 14:51:55 -0800308 pattern_context *);
309
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000310static PyCodeObject *assemble(struct compiler *, int addNone);
Mark Shannon332cd5e2018-01-30 00:41:04 +0000311static PyObject *__doc__, *__annotations__;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000312
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400313#define CAPSULE_NAME "compile.c compiler unit"
Benjamin Petersonb173f782009-05-05 22:31:58 +0000314
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000315PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000316_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 /* Name mangling: __private becomes _classname__private.
319 This is independent from how the name is used. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200320 PyObject *result;
321 size_t nlen, plen, ipriv;
322 Py_UCS4 maxchar;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200324 PyUnicode_READ_CHAR(ident, 0) != '_' ||
325 PyUnicode_READ_CHAR(ident, 1) != '_') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 Py_INCREF(ident);
327 return ident;
328 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200329 nlen = PyUnicode_GET_LENGTH(ident);
330 plen = PyUnicode_GET_LENGTH(privateobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 /* Don't mangle __id__ or names with dots.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 The only time a name with a dot can occur is when
334 we are compiling an import statement that has a
335 package name.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 TODO(jhylton): Decide whether we want to support
338 mangling of the module name, e.g. __M.X.
339 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200340 if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
341 PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
342 PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 Py_INCREF(ident);
344 return ident; /* Don't mangle __whatever__ */
345 }
346 /* Strip leading underscores from class name */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200347 ipriv = 0;
348 while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
349 ipriv++;
350 if (ipriv == plen) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 Py_INCREF(ident);
352 return ident; /* Don't mangle if class is just underscores */
353 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200354 plen -= ipriv;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000355
Antoine Pitrou55bff892013-04-06 21:21:04 +0200356 if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
357 PyErr_SetString(PyExc_OverflowError,
358 "private identifier too large to be mangled");
359 return NULL;
360 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000361
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200362 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
363 if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
364 maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
365
366 result = PyUnicode_New(1 + nlen + plen, maxchar);
367 if (!result)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200369 /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
370 PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
Victor Stinner6c7a52a2011-09-28 21:39:17 +0200371 if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
372 Py_DECREF(result);
373 return NULL;
374 }
375 if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
376 Py_DECREF(result);
377 return NULL;
378 }
Victor Stinner8f825062012-04-27 13:55:39 +0200379 assert(_PyUnicode_CheckConsistency(result, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200380 return result;
Michael W. Hudson60934622004-08-12 17:56:29 +0000381}
382
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000383static int
384compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000387
INADA Naokic2e16072018-11-26 21:23:22 +0900388 c->c_const_cache = PyDict_New();
389 if (!c->c_const_cache) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 return 0;
INADA Naokic2e16072018-11-26 21:23:22 +0900391 }
392
393 c->c_stack = PyList_New(0);
394 if (!c->c_stack) {
395 Py_CLEAR(c->c_const_cache);
396 return 0;
397 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000400}
401
402PyCodeObject *
Victor Stinnera81fca62021-03-24 00:51:50 +0100403_PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
404 int optimize, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000405{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 struct compiler c;
407 PyCodeObject *co = NULL;
Victor Stinner37d66d72019-06-13 02:16:41 +0200408 PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 if (!__doc__) {
412 __doc__ = PyUnicode_InternFromString("__doc__");
413 if (!__doc__)
414 return NULL;
415 }
Mark Shannon332cd5e2018-01-30 00:41:04 +0000416 if (!__annotations__) {
417 __annotations__ = PyUnicode_InternFromString("__annotations__");
418 if (!__annotations__)
419 return NULL;
420 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 if (!compiler_init(&c))
422 return NULL;
Victor Stinner14e461d2013-08-26 22:28:21 +0200423 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 c.c_filename = filename;
425 c.c_arena = arena;
Victor Stinnera81fca62021-03-24 00:51:50 +0100426 c.c_future = _PyFuture_FromAST(mod, filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 if (c.c_future == NULL)
428 goto finally;
429 if (!flags) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 flags = &local_flags;
431 }
432 merged = c.c_future->ff_features | flags->cf_flags;
433 c.c_future->ff_features = merged;
434 flags->cf_flags = merged;
435 c.c_flags = flags;
Victor Stinnerda7933e2020-04-13 03:04:28 +0200436 c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000438
Pablo Galindod112c602020-03-18 23:02:09 +0000439 _PyASTOptimizeState state;
440 state.optimize = c.c_optimize;
441 state.ff_features = merged;
442
443 if (!_PyAST_Optimize(mod, arena, &state)) {
INADA Naoki7ea143a2017-12-14 16:47:20 +0900444 goto finally;
445 }
446
Victor Stinner28ad12f2021-03-19 12:41:49 +0100447 c.c_st = _PySymtable_Build(mod, filename, c.c_future);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 if (c.c_st == NULL) {
449 if (!PyErr_Occurred())
450 PyErr_SetString(PyExc_SystemError, "no symtable");
451 goto finally;
452 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000455
Thomas Wouters1175c432006-02-27 22:49:54 +0000456 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 compiler_free(&c);
458 assert(co || PyErr_Occurred());
459 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000460}
461
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000462static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000463compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000464{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 if (c->c_st)
Victor Stinner28ad12f2021-03-19 12:41:49 +0100466 _PySymtable_Free(c->c_st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 if (c->c_future)
468 PyObject_Free(c->c_future);
Victor Stinner14e461d2013-08-26 22:28:21 +0200469 Py_XDECREF(c->c_filename);
INADA Naokic2e16072018-11-26 21:23:22 +0900470 Py_DECREF(c->c_const_cache);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000472}
473
Guido van Rossum79f25d91997-04-29 20:08:16 +0000474static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000475list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 Py_ssize_t i, n;
478 PyObject *v, *k;
479 PyObject *dict = PyDict_New();
480 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 n = PyList_Size(list);
483 for (i = 0; i < n; i++) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100484 v = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 if (!v) {
486 Py_DECREF(dict);
487 return NULL;
488 }
489 k = PyList_GET_ITEM(list, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300490 if (PyDict_SetItem(dict, k, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 Py_DECREF(v);
492 Py_DECREF(dict);
493 return NULL;
494 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 Py_DECREF(v);
496 }
497 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000498}
499
500/* Return new dict containing names from src that match scope(s).
501
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000502src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000504values are integers, starting at offset and increasing by one for
505each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506*/
507
508static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +0100509dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000510{
Benjamin Peterson51ab2832012-07-18 15:12:47 -0700511 Py_ssize_t i = offset, scope, num_keys, key_i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 PyObject *k, *v, *dest = PyDict_New();
Meador Inge2ca63152012-07-18 14:20:11 -0500513 PyObject *sorted_keys;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 assert(offset >= 0);
516 if (dest == NULL)
517 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000518
Meador Inge2ca63152012-07-18 14:20:11 -0500519 /* Sort the keys so that we have a deterministic order on the indexes
520 saved in the returned dictionary. These indexes are used as indexes
521 into the free and cell var storage. Therefore if they aren't
522 deterministic, then the generated bytecode is not deterministic.
523 */
524 sorted_keys = PyDict_Keys(src);
525 if (sorted_keys == NULL)
526 return NULL;
527 if (PyList_Sort(sorted_keys) != 0) {
528 Py_DECREF(sorted_keys);
529 return NULL;
530 }
Meador Ingef69e24e2012-07-18 16:41:03 -0500531 num_keys = PyList_GET_SIZE(sorted_keys);
Meador Inge2ca63152012-07-18 14:20:11 -0500532
533 for (key_i = 0; key_i < num_keys; key_i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 /* XXX this should probably be a macro in symtable.h */
535 long vi;
Meador Inge2ca63152012-07-18 14:20:11 -0500536 k = PyList_GET_ITEM(sorted_keys, key_i);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200537 v = PyDict_GetItemWithError(src, k);
538 assert(v && PyLong_Check(v));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 vi = PyLong_AS_LONG(v);
540 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 if (scope == scope_type || vi & flag) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300543 PyObject *item = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 if (item == NULL) {
Meador Inge2ca63152012-07-18 14:20:11 -0500545 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 Py_DECREF(dest);
547 return NULL;
548 }
549 i++;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300550 if (PyDict_SetItem(dest, k, item) < 0) {
Meador Inge2ca63152012-07-18 14:20:11 -0500551 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 Py_DECREF(item);
553 Py_DECREF(dest);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 return NULL;
555 }
556 Py_DECREF(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 }
558 }
Meador Inge2ca63152012-07-18 14:20:11 -0500559 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000561}
562
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563static void
564compiler_unit_check(struct compiler_unit *u)
565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 basicblock *block;
567 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +0100568 assert(!_PyMem_IsPtrFreed(block));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 if (block->b_instr != NULL) {
570 assert(block->b_ialloc > 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +0100571 assert(block->b_iused >= 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 assert(block->b_ialloc >= block->b_iused);
573 }
574 else {
575 assert (block->b_iused == 0);
576 assert (block->b_ialloc == 0);
577 }
578 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579}
580
581static void
582compiler_unit_free(struct compiler_unit *u)
583{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 compiler_unit_check(u);
587 b = u->u_blocks;
588 while (b != NULL) {
589 if (b->b_instr)
590 PyObject_Free((void *)b->b_instr);
591 next = b->b_list;
592 PyObject_Free((void *)b);
593 b = next;
594 }
595 Py_CLEAR(u->u_ste);
596 Py_CLEAR(u->u_name);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400597 Py_CLEAR(u->u_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 Py_CLEAR(u->u_consts);
599 Py_CLEAR(u->u_names);
600 Py_CLEAR(u->u_varnames);
601 Py_CLEAR(u->u_freevars);
602 Py_CLEAR(u->u_cellvars);
603 Py_CLEAR(u->u_private);
604 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000605}
606
607static int
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100608compiler_enter_scope(struct compiler *c, identifier name,
609 int scope_type, void *key, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 struct compiler_unit *u;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100612 basicblock *block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613
Andy Lester7668a8b2020-03-24 23:26:44 -0500614 u = (struct compiler_unit *)PyObject_Calloc(1, sizeof(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 struct compiler_unit));
616 if (!u) {
617 PyErr_NoMemory();
618 return 0;
619 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100620 u->u_scope_type = scope_type;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 u->u_argcount = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100622 u->u_posonlyargcount = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 u->u_kwonlyargcount = 0;
624 u->u_ste = PySymtable_Lookup(c->c_st, key);
625 if (!u->u_ste) {
626 compiler_unit_free(u);
627 return 0;
628 }
629 Py_INCREF(name);
630 u->u_name = name;
631 u->u_varnames = list2dict(u->u_ste->ste_varnames);
632 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
633 if (!u->u_varnames || !u->u_cellvars) {
634 compiler_unit_free(u);
635 return 0;
636 }
Benjamin Peterson312595c2013-05-15 15:26:42 -0500637 if (u->u_ste->ste_needs_class_closure) {
Martin Panter7462b6492015-11-02 03:37:02 +0000638 /* Cook up an implicit __class__ cell. */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500639 _Py_IDENTIFIER(__class__);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300640 PyObject *name;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500641 int res;
642 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200643 assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500644 name = _PyUnicode_FromId(&PyId___class__);
645 if (!name) {
646 compiler_unit_free(u);
647 return 0;
648 }
Victor Stinnerc9bc2902020-10-27 02:24:34 +0100649 res = PyDict_SetItem(u->u_cellvars, name, _PyLong_GetZero());
Benjamin Peterson312595c2013-05-15 15:26:42 -0500650 if (res < 0) {
651 compiler_unit_free(u);
652 return 0;
653 }
654 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200657 PyDict_GET_SIZE(u->u_cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 if (!u->u_freevars) {
659 compiler_unit_free(u);
660 return 0;
661 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 u->u_blocks = NULL;
664 u->u_nfblocks = 0;
665 u->u_firstlineno = lineno;
666 u->u_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000667 u->u_col_offset = 0;
Pablo Galindoa77aac42021-04-23 14:27:05 +0100668 u->u_end_lineno = 0;
669 u->u_end_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 u->u_consts = PyDict_New();
671 if (!u->u_consts) {
672 compiler_unit_free(u);
673 return 0;
674 }
675 u->u_names = PyDict_New();
676 if (!u->u_names) {
677 compiler_unit_free(u);
678 return 0;
679 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 /* Push the old compiler_unit on the stack. */
684 if (c->u) {
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400685 PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
687 Py_XDECREF(capsule);
688 compiler_unit_free(u);
689 return 0;
690 }
691 Py_DECREF(capsule);
692 u->u_private = c->u->u_private;
693 Py_XINCREF(u->u_private);
694 }
695 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 c->c_nestlevel++;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100698
699 block = compiler_new_block(c);
700 if (block == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 return 0;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100702 c->u->u_curblock = block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400704 if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
705 if (!compiler_set_qualname(c))
706 return 0;
707 }
708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000710}
711
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000712static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000713compiler_exit_scope(struct compiler *c)
714{
Victor Stinnera6192632021-01-29 16:53:03 +0100715 // Don't call PySequence_DelItem() with an exception raised
716 PyObject *exc_type, *exc_val, *exc_tb;
717 PyErr_Fetch(&exc_type, &exc_val, &exc_tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 c->c_nestlevel--;
720 compiler_unit_free(c->u);
721 /* Restore c->u to the parent unit. */
Victor Stinnera6192632021-01-29 16:53:03 +0100722 Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 if (n >= 0) {
Victor Stinnera6192632021-01-29 16:53:03 +0100724 PyObject *capsule = PyList_GET_ITEM(c->c_stack, n);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400725 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 assert(c->u);
727 /* we are deleting from a list so this really shouldn't fail */
Victor Stinnera6192632021-01-29 16:53:03 +0100728 if (PySequence_DelItem(c->c_stack, n) < 0) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +0100729 _PyErr_WriteUnraisableMsg("on removing the last compiler "
730 "stack item", NULL);
Victor Stinnera6192632021-01-29 16:53:03 +0100731 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 compiler_unit_check(c->u);
733 }
Victor Stinnera6192632021-01-29 16:53:03 +0100734 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 c->u = NULL;
Victor Stinnera6192632021-01-29 16:53:03 +0100736 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737
Victor Stinnera6192632021-01-29 16:53:03 +0100738 PyErr_Restore(exc_type, exc_val, exc_tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000739}
740
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400741static int
742compiler_set_qualname(struct compiler *c)
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100743{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100744 _Py_static_string(dot, ".");
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400745 _Py_static_string(dot_locals, ".<locals>");
746 Py_ssize_t stack_size;
747 struct compiler_unit *u = c->u;
748 PyObject *name, *base, *dot_str, *dot_locals_str;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100749
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400750 base = NULL;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100751 stack_size = PyList_GET_SIZE(c->c_stack);
Benjamin Petersona8a38b82013-10-19 16:14:39 -0400752 assert(stack_size >= 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400753 if (stack_size > 1) {
754 int scope, force_global = 0;
755 struct compiler_unit *parent;
756 PyObject *mangled, *capsule;
757
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400758 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400759 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400760 assert(parent);
761
Yury Selivanov75445082015-05-11 22:57:16 -0400762 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
763 || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
764 || u->u_scope_type == COMPILER_SCOPE_CLASS) {
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400765 assert(u->u_name);
766 mangled = _Py_Mangle(parent->u_private, u->u_name);
767 if (!mangled)
768 return 0;
Victor Stinner28ad12f2021-03-19 12:41:49 +0100769 scope = _PyST_GetScope(parent->u_ste, mangled);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400770 Py_DECREF(mangled);
771 assert(scope != GLOBAL_IMPLICIT);
772 if (scope == GLOBAL_EXPLICIT)
773 force_global = 1;
774 }
775
776 if (!force_global) {
777 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
Yury Selivanov75445082015-05-11 22:57:16 -0400778 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400779 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
780 dot_locals_str = _PyUnicode_FromId(&dot_locals);
781 if (dot_locals_str == NULL)
782 return 0;
783 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
784 if (base == NULL)
785 return 0;
786 }
787 else {
788 Py_INCREF(parent->u_qualname);
789 base = parent->u_qualname;
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400790 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100791 }
792 }
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400793
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400794 if (base != NULL) {
795 dot_str = _PyUnicode_FromId(&dot);
796 if (dot_str == NULL) {
797 Py_DECREF(base);
798 return 0;
799 }
800 name = PyUnicode_Concat(base, dot_str);
801 Py_DECREF(base);
802 if (name == NULL)
803 return 0;
804 PyUnicode_Append(&name, u->u_name);
805 if (name == NULL)
806 return 0;
807 }
808 else {
809 Py_INCREF(u->u_name);
810 name = u->u_name;
811 }
812 u->u_qualname = name;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100813
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400814 return 1;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100815}
816
Eric V. Smith235a6f02015-09-19 14:51:32 -0400817
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818/* Allocate a new block and return a pointer to it.
819 Returns NULL on error.
820*/
821
822static basicblock *
823compiler_new_block(struct compiler *c)
824{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 basicblock *b;
826 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 u = c->u;
Andy Lester7668a8b2020-03-24 23:26:44 -0500829 b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 if (b == NULL) {
831 PyErr_NoMemory();
832 return NULL;
833 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 /* Extend the singly linked list of blocks with new block. */
835 b->b_list = u->u_blocks;
836 u->u_blocks = b;
837 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838}
839
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840static basicblock *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841compiler_next_block(struct compiler *c)
842{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 basicblock *block = compiler_new_block(c);
844 if (block == NULL)
845 return NULL;
846 c->u->u_curblock->b_next = block;
847 c->u->u_curblock = block;
848 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849}
850
851static basicblock *
852compiler_use_next_block(struct compiler *c, basicblock *block)
853{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 assert(block != NULL);
855 c->u->u_curblock->b_next = block;
856 c->u->u_curblock = block;
857 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858}
859
Mark Shannon5977a792020-12-02 13:31:40 +0000860static basicblock *
861compiler_copy_block(struct compiler *c, basicblock *block)
862{
863 /* Cannot copy a block if it has a fallthrough, since
864 * a block can only have one fallthrough predecessor.
865 */
866 assert(block->b_nofallthrough);
Mark Shannon762ef852021-08-09 10:54:48 +0100867 basicblock *result = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +0000868 if (result == NULL) {
869 return NULL;
870 }
871 for (int i = 0; i < block->b_iused; i++) {
872 int n = compiler_next_instr(result);
873 if (n < 0) {
874 return NULL;
875 }
876 result->b_instr[n] = block->b_instr[i];
877 }
878 result->b_exit = block->b_exit;
Mark Shannon3bd60352021-01-13 12:05:43 +0000879 result->b_nofallthrough = 1;
Mark Shannon5977a792020-12-02 13:31:40 +0000880 return result;
881}
882
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000883/* Returns the offset of the next instruction in the current block's
884 b_instr array. Resizes the b_instr as necessary.
885 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000886*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000887
888static int
Andy Lester76d58772020-03-10 21:18:12 -0500889compiler_next_instr(basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000890{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 assert(b != NULL);
892 if (b->b_instr == NULL) {
Andy Lester7668a8b2020-03-24 23:26:44 -0500893 b->b_instr = (struct instr *)PyObject_Calloc(
894 DEFAULT_BLOCK_SIZE, sizeof(struct instr));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 if (b->b_instr == NULL) {
896 PyErr_NoMemory();
897 return -1;
898 }
899 b->b_ialloc = DEFAULT_BLOCK_SIZE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 }
901 else if (b->b_iused == b->b_ialloc) {
902 struct instr *tmp;
903 size_t oldsize, newsize;
904 oldsize = b->b_ialloc * sizeof(struct instr);
905 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000906
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -0700907 if (oldsize > (SIZE_MAX >> 1)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 PyErr_NoMemory();
909 return -1;
910 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 if (newsize == 0) {
913 PyErr_NoMemory();
914 return -1;
915 }
916 b->b_ialloc <<= 1;
917 tmp = (struct instr *)PyObject_Realloc(
918 (void *)b->b_instr, newsize);
919 if (tmp == NULL) {
920 PyErr_NoMemory();
921 return -1;
922 }
923 b->b_instr = tmp;
924 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
925 }
926 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000927}
928
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200929/* Set the line number and column offset for the following instructions.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000930
Christian Heimes2202f872008-02-06 14:31:34 +0000931 The line number is reset in the following cases:
932 - when entering a new scope
933 - on each statement
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200934 - on each expression and sub-expression
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200935 - before the "except" and "finally" clauses
Thomas Wouters89f507f2006-12-13 04:49:30 +0000936*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000937
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200938#define SET_LOC(c, x) \
939 (c)->u->u_lineno = (x)->lineno; \
Pablo Galindoa77aac42021-04-23 14:27:05 +0100940 (c)->u->u_col_offset = (x)->col_offset; \
941 (c)->u->u_end_lineno = (x)->end_lineno; \
942 (c)->u->u_end_col_offset = (x)->end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000943
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200944/* Return the stack effect of opcode with argument oparg.
945
946 Some opcodes have different stack effect when jump to the target and
947 when not jump. The 'jump' parameter specifies the case:
948
949 * 0 -- when not jump
950 * 1 -- when jump
951 * -1 -- maximal
952 */
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200953static int
954stack_effect(int opcode, int oparg, int jump)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000955{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 switch (opcode) {
Serhiy Storchaka57faf342018-04-25 22:04:06 +0300957 case NOP:
958 case EXTENDED_ARG:
959 return 0;
960
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200961 /* Stack manipulation */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 case POP_TOP:
963 return -1;
964 case ROT_TWO:
965 case ROT_THREE:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200966 case ROT_FOUR:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 return 0;
968 case DUP_TOP:
969 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000970 case DUP_TOP_TWO:
971 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000972
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200973 /* Unary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 case UNARY_POSITIVE:
975 case UNARY_NEGATIVE:
976 case UNARY_NOT:
977 case UNARY_INVERT:
978 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 case SET_ADD:
981 case LIST_APPEND:
982 return -1;
983 case MAP_ADD:
984 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000985
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200986 /* Binary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 case BINARY_POWER:
988 case BINARY_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400989 case BINARY_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 case BINARY_MODULO:
991 case BINARY_ADD:
992 case BINARY_SUBTRACT:
993 case BINARY_SUBSCR:
994 case BINARY_FLOOR_DIVIDE:
995 case BINARY_TRUE_DIVIDE:
996 return -1;
997 case INPLACE_FLOOR_DIVIDE:
998 case INPLACE_TRUE_DIVIDE:
999 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 case INPLACE_ADD:
1002 case INPLACE_SUBTRACT:
1003 case INPLACE_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -04001004 case INPLACE_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 case INPLACE_MODULO:
1006 return -1;
1007 case STORE_SUBSCR:
1008 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 case DELETE_SUBSCR:
1010 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 case BINARY_LSHIFT:
1013 case BINARY_RSHIFT:
1014 case BINARY_AND:
1015 case BINARY_XOR:
1016 case BINARY_OR:
1017 return -1;
1018 case INPLACE_POWER:
1019 return -1;
1020 case GET_ITER:
1021 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 case PRINT_EXPR:
1024 return -1;
1025 case LOAD_BUILD_CLASS:
1026 return 1;
1027 case INPLACE_LSHIFT:
1028 case INPLACE_RSHIFT:
1029 case INPLACE_AND:
1030 case INPLACE_XOR:
1031 case INPLACE_OR:
1032 return -1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 case SETUP_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001035 /* 1 in the normal flow.
1036 * Restore the stack position and push 6 values before jumping to
1037 * the handler if an exception be raised. */
1038 return jump ? 6 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 case RETURN_VALUE:
1040 return -1;
1041 case IMPORT_STAR:
1042 return -1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001043 case SETUP_ANNOTATIONS:
1044 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 case YIELD_VALUE:
1046 return 0;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001047 case YIELD_FROM:
1048 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 case POP_BLOCK:
1050 return 0;
1051 case POP_EXCEPT:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001052 return -3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 case STORE_NAME:
1055 return -1;
1056 case DELETE_NAME:
1057 return 0;
1058 case UNPACK_SEQUENCE:
1059 return oparg-1;
1060 case UNPACK_EX:
1061 return (oparg&0xFF) + (oparg>>8);
1062 case FOR_ITER:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001063 /* -1 at end of iterator, 1 if continue iterating. */
1064 return jump > 0 ? -1 : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 case STORE_ATTR:
1067 return -2;
1068 case DELETE_ATTR:
1069 return -1;
1070 case STORE_GLOBAL:
1071 return -1;
1072 case DELETE_GLOBAL:
1073 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 case LOAD_CONST:
1075 return 1;
1076 case LOAD_NAME:
1077 return 1;
1078 case BUILD_TUPLE:
1079 case BUILD_LIST:
1080 case BUILD_SET:
Serhiy Storchakaea525a22016-09-06 22:07:53 +03001081 case BUILD_STRING:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 return 1-oparg;
1083 case BUILD_MAP:
Benjamin Petersonb6855152015-09-10 21:02:39 -07001084 return 1 - 2*oparg;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001085 case BUILD_CONST_KEY_MAP:
1086 return -oparg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 case LOAD_ATTR:
1088 return 0;
1089 case COMPARE_OP:
Mark Shannon9af0e472020-01-14 10:12:45 +00001090 case IS_OP:
1091 case CONTAINS_OP:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 return -1;
Mark Shannon9af0e472020-01-14 10:12:45 +00001093 case JUMP_IF_NOT_EXC_MATCH:
1094 return -2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 case IMPORT_NAME:
1096 return -1;
1097 case IMPORT_FROM:
1098 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001100 /* Jumps */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 case JUMP_FORWARD:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 case JUMP_ABSOLUTE:
1103 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001104
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001105 case JUMP_IF_TRUE_OR_POP:
1106 case JUMP_IF_FALSE_OR_POP:
1107 return jump ? 0 : -1;
1108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 case POP_JUMP_IF_FALSE:
1110 case POP_JUMP_IF_TRUE:
1111 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 case LOAD_GLOBAL:
1114 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001115
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001116 /* Exception handling */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 case SETUP_FINALLY:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001118 /* 0 in the normal flow.
1119 * Restore the stack position and push 6 values before jumping to
1120 * the handler if an exception be raised. */
1121 return jump ? 6 : 0;
Mark Shannonfee55262019-11-21 09:11:43 +00001122 case RERAISE:
1123 return -3;
1124
1125 case WITH_EXCEPT_START:
1126 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 case LOAD_FAST:
1129 return 1;
1130 case STORE_FAST:
1131 return -1;
1132 case DELETE_FAST:
1133 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 case RAISE_VARARGS:
1136 return -oparg;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001137
1138 /* Functions and calls */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 case CALL_FUNCTION:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001140 return -oparg;
Yury Selivanovf2392132016-12-13 19:03:51 -05001141 case CALL_METHOD:
1142 return -oparg-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 case CALL_FUNCTION_KW:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001144 return -oparg-1;
1145 case CALL_FUNCTION_EX:
Matthieu Dartiailh3a9ac822017-02-21 14:25:22 +01001146 return -1 - ((oparg & 0x01) != 0);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001147 case MAKE_FUNCTION:
1148 return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1149 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 case BUILD_SLICE:
1151 if (oparg == 3)
1152 return -2;
1153 else
1154 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001156 /* Closures */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 case LOAD_CLOSURE:
1158 return 1;
1159 case LOAD_DEREF:
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001160 case LOAD_CLASSDEREF:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 return 1;
1162 case STORE_DEREF:
1163 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001164 case DELETE_DEREF:
1165 return 0;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001166
1167 /* Iterators and generators */
Yury Selivanov75445082015-05-11 22:57:16 -04001168 case GET_AWAITABLE:
1169 return 0;
1170 case SETUP_ASYNC_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001171 /* 0 in the normal flow.
1172 * Restore the stack position to the position before the result
1173 * of __aenter__ and push 6 values before jumping to the handler
1174 * if an exception be raised. */
1175 return jump ? -1 + 6 : 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001176 case BEFORE_ASYNC_WITH:
1177 return 1;
1178 case GET_AITER:
1179 return 0;
1180 case GET_ANEXT:
1181 return 1;
Yury Selivanov5376ba92015-06-22 12:19:30 -04001182 case GET_YIELD_FROM_ITER:
1183 return 0;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02001184 case END_ASYNC_FOR:
1185 return -7;
Eric V. Smitha78c7952015-11-03 12:45:05 -05001186 case FORMAT_VALUE:
1187 /* If there's a fmt_spec on the stack, we go from 2->1,
1188 else 1->1. */
1189 return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
Yury Selivanovf2392132016-12-13 19:03:51 -05001190 case LOAD_METHOD:
1191 return 1;
Zackery Spytzce6a0702019-08-25 03:44:09 -06001192 case LOAD_ASSERTION_ERROR:
1193 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001194 case LIST_TO_TUPLE:
1195 return 0;
Mark Shannonb37181e2021-04-06 11:48:59 +01001196 case GEN_START:
1197 return -1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001198 case LIST_EXTEND:
1199 case SET_UPDATE:
Mark Shannon8a4cd702020-01-27 09:57:45 +00001200 case DICT_MERGE:
1201 case DICT_UPDATE:
Mark Shannon13bc1392020-01-23 09:25:17 +00001202 return -1;
Brandt Bucher145bf262021-02-26 14:51:55 -08001203 case COPY_DICT_WITHOUT_KEYS:
1204 return 0;
1205 case MATCH_CLASS:
1206 return -1;
1207 case GET_LEN:
1208 case MATCH_MAPPING:
1209 case MATCH_SEQUENCE:
1210 return 1;
1211 case MATCH_KEYS:
1212 return 2;
Brandt Bucher0ad1e032021-05-02 13:02:10 -07001213 case ROT_N:
1214 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 default:
Larry Hastings3a907972013-11-23 14:49:22 -08001216 return PY_INVALID_STACK_EFFECT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 }
Larry Hastings3a907972013-11-23 14:49:22 -08001218 return PY_INVALID_STACK_EFFECT; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219}
1220
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001221int
Serhiy Storchaka7bdf2822018-09-18 09:54:26 +03001222PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump)
1223{
1224 return stack_effect(opcode, oparg, jump);
1225}
1226
1227int
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001228PyCompile_OpcodeStackEffect(int opcode, int oparg)
1229{
1230 return stack_effect(opcode, oparg, -1);
1231}
1232
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233/* Add an opcode with no argument.
1234 Returns 0 on failure, 1 on success.
1235*/
1236
1237static int
Mark Shannon3bd60352021-01-13 12:05:43 +00001238compiler_addop_line(struct compiler *c, int opcode, int line)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 basicblock *b;
1241 struct instr *i;
1242 int off;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001243 assert(!HAS_ARG(opcode));
Andy Lester76d58772020-03-10 21:18:12 -05001244 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 if (off < 0)
1246 return 0;
1247 b = c->u->u_curblock;
1248 i = &b->b_instr[off];
1249 i->i_opcode = opcode;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001250 i->i_oparg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 if (opcode == RETURN_VALUE)
1252 b->b_return = 1;
Mark Shannon3bd60352021-01-13 12:05:43 +00001253 i->i_lineno = line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001255}
1256
Mark Shannon3bd60352021-01-13 12:05:43 +00001257static int
1258compiler_addop(struct compiler *c, int opcode)
1259{
1260 return compiler_addop_line(c, opcode, c->u->u_lineno);
1261}
1262
1263static int
1264compiler_addop_noline(struct compiler *c, int opcode)
1265{
1266 return compiler_addop_line(c, opcode, -1);
1267}
1268
1269
Victor Stinnerf8e32212013-11-19 23:56:34 +01001270static Py_ssize_t
Andy Lester76d58772020-03-10 21:18:12 -05001271compiler_add_o(PyObject *dict, PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001273 PyObject *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001275
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001276 v = PyDict_GetItemWithError(dict, o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 if (!v) {
Stefan Krahc0cbed12015-07-27 12:56:49 +02001278 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 return -1;
Stefan Krahc0cbed12015-07-27 12:56:49 +02001280 }
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001281 arg = PyDict_GET_SIZE(dict);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001282 v = PyLong_FromSsize_t(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 if (!v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 return -1;
1285 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001286 if (PyDict_SetItem(dict, o, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 Py_DECREF(v);
1288 return -1;
1289 }
1290 Py_DECREF(v);
1291 }
1292 else
1293 arg = PyLong_AsLong(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001294 return arg;
1295}
1296
INADA Naokic2e16072018-11-26 21:23:22 +09001297// Merge const *o* recursively and return constant key object.
1298static PyObject*
1299merge_consts_recursive(struct compiler *c, PyObject *o)
1300{
1301 // None and Ellipsis are singleton, and key is the singleton.
1302 // No need to merge object and key.
1303 if (o == Py_None || o == Py_Ellipsis) {
1304 Py_INCREF(o);
1305 return o;
1306 }
1307
1308 PyObject *key = _PyCode_ConstantKey(o);
1309 if (key == NULL) {
1310 return NULL;
1311 }
1312
1313 // t is borrowed reference
1314 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
1315 if (t != key) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001316 // o is registered in c_const_cache. Just use it.
Zackery Spytz9b4a1b12019-03-20 03:16:25 -06001317 Py_XINCREF(t);
INADA Naokic2e16072018-11-26 21:23:22 +09001318 Py_DECREF(key);
1319 return t;
1320 }
1321
INADA Naokif7e4d362018-11-29 00:58:46 +09001322 // We registered o in c_const_cache.
Simeon63b5fc52019-04-09 19:36:57 -04001323 // When o is a tuple or frozenset, we want to merge its
INADA Naokif7e4d362018-11-29 00:58:46 +09001324 // items too.
INADA Naokic2e16072018-11-26 21:23:22 +09001325 if (PyTuple_CheckExact(o)) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001326 Py_ssize_t len = PyTuple_GET_SIZE(o);
1327 for (Py_ssize_t i = 0; i < len; i++) {
INADA Naokic2e16072018-11-26 21:23:22 +09001328 PyObject *item = PyTuple_GET_ITEM(o, i);
1329 PyObject *u = merge_consts_recursive(c, item);
1330 if (u == NULL) {
1331 Py_DECREF(key);
1332 return NULL;
1333 }
1334
1335 // See _PyCode_ConstantKey()
1336 PyObject *v; // borrowed
1337 if (PyTuple_CheckExact(u)) {
1338 v = PyTuple_GET_ITEM(u, 1);
1339 }
1340 else {
1341 v = u;
1342 }
1343 if (v != item) {
1344 Py_INCREF(v);
1345 PyTuple_SET_ITEM(o, i, v);
1346 Py_DECREF(item);
1347 }
1348
1349 Py_DECREF(u);
1350 }
1351 }
INADA Naokif7e4d362018-11-29 00:58:46 +09001352 else if (PyFrozenSet_CheckExact(o)) {
Simeon63b5fc52019-04-09 19:36:57 -04001353 // *key* is tuple. And its first item is frozenset of
INADA Naokif7e4d362018-11-29 00:58:46 +09001354 // constant keys.
1355 // See _PyCode_ConstantKey() for detail.
1356 assert(PyTuple_CheckExact(key));
1357 assert(PyTuple_GET_SIZE(key) == 2);
1358
1359 Py_ssize_t len = PySet_GET_SIZE(o);
1360 if (len == 0) { // empty frozenset should not be re-created.
1361 return key;
1362 }
1363 PyObject *tuple = PyTuple_New(len);
1364 if (tuple == NULL) {
1365 Py_DECREF(key);
1366 return NULL;
1367 }
1368 Py_ssize_t i = 0, pos = 0;
1369 PyObject *item;
1370 Py_hash_t hash;
1371 while (_PySet_NextEntry(o, &pos, &item, &hash)) {
1372 PyObject *k = merge_consts_recursive(c, item);
1373 if (k == NULL) {
1374 Py_DECREF(tuple);
1375 Py_DECREF(key);
1376 return NULL;
1377 }
1378 PyObject *u;
1379 if (PyTuple_CheckExact(k)) {
1380 u = PyTuple_GET_ITEM(k, 1);
1381 Py_INCREF(u);
1382 Py_DECREF(k);
1383 }
1384 else {
1385 u = k;
1386 }
1387 PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u.
1388 i++;
1389 }
1390
1391 // Instead of rewriting o, we create new frozenset and embed in the
1392 // key tuple. Caller should get merged frozenset from the key tuple.
1393 PyObject *new = PyFrozenSet_New(tuple);
1394 Py_DECREF(tuple);
1395 if (new == NULL) {
1396 Py_DECREF(key);
1397 return NULL;
1398 }
1399 assert(PyTuple_GET_ITEM(key, 1) == o);
1400 Py_DECREF(o);
1401 PyTuple_SET_ITEM(key, 1, new);
1402 }
INADA Naokic2e16072018-11-26 21:23:22 +09001403
1404 return key;
1405}
1406
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001407static Py_ssize_t
1408compiler_add_const(struct compiler *c, PyObject *o)
1409{
INADA Naokic2e16072018-11-26 21:23:22 +09001410 PyObject *key = merge_consts_recursive(c, o);
1411 if (key == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001412 return -1;
INADA Naokic2e16072018-11-26 21:23:22 +09001413 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001414
Andy Lester76d58772020-03-10 21:18:12 -05001415 Py_ssize_t arg = compiler_add_o(c->u->u_consts, key);
INADA Naokic2e16072018-11-26 21:23:22 +09001416 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418}
1419
1420static int
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001421compiler_addop_load_const(struct compiler *c, PyObject *o)
1422{
1423 Py_ssize_t arg = compiler_add_const(c, o);
1424 if (arg < 0)
1425 return 0;
1426 return compiler_addop_i(c, LOAD_CONST, arg);
1427}
1428
1429static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432{
Andy Lester76d58772020-03-10 21:18:12 -05001433 Py_ssize_t arg = compiler_add_o(dict, o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001434 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001435 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436 return compiler_addop_i(c, opcode, arg);
1437}
1438
1439static int
1440compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001443 Py_ssize_t arg;
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001444
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001445 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1446 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001447 return 0;
Andy Lester76d58772020-03-10 21:18:12 -05001448 arg = compiler_add_o(dict, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449 Py_DECREF(mangled);
1450 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001451 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452 return compiler_addop_i(c, opcode, arg);
1453}
1454
1455/* Add an opcode with an integer argument.
1456 Returns 0 on failure, 1 on success.
1457*/
1458
1459static int
Mark Shannon11e0b292021-04-15 14:28:56 +01001460compiler_addop_i_line(struct compiler *c, int opcode, Py_ssize_t oparg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 struct instr *i;
1463 int off;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001464
Victor Stinner2ad474b2016-03-01 23:34:47 +01001465 /* oparg value is unsigned, but a signed C int is usually used to store
1466 it in the C code (like Python/ceval.c).
1467
1468 Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1469
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001470 The argument of a concrete bytecode instruction is limited to 8-bit.
1471 EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1472 assert(HAS_ARG(opcode));
Victor Stinner2ad474b2016-03-01 23:34:47 +01001473 assert(0 <= oparg && oparg <= 2147483647);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001474
Andy Lester76d58772020-03-10 21:18:12 -05001475 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 if (off < 0)
1477 return 0;
1478 i = &c->u->u_curblock->b_instr[off];
Victor Stinnerf8e32212013-11-19 23:56:34 +01001479 i->i_opcode = opcode;
1480 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
Mark Shannon11e0b292021-04-15 14:28:56 +01001481 i->i_lineno = lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001483}
1484
Mark Shannon11e0b292021-04-15 14:28:56 +01001485static int
1486compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
1487{
1488 return compiler_addop_i_line(c, opcode, oparg, c->u->u_lineno);
1489}
1490
1491static int
1492compiler_addop_i_noline(struct compiler *c, int opcode, Py_ssize_t oparg)
1493{
1494 return compiler_addop_i_line(c, opcode, oparg, -1);
1495}
1496
Mark Shannon28b75c82020-12-23 11:43:10 +00001497static int add_jump_to_block(basicblock *b, int opcode, int lineno, basicblock *target)
1498{
1499 assert(HAS_ARG(opcode));
1500 assert(b != NULL);
1501 assert(target != NULL);
1502
1503 int off = compiler_next_instr(b);
1504 struct instr *i = &b->b_instr[off];
1505 if (off < 0) {
1506 return 0;
1507 }
1508 i->i_opcode = opcode;
1509 i->i_target = target;
1510 i->i_lineno = lineno;
1511 return 1;
1512}
1513
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514static int
Mark Shannon582aaf12020-08-04 17:30:11 +01001515compiler_addop_j(struct compiler *c, int opcode, basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516{
Mark Shannon28b75c82020-12-23 11:43:10 +00001517 return add_jump_to_block(c->u->u_curblock, opcode, c->u->u_lineno, b);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001518}
1519
Mark Shannon127dde52021-01-04 18:06:55 +00001520static int
1521compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b)
1522{
1523 return add_jump_to_block(c->u->u_curblock, opcode, -1, b);
1524}
1525
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +01001526/* NEXT_BLOCK() creates an implicit jump from the current block
1527 to the new block.
1528
1529 The returns inside this macro make it impossible to decref objects
1530 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 if (compiler_next_block((C)) == NULL) \
1534 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535}
1536
1537#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 if (!compiler_addop((C), (OP))) \
1539 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001540}
1541
Mark Shannon3bd60352021-01-13 12:05:43 +00001542#define ADDOP_NOLINE(C, OP) { \
1543 if (!compiler_addop_noline((C), (OP))) \
1544 return 0; \
1545}
1546
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001547#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 if (!compiler_addop((C), (OP))) { \
1549 compiler_exit_scope(c); \
1550 return 0; \
1551 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001552}
1553
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001554#define ADDOP_LOAD_CONST(C, O) { \
1555 if (!compiler_addop_load_const((C), (O))) \
1556 return 0; \
1557}
1558
1559/* Same as ADDOP_LOAD_CONST, but steals a reference. */
1560#define ADDOP_LOAD_CONST_NEW(C, O) { \
1561 PyObject *__new_const = (O); \
1562 if (__new_const == NULL) { \
1563 return 0; \
1564 } \
1565 if (!compiler_addop_load_const((C), __new_const)) { \
1566 Py_DECREF(__new_const); \
1567 return 0; \
1568 } \
1569 Py_DECREF(__new_const); \
1570}
1571
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001572#define ADDOP_O(C, OP, O, TYPE) { \
Miss Islington (bot)ebbd0ac2021-08-31 11:08:32 -07001573 assert((OP) != LOAD_CONST); /* use ADDOP_LOAD_CONST */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1575 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001576}
1577
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001578/* Same as ADDOP_O, but steals a reference. */
1579#define ADDOP_N(C, OP, O, TYPE) { \
Miss Islington (bot)ebbd0ac2021-08-31 11:08:32 -07001580 assert((OP) != LOAD_CONST); /* use ADDOP_LOAD_CONST_NEW */ \
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001581 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1582 Py_DECREF((O)); \
1583 return 0; \
1584 } \
1585 Py_DECREF((O)); \
1586}
1587
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001588#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1590 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591}
1592
1593#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 if (!compiler_addop_i((C), (OP), (O))) \
1595 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596}
1597
Mark Shannon11e0b292021-04-15 14:28:56 +01001598#define ADDOP_I_NOLINE(C, OP, O) { \
1599 if (!compiler_addop_i_noline((C), (OP), (O))) \
1600 return 0; \
1601}
1602
Mark Shannon582aaf12020-08-04 17:30:11 +01001603#define ADDOP_JUMP(C, OP, O) { \
1604 if (!compiler_addop_j((C), (OP), (O))) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001606}
1607
Mark Shannon127dde52021-01-04 18:06:55 +00001608/* Add a jump with no line number.
1609 * Used for artificial jumps that have no corresponding
1610 * token in the source code. */
1611#define ADDOP_JUMP_NOLINE(C, OP, O) { \
1612 if (!compiler_addop_j_noline((C), (OP), (O))) \
1613 return 0; \
1614}
1615
Mark Shannon9af0e472020-01-14 10:12:45 +00001616#define ADDOP_COMPARE(C, CMP) { \
1617 if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \
1618 return 0; \
1619}
1620
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001621/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1622 the ASDL name to synthesize the name of the C type and the visit function.
1623*/
1624
1625#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 if (!compiler_visit_ ## TYPE((C), (V))) \
1627 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001628}
1629
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001630#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 if (!compiler_visit_ ## TYPE((C), (V))) { \
1632 compiler_exit_scope(c); \
1633 return 0; \
1634 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001635}
1636
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001637#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 if (!compiler_visit_slice((C), (V), (CTX))) \
1639 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001640}
1641
1642#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001644 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1646 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1647 if (!compiler_visit_ ## TYPE((C), elt)) \
1648 return 0; \
1649 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001650}
1651
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001652#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001654 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1656 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1657 if (!compiler_visit_ ## TYPE((C), elt)) { \
1658 compiler_exit_scope(c); \
1659 return 0; \
1660 } \
1661 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001662}
1663
Brandt Bucher145bf262021-02-26 14:51:55 -08001664#define RETURN_IF_FALSE(X) \
1665 if (!(X)) { \
1666 return 0; \
1667 }
1668
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001669/* Search if variable annotations are present statically in a block. */
1670
1671static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001672find_ann(asdl_stmt_seq *stmts)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001673{
1674 int i, j, res = 0;
1675 stmt_ty st;
1676
1677 for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1678 st = (stmt_ty)asdl_seq_GET(stmts, i);
1679 switch (st->kind) {
1680 case AnnAssign_kind:
1681 return 1;
1682 case For_kind:
1683 res = find_ann(st->v.For.body) ||
1684 find_ann(st->v.For.orelse);
1685 break;
1686 case AsyncFor_kind:
1687 res = find_ann(st->v.AsyncFor.body) ||
1688 find_ann(st->v.AsyncFor.orelse);
1689 break;
1690 case While_kind:
1691 res = find_ann(st->v.While.body) ||
1692 find_ann(st->v.While.orelse);
1693 break;
1694 case If_kind:
1695 res = find_ann(st->v.If.body) ||
1696 find_ann(st->v.If.orelse);
1697 break;
1698 case With_kind:
1699 res = find_ann(st->v.With.body);
1700 break;
1701 case AsyncWith_kind:
1702 res = find_ann(st->v.AsyncWith.body);
1703 break;
1704 case Try_kind:
1705 for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1706 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1707 st->v.Try.handlers, j);
1708 if (find_ann(handler->v.ExceptHandler.body)) {
1709 return 1;
1710 }
1711 }
1712 res = find_ann(st->v.Try.body) ||
1713 find_ann(st->v.Try.finalbody) ||
1714 find_ann(st->v.Try.orelse);
1715 break;
1716 default:
1717 res = 0;
1718 }
1719 if (res) {
1720 break;
1721 }
1722 }
1723 return res;
1724}
1725
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001726/*
1727 * Frame block handling functions
1728 */
1729
1730static int
1731compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b,
Mark Shannonfee55262019-11-21 09:11:43 +00001732 basicblock *exit, void *datum)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001733{
1734 struct fblockinfo *f;
1735 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
Mark Shannon02d126a2020-09-25 14:04:19 +01001736 return compiler_error(c, "too many statically nested blocks");
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001737 }
1738 f = &c->u->u_fblock[c->u->u_nfblocks++];
1739 f->fb_type = t;
1740 f->fb_block = b;
1741 f->fb_exit = exit;
Mark Shannonfee55262019-11-21 09:11:43 +00001742 f->fb_datum = datum;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001743 return 1;
1744}
1745
1746static void
1747compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
1748{
1749 struct compiler_unit *u = c->u;
1750 assert(u->u_nfblocks > 0);
1751 u->u_nfblocks--;
1752 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
1753 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
1754}
1755
Mark Shannonfee55262019-11-21 09:11:43 +00001756static int
1757compiler_call_exit_with_nones(struct compiler *c) {
Miss Islington (bot)ebbd0ac2021-08-31 11:08:32 -07001758 ADDOP_LOAD_CONST(c, Py_None);
Mark Shannonfee55262019-11-21 09:11:43 +00001759 ADDOP(c, DUP_TOP);
1760 ADDOP(c, DUP_TOP);
1761 ADDOP_I(c, CALL_FUNCTION, 3);
1762 return 1;
1763}
1764
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001765/* Unwind a frame block. If preserve_tos is true, the TOS before
Mark Shannonfee55262019-11-21 09:11:43 +00001766 * popping the blocks will be restored afterwards, unless another
1767 * return, break or continue is found. In which case, the TOS will
1768 * be popped.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001769 */
1770static int
1771compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
1772 int preserve_tos)
1773{
1774 switch (info->fb_type) {
1775 case WHILE_LOOP:
Mark Shannon02d126a2020-09-25 14:04:19 +01001776 case EXCEPTION_HANDLER:
tomKPZ7a7ba3d2021-04-07 07:43:45 -07001777 case ASYNC_COMPREHENSION_GENERATOR:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001778 return 1;
1779
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001780 case FOR_LOOP:
1781 /* Pop the iterator */
1782 if (preserve_tos) {
1783 ADDOP(c, ROT_TWO);
1784 }
1785 ADDOP(c, POP_TOP);
1786 return 1;
1787
Mark Shannon02d126a2020-09-25 14:04:19 +01001788 case TRY_EXCEPT:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001789 ADDOP(c, POP_BLOCK);
1790 return 1;
1791
1792 case FINALLY_TRY:
Mark Shannon5274b682020-12-16 13:07:01 +00001793 /* This POP_BLOCK gets the line number of the unwinding statement */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001794 ADDOP(c, POP_BLOCK);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001795 if (preserve_tos) {
Mark Shannonfee55262019-11-21 09:11:43 +00001796 if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) {
1797 return 0;
1798 }
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001799 }
Mark Shannon5274b682020-12-16 13:07:01 +00001800 /* Emit the finally block */
Mark Shannonfee55262019-11-21 09:11:43 +00001801 VISIT_SEQ(c, stmt, info->fb_datum);
1802 if (preserve_tos) {
1803 compiler_pop_fblock(c, POP_VALUE, NULL);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001804 }
Mark Shannon5274b682020-12-16 13:07:01 +00001805 /* The finally block should appear to execute after the
1806 * statement causing the unwinding, so make the unwinding
1807 * instruction artificial */
1808 c->u->u_lineno = -1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001809 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001810
Mark Shannonfee55262019-11-21 09:11:43 +00001811 case FINALLY_END:
1812 if (preserve_tos) {
1813 ADDOP(c, ROT_FOUR);
1814 }
1815 ADDOP(c, POP_TOP);
1816 ADDOP(c, POP_TOP);
1817 ADDOP(c, POP_TOP);
1818 if (preserve_tos) {
1819 ADDOP(c, ROT_FOUR);
1820 }
1821 ADDOP(c, POP_EXCEPT);
1822 return 1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001823
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001824 case WITH:
1825 case ASYNC_WITH:
Mark Shannon5979e812021-04-30 14:32:47 +01001826 SET_LOC(c, (stmt_ty)info->fb_datum);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001827 ADDOP(c, POP_BLOCK);
1828 if (preserve_tos) {
1829 ADDOP(c, ROT_TWO);
1830 }
Mark Shannonfee55262019-11-21 09:11:43 +00001831 if(!compiler_call_exit_with_nones(c)) {
1832 return 0;
1833 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001834 if (info->fb_type == ASYNC_WITH) {
1835 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001836 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001837 ADDOP(c, YIELD_FROM);
1838 }
Mark Shannonfee55262019-11-21 09:11:43 +00001839 ADDOP(c, POP_TOP);
Mark Shannoncea05852021-06-03 19:57:31 +01001840 /* The exit block should appear to execute after the
1841 * statement causing the unwinding, so make the unwinding
1842 * instruction artificial */
1843 c->u->u_lineno = -1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001844 return 1;
1845
1846 case HANDLER_CLEANUP:
Mark Shannonfee55262019-11-21 09:11:43 +00001847 if (info->fb_datum) {
1848 ADDOP(c, POP_BLOCK);
1849 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001850 if (preserve_tos) {
1851 ADDOP(c, ROT_FOUR);
1852 }
Mark Shannonfee55262019-11-21 09:11:43 +00001853 ADDOP(c, POP_EXCEPT);
1854 if (info->fb_datum) {
1855 ADDOP_LOAD_CONST(c, Py_None);
1856 compiler_nameop(c, info->fb_datum, Store);
1857 compiler_nameop(c, info->fb_datum, Del);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001858 }
Mark Shannonfee55262019-11-21 09:11:43 +00001859 return 1;
1860
1861 case POP_VALUE:
1862 if (preserve_tos) {
1863 ADDOP(c, ROT_TWO);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001864 }
Mark Shannonfee55262019-11-21 09:11:43 +00001865 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001866 return 1;
1867 }
1868 Py_UNREACHABLE();
1869}
1870
Mark Shannonfee55262019-11-21 09:11:43 +00001871/** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */
1872static int
1873compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) {
1874 if (c->u->u_nfblocks == 0) {
1875 return 1;
1876 }
1877 struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1];
1878 if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) {
1879 *loop = top;
1880 return 1;
1881 }
1882 struct fblockinfo copy = *top;
1883 c->u->u_nfblocks--;
1884 if (!compiler_unwind_fblock(c, &copy, preserve_tos)) {
1885 return 0;
1886 }
1887 if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) {
1888 return 0;
1889 }
1890 c->u->u_fblock[c->u->u_nfblocks] = copy;
1891 c->u->u_nfblocks++;
1892 return 1;
1893}
1894
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001895/* Compile a sequence of statements, checking for a docstring
1896 and for annotations. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897
1898static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001899compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001900{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001901 int i = 0;
1902 stmt_ty st;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001903 PyObject *docstring;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001904
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001905 /* Set current line number to the line number of first statement.
1906 This way line number for SETUP_ANNOTATIONS will always
1907 coincide with the line number of first "real" statement in module.
Hansraj Das01171eb2019-10-09 07:54:02 +05301908 If body is empty, then lineno will be set later in assemble. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001909 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) {
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001910 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001911 SET_LOC(c, st);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001912 }
1913 /* Every annotated class and module should have __annotations__. */
1914 if (find_ann(stmts)) {
1915 ADDOP(c, SETUP_ANNOTATIONS);
1916 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001917 if (!asdl_seq_LEN(stmts))
1918 return 1;
INADA Naokicb41b272017-02-23 00:31:59 +09001919 /* if not -OO mode, set docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001920 if (c->c_optimize < 2) {
1921 docstring = _PyAST_GetDocString(stmts);
1922 if (docstring) {
1923 i = 1;
1924 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1925 assert(st->kind == Expr_kind);
1926 VISIT(c, expr, st->v.Expr.value);
1927 if (!compiler_nameop(c, __doc__, Store))
1928 return 0;
1929 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001931 for (; i < asdl_seq_LEN(stmts); i++)
1932 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001934}
1935
1936static PyCodeObject *
1937compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001938{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 PyCodeObject *co;
1940 int addNone = 1;
1941 static PyObject *module;
1942 if (!module) {
1943 module = PyUnicode_InternFromString("<module>");
1944 if (!module)
1945 return NULL;
1946 }
1947 /* Use 0 for firstlineno initially, will fixup in assemble(). */
Mark Shannon877df852020-11-12 09:43:29 +00001948 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 return NULL;
1950 switch (mod->kind) {
1951 case Module_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001952 if (!compiler_body(c, mod->v.Module.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 compiler_exit_scope(c);
1954 return 0;
1955 }
1956 break;
1957 case Interactive_kind:
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001958 if (find_ann(mod->v.Interactive.body)) {
1959 ADDOP(c, SETUP_ANNOTATIONS);
1960 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 c->c_interactive = 1;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001962 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 break;
1964 case Expression_kind:
1965 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1966 addNone = 0;
1967 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 default:
1969 PyErr_Format(PyExc_SystemError,
1970 "module kind %d should not be possible",
1971 mod->kind);
1972 return 0;
1973 }
1974 co = assemble(c, addNone);
1975 compiler_exit_scope(c);
1976 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001977}
1978
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001979/* The test for LOCAL must come before the test for FREE in order to
1980 handle classes where name is both local and free. The local var is
1981 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001982*/
1983
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001984static int
1985get_ref_type(struct compiler *c, PyObject *name)
1986{
Victor Stinner0b1bc562013-05-16 22:17:17 +02001987 int scope;
Benjamin Peterson312595c2013-05-15 15:26:42 -05001988 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001989 _PyUnicode_EqualToASCIIString(name, "__class__"))
Benjamin Peterson312595c2013-05-15 15:26:42 -05001990 return CELL;
Victor Stinner28ad12f2021-03-19 12:41:49 +01001991 scope = _PyST_GetScope(c->u->u_ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 if (scope == 0) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001993 PyErr_Format(PyExc_SystemError,
Victor Stinner28ad12f2021-03-19 12:41:49 +01001994 "_PyST_GetScope(name=%R) failed: "
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001995 "unknown scope in unit %S (%R); "
1996 "symbols: %R; locals: %R; globals: %R",
1997 name,
1998 c->u->u_name, c->u->u_ste->ste_id,
1999 c->u->u_ste->ste_symbols, c->u->u_varnames, c->u->u_names);
2000 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002002 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003}
2004
2005static int
2006compiler_lookup_arg(PyObject *dict, PyObject *name)
2007{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002008 PyObject *v;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02002009 v = PyDict_GetItemWithError(dict, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00002011 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00002012 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002013}
2014
2015static int
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002016compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags,
2017 PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002018{
Victor Stinnerad9a0662013-11-19 22:23:20 +01002019 Py_ssize_t i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002020 if (qualname == NULL)
2021 qualname = co->co_name;
2022
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002023 if (free) {
2024 for (i = 0; i < free; ++i) {
2025 /* Bypass com_addop_varname because it will generate
2026 LOAD_DEREF but LOAD_CLOSURE is needed.
2027 */
2028 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002030 /* Special case: If a class contains a method with a
2031 free variable that has the same name as a method,
2032 the name will be considered free *and* local in the
2033 class. It should be handled by the closure, as
Min ho Kimc4cacc82019-07-31 08:16:13 +10002034 well as by the normal name lookup logic.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002035 */
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002036 int reftype = get_ref_type(c, name);
2037 if (reftype == -1) {
2038 return 0;
2039 }
2040 int arg;
2041 if (reftype == CELL) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002042 arg = compiler_lookup_arg(c->u->u_cellvars, name);
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002043 }
2044 else {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002045 arg = compiler_lookup_arg(c->u->u_freevars, name);
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002046 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002047 if (arg == -1) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002048 PyErr_Format(PyExc_SystemError,
2049 "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; "
2050 "freevars of code %S: %R",
2051 name,
2052 reftype,
2053 c->u->u_name,
2054 co->co_name,
2055 co->co_freevars);
2056 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002057 }
2058 ADDOP_I(c, LOAD_CLOSURE, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002060 flags |= 0x08;
2061 ADDOP_I(c, BUILD_TUPLE, free);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002063 ADDOP_LOAD_CONST(c, (PyObject*)co);
2064 ADDOP_LOAD_CONST(c, qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002065 ADDOP_I(c, MAKE_FUNCTION, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002067}
2068
2069static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002070compiler_decorators(struct compiler *c, asdl_expr_seq* decos)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002071{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 if (!decos)
2075 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2078 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
2079 }
2080 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081}
2082
2083static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002084compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs,
2085 asdl_expr_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00002086{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002087 /* Push a dict of keyword-only default values.
2088
2089 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
2090 */
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002091 int i;
2092 PyObject *keys = NULL;
2093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
2095 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
2096 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
2097 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04002098 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002099 if (!mangled) {
2100 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002102 if (keys == NULL) {
2103 keys = PyList_New(1);
2104 if (keys == NULL) {
2105 Py_DECREF(mangled);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002106 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002107 }
2108 PyList_SET_ITEM(keys, 0, mangled);
2109 }
2110 else {
2111 int res = PyList_Append(keys, mangled);
2112 Py_DECREF(mangled);
2113 if (res == -1) {
2114 goto error;
2115 }
2116 }
2117 if (!compiler_visit_expr(c, default_)) {
2118 goto error;
2119 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002120 }
2121 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002122 if (keys != NULL) {
2123 Py_ssize_t default_count = PyList_GET_SIZE(keys);
2124 PyObject *keys_tuple = PyList_AsTuple(keys);
2125 Py_DECREF(keys);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002126 ADDOP_LOAD_CONST_NEW(c, keys_tuple);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002127 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002128 assert(default_count > 0);
2129 return 1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002130 }
2131 else {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002132 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002133 }
2134
2135error:
2136 Py_XDECREF(keys);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002137 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002138}
2139
2140static int
Guido van Rossum95e4d582018-01-26 08:20:18 -08002141compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
2142{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +03002143 ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation));
Guido van Rossum95e4d582018-01-26 08:20:18 -08002144 return 1;
2145}
2146
2147static int
Neal Norwitzc1505362006-12-28 06:47:50 +00002148compiler_visit_argannotation(struct compiler *c, identifier id,
Yurii Karabas73019792020-11-25 12:43:18 +02002149 expr_ty annotation, Py_ssize_t *annotations_len)
Neal Norwitzc1505362006-12-28 06:47:50 +00002150{
Pablo Galindob0544ba2021-04-21 12:41:19 +01002151 if (!annotation) {
2152 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153 }
Pablo Galindob0544ba2021-04-21 12:41:19 +01002154
2155 PyObject *mangled = _Py_Mangle(c->u->u_private, id);
2156 if (!mangled) {
2157 return 0;
2158 }
2159 ADDOP_LOAD_CONST(c, mangled);
2160 Py_DECREF(mangled);
2161
2162 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
2163 VISIT(c, annexpr, annotation)
2164 }
2165 else {
2166 VISIT(c, expr, annotation);
2167 }
2168 *annotations_len += 2;
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002169 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002170}
2171
2172static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002173compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args,
Yurii Karabas73019792020-11-25 12:43:18 +02002174 Py_ssize_t *annotations_len)
Neal Norwitzc1505362006-12-28 06:47:50 +00002175{
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002176 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177 for (i = 0; i < asdl_seq_LEN(args); i++) {
2178 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002179 if (!compiler_visit_argannotation(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 c,
2181 arg->arg,
2182 arg->annotation,
Yurii Karabas73019792020-11-25 12:43:18 +02002183 annotations_len))
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002184 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002186 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002187}
2188
2189static int
2190compiler_visit_annotations(struct compiler *c, arguments_ty args,
2191 expr_ty returns)
2192{
Yurii Karabas73019792020-11-25 12:43:18 +02002193 /* Push arg annotation names and values.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002194 The expressions are evaluated out-of-order wrt the source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00002195
Yurii Karabas73019792020-11-25 12:43:18 +02002196 Return 0 on error, -1 if no annotations pushed, 1 if a annotations is pushed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 */
2198 static identifier return_str;
Yurii Karabas73019792020-11-25 12:43:18 +02002199 Py_ssize_t annotations_len = 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002200
Yurii Karabas73019792020-11-25 12:43:18 +02002201 if (!compiler_visit_argannotations(c, args->args, &annotations_len))
2202 return 0;
2203 if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len))
2204 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002205 if (args->vararg && args->vararg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002206 !compiler_visit_argannotation(c, args->vararg->arg,
Yurii Karabas73019792020-11-25 12:43:18 +02002207 args->vararg->annotation, &annotations_len))
2208 return 0;
2209 if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len))
2210 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002211 if (args->kwarg && args->kwarg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002212 !compiler_visit_argannotation(c, args->kwarg->arg,
Yurii Karabas73019792020-11-25 12:43:18 +02002213 args->kwarg->annotation, &annotations_len))
2214 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 if (!return_str) {
2217 return_str = PyUnicode_InternFromString("return");
2218 if (!return_str)
Yurii Karabas73019792020-11-25 12:43:18 +02002219 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 }
Yurii Karabas73019792020-11-25 12:43:18 +02002221 if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) {
2222 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 }
2224
Yurii Karabas73019792020-11-25 12:43:18 +02002225 if (annotations_len) {
2226 ADDOP_I(c, BUILD_TUPLE, annotations_len);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002227 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002229
Yurii Karabas73019792020-11-25 12:43:18 +02002230 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002231}
2232
2233static int
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002234compiler_visit_defaults(struct compiler *c, arguments_ty args)
2235{
2236 VISIT_SEQ(c, expr, args->defaults);
2237 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
2238 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002239}
2240
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002241static Py_ssize_t
2242compiler_default_arguments(struct compiler *c, arguments_ty args)
2243{
2244 Py_ssize_t funcflags = 0;
2245 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002246 if (!compiler_visit_defaults(c, args))
2247 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002248 funcflags |= 0x01;
2249 }
2250 if (args->kwonlyargs) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002251 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002252 args->kw_defaults);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002253 if (res == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002254 return -1;
2255 }
2256 else if (res > 0) {
2257 funcflags |= 0x02;
2258 }
2259 }
2260 return funcflags;
2261}
2262
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002263static int
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002264forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx)
2265{
2266
2267 if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2268 compiler_error(c, "cannot assign to __debug__");
2269 return 1;
2270 }
Dong-hee Na32c1caa2021-08-26 09:52:21 +00002271 if (ctx == Del && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2272 compiler_error(c, "cannot delete __debug__");
2273 return 1;
2274 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002275 return 0;
2276}
2277
2278static int
2279compiler_check_debug_one_arg(struct compiler *c, arg_ty arg)
2280{
2281 if (arg != NULL) {
2282 if (forbidden_name(c, arg->arg, Store))
2283 return 0;
2284 }
2285 return 1;
2286}
2287
2288static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002289compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args)
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002290{
2291 if (args != NULL) {
Pablo Galindoee40e4b2020-04-23 03:43:08 +01002292 for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002293 if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i)))
2294 return 0;
2295 }
2296 }
2297 return 1;
2298}
2299
2300static int
2301compiler_check_debug_args(struct compiler *c, arguments_ty args)
2302{
2303 if (!compiler_check_debug_args_seq(c, args->posonlyargs))
2304 return 0;
2305 if (!compiler_check_debug_args_seq(c, args->args))
2306 return 0;
2307 if (!compiler_check_debug_one_arg(c, args->vararg))
2308 return 0;
2309 if (!compiler_check_debug_args_seq(c, args->kwonlyargs))
2310 return 0;
2311 if (!compiler_check_debug_one_arg(c, args->kwarg))
2312 return 0;
2313 return 1;
2314}
2315
2316static int
Yury Selivanov75445082015-05-11 22:57:16 -04002317compiler_function(struct compiler *c, stmt_ty s, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 PyCodeObject *co;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002320 PyObject *qualname, *docstring = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002321 arguments_ty args;
2322 expr_ty returns;
2323 identifier name;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002324 asdl_expr_seq* decos;
2325 asdl_stmt_seq *body;
INADA Naokicb41b272017-02-23 00:31:59 +09002326 Py_ssize_t i, funcflags;
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002327 int annotations;
Yury Selivanov75445082015-05-11 22:57:16 -04002328 int scope_type;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002329 int firstlineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002330
Yury Selivanov75445082015-05-11 22:57:16 -04002331 if (is_async) {
2332 assert(s->kind == AsyncFunctionDef_kind);
2333
2334 args = s->v.AsyncFunctionDef.args;
2335 returns = s->v.AsyncFunctionDef.returns;
2336 decos = s->v.AsyncFunctionDef.decorator_list;
2337 name = s->v.AsyncFunctionDef.name;
2338 body = s->v.AsyncFunctionDef.body;
2339
2340 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
2341 } else {
2342 assert(s->kind == FunctionDef_kind);
2343
2344 args = s->v.FunctionDef.args;
2345 returns = s->v.FunctionDef.returns;
2346 decos = s->v.FunctionDef.decorator_list;
2347 name = s->v.FunctionDef.name;
2348 body = s->v.FunctionDef.body;
2349
2350 scope_type = COMPILER_SCOPE_FUNCTION;
2351 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002352
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002353 if (!compiler_check_debug_args(c, args))
2354 return 0;
2355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 if (!compiler_decorators(c, decos))
2357 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002358
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002359 firstlineno = s->lineno;
2360 if (asdl_seq_LEN(decos)) {
2361 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2362 }
2363
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002364 funcflags = compiler_default_arguments(c, args);
2365 if (funcflags == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002367 }
2368
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002369 annotations = compiler_visit_annotations(c, args, returns);
2370 if (annotations == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002371 return 0;
2372 }
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002373 else if (annotations > 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002374 funcflags |= 0x04;
2375 }
2376
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002377 if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002378 return 0;
2379 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002380
INADA Naokicb41b272017-02-23 00:31:59 +09002381 /* if not -OO mode, add docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002382 if (c->c_optimize < 2) {
2383 docstring = _PyAST_GetDocString(body);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002384 }
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002385 if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 compiler_exit_scope(c);
2387 return 0;
2388 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002391 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Mark Shannon877df852020-11-12 09:43:29 +00002393 for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) {
Mark Shannonfd009e62020-11-13 12:53:53 +00002394 VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i));
Mark Shannon877df852020-11-12 09:43:29 +00002395 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002397 qualname = c->u->u_qualname;
2398 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002400 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002401 Py_XDECREF(qualname);
2402 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002404 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002405
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002406 if (!compiler_make_closure(c, co, funcflags, qualname)) {
2407 Py_DECREF(qualname);
2408 Py_DECREF(co);
2409 return 0;
2410 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002411 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 /* decorators */
2415 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2416 ADDOP_I(c, CALL_FUNCTION, 1);
2417 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002418
Yury Selivanov75445082015-05-11 22:57:16 -04002419 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002420}
2421
2422static int
2423compiler_class(struct compiler *c, stmt_ty s)
2424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 PyCodeObject *co;
2426 PyObject *str;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002427 int i, firstlineno;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002428 asdl_expr_seq *decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 if (!compiler_decorators(c, decos))
2431 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002432
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002433 firstlineno = s->lineno;
2434 if (asdl_seq_LEN(decos)) {
2435 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2436 }
2437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002438 /* ultimately generate code for:
2439 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
2440 where:
2441 <func> is a function/closure created from the class body;
2442 it has a single argument (__locals__) where the dict
2443 (or MutableSequence) representing the locals is passed
2444 <name> is the class name
2445 <bases> is the positional arguments and *varargs argument
2446 <keywords> is the keyword arguments and **kwds argument
2447 This borrows from compiler_call.
2448 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002450 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002451 if (!compiler_enter_scope(c, s->v.ClassDef.name,
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002452 COMPILER_SCOPE_CLASS, (void *)s, firstlineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002453 return 0;
2454 /* this block represents what we do in the new scope */
2455 {
2456 /* use the class name for name mangling */
2457 Py_INCREF(s->v.ClassDef.name);
Serhiy Storchaka48842712016-04-06 09:45:48 +03002458 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 /* load (global) __name__ ... */
2460 str = PyUnicode_InternFromString("__name__");
2461 if (!str || !compiler_nameop(c, str, Load)) {
2462 Py_XDECREF(str);
2463 compiler_exit_scope(c);
2464 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002465 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 Py_DECREF(str);
2467 /* ... and store it as __module__ */
2468 str = PyUnicode_InternFromString("__module__");
2469 if (!str || !compiler_nameop(c, str, Store)) {
2470 Py_XDECREF(str);
2471 compiler_exit_scope(c);
2472 return 0;
2473 }
2474 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002475 assert(c->u->u_qualname);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002476 ADDOP_LOAD_CONST(c, c->u->u_qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002477 str = PyUnicode_InternFromString("__qualname__");
2478 if (!str || !compiler_nameop(c, str, Store)) {
2479 Py_XDECREF(str);
2480 compiler_exit_scope(c);
2481 return 0;
2482 }
2483 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 /* compile the body proper */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002485 if (!compiler_body(c, s->v.ClassDef.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 compiler_exit_scope(c);
2487 return 0;
2488 }
Mark Shannone56d54e2021-01-15 13:52:00 +00002489 /* The following code is artificial */
2490 c->u->u_lineno = -1;
Nick Coghlan19d24672016-12-05 16:47:55 +10002491 /* Return __classcell__ if it is referenced, otherwise return None */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002492 if (c->u->u_ste->ste_needs_class_closure) {
Nick Coghlan19d24672016-12-05 16:47:55 +10002493 /* Store __classcell__ into class namespace & return it */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002494 str = PyUnicode_InternFromString("__class__");
2495 if (str == NULL) {
2496 compiler_exit_scope(c);
2497 return 0;
2498 }
2499 i = compiler_lookup_arg(c->u->u_cellvars, str);
2500 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01002501 if (i < 0) {
2502 compiler_exit_scope(c);
2503 return 0;
2504 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002505 assert(i == 0);
Nick Coghlan944368e2016-09-11 14:45:49 +10002506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 ADDOP_I(c, LOAD_CLOSURE, i);
Nick Coghlan19d24672016-12-05 16:47:55 +10002508 ADDOP(c, DUP_TOP);
Nick Coghlan944368e2016-09-11 14:45:49 +10002509 str = PyUnicode_InternFromString("__classcell__");
2510 if (!str || !compiler_nameop(c, str, Store)) {
2511 Py_XDECREF(str);
2512 compiler_exit_scope(c);
2513 return 0;
2514 }
2515 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002517 else {
Nick Coghlan19d24672016-12-05 16:47:55 +10002518 /* No methods referenced __class__, so just return None */
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002519 assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002520 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson312595c2013-05-15 15:26:42 -05002521 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002522 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 /* create the code object */
2524 co = assemble(c, 1);
2525 }
2526 /* leave the new scope */
2527 compiler_exit_scope(c);
2528 if (co == NULL)
2529 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002531 /* 2. load the 'build_class' function */
2532 ADDOP(c, LOAD_BUILD_CLASS);
2533
2534 /* 3. load a function (or closure) made from the code object */
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002535 if (!compiler_make_closure(c, co, 0, NULL)) {
2536 Py_DECREF(co);
2537 return 0;
2538 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002539 Py_DECREF(co);
2540
2541 /* 4. load class name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002542 ADDOP_LOAD_CONST(c, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543
2544 /* 5. generate the rest of the code for the call */
Pablo Galindoa5634c42020-09-16 19:42:00 +01002545 if (!compiler_call_helper(c, 2, s->v.ClassDef.bases, s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002546 return 0;
2547
2548 /* 6. apply decorators */
2549 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2550 ADDOP_I(c, CALL_FUNCTION, 1);
2551 }
2552
2553 /* 7. store into <name> */
2554 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2555 return 0;
2556 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002557}
2558
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02002559/* Return 0 if the expression is a constant value except named singletons.
2560 Return 1 otherwise. */
2561static int
2562check_is_arg(expr_ty e)
2563{
2564 if (e->kind != Constant_kind) {
2565 return 1;
2566 }
2567 PyObject *value = e->v.Constant.value;
2568 return (value == Py_None
2569 || value == Py_False
2570 || value == Py_True
2571 || value == Py_Ellipsis);
2572}
2573
2574/* Check operands of identity chacks ("is" and "is not").
2575 Emit a warning if any operand is a constant except named singletons.
2576 Return 0 on error.
2577 */
2578static int
2579check_compare(struct compiler *c, expr_ty e)
2580{
2581 Py_ssize_t i, n;
2582 int left = check_is_arg(e->v.Compare.left);
2583 n = asdl_seq_LEN(e->v.Compare.ops);
2584 for (i = 0; i < n; i++) {
2585 cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i);
2586 int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2587 if (op == Is || op == IsNot) {
2588 if (!right || !left) {
2589 const char *msg = (op == Is)
2590 ? "\"is\" with a literal. Did you mean \"==\"?"
2591 : "\"is not\" with a literal. Did you mean \"!=\"?";
2592 return compiler_warn(c, msg);
2593 }
2594 }
2595 left = right;
2596 }
2597 return 1;
2598}
2599
Mark Shannon9af0e472020-01-14 10:12:45 +00002600static int compiler_addcompare(struct compiler *c, cmpop_ty op)
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002601{
Mark Shannon9af0e472020-01-14 10:12:45 +00002602 int cmp;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002603 switch (op) {
2604 case Eq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002605 cmp = Py_EQ;
2606 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002607 case NotEq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002608 cmp = Py_NE;
2609 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002610 case Lt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002611 cmp = Py_LT;
2612 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002613 case LtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002614 cmp = Py_LE;
2615 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002616 case Gt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002617 cmp = Py_GT;
2618 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002619 case GtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002620 cmp = Py_GE;
2621 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002622 case Is:
Mark Shannon9af0e472020-01-14 10:12:45 +00002623 ADDOP_I(c, IS_OP, 0);
2624 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002625 case IsNot:
Mark Shannon9af0e472020-01-14 10:12:45 +00002626 ADDOP_I(c, IS_OP, 1);
2627 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002628 case In:
Mark Shannon9af0e472020-01-14 10:12:45 +00002629 ADDOP_I(c, CONTAINS_OP, 0);
2630 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002631 case NotIn:
Mark Shannon9af0e472020-01-14 10:12:45 +00002632 ADDOP_I(c, CONTAINS_OP, 1);
2633 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002634 default:
Mark Shannon9af0e472020-01-14 10:12:45 +00002635 Py_UNREACHABLE();
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002636 }
Mark Shannon9af0e472020-01-14 10:12:45 +00002637 ADDOP_I(c, COMPARE_OP, cmp);
2638 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002639}
2640
Mark Shannon9af0e472020-01-14 10:12:45 +00002641
2642
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002643static int
2644compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2645{
2646 switch (e->kind) {
2647 case UnaryOp_kind:
2648 if (e->v.UnaryOp.op == Not)
2649 return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2650 /* fallback to general implementation */
2651 break;
2652 case BoolOp_kind: {
Pablo Galindoa5634c42020-09-16 19:42:00 +01002653 asdl_expr_seq *s = e->v.BoolOp.values;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002654 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2655 assert(n >= 0);
2656 int cond2 = e->v.BoolOp.op == Or;
2657 basicblock *next2 = next;
2658 if (!cond2 != !cond) {
2659 next2 = compiler_new_block(c);
2660 if (next2 == NULL)
2661 return 0;
2662 }
2663 for (i = 0; i < n; ++i) {
2664 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2665 return 0;
2666 }
2667 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2668 return 0;
2669 if (next2 != next)
2670 compiler_use_next_block(c, next2);
2671 return 1;
2672 }
2673 case IfExp_kind: {
2674 basicblock *end, *next2;
2675 end = compiler_new_block(c);
2676 if (end == NULL)
2677 return 0;
2678 next2 = compiler_new_block(c);
2679 if (next2 == NULL)
2680 return 0;
2681 if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2682 return 0;
2683 if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2684 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002685 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002686 compiler_use_next_block(c, next2);
2687 if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2688 return 0;
2689 compiler_use_next_block(c, end);
2690 return 1;
2691 }
2692 case Compare_kind: {
2693 Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2694 if (n > 0) {
Serhiy Storchaka45835252019-02-16 08:29:46 +02002695 if (!check_compare(c, e)) {
2696 return 0;
2697 }
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002698 basicblock *cleanup = compiler_new_block(c);
2699 if (cleanup == NULL)
2700 return 0;
2701 VISIT(c, expr, e->v.Compare.left);
2702 for (i = 0; i < n; i++) {
2703 VISIT(c, expr,
2704 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2705 ADDOP(c, DUP_TOP);
2706 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00002707 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01002708 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002709 NEXT_BLOCK(c);
2710 }
2711 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00002712 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Mark Shannon582aaf12020-08-04 17:30:11 +01002713 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Mark Shannon266b4622020-11-17 19:30:14 +00002714 NEXT_BLOCK(c);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002715 basicblock *end = compiler_new_block(c);
2716 if (end == NULL)
2717 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002718 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002719 compiler_use_next_block(c, cleanup);
2720 ADDOP(c, POP_TOP);
2721 if (!cond) {
Mark Shannon127dde52021-01-04 18:06:55 +00002722 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002723 }
2724 compiler_use_next_block(c, end);
2725 return 1;
2726 }
2727 /* fallback to general implementation */
2728 break;
2729 }
2730 default:
2731 /* fallback to general implementation */
2732 break;
2733 }
2734
2735 /* general implementation */
2736 VISIT(c, expr, e);
Mark Shannon582aaf12020-08-04 17:30:11 +01002737 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Mark Shannon266b4622020-11-17 19:30:14 +00002738 NEXT_BLOCK(c);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002739 return 1;
2740}
2741
2742static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002743compiler_ifexp(struct compiler *c, expr_ty e)
2744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002745 basicblock *end, *next;
2746
2747 assert(e->kind == IfExp_kind);
2748 end = compiler_new_block(c);
2749 if (end == NULL)
2750 return 0;
2751 next = compiler_new_block(c);
2752 if (next == NULL)
2753 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002754 if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2755 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 VISIT(c, expr, e->v.IfExp.body);
Mark Shannon127dde52021-01-04 18:06:55 +00002757 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002758 compiler_use_next_block(c, next);
2759 VISIT(c, expr, e->v.IfExp.orelse);
2760 compiler_use_next_block(c, end);
2761 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002762}
2763
2764static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002765compiler_lambda(struct compiler *c, expr_ty e)
2766{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002768 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002769 static identifier name;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002770 Py_ssize_t funcflags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002771 arguments_ty args = e->v.Lambda.args;
2772 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002773
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002774 if (!compiler_check_debug_args(c, args))
2775 return 0;
2776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 if (!name) {
2778 name = PyUnicode_InternFromString("<lambda>");
2779 if (!name)
2780 return 0;
2781 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002782
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002783 funcflags = compiler_default_arguments(c, args);
2784 if (funcflags == -1) {
2785 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002787
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002788 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002789 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002790 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 /* Make None the first constant, so the lambda can't have a
2793 docstring. */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002794 if (compiler_add_const(c, Py_None) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002795 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002797 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002798 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2800 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2801 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002802 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002803 }
2804 else {
2805 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002806 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002807 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002808 qualname = c->u->u_qualname;
2809 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 compiler_exit_scope(c);
Pablo Galindo7fdab832021-01-29 22:40:59 +00002811 if (co == NULL) {
2812 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 return 0;
Pablo Galindo7fdab832021-01-29 22:40:59 +00002814 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002815
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002816 if (!compiler_make_closure(c, co, funcflags, qualname)) {
2817 Py_DECREF(qualname);
2818 Py_DECREF(co);
2819 return 0;
2820 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002821 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002822 Py_DECREF(co);
2823
2824 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825}
2826
2827static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002828compiler_if(struct compiler *c, stmt_ty s)
2829{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 basicblock *end, *next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 assert(s->kind == If_kind);
2832 end = compiler_new_block(c);
Mark Shannon8473cf82020-12-15 11:07:50 +00002833 if (end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 return 0;
Mark Shannon8473cf82020-12-15 11:07:50 +00002835 }
2836 if (asdl_seq_LEN(s->v.If.orelse)) {
2837 next = compiler_new_block(c);
2838 if (next == NULL) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002839 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002840 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002841 }
2842 else {
2843 next = end;
2844 }
2845 if (!compiler_jump_if(c, s->v.If.test, next, 0)) {
2846 return 0;
2847 }
2848 VISIT_SEQ(c, stmt, s->v.If.body);
2849 if (asdl_seq_LEN(s->v.If.orelse)) {
Mark Shannon127dde52021-01-04 18:06:55 +00002850 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Mark Shannon8473cf82020-12-15 11:07:50 +00002851 compiler_use_next_block(c, next);
2852 VISIT_SEQ(c, stmt, s->v.If.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 }
2854 compiler_use_next_block(c, end);
2855 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002856}
2857
2858static int
2859compiler_for(struct compiler *c, stmt_ty s)
2860{
Mark Shannon5977a792020-12-02 13:31:40 +00002861 basicblock *start, *body, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 start = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002864 body = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 cleanup = compiler_new_block(c);
2866 end = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002867 if (start == NULL || body == NULL || end == NULL || cleanup == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002869 }
2870 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002872 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 VISIT(c, expr, s->v.For.iter);
2874 ADDOP(c, GET_ITER);
2875 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01002876 ADDOP_JUMP(c, FOR_ITER, cleanup);
Mark Shannon5977a792020-12-02 13:31:40 +00002877 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 VISIT(c, expr, s->v.For.target);
2879 VISIT_SEQ(c, stmt, s->v.For.body);
Mark Shannonf5e97b72020-12-14 11:28:39 +00002880 /* Mark jump as artificial */
2881 c->u->u_lineno = -1;
Mark Shannon582aaf12020-08-04 17:30:11 +01002882 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 compiler_use_next_block(c, cleanup);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002884
2885 compiler_pop_fblock(c, FOR_LOOP, start);
2886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 VISIT_SEQ(c, stmt, s->v.For.orelse);
2888 compiler_use_next_block(c, end);
2889 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002890}
2891
Yury Selivanov75445082015-05-11 22:57:16 -04002892
2893static int
2894compiler_async_for(struct compiler *c, stmt_ty s)
2895{
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002896 basicblock *start, *except, *end;
Pablo Galindo90235812020-03-15 04:29:22 +00002897 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07002898 c->u->u_ste->ste_coroutine = 1;
2899 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
Zsolt Dollensteine2396502018-04-27 08:58:56 -07002900 return compiler_error(c, "'async for' outside async function");
2901 }
2902
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002903 start = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002904 except = compiler_new_block(c);
2905 end = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002906
Mark Shannonfee55262019-11-21 09:11:43 +00002907 if (start == NULL || except == NULL || end == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002908 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002909 }
Yury Selivanov75445082015-05-11 22:57:16 -04002910 VISIT(c, expr, s->v.AsyncFor.iter);
2911 ADDOP(c, GET_AITER);
Yury Selivanov75445082015-05-11 22:57:16 -04002912
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002913 compiler_use_next_block(c, start);
Mark Shannonfee55262019-11-21 09:11:43 +00002914 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002915 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002916 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002917 /* SETUP_FINALLY to guard the __anext__ call */
Mark Shannon582aaf12020-08-04 17:30:11 +01002918 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov75445082015-05-11 22:57:16 -04002919 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002920 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04002921 ADDOP(c, YIELD_FROM);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002922 ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */
Yury Selivanov75445082015-05-11 22:57:16 -04002923
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002924 /* Success block for __anext__ */
2925 VISIT(c, expr, s->v.AsyncFor.target);
2926 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002927 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002928
2929 compiler_pop_fblock(c, FOR_LOOP, start);
Yury Selivanov75445082015-05-11 22:57:16 -04002930
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002931 /* Except block for __anext__ */
Yury Selivanov75445082015-05-11 22:57:16 -04002932 compiler_use_next_block(c, except);
Mark Shannon877df852020-11-12 09:43:29 +00002933
Mark Shannon47695e32021-07-15 15:54:38 +01002934 /* Use same line number as the iterator,
2935 * as the END_ASYNC_FOR succeeds the `for`, not the body. */
2936 SET_LOC(c, s->v.AsyncFor.iter);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002937 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov75445082015-05-11 22:57:16 -04002938
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002939 /* `else` block */
Yury Selivanov75445082015-05-11 22:57:16 -04002940 VISIT_SEQ(c, stmt, s->v.For.orelse);
2941
2942 compiler_use_next_block(c, end);
2943
2944 return 1;
2945}
2946
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002947static int
2948compiler_while(struct compiler *c, stmt_ty s)
2949{
Mark Shannon266b4622020-11-17 19:30:14 +00002950 basicblock *loop, *body, *end, *anchor = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002951 loop = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002952 body = compiler_new_block(c);
2953 anchor = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002954 end = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002955 if (loop == NULL || body == NULL || anchor == NULL || end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002956 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002957 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002958 compiler_use_next_block(c, loop);
Mark Shannon266b4622020-11-17 19:30:14 +00002959 if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002960 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002962 if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) {
2963 return 0;
Mark Shannon266b4622020-11-17 19:30:14 +00002964 }
2965
2966 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002967 VISIT_SEQ(c, stmt, s->v.While.body);
Mark Shannon8473cf82020-12-15 11:07:50 +00002968 SET_LOC(c, s);
2969 if (!compiler_jump_if(c, s->v.While.test, body, 1)) {
2970 return 0;
2971 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002972
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002973 compiler_pop_fblock(c, WHILE_LOOP, loop);
2974
Mark Shannon266b4622020-11-17 19:30:14 +00002975 compiler_use_next_block(c, anchor);
2976 if (s->v.While.orelse) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 VISIT_SEQ(c, stmt, s->v.While.orelse);
Mark Shannon266b4622020-11-17 19:30:14 +00002978 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002979 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002982}
2983
2984static int
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002985compiler_return(struct compiler *c, stmt_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002986{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002987 int preserve_tos = ((s->v.Return.value != NULL) &&
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002988 (s->v.Return.value->kind != Constant_kind));
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002989 if (c->u->u_ste->ste_type != FunctionBlock)
2990 return compiler_error(c, "'return' outside function");
2991 if (s->v.Return.value != NULL &&
2992 c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2993 {
2994 return compiler_error(
2995 c, "'return' with value in async generator");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002997 if (preserve_tos) {
2998 VISIT(c, expr, s->v.Return.value);
Mark Shannon5274b682020-12-16 13:07:01 +00002999 } else {
Mark Shannoncea05852021-06-03 19:57:31 +01003000 /* Emit instruction with line number for return value */
Mark Shannon5274b682020-12-16 13:07:01 +00003001 if (s->v.Return.value != NULL) {
3002 SET_LOC(c, s->v.Return.value);
3003 ADDOP(c, NOP);
3004 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003005 }
Mark Shannoncea05852021-06-03 19:57:31 +01003006 if (s->v.Return.value == NULL || s->v.Return.value->lineno != s->lineno) {
3007 SET_LOC(c, s);
3008 ADDOP(c, NOP);
3009 }
3010
Mark Shannonfee55262019-11-21 09:11:43 +00003011 if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL))
3012 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003013 if (s->v.Return.value == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003014 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003015 }
3016 else if (!preserve_tos) {
Mark Shannon5274b682020-12-16 13:07:01 +00003017 ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003018 }
3019 ADDOP(c, RETURN_VALUE);
Mark Shannon266b4622020-11-17 19:30:14 +00003020 NEXT_BLOCK(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003023}
3024
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003025static int
3026compiler_break(struct compiler *c)
3027{
Mark Shannonfee55262019-11-21 09:11:43 +00003028 struct fblockinfo *loop = NULL;
Mark Shannoncea05852021-06-03 19:57:31 +01003029 /* Emit instruction with line number */
3030 ADDOP(c, NOP);
Mark Shannonfee55262019-11-21 09:11:43 +00003031 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
3032 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003033 }
Mark Shannonfee55262019-11-21 09:11:43 +00003034 if (loop == NULL) {
3035 return compiler_error(c, "'break' outside loop");
3036 }
3037 if (!compiler_unwind_fblock(c, loop, 0)) {
3038 return 0;
3039 }
Mark Shannon582aaf12020-08-04 17:30:11 +01003040 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit);
Mark Shannon266b4622020-11-17 19:30:14 +00003041 NEXT_BLOCK(c);
Mark Shannonfee55262019-11-21 09:11:43 +00003042 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003043}
3044
3045static int
3046compiler_continue(struct compiler *c)
3047{
Mark Shannonfee55262019-11-21 09:11:43 +00003048 struct fblockinfo *loop = NULL;
Mark Shannoncea05852021-06-03 19:57:31 +01003049 /* Emit instruction with line number */
3050 ADDOP(c, NOP);
Mark Shannonfee55262019-11-21 09:11:43 +00003051 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
3052 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003053 }
Mark Shannonfee55262019-11-21 09:11:43 +00003054 if (loop == NULL) {
3055 return compiler_error(c, "'continue' not properly in loop");
3056 }
Mark Shannon582aaf12020-08-04 17:30:11 +01003057 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block);
Mark Shannon266b4622020-11-17 19:30:14 +00003058 NEXT_BLOCK(c)
Mark Shannonfee55262019-11-21 09:11:43 +00003059 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003060}
3061
3062
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003063/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003064
3065 SETUP_FINALLY L
3066 <code for body>
3067 POP_BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00003068 <code for finalbody>
3069 JUMP E
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003070 L:
3071 <code for finalbody>
Mark Shannonfee55262019-11-21 09:11:43 +00003072 E:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003074 The special instructions use the block stack. Each block
3075 stack entry contains the instruction that created it (here
3076 SETUP_FINALLY), the level of the value stack at the time the
3077 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003078
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003079 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003080 Pushes the current value stack level and the label
3081 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003082 POP_BLOCK:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003083 Pops en entry from the block stack.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003084
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003085 The block stack is unwound when an exception is raised:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003086 when a SETUP_FINALLY entry is found, the raised and the caught
3087 exceptions are pushed onto the value stack (and the exception
3088 condition is cleared), and the interpreter jumps to the label
3089 gotten from the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003090*/
3091
3092static int
3093compiler_try_finally(struct compiler *c, stmt_ty s)
3094{
Mark Shannonfee55262019-11-21 09:11:43 +00003095 basicblock *body, *end, *exit;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003097 body = compiler_new_block(c);
3098 end = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00003099 exit = compiler_new_block(c);
3100 if (body == NULL || end == NULL || exit == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003101 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003102
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003103 /* `try` block */
Mark Shannon582aaf12020-08-04 17:30:11 +01003104 ADDOP_JUMP(c, SETUP_FINALLY, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003105 compiler_use_next_block(c, body);
Mark Shannonfee55262019-11-21 09:11:43 +00003106 if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003107 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003108 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
3109 if (!compiler_try_except(c, s))
3110 return 0;
3111 }
3112 else {
3113 VISIT_SEQ(c, stmt, s->v.Try.body);
3114 }
Mark Shannon3bd60352021-01-13 12:05:43 +00003115 ADDOP_NOLINE(c, POP_BLOCK);
Mark Shannonfee55262019-11-21 09:11:43 +00003116 compiler_pop_fblock(c, FINALLY_TRY, body);
3117 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Mark Shannon127dde52021-01-04 18:06:55 +00003118 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00003119 /* `finally` block */
3120 compiler_use_next_block(c, end);
3121 if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL))
3122 return 0;
3123 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3124 compiler_pop_fblock(c, FINALLY_END, end);
Mark Shannonbf353f32020-12-17 13:55:28 +00003125 ADDOP_I(c, RERAISE, 0);
Mark Shannonfee55262019-11-21 09:11:43 +00003126 compiler_use_next_block(c, exit);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003127 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003128}
3129
3130/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003131 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003132 (The contents of the value stack is shown in [], with the top
3133 at the right; 'tb' is trace-back info, 'val' the exception's
3134 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003135
3136 Value stack Label Instruction Argument
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003137 [] SETUP_FINALLY L1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 [] <code for S>
3139 [] POP_BLOCK
3140 [] JUMP_FORWARD L0
3141
3142 [tb, val, exc] L1: DUP )
3143 [tb, val, exc, exc] <evaluate E1> )
Mark Shannon9af0e472020-01-14 10:12:45 +00003144 [tb, val, exc, exc, E1] JUMP_IF_NOT_EXC_MATCH L2 ) only if E1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145 [tb, val, exc] POP
3146 [tb, val] <assign to V1> (or POP if no V1)
3147 [tb] POP
3148 [] <code for S1>
3149 JUMP_FORWARD L0
3150
3151 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 .............................etc.......................
3153
Mark Shannonfee55262019-11-21 09:11:43 +00003154 [tb, val, exc] Ln+1: RERAISE # re-raise exception
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003155
3156 [] L0: <next statement>
3157
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003158 Of course, parts are not generated if Vi or Ei is not present.
3159*/
3160static int
3161compiler_try_except(struct compiler *c, stmt_ty s)
3162{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003163 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003164 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003166 body = compiler_new_block(c);
3167 except = compiler_new_block(c);
3168 orelse = compiler_new_block(c);
3169 end = compiler_new_block(c);
3170 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
3171 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01003172 ADDOP_JUMP(c, SETUP_FINALLY, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003173 compiler_use_next_block(c, body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003174 if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003175 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003176 VISIT_SEQ(c, stmt, s->v.Try.body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003177 compiler_pop_fblock(c, TRY_EXCEPT, body);
Mark Shannon3bd60352021-01-13 12:05:43 +00003178 ADDOP_NOLINE(c, POP_BLOCK);
3179 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003180 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 compiler_use_next_block(c, except);
Mark Shannon02d126a2020-09-25 14:04:19 +01003182 /* Runtime will push a block here, so we need to account for that */
3183 if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL))
3184 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003185 for (i = 0; i < n; i++) {
3186 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003187 s->v.Try.handlers, i);
Mark Shannon8d4b1842021-05-06 13:38:50 +01003188 SET_LOC(c, handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 if (!handler->v.ExceptHandler.type && i < n-1)
3190 return compiler_error(c, "default 'except:' must be last");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003191 except = compiler_new_block(c);
3192 if (except == NULL)
3193 return 0;
3194 if (handler->v.ExceptHandler.type) {
3195 ADDOP(c, DUP_TOP);
3196 VISIT(c, expr, handler->v.ExceptHandler.type);
Mark Shannon582aaf12020-08-04 17:30:11 +01003197 ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except);
Mark Shannon266b4622020-11-17 19:30:14 +00003198 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003199 }
3200 ADDOP(c, POP_TOP);
3201 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003202 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00003203
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003204 cleanup_end = compiler_new_block(c);
3205 cleanup_body = compiler_new_block(c);
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003206 if (cleanup_end == NULL || cleanup_body == NULL) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003207 return 0;
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003208 }
Guido van Rossumb940e112007-01-10 16:19:56 +00003209
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003210 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3211 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003212
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003213 /*
3214 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003215 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003216 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003217 try:
3218 # body
3219 finally:
Chris Angelicoad098b62019-05-21 23:34:19 +10003220 name = None # in case body contains "del name"
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003221 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003222 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003224 /* second try: */
Mark Shannon582aaf12020-08-04 17:30:11 +01003225 ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003226 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003227 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003228 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003229
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003230 /* second # body */
3231 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003232 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannon877df852020-11-12 09:43:29 +00003233 /* name = None; del name; # Mark as artificial */
3234 c->u->u_lineno = -1;
Mark Shannon794ff7d2021-07-14 11:43:56 +01003235 ADDOP(c, POP_BLOCK);
3236 ADDOP(c, POP_EXCEPT);
Mark Shannonfee55262019-11-21 09:11:43 +00003237 ADDOP_LOAD_CONST(c, Py_None);
3238 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3239 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Mark Shannon582aaf12020-08-04 17:30:11 +01003240 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003241
Mark Shannonfee55262019-11-21 09:11:43 +00003242 /* except: */
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003243 compiler_use_next_block(c, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003244
Mark Shannon877df852020-11-12 09:43:29 +00003245 /* name = None; del name; # Mark as artificial */
3246 c->u->u_lineno = -1;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003247 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003248 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003249 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250
Mark Shannonbf353f32020-12-17 13:55:28 +00003251 ADDOP_I(c, RERAISE, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003252 }
3253 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003254 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003255
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003256 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05003257 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003258 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003259
Guido van Rossumb940e112007-01-10 16:19:56 +00003260 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003261 ADDOP(c, POP_TOP);
3262 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003263 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003264 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003265 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003266 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannon127dde52021-01-04 18:06:55 +00003267 c->u->u_lineno = -1;
Mark Shannonfee55262019-11-21 09:11:43 +00003268 ADDOP(c, POP_EXCEPT);
Mark Shannon582aaf12020-08-04 17:30:11 +01003269 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003270 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003271 compiler_use_next_block(c, except);
3272 }
Mark Shannon02d126a2020-09-25 14:04:19 +01003273 compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL);
Mark Shannonf2dbfd72020-12-21 13:53:50 +00003274 /* Mark as artificial */
3275 c->u->u_lineno = -1;
Mark Shannonbf353f32020-12-17 13:55:28 +00003276 ADDOP_I(c, RERAISE, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003277 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003278 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 compiler_use_next_block(c, end);
3280 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003281}
3282
3283static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003284compiler_try(struct compiler *c, stmt_ty s) {
3285 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
3286 return compiler_try_finally(c, s);
3287 else
3288 return compiler_try_except(c, s);
3289}
3290
3291
3292static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003293compiler_import_as(struct compiler *c, identifier name, identifier asname)
3294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003295 /* The IMPORT_NAME opcode was already generated. This function
3296 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003298 If there is a dot in name, we need to split it and emit a
Serhiy Storchakaf93234b2017-05-09 22:31:05 +03003299 IMPORT_FROM for each name.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003300 */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003301 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
3302 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003303 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003304 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003305 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003306 /* Consume the base module name to get the first attribute */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003307 while (1) {
3308 Py_ssize_t pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003309 PyObject *attr;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003310 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003311 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003312 return 0;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003313 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 if (!attr)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003315 return 0;
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003316 ADDOP_N(c, IMPORT_FROM, attr, names);
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003317 if (dot == -1) {
3318 break;
3319 }
3320 ADDOP(c, ROT_TWO);
3321 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003322 }
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003323 if (!compiler_nameop(c, asname, Store)) {
3324 return 0;
3325 }
3326 ADDOP(c, POP_TOP);
3327 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003328 }
3329 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003330}
3331
3332static int
3333compiler_import(struct compiler *c, stmt_ty s)
3334{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003335 /* The Import node stores a module name like a.b.c as a single
3336 string. This is convenient for all cases except
3337 import a.b.c as d
3338 where we need to parse that string to extract the individual
3339 module names.
3340 XXX Perhaps change the representation to make this case simpler?
3341 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01003342 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003343
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003344 PyObject *zero = _PyLong_GetZero(); // borrowed reference
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003345 for (i = 0; i < n; i++) {
3346 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
3347 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003348
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003349 ADDOP_LOAD_CONST(c, zero);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003350 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003351 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003353 if (alias->asname) {
3354 r = compiler_import_as(c, alias->name, alias->asname);
3355 if (!r)
3356 return r;
3357 }
3358 else {
3359 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003360 Py_ssize_t dot = PyUnicode_FindChar(
3361 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02003362 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003363 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02003364 if (tmp == NULL)
3365 return 0;
3366 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003367 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003368 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003369 Py_DECREF(tmp);
3370 }
3371 if (!r)
3372 return r;
3373 }
3374 }
3375 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003376}
3377
3378static int
3379compiler_from_import(struct compiler *c, stmt_ty s)
3380{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003381 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003382 PyObject *names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003383 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003385 if (!empty_string) {
3386 empty_string = PyUnicode_FromString("");
3387 if (!empty_string)
3388 return 0;
3389 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003390
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003391 ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02003392
3393 names = PyTuple_New(n);
3394 if (!names)
3395 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003397 /* build up the names */
3398 for (i = 0; i < n; i++) {
3399 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3400 Py_INCREF(alias->name);
3401 PyTuple_SET_ITEM(names, i, alias->name);
3402 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003404 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003405 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003406 Py_DECREF(names);
3407 return compiler_error(c, "from __future__ imports must occur "
3408 "at the beginning of the file");
3409 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003410 ADDOP_LOAD_CONST_NEW(c, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003412 if (s->v.ImportFrom.module) {
3413 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
3414 }
3415 else {
3416 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
3417 }
3418 for (i = 0; i < n; i++) {
3419 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3420 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003421
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003422 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003423 assert(n == 1);
3424 ADDOP(c, IMPORT_STAR);
3425 return 1;
3426 }
3427
3428 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
3429 store_name = alias->name;
3430 if (alias->asname)
3431 store_name = alias->asname;
3432
3433 if (!compiler_nameop(c, store_name, Store)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003434 return 0;
3435 }
3436 }
3437 /* remove imported module */
3438 ADDOP(c, POP_TOP);
3439 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003440}
3441
3442static int
3443compiler_assert(struct compiler *c, stmt_ty s)
3444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003446
tsukasa-aua8ef4572021-03-16 22:14:41 +11003447 /* Always emit a warning if the test is a non-zero length tuple */
3448 if ((s->v.Assert.test->kind == Tuple_kind &&
3449 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) ||
3450 (s->v.Assert.test->kind == Constant_kind &&
3451 PyTuple_Check(s->v.Assert.test->v.Constant.value) &&
3452 PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0))
Serhiy Storchakad31e7732018-10-21 10:09:39 +03003453 {
3454 if (!compiler_warn(c, "assertion is always true, "
3455 "perhaps remove parentheses?"))
3456 {
Victor Stinner14e461d2013-08-26 22:28:21 +02003457 return 0;
3458 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003459 }
tsukasa-aua8ef4572021-03-16 22:14:41 +11003460 if (c->c_optimize)
3461 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003462 end = compiler_new_block(c);
3463 if (end == NULL)
3464 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003465 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
3466 return 0;
Zackery Spytzce6a0702019-08-25 03:44:09 -06003467 ADDOP(c, LOAD_ASSERTION_ERROR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003468 if (s->v.Assert.msg) {
3469 VISIT(c, expr, s->v.Assert.msg);
3470 ADDOP_I(c, CALL_FUNCTION, 1);
3471 }
3472 ADDOP_I(c, RAISE_VARARGS, 1);
3473 compiler_use_next_block(c, end);
3474 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003475}
3476
3477static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003478compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3479{
3480 if (c->c_interactive && c->c_nestlevel <= 1) {
3481 VISIT(c, expr, value);
3482 ADDOP(c, PRINT_EXPR);
3483 return 1;
3484 }
3485
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003486 if (value->kind == Constant_kind) {
Victor Stinner15a30952016-02-08 22:45:06 +01003487 /* ignore constant statement */
Mark Shannon877df852020-11-12 09:43:29 +00003488 ADDOP(c, NOP);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003489 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003490 }
3491
3492 VISIT(c, expr, value);
Mark Shannonc5440932021-03-15 14:24:25 +00003493 /* Mark POP_TOP as artificial */
3494 c->u->u_lineno = -1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003495 ADDOP(c, POP_TOP);
3496 return 1;
3497}
3498
3499static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003500compiler_visit_stmt(struct compiler *c, stmt_ty s)
3501{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003502 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003504 /* Always assign a lineno to the next instruction for a stmt. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003505 SET_LOC(c, s);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003507 switch (s->kind) {
3508 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04003509 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003510 case ClassDef_kind:
3511 return compiler_class(c, s);
3512 case Return_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003513 return compiler_return(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003514 case Delete_kind:
3515 VISIT_SEQ(c, expr, s->v.Delete.targets)
3516 break;
3517 case Assign_kind:
3518 n = asdl_seq_LEN(s->v.Assign.targets);
3519 VISIT(c, expr, s->v.Assign.value);
3520 for (i = 0; i < n; i++) {
3521 if (i < n - 1)
3522 ADDOP(c, DUP_TOP);
3523 VISIT(c, expr,
3524 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3525 }
3526 break;
3527 case AugAssign_kind:
3528 return compiler_augassign(c, s);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003529 case AnnAssign_kind:
3530 return compiler_annassign(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003531 case For_kind:
3532 return compiler_for(c, s);
3533 case While_kind:
3534 return compiler_while(c, s);
3535 case If_kind:
3536 return compiler_if(c, s);
Brandt Bucher145bf262021-02-26 14:51:55 -08003537 case Match_kind:
3538 return compiler_match(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003539 case Raise_kind:
3540 n = 0;
3541 if (s->v.Raise.exc) {
3542 VISIT(c, expr, s->v.Raise.exc);
3543 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003544 if (s->v.Raise.cause) {
3545 VISIT(c, expr, s->v.Raise.cause);
3546 n++;
3547 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01003549 ADDOP_I(c, RAISE_VARARGS, (int)n);
Mark Shannon266b4622020-11-17 19:30:14 +00003550 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003551 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003552 case Try_kind:
3553 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003554 case Assert_kind:
3555 return compiler_assert(c, s);
3556 case Import_kind:
3557 return compiler_import(c, s);
3558 case ImportFrom_kind:
3559 return compiler_from_import(c, s);
3560 case Global_kind:
3561 case Nonlocal_kind:
3562 break;
3563 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003564 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003565 case Pass_kind:
Mark Shannon877df852020-11-12 09:43:29 +00003566 ADDOP(c, NOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003567 break;
3568 case Break_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003569 return compiler_break(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003570 case Continue_kind:
3571 return compiler_continue(c);
3572 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003573 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04003574 case AsyncFunctionDef_kind:
3575 return compiler_function(c, s, 1);
3576 case AsyncWith_kind:
3577 return compiler_async_with(c, s, 0);
3578 case AsyncFor_kind:
3579 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003580 }
Yury Selivanov75445082015-05-11 22:57:16 -04003581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003582 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003583}
3584
3585static int
3586unaryop(unaryop_ty op)
3587{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003588 switch (op) {
3589 case Invert:
3590 return UNARY_INVERT;
3591 case Not:
3592 return UNARY_NOT;
3593 case UAdd:
3594 return UNARY_POSITIVE;
3595 case USub:
3596 return UNARY_NEGATIVE;
3597 default:
3598 PyErr_Format(PyExc_SystemError,
3599 "unary op %d should not be possible", op);
3600 return 0;
3601 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003602}
3603
3604static int
Andy Lester76d58772020-03-10 21:18:12 -05003605binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003607 switch (op) {
3608 case Add:
3609 return BINARY_ADD;
3610 case Sub:
3611 return BINARY_SUBTRACT;
3612 case Mult:
3613 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003614 case MatMult:
3615 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003616 case Div:
3617 return BINARY_TRUE_DIVIDE;
3618 case Mod:
3619 return BINARY_MODULO;
3620 case Pow:
3621 return BINARY_POWER;
3622 case LShift:
3623 return BINARY_LSHIFT;
3624 case RShift:
3625 return BINARY_RSHIFT;
3626 case BitOr:
3627 return BINARY_OR;
3628 case BitXor:
3629 return BINARY_XOR;
3630 case BitAnd:
3631 return BINARY_AND;
3632 case FloorDiv:
3633 return BINARY_FLOOR_DIVIDE;
3634 default:
3635 PyErr_Format(PyExc_SystemError,
3636 "binary op %d should not be possible", op);
3637 return 0;
3638 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003639}
3640
3641static int
Andy Lester76d58772020-03-10 21:18:12 -05003642inplace_binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003643{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003644 switch (op) {
3645 case Add:
3646 return INPLACE_ADD;
3647 case Sub:
3648 return INPLACE_SUBTRACT;
3649 case Mult:
3650 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003651 case MatMult:
3652 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003653 case Div:
3654 return INPLACE_TRUE_DIVIDE;
3655 case Mod:
3656 return INPLACE_MODULO;
3657 case Pow:
3658 return INPLACE_POWER;
3659 case LShift:
3660 return INPLACE_LSHIFT;
3661 case RShift:
3662 return INPLACE_RSHIFT;
3663 case BitOr:
3664 return INPLACE_OR;
3665 case BitXor:
3666 return INPLACE_XOR;
3667 case BitAnd:
3668 return INPLACE_AND;
3669 case FloorDiv:
3670 return INPLACE_FLOOR_DIVIDE;
3671 default:
3672 PyErr_Format(PyExc_SystemError,
3673 "inplace binary op %d should not be possible", op);
3674 return 0;
3675 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003676}
3677
3678static int
3679compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3680{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003681 int op, scope;
3682 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003683 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003685 PyObject *dict = c->u->u_names;
3686 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003687
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003688 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3689 !_PyUnicode_EqualToASCIIString(name, "True") &&
3690 !_PyUnicode_EqualToASCIIString(name, "False"));
Benjamin Peterson70b224d2012-12-06 17:49:58 -05003691
Pablo Galindoc5fc1562020-04-22 23:29:27 +01003692 if (forbidden_name(c, name, ctx))
3693 return 0;
3694
Serhiy Storchakabd6ec4d2017-12-18 14:29:12 +02003695 mangled = _Py_Mangle(c->u->u_private, name);
3696 if (!mangled)
3697 return 0;
3698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003699 op = 0;
3700 optype = OP_NAME;
Victor Stinner28ad12f2021-03-19 12:41:49 +01003701 scope = _PyST_GetScope(c->u->u_ste, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003702 switch (scope) {
3703 case FREE:
3704 dict = c->u->u_freevars;
3705 optype = OP_DEREF;
3706 break;
3707 case CELL:
3708 dict = c->u->u_cellvars;
3709 optype = OP_DEREF;
3710 break;
3711 case LOCAL:
3712 if (c->u->u_ste->ste_type == FunctionBlock)
3713 optype = OP_FAST;
3714 break;
3715 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04003716 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003717 optype = OP_GLOBAL;
3718 break;
3719 case GLOBAL_EXPLICIT:
3720 optype = OP_GLOBAL;
3721 break;
3722 default:
3723 /* scope can be 0 */
3724 break;
3725 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003727 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003728 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 switch (optype) {
3731 case OP_DEREF:
3732 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003733 case Load:
3734 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3735 break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003736 case Store: op = STORE_DEREF; break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003737 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003738 }
3739 break;
3740 case OP_FAST:
3741 switch (ctx) {
3742 case Load: op = LOAD_FAST; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003743 case Store: op = STORE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003744 case Del: op = DELETE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003745 }
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003746 ADDOP_N(c, op, mangled, varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003747 return 1;
3748 case OP_GLOBAL:
3749 switch (ctx) {
3750 case Load: op = LOAD_GLOBAL; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003751 case Store: op = STORE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003752 case Del: op = DELETE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003753 }
3754 break;
3755 case OP_NAME:
3756 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00003757 case Load: op = LOAD_NAME; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003758 case Store: op = STORE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003759 case Del: op = DELETE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003760 }
3761 break;
3762 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003764 assert(op);
Andy Lester76d58772020-03-10 21:18:12 -05003765 arg = compiler_add_o(dict, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003766 Py_DECREF(mangled);
3767 if (arg < 0)
3768 return 0;
3769 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003770}
3771
3772static int
3773compiler_boolop(struct compiler *c, expr_ty e)
3774{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003775 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003776 int jumpi;
3777 Py_ssize_t i, n;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003778 asdl_expr_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003780 assert(e->kind == BoolOp_kind);
3781 if (e->v.BoolOp.op == And)
3782 jumpi = JUMP_IF_FALSE_OR_POP;
3783 else
3784 jumpi = JUMP_IF_TRUE_OR_POP;
3785 end = compiler_new_block(c);
3786 if (end == NULL)
3787 return 0;
3788 s = e->v.BoolOp.values;
3789 n = asdl_seq_LEN(s) - 1;
3790 assert(n >= 0);
3791 for (i = 0; i < n; ++i) {
3792 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003793 ADDOP_JUMP(c, jumpi, end);
Mark Shannon6e8128f2020-07-30 10:03:00 +01003794 basicblock *next = compiler_new_block(c);
3795 if (next == NULL) {
3796 return 0;
3797 }
3798 compiler_use_next_block(c, next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003799 }
3800 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3801 compiler_use_next_block(c, end);
3802 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003803}
3804
3805static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003806starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
Mark Shannon13bc1392020-01-23 09:25:17 +00003807 int build, int add, int extend, int tuple)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003808{
3809 Py_ssize_t n = asdl_seq_LEN(elts);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003810 if (n > 2 && are_all_items_const(elts, 0, n)) {
3811 PyObject *folded = PyTuple_New(n);
3812 if (folded == NULL) {
3813 return 0;
3814 }
3815 PyObject *val;
Mark Shannon11e0b292021-04-15 14:28:56 +01003816 for (Py_ssize_t i = 0; i < n; i++) {
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003817 val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
3818 Py_INCREF(val);
3819 PyTuple_SET_ITEM(folded, i, val);
3820 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003821 if (tuple) {
3822 ADDOP_LOAD_CONST_NEW(c, folded);
3823 } else {
3824 if (add == SET_ADD) {
3825 Py_SETREF(folded, PyFrozenSet_New(folded));
3826 if (folded == NULL) {
3827 return 0;
3828 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003829 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003830 ADDOP_I(c, build, pushed);
3831 ADDOP_LOAD_CONST_NEW(c, folded);
3832 ADDOP_I(c, extend, 1);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003833 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003834 return 1;
3835 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003836
Mark Shannon11e0b292021-04-15 14:28:56 +01003837 int big = n+pushed > STACK_USE_GUIDELINE;
3838 int seen_star = 0;
3839 for (Py_ssize_t i = 0; i < n; i++) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003840 expr_ty elt = asdl_seq_GET(elts, i);
3841 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003842 seen_star = 1;
3843 }
3844 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003845 if (!seen_star && !big) {
3846 for (Py_ssize_t i = 0; i < n; i++) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003847 expr_ty elt = asdl_seq_GET(elts, i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003848 VISIT(c, expr, elt);
Mark Shannon13bc1392020-01-23 09:25:17 +00003849 }
3850 if (tuple) {
3851 ADDOP_I(c, BUILD_TUPLE, n+pushed);
3852 } else {
3853 ADDOP_I(c, build, n+pushed);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003854 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003855 return 1;
3856 }
3857 int sequence_built = 0;
3858 if (big) {
3859 ADDOP_I(c, build, pushed);
3860 sequence_built = 1;
3861 }
3862 for (Py_ssize_t i = 0; i < n; i++) {
3863 expr_ty elt = asdl_seq_GET(elts, i);
3864 if (elt->kind == Starred_kind) {
3865 if (sequence_built == 0) {
3866 ADDOP_I(c, build, i+pushed);
3867 sequence_built = 1;
3868 }
3869 VISIT(c, expr, elt->v.Starred.value);
3870 ADDOP_I(c, extend, 1);
3871 }
3872 else {
3873 VISIT(c, expr, elt);
3874 if (sequence_built) {
3875 ADDOP_I(c, add, 1);
3876 }
3877 }
3878 }
3879 assert(sequence_built);
3880 if (tuple) {
3881 ADDOP(c, LIST_TO_TUPLE);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003882 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003883 return 1;
3884}
3885
3886static int
Brandt Bucher145bf262021-02-26 14:51:55 -08003887unpack_helper(struct compiler *c, asdl_expr_seq *elts)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003888{
3889 Py_ssize_t n = asdl_seq_LEN(elts);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003890 int seen_star = 0;
Brandt Bucher145bf262021-02-26 14:51:55 -08003891 for (Py_ssize_t i = 0; i < n; i++) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003892 expr_ty elt = asdl_seq_GET(elts, i);
3893 if (elt->kind == Starred_kind && !seen_star) {
3894 if ((i >= (1 << 8)) ||
3895 (n-i-1 >= (INT_MAX >> 8)))
3896 return compiler_error(c,
3897 "too many expressions in "
3898 "star-unpacking assignment");
3899 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3900 seen_star = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003901 }
3902 else if (elt->kind == Starred_kind) {
3903 return compiler_error(c,
Furkan Öndercb6534e2020-03-26 04:54:31 +03003904 "multiple starred expressions in assignment");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003905 }
3906 }
3907 if (!seen_star) {
3908 ADDOP_I(c, UNPACK_SEQUENCE, n);
3909 }
Brandt Bucher145bf262021-02-26 14:51:55 -08003910 return 1;
3911}
3912
3913static int
3914assignment_helper(struct compiler *c, asdl_expr_seq *elts)
3915{
3916 Py_ssize_t n = asdl_seq_LEN(elts);
3917 RETURN_IF_FALSE(unpack_helper(c, elts));
3918 for (Py_ssize_t i = 0; i < n; i++) {
Brandt Bucherd5aa2e92020-03-07 19:44:18 -08003919 expr_ty elt = asdl_seq_GET(elts, i);
3920 VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value);
3921 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003922 return 1;
3923}
3924
3925static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003926compiler_list(struct compiler *c, expr_ty e)
3927{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003928 asdl_expr_seq *elts = e->v.List.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003929 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003930 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003931 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003932 else if (e->v.List.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003933 return starunpack_helper(c, elts, 0, BUILD_LIST,
3934 LIST_APPEND, LIST_EXTEND, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003935 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003936 else
3937 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003938 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003939}
3940
3941static int
3942compiler_tuple(struct compiler *c, expr_ty e)
3943{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003944 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003945 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003946 return assignment_helper(c, elts);
3947 }
3948 else if (e->v.Tuple.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003949 return starunpack_helper(c, elts, 0, BUILD_LIST,
3950 LIST_APPEND, LIST_EXTEND, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003951 }
3952 else
3953 VISIT_SEQ(c, expr, elts);
3954 return 1;
3955}
3956
3957static int
3958compiler_set(struct compiler *c, expr_ty e)
3959{
Mark Shannon13bc1392020-01-23 09:25:17 +00003960 return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET,
3961 SET_ADD, SET_UPDATE, 0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003962}
3963
3964static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003965are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003966{
3967 Py_ssize_t i;
3968 for (i = begin; i < end; i++) {
3969 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003970 if (key == NULL || key->kind != Constant_kind)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003971 return 0;
3972 }
3973 return 1;
3974}
3975
3976static int
3977compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3978{
3979 Py_ssize_t i, n = end - begin;
3980 PyObject *keys, *key;
Mark Shannon11e0b292021-04-15 14:28:56 +01003981 int big = n*2 > STACK_USE_GUIDELINE;
3982 if (n > 1 && !big && are_all_items_const(e->v.Dict.keys, begin, end)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003983 for (i = begin; i < end; i++) {
3984 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3985 }
3986 keys = PyTuple_New(n);
3987 if (keys == NULL) {
3988 return 0;
3989 }
3990 for (i = begin; i < end; i++) {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003991 key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003992 Py_INCREF(key);
3993 PyTuple_SET_ITEM(keys, i - begin, key);
3994 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003995 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003996 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
Mark Shannon11e0b292021-04-15 14:28:56 +01003997 return 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003998 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003999 if (big) {
4000 ADDOP_I(c, BUILD_MAP, 0);
4001 }
4002 for (i = begin; i < end; i++) {
4003 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
4004 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
4005 if (big) {
4006 ADDOP_I(c, MAP_ADD, 1);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004007 }
Mark Shannon11e0b292021-04-15 14:28:56 +01004008 }
4009 if (!big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004010 ADDOP_I(c, BUILD_MAP, n);
4011 }
4012 return 1;
4013}
4014
4015static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004016compiler_dict(struct compiler *c, expr_ty e)
4017{
Victor Stinner976bb402016-03-23 11:36:19 +01004018 Py_ssize_t i, n, elements;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004019 int have_dict;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004020 int is_unpacking = 0;
4021 n = asdl_seq_LEN(e->v.Dict.values);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004022 have_dict = 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004023 elements = 0;
4024 for (i = 0; i < n; i++) {
4025 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004026 if (is_unpacking) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004027 if (elements) {
4028 if (!compiler_subdict(c, e, i - elements, i)) {
4029 return 0;
4030 }
4031 if (have_dict) {
4032 ADDOP_I(c, DICT_UPDATE, 1);
4033 }
4034 have_dict = 1;
4035 elements = 0;
4036 }
4037 if (have_dict == 0) {
4038 ADDOP_I(c, BUILD_MAP, 0);
4039 have_dict = 1;
4040 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004041 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Mark Shannon8a4cd702020-01-27 09:57:45 +00004042 ADDOP_I(c, DICT_UPDATE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004043 }
4044 else {
Mark Shannon11e0b292021-04-15 14:28:56 +01004045 if (elements*2 > STACK_USE_GUIDELINE) {
Pablo Galindoc51db0e2020-08-13 09:48:41 +01004046 if (!compiler_subdict(c, e, i - elements, i + 1)) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004047 return 0;
4048 }
4049 if (have_dict) {
4050 ADDOP_I(c, DICT_UPDATE, 1);
4051 }
4052 have_dict = 1;
4053 elements = 0;
4054 }
4055 else {
4056 elements++;
4057 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004058 }
4059 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004060 if (elements) {
4061 if (!compiler_subdict(c, e, n - elements, n)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004062 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004063 }
4064 if (have_dict) {
4065 ADDOP_I(c, DICT_UPDATE, 1);
4066 }
4067 have_dict = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004068 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004069 if (!have_dict) {
4070 ADDOP_I(c, BUILD_MAP, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004071 }
4072 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004073}
4074
4075static int
4076compiler_compare(struct compiler *c, expr_ty e)
4077{
Victor Stinnerad9a0662013-11-19 22:23:20 +01004078 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004079
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02004080 if (!check_compare(c, e)) {
4081 return 0;
4082 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004083 VISIT(c, expr, e->v.Compare.left);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004084 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
4085 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
4086 if (n == 0) {
4087 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Mark Shannon9af0e472020-01-14 10:12:45 +00004088 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004089 }
4090 else {
4091 basicblock *cleanup = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004092 if (cleanup == NULL)
4093 return 0;
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004094 for (i = 0; i < n; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004095 VISIT(c, expr,
4096 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004097 ADDOP(c, DUP_TOP);
4098 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00004099 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01004100 ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004101 NEXT_BLOCK(c);
4102 }
4103 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00004104 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004105 basicblock *end = compiler_new_block(c);
4106 if (end == NULL)
4107 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00004108 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004109 compiler_use_next_block(c, cleanup);
4110 ADDOP(c, ROT_TWO);
4111 ADDOP(c, POP_TOP);
4112 compiler_use_next_block(c, end);
4113 }
4114 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004115}
4116
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004117static PyTypeObject *
4118infer_type(expr_ty e)
4119{
4120 switch (e->kind) {
4121 case Tuple_kind:
4122 return &PyTuple_Type;
4123 case List_kind:
4124 case ListComp_kind:
4125 return &PyList_Type;
4126 case Dict_kind:
4127 case DictComp_kind:
4128 return &PyDict_Type;
4129 case Set_kind:
4130 case SetComp_kind:
4131 return &PySet_Type;
4132 case GeneratorExp_kind:
4133 return &PyGen_Type;
4134 case Lambda_kind:
4135 return &PyFunction_Type;
4136 case JoinedStr_kind:
4137 case FormattedValue_kind:
4138 return &PyUnicode_Type;
4139 case Constant_kind:
Victor Stinnera102ed72020-02-07 02:24:48 +01004140 return Py_TYPE(e->v.Constant.value);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004141 default:
4142 return NULL;
4143 }
4144}
4145
4146static int
4147check_caller(struct compiler *c, expr_ty e)
4148{
4149 switch (e->kind) {
4150 case Constant_kind:
4151 case Tuple_kind:
4152 case List_kind:
4153 case ListComp_kind:
4154 case Dict_kind:
4155 case DictComp_kind:
4156 case Set_kind:
4157 case SetComp_kind:
4158 case GeneratorExp_kind:
4159 case JoinedStr_kind:
4160 case FormattedValue_kind:
4161 return compiler_warn(c, "'%.200s' object is not callable; "
4162 "perhaps you missed a comma?",
4163 infer_type(e)->tp_name);
4164 default:
4165 return 1;
4166 }
4167}
4168
4169static int
4170check_subscripter(struct compiler *c, expr_ty e)
4171{
4172 PyObject *v;
4173
4174 switch (e->kind) {
4175 case Constant_kind:
4176 v = e->v.Constant.value;
4177 if (!(v == Py_None || v == Py_Ellipsis ||
4178 PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
4179 PyAnySet_Check(v)))
4180 {
4181 return 1;
4182 }
4183 /* fall through */
4184 case Set_kind:
4185 case SetComp_kind:
4186 case GeneratorExp_kind:
4187 case Lambda_kind:
4188 return compiler_warn(c, "'%.200s' object is not subscriptable; "
4189 "perhaps you missed a comma?",
4190 infer_type(e)->tp_name);
4191 default:
4192 return 1;
4193 }
4194}
4195
4196static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004197check_index(struct compiler *c, expr_ty e, expr_ty s)
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004198{
4199 PyObject *v;
4200
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004201 PyTypeObject *index_type = infer_type(s);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004202 if (index_type == NULL
4203 || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
4204 || index_type == &PySlice_Type) {
4205 return 1;
4206 }
4207
4208 switch (e->kind) {
4209 case Constant_kind:
4210 v = e->v.Constant.value;
4211 if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
4212 return 1;
4213 }
4214 /* fall through */
4215 case Tuple_kind:
4216 case List_kind:
4217 case ListComp_kind:
4218 case JoinedStr_kind:
4219 case FormattedValue_kind:
4220 return compiler_warn(c, "%.200s indices must be integers or slices, "
4221 "not %.200s; "
4222 "perhaps you missed a comma?",
4223 infer_type(e)->tp_name,
4224 index_type->tp_name);
4225 default:
4226 return 1;
4227 }
4228}
4229
Zackery Spytz97f5de02019-03-22 01:30:32 -06004230// Return 1 if the method call was optimized, -1 if not, and 0 on error.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004231static int
Yury Selivanovf2392132016-12-13 19:03:51 -05004232maybe_optimize_method_call(struct compiler *c, expr_ty e)
4233{
4234 Py_ssize_t argsl, i;
4235 expr_ty meth = e->v.Call.func;
Pablo Galindoa5634c42020-09-16 19:42:00 +01004236 asdl_expr_seq *args = e->v.Call.args;
Yury Selivanovf2392132016-12-13 19:03:51 -05004237
4238 /* Check that the call node is an attribute access, and that
4239 the call doesn't have keyword parameters. */
4240 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
Mark Shannon11e0b292021-04-15 14:28:56 +01004241 asdl_seq_LEN(e->v.Call.keywords)) {
Yury Selivanovf2392132016-12-13 19:03:51 -05004242 return -1;
Mark Shannon11e0b292021-04-15 14:28:56 +01004243 }
4244 /* Check that there aren't too many arguments */
Yury Selivanovf2392132016-12-13 19:03:51 -05004245 argsl = asdl_seq_LEN(args);
Mark Shannon11e0b292021-04-15 14:28:56 +01004246 if (argsl >= STACK_USE_GUIDELINE) {
4247 return -1;
4248 }
4249 /* Check that there are no *varargs types of arguments. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004250 for (i = 0; i < argsl; i++) {
4251 expr_ty elt = asdl_seq_GET(args, i);
4252 if (elt->kind == Starred_kind) {
4253 return -1;
4254 }
4255 }
4256
4257 /* Alright, we can optimize the code. */
4258 VISIT(c, expr, meth->v.Attribute.value);
Mark Shannond48848c2021-03-14 18:01:30 +00004259 int old_lineno = c->u->u_lineno;
4260 c->u->u_lineno = meth->end_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004261 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
4262 VISIT_SEQ(c, expr, e->v.Call.args);
4263 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
Mark Shannond48848c2021-03-14 18:01:30 +00004264 c->u->u_lineno = old_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004265 return 1;
4266}
4267
4268static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004269validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
Zackery Spytz08050e92020-04-06 00:47:47 -06004270{
4271 Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
4272 for (Py_ssize_t i = 0; i < nkeywords; i++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004273 keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
4274 if (key->arg == NULL) {
4275 continue;
4276 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +01004277 if (forbidden_name(c, key->arg, Store)) {
4278 return -1;
4279 }
Zackery Spytz08050e92020-04-06 00:47:47 -06004280 for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004281 keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
4282 if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07004283 SET_LOC(c, other);
Brandt Bucher145bf262021-02-26 14:51:55 -08004284 compiler_error(c, "keyword argument repeated: %U", key->arg);
Pablo Galindo254ec782020-04-03 20:37:13 +01004285 return -1;
4286 }
4287 }
4288 }
4289 return 0;
4290}
4291
4292static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004293compiler_call(struct compiler *c, expr_ty e)
4294{
Zackery Spytz97f5de02019-03-22 01:30:32 -06004295 int ret = maybe_optimize_method_call(c, e);
4296 if (ret >= 0) {
4297 return ret;
4298 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004299 if (!check_caller(c, e->v.Call.func)) {
4300 return 0;
4301 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004302 VISIT(c, expr, e->v.Call.func);
4303 return compiler_call_helper(c, 0,
4304 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004305 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004306}
4307
Eric V. Smith235a6f02015-09-19 14:51:32 -04004308static int
4309compiler_joined_str(struct compiler *c, expr_ty e)
4310{
Mark Shannon11e0b292021-04-15 14:28:56 +01004311
4312 Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values);
4313 if (value_count > STACK_USE_GUIDELINE) {
4314 ADDOP_LOAD_CONST_NEW(c, _PyUnicode_FromASCII("", 0));
4315 PyObject *join = _PyUnicode_FromASCII("join", 4);
4316 if (join == NULL) {
4317 return 0;
4318 }
4319 ADDOP_NAME(c, LOAD_METHOD, join, names);
4320 Py_DECREF(join);
4321 ADDOP_I(c, BUILD_LIST, 0);
4322 for (Py_ssize_t i = 0; i < asdl_seq_LEN(e->v.JoinedStr.values); i++) {
4323 VISIT(c, expr, asdl_seq_GET(e->v.JoinedStr.values, i));
4324 ADDOP_I(c, LIST_APPEND, 1);
4325 }
4326 ADDOP_I(c, CALL_METHOD, 1);
4327 }
4328 else {
4329 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
4330 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) {
4331 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
4332 }
4333 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004334 return 1;
4335}
4336
Eric V. Smitha78c7952015-11-03 12:45:05 -05004337/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004338static int
4339compiler_formatted_value(struct compiler *c, expr_ty e)
4340{
Eric V. Smitha78c7952015-11-03 12:45:05 -05004341 /* Our oparg encodes 2 pieces of information: the conversion
4342 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04004343
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004344 Convert the conversion char to 3 bits:
4345 : 000 0x0 FVC_NONE The default if nothing specified.
Eric V. Smitha78c7952015-11-03 12:45:05 -05004346 !s : 001 0x1 FVC_STR
4347 !r : 010 0x2 FVC_REPR
4348 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04004349
Eric V. Smitha78c7952015-11-03 12:45:05 -05004350 next bit is whether or not we have a format spec:
4351 yes : 100 0x4
4352 no : 000 0x0
4353 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004354
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004355 int conversion = e->v.FormattedValue.conversion;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004356 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004357
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004358 /* The expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004359 VISIT(c, expr, e->v.FormattedValue.value);
4360
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004361 switch (conversion) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004362 case 's': oparg = FVC_STR; break;
4363 case 'r': oparg = FVC_REPR; break;
4364 case 'a': oparg = FVC_ASCII; break;
4365 case -1: oparg = FVC_NONE; break;
4366 default:
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004367 PyErr_Format(PyExc_SystemError,
4368 "Unrecognized conversion character %d", conversion);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004369 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004370 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004371 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004372 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004373 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004374 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004375 }
4376
Eric V. Smitha78c7952015-11-03 12:45:05 -05004377 /* And push our opcode and oparg */
4378 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004379
Eric V. Smith235a6f02015-09-19 14:51:32 -04004380 return 1;
4381}
4382
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004383static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004384compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004385{
4386 Py_ssize_t i, n = end - begin;
4387 keyword_ty kw;
4388 PyObject *keys, *key;
4389 assert(n > 0);
Mark Shannon11e0b292021-04-15 14:28:56 +01004390 int big = n*2 > STACK_USE_GUIDELINE;
4391 if (n > 1 && !big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004392 for (i = begin; i < end; i++) {
4393 kw = asdl_seq_GET(keywords, i);
4394 VISIT(c, expr, kw->value);
4395 }
4396 keys = PyTuple_New(n);
4397 if (keys == NULL) {
4398 return 0;
4399 }
4400 for (i = begin; i < end; i++) {
4401 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
4402 Py_INCREF(key);
4403 PyTuple_SET_ITEM(keys, i - begin, key);
4404 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004405 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004406 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
Mark Shannon11e0b292021-04-15 14:28:56 +01004407 return 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004408 }
Mark Shannon11e0b292021-04-15 14:28:56 +01004409 if (big) {
4410 ADDOP_I_NOLINE(c, BUILD_MAP, 0);
4411 }
4412 for (i = begin; i < end; i++) {
4413 kw = asdl_seq_GET(keywords, i);
4414 ADDOP_LOAD_CONST(c, kw->arg);
4415 VISIT(c, expr, kw->value);
4416 if (big) {
4417 ADDOP_I_NOLINE(c, MAP_ADD, 1);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004418 }
Mark Shannon11e0b292021-04-15 14:28:56 +01004419 }
4420 if (!big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004421 ADDOP_I(c, BUILD_MAP, n);
4422 }
4423 return 1;
4424}
4425
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004426/* shared code between compiler_call and compiler_class */
4427static int
4428compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01004429 int n, /* Args already pushed */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004430 asdl_expr_seq *args,
4431 asdl_keyword_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004432{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004433 Py_ssize_t i, nseen, nelts, nkwelts;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004434
Pablo Galindo254ec782020-04-03 20:37:13 +01004435 if (validate_keywords(c, keywords) == -1) {
4436 return 0;
4437 }
4438
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004439 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004440 nkwelts = asdl_seq_LEN(keywords);
4441
Mark Shannon11e0b292021-04-15 14:28:56 +01004442 if (nelts + nkwelts*2 > STACK_USE_GUIDELINE) {
4443 goto ex_call;
4444 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004445 for (i = 0; i < nelts; i++) {
4446 expr_ty elt = asdl_seq_GET(args, i);
4447 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00004448 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004449 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004450 }
4451 for (i = 0; i < nkwelts; i++) {
4452 keyword_ty kw = asdl_seq_GET(keywords, i);
4453 if (kw->arg == NULL) {
4454 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004455 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004456 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004457
Mark Shannon13bc1392020-01-23 09:25:17 +00004458 /* No * or ** args, so can use faster calling sequence */
4459 for (i = 0; i < nelts; i++) {
4460 expr_ty elt = asdl_seq_GET(args, i);
4461 assert(elt->kind != Starred_kind);
4462 VISIT(c, expr, elt);
4463 }
4464 if (nkwelts) {
4465 PyObject *names;
4466 VISIT_SEQ(c, keyword, keywords);
4467 names = PyTuple_New(nkwelts);
4468 if (names == NULL) {
4469 return 0;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004470 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004471 for (i = 0; i < nkwelts; i++) {
4472 keyword_ty kw = asdl_seq_GET(keywords, i);
4473 Py_INCREF(kw->arg);
4474 PyTuple_SET_ITEM(names, i, kw->arg);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004475 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004476 ADDOP_LOAD_CONST_NEW(c, names);
4477 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
4478 return 1;
4479 }
4480 else {
4481 ADDOP_I(c, CALL_FUNCTION, n + nelts);
4482 return 1;
4483 }
4484
4485ex_call:
4486
4487 /* Do positional arguments. */
4488 if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
4489 VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
4490 }
4491 else if (starunpack_helper(c, args, n, BUILD_LIST,
4492 LIST_APPEND, LIST_EXTEND, 1) == 0) {
4493 return 0;
4494 }
4495 /* Then keyword arguments */
4496 if (nkwelts) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004497 /* Has a new dict been pushed */
4498 int have_dict = 0;
Mark Shannon13bc1392020-01-23 09:25:17 +00004499
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004500 nseen = 0; /* the number of keyword arguments on the stack following */
4501 for (i = 0; i < nkwelts; i++) {
4502 keyword_ty kw = asdl_seq_GET(keywords, i);
4503 if (kw->arg == NULL) {
4504 /* A keyword argument unpacking. */
4505 if (nseen) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004506 if (!compiler_subkwargs(c, keywords, i - nseen, i)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004507 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004508 }
Mark Shannondb64f122020-06-01 10:42:42 +01004509 if (have_dict) {
4510 ADDOP_I(c, DICT_MERGE, 1);
4511 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004512 have_dict = 1;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004513 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004514 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004515 if (!have_dict) {
4516 ADDOP_I(c, BUILD_MAP, 0);
4517 have_dict = 1;
4518 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004519 VISIT(c, expr, kw->value);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004520 ADDOP_I(c, DICT_MERGE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004521 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004522 else {
4523 nseen++;
4524 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004525 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004526 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004527 /* Pack up any trailing keyword arguments. */
Mark Shannon8a4cd702020-01-27 09:57:45 +00004528 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004529 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004530 }
4531 if (have_dict) {
4532 ADDOP_I(c, DICT_MERGE, 1);
4533 }
4534 have_dict = 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004535 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004536 assert(have_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004537 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004538 ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0);
4539 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004540}
4541
Nick Coghlan650f0d02007-04-15 12:05:43 +00004542
4543/* List and set comprehensions and generator expressions work by creating a
4544 nested function to perform the actual iteration. This means that the
4545 iteration variables don't leak into the current scope.
4546 The defined function is called immediately following its definition, with the
4547 result of that call being the result of the expression.
4548 The LC/SC version returns the populated container, while the GE version is
4549 flagged in symtable.c as a generator, so it returns the generator object
4550 when the function is called.
Nick Coghlan650f0d02007-04-15 12:05:43 +00004551
4552 Possible cleanups:
4553 - iterate over the generator sequence instead of using recursion
4554*/
4555
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004556
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004557static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004558compiler_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004559 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004560 int depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004561 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004562{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004563 comprehension_ty gen;
4564 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4565 if (gen->is_async) {
4566 return compiler_async_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004567 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004568 } else {
4569 return compiler_sync_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004570 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004571 }
4572}
4573
4574static int
4575compiler_sync_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004576 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004577 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004578 expr_ty elt, expr_ty val, int type)
4579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004580 /* generate code for the iterator, then each of the ifs,
4581 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004583 comprehension_ty gen;
4584 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004585 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004587 start = compiler_new_block(c);
4588 skip = compiler_new_block(c);
4589 if_cleanup = compiler_new_block(c);
4590 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004592 if (start == NULL || skip == NULL || if_cleanup == NULL ||
4593 anchor == NULL)
4594 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004596 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004598 if (gen_index == 0) {
4599 /* Receive outermost iter as an implicit argument */
4600 c->u->u_argcount = 1;
4601 ADDOP_I(c, LOAD_FAST, 0);
4602 }
4603 else {
4604 /* Sub-iter - calculate on the fly */
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004605 /* Fast path for the temporary variable assignment idiom:
4606 for y in [f(x)]
4607 */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004608 asdl_expr_seq *elts;
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004609 switch (gen->iter->kind) {
4610 case List_kind:
4611 elts = gen->iter->v.List.elts;
4612 break;
4613 case Tuple_kind:
4614 elts = gen->iter->v.Tuple.elts;
4615 break;
4616 default:
4617 elts = NULL;
4618 }
4619 if (asdl_seq_LEN(elts) == 1) {
4620 expr_ty elt = asdl_seq_GET(elts, 0);
4621 if (elt->kind != Starred_kind) {
4622 VISIT(c, expr, elt);
4623 start = NULL;
4624 }
4625 }
4626 if (start) {
4627 VISIT(c, expr, gen->iter);
4628 ADDOP(c, GET_ITER);
4629 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004630 }
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004631 if (start) {
4632 depth++;
4633 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01004634 ADDOP_JUMP(c, FOR_ITER, anchor);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004635 NEXT_BLOCK(c);
4636 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004637 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004639 /* XXX this needs to be cleaned up...a lot! */
4640 n = asdl_seq_LEN(gen->ifs);
4641 for (i = 0; i < n; i++) {
4642 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004643 if (!compiler_jump_if(c, e, if_cleanup, 0))
4644 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004645 NEXT_BLOCK(c);
4646 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004648 if (++gen_index < asdl_seq_LEN(generators))
4649 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004650 generators, gen_index, depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004651 elt, val, type))
4652 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004654 /* only append after the last for generator */
4655 if (gen_index >= asdl_seq_LEN(generators)) {
4656 /* comprehension specific code */
4657 switch (type) {
4658 case COMP_GENEXP:
4659 VISIT(c, expr, elt);
4660 ADDOP(c, YIELD_VALUE);
4661 ADDOP(c, POP_TOP);
4662 break;
4663 case COMP_LISTCOMP:
4664 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004665 ADDOP_I(c, LIST_APPEND, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004666 break;
4667 case COMP_SETCOMP:
4668 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004669 ADDOP_I(c, SET_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004670 break;
4671 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004672 /* With '{k: v}', k is evaluated before v, so we do
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004673 the same. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004674 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004675 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004676 ADDOP_I(c, MAP_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004677 break;
4678 default:
4679 return 0;
4680 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004682 compiler_use_next_block(c, skip);
4683 }
4684 compiler_use_next_block(c, if_cleanup);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004685 if (start) {
Mark Shannon582aaf12020-08-04 17:30:11 +01004686 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004687 compiler_use_next_block(c, anchor);
4688 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004689
4690 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004691}
4692
4693static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004694compiler_async_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004695 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004696 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004697 expr_ty elt, expr_ty val, int type)
4698{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004699 comprehension_ty gen;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004700 basicblock *start, *if_cleanup, *except;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004701 Py_ssize_t i, n;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004702 start = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004703 except = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004704 if_cleanup = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004705
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004706 if (start == NULL || if_cleanup == NULL || except == NULL) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004707 return 0;
4708 }
4709
4710 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4711
4712 if (gen_index == 0) {
4713 /* Receive outermost iter as an implicit argument */
4714 c->u->u_argcount = 1;
4715 ADDOP_I(c, LOAD_FAST, 0);
4716 }
4717 else {
4718 /* Sub-iter - calculate on the fly */
4719 VISIT(c, expr, gen->iter);
4720 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004721 }
4722
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004723 compiler_use_next_block(c, start);
tomKPZ7a7ba3d2021-04-07 07:43:45 -07004724 /* Runtime will push a block here, so we need to account for that */
4725 if (!compiler_push_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start,
4726 NULL, NULL)) {
4727 return 0;
4728 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004729
Mark Shannon582aaf12020-08-04 17:30:11 +01004730 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004731 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004732 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004733 ADDOP(c, YIELD_FROM);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004734 ADDOP(c, POP_BLOCK);
Serhiy Storchaka24d32012018-03-10 18:22:34 +02004735 VISIT(c, expr, gen->target);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004736
4737 n = asdl_seq_LEN(gen->ifs);
4738 for (i = 0; i < n; i++) {
4739 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004740 if (!compiler_jump_if(c, e, if_cleanup, 0))
4741 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004742 NEXT_BLOCK(c);
4743 }
4744
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004745 depth++;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004746 if (++gen_index < asdl_seq_LEN(generators))
4747 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004748 generators, gen_index, depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004749 elt, val, type))
4750 return 0;
4751
4752 /* only append after the last for generator */
4753 if (gen_index >= asdl_seq_LEN(generators)) {
4754 /* comprehension specific code */
4755 switch (type) {
4756 case COMP_GENEXP:
4757 VISIT(c, expr, elt);
4758 ADDOP(c, YIELD_VALUE);
4759 ADDOP(c, POP_TOP);
4760 break;
4761 case COMP_LISTCOMP:
4762 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004763 ADDOP_I(c, LIST_APPEND, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004764 break;
4765 case COMP_SETCOMP:
4766 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004767 ADDOP_I(c, SET_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004768 break;
4769 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004770 /* With '{k: v}', k is evaluated before v, so we do
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004771 the same. */
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004772 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004773 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004774 ADDOP_I(c, MAP_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004775 break;
4776 default:
4777 return 0;
4778 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004779 }
4780 compiler_use_next_block(c, if_cleanup);
Mark Shannon582aaf12020-08-04 17:30:11 +01004781 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004782
tomKPZ7a7ba3d2021-04-07 07:43:45 -07004783 compiler_pop_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start);
4784
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004785 compiler_use_next_block(c, except);
4786 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004787
4788 return 1;
4789}
4790
4791static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004792compiler_comprehension(struct compiler *c, expr_ty e, int type,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004793 identifier name, asdl_comprehension_seq *generators, expr_ty elt,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004794 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004795{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004796 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004797 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004798 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004799 int is_async_generator = 0;
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004800 int top_level_await = IS_TOP_LEVEL_AWAIT(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004801
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004802
Batuhan Taßkaya9052f7a2020-03-19 14:35:44 +03004803 int is_async_function = c->u->u_ste->ste_coroutine;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004804
Batuhan Taßkaya9052f7a2020-03-19 14:35:44 +03004805 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004806 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4807 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004808 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004809 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004810 }
Mark Shannon7674c832021-06-21 11:47:16 +01004811 SET_LOC(c, e);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004812
4813 is_async_generator = c->u->u_ste->ste_coroutine;
4814
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004815 if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004816 compiler_error(c, "asynchronous comprehension outside of "
4817 "an asynchronous function");
4818 goto error_in_scope;
4819 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004821 if (type != COMP_GENEXP) {
4822 int op;
4823 switch (type) {
4824 case COMP_LISTCOMP:
4825 op = BUILD_LIST;
4826 break;
4827 case COMP_SETCOMP:
4828 op = BUILD_SET;
4829 break;
4830 case COMP_DICTCOMP:
4831 op = BUILD_MAP;
4832 break;
4833 default:
4834 PyErr_Format(PyExc_SystemError,
4835 "unknown comprehension type %d", type);
4836 goto error_in_scope;
4837 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004839 ADDOP_I(c, op, 0);
4840 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004841
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004842 if (!compiler_comprehension_generator(c, generators, 0, 0, elt,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004843 val, type))
4844 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004846 if (type != COMP_GENEXP) {
4847 ADDOP(c, RETURN_VALUE);
4848 }
4849
4850 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004851 qualname = c->u->u_qualname;
4852 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004853 compiler_exit_scope(c);
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004854 if (top_level_await && is_async_generator){
4855 c->u->u_ste->ste_coroutine = 1;
4856 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004857 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004858 goto error;
4859
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004860 if (!compiler_make_closure(c, co, 0, qualname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004861 goto error;
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004862 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004863 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004864 Py_DECREF(co);
4865
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004866 VISIT(c, expr, outermost->iter);
4867
4868 if (outermost->is_async) {
4869 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004870 } else {
4871 ADDOP(c, GET_ITER);
4872 }
4873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004874 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004875
4876 if (is_async_generator && type != COMP_GENEXP) {
4877 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004878 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004879 ADDOP(c, YIELD_FROM);
4880 }
4881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004882 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004883error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004884 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004885error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004886 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004887 Py_XDECREF(co);
4888 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004889}
4890
4891static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004892compiler_genexp(struct compiler *c, expr_ty e)
4893{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004894 static identifier name;
4895 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004896 name = PyUnicode_InternFromString("<genexpr>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004897 if (!name)
4898 return 0;
4899 }
4900 assert(e->kind == GeneratorExp_kind);
4901 return compiler_comprehension(c, e, COMP_GENEXP, name,
4902 e->v.GeneratorExp.generators,
4903 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004904}
4905
4906static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004907compiler_listcomp(struct compiler *c, expr_ty e)
4908{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004909 static identifier name;
4910 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004911 name = PyUnicode_InternFromString("<listcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004912 if (!name)
4913 return 0;
4914 }
4915 assert(e->kind == ListComp_kind);
4916 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4917 e->v.ListComp.generators,
4918 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004919}
4920
4921static int
4922compiler_setcomp(struct compiler *c, expr_ty e)
4923{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004924 static identifier name;
4925 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004926 name = PyUnicode_InternFromString("<setcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004927 if (!name)
4928 return 0;
4929 }
4930 assert(e->kind == SetComp_kind);
4931 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4932 e->v.SetComp.generators,
4933 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004934}
4935
4936
4937static int
4938compiler_dictcomp(struct compiler *c, expr_ty e)
4939{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004940 static identifier name;
4941 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004942 name = PyUnicode_InternFromString("<dictcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004943 if (!name)
4944 return 0;
4945 }
4946 assert(e->kind == DictComp_kind);
4947 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4948 e->v.DictComp.generators,
4949 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004950}
4951
4952
4953static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004954compiler_visit_keyword(struct compiler *c, keyword_ty k)
4955{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004956 VISIT(c, expr, k->value);
4957 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004958}
4959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004960/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004961 whether they are true or false.
4962
4963 Return values: 1 for true, 0 for false, -1 for non-constant.
4964 */
4965
4966static int
Mark Shannonfee55262019-11-21 09:11:43 +00004967compiler_with_except_finish(struct compiler *c) {
4968 basicblock *exit;
4969 exit = compiler_new_block(c);
4970 if (exit == NULL)
4971 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01004972 ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit);
Mark Shannon266b4622020-11-17 19:30:14 +00004973 NEXT_BLOCK(c);
Mark Shannonbf353f32020-12-17 13:55:28 +00004974 ADDOP_I(c, RERAISE, 1);
Mark Shannonfee55262019-11-21 09:11:43 +00004975 compiler_use_next_block(c, exit);
4976 ADDOP(c, POP_TOP);
4977 ADDOP(c, POP_TOP);
4978 ADDOP(c, POP_TOP);
4979 ADDOP(c, POP_EXCEPT);
4980 ADDOP(c, POP_TOP);
4981 return 1;
4982}
Yury Selivanov75445082015-05-11 22:57:16 -04004983
4984/*
4985 Implements the async with statement.
4986
4987 The semantics outlined in that PEP are as follows:
4988
4989 async with EXPR as VAR:
4990 BLOCK
4991
4992 It is implemented roughly as:
4993
4994 context = EXPR
4995 exit = context.__aexit__ # not calling it
4996 value = await context.__aenter__()
4997 try:
4998 VAR = value # if VAR present in the syntax
4999 BLOCK
5000 finally:
5001 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03005002 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04005003 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03005004 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04005005 if not (await exit(*exc)):
5006 raise
5007 */
5008static int
5009compiler_async_with(struct compiler *c, stmt_ty s, int pos)
5010{
Mark Shannonfee55262019-11-21 09:11:43 +00005011 basicblock *block, *final, *exit;
Yury Selivanov75445082015-05-11 22:57:16 -04005012 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
5013
5014 assert(s->kind == AsyncWith_kind);
Pablo Galindo90235812020-03-15 04:29:22 +00005015 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005016 c->u->u_ste->ste_coroutine = 1;
5017 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
Zsolt Dollensteine2396502018-04-27 08:58:56 -07005018 return compiler_error(c, "'async with' outside async function");
5019 }
Yury Selivanov75445082015-05-11 22:57:16 -04005020
5021 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00005022 final = compiler_new_block(c);
5023 exit = compiler_new_block(c);
5024 if (!block || !final || !exit)
Yury Selivanov75445082015-05-11 22:57:16 -04005025 return 0;
5026
5027 /* Evaluate EXPR */
5028 VISIT(c, expr, item->context_expr);
5029
5030 ADDOP(c, BEFORE_ASYNC_WITH);
5031 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005032 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005033 ADDOP(c, YIELD_FROM);
5034
Mark Shannon582aaf12020-08-04 17:30:11 +01005035 ADDOP_JUMP(c, SETUP_ASYNC_WITH, final);
Yury Selivanov75445082015-05-11 22:57:16 -04005036
5037 /* SETUP_ASYNC_WITH pushes a finally block. */
5038 compiler_use_next_block(c, block);
Mark Shannon5979e812021-04-30 14:32:47 +01005039 if (!compiler_push_fblock(c, ASYNC_WITH, block, final, s)) {
Yury Selivanov75445082015-05-11 22:57:16 -04005040 return 0;
5041 }
5042
5043 if (item->optional_vars) {
5044 VISIT(c, expr, item->optional_vars);
5045 }
5046 else {
5047 /* Discard result from context.__aenter__() */
5048 ADDOP(c, POP_TOP);
5049 }
5050
5051 pos++;
5052 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
5053 /* BLOCK code */
5054 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
5055 else if (!compiler_async_with(c, s, pos))
5056 return 0;
5057
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005058 compiler_pop_fblock(c, ASYNC_WITH, block);
Mark Shannonfee55262019-11-21 09:11:43 +00005059 ADDOP(c, POP_BLOCK);
5060 /* End of body; start the cleanup */
Yury Selivanov75445082015-05-11 22:57:16 -04005061
Mark Shannonfee55262019-11-21 09:11:43 +00005062 /* For successful outcome:
5063 * call __exit__(None, None, None)
5064 */
Mark Shannon5979e812021-04-30 14:32:47 +01005065 SET_LOC(c, s);
Mark Shannonfee55262019-11-21 09:11:43 +00005066 if(!compiler_call_exit_with_nones(c))
Yury Selivanov75445082015-05-11 22:57:16 -04005067 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00005068 ADDOP(c, GET_AWAITABLE);
Miss Islington (bot)ebbd0ac2021-08-31 11:08:32 -07005069 ADDOP_LOAD_CONST(c, Py_None);
Mark Shannonfee55262019-11-21 09:11:43 +00005070 ADDOP(c, YIELD_FROM);
Yury Selivanov75445082015-05-11 22:57:16 -04005071
Mark Shannonfee55262019-11-21 09:11:43 +00005072 ADDOP(c, POP_TOP);
Yury Selivanov75445082015-05-11 22:57:16 -04005073
Mark Shannon582aaf12020-08-04 17:30:11 +01005074 ADDOP_JUMP(c, JUMP_ABSOLUTE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00005075
5076 /* For exceptional outcome: */
5077 compiler_use_next_block(c, final);
Mark Shannonfee55262019-11-21 09:11:43 +00005078 ADDOP(c, WITH_EXCEPT_START);
Yury Selivanov75445082015-05-11 22:57:16 -04005079 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005080 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005081 ADDOP(c, YIELD_FROM);
Mark Shannonfee55262019-11-21 09:11:43 +00005082 compiler_with_except_finish(c);
Yury Selivanov75445082015-05-11 22:57:16 -04005083
Mark Shannonfee55262019-11-21 09:11:43 +00005084compiler_use_next_block(c, exit);
Yury Selivanov75445082015-05-11 22:57:16 -04005085 return 1;
5086}
5087
5088
Guido van Rossumc2e20742006-02-27 22:32:47 +00005089/*
5090 Implements the with statement from PEP 343.
Guido van Rossumc2e20742006-02-27 22:32:47 +00005091 with EXPR as VAR:
5092 BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00005093 is implemented as:
5094 <code for EXPR>
5095 SETUP_WITH E
5096 <code to store to VAR> or POP_TOP
5097 <code for BLOCK>
5098 LOAD_CONST (None, None, None)
5099 CALL_FUNCTION_EX 0
5100 JUMP_FORWARD EXIT
5101 E: WITH_EXCEPT_START (calls EXPR.__exit__)
5102 POP_JUMP_IF_TRUE T:
5103 RERAISE
5104 T: POP_TOP * 3 (remove exception from stack)
5105 POP_EXCEPT
5106 POP_TOP
5107 EXIT:
Guido van Rossumc2e20742006-02-27 22:32:47 +00005108 */
Mark Shannonfee55262019-11-21 09:11:43 +00005109
Guido van Rossumc2e20742006-02-27 22:32:47 +00005110static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005111compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00005112{
Mark Shannonfee55262019-11-21 09:11:43 +00005113 basicblock *block, *final, *exit;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005114 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005115
5116 assert(s->kind == With_kind);
5117
Guido van Rossumc2e20742006-02-27 22:32:47 +00005118 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00005119 final = compiler_new_block(c);
5120 exit = compiler_new_block(c);
5121 if (!block || !final || !exit)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005122 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005123
Thomas Wouters477c8d52006-05-27 19:21:47 +00005124 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005125 VISIT(c, expr, item->context_expr);
Mark Shannonfee55262019-11-21 09:11:43 +00005126 /* Will push bound __exit__ */
Mark Shannon582aaf12020-08-04 17:30:11 +01005127 ADDOP_JUMP(c, SETUP_WITH, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005128
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005129 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00005130 compiler_use_next_block(c, block);
Mark Shannon5979e812021-04-30 14:32:47 +01005131 if (!compiler_push_fblock(c, WITH, block, final, s)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005132 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005133 }
5134
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005135 if (item->optional_vars) {
5136 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005137 }
5138 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005139 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005140 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005141 }
5142
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005143 pos++;
5144 if (pos == asdl_seq_LEN(s->v.With.items))
5145 /* BLOCK code */
5146 VISIT_SEQ(c, stmt, s->v.With.body)
5147 else if (!compiler_with(c, s, pos))
5148 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005149
Mark Shannon3bd60352021-01-13 12:05:43 +00005150
5151 /* Mark all following code as artificial */
5152 c->u->u_lineno = -1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005153 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005154 compiler_pop_fblock(c, WITH, block);
Mark Shannon13bc1392020-01-23 09:25:17 +00005155
Mark Shannonfee55262019-11-21 09:11:43 +00005156 /* End of body; start the cleanup. */
Mark Shannon13bc1392020-01-23 09:25:17 +00005157
Mark Shannonfee55262019-11-21 09:11:43 +00005158 /* For successful outcome:
5159 * call __exit__(None, None, None)
5160 */
Mark Shannon5979e812021-04-30 14:32:47 +01005161 SET_LOC(c, s);
Mark Shannonfee55262019-11-21 09:11:43 +00005162 if (!compiler_call_exit_with_nones(c))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005163 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00005164 ADDOP(c, POP_TOP);
Mark Shannon582aaf12020-08-04 17:30:11 +01005165 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005166
Mark Shannonfee55262019-11-21 09:11:43 +00005167 /* For exceptional outcome: */
5168 compiler_use_next_block(c, final);
Mark Shannonfee55262019-11-21 09:11:43 +00005169 ADDOP(c, WITH_EXCEPT_START);
5170 compiler_with_except_finish(c);
5171
5172 compiler_use_next_block(c, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005173 return 1;
5174}
5175
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005176static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005177compiler_visit_expr1(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005178{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005179 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07005180 case NamedExpr_kind:
5181 VISIT(c, expr, e->v.NamedExpr.value);
5182 ADDOP(c, DUP_TOP);
5183 VISIT(c, expr, e->v.NamedExpr.target);
5184 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005185 case BoolOp_kind:
5186 return compiler_boolop(c, e);
5187 case BinOp_kind:
5188 VISIT(c, expr, e->v.BinOp.left);
5189 VISIT(c, expr, e->v.BinOp.right);
Andy Lester76d58772020-03-10 21:18:12 -05005190 ADDOP(c, binop(e->v.BinOp.op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005191 break;
5192 case UnaryOp_kind:
5193 VISIT(c, expr, e->v.UnaryOp.operand);
5194 ADDOP(c, unaryop(e->v.UnaryOp.op));
5195 break;
5196 case Lambda_kind:
5197 return compiler_lambda(c, e);
5198 case IfExp_kind:
5199 return compiler_ifexp(c, e);
5200 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005201 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005202 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005203 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005204 case GeneratorExp_kind:
5205 return compiler_genexp(c, e);
5206 case ListComp_kind:
5207 return compiler_listcomp(c, e);
5208 case SetComp_kind:
5209 return compiler_setcomp(c, e);
5210 case DictComp_kind:
5211 return compiler_dictcomp(c, e);
5212 case Yield_kind:
5213 if (c->u->u_ste->ste_type != FunctionBlock)
5214 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005215 if (e->v.Yield.value) {
5216 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005217 }
5218 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005219 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005220 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005221 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005222 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005223 case YieldFrom_kind:
5224 if (c->u->u_ste->ste_type != FunctionBlock)
5225 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04005226
5227 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
5228 return compiler_error(c, "'yield from' inside async function");
5229
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005230 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04005231 ADDOP(c, GET_YIELD_FROM_ITER);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005232 ADDOP_LOAD_CONST(c, Py_None);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005233 ADDOP(c, YIELD_FROM);
5234 break;
Yury Selivanov75445082015-05-11 22:57:16 -04005235 case Await_kind:
Pablo Galindo90235812020-03-15 04:29:22 +00005236 if (!IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005237 if (c->u->u_ste->ste_type != FunctionBlock){
5238 return compiler_error(c, "'await' outside function");
5239 }
Yury Selivanov75445082015-05-11 22:57:16 -04005240
Victor Stinner331a6a52019-05-27 16:39:22 +02005241 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005242 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
5243 return compiler_error(c, "'await' outside async function");
5244 }
5245 }
Yury Selivanov75445082015-05-11 22:57:16 -04005246
5247 VISIT(c, expr, e->v.Await.value);
5248 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005249 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005250 ADDOP(c, YIELD_FROM);
5251 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005252 case Compare_kind:
5253 return compiler_compare(c, e);
5254 case Call_kind:
5255 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005256 case Constant_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005257 ADDOP_LOAD_CONST(c, e->v.Constant.value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005258 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005259 case JoinedStr_kind:
5260 return compiler_joined_str(c, e);
5261 case FormattedValue_kind:
5262 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005263 /* The following exprs can be assignment targets. */
5264 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005265 VISIT(c, expr, e->v.Attribute.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005266 switch (e->v.Attribute.ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005267 case Load:
Mark Shannond48848c2021-03-14 18:01:30 +00005268 {
5269 int old_lineno = c->u->u_lineno;
5270 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005271 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005272 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005273 break;
Mark Shannond48848c2021-03-14 18:01:30 +00005274 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005275 case Store:
Mark Shannond48848c2021-03-14 18:01:30 +00005276 if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005277 return 0;
Mark Shannond48848c2021-03-14 18:01:30 +00005278 }
5279 int old_lineno = c->u->u_lineno;
5280 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005281 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005282 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005283 break;
5284 case Del:
5285 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
5286 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005287 }
5288 break;
5289 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005290 return compiler_subscript(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005291 case Starred_kind:
5292 switch (e->v.Starred.ctx) {
5293 case Store:
5294 /* In all legitimate cases, the Starred node was already replaced
5295 * by compiler_list/compiler_tuple. XXX: is that okay? */
5296 return compiler_error(c,
5297 "starred assignment target must be in a list or tuple");
5298 default:
5299 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005300 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005301 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005302 break;
5303 case Slice_kind:
5304 return compiler_slice(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005305 case Name_kind:
5306 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
5307 /* child nodes of List and Tuple will have expr_context set */
5308 case List_kind:
5309 return compiler_list(c, e);
5310 case Tuple_kind:
5311 return compiler_tuple(c, e);
5312 }
5313 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005314}
5315
5316static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005317compiler_visit_expr(struct compiler *c, expr_ty e)
5318{
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005319 int old_lineno = c->u->u_lineno;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005320 int old_end_lineno = c->u->u_end_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005321 int old_col_offset = c->u->u_col_offset;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005322 int old_end_col_offset = c->u->u_end_col_offset;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005323 SET_LOC(c, e);
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005324 int res = compiler_visit_expr1(c, e);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005325 c->u->u_lineno = old_lineno;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005326 c->u->u_end_lineno = old_end_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005327 c->u->u_col_offset = old_col_offset;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005328 c->u->u_end_col_offset = old_end_col_offset;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005329 return res;
5330}
5331
5332static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005333compiler_augassign(struct compiler *c, stmt_ty s)
5334{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005335 assert(s->kind == AugAssign_kind);
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005336 expr_ty e = s->v.AugAssign.target;
5337
5338 int old_lineno = c->u->u_lineno;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005339 int old_end_lineno = c->u->u_end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005340 int old_col_offset = c->u->u_col_offset;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005341 int old_end_col_offset = c->u->u_end_col_offset;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005342 SET_LOC(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005344 switch (e->kind) {
5345 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005346 VISIT(c, expr, e->v.Attribute.value);
5347 ADDOP(c, DUP_TOP);
Mark Shannond48848c2021-03-14 18:01:30 +00005348 int old_lineno = c->u->u_lineno;
5349 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005350 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005351 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005352 break;
5353 case Subscript_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005354 VISIT(c, expr, e->v.Subscript.value);
5355 VISIT(c, expr, e->v.Subscript.slice);
5356 ADDOP(c, DUP_TOP_TWO);
5357 ADDOP(c, BINARY_SUBSCR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005358 break;
5359 case Name_kind:
5360 if (!compiler_nameop(c, e->v.Name.id, Load))
5361 return 0;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005362 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005363 default:
5364 PyErr_Format(PyExc_SystemError,
5365 "invalid node type (%d) for augmented assignment",
5366 e->kind);
5367 return 0;
5368 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005369
5370 c->u->u_lineno = old_lineno;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005371 c->u->u_end_lineno = old_end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005372 c->u->u_col_offset = old_col_offset;
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005373 c->u->u_end_col_offset = old_end_col_offset;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005374
5375 VISIT(c, expr, s->v.AugAssign.value);
5376 ADDOP(c, inplace_binop(s->v.AugAssign.op));
5377
5378 SET_LOC(c, e);
5379
5380 switch (e->kind) {
5381 case Attribute_kind:
Mark Shannond48848c2021-03-14 18:01:30 +00005382 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005383 ADDOP(c, ROT_TWO);
5384 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5385 break;
5386 case Subscript_kind:
5387 ADDOP(c, ROT_THREE);
5388 ADDOP(c, STORE_SUBSCR);
5389 break;
5390 case Name_kind:
5391 return compiler_nameop(c, e->v.Name.id, Store);
5392 default:
5393 Py_UNREACHABLE();
5394 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005395 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005396}
5397
5398static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005399check_ann_expr(struct compiler *c, expr_ty e)
5400{
5401 VISIT(c, expr, e);
5402 ADDOP(c, POP_TOP);
5403 return 1;
5404}
5405
5406static int
5407check_annotation(struct compiler *c, stmt_ty s)
5408{
Batuhan Taskaya8cc3cfa2021-04-25 05:31:20 +03005409 /* Annotations of complex targets does not produce anything
5410 under annotations future */
5411 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
5412 return 1;
5413 }
5414
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005415 /* Annotations are only evaluated in a module or class. */
5416 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5417 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
5418 return check_ann_expr(c, s->v.AnnAssign.annotation);
5419 }
5420 return 1;
5421}
5422
5423static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005424check_ann_subscr(struct compiler *c, expr_ty e)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005425{
5426 /* We check that everything in a subscript is defined at runtime. */
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005427 switch (e->kind) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005428 case Slice_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005429 if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005430 return 0;
5431 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005432 if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) {
5433 return 0;
5434 }
5435 if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) {
5436 return 0;
5437 }
5438 return 1;
5439 case Tuple_kind: {
5440 /* extended slice */
Pablo Galindoa5634c42020-09-16 19:42:00 +01005441 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005442 Py_ssize_t i, n = asdl_seq_LEN(elts);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005443 for (i = 0; i < n; i++) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005444 if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005445 return 0;
5446 }
5447 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005448 return 1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005449 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005450 default:
5451 return check_ann_expr(c, e);
5452 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005453}
5454
5455static int
5456compiler_annassign(struct compiler *c, stmt_ty s)
5457{
5458 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07005459 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005460
5461 assert(s->kind == AnnAssign_kind);
5462
5463 /* We perform the actual assignment first. */
5464 if (s->v.AnnAssign.value) {
5465 VISIT(c, expr, s->v.AnnAssign.value);
5466 VISIT(c, expr, targ);
5467 }
5468 switch (targ->kind) {
5469 case Name_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005470 if (forbidden_name(c, targ->v.Name.id, Store))
5471 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005472 /* If we have a simple name in a module or class, store annotation. */
5473 if (s->v.AnnAssign.simple &&
5474 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5475 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Pablo Galindob0544ba2021-04-21 12:41:19 +01005476 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
5477 VISIT(c, annexpr, s->v.AnnAssign.annotation)
5478 }
5479 else {
5480 VISIT(c, expr, s->v.AnnAssign.annotation);
5481 }
Mark Shannon332cd5e2018-01-30 00:41:04 +00005482 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02005483 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005484 ADDOP_LOAD_CONST_NEW(c, mangled);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005485 ADDOP(c, STORE_SUBSCR);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005486 }
5487 break;
5488 case Attribute_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005489 if (forbidden_name(c, targ->v.Attribute.attr, Store))
5490 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005491 if (!s->v.AnnAssign.value &&
5492 !check_ann_expr(c, targ->v.Attribute.value)) {
5493 return 0;
5494 }
5495 break;
5496 case Subscript_kind:
5497 if (!s->v.AnnAssign.value &&
5498 (!check_ann_expr(c, targ->v.Subscript.value) ||
5499 !check_ann_subscr(c, targ->v.Subscript.slice))) {
5500 return 0;
5501 }
5502 break;
5503 default:
5504 PyErr_Format(PyExc_SystemError,
5505 "invalid node type (%d) for annotated assignment",
5506 targ->kind);
5507 return 0;
5508 }
5509 /* Annotation is evaluated last. */
5510 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
5511 return 0;
5512 }
5513 return 1;
5514}
5515
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005516/* Raises a SyntaxError and returns 0.
5517 If something goes wrong, a different exception may be raised.
5518*/
5519
5520static int
Brandt Bucher145bf262021-02-26 14:51:55 -08005521compiler_error(struct compiler *c, const char *format, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005522{
Brandt Bucher145bf262021-02-26 14:51:55 -08005523 va_list vargs;
5524#ifdef HAVE_STDARG_PROTOTYPES
5525 va_start(vargs, format);
5526#else
5527 va_start(vargs);
5528#endif
5529 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5530 va_end(vargs);
5531 if (msg == NULL) {
5532 return 0;
5533 }
5534 PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
5535 if (loc == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005536 Py_INCREF(Py_None);
5537 loc = Py_None;
5538 }
Pablo Galindoa77aac42021-04-23 14:27:05 +01005539 PyObject *args = Py_BuildValue("O(OiiOii)", msg, c->c_filename,
5540 c->u->u_lineno, c->u->u_col_offset + 1, loc,
5541 c->u->u_end_lineno, c->u->u_end_col_offset + 1);
Brandt Bucher145bf262021-02-26 14:51:55 -08005542 Py_DECREF(msg);
5543 if (args == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005544 goto exit;
Brandt Bucher145bf262021-02-26 14:51:55 -08005545 }
5546 PyErr_SetObject(PyExc_SyntaxError, args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005547 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005548 Py_DECREF(loc);
Brandt Bucher145bf262021-02-26 14:51:55 -08005549 Py_XDECREF(args);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005550 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005551}
5552
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005553/* Emits a SyntaxWarning and returns 1 on success.
5554 If a SyntaxWarning raised as error, replaces it with a SyntaxError
5555 and returns 0.
5556*/
5557static int
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005558compiler_warn(struct compiler *c, const char *format, ...)
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005559{
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005560 va_list vargs;
5561#ifdef HAVE_STDARG_PROTOTYPES
5562 va_start(vargs, format);
5563#else
5564 va_start(vargs);
5565#endif
5566 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5567 va_end(vargs);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005568 if (msg == NULL) {
5569 return 0;
5570 }
5571 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
5572 c->u->u_lineno, NULL, NULL) < 0)
5573 {
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005574 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005575 /* Replace the SyntaxWarning exception with a SyntaxError
5576 to get a more accurate error report */
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005577 PyErr_Clear();
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005578 assert(PyUnicode_AsUTF8(msg) != NULL);
5579 compiler_error(c, PyUnicode_AsUTF8(msg));
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005580 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005581 Py_DECREF(msg);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005582 return 0;
5583 }
5584 Py_DECREF(msg);
5585 return 1;
5586}
5587
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005588static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005589compiler_subscript(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005590{
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005591 expr_context_ty ctx = e->v.Subscript.ctx;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005592 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005593
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005594 if (ctx == Load) {
5595 if (!check_subscripter(c, e->v.Subscript.value)) {
5596 return 0;
5597 }
5598 if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
5599 return 0;
5600 }
5601 }
5602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005603 switch (ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005604 case Load: op = BINARY_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005605 case Store: op = STORE_SUBSCR; break;
5606 case Del: op = DELETE_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005607 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005608 assert(op);
5609 VISIT(c, expr, e->v.Subscript.value);
5610 VISIT(c, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005611 ADDOP(c, op);
5612 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005613}
5614
5615static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005616compiler_slice(struct compiler *c, expr_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005618 int n = 2;
5619 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005621 /* only handles the cases where BUILD_SLICE is emitted */
5622 if (s->v.Slice.lower) {
5623 VISIT(c, expr, s->v.Slice.lower);
5624 }
5625 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005626 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005627 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005629 if (s->v.Slice.upper) {
5630 VISIT(c, expr, s->v.Slice.upper);
5631 }
5632 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005633 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005634 }
5635
5636 if (s->v.Slice.step) {
5637 n++;
5638 VISIT(c, expr, s->v.Slice.step);
5639 }
5640 ADDOP_I(c, BUILD_SLICE, n);
5641 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005642}
5643
Brandt Bucher145bf262021-02-26 14:51:55 -08005644
5645// PEP 634: Structural Pattern Matching
5646
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005647// To keep things simple, all compiler_pattern_* and pattern_helper_* routines
5648// follow the convention of consuming TOS (the subject for the given pattern)
5649// and calling jump_to_fail_pop on failure (no match).
5650
5651// When calling into these routines, it's important that pc->on_top be kept
5652// updated to reflect the current number of items that we are using on the top
5653// of the stack: they will be popped on failure, and any name captures will be
5654// stored *underneath* them on success. This lets us defer all names stores
5655// until the *entire* pattern matches.
Brandt Bucher145bf262021-02-26 14:51:55 -08005656
Brandt Bucher145bf262021-02-26 14:51:55 -08005657#define WILDCARD_CHECK(N) \
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005658 ((N)->kind == MatchAs_kind && !(N)->v.MatchAs.name)
Brandt Bucher145bf262021-02-26 14:51:55 -08005659
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005660#define WILDCARD_STAR_CHECK(N) \
5661 ((N)->kind == MatchStar_kind && !(N)->v.MatchStar.name)
5662
5663// Limit permitted subexpressions, even if the parser & AST validator let them through
5664#define MATCH_VALUE_EXPR(N) \
5665 ((N)->kind == Constant_kind || (N)->kind == Attribute_kind)
Brandt Bucher145bf262021-02-26 14:51:55 -08005666
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005667// Allocate or resize pc->fail_pop to allow for n items to be popped on failure.
5668static int
5669ensure_fail_pop(struct compiler *c, pattern_context *pc, Py_ssize_t n)
5670{
5671 Py_ssize_t size = n + 1;
5672 if (size <= pc->fail_pop_size) {
5673 return 1;
5674 }
5675 Py_ssize_t needed = sizeof(basicblock*) * size;
5676 basicblock **resized = PyObject_Realloc(pc->fail_pop, needed);
5677 if (resized == NULL) {
5678 PyErr_NoMemory();
5679 return 0;
5680 }
5681 pc->fail_pop = resized;
5682 while (pc->fail_pop_size < size) {
5683 basicblock *new_block;
5684 RETURN_IF_FALSE(new_block = compiler_new_block(c));
5685 pc->fail_pop[pc->fail_pop_size++] = new_block;
5686 }
5687 return 1;
5688}
5689
5690// Use op to jump to the correct fail_pop block.
5691static int
5692jump_to_fail_pop(struct compiler *c, pattern_context *pc, int op)
5693{
5694 // Pop any items on the top of the stack, plus any objects we were going to
5695 // capture on success:
5696 Py_ssize_t pops = pc->on_top + PyList_GET_SIZE(pc->stores);
5697 RETURN_IF_FALSE(ensure_fail_pop(c, pc, pops));
5698 ADDOP_JUMP(c, op, pc->fail_pop[pops]);
5699 NEXT_BLOCK(c);
5700 return 1;
5701}
5702
5703// Build all of the fail_pop blocks and reset fail_pop.
5704static int
5705emit_and_reset_fail_pop(struct compiler *c, pattern_context *pc)
5706{
5707 if (!pc->fail_pop_size) {
5708 assert(pc->fail_pop == NULL);
5709 NEXT_BLOCK(c);
5710 return 1;
5711 }
5712 while (--pc->fail_pop_size) {
5713 compiler_use_next_block(c, pc->fail_pop[pc->fail_pop_size]);
5714 if (!compiler_addop(c, POP_TOP)) {
5715 pc->fail_pop_size = 0;
5716 PyObject_Free(pc->fail_pop);
5717 pc->fail_pop = NULL;
5718 return 0;
5719 }
5720 }
5721 compiler_use_next_block(c, pc->fail_pop[0]);
5722 PyObject_Free(pc->fail_pop);
5723 pc->fail_pop = NULL;
5724 return 1;
5725}
5726
5727static int
5728compiler_error_duplicate_store(struct compiler *c, identifier n)
5729{
5730 return compiler_error(c, "multiple assignments to name %R in pattern", n);
5731}
5732
Brandt Bucher145bf262021-02-26 14:51:55 -08005733static int
5734pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc)
5735{
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005736 if (n == NULL) {
5737 ADDOP(c, POP_TOP);
5738 return 1;
5739 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005740 if (forbidden_name(c, n, Store)) {
5741 return 0;
5742 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005743 // Can't assign to the same name twice:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005744 int duplicate = PySequence_Contains(pc->stores, n);
5745 if (duplicate < 0) {
5746 return 0;
Brandt Bucher145bf262021-02-26 14:51:55 -08005747 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005748 if (duplicate) {
5749 return compiler_error_duplicate_store(c, n);
Brandt Bucher145bf262021-02-26 14:51:55 -08005750 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005751 // Rotate this object underneath any items we need to preserve:
5752 ADDOP_I(c, ROT_N, pc->on_top + PyList_GET_SIZE(pc->stores) + 1);
5753 return !PyList_Append(pc->stores, n);
Brandt Bucher145bf262021-02-26 14:51:55 -08005754}
5755
5756
5757static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005758pattern_unpack_helper(struct compiler *c, asdl_pattern_seq *elts)
5759{
5760 Py_ssize_t n = asdl_seq_LEN(elts);
5761 int seen_star = 0;
5762 for (Py_ssize_t i = 0; i < n; i++) {
5763 pattern_ty elt = asdl_seq_GET(elts, i);
5764 if (elt->kind == MatchStar_kind && !seen_star) {
5765 if ((i >= (1 << 8)) ||
5766 (n-i-1 >= (INT_MAX >> 8)))
5767 return compiler_error(c,
5768 "too many expressions in "
5769 "star-unpacking sequence pattern");
5770 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
5771 seen_star = 1;
5772 }
5773 else if (elt->kind == MatchStar_kind) {
5774 return compiler_error(c,
5775 "multiple starred expressions in sequence pattern");
5776 }
5777 }
5778 if (!seen_star) {
5779 ADDOP_I(c, UNPACK_SEQUENCE, n);
5780 }
5781 return 1;
5782}
5783
5784static int
5785pattern_helper_sequence_unpack(struct compiler *c, asdl_pattern_seq *patterns,
Brandt Bucher145bf262021-02-26 14:51:55 -08005786 Py_ssize_t star, pattern_context *pc)
5787{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005788 RETURN_IF_FALSE(pattern_unpack_helper(c, patterns));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005789 Py_ssize_t size = asdl_seq_LEN(patterns);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005790 // We've now got a bunch of new subjects on the stack. They need to remain
5791 // there after each subpattern match:
5792 pc->on_top += size;
Brandt Bucher145bf262021-02-26 14:51:55 -08005793 for (Py_ssize_t i = 0; i < size; i++) {
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005794 // One less item to keep track of each time we loop through:
5795 pc->on_top--;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005796 pattern_ty pattern = asdl_seq_GET(patterns, i);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005797 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08005798 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005799 return 1;
Brandt Bucher145bf262021-02-26 14:51:55 -08005800}
5801
5802// Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of
5803// UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a
5804// starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc.
5805static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005806pattern_helper_sequence_subscr(struct compiler *c, asdl_pattern_seq *patterns,
Brandt Bucher145bf262021-02-26 14:51:55 -08005807 Py_ssize_t star, pattern_context *pc)
5808{
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005809 // We need to keep the subject around for extracting elements:
5810 pc->on_top++;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005811 Py_ssize_t size = asdl_seq_LEN(patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08005812 for (Py_ssize_t i = 0; i < size; i++) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005813 pattern_ty pattern = asdl_seq_GET(patterns, i);
5814 if (WILDCARD_CHECK(pattern)) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005815 continue;
5816 }
5817 if (i == star) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005818 assert(WILDCARD_STAR_CHECK(pattern));
Brandt Bucher145bf262021-02-26 14:51:55 -08005819 continue;
5820 }
5821 ADDOP(c, DUP_TOP);
5822 if (i < star) {
5823 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5824 }
5825 else {
5826 // The subject may not support negative indexing! Compute a
5827 // nonnegative index:
5828 ADDOP(c, GET_LEN);
5829 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i));
5830 ADDOP(c, BINARY_SUBTRACT);
5831 }
5832 ADDOP(c, BINARY_SUBSCR);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005833 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08005834 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005835 // Pop the subject, we're done with it:
5836 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08005837 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08005838 return 1;
5839}
5840
Brandt Bucher145bf262021-02-26 14:51:55 -08005841// Like compiler_pattern, but turn off checks for irrefutability.
5842static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005843compiler_pattern_subpattern(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005844{
5845 int allow_irrefutable = pc->allow_irrefutable;
5846 pc->allow_irrefutable = 1;
5847 RETURN_IF_FALSE(compiler_pattern(c, p, pc));
5848 pc->allow_irrefutable = allow_irrefutable;
5849 return 1;
5850}
5851
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005852static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005853compiler_pattern_as(struct compiler *c, pattern_ty p, pattern_context *pc)
5854{
5855 assert(p->kind == MatchAs_kind);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005856 if (p->v.MatchAs.pattern == NULL) {
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005857 // An irrefutable match:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005858 if (!pc->allow_irrefutable) {
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005859 if (p->v.MatchAs.name) {
5860 const char *e = "name capture %R makes remaining patterns unreachable";
5861 return compiler_error(c, e, p->v.MatchAs.name);
5862 }
5863 const char *e = "wildcard makes remaining patterns unreachable";
5864 return compiler_error(c, e);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005865 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005866 return pattern_helper_store_name(c, p->v.MatchAs.name, pc);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005867 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005868 // Need to make a copy for (possibly) storing later:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005869 pc->on_top++;
Brandt Bucher145bf262021-02-26 14:51:55 -08005870 ADDOP(c, DUP_TOP);
5871 RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc));
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005872 // Success! Store it:
5873 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08005874 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08005875 return 1;
5876}
5877
Brandt Bucher145bf262021-02-26 14:51:55 -08005878static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005879compiler_pattern_star(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005880{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005881 assert(p->kind == MatchStar_kind);
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005882 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchStar.name, pc));
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005883 return 1;
Brandt Bucher145bf262021-02-26 14:51:55 -08005884}
5885
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005886static int
5887validate_kwd_attrs(struct compiler *c, asdl_identifier_seq *attrs, asdl_pattern_seq* patterns)
5888{
5889 // Any errors will point to the pattern rather than the arg name as the
5890 // parser is only supplying identifiers rather than Name or keyword nodes
5891 Py_ssize_t nattrs = asdl_seq_LEN(attrs);
5892 for (Py_ssize_t i = 0; i < nattrs; i++) {
5893 identifier attr = ((identifier)asdl_seq_GET(attrs, i));
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005894 SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, i)));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005895 if (forbidden_name(c, attr, Store)) {
5896 return -1;
5897 }
5898 for (Py_ssize_t j = i + 1; j < nattrs; j++) {
5899 identifier other = ((identifier)asdl_seq_GET(attrs, j));
5900 if (!PyUnicode_Compare(attr, other)) {
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005901 SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, j)));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005902 compiler_error(c, "attribute name repeated in class pattern: %U", attr);
5903 return -1;
5904 }
5905 }
5906 }
5907 return 0;
5908}
Brandt Bucher145bf262021-02-26 14:51:55 -08005909
5910static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005911compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005912{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005913 assert(p->kind == MatchClass_kind);
5914 asdl_pattern_seq *patterns = p->v.MatchClass.patterns;
5915 asdl_identifier_seq *kwd_attrs = p->v.MatchClass.kwd_attrs;
5916 asdl_pattern_seq *kwd_patterns = p->v.MatchClass.kwd_patterns;
5917 Py_ssize_t nargs = asdl_seq_LEN(patterns);
5918 Py_ssize_t nattrs = asdl_seq_LEN(kwd_attrs);
5919 Py_ssize_t nkwd_patterns = asdl_seq_LEN(kwd_patterns);
5920 if (nattrs != nkwd_patterns) {
5921 // AST validator shouldn't let this happen, but if it does,
5922 // just fail, don't crash out of the interpreter
5923 const char * e = "kwd_attrs (%d) / kwd_patterns (%d) length mismatch in class pattern";
5924 return compiler_error(c, e, nattrs, nkwd_patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08005925 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005926 if (INT_MAX < nargs || INT_MAX < nargs + nattrs - 1) {
5927 const char *e = "too many sub-patterns in class pattern %R";
5928 return compiler_error(c, e, p->v.MatchClass.cls);
5929 }
5930 if (nattrs) {
5931 RETURN_IF_FALSE(!validate_kwd_attrs(c, kwd_attrs, kwd_patterns));
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07005932 SET_LOC(c, p);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005933 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005934 VISIT(c, expr, p->v.MatchClass.cls);
5935 PyObject *attr_names;
5936 RETURN_IF_FALSE(attr_names = PyTuple_New(nattrs));
Brandt Bucher145bf262021-02-26 14:51:55 -08005937 Py_ssize_t i;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005938 for (i = 0; i < nattrs; i++) {
5939 PyObject *name = asdl_seq_GET(kwd_attrs, i);
Brandt Bucher145bf262021-02-26 14:51:55 -08005940 Py_INCREF(name);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005941 PyTuple_SET_ITEM(attr_names, i, name);
Brandt Bucher145bf262021-02-26 14:51:55 -08005942 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005943 ADDOP_LOAD_CONST_NEW(c, attr_names);
Brandt Bucher145bf262021-02-26 14:51:55 -08005944 ADDOP_I(c, MATCH_CLASS, nargs);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005945 // TOS is now a tuple of (nargs + nattrs) attributes. Preserve it:
5946 pc->on_top++;
5947 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005948 for (i = 0; i < nargs + nattrs; i++) {
5949 pattern_ty pattern;
Brandt Bucher145bf262021-02-26 14:51:55 -08005950 if (i < nargs) {
5951 // Positional:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005952 pattern = asdl_seq_GET(patterns, i);
Brandt Bucher145bf262021-02-26 14:51:55 -08005953 }
5954 else {
5955 // Keyword:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005956 pattern = asdl_seq_GET(kwd_patterns, i - nargs);
Brandt Bucher145bf262021-02-26 14:51:55 -08005957 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005958 if (WILDCARD_CHECK(pattern)) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005959 continue;
5960 }
5961 // Get the i-th attribute, and match it against the i-th pattern:
5962 ADDOP(c, DUP_TOP);
5963 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5964 ADDOP(c, BINARY_SUBSCR);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005965 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08005966 }
5967 // Success! Pop the tuple of attributes:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005968 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08005969 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08005970 return 1;
5971}
5972
Brandt Bucher145bf262021-02-26 14:51:55 -08005973static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005974compiler_pattern_mapping(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005975{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005976 assert(p->kind == MatchMapping_kind);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005977 asdl_expr_seq *keys = p->v.MatchMapping.keys;
5978 asdl_pattern_seq *patterns = p->v.MatchMapping.patterns;
5979 Py_ssize_t size = asdl_seq_LEN(keys);
5980 Py_ssize_t npatterns = asdl_seq_LEN(patterns);
5981 if (size != npatterns) {
5982 // AST validator shouldn't let this happen, but if it does,
5983 // just fail, don't crash out of the interpreter
5984 const char * e = "keys (%d) / patterns (%d) length mismatch in mapping pattern";
5985 return compiler_error(c, e, size, npatterns);
5986 }
5987 // We have a double-star target if "rest" is set
5988 PyObject *star_target = p->v.MatchMapping.rest;
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005989 // We need to keep the subject on top during the mapping and length checks:
5990 pc->on_top++;
Brandt Bucher145bf262021-02-26 14:51:55 -08005991 ADDOP(c, MATCH_MAPPING);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005992 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005993 if (!size && !star_target) {
Brandt Bucher0ad1e032021-05-02 13:02:10 -07005994 // If the pattern is just "{}", we're done! Pop the subject:
5995 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08005996 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08005997 return 1;
5998 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005999 if (size) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006000 // If the pattern has any keys in it, perform a length check:
6001 ADDOP(c, GET_LEN);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006002 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
Brandt Bucher145bf262021-02-26 14:51:55 -08006003 ADDOP_COMPARE(c, GtE);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006004 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006005 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006006 if (INT_MAX < size - 1) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006007 return compiler_error(c, "too many sub-patterns in mapping pattern");
6008 }
6009 // Collect all of the keys into a tuple for MATCH_KEYS and
6010 // COPY_DICT_WITHOUT_KEYS. They can either be dotted names or literals:
Miss Islington (bot)016af142021-07-14 18:00:35 -07006011
6012 // Maintaining a set of Constant_kind kind keys allows us to raise a
6013 // SyntaxError in the case of duplicates.
6014 PyObject *seen = PySet_New(NULL);
6015 if (seen == NULL) {
6016 return 0;
6017 }
6018
6019 // NOTE: goto error on failure in the loop below to avoid leaking `seen`
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006020 for (Py_ssize_t i = 0; i < size; i++) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006021 expr_ty key = asdl_seq_GET(keys, i);
6022 if (key == NULL) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006023 const char *e = "can't use NULL keys in MatchMapping "
6024 "(set 'rest' parameter instead)";
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07006025 SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, i)));
Miss Islington (bot)016af142021-07-14 18:00:35 -07006026 compiler_error(c, e);
6027 goto error;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006028 }
Miss Islington (bot)016af142021-07-14 18:00:35 -07006029
6030 if (key->kind == Constant_kind) {
6031 int in_seen = PySet_Contains(seen, key->v.Constant.value);
6032 if (in_seen < 0) {
6033 goto error;
6034 }
6035 if (in_seen) {
6036 const char *e = "mapping pattern checks duplicate key (%R)";
6037 compiler_error(c, e, key->v.Constant.value);
6038 goto error;
6039 }
6040 if (PySet_Add(seen, key->v.Constant.value)) {
6041 goto error;
6042 }
6043 }
6044
6045 else if (key->kind != Attribute_kind) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006046 const char *e = "mapping pattern keys may only match literals and attribute lookups";
Miss Islington (bot)016af142021-07-14 18:00:35 -07006047 compiler_error(c, e);
6048 goto error;
Brandt Bucher145bf262021-02-26 14:51:55 -08006049 }
Miss Islington (bot)016af142021-07-14 18:00:35 -07006050 if (!compiler_visit_expr(c, key)) {
6051 goto error;
6052 }
Brandt Bucher145bf262021-02-26 14:51:55 -08006053 }
Miss Islington (bot)016af142021-07-14 18:00:35 -07006054
6055 // all keys have been checked; there are no duplicates
6056 Py_DECREF(seen);
6057
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006058 ADDOP_I(c, BUILD_TUPLE, size);
Brandt Bucher145bf262021-02-26 14:51:55 -08006059 ADDOP(c, MATCH_KEYS);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006060 // There's now a tuple of keys and a tuple of values on top of the subject:
6061 pc->on_top += 2;
6062 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
6063 // So far so good. Use that tuple of values on the stack to match
Brandt Bucher145bf262021-02-26 14:51:55 -08006064 // sub-patterns against:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006065 for (Py_ssize_t i = 0; i < size; i++) {
6066 pattern_ty pattern = asdl_seq_GET(patterns, i);
6067 if (WILDCARD_CHECK(pattern)) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006068 continue;
6069 }
6070 ADDOP(c, DUP_TOP);
6071 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
6072 ADDOP(c, BINARY_SUBSCR);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006073 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006074 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006075 // If we get this far, it's a match! We're done with the tuple of values,
6076 // and whatever happens next should consume the tuple of keys underneath it:
6077 pc->on_top -= 2;
Brandt Bucher145bf262021-02-26 14:51:55 -08006078 ADDOP(c, POP_TOP);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006079 if (star_target) {
6080 // If we have a starred name, bind a dict of remaining items to it:
Brandt Bucher145bf262021-02-26 14:51:55 -08006081 ADDOP(c, COPY_DICT_WITHOUT_KEYS);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006082 RETURN_IF_FALSE(pattern_helper_store_name(c, star_target, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006083 }
6084 else {
6085 // Otherwise, we don't care about this tuple of keys anymore:
6086 ADDOP(c, POP_TOP);
6087 }
6088 // Pop the subject:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006089 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08006090 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08006091 return 1;
Miss Islington (bot)016af142021-07-14 18:00:35 -07006092
6093error:
6094 Py_DECREF(seen);
6095 return 0;
Brandt Bucher145bf262021-02-26 14:51:55 -08006096}
6097
Brandt Bucher145bf262021-02-26 14:51:55 -08006098static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006099compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006100{
6101 assert(p->kind == MatchOr_kind);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006102 basicblock *end;
Brandt Bucher145bf262021-02-26 14:51:55 -08006103 RETURN_IF_FALSE(end = compiler_new_block(c));
Brandt Bucher145bf262021-02-26 14:51:55 -08006104 Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns);
6105 assert(size > 1);
6106 // We're going to be messing with pc. Keep the original info handy:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006107 pattern_context old_pc = *pc;
6108 Py_INCREF(pc->stores);
6109 // control is the list of names bound by the first alternative. It is used
6110 // for checking different name bindings in alternatives, and for correcting
6111 // the order in which extracted elements are placed on the stack.
6112 PyObject *control = NULL;
6113 // NOTE: We can't use returning macros anymore! goto error on error.
Brandt Bucher145bf262021-02-26 14:51:55 -08006114 for (Py_ssize_t i = 0; i < size; i++) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006115 pattern_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i);
Brandt Bucher145bf262021-02-26 14:51:55 -08006116 SET_LOC(c, alt);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006117 PyObject *pc_stores = PyList_New(0);
6118 if (pc_stores == NULL) {
6119 goto error;
Brandt Bucher145bf262021-02-26 14:51:55 -08006120 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006121 Py_SETREF(pc->stores, pc_stores);
6122 // An irrefutable sub-pattern must be last, if it is allowed at all:
6123 pc->allow_irrefutable = (i == size - 1) && old_pc.allow_irrefutable;
6124 pc->fail_pop = NULL;
6125 pc->fail_pop_size = 0;
6126 pc->on_top = 0;
6127 if (!compiler_addop(c, DUP_TOP) || !compiler_pattern(c, alt, pc)) {
6128 goto error;
6129 }
6130 // Success!
6131 Py_ssize_t nstores = PyList_GET_SIZE(pc->stores);
Brandt Bucher145bf262021-02-26 14:51:55 -08006132 if (!i) {
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006133 // This is the first alternative, so save its stores as a "control"
6134 // for the others (they can't bind a different set of names, and
6135 // might need to be reordered):
6136 assert(control == NULL);
Brandt Bucher145bf262021-02-26 14:51:55 -08006137 control = pc->stores;
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006138 Py_INCREF(control);
Brandt Bucher145bf262021-02-26 14:51:55 -08006139 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006140 else if (nstores != PyList_GET_SIZE(control)) {
6141 goto diff;
Brandt Bucher145bf262021-02-26 14:51:55 -08006142 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006143 else if (nstores) {
6144 // There were captures. Check to see if we differ from control:
6145 Py_ssize_t icontrol = nstores;
6146 while (icontrol--) {
6147 PyObject *name = PyList_GET_ITEM(control, icontrol);
6148 Py_ssize_t istores = PySequence_Index(pc->stores, name);
6149 if (istores < 0) {
6150 PyErr_Clear();
6151 goto diff;
6152 }
6153 if (icontrol != istores) {
6154 // Reorder the names on the stack to match the order of the
6155 // names in control. There's probably a better way of doing
6156 // this; the current solution is potentially very
6157 // inefficient when each alternative subpattern binds lots
6158 // of names in different orders. It's fine for reasonable
6159 // cases, though.
6160 assert(istores < icontrol);
6161 Py_ssize_t rotations = istores + 1;
Christian Claussccd82a02021-10-07 17:30:08 +02006162 // Perform the same rotation on pc->stores:
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006163 PyObject *rotated = PyList_GetSlice(pc->stores, 0,
6164 rotations);
6165 if (rotated == NULL ||
6166 PyList_SetSlice(pc->stores, 0, rotations, NULL) ||
6167 PyList_SetSlice(pc->stores, icontrol - istores,
6168 icontrol - istores, rotated))
6169 {
6170 Py_XDECREF(rotated);
6171 goto error;
6172 }
6173 Py_DECREF(rotated);
6174 // That just did:
6175 // rotated = pc_stores[:rotations]
6176 // del pc_stores[:rotations]
6177 // pc_stores[icontrol-istores:icontrol-istores] = rotated
6178 // Do the same thing to the stack, using several ROT_Ns:
6179 while (rotations--) {
6180 if (!compiler_addop_i(c, ROT_N, icontrol + 1)) {
6181 goto error;
6182 }
6183 }
6184 }
6185 }
6186 }
6187 assert(control);
6188 if (!compiler_addop_j(c, JUMP_FORWARD, end) ||
6189 !compiler_next_block(c) ||
6190 !emit_and_reset_fail_pop(c, pc))
6191 {
6192 goto error;
6193 }
Brandt Bucher145bf262021-02-26 14:51:55 -08006194 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006195 Py_DECREF(pc->stores);
6196 *pc = old_pc;
6197 Py_INCREF(pc->stores);
6198 // Need to NULL this for the PyObject_Free call in the error block.
6199 old_pc.fail_pop = NULL;
6200 // No match. Pop the remaining copy of the subject and fail:
6201 if (!compiler_addop(c, POP_TOP) || !jump_to_fail_pop(c, pc, JUMP_FORWARD)) {
6202 goto error;
6203 }
Brandt Bucher145bf262021-02-26 14:51:55 -08006204 compiler_use_next_block(c, end);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006205 Py_ssize_t nstores = PyList_GET_SIZE(control);
6206 // There's a bunch of stuff on the stack between any where the new stores
6207 // are and where they need to be:
6208 // - The other stores.
6209 // - A copy of the subject.
6210 // - Anything else that may be on top of the stack.
6211 // - Any previous stores we've already stashed away on the stack.
Pablo Galindo39494282021-05-03 16:20:46 +01006212 Py_ssize_t nrots = nstores + 1 + pc->on_top + PyList_GET_SIZE(pc->stores);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006213 for (Py_ssize_t i = 0; i < nstores; i++) {
6214 // Rotate this capture to its proper place on the stack:
6215 if (!compiler_addop_i(c, ROT_N, nrots)) {
6216 goto error;
6217 }
6218 // Update the list of previous stores with this new name, checking for
6219 // duplicates:
6220 PyObject *name = PyList_GET_ITEM(control, i);
6221 int dupe = PySequence_Contains(pc->stores, name);
6222 if (dupe < 0) {
6223 goto error;
6224 }
6225 if (dupe) {
6226 compiler_error_duplicate_store(c, name);
6227 goto error;
6228 }
6229 if (PyList_Append(pc->stores, name)) {
6230 goto error;
6231 }
6232 }
6233 Py_DECREF(old_pc.stores);
6234 Py_DECREF(control);
6235 // NOTE: Returning macros are safe again.
6236 // Pop the copy of the subject:
6237 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08006238 return 1;
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006239diff:
6240 compiler_error(c, "alternative patterns bind different names");
6241error:
6242 PyObject_Free(old_pc.fail_pop);
6243 Py_DECREF(old_pc.stores);
Brandt Bucher145bf262021-02-26 14:51:55 -08006244 Py_XDECREF(control);
6245 return 0;
6246}
6247
6248
6249static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006250compiler_pattern_sequence(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006251{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006252 assert(p->kind == MatchSequence_kind);
6253 asdl_pattern_seq *patterns = p->v.MatchSequence.patterns;
6254 Py_ssize_t size = asdl_seq_LEN(patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08006255 Py_ssize_t star = -1;
6256 int only_wildcard = 1;
6257 int star_wildcard = 0;
6258 // Find a starred name, if it exists. There may be at most one:
6259 for (Py_ssize_t i = 0; i < size; i++) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006260 pattern_ty pattern = asdl_seq_GET(patterns, i);
6261 if (pattern->kind == MatchStar_kind) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006262 if (star >= 0) {
6263 const char *e = "multiple starred names in sequence pattern";
6264 return compiler_error(c, e);
6265 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006266 star_wildcard = WILDCARD_STAR_CHECK(pattern);
6267 only_wildcard &= star_wildcard;
Brandt Bucher145bf262021-02-26 14:51:55 -08006268 star = i;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006269 continue;
Brandt Bucher145bf262021-02-26 14:51:55 -08006270 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006271 only_wildcard &= WILDCARD_CHECK(pattern);
Brandt Bucher145bf262021-02-26 14:51:55 -08006272 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006273 // We need to keep the subject on top during the sequence and length checks:
6274 pc->on_top++;
Brandt Bucher145bf262021-02-26 14:51:55 -08006275 ADDOP(c, MATCH_SEQUENCE);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006276 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006277 if (star < 0) {
6278 // No star: len(subject) == size
6279 ADDOP(c, GET_LEN);
6280 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
6281 ADDOP_COMPARE(c, Eq);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006282 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006283 }
6284 else if (size > 1) {
6285 // Star: len(subject) >= size - 1
6286 ADDOP(c, GET_LEN);
6287 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1));
6288 ADDOP_COMPARE(c, GtE);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006289 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006290 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006291 // Whatever comes next should consume the subject:
6292 pc->on_top--;
Brandt Bucher145bf262021-02-26 14:51:55 -08006293 if (only_wildcard) {
6294 // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc.
6295 ADDOP(c, POP_TOP);
Brandt Bucher145bf262021-02-26 14:51:55 -08006296 }
6297 else if (star_wildcard) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006298 RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, patterns, star, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006299 }
6300 else {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006301 RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, patterns, star, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006302 }
Brandt Bucher145bf262021-02-26 14:51:55 -08006303 return 1;
6304}
6305
Brandt Bucher145bf262021-02-26 14:51:55 -08006306static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006307compiler_pattern_value(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006308{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006309 assert(p->kind == MatchValue_kind);
6310 expr_ty value = p->v.MatchValue.value;
6311 if (!MATCH_VALUE_EXPR(value)) {
6312 const char *e = "patterns may only match literals and attribute lookups";
6313 return compiler_error(c, e);
6314 }
6315 VISIT(c, expr, value);
Brandt Bucher145bf262021-02-26 14:51:55 -08006316 ADDOP_COMPARE(c, Eq);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006317 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006318 return 1;
6319}
6320
Brandt Bucher145bf262021-02-26 14:51:55 -08006321static int
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07006322compiler_pattern_singleton(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006323{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006324 assert(p->kind == MatchSingleton_kind);
6325 ADDOP_LOAD_CONST(c, p->v.MatchSingleton.value);
6326 ADDOP_COMPARE(c, Is);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006327 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
Brandt Bucher145bf262021-02-26 14:51:55 -08006328 return 1;
6329}
6330
Brandt Bucher145bf262021-02-26 14:51:55 -08006331static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006332compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006333{
6334 SET_LOC(c, p);
6335 switch (p->kind) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006336 case MatchValue_kind:
Brandt Bucher145bf262021-02-26 14:51:55 -08006337 return compiler_pattern_value(c, p, pc);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006338 case MatchSingleton_kind:
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07006339 return compiler_pattern_singleton(c, p, pc);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006340 case MatchSequence_kind:
Brandt Bucher145bf262021-02-26 14:51:55 -08006341 return compiler_pattern_sequence(c, p, pc);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006342 case MatchMapping_kind:
6343 return compiler_pattern_mapping(c, p, pc);
6344 case MatchClass_kind:
6345 return compiler_pattern_class(c, p, pc);
6346 case MatchStar_kind:
6347 return compiler_pattern_star(c, p, pc);
Brandt Bucher145bf262021-02-26 14:51:55 -08006348 case MatchAs_kind:
6349 return compiler_pattern_as(c, p, pc);
6350 case MatchOr_kind:
6351 return compiler_pattern_or(c, p, pc);
Brandt Bucher145bf262021-02-26 14:51:55 -08006352 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006353 // AST validator shouldn't let this happen, but if it does,
6354 // just fail, don't crash out of the interpreter
6355 const char *e = "invalid match pattern node in AST (kind=%d)";
6356 return compiler_error(c, e, p->kind);
Brandt Bucher145bf262021-02-26 14:51:55 -08006357}
6358
Brandt Bucher145bf262021-02-26 14:51:55 -08006359static int
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006360compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006361{
6362 VISIT(c, expr, s->v.Match.subject);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006363 basicblock *end;
Brandt Bucher145bf262021-02-26 14:51:55 -08006364 RETURN_IF_FALSE(end = compiler_new_block(c));
6365 Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006366 assert(cases > 0);
Brandt Bucher145bf262021-02-26 14:51:55 -08006367 match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6368 int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases;
6369 for (Py_ssize_t i = 0; i < cases - has_default; i++) {
6370 m = asdl_seq_GET(s->v.Match.cases, i);
6371 SET_LOC(c, m->pattern);
Brandt Bucher145bf262021-02-26 14:51:55 -08006372 // Only copy the subject if we're *not* on the last case:
6373 if (i != cases - has_default - 1) {
6374 ADDOP(c, DUP_TOP);
6375 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006376 RETURN_IF_FALSE(pc->stores = PyList_New(0));
6377 // Irrefutable cases must be either guarded, last, or both:
6378 pc->allow_irrefutable = m->guard != NULL || i == cases - 1;
6379 pc->fail_pop = NULL;
6380 pc->fail_pop_size = 0;
6381 pc->on_top = 0;
6382 // NOTE: Can't use returning macros here (they'll leak pc->stores)!
6383 if (!compiler_pattern(c, m->pattern, pc)) {
6384 Py_DECREF(pc->stores);
6385 return 0;
6386 }
6387 assert(!pc->on_top);
6388 // It's a match! Store all of the captured names (they're on the stack).
6389 Py_ssize_t nstores = PyList_GET_SIZE(pc->stores);
6390 for (Py_ssize_t n = 0; n < nstores; n++) {
6391 PyObject *name = PyList_GET_ITEM(pc->stores, n);
6392 if (!compiler_nameop(c, name, Store)) {
6393 Py_DECREF(pc->stores);
6394 return 0;
6395 }
6396 }
6397 Py_DECREF(pc->stores);
6398 // NOTE: Returning macros are safe again.
Brandt Bucher145bf262021-02-26 14:51:55 -08006399 if (m->guard) {
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006400 RETURN_IF_FALSE(ensure_fail_pop(c, pc, 0));
6401 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, pc->fail_pop[0], 0));
Brandt Bucher145bf262021-02-26 14:51:55 -08006402 }
6403 // Success! Pop the subject off, we're done with it:
6404 if (i != cases - has_default - 1) {
6405 ADDOP(c, POP_TOP);
6406 }
6407 VISIT_SEQ(c, stmt, m->body);
6408 ADDOP_JUMP(c, JUMP_FORWARD, end);
Miss Islington (bot)01601aa2021-07-25 17:04:06 -07006409 // If the pattern fails to match, we want the line number of the
6410 // cleanup to be associated with the failed pattern, not the last line
6411 // of the body
6412 SET_LOC(c, m->pattern);
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006413 RETURN_IF_FALSE(emit_and_reset_fail_pop(c, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006414 }
6415 if (has_default) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006416 // A trailing "case _" is common, and lets us save a bit of redundant
6417 // pushing and popping in the loop above:
6418 m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6419 SET_LOC(c, m->pattern);
Miss Islington (bot)01601aa2021-07-25 17:04:06 -07006420 if (cases == 1) {
6421 // No matches. Done with the subject:
6422 ADDOP(c, POP_TOP);
6423 }
6424 else {
6425 // Show line coverage for default case (it doesn't create bytecode)
6426 ADDOP(c, NOP);
6427 }
Brandt Bucher145bf262021-02-26 14:51:55 -08006428 if (m->guard) {
6429 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0));
6430 }
6431 VISIT_SEQ(c, stmt, m->body);
6432 }
6433 compiler_use_next_block(c, end);
6434 return 1;
6435}
6436
Brandt Bucher0ad1e032021-05-02 13:02:10 -07006437static int
6438compiler_match(struct compiler *c, stmt_ty s)
6439{
6440 pattern_context pc;
6441 pc.fail_pop = NULL;
6442 int result = compiler_match_inner(c, s, &pc);
6443 PyObject_Free(pc.fail_pop);
6444 return result;
6445}
6446
Brandt Bucher145bf262021-02-26 14:51:55 -08006447#undef WILDCARD_CHECK
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006448#undef WILDCARD_STAR_CHECK
Brandt Bucher145bf262021-02-26 14:51:55 -08006449
Thomas Wouters89f507f2006-12-13 04:49:30 +00006450/* End of the compiler section, beginning of the assembler section */
6451
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006452/* do depth-first search of basic block graph, starting with block.
T. Wouters99b54d62019-09-12 07:05:33 -07006453 post records the block indices in post-order.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006454
6455 XXX must handle implicit jumps from one block to next
6456*/
6457
Thomas Wouters89f507f2006-12-13 04:49:30 +00006458struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006459 PyObject *a_bytecode; /* string containing bytecode */
6460 int a_offset; /* offset into bytecode */
6461 int a_nblocks; /* number of reachable blocks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006462 PyObject *a_lnotab; /* string containing lnotab */
6463 int a_lnotab_off; /* offset into lnotab */
Mark Shannon877df852020-11-12 09:43:29 +00006464 int a_prevlineno; /* lineno of last emitted line in line table */
6465 int a_lineno; /* lineno of last emitted instruction */
6466 int a_lineno_start; /* bytecode start offset of current lineno */
Mark Shannoncc75ab72020-11-12 19:49:33 +00006467 basicblock *a_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006468};
6469
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006470Py_LOCAL_INLINE(void)
6471stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006472{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02006473 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
Mark Shannonfee55262019-11-21 09:11:43 +00006474 if (b->b_startdepth < depth && b->b_startdepth < 100) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006475 assert(b->b_startdepth < 0);
6476 b->b_startdepth = depth;
6477 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02006478 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006479}
6480
6481/* Find the flow path that needs the largest stack. We assume that
6482 * cycles in the flow graph have no net effect on the stack depth.
6483 */
6484static int
6485stackdepth(struct compiler *c)
6486{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006487 basicblock *b, *entryblock = NULL;
6488 basicblock **stack, **sp;
6489 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006490 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006491 b->b_startdepth = INT_MIN;
6492 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006493 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006494 }
Mark Shannon67969f52021-04-07 10:52:07 +01006495 assert(entryblock!= NULL);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006496 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
6497 if (!stack) {
6498 PyErr_NoMemory();
6499 return -1;
6500 }
6501
6502 sp = stack;
Mark Shannonb37181e2021-04-06 11:48:59 +01006503 if (c->u->u_ste->ste_generator || c->u->u_ste->ste_coroutine) {
6504 stackdepth_push(&sp, entryblock, 1);
6505 } else {
6506 stackdepth_push(&sp, entryblock, 0);
6507 }
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006508 while (sp != stack) {
6509 b = *--sp;
6510 int depth = b->b_startdepth;
6511 assert(depth >= 0);
6512 basicblock *next = b->b_next;
6513 for (int i = 0; i < b->b_iused; i++) {
6514 struct instr *instr = &b->b_instr[i];
6515 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
6516 if (effect == PY_INVALID_STACK_EFFECT) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01006517 PyErr_Format(PyExc_SystemError,
6518 "compiler stack_effect(opcode=%d, arg=%i) failed",
6519 instr->i_opcode, instr->i_oparg);
6520 return -1;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006521 }
6522 int new_depth = depth + effect;
6523 if (new_depth > maxdepth) {
6524 maxdepth = new_depth;
6525 }
6526 assert(depth >= 0); /* invalid code or bug in stackdepth() */
Mark Shannon582aaf12020-08-04 17:30:11 +01006527 if (is_jump(instr)) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006528 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
6529 assert(effect != PY_INVALID_STACK_EFFECT);
6530 int target_depth = depth + effect;
6531 if (target_depth > maxdepth) {
6532 maxdepth = target_depth;
6533 }
6534 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006535 stackdepth_push(&sp, instr->i_target, target_depth);
6536 }
6537 depth = new_depth;
6538 if (instr->i_opcode == JUMP_ABSOLUTE ||
6539 instr->i_opcode == JUMP_FORWARD ||
6540 instr->i_opcode == RETURN_VALUE ||
Mark Shannonfee55262019-11-21 09:11:43 +00006541 instr->i_opcode == RAISE_VARARGS ||
6542 instr->i_opcode == RERAISE)
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006543 {
6544 /* remaining code is dead */
6545 next = NULL;
6546 break;
6547 }
6548 }
6549 if (next != NULL) {
Mark Shannon266b4622020-11-17 19:30:14 +00006550 assert(b->b_nofallthrough == 0);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006551 stackdepth_push(&sp, next, depth);
6552 }
6553 }
6554 PyObject_Free(stack);
6555 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006556}
6557
6558static int
6559assemble_init(struct assembler *a, int nblocks, int firstlineno)
6560{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006561 memset(a, 0, sizeof(struct assembler));
Mark Shannon877df852020-11-12 09:43:29 +00006562 a->a_prevlineno = a->a_lineno = firstlineno;
Mark Shannonfd009e62020-11-13 12:53:53 +00006563 a->a_lnotab = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006564 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006565 if (a->a_bytecode == NULL) {
6566 goto error;
6567 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006568 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006569 if (a->a_lnotab == NULL) {
6570 goto error;
6571 }
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07006572 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006573 PyErr_NoMemory();
Mark Shannonfd009e62020-11-13 12:53:53 +00006574 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006575 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006576 return 1;
Mark Shannonfd009e62020-11-13 12:53:53 +00006577error:
6578 Py_XDECREF(a->a_bytecode);
6579 Py_XDECREF(a->a_lnotab);
6580 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006581}
6582
6583static void
6584assemble_free(struct assembler *a)
6585{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006586 Py_XDECREF(a->a_bytecode);
6587 Py_XDECREF(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006588}
6589
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006590static int
6591blocksize(basicblock *b)
6592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006593 int i;
6594 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006596 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006597 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006598 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006599}
6600
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006601static int
Mark Shannon877df852020-11-12 09:43:29 +00006602assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006603{
Mark Shannon877df852020-11-12 09:43:29 +00006604 Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006605 if (a->a_lnotab_off + 2 >= len) {
6606 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
6607 return 0;
6608 }
Pablo Galindo86e322f2021-01-30 13:54:22 +00006609 unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab);
6610 lnotab += a->a_lnotab_off;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006611 a->a_lnotab_off += 2;
Mark Shannon877df852020-11-12 09:43:29 +00006612 *lnotab++ = bdelta;
6613 *lnotab++ = ldelta;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006614 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006615}
6616
Mark Shannon877df852020-11-12 09:43:29 +00006617/* Appends a range to the end of the line number table. See
6618 * Objects/lnotab_notes.txt for the description of the line number table. */
6619
6620static int
6621assemble_line_range(struct assembler *a)
6622{
6623 int ldelta, bdelta;
Serhiy Storchaka1670d592021-10-04 17:07:21 +03006624 bdelta = (a->a_offset - a->a_lineno_start) * sizeof(_Py_CODEUNIT);
Mark Shannon877df852020-11-12 09:43:29 +00006625 if (bdelta == 0) {
6626 return 1;
6627 }
6628 if (a->a_lineno < 0) {
6629 ldelta = -128;
6630 }
6631 else {
6632 ldelta = a->a_lineno - a->a_prevlineno;
6633 a->a_prevlineno = a->a_lineno;
6634 while (ldelta > 127) {
6635 if (!assemble_emit_linetable_pair(a, 0, 127)) {
6636 return 0;
6637 }
6638 ldelta -= 127;
6639 }
6640 while (ldelta < -127) {
6641 if (!assemble_emit_linetable_pair(a, 0, -127)) {
6642 return 0;
6643 }
6644 ldelta += 127;
6645 }
6646 }
6647 assert(-128 <= ldelta && ldelta < 128);
6648 while (bdelta > 254) {
6649 if (!assemble_emit_linetable_pair(a, 254, ldelta)) {
6650 return 0;
6651 }
6652 ldelta = a->a_lineno < 0 ? -128 : 0;
6653 bdelta -= 254;
6654 }
6655 if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) {
6656 return 0;
6657 }
6658 a->a_lineno_start = a->a_offset;
6659 return 1;
6660}
6661
6662static int
6663assemble_lnotab(struct assembler *a, struct instr *i)
6664{
6665 if (i->i_lineno == a->a_lineno) {
6666 return 1;
6667 }
6668 if (!assemble_line_range(a)) {
6669 return 0;
6670 }
6671 a->a_lineno = i->i_lineno;
6672 return 1;
6673}
6674
6675
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006676/* assemble_emit()
6677 Extend the bytecode with a new instruction.
6678 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00006679*/
6680
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006681static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006682assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006683{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006684 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006685 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03006686 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006687
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006688 arg = i->i_oparg;
6689 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006690 if (i->i_lineno && !assemble_lnotab(a, i))
6691 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006692 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006693 if (len > PY_SSIZE_T_MAX / 2)
6694 return 0;
6695 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
6696 return 0;
6697 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03006698 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006699 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006700 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006701 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006702}
6703
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00006704static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006705assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006707 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006708 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006709 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00006710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006711 /* Compute the size of each block and fixup jump args.
6712 Replace block pointer with position in bytecode. */
6713 do {
6714 totsize = 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006715 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006716 bsize = blocksize(b);
6717 b->b_offset = totsize;
6718 totsize += bsize;
6719 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006720 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006721 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6722 bsize = b->b_offset;
6723 for (i = 0; i < b->b_iused; i++) {
6724 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006725 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006726 /* Relative jumps are computed relative to
6727 the instruction pointer after fetching
6728 the jump instruction.
6729 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006730 bsize += isize;
Mark Shannon582aaf12020-08-04 17:30:11 +01006731 if (is_jump(instr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006732 instr->i_oparg = instr->i_target->b_offset;
Mark Shannon582aaf12020-08-04 17:30:11 +01006733 if (is_relative_jump(instr)) {
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006734 instr->i_oparg -= bsize;
6735 }
6736 if (instrsize(instr->i_oparg) != isize) {
6737 extended_arg_recompile = 1;
6738 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006739 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006740 }
6741 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00006742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006743 /* XXX: This is an awful hack that could hurt performance, but
6744 on the bright side it should work until we come up
6745 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006747 The issue is that in the first loop blocksize() is called
6748 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006749 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006750 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006752 So we loop until we stop seeing new EXTENDED_ARGs.
6753 The only EXTENDED_ARGs that could be popping up are
6754 ones in jump instructions. So this should converge
6755 fairly quickly.
6756 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006757 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00006758}
6759
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006760static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01006761dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006762{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006763 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006764 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006766 tuple = PyTuple_New(size);
6767 if (tuple == NULL)
6768 return NULL;
6769 while (PyDict_Next(dict, &pos, &k, &v)) {
6770 i = PyLong_AS_LONG(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006771 Py_INCREF(k);
6772 assert((i - offset) < size);
6773 assert((i - offset) >= 0);
6774 PyTuple_SET_ITEM(tuple, i - offset, k);
6775 }
6776 return tuple;
6777}
6778
6779static PyObject *
6780consts_dict_keys_inorder(PyObject *dict)
6781{
6782 PyObject *consts, *k, *v;
6783 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
6784
6785 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
6786 if (consts == NULL)
6787 return NULL;
6788 while (PyDict_Next(dict, &pos, &k, &v)) {
6789 i = PyLong_AS_LONG(v);
Christian Claussccd82a02021-10-07 17:30:08 +02006790 /* The keys of the dictionary can be tuples wrapping a constant.
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +03006791 * (see compiler_add_o and _PyCode_ConstantKey). In that case
6792 * the object we want is always second. */
6793 if (PyTuple_CheckExact(k)) {
6794 k = PyTuple_GET_ITEM(k, 1);
6795 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006796 Py_INCREF(k);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006797 assert(i < size);
6798 assert(i >= 0);
6799 PyList_SET_ITEM(consts, i, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006800 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006801 return consts;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006802}
6803
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006804static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006805compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006806{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006807 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006808 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006809 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04006810 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006811 if (ste->ste_nested)
6812 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07006813 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006814 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07006815 if (!ste->ste_generator && ste->ste_coroutine)
6816 flags |= CO_COROUTINE;
6817 if (ste->ste_generator && ste->ste_coroutine)
6818 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006819 if (ste->ste_varargs)
6820 flags |= CO_VARARGS;
6821 if (ste->ste_varkeywords)
6822 flags |= CO_VARKEYWORDS;
6823 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006825 /* (Only) inherit compilerflags in PyCF_MASK */
6826 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006827
Pablo Galindo90235812020-03-15 04:29:22 +00006828 if ((IS_TOP_LEVEL_AWAIT(c)) &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07006829 ste->ste_coroutine &&
6830 !ste->ste_generator) {
6831 flags |= CO_COROUTINE;
6832 }
6833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006834 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00006835}
6836
Inada Naokibdb941b2021-02-10 09:20:42 +09006837// Merge *obj* with constant cache.
INADA Naokic2e16072018-11-26 21:23:22 +09006838// Unlike merge_consts_recursive(), this function doesn't work recursively.
6839static int
Inada Naokibdb941b2021-02-10 09:20:42 +09006840merge_const_one(struct compiler *c, PyObject **obj)
INADA Naokic2e16072018-11-26 21:23:22 +09006841{
Inada Naokibdb941b2021-02-10 09:20:42 +09006842 PyObject *key = _PyCode_ConstantKey(*obj);
INADA Naokic2e16072018-11-26 21:23:22 +09006843 if (key == NULL) {
6844 return 0;
6845 }
6846
6847 // t is borrowed reference
6848 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
6849 Py_DECREF(key);
6850 if (t == NULL) {
6851 return 0;
6852 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006853 if (t == key) { // obj is new constant.
INADA Naokic2e16072018-11-26 21:23:22 +09006854 return 1;
6855 }
6856
Inada Naokibdb941b2021-02-10 09:20:42 +09006857 if (PyTuple_CheckExact(t)) {
6858 // t is still borrowed reference
6859 t = PyTuple_GET_ITEM(t, 1);
6860 }
6861
6862 Py_INCREF(t);
6863 Py_DECREF(*obj);
6864 *obj = t;
INADA Naokic2e16072018-11-26 21:23:22 +09006865 return 1;
6866}
6867
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006868static PyCodeObject *
Mark Shannon6e8128f2020-07-30 10:03:00 +01006869makecode(struct compiler *c, struct assembler *a, PyObject *consts)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006870{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006871 PyCodeObject *co = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006872 PyObject *names = NULL;
6873 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006874 PyObject *name = NULL;
6875 PyObject *freevars = NULL;
6876 PyObject *cellvars = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006877 Py_ssize_t nlocals;
6878 int nlocals_int;
6879 int flags;
Pablo Galindocd74e662019-06-01 18:08:04 +01006880 int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006882 names = dict_keys_inorder(c->u->u_names, 0);
6883 varnames = dict_keys_inorder(c->u->u_varnames, 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006884 if (!names || !varnames) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006885 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006886 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006887 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
6888 if (!cellvars)
6889 goto error;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006890 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006891 if (!freevars)
6892 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006893
Inada Naokibdb941b2021-02-10 09:20:42 +09006894 if (!merge_const_one(c, &names) ||
6895 !merge_const_one(c, &varnames) ||
6896 !merge_const_one(c, &cellvars) ||
6897 !merge_const_one(c, &freevars))
INADA Naokic2e16072018-11-26 21:23:22 +09006898 {
6899 goto error;
6900 }
6901
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006902 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01006903 assert(nlocals < INT_MAX);
6904 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
6905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006906 flags = compute_code_flags(c);
6907 if (flags < 0)
6908 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006909
Mark Shannon6e8128f2020-07-30 10:03:00 +01006910 consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
6911 if (consts == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006912 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006913 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006914 if (!merge_const_one(c, &consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006915 Py_DECREF(consts);
INADA Naokic2e16072018-11-26 21:23:22 +09006916 goto error;
6917 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006918
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01006919 posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int);
Pablo Galindocd74e662019-06-01 18:08:04 +01006920 posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +01006921 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006922 maxdepth = stackdepth(c);
6923 if (maxdepth < 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006924 Py_DECREF(consts);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006925 goto error;
6926 }
Mark Shannon11e0b292021-04-15 14:28:56 +01006927 if (maxdepth > MAX_ALLOWED_STACK_USE) {
6928 PyErr_Format(PyExc_SystemError,
6929 "excessive stack use: stack is %d deep",
6930 maxdepth);
6931 Py_DECREF(consts);
6932 goto error;
6933 }
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006934 co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
Mark Shannon13bc1392020-01-23 09:25:17 +00006935 posonlyargcount, kwonlyargcount, nlocals_int,
Mark Shannon6e8128f2020-07-30 10:03:00 +01006936 maxdepth, flags, a->a_bytecode, consts, names,
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006937 varnames, freevars, cellvars, c->c_filename,
6938 c->u->u_name, c->u->u_firstlineno, a->a_lnotab);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006939 Py_DECREF(consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006940 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006941 Py_XDECREF(names);
6942 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006943 Py_XDECREF(name);
6944 Py_XDECREF(freevars);
6945 Py_XDECREF(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006946 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006947}
6948
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006949
6950/* For debugging purposes only */
6951#if 0
6952static void
Mark Shannon37686f72021-07-16 11:49:10 +01006953dump_instr(struct instr *i)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006954{
Mark Shannon37686f72021-07-16 11:49:10 +01006955 const char *jrel = (is_relative_jump(i)) ? "jrel " : "";
6956 const char *jabs = (is_jump(i) && !is_relative_jump(i))? "jabs " : "";
6957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006958 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006960 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006961 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006962 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006963 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006964 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
6965 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006966}
6967
6968static void
6969dump_basicblock(const basicblock *b)
6970{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006971 const char *b_return = b->b_return ? "return " : "";
Pablo Galindo60eb9f12020-06-28 01:55:47 +01006972 fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n",
6973 b->b_iused, b->b_startdepth, b->b_offset, b_return);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006974 if (b->b_instr) {
6975 int i;
6976 for (i = 0; i < b->b_iused; i++) {
6977 fprintf(stderr, " [%02d] ", i);
6978 dump_instr(b->b_instr + i);
6979 }
6980 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006981}
6982#endif
6983
Mark Shannon5977a792020-12-02 13:31:40 +00006984
6985static int
6986normalize_basic_block(basicblock *bb);
6987
Mark Shannon6e8128f2020-07-30 10:03:00 +01006988static int
Inada Naoki8a232c72021-04-16 14:01:04 +09006989optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006990
Ɓukasz Langad41abe82021-09-08 18:25:09 +02006991static int
6992trim_unused_consts(struct compiler *c, struct assembler *a, PyObject *consts);
6993
Mark Shannon762ef852021-08-09 10:54:48 +01006994/* Duplicates exit BBs, so that line numbers can be propagated to them */
Mark Shannon5977a792020-12-02 13:31:40 +00006995static int
Mark Shannon762ef852021-08-09 10:54:48 +01006996duplicate_exits_without_lineno(struct compiler *c);
Mark Shannon5977a792020-12-02 13:31:40 +00006997
Mark Shannonb37181e2021-04-06 11:48:59 +01006998static int
Mark Shannon37686f72021-07-16 11:49:10 +01006999extend_block(basicblock *bb);
7000
7001static int
Mark Shannonb37181e2021-04-06 11:48:59 +01007002insert_generator_prefix(struct compiler *c, basicblock *entryblock) {
7003
7004 int flags = compute_code_flags(c);
7005 if (flags < 0) {
7006 return -1;
7007 }
7008 int kind;
7009 if (flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
7010 if (flags & CO_COROUTINE) {
7011 kind = 1;
7012 }
7013 else if (flags & CO_ASYNC_GENERATOR) {
7014 kind = 2;
7015 }
7016 else {
7017 kind = 0;
7018 }
7019 }
7020 else {
7021 return 0;
7022 }
7023 if (compiler_next_instr(entryblock) < 0) {
7024 return -1;
7025 }
7026 for (int i = entryblock->b_iused-1; i > 0; i--) {
7027 entryblock->b_instr[i] = entryblock->b_instr[i-1];
7028 }
7029 entryblock->b_instr[0].i_opcode = GEN_START;
7030 entryblock->b_instr[0].i_oparg = kind;
7031 entryblock->b_instr[0].i_lineno = -1;
7032 entryblock->b_instr[0].i_target = NULL;
7033 return 0;
7034}
7035
Mark Shannon0acdf252021-05-13 14:11:41 +01007036/* Make sure that all returns have a line number, even if early passes
7037 * have failed to propagate a correct line number.
7038 * The resulting line number may not be correct according to PEP 626,
7039 * but should be "good enough", and no worse than in older versions. */
7040static void
7041guarantee_lineno_for_exits(struct assembler *a, int firstlineno) {
7042 int lineno = firstlineno;
7043 assert(lineno > 0);
7044 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7045 if (b->b_iused == 0) {
7046 continue;
7047 }
7048 struct instr *last = &b->b_instr[b->b_iused-1];
7049 if (last->i_lineno < 0) {
Mark Shannon762ef852021-08-09 10:54:48 +01007050 if (last->i_opcode == RETURN_VALUE) {
Mark Shannon0acdf252021-05-13 14:11:41 +01007051 for (int i = 0; i < b->b_iused; i++) {
7052 assert(b->b_instr[i].i_lineno < 0);
Mark Shannon762ef852021-08-09 10:54:48 +01007053
Mark Shannon0acdf252021-05-13 14:11:41 +01007054 b->b_instr[i].i_lineno = lineno;
7055 }
7056 }
7057 }
7058 else {
7059 lineno = last->i_lineno;
7060 }
7061 }
7062}
7063
Mark Shannon762ef852021-08-09 10:54:48 +01007064static void
7065propagate_line_numbers(struct assembler *a);
7066
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007067static PyCodeObject *
7068assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00007069{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007070 basicblock *b, *entryblock;
7071 struct assembler a;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007072 int j, nblocks;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007073 PyCodeObject *co = NULL;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007074 PyObject *consts = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00007075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007076 /* Make sure every block that falls off the end returns None.
7077 XXX NEXT_BLOCK() isn't quite right, because if the last
7078 block ends with a jump or return b_next shouldn't set.
7079 */
7080 if (!c->u->u_curblock->b_return) {
Mark Shannon877df852020-11-12 09:43:29 +00007081 c->u->u_lineno = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007082 if (addNone)
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03007083 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007084 ADDOP(c, RETURN_VALUE);
7085 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00007086
Mark Shannon5977a792020-12-02 13:31:40 +00007087 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7088 if (normalize_basic_block(b)) {
Alex Henrie503627f2021-03-02 03:20:25 -07007089 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00007090 }
7091 }
7092
Mark Shannon37686f72021-07-16 11:49:10 +01007093 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7094 if (extend_block(b)) {
7095 return NULL;
7096 }
7097 }
7098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007099 nblocks = 0;
7100 entryblock = NULL;
7101 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
7102 nblocks++;
7103 entryblock = b;
7104 }
Mark Shannon67969f52021-04-07 10:52:07 +01007105 assert(entryblock != NULL);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00007106
Mark Shannonb37181e2021-04-06 11:48:59 +01007107 if (insert_generator_prefix(c, entryblock)) {
7108 goto error;
7109 }
7110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007111 /* Set firstlineno if it wasn't explicitly set. */
7112 if (!c->u->u_firstlineno) {
Mark Shannon67969f52021-04-07 10:52:07 +01007113 if (entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007114 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
Mark Shannon877df852020-11-12 09:43:29 +00007115 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007116 c->u->u_firstlineno = 1;
7117 }
Mark Shannon5977a792020-12-02 13:31:40 +00007118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007119 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
7120 goto error;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007121 a.a_entry = entryblock;
7122 a.a_nblocks = nblocks;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00007123
Mark Shannon6e8128f2020-07-30 10:03:00 +01007124 consts = consts_dict_keys_inorder(c->u->u_consts);
7125 if (consts == NULL) {
7126 goto error;
7127 }
Mark Shannon37686f72021-07-16 11:49:10 +01007128
Inada Naoki8a232c72021-04-16 14:01:04 +09007129 if (optimize_cfg(c, &a, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007130 goto error;
7131 }
Mark Shannon762ef852021-08-09 10:54:48 +01007132 if (duplicate_exits_without_lineno(c)) {
7133 return NULL;
7134 }
Ɓukasz Langad41abe82021-09-08 18:25:09 +02007135 if (trim_unused_consts(c, &a, consts)) {
7136 goto error;
7137 }
Mark Shannon762ef852021-08-09 10:54:48 +01007138 propagate_line_numbers(&a);
Mark Shannon0acdf252021-05-13 14:11:41 +01007139 guarantee_lineno_for_exits(&a, c->u->u_firstlineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007140 /* Can't modify the bytecode after computing jump offsets. */
7141 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00007142
Mark Shannoncc75ab72020-11-12 19:49:33 +00007143 /* Emit code. */
7144 for(b = entryblock; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007145 for (j = 0; j < b->b_iused; j++)
7146 if (!assemble_emit(&a, &b->b_instr[j]))
7147 goto error;
7148 }
Mark Shannon877df852020-11-12 09:43:29 +00007149 if (!assemble_line_range(&a)) {
7150 return 0;
7151 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00007152
Inada Naokibdb941b2021-02-10 09:20:42 +09007153 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007154 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09007155 }
7156 if (!merge_const_one(c, &a.a_lnotab)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007157 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09007158 }
7159 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) {
7160 goto error;
7161 }
7162 if (!merge_const_one(c, &a.a_bytecode)) {
7163 goto error;
7164 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00007165
Mark Shannon6e8128f2020-07-30 10:03:00 +01007166 co = makecode(c, &a, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007167 error:
Mark Shannon6e8128f2020-07-30 10:03:00 +01007168 Py_XDECREF(consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007169 assemble_free(&a);
7170 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00007171}
Georg Brandl8334fd92010-12-04 10:26:46 +00007172
Mark Shannon6e8128f2020-07-30 10:03:00 +01007173/* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
7174 with LOAD_CONST (c1, c2, ... cn).
7175 The consts table must still be in list form so that the
7176 new constant (c1, c2, ... cn) can be appended.
7177 Called with codestr pointing to the first LOAD_CONST.
7178*/
7179static int
Inada Naoki8a232c72021-04-16 14:01:04 +09007180fold_tuple_on_constants(struct compiler *c,
7181 struct instr *inst,
Mark Shannon6e8128f2020-07-30 10:03:00 +01007182 int n, PyObject *consts)
7183{
7184 /* Pre-conditions */
7185 assert(PyList_CheckExact(consts));
7186 assert(inst[n].i_opcode == BUILD_TUPLE);
7187 assert(inst[n].i_oparg == n);
7188
7189 for (int i = 0; i < n; i++) {
7190 if (inst[i].i_opcode != LOAD_CONST) {
7191 return 0;
7192 }
7193 }
7194
7195 /* Buildup new tuple of constants */
7196 PyObject *newconst = PyTuple_New(n);
7197 if (newconst == NULL) {
7198 return -1;
7199 }
7200 for (int i = 0; i < n; i++) {
7201 int arg = inst[i].i_oparg;
7202 PyObject *constant = PyList_GET_ITEM(consts, arg);
7203 Py_INCREF(constant);
7204 PyTuple_SET_ITEM(newconst, i, constant);
7205 }
Inada Naoki8a232c72021-04-16 14:01:04 +09007206 if (merge_const_one(c, &newconst) == 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007207 Py_DECREF(newconst);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007208 return -1;
7209 }
Inada Naoki8a232c72021-04-16 14:01:04 +09007210
7211 Py_ssize_t index;
7212 for (index = 0; index < PyList_GET_SIZE(consts); index++) {
7213 if (PyList_GET_ITEM(consts, index) == newconst) {
7214 break;
7215 }
7216 }
7217 if (index == PyList_GET_SIZE(consts)) {
7218 if ((size_t)index >= (size_t)INT_MAX - 1) {
7219 Py_DECREF(newconst);
7220 PyErr_SetString(PyExc_OverflowError, "too many constants");
7221 return -1;
7222 }
7223 if (PyList_Append(consts, newconst)) {
7224 Py_DECREF(newconst);
7225 return -1;
7226 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007227 }
7228 Py_DECREF(newconst);
7229 for (int i = 0; i < n; i++) {
7230 inst[i].i_opcode = NOP;
7231 }
7232 inst[n].i_opcode = LOAD_CONST;
Victor Stinner71f2ff42020-09-23 14:06:55 +02007233 inst[n].i_oparg = (int)index;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007234 return 0;
7235}
7236
Mark Shannon28b75c82020-12-23 11:43:10 +00007237
Brandt Bucher0ad1e032021-05-02 13:02:10 -07007238// Eliminate n * ROT_N(n).
7239static void
7240fold_rotations(struct instr *inst, int n)
7241{
7242 for (int i = 0; i < n; i++) {
7243 int rot;
7244 switch (inst[i].i_opcode) {
7245 case ROT_N:
7246 rot = inst[i].i_oparg;
7247 break;
7248 case ROT_FOUR:
7249 rot = 4;
7250 break;
7251 case ROT_THREE:
7252 rot = 3;
7253 break;
7254 case ROT_TWO:
7255 rot = 2;
7256 break;
7257 default:
7258 return;
7259 }
7260 if (rot != n) {
7261 return;
7262 }
7263 }
7264 for (int i = 0; i < n; i++) {
7265 inst[i].i_opcode = NOP;
7266 }
7267}
7268
Brandt Buchera89bbde2021-11-11 13:52:43 -08007269// Attempt to eliminate jumps to jumps by updating inst to jump to
7270// target->i_target using the provided opcode. Return whether or not the
7271// optimization was successful.
7272static bool
7273jump_thread(struct instr *inst, struct instr *target, int opcode)
7274{
7275 assert(is_jump(inst));
7276 assert(is_jump(target));
7277 // bpo-45773: If inst->i_target == target->i_target, then nothing actually
7278 // changes (and we fall into an infinite loop):
7279 if (inst->i_lineno == target->i_lineno &&
7280 inst->i_target != target->i_target)
7281 {
7282 inst->i_target = target->i_target;
7283 inst->i_opcode = opcode;
7284 return true;
Mark Shannon28b75c82020-12-23 11:43:10 +00007285 }
Brandt Buchera89bbde2021-11-11 13:52:43 -08007286 return false;
Mark Shannon28b75c82020-12-23 11:43:10 +00007287}
7288
Mark Shannoncc75ab72020-11-12 19:49:33 +00007289/* Maximum size of basic block that should be copied in optimizer */
7290#define MAX_COPY_SIZE 4
Mark Shannon6e8128f2020-07-30 10:03:00 +01007291
7292/* Optimization */
7293static int
Inada Naoki8a232c72021-04-16 14:01:04 +09007294optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
Mark Shannon6e8128f2020-07-30 10:03:00 +01007295{
7296 assert(PyList_CheckExact(consts));
7297 struct instr nop;
7298 nop.i_opcode = NOP;
7299 struct instr *target;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007300 for (int i = 0; i < bb->b_iused; i++) {
7301 struct instr *inst = &bb->b_instr[i];
7302 int oparg = inst->i_oparg;
7303 int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01007304 if (is_jump(inst)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007305 /* Skip over empty basic blocks. */
7306 while (inst->i_target->b_iused == 0) {
7307 inst->i_target = inst->i_target->b_next;
7308 }
7309 target = &inst->i_target->b_instr[0];
7310 }
7311 else {
7312 target = &nop;
7313 }
7314 switch (inst->i_opcode) {
Mark Shannon266b4622020-11-17 19:30:14 +00007315 /* Remove LOAD_CONST const; conditional jump */
Mark Shannon6e8128f2020-07-30 10:03:00 +01007316 case LOAD_CONST:
Mark Shannon266b4622020-11-17 19:30:14 +00007317 {
7318 PyObject* cnt;
7319 int is_true;
7320 int jump_if_true;
7321 switch(nextop) {
7322 case POP_JUMP_IF_FALSE:
7323 case POP_JUMP_IF_TRUE:
7324 cnt = PyList_GET_ITEM(consts, oparg);
7325 is_true = PyObject_IsTrue(cnt);
7326 if (is_true == -1) {
7327 goto error;
7328 }
7329 inst->i_opcode = NOP;
7330 jump_if_true = nextop == POP_JUMP_IF_TRUE;
7331 if (is_true == jump_if_true) {
7332 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
7333 bb->b_nofallthrough = 1;
7334 }
7335 else {
7336 bb->b_instr[i+1].i_opcode = NOP;
7337 }
7338 break;
7339 case JUMP_IF_FALSE_OR_POP:
7340 case JUMP_IF_TRUE_OR_POP:
7341 cnt = PyList_GET_ITEM(consts, oparg);
7342 is_true = PyObject_IsTrue(cnt);
7343 if (is_true == -1) {
7344 goto error;
7345 }
7346 jump_if_true = nextop == JUMP_IF_TRUE_OR_POP;
7347 if (is_true == jump_if_true) {
7348 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
7349 bb->b_nofallthrough = 1;
7350 }
7351 else {
7352 inst->i_opcode = NOP;
7353 bb->b_instr[i+1].i_opcode = NOP;
7354 }
7355 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007356 }
7357 break;
Mark Shannon266b4622020-11-17 19:30:14 +00007358 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007359
7360 /* Try to fold tuples of constants.
7361 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
7362 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
7363 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
7364 case BUILD_TUPLE:
7365 if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
7366 switch(oparg) {
7367 case 1:
7368 inst->i_opcode = NOP;
7369 bb->b_instr[i+1].i_opcode = NOP;
7370 break;
7371 case 2:
7372 inst->i_opcode = ROT_TWO;
7373 bb->b_instr[i+1].i_opcode = NOP;
7374 break;
7375 case 3:
7376 inst->i_opcode = ROT_THREE;
7377 bb->b_instr[i+1].i_opcode = ROT_TWO;
7378 }
7379 break;
7380 }
7381 if (i >= oparg) {
Inada Naoki8a232c72021-04-16 14:01:04 +09007382 if (fold_tuple_on_constants(c, inst-oparg, oparg, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007383 goto error;
7384 }
7385 }
7386 break;
7387
7388 /* Simplify conditional jump to conditional jump where the
7389 result of the first test implies the success of a similar
7390 test or the failure of the opposite test.
7391 Arises in code like:
7392 "a and b or c"
7393 "(a and b) and c"
7394 "(a or b) or c"
7395 "(a or b) and c"
7396 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
7397 --> x:JUMP_IF_FALSE_OR_POP z
7398 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
7399 --> x:POP_JUMP_IF_FALSE y+1
7400 where y+1 is the instruction following the second test.
7401 */
7402 case JUMP_IF_FALSE_OR_POP:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007403 switch (target->i_opcode) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007404 case POP_JUMP_IF_FALSE:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007405 i -= jump_thread(inst, target, POP_JUMP_IF_FALSE);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007406 break;
7407 case JUMP_ABSOLUTE:
7408 case JUMP_FORWARD:
7409 case JUMP_IF_FALSE_OR_POP:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007410 i -= jump_thread(inst, target, JUMP_IF_FALSE_OR_POP);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007411 break;
7412 case JUMP_IF_TRUE_OR_POP:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007413 case POP_JUMP_IF_TRUE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007414 if (inst->i_lineno == target->i_lineno) {
Brandt Buchera89bbde2021-11-11 13:52:43 -08007415 // We don't need to bother checking for loops here,
7416 // since a block's b_next cannot point to itself:
7417 assert(inst->i_target != inst->i_target->b_next);
Mark Shannon28b75c82020-12-23 11:43:10 +00007418 inst->i_opcode = POP_JUMP_IF_FALSE;
7419 inst->i_target = inst->i_target->b_next;
7420 --i;
7421 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007422 break;
7423 }
7424 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007425 case JUMP_IF_TRUE_OR_POP:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007426 switch (target->i_opcode) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007427 case POP_JUMP_IF_TRUE:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007428 i -= jump_thread(inst, target, POP_JUMP_IF_TRUE);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007429 break;
7430 case JUMP_ABSOLUTE:
7431 case JUMP_FORWARD:
7432 case JUMP_IF_TRUE_OR_POP:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007433 i -= jump_thread(inst, target, JUMP_IF_TRUE_OR_POP);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007434 break;
7435 case JUMP_IF_FALSE_OR_POP:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007436 case POP_JUMP_IF_FALSE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007437 if (inst->i_lineno == target->i_lineno) {
Brandt Buchera89bbde2021-11-11 13:52:43 -08007438 // We don't need to bother checking for loops here,
7439 // since a block's b_next cannot point to itself:
7440 assert(inst->i_target != inst->i_target->b_next);
Mark Shannon28b75c82020-12-23 11:43:10 +00007441 inst->i_opcode = POP_JUMP_IF_TRUE;
7442 inst->i_target = inst->i_target->b_next;
7443 --i;
7444 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007445 break;
7446 }
7447 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007448 case POP_JUMP_IF_FALSE:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007449 switch (target->i_opcode) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007450 case JUMP_ABSOLUTE:
7451 case JUMP_FORWARD:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007452 i -= jump_thread(inst, target, POP_JUMP_IF_FALSE);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007453 }
7454 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007455 case POP_JUMP_IF_TRUE:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007456 switch (target->i_opcode) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007457 case JUMP_ABSOLUTE:
7458 case JUMP_FORWARD:
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 }
7461 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007462 case JUMP_ABSOLUTE:
7463 case JUMP_FORWARD:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007464 switch (target->i_opcode) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007465 case JUMP_ABSOLUTE:
Brandt Buchera89bbde2021-11-11 13:52:43 -08007466 case JUMP_FORWARD:
7467 i -= jump_thread(inst, target, JUMP_ABSOLUTE);
Mark Shannon37686f72021-07-16 11:49:10 +01007468 }
7469 break;
7470 case FOR_ITER:
Mark Shannon37686f72021-07-16 11:49:10 +01007471 if (target->i_opcode == JUMP_FORWARD) {
Brandt Buchera89bbde2021-11-11 13:52:43 -08007472 i -= jump_thread(inst, target, FOR_ITER);
Mark Shannoncc75ab72020-11-12 19:49:33 +00007473 }
Brandt Bucher0ad1e032021-05-02 13:02:10 -07007474 break;
7475 case ROT_N:
7476 switch (oparg) {
7477 case 0:
7478 case 1:
7479 inst->i_opcode = NOP;
7480 continue;
7481 case 2:
7482 inst->i_opcode = ROT_TWO;
7483 break;
7484 case 3:
7485 inst->i_opcode = ROT_THREE;
7486 break;
7487 case 4:
7488 inst->i_opcode = ROT_FOUR;
7489 break;
7490 }
7491 if (i >= oparg - 1) {
7492 fold_rotations(inst - oparg + 1, oparg);
7493 }
7494 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007495 }
7496 }
7497 return 0;
7498error:
7499 return -1;
7500}
7501
Mark Shannon37686f72021-07-16 11:49:10 +01007502/* If this block ends with an unconditional jump to an exit block,
7503 * then remove the jump and extend this block with the target.
7504 */
7505static int
7506extend_block(basicblock *bb) {
7507 if (bb->b_iused == 0) {
7508 return 0;
7509 }
7510 struct instr *last = &bb->b_instr[bb->b_iused-1];
7511 if (last->i_opcode != JUMP_ABSOLUTE && last->i_opcode != JUMP_FORWARD) {
7512 return 0;
7513 }
7514 if (last->i_target->b_exit && last->i_target->b_iused <= MAX_COPY_SIZE) {
7515 basicblock *to_copy = last->i_target;
7516 last->i_opcode = NOP;
7517 for (int i = 0; i < to_copy->b_iused; i++) {
7518 int index = compiler_next_instr(bb);
7519 if (index < 0) {
7520 return -1;
7521 }
7522 bb->b_instr[index] = to_copy->b_instr[i];
7523 }
7524 bb->b_exit = 1;
7525 }
7526 return 0;
7527}
Mark Shannon6e8128f2020-07-30 10:03:00 +01007528
7529static void
Mark Shannon1659ad12021-01-13 15:05:04 +00007530clean_basic_block(basicblock *bb, int prev_lineno) {
7531 /* Remove NOPs when legal to do so. */
Mark Shannon6e8128f2020-07-30 10:03:00 +01007532 int dest = 0;
7533 for (int src = 0; src < bb->b_iused; src++) {
Mark Shannon877df852020-11-12 09:43:29 +00007534 int lineno = bb->b_instr[src].i_lineno;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007535 if (bb->b_instr[src].i_opcode == NOP) {
Mark Shannon266b4622020-11-17 19:30:14 +00007536 /* Eliminate no-op if it doesn't have a line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007537 if (lineno < 0) {
7538 continue;
7539 }
Mark Shannon266b4622020-11-17 19:30:14 +00007540 /* or, if the previous instruction had the same line number. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007541 if (prev_lineno == lineno) {
7542 continue;
7543 }
Mark Shannon266b4622020-11-17 19:30:14 +00007544 /* or, if the next instruction has same line number or no line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007545 if (src < bb->b_iused - 1) {
7546 int next_lineno = bb->b_instr[src+1].i_lineno;
7547 if (next_lineno < 0 || next_lineno == lineno) {
7548 bb->b_instr[src+1].i_lineno = lineno;
7549 continue;
Mark Shannon877df852020-11-12 09:43:29 +00007550 }
7551 }
Mark Shannon266b4622020-11-17 19:30:14 +00007552 else {
7553 basicblock* next = bb->b_next;
7554 while (next && next->b_iused == 0) {
7555 next = next->b_next;
7556 }
7557 /* or if last instruction in BB and next BB has same line number */
7558 if (next) {
7559 if (lineno == next->b_instr[0].i_lineno) {
7560 continue;
7561 }
7562 }
7563 }
7564
Mark Shannon6e8128f2020-07-30 10:03:00 +01007565 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007566 if (dest != src) {
7567 bb->b_instr[dest] = bb->b_instr[src];
7568 }
7569 dest++;
7570 prev_lineno = lineno;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007571 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007572 assert(dest <= bb->b_iused);
7573 bb->b_iused = dest;
7574}
7575
Mark Shannon266b4622020-11-17 19:30:14 +00007576static int
7577normalize_basic_block(basicblock *bb) {
7578 /* Mark blocks as exit and/or nofallthrough.
7579 Raise SystemError if CFG is malformed. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007580 for (int i = 0; i < bb->b_iused; i++) {
7581 switch(bb->b_instr[i].i_opcode) {
7582 case RETURN_VALUE:
7583 case RAISE_VARARGS:
7584 case RERAISE:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007585 bb->b_exit = 1;
Mark Shannon5977a792020-12-02 13:31:40 +00007586 bb->b_nofallthrough = 1;
7587 break;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007588 case JUMP_ABSOLUTE:
7589 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007590 bb->b_nofallthrough = 1;
Mark Shannon266b4622020-11-17 19:30:14 +00007591 /* fall through */
7592 case POP_JUMP_IF_FALSE:
7593 case POP_JUMP_IF_TRUE:
7594 case JUMP_IF_FALSE_OR_POP:
7595 case JUMP_IF_TRUE_OR_POP:
Mark Shannon5977a792020-12-02 13:31:40 +00007596 case FOR_ITER:
Mark Shannon266b4622020-11-17 19:30:14 +00007597 if (i != bb->b_iused-1) {
7598 PyErr_SetString(PyExc_SystemError, "malformed control flow graph.");
7599 return -1;
7600 }
Mark Shannon5977a792020-12-02 13:31:40 +00007601 /* Skip over empty basic blocks. */
7602 while (bb->b_instr[i].i_target->b_iused == 0) {
7603 bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next;
7604 }
7605
Mark Shannoncc75ab72020-11-12 19:49:33 +00007606 }
7607 }
Mark Shannon266b4622020-11-17 19:30:14 +00007608 return 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007609}
7610
Mark Shannon6e8128f2020-07-30 10:03:00 +01007611static int
7612mark_reachable(struct assembler *a) {
7613 basicblock **stack, **sp;
7614 sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks);
7615 if (stack == NULL) {
7616 return -1;
7617 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007618 a->a_entry->b_predecessors = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007619 *sp++ = a->a_entry;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007620 while (sp > stack) {
7621 basicblock *b = *(--sp);
Mark Shannon3bd60352021-01-13 12:05:43 +00007622 if (b->b_next && !b->b_nofallthrough) {
7623 if (b->b_next->b_predecessors == 0) {
7624 *sp++ = b->b_next;
7625 }
7626 b->b_next->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007627 }
7628 for (int i = 0; i < b->b_iused; i++) {
7629 basicblock *target;
Mark Shannon582aaf12020-08-04 17:30:11 +01007630 if (is_jump(&b->b_instr[i])) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007631 target = b->b_instr[i].i_target;
Mark Shannon3bd60352021-01-13 12:05:43 +00007632 if (target->b_predecessors == 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007633 *sp++ = target;
7634 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007635 target->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007636 }
7637 }
7638 }
7639 PyObject_Free(stack);
7640 return 0;
7641}
7642
Mark Shannon3bd60352021-01-13 12:05:43 +00007643static void
7644eliminate_empty_basic_blocks(basicblock *entry) {
7645 /* Eliminate empty blocks */
7646 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7647 basicblock *next = b->b_next;
7648 if (next) {
7649 while (next->b_iused == 0 && next->b_next) {
7650 next = next->b_next;
7651 }
7652 b->b_next = next;
7653 }
7654 }
7655 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7656 if (b->b_iused == 0) {
7657 continue;
7658 }
7659 if (is_jump(&b->b_instr[b->b_iused-1])) {
7660 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7661 while (target->b_iused == 0) {
7662 target = target->b_next;
7663 }
7664 b->b_instr[b->b_iused-1].i_target = target;
7665 }
7666 }
7667}
7668
7669
Mark Shannon5977a792020-12-02 13:31:40 +00007670/* If an instruction has no line number, but it's predecessor in the BB does,
Mark Shannon3bd60352021-01-13 12:05:43 +00007671 * then copy the line number. If a successor block has no line number, and only
7672 * one predecessor, then inherit the line number.
7673 * This ensures that all exit blocks (with one predecessor) receive a line number.
7674 * Also reduces the size of the line number table,
Mark Shannon5977a792020-12-02 13:31:40 +00007675 * but has no impact on the generated line number events.
7676 */
7677static void
Mark Shannon762ef852021-08-09 10:54:48 +01007678propagate_line_numbers(struct assembler *a) {
Mark Shannon5977a792020-12-02 13:31:40 +00007679 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007680 if (b->b_iused == 0) {
7681 continue;
7682 }
Mark Shannon5977a792020-12-02 13:31:40 +00007683 int prev_lineno = -1;
7684 for (int i = 0; i < b->b_iused; i++) {
7685 if (b->b_instr[i].i_lineno < 0) {
7686 b->b_instr[i].i_lineno = prev_lineno;
7687 }
7688 else {
7689 prev_lineno = b->b_instr[i].i_lineno;
7690 }
7691 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007692 if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) {
7693 assert(b->b_next->b_iused);
7694 if (b->b_next->b_instr[0].i_lineno < 0) {
7695 b->b_next->b_instr[0].i_lineno = prev_lineno;
7696 }
7697 }
7698 if (is_jump(&b->b_instr[b->b_iused-1])) {
7699 switch (b->b_instr[b->b_iused-1].i_opcode) {
7700 /* Note: Only actual jumps, not exception handlers */
7701 case SETUP_ASYNC_WITH:
7702 case SETUP_WITH:
7703 case SETUP_FINALLY:
7704 continue;
7705 }
7706 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7707 if (target->b_predecessors == 1) {
7708 if (target->b_instr[0].i_lineno < 0) {
7709 target->b_instr[0].i_lineno = prev_lineno;
7710 }
7711 }
7712 }
Mark Shannon5977a792020-12-02 13:31:40 +00007713 }
7714}
7715
7716/* Perform optimizations on a control flow graph.
Mark Shannon6e8128f2020-07-30 10:03:00 +01007717 The consts object should still be in list form to allow new constants
7718 to be appended.
7719
7720 All transformations keep the code size the same or smaller.
7721 For those that reduce size, the gaps are initially filled with
7722 NOPs. Later those NOPs are removed.
7723*/
7724
7725static int
Inada Naoki8a232c72021-04-16 14:01:04 +09007726optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts)
Mark Shannon6e8128f2020-07-30 10:03:00 +01007727{
Mark Shannoncc75ab72020-11-12 19:49:33 +00007728 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Inada Naoki8a232c72021-04-16 14:01:04 +09007729 if (optimize_basic_block(c, b, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007730 return -1;
7731 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007732 clean_basic_block(b, -1);
Mark Shannon3bd60352021-01-13 12:05:43 +00007733 assert(b->b_predecessors == 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007734 }
Mark Shannon762ef852021-08-09 10:54:48 +01007735 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7736 if (extend_block(b)) {
7737 return -1;
7738 }
7739 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007740 if (mark_reachable(a)) {
7741 return -1;
7742 }
7743 /* Delete unreachable instructions */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007744 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007745 if (b->b_predecessors == 0) {
Mark Shannoncc75ab72020-11-12 19:49:33 +00007746 b->b_iused = 0;
Om Gc71581c2020-12-16 17:48:05 +05307747 b->b_nofallthrough = 0;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007748 }
7749 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007750 basicblock *pred = NULL;
7751 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7752 int prev_lineno = -1;
7753 if (pred && pred->b_iused) {
7754 prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno;
7755 }
7756 clean_basic_block(b, prev_lineno);
7757 pred = b->b_nofallthrough ? NULL : b;
7758 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007759 eliminate_empty_basic_blocks(a->a_entry);
Om Gc71581c2020-12-16 17:48:05 +05307760 /* Delete jump instructions made redundant by previous step. If a non-empty
7761 block ends with a jump instruction, check if the next non-empty block
7762 reached through normal flow control is the target of that jump. If it
7763 is, then the jump instruction is redundant and can be deleted.
7764 */
Mark Shannon3bd60352021-01-13 12:05:43 +00007765 int maybe_empty_blocks = 0;
Om Gc71581c2020-12-16 17:48:05 +05307766 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7767 if (b->b_iused > 0) {
7768 struct instr *b_last_instr = &b->b_instr[b->b_iused - 1];
Mark Shannon802b6452021-02-02 14:59:15 +00007769 if (b_last_instr->i_opcode == JUMP_ABSOLUTE ||
Om Gc71581c2020-12-16 17:48:05 +05307770 b_last_instr->i_opcode == JUMP_FORWARD) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007771 if (b_last_instr->i_target == b->b_next) {
7772 assert(b->b_next->b_iused);
Om Gc71581c2020-12-16 17:48:05 +05307773 b->b_nofallthrough = 0;
Mark Shannon802b6452021-02-02 14:59:15 +00007774 b_last_instr->i_opcode = NOP;
7775 clean_basic_block(b, -1);
7776 maybe_empty_blocks = 1;
Om Gc71581c2020-12-16 17:48:05 +05307777 }
7778 }
7779 }
7780 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007781 if (maybe_empty_blocks) {
7782 eliminate_empty_basic_blocks(a->a_entry);
7783 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007784 return 0;
7785}
7786
Ɓukasz Langad41abe82021-09-08 18:25:09 +02007787// Remove trailing unused constants.
7788static int
7789trim_unused_consts(struct compiler *c, struct assembler *a, PyObject *consts)
7790{
7791 assert(PyList_CheckExact(consts));
7792
7793 // The first constant may be docstring; keep it always.
7794 int max_const_index = 0;
7795 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7796 for (int i = 0; i < b->b_iused; i++) {
7797 if (b->b_instr[i].i_opcode == LOAD_CONST &&
7798 b->b_instr[i].i_oparg > max_const_index) {
7799 max_const_index = b->b_instr[i].i_oparg;
7800 }
7801 }
7802 }
7803 if (max_const_index+1 < PyList_GET_SIZE(consts)) {
7804 //fprintf(stderr, "removing trailing consts: max=%d, size=%d\n",
7805 // max_const_index, (int)PyList_GET_SIZE(consts));
7806 if (PyList_SetSlice(consts, max_const_index+1,
7807 PyList_GET_SIZE(consts), NULL) < 0) {
7808 return 1;
7809 }
7810 }
7811 return 0;
7812}
7813
Mark Shannon5977a792020-12-02 13:31:40 +00007814static inline int
7815is_exit_without_lineno(basicblock *b) {
7816 return b->b_exit && b->b_instr[0].i_lineno < 0;
7817}
7818
7819/* PEP 626 mandates that the f_lineno of a frame is correct
7820 * after a frame terminates. It would be prohibitively expensive
7821 * to continuously update the f_lineno field at runtime,
7822 * so we make sure that all exiting instruction (raises and returns)
7823 * have a valid line number, allowing us to compute f_lineno lazily.
7824 * We can do this by duplicating the exit blocks without line number
7825 * so that none have more than one predecessor. We can then safely
7826 * copy the line number from the sole predecessor block.
7827 */
7828static int
Mark Shannon762ef852021-08-09 10:54:48 +01007829duplicate_exits_without_lineno(struct compiler *c)
Mark Shannon5977a792020-12-02 13:31:40 +00007830{
7831 /* Copy all exit blocks without line number that are targets of a jump.
7832 */
7833 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7834 if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) {
7835 switch (b->b_instr[b->b_iused-1].i_opcode) {
7836 /* Note: Only actual jumps, not exception handlers */
7837 case SETUP_ASYNC_WITH:
7838 case SETUP_WITH:
7839 case SETUP_FINALLY:
7840 continue;
7841 }
7842 basicblock *target = b->b_instr[b->b_iused-1].i_target;
Mark Shannon762ef852021-08-09 10:54:48 +01007843 if (is_exit_without_lineno(target) && target->b_predecessors > 1) {
Mark Shannon5977a792020-12-02 13:31:40 +00007844 basicblock *new_target = compiler_copy_block(c, target);
7845 if (new_target == NULL) {
7846 return -1;
7847 }
7848 new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7849 b->b_instr[b->b_iused-1].i_target = new_target;
Mark Shannon762ef852021-08-09 10:54:48 +01007850 target->b_predecessors--;
7851 new_target->b_predecessors = 1;
7852 new_target->b_next = target->b_next;
7853 target->b_next = new_target;
Mark Shannon5977a792020-12-02 13:31:40 +00007854 }
7855 }
Mark Shannoneaccc122020-12-04 15:22:12 +00007856 }
Mark Shannonee9f98d2021-01-05 12:04:10 +00007857 /* Eliminate empty blocks */
7858 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7859 while (b->b_next && b->b_next->b_iused == 0) {
7860 b->b_next = b->b_next->b_next;
7861 }
7862 }
Mark Shannon5977a792020-12-02 13:31:40 +00007863 /* Any remaining reachable exit blocks without line number can only be reached by
7864 * fall through, and thus can only have a single predecessor */
7865 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7866 if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) {
7867 if (is_exit_without_lineno(b->b_next)) {
7868 assert(b->b_next->b_iused > 0);
7869 b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7870 }
7871 }
7872 }
7873 return 0;
7874}
7875
7876
Mark Shannon6e8128f2020-07-30 10:03:00 +01007877/* Retained for API compatibility.
7878 * Optimization is now done in optimize_cfg */
7879
7880PyObject *
7881PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
7882 PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
7883{
7884 Py_INCREF(code);
7885 return code;
7886}