blob: 3cf612219675938a6695142bfc6dfc0d6a15b59f [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file compiles an abstract syntax tree (AST) into Python bytecode.
3 *
Victor Stinnera81fca62021-03-24 00:51:50 +01004 * The primary entry point is _PyAST_Compile(), which returns a
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005 * PyCodeObject. The compiler makes several passes to build the code
6 * object:
7 * 1. Checks for future statements. See future.c
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00008 * 2. Builds a symbol table. See symtable.c.
Thomas Wouters89f507f2006-12-13 04:49:30 +00009 * 3. Generate code for basic blocks. See compiler_mod() in this file.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000010 * 4. Assemble the basic blocks into final code. See assemble() in
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000011 * this file.
Mark Shannon6e8128f2020-07-30 10:03:00 +010012 * 5. Optimize the byte code (peephole optimizations).
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013 *
14 * Note that compiler_mod() suggests module, but the module ast type
15 * (mod_ty) has cases for expressions and interactive statements.
Nick Coghlan944d3eb2005-11-16 12:46:55 +000016 *
Jeremy Hyltone9357b22006-03-01 15:47:05 +000017 * CAUTION: The VISIT_* macros abort the current function when they
18 * encounter a problem. So don't invoke them when there is memory
19 * which needs to be released. Code blocks are OK, as the compiler
Thomas Wouters89f507f2006-12-13 04:49:30 +000020 * structure takes care of releasing those. Use the arena to manage
21 * objects.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022 */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000023
Guido van Rossum79f25d91997-04-29 20:08:16 +000024#include "Python.h"
Victor Stinner526fdeb2021-03-17 23:50:50 +010025#include "pycore_ast.h" // _PyAST_GetDocString()
Victor Stinnera81fca62021-03-24 00:51:50 +010026#include "pycore_compile.h" // _PyFuture_FromAST()
Victor Stinnerba7a99d2021-01-30 01:46:44 +010027#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
Victor Stinnerc9bc2902020-10-27 02:24:34 +010028#include "pycore_long.h" // _PyLong_GetZero()
Victor Stinner28ad12f2021-03-19 12:41:49 +010029#include "pycore_symtable.h" // PySTEntryObject
Guido van Rossum3f5da241990-12-20 15:06:42 +000030
Mark Shannon582aaf12020-08-04 17:30:11 +010031#define NEED_OPCODE_JUMP_TABLES
Victor Stinner526fdeb2021-03-17 23:50:50 +010032#include "opcode.h" // EXTENDED_ARG
33#include "wordcode_helpers.h" // instrsize()
34
Guido van Rossumb05a5c71997-05-07 17:46:13 +000035
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000036#define DEFAULT_BLOCK_SIZE 16
37#define DEFAULT_BLOCKS 8
38#define DEFAULT_CODE_SIZE 128
39#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000040
Nick Coghlan650f0d02007-04-15 12:05:43 +000041#define COMP_GENEXP 0
42#define COMP_LISTCOMP 1
43#define COMP_SETCOMP 2
Guido van Rossum992d4a32007-07-11 13:09:30 +000044#define COMP_DICTCOMP 3
Nick Coghlan650f0d02007-04-15 12:05:43 +000045
Mark Shannon11e0b292021-04-15 14:28:56 +010046/* A soft limit for stack use, to avoid excessive
47 * memory use for large constants, etc.
48 *
49 * The value 30 is plucked out of thin air.
50 * Code that could use more stack than this is
51 * rare, so the exact value is unimportant.
52 */
53#define STACK_USE_GUIDELINE 30
54
55/* If we exceed this limit, it should
56 * be considered a compiler bug.
57 * Currently it should be impossible
58 * to exceed STACK_USE_GUIDELINE * 100,
59 * as 100 is the maximum parse depth.
60 * For performance reasons we will
61 * want to reduce this to a
62 * few hundred in the future.
63 *
64 * NOTE: Whatever MAX_ALLOWED_STACK_USE is
65 * set to, it should never restrict what Python
66 * we can write, just how we compile it.
67 */
68#define MAX_ALLOWED_STACK_USE (STACK_USE_GUIDELINE * 100)
69
Pablo Galindo90235812020-03-15 04:29:22 +000070#define IS_TOP_LEVEL_AWAIT(c) ( \
71 (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \
72 && (c->u->u_ste->ste_type == ModuleBlock))
73
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000074struct instr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 unsigned char i_opcode;
76 int i_oparg;
77 struct basicblock_ *i_target; /* target block (if jump instruction) */
78 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000079};
80
Mark Shannon582aaf12020-08-04 17:30:11 +010081#define LOG_BITS_PER_INT 5
82#define MASK_LOW_LOG_BITS 31
83
84static inline int
85is_bit_set_in_table(uint32_t *table, int bitindex) {
86 /* Is the relevant bit set in the relevant word? */
87 /* 256 bits fit into 8 32-bits words.
88 * Word is indexed by (bitindex>>ln(size of int in bits)).
89 * Bit within word is the low bits of bitindex.
90 */
91 uint32_t word = table[bitindex >> LOG_BITS_PER_INT];
92 return (word >> (bitindex & MASK_LOW_LOG_BITS)) & 1;
93}
94
95static inline int
96is_relative_jump(struct instr *i)
97{
98 return is_bit_set_in_table(_PyOpcode_RelativeJump, i->i_opcode);
99}
100
101static inline int
102is_jump(struct instr *i)
103{
104 return is_bit_set_in_table(_PyOpcode_Jump, i->i_opcode);
105}
106
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000107typedef struct basicblock_ {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000108 /* Each basicblock in a compilation unit is linked via b_list in the
109 reverse order that the block are allocated. b_list points to the next
110 block, not to be confused with b_next, which is next by control flow. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 struct basicblock_ *b_list;
112 /* number of instructions used */
113 int b_iused;
114 /* length of instruction array (b_instr) */
115 int b_ialloc;
116 /* pointer to an array of instructions, initially NULL */
117 struct instr *b_instr;
118 /* If b_next is non-NULL, it is a pointer to the next
119 block reached by normal control flow. */
120 struct basicblock_ *b_next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 /* b_return is true if a RETURN_VALUE opcode is inserted. */
122 unsigned b_return : 1;
Mark Shannon3bd60352021-01-13 12:05:43 +0000123 /* Number of predecssors that a block has. */
124 int b_predecessors;
Mark Shannoncc75ab72020-11-12 19:49:33 +0000125 /* Basic block has no fall through (it ends with a return, raise or jump) */
126 unsigned b_nofallthrough : 1;
127 /* Basic block exits scope (it ends with a return or raise) */
128 unsigned b_exit : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 /* depth of stack upon entry of block, computed by stackdepth() */
130 int b_startdepth;
131 /* instruction offset for block, computed by assemble_jump_offsets() */
132 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000133} basicblock;
134
135/* fblockinfo tracks the current frame block.
136
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000137A frame block is used to handle loops, try/except, and try/finally.
138It's called a frame block to distinguish it from a basic block in the
139compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000140*/
141
Mark Shannon02d126a2020-09-25 14:04:19 +0100142enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END,
tomKPZ7a7ba3d2021-04-07 07:43:45 -0700143 WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER,
144 ASYNC_COMPREHENSION_GENERATOR };
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000145
146struct fblockinfo {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 enum fblocktype fb_type;
148 basicblock *fb_block;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200149 /* (optional) type-specific exit or cleanup block */
150 basicblock *fb_exit;
Mark Shannonfee55262019-11-21 09:11:43 +0000151 /* (optional) additional information required for unwinding */
152 void *fb_datum;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000153};
154
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100155enum {
156 COMPILER_SCOPE_MODULE,
157 COMPILER_SCOPE_CLASS,
158 COMPILER_SCOPE_FUNCTION,
Yury Selivanov75445082015-05-11 22:57:16 -0400159 COMPILER_SCOPE_ASYNC_FUNCTION,
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400160 COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100161 COMPILER_SCOPE_COMPREHENSION,
162};
163
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000164/* The following items change on entry and exit of code blocks.
165 They must be saved and restored when returning to a block.
166*/
167struct compiler_unit {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 PySTEntryObject *u_ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 PyObject *u_name;
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400171 PyObject *u_qualname; /* dot-separated qualified name (lazy) */
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100172 int u_scope_type;
173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 /* The following fields are dicts that map objects to
175 the index of them in co_XXX. The index is used as
176 the argument for opcodes that refer to those collections.
177 */
178 PyObject *u_consts; /* all constants */
179 PyObject *u_names; /* all names */
180 PyObject *u_varnames; /* local variables */
181 PyObject *u_cellvars; /* cell variables */
182 PyObject *u_freevars; /* free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 PyObject *u_private; /* for private name mangling */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185
Victor Stinnerf8e32212013-11-19 23:56:34 +0100186 Py_ssize_t u_argcount; /* number of arguments for block */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100187 Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */
Victor Stinnerf8e32212013-11-19 23:56:34 +0100188 Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 /* Pointer to the most recently allocated block. By following b_list
190 members, you can reach all early allocated blocks. */
191 basicblock *u_blocks;
192 basicblock *u_curblock; /* pointer to current block */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 int u_nfblocks;
195 struct fblockinfo u_fblock[CO_MAXBLOCKS];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 int u_firstlineno; /* the first lineno of the block */
198 int u_lineno; /* the lineno for the current stmt */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000199 int u_col_offset; /* the offset of the current stmt */
Pablo Galindoa77aac42021-04-23 14:27:05 +0100200 int u_end_lineno; /* the end line of the current stmt */
201 int u_end_col_offset; /* the end offset of the current stmt */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000202};
203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204/* This struct captures the global state of a compilation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000206The u pointer points to the current compilation unit, while units
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207for enclosing blocks are stored in c_stack. The u and c_stack are
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000208managed by compiler_enter_scope() and compiler_exit_scope().
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000209
210Note that we don't track recursion levels during compilation - the
211task of detecting and rejecting excessive levels of nesting is
212handled by the symbol analysis pass.
213
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000214*/
215
216struct compiler {
Victor Stinner14e461d2013-08-26 22:28:21 +0200217 PyObject *c_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 struct symtable *c_st;
219 PyFutureFeatures *c_future; /* pointer to module's __future__ */
220 PyCompilerFlags *c_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000221
Georg Brandl8334fd92010-12-04 10:26:46 +0000222 int c_optimize; /* optimization level */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 int c_interactive; /* true if in interactive mode */
224 int c_nestlevel;
INADA Naokic2e16072018-11-26 21:23:22 +0900225 PyObject *c_const_cache; /* Python dict holding all constants,
226 including names tuple */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 struct compiler_unit *u; /* compiler state for current block */
228 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
229 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000230};
231
Brandt Bucher145bf262021-02-26 14:51:55 -0800232typedef struct {
233 PyObject *stores;
234 int allow_irrefutable;
235} pattern_context;
236
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100237static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000238static void compiler_free(struct compiler *);
239static basicblock *compiler_new_block(struct compiler *);
Andy Lester76d58772020-03-10 21:18:12 -0500240static int compiler_next_instr(basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000241static int compiler_addop(struct compiler *, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +0100242static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
Mark Shannon582aaf12020-08-04 17:30:11 +0100243static int compiler_addop_j(struct compiler *, int, basicblock *);
Mark Shannon127dde52021-01-04 18:06:55 +0000244static int compiler_addop_j_noline(struct compiler *, int, basicblock *);
Brandt Bucher145bf262021-02-26 14:51:55 -0800245static int compiler_error(struct compiler *, const char *, ...);
Serhiy Storchaka62e44812019-02-16 08:12:19 +0200246static int compiler_warn(struct compiler *, const char *, ...);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000247static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
248
249static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
250static int compiler_visit_stmt(struct compiler *, stmt_ty);
251static int compiler_visit_keyword(struct compiler *, keyword_ty);
252static int compiler_visit_expr(struct compiler *, expr_ty);
253static int compiler_augassign(struct compiler *, stmt_ty);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700254static int compiler_annassign(struct compiler *, stmt_ty);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +0200255static int compiler_subscript(struct compiler *, expr_ty);
256static int compiler_slice(struct compiler *, expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000257
Andy Lester76d58772020-03-10 21:18:12 -0500258static int inplace_binop(operator_ty);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100259static int are_all_items_const(asdl_expr_seq *, Py_ssize_t, Py_ssize_t);
Mark Shannon8473cf82020-12-15 11:07:50 +0000260
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000261
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500262static int compiler_with(struct compiler *, stmt_ty, int);
Yury Selivanov75445082015-05-11 22:57:16 -0400263static int compiler_async_with(struct compiler *, stmt_ty, int);
264static int compiler_async_for(struct compiler *, stmt_ty);
Victor Stinner976bb402016-03-23 11:36:19 +0100265static int compiler_call_helper(struct compiler *c, int n,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100266 asdl_expr_seq *args,
267 asdl_keyword_seq *keywords);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500268static int compiler_try_except(struct compiler *, stmt_ty);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400269static int compiler_set_qualname(struct compiler *);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000270
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700271static int compiler_sync_comprehension_generator(
272 struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100273 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +0200274 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700275 expr_ty elt, expr_ty val, int type);
276
277static int compiler_async_comprehension_generator(
278 struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100279 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +0200280 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700281 expr_ty elt, expr_ty val, int type);
282
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000283static int compiler_pattern(struct compiler *, pattern_ty, pattern_context *);
Brandt Bucher145bf262021-02-26 14:51:55 -0800284static int compiler_match(struct compiler *, stmt_ty);
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000285static int compiler_pattern_subpattern(struct compiler *, pattern_ty,
Brandt Bucher145bf262021-02-26 14:51:55 -0800286 pattern_context *);
287
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000288static PyCodeObject *assemble(struct compiler *, int addNone);
Mark Shannon332cd5e2018-01-30 00:41:04 +0000289static PyObject *__doc__, *__annotations__;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000290
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400291#define CAPSULE_NAME "compile.c compiler unit"
Benjamin Petersonb173f782009-05-05 22:31:58 +0000292
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000293PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000294_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 /* Name mangling: __private becomes _classname__private.
297 This is independent from how the name is used. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200298 PyObject *result;
299 size_t nlen, plen, ipriv;
300 Py_UCS4 maxchar;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200302 PyUnicode_READ_CHAR(ident, 0) != '_' ||
303 PyUnicode_READ_CHAR(ident, 1) != '_') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 Py_INCREF(ident);
305 return ident;
306 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200307 nlen = PyUnicode_GET_LENGTH(ident);
308 plen = PyUnicode_GET_LENGTH(privateobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 /* Don't mangle __id__ or names with dots.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 The only time a name with a dot can occur is when
312 we are compiling an import statement that has a
313 package name.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 TODO(jhylton): Decide whether we want to support
316 mangling of the module name, e.g. __M.X.
317 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200318 if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
319 PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
320 PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 Py_INCREF(ident);
322 return ident; /* Don't mangle __whatever__ */
323 }
324 /* Strip leading underscores from class name */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200325 ipriv = 0;
326 while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
327 ipriv++;
328 if (ipriv == plen) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 Py_INCREF(ident);
330 return ident; /* Don't mangle if class is just underscores */
331 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200332 plen -= ipriv;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000333
Antoine Pitrou55bff892013-04-06 21:21:04 +0200334 if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
335 PyErr_SetString(PyExc_OverflowError,
336 "private identifier too large to be mangled");
337 return NULL;
338 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000339
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200340 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
341 if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
342 maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
343
344 result = PyUnicode_New(1 + nlen + plen, maxchar);
345 if (!result)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200347 /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
348 PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
Victor Stinner6c7a52a2011-09-28 21:39:17 +0200349 if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
350 Py_DECREF(result);
351 return NULL;
352 }
353 if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
354 Py_DECREF(result);
355 return NULL;
356 }
Victor Stinner8f825062012-04-27 13:55:39 +0200357 assert(_PyUnicode_CheckConsistency(result, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200358 return result;
Michael W. Hudson60934622004-08-12 17:56:29 +0000359}
360
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000361static int
362compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000365
INADA Naokic2e16072018-11-26 21:23:22 +0900366 c->c_const_cache = PyDict_New();
367 if (!c->c_const_cache) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 return 0;
INADA Naokic2e16072018-11-26 21:23:22 +0900369 }
370
371 c->c_stack = PyList_New(0);
372 if (!c->c_stack) {
373 Py_CLEAR(c->c_const_cache);
374 return 0;
375 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000378}
379
380PyCodeObject *
Victor Stinnera81fca62021-03-24 00:51:50 +0100381_PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
382 int optimize, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000383{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 struct compiler c;
385 PyCodeObject *co = NULL;
Victor Stinner37d66d72019-06-13 02:16:41 +0200386 PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 if (!__doc__) {
390 __doc__ = PyUnicode_InternFromString("__doc__");
391 if (!__doc__)
392 return NULL;
393 }
Mark Shannon332cd5e2018-01-30 00:41:04 +0000394 if (!__annotations__) {
395 __annotations__ = PyUnicode_InternFromString("__annotations__");
396 if (!__annotations__)
397 return NULL;
398 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 if (!compiler_init(&c))
400 return NULL;
Victor Stinner14e461d2013-08-26 22:28:21 +0200401 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 c.c_filename = filename;
403 c.c_arena = arena;
Victor Stinnera81fca62021-03-24 00:51:50 +0100404 c.c_future = _PyFuture_FromAST(mod, filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 if (c.c_future == NULL)
406 goto finally;
407 if (!flags) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 flags = &local_flags;
409 }
410 merged = c.c_future->ff_features | flags->cf_flags;
411 c.c_future->ff_features = merged;
412 flags->cf_flags = merged;
413 c.c_flags = flags;
Victor Stinnerda7933e2020-04-13 03:04:28 +0200414 c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000416
Pablo Galindod112c602020-03-18 23:02:09 +0000417 _PyASTOptimizeState state;
418 state.optimize = c.c_optimize;
419 state.ff_features = merged;
420
421 if (!_PyAST_Optimize(mod, arena, &state)) {
INADA Naoki7ea143a2017-12-14 16:47:20 +0900422 goto finally;
423 }
424
Victor Stinner28ad12f2021-03-19 12:41:49 +0100425 c.c_st = _PySymtable_Build(mod, filename, c.c_future);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 if (c.c_st == NULL) {
427 if (!PyErr_Occurred())
428 PyErr_SetString(PyExc_SystemError, "no symtable");
429 goto finally;
430 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000433
Thomas Wouters1175c432006-02-27 22:49:54 +0000434 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 compiler_free(&c);
436 assert(co || PyErr_Occurred());
437 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000438}
439
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000440static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000441compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 if (c->c_st)
Victor Stinner28ad12f2021-03-19 12:41:49 +0100444 _PySymtable_Free(c->c_st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 if (c->c_future)
446 PyObject_Free(c->c_future);
Victor Stinner14e461d2013-08-26 22:28:21 +0200447 Py_XDECREF(c->c_filename);
INADA Naokic2e16072018-11-26 21:23:22 +0900448 Py_DECREF(c->c_const_cache);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000450}
451
Guido van Rossum79f25d91997-04-29 20:08:16 +0000452static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000453list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 Py_ssize_t i, n;
456 PyObject *v, *k;
457 PyObject *dict = PyDict_New();
458 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 n = PyList_Size(list);
461 for (i = 0; i < n; i++) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100462 v = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 if (!v) {
464 Py_DECREF(dict);
465 return NULL;
466 }
467 k = PyList_GET_ITEM(list, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300468 if (PyDict_SetItem(dict, k, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 Py_DECREF(v);
470 Py_DECREF(dict);
471 return NULL;
472 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 Py_DECREF(v);
474 }
475 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000476}
477
478/* Return new dict containing names from src that match scope(s).
479
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000480src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000482values are integers, starting at offset and increasing by one for
483each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000484*/
485
486static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +0100487dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000488{
Benjamin Peterson51ab2832012-07-18 15:12:47 -0700489 Py_ssize_t i = offset, scope, num_keys, key_i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 PyObject *k, *v, *dest = PyDict_New();
Meador Inge2ca63152012-07-18 14:20:11 -0500491 PyObject *sorted_keys;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 assert(offset >= 0);
494 if (dest == NULL)
495 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000496
Meador Inge2ca63152012-07-18 14:20:11 -0500497 /* Sort the keys so that we have a deterministic order on the indexes
498 saved in the returned dictionary. These indexes are used as indexes
499 into the free and cell var storage. Therefore if they aren't
500 deterministic, then the generated bytecode is not deterministic.
501 */
502 sorted_keys = PyDict_Keys(src);
503 if (sorted_keys == NULL)
504 return NULL;
505 if (PyList_Sort(sorted_keys) != 0) {
506 Py_DECREF(sorted_keys);
507 return NULL;
508 }
Meador Ingef69e24e2012-07-18 16:41:03 -0500509 num_keys = PyList_GET_SIZE(sorted_keys);
Meador Inge2ca63152012-07-18 14:20:11 -0500510
511 for (key_i = 0; key_i < num_keys; key_i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 /* XXX this should probably be a macro in symtable.h */
513 long vi;
Meador Inge2ca63152012-07-18 14:20:11 -0500514 k = PyList_GET_ITEM(sorted_keys, key_i);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200515 v = PyDict_GetItemWithError(src, k);
516 assert(v && PyLong_Check(v));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 vi = PyLong_AS_LONG(v);
518 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 if (scope == scope_type || vi & flag) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300521 PyObject *item = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 if (item == NULL) {
Meador Inge2ca63152012-07-18 14:20:11 -0500523 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 Py_DECREF(dest);
525 return NULL;
526 }
527 i++;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300528 if (PyDict_SetItem(dest, k, item) < 0) {
Meador Inge2ca63152012-07-18 14:20:11 -0500529 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 Py_DECREF(item);
531 Py_DECREF(dest);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 return NULL;
533 }
534 Py_DECREF(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 }
536 }
Meador Inge2ca63152012-07-18 14:20:11 -0500537 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000539}
540
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541static void
542compiler_unit_check(struct compiler_unit *u)
543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 basicblock *block;
545 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +0100546 assert(!_PyMem_IsPtrFreed(block));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 if (block->b_instr != NULL) {
548 assert(block->b_ialloc > 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +0100549 assert(block->b_iused >= 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 assert(block->b_ialloc >= block->b_iused);
551 }
552 else {
553 assert (block->b_iused == 0);
554 assert (block->b_ialloc == 0);
555 }
556 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000557}
558
559static void
560compiler_unit_free(struct compiler_unit *u)
561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 compiler_unit_check(u);
565 b = u->u_blocks;
566 while (b != NULL) {
567 if (b->b_instr)
568 PyObject_Free((void *)b->b_instr);
569 next = b->b_list;
570 PyObject_Free((void *)b);
571 b = next;
572 }
573 Py_CLEAR(u->u_ste);
574 Py_CLEAR(u->u_name);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400575 Py_CLEAR(u->u_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 Py_CLEAR(u->u_consts);
577 Py_CLEAR(u->u_names);
578 Py_CLEAR(u->u_varnames);
579 Py_CLEAR(u->u_freevars);
580 Py_CLEAR(u->u_cellvars);
581 Py_CLEAR(u->u_private);
582 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583}
584
585static int
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100586compiler_enter_scope(struct compiler *c, identifier name,
587 int scope_type, void *key, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 struct compiler_unit *u;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100590 basicblock *block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000591
Andy Lester7668a8b2020-03-24 23:26:44 -0500592 u = (struct compiler_unit *)PyObject_Calloc(1, sizeof(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 struct compiler_unit));
594 if (!u) {
595 PyErr_NoMemory();
596 return 0;
597 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100598 u->u_scope_type = scope_type;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 u->u_argcount = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100600 u->u_posonlyargcount = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 u->u_kwonlyargcount = 0;
602 u->u_ste = PySymtable_Lookup(c->c_st, key);
603 if (!u->u_ste) {
604 compiler_unit_free(u);
605 return 0;
606 }
607 Py_INCREF(name);
608 u->u_name = name;
609 u->u_varnames = list2dict(u->u_ste->ste_varnames);
610 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
611 if (!u->u_varnames || !u->u_cellvars) {
612 compiler_unit_free(u);
613 return 0;
614 }
Benjamin Peterson312595c2013-05-15 15:26:42 -0500615 if (u->u_ste->ste_needs_class_closure) {
Martin Panter7462b6492015-11-02 03:37:02 +0000616 /* Cook up an implicit __class__ cell. */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500617 _Py_IDENTIFIER(__class__);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300618 PyObject *name;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500619 int res;
620 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200621 assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500622 name = _PyUnicode_FromId(&PyId___class__);
623 if (!name) {
624 compiler_unit_free(u);
625 return 0;
626 }
Victor Stinnerc9bc2902020-10-27 02:24:34 +0100627 res = PyDict_SetItem(u->u_cellvars, name, _PyLong_GetZero());
Benjamin Peterson312595c2013-05-15 15:26:42 -0500628 if (res < 0) {
629 compiler_unit_free(u);
630 return 0;
631 }
632 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200635 PyDict_GET_SIZE(u->u_cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 if (!u->u_freevars) {
637 compiler_unit_free(u);
638 return 0;
639 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 u->u_blocks = NULL;
642 u->u_nfblocks = 0;
643 u->u_firstlineno = lineno;
644 u->u_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000645 u->u_col_offset = 0;
Pablo Galindoa77aac42021-04-23 14:27:05 +0100646 u->u_end_lineno = 0;
647 u->u_end_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 u->u_consts = PyDict_New();
649 if (!u->u_consts) {
650 compiler_unit_free(u);
651 return 0;
652 }
653 u->u_names = PyDict_New();
654 if (!u->u_names) {
655 compiler_unit_free(u);
656 return 0;
657 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 /* Push the old compiler_unit on the stack. */
662 if (c->u) {
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400663 PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
665 Py_XDECREF(capsule);
666 compiler_unit_free(u);
667 return 0;
668 }
669 Py_DECREF(capsule);
670 u->u_private = c->u->u_private;
671 Py_XINCREF(u->u_private);
672 }
673 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 c->c_nestlevel++;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100676
677 block = compiler_new_block(c);
678 if (block == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 return 0;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100680 c->u->u_curblock = block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000681
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400682 if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
683 if (!compiler_set_qualname(c))
684 return 0;
685 }
686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000688}
689
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000690static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000691compiler_exit_scope(struct compiler *c)
692{
Victor Stinnera6192632021-01-29 16:53:03 +0100693 // Don't call PySequence_DelItem() with an exception raised
694 PyObject *exc_type, *exc_val, *exc_tb;
695 PyErr_Fetch(&exc_type, &exc_val, &exc_tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 c->c_nestlevel--;
698 compiler_unit_free(c->u);
699 /* Restore c->u to the parent unit. */
Victor Stinnera6192632021-01-29 16:53:03 +0100700 Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 if (n >= 0) {
Victor Stinnera6192632021-01-29 16:53:03 +0100702 PyObject *capsule = PyList_GET_ITEM(c->c_stack, n);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400703 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 assert(c->u);
705 /* we are deleting from a list so this really shouldn't fail */
Victor Stinnera6192632021-01-29 16:53:03 +0100706 if (PySequence_DelItem(c->c_stack, n) < 0) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +0100707 _PyErr_WriteUnraisableMsg("on removing the last compiler "
708 "stack item", NULL);
Victor Stinnera6192632021-01-29 16:53:03 +0100709 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 compiler_unit_check(c->u);
711 }
Victor Stinnera6192632021-01-29 16:53:03 +0100712 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 c->u = NULL;
Victor Stinnera6192632021-01-29 16:53:03 +0100714 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000715
Victor Stinnera6192632021-01-29 16:53:03 +0100716 PyErr_Restore(exc_type, exc_val, exc_tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000717}
718
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400719static int
720compiler_set_qualname(struct compiler *c)
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100721{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100722 _Py_static_string(dot, ".");
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400723 _Py_static_string(dot_locals, ".<locals>");
724 Py_ssize_t stack_size;
725 struct compiler_unit *u = c->u;
726 PyObject *name, *base, *dot_str, *dot_locals_str;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100727
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400728 base = NULL;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100729 stack_size = PyList_GET_SIZE(c->c_stack);
Benjamin Petersona8a38b82013-10-19 16:14:39 -0400730 assert(stack_size >= 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400731 if (stack_size > 1) {
732 int scope, force_global = 0;
733 struct compiler_unit *parent;
734 PyObject *mangled, *capsule;
735
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400736 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400737 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400738 assert(parent);
739
Yury Selivanov75445082015-05-11 22:57:16 -0400740 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
741 || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
742 || u->u_scope_type == COMPILER_SCOPE_CLASS) {
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400743 assert(u->u_name);
744 mangled = _Py_Mangle(parent->u_private, u->u_name);
745 if (!mangled)
746 return 0;
Victor Stinner28ad12f2021-03-19 12:41:49 +0100747 scope = _PyST_GetScope(parent->u_ste, mangled);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400748 Py_DECREF(mangled);
749 assert(scope != GLOBAL_IMPLICIT);
750 if (scope == GLOBAL_EXPLICIT)
751 force_global = 1;
752 }
753
754 if (!force_global) {
755 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
Yury Selivanov75445082015-05-11 22:57:16 -0400756 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400757 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
758 dot_locals_str = _PyUnicode_FromId(&dot_locals);
759 if (dot_locals_str == NULL)
760 return 0;
761 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
762 if (base == NULL)
763 return 0;
764 }
765 else {
766 Py_INCREF(parent->u_qualname);
767 base = parent->u_qualname;
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400768 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100769 }
770 }
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400771
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400772 if (base != NULL) {
773 dot_str = _PyUnicode_FromId(&dot);
774 if (dot_str == NULL) {
775 Py_DECREF(base);
776 return 0;
777 }
778 name = PyUnicode_Concat(base, dot_str);
779 Py_DECREF(base);
780 if (name == NULL)
781 return 0;
782 PyUnicode_Append(&name, u->u_name);
783 if (name == NULL)
784 return 0;
785 }
786 else {
787 Py_INCREF(u->u_name);
788 name = u->u_name;
789 }
790 u->u_qualname = name;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100791
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400792 return 1;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100793}
794
Eric V. Smith235a6f02015-09-19 14:51:32 -0400795
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796/* Allocate a new block and return a pointer to it.
797 Returns NULL on error.
798*/
799
800static basicblock *
801compiler_new_block(struct compiler *c)
802{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 basicblock *b;
804 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 u = c->u;
Andy Lester7668a8b2020-03-24 23:26:44 -0500807 b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 if (b == NULL) {
809 PyErr_NoMemory();
810 return NULL;
811 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 /* Extend the singly linked list of blocks with new block. */
813 b->b_list = u->u_blocks;
814 u->u_blocks = b;
815 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816}
817
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818static basicblock *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819compiler_next_block(struct compiler *c)
820{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 basicblock *block = compiler_new_block(c);
822 if (block == NULL)
823 return NULL;
824 c->u->u_curblock->b_next = block;
825 c->u->u_curblock = block;
826 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827}
828
829static basicblock *
830compiler_use_next_block(struct compiler *c, basicblock *block)
831{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 assert(block != NULL);
833 c->u->u_curblock->b_next = block;
834 c->u->u_curblock = block;
835 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836}
837
Mark Shannon5977a792020-12-02 13:31:40 +0000838static basicblock *
839compiler_copy_block(struct compiler *c, basicblock *block)
840{
841 /* Cannot copy a block if it has a fallthrough, since
842 * a block can only have one fallthrough predecessor.
843 */
844 assert(block->b_nofallthrough);
845 basicblock *result = compiler_next_block(c);
846 if (result == NULL) {
847 return NULL;
848 }
849 for (int i = 0; i < block->b_iused; i++) {
850 int n = compiler_next_instr(result);
851 if (n < 0) {
852 return NULL;
853 }
854 result->b_instr[n] = block->b_instr[i];
855 }
856 result->b_exit = block->b_exit;
Mark Shannon3bd60352021-01-13 12:05:43 +0000857 result->b_nofallthrough = 1;
Mark Shannon5977a792020-12-02 13:31:40 +0000858 return result;
859}
860
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861/* Returns the offset of the next instruction in the current block's
862 b_instr array. Resizes the b_instr as necessary.
863 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000864*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865
866static int
Andy Lester76d58772020-03-10 21:18:12 -0500867compiler_next_instr(basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000868{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 assert(b != NULL);
870 if (b->b_instr == NULL) {
Andy Lester7668a8b2020-03-24 23:26:44 -0500871 b->b_instr = (struct instr *)PyObject_Calloc(
872 DEFAULT_BLOCK_SIZE, sizeof(struct instr));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 if (b->b_instr == NULL) {
874 PyErr_NoMemory();
875 return -1;
876 }
877 b->b_ialloc = DEFAULT_BLOCK_SIZE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 }
879 else if (b->b_iused == b->b_ialloc) {
880 struct instr *tmp;
881 size_t oldsize, newsize;
882 oldsize = b->b_ialloc * sizeof(struct instr);
883 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000884
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -0700885 if (oldsize > (SIZE_MAX >> 1)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 PyErr_NoMemory();
887 return -1;
888 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 if (newsize == 0) {
891 PyErr_NoMemory();
892 return -1;
893 }
894 b->b_ialloc <<= 1;
895 tmp = (struct instr *)PyObject_Realloc(
896 (void *)b->b_instr, newsize);
897 if (tmp == NULL) {
898 PyErr_NoMemory();
899 return -1;
900 }
901 b->b_instr = tmp;
902 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
903 }
904 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000905}
906
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200907/* Set the line number and column offset for the following instructions.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000908
Christian Heimes2202f872008-02-06 14:31:34 +0000909 The line number is reset in the following cases:
910 - when entering a new scope
911 - on each statement
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200912 - on each expression and sub-expression
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200913 - before the "except" and "finally" clauses
Thomas Wouters89f507f2006-12-13 04:49:30 +0000914*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000915
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200916#define SET_LOC(c, x) \
917 (c)->u->u_lineno = (x)->lineno; \
Pablo Galindoa77aac42021-04-23 14:27:05 +0100918 (c)->u->u_col_offset = (x)->col_offset; \
919 (c)->u->u_end_lineno = (x)->end_lineno; \
920 (c)->u->u_end_col_offset = (x)->end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000921
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200922/* Return the stack effect of opcode with argument oparg.
923
924 Some opcodes have different stack effect when jump to the target and
925 when not jump. The 'jump' parameter specifies the case:
926
927 * 0 -- when not jump
928 * 1 -- when jump
929 * -1 -- maximal
930 */
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200931static int
932stack_effect(int opcode, int oparg, int jump)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000933{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 switch (opcode) {
Serhiy Storchaka57faf342018-04-25 22:04:06 +0300935 case NOP:
936 case EXTENDED_ARG:
937 return 0;
938
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200939 /* Stack manipulation */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 case POP_TOP:
941 return -1;
942 case ROT_TWO:
943 case ROT_THREE:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200944 case ROT_FOUR:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 return 0;
946 case DUP_TOP:
947 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000948 case DUP_TOP_TWO:
949 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000950
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200951 /* Unary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 case UNARY_POSITIVE:
953 case UNARY_NEGATIVE:
954 case UNARY_NOT:
955 case UNARY_INVERT:
956 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 case SET_ADD:
959 case LIST_APPEND:
960 return -1;
961 case MAP_ADD:
962 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000963
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200964 /* Binary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 case BINARY_POWER:
966 case BINARY_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400967 case BINARY_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 case BINARY_MODULO:
969 case BINARY_ADD:
970 case BINARY_SUBTRACT:
971 case BINARY_SUBSCR:
972 case BINARY_FLOOR_DIVIDE:
973 case BINARY_TRUE_DIVIDE:
974 return -1;
975 case INPLACE_FLOOR_DIVIDE:
976 case INPLACE_TRUE_DIVIDE:
977 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 case INPLACE_ADD:
980 case INPLACE_SUBTRACT:
981 case INPLACE_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400982 case INPLACE_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 case INPLACE_MODULO:
984 return -1;
985 case STORE_SUBSCR:
986 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 case DELETE_SUBSCR:
988 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 case BINARY_LSHIFT:
991 case BINARY_RSHIFT:
992 case BINARY_AND:
993 case BINARY_XOR:
994 case BINARY_OR:
995 return -1;
996 case INPLACE_POWER:
997 return -1;
998 case GET_ITER:
999 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 case PRINT_EXPR:
1002 return -1;
1003 case LOAD_BUILD_CLASS:
1004 return 1;
1005 case INPLACE_LSHIFT:
1006 case INPLACE_RSHIFT:
1007 case INPLACE_AND:
1008 case INPLACE_XOR:
1009 case INPLACE_OR:
1010 return -1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 case SETUP_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001013 /* 1 in the normal flow.
1014 * Restore the stack position and push 6 values before jumping to
1015 * the handler if an exception be raised. */
1016 return jump ? 6 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 case RETURN_VALUE:
1018 return -1;
1019 case IMPORT_STAR:
1020 return -1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001021 case SETUP_ANNOTATIONS:
1022 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 case YIELD_VALUE:
1024 return 0;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001025 case YIELD_FROM:
1026 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 case POP_BLOCK:
1028 return 0;
1029 case POP_EXCEPT:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001030 return -3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 case STORE_NAME:
1033 return -1;
1034 case DELETE_NAME:
1035 return 0;
1036 case UNPACK_SEQUENCE:
1037 return oparg-1;
1038 case UNPACK_EX:
1039 return (oparg&0xFF) + (oparg>>8);
1040 case FOR_ITER:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001041 /* -1 at end of iterator, 1 if continue iterating. */
1042 return jump > 0 ? -1 : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 case STORE_ATTR:
1045 return -2;
1046 case DELETE_ATTR:
1047 return -1;
1048 case STORE_GLOBAL:
1049 return -1;
1050 case DELETE_GLOBAL:
1051 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 case LOAD_CONST:
1053 return 1;
1054 case LOAD_NAME:
1055 return 1;
1056 case BUILD_TUPLE:
1057 case BUILD_LIST:
1058 case BUILD_SET:
Serhiy Storchakaea525a22016-09-06 22:07:53 +03001059 case BUILD_STRING:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 return 1-oparg;
1061 case BUILD_MAP:
Benjamin Petersonb6855152015-09-10 21:02:39 -07001062 return 1 - 2*oparg;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001063 case BUILD_CONST_KEY_MAP:
1064 return -oparg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 case LOAD_ATTR:
1066 return 0;
1067 case COMPARE_OP:
Mark Shannon9af0e472020-01-14 10:12:45 +00001068 case IS_OP:
1069 case CONTAINS_OP:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 return -1;
Mark Shannon9af0e472020-01-14 10:12:45 +00001071 case JUMP_IF_NOT_EXC_MATCH:
1072 return -2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 case IMPORT_NAME:
1074 return -1;
1075 case IMPORT_FROM:
1076 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001078 /* Jumps */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 case JUMP_FORWARD:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 case JUMP_ABSOLUTE:
1081 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001082
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001083 case JUMP_IF_TRUE_OR_POP:
1084 case JUMP_IF_FALSE_OR_POP:
1085 return jump ? 0 : -1;
1086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 case POP_JUMP_IF_FALSE:
1088 case POP_JUMP_IF_TRUE:
1089 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 case LOAD_GLOBAL:
1092 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001094 /* Exception handling */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 case SETUP_FINALLY:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001096 /* 0 in the normal flow.
1097 * Restore the stack position and push 6 values before jumping to
1098 * the handler if an exception be raised. */
1099 return jump ? 6 : 0;
Mark Shannonfee55262019-11-21 09:11:43 +00001100 case RERAISE:
1101 return -3;
1102
1103 case WITH_EXCEPT_START:
1104 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 case LOAD_FAST:
1107 return 1;
1108 case STORE_FAST:
1109 return -1;
1110 case DELETE_FAST:
1111 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 case RAISE_VARARGS:
1114 return -oparg;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001115
1116 /* Functions and calls */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 case CALL_FUNCTION:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001118 return -oparg;
Yury Selivanovf2392132016-12-13 19:03:51 -05001119 case CALL_METHOD:
1120 return -oparg-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 case CALL_FUNCTION_KW:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001122 return -oparg-1;
1123 case CALL_FUNCTION_EX:
Matthieu Dartiailh3a9ac822017-02-21 14:25:22 +01001124 return -1 - ((oparg & 0x01) != 0);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001125 case MAKE_FUNCTION:
1126 return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1127 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 case BUILD_SLICE:
1129 if (oparg == 3)
1130 return -2;
1131 else
1132 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001134 /* Closures */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 case LOAD_CLOSURE:
1136 return 1;
1137 case LOAD_DEREF:
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001138 case LOAD_CLASSDEREF:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 return 1;
1140 case STORE_DEREF:
1141 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001142 case DELETE_DEREF:
1143 return 0;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001144
1145 /* Iterators and generators */
Yury Selivanov75445082015-05-11 22:57:16 -04001146 case GET_AWAITABLE:
1147 return 0;
1148 case SETUP_ASYNC_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001149 /* 0 in the normal flow.
1150 * Restore the stack position to the position before the result
1151 * of __aenter__ and push 6 values before jumping to the handler
1152 * if an exception be raised. */
1153 return jump ? -1 + 6 : 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001154 case BEFORE_ASYNC_WITH:
1155 return 1;
1156 case GET_AITER:
1157 return 0;
1158 case GET_ANEXT:
1159 return 1;
Yury Selivanov5376ba92015-06-22 12:19:30 -04001160 case GET_YIELD_FROM_ITER:
1161 return 0;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02001162 case END_ASYNC_FOR:
1163 return -7;
Eric V. Smitha78c7952015-11-03 12:45:05 -05001164 case FORMAT_VALUE:
1165 /* If there's a fmt_spec on the stack, we go from 2->1,
1166 else 1->1. */
1167 return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
Yury Selivanovf2392132016-12-13 19:03:51 -05001168 case LOAD_METHOD:
1169 return 1;
Zackery Spytzce6a0702019-08-25 03:44:09 -06001170 case LOAD_ASSERTION_ERROR:
1171 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001172 case LIST_TO_TUPLE:
1173 return 0;
Mark Shannonb37181e2021-04-06 11:48:59 +01001174 case GEN_START:
1175 return -1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001176 case LIST_EXTEND:
1177 case SET_UPDATE:
Mark Shannon8a4cd702020-01-27 09:57:45 +00001178 case DICT_MERGE:
1179 case DICT_UPDATE:
Mark Shannon13bc1392020-01-23 09:25:17 +00001180 return -1;
Brandt Bucher145bf262021-02-26 14:51:55 -08001181 case COPY_DICT_WITHOUT_KEYS:
1182 return 0;
1183 case MATCH_CLASS:
1184 return -1;
1185 case GET_LEN:
1186 case MATCH_MAPPING:
1187 case MATCH_SEQUENCE:
1188 return 1;
1189 case MATCH_KEYS:
1190 return 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 default:
Larry Hastings3a907972013-11-23 14:49:22 -08001192 return PY_INVALID_STACK_EFFECT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 }
Larry Hastings3a907972013-11-23 14:49:22 -08001194 return PY_INVALID_STACK_EFFECT; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195}
1196
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001197int
Serhiy Storchaka7bdf2822018-09-18 09:54:26 +03001198PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump)
1199{
1200 return stack_effect(opcode, oparg, jump);
1201}
1202
1203int
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001204PyCompile_OpcodeStackEffect(int opcode, int oparg)
1205{
1206 return stack_effect(opcode, oparg, -1);
1207}
1208
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209/* Add an opcode with no argument.
1210 Returns 0 on failure, 1 on success.
1211*/
1212
1213static int
Mark Shannon3bd60352021-01-13 12:05:43 +00001214compiler_addop_line(struct compiler *c, int opcode, int line)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 basicblock *b;
1217 struct instr *i;
1218 int off;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001219 assert(!HAS_ARG(opcode));
Andy Lester76d58772020-03-10 21:18:12 -05001220 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 if (off < 0)
1222 return 0;
1223 b = c->u->u_curblock;
1224 i = &b->b_instr[off];
1225 i->i_opcode = opcode;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001226 i->i_oparg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 if (opcode == RETURN_VALUE)
1228 b->b_return = 1;
Mark Shannon3bd60352021-01-13 12:05:43 +00001229 i->i_lineno = line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001231}
1232
Mark Shannon3bd60352021-01-13 12:05:43 +00001233static int
1234compiler_addop(struct compiler *c, int opcode)
1235{
1236 return compiler_addop_line(c, opcode, c->u->u_lineno);
1237}
1238
1239static int
1240compiler_addop_noline(struct compiler *c, int opcode)
1241{
1242 return compiler_addop_line(c, opcode, -1);
1243}
1244
1245
Victor Stinnerf8e32212013-11-19 23:56:34 +01001246static Py_ssize_t
Andy Lester76d58772020-03-10 21:18:12 -05001247compiler_add_o(PyObject *dict, PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001249 PyObject *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001252 v = PyDict_GetItemWithError(dict, o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 if (!v) {
Stefan Krahc0cbed12015-07-27 12:56:49 +02001254 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 return -1;
Stefan Krahc0cbed12015-07-27 12:56:49 +02001256 }
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001257 arg = PyDict_GET_SIZE(dict);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001258 v = PyLong_FromSsize_t(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 if (!v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 return -1;
1261 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001262 if (PyDict_SetItem(dict, o, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 Py_DECREF(v);
1264 return -1;
1265 }
1266 Py_DECREF(v);
1267 }
1268 else
1269 arg = PyLong_AsLong(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001270 return arg;
1271}
1272
INADA Naokic2e16072018-11-26 21:23:22 +09001273// Merge const *o* recursively and return constant key object.
1274static PyObject*
1275merge_consts_recursive(struct compiler *c, PyObject *o)
1276{
1277 // None and Ellipsis are singleton, and key is the singleton.
1278 // No need to merge object and key.
1279 if (o == Py_None || o == Py_Ellipsis) {
1280 Py_INCREF(o);
1281 return o;
1282 }
1283
1284 PyObject *key = _PyCode_ConstantKey(o);
1285 if (key == NULL) {
1286 return NULL;
1287 }
1288
1289 // t is borrowed reference
1290 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
1291 if (t != key) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001292 // o is registered in c_const_cache. Just use it.
Zackery Spytz9b4a1b12019-03-20 03:16:25 -06001293 Py_XINCREF(t);
INADA Naokic2e16072018-11-26 21:23:22 +09001294 Py_DECREF(key);
1295 return t;
1296 }
1297
INADA Naokif7e4d362018-11-29 00:58:46 +09001298 // We registered o in c_const_cache.
Simeon63b5fc52019-04-09 19:36:57 -04001299 // When o is a tuple or frozenset, we want to merge its
INADA Naokif7e4d362018-11-29 00:58:46 +09001300 // items too.
INADA Naokic2e16072018-11-26 21:23:22 +09001301 if (PyTuple_CheckExact(o)) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001302 Py_ssize_t len = PyTuple_GET_SIZE(o);
1303 for (Py_ssize_t i = 0; i < len; i++) {
INADA Naokic2e16072018-11-26 21:23:22 +09001304 PyObject *item = PyTuple_GET_ITEM(o, i);
1305 PyObject *u = merge_consts_recursive(c, item);
1306 if (u == NULL) {
1307 Py_DECREF(key);
1308 return NULL;
1309 }
1310
1311 // See _PyCode_ConstantKey()
1312 PyObject *v; // borrowed
1313 if (PyTuple_CheckExact(u)) {
1314 v = PyTuple_GET_ITEM(u, 1);
1315 }
1316 else {
1317 v = u;
1318 }
1319 if (v != item) {
1320 Py_INCREF(v);
1321 PyTuple_SET_ITEM(o, i, v);
1322 Py_DECREF(item);
1323 }
1324
1325 Py_DECREF(u);
1326 }
1327 }
INADA Naokif7e4d362018-11-29 00:58:46 +09001328 else if (PyFrozenSet_CheckExact(o)) {
Simeon63b5fc52019-04-09 19:36:57 -04001329 // *key* is tuple. And its first item is frozenset of
INADA Naokif7e4d362018-11-29 00:58:46 +09001330 // constant keys.
1331 // See _PyCode_ConstantKey() for detail.
1332 assert(PyTuple_CheckExact(key));
1333 assert(PyTuple_GET_SIZE(key) == 2);
1334
1335 Py_ssize_t len = PySet_GET_SIZE(o);
1336 if (len == 0) { // empty frozenset should not be re-created.
1337 return key;
1338 }
1339 PyObject *tuple = PyTuple_New(len);
1340 if (tuple == NULL) {
1341 Py_DECREF(key);
1342 return NULL;
1343 }
1344 Py_ssize_t i = 0, pos = 0;
1345 PyObject *item;
1346 Py_hash_t hash;
1347 while (_PySet_NextEntry(o, &pos, &item, &hash)) {
1348 PyObject *k = merge_consts_recursive(c, item);
1349 if (k == NULL) {
1350 Py_DECREF(tuple);
1351 Py_DECREF(key);
1352 return NULL;
1353 }
1354 PyObject *u;
1355 if (PyTuple_CheckExact(k)) {
1356 u = PyTuple_GET_ITEM(k, 1);
1357 Py_INCREF(u);
1358 Py_DECREF(k);
1359 }
1360 else {
1361 u = k;
1362 }
1363 PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u.
1364 i++;
1365 }
1366
1367 // Instead of rewriting o, we create new frozenset and embed in the
1368 // key tuple. Caller should get merged frozenset from the key tuple.
1369 PyObject *new = PyFrozenSet_New(tuple);
1370 Py_DECREF(tuple);
1371 if (new == NULL) {
1372 Py_DECREF(key);
1373 return NULL;
1374 }
1375 assert(PyTuple_GET_ITEM(key, 1) == o);
1376 Py_DECREF(o);
1377 PyTuple_SET_ITEM(key, 1, new);
1378 }
INADA Naokic2e16072018-11-26 21:23:22 +09001379
1380 return key;
1381}
1382
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001383static Py_ssize_t
1384compiler_add_const(struct compiler *c, PyObject *o)
1385{
INADA Naokic2e16072018-11-26 21:23:22 +09001386 PyObject *key = merge_consts_recursive(c, o);
1387 if (key == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001388 return -1;
INADA Naokic2e16072018-11-26 21:23:22 +09001389 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001390
Andy Lester76d58772020-03-10 21:18:12 -05001391 Py_ssize_t arg = compiler_add_o(c->u->u_consts, key);
INADA Naokic2e16072018-11-26 21:23:22 +09001392 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001394}
1395
1396static int
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001397compiler_addop_load_const(struct compiler *c, PyObject *o)
1398{
1399 Py_ssize_t arg = compiler_add_const(c, o);
1400 if (arg < 0)
1401 return 0;
1402 return compiler_addop_i(c, LOAD_CONST, arg);
1403}
1404
1405static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001406compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408{
Andy Lester76d58772020-03-10 21:18:12 -05001409 Py_ssize_t arg = compiler_add_o(dict, o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001410 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001411 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412 return compiler_addop_i(c, opcode, arg);
1413}
1414
1415static int
1416compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001419 Py_ssize_t arg;
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001420
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001421 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1422 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001423 return 0;
Andy Lester76d58772020-03-10 21:18:12 -05001424 arg = compiler_add_o(dict, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425 Py_DECREF(mangled);
1426 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001427 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428 return compiler_addop_i(c, opcode, arg);
1429}
1430
1431/* Add an opcode with an integer argument.
1432 Returns 0 on failure, 1 on success.
1433*/
1434
1435static int
Mark Shannon11e0b292021-04-15 14:28:56 +01001436compiler_addop_i_line(struct compiler *c, int opcode, Py_ssize_t oparg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 struct instr *i;
1439 int off;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001440
Victor Stinner2ad474b2016-03-01 23:34:47 +01001441 /* oparg value is unsigned, but a signed C int is usually used to store
1442 it in the C code (like Python/ceval.c).
1443
1444 Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1445
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001446 The argument of a concrete bytecode instruction is limited to 8-bit.
1447 EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1448 assert(HAS_ARG(opcode));
Victor Stinner2ad474b2016-03-01 23:34:47 +01001449 assert(0 <= oparg && oparg <= 2147483647);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001450
Andy Lester76d58772020-03-10 21:18:12 -05001451 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 if (off < 0)
1453 return 0;
1454 i = &c->u->u_curblock->b_instr[off];
Victor Stinnerf8e32212013-11-19 23:56:34 +01001455 i->i_opcode = opcode;
1456 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
Mark Shannon11e0b292021-04-15 14:28:56 +01001457 i->i_lineno = lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459}
1460
Mark Shannon11e0b292021-04-15 14:28:56 +01001461static int
1462compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
1463{
1464 return compiler_addop_i_line(c, opcode, oparg, c->u->u_lineno);
1465}
1466
1467static int
1468compiler_addop_i_noline(struct compiler *c, int opcode, Py_ssize_t oparg)
1469{
1470 return compiler_addop_i_line(c, opcode, oparg, -1);
1471}
1472
Mark Shannon28b75c82020-12-23 11:43:10 +00001473static int add_jump_to_block(basicblock *b, int opcode, int lineno, basicblock *target)
1474{
1475 assert(HAS_ARG(opcode));
1476 assert(b != NULL);
1477 assert(target != NULL);
1478
1479 int off = compiler_next_instr(b);
1480 struct instr *i = &b->b_instr[off];
1481 if (off < 0) {
1482 return 0;
1483 }
1484 i->i_opcode = opcode;
1485 i->i_target = target;
1486 i->i_lineno = lineno;
1487 return 1;
1488}
1489
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001490static int
Mark Shannon582aaf12020-08-04 17:30:11 +01001491compiler_addop_j(struct compiler *c, int opcode, basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001492{
Mark Shannon28b75c82020-12-23 11:43:10 +00001493 return add_jump_to_block(c->u->u_curblock, opcode, c->u->u_lineno, b);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494}
1495
Mark Shannon127dde52021-01-04 18:06:55 +00001496static int
1497compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b)
1498{
1499 return add_jump_to_block(c->u->u_curblock, opcode, -1, b);
1500}
1501
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +01001502/* NEXT_BLOCK() creates an implicit jump from the current block
1503 to the new block.
1504
1505 The returns inside this macro make it impossible to decref objects
1506 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 if (compiler_next_block((C)) == NULL) \
1510 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001511}
1512
1513#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 if (!compiler_addop((C), (OP))) \
1515 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516}
1517
Mark Shannon3bd60352021-01-13 12:05:43 +00001518#define ADDOP_NOLINE(C, OP) { \
1519 if (!compiler_addop_noline((C), (OP))) \
1520 return 0; \
1521}
1522
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001523#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 if (!compiler_addop((C), (OP))) { \
1525 compiler_exit_scope(c); \
1526 return 0; \
1527 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001528}
1529
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001530#define ADDOP_LOAD_CONST(C, O) { \
1531 if (!compiler_addop_load_const((C), (O))) \
1532 return 0; \
1533}
1534
1535/* Same as ADDOP_LOAD_CONST, but steals a reference. */
1536#define ADDOP_LOAD_CONST_NEW(C, O) { \
1537 PyObject *__new_const = (O); \
1538 if (__new_const == NULL) { \
1539 return 0; \
1540 } \
1541 if (!compiler_addop_load_const((C), __new_const)) { \
1542 Py_DECREF(__new_const); \
1543 return 0; \
1544 } \
1545 Py_DECREF(__new_const); \
1546}
1547
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001548#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1550 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001551}
1552
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001553/* Same as ADDOP_O, but steals a reference. */
1554#define ADDOP_N(C, OP, O, TYPE) { \
1555 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1556 Py_DECREF((O)); \
1557 return 0; \
1558 } \
1559 Py_DECREF((O)); \
1560}
1561
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001562#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1564 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001565}
1566
1567#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 if (!compiler_addop_i((C), (OP), (O))) \
1569 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001570}
1571
Mark Shannon11e0b292021-04-15 14:28:56 +01001572#define ADDOP_I_NOLINE(C, OP, O) { \
1573 if (!compiler_addop_i_noline((C), (OP), (O))) \
1574 return 0; \
1575}
1576
Mark Shannon582aaf12020-08-04 17:30:11 +01001577#define ADDOP_JUMP(C, OP, O) { \
1578 if (!compiler_addop_j((C), (OP), (O))) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001580}
1581
Mark Shannon127dde52021-01-04 18:06:55 +00001582/* Add a jump with no line number.
1583 * Used for artificial jumps that have no corresponding
1584 * token in the source code. */
1585#define ADDOP_JUMP_NOLINE(C, OP, O) { \
1586 if (!compiler_addop_j_noline((C), (OP), (O))) \
1587 return 0; \
1588}
1589
Mark Shannon9af0e472020-01-14 10:12:45 +00001590#define ADDOP_COMPARE(C, CMP) { \
1591 if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \
1592 return 0; \
1593}
1594
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001595/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1596 the ASDL name to synthesize the name of the C type and the visit function.
1597*/
1598
1599#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 if (!compiler_visit_ ## TYPE((C), (V))) \
1601 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001602}
1603
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001604#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 if (!compiler_visit_ ## TYPE((C), (V))) { \
1606 compiler_exit_scope(c); \
1607 return 0; \
1608 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001609}
1610
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001611#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 if (!compiler_visit_slice((C), (V), (CTX))) \
1613 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001614}
1615
1616#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001618 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1620 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1621 if (!compiler_visit_ ## TYPE((C), elt)) \
1622 return 0; \
1623 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001624}
1625
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001626#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001628 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1630 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1631 if (!compiler_visit_ ## TYPE((C), elt)) { \
1632 compiler_exit_scope(c); \
1633 return 0; \
1634 } \
1635 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001636}
1637
Brandt Bucher145bf262021-02-26 14:51:55 -08001638#define RETURN_IF_FALSE(X) \
1639 if (!(X)) { \
1640 return 0; \
1641 }
1642
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001643/* Search if variable annotations are present statically in a block. */
1644
1645static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001646find_ann(asdl_stmt_seq *stmts)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001647{
1648 int i, j, res = 0;
1649 stmt_ty st;
1650
1651 for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1652 st = (stmt_ty)asdl_seq_GET(stmts, i);
1653 switch (st->kind) {
1654 case AnnAssign_kind:
1655 return 1;
1656 case For_kind:
1657 res = find_ann(st->v.For.body) ||
1658 find_ann(st->v.For.orelse);
1659 break;
1660 case AsyncFor_kind:
1661 res = find_ann(st->v.AsyncFor.body) ||
1662 find_ann(st->v.AsyncFor.orelse);
1663 break;
1664 case While_kind:
1665 res = find_ann(st->v.While.body) ||
1666 find_ann(st->v.While.orelse);
1667 break;
1668 case If_kind:
1669 res = find_ann(st->v.If.body) ||
1670 find_ann(st->v.If.orelse);
1671 break;
1672 case With_kind:
1673 res = find_ann(st->v.With.body);
1674 break;
1675 case AsyncWith_kind:
1676 res = find_ann(st->v.AsyncWith.body);
1677 break;
1678 case Try_kind:
1679 for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1680 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1681 st->v.Try.handlers, j);
1682 if (find_ann(handler->v.ExceptHandler.body)) {
1683 return 1;
1684 }
1685 }
1686 res = find_ann(st->v.Try.body) ||
1687 find_ann(st->v.Try.finalbody) ||
1688 find_ann(st->v.Try.orelse);
1689 break;
1690 default:
1691 res = 0;
1692 }
1693 if (res) {
1694 break;
1695 }
1696 }
1697 return res;
1698}
1699
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001700/*
1701 * Frame block handling functions
1702 */
1703
1704static int
1705compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b,
Mark Shannonfee55262019-11-21 09:11:43 +00001706 basicblock *exit, void *datum)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001707{
1708 struct fblockinfo *f;
1709 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
Mark Shannon02d126a2020-09-25 14:04:19 +01001710 return compiler_error(c, "too many statically nested blocks");
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001711 }
1712 f = &c->u->u_fblock[c->u->u_nfblocks++];
1713 f->fb_type = t;
1714 f->fb_block = b;
1715 f->fb_exit = exit;
Mark Shannonfee55262019-11-21 09:11:43 +00001716 f->fb_datum = datum;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001717 return 1;
1718}
1719
1720static void
1721compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
1722{
1723 struct compiler_unit *u = c->u;
1724 assert(u->u_nfblocks > 0);
1725 u->u_nfblocks--;
1726 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
1727 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
1728}
1729
Mark Shannonfee55262019-11-21 09:11:43 +00001730static int
1731compiler_call_exit_with_nones(struct compiler *c) {
1732 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1733 ADDOP(c, DUP_TOP);
1734 ADDOP(c, DUP_TOP);
1735 ADDOP_I(c, CALL_FUNCTION, 3);
1736 return 1;
1737}
1738
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001739/* Unwind a frame block. If preserve_tos is true, the TOS before
Mark Shannonfee55262019-11-21 09:11:43 +00001740 * popping the blocks will be restored afterwards, unless another
1741 * return, break or continue is found. In which case, the TOS will
1742 * be popped.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001743 */
1744static int
1745compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
1746 int preserve_tos)
1747{
1748 switch (info->fb_type) {
1749 case WHILE_LOOP:
Mark Shannon02d126a2020-09-25 14:04:19 +01001750 case EXCEPTION_HANDLER:
tomKPZ7a7ba3d2021-04-07 07:43:45 -07001751 case ASYNC_COMPREHENSION_GENERATOR:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001752 return 1;
1753
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001754 case FOR_LOOP:
1755 /* Pop the iterator */
1756 if (preserve_tos) {
1757 ADDOP(c, ROT_TWO);
1758 }
1759 ADDOP(c, POP_TOP);
1760 return 1;
1761
Mark Shannon02d126a2020-09-25 14:04:19 +01001762 case TRY_EXCEPT:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001763 ADDOP(c, POP_BLOCK);
1764 return 1;
1765
1766 case FINALLY_TRY:
Mark Shannon5274b682020-12-16 13:07:01 +00001767 /* This POP_BLOCK gets the line number of the unwinding statement */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001768 ADDOP(c, POP_BLOCK);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001769 if (preserve_tos) {
Mark Shannonfee55262019-11-21 09:11:43 +00001770 if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) {
1771 return 0;
1772 }
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001773 }
Mark Shannon5274b682020-12-16 13:07:01 +00001774 /* Emit the finally block */
Mark Shannonfee55262019-11-21 09:11:43 +00001775 VISIT_SEQ(c, stmt, info->fb_datum);
1776 if (preserve_tos) {
1777 compiler_pop_fblock(c, POP_VALUE, NULL);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001778 }
Mark Shannon5274b682020-12-16 13:07:01 +00001779 /* The finally block should appear to execute after the
1780 * statement causing the unwinding, so make the unwinding
1781 * instruction artificial */
1782 c->u->u_lineno = -1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001783 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001784
Mark Shannonfee55262019-11-21 09:11:43 +00001785 case FINALLY_END:
1786 if (preserve_tos) {
1787 ADDOP(c, ROT_FOUR);
1788 }
1789 ADDOP(c, POP_TOP);
1790 ADDOP(c, POP_TOP);
1791 ADDOP(c, POP_TOP);
1792 if (preserve_tos) {
1793 ADDOP(c, ROT_FOUR);
1794 }
1795 ADDOP(c, POP_EXCEPT);
1796 return 1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001797
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001798 case WITH:
1799 case ASYNC_WITH:
1800 ADDOP(c, POP_BLOCK);
1801 if (preserve_tos) {
1802 ADDOP(c, ROT_TWO);
1803 }
Mark Shannonfee55262019-11-21 09:11:43 +00001804 if(!compiler_call_exit_with_nones(c)) {
1805 return 0;
1806 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001807 if (info->fb_type == ASYNC_WITH) {
1808 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001809 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001810 ADDOP(c, YIELD_FROM);
1811 }
Mark Shannonfee55262019-11-21 09:11:43 +00001812 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001813 return 1;
1814
1815 case HANDLER_CLEANUP:
Mark Shannonfee55262019-11-21 09:11:43 +00001816 if (info->fb_datum) {
1817 ADDOP(c, POP_BLOCK);
1818 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001819 if (preserve_tos) {
1820 ADDOP(c, ROT_FOUR);
1821 }
Mark Shannonfee55262019-11-21 09:11:43 +00001822 ADDOP(c, POP_EXCEPT);
1823 if (info->fb_datum) {
1824 ADDOP_LOAD_CONST(c, Py_None);
1825 compiler_nameop(c, info->fb_datum, Store);
1826 compiler_nameop(c, info->fb_datum, Del);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001827 }
Mark Shannonfee55262019-11-21 09:11:43 +00001828 return 1;
1829
1830 case POP_VALUE:
1831 if (preserve_tos) {
1832 ADDOP(c, ROT_TWO);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001833 }
Mark Shannonfee55262019-11-21 09:11:43 +00001834 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001835 return 1;
1836 }
1837 Py_UNREACHABLE();
1838}
1839
Mark Shannonfee55262019-11-21 09:11:43 +00001840/** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */
1841static int
1842compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) {
1843 if (c->u->u_nfblocks == 0) {
1844 return 1;
1845 }
1846 struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1];
1847 if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) {
1848 *loop = top;
1849 return 1;
1850 }
1851 struct fblockinfo copy = *top;
1852 c->u->u_nfblocks--;
1853 if (!compiler_unwind_fblock(c, &copy, preserve_tos)) {
1854 return 0;
1855 }
1856 if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) {
1857 return 0;
1858 }
1859 c->u->u_fblock[c->u->u_nfblocks] = copy;
1860 c->u->u_nfblocks++;
1861 return 1;
1862}
1863
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001864/* Compile a sequence of statements, checking for a docstring
1865 and for annotations. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001866
1867static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001868compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001869{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001870 int i = 0;
1871 stmt_ty st;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001872 PyObject *docstring;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001873
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001874 /* Set current line number to the line number of first statement.
1875 This way line number for SETUP_ANNOTATIONS will always
1876 coincide with the line number of first "real" statement in module.
Hansraj Das01171eb2019-10-09 07:54:02 +05301877 If body is empty, then lineno will be set later in assemble. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001878 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) {
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001879 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001880 SET_LOC(c, st);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001881 }
1882 /* Every annotated class and module should have __annotations__. */
1883 if (find_ann(stmts)) {
1884 ADDOP(c, SETUP_ANNOTATIONS);
1885 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001886 if (!asdl_seq_LEN(stmts))
1887 return 1;
INADA Naokicb41b272017-02-23 00:31:59 +09001888 /* if not -OO mode, set docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001889 if (c->c_optimize < 2) {
1890 docstring = _PyAST_GetDocString(stmts);
1891 if (docstring) {
1892 i = 1;
1893 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1894 assert(st->kind == Expr_kind);
1895 VISIT(c, expr, st->v.Expr.value);
1896 if (!compiler_nameop(c, __doc__, Store))
1897 return 0;
1898 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001900 for (; i < asdl_seq_LEN(stmts); i++)
1901 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001903}
1904
1905static PyCodeObject *
1906compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001907{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 PyCodeObject *co;
1909 int addNone = 1;
1910 static PyObject *module;
1911 if (!module) {
1912 module = PyUnicode_InternFromString("<module>");
1913 if (!module)
1914 return NULL;
1915 }
1916 /* Use 0 for firstlineno initially, will fixup in assemble(). */
Mark Shannon877df852020-11-12 09:43:29 +00001917 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 return NULL;
1919 switch (mod->kind) {
1920 case Module_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001921 if (!compiler_body(c, mod->v.Module.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 compiler_exit_scope(c);
1923 return 0;
1924 }
1925 break;
1926 case Interactive_kind:
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001927 if (find_ann(mod->v.Interactive.body)) {
1928 ADDOP(c, SETUP_ANNOTATIONS);
1929 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 c->c_interactive = 1;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001931 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 break;
1933 case Expression_kind:
1934 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1935 addNone = 0;
1936 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 default:
1938 PyErr_Format(PyExc_SystemError,
1939 "module kind %d should not be possible",
1940 mod->kind);
1941 return 0;
1942 }
1943 co = assemble(c, addNone);
1944 compiler_exit_scope(c);
1945 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001946}
1947
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001948/* The test for LOCAL must come before the test for FREE in order to
1949 handle classes where name is both local and free. The local var is
1950 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001951*/
1952
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001953static int
1954get_ref_type(struct compiler *c, PyObject *name)
1955{
Victor Stinner0b1bc562013-05-16 22:17:17 +02001956 int scope;
Benjamin Peterson312595c2013-05-15 15:26:42 -05001957 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001958 _PyUnicode_EqualToASCIIString(name, "__class__"))
Benjamin Peterson312595c2013-05-15 15:26:42 -05001959 return CELL;
Victor Stinner28ad12f2021-03-19 12:41:49 +01001960 scope = _PyST_GetScope(c->u->u_ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 if (scope == 0) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001962 PyErr_Format(PyExc_SystemError,
Victor Stinner28ad12f2021-03-19 12:41:49 +01001963 "_PyST_GetScope(name=%R) failed: "
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001964 "unknown scope in unit %S (%R); "
1965 "symbols: %R; locals: %R; globals: %R",
1966 name,
1967 c->u->u_name, c->u->u_ste->ste_id,
1968 c->u->u_ste->ste_symbols, c->u->u_varnames, c->u->u_names);
1969 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001972}
1973
1974static int
1975compiler_lookup_arg(PyObject *dict, PyObject *name)
1976{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001977 PyObject *v;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001978 v = PyDict_GetItemWithError(dict, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001979 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001980 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001981 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001982}
1983
1984static int
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001985compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags,
1986 PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001987{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001988 Py_ssize_t i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001989 if (qualname == NULL)
1990 qualname = co->co_name;
1991
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001992 if (free) {
1993 for (i = 0; i < free; ++i) {
1994 /* Bypass com_addop_varname because it will generate
1995 LOAD_DEREF but LOAD_CLOSURE is needed.
1996 */
1997 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001998
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001999 /* Special case: If a class contains a method with a
2000 free variable that has the same name as a method,
2001 the name will be considered free *and* local in the
2002 class. It should be handled by the closure, as
Min ho Kimc4cacc82019-07-31 08:16:13 +10002003 well as by the normal name lookup logic.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002004 */
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002005 int reftype = get_ref_type(c, name);
2006 if (reftype == -1) {
2007 return 0;
2008 }
2009 int arg;
2010 if (reftype == CELL) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002011 arg = compiler_lookup_arg(c->u->u_cellvars, name);
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002012 }
2013 else {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002014 arg = compiler_lookup_arg(c->u->u_freevars, name);
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002015 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002016 if (arg == -1) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002017 PyErr_Format(PyExc_SystemError,
2018 "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; "
2019 "freevars of code %S: %R",
2020 name,
2021 reftype,
2022 c->u->u_name,
2023 co->co_name,
2024 co->co_freevars);
2025 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002026 }
2027 ADDOP_I(c, LOAD_CLOSURE, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002029 flags |= 0x08;
2030 ADDOP_I(c, BUILD_TUPLE, free);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002031 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002032 ADDOP_LOAD_CONST(c, (PyObject*)co);
2033 ADDOP_LOAD_CONST(c, qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002034 ADDOP_I(c, MAKE_FUNCTION, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002036}
2037
2038static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002039compiler_decorators(struct compiler *c, asdl_expr_seq* decos)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002040{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002041 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 if (!decos)
2044 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2047 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
2048 }
2049 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002050}
2051
2052static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002053compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs,
2054 asdl_expr_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00002055{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002056 /* Push a dict of keyword-only default values.
2057
2058 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
2059 */
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002060 int i;
2061 PyObject *keys = NULL;
2062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
2064 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
2065 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
2066 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04002067 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002068 if (!mangled) {
2069 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002071 if (keys == NULL) {
2072 keys = PyList_New(1);
2073 if (keys == NULL) {
2074 Py_DECREF(mangled);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002075 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002076 }
2077 PyList_SET_ITEM(keys, 0, mangled);
2078 }
2079 else {
2080 int res = PyList_Append(keys, mangled);
2081 Py_DECREF(mangled);
2082 if (res == -1) {
2083 goto error;
2084 }
2085 }
2086 if (!compiler_visit_expr(c, default_)) {
2087 goto error;
2088 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 }
2090 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002091 if (keys != NULL) {
2092 Py_ssize_t default_count = PyList_GET_SIZE(keys);
2093 PyObject *keys_tuple = PyList_AsTuple(keys);
2094 Py_DECREF(keys);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002095 ADDOP_LOAD_CONST_NEW(c, keys_tuple);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002096 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002097 assert(default_count > 0);
2098 return 1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002099 }
2100 else {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002101 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002102 }
2103
2104error:
2105 Py_XDECREF(keys);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002106 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002107}
2108
2109static int
Guido van Rossum95e4d582018-01-26 08:20:18 -08002110compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
2111{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +03002112 ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation));
Guido van Rossum95e4d582018-01-26 08:20:18 -08002113 return 1;
2114}
2115
2116static int
Neal Norwitzc1505362006-12-28 06:47:50 +00002117compiler_visit_argannotation(struct compiler *c, identifier id,
Yurii Karabas73019792020-11-25 12:43:18 +02002118 expr_ty annotation, Py_ssize_t *annotations_len)
Neal Norwitzc1505362006-12-28 06:47:50 +00002119{
Pablo Galindob0544ba2021-04-21 12:41:19 +01002120 if (!annotation) {
2121 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 }
Pablo Galindob0544ba2021-04-21 12:41:19 +01002123
2124 PyObject *mangled = _Py_Mangle(c->u->u_private, id);
2125 if (!mangled) {
2126 return 0;
2127 }
2128 ADDOP_LOAD_CONST(c, mangled);
2129 Py_DECREF(mangled);
2130
2131 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
2132 VISIT(c, annexpr, annotation)
2133 }
2134 else {
2135 VISIT(c, expr, annotation);
2136 }
2137 *annotations_len += 2;
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002138 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002139}
2140
2141static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002142compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args,
Yurii Karabas73019792020-11-25 12:43:18 +02002143 Py_ssize_t *annotations_len)
Neal Norwitzc1505362006-12-28 06:47:50 +00002144{
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002145 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 for (i = 0; i < asdl_seq_LEN(args); i++) {
2147 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002148 if (!compiler_visit_argannotation(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002149 c,
2150 arg->arg,
2151 arg->annotation,
Yurii Karabas73019792020-11-25 12:43:18 +02002152 annotations_len))
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002153 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002155 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002156}
2157
2158static int
2159compiler_visit_annotations(struct compiler *c, arguments_ty args,
2160 expr_ty returns)
2161{
Yurii Karabas73019792020-11-25 12:43:18 +02002162 /* Push arg annotation names and values.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002163 The expressions are evaluated out-of-order wrt the source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00002164
Yurii Karabas73019792020-11-25 12:43:18 +02002165 Return 0 on error, -1 if no annotations pushed, 1 if a annotations is pushed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166 */
2167 static identifier return_str;
Yurii Karabas73019792020-11-25 12:43:18 +02002168 Py_ssize_t annotations_len = 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002169
Yurii Karabas73019792020-11-25 12:43:18 +02002170 if (!compiler_visit_argannotations(c, args->args, &annotations_len))
2171 return 0;
2172 if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len))
2173 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002174 if (args->vararg && args->vararg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002175 !compiler_visit_argannotation(c, args->vararg->arg,
Yurii Karabas73019792020-11-25 12:43:18 +02002176 args->vararg->annotation, &annotations_len))
2177 return 0;
2178 if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len))
2179 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002180 if (args->kwarg && args->kwarg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002181 !compiler_visit_argannotation(c, args->kwarg->arg,
Yurii Karabas73019792020-11-25 12:43:18 +02002182 args->kwarg->annotation, &annotations_len))
2183 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 if (!return_str) {
2186 return_str = PyUnicode_InternFromString("return");
2187 if (!return_str)
Yurii Karabas73019792020-11-25 12:43:18 +02002188 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 }
Yurii Karabas73019792020-11-25 12:43:18 +02002190 if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) {
2191 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 }
2193
Yurii Karabas73019792020-11-25 12:43:18 +02002194 if (annotations_len) {
2195 ADDOP_I(c, BUILD_TUPLE, annotations_len);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002196 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002198
Yurii Karabas73019792020-11-25 12:43:18 +02002199 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002200}
2201
2202static int
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002203compiler_visit_defaults(struct compiler *c, arguments_ty args)
2204{
2205 VISIT_SEQ(c, expr, args->defaults);
2206 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
2207 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002208}
2209
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002210static Py_ssize_t
2211compiler_default_arguments(struct compiler *c, arguments_ty args)
2212{
2213 Py_ssize_t funcflags = 0;
2214 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002215 if (!compiler_visit_defaults(c, args))
2216 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002217 funcflags |= 0x01;
2218 }
2219 if (args->kwonlyargs) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002220 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002221 args->kw_defaults);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002222 if (res == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002223 return -1;
2224 }
2225 else if (res > 0) {
2226 funcflags |= 0x02;
2227 }
2228 }
2229 return funcflags;
2230}
2231
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002232static int
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002233forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx)
2234{
2235
2236 if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2237 compiler_error(c, "cannot assign to __debug__");
2238 return 1;
2239 }
2240 return 0;
2241}
2242
2243static int
2244compiler_check_debug_one_arg(struct compiler *c, arg_ty arg)
2245{
2246 if (arg != NULL) {
2247 if (forbidden_name(c, arg->arg, Store))
2248 return 0;
2249 }
2250 return 1;
2251}
2252
2253static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002254compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args)
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002255{
2256 if (args != NULL) {
Pablo Galindoee40e4b2020-04-23 03:43:08 +01002257 for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002258 if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i)))
2259 return 0;
2260 }
2261 }
2262 return 1;
2263}
2264
2265static int
2266compiler_check_debug_args(struct compiler *c, arguments_ty args)
2267{
2268 if (!compiler_check_debug_args_seq(c, args->posonlyargs))
2269 return 0;
2270 if (!compiler_check_debug_args_seq(c, args->args))
2271 return 0;
2272 if (!compiler_check_debug_one_arg(c, args->vararg))
2273 return 0;
2274 if (!compiler_check_debug_args_seq(c, args->kwonlyargs))
2275 return 0;
2276 if (!compiler_check_debug_one_arg(c, args->kwarg))
2277 return 0;
2278 return 1;
2279}
2280
2281static int
Yury Selivanov75445082015-05-11 22:57:16 -04002282compiler_function(struct compiler *c, stmt_ty s, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 PyCodeObject *co;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002285 PyObject *qualname, *docstring = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002286 arguments_ty args;
2287 expr_ty returns;
2288 identifier name;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002289 asdl_expr_seq* decos;
2290 asdl_stmt_seq *body;
INADA Naokicb41b272017-02-23 00:31:59 +09002291 Py_ssize_t i, funcflags;
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002292 int annotations;
Yury Selivanov75445082015-05-11 22:57:16 -04002293 int scope_type;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002294 int firstlineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002295
Yury Selivanov75445082015-05-11 22:57:16 -04002296 if (is_async) {
2297 assert(s->kind == AsyncFunctionDef_kind);
2298
2299 args = s->v.AsyncFunctionDef.args;
2300 returns = s->v.AsyncFunctionDef.returns;
2301 decos = s->v.AsyncFunctionDef.decorator_list;
2302 name = s->v.AsyncFunctionDef.name;
2303 body = s->v.AsyncFunctionDef.body;
2304
2305 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
2306 } else {
2307 assert(s->kind == FunctionDef_kind);
2308
2309 args = s->v.FunctionDef.args;
2310 returns = s->v.FunctionDef.returns;
2311 decos = s->v.FunctionDef.decorator_list;
2312 name = s->v.FunctionDef.name;
2313 body = s->v.FunctionDef.body;
2314
2315 scope_type = COMPILER_SCOPE_FUNCTION;
2316 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002317
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002318 if (!compiler_check_debug_args(c, args))
2319 return 0;
2320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 if (!compiler_decorators(c, decos))
2322 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002323
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002324 firstlineno = s->lineno;
2325 if (asdl_seq_LEN(decos)) {
2326 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2327 }
2328
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002329 funcflags = compiler_default_arguments(c, args);
2330 if (funcflags == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002332 }
2333
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002334 annotations = compiler_visit_annotations(c, args, returns);
2335 if (annotations == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002336 return 0;
2337 }
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002338 else if (annotations > 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002339 funcflags |= 0x04;
2340 }
2341
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002342 if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002343 return 0;
2344 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002345
INADA Naokicb41b272017-02-23 00:31:59 +09002346 /* if not -OO mode, add docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002347 if (c->c_optimize < 2) {
2348 docstring = _PyAST_GetDocString(body);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002349 }
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002350 if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 compiler_exit_scope(c);
2352 return 0;
2353 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002356 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Mark Shannon877df852020-11-12 09:43:29 +00002358 for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) {
Mark Shannonfd009e62020-11-13 12:53:53 +00002359 VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i));
Mark Shannon877df852020-11-12 09:43:29 +00002360 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002362 qualname = c->u->u_qualname;
2363 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002365 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002366 Py_XDECREF(qualname);
2367 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002369 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002370
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002371 if (!compiler_make_closure(c, co, funcflags, qualname)) {
2372 Py_DECREF(qualname);
2373 Py_DECREF(co);
2374 return 0;
2375 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002376 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 /* decorators */
2380 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2381 ADDOP_I(c, CALL_FUNCTION, 1);
2382 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002383
Yury Selivanov75445082015-05-11 22:57:16 -04002384 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002385}
2386
2387static int
2388compiler_class(struct compiler *c, stmt_ty s)
2389{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 PyCodeObject *co;
2391 PyObject *str;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002392 int i, firstlineno;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002393 asdl_expr_seq *decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 if (!compiler_decorators(c, decos))
2396 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002397
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002398 firstlineno = s->lineno;
2399 if (asdl_seq_LEN(decos)) {
2400 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2401 }
2402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 /* ultimately generate code for:
2404 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
2405 where:
2406 <func> is a function/closure created from the class body;
2407 it has a single argument (__locals__) where the dict
2408 (or MutableSequence) representing the locals is passed
2409 <name> is the class name
2410 <bases> is the positional arguments and *varargs argument
2411 <keywords> is the keyword arguments and **kwds argument
2412 This borrows from compiler_call.
2413 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002416 if (!compiler_enter_scope(c, s->v.ClassDef.name,
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002417 COMPILER_SCOPE_CLASS, (void *)s, firstlineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 return 0;
2419 /* this block represents what we do in the new scope */
2420 {
2421 /* use the class name for name mangling */
2422 Py_INCREF(s->v.ClassDef.name);
Serhiy Storchaka48842712016-04-06 09:45:48 +03002423 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 /* load (global) __name__ ... */
2425 str = PyUnicode_InternFromString("__name__");
2426 if (!str || !compiler_nameop(c, str, Load)) {
2427 Py_XDECREF(str);
2428 compiler_exit_scope(c);
2429 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002430 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 Py_DECREF(str);
2432 /* ... and store it as __module__ */
2433 str = PyUnicode_InternFromString("__module__");
2434 if (!str || !compiler_nameop(c, str, Store)) {
2435 Py_XDECREF(str);
2436 compiler_exit_scope(c);
2437 return 0;
2438 }
2439 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002440 assert(c->u->u_qualname);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002441 ADDOP_LOAD_CONST(c, c->u->u_qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002442 str = PyUnicode_InternFromString("__qualname__");
2443 if (!str || !compiler_nameop(c, str, Store)) {
2444 Py_XDECREF(str);
2445 compiler_exit_scope(c);
2446 return 0;
2447 }
2448 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 /* compile the body proper */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002450 if (!compiler_body(c, s->v.ClassDef.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 compiler_exit_scope(c);
2452 return 0;
2453 }
Mark Shannone56d54e2021-01-15 13:52:00 +00002454 /* The following code is artificial */
2455 c->u->u_lineno = -1;
Nick Coghlan19d24672016-12-05 16:47:55 +10002456 /* Return __classcell__ if it is referenced, otherwise return None */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002457 if (c->u->u_ste->ste_needs_class_closure) {
Nick Coghlan19d24672016-12-05 16:47:55 +10002458 /* Store __classcell__ into class namespace & return it */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002459 str = PyUnicode_InternFromString("__class__");
2460 if (str == NULL) {
2461 compiler_exit_scope(c);
2462 return 0;
2463 }
2464 i = compiler_lookup_arg(c->u->u_cellvars, str);
2465 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01002466 if (i < 0) {
2467 compiler_exit_scope(c);
2468 return 0;
2469 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002470 assert(i == 0);
Nick Coghlan944368e2016-09-11 14:45:49 +10002471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002472 ADDOP_I(c, LOAD_CLOSURE, i);
Nick Coghlan19d24672016-12-05 16:47:55 +10002473 ADDOP(c, DUP_TOP);
Nick Coghlan944368e2016-09-11 14:45:49 +10002474 str = PyUnicode_InternFromString("__classcell__");
2475 if (!str || !compiler_nameop(c, str, Store)) {
2476 Py_XDECREF(str);
2477 compiler_exit_scope(c);
2478 return 0;
2479 }
2480 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002482 else {
Nick Coghlan19d24672016-12-05 16:47:55 +10002483 /* No methods referenced __class__, so just return None */
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002484 assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002485 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson312595c2013-05-15 15:26:42 -05002486 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002487 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 /* create the code object */
2489 co = assemble(c, 1);
2490 }
2491 /* leave the new scope */
2492 compiler_exit_scope(c);
2493 if (co == NULL)
2494 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 /* 2. load the 'build_class' function */
2497 ADDOP(c, LOAD_BUILD_CLASS);
2498
2499 /* 3. load a function (or closure) made from the code object */
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002500 if (!compiler_make_closure(c, co, 0, NULL)) {
2501 Py_DECREF(co);
2502 return 0;
2503 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002504 Py_DECREF(co);
2505
2506 /* 4. load class name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002507 ADDOP_LOAD_CONST(c, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002508
2509 /* 5. generate the rest of the code for the call */
Pablo Galindoa5634c42020-09-16 19:42:00 +01002510 if (!compiler_call_helper(c, 2, s->v.ClassDef.bases, s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002511 return 0;
2512
2513 /* 6. apply decorators */
2514 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2515 ADDOP_I(c, CALL_FUNCTION, 1);
2516 }
2517
2518 /* 7. store into <name> */
2519 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2520 return 0;
2521 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002522}
2523
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02002524/* Return 0 if the expression is a constant value except named singletons.
2525 Return 1 otherwise. */
2526static int
2527check_is_arg(expr_ty e)
2528{
2529 if (e->kind != Constant_kind) {
2530 return 1;
2531 }
2532 PyObject *value = e->v.Constant.value;
2533 return (value == Py_None
2534 || value == Py_False
2535 || value == Py_True
2536 || value == Py_Ellipsis);
2537}
2538
2539/* Check operands of identity chacks ("is" and "is not").
2540 Emit a warning if any operand is a constant except named singletons.
2541 Return 0 on error.
2542 */
2543static int
2544check_compare(struct compiler *c, expr_ty e)
2545{
2546 Py_ssize_t i, n;
2547 int left = check_is_arg(e->v.Compare.left);
2548 n = asdl_seq_LEN(e->v.Compare.ops);
2549 for (i = 0; i < n; i++) {
2550 cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i);
2551 int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2552 if (op == Is || op == IsNot) {
2553 if (!right || !left) {
2554 const char *msg = (op == Is)
2555 ? "\"is\" with a literal. Did you mean \"==\"?"
2556 : "\"is not\" with a literal. Did you mean \"!=\"?";
2557 return compiler_warn(c, msg);
2558 }
2559 }
2560 left = right;
2561 }
2562 return 1;
2563}
2564
Mark Shannon9af0e472020-01-14 10:12:45 +00002565static int compiler_addcompare(struct compiler *c, cmpop_ty op)
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002566{
Mark Shannon9af0e472020-01-14 10:12:45 +00002567 int cmp;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002568 switch (op) {
2569 case Eq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002570 cmp = Py_EQ;
2571 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002572 case NotEq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002573 cmp = Py_NE;
2574 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002575 case Lt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002576 cmp = Py_LT;
2577 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002578 case LtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002579 cmp = Py_LE;
2580 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002581 case Gt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002582 cmp = Py_GT;
2583 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002584 case GtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002585 cmp = Py_GE;
2586 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002587 case Is:
Mark Shannon9af0e472020-01-14 10:12:45 +00002588 ADDOP_I(c, IS_OP, 0);
2589 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002590 case IsNot:
Mark Shannon9af0e472020-01-14 10:12:45 +00002591 ADDOP_I(c, IS_OP, 1);
2592 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002593 case In:
Mark Shannon9af0e472020-01-14 10:12:45 +00002594 ADDOP_I(c, CONTAINS_OP, 0);
2595 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002596 case NotIn:
Mark Shannon9af0e472020-01-14 10:12:45 +00002597 ADDOP_I(c, CONTAINS_OP, 1);
2598 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002599 default:
Mark Shannon9af0e472020-01-14 10:12:45 +00002600 Py_UNREACHABLE();
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002601 }
Mark Shannon9af0e472020-01-14 10:12:45 +00002602 ADDOP_I(c, COMPARE_OP, cmp);
2603 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002604}
2605
Mark Shannon9af0e472020-01-14 10:12:45 +00002606
2607
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002608static int
2609compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2610{
2611 switch (e->kind) {
2612 case UnaryOp_kind:
2613 if (e->v.UnaryOp.op == Not)
2614 return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2615 /* fallback to general implementation */
2616 break;
2617 case BoolOp_kind: {
Pablo Galindoa5634c42020-09-16 19:42:00 +01002618 asdl_expr_seq *s = e->v.BoolOp.values;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002619 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2620 assert(n >= 0);
2621 int cond2 = e->v.BoolOp.op == Or;
2622 basicblock *next2 = next;
2623 if (!cond2 != !cond) {
2624 next2 = compiler_new_block(c);
2625 if (next2 == NULL)
2626 return 0;
2627 }
2628 for (i = 0; i < n; ++i) {
2629 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2630 return 0;
2631 }
2632 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2633 return 0;
2634 if (next2 != next)
2635 compiler_use_next_block(c, next2);
2636 return 1;
2637 }
2638 case IfExp_kind: {
2639 basicblock *end, *next2;
2640 end = compiler_new_block(c);
2641 if (end == NULL)
2642 return 0;
2643 next2 = compiler_new_block(c);
2644 if (next2 == NULL)
2645 return 0;
2646 if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2647 return 0;
2648 if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2649 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002650 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002651 compiler_use_next_block(c, next2);
2652 if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2653 return 0;
2654 compiler_use_next_block(c, end);
2655 return 1;
2656 }
2657 case Compare_kind: {
2658 Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2659 if (n > 0) {
Serhiy Storchaka45835252019-02-16 08:29:46 +02002660 if (!check_compare(c, e)) {
2661 return 0;
2662 }
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002663 basicblock *cleanup = compiler_new_block(c);
2664 if (cleanup == NULL)
2665 return 0;
2666 VISIT(c, expr, e->v.Compare.left);
2667 for (i = 0; i < n; i++) {
2668 VISIT(c, expr,
2669 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2670 ADDOP(c, DUP_TOP);
2671 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00002672 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01002673 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002674 NEXT_BLOCK(c);
2675 }
2676 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00002677 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Mark Shannon582aaf12020-08-04 17:30:11 +01002678 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Mark Shannon266b4622020-11-17 19:30:14 +00002679 NEXT_BLOCK(c);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002680 basicblock *end = compiler_new_block(c);
2681 if (end == NULL)
2682 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002683 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002684 compiler_use_next_block(c, cleanup);
2685 ADDOP(c, POP_TOP);
2686 if (!cond) {
Mark Shannon127dde52021-01-04 18:06:55 +00002687 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002688 }
2689 compiler_use_next_block(c, end);
2690 return 1;
2691 }
2692 /* fallback to general implementation */
2693 break;
2694 }
2695 default:
2696 /* fallback to general implementation */
2697 break;
2698 }
2699
2700 /* general implementation */
2701 VISIT(c, expr, e);
Mark Shannon582aaf12020-08-04 17:30:11 +01002702 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Mark Shannon266b4622020-11-17 19:30:14 +00002703 NEXT_BLOCK(c);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002704 return 1;
2705}
2706
2707static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002708compiler_ifexp(struct compiler *c, expr_ty e)
2709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002710 basicblock *end, *next;
2711
2712 assert(e->kind == IfExp_kind);
2713 end = compiler_new_block(c);
2714 if (end == NULL)
2715 return 0;
2716 next = compiler_new_block(c);
2717 if (next == NULL)
2718 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002719 if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2720 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002721 VISIT(c, expr, e->v.IfExp.body);
Mark Shannon127dde52021-01-04 18:06:55 +00002722 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002723 compiler_use_next_block(c, next);
2724 VISIT(c, expr, e->v.IfExp.orelse);
2725 compiler_use_next_block(c, end);
2726 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002727}
2728
2729static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002730compiler_lambda(struct compiler *c, expr_ty e)
2731{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002732 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002733 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002734 static identifier name;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002735 Py_ssize_t funcflags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002736 arguments_ty args = e->v.Lambda.args;
2737 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002738
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002739 if (!compiler_check_debug_args(c, args))
2740 return 0;
2741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 if (!name) {
2743 name = PyUnicode_InternFromString("<lambda>");
2744 if (!name)
2745 return 0;
2746 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002747
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002748 funcflags = compiler_default_arguments(c, args);
2749 if (funcflags == -1) {
2750 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002752
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002753 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002754 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 /* Make None the first constant, so the lambda can't have a
2758 docstring. */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002759 if (compiler_add_const(c, Py_None) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002763 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002764 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2765 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2766 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002767 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002768 }
2769 else {
2770 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002771 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002773 qualname = c->u->u_qualname;
2774 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002775 compiler_exit_scope(c);
Pablo Galindo7fdab832021-01-29 22:40:59 +00002776 if (co == NULL) {
2777 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002778 return 0;
Pablo Galindo7fdab832021-01-29 22:40:59 +00002779 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002780
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002781 if (!compiler_make_closure(c, co, funcflags, qualname)) {
2782 Py_DECREF(qualname);
2783 Py_DECREF(co);
2784 return 0;
2785 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002786 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002787 Py_DECREF(co);
2788
2789 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002790}
2791
2792static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002793compiler_if(struct compiler *c, stmt_ty s)
2794{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002795 basicblock *end, *next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 assert(s->kind == If_kind);
2797 end = compiler_new_block(c);
Mark Shannon8473cf82020-12-15 11:07:50 +00002798 if (end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 return 0;
Mark Shannon8473cf82020-12-15 11:07:50 +00002800 }
2801 if (asdl_seq_LEN(s->v.If.orelse)) {
2802 next = compiler_new_block(c);
2803 if (next == NULL) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002804 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002805 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002806 }
2807 else {
2808 next = end;
2809 }
2810 if (!compiler_jump_if(c, s->v.If.test, next, 0)) {
2811 return 0;
2812 }
2813 VISIT_SEQ(c, stmt, s->v.If.body);
2814 if (asdl_seq_LEN(s->v.If.orelse)) {
Mark Shannon127dde52021-01-04 18:06:55 +00002815 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Mark Shannon8473cf82020-12-15 11:07:50 +00002816 compiler_use_next_block(c, next);
2817 VISIT_SEQ(c, stmt, s->v.If.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 }
2819 compiler_use_next_block(c, end);
2820 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002821}
2822
2823static int
2824compiler_for(struct compiler *c, stmt_ty s)
2825{
Mark Shannon5977a792020-12-02 13:31:40 +00002826 basicblock *start, *body, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 start = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002829 body = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 cleanup = compiler_new_block(c);
2831 end = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002832 if (start == NULL || body == NULL || end == NULL || cleanup == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002834 }
2835 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002837 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002838 VISIT(c, expr, s->v.For.iter);
2839 ADDOP(c, GET_ITER);
2840 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01002841 ADDOP_JUMP(c, FOR_ITER, cleanup);
Mark Shannon5977a792020-12-02 13:31:40 +00002842 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 VISIT(c, expr, s->v.For.target);
2844 VISIT_SEQ(c, stmt, s->v.For.body);
Mark Shannonf5e97b72020-12-14 11:28:39 +00002845 /* Mark jump as artificial */
2846 c->u->u_lineno = -1;
Mark Shannon582aaf12020-08-04 17:30:11 +01002847 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002848 compiler_use_next_block(c, cleanup);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002849
2850 compiler_pop_fblock(c, FOR_LOOP, start);
2851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 VISIT_SEQ(c, stmt, s->v.For.orelse);
2853 compiler_use_next_block(c, end);
2854 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002855}
2856
Yury Selivanov75445082015-05-11 22:57:16 -04002857
2858static int
2859compiler_async_for(struct compiler *c, stmt_ty s)
2860{
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002861 basicblock *start, *except, *end;
Pablo Galindo90235812020-03-15 04:29:22 +00002862 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07002863 c->u->u_ste->ste_coroutine = 1;
2864 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
Zsolt Dollensteine2396502018-04-27 08:58:56 -07002865 return compiler_error(c, "'async for' outside async function");
2866 }
2867
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002868 start = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002869 except = compiler_new_block(c);
2870 end = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002871
Mark Shannonfee55262019-11-21 09:11:43 +00002872 if (start == NULL || except == NULL || end == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002873 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002874 }
Yury Selivanov75445082015-05-11 22:57:16 -04002875 VISIT(c, expr, s->v.AsyncFor.iter);
2876 ADDOP(c, GET_AITER);
Yury Selivanov75445082015-05-11 22:57:16 -04002877
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002878 compiler_use_next_block(c, start);
Mark Shannonfee55262019-11-21 09:11:43 +00002879 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002880 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002881 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002882 /* SETUP_FINALLY to guard the __anext__ call */
Mark Shannon582aaf12020-08-04 17:30:11 +01002883 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov75445082015-05-11 22:57:16 -04002884 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002885 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04002886 ADDOP(c, YIELD_FROM);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002887 ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */
Yury Selivanov75445082015-05-11 22:57:16 -04002888
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002889 /* Success block for __anext__ */
2890 VISIT(c, expr, s->v.AsyncFor.target);
2891 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002892 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002893
2894 compiler_pop_fblock(c, FOR_LOOP, start);
Yury Selivanov75445082015-05-11 22:57:16 -04002895
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002896 /* Except block for __anext__ */
Yury Selivanov75445082015-05-11 22:57:16 -04002897 compiler_use_next_block(c, except);
Mark Shannon877df852020-11-12 09:43:29 +00002898
2899 c->u->u_lineno = -1;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002900 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov75445082015-05-11 22:57:16 -04002901
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002902 /* `else` block */
Yury Selivanov75445082015-05-11 22:57:16 -04002903 VISIT_SEQ(c, stmt, s->v.For.orelse);
2904
2905 compiler_use_next_block(c, end);
2906
2907 return 1;
2908}
2909
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002910static int
2911compiler_while(struct compiler *c, stmt_ty s)
2912{
Mark Shannon266b4622020-11-17 19:30:14 +00002913 basicblock *loop, *body, *end, *anchor = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002914 loop = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002915 body = compiler_new_block(c);
2916 anchor = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 end = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002918 if (loop == NULL || body == NULL || anchor == NULL || end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002919 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 compiler_use_next_block(c, loop);
Mark Shannon266b4622020-11-17 19:30:14 +00002922 if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002925 if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) {
2926 return 0;
Mark Shannon266b4622020-11-17 19:30:14 +00002927 }
2928
2929 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002930 VISIT_SEQ(c, stmt, s->v.While.body);
Mark Shannon8473cf82020-12-15 11:07:50 +00002931 SET_LOC(c, s);
2932 if (!compiler_jump_if(c, s->v.While.test, body, 1)) {
2933 return 0;
2934 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002935
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002936 compiler_pop_fblock(c, WHILE_LOOP, loop);
2937
Mark Shannon266b4622020-11-17 19:30:14 +00002938 compiler_use_next_block(c, anchor);
2939 if (s->v.While.orelse) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002940 VISIT_SEQ(c, stmt, s->v.While.orelse);
Mark Shannon266b4622020-11-17 19:30:14 +00002941 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002944 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002945}
2946
2947static int
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002948compiler_return(struct compiler *c, stmt_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002949{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002950 int preserve_tos = ((s->v.Return.value != NULL) &&
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002951 (s->v.Return.value->kind != Constant_kind));
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002952 if (c->u->u_ste->ste_type != FunctionBlock)
2953 return compiler_error(c, "'return' outside function");
2954 if (s->v.Return.value != NULL &&
2955 c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2956 {
2957 return compiler_error(
2958 c, "'return' with value in async generator");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002960 if (preserve_tos) {
2961 VISIT(c, expr, s->v.Return.value);
Mark Shannon5274b682020-12-16 13:07:01 +00002962 } else {
2963 /* Emit instruction with line number for expression */
2964 if (s->v.Return.value != NULL) {
2965 SET_LOC(c, s->v.Return.value);
2966 ADDOP(c, NOP);
2967 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002968 }
Mark Shannonfee55262019-11-21 09:11:43 +00002969 if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL))
2970 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002971 if (s->v.Return.value == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002972 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002973 }
2974 else if (!preserve_tos) {
Mark Shannon5274b682020-12-16 13:07:01 +00002975 ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002976 }
2977 ADDOP(c, RETURN_VALUE);
Mark Shannon266b4622020-11-17 19:30:14 +00002978 NEXT_BLOCK(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002980 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002981}
2982
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002983static int
2984compiler_break(struct compiler *c)
2985{
Mark Shannonfee55262019-11-21 09:11:43 +00002986 struct fblockinfo *loop = NULL;
2987 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
2988 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002989 }
Mark Shannonfee55262019-11-21 09:11:43 +00002990 if (loop == NULL) {
2991 return compiler_error(c, "'break' outside loop");
2992 }
2993 if (!compiler_unwind_fblock(c, loop, 0)) {
2994 return 0;
2995 }
Mark Shannon582aaf12020-08-04 17:30:11 +01002996 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit);
Mark Shannon266b4622020-11-17 19:30:14 +00002997 NEXT_BLOCK(c);
Mark Shannonfee55262019-11-21 09:11:43 +00002998 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002999}
3000
3001static int
3002compiler_continue(struct compiler *c)
3003{
Mark Shannonfee55262019-11-21 09:11:43 +00003004 struct fblockinfo *loop = NULL;
3005 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
3006 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003007 }
Mark Shannonfee55262019-11-21 09:11:43 +00003008 if (loop == NULL) {
3009 return compiler_error(c, "'continue' not properly in loop");
3010 }
Mark Shannon582aaf12020-08-04 17:30:11 +01003011 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block);
Mark Shannon266b4622020-11-17 19:30:14 +00003012 NEXT_BLOCK(c)
Mark Shannonfee55262019-11-21 09:11:43 +00003013 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003014}
3015
3016
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003017/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018
3019 SETUP_FINALLY L
3020 <code for body>
3021 POP_BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00003022 <code for finalbody>
3023 JUMP E
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003024 L:
3025 <code for finalbody>
Mark Shannonfee55262019-11-21 09:11:43 +00003026 E:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003027
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003028 The special instructions use the block stack. Each block
3029 stack entry contains the instruction that created it (here
3030 SETUP_FINALLY), the level of the value stack at the time the
3031 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003033 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003034 Pushes the current value stack level and the label
3035 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003036 POP_BLOCK:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003037 Pops en entry from the block stack.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003038
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003039 The block stack is unwound when an exception is raised:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003040 when a SETUP_FINALLY entry is found, the raised and the caught
3041 exceptions are pushed onto the value stack (and the exception
3042 condition is cleared), and the interpreter jumps to the label
3043 gotten from the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003044*/
3045
3046static int
3047compiler_try_finally(struct compiler *c, stmt_ty s)
3048{
Mark Shannonfee55262019-11-21 09:11:43 +00003049 basicblock *body, *end, *exit;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003051 body = compiler_new_block(c);
3052 end = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00003053 exit = compiler_new_block(c);
3054 if (body == NULL || end == NULL || exit == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003056
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003057 /* `try` block */
Mark Shannon582aaf12020-08-04 17:30:11 +01003058 ADDOP_JUMP(c, SETUP_FINALLY, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 compiler_use_next_block(c, body);
Mark Shannonfee55262019-11-21 09:11:43 +00003060 if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003062 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
3063 if (!compiler_try_except(c, s))
3064 return 0;
3065 }
3066 else {
3067 VISIT_SEQ(c, stmt, s->v.Try.body);
3068 }
Mark Shannon3bd60352021-01-13 12:05:43 +00003069 ADDOP_NOLINE(c, POP_BLOCK);
Mark Shannonfee55262019-11-21 09:11:43 +00003070 compiler_pop_fblock(c, FINALLY_TRY, body);
3071 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Mark Shannon127dde52021-01-04 18:06:55 +00003072 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00003073 /* `finally` block */
3074 compiler_use_next_block(c, end);
3075 if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL))
3076 return 0;
3077 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3078 compiler_pop_fblock(c, FINALLY_END, end);
Mark Shannonbf353f32020-12-17 13:55:28 +00003079 ADDOP_I(c, RERAISE, 0);
Mark Shannonfee55262019-11-21 09:11:43 +00003080 compiler_use_next_block(c, exit);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003082}
3083
3084/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003085 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003086 (The contents of the value stack is shown in [], with the top
3087 at the right; 'tb' is trace-back info, 'val' the exception's
3088 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089
3090 Value stack Label Instruction Argument
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003091 [] SETUP_FINALLY L1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003092 [] <code for S>
3093 [] POP_BLOCK
3094 [] JUMP_FORWARD L0
3095
3096 [tb, val, exc] L1: DUP )
3097 [tb, val, exc, exc] <evaluate E1> )
Mark Shannon9af0e472020-01-14 10:12:45 +00003098 [tb, val, exc, exc, E1] JUMP_IF_NOT_EXC_MATCH L2 ) only if E1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003099 [tb, val, exc] POP
3100 [tb, val] <assign to V1> (or POP if no V1)
3101 [tb] POP
3102 [] <code for S1>
3103 JUMP_FORWARD L0
3104
3105 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003106 .............................etc.......................
3107
Mark Shannonfee55262019-11-21 09:11:43 +00003108 [tb, val, exc] Ln+1: RERAISE # re-raise exception
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003109
3110 [] L0: <next statement>
3111
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003112 Of course, parts are not generated if Vi or Ei is not present.
3113*/
3114static int
3115compiler_try_except(struct compiler *c, stmt_ty s)
3116{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003117 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003118 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003120 body = compiler_new_block(c);
3121 except = compiler_new_block(c);
3122 orelse = compiler_new_block(c);
3123 end = compiler_new_block(c);
3124 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
3125 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01003126 ADDOP_JUMP(c, SETUP_FINALLY, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003127 compiler_use_next_block(c, body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003128 if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003130 VISIT_SEQ(c, stmt, s->v.Try.body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003131 compiler_pop_fblock(c, TRY_EXCEPT, body);
Mark Shannon3bd60352021-01-13 12:05:43 +00003132 ADDOP_NOLINE(c, POP_BLOCK);
3133 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003134 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003135 compiler_use_next_block(c, except);
Mark Shannon02d126a2020-09-25 14:04:19 +01003136 /* Runtime will push a block here, so we need to account for that */
3137 if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL))
3138 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 for (i = 0; i < n; i++) {
3140 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003141 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003142 if (!handler->v.ExceptHandler.type && i < n-1)
3143 return compiler_error(c, "default 'except:' must be last");
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003144 SET_LOC(c, handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145 except = compiler_new_block(c);
3146 if (except == NULL)
3147 return 0;
3148 if (handler->v.ExceptHandler.type) {
3149 ADDOP(c, DUP_TOP);
3150 VISIT(c, expr, handler->v.ExceptHandler.type);
Mark Shannon582aaf12020-08-04 17:30:11 +01003151 ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except);
Mark Shannon266b4622020-11-17 19:30:14 +00003152 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003153 }
3154 ADDOP(c, POP_TOP);
3155 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003156 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00003157
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003158 cleanup_end = compiler_new_block(c);
3159 cleanup_body = compiler_new_block(c);
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003160 if (cleanup_end == NULL || cleanup_body == NULL) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003161 return 0;
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003162 }
Guido van Rossumb940e112007-01-10 16:19:56 +00003163
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003164 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3165 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003166
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003167 /*
3168 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003169 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003170 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003171 try:
3172 # body
3173 finally:
Chris Angelicoad098b62019-05-21 23:34:19 +10003174 name = None # in case body contains "del name"
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003175 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003176 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003177
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003178 /* second try: */
Mark Shannon582aaf12020-08-04 17:30:11 +01003179 ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003180 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003181 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003182 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003183
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003184 /* second # body */
3185 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003186 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003187 ADDOP(c, POP_BLOCK);
3188 ADDOP(c, POP_EXCEPT);
Mark Shannon877df852020-11-12 09:43:29 +00003189 /* name = None; del name; # Mark as artificial */
3190 c->u->u_lineno = -1;
Mark Shannonfee55262019-11-21 09:11:43 +00003191 ADDOP_LOAD_CONST(c, Py_None);
3192 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3193 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Mark Shannon582aaf12020-08-04 17:30:11 +01003194 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003195
Mark Shannonfee55262019-11-21 09:11:43 +00003196 /* except: */
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003197 compiler_use_next_block(c, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198
Mark Shannon877df852020-11-12 09:43:29 +00003199 /* name = None; del name; # Mark as artificial */
3200 c->u->u_lineno = -1;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003201 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003202 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003203 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003204
Mark Shannonbf353f32020-12-17 13:55:28 +00003205 ADDOP_I(c, RERAISE, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003206 }
3207 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003208 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003209
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003210 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05003211 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003212 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003213
Guido van Rossumb940e112007-01-10 16:19:56 +00003214 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003215 ADDOP(c, POP_TOP);
3216 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003217 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003218 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003220 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannon127dde52021-01-04 18:06:55 +00003221 /* name = None; del name; # Mark as artificial */
3222 c->u->u_lineno = -1;
Mark Shannonfee55262019-11-21 09:11:43 +00003223 ADDOP(c, POP_EXCEPT);
Mark Shannon582aaf12020-08-04 17:30:11 +01003224 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003226 compiler_use_next_block(c, except);
3227 }
Mark Shannon02d126a2020-09-25 14:04:19 +01003228 compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL);
Mark Shannonf2dbfd72020-12-21 13:53:50 +00003229 /* Mark as artificial */
3230 c->u->u_lineno = -1;
Mark Shannonbf353f32020-12-17 13:55:28 +00003231 ADDOP_I(c, RERAISE, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003232 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003233 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003234 compiler_use_next_block(c, end);
3235 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003236}
3237
3238static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003239compiler_try(struct compiler *c, stmt_ty s) {
3240 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
3241 return compiler_try_finally(c, s);
3242 else
3243 return compiler_try_except(c, s);
3244}
3245
3246
3247static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003248compiler_import_as(struct compiler *c, identifier name, identifier asname)
3249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250 /* The IMPORT_NAME opcode was already generated. This function
3251 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003253 If there is a dot in name, we need to split it and emit a
Serhiy Storchakaf93234b2017-05-09 22:31:05 +03003254 IMPORT_FROM for each name.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003255 */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003256 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
3257 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003258 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003259 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003260 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 /* Consume the base module name to get the first attribute */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003262 while (1) {
3263 Py_ssize_t pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003264 PyObject *attr;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003265 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003266 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003267 return 0;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003268 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003269 if (!attr)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003270 return 0;
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003271 ADDOP_N(c, IMPORT_FROM, attr, names);
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003272 if (dot == -1) {
3273 break;
3274 }
3275 ADDOP(c, ROT_TWO);
3276 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003277 }
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003278 if (!compiler_nameop(c, asname, Store)) {
3279 return 0;
3280 }
3281 ADDOP(c, POP_TOP);
3282 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003283 }
3284 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003285}
3286
3287static int
3288compiler_import(struct compiler *c, stmt_ty s)
3289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290 /* The Import node stores a module name like a.b.c as a single
3291 string. This is convenient for all cases except
3292 import a.b.c as d
3293 where we need to parse that string to extract the individual
3294 module names.
3295 XXX Perhaps change the representation to make this case simpler?
3296 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01003297 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003298
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003299 PyObject *zero = _PyLong_GetZero(); // borrowed reference
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003300 for (i = 0; i < n; i++) {
3301 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
3302 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003303
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003304 ADDOP_LOAD_CONST(c, zero);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003305 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003306 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003308 if (alias->asname) {
3309 r = compiler_import_as(c, alias->name, alias->asname);
3310 if (!r)
3311 return r;
3312 }
3313 else {
3314 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003315 Py_ssize_t dot = PyUnicode_FindChar(
3316 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02003317 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003318 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02003319 if (tmp == NULL)
3320 return 0;
3321 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003322 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003323 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 Py_DECREF(tmp);
3325 }
3326 if (!r)
3327 return r;
3328 }
3329 }
3330 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003331}
3332
3333static int
3334compiler_from_import(struct compiler *c, stmt_ty s)
3335{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003336 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003337 PyObject *names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003338 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003340 if (!empty_string) {
3341 empty_string = PyUnicode_FromString("");
3342 if (!empty_string)
3343 return 0;
3344 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003345
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003346 ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02003347
3348 names = PyTuple_New(n);
3349 if (!names)
3350 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003352 /* build up the names */
3353 for (i = 0; i < n; i++) {
3354 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3355 Py_INCREF(alias->name);
3356 PyTuple_SET_ITEM(names, i, alias->name);
3357 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003359 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003360 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003361 Py_DECREF(names);
3362 return compiler_error(c, "from __future__ imports must occur "
3363 "at the beginning of the file");
3364 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003365 ADDOP_LOAD_CONST_NEW(c, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003367 if (s->v.ImportFrom.module) {
3368 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
3369 }
3370 else {
3371 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
3372 }
3373 for (i = 0; i < n; i++) {
3374 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3375 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003376
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003377 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003378 assert(n == 1);
3379 ADDOP(c, IMPORT_STAR);
3380 return 1;
3381 }
3382
3383 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
3384 store_name = alias->name;
3385 if (alias->asname)
3386 store_name = alias->asname;
3387
3388 if (!compiler_nameop(c, store_name, Store)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003389 return 0;
3390 }
3391 }
3392 /* remove imported module */
3393 ADDOP(c, POP_TOP);
3394 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003395}
3396
3397static int
3398compiler_assert(struct compiler *c, stmt_ty s)
3399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003400 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003401
tsukasa-aua8ef4572021-03-16 22:14:41 +11003402 /* Always emit a warning if the test is a non-zero length tuple */
3403 if ((s->v.Assert.test->kind == Tuple_kind &&
3404 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) ||
3405 (s->v.Assert.test->kind == Constant_kind &&
3406 PyTuple_Check(s->v.Assert.test->v.Constant.value) &&
3407 PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0))
Serhiy Storchakad31e7732018-10-21 10:09:39 +03003408 {
3409 if (!compiler_warn(c, "assertion is always true, "
3410 "perhaps remove parentheses?"))
3411 {
Victor Stinner14e461d2013-08-26 22:28:21 +02003412 return 0;
3413 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003414 }
tsukasa-aua8ef4572021-03-16 22:14:41 +11003415 if (c->c_optimize)
3416 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003417 end = compiler_new_block(c);
3418 if (end == NULL)
3419 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003420 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
3421 return 0;
Zackery Spytzce6a0702019-08-25 03:44:09 -06003422 ADDOP(c, LOAD_ASSERTION_ERROR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003423 if (s->v.Assert.msg) {
3424 VISIT(c, expr, s->v.Assert.msg);
3425 ADDOP_I(c, CALL_FUNCTION, 1);
3426 }
3427 ADDOP_I(c, RAISE_VARARGS, 1);
3428 compiler_use_next_block(c, end);
3429 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003430}
3431
3432static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003433compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3434{
3435 if (c->c_interactive && c->c_nestlevel <= 1) {
3436 VISIT(c, expr, value);
3437 ADDOP(c, PRINT_EXPR);
3438 return 1;
3439 }
3440
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003441 if (value->kind == Constant_kind) {
Victor Stinner15a30952016-02-08 22:45:06 +01003442 /* ignore constant statement */
Mark Shannon877df852020-11-12 09:43:29 +00003443 ADDOP(c, NOP);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003444 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003445 }
3446
3447 VISIT(c, expr, value);
Mark Shannonc5440932021-03-15 14:24:25 +00003448 /* Mark POP_TOP as artificial */
3449 c->u->u_lineno = -1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003450 ADDOP(c, POP_TOP);
3451 return 1;
3452}
3453
3454static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003455compiler_visit_stmt(struct compiler *c, stmt_ty s)
3456{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003457 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003459 /* Always assign a lineno to the next instruction for a stmt. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003460 SET_LOC(c, s);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003462 switch (s->kind) {
3463 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04003464 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003465 case ClassDef_kind:
3466 return compiler_class(c, s);
3467 case Return_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003468 return compiler_return(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003469 case Delete_kind:
3470 VISIT_SEQ(c, expr, s->v.Delete.targets)
3471 break;
3472 case Assign_kind:
3473 n = asdl_seq_LEN(s->v.Assign.targets);
3474 VISIT(c, expr, s->v.Assign.value);
3475 for (i = 0; i < n; i++) {
3476 if (i < n - 1)
3477 ADDOP(c, DUP_TOP);
3478 VISIT(c, expr,
3479 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3480 }
3481 break;
3482 case AugAssign_kind:
3483 return compiler_augassign(c, s);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003484 case AnnAssign_kind:
3485 return compiler_annassign(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003486 case For_kind:
3487 return compiler_for(c, s);
3488 case While_kind:
3489 return compiler_while(c, s);
3490 case If_kind:
3491 return compiler_if(c, s);
Brandt Bucher145bf262021-02-26 14:51:55 -08003492 case Match_kind:
3493 return compiler_match(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003494 case Raise_kind:
3495 n = 0;
3496 if (s->v.Raise.exc) {
3497 VISIT(c, expr, s->v.Raise.exc);
3498 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003499 if (s->v.Raise.cause) {
3500 VISIT(c, expr, s->v.Raise.cause);
3501 n++;
3502 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01003504 ADDOP_I(c, RAISE_VARARGS, (int)n);
Mark Shannon266b4622020-11-17 19:30:14 +00003505 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003506 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003507 case Try_kind:
3508 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003509 case Assert_kind:
3510 return compiler_assert(c, s);
3511 case Import_kind:
3512 return compiler_import(c, s);
3513 case ImportFrom_kind:
3514 return compiler_from_import(c, s);
3515 case Global_kind:
3516 case Nonlocal_kind:
3517 break;
3518 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003519 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003520 case Pass_kind:
Mark Shannon877df852020-11-12 09:43:29 +00003521 ADDOP(c, NOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003522 break;
3523 case Break_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003524 return compiler_break(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003525 case Continue_kind:
3526 return compiler_continue(c);
3527 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003528 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04003529 case AsyncFunctionDef_kind:
3530 return compiler_function(c, s, 1);
3531 case AsyncWith_kind:
3532 return compiler_async_with(c, s, 0);
3533 case AsyncFor_kind:
3534 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003535 }
Yury Selivanov75445082015-05-11 22:57:16 -04003536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003537 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003538}
3539
3540static int
3541unaryop(unaryop_ty op)
3542{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003543 switch (op) {
3544 case Invert:
3545 return UNARY_INVERT;
3546 case Not:
3547 return UNARY_NOT;
3548 case UAdd:
3549 return UNARY_POSITIVE;
3550 case USub:
3551 return UNARY_NEGATIVE;
3552 default:
3553 PyErr_Format(PyExc_SystemError,
3554 "unary op %d should not be possible", op);
3555 return 0;
3556 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003557}
3558
3559static int
Andy Lester76d58772020-03-10 21:18:12 -05003560binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003562 switch (op) {
3563 case Add:
3564 return BINARY_ADD;
3565 case Sub:
3566 return BINARY_SUBTRACT;
3567 case Mult:
3568 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003569 case MatMult:
3570 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003571 case Div:
3572 return BINARY_TRUE_DIVIDE;
3573 case Mod:
3574 return BINARY_MODULO;
3575 case Pow:
3576 return BINARY_POWER;
3577 case LShift:
3578 return BINARY_LSHIFT;
3579 case RShift:
3580 return BINARY_RSHIFT;
3581 case BitOr:
3582 return BINARY_OR;
3583 case BitXor:
3584 return BINARY_XOR;
3585 case BitAnd:
3586 return BINARY_AND;
3587 case FloorDiv:
3588 return BINARY_FLOOR_DIVIDE;
3589 default:
3590 PyErr_Format(PyExc_SystemError,
3591 "binary op %d should not be possible", op);
3592 return 0;
3593 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003594}
3595
3596static int
Andy Lester76d58772020-03-10 21:18:12 -05003597inplace_binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003598{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003599 switch (op) {
3600 case Add:
3601 return INPLACE_ADD;
3602 case Sub:
3603 return INPLACE_SUBTRACT;
3604 case Mult:
3605 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003606 case MatMult:
3607 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 case Div:
3609 return INPLACE_TRUE_DIVIDE;
3610 case Mod:
3611 return INPLACE_MODULO;
3612 case Pow:
3613 return INPLACE_POWER;
3614 case LShift:
3615 return INPLACE_LSHIFT;
3616 case RShift:
3617 return INPLACE_RSHIFT;
3618 case BitOr:
3619 return INPLACE_OR;
3620 case BitXor:
3621 return INPLACE_XOR;
3622 case BitAnd:
3623 return INPLACE_AND;
3624 case FloorDiv:
3625 return INPLACE_FLOOR_DIVIDE;
3626 default:
3627 PyErr_Format(PyExc_SystemError,
3628 "inplace binary op %d should not be possible", op);
3629 return 0;
3630 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003631}
3632
3633static int
3634compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3635{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003636 int op, scope;
3637 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003638 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003640 PyObject *dict = c->u->u_names;
3641 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003642
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003643 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3644 !_PyUnicode_EqualToASCIIString(name, "True") &&
3645 !_PyUnicode_EqualToASCIIString(name, "False"));
Benjamin Peterson70b224d2012-12-06 17:49:58 -05003646
Pablo Galindoc5fc1562020-04-22 23:29:27 +01003647 if (forbidden_name(c, name, ctx))
3648 return 0;
3649
Serhiy Storchakabd6ec4d2017-12-18 14:29:12 +02003650 mangled = _Py_Mangle(c->u->u_private, name);
3651 if (!mangled)
3652 return 0;
3653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003654 op = 0;
3655 optype = OP_NAME;
Victor Stinner28ad12f2021-03-19 12:41:49 +01003656 scope = _PyST_GetScope(c->u->u_ste, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003657 switch (scope) {
3658 case FREE:
3659 dict = c->u->u_freevars;
3660 optype = OP_DEREF;
3661 break;
3662 case CELL:
3663 dict = c->u->u_cellvars;
3664 optype = OP_DEREF;
3665 break;
3666 case LOCAL:
3667 if (c->u->u_ste->ste_type == FunctionBlock)
3668 optype = OP_FAST;
3669 break;
3670 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04003671 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003672 optype = OP_GLOBAL;
3673 break;
3674 case GLOBAL_EXPLICIT:
3675 optype = OP_GLOBAL;
3676 break;
3677 default:
3678 /* scope can be 0 */
3679 break;
3680 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003682 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003683 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003685 switch (optype) {
3686 case OP_DEREF:
3687 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003688 case Load:
3689 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3690 break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003691 case Store: op = STORE_DEREF; break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003692 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003693 }
3694 break;
3695 case OP_FAST:
3696 switch (ctx) {
3697 case Load: op = LOAD_FAST; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003698 case Store: op = STORE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003699 case Del: op = DELETE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003700 }
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003701 ADDOP_N(c, op, mangled, varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003702 return 1;
3703 case OP_GLOBAL:
3704 switch (ctx) {
3705 case Load: op = LOAD_GLOBAL; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003706 case Store: op = STORE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003707 case Del: op = DELETE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003708 }
3709 break;
3710 case OP_NAME:
3711 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00003712 case Load: op = LOAD_NAME; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003713 case Store: op = STORE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003714 case Del: op = DELETE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003715 }
3716 break;
3717 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003719 assert(op);
Andy Lester76d58772020-03-10 21:18:12 -05003720 arg = compiler_add_o(dict, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003721 Py_DECREF(mangled);
3722 if (arg < 0)
3723 return 0;
3724 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003725}
3726
3727static int
3728compiler_boolop(struct compiler *c, expr_ty e)
3729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003731 int jumpi;
3732 Py_ssize_t i, n;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003733 asdl_expr_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003735 assert(e->kind == BoolOp_kind);
3736 if (e->v.BoolOp.op == And)
3737 jumpi = JUMP_IF_FALSE_OR_POP;
3738 else
3739 jumpi = JUMP_IF_TRUE_OR_POP;
3740 end = compiler_new_block(c);
3741 if (end == NULL)
3742 return 0;
3743 s = e->v.BoolOp.values;
3744 n = asdl_seq_LEN(s) - 1;
3745 assert(n >= 0);
3746 for (i = 0; i < n; ++i) {
3747 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003748 ADDOP_JUMP(c, jumpi, end);
Mark Shannon6e8128f2020-07-30 10:03:00 +01003749 basicblock *next = compiler_new_block(c);
3750 if (next == NULL) {
3751 return 0;
3752 }
3753 compiler_use_next_block(c, next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003754 }
3755 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3756 compiler_use_next_block(c, end);
3757 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003758}
3759
3760static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003761starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
Mark Shannon13bc1392020-01-23 09:25:17 +00003762 int build, int add, int extend, int tuple)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003763{
3764 Py_ssize_t n = asdl_seq_LEN(elts);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003765 if (n > 2 && are_all_items_const(elts, 0, n)) {
3766 PyObject *folded = PyTuple_New(n);
3767 if (folded == NULL) {
3768 return 0;
3769 }
3770 PyObject *val;
Mark Shannon11e0b292021-04-15 14:28:56 +01003771 for (Py_ssize_t i = 0; i < n; i++) {
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003772 val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
3773 Py_INCREF(val);
3774 PyTuple_SET_ITEM(folded, i, val);
3775 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003776 if (tuple) {
3777 ADDOP_LOAD_CONST_NEW(c, folded);
3778 } else {
3779 if (add == SET_ADD) {
3780 Py_SETREF(folded, PyFrozenSet_New(folded));
3781 if (folded == NULL) {
3782 return 0;
3783 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003784 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003785 ADDOP_I(c, build, pushed);
3786 ADDOP_LOAD_CONST_NEW(c, folded);
3787 ADDOP_I(c, extend, 1);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003788 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003789 return 1;
3790 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003791
Mark Shannon11e0b292021-04-15 14:28:56 +01003792 int big = n+pushed > STACK_USE_GUIDELINE;
3793 int seen_star = 0;
3794 for (Py_ssize_t i = 0; i < n; i++) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003795 expr_ty elt = asdl_seq_GET(elts, i);
3796 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003797 seen_star = 1;
3798 }
3799 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003800 if (!seen_star && !big) {
3801 for (Py_ssize_t i = 0; i < n; i++) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003802 expr_ty elt = asdl_seq_GET(elts, i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003803 VISIT(c, expr, elt);
Mark Shannon13bc1392020-01-23 09:25:17 +00003804 }
3805 if (tuple) {
3806 ADDOP_I(c, BUILD_TUPLE, n+pushed);
3807 } else {
3808 ADDOP_I(c, build, n+pushed);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003809 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003810 return 1;
3811 }
3812 int sequence_built = 0;
3813 if (big) {
3814 ADDOP_I(c, build, pushed);
3815 sequence_built = 1;
3816 }
3817 for (Py_ssize_t i = 0; i < n; i++) {
3818 expr_ty elt = asdl_seq_GET(elts, i);
3819 if (elt->kind == Starred_kind) {
3820 if (sequence_built == 0) {
3821 ADDOP_I(c, build, i+pushed);
3822 sequence_built = 1;
3823 }
3824 VISIT(c, expr, elt->v.Starred.value);
3825 ADDOP_I(c, extend, 1);
3826 }
3827 else {
3828 VISIT(c, expr, elt);
3829 if (sequence_built) {
3830 ADDOP_I(c, add, 1);
3831 }
3832 }
3833 }
3834 assert(sequence_built);
3835 if (tuple) {
3836 ADDOP(c, LIST_TO_TUPLE);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003837 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003838 return 1;
3839}
3840
3841static int
Brandt Bucher145bf262021-02-26 14:51:55 -08003842unpack_helper(struct compiler *c, asdl_expr_seq *elts)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003843{
3844 Py_ssize_t n = asdl_seq_LEN(elts);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003845 int seen_star = 0;
Brandt Bucher145bf262021-02-26 14:51:55 -08003846 for (Py_ssize_t i = 0; i < n; i++) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003847 expr_ty elt = asdl_seq_GET(elts, i);
3848 if (elt->kind == Starred_kind && !seen_star) {
3849 if ((i >= (1 << 8)) ||
3850 (n-i-1 >= (INT_MAX >> 8)))
3851 return compiler_error(c,
3852 "too many expressions in "
3853 "star-unpacking assignment");
3854 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3855 seen_star = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003856 }
3857 else if (elt->kind == Starred_kind) {
3858 return compiler_error(c,
Furkan Öndercb6534e2020-03-26 04:54:31 +03003859 "multiple starred expressions in assignment");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003860 }
3861 }
3862 if (!seen_star) {
3863 ADDOP_I(c, UNPACK_SEQUENCE, n);
3864 }
Brandt Bucher145bf262021-02-26 14:51:55 -08003865 return 1;
3866}
3867
3868static int
3869assignment_helper(struct compiler *c, asdl_expr_seq *elts)
3870{
3871 Py_ssize_t n = asdl_seq_LEN(elts);
3872 RETURN_IF_FALSE(unpack_helper(c, elts));
3873 for (Py_ssize_t i = 0; i < n; i++) {
Brandt Bucherd5aa2e92020-03-07 19:44:18 -08003874 expr_ty elt = asdl_seq_GET(elts, i);
3875 VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value);
3876 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003877 return 1;
3878}
3879
3880static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003881compiler_list(struct compiler *c, expr_ty e)
3882{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003883 asdl_expr_seq *elts = e->v.List.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003884 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003885 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003886 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003887 else if (e->v.List.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003888 return starunpack_helper(c, elts, 0, BUILD_LIST,
3889 LIST_APPEND, LIST_EXTEND, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003890 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003891 else
3892 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003893 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003894}
3895
3896static int
3897compiler_tuple(struct compiler *c, expr_ty e)
3898{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003899 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003900 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003901 return assignment_helper(c, elts);
3902 }
3903 else if (e->v.Tuple.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003904 return starunpack_helper(c, elts, 0, BUILD_LIST,
3905 LIST_APPEND, LIST_EXTEND, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003906 }
3907 else
3908 VISIT_SEQ(c, expr, elts);
3909 return 1;
3910}
3911
3912static int
3913compiler_set(struct compiler *c, expr_ty e)
3914{
Mark Shannon13bc1392020-01-23 09:25:17 +00003915 return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET,
3916 SET_ADD, SET_UPDATE, 0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003917}
3918
3919static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003920are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003921{
3922 Py_ssize_t i;
3923 for (i = begin; i < end; i++) {
3924 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003925 if (key == NULL || key->kind != Constant_kind)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003926 return 0;
3927 }
3928 return 1;
3929}
3930
3931static int
3932compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3933{
3934 Py_ssize_t i, n = end - begin;
3935 PyObject *keys, *key;
Mark Shannon11e0b292021-04-15 14:28:56 +01003936 int big = n*2 > STACK_USE_GUIDELINE;
3937 if (n > 1 && !big && are_all_items_const(e->v.Dict.keys, begin, end)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003938 for (i = begin; i < end; i++) {
3939 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3940 }
3941 keys = PyTuple_New(n);
3942 if (keys == NULL) {
3943 return 0;
3944 }
3945 for (i = begin; i < end; i++) {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003946 key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003947 Py_INCREF(key);
3948 PyTuple_SET_ITEM(keys, i - begin, key);
3949 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003950 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003951 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
Mark Shannon11e0b292021-04-15 14:28:56 +01003952 return 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003953 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003954 if (big) {
3955 ADDOP_I(c, BUILD_MAP, 0);
3956 }
3957 for (i = begin; i < end; i++) {
3958 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3959 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3960 if (big) {
3961 ADDOP_I(c, MAP_ADD, 1);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003962 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003963 }
3964 if (!big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003965 ADDOP_I(c, BUILD_MAP, n);
3966 }
3967 return 1;
3968}
3969
3970static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003971compiler_dict(struct compiler *c, expr_ty e)
3972{
Victor Stinner976bb402016-03-23 11:36:19 +01003973 Py_ssize_t i, n, elements;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003974 int have_dict;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003975 int is_unpacking = 0;
3976 n = asdl_seq_LEN(e->v.Dict.values);
Mark Shannon8a4cd702020-01-27 09:57:45 +00003977 have_dict = 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003978 elements = 0;
3979 for (i = 0; i < n; i++) {
3980 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003981 if (is_unpacking) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003982 if (elements) {
3983 if (!compiler_subdict(c, e, i - elements, i)) {
3984 return 0;
3985 }
3986 if (have_dict) {
3987 ADDOP_I(c, DICT_UPDATE, 1);
3988 }
3989 have_dict = 1;
3990 elements = 0;
3991 }
3992 if (have_dict == 0) {
3993 ADDOP_I(c, BUILD_MAP, 0);
3994 have_dict = 1;
3995 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003996 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Mark Shannon8a4cd702020-01-27 09:57:45 +00003997 ADDOP_I(c, DICT_UPDATE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003998 }
3999 else {
Mark Shannon11e0b292021-04-15 14:28:56 +01004000 if (elements*2 > STACK_USE_GUIDELINE) {
Pablo Galindoc51db0e2020-08-13 09:48:41 +01004001 if (!compiler_subdict(c, e, i - elements, i + 1)) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004002 return 0;
4003 }
4004 if (have_dict) {
4005 ADDOP_I(c, DICT_UPDATE, 1);
4006 }
4007 have_dict = 1;
4008 elements = 0;
4009 }
4010 else {
4011 elements++;
4012 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004013 }
4014 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004015 if (elements) {
4016 if (!compiler_subdict(c, e, n - elements, n)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004017 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004018 }
4019 if (have_dict) {
4020 ADDOP_I(c, DICT_UPDATE, 1);
4021 }
4022 have_dict = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004023 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004024 if (!have_dict) {
4025 ADDOP_I(c, BUILD_MAP, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004026 }
4027 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004028}
4029
4030static int
4031compiler_compare(struct compiler *c, expr_ty e)
4032{
Victor Stinnerad9a0662013-11-19 22:23:20 +01004033 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004034
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02004035 if (!check_compare(c, e)) {
4036 return 0;
4037 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004038 VISIT(c, expr, e->v.Compare.left);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004039 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
4040 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
4041 if (n == 0) {
4042 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Mark Shannon9af0e472020-01-14 10:12:45 +00004043 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004044 }
4045 else {
4046 basicblock *cleanup = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004047 if (cleanup == NULL)
4048 return 0;
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004049 for (i = 0; i < n; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004050 VISIT(c, expr,
4051 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004052 ADDOP(c, DUP_TOP);
4053 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00004054 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01004055 ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004056 NEXT_BLOCK(c);
4057 }
4058 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00004059 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004060 basicblock *end = compiler_new_block(c);
4061 if (end == NULL)
4062 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00004063 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004064 compiler_use_next_block(c, cleanup);
4065 ADDOP(c, ROT_TWO);
4066 ADDOP(c, POP_TOP);
4067 compiler_use_next_block(c, end);
4068 }
4069 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004070}
4071
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004072static PyTypeObject *
4073infer_type(expr_ty e)
4074{
4075 switch (e->kind) {
4076 case Tuple_kind:
4077 return &PyTuple_Type;
4078 case List_kind:
4079 case ListComp_kind:
4080 return &PyList_Type;
4081 case Dict_kind:
4082 case DictComp_kind:
4083 return &PyDict_Type;
4084 case Set_kind:
4085 case SetComp_kind:
4086 return &PySet_Type;
4087 case GeneratorExp_kind:
4088 return &PyGen_Type;
4089 case Lambda_kind:
4090 return &PyFunction_Type;
4091 case JoinedStr_kind:
4092 case FormattedValue_kind:
4093 return &PyUnicode_Type;
4094 case Constant_kind:
Victor Stinnera102ed72020-02-07 02:24:48 +01004095 return Py_TYPE(e->v.Constant.value);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004096 default:
4097 return NULL;
4098 }
4099}
4100
4101static int
4102check_caller(struct compiler *c, expr_ty e)
4103{
4104 switch (e->kind) {
4105 case Constant_kind:
4106 case Tuple_kind:
4107 case List_kind:
4108 case ListComp_kind:
4109 case Dict_kind:
4110 case DictComp_kind:
4111 case Set_kind:
4112 case SetComp_kind:
4113 case GeneratorExp_kind:
4114 case JoinedStr_kind:
4115 case FormattedValue_kind:
4116 return compiler_warn(c, "'%.200s' object is not callable; "
4117 "perhaps you missed a comma?",
4118 infer_type(e)->tp_name);
4119 default:
4120 return 1;
4121 }
4122}
4123
4124static int
4125check_subscripter(struct compiler *c, expr_ty e)
4126{
4127 PyObject *v;
4128
4129 switch (e->kind) {
4130 case Constant_kind:
4131 v = e->v.Constant.value;
4132 if (!(v == Py_None || v == Py_Ellipsis ||
4133 PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
4134 PyAnySet_Check(v)))
4135 {
4136 return 1;
4137 }
4138 /* fall through */
4139 case Set_kind:
4140 case SetComp_kind:
4141 case GeneratorExp_kind:
4142 case Lambda_kind:
4143 return compiler_warn(c, "'%.200s' object is not subscriptable; "
4144 "perhaps you missed a comma?",
4145 infer_type(e)->tp_name);
4146 default:
4147 return 1;
4148 }
4149}
4150
4151static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004152check_index(struct compiler *c, expr_ty e, expr_ty s)
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004153{
4154 PyObject *v;
4155
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004156 PyTypeObject *index_type = infer_type(s);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004157 if (index_type == NULL
4158 || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
4159 || index_type == &PySlice_Type) {
4160 return 1;
4161 }
4162
4163 switch (e->kind) {
4164 case Constant_kind:
4165 v = e->v.Constant.value;
4166 if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
4167 return 1;
4168 }
4169 /* fall through */
4170 case Tuple_kind:
4171 case List_kind:
4172 case ListComp_kind:
4173 case JoinedStr_kind:
4174 case FormattedValue_kind:
4175 return compiler_warn(c, "%.200s indices must be integers or slices, "
4176 "not %.200s; "
4177 "perhaps you missed a comma?",
4178 infer_type(e)->tp_name,
4179 index_type->tp_name);
4180 default:
4181 return 1;
4182 }
4183}
4184
Zackery Spytz97f5de02019-03-22 01:30:32 -06004185// Return 1 if the method call was optimized, -1 if not, and 0 on error.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004186static int
Yury Selivanovf2392132016-12-13 19:03:51 -05004187maybe_optimize_method_call(struct compiler *c, expr_ty e)
4188{
4189 Py_ssize_t argsl, i;
4190 expr_ty meth = e->v.Call.func;
Pablo Galindoa5634c42020-09-16 19:42:00 +01004191 asdl_expr_seq *args = e->v.Call.args;
Yury Selivanovf2392132016-12-13 19:03:51 -05004192
4193 /* Check that the call node is an attribute access, and that
4194 the call doesn't have keyword parameters. */
4195 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
Mark Shannon11e0b292021-04-15 14:28:56 +01004196 asdl_seq_LEN(e->v.Call.keywords)) {
Yury Selivanovf2392132016-12-13 19:03:51 -05004197 return -1;
Mark Shannon11e0b292021-04-15 14:28:56 +01004198 }
4199 /* Check that there aren't too many arguments */
Yury Selivanovf2392132016-12-13 19:03:51 -05004200 argsl = asdl_seq_LEN(args);
Mark Shannon11e0b292021-04-15 14:28:56 +01004201 if (argsl >= STACK_USE_GUIDELINE) {
4202 return -1;
4203 }
4204 /* Check that there are no *varargs types of arguments. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004205 for (i = 0; i < argsl; i++) {
4206 expr_ty elt = asdl_seq_GET(args, i);
4207 if (elt->kind == Starred_kind) {
4208 return -1;
4209 }
4210 }
4211
4212 /* Alright, we can optimize the code. */
4213 VISIT(c, expr, meth->v.Attribute.value);
Mark Shannond48848c2021-03-14 18:01:30 +00004214 int old_lineno = c->u->u_lineno;
4215 c->u->u_lineno = meth->end_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004216 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
4217 VISIT_SEQ(c, expr, e->v.Call.args);
4218 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
Mark Shannond48848c2021-03-14 18:01:30 +00004219 c->u->u_lineno = old_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004220 return 1;
4221}
4222
4223static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004224validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
Zackery Spytz08050e92020-04-06 00:47:47 -06004225{
4226 Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
4227 for (Py_ssize_t i = 0; i < nkeywords; i++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004228 keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
4229 if (key->arg == NULL) {
4230 continue;
4231 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +01004232 if (forbidden_name(c, key->arg, Store)) {
4233 return -1;
4234 }
Zackery Spytz08050e92020-04-06 00:47:47 -06004235 for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004236 keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
4237 if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004238 c->u->u_col_offset = other->col_offset;
Brandt Bucher145bf262021-02-26 14:51:55 -08004239 compiler_error(c, "keyword argument repeated: %U", key->arg);
Pablo Galindo254ec782020-04-03 20:37:13 +01004240 return -1;
4241 }
4242 }
4243 }
4244 return 0;
4245}
4246
4247static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004248compiler_call(struct compiler *c, expr_ty e)
4249{
Zackery Spytz97f5de02019-03-22 01:30:32 -06004250 int ret = maybe_optimize_method_call(c, e);
4251 if (ret >= 0) {
4252 return ret;
4253 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004254 if (!check_caller(c, e->v.Call.func)) {
4255 return 0;
4256 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004257 VISIT(c, expr, e->v.Call.func);
4258 return compiler_call_helper(c, 0,
4259 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004260 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004261}
4262
Eric V. Smith235a6f02015-09-19 14:51:32 -04004263static int
4264compiler_joined_str(struct compiler *c, expr_ty e)
4265{
Mark Shannon11e0b292021-04-15 14:28:56 +01004266
4267 Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values);
4268 if (value_count > STACK_USE_GUIDELINE) {
4269 ADDOP_LOAD_CONST_NEW(c, _PyUnicode_FromASCII("", 0));
4270 PyObject *join = _PyUnicode_FromASCII("join", 4);
4271 if (join == NULL) {
4272 return 0;
4273 }
4274 ADDOP_NAME(c, LOAD_METHOD, join, names);
4275 Py_DECREF(join);
4276 ADDOP_I(c, BUILD_LIST, 0);
4277 for (Py_ssize_t i = 0; i < asdl_seq_LEN(e->v.JoinedStr.values); i++) {
4278 VISIT(c, expr, asdl_seq_GET(e->v.JoinedStr.values, i));
4279 ADDOP_I(c, LIST_APPEND, 1);
4280 }
4281 ADDOP_I(c, CALL_METHOD, 1);
4282 }
4283 else {
4284 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
4285 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) {
4286 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
4287 }
4288 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004289 return 1;
4290}
4291
Eric V. Smitha78c7952015-11-03 12:45:05 -05004292/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004293static int
4294compiler_formatted_value(struct compiler *c, expr_ty e)
4295{
Eric V. Smitha78c7952015-11-03 12:45:05 -05004296 /* Our oparg encodes 2 pieces of information: the conversion
4297 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04004298
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004299 Convert the conversion char to 3 bits:
4300 : 000 0x0 FVC_NONE The default if nothing specified.
Eric V. Smitha78c7952015-11-03 12:45:05 -05004301 !s : 001 0x1 FVC_STR
4302 !r : 010 0x2 FVC_REPR
4303 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04004304
Eric V. Smitha78c7952015-11-03 12:45:05 -05004305 next bit is whether or not we have a format spec:
4306 yes : 100 0x4
4307 no : 000 0x0
4308 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004309
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004310 int conversion = e->v.FormattedValue.conversion;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004311 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004312
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004313 /* The expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004314 VISIT(c, expr, e->v.FormattedValue.value);
4315
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004316 switch (conversion) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004317 case 's': oparg = FVC_STR; break;
4318 case 'r': oparg = FVC_REPR; break;
4319 case 'a': oparg = FVC_ASCII; break;
4320 case -1: oparg = FVC_NONE; break;
4321 default:
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004322 PyErr_Format(PyExc_SystemError,
4323 "Unrecognized conversion character %d", conversion);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004324 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004325 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004326 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004327 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004328 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004329 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004330 }
4331
Eric V. Smitha78c7952015-11-03 12:45:05 -05004332 /* And push our opcode and oparg */
4333 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004334
Eric V. Smith235a6f02015-09-19 14:51:32 -04004335 return 1;
4336}
4337
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004338static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004339compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004340{
4341 Py_ssize_t i, n = end - begin;
4342 keyword_ty kw;
4343 PyObject *keys, *key;
4344 assert(n > 0);
Mark Shannon11e0b292021-04-15 14:28:56 +01004345 int big = n*2 > STACK_USE_GUIDELINE;
4346 if (n > 1 && !big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004347 for (i = begin; i < end; i++) {
4348 kw = asdl_seq_GET(keywords, i);
4349 VISIT(c, expr, kw->value);
4350 }
4351 keys = PyTuple_New(n);
4352 if (keys == NULL) {
4353 return 0;
4354 }
4355 for (i = begin; i < end; i++) {
4356 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
4357 Py_INCREF(key);
4358 PyTuple_SET_ITEM(keys, i - begin, key);
4359 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004360 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004361 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
Mark Shannon11e0b292021-04-15 14:28:56 +01004362 return 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004363 }
Mark Shannon11e0b292021-04-15 14:28:56 +01004364 if (big) {
4365 ADDOP_I_NOLINE(c, BUILD_MAP, 0);
4366 }
4367 for (i = begin; i < end; i++) {
4368 kw = asdl_seq_GET(keywords, i);
4369 ADDOP_LOAD_CONST(c, kw->arg);
4370 VISIT(c, expr, kw->value);
4371 if (big) {
4372 ADDOP_I_NOLINE(c, MAP_ADD, 1);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004373 }
Mark Shannon11e0b292021-04-15 14:28:56 +01004374 }
4375 if (!big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004376 ADDOP_I(c, BUILD_MAP, n);
4377 }
4378 return 1;
4379}
4380
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004381/* shared code between compiler_call and compiler_class */
4382static int
4383compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01004384 int n, /* Args already pushed */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004385 asdl_expr_seq *args,
4386 asdl_keyword_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004387{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004388 Py_ssize_t i, nseen, nelts, nkwelts;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004389
Pablo Galindo254ec782020-04-03 20:37:13 +01004390 if (validate_keywords(c, keywords) == -1) {
4391 return 0;
4392 }
4393
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004394 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004395 nkwelts = asdl_seq_LEN(keywords);
4396
Mark Shannon11e0b292021-04-15 14:28:56 +01004397 if (nelts + nkwelts*2 > STACK_USE_GUIDELINE) {
4398 goto ex_call;
4399 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004400 for (i = 0; i < nelts; i++) {
4401 expr_ty elt = asdl_seq_GET(args, i);
4402 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00004403 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004404 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004405 }
4406 for (i = 0; i < nkwelts; i++) {
4407 keyword_ty kw = asdl_seq_GET(keywords, i);
4408 if (kw->arg == NULL) {
4409 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004410 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004411 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004412
Mark Shannon13bc1392020-01-23 09:25:17 +00004413 /* No * or ** args, so can use faster calling sequence */
4414 for (i = 0; i < nelts; i++) {
4415 expr_ty elt = asdl_seq_GET(args, i);
4416 assert(elt->kind != Starred_kind);
4417 VISIT(c, expr, elt);
4418 }
4419 if (nkwelts) {
4420 PyObject *names;
4421 VISIT_SEQ(c, keyword, keywords);
4422 names = PyTuple_New(nkwelts);
4423 if (names == NULL) {
4424 return 0;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004425 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004426 for (i = 0; i < nkwelts; i++) {
4427 keyword_ty kw = asdl_seq_GET(keywords, i);
4428 Py_INCREF(kw->arg);
4429 PyTuple_SET_ITEM(names, i, kw->arg);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004430 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004431 ADDOP_LOAD_CONST_NEW(c, names);
4432 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
4433 return 1;
4434 }
4435 else {
4436 ADDOP_I(c, CALL_FUNCTION, n + nelts);
4437 return 1;
4438 }
4439
4440ex_call:
4441
4442 /* Do positional arguments. */
4443 if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
4444 VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
4445 }
4446 else if (starunpack_helper(c, args, n, BUILD_LIST,
4447 LIST_APPEND, LIST_EXTEND, 1) == 0) {
4448 return 0;
4449 }
4450 /* Then keyword arguments */
4451 if (nkwelts) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004452 /* Has a new dict been pushed */
4453 int have_dict = 0;
Mark Shannon13bc1392020-01-23 09:25:17 +00004454
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004455 nseen = 0; /* the number of keyword arguments on the stack following */
4456 for (i = 0; i < nkwelts; i++) {
4457 keyword_ty kw = asdl_seq_GET(keywords, i);
4458 if (kw->arg == NULL) {
4459 /* A keyword argument unpacking. */
4460 if (nseen) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004461 if (!compiler_subkwargs(c, keywords, i - nseen, i)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004462 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004463 }
Mark Shannondb64f122020-06-01 10:42:42 +01004464 if (have_dict) {
4465 ADDOP_I(c, DICT_MERGE, 1);
4466 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004467 have_dict = 1;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004468 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004469 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004470 if (!have_dict) {
4471 ADDOP_I(c, BUILD_MAP, 0);
4472 have_dict = 1;
4473 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004474 VISIT(c, expr, kw->value);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004475 ADDOP_I(c, DICT_MERGE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004476 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004477 else {
4478 nseen++;
4479 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004480 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004481 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004482 /* Pack up any trailing keyword arguments. */
Mark Shannon8a4cd702020-01-27 09:57:45 +00004483 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004484 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004485 }
4486 if (have_dict) {
4487 ADDOP_I(c, DICT_MERGE, 1);
4488 }
4489 have_dict = 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004490 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004491 assert(have_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004492 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004493 ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0);
4494 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004495}
4496
Nick Coghlan650f0d02007-04-15 12:05:43 +00004497
4498/* List and set comprehensions and generator expressions work by creating a
4499 nested function to perform the actual iteration. This means that the
4500 iteration variables don't leak into the current scope.
4501 The defined function is called immediately following its definition, with the
4502 result of that call being the result of the expression.
4503 The LC/SC version returns the populated container, while the GE version is
4504 flagged in symtable.c as a generator, so it returns the generator object
4505 when the function is called.
Nick Coghlan650f0d02007-04-15 12:05:43 +00004506
4507 Possible cleanups:
4508 - iterate over the generator sequence instead of using recursion
4509*/
4510
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004511
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004512static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004513compiler_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004514 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004515 int depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004516 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004517{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004518 comprehension_ty gen;
4519 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4520 if (gen->is_async) {
4521 return compiler_async_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004522 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004523 } else {
4524 return compiler_sync_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004525 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004526 }
4527}
4528
4529static int
4530compiler_sync_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004531 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004532 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004533 expr_ty elt, expr_ty val, int type)
4534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004535 /* generate code for the iterator, then each of the ifs,
4536 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 comprehension_ty gen;
4539 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004540 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004542 start = compiler_new_block(c);
4543 skip = compiler_new_block(c);
4544 if_cleanup = compiler_new_block(c);
4545 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004547 if (start == NULL || skip == NULL || if_cleanup == NULL ||
4548 anchor == NULL)
4549 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004551 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553 if (gen_index == 0) {
4554 /* Receive outermost iter as an implicit argument */
4555 c->u->u_argcount = 1;
4556 ADDOP_I(c, LOAD_FAST, 0);
4557 }
4558 else {
4559 /* Sub-iter - calculate on the fly */
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004560 /* Fast path for the temporary variable assignment idiom:
4561 for y in [f(x)]
4562 */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004563 asdl_expr_seq *elts;
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004564 switch (gen->iter->kind) {
4565 case List_kind:
4566 elts = gen->iter->v.List.elts;
4567 break;
4568 case Tuple_kind:
4569 elts = gen->iter->v.Tuple.elts;
4570 break;
4571 default:
4572 elts = NULL;
4573 }
4574 if (asdl_seq_LEN(elts) == 1) {
4575 expr_ty elt = asdl_seq_GET(elts, 0);
4576 if (elt->kind != Starred_kind) {
4577 VISIT(c, expr, elt);
4578 start = NULL;
4579 }
4580 }
4581 if (start) {
4582 VISIT(c, expr, gen->iter);
4583 ADDOP(c, GET_ITER);
4584 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004585 }
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004586 if (start) {
4587 depth++;
4588 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01004589 ADDOP_JUMP(c, FOR_ITER, anchor);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004590 NEXT_BLOCK(c);
4591 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004592 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004594 /* XXX this needs to be cleaned up...a lot! */
4595 n = asdl_seq_LEN(gen->ifs);
4596 for (i = 0; i < n; i++) {
4597 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004598 if (!compiler_jump_if(c, e, if_cleanup, 0))
4599 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004600 NEXT_BLOCK(c);
4601 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004603 if (++gen_index < asdl_seq_LEN(generators))
4604 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004605 generators, gen_index, depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004606 elt, val, type))
4607 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004609 /* only append after the last for generator */
4610 if (gen_index >= asdl_seq_LEN(generators)) {
4611 /* comprehension specific code */
4612 switch (type) {
4613 case COMP_GENEXP:
4614 VISIT(c, expr, elt);
4615 ADDOP(c, YIELD_VALUE);
4616 ADDOP(c, POP_TOP);
4617 break;
4618 case COMP_LISTCOMP:
4619 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004620 ADDOP_I(c, LIST_APPEND, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004621 break;
4622 case COMP_SETCOMP:
4623 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004624 ADDOP_I(c, SET_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004625 break;
4626 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004627 /* With '{k: v}', k is evaluated before v, so we do
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004628 the same. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004629 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004630 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004631 ADDOP_I(c, MAP_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004632 break;
4633 default:
4634 return 0;
4635 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004637 compiler_use_next_block(c, skip);
4638 }
4639 compiler_use_next_block(c, if_cleanup);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004640 if (start) {
Mark Shannon582aaf12020-08-04 17:30:11 +01004641 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004642 compiler_use_next_block(c, anchor);
4643 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004644
4645 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004646}
4647
4648static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004649compiler_async_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004650 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004651 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004652 expr_ty elt, expr_ty val, int type)
4653{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004654 comprehension_ty gen;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004655 basicblock *start, *if_cleanup, *except;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004656 Py_ssize_t i, n;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004657 start = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004658 except = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004659 if_cleanup = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004660
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004661 if (start == NULL || if_cleanup == NULL || except == NULL) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004662 return 0;
4663 }
4664
4665 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4666
4667 if (gen_index == 0) {
4668 /* Receive outermost iter as an implicit argument */
4669 c->u->u_argcount = 1;
4670 ADDOP_I(c, LOAD_FAST, 0);
4671 }
4672 else {
4673 /* Sub-iter - calculate on the fly */
4674 VISIT(c, expr, gen->iter);
4675 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004676 }
4677
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004678 compiler_use_next_block(c, start);
tomKPZ7a7ba3d2021-04-07 07:43:45 -07004679 /* Runtime will push a block here, so we need to account for that */
4680 if (!compiler_push_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start,
4681 NULL, NULL)) {
4682 return 0;
4683 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004684
Mark Shannon582aaf12020-08-04 17:30:11 +01004685 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004686 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004687 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004688 ADDOP(c, YIELD_FROM);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004689 ADDOP(c, POP_BLOCK);
Serhiy Storchaka24d32012018-03-10 18:22:34 +02004690 VISIT(c, expr, gen->target);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004691
4692 n = asdl_seq_LEN(gen->ifs);
4693 for (i = 0; i < n; i++) {
4694 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004695 if (!compiler_jump_if(c, e, if_cleanup, 0))
4696 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004697 NEXT_BLOCK(c);
4698 }
4699
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004700 depth++;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004701 if (++gen_index < asdl_seq_LEN(generators))
4702 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004703 generators, gen_index, depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004704 elt, val, type))
4705 return 0;
4706
4707 /* only append after the last for generator */
4708 if (gen_index >= asdl_seq_LEN(generators)) {
4709 /* comprehension specific code */
4710 switch (type) {
4711 case COMP_GENEXP:
4712 VISIT(c, expr, elt);
4713 ADDOP(c, YIELD_VALUE);
4714 ADDOP(c, POP_TOP);
4715 break;
4716 case COMP_LISTCOMP:
4717 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004718 ADDOP_I(c, LIST_APPEND, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004719 break;
4720 case COMP_SETCOMP:
4721 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004722 ADDOP_I(c, SET_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004723 break;
4724 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004725 /* With '{k: v}', k is evaluated before v, so we do
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004726 the same. */
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004727 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004728 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004729 ADDOP_I(c, MAP_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004730 break;
4731 default:
4732 return 0;
4733 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004734 }
4735 compiler_use_next_block(c, if_cleanup);
Mark Shannon582aaf12020-08-04 17:30:11 +01004736 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004737
tomKPZ7a7ba3d2021-04-07 07:43:45 -07004738 compiler_pop_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start);
4739
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004740 compiler_use_next_block(c, except);
4741 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004742
4743 return 1;
4744}
4745
4746static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004747compiler_comprehension(struct compiler *c, expr_ty e, int type,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004748 identifier name, asdl_comprehension_seq *generators, expr_ty elt,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004749 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004751 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004752 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004753 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004754 int is_async_generator = 0;
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004755 int top_level_await = IS_TOP_LEVEL_AWAIT(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004756
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004757
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004758 int is_async_function = c->u->u_ste->ste_coroutine;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004759
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004760 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004761 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4762 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004763 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004764 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004765 }
4766
4767 is_async_generator = c->u->u_ste->ste_coroutine;
4768
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004769 if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004770 compiler_error(c, "asynchronous comprehension outside of "
4771 "an asynchronous function");
4772 goto error_in_scope;
4773 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004775 if (type != COMP_GENEXP) {
4776 int op;
4777 switch (type) {
4778 case COMP_LISTCOMP:
4779 op = BUILD_LIST;
4780 break;
4781 case COMP_SETCOMP:
4782 op = BUILD_SET;
4783 break;
4784 case COMP_DICTCOMP:
4785 op = BUILD_MAP;
4786 break;
4787 default:
4788 PyErr_Format(PyExc_SystemError,
4789 "unknown comprehension type %d", type);
4790 goto error_in_scope;
4791 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004792
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004793 ADDOP_I(c, op, 0);
4794 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004795
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004796 if (!compiler_comprehension_generator(c, generators, 0, 0, elt,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004797 val, type))
4798 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004800 if (type != COMP_GENEXP) {
4801 ADDOP(c, RETURN_VALUE);
4802 }
4803
4804 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004805 qualname = c->u->u_qualname;
4806 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004807 compiler_exit_scope(c);
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004808 if (top_level_await && is_async_generator){
4809 c->u->u_ste->ste_coroutine = 1;
4810 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004811 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004812 goto error;
4813
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004814 if (!compiler_make_closure(c, co, 0, qualname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004815 goto error;
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004816 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004817 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004818 Py_DECREF(co);
4819
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004820 VISIT(c, expr, outermost->iter);
4821
4822 if (outermost->is_async) {
4823 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004824 } else {
4825 ADDOP(c, GET_ITER);
4826 }
4827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004828 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004829
4830 if (is_async_generator && type != COMP_GENEXP) {
4831 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004832 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004833 ADDOP(c, YIELD_FROM);
4834 }
4835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004836 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004837error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004838 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004839error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004840 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004841 Py_XDECREF(co);
4842 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004843}
4844
4845static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004846compiler_genexp(struct compiler *c, expr_ty e)
4847{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004848 static identifier name;
4849 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004850 name = PyUnicode_InternFromString("<genexpr>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004851 if (!name)
4852 return 0;
4853 }
4854 assert(e->kind == GeneratorExp_kind);
4855 return compiler_comprehension(c, e, COMP_GENEXP, name,
4856 e->v.GeneratorExp.generators,
4857 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004858}
4859
4860static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004861compiler_listcomp(struct compiler *c, expr_ty e)
4862{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004863 static identifier name;
4864 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004865 name = PyUnicode_InternFromString("<listcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004866 if (!name)
4867 return 0;
4868 }
4869 assert(e->kind == ListComp_kind);
4870 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4871 e->v.ListComp.generators,
4872 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004873}
4874
4875static int
4876compiler_setcomp(struct compiler *c, expr_ty e)
4877{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004878 static identifier name;
4879 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004880 name = PyUnicode_InternFromString("<setcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004881 if (!name)
4882 return 0;
4883 }
4884 assert(e->kind == SetComp_kind);
4885 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4886 e->v.SetComp.generators,
4887 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004888}
4889
4890
4891static int
4892compiler_dictcomp(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("<dictcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004897 if (!name)
4898 return 0;
4899 }
4900 assert(e->kind == DictComp_kind);
4901 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4902 e->v.DictComp.generators,
4903 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004904}
4905
4906
4907static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004908compiler_visit_keyword(struct compiler *c, keyword_ty k)
4909{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004910 VISIT(c, expr, k->value);
4911 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004912}
4913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004914/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004915 whether they are true or false.
4916
4917 Return values: 1 for true, 0 for false, -1 for non-constant.
4918 */
4919
4920static int
Mark Shannonfee55262019-11-21 09:11:43 +00004921compiler_with_except_finish(struct compiler *c) {
4922 basicblock *exit;
4923 exit = compiler_new_block(c);
4924 if (exit == NULL)
4925 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01004926 ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit);
Mark Shannon266b4622020-11-17 19:30:14 +00004927 NEXT_BLOCK(c);
Mark Shannonbf353f32020-12-17 13:55:28 +00004928 ADDOP_I(c, RERAISE, 1);
Mark Shannonfee55262019-11-21 09:11:43 +00004929 compiler_use_next_block(c, exit);
4930 ADDOP(c, POP_TOP);
4931 ADDOP(c, POP_TOP);
4932 ADDOP(c, POP_TOP);
4933 ADDOP(c, POP_EXCEPT);
4934 ADDOP(c, POP_TOP);
4935 return 1;
4936}
Yury Selivanov75445082015-05-11 22:57:16 -04004937
4938/*
4939 Implements the async with statement.
4940
4941 The semantics outlined in that PEP are as follows:
4942
4943 async with EXPR as VAR:
4944 BLOCK
4945
4946 It is implemented roughly as:
4947
4948 context = EXPR
4949 exit = context.__aexit__ # not calling it
4950 value = await context.__aenter__()
4951 try:
4952 VAR = value # if VAR present in the syntax
4953 BLOCK
4954 finally:
4955 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004956 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04004957 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004958 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04004959 if not (await exit(*exc)):
4960 raise
4961 */
4962static int
4963compiler_async_with(struct compiler *c, stmt_ty s, int pos)
4964{
Mark Shannonfee55262019-11-21 09:11:43 +00004965 basicblock *block, *final, *exit;
Yury Selivanov75445082015-05-11 22:57:16 -04004966 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
4967
4968 assert(s->kind == AsyncWith_kind);
Pablo Galindo90235812020-03-15 04:29:22 +00004969 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07004970 c->u->u_ste->ste_coroutine = 1;
4971 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
Zsolt Dollensteine2396502018-04-27 08:58:56 -07004972 return compiler_error(c, "'async with' outside async function");
4973 }
Yury Selivanov75445082015-05-11 22:57:16 -04004974
4975 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004976 final = compiler_new_block(c);
4977 exit = compiler_new_block(c);
4978 if (!block || !final || !exit)
Yury Selivanov75445082015-05-11 22:57:16 -04004979 return 0;
4980
4981 /* Evaluate EXPR */
4982 VISIT(c, expr, item->context_expr);
4983
4984 ADDOP(c, BEFORE_ASYNC_WITH);
4985 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004986 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004987 ADDOP(c, YIELD_FROM);
4988
Mark Shannon582aaf12020-08-04 17:30:11 +01004989 ADDOP_JUMP(c, SETUP_ASYNC_WITH, final);
Yury Selivanov75445082015-05-11 22:57:16 -04004990
4991 /* SETUP_ASYNC_WITH pushes a finally block. */
4992 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004993 if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004994 return 0;
4995 }
4996
4997 if (item->optional_vars) {
4998 VISIT(c, expr, item->optional_vars);
4999 }
5000 else {
5001 /* Discard result from context.__aenter__() */
5002 ADDOP(c, POP_TOP);
5003 }
5004
5005 pos++;
5006 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
5007 /* BLOCK code */
5008 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
5009 else if (!compiler_async_with(c, s, pos))
5010 return 0;
5011
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005012 compiler_pop_fblock(c, ASYNC_WITH, block);
Mark Shannonfee55262019-11-21 09:11:43 +00005013 ADDOP(c, POP_BLOCK);
5014 /* End of body; start the cleanup */
Yury Selivanov75445082015-05-11 22:57:16 -04005015
Mark Shannonfee55262019-11-21 09:11:43 +00005016 /* For successful outcome:
5017 * call __exit__(None, None, None)
5018 */
5019 if(!compiler_call_exit_with_nones(c))
Yury Selivanov75445082015-05-11 22:57:16 -04005020 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00005021 ADDOP(c, GET_AWAITABLE);
5022 ADDOP_O(c, LOAD_CONST, Py_None, consts);
5023 ADDOP(c, YIELD_FROM);
Yury Selivanov75445082015-05-11 22:57:16 -04005024
Mark Shannonfee55262019-11-21 09:11:43 +00005025 ADDOP(c, POP_TOP);
Yury Selivanov75445082015-05-11 22:57:16 -04005026
Mark Shannon582aaf12020-08-04 17:30:11 +01005027 ADDOP_JUMP(c, JUMP_ABSOLUTE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00005028
5029 /* For exceptional outcome: */
5030 compiler_use_next_block(c, final);
5031
5032 ADDOP(c, WITH_EXCEPT_START);
Yury Selivanov75445082015-05-11 22:57:16 -04005033 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005034 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005035 ADDOP(c, YIELD_FROM);
Mark Shannonfee55262019-11-21 09:11:43 +00005036 compiler_with_except_finish(c);
Yury Selivanov75445082015-05-11 22:57:16 -04005037
Mark Shannonfee55262019-11-21 09:11:43 +00005038compiler_use_next_block(c, exit);
Yury Selivanov75445082015-05-11 22:57:16 -04005039 return 1;
5040}
5041
5042
Guido van Rossumc2e20742006-02-27 22:32:47 +00005043/*
5044 Implements the with statement from PEP 343.
Guido van Rossumc2e20742006-02-27 22:32:47 +00005045 with EXPR as VAR:
5046 BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00005047 is implemented as:
5048 <code for EXPR>
5049 SETUP_WITH E
5050 <code to store to VAR> or POP_TOP
5051 <code for BLOCK>
5052 LOAD_CONST (None, None, None)
5053 CALL_FUNCTION_EX 0
5054 JUMP_FORWARD EXIT
5055 E: WITH_EXCEPT_START (calls EXPR.__exit__)
5056 POP_JUMP_IF_TRUE T:
5057 RERAISE
5058 T: POP_TOP * 3 (remove exception from stack)
5059 POP_EXCEPT
5060 POP_TOP
5061 EXIT:
Guido van Rossumc2e20742006-02-27 22:32:47 +00005062 */
Mark Shannonfee55262019-11-21 09:11:43 +00005063
Guido van Rossumc2e20742006-02-27 22:32:47 +00005064static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005065compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00005066{
Mark Shannonfee55262019-11-21 09:11:43 +00005067 basicblock *block, *final, *exit;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005068 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005069
5070 assert(s->kind == With_kind);
5071
Guido van Rossumc2e20742006-02-27 22:32:47 +00005072 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00005073 final = compiler_new_block(c);
5074 exit = compiler_new_block(c);
5075 if (!block || !final || !exit)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005076 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005077
Thomas Wouters477c8d52006-05-27 19:21:47 +00005078 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005079 VISIT(c, expr, item->context_expr);
Mark Shannonfee55262019-11-21 09:11:43 +00005080 /* Will push bound __exit__ */
Mark Shannon582aaf12020-08-04 17:30:11 +01005081 ADDOP_JUMP(c, SETUP_WITH, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005082
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005083 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00005084 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00005085 if (!compiler_push_fblock(c, WITH, block, final, NULL)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005086 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005087 }
5088
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005089 if (item->optional_vars) {
5090 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005091 }
5092 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005093 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005094 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005095 }
5096
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005097 pos++;
5098 if (pos == asdl_seq_LEN(s->v.With.items))
5099 /* BLOCK code */
5100 VISIT_SEQ(c, stmt, s->v.With.body)
5101 else if (!compiler_with(c, s, pos))
5102 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005103
Mark Shannon3bd60352021-01-13 12:05:43 +00005104
5105 /* Mark all following code as artificial */
5106 c->u->u_lineno = -1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005107 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005108 compiler_pop_fblock(c, WITH, block);
Mark Shannon13bc1392020-01-23 09:25:17 +00005109
Mark Shannonfee55262019-11-21 09:11:43 +00005110 /* End of body; start the cleanup. */
Mark Shannon13bc1392020-01-23 09:25:17 +00005111
Mark Shannonfee55262019-11-21 09:11:43 +00005112 /* For successful outcome:
5113 * call __exit__(None, None, None)
5114 */
5115 if (!compiler_call_exit_with_nones(c))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005116 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00005117 ADDOP(c, POP_TOP);
Mark Shannon582aaf12020-08-04 17:30:11 +01005118 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005119
Mark Shannonfee55262019-11-21 09:11:43 +00005120 /* For exceptional outcome: */
5121 compiler_use_next_block(c, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005122
Mark Shannonfee55262019-11-21 09:11:43 +00005123 ADDOP(c, WITH_EXCEPT_START);
5124 compiler_with_except_finish(c);
5125
5126 compiler_use_next_block(c, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005127 return 1;
5128}
5129
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005130static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005131compiler_visit_expr1(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005133 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07005134 case NamedExpr_kind:
5135 VISIT(c, expr, e->v.NamedExpr.value);
5136 ADDOP(c, DUP_TOP);
5137 VISIT(c, expr, e->v.NamedExpr.target);
5138 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005139 case BoolOp_kind:
5140 return compiler_boolop(c, e);
5141 case BinOp_kind:
5142 VISIT(c, expr, e->v.BinOp.left);
5143 VISIT(c, expr, e->v.BinOp.right);
Andy Lester76d58772020-03-10 21:18:12 -05005144 ADDOP(c, binop(e->v.BinOp.op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005145 break;
5146 case UnaryOp_kind:
5147 VISIT(c, expr, e->v.UnaryOp.operand);
5148 ADDOP(c, unaryop(e->v.UnaryOp.op));
5149 break;
5150 case Lambda_kind:
5151 return compiler_lambda(c, e);
5152 case IfExp_kind:
5153 return compiler_ifexp(c, e);
5154 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005155 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005156 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005157 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005158 case GeneratorExp_kind:
5159 return compiler_genexp(c, e);
5160 case ListComp_kind:
5161 return compiler_listcomp(c, e);
5162 case SetComp_kind:
5163 return compiler_setcomp(c, e);
5164 case DictComp_kind:
5165 return compiler_dictcomp(c, e);
5166 case Yield_kind:
5167 if (c->u->u_ste->ste_type != FunctionBlock)
5168 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005169 if (e->v.Yield.value) {
5170 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005171 }
5172 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005173 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005174 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005175 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005176 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005177 case YieldFrom_kind:
5178 if (c->u->u_ste->ste_type != FunctionBlock)
5179 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04005180
5181 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
5182 return compiler_error(c, "'yield from' inside async function");
5183
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005184 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04005185 ADDOP(c, GET_YIELD_FROM_ITER);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005186 ADDOP_LOAD_CONST(c, Py_None);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005187 ADDOP(c, YIELD_FROM);
5188 break;
Yury Selivanov75445082015-05-11 22:57:16 -04005189 case Await_kind:
Pablo Galindo90235812020-03-15 04:29:22 +00005190 if (!IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005191 if (c->u->u_ste->ste_type != FunctionBlock){
5192 return compiler_error(c, "'await' outside function");
5193 }
Yury Selivanov75445082015-05-11 22:57:16 -04005194
Victor Stinner331a6a52019-05-27 16:39:22 +02005195 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005196 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
5197 return compiler_error(c, "'await' outside async function");
5198 }
5199 }
Yury Selivanov75445082015-05-11 22:57:16 -04005200
5201 VISIT(c, expr, e->v.Await.value);
5202 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005203 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005204 ADDOP(c, YIELD_FROM);
5205 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005206 case Compare_kind:
5207 return compiler_compare(c, e);
5208 case Call_kind:
5209 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005210 case Constant_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005211 ADDOP_LOAD_CONST(c, e->v.Constant.value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005212 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005213 case JoinedStr_kind:
5214 return compiler_joined_str(c, e);
5215 case FormattedValue_kind:
5216 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005217 /* The following exprs can be assignment targets. */
5218 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005219 VISIT(c, expr, e->v.Attribute.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005220 switch (e->v.Attribute.ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005221 case Load:
Mark Shannond48848c2021-03-14 18:01:30 +00005222 {
5223 int old_lineno = c->u->u_lineno;
5224 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005225 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005226 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005227 break;
Mark Shannond48848c2021-03-14 18:01:30 +00005228 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005229 case Store:
Mark Shannond48848c2021-03-14 18:01:30 +00005230 if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005231 return 0;
Mark Shannond48848c2021-03-14 18:01:30 +00005232 }
5233 int old_lineno = c->u->u_lineno;
5234 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005235 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005236 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005237 break;
5238 case Del:
5239 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
5240 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005241 }
5242 break;
5243 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005244 return compiler_subscript(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005245 case Starred_kind:
5246 switch (e->v.Starred.ctx) {
5247 case Store:
5248 /* In all legitimate cases, the Starred node was already replaced
5249 * by compiler_list/compiler_tuple. XXX: is that okay? */
5250 return compiler_error(c,
5251 "starred assignment target must be in a list or tuple");
5252 default:
5253 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005254 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005255 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005256 break;
5257 case Slice_kind:
5258 return compiler_slice(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005259 case Name_kind:
5260 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
5261 /* child nodes of List and Tuple will have expr_context set */
5262 case List_kind:
5263 return compiler_list(c, e);
5264 case Tuple_kind:
5265 return compiler_tuple(c, e);
5266 }
5267 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005268}
5269
5270static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005271compiler_visit_expr(struct compiler *c, expr_ty e)
5272{
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005273 int old_lineno = c->u->u_lineno;
5274 int old_col_offset = c->u->u_col_offset;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005275 SET_LOC(c, e);
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005276 int res = compiler_visit_expr1(c, e);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005277 c->u->u_lineno = old_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005278 c->u->u_col_offset = old_col_offset;
5279 return res;
5280}
5281
5282static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005283compiler_augassign(struct compiler *c, stmt_ty s)
5284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005285 assert(s->kind == AugAssign_kind);
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005286 expr_ty e = s->v.AugAssign.target;
5287
5288 int old_lineno = c->u->u_lineno;
5289 int old_col_offset = c->u->u_col_offset;
5290 SET_LOC(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005292 switch (e->kind) {
5293 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005294 VISIT(c, expr, e->v.Attribute.value);
5295 ADDOP(c, DUP_TOP);
Mark Shannond48848c2021-03-14 18:01:30 +00005296 int old_lineno = c->u->u_lineno;
5297 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005298 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005299 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005300 break;
5301 case Subscript_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005302 VISIT(c, expr, e->v.Subscript.value);
5303 VISIT(c, expr, e->v.Subscript.slice);
5304 ADDOP(c, DUP_TOP_TWO);
5305 ADDOP(c, BINARY_SUBSCR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005306 break;
5307 case Name_kind:
5308 if (!compiler_nameop(c, e->v.Name.id, Load))
5309 return 0;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005310 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005311 default:
5312 PyErr_Format(PyExc_SystemError,
5313 "invalid node type (%d) for augmented assignment",
5314 e->kind);
5315 return 0;
5316 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005317
5318 c->u->u_lineno = old_lineno;
5319 c->u->u_col_offset = old_col_offset;
5320
5321 VISIT(c, expr, s->v.AugAssign.value);
5322 ADDOP(c, inplace_binop(s->v.AugAssign.op));
5323
5324 SET_LOC(c, e);
5325
5326 switch (e->kind) {
5327 case Attribute_kind:
Mark Shannond48848c2021-03-14 18:01:30 +00005328 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005329 ADDOP(c, ROT_TWO);
5330 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5331 break;
5332 case Subscript_kind:
5333 ADDOP(c, ROT_THREE);
5334 ADDOP(c, STORE_SUBSCR);
5335 break;
5336 case Name_kind:
5337 return compiler_nameop(c, e->v.Name.id, Store);
5338 default:
5339 Py_UNREACHABLE();
5340 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005341 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005342}
5343
5344static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005345check_ann_expr(struct compiler *c, expr_ty e)
5346{
5347 VISIT(c, expr, e);
5348 ADDOP(c, POP_TOP);
5349 return 1;
5350}
5351
5352static int
5353check_annotation(struct compiler *c, stmt_ty s)
5354{
Batuhan Taskaya8cc3cfa2021-04-25 05:31:20 +03005355 /* Annotations of complex targets does not produce anything
5356 under annotations future */
5357 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
5358 return 1;
5359 }
5360
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005361 /* Annotations are only evaluated in a module or class. */
5362 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5363 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
5364 return check_ann_expr(c, s->v.AnnAssign.annotation);
5365 }
5366 return 1;
5367}
5368
5369static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005370check_ann_subscr(struct compiler *c, expr_ty e)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005371{
5372 /* We check that everything in a subscript is defined at runtime. */
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005373 switch (e->kind) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005374 case Slice_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005375 if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005376 return 0;
5377 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005378 if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) {
5379 return 0;
5380 }
5381 if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) {
5382 return 0;
5383 }
5384 return 1;
5385 case Tuple_kind: {
5386 /* extended slice */
Pablo Galindoa5634c42020-09-16 19:42:00 +01005387 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005388 Py_ssize_t i, n = asdl_seq_LEN(elts);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005389 for (i = 0; i < n; i++) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005390 if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005391 return 0;
5392 }
5393 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005394 return 1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005395 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005396 default:
5397 return check_ann_expr(c, e);
5398 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005399}
5400
5401static int
5402compiler_annassign(struct compiler *c, stmt_ty s)
5403{
5404 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07005405 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005406
5407 assert(s->kind == AnnAssign_kind);
5408
5409 /* We perform the actual assignment first. */
5410 if (s->v.AnnAssign.value) {
5411 VISIT(c, expr, s->v.AnnAssign.value);
5412 VISIT(c, expr, targ);
5413 }
5414 switch (targ->kind) {
5415 case Name_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005416 if (forbidden_name(c, targ->v.Name.id, Store))
5417 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005418 /* If we have a simple name in a module or class, store annotation. */
5419 if (s->v.AnnAssign.simple &&
5420 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5421 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Pablo Galindob0544ba2021-04-21 12:41:19 +01005422 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
5423 VISIT(c, annexpr, s->v.AnnAssign.annotation)
5424 }
5425 else {
5426 VISIT(c, expr, s->v.AnnAssign.annotation);
5427 }
Mark Shannon332cd5e2018-01-30 00:41:04 +00005428 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02005429 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005430 ADDOP_LOAD_CONST_NEW(c, mangled);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005431 ADDOP(c, STORE_SUBSCR);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005432 }
5433 break;
5434 case Attribute_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005435 if (forbidden_name(c, targ->v.Attribute.attr, Store))
5436 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005437 if (!s->v.AnnAssign.value &&
5438 !check_ann_expr(c, targ->v.Attribute.value)) {
5439 return 0;
5440 }
5441 break;
5442 case Subscript_kind:
5443 if (!s->v.AnnAssign.value &&
5444 (!check_ann_expr(c, targ->v.Subscript.value) ||
5445 !check_ann_subscr(c, targ->v.Subscript.slice))) {
5446 return 0;
5447 }
5448 break;
5449 default:
5450 PyErr_Format(PyExc_SystemError,
5451 "invalid node type (%d) for annotated assignment",
5452 targ->kind);
5453 return 0;
5454 }
5455 /* Annotation is evaluated last. */
5456 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
5457 return 0;
5458 }
5459 return 1;
5460}
5461
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005462/* Raises a SyntaxError and returns 0.
5463 If something goes wrong, a different exception may be raised.
5464*/
5465
5466static int
Brandt Bucher145bf262021-02-26 14:51:55 -08005467compiler_error(struct compiler *c, const char *format, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005468{
Brandt Bucher145bf262021-02-26 14:51:55 -08005469 va_list vargs;
5470#ifdef HAVE_STDARG_PROTOTYPES
5471 va_start(vargs, format);
5472#else
5473 va_start(vargs);
5474#endif
5475 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5476 va_end(vargs);
5477 if (msg == NULL) {
5478 return 0;
5479 }
5480 PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
5481 if (loc == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005482 Py_INCREF(Py_None);
5483 loc = Py_None;
5484 }
Pablo Galindoa77aac42021-04-23 14:27:05 +01005485 PyObject *args = Py_BuildValue("O(OiiOii)", msg, c->c_filename,
5486 c->u->u_lineno, c->u->u_col_offset + 1, loc,
5487 c->u->u_end_lineno, c->u->u_end_col_offset + 1);
Brandt Bucher145bf262021-02-26 14:51:55 -08005488 Py_DECREF(msg);
5489 if (args == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005490 goto exit;
Brandt Bucher145bf262021-02-26 14:51:55 -08005491 }
5492 PyErr_SetObject(PyExc_SyntaxError, args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005493 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005494 Py_DECREF(loc);
Brandt Bucher145bf262021-02-26 14:51:55 -08005495 Py_XDECREF(args);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005496 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005497}
5498
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005499/* Emits a SyntaxWarning and returns 1 on success.
5500 If a SyntaxWarning raised as error, replaces it with a SyntaxError
5501 and returns 0.
5502*/
5503static int
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005504compiler_warn(struct compiler *c, const char *format, ...)
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005505{
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005506 va_list vargs;
5507#ifdef HAVE_STDARG_PROTOTYPES
5508 va_start(vargs, format);
5509#else
5510 va_start(vargs);
5511#endif
5512 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5513 va_end(vargs);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005514 if (msg == NULL) {
5515 return 0;
5516 }
5517 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
5518 c->u->u_lineno, NULL, NULL) < 0)
5519 {
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005520 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005521 /* Replace the SyntaxWarning exception with a SyntaxError
5522 to get a more accurate error report */
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005523 PyErr_Clear();
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005524 assert(PyUnicode_AsUTF8(msg) != NULL);
5525 compiler_error(c, PyUnicode_AsUTF8(msg));
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005526 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005527 Py_DECREF(msg);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005528 return 0;
5529 }
5530 Py_DECREF(msg);
5531 return 1;
5532}
5533
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005534static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005535compiler_subscript(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005536{
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005537 expr_context_ty ctx = e->v.Subscript.ctx;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005538 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005539
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005540 if (ctx == Load) {
5541 if (!check_subscripter(c, e->v.Subscript.value)) {
5542 return 0;
5543 }
5544 if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
5545 return 0;
5546 }
5547 }
5548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005549 switch (ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005550 case Load: op = BINARY_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005551 case Store: op = STORE_SUBSCR; break;
5552 case Del: op = DELETE_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005553 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005554 assert(op);
5555 VISIT(c, expr, e->v.Subscript.value);
5556 VISIT(c, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005557 ADDOP(c, op);
5558 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005559}
5560
5561static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005562compiler_slice(struct compiler *c, expr_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005563{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005564 int n = 2;
5565 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005567 /* only handles the cases where BUILD_SLICE is emitted */
5568 if (s->v.Slice.lower) {
5569 VISIT(c, expr, s->v.Slice.lower);
5570 }
5571 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005572 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005573 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005575 if (s->v.Slice.upper) {
5576 VISIT(c, expr, s->v.Slice.upper);
5577 }
5578 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005579 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005580 }
5581
5582 if (s->v.Slice.step) {
5583 n++;
5584 VISIT(c, expr, s->v.Slice.step);
5585 }
5586 ADDOP_I(c, BUILD_SLICE, n);
5587 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005588}
5589
Brandt Bucher145bf262021-02-26 14:51:55 -08005590
5591// PEP 634: Structural Pattern Matching
5592
5593// To keep things simple, all compiler_pattern_* routines follow the convention
5594// of replacing TOS (the subject for the given pattern) with either True (match)
5595// or False (no match). We do this even for irrefutable patterns; the idea is
5596// that it's much easier to smooth out any redundant pushing, popping, and
5597// jumping in the peephole optimizer than to detect or predict it here.
5598
Brandt Bucher145bf262021-02-26 14:51:55 -08005599#define WILDCARD_CHECK(N) \
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005600 ((N)->kind == MatchAs_kind && !(N)->v.MatchAs.name)
Brandt Bucher145bf262021-02-26 14:51:55 -08005601
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005602#define WILDCARD_STAR_CHECK(N) \
5603 ((N)->kind == MatchStar_kind && !(N)->v.MatchStar.name)
5604
5605// Limit permitted subexpressions, even if the parser & AST validator let them through
5606#define MATCH_VALUE_EXPR(N) \
5607 ((N)->kind == Constant_kind || (N)->kind == Attribute_kind)
Brandt Bucher145bf262021-02-26 14:51:55 -08005608
5609static int
5610pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc)
5611{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005612 if (forbidden_name(c, n, Store)) {
5613 return 0;
5614 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005615 // Can't assign to the same name twice:
5616 if (pc->stores == NULL) {
5617 RETURN_IF_FALSE(pc->stores = PySet_New(NULL));
5618 }
5619 else {
5620 int duplicate = PySet_Contains(pc->stores, n);
5621 if (duplicate < 0) {
5622 return 0;
5623 }
5624 if (duplicate) {
5625 const char *e = "multiple assignments to name %R in pattern";
5626 return compiler_error(c, e, n);
5627 }
5628 }
5629 RETURN_IF_FALSE(!PySet_Add(pc->stores, n));
5630 RETURN_IF_FALSE(compiler_nameop(c, n, Store));
5631 return 1;
5632}
5633
5634
5635static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005636pattern_unpack_helper(struct compiler *c, asdl_pattern_seq *elts)
5637{
5638 Py_ssize_t n = asdl_seq_LEN(elts);
5639 int seen_star = 0;
5640 for (Py_ssize_t i = 0; i < n; i++) {
5641 pattern_ty elt = asdl_seq_GET(elts, i);
5642 if (elt->kind == MatchStar_kind && !seen_star) {
5643 if ((i >= (1 << 8)) ||
5644 (n-i-1 >= (INT_MAX >> 8)))
5645 return compiler_error(c,
5646 "too many expressions in "
5647 "star-unpacking sequence pattern");
5648 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
5649 seen_star = 1;
5650 }
5651 else if (elt->kind == MatchStar_kind) {
5652 return compiler_error(c,
5653 "multiple starred expressions in sequence pattern");
5654 }
5655 }
5656 if (!seen_star) {
5657 ADDOP_I(c, UNPACK_SEQUENCE, n);
5658 }
5659 return 1;
5660}
5661
5662static int
5663pattern_helper_sequence_unpack(struct compiler *c, asdl_pattern_seq *patterns,
Brandt Bucher145bf262021-02-26 14:51:55 -08005664 Py_ssize_t star, pattern_context *pc)
5665{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005666 RETURN_IF_FALSE(pattern_unpack_helper(c, patterns));
Brandt Bucher145bf262021-02-26 14:51:55 -08005667 // We've now got a bunch of new subjects on the stack. If any of them fail
5668 // to match, we need to pop everything else off, then finally push False.
5669 // fails is an array of blocks that correspond to the necessary amount of
5670 // popping for each element:
5671 basicblock **fails;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005672 Py_ssize_t size = asdl_seq_LEN(patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08005673 fails = (basicblock **)PyObject_Malloc(sizeof(basicblock*) * size);
5674 if (fails == NULL) {
5675 PyErr_NoMemory();
5676 return 0;
5677 }
5678 // NOTE: Can't use our nice returning macros anymore: they'll leak memory!
5679 // goto error on error.
5680 for (Py_ssize_t i = 0; i < size; i++) {
5681 fails[i] = compiler_new_block(c);
5682 if (fails[i] == NULL) {
5683 goto error;
5684 }
5685 }
5686 for (Py_ssize_t i = 0; i < size; i++) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005687 pattern_ty pattern = asdl_seq_GET(patterns, i);
5688 assert((i == star) == (pattern->kind == MatchStar_kind));
5689 if (!compiler_pattern_subpattern(c, pattern, pc) ||
Brandt Bucher145bf262021-02-26 14:51:55 -08005690 !compiler_addop_j(c, POP_JUMP_IF_FALSE, fails[i]) ||
5691 compiler_next_block(c) == NULL)
5692 {
5693 goto error;
5694 }
5695 }
5696 // Success!
5697 basicblock *end = compiler_new_block(c);
5698 if (end == NULL ||
5699 !compiler_addop_load_const(c, Py_True) ||
5700 !compiler_addop_j(c, JUMP_FORWARD, end))
5701 {
5702 goto error;
5703 }
5704 // This is where we handle failed sub-patterns. For a sequence pattern like
5705 // [a, b, c, d], this will look like:
5706 // fails[0]: POP_TOP
5707 // fails[1]: POP_TOP
5708 // fails[2]: POP_TOP
5709 // fails[3]: LOAD_CONST False
5710 for (Py_ssize_t i = 0; i < size - 1; i++) {
5711 compiler_use_next_block(c, fails[i]);
5712 if (!compiler_addop(c, POP_TOP)) {
5713 goto error;
5714 }
5715 }
5716 compiler_use_next_block(c, fails[size - 1]);
5717 if (!compiler_addop_load_const(c, Py_False)) {
5718 goto error;
5719 }
5720 compiler_use_next_block(c, end);
5721 PyObject_Free(fails);
5722 return 1;
5723error:
5724 PyObject_Free(fails);
5725 return 0;
5726}
5727
5728// Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of
5729// UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a
5730// starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc.
5731static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005732pattern_helper_sequence_subscr(struct compiler *c, asdl_pattern_seq *patterns,
Brandt Bucher145bf262021-02-26 14:51:55 -08005733 Py_ssize_t star, pattern_context *pc)
5734{
5735 basicblock *end, *fail_pop_1;
5736 RETURN_IF_FALSE(end = compiler_new_block(c));
5737 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005738 Py_ssize_t size = asdl_seq_LEN(patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08005739 for (Py_ssize_t i = 0; i < size; i++) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005740 pattern_ty pattern = asdl_seq_GET(patterns, i);
5741 if (WILDCARD_CHECK(pattern)) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005742 continue;
5743 }
5744 if (i == star) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005745 assert(WILDCARD_STAR_CHECK(pattern));
Brandt Bucher145bf262021-02-26 14:51:55 -08005746 continue;
5747 }
5748 ADDOP(c, DUP_TOP);
5749 if (i < star) {
5750 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5751 }
5752 else {
5753 // The subject may not support negative indexing! Compute a
5754 // nonnegative index:
5755 ADDOP(c, GET_LEN);
5756 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i));
5757 ADDOP(c, BINARY_SUBTRACT);
5758 }
5759 ADDOP(c, BINARY_SUBSCR);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005760 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08005761 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5762 NEXT_BLOCK(c);
5763 }
5764 ADDOP(c, POP_TOP);
5765 ADDOP_LOAD_CONST(c, Py_True);
5766 ADDOP_JUMP(c, JUMP_FORWARD, end);
5767 compiler_use_next_block(c, fail_pop_1);
5768 ADDOP(c, POP_TOP);
5769 ADDOP_LOAD_CONST(c, Py_False);
5770 compiler_use_next_block(c, end);
5771 return 1;
5772}
5773
Brandt Bucher145bf262021-02-26 14:51:55 -08005774// Like compiler_pattern, but turn off checks for irrefutability.
5775static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005776compiler_pattern_subpattern(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005777{
5778 int allow_irrefutable = pc->allow_irrefutable;
5779 pc->allow_irrefutable = 1;
5780 RETURN_IF_FALSE(compiler_pattern(c, p, pc));
5781 pc->allow_irrefutable = allow_irrefutable;
5782 return 1;
5783}
5784
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005785static int
5786compiler_pattern_capture(struct compiler *c, identifier n, pattern_context *pc)
5787{
5788 RETURN_IF_FALSE(pattern_helper_store_name(c, n, pc));
5789 ADDOP_LOAD_CONST(c, Py_True);
5790 return 1;
5791}
Brandt Bucher145bf262021-02-26 14:51:55 -08005792
5793static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005794compiler_pattern_wildcard(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005795{
5796 assert(p->kind == MatchAs_kind);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005797 if (!pc->allow_irrefutable) {
5798 // Whoops, can't have a wildcard here!
5799 const char *e = "wildcard makes remaining patterns unreachable";
5800 return compiler_error(c, e);
5801 }
5802 ADDOP(c, POP_TOP);
5803 ADDOP_LOAD_CONST(c, Py_True);
5804 return 1;
5805}
5806
5807static int
5808compiler_pattern_as(struct compiler *c, pattern_ty p, pattern_context *pc)
5809{
5810 assert(p->kind == MatchAs_kind);
5811 if (p->v.MatchAs.name == NULL) {
5812 return compiler_pattern_wildcard(c, p, pc);
5813 }
5814 if (p->v.MatchAs.pattern == NULL) {
5815 if (!pc->allow_irrefutable) {
5816 // Whoops, can't have a name capture here!
5817 const char *e = "name capture %R makes remaining patterns unreachable";
5818 return compiler_error(c, e, p->v.MatchAs.name);
5819 }
5820 return compiler_pattern_capture(c, p->v.MatchAs.name, pc);
5821 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005822 basicblock *end, *fail_pop_1;
5823 RETURN_IF_FALSE(end = compiler_new_block(c));
5824 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5825 // Need to make a copy for (possibly) storing later:
5826 ADDOP(c, DUP_TOP);
5827 RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc));
5828 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5829 NEXT_BLOCK(c);
5830 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc));
5831 ADDOP_LOAD_CONST(c, Py_True);
5832 ADDOP_JUMP(c, JUMP_FORWARD, end);
5833 compiler_use_next_block(c, fail_pop_1);
5834 // Need to pop that unused copy from before:
5835 ADDOP(c, POP_TOP);
5836 ADDOP_LOAD_CONST(c, Py_False);
5837 compiler_use_next_block(c, end);
5838 return 1;
5839}
5840
Brandt Bucher145bf262021-02-26 14:51:55 -08005841static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005842compiler_pattern_star(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005843{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005844 assert(p->kind == MatchStar_kind);
Brandt Bucher145bf262021-02-26 14:51:55 -08005845 if (!pc->allow_irrefutable) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005846 // Whoops, can't have a star capture here!
5847 const char *e = "star captures are only allowed as part of sequence patterns";
5848 return compiler_error(c, e);
Brandt Bucher145bf262021-02-26 14:51:55 -08005849 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005850 return compiler_pattern_capture(c, p->v.MatchStar.name, pc);
Brandt Bucher145bf262021-02-26 14:51:55 -08005851}
5852
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005853static int
5854validate_kwd_attrs(struct compiler *c, asdl_identifier_seq *attrs, asdl_pattern_seq* patterns)
5855{
5856 // Any errors will point to the pattern rather than the arg name as the
5857 // parser is only supplying identifiers rather than Name or keyword nodes
5858 Py_ssize_t nattrs = asdl_seq_LEN(attrs);
5859 for (Py_ssize_t i = 0; i < nattrs; i++) {
5860 identifier attr = ((identifier)asdl_seq_GET(attrs, i));
5861 c->u->u_col_offset = ((pattern_ty) asdl_seq_GET(patterns, i))->col_offset;
5862 if (forbidden_name(c, attr, Store)) {
5863 return -1;
5864 }
5865 for (Py_ssize_t j = i + 1; j < nattrs; j++) {
5866 identifier other = ((identifier)asdl_seq_GET(attrs, j));
5867 if (!PyUnicode_Compare(attr, other)) {
5868 c->u->u_col_offset = ((pattern_ty) asdl_seq_GET(patterns, j))->col_offset;
5869 compiler_error(c, "attribute name repeated in class pattern: %U", attr);
5870 return -1;
5871 }
5872 }
5873 }
5874 return 0;
5875}
Brandt Bucher145bf262021-02-26 14:51:55 -08005876
5877static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005878compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005879{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005880 assert(p->kind == MatchClass_kind);
5881 asdl_pattern_seq *patterns = p->v.MatchClass.patterns;
5882 asdl_identifier_seq *kwd_attrs = p->v.MatchClass.kwd_attrs;
5883 asdl_pattern_seq *kwd_patterns = p->v.MatchClass.kwd_patterns;
5884 Py_ssize_t nargs = asdl_seq_LEN(patterns);
5885 Py_ssize_t nattrs = asdl_seq_LEN(kwd_attrs);
5886 Py_ssize_t nkwd_patterns = asdl_seq_LEN(kwd_patterns);
5887 if (nattrs != nkwd_patterns) {
5888 // AST validator shouldn't let this happen, but if it does,
5889 // just fail, don't crash out of the interpreter
5890 const char * e = "kwd_attrs (%d) / kwd_patterns (%d) length mismatch in class pattern";
5891 return compiler_error(c, e, nattrs, nkwd_patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08005892 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005893 if (INT_MAX < nargs || INT_MAX < nargs + nattrs - 1) {
5894 const char *e = "too many sub-patterns in class pattern %R";
5895 return compiler_error(c, e, p->v.MatchClass.cls);
5896 }
5897 if (nattrs) {
5898 RETURN_IF_FALSE(!validate_kwd_attrs(c, kwd_attrs, kwd_patterns));
5899 c->u->u_col_offset = p->col_offset; // validate_kwd_attrs moves this
5900 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005901 basicblock *end, *fail_pop_1;
5902 RETURN_IF_FALSE(end = compiler_new_block(c));
5903 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005904 VISIT(c, expr, p->v.MatchClass.cls);
5905 PyObject *attr_names;
5906 RETURN_IF_FALSE(attr_names = PyTuple_New(nattrs));
Brandt Bucher145bf262021-02-26 14:51:55 -08005907 Py_ssize_t i;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005908 for (i = 0; i < nattrs; i++) {
5909 PyObject *name = asdl_seq_GET(kwd_attrs, i);
Brandt Bucher145bf262021-02-26 14:51:55 -08005910 Py_INCREF(name);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005911 PyTuple_SET_ITEM(attr_names, i, name);
Brandt Bucher145bf262021-02-26 14:51:55 -08005912 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005913 ADDOP_LOAD_CONST_NEW(c, attr_names);
Brandt Bucher145bf262021-02-26 14:51:55 -08005914 ADDOP_I(c, MATCH_CLASS, nargs);
5915 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5916 NEXT_BLOCK(c);
5917 // TOS is now a tuple of (nargs + nkwargs) attributes.
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005918 for (i = 0; i < nargs + nattrs; i++) {
5919 pattern_ty pattern;
Brandt Bucher145bf262021-02-26 14:51:55 -08005920 if (i < nargs) {
5921 // Positional:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005922 pattern = asdl_seq_GET(patterns, i);
Brandt Bucher145bf262021-02-26 14:51:55 -08005923 }
5924 else {
5925 // Keyword:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005926 pattern = asdl_seq_GET(kwd_patterns, i - nargs);
Brandt Bucher145bf262021-02-26 14:51:55 -08005927 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005928 if (WILDCARD_CHECK(pattern)) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005929 continue;
5930 }
5931 // Get the i-th attribute, and match it against the i-th pattern:
5932 ADDOP(c, DUP_TOP);
5933 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5934 ADDOP(c, BINARY_SUBSCR);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005935 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08005936 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5937 NEXT_BLOCK(c);
5938 }
5939 // Success! Pop the tuple of attributes:
5940 ADDOP(c, POP_TOP);
5941 ADDOP_LOAD_CONST(c, Py_True);
5942 ADDOP_JUMP(c, JUMP_FORWARD, end);
5943 compiler_use_next_block(c, fail_pop_1);
5944 ADDOP(c, POP_TOP);
5945 ADDOP_LOAD_CONST(c, Py_False);
5946 compiler_use_next_block(c, end);
5947 return 1;
5948}
5949
Brandt Bucher145bf262021-02-26 14:51:55 -08005950static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005951compiler_pattern_mapping(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005952{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005953 assert(p->kind == MatchMapping_kind);
Brandt Bucher145bf262021-02-26 14:51:55 -08005954 basicblock *end, *fail_pop_1, *fail_pop_3;
5955 RETURN_IF_FALSE(end = compiler_new_block(c));
5956 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5957 RETURN_IF_FALSE(fail_pop_3 = compiler_new_block(c));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005958 asdl_expr_seq *keys = p->v.MatchMapping.keys;
5959 asdl_pattern_seq *patterns = p->v.MatchMapping.patterns;
5960 Py_ssize_t size = asdl_seq_LEN(keys);
5961 Py_ssize_t npatterns = asdl_seq_LEN(patterns);
5962 if (size != npatterns) {
5963 // AST validator shouldn't let this happen, but if it does,
5964 // just fail, don't crash out of the interpreter
5965 const char * e = "keys (%d) / patterns (%d) length mismatch in mapping pattern";
5966 return compiler_error(c, e, size, npatterns);
5967 }
5968 // We have a double-star target if "rest" is set
5969 PyObject *star_target = p->v.MatchMapping.rest;
Brandt Bucher145bf262021-02-26 14:51:55 -08005970 ADDOP(c, MATCH_MAPPING);
5971 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5972 NEXT_BLOCK(c);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005973 if (!size && !star_target) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005974 // If the pattern is just "{}", we're done!
5975 ADDOP(c, POP_TOP);
5976 ADDOP_LOAD_CONST(c, Py_True);
5977 ADDOP_JUMP(c, JUMP_FORWARD, end);
5978 compiler_use_next_block(c, fail_pop_1);
5979 ADDOP(c, POP_TOP);
5980 ADDOP_LOAD_CONST(c, Py_False);
5981 compiler_use_next_block(c, end);
5982 return 1;
5983 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005984 if (size) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005985 // If the pattern has any keys in it, perform a length check:
5986 ADDOP(c, GET_LEN);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005987 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
Brandt Bucher145bf262021-02-26 14:51:55 -08005988 ADDOP_COMPARE(c, GtE);
5989 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5990 NEXT_BLOCK(c);
5991 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005992 if (INT_MAX < size - 1) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005993 return compiler_error(c, "too many sub-patterns in mapping pattern");
5994 }
5995 // Collect all of the keys into a tuple for MATCH_KEYS and
5996 // COPY_DICT_WITHOUT_KEYS. They can either be dotted names or literals:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005997 for (Py_ssize_t i = 0; i < size; i++) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005998 expr_ty key = asdl_seq_GET(keys, i);
5999 if (key == NULL) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006000 const char *e = "can't use NULL keys in MatchMapping "
6001 "(set 'rest' parameter instead)";
6002 c->u->u_col_offset = ((pattern_ty) asdl_seq_GET(patterns, i))->col_offset;
6003 return compiler_error(c, e);
6004 }
6005 if (!MATCH_VALUE_EXPR(key)) {
6006 const char *e = "mapping pattern keys may only match literals and attribute lookups";
Brandt Bucher145bf262021-02-26 14:51:55 -08006007 return compiler_error(c, e);
6008 }
6009 VISIT(c, expr, key);
6010 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006011 ADDOP_I(c, BUILD_TUPLE, size);
Brandt Bucher145bf262021-02-26 14:51:55 -08006012 ADDOP(c, MATCH_KEYS);
6013 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3);
6014 NEXT_BLOCK(c);
6015 // So far so good. There's now a tuple of values on the stack to match
6016 // sub-patterns against:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006017 for (Py_ssize_t i = 0; i < size; i++) {
6018 pattern_ty pattern = asdl_seq_GET(patterns, i);
6019 if (WILDCARD_CHECK(pattern)) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006020 continue;
6021 }
6022 ADDOP(c, DUP_TOP);
6023 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
6024 ADDOP(c, BINARY_SUBSCR);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006025 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006026 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3);
6027 NEXT_BLOCK(c);
6028 }
6029 // If we get this far, it's a match! We're done with that tuple of values.
6030 ADDOP(c, POP_TOP);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006031 if (star_target) {
6032 // If we have a starred name, bind a dict of remaining items to it:
Brandt Bucher145bf262021-02-26 14:51:55 -08006033 ADDOP(c, COPY_DICT_WITHOUT_KEYS);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006034 RETURN_IF_FALSE(pattern_helper_store_name(c, star_target, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006035 }
6036 else {
6037 // Otherwise, we don't care about this tuple of keys anymore:
6038 ADDOP(c, POP_TOP);
6039 }
6040 // Pop the subject:
6041 ADDOP(c, POP_TOP);
6042 ADDOP_LOAD_CONST(c, Py_True);
6043 ADDOP_JUMP(c, JUMP_FORWARD, end);
6044 // The top two items are a tuple of values or None, followed by a tuple of
6045 // keys. Pop them both:
6046 compiler_use_next_block(c, fail_pop_3);
6047 ADDOP(c, POP_TOP);
6048 ADDOP(c, POP_TOP);
6049 compiler_use_next_block(c, fail_pop_1);
6050 // Pop the subject:
6051 ADDOP(c, POP_TOP);
6052 ADDOP_LOAD_CONST(c, Py_False);
6053 compiler_use_next_block(c, end);
6054 return 1;
6055}
6056
Brandt Bucher145bf262021-02-26 14:51:55 -08006057static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006058compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006059{
6060 assert(p->kind == MatchOr_kind);
6061 // control is the set of names bound by the first alternative. If all of the
6062 // others bind the same names (they should), then this becomes pc->stores.
6063 PyObject *control = NULL;
6064 basicblock *end, *pass_pop_1;
6065 RETURN_IF_FALSE(end = compiler_new_block(c));
6066 RETURN_IF_FALSE(pass_pop_1 = compiler_new_block(c));
6067 Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns);
6068 assert(size > 1);
6069 // We're going to be messing with pc. Keep the original info handy:
6070 PyObject *stores_init = pc->stores;
6071 int allow_irrefutable = pc->allow_irrefutable;
6072 for (Py_ssize_t i = 0; i < size; i++) {
6073 // NOTE: Can't use our nice returning macros in here: they'll leak sets!
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006074 pattern_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i);
Brandt Bucher145bf262021-02-26 14:51:55 -08006075 pc->stores = PySet_New(stores_init);
6076 // An irrefutable sub-pattern must be last, if it is allowed at all:
6077 int is_last = i == size - 1;
6078 pc->allow_irrefutable = allow_irrefutable && is_last;
6079 SET_LOC(c, alt);
6080 if (pc->stores == NULL ||
6081 // Only copy the subject if we're *not* on the last alternative:
6082 (!is_last && !compiler_addop(c, DUP_TOP)) ||
6083 !compiler_pattern(c, alt, pc) ||
6084 // Only jump if we're *not* on the last alternative:
6085 (!is_last && !compiler_addop_j(c, POP_JUMP_IF_TRUE, pass_pop_1)) ||
6086 !compiler_next_block(c))
6087 {
6088 goto fail;
6089 }
6090 if (!i) {
6091 // If this is the first alternative, save its stores as a "control"
6092 // for the others (they can't bind a different set of names):
6093 control = pc->stores;
6094 continue;
6095 }
6096 if (PySet_GET_SIZE(pc->stores) || PySet_GET_SIZE(control)) {
6097 // Otherwise, check to see if we differ from the control set:
6098 PyObject *diff = PyNumber_InPlaceXor(pc->stores, control);
6099 if (diff == NULL) {
6100 goto fail;
6101 }
6102 if (PySet_GET_SIZE(diff)) {
6103 // The names differ! Raise.
6104 Py_DECREF(diff);
6105 compiler_error(c, "alternative patterns bind different names");
6106 goto fail;
6107 }
6108 Py_DECREF(diff);
6109 }
6110 Py_DECREF(pc->stores);
6111 }
6112 Py_XDECREF(stores_init);
6113 // Update pc->stores and restore pc->allow_irrefutable:
6114 pc->stores = control;
6115 pc->allow_irrefutable = allow_irrefutable;
6116 ADDOP_JUMP(c, JUMP_FORWARD, end);
6117 compiler_use_next_block(c, pass_pop_1);
6118 ADDOP(c, POP_TOP);
6119 ADDOP_LOAD_CONST(c, Py_True);
6120 compiler_use_next_block(c, end);
6121 return 1;
6122fail:
6123 Py_XDECREF(stores_init);
6124 Py_XDECREF(control);
6125 return 0;
6126}
6127
6128
6129static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006130compiler_pattern_sequence(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006131{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006132 assert(p->kind == MatchSequence_kind);
6133 asdl_pattern_seq *patterns = p->v.MatchSequence.patterns;
6134 Py_ssize_t size = asdl_seq_LEN(patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08006135 Py_ssize_t star = -1;
6136 int only_wildcard = 1;
6137 int star_wildcard = 0;
6138 // Find a starred name, if it exists. There may be at most one:
6139 for (Py_ssize_t i = 0; i < size; i++) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006140 pattern_ty pattern = asdl_seq_GET(patterns, i);
6141 if (pattern->kind == MatchStar_kind) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006142 if (star >= 0) {
6143 const char *e = "multiple starred names in sequence pattern";
6144 return compiler_error(c, e);
6145 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006146 star_wildcard = WILDCARD_STAR_CHECK(pattern);
6147 only_wildcard &= star_wildcard;
Brandt Bucher145bf262021-02-26 14:51:55 -08006148 star = i;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006149 continue;
Brandt Bucher145bf262021-02-26 14:51:55 -08006150 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006151 only_wildcard &= WILDCARD_CHECK(pattern);
Brandt Bucher145bf262021-02-26 14:51:55 -08006152 }
6153 basicblock *end, *fail_pop_1;
6154 RETURN_IF_FALSE(end = compiler_new_block(c));
6155 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
6156 ADDOP(c, MATCH_SEQUENCE);
6157 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
6158 NEXT_BLOCK(c);
6159 if (star < 0) {
6160 // No star: len(subject) == size
6161 ADDOP(c, GET_LEN);
6162 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
6163 ADDOP_COMPARE(c, Eq);
6164 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
6165 NEXT_BLOCK(c);
6166 }
6167 else if (size > 1) {
6168 // Star: len(subject) >= size - 1
6169 ADDOP(c, GET_LEN);
6170 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1));
6171 ADDOP_COMPARE(c, GtE);
6172 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
6173 NEXT_BLOCK(c);
6174 }
6175 if (only_wildcard) {
6176 // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc.
6177 ADDOP(c, POP_TOP);
6178 ADDOP_LOAD_CONST(c, Py_True);
6179 }
6180 else if (star_wildcard) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006181 RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, patterns, star, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006182 }
6183 else {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006184 RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, patterns, star, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006185 }
6186 ADDOP_JUMP(c, JUMP_FORWARD, end);
6187 compiler_use_next_block(c, fail_pop_1);
6188 ADDOP(c, POP_TOP)
6189 ADDOP_LOAD_CONST(c, Py_False);
6190 compiler_use_next_block(c, end);
6191 return 1;
6192}
6193
Brandt Bucher145bf262021-02-26 14:51:55 -08006194static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006195compiler_pattern_value(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006196{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006197 assert(p->kind == MatchValue_kind);
6198 expr_ty value = p->v.MatchValue.value;
6199 if (!MATCH_VALUE_EXPR(value)) {
6200 const char *e = "patterns may only match literals and attribute lookups";
6201 return compiler_error(c, e);
6202 }
6203 VISIT(c, expr, value);
Brandt Bucher145bf262021-02-26 14:51:55 -08006204 ADDOP_COMPARE(c, Eq);
6205 return 1;
6206}
6207
Brandt Bucher145bf262021-02-26 14:51:55 -08006208static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006209compiler_pattern_constant(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006210{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006211 assert(p->kind == MatchSingleton_kind);
6212 ADDOP_LOAD_CONST(c, p->v.MatchSingleton.value);
6213 ADDOP_COMPARE(c, Is);
Brandt Bucher145bf262021-02-26 14:51:55 -08006214 return 1;
6215}
6216
Brandt Bucher145bf262021-02-26 14:51:55 -08006217static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006218compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006219{
6220 SET_LOC(c, p);
6221 switch (p->kind) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006222 case MatchValue_kind:
Brandt Bucher145bf262021-02-26 14:51:55 -08006223 return compiler_pattern_value(c, p, pc);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006224 case MatchSingleton_kind:
6225 return compiler_pattern_constant(c, p, pc);
6226 case MatchSequence_kind:
Brandt Bucher145bf262021-02-26 14:51:55 -08006227 return compiler_pattern_sequence(c, p, pc);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006228 case MatchMapping_kind:
6229 return compiler_pattern_mapping(c, p, pc);
6230 case MatchClass_kind:
6231 return compiler_pattern_class(c, p, pc);
6232 case MatchStar_kind:
6233 return compiler_pattern_star(c, p, pc);
Brandt Bucher145bf262021-02-26 14:51:55 -08006234 case MatchAs_kind:
6235 return compiler_pattern_as(c, p, pc);
6236 case MatchOr_kind:
6237 return compiler_pattern_or(c, p, pc);
Brandt Bucher145bf262021-02-26 14:51:55 -08006238 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006239 // AST validator shouldn't let this happen, but if it does,
6240 // just fail, don't crash out of the interpreter
6241 const char *e = "invalid match pattern node in AST (kind=%d)";
6242 return compiler_error(c, e, p->kind);
Brandt Bucher145bf262021-02-26 14:51:55 -08006243}
6244
Brandt Bucher145bf262021-02-26 14:51:55 -08006245static int
6246compiler_match(struct compiler *c, stmt_ty s)
6247{
6248 VISIT(c, expr, s->v.Match.subject);
6249 basicblock *next, *end;
6250 RETURN_IF_FALSE(end = compiler_new_block(c));
6251 Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006252 assert(cases > 0);
Brandt Bucher145bf262021-02-26 14:51:55 -08006253 pattern_context pc;
6254 // We use pc.stores to track:
6255 // - Repeated name assignments in the same pattern.
6256 // - Different name assignments in alternatives.
6257 // It's a set of names, but we don't create it until it's needed:
6258 pc.stores = NULL;
6259 match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6260 int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases;
6261 for (Py_ssize_t i = 0; i < cases - has_default; i++) {
6262 m = asdl_seq_GET(s->v.Match.cases, i);
6263 SET_LOC(c, m->pattern);
6264 RETURN_IF_FALSE(next = compiler_new_block(c));
6265 // If pc.allow_irrefutable is 0, any name captures against our subject
6266 // will raise. Irrefutable cases must be either guarded, last, or both:
6267 pc.allow_irrefutable = m->guard != NULL || i == cases - 1;
6268 // Only copy the subject if we're *not* on the last case:
6269 if (i != cases - has_default - 1) {
6270 ADDOP(c, DUP_TOP);
6271 }
6272 int result = compiler_pattern(c, m->pattern, &pc);
6273 Py_CLEAR(pc.stores);
6274 RETURN_IF_FALSE(result);
6275 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, next);
6276 NEXT_BLOCK(c);
6277 if (m->guard) {
6278 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, next, 0));
6279 }
6280 // Success! Pop the subject off, we're done with it:
6281 if (i != cases - has_default - 1) {
6282 ADDOP(c, POP_TOP);
6283 }
6284 VISIT_SEQ(c, stmt, m->body);
6285 ADDOP_JUMP(c, JUMP_FORWARD, end);
6286 compiler_use_next_block(c, next);
6287 }
6288 if (has_default) {
6289 if (cases == 1) {
6290 // No matches. Done with the subject:
6291 ADDOP(c, POP_TOP);
6292 }
6293 // A trailing "case _" is common, and lets us save a bit of redundant
6294 // pushing and popping in the loop above:
6295 m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6296 SET_LOC(c, m->pattern);
6297 if (m->guard) {
6298 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0));
6299 }
6300 VISIT_SEQ(c, stmt, m->body);
6301 }
6302 compiler_use_next_block(c, end);
6303 return 1;
6304}
6305
Brandt Bucher145bf262021-02-26 14:51:55 -08006306#undef WILDCARD_CHECK
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006307#undef WILDCARD_STAR_CHECK
Brandt Bucher145bf262021-02-26 14:51:55 -08006308
Thomas Wouters89f507f2006-12-13 04:49:30 +00006309/* End of the compiler section, beginning of the assembler section */
6310
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006311/* do depth-first search of basic block graph, starting with block.
T. Wouters99b54d62019-09-12 07:05:33 -07006312 post records the block indices in post-order.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006313
6314 XXX must handle implicit jumps from one block to next
6315*/
6316
Thomas Wouters89f507f2006-12-13 04:49:30 +00006317struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006318 PyObject *a_bytecode; /* string containing bytecode */
6319 int a_offset; /* offset into bytecode */
6320 int a_nblocks; /* number of reachable blocks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006321 PyObject *a_lnotab; /* string containing lnotab */
6322 int a_lnotab_off; /* offset into lnotab */
Mark Shannon877df852020-11-12 09:43:29 +00006323 int a_prevlineno; /* lineno of last emitted line in line table */
6324 int a_lineno; /* lineno of last emitted instruction */
6325 int a_lineno_start; /* bytecode start offset of current lineno */
Mark Shannoncc75ab72020-11-12 19:49:33 +00006326 basicblock *a_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006327};
6328
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006329Py_LOCAL_INLINE(void)
6330stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006331{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02006332 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
Mark Shannonfee55262019-11-21 09:11:43 +00006333 if (b->b_startdepth < depth && b->b_startdepth < 100) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006334 assert(b->b_startdepth < 0);
6335 b->b_startdepth = depth;
6336 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02006337 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006338}
6339
6340/* Find the flow path that needs the largest stack. We assume that
6341 * cycles in the flow graph have no net effect on the stack depth.
6342 */
6343static int
6344stackdepth(struct compiler *c)
6345{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006346 basicblock *b, *entryblock = NULL;
6347 basicblock **stack, **sp;
6348 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006349 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006350 b->b_startdepth = INT_MIN;
6351 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006352 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006353 }
Mark Shannon67969f52021-04-07 10:52:07 +01006354 assert(entryblock!= NULL);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006355 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
6356 if (!stack) {
6357 PyErr_NoMemory();
6358 return -1;
6359 }
6360
6361 sp = stack;
Mark Shannonb37181e2021-04-06 11:48:59 +01006362 if (c->u->u_ste->ste_generator || c->u->u_ste->ste_coroutine) {
6363 stackdepth_push(&sp, entryblock, 1);
6364 } else {
6365 stackdepth_push(&sp, entryblock, 0);
6366 }
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006367 while (sp != stack) {
6368 b = *--sp;
6369 int depth = b->b_startdepth;
6370 assert(depth >= 0);
6371 basicblock *next = b->b_next;
6372 for (int i = 0; i < b->b_iused; i++) {
6373 struct instr *instr = &b->b_instr[i];
6374 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
6375 if (effect == PY_INVALID_STACK_EFFECT) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01006376 PyErr_Format(PyExc_SystemError,
6377 "compiler stack_effect(opcode=%d, arg=%i) failed",
6378 instr->i_opcode, instr->i_oparg);
6379 return -1;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006380 }
6381 int new_depth = depth + effect;
6382 if (new_depth > maxdepth) {
6383 maxdepth = new_depth;
6384 }
6385 assert(depth >= 0); /* invalid code or bug in stackdepth() */
Mark Shannon582aaf12020-08-04 17:30:11 +01006386 if (is_jump(instr)) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006387 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
6388 assert(effect != PY_INVALID_STACK_EFFECT);
6389 int target_depth = depth + effect;
6390 if (target_depth > maxdepth) {
6391 maxdepth = target_depth;
6392 }
6393 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006394 stackdepth_push(&sp, instr->i_target, target_depth);
6395 }
6396 depth = new_depth;
6397 if (instr->i_opcode == JUMP_ABSOLUTE ||
6398 instr->i_opcode == JUMP_FORWARD ||
6399 instr->i_opcode == RETURN_VALUE ||
Mark Shannonfee55262019-11-21 09:11:43 +00006400 instr->i_opcode == RAISE_VARARGS ||
6401 instr->i_opcode == RERAISE)
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006402 {
6403 /* remaining code is dead */
6404 next = NULL;
6405 break;
6406 }
6407 }
6408 if (next != NULL) {
Mark Shannon266b4622020-11-17 19:30:14 +00006409 assert(b->b_nofallthrough == 0);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006410 stackdepth_push(&sp, next, depth);
6411 }
6412 }
6413 PyObject_Free(stack);
6414 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006415}
6416
6417static int
6418assemble_init(struct assembler *a, int nblocks, int firstlineno)
6419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006420 memset(a, 0, sizeof(struct assembler));
Mark Shannon877df852020-11-12 09:43:29 +00006421 a->a_prevlineno = a->a_lineno = firstlineno;
Mark Shannonfd009e62020-11-13 12:53:53 +00006422 a->a_lnotab = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006423 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006424 if (a->a_bytecode == NULL) {
6425 goto error;
6426 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006427 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006428 if (a->a_lnotab == NULL) {
6429 goto error;
6430 }
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07006431 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006432 PyErr_NoMemory();
Mark Shannonfd009e62020-11-13 12:53:53 +00006433 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006434 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006435 return 1;
Mark Shannonfd009e62020-11-13 12:53:53 +00006436error:
6437 Py_XDECREF(a->a_bytecode);
6438 Py_XDECREF(a->a_lnotab);
6439 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006440}
6441
6442static void
6443assemble_free(struct assembler *a)
6444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006445 Py_XDECREF(a->a_bytecode);
6446 Py_XDECREF(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006447}
6448
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006449static int
6450blocksize(basicblock *b)
6451{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006452 int i;
6453 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006455 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006456 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006457 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006458}
6459
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006460static int
Mark Shannon877df852020-11-12 09:43:29 +00006461assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006462{
Mark Shannon877df852020-11-12 09:43:29 +00006463 Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006464 if (a->a_lnotab_off + 2 >= len) {
6465 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
6466 return 0;
6467 }
Pablo Galindo86e322f2021-01-30 13:54:22 +00006468 unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab);
6469 lnotab += a->a_lnotab_off;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006470 a->a_lnotab_off += 2;
Mark Shannon877df852020-11-12 09:43:29 +00006471 *lnotab++ = bdelta;
6472 *lnotab++ = ldelta;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006473 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006474}
6475
Mark Shannon877df852020-11-12 09:43:29 +00006476/* Appends a range to the end of the line number table. See
6477 * Objects/lnotab_notes.txt for the description of the line number table. */
6478
6479static int
6480assemble_line_range(struct assembler *a)
6481{
6482 int ldelta, bdelta;
6483 bdelta = (a->a_offset - a->a_lineno_start) * 2;
6484 if (bdelta == 0) {
6485 return 1;
6486 }
6487 if (a->a_lineno < 0) {
6488 ldelta = -128;
6489 }
6490 else {
6491 ldelta = a->a_lineno - a->a_prevlineno;
6492 a->a_prevlineno = a->a_lineno;
6493 while (ldelta > 127) {
6494 if (!assemble_emit_linetable_pair(a, 0, 127)) {
6495 return 0;
6496 }
6497 ldelta -= 127;
6498 }
6499 while (ldelta < -127) {
6500 if (!assemble_emit_linetable_pair(a, 0, -127)) {
6501 return 0;
6502 }
6503 ldelta += 127;
6504 }
6505 }
6506 assert(-128 <= ldelta && ldelta < 128);
6507 while (bdelta > 254) {
6508 if (!assemble_emit_linetable_pair(a, 254, ldelta)) {
6509 return 0;
6510 }
6511 ldelta = a->a_lineno < 0 ? -128 : 0;
6512 bdelta -= 254;
6513 }
6514 if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) {
6515 return 0;
6516 }
6517 a->a_lineno_start = a->a_offset;
6518 return 1;
6519}
6520
6521static int
6522assemble_lnotab(struct assembler *a, struct instr *i)
6523{
6524 if (i->i_lineno == a->a_lineno) {
6525 return 1;
6526 }
6527 if (!assemble_line_range(a)) {
6528 return 0;
6529 }
6530 a->a_lineno = i->i_lineno;
6531 return 1;
6532}
6533
6534
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006535/* assemble_emit()
6536 Extend the bytecode with a new instruction.
6537 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00006538*/
6539
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006540static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006541assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006542{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006543 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006544 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03006545 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006546
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006547 arg = i->i_oparg;
6548 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006549 if (i->i_lineno && !assemble_lnotab(a, i))
6550 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006551 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006552 if (len > PY_SSIZE_T_MAX / 2)
6553 return 0;
6554 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
6555 return 0;
6556 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03006557 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006558 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006559 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006560 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006561}
6562
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00006563static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006564assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006566 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006567 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006568 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00006569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006570 /* Compute the size of each block and fixup jump args.
6571 Replace block pointer with position in bytecode. */
6572 do {
6573 totsize = 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006574 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006575 bsize = blocksize(b);
6576 b->b_offset = totsize;
6577 totsize += bsize;
6578 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006579 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006580 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6581 bsize = b->b_offset;
6582 for (i = 0; i < b->b_iused; i++) {
6583 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006584 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006585 /* Relative jumps are computed relative to
6586 the instruction pointer after fetching
6587 the jump instruction.
6588 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006589 bsize += isize;
Mark Shannon582aaf12020-08-04 17:30:11 +01006590 if (is_jump(instr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006591 instr->i_oparg = instr->i_target->b_offset;
Mark Shannon582aaf12020-08-04 17:30:11 +01006592 if (is_relative_jump(instr)) {
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006593 instr->i_oparg -= bsize;
6594 }
6595 if (instrsize(instr->i_oparg) != isize) {
6596 extended_arg_recompile = 1;
6597 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006598 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006599 }
6600 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00006601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006602 /* XXX: This is an awful hack that could hurt performance, but
6603 on the bright side it should work until we come up
6604 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006606 The issue is that in the first loop blocksize() is called
6607 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006608 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006609 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006611 So we loop until we stop seeing new EXTENDED_ARGs.
6612 The only EXTENDED_ARGs that could be popping up are
6613 ones in jump instructions. So this should converge
6614 fairly quickly.
6615 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006616 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00006617}
6618
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006619static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01006620dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006622 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006623 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006625 tuple = PyTuple_New(size);
6626 if (tuple == NULL)
6627 return NULL;
6628 while (PyDict_Next(dict, &pos, &k, &v)) {
6629 i = PyLong_AS_LONG(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006630 Py_INCREF(k);
6631 assert((i - offset) < size);
6632 assert((i - offset) >= 0);
6633 PyTuple_SET_ITEM(tuple, i - offset, k);
6634 }
6635 return tuple;
6636}
6637
6638static PyObject *
6639consts_dict_keys_inorder(PyObject *dict)
6640{
6641 PyObject *consts, *k, *v;
6642 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
6643
6644 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
6645 if (consts == NULL)
6646 return NULL;
6647 while (PyDict_Next(dict, &pos, &k, &v)) {
6648 i = PyLong_AS_LONG(v);
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +03006649 /* The keys of the dictionary can be tuples wrapping a contant.
6650 * (see compiler_add_o and _PyCode_ConstantKey). In that case
6651 * the object we want is always second. */
6652 if (PyTuple_CheckExact(k)) {
6653 k = PyTuple_GET_ITEM(k, 1);
6654 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006655 Py_INCREF(k);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006656 assert(i < size);
6657 assert(i >= 0);
6658 PyList_SET_ITEM(consts, i, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006659 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006660 return consts;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006661}
6662
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006663static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006664compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006665{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006666 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006667 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006668 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04006669 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006670 if (ste->ste_nested)
6671 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07006672 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006673 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07006674 if (!ste->ste_generator && ste->ste_coroutine)
6675 flags |= CO_COROUTINE;
6676 if (ste->ste_generator && ste->ste_coroutine)
6677 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006678 if (ste->ste_varargs)
6679 flags |= CO_VARARGS;
6680 if (ste->ste_varkeywords)
6681 flags |= CO_VARKEYWORDS;
6682 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006684 /* (Only) inherit compilerflags in PyCF_MASK */
6685 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006686
Pablo Galindo90235812020-03-15 04:29:22 +00006687 if ((IS_TOP_LEVEL_AWAIT(c)) &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07006688 ste->ste_coroutine &&
6689 !ste->ste_generator) {
6690 flags |= CO_COROUTINE;
6691 }
6692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006693 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00006694}
6695
Inada Naokibdb941b2021-02-10 09:20:42 +09006696// Merge *obj* with constant cache.
INADA Naokic2e16072018-11-26 21:23:22 +09006697// Unlike merge_consts_recursive(), this function doesn't work recursively.
6698static int
Inada Naokibdb941b2021-02-10 09:20:42 +09006699merge_const_one(struct compiler *c, PyObject **obj)
INADA Naokic2e16072018-11-26 21:23:22 +09006700{
Inada Naokibdb941b2021-02-10 09:20:42 +09006701 PyObject *key = _PyCode_ConstantKey(*obj);
INADA Naokic2e16072018-11-26 21:23:22 +09006702 if (key == NULL) {
6703 return 0;
6704 }
6705
6706 // t is borrowed reference
6707 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
6708 Py_DECREF(key);
6709 if (t == NULL) {
6710 return 0;
6711 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006712 if (t == key) { // obj is new constant.
INADA Naokic2e16072018-11-26 21:23:22 +09006713 return 1;
6714 }
6715
Inada Naokibdb941b2021-02-10 09:20:42 +09006716 if (PyTuple_CheckExact(t)) {
6717 // t is still borrowed reference
6718 t = PyTuple_GET_ITEM(t, 1);
6719 }
6720
6721 Py_INCREF(t);
6722 Py_DECREF(*obj);
6723 *obj = t;
INADA Naokic2e16072018-11-26 21:23:22 +09006724 return 1;
6725}
6726
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006727static PyCodeObject *
Mark Shannon6e8128f2020-07-30 10:03:00 +01006728makecode(struct compiler *c, struct assembler *a, PyObject *consts)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006730 PyCodeObject *co = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006731 PyObject *names = NULL;
6732 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006733 PyObject *name = NULL;
6734 PyObject *freevars = NULL;
6735 PyObject *cellvars = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006736 Py_ssize_t nlocals;
6737 int nlocals_int;
6738 int flags;
Pablo Galindocd74e662019-06-01 18:08:04 +01006739 int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006741 names = dict_keys_inorder(c->u->u_names, 0);
6742 varnames = dict_keys_inorder(c->u->u_varnames, 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006743 if (!names || !varnames) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006744 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006745 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006746 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
6747 if (!cellvars)
6748 goto error;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006749 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006750 if (!freevars)
6751 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006752
Inada Naokibdb941b2021-02-10 09:20:42 +09006753 if (!merge_const_one(c, &names) ||
6754 !merge_const_one(c, &varnames) ||
6755 !merge_const_one(c, &cellvars) ||
6756 !merge_const_one(c, &freevars))
INADA Naokic2e16072018-11-26 21:23:22 +09006757 {
6758 goto error;
6759 }
6760
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006761 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01006762 assert(nlocals < INT_MAX);
6763 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
6764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006765 flags = compute_code_flags(c);
6766 if (flags < 0)
6767 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006768
Mark Shannon6e8128f2020-07-30 10:03:00 +01006769 consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
6770 if (consts == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006771 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006772 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006773 if (!merge_const_one(c, &consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006774 Py_DECREF(consts);
INADA Naokic2e16072018-11-26 21:23:22 +09006775 goto error;
6776 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006777
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01006778 posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int);
Pablo Galindocd74e662019-06-01 18:08:04 +01006779 posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +01006780 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006781 maxdepth = stackdepth(c);
6782 if (maxdepth < 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006783 Py_DECREF(consts);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006784 goto error;
6785 }
Mark Shannon11e0b292021-04-15 14:28:56 +01006786 if (maxdepth > MAX_ALLOWED_STACK_USE) {
6787 PyErr_Format(PyExc_SystemError,
6788 "excessive stack use: stack is %d deep",
6789 maxdepth);
6790 Py_DECREF(consts);
6791 goto error;
6792 }
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006793 co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
Mark Shannon13bc1392020-01-23 09:25:17 +00006794 posonlyargcount, kwonlyargcount, nlocals_int,
Mark Shannon6e8128f2020-07-30 10:03:00 +01006795 maxdepth, flags, a->a_bytecode, consts, names,
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006796 varnames, freevars, cellvars, c->c_filename,
6797 c->u->u_name, c->u->u_firstlineno, a->a_lnotab);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006798 Py_DECREF(consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006799 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006800 Py_XDECREF(names);
6801 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006802 Py_XDECREF(name);
6803 Py_XDECREF(freevars);
6804 Py_XDECREF(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006805 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006806}
6807
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006808
6809/* For debugging purposes only */
6810#if 0
6811static void
6812dump_instr(const struct instr *i)
6813{
Mark Shannon582aaf12020-08-04 17:30:11 +01006814 const char *jrel = (is_relative_jump(instr)) ? "jrel " : "";
6815 const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : "";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006816 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006818 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006819 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006820 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006821 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006822 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
6823 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006824}
6825
6826static void
6827dump_basicblock(const basicblock *b)
6828{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006829 const char *b_return = b->b_return ? "return " : "";
Pablo Galindo60eb9f12020-06-28 01:55:47 +01006830 fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n",
6831 b->b_iused, b->b_startdepth, b->b_offset, b_return);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006832 if (b->b_instr) {
6833 int i;
6834 for (i = 0; i < b->b_iused; i++) {
6835 fprintf(stderr, " [%02d] ", i);
6836 dump_instr(b->b_instr + i);
6837 }
6838 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006839}
6840#endif
6841
Mark Shannon5977a792020-12-02 13:31:40 +00006842
6843static int
6844normalize_basic_block(basicblock *bb);
6845
Mark Shannon6e8128f2020-07-30 10:03:00 +01006846static int
Inada Naoki8a232c72021-04-16 14:01:04 +09006847optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006848
Mark Shannon5977a792020-12-02 13:31:40 +00006849static int
6850ensure_exits_have_lineno(struct compiler *c);
6851
Mark Shannonb37181e2021-04-06 11:48:59 +01006852static int
6853insert_generator_prefix(struct compiler *c, basicblock *entryblock) {
6854
6855 int flags = compute_code_flags(c);
6856 if (flags < 0) {
6857 return -1;
6858 }
6859 int kind;
6860 if (flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
6861 if (flags & CO_COROUTINE) {
6862 kind = 1;
6863 }
6864 else if (flags & CO_ASYNC_GENERATOR) {
6865 kind = 2;
6866 }
6867 else {
6868 kind = 0;
6869 }
6870 }
6871 else {
6872 return 0;
6873 }
6874 if (compiler_next_instr(entryblock) < 0) {
6875 return -1;
6876 }
6877 for (int i = entryblock->b_iused-1; i > 0; i--) {
6878 entryblock->b_instr[i] = entryblock->b_instr[i-1];
6879 }
6880 entryblock->b_instr[0].i_opcode = GEN_START;
6881 entryblock->b_instr[0].i_oparg = kind;
6882 entryblock->b_instr[0].i_lineno = -1;
6883 entryblock->b_instr[0].i_target = NULL;
6884 return 0;
6885}
6886
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006887static PyCodeObject *
6888assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006889{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006890 basicblock *b, *entryblock;
6891 struct assembler a;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006892 int j, nblocks;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006893 PyCodeObject *co = NULL;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006894 PyObject *consts = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006896 /* Make sure every block that falls off the end returns None.
6897 XXX NEXT_BLOCK() isn't quite right, because if the last
6898 block ends with a jump or return b_next shouldn't set.
6899 */
6900 if (!c->u->u_curblock->b_return) {
Mark Shannon877df852020-11-12 09:43:29 +00006901 c->u->u_lineno = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006902 if (addNone)
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006903 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006904 ADDOP(c, RETURN_VALUE);
6905 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006906
Mark Shannon5977a792020-12-02 13:31:40 +00006907 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
6908 if (normalize_basic_block(b)) {
Alex Henrie503627f2021-03-02 03:20:25 -07006909 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00006910 }
6911 }
6912
6913 if (ensure_exits_have_lineno(c)) {
Alex Henrie503627f2021-03-02 03:20:25 -07006914 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00006915 }
6916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006917 nblocks = 0;
6918 entryblock = NULL;
6919 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6920 nblocks++;
6921 entryblock = b;
6922 }
Mark Shannon67969f52021-04-07 10:52:07 +01006923 assert(entryblock != NULL);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006924
Mark Shannonb37181e2021-04-06 11:48:59 +01006925 if (insert_generator_prefix(c, entryblock)) {
6926 goto error;
6927 }
6928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006929 /* Set firstlineno if it wasn't explicitly set. */
6930 if (!c->u->u_firstlineno) {
Mark Shannon67969f52021-04-07 10:52:07 +01006931 if (entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006932 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
Mark Shannon877df852020-11-12 09:43:29 +00006933 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006934 c->u->u_firstlineno = 1;
6935 }
Mark Shannon5977a792020-12-02 13:31:40 +00006936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006937 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
6938 goto error;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006939 a.a_entry = entryblock;
6940 a.a_nblocks = nblocks;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006941
Mark Shannon6e8128f2020-07-30 10:03:00 +01006942 consts = consts_dict_keys_inorder(c->u->u_consts);
6943 if (consts == NULL) {
6944 goto error;
6945 }
Inada Naoki8a232c72021-04-16 14:01:04 +09006946 if (optimize_cfg(c, &a, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006947 goto error;
6948 }
6949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006950 /* Can't modify the bytecode after computing jump offsets. */
6951 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00006952
Mark Shannoncc75ab72020-11-12 19:49:33 +00006953 /* Emit code. */
6954 for(b = entryblock; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006955 for (j = 0; j < b->b_iused; j++)
6956 if (!assemble_emit(&a, &b->b_instr[j]))
6957 goto error;
6958 }
Mark Shannon877df852020-11-12 09:43:29 +00006959 if (!assemble_line_range(&a)) {
6960 return 0;
6961 }
6962 /* Emit sentinel at end of line number table */
6963 if (!assemble_emit_linetable_pair(&a, 255, -128)) {
6964 goto error;
6965 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00006966
Inada Naokibdb941b2021-02-10 09:20:42 +09006967 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006968 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09006969 }
6970 if (!merge_const_one(c, &a.a_lnotab)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006971 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09006972 }
6973 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) {
6974 goto error;
6975 }
6976 if (!merge_const_one(c, &a.a_bytecode)) {
6977 goto error;
6978 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006979
Mark Shannon6e8128f2020-07-30 10:03:00 +01006980 co = makecode(c, &a, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006981 error:
Mark Shannon6e8128f2020-07-30 10:03:00 +01006982 Py_XDECREF(consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006983 assemble_free(&a);
6984 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006985}
Georg Brandl8334fd92010-12-04 10:26:46 +00006986
Mark Shannon6e8128f2020-07-30 10:03:00 +01006987/* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
6988 with LOAD_CONST (c1, c2, ... cn).
6989 The consts table must still be in list form so that the
6990 new constant (c1, c2, ... cn) can be appended.
6991 Called with codestr pointing to the first LOAD_CONST.
6992*/
6993static int
Inada Naoki8a232c72021-04-16 14:01:04 +09006994fold_tuple_on_constants(struct compiler *c,
6995 struct instr *inst,
Mark Shannon6e8128f2020-07-30 10:03:00 +01006996 int n, PyObject *consts)
6997{
6998 /* Pre-conditions */
6999 assert(PyList_CheckExact(consts));
7000 assert(inst[n].i_opcode == BUILD_TUPLE);
7001 assert(inst[n].i_oparg == n);
7002
7003 for (int i = 0; i < n; i++) {
7004 if (inst[i].i_opcode != LOAD_CONST) {
7005 return 0;
7006 }
7007 }
7008
7009 /* Buildup new tuple of constants */
7010 PyObject *newconst = PyTuple_New(n);
7011 if (newconst == NULL) {
7012 return -1;
7013 }
7014 for (int i = 0; i < n; i++) {
7015 int arg = inst[i].i_oparg;
7016 PyObject *constant = PyList_GET_ITEM(consts, arg);
7017 Py_INCREF(constant);
7018 PyTuple_SET_ITEM(newconst, i, constant);
7019 }
Inada Naoki8a232c72021-04-16 14:01:04 +09007020 if (merge_const_one(c, &newconst) == 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007021 Py_DECREF(newconst);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007022 return -1;
7023 }
Inada Naoki8a232c72021-04-16 14:01:04 +09007024
7025 Py_ssize_t index;
7026 for (index = 0; index < PyList_GET_SIZE(consts); index++) {
7027 if (PyList_GET_ITEM(consts, index) == newconst) {
7028 break;
7029 }
7030 }
7031 if (index == PyList_GET_SIZE(consts)) {
7032 if ((size_t)index >= (size_t)INT_MAX - 1) {
7033 Py_DECREF(newconst);
7034 PyErr_SetString(PyExc_OverflowError, "too many constants");
7035 return -1;
7036 }
7037 if (PyList_Append(consts, newconst)) {
7038 Py_DECREF(newconst);
7039 return -1;
7040 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007041 }
7042 Py_DECREF(newconst);
7043 for (int i = 0; i < n; i++) {
7044 inst[i].i_opcode = NOP;
7045 }
7046 inst[n].i_opcode = LOAD_CONST;
Victor Stinner71f2ff42020-09-23 14:06:55 +02007047 inst[n].i_oparg = (int)index;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007048 return 0;
7049}
7050
Mark Shannon28b75c82020-12-23 11:43:10 +00007051
7052static int
7053eliminate_jump_to_jump(basicblock *bb, int opcode) {
7054 assert (bb->b_iused > 0);
7055 struct instr *inst = &bb->b_instr[bb->b_iused-1];
7056 assert (is_jump(inst));
7057 assert (inst->i_target->b_iused > 0);
7058 struct instr *target = &inst->i_target->b_instr[0];
7059 if (inst->i_target == target->i_target) {
7060 /* Nothing to do */
7061 return 0;
7062 }
7063 int lineno = target->i_lineno;
7064 if (add_jump_to_block(bb, opcode, lineno, target->i_target) == 0) {
7065 return -1;
7066 }
7067 assert (bb->b_iused >= 2);
7068 bb->b_instr[bb->b_iused-2].i_opcode = NOP;
7069 return 0;
7070}
7071
Mark Shannoncc75ab72020-11-12 19:49:33 +00007072/* Maximum size of basic block that should be copied in optimizer */
7073#define MAX_COPY_SIZE 4
Mark Shannon6e8128f2020-07-30 10:03:00 +01007074
7075/* Optimization */
7076static int
Inada Naoki8a232c72021-04-16 14:01:04 +09007077optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
Mark Shannon6e8128f2020-07-30 10:03:00 +01007078{
7079 assert(PyList_CheckExact(consts));
7080 struct instr nop;
7081 nop.i_opcode = NOP;
7082 struct instr *target;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007083 for (int i = 0; i < bb->b_iused; i++) {
7084 struct instr *inst = &bb->b_instr[i];
7085 int oparg = inst->i_oparg;
7086 int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01007087 if (is_jump(inst)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007088 /* Skip over empty basic blocks. */
7089 while (inst->i_target->b_iused == 0) {
7090 inst->i_target = inst->i_target->b_next;
7091 }
7092 target = &inst->i_target->b_instr[0];
7093 }
7094 else {
7095 target = &nop;
7096 }
7097 switch (inst->i_opcode) {
Mark Shannon266b4622020-11-17 19:30:14 +00007098 /* Remove LOAD_CONST const; conditional jump */
Mark Shannon6e8128f2020-07-30 10:03:00 +01007099 case LOAD_CONST:
Mark Shannon266b4622020-11-17 19:30:14 +00007100 {
7101 PyObject* cnt;
7102 int is_true;
7103 int jump_if_true;
7104 switch(nextop) {
7105 case POP_JUMP_IF_FALSE:
7106 case POP_JUMP_IF_TRUE:
7107 cnt = PyList_GET_ITEM(consts, oparg);
7108 is_true = PyObject_IsTrue(cnt);
7109 if (is_true == -1) {
7110 goto error;
7111 }
7112 inst->i_opcode = NOP;
7113 jump_if_true = nextop == POP_JUMP_IF_TRUE;
7114 if (is_true == jump_if_true) {
7115 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
7116 bb->b_nofallthrough = 1;
7117 }
7118 else {
7119 bb->b_instr[i+1].i_opcode = NOP;
7120 }
7121 break;
7122 case JUMP_IF_FALSE_OR_POP:
7123 case JUMP_IF_TRUE_OR_POP:
7124 cnt = PyList_GET_ITEM(consts, oparg);
7125 is_true = PyObject_IsTrue(cnt);
7126 if (is_true == -1) {
7127 goto error;
7128 }
7129 jump_if_true = nextop == JUMP_IF_TRUE_OR_POP;
7130 if (is_true == jump_if_true) {
7131 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
7132 bb->b_nofallthrough = 1;
7133 }
7134 else {
7135 inst->i_opcode = NOP;
7136 bb->b_instr[i+1].i_opcode = NOP;
7137 }
7138 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007139 }
7140 break;
Mark Shannon266b4622020-11-17 19:30:14 +00007141 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007142
7143 /* Try to fold tuples of constants.
7144 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
7145 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
7146 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
7147 case BUILD_TUPLE:
7148 if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
7149 switch(oparg) {
7150 case 1:
7151 inst->i_opcode = NOP;
7152 bb->b_instr[i+1].i_opcode = NOP;
7153 break;
7154 case 2:
7155 inst->i_opcode = ROT_TWO;
7156 bb->b_instr[i+1].i_opcode = NOP;
7157 break;
7158 case 3:
7159 inst->i_opcode = ROT_THREE;
7160 bb->b_instr[i+1].i_opcode = ROT_TWO;
7161 }
7162 break;
7163 }
7164 if (i >= oparg) {
Inada Naoki8a232c72021-04-16 14:01:04 +09007165 if (fold_tuple_on_constants(c, inst-oparg, oparg, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007166 goto error;
7167 }
7168 }
7169 break;
7170
7171 /* Simplify conditional jump to conditional jump where the
7172 result of the first test implies the success of a similar
7173 test or the failure of the opposite test.
7174 Arises in code like:
7175 "a and b or c"
7176 "(a and b) and c"
7177 "(a or b) or c"
7178 "(a or b) and c"
7179 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
7180 --> x:JUMP_IF_FALSE_OR_POP z
7181 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
7182 --> x:POP_JUMP_IF_FALSE y+1
7183 where y+1 is the instruction following the second test.
7184 */
7185 case JUMP_IF_FALSE_OR_POP:
7186 switch(target->i_opcode) {
7187 case POP_JUMP_IF_FALSE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007188 if (inst->i_lineno == target->i_lineno) {
7189 *inst = *target;
7190 i--;
7191 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007192 break;
7193 case JUMP_ABSOLUTE:
7194 case JUMP_FORWARD:
7195 case JUMP_IF_FALSE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00007196 if (inst->i_lineno == target->i_lineno &&
7197 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00007198 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007199 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007200 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007201 break;
7202 case JUMP_IF_TRUE_OR_POP:
7203 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00007204 if (inst->i_lineno == target->i_lineno) {
7205 inst->i_opcode = POP_JUMP_IF_FALSE;
7206 inst->i_target = inst->i_target->b_next;
7207 --i;
7208 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007209 break;
7210 }
7211 break;
7212
7213 case JUMP_IF_TRUE_OR_POP:
7214 switch(target->i_opcode) {
7215 case POP_JUMP_IF_TRUE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007216 if (inst->i_lineno == target->i_lineno) {
7217 *inst = *target;
7218 i--;
7219 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007220 break;
7221 case JUMP_ABSOLUTE:
7222 case JUMP_FORWARD:
7223 case JUMP_IF_TRUE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00007224 if (inst->i_lineno == target->i_lineno &&
7225 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00007226 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007227 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007228 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007229 break;
7230 case JUMP_IF_FALSE_OR_POP:
7231 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00007232 if (inst->i_lineno == target->i_lineno) {
7233 inst->i_opcode = POP_JUMP_IF_TRUE;
7234 inst->i_target = inst->i_target->b_next;
7235 --i;
7236 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007237 break;
7238 }
7239 break;
7240
7241 case POP_JUMP_IF_FALSE:
7242 switch(target->i_opcode) {
7243 case JUMP_ABSOLUTE:
7244 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007245 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00007246 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007247 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007248 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007249 break;
7250 }
7251 break;
7252
7253 case POP_JUMP_IF_TRUE:
7254 switch(target->i_opcode) {
7255 case JUMP_ABSOLUTE:
7256 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007257 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00007258 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007259 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007260 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007261 break;
7262 }
7263 break;
7264
7265 case JUMP_ABSOLUTE:
7266 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007267 assert (i == bb->b_iused-1);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007268 switch(target->i_opcode) {
7269 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007270 if (eliminate_jump_to_jump(bb, inst->i_opcode)) {
7271 goto error;
Mark Shannon266b4622020-11-17 19:30:14 +00007272 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007273 break;
Mark Shannon28b75c82020-12-23 11:43:10 +00007274
Mark Shannon6e8128f2020-07-30 10:03:00 +01007275 case JUMP_ABSOLUTE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007276 if (eliminate_jump_to_jump(bb, JUMP_ABSOLUTE)) {
7277 goto error;
Mark Shannon266b4622020-11-17 19:30:14 +00007278 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007279 break;
Mark Shannon28b75c82020-12-23 11:43:10 +00007280 default:
7281 if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) {
7282 basicblock *to_copy = inst->i_target;
7283 inst->i_opcode = NOP;
7284 for (i = 0; i < to_copy->b_iused; i++) {
7285 int index = compiler_next_instr(bb);
7286 if (index < 0) {
7287 return -1;
7288 }
7289 bb->b_instr[index] = to_copy->b_instr[i];
7290 }
7291 bb->b_exit = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007292 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007293 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007294 }
7295 }
7296 return 0;
7297error:
7298 return -1;
7299}
7300
7301
7302static void
Mark Shannon1659ad12021-01-13 15:05:04 +00007303clean_basic_block(basicblock *bb, int prev_lineno) {
7304 /* Remove NOPs when legal to do so. */
Mark Shannon6e8128f2020-07-30 10:03:00 +01007305 int dest = 0;
7306 for (int src = 0; src < bb->b_iused; src++) {
Mark Shannon877df852020-11-12 09:43:29 +00007307 int lineno = bb->b_instr[src].i_lineno;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007308 if (bb->b_instr[src].i_opcode == NOP) {
Mark Shannon266b4622020-11-17 19:30:14 +00007309 /* Eliminate no-op if it doesn't have a line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007310 if (lineno < 0) {
7311 continue;
7312 }
Mark Shannon266b4622020-11-17 19:30:14 +00007313 /* or, if the previous instruction had the same line number. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007314 if (prev_lineno == lineno) {
7315 continue;
7316 }
Mark Shannon266b4622020-11-17 19:30:14 +00007317 /* or, if the next instruction has same line number or no line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007318 if (src < bb->b_iused - 1) {
7319 int next_lineno = bb->b_instr[src+1].i_lineno;
7320 if (next_lineno < 0 || next_lineno == lineno) {
7321 bb->b_instr[src+1].i_lineno = lineno;
7322 continue;
Mark Shannon877df852020-11-12 09:43:29 +00007323 }
7324 }
Mark Shannon266b4622020-11-17 19:30:14 +00007325 else {
7326 basicblock* next = bb->b_next;
7327 while (next && next->b_iused == 0) {
7328 next = next->b_next;
7329 }
7330 /* or if last instruction in BB and next BB has same line number */
7331 if (next) {
7332 if (lineno == next->b_instr[0].i_lineno) {
7333 continue;
7334 }
7335 }
7336 }
7337
Mark Shannon6e8128f2020-07-30 10:03:00 +01007338 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007339 if (dest != src) {
7340 bb->b_instr[dest] = bb->b_instr[src];
7341 }
7342 dest++;
7343 prev_lineno = lineno;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007344 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007345 assert(dest <= bb->b_iused);
7346 bb->b_iused = dest;
7347}
7348
Mark Shannon266b4622020-11-17 19:30:14 +00007349static int
7350normalize_basic_block(basicblock *bb) {
7351 /* Mark blocks as exit and/or nofallthrough.
7352 Raise SystemError if CFG is malformed. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007353 for (int i = 0; i < bb->b_iused; i++) {
7354 switch(bb->b_instr[i].i_opcode) {
7355 case RETURN_VALUE:
7356 case RAISE_VARARGS:
7357 case RERAISE:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007358 bb->b_exit = 1;
Mark Shannon5977a792020-12-02 13:31:40 +00007359 bb->b_nofallthrough = 1;
7360 break;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007361 case JUMP_ABSOLUTE:
7362 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007363 bb->b_nofallthrough = 1;
Mark Shannon266b4622020-11-17 19:30:14 +00007364 /* fall through */
7365 case POP_JUMP_IF_FALSE:
7366 case POP_JUMP_IF_TRUE:
7367 case JUMP_IF_FALSE_OR_POP:
7368 case JUMP_IF_TRUE_OR_POP:
Mark Shannon5977a792020-12-02 13:31:40 +00007369 case FOR_ITER:
Mark Shannon266b4622020-11-17 19:30:14 +00007370 if (i != bb->b_iused-1) {
7371 PyErr_SetString(PyExc_SystemError, "malformed control flow graph.");
7372 return -1;
7373 }
Mark Shannon5977a792020-12-02 13:31:40 +00007374 /* Skip over empty basic blocks. */
7375 while (bb->b_instr[i].i_target->b_iused == 0) {
7376 bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next;
7377 }
7378
Mark Shannoncc75ab72020-11-12 19:49:33 +00007379 }
7380 }
Mark Shannon266b4622020-11-17 19:30:14 +00007381 return 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007382}
7383
Mark Shannon6e8128f2020-07-30 10:03:00 +01007384static int
7385mark_reachable(struct assembler *a) {
7386 basicblock **stack, **sp;
7387 sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks);
7388 if (stack == NULL) {
7389 return -1;
7390 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007391 a->a_entry->b_predecessors = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007392 *sp++ = a->a_entry;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007393 while (sp > stack) {
7394 basicblock *b = *(--sp);
Mark Shannon3bd60352021-01-13 12:05:43 +00007395 if (b->b_next && !b->b_nofallthrough) {
7396 if (b->b_next->b_predecessors == 0) {
7397 *sp++ = b->b_next;
7398 }
7399 b->b_next->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007400 }
7401 for (int i = 0; i < b->b_iused; i++) {
7402 basicblock *target;
Mark Shannon582aaf12020-08-04 17:30:11 +01007403 if (is_jump(&b->b_instr[i])) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007404 target = b->b_instr[i].i_target;
Mark Shannon3bd60352021-01-13 12:05:43 +00007405 if (target->b_predecessors == 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007406 *sp++ = target;
7407 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007408 target->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007409 }
7410 }
7411 }
7412 PyObject_Free(stack);
7413 return 0;
7414}
7415
Mark Shannon3bd60352021-01-13 12:05:43 +00007416static void
7417eliminate_empty_basic_blocks(basicblock *entry) {
7418 /* Eliminate empty blocks */
7419 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7420 basicblock *next = b->b_next;
7421 if (next) {
7422 while (next->b_iused == 0 && next->b_next) {
7423 next = next->b_next;
7424 }
7425 b->b_next = next;
7426 }
7427 }
7428 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7429 if (b->b_iused == 0) {
7430 continue;
7431 }
7432 if (is_jump(&b->b_instr[b->b_iused-1])) {
7433 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7434 while (target->b_iused == 0) {
7435 target = target->b_next;
7436 }
7437 b->b_instr[b->b_iused-1].i_target = target;
7438 }
7439 }
7440}
7441
7442
Mark Shannon5977a792020-12-02 13:31:40 +00007443/* If an instruction has no line number, but it's predecessor in the BB does,
Mark Shannon3bd60352021-01-13 12:05:43 +00007444 * then copy the line number. If a successor block has no line number, and only
7445 * one predecessor, then inherit the line number.
7446 * This ensures that all exit blocks (with one predecessor) receive a line number.
7447 * Also reduces the size of the line number table,
Mark Shannon5977a792020-12-02 13:31:40 +00007448 * but has no impact on the generated line number events.
7449 */
7450static void
Mark Shannon3bd60352021-01-13 12:05:43 +00007451propogate_line_numbers(struct assembler *a) {
Mark Shannon5977a792020-12-02 13:31:40 +00007452 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007453 if (b->b_iused == 0) {
7454 continue;
7455 }
Mark Shannon5977a792020-12-02 13:31:40 +00007456 int prev_lineno = -1;
7457 for (int i = 0; i < b->b_iused; i++) {
7458 if (b->b_instr[i].i_lineno < 0) {
7459 b->b_instr[i].i_lineno = prev_lineno;
7460 }
7461 else {
7462 prev_lineno = b->b_instr[i].i_lineno;
7463 }
7464 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007465 if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) {
7466 assert(b->b_next->b_iused);
7467 if (b->b_next->b_instr[0].i_lineno < 0) {
7468 b->b_next->b_instr[0].i_lineno = prev_lineno;
7469 }
7470 }
7471 if (is_jump(&b->b_instr[b->b_iused-1])) {
7472 switch (b->b_instr[b->b_iused-1].i_opcode) {
7473 /* Note: Only actual jumps, not exception handlers */
7474 case SETUP_ASYNC_WITH:
7475 case SETUP_WITH:
7476 case SETUP_FINALLY:
7477 continue;
7478 }
7479 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7480 if (target->b_predecessors == 1) {
7481 if (target->b_instr[0].i_lineno < 0) {
7482 target->b_instr[0].i_lineno = prev_lineno;
7483 }
7484 }
7485 }
Mark Shannon5977a792020-12-02 13:31:40 +00007486 }
7487}
7488
7489/* Perform optimizations on a control flow graph.
Mark Shannon6e8128f2020-07-30 10:03:00 +01007490 The consts object should still be in list form to allow new constants
7491 to be appended.
7492
7493 All transformations keep the code size the same or smaller.
7494 For those that reduce size, the gaps are initially filled with
7495 NOPs. Later those NOPs are removed.
7496*/
7497
7498static int
Inada Naoki8a232c72021-04-16 14:01:04 +09007499optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts)
Mark Shannon6e8128f2020-07-30 10:03:00 +01007500{
Mark Shannoncc75ab72020-11-12 19:49:33 +00007501 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Inada Naoki8a232c72021-04-16 14:01:04 +09007502 if (optimize_basic_block(c, b, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007503 return -1;
7504 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007505 clean_basic_block(b, -1);
Mark Shannon3bd60352021-01-13 12:05:43 +00007506 assert(b->b_predecessors == 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007507 }
7508 if (mark_reachable(a)) {
7509 return -1;
7510 }
7511 /* Delete unreachable instructions */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007512 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007513 if (b->b_predecessors == 0) {
Mark Shannoncc75ab72020-11-12 19:49:33 +00007514 b->b_iused = 0;
Om Gc71581c2020-12-16 17:48:05 +05307515 b->b_nofallthrough = 0;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007516 }
7517 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007518 basicblock *pred = NULL;
7519 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7520 int prev_lineno = -1;
7521 if (pred && pred->b_iused) {
7522 prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno;
7523 }
7524 clean_basic_block(b, prev_lineno);
7525 pred = b->b_nofallthrough ? NULL : b;
7526 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007527 eliminate_empty_basic_blocks(a->a_entry);
Om Gc71581c2020-12-16 17:48:05 +05307528 /* Delete jump instructions made redundant by previous step. If a non-empty
7529 block ends with a jump instruction, check if the next non-empty block
7530 reached through normal flow control is the target of that jump. If it
7531 is, then the jump instruction is redundant and can be deleted.
7532 */
Mark Shannon3bd60352021-01-13 12:05:43 +00007533 int maybe_empty_blocks = 0;
Om Gc71581c2020-12-16 17:48:05 +05307534 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7535 if (b->b_iused > 0) {
7536 struct instr *b_last_instr = &b->b_instr[b->b_iused - 1];
Mark Shannon802b6452021-02-02 14:59:15 +00007537 if (b_last_instr->i_opcode == JUMP_ABSOLUTE ||
Om Gc71581c2020-12-16 17:48:05 +05307538 b_last_instr->i_opcode == JUMP_FORWARD) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007539 if (b_last_instr->i_target == b->b_next) {
7540 assert(b->b_next->b_iused);
Om Gc71581c2020-12-16 17:48:05 +05307541 b->b_nofallthrough = 0;
Mark Shannon802b6452021-02-02 14:59:15 +00007542 b_last_instr->i_opcode = NOP;
7543 clean_basic_block(b, -1);
7544 maybe_empty_blocks = 1;
Om Gc71581c2020-12-16 17:48:05 +05307545 }
7546 }
7547 }
7548 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007549 if (maybe_empty_blocks) {
7550 eliminate_empty_basic_blocks(a->a_entry);
7551 }
7552 propogate_line_numbers(a);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007553 return 0;
7554}
7555
Mark Shannon5977a792020-12-02 13:31:40 +00007556static inline int
7557is_exit_without_lineno(basicblock *b) {
7558 return b->b_exit && b->b_instr[0].i_lineno < 0;
7559}
7560
7561/* PEP 626 mandates that the f_lineno of a frame is correct
7562 * after a frame terminates. It would be prohibitively expensive
7563 * to continuously update the f_lineno field at runtime,
7564 * so we make sure that all exiting instruction (raises and returns)
7565 * have a valid line number, allowing us to compute f_lineno lazily.
7566 * We can do this by duplicating the exit blocks without line number
7567 * so that none have more than one predecessor. We can then safely
7568 * copy the line number from the sole predecessor block.
7569 */
7570static int
7571ensure_exits_have_lineno(struct compiler *c)
7572{
Mark Shannoneaccc122020-12-04 15:22:12 +00007573 basicblock *entry = NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00007574 /* Copy all exit blocks without line number that are targets of a jump.
7575 */
7576 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7577 if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) {
7578 switch (b->b_instr[b->b_iused-1].i_opcode) {
7579 /* Note: Only actual jumps, not exception handlers */
7580 case SETUP_ASYNC_WITH:
7581 case SETUP_WITH:
7582 case SETUP_FINALLY:
7583 continue;
7584 }
7585 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7586 if (is_exit_without_lineno(target)) {
7587 basicblock *new_target = compiler_copy_block(c, target);
7588 if (new_target == NULL) {
7589 return -1;
7590 }
7591 new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7592 b->b_instr[b->b_iused-1].i_target = new_target;
7593 }
7594 }
Mark Shannoneaccc122020-12-04 15:22:12 +00007595 entry = b;
7596 }
7597 assert(entry != NULL);
7598 if (is_exit_without_lineno(entry)) {
7599 entry->b_instr[0].i_lineno = c->u->u_firstlineno;
Mark Shannon5977a792020-12-02 13:31:40 +00007600 }
Mark Shannonee9f98d2021-01-05 12:04:10 +00007601 /* Eliminate empty blocks */
7602 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7603 while (b->b_next && b->b_next->b_iused == 0) {
7604 b->b_next = b->b_next->b_next;
7605 }
7606 }
Mark Shannon5977a792020-12-02 13:31:40 +00007607 /* Any remaining reachable exit blocks without line number can only be reached by
7608 * fall through, and thus can only have a single predecessor */
7609 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7610 if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) {
7611 if (is_exit_without_lineno(b->b_next)) {
7612 assert(b->b_next->b_iused > 0);
7613 b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7614 }
7615 }
7616 }
7617 return 0;
7618}
7619
7620
Mark Shannon6e8128f2020-07-30 10:03:00 +01007621/* Retained for API compatibility.
7622 * Optimization is now done in optimize_cfg */
7623
7624PyObject *
7625PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
7626 PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
7627{
7628 Py_INCREF(code);
7629 return code;
7630}