blob: 4411edb6678438b6de9f0760f1b066a05c4c3c67 [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{
Mark Shannon5979e812021-04-30 14:32:47 +01001748 int loc;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001749 switch (info->fb_type) {
1750 case WHILE_LOOP:
Mark Shannon02d126a2020-09-25 14:04:19 +01001751 case EXCEPTION_HANDLER:
tomKPZ7a7ba3d2021-04-07 07:43:45 -07001752 case ASYNC_COMPREHENSION_GENERATOR:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001753 return 1;
1754
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001755 case FOR_LOOP:
1756 /* Pop the iterator */
1757 if (preserve_tos) {
1758 ADDOP(c, ROT_TWO);
1759 }
1760 ADDOP(c, POP_TOP);
1761 return 1;
1762
Mark Shannon02d126a2020-09-25 14:04:19 +01001763 case TRY_EXCEPT:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001764 ADDOP(c, POP_BLOCK);
1765 return 1;
1766
1767 case FINALLY_TRY:
Mark Shannon5274b682020-12-16 13:07:01 +00001768 /* This POP_BLOCK gets the line number of the unwinding statement */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001769 ADDOP(c, POP_BLOCK);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001770 if (preserve_tos) {
Mark Shannonfee55262019-11-21 09:11:43 +00001771 if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) {
1772 return 0;
1773 }
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001774 }
Mark Shannon5274b682020-12-16 13:07:01 +00001775 /* Emit the finally block */
Mark Shannonfee55262019-11-21 09:11:43 +00001776 VISIT_SEQ(c, stmt, info->fb_datum);
1777 if (preserve_tos) {
1778 compiler_pop_fblock(c, POP_VALUE, NULL);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001779 }
Mark Shannon5274b682020-12-16 13:07:01 +00001780 /* The finally block should appear to execute after the
1781 * statement causing the unwinding, so make the unwinding
1782 * instruction artificial */
1783 c->u->u_lineno = -1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001784 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001785
Mark Shannonfee55262019-11-21 09:11:43 +00001786 case FINALLY_END:
1787 if (preserve_tos) {
1788 ADDOP(c, ROT_FOUR);
1789 }
1790 ADDOP(c, POP_TOP);
1791 ADDOP(c, POP_TOP);
1792 ADDOP(c, POP_TOP);
1793 if (preserve_tos) {
1794 ADDOP(c, ROT_FOUR);
1795 }
1796 ADDOP(c, POP_EXCEPT);
1797 return 1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001798
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001799 case WITH:
1800 case ASYNC_WITH:
Mark Shannon5979e812021-04-30 14:32:47 +01001801 loc = c->u->u_lineno;
1802 SET_LOC(c, (stmt_ty)info->fb_datum);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001803 ADDOP(c, POP_BLOCK);
1804 if (preserve_tos) {
1805 ADDOP(c, ROT_TWO);
1806 }
Mark Shannonfee55262019-11-21 09:11:43 +00001807 if(!compiler_call_exit_with_nones(c)) {
1808 return 0;
1809 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001810 if (info->fb_type == ASYNC_WITH) {
1811 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001812 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001813 ADDOP(c, YIELD_FROM);
1814 }
Mark Shannonfee55262019-11-21 09:11:43 +00001815 ADDOP(c, POP_TOP);
Mark Shannon5979e812021-04-30 14:32:47 +01001816 c->u->u_lineno = loc;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001817 return 1;
1818
1819 case HANDLER_CLEANUP:
Mark Shannonfee55262019-11-21 09:11:43 +00001820 if (info->fb_datum) {
1821 ADDOP(c, POP_BLOCK);
1822 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001823 if (preserve_tos) {
1824 ADDOP(c, ROT_FOUR);
1825 }
Mark Shannonfee55262019-11-21 09:11:43 +00001826 ADDOP(c, POP_EXCEPT);
1827 if (info->fb_datum) {
1828 ADDOP_LOAD_CONST(c, Py_None);
1829 compiler_nameop(c, info->fb_datum, Store);
1830 compiler_nameop(c, info->fb_datum, Del);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001831 }
Mark Shannonfee55262019-11-21 09:11:43 +00001832 return 1;
1833
1834 case POP_VALUE:
1835 if (preserve_tos) {
1836 ADDOP(c, ROT_TWO);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001837 }
Mark Shannonfee55262019-11-21 09:11:43 +00001838 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001839 return 1;
1840 }
1841 Py_UNREACHABLE();
1842}
1843
Mark Shannonfee55262019-11-21 09:11:43 +00001844/** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */
1845static int
1846compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) {
1847 if (c->u->u_nfblocks == 0) {
1848 return 1;
1849 }
1850 struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1];
1851 if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) {
1852 *loop = top;
1853 return 1;
1854 }
1855 struct fblockinfo copy = *top;
1856 c->u->u_nfblocks--;
1857 if (!compiler_unwind_fblock(c, &copy, preserve_tos)) {
1858 return 0;
1859 }
1860 if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) {
1861 return 0;
1862 }
1863 c->u->u_fblock[c->u->u_nfblocks] = copy;
1864 c->u->u_nfblocks++;
1865 return 1;
1866}
1867
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001868/* Compile a sequence of statements, checking for a docstring
1869 and for annotations. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001870
1871static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001872compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001873{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001874 int i = 0;
1875 stmt_ty st;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001876 PyObject *docstring;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001877
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001878 /* Set current line number to the line number of first statement.
1879 This way line number for SETUP_ANNOTATIONS will always
1880 coincide with the line number of first "real" statement in module.
Hansraj Das01171eb2019-10-09 07:54:02 +05301881 If body is empty, then lineno will be set later in assemble. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001882 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) {
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001883 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001884 SET_LOC(c, st);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001885 }
1886 /* Every annotated class and module should have __annotations__. */
1887 if (find_ann(stmts)) {
1888 ADDOP(c, SETUP_ANNOTATIONS);
1889 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001890 if (!asdl_seq_LEN(stmts))
1891 return 1;
INADA Naokicb41b272017-02-23 00:31:59 +09001892 /* if not -OO mode, set docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001893 if (c->c_optimize < 2) {
1894 docstring = _PyAST_GetDocString(stmts);
1895 if (docstring) {
1896 i = 1;
1897 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1898 assert(st->kind == Expr_kind);
1899 VISIT(c, expr, st->v.Expr.value);
1900 if (!compiler_nameop(c, __doc__, Store))
1901 return 0;
1902 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001904 for (; i < asdl_seq_LEN(stmts); i++)
1905 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001907}
1908
1909static PyCodeObject *
1910compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001911{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 PyCodeObject *co;
1913 int addNone = 1;
1914 static PyObject *module;
1915 if (!module) {
1916 module = PyUnicode_InternFromString("<module>");
1917 if (!module)
1918 return NULL;
1919 }
1920 /* Use 0 for firstlineno initially, will fixup in assemble(). */
Mark Shannon877df852020-11-12 09:43:29 +00001921 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 return NULL;
1923 switch (mod->kind) {
1924 case Module_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001925 if (!compiler_body(c, mod->v.Module.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 compiler_exit_scope(c);
1927 return 0;
1928 }
1929 break;
1930 case Interactive_kind:
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001931 if (find_ann(mod->v.Interactive.body)) {
1932 ADDOP(c, SETUP_ANNOTATIONS);
1933 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 c->c_interactive = 1;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001935 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 break;
1937 case Expression_kind:
1938 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1939 addNone = 0;
1940 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 default:
1942 PyErr_Format(PyExc_SystemError,
1943 "module kind %d should not be possible",
1944 mod->kind);
1945 return 0;
1946 }
1947 co = assemble(c, addNone);
1948 compiler_exit_scope(c);
1949 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001950}
1951
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001952/* The test for LOCAL must come before the test for FREE in order to
1953 handle classes where name is both local and free. The local var is
1954 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001955*/
1956
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001957static int
1958get_ref_type(struct compiler *c, PyObject *name)
1959{
Victor Stinner0b1bc562013-05-16 22:17:17 +02001960 int scope;
Benjamin Peterson312595c2013-05-15 15:26:42 -05001961 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001962 _PyUnicode_EqualToASCIIString(name, "__class__"))
Benjamin Peterson312595c2013-05-15 15:26:42 -05001963 return CELL;
Victor Stinner28ad12f2021-03-19 12:41:49 +01001964 scope = _PyST_GetScope(c->u->u_ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 if (scope == 0) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001966 PyErr_Format(PyExc_SystemError,
Victor Stinner28ad12f2021-03-19 12:41:49 +01001967 "_PyST_GetScope(name=%R) failed: "
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001968 "unknown scope in unit %S (%R); "
1969 "symbols: %R; locals: %R; globals: %R",
1970 name,
1971 c->u->u_name, c->u->u_ste->ste_id,
1972 c->u->u_ste->ste_symbols, c->u->u_varnames, c->u->u_names);
1973 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001976}
1977
1978static int
1979compiler_lookup_arg(PyObject *dict, PyObject *name)
1980{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001981 PyObject *v;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001982 v = PyDict_GetItemWithError(dict, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001983 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001984 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001985 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986}
1987
1988static int
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001989compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags,
1990 PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001991{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001992 Py_ssize_t i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001993 if (qualname == NULL)
1994 qualname = co->co_name;
1995
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001996 if (free) {
1997 for (i = 0; i < free; ++i) {
1998 /* Bypass com_addop_varname because it will generate
1999 LOAD_DEREF but LOAD_CLOSURE is needed.
2000 */
2001 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002002
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002003 /* Special case: If a class contains a method with a
2004 free variable that has the same name as a method,
2005 the name will be considered free *and* local in the
2006 class. It should be handled by the closure, as
Min ho Kimc4cacc82019-07-31 08:16:13 +10002007 well as by the normal name lookup logic.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002008 */
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002009 int reftype = get_ref_type(c, name);
2010 if (reftype == -1) {
2011 return 0;
2012 }
2013 int arg;
2014 if (reftype == CELL) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002015 arg = compiler_lookup_arg(c->u->u_cellvars, name);
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002016 }
2017 else {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002018 arg = compiler_lookup_arg(c->u->u_freevars, name);
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002019 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002020 if (arg == -1) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002021 PyErr_Format(PyExc_SystemError,
2022 "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; "
2023 "freevars of code %S: %R",
2024 name,
2025 reftype,
2026 c->u->u_name,
2027 co->co_name,
2028 co->co_freevars);
2029 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002030 }
2031 ADDOP_I(c, LOAD_CLOSURE, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002033 flags |= 0x08;
2034 ADDOP_I(c, BUILD_TUPLE, free);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002036 ADDOP_LOAD_CONST(c, (PyObject*)co);
2037 ADDOP_LOAD_CONST(c, qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002038 ADDOP_I(c, MAKE_FUNCTION, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002039 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002040}
2041
2042static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002043compiler_decorators(struct compiler *c, asdl_expr_seq* decos)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002044{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 if (!decos)
2048 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2051 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
2052 }
2053 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002054}
2055
2056static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002057compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs,
2058 asdl_expr_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00002059{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002060 /* Push a dict of keyword-only default values.
2061
2062 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
2063 */
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002064 int i;
2065 PyObject *keys = NULL;
2066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
2068 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
2069 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
2070 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04002071 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002072 if (!mangled) {
2073 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002075 if (keys == NULL) {
2076 keys = PyList_New(1);
2077 if (keys == NULL) {
2078 Py_DECREF(mangled);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002079 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002080 }
2081 PyList_SET_ITEM(keys, 0, mangled);
2082 }
2083 else {
2084 int res = PyList_Append(keys, mangled);
2085 Py_DECREF(mangled);
2086 if (res == -1) {
2087 goto error;
2088 }
2089 }
2090 if (!compiler_visit_expr(c, default_)) {
2091 goto error;
2092 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 }
2094 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002095 if (keys != NULL) {
2096 Py_ssize_t default_count = PyList_GET_SIZE(keys);
2097 PyObject *keys_tuple = PyList_AsTuple(keys);
2098 Py_DECREF(keys);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002099 ADDOP_LOAD_CONST_NEW(c, keys_tuple);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002100 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002101 assert(default_count > 0);
2102 return 1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002103 }
2104 else {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002105 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002106 }
2107
2108error:
2109 Py_XDECREF(keys);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002110 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002111}
2112
2113static int
Guido van Rossum95e4d582018-01-26 08:20:18 -08002114compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
2115{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +03002116 ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation));
Guido van Rossum95e4d582018-01-26 08:20:18 -08002117 return 1;
2118}
2119
2120static int
Neal Norwitzc1505362006-12-28 06:47:50 +00002121compiler_visit_argannotation(struct compiler *c, identifier id,
Yurii Karabas73019792020-11-25 12:43:18 +02002122 expr_ty annotation, Py_ssize_t *annotations_len)
Neal Norwitzc1505362006-12-28 06:47:50 +00002123{
Pablo Galindob0544ba2021-04-21 12:41:19 +01002124 if (!annotation) {
2125 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002126 }
Pablo Galindob0544ba2021-04-21 12:41:19 +01002127
2128 PyObject *mangled = _Py_Mangle(c->u->u_private, id);
2129 if (!mangled) {
2130 return 0;
2131 }
2132 ADDOP_LOAD_CONST(c, mangled);
2133 Py_DECREF(mangled);
2134
2135 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
2136 VISIT(c, annexpr, annotation)
2137 }
2138 else {
2139 VISIT(c, expr, annotation);
2140 }
2141 *annotations_len += 2;
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002142 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002143}
2144
2145static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002146compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args,
Yurii Karabas73019792020-11-25 12:43:18 +02002147 Py_ssize_t *annotations_len)
Neal Norwitzc1505362006-12-28 06:47:50 +00002148{
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002149 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 for (i = 0; i < asdl_seq_LEN(args); i++) {
2151 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002152 if (!compiler_visit_argannotation(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153 c,
2154 arg->arg,
2155 arg->annotation,
Yurii Karabas73019792020-11-25 12:43:18 +02002156 annotations_len))
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002157 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002158 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002159 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002160}
2161
2162static int
2163compiler_visit_annotations(struct compiler *c, arguments_ty args,
2164 expr_ty returns)
2165{
Yurii Karabas73019792020-11-25 12:43:18 +02002166 /* Push arg annotation names and values.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002167 The expressions are evaluated out-of-order wrt the source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00002168
Yurii Karabas73019792020-11-25 12:43:18 +02002169 Return 0 on error, -1 if no annotations pushed, 1 if a annotations is pushed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 */
2171 static identifier return_str;
Yurii Karabas73019792020-11-25 12:43:18 +02002172 Py_ssize_t annotations_len = 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002173
Yurii Karabas73019792020-11-25 12:43:18 +02002174 if (!compiler_visit_argannotations(c, args->args, &annotations_len))
2175 return 0;
2176 if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len))
2177 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002178 if (args->vararg && args->vararg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002179 !compiler_visit_argannotation(c, args->vararg->arg,
Yurii Karabas73019792020-11-25 12:43:18 +02002180 args->vararg->annotation, &annotations_len))
2181 return 0;
2182 if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len))
2183 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002184 if (args->kwarg && args->kwarg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002185 !compiler_visit_argannotation(c, args->kwarg->arg,
Yurii Karabas73019792020-11-25 12:43:18 +02002186 args->kwarg->annotation, &annotations_len))
2187 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 if (!return_str) {
2190 return_str = PyUnicode_InternFromString("return");
2191 if (!return_str)
Yurii Karabas73019792020-11-25 12:43:18 +02002192 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 }
Yurii Karabas73019792020-11-25 12:43:18 +02002194 if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) {
2195 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196 }
2197
Yurii Karabas73019792020-11-25 12:43:18 +02002198 if (annotations_len) {
2199 ADDOP_I(c, BUILD_TUPLE, annotations_len);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002200 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002201 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002202
Yurii Karabas73019792020-11-25 12:43:18 +02002203 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002204}
2205
2206static int
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002207compiler_visit_defaults(struct compiler *c, arguments_ty args)
2208{
2209 VISIT_SEQ(c, expr, args->defaults);
2210 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
2211 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002212}
2213
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002214static Py_ssize_t
2215compiler_default_arguments(struct compiler *c, arguments_ty args)
2216{
2217 Py_ssize_t funcflags = 0;
2218 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002219 if (!compiler_visit_defaults(c, args))
2220 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002221 funcflags |= 0x01;
2222 }
2223 if (args->kwonlyargs) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002224 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002225 args->kw_defaults);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002226 if (res == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002227 return -1;
2228 }
2229 else if (res > 0) {
2230 funcflags |= 0x02;
2231 }
2232 }
2233 return funcflags;
2234}
2235
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002236static int
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002237forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx)
2238{
2239
2240 if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2241 compiler_error(c, "cannot assign to __debug__");
2242 return 1;
2243 }
2244 return 0;
2245}
2246
2247static int
2248compiler_check_debug_one_arg(struct compiler *c, arg_ty arg)
2249{
2250 if (arg != NULL) {
2251 if (forbidden_name(c, arg->arg, Store))
2252 return 0;
2253 }
2254 return 1;
2255}
2256
2257static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002258compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args)
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002259{
2260 if (args != NULL) {
Pablo Galindoee40e4b2020-04-23 03:43:08 +01002261 for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002262 if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i)))
2263 return 0;
2264 }
2265 }
2266 return 1;
2267}
2268
2269static int
2270compiler_check_debug_args(struct compiler *c, arguments_ty args)
2271{
2272 if (!compiler_check_debug_args_seq(c, args->posonlyargs))
2273 return 0;
2274 if (!compiler_check_debug_args_seq(c, args->args))
2275 return 0;
2276 if (!compiler_check_debug_one_arg(c, args->vararg))
2277 return 0;
2278 if (!compiler_check_debug_args_seq(c, args->kwonlyargs))
2279 return 0;
2280 if (!compiler_check_debug_one_arg(c, args->kwarg))
2281 return 0;
2282 return 1;
2283}
2284
2285static int
Yury Selivanov75445082015-05-11 22:57:16 -04002286compiler_function(struct compiler *c, stmt_ty s, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 PyCodeObject *co;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002289 PyObject *qualname, *docstring = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002290 arguments_ty args;
2291 expr_ty returns;
2292 identifier name;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002293 asdl_expr_seq* decos;
2294 asdl_stmt_seq *body;
INADA Naokicb41b272017-02-23 00:31:59 +09002295 Py_ssize_t i, funcflags;
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002296 int annotations;
Yury Selivanov75445082015-05-11 22:57:16 -04002297 int scope_type;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002298 int firstlineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002299
Yury Selivanov75445082015-05-11 22:57:16 -04002300 if (is_async) {
2301 assert(s->kind == AsyncFunctionDef_kind);
2302
2303 args = s->v.AsyncFunctionDef.args;
2304 returns = s->v.AsyncFunctionDef.returns;
2305 decos = s->v.AsyncFunctionDef.decorator_list;
2306 name = s->v.AsyncFunctionDef.name;
2307 body = s->v.AsyncFunctionDef.body;
2308
2309 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
2310 } else {
2311 assert(s->kind == FunctionDef_kind);
2312
2313 args = s->v.FunctionDef.args;
2314 returns = s->v.FunctionDef.returns;
2315 decos = s->v.FunctionDef.decorator_list;
2316 name = s->v.FunctionDef.name;
2317 body = s->v.FunctionDef.body;
2318
2319 scope_type = COMPILER_SCOPE_FUNCTION;
2320 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002321
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002322 if (!compiler_check_debug_args(c, args))
2323 return 0;
2324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002325 if (!compiler_decorators(c, decos))
2326 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002327
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002328 firstlineno = s->lineno;
2329 if (asdl_seq_LEN(decos)) {
2330 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2331 }
2332
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002333 funcflags = compiler_default_arguments(c, args);
2334 if (funcflags == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002336 }
2337
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002338 annotations = compiler_visit_annotations(c, args, returns);
2339 if (annotations == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002340 return 0;
2341 }
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002342 else if (annotations > 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002343 funcflags |= 0x04;
2344 }
2345
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002346 if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002347 return 0;
2348 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002349
INADA Naokicb41b272017-02-23 00:31:59 +09002350 /* if not -OO mode, add docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002351 if (c->c_optimize < 2) {
2352 docstring = _PyAST_GetDocString(body);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002353 }
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002354 if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 compiler_exit_scope(c);
2356 return 0;
2357 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002360 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Mark Shannon877df852020-11-12 09:43:29 +00002362 for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) {
Mark Shannonfd009e62020-11-13 12:53:53 +00002363 VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i));
Mark Shannon877df852020-11-12 09:43:29 +00002364 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002366 qualname = c->u->u_qualname;
2367 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002369 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002370 Py_XDECREF(qualname);
2371 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002373 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002374
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002375 if (!compiler_make_closure(c, co, funcflags, qualname)) {
2376 Py_DECREF(qualname);
2377 Py_DECREF(co);
2378 return 0;
2379 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002380 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 /* decorators */
2384 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2385 ADDOP_I(c, CALL_FUNCTION, 1);
2386 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002387
Yury Selivanov75445082015-05-11 22:57:16 -04002388 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002389}
2390
2391static int
2392compiler_class(struct compiler *c, stmt_ty s)
2393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 PyCodeObject *co;
2395 PyObject *str;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002396 int i, firstlineno;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002397 asdl_expr_seq *decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 if (!compiler_decorators(c, decos))
2400 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002401
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002402 firstlineno = s->lineno;
2403 if (asdl_seq_LEN(decos)) {
2404 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2405 }
2406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 /* ultimately generate code for:
2408 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
2409 where:
2410 <func> is a function/closure created from the class body;
2411 it has a single argument (__locals__) where the dict
2412 (or MutableSequence) representing the locals is passed
2413 <name> is the class name
2414 <bases> is the positional arguments and *varargs argument
2415 <keywords> is the keyword arguments and **kwds argument
2416 This borrows from compiler_call.
2417 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002420 if (!compiler_enter_scope(c, s->v.ClassDef.name,
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002421 COMPILER_SCOPE_CLASS, (void *)s, firstlineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 return 0;
2423 /* this block represents what we do in the new scope */
2424 {
2425 /* use the class name for name mangling */
2426 Py_INCREF(s->v.ClassDef.name);
Serhiy Storchaka48842712016-04-06 09:45:48 +03002427 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 /* load (global) __name__ ... */
2429 str = PyUnicode_InternFromString("__name__");
2430 if (!str || !compiler_nameop(c, str, Load)) {
2431 Py_XDECREF(str);
2432 compiler_exit_scope(c);
2433 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002434 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 Py_DECREF(str);
2436 /* ... and store it as __module__ */
2437 str = PyUnicode_InternFromString("__module__");
2438 if (!str || !compiler_nameop(c, str, Store)) {
2439 Py_XDECREF(str);
2440 compiler_exit_scope(c);
2441 return 0;
2442 }
2443 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002444 assert(c->u->u_qualname);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002445 ADDOP_LOAD_CONST(c, c->u->u_qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002446 str = PyUnicode_InternFromString("__qualname__");
2447 if (!str || !compiler_nameop(c, str, Store)) {
2448 Py_XDECREF(str);
2449 compiler_exit_scope(c);
2450 return 0;
2451 }
2452 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002453 /* compile the body proper */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002454 if (!compiler_body(c, s->v.ClassDef.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 compiler_exit_scope(c);
2456 return 0;
2457 }
Mark Shannone56d54e2021-01-15 13:52:00 +00002458 /* The following code is artificial */
2459 c->u->u_lineno = -1;
Nick Coghlan19d24672016-12-05 16:47:55 +10002460 /* Return __classcell__ if it is referenced, otherwise return None */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002461 if (c->u->u_ste->ste_needs_class_closure) {
Nick Coghlan19d24672016-12-05 16:47:55 +10002462 /* Store __classcell__ into class namespace & return it */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002463 str = PyUnicode_InternFromString("__class__");
2464 if (str == NULL) {
2465 compiler_exit_scope(c);
2466 return 0;
2467 }
2468 i = compiler_lookup_arg(c->u->u_cellvars, str);
2469 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01002470 if (i < 0) {
2471 compiler_exit_scope(c);
2472 return 0;
2473 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002474 assert(i == 0);
Nick Coghlan944368e2016-09-11 14:45:49 +10002475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476 ADDOP_I(c, LOAD_CLOSURE, i);
Nick Coghlan19d24672016-12-05 16:47:55 +10002477 ADDOP(c, DUP_TOP);
Nick Coghlan944368e2016-09-11 14:45:49 +10002478 str = PyUnicode_InternFromString("__classcell__");
2479 if (!str || !compiler_nameop(c, str, Store)) {
2480 Py_XDECREF(str);
2481 compiler_exit_scope(c);
2482 return 0;
2483 }
2484 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002486 else {
Nick Coghlan19d24672016-12-05 16:47:55 +10002487 /* No methods referenced __class__, so just return None */
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002488 assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002489 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson312595c2013-05-15 15:26:42 -05002490 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002491 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 /* create the code object */
2493 co = assemble(c, 1);
2494 }
2495 /* leave the new scope */
2496 compiler_exit_scope(c);
2497 if (co == NULL)
2498 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500 /* 2. load the 'build_class' function */
2501 ADDOP(c, LOAD_BUILD_CLASS);
2502
2503 /* 3. load a function (or closure) made from the code object */
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002504 if (!compiler_make_closure(c, co, 0, NULL)) {
2505 Py_DECREF(co);
2506 return 0;
2507 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002508 Py_DECREF(co);
2509
2510 /* 4. load class name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002511 ADDOP_LOAD_CONST(c, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002512
2513 /* 5. generate the rest of the code for the call */
Pablo Galindoa5634c42020-09-16 19:42:00 +01002514 if (!compiler_call_helper(c, 2, s->v.ClassDef.bases, s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 return 0;
2516
2517 /* 6. apply decorators */
2518 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2519 ADDOP_I(c, CALL_FUNCTION, 1);
2520 }
2521
2522 /* 7. store into <name> */
2523 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2524 return 0;
2525 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002526}
2527
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02002528/* Return 0 if the expression is a constant value except named singletons.
2529 Return 1 otherwise. */
2530static int
2531check_is_arg(expr_ty e)
2532{
2533 if (e->kind != Constant_kind) {
2534 return 1;
2535 }
2536 PyObject *value = e->v.Constant.value;
2537 return (value == Py_None
2538 || value == Py_False
2539 || value == Py_True
2540 || value == Py_Ellipsis);
2541}
2542
2543/* Check operands of identity chacks ("is" and "is not").
2544 Emit a warning if any operand is a constant except named singletons.
2545 Return 0 on error.
2546 */
2547static int
2548check_compare(struct compiler *c, expr_ty e)
2549{
2550 Py_ssize_t i, n;
2551 int left = check_is_arg(e->v.Compare.left);
2552 n = asdl_seq_LEN(e->v.Compare.ops);
2553 for (i = 0; i < n; i++) {
2554 cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i);
2555 int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2556 if (op == Is || op == IsNot) {
2557 if (!right || !left) {
2558 const char *msg = (op == Is)
2559 ? "\"is\" with a literal. Did you mean \"==\"?"
2560 : "\"is not\" with a literal. Did you mean \"!=\"?";
2561 return compiler_warn(c, msg);
2562 }
2563 }
2564 left = right;
2565 }
2566 return 1;
2567}
2568
Mark Shannon9af0e472020-01-14 10:12:45 +00002569static int compiler_addcompare(struct compiler *c, cmpop_ty op)
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002570{
Mark Shannon9af0e472020-01-14 10:12:45 +00002571 int cmp;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002572 switch (op) {
2573 case Eq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002574 cmp = Py_EQ;
2575 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002576 case NotEq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002577 cmp = Py_NE;
2578 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002579 case Lt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002580 cmp = Py_LT;
2581 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002582 case LtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002583 cmp = Py_LE;
2584 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002585 case Gt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002586 cmp = Py_GT;
2587 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002588 case GtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002589 cmp = Py_GE;
2590 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002591 case Is:
Mark Shannon9af0e472020-01-14 10:12:45 +00002592 ADDOP_I(c, IS_OP, 0);
2593 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002594 case IsNot:
Mark Shannon9af0e472020-01-14 10:12:45 +00002595 ADDOP_I(c, IS_OP, 1);
2596 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002597 case In:
Mark Shannon9af0e472020-01-14 10:12:45 +00002598 ADDOP_I(c, CONTAINS_OP, 0);
2599 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002600 case NotIn:
Mark Shannon9af0e472020-01-14 10:12:45 +00002601 ADDOP_I(c, CONTAINS_OP, 1);
2602 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002603 default:
Mark Shannon9af0e472020-01-14 10:12:45 +00002604 Py_UNREACHABLE();
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002605 }
Mark Shannon9af0e472020-01-14 10:12:45 +00002606 ADDOP_I(c, COMPARE_OP, cmp);
2607 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002608}
2609
Mark Shannon9af0e472020-01-14 10:12:45 +00002610
2611
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002612static int
2613compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2614{
2615 switch (e->kind) {
2616 case UnaryOp_kind:
2617 if (e->v.UnaryOp.op == Not)
2618 return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2619 /* fallback to general implementation */
2620 break;
2621 case BoolOp_kind: {
Pablo Galindoa5634c42020-09-16 19:42:00 +01002622 asdl_expr_seq *s = e->v.BoolOp.values;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002623 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2624 assert(n >= 0);
2625 int cond2 = e->v.BoolOp.op == Or;
2626 basicblock *next2 = next;
2627 if (!cond2 != !cond) {
2628 next2 = compiler_new_block(c);
2629 if (next2 == NULL)
2630 return 0;
2631 }
2632 for (i = 0; i < n; ++i) {
2633 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2634 return 0;
2635 }
2636 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2637 return 0;
2638 if (next2 != next)
2639 compiler_use_next_block(c, next2);
2640 return 1;
2641 }
2642 case IfExp_kind: {
2643 basicblock *end, *next2;
2644 end = compiler_new_block(c);
2645 if (end == NULL)
2646 return 0;
2647 next2 = compiler_new_block(c);
2648 if (next2 == NULL)
2649 return 0;
2650 if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2651 return 0;
2652 if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2653 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002654 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002655 compiler_use_next_block(c, next2);
2656 if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2657 return 0;
2658 compiler_use_next_block(c, end);
2659 return 1;
2660 }
2661 case Compare_kind: {
2662 Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2663 if (n > 0) {
Serhiy Storchaka45835252019-02-16 08:29:46 +02002664 if (!check_compare(c, e)) {
2665 return 0;
2666 }
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002667 basicblock *cleanup = compiler_new_block(c);
2668 if (cleanup == NULL)
2669 return 0;
2670 VISIT(c, expr, e->v.Compare.left);
2671 for (i = 0; i < n; i++) {
2672 VISIT(c, expr,
2673 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2674 ADDOP(c, DUP_TOP);
2675 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00002676 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01002677 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002678 NEXT_BLOCK(c);
2679 }
2680 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00002681 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Mark Shannon582aaf12020-08-04 17:30:11 +01002682 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Mark Shannon266b4622020-11-17 19:30:14 +00002683 NEXT_BLOCK(c);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002684 basicblock *end = compiler_new_block(c);
2685 if (end == NULL)
2686 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002687 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002688 compiler_use_next_block(c, cleanup);
2689 ADDOP(c, POP_TOP);
2690 if (!cond) {
Mark Shannon127dde52021-01-04 18:06:55 +00002691 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002692 }
2693 compiler_use_next_block(c, end);
2694 return 1;
2695 }
2696 /* fallback to general implementation */
2697 break;
2698 }
2699 default:
2700 /* fallback to general implementation */
2701 break;
2702 }
2703
2704 /* general implementation */
2705 VISIT(c, expr, e);
Mark Shannon582aaf12020-08-04 17:30:11 +01002706 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Mark Shannon266b4622020-11-17 19:30:14 +00002707 NEXT_BLOCK(c);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002708 return 1;
2709}
2710
2711static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002712compiler_ifexp(struct compiler *c, expr_ty e)
2713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002714 basicblock *end, *next;
2715
2716 assert(e->kind == IfExp_kind);
2717 end = compiler_new_block(c);
2718 if (end == NULL)
2719 return 0;
2720 next = compiler_new_block(c);
2721 if (next == NULL)
2722 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002723 if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2724 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002725 VISIT(c, expr, e->v.IfExp.body);
Mark Shannon127dde52021-01-04 18:06:55 +00002726 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002727 compiler_use_next_block(c, next);
2728 VISIT(c, expr, e->v.IfExp.orelse);
2729 compiler_use_next_block(c, end);
2730 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002731}
2732
2733static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002734compiler_lambda(struct compiler *c, expr_ty e)
2735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002736 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002737 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002738 static identifier name;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002739 Py_ssize_t funcflags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 arguments_ty args = e->v.Lambda.args;
2741 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002742
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002743 if (!compiler_check_debug_args(c, args))
2744 return 0;
2745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 if (!name) {
2747 name = PyUnicode_InternFromString("<lambda>");
2748 if (!name)
2749 return 0;
2750 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002751
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002752 funcflags = compiler_default_arguments(c, args);
2753 if (funcflags == -1) {
2754 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002756
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002757 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002758 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002759 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 /* Make None the first constant, so the lambda can't have a
2762 docstring. */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002763 if (compiler_add_const(c, Py_None) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002764 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002767 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002768 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2769 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2770 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002771 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 }
2773 else {
2774 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002775 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002776 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002777 qualname = c->u->u_qualname;
2778 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002779 compiler_exit_scope(c);
Pablo Galindo7fdab832021-01-29 22:40:59 +00002780 if (co == NULL) {
2781 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002782 return 0;
Pablo Galindo7fdab832021-01-29 22:40:59 +00002783 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002784
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002785 if (!compiler_make_closure(c, co, funcflags, qualname)) {
2786 Py_DECREF(qualname);
2787 Py_DECREF(co);
2788 return 0;
2789 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002790 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002791 Py_DECREF(co);
2792
2793 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002794}
2795
2796static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002797compiler_if(struct compiler *c, stmt_ty s)
2798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 basicblock *end, *next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002800 assert(s->kind == If_kind);
2801 end = compiler_new_block(c);
Mark Shannon8473cf82020-12-15 11:07:50 +00002802 if (end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002803 return 0;
Mark Shannon8473cf82020-12-15 11:07:50 +00002804 }
2805 if (asdl_seq_LEN(s->v.If.orelse)) {
2806 next = compiler_new_block(c);
2807 if (next == NULL) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002808 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002809 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002810 }
2811 else {
2812 next = end;
2813 }
2814 if (!compiler_jump_if(c, s->v.If.test, next, 0)) {
2815 return 0;
2816 }
2817 VISIT_SEQ(c, stmt, s->v.If.body);
2818 if (asdl_seq_LEN(s->v.If.orelse)) {
Mark Shannon127dde52021-01-04 18:06:55 +00002819 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Mark Shannon8473cf82020-12-15 11:07:50 +00002820 compiler_use_next_block(c, next);
2821 VISIT_SEQ(c, stmt, s->v.If.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002822 }
2823 compiler_use_next_block(c, end);
2824 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825}
2826
2827static int
2828compiler_for(struct compiler *c, stmt_ty s)
2829{
Mark Shannon5977a792020-12-02 13:31:40 +00002830 basicblock *start, *body, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 start = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002833 body = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 cleanup = compiler_new_block(c);
2835 end = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002836 if (start == NULL || body == NULL || end == NULL || cleanup == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002838 }
2839 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002841 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 VISIT(c, expr, s->v.For.iter);
2843 ADDOP(c, GET_ITER);
2844 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01002845 ADDOP_JUMP(c, FOR_ITER, cleanup);
Mark Shannon5977a792020-12-02 13:31:40 +00002846 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 VISIT(c, expr, s->v.For.target);
2848 VISIT_SEQ(c, stmt, s->v.For.body);
Mark Shannonf5e97b72020-12-14 11:28:39 +00002849 /* Mark jump as artificial */
2850 c->u->u_lineno = -1;
Mark Shannon582aaf12020-08-04 17:30:11 +01002851 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 compiler_use_next_block(c, cleanup);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002853
2854 compiler_pop_fblock(c, FOR_LOOP, start);
2855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 VISIT_SEQ(c, stmt, s->v.For.orelse);
2857 compiler_use_next_block(c, end);
2858 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002859}
2860
Yury Selivanov75445082015-05-11 22:57:16 -04002861
2862static int
2863compiler_async_for(struct compiler *c, stmt_ty s)
2864{
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002865 basicblock *start, *except, *end;
Pablo Galindo90235812020-03-15 04:29:22 +00002866 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07002867 c->u->u_ste->ste_coroutine = 1;
2868 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
Zsolt Dollensteine2396502018-04-27 08:58:56 -07002869 return compiler_error(c, "'async for' outside async function");
2870 }
2871
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002872 start = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002873 except = compiler_new_block(c);
2874 end = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002875
Mark Shannonfee55262019-11-21 09:11:43 +00002876 if (start == NULL || except == NULL || end == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002877 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002878 }
Yury Selivanov75445082015-05-11 22:57:16 -04002879 VISIT(c, expr, s->v.AsyncFor.iter);
2880 ADDOP(c, GET_AITER);
Yury Selivanov75445082015-05-11 22:57:16 -04002881
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002882 compiler_use_next_block(c, start);
Mark Shannonfee55262019-11-21 09:11:43 +00002883 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002884 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002885 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002886 /* SETUP_FINALLY to guard the __anext__ call */
Mark Shannon582aaf12020-08-04 17:30:11 +01002887 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov75445082015-05-11 22:57:16 -04002888 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002889 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04002890 ADDOP(c, YIELD_FROM);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002891 ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */
Yury Selivanov75445082015-05-11 22:57:16 -04002892
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002893 /* Success block for __anext__ */
2894 VISIT(c, expr, s->v.AsyncFor.target);
2895 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002896 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002897
2898 compiler_pop_fblock(c, FOR_LOOP, start);
Yury Selivanov75445082015-05-11 22:57:16 -04002899
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002900 /* Except block for __anext__ */
Yury Selivanov75445082015-05-11 22:57:16 -04002901 compiler_use_next_block(c, except);
Mark Shannon877df852020-11-12 09:43:29 +00002902
2903 c->u->u_lineno = -1;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002904 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov75445082015-05-11 22:57:16 -04002905
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002906 /* `else` block */
Yury Selivanov75445082015-05-11 22:57:16 -04002907 VISIT_SEQ(c, stmt, s->v.For.orelse);
2908
2909 compiler_use_next_block(c, end);
2910
2911 return 1;
2912}
2913
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002914static int
2915compiler_while(struct compiler *c, stmt_ty s)
2916{
Mark Shannon266b4622020-11-17 19:30:14 +00002917 basicblock *loop, *body, *end, *anchor = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 loop = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002919 body = compiler_new_block(c);
2920 anchor = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 end = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002922 if (loop == NULL || body == NULL || anchor == NULL || end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 compiler_use_next_block(c, loop);
Mark Shannon266b4622020-11-17 19:30:14 +00002926 if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002928 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002929 if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) {
2930 return 0;
Mark Shannon266b4622020-11-17 19:30:14 +00002931 }
2932
2933 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 VISIT_SEQ(c, stmt, s->v.While.body);
Mark Shannon8473cf82020-12-15 11:07:50 +00002935 SET_LOC(c, s);
2936 if (!compiler_jump_if(c, s->v.While.test, body, 1)) {
2937 return 0;
2938 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002939
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002940 compiler_pop_fblock(c, WHILE_LOOP, loop);
2941
Mark Shannon266b4622020-11-17 19:30:14 +00002942 compiler_use_next_block(c, anchor);
2943 if (s->v.While.orelse) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002944 VISIT_SEQ(c, stmt, s->v.While.orelse);
Mark Shannon266b4622020-11-17 19:30:14 +00002945 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002948 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002949}
2950
2951static int
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002952compiler_return(struct compiler *c, stmt_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002953{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002954 int preserve_tos = ((s->v.Return.value != NULL) &&
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002955 (s->v.Return.value->kind != Constant_kind));
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002956 if (c->u->u_ste->ste_type != FunctionBlock)
2957 return compiler_error(c, "'return' outside function");
2958 if (s->v.Return.value != NULL &&
2959 c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2960 {
2961 return compiler_error(
2962 c, "'return' with value in async generator");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002963 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002964 if (preserve_tos) {
2965 VISIT(c, expr, s->v.Return.value);
Mark Shannon5274b682020-12-16 13:07:01 +00002966 } else {
2967 /* Emit instruction with line number for expression */
2968 if (s->v.Return.value != NULL) {
2969 SET_LOC(c, s->v.Return.value);
2970 ADDOP(c, NOP);
2971 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002972 }
Mark Shannonfee55262019-11-21 09:11:43 +00002973 if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL))
2974 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002975 if (s->v.Return.value == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002976 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002977 }
2978 else if (!preserve_tos) {
Mark Shannon5274b682020-12-16 13:07:01 +00002979 ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002980 }
2981 ADDOP(c, RETURN_VALUE);
Mark Shannon266b4622020-11-17 19:30:14 +00002982 NEXT_BLOCK(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002984 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002985}
2986
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002987static int
2988compiler_break(struct compiler *c)
2989{
Mark Shannonfee55262019-11-21 09:11:43 +00002990 struct fblockinfo *loop = NULL;
2991 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
2992 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002993 }
Mark Shannonfee55262019-11-21 09:11:43 +00002994 if (loop == NULL) {
2995 return compiler_error(c, "'break' outside loop");
2996 }
2997 if (!compiler_unwind_fblock(c, loop, 0)) {
2998 return 0;
2999 }
Mark Shannon582aaf12020-08-04 17:30:11 +01003000 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit);
Mark Shannon266b4622020-11-17 19:30:14 +00003001 NEXT_BLOCK(c);
Mark Shannonfee55262019-11-21 09:11:43 +00003002 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003003}
3004
3005static int
3006compiler_continue(struct compiler *c)
3007{
Mark Shannonfee55262019-11-21 09:11:43 +00003008 struct fblockinfo *loop = NULL;
3009 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
3010 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003011 }
Mark Shannonfee55262019-11-21 09:11:43 +00003012 if (loop == NULL) {
3013 return compiler_error(c, "'continue' not properly in loop");
3014 }
Mark Shannon582aaf12020-08-04 17:30:11 +01003015 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block);
Mark Shannon266b4622020-11-17 19:30:14 +00003016 NEXT_BLOCK(c)
Mark Shannonfee55262019-11-21 09:11:43 +00003017 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003018}
3019
3020
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003021/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022
3023 SETUP_FINALLY L
3024 <code for body>
3025 POP_BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00003026 <code for finalbody>
3027 JUMP E
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003028 L:
3029 <code for finalbody>
Mark Shannonfee55262019-11-21 09:11:43 +00003030 E:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003032 The special instructions use the block stack. Each block
3033 stack entry contains the instruction that created it (here
3034 SETUP_FINALLY), the level of the value stack at the time the
3035 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003036
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003037 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003038 Pushes the current value stack level and the label
3039 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003040 POP_BLOCK:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003041 Pops en entry from the block stack.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003042
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003043 The block stack is unwound when an exception is raised:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003044 when a SETUP_FINALLY entry is found, the raised and the caught
3045 exceptions are pushed onto the value stack (and the exception
3046 condition is cleared), and the interpreter jumps to the label
3047 gotten from the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003048*/
3049
3050static int
3051compiler_try_finally(struct compiler *c, stmt_ty s)
3052{
Mark Shannonfee55262019-11-21 09:11:43 +00003053 basicblock *body, *end, *exit;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 body = compiler_new_block(c);
3056 end = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00003057 exit = compiler_new_block(c);
3058 if (body == NULL || end == NULL || exit == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003060
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003061 /* `try` block */
Mark Shannon582aaf12020-08-04 17:30:11 +01003062 ADDOP_JUMP(c, SETUP_FINALLY, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 compiler_use_next_block(c, body);
Mark Shannonfee55262019-11-21 09:11:43 +00003064 if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003066 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
3067 if (!compiler_try_except(c, s))
3068 return 0;
3069 }
3070 else {
3071 VISIT_SEQ(c, stmt, s->v.Try.body);
3072 }
Mark Shannon3bd60352021-01-13 12:05:43 +00003073 ADDOP_NOLINE(c, POP_BLOCK);
Mark Shannonfee55262019-11-21 09:11:43 +00003074 compiler_pop_fblock(c, FINALLY_TRY, body);
3075 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Mark Shannon127dde52021-01-04 18:06:55 +00003076 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00003077 /* `finally` block */
3078 compiler_use_next_block(c, end);
3079 if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL))
3080 return 0;
3081 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3082 compiler_pop_fblock(c, FINALLY_END, end);
Mark Shannonbf353f32020-12-17 13:55:28 +00003083 ADDOP_I(c, RERAISE, 0);
Mark Shannonfee55262019-11-21 09:11:43 +00003084 compiler_use_next_block(c, exit);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003085 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003086}
3087
3088/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003089 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003090 (The contents of the value stack is shown in [], with the top
3091 at the right; 'tb' is trace-back info, 'val' the exception's
3092 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093
3094 Value stack Label Instruction Argument
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003095 [] SETUP_FINALLY L1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003096 [] <code for S>
3097 [] POP_BLOCK
3098 [] JUMP_FORWARD L0
3099
3100 [tb, val, exc] L1: DUP )
3101 [tb, val, exc, exc] <evaluate E1> )
Mark Shannon9af0e472020-01-14 10:12:45 +00003102 [tb, val, exc, exc, E1] JUMP_IF_NOT_EXC_MATCH L2 ) only if E1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 [tb, val, exc] POP
3104 [tb, val] <assign to V1> (or POP if no V1)
3105 [tb] POP
3106 [] <code for S1>
3107 JUMP_FORWARD L0
3108
3109 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003110 .............................etc.......................
3111
Mark Shannonfee55262019-11-21 09:11:43 +00003112 [tb, val, exc] Ln+1: RERAISE # re-raise exception
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113
3114 [] L0: <next statement>
3115
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003116 Of course, parts are not generated if Vi or Ei is not present.
3117*/
3118static int
3119compiler_try_except(struct compiler *c, stmt_ty s)
3120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003122 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003124 body = compiler_new_block(c);
3125 except = compiler_new_block(c);
3126 orelse = compiler_new_block(c);
3127 end = compiler_new_block(c);
3128 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
3129 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01003130 ADDOP_JUMP(c, SETUP_FINALLY, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131 compiler_use_next_block(c, body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003132 if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003133 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003134 VISIT_SEQ(c, stmt, s->v.Try.body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003135 compiler_pop_fblock(c, TRY_EXCEPT, body);
Mark Shannon3bd60352021-01-13 12:05:43 +00003136 ADDOP_NOLINE(c, POP_BLOCK);
3137 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003138 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 compiler_use_next_block(c, except);
Mark Shannon02d126a2020-09-25 14:04:19 +01003140 /* Runtime will push a block here, so we need to account for that */
3141 if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL))
3142 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003143 for (i = 0; i < n; i++) {
3144 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003145 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003146 if (!handler->v.ExceptHandler.type && i < n-1)
3147 return compiler_error(c, "default 'except:' must be last");
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003148 SET_LOC(c, handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003149 except = compiler_new_block(c);
3150 if (except == NULL)
3151 return 0;
3152 if (handler->v.ExceptHandler.type) {
3153 ADDOP(c, DUP_TOP);
3154 VISIT(c, expr, handler->v.ExceptHandler.type);
Mark Shannon582aaf12020-08-04 17:30:11 +01003155 ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except);
Mark Shannon266b4622020-11-17 19:30:14 +00003156 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003157 }
3158 ADDOP(c, POP_TOP);
3159 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003160 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00003161
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003162 cleanup_end = compiler_new_block(c);
3163 cleanup_body = compiler_new_block(c);
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003164 if (cleanup_end == NULL || cleanup_body == NULL) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003165 return 0;
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003166 }
Guido van Rossumb940e112007-01-10 16:19:56 +00003167
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003168 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3169 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003170
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003171 /*
3172 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003173 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003174 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003175 try:
3176 # body
3177 finally:
Chris Angelicoad098b62019-05-21 23:34:19 +10003178 name = None # in case body contains "del name"
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003179 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003180 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003182 /* second try: */
Mark Shannon582aaf12020-08-04 17:30:11 +01003183 ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003184 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003185 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003186 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003187
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003188 /* second # body */
3189 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003190 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003191 ADDOP(c, POP_BLOCK);
3192 ADDOP(c, POP_EXCEPT);
Mark Shannon877df852020-11-12 09:43:29 +00003193 /* name = None; del name; # Mark as artificial */
3194 c->u->u_lineno = -1;
Mark Shannonfee55262019-11-21 09:11:43 +00003195 ADDOP_LOAD_CONST(c, Py_None);
3196 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3197 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Mark Shannon582aaf12020-08-04 17:30:11 +01003198 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003199
Mark Shannonfee55262019-11-21 09:11:43 +00003200 /* except: */
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003201 compiler_use_next_block(c, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003202
Mark Shannon877df852020-11-12 09:43:29 +00003203 /* name = None; del name; # Mark as artificial */
3204 c->u->u_lineno = -1;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003205 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003206 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003207 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208
Mark Shannonbf353f32020-12-17 13:55:28 +00003209 ADDOP_I(c, RERAISE, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 }
3211 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003212 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003213
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003214 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05003215 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003216 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217
Guido van Rossumb940e112007-01-10 16:19:56 +00003218 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003219 ADDOP(c, POP_TOP);
3220 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003221 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003222 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003224 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannon127dde52021-01-04 18:06:55 +00003225 /* name = None; del name; # Mark as artificial */
3226 c->u->u_lineno = -1;
Mark Shannonfee55262019-11-21 09:11:43 +00003227 ADDOP(c, POP_EXCEPT);
Mark Shannon582aaf12020-08-04 17:30:11 +01003228 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003229 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230 compiler_use_next_block(c, except);
3231 }
Mark Shannon02d126a2020-09-25 14:04:19 +01003232 compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL);
Mark Shannonf2dbfd72020-12-21 13:53:50 +00003233 /* Mark as artificial */
3234 c->u->u_lineno = -1;
Mark Shannonbf353f32020-12-17 13:55:28 +00003235 ADDOP_I(c, RERAISE, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003236 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003237 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003238 compiler_use_next_block(c, end);
3239 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003240}
3241
3242static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003243compiler_try(struct compiler *c, stmt_ty s) {
3244 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
3245 return compiler_try_finally(c, s);
3246 else
3247 return compiler_try_except(c, s);
3248}
3249
3250
3251static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003252compiler_import_as(struct compiler *c, identifier name, identifier asname)
3253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 /* The IMPORT_NAME opcode was already generated. This function
3255 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257 If there is a dot in name, we need to split it and emit a
Serhiy Storchakaf93234b2017-05-09 22:31:05 +03003258 IMPORT_FROM for each name.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003259 */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003260 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
3261 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003262 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003263 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003264 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003265 /* Consume the base module name to get the first attribute */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003266 while (1) {
3267 Py_ssize_t pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003268 PyObject *attr;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003269 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003270 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003271 return 0;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003272 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003273 if (!attr)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003274 return 0;
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003275 ADDOP_N(c, IMPORT_FROM, attr, names);
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003276 if (dot == -1) {
3277 break;
3278 }
3279 ADDOP(c, ROT_TWO);
3280 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003281 }
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003282 if (!compiler_nameop(c, asname, Store)) {
3283 return 0;
3284 }
3285 ADDOP(c, POP_TOP);
3286 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003287 }
3288 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003289}
3290
3291static int
3292compiler_import(struct compiler *c, stmt_ty s)
3293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003294 /* The Import node stores a module name like a.b.c as a single
3295 string. This is convenient for all cases except
3296 import a.b.c as d
3297 where we need to parse that string to extract the individual
3298 module names.
3299 XXX Perhaps change the representation to make this case simpler?
3300 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01003301 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003302
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003303 PyObject *zero = _PyLong_GetZero(); // borrowed reference
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003304 for (i = 0; i < n; i++) {
3305 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
3306 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003307
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003308 ADDOP_LOAD_CONST(c, zero);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003309 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003310 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003312 if (alias->asname) {
3313 r = compiler_import_as(c, alias->name, alias->asname);
3314 if (!r)
3315 return r;
3316 }
3317 else {
3318 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003319 Py_ssize_t dot = PyUnicode_FindChar(
3320 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02003321 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003322 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02003323 if (tmp == NULL)
3324 return 0;
3325 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003327 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003328 Py_DECREF(tmp);
3329 }
3330 if (!r)
3331 return r;
3332 }
3333 }
3334 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003335}
3336
3337static int
3338compiler_from_import(struct compiler *c, stmt_ty s)
3339{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003340 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003341 PyObject *names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003342 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003344 if (!empty_string) {
3345 empty_string = PyUnicode_FromString("");
3346 if (!empty_string)
3347 return 0;
3348 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003349
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003350 ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02003351
3352 names = PyTuple_New(n);
3353 if (!names)
3354 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003356 /* build up the names */
3357 for (i = 0; i < n; i++) {
3358 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3359 Py_INCREF(alias->name);
3360 PyTuple_SET_ITEM(names, i, alias->name);
3361 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003363 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003364 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003365 Py_DECREF(names);
3366 return compiler_error(c, "from __future__ imports must occur "
3367 "at the beginning of the file");
3368 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003369 ADDOP_LOAD_CONST_NEW(c, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371 if (s->v.ImportFrom.module) {
3372 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
3373 }
3374 else {
3375 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
3376 }
3377 for (i = 0; i < n; i++) {
3378 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3379 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003380
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003381 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003382 assert(n == 1);
3383 ADDOP(c, IMPORT_STAR);
3384 return 1;
3385 }
3386
3387 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
3388 store_name = alias->name;
3389 if (alias->asname)
3390 store_name = alias->asname;
3391
3392 if (!compiler_nameop(c, store_name, Store)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003393 return 0;
3394 }
3395 }
3396 /* remove imported module */
3397 ADDOP(c, POP_TOP);
3398 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003399}
3400
3401static int
3402compiler_assert(struct compiler *c, stmt_ty s)
3403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003404 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003405
tsukasa-aua8ef4572021-03-16 22:14:41 +11003406 /* Always emit a warning if the test is a non-zero length tuple */
3407 if ((s->v.Assert.test->kind == Tuple_kind &&
3408 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) ||
3409 (s->v.Assert.test->kind == Constant_kind &&
3410 PyTuple_Check(s->v.Assert.test->v.Constant.value) &&
3411 PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0))
Serhiy Storchakad31e7732018-10-21 10:09:39 +03003412 {
3413 if (!compiler_warn(c, "assertion is always true, "
3414 "perhaps remove parentheses?"))
3415 {
Victor Stinner14e461d2013-08-26 22:28:21 +02003416 return 0;
3417 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003418 }
tsukasa-aua8ef4572021-03-16 22:14:41 +11003419 if (c->c_optimize)
3420 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003421 end = compiler_new_block(c);
3422 if (end == NULL)
3423 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003424 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
3425 return 0;
Zackery Spytzce6a0702019-08-25 03:44:09 -06003426 ADDOP(c, LOAD_ASSERTION_ERROR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003427 if (s->v.Assert.msg) {
3428 VISIT(c, expr, s->v.Assert.msg);
3429 ADDOP_I(c, CALL_FUNCTION, 1);
3430 }
3431 ADDOP_I(c, RAISE_VARARGS, 1);
3432 compiler_use_next_block(c, end);
3433 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003434}
3435
3436static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003437compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3438{
3439 if (c->c_interactive && c->c_nestlevel <= 1) {
3440 VISIT(c, expr, value);
3441 ADDOP(c, PRINT_EXPR);
3442 return 1;
3443 }
3444
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003445 if (value->kind == Constant_kind) {
Victor Stinner15a30952016-02-08 22:45:06 +01003446 /* ignore constant statement */
Mark Shannon877df852020-11-12 09:43:29 +00003447 ADDOP(c, NOP);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003448 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003449 }
3450
3451 VISIT(c, expr, value);
Mark Shannonc5440932021-03-15 14:24:25 +00003452 /* Mark POP_TOP as artificial */
3453 c->u->u_lineno = -1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003454 ADDOP(c, POP_TOP);
3455 return 1;
3456}
3457
3458static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003459compiler_visit_stmt(struct compiler *c, stmt_ty s)
3460{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003461 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003463 /* Always assign a lineno to the next instruction for a stmt. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003464 SET_LOC(c, s);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003466 switch (s->kind) {
3467 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04003468 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003469 case ClassDef_kind:
3470 return compiler_class(c, s);
3471 case Return_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003472 return compiler_return(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003473 case Delete_kind:
3474 VISIT_SEQ(c, expr, s->v.Delete.targets)
3475 break;
3476 case Assign_kind:
3477 n = asdl_seq_LEN(s->v.Assign.targets);
3478 VISIT(c, expr, s->v.Assign.value);
3479 for (i = 0; i < n; i++) {
3480 if (i < n - 1)
3481 ADDOP(c, DUP_TOP);
3482 VISIT(c, expr,
3483 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3484 }
3485 break;
3486 case AugAssign_kind:
3487 return compiler_augassign(c, s);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003488 case AnnAssign_kind:
3489 return compiler_annassign(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003490 case For_kind:
3491 return compiler_for(c, s);
3492 case While_kind:
3493 return compiler_while(c, s);
3494 case If_kind:
3495 return compiler_if(c, s);
Brandt Bucher145bf262021-02-26 14:51:55 -08003496 case Match_kind:
3497 return compiler_match(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003498 case Raise_kind:
3499 n = 0;
3500 if (s->v.Raise.exc) {
3501 VISIT(c, expr, s->v.Raise.exc);
3502 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003503 if (s->v.Raise.cause) {
3504 VISIT(c, expr, s->v.Raise.cause);
3505 n++;
3506 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003507 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01003508 ADDOP_I(c, RAISE_VARARGS, (int)n);
Mark Shannon266b4622020-11-17 19:30:14 +00003509 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003510 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003511 case Try_kind:
3512 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003513 case Assert_kind:
3514 return compiler_assert(c, s);
3515 case Import_kind:
3516 return compiler_import(c, s);
3517 case ImportFrom_kind:
3518 return compiler_from_import(c, s);
3519 case Global_kind:
3520 case Nonlocal_kind:
3521 break;
3522 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003523 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003524 case Pass_kind:
Mark Shannon877df852020-11-12 09:43:29 +00003525 ADDOP(c, NOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003526 break;
3527 case Break_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003528 return compiler_break(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003529 case Continue_kind:
3530 return compiler_continue(c);
3531 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003532 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04003533 case AsyncFunctionDef_kind:
3534 return compiler_function(c, s, 1);
3535 case AsyncWith_kind:
3536 return compiler_async_with(c, s, 0);
3537 case AsyncFor_kind:
3538 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003539 }
Yury Selivanov75445082015-05-11 22:57:16 -04003540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003541 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003542}
3543
3544static int
3545unaryop(unaryop_ty op)
3546{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547 switch (op) {
3548 case Invert:
3549 return UNARY_INVERT;
3550 case Not:
3551 return UNARY_NOT;
3552 case UAdd:
3553 return UNARY_POSITIVE;
3554 case USub:
3555 return UNARY_NEGATIVE;
3556 default:
3557 PyErr_Format(PyExc_SystemError,
3558 "unary op %d should not be possible", op);
3559 return 0;
3560 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003561}
3562
3563static int
Andy Lester76d58772020-03-10 21:18:12 -05003564binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003566 switch (op) {
3567 case Add:
3568 return BINARY_ADD;
3569 case Sub:
3570 return BINARY_SUBTRACT;
3571 case Mult:
3572 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003573 case MatMult:
3574 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003575 case Div:
3576 return BINARY_TRUE_DIVIDE;
3577 case Mod:
3578 return BINARY_MODULO;
3579 case Pow:
3580 return BINARY_POWER;
3581 case LShift:
3582 return BINARY_LSHIFT;
3583 case RShift:
3584 return BINARY_RSHIFT;
3585 case BitOr:
3586 return BINARY_OR;
3587 case BitXor:
3588 return BINARY_XOR;
3589 case BitAnd:
3590 return BINARY_AND;
3591 case FloorDiv:
3592 return BINARY_FLOOR_DIVIDE;
3593 default:
3594 PyErr_Format(PyExc_SystemError,
3595 "binary op %d should not be possible", op);
3596 return 0;
3597 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003598}
3599
3600static int
Andy Lester76d58772020-03-10 21:18:12 -05003601inplace_binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003603 switch (op) {
3604 case Add:
3605 return INPLACE_ADD;
3606 case Sub:
3607 return INPLACE_SUBTRACT;
3608 case Mult:
3609 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003610 case MatMult:
3611 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 case Div:
3613 return INPLACE_TRUE_DIVIDE;
3614 case Mod:
3615 return INPLACE_MODULO;
3616 case Pow:
3617 return INPLACE_POWER;
3618 case LShift:
3619 return INPLACE_LSHIFT;
3620 case RShift:
3621 return INPLACE_RSHIFT;
3622 case BitOr:
3623 return INPLACE_OR;
3624 case BitXor:
3625 return INPLACE_XOR;
3626 case BitAnd:
3627 return INPLACE_AND;
3628 case FloorDiv:
3629 return INPLACE_FLOOR_DIVIDE;
3630 default:
3631 PyErr_Format(PyExc_SystemError,
3632 "inplace binary op %d should not be possible", op);
3633 return 0;
3634 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003635}
3636
3637static int
3638compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3639{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003640 int op, scope;
3641 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003642 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003644 PyObject *dict = c->u->u_names;
3645 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003646
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003647 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3648 !_PyUnicode_EqualToASCIIString(name, "True") &&
3649 !_PyUnicode_EqualToASCIIString(name, "False"));
Benjamin Peterson70b224d2012-12-06 17:49:58 -05003650
Pablo Galindoc5fc1562020-04-22 23:29:27 +01003651 if (forbidden_name(c, name, ctx))
3652 return 0;
3653
Serhiy Storchakabd6ec4d2017-12-18 14:29:12 +02003654 mangled = _Py_Mangle(c->u->u_private, name);
3655 if (!mangled)
3656 return 0;
3657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003658 op = 0;
3659 optype = OP_NAME;
Victor Stinner28ad12f2021-03-19 12:41:49 +01003660 scope = _PyST_GetScope(c->u->u_ste, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003661 switch (scope) {
3662 case FREE:
3663 dict = c->u->u_freevars;
3664 optype = OP_DEREF;
3665 break;
3666 case CELL:
3667 dict = c->u->u_cellvars;
3668 optype = OP_DEREF;
3669 break;
3670 case LOCAL:
3671 if (c->u->u_ste->ste_type == FunctionBlock)
3672 optype = OP_FAST;
3673 break;
3674 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04003675 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003676 optype = OP_GLOBAL;
3677 break;
3678 case GLOBAL_EXPLICIT:
3679 optype = OP_GLOBAL;
3680 break;
3681 default:
3682 /* scope can be 0 */
3683 break;
3684 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003686 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003687 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003689 switch (optype) {
3690 case OP_DEREF:
3691 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003692 case Load:
3693 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3694 break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003695 case Store: op = STORE_DEREF; break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003696 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003697 }
3698 break;
3699 case OP_FAST:
3700 switch (ctx) {
3701 case Load: op = LOAD_FAST; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003702 case Store: op = STORE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003703 case Del: op = DELETE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003704 }
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003705 ADDOP_N(c, op, mangled, varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003706 return 1;
3707 case OP_GLOBAL:
3708 switch (ctx) {
3709 case Load: op = LOAD_GLOBAL; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003710 case Store: op = STORE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 case Del: op = DELETE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003712 }
3713 break;
3714 case OP_NAME:
3715 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00003716 case Load: op = LOAD_NAME; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003717 case Store: op = STORE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003718 case Del: op = DELETE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003719 }
3720 break;
3721 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003723 assert(op);
Andy Lester76d58772020-03-10 21:18:12 -05003724 arg = compiler_add_o(dict, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003725 Py_DECREF(mangled);
3726 if (arg < 0)
3727 return 0;
3728 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003729}
3730
3731static int
3732compiler_boolop(struct compiler *c, expr_ty e)
3733{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003734 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003735 int jumpi;
3736 Py_ssize_t i, n;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003737 asdl_expr_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003739 assert(e->kind == BoolOp_kind);
3740 if (e->v.BoolOp.op == And)
3741 jumpi = JUMP_IF_FALSE_OR_POP;
3742 else
3743 jumpi = JUMP_IF_TRUE_OR_POP;
3744 end = compiler_new_block(c);
3745 if (end == NULL)
3746 return 0;
3747 s = e->v.BoolOp.values;
3748 n = asdl_seq_LEN(s) - 1;
3749 assert(n >= 0);
3750 for (i = 0; i < n; ++i) {
3751 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003752 ADDOP_JUMP(c, jumpi, end);
Mark Shannon6e8128f2020-07-30 10:03:00 +01003753 basicblock *next = compiler_new_block(c);
3754 if (next == NULL) {
3755 return 0;
3756 }
3757 compiler_use_next_block(c, next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 }
3759 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3760 compiler_use_next_block(c, end);
3761 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003762}
3763
3764static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003765starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
Mark Shannon13bc1392020-01-23 09:25:17 +00003766 int build, int add, int extend, int tuple)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003767{
3768 Py_ssize_t n = asdl_seq_LEN(elts);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003769 if (n > 2 && are_all_items_const(elts, 0, n)) {
3770 PyObject *folded = PyTuple_New(n);
3771 if (folded == NULL) {
3772 return 0;
3773 }
3774 PyObject *val;
Mark Shannon11e0b292021-04-15 14:28:56 +01003775 for (Py_ssize_t i = 0; i < n; i++) {
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003776 val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
3777 Py_INCREF(val);
3778 PyTuple_SET_ITEM(folded, i, val);
3779 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003780 if (tuple) {
3781 ADDOP_LOAD_CONST_NEW(c, folded);
3782 } else {
3783 if (add == SET_ADD) {
3784 Py_SETREF(folded, PyFrozenSet_New(folded));
3785 if (folded == NULL) {
3786 return 0;
3787 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003788 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003789 ADDOP_I(c, build, pushed);
3790 ADDOP_LOAD_CONST_NEW(c, folded);
3791 ADDOP_I(c, extend, 1);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003792 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003793 return 1;
3794 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003795
Mark Shannon11e0b292021-04-15 14:28:56 +01003796 int big = n+pushed > STACK_USE_GUIDELINE;
3797 int seen_star = 0;
3798 for (Py_ssize_t i = 0; i < n; i++) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003799 expr_ty elt = asdl_seq_GET(elts, i);
3800 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003801 seen_star = 1;
3802 }
3803 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003804 if (!seen_star && !big) {
3805 for (Py_ssize_t i = 0; i < n; i++) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003806 expr_ty elt = asdl_seq_GET(elts, i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003807 VISIT(c, expr, elt);
Mark Shannon13bc1392020-01-23 09:25:17 +00003808 }
3809 if (tuple) {
3810 ADDOP_I(c, BUILD_TUPLE, n+pushed);
3811 } else {
3812 ADDOP_I(c, build, n+pushed);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003813 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003814 return 1;
3815 }
3816 int sequence_built = 0;
3817 if (big) {
3818 ADDOP_I(c, build, pushed);
3819 sequence_built = 1;
3820 }
3821 for (Py_ssize_t i = 0; i < n; i++) {
3822 expr_ty elt = asdl_seq_GET(elts, i);
3823 if (elt->kind == Starred_kind) {
3824 if (sequence_built == 0) {
3825 ADDOP_I(c, build, i+pushed);
3826 sequence_built = 1;
3827 }
3828 VISIT(c, expr, elt->v.Starred.value);
3829 ADDOP_I(c, extend, 1);
3830 }
3831 else {
3832 VISIT(c, expr, elt);
3833 if (sequence_built) {
3834 ADDOP_I(c, add, 1);
3835 }
3836 }
3837 }
3838 assert(sequence_built);
3839 if (tuple) {
3840 ADDOP(c, LIST_TO_TUPLE);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003841 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003842 return 1;
3843}
3844
3845static int
Brandt Bucher145bf262021-02-26 14:51:55 -08003846unpack_helper(struct compiler *c, asdl_expr_seq *elts)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003847{
3848 Py_ssize_t n = asdl_seq_LEN(elts);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003849 int seen_star = 0;
Brandt Bucher145bf262021-02-26 14:51:55 -08003850 for (Py_ssize_t i = 0; i < n; i++) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003851 expr_ty elt = asdl_seq_GET(elts, i);
3852 if (elt->kind == Starred_kind && !seen_star) {
3853 if ((i >= (1 << 8)) ||
3854 (n-i-1 >= (INT_MAX >> 8)))
3855 return compiler_error(c,
3856 "too many expressions in "
3857 "star-unpacking assignment");
3858 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3859 seen_star = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003860 }
3861 else if (elt->kind == Starred_kind) {
3862 return compiler_error(c,
Furkan Öndercb6534e2020-03-26 04:54:31 +03003863 "multiple starred expressions in assignment");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003864 }
3865 }
3866 if (!seen_star) {
3867 ADDOP_I(c, UNPACK_SEQUENCE, n);
3868 }
Brandt Bucher145bf262021-02-26 14:51:55 -08003869 return 1;
3870}
3871
3872static int
3873assignment_helper(struct compiler *c, asdl_expr_seq *elts)
3874{
3875 Py_ssize_t n = asdl_seq_LEN(elts);
3876 RETURN_IF_FALSE(unpack_helper(c, elts));
3877 for (Py_ssize_t i = 0; i < n; i++) {
Brandt Bucherd5aa2e92020-03-07 19:44:18 -08003878 expr_ty elt = asdl_seq_GET(elts, i);
3879 VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value);
3880 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003881 return 1;
3882}
3883
3884static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003885compiler_list(struct compiler *c, expr_ty e)
3886{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003887 asdl_expr_seq *elts = e->v.List.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003888 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003889 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003890 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003891 else if (e->v.List.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003892 return starunpack_helper(c, elts, 0, BUILD_LIST,
3893 LIST_APPEND, LIST_EXTEND, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003894 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003895 else
3896 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003897 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003898}
3899
3900static int
3901compiler_tuple(struct compiler *c, expr_ty e)
3902{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003903 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003904 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003905 return assignment_helper(c, elts);
3906 }
3907 else if (e->v.Tuple.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003908 return starunpack_helper(c, elts, 0, BUILD_LIST,
3909 LIST_APPEND, LIST_EXTEND, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003910 }
3911 else
3912 VISIT_SEQ(c, expr, elts);
3913 return 1;
3914}
3915
3916static int
3917compiler_set(struct compiler *c, expr_ty e)
3918{
Mark Shannon13bc1392020-01-23 09:25:17 +00003919 return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET,
3920 SET_ADD, SET_UPDATE, 0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003921}
3922
3923static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003924are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003925{
3926 Py_ssize_t i;
3927 for (i = begin; i < end; i++) {
3928 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003929 if (key == NULL || key->kind != Constant_kind)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003930 return 0;
3931 }
3932 return 1;
3933}
3934
3935static int
3936compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3937{
3938 Py_ssize_t i, n = end - begin;
3939 PyObject *keys, *key;
Mark Shannon11e0b292021-04-15 14:28:56 +01003940 int big = n*2 > STACK_USE_GUIDELINE;
3941 if (n > 1 && !big && are_all_items_const(e->v.Dict.keys, begin, end)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003942 for (i = begin; i < end; i++) {
3943 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3944 }
3945 keys = PyTuple_New(n);
3946 if (keys == NULL) {
3947 return 0;
3948 }
3949 for (i = begin; i < end; i++) {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003950 key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003951 Py_INCREF(key);
3952 PyTuple_SET_ITEM(keys, i - begin, key);
3953 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003954 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003955 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
Mark Shannon11e0b292021-04-15 14:28:56 +01003956 return 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003957 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003958 if (big) {
3959 ADDOP_I(c, BUILD_MAP, 0);
3960 }
3961 for (i = begin; i < end; i++) {
3962 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3963 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3964 if (big) {
3965 ADDOP_I(c, MAP_ADD, 1);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003966 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003967 }
3968 if (!big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003969 ADDOP_I(c, BUILD_MAP, n);
3970 }
3971 return 1;
3972}
3973
3974static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003975compiler_dict(struct compiler *c, expr_ty e)
3976{
Victor Stinner976bb402016-03-23 11:36:19 +01003977 Py_ssize_t i, n, elements;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003978 int have_dict;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003979 int is_unpacking = 0;
3980 n = asdl_seq_LEN(e->v.Dict.values);
Mark Shannon8a4cd702020-01-27 09:57:45 +00003981 have_dict = 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003982 elements = 0;
3983 for (i = 0; i < n; i++) {
3984 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003985 if (is_unpacking) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003986 if (elements) {
3987 if (!compiler_subdict(c, e, i - elements, i)) {
3988 return 0;
3989 }
3990 if (have_dict) {
3991 ADDOP_I(c, DICT_UPDATE, 1);
3992 }
3993 have_dict = 1;
3994 elements = 0;
3995 }
3996 if (have_dict == 0) {
3997 ADDOP_I(c, BUILD_MAP, 0);
3998 have_dict = 1;
3999 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004000 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Mark Shannon8a4cd702020-01-27 09:57:45 +00004001 ADDOP_I(c, DICT_UPDATE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004002 }
4003 else {
Mark Shannon11e0b292021-04-15 14:28:56 +01004004 if (elements*2 > STACK_USE_GUIDELINE) {
Pablo Galindoc51db0e2020-08-13 09:48:41 +01004005 if (!compiler_subdict(c, e, i - elements, i + 1)) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004006 return 0;
4007 }
4008 if (have_dict) {
4009 ADDOP_I(c, DICT_UPDATE, 1);
4010 }
4011 have_dict = 1;
4012 elements = 0;
4013 }
4014 else {
4015 elements++;
4016 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004017 }
4018 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004019 if (elements) {
4020 if (!compiler_subdict(c, e, n - elements, n)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004021 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004022 }
4023 if (have_dict) {
4024 ADDOP_I(c, DICT_UPDATE, 1);
4025 }
4026 have_dict = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004027 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004028 if (!have_dict) {
4029 ADDOP_I(c, BUILD_MAP, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004030 }
4031 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004032}
4033
4034static int
4035compiler_compare(struct compiler *c, expr_ty e)
4036{
Victor Stinnerad9a0662013-11-19 22:23:20 +01004037 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004038
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02004039 if (!check_compare(c, e)) {
4040 return 0;
4041 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004042 VISIT(c, expr, e->v.Compare.left);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004043 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
4044 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
4045 if (n == 0) {
4046 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Mark Shannon9af0e472020-01-14 10:12:45 +00004047 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004048 }
4049 else {
4050 basicblock *cleanup = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004051 if (cleanup == NULL)
4052 return 0;
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004053 for (i = 0; i < n; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004054 VISIT(c, expr,
4055 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004056 ADDOP(c, DUP_TOP);
4057 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00004058 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01004059 ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004060 NEXT_BLOCK(c);
4061 }
4062 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00004063 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004064 basicblock *end = compiler_new_block(c);
4065 if (end == NULL)
4066 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00004067 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 compiler_use_next_block(c, cleanup);
4069 ADDOP(c, ROT_TWO);
4070 ADDOP(c, POP_TOP);
4071 compiler_use_next_block(c, end);
4072 }
4073 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004074}
4075
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004076static PyTypeObject *
4077infer_type(expr_ty e)
4078{
4079 switch (e->kind) {
4080 case Tuple_kind:
4081 return &PyTuple_Type;
4082 case List_kind:
4083 case ListComp_kind:
4084 return &PyList_Type;
4085 case Dict_kind:
4086 case DictComp_kind:
4087 return &PyDict_Type;
4088 case Set_kind:
4089 case SetComp_kind:
4090 return &PySet_Type;
4091 case GeneratorExp_kind:
4092 return &PyGen_Type;
4093 case Lambda_kind:
4094 return &PyFunction_Type;
4095 case JoinedStr_kind:
4096 case FormattedValue_kind:
4097 return &PyUnicode_Type;
4098 case Constant_kind:
Victor Stinnera102ed72020-02-07 02:24:48 +01004099 return Py_TYPE(e->v.Constant.value);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004100 default:
4101 return NULL;
4102 }
4103}
4104
4105static int
4106check_caller(struct compiler *c, expr_ty e)
4107{
4108 switch (e->kind) {
4109 case Constant_kind:
4110 case Tuple_kind:
4111 case List_kind:
4112 case ListComp_kind:
4113 case Dict_kind:
4114 case DictComp_kind:
4115 case Set_kind:
4116 case SetComp_kind:
4117 case GeneratorExp_kind:
4118 case JoinedStr_kind:
4119 case FormattedValue_kind:
4120 return compiler_warn(c, "'%.200s' object is not callable; "
4121 "perhaps you missed a comma?",
4122 infer_type(e)->tp_name);
4123 default:
4124 return 1;
4125 }
4126}
4127
4128static int
4129check_subscripter(struct compiler *c, expr_ty e)
4130{
4131 PyObject *v;
4132
4133 switch (e->kind) {
4134 case Constant_kind:
4135 v = e->v.Constant.value;
4136 if (!(v == Py_None || v == Py_Ellipsis ||
4137 PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
4138 PyAnySet_Check(v)))
4139 {
4140 return 1;
4141 }
4142 /* fall through */
4143 case Set_kind:
4144 case SetComp_kind:
4145 case GeneratorExp_kind:
4146 case Lambda_kind:
4147 return compiler_warn(c, "'%.200s' object is not subscriptable; "
4148 "perhaps you missed a comma?",
4149 infer_type(e)->tp_name);
4150 default:
4151 return 1;
4152 }
4153}
4154
4155static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004156check_index(struct compiler *c, expr_ty e, expr_ty s)
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004157{
4158 PyObject *v;
4159
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004160 PyTypeObject *index_type = infer_type(s);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004161 if (index_type == NULL
4162 || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
4163 || index_type == &PySlice_Type) {
4164 return 1;
4165 }
4166
4167 switch (e->kind) {
4168 case Constant_kind:
4169 v = e->v.Constant.value;
4170 if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
4171 return 1;
4172 }
4173 /* fall through */
4174 case Tuple_kind:
4175 case List_kind:
4176 case ListComp_kind:
4177 case JoinedStr_kind:
4178 case FormattedValue_kind:
4179 return compiler_warn(c, "%.200s indices must be integers or slices, "
4180 "not %.200s; "
4181 "perhaps you missed a comma?",
4182 infer_type(e)->tp_name,
4183 index_type->tp_name);
4184 default:
4185 return 1;
4186 }
4187}
4188
Zackery Spytz97f5de02019-03-22 01:30:32 -06004189// Return 1 if the method call was optimized, -1 if not, and 0 on error.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004190static int
Yury Selivanovf2392132016-12-13 19:03:51 -05004191maybe_optimize_method_call(struct compiler *c, expr_ty e)
4192{
4193 Py_ssize_t argsl, i;
4194 expr_ty meth = e->v.Call.func;
Pablo Galindoa5634c42020-09-16 19:42:00 +01004195 asdl_expr_seq *args = e->v.Call.args;
Yury Selivanovf2392132016-12-13 19:03:51 -05004196
4197 /* Check that the call node is an attribute access, and that
4198 the call doesn't have keyword parameters. */
4199 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
Mark Shannon11e0b292021-04-15 14:28:56 +01004200 asdl_seq_LEN(e->v.Call.keywords)) {
Yury Selivanovf2392132016-12-13 19:03:51 -05004201 return -1;
Mark Shannon11e0b292021-04-15 14:28:56 +01004202 }
4203 /* Check that there aren't too many arguments */
Yury Selivanovf2392132016-12-13 19:03:51 -05004204 argsl = asdl_seq_LEN(args);
Mark Shannon11e0b292021-04-15 14:28:56 +01004205 if (argsl >= STACK_USE_GUIDELINE) {
4206 return -1;
4207 }
4208 /* Check that there are no *varargs types of arguments. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004209 for (i = 0; i < argsl; i++) {
4210 expr_ty elt = asdl_seq_GET(args, i);
4211 if (elt->kind == Starred_kind) {
4212 return -1;
4213 }
4214 }
4215
4216 /* Alright, we can optimize the code. */
4217 VISIT(c, expr, meth->v.Attribute.value);
Mark Shannond48848c2021-03-14 18:01:30 +00004218 int old_lineno = c->u->u_lineno;
4219 c->u->u_lineno = meth->end_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004220 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
4221 VISIT_SEQ(c, expr, e->v.Call.args);
4222 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
Mark Shannond48848c2021-03-14 18:01:30 +00004223 c->u->u_lineno = old_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004224 return 1;
4225}
4226
4227static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004228validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
Zackery Spytz08050e92020-04-06 00:47:47 -06004229{
4230 Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
4231 for (Py_ssize_t i = 0; i < nkeywords; i++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004232 keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
4233 if (key->arg == NULL) {
4234 continue;
4235 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +01004236 if (forbidden_name(c, key->arg, Store)) {
4237 return -1;
4238 }
Zackery Spytz08050e92020-04-06 00:47:47 -06004239 for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004240 keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
4241 if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004242 c->u->u_col_offset = other->col_offset;
Brandt Bucher145bf262021-02-26 14:51:55 -08004243 compiler_error(c, "keyword argument repeated: %U", key->arg);
Pablo Galindo254ec782020-04-03 20:37:13 +01004244 return -1;
4245 }
4246 }
4247 }
4248 return 0;
4249}
4250
4251static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004252compiler_call(struct compiler *c, expr_ty e)
4253{
Zackery Spytz97f5de02019-03-22 01:30:32 -06004254 int ret = maybe_optimize_method_call(c, e);
4255 if (ret >= 0) {
4256 return ret;
4257 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004258 if (!check_caller(c, e->v.Call.func)) {
4259 return 0;
4260 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004261 VISIT(c, expr, e->v.Call.func);
4262 return compiler_call_helper(c, 0,
4263 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004264 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004265}
4266
Eric V. Smith235a6f02015-09-19 14:51:32 -04004267static int
4268compiler_joined_str(struct compiler *c, expr_ty e)
4269{
Mark Shannon11e0b292021-04-15 14:28:56 +01004270
4271 Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values);
4272 if (value_count > STACK_USE_GUIDELINE) {
4273 ADDOP_LOAD_CONST_NEW(c, _PyUnicode_FromASCII("", 0));
4274 PyObject *join = _PyUnicode_FromASCII("join", 4);
4275 if (join == NULL) {
4276 return 0;
4277 }
4278 ADDOP_NAME(c, LOAD_METHOD, join, names);
4279 Py_DECREF(join);
4280 ADDOP_I(c, BUILD_LIST, 0);
4281 for (Py_ssize_t i = 0; i < asdl_seq_LEN(e->v.JoinedStr.values); i++) {
4282 VISIT(c, expr, asdl_seq_GET(e->v.JoinedStr.values, i));
4283 ADDOP_I(c, LIST_APPEND, 1);
4284 }
4285 ADDOP_I(c, CALL_METHOD, 1);
4286 }
4287 else {
4288 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
4289 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) {
4290 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
4291 }
4292 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004293 return 1;
4294}
4295
Eric V. Smitha78c7952015-11-03 12:45:05 -05004296/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004297static int
4298compiler_formatted_value(struct compiler *c, expr_ty e)
4299{
Eric V. Smitha78c7952015-11-03 12:45:05 -05004300 /* Our oparg encodes 2 pieces of information: the conversion
4301 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04004302
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004303 Convert the conversion char to 3 bits:
4304 : 000 0x0 FVC_NONE The default if nothing specified.
Eric V. Smitha78c7952015-11-03 12:45:05 -05004305 !s : 001 0x1 FVC_STR
4306 !r : 010 0x2 FVC_REPR
4307 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04004308
Eric V. Smitha78c7952015-11-03 12:45:05 -05004309 next bit is whether or not we have a format spec:
4310 yes : 100 0x4
4311 no : 000 0x0
4312 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004313
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004314 int conversion = e->v.FormattedValue.conversion;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004315 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004316
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004317 /* The expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004318 VISIT(c, expr, e->v.FormattedValue.value);
4319
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004320 switch (conversion) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004321 case 's': oparg = FVC_STR; break;
4322 case 'r': oparg = FVC_REPR; break;
4323 case 'a': oparg = FVC_ASCII; break;
4324 case -1: oparg = FVC_NONE; break;
4325 default:
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004326 PyErr_Format(PyExc_SystemError,
4327 "Unrecognized conversion character %d", conversion);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004328 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004329 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004330 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004331 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004332 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004333 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004334 }
4335
Eric V. Smitha78c7952015-11-03 12:45:05 -05004336 /* And push our opcode and oparg */
4337 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004338
Eric V. Smith235a6f02015-09-19 14:51:32 -04004339 return 1;
4340}
4341
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004342static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004343compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004344{
4345 Py_ssize_t i, n = end - begin;
4346 keyword_ty kw;
4347 PyObject *keys, *key;
4348 assert(n > 0);
Mark Shannon11e0b292021-04-15 14:28:56 +01004349 int big = n*2 > STACK_USE_GUIDELINE;
4350 if (n > 1 && !big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004351 for (i = begin; i < end; i++) {
4352 kw = asdl_seq_GET(keywords, i);
4353 VISIT(c, expr, kw->value);
4354 }
4355 keys = PyTuple_New(n);
4356 if (keys == NULL) {
4357 return 0;
4358 }
4359 for (i = begin; i < end; i++) {
4360 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
4361 Py_INCREF(key);
4362 PyTuple_SET_ITEM(keys, i - begin, key);
4363 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004364 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004365 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
Mark Shannon11e0b292021-04-15 14:28:56 +01004366 return 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004367 }
Mark Shannon11e0b292021-04-15 14:28:56 +01004368 if (big) {
4369 ADDOP_I_NOLINE(c, BUILD_MAP, 0);
4370 }
4371 for (i = begin; i < end; i++) {
4372 kw = asdl_seq_GET(keywords, i);
4373 ADDOP_LOAD_CONST(c, kw->arg);
4374 VISIT(c, expr, kw->value);
4375 if (big) {
4376 ADDOP_I_NOLINE(c, MAP_ADD, 1);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004377 }
Mark Shannon11e0b292021-04-15 14:28:56 +01004378 }
4379 if (!big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004380 ADDOP_I(c, BUILD_MAP, n);
4381 }
4382 return 1;
4383}
4384
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004385/* shared code between compiler_call and compiler_class */
4386static int
4387compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01004388 int n, /* Args already pushed */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004389 asdl_expr_seq *args,
4390 asdl_keyword_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004391{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004392 Py_ssize_t i, nseen, nelts, nkwelts;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004393
Pablo Galindo254ec782020-04-03 20:37:13 +01004394 if (validate_keywords(c, keywords) == -1) {
4395 return 0;
4396 }
4397
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004398 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004399 nkwelts = asdl_seq_LEN(keywords);
4400
Mark Shannon11e0b292021-04-15 14:28:56 +01004401 if (nelts + nkwelts*2 > STACK_USE_GUIDELINE) {
4402 goto ex_call;
4403 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004404 for (i = 0; i < nelts; i++) {
4405 expr_ty elt = asdl_seq_GET(args, i);
4406 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00004407 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004408 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004409 }
4410 for (i = 0; i < nkwelts; i++) {
4411 keyword_ty kw = asdl_seq_GET(keywords, i);
4412 if (kw->arg == NULL) {
4413 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004414 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004415 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004416
Mark Shannon13bc1392020-01-23 09:25:17 +00004417 /* No * or ** args, so can use faster calling sequence */
4418 for (i = 0; i < nelts; i++) {
4419 expr_ty elt = asdl_seq_GET(args, i);
4420 assert(elt->kind != Starred_kind);
4421 VISIT(c, expr, elt);
4422 }
4423 if (nkwelts) {
4424 PyObject *names;
4425 VISIT_SEQ(c, keyword, keywords);
4426 names = PyTuple_New(nkwelts);
4427 if (names == NULL) {
4428 return 0;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004429 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004430 for (i = 0; i < nkwelts; i++) {
4431 keyword_ty kw = asdl_seq_GET(keywords, i);
4432 Py_INCREF(kw->arg);
4433 PyTuple_SET_ITEM(names, i, kw->arg);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004434 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004435 ADDOP_LOAD_CONST_NEW(c, names);
4436 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
4437 return 1;
4438 }
4439 else {
4440 ADDOP_I(c, CALL_FUNCTION, n + nelts);
4441 return 1;
4442 }
4443
4444ex_call:
4445
4446 /* Do positional arguments. */
4447 if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
4448 VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
4449 }
4450 else if (starunpack_helper(c, args, n, BUILD_LIST,
4451 LIST_APPEND, LIST_EXTEND, 1) == 0) {
4452 return 0;
4453 }
4454 /* Then keyword arguments */
4455 if (nkwelts) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004456 /* Has a new dict been pushed */
4457 int have_dict = 0;
Mark Shannon13bc1392020-01-23 09:25:17 +00004458
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004459 nseen = 0; /* the number of keyword arguments on the stack following */
4460 for (i = 0; i < nkwelts; i++) {
4461 keyword_ty kw = asdl_seq_GET(keywords, i);
4462 if (kw->arg == NULL) {
4463 /* A keyword argument unpacking. */
4464 if (nseen) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004465 if (!compiler_subkwargs(c, keywords, i - nseen, i)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004466 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004467 }
Mark Shannondb64f122020-06-01 10:42:42 +01004468 if (have_dict) {
4469 ADDOP_I(c, DICT_MERGE, 1);
4470 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004471 have_dict = 1;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004472 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004473 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004474 if (!have_dict) {
4475 ADDOP_I(c, BUILD_MAP, 0);
4476 have_dict = 1;
4477 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004478 VISIT(c, expr, kw->value);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004479 ADDOP_I(c, DICT_MERGE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004480 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004481 else {
4482 nseen++;
4483 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004484 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004485 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004486 /* Pack up any trailing keyword arguments. */
Mark Shannon8a4cd702020-01-27 09:57:45 +00004487 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004488 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004489 }
4490 if (have_dict) {
4491 ADDOP_I(c, DICT_MERGE, 1);
4492 }
4493 have_dict = 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004494 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004495 assert(have_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004496 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004497 ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0);
4498 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004499}
4500
Nick Coghlan650f0d02007-04-15 12:05:43 +00004501
4502/* List and set comprehensions and generator expressions work by creating a
4503 nested function to perform the actual iteration. This means that the
4504 iteration variables don't leak into the current scope.
4505 The defined function is called immediately following its definition, with the
4506 result of that call being the result of the expression.
4507 The LC/SC version returns the populated container, while the GE version is
4508 flagged in symtable.c as a generator, so it returns the generator object
4509 when the function is called.
Nick Coghlan650f0d02007-04-15 12:05:43 +00004510
4511 Possible cleanups:
4512 - iterate over the generator sequence instead of using recursion
4513*/
4514
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004515
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004516static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004517compiler_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004518 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004519 int depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004520 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004521{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004522 comprehension_ty gen;
4523 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4524 if (gen->is_async) {
4525 return compiler_async_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004526 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004527 } else {
4528 return compiler_sync_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004529 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004530 }
4531}
4532
4533static int
4534compiler_sync_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004535 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004536 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004537 expr_ty elt, expr_ty val, int type)
4538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004539 /* generate code for the iterator, then each of the ifs,
4540 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004542 comprehension_ty gen;
4543 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004544 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004546 start = compiler_new_block(c);
4547 skip = compiler_new_block(c);
4548 if_cleanup = compiler_new_block(c);
4549 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004551 if (start == NULL || skip == NULL || if_cleanup == NULL ||
4552 anchor == NULL)
4553 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004555 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004557 if (gen_index == 0) {
4558 /* Receive outermost iter as an implicit argument */
4559 c->u->u_argcount = 1;
4560 ADDOP_I(c, LOAD_FAST, 0);
4561 }
4562 else {
4563 /* Sub-iter - calculate on the fly */
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004564 /* Fast path for the temporary variable assignment idiom:
4565 for y in [f(x)]
4566 */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004567 asdl_expr_seq *elts;
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004568 switch (gen->iter->kind) {
4569 case List_kind:
4570 elts = gen->iter->v.List.elts;
4571 break;
4572 case Tuple_kind:
4573 elts = gen->iter->v.Tuple.elts;
4574 break;
4575 default:
4576 elts = NULL;
4577 }
4578 if (asdl_seq_LEN(elts) == 1) {
4579 expr_ty elt = asdl_seq_GET(elts, 0);
4580 if (elt->kind != Starred_kind) {
4581 VISIT(c, expr, elt);
4582 start = NULL;
4583 }
4584 }
4585 if (start) {
4586 VISIT(c, expr, gen->iter);
4587 ADDOP(c, GET_ITER);
4588 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004589 }
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004590 if (start) {
4591 depth++;
4592 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01004593 ADDOP_JUMP(c, FOR_ITER, anchor);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004594 NEXT_BLOCK(c);
4595 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004596 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004598 /* XXX this needs to be cleaned up...a lot! */
4599 n = asdl_seq_LEN(gen->ifs);
4600 for (i = 0; i < n; i++) {
4601 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004602 if (!compiler_jump_if(c, e, if_cleanup, 0))
4603 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004604 NEXT_BLOCK(c);
4605 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004607 if (++gen_index < asdl_seq_LEN(generators))
4608 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004609 generators, gen_index, depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004610 elt, val, type))
4611 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004613 /* only append after the last for generator */
4614 if (gen_index >= asdl_seq_LEN(generators)) {
4615 /* comprehension specific code */
4616 switch (type) {
4617 case COMP_GENEXP:
4618 VISIT(c, expr, elt);
4619 ADDOP(c, YIELD_VALUE);
4620 ADDOP(c, POP_TOP);
4621 break;
4622 case COMP_LISTCOMP:
4623 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004624 ADDOP_I(c, LIST_APPEND, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004625 break;
4626 case COMP_SETCOMP:
4627 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004628 ADDOP_I(c, SET_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004629 break;
4630 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004631 /* With '{k: v}', k is evaluated before v, so we do
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004632 the same. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004633 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004634 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004635 ADDOP_I(c, MAP_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004636 break;
4637 default:
4638 return 0;
4639 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004641 compiler_use_next_block(c, skip);
4642 }
4643 compiler_use_next_block(c, if_cleanup);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004644 if (start) {
Mark Shannon582aaf12020-08-04 17:30:11 +01004645 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004646 compiler_use_next_block(c, anchor);
4647 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004648
4649 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004650}
4651
4652static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004653compiler_async_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004654 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004655 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004656 expr_ty elt, expr_ty val, int type)
4657{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004658 comprehension_ty gen;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004659 basicblock *start, *if_cleanup, *except;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004660 Py_ssize_t i, n;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004661 start = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004662 except = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004663 if_cleanup = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004664
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004665 if (start == NULL || if_cleanup == NULL || except == NULL) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004666 return 0;
4667 }
4668
4669 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4670
4671 if (gen_index == 0) {
4672 /* Receive outermost iter as an implicit argument */
4673 c->u->u_argcount = 1;
4674 ADDOP_I(c, LOAD_FAST, 0);
4675 }
4676 else {
4677 /* Sub-iter - calculate on the fly */
4678 VISIT(c, expr, gen->iter);
4679 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004680 }
4681
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004682 compiler_use_next_block(c, start);
tomKPZ7a7ba3d2021-04-07 07:43:45 -07004683 /* Runtime will push a block here, so we need to account for that */
4684 if (!compiler_push_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start,
4685 NULL, NULL)) {
4686 return 0;
4687 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004688
Mark Shannon582aaf12020-08-04 17:30:11 +01004689 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004690 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004691 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004692 ADDOP(c, YIELD_FROM);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004693 ADDOP(c, POP_BLOCK);
Serhiy Storchaka24d32012018-03-10 18:22:34 +02004694 VISIT(c, expr, gen->target);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004695
4696 n = asdl_seq_LEN(gen->ifs);
4697 for (i = 0; i < n; i++) {
4698 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004699 if (!compiler_jump_if(c, e, if_cleanup, 0))
4700 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004701 NEXT_BLOCK(c);
4702 }
4703
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004704 depth++;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004705 if (++gen_index < asdl_seq_LEN(generators))
4706 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004707 generators, gen_index, depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004708 elt, val, type))
4709 return 0;
4710
4711 /* only append after the last for generator */
4712 if (gen_index >= asdl_seq_LEN(generators)) {
4713 /* comprehension specific code */
4714 switch (type) {
4715 case COMP_GENEXP:
4716 VISIT(c, expr, elt);
4717 ADDOP(c, YIELD_VALUE);
4718 ADDOP(c, POP_TOP);
4719 break;
4720 case COMP_LISTCOMP:
4721 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004722 ADDOP_I(c, LIST_APPEND, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004723 break;
4724 case COMP_SETCOMP:
4725 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004726 ADDOP_I(c, SET_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004727 break;
4728 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004729 /* With '{k: v}', k is evaluated before v, so we do
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004730 the same. */
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004731 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004732 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004733 ADDOP_I(c, MAP_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004734 break;
4735 default:
4736 return 0;
4737 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004738 }
4739 compiler_use_next_block(c, if_cleanup);
Mark Shannon582aaf12020-08-04 17:30:11 +01004740 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004741
tomKPZ7a7ba3d2021-04-07 07:43:45 -07004742 compiler_pop_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start);
4743
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004744 compiler_use_next_block(c, except);
4745 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004746
4747 return 1;
4748}
4749
4750static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004751compiler_comprehension(struct compiler *c, expr_ty e, int type,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004752 identifier name, asdl_comprehension_seq *generators, expr_ty elt,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004753 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004754{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004755 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004756 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004757 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004758 int is_async_generator = 0;
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004759 int top_level_await = IS_TOP_LEVEL_AWAIT(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004760
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004761
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004762 int is_async_function = c->u->u_ste->ste_coroutine;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004763
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004764 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004765 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4766 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004767 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004768 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004769 }
4770
4771 is_async_generator = c->u->u_ste->ste_coroutine;
4772
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004773 if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004774 compiler_error(c, "asynchronous comprehension outside of "
4775 "an asynchronous function");
4776 goto error_in_scope;
4777 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004779 if (type != COMP_GENEXP) {
4780 int op;
4781 switch (type) {
4782 case COMP_LISTCOMP:
4783 op = BUILD_LIST;
4784 break;
4785 case COMP_SETCOMP:
4786 op = BUILD_SET;
4787 break;
4788 case COMP_DICTCOMP:
4789 op = BUILD_MAP;
4790 break;
4791 default:
4792 PyErr_Format(PyExc_SystemError,
4793 "unknown comprehension type %d", type);
4794 goto error_in_scope;
4795 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004797 ADDOP_I(c, op, 0);
4798 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004799
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004800 if (!compiler_comprehension_generator(c, generators, 0, 0, elt,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004801 val, type))
4802 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004804 if (type != COMP_GENEXP) {
4805 ADDOP(c, RETURN_VALUE);
4806 }
4807
4808 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004809 qualname = c->u->u_qualname;
4810 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004811 compiler_exit_scope(c);
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004812 if (top_level_await && is_async_generator){
4813 c->u->u_ste->ste_coroutine = 1;
4814 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004815 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004816 goto error;
4817
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004818 if (!compiler_make_closure(c, co, 0, qualname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004819 goto error;
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004820 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004821 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004822 Py_DECREF(co);
4823
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004824 VISIT(c, expr, outermost->iter);
4825
4826 if (outermost->is_async) {
4827 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004828 } else {
4829 ADDOP(c, GET_ITER);
4830 }
4831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004832 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004833
4834 if (is_async_generator && type != COMP_GENEXP) {
4835 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004836 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004837 ADDOP(c, YIELD_FROM);
4838 }
4839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004840 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004841error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004842 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004843error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004844 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004845 Py_XDECREF(co);
4846 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004847}
4848
4849static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004850compiler_genexp(struct compiler *c, expr_ty e)
4851{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004852 static identifier name;
4853 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004854 name = PyUnicode_InternFromString("<genexpr>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004855 if (!name)
4856 return 0;
4857 }
4858 assert(e->kind == GeneratorExp_kind);
4859 return compiler_comprehension(c, e, COMP_GENEXP, name,
4860 e->v.GeneratorExp.generators,
4861 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004862}
4863
4864static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004865compiler_listcomp(struct compiler *c, expr_ty e)
4866{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004867 static identifier name;
4868 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004869 name = PyUnicode_InternFromString("<listcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004870 if (!name)
4871 return 0;
4872 }
4873 assert(e->kind == ListComp_kind);
4874 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4875 e->v.ListComp.generators,
4876 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004877}
4878
4879static int
4880compiler_setcomp(struct compiler *c, expr_ty e)
4881{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004882 static identifier name;
4883 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004884 name = PyUnicode_InternFromString("<setcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004885 if (!name)
4886 return 0;
4887 }
4888 assert(e->kind == SetComp_kind);
4889 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4890 e->v.SetComp.generators,
4891 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004892}
4893
4894
4895static int
4896compiler_dictcomp(struct compiler *c, expr_ty e)
4897{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004898 static identifier name;
4899 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004900 name = PyUnicode_InternFromString("<dictcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004901 if (!name)
4902 return 0;
4903 }
4904 assert(e->kind == DictComp_kind);
4905 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4906 e->v.DictComp.generators,
4907 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004908}
4909
4910
4911static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004912compiler_visit_keyword(struct compiler *c, keyword_ty k)
4913{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004914 VISIT(c, expr, k->value);
4915 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004916}
4917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004918/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004919 whether they are true or false.
4920
4921 Return values: 1 for true, 0 for false, -1 for non-constant.
4922 */
4923
4924static int
Mark Shannonfee55262019-11-21 09:11:43 +00004925compiler_with_except_finish(struct compiler *c) {
4926 basicblock *exit;
4927 exit = compiler_new_block(c);
4928 if (exit == NULL)
4929 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01004930 ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit);
Mark Shannon266b4622020-11-17 19:30:14 +00004931 NEXT_BLOCK(c);
Mark Shannonbf353f32020-12-17 13:55:28 +00004932 ADDOP_I(c, RERAISE, 1);
Mark Shannonfee55262019-11-21 09:11:43 +00004933 compiler_use_next_block(c, exit);
4934 ADDOP(c, POP_TOP);
4935 ADDOP(c, POP_TOP);
4936 ADDOP(c, POP_TOP);
4937 ADDOP(c, POP_EXCEPT);
4938 ADDOP(c, POP_TOP);
4939 return 1;
4940}
Yury Selivanov75445082015-05-11 22:57:16 -04004941
4942/*
4943 Implements the async with statement.
4944
4945 The semantics outlined in that PEP are as follows:
4946
4947 async with EXPR as VAR:
4948 BLOCK
4949
4950 It is implemented roughly as:
4951
4952 context = EXPR
4953 exit = context.__aexit__ # not calling it
4954 value = await context.__aenter__()
4955 try:
4956 VAR = value # if VAR present in the syntax
4957 BLOCK
4958 finally:
4959 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004960 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04004961 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004962 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04004963 if not (await exit(*exc)):
4964 raise
4965 */
4966static int
4967compiler_async_with(struct compiler *c, stmt_ty s, int pos)
4968{
Mark Shannonfee55262019-11-21 09:11:43 +00004969 basicblock *block, *final, *exit;
Yury Selivanov75445082015-05-11 22:57:16 -04004970 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
4971
4972 assert(s->kind == AsyncWith_kind);
Pablo Galindo90235812020-03-15 04:29:22 +00004973 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07004974 c->u->u_ste->ste_coroutine = 1;
4975 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
Zsolt Dollensteine2396502018-04-27 08:58:56 -07004976 return compiler_error(c, "'async with' outside async function");
4977 }
Yury Selivanov75445082015-05-11 22:57:16 -04004978
4979 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004980 final = compiler_new_block(c);
4981 exit = compiler_new_block(c);
4982 if (!block || !final || !exit)
Yury Selivanov75445082015-05-11 22:57:16 -04004983 return 0;
4984
4985 /* Evaluate EXPR */
4986 VISIT(c, expr, item->context_expr);
4987
4988 ADDOP(c, BEFORE_ASYNC_WITH);
4989 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004990 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004991 ADDOP(c, YIELD_FROM);
4992
Mark Shannon582aaf12020-08-04 17:30:11 +01004993 ADDOP_JUMP(c, SETUP_ASYNC_WITH, final);
Yury Selivanov75445082015-05-11 22:57:16 -04004994
4995 /* SETUP_ASYNC_WITH pushes a finally block. */
4996 compiler_use_next_block(c, block);
Mark Shannon5979e812021-04-30 14:32:47 +01004997 if (!compiler_push_fblock(c, ASYNC_WITH, block, final, s)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004998 return 0;
4999 }
5000
5001 if (item->optional_vars) {
5002 VISIT(c, expr, item->optional_vars);
5003 }
5004 else {
5005 /* Discard result from context.__aenter__() */
5006 ADDOP(c, POP_TOP);
5007 }
5008
5009 pos++;
5010 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
5011 /* BLOCK code */
5012 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
5013 else if (!compiler_async_with(c, s, pos))
5014 return 0;
5015
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005016 compiler_pop_fblock(c, ASYNC_WITH, block);
Mark Shannonfee55262019-11-21 09:11:43 +00005017 ADDOP(c, POP_BLOCK);
5018 /* End of body; start the cleanup */
Yury Selivanov75445082015-05-11 22:57:16 -04005019
Mark Shannonfee55262019-11-21 09:11:43 +00005020 /* For successful outcome:
5021 * call __exit__(None, None, None)
5022 */
Mark Shannon5979e812021-04-30 14:32:47 +01005023 SET_LOC(c, s);
Mark Shannonfee55262019-11-21 09:11:43 +00005024 if(!compiler_call_exit_with_nones(c))
Yury Selivanov75445082015-05-11 22:57:16 -04005025 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00005026 ADDOP(c, GET_AWAITABLE);
5027 ADDOP_O(c, LOAD_CONST, Py_None, consts);
5028 ADDOP(c, YIELD_FROM);
Yury Selivanov75445082015-05-11 22:57:16 -04005029
Mark Shannonfee55262019-11-21 09:11:43 +00005030 ADDOP(c, POP_TOP);
Yury Selivanov75445082015-05-11 22:57:16 -04005031
Mark Shannon582aaf12020-08-04 17:30:11 +01005032 ADDOP_JUMP(c, JUMP_ABSOLUTE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00005033
5034 /* For exceptional outcome: */
5035 compiler_use_next_block(c, final);
Mark Shannonfee55262019-11-21 09:11:43 +00005036 ADDOP(c, WITH_EXCEPT_START);
Yury Selivanov75445082015-05-11 22:57:16 -04005037 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005038 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005039 ADDOP(c, YIELD_FROM);
Mark Shannonfee55262019-11-21 09:11:43 +00005040 compiler_with_except_finish(c);
Yury Selivanov75445082015-05-11 22:57:16 -04005041
Mark Shannonfee55262019-11-21 09:11:43 +00005042compiler_use_next_block(c, exit);
Yury Selivanov75445082015-05-11 22:57:16 -04005043 return 1;
5044}
5045
5046
Guido van Rossumc2e20742006-02-27 22:32:47 +00005047/*
5048 Implements the with statement from PEP 343.
Guido van Rossumc2e20742006-02-27 22:32:47 +00005049 with EXPR as VAR:
5050 BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00005051 is implemented as:
5052 <code for EXPR>
5053 SETUP_WITH E
5054 <code to store to VAR> or POP_TOP
5055 <code for BLOCK>
5056 LOAD_CONST (None, None, None)
5057 CALL_FUNCTION_EX 0
5058 JUMP_FORWARD EXIT
5059 E: WITH_EXCEPT_START (calls EXPR.__exit__)
5060 POP_JUMP_IF_TRUE T:
5061 RERAISE
5062 T: POP_TOP * 3 (remove exception from stack)
5063 POP_EXCEPT
5064 POP_TOP
5065 EXIT:
Guido van Rossumc2e20742006-02-27 22:32:47 +00005066 */
Mark Shannonfee55262019-11-21 09:11:43 +00005067
Guido van Rossumc2e20742006-02-27 22:32:47 +00005068static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005069compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00005070{
Mark Shannonfee55262019-11-21 09:11:43 +00005071 basicblock *block, *final, *exit;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005072 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005073
5074 assert(s->kind == With_kind);
5075
Guido van Rossumc2e20742006-02-27 22:32:47 +00005076 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00005077 final = compiler_new_block(c);
5078 exit = compiler_new_block(c);
5079 if (!block || !final || !exit)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005080 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005081
Thomas Wouters477c8d52006-05-27 19:21:47 +00005082 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005083 VISIT(c, expr, item->context_expr);
Mark Shannonfee55262019-11-21 09:11:43 +00005084 /* Will push bound __exit__ */
Mark Shannon582aaf12020-08-04 17:30:11 +01005085 ADDOP_JUMP(c, SETUP_WITH, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005086
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005087 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00005088 compiler_use_next_block(c, block);
Mark Shannon5979e812021-04-30 14:32:47 +01005089 if (!compiler_push_fblock(c, WITH, block, final, s)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005090 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005091 }
5092
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005093 if (item->optional_vars) {
5094 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005095 }
5096 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005097 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005098 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005099 }
5100
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005101 pos++;
5102 if (pos == asdl_seq_LEN(s->v.With.items))
5103 /* BLOCK code */
5104 VISIT_SEQ(c, stmt, s->v.With.body)
5105 else if (!compiler_with(c, s, pos))
5106 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005107
Mark Shannon3bd60352021-01-13 12:05:43 +00005108
5109 /* Mark all following code as artificial */
5110 c->u->u_lineno = -1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005111 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005112 compiler_pop_fblock(c, WITH, block);
Mark Shannon13bc1392020-01-23 09:25:17 +00005113
Mark Shannonfee55262019-11-21 09:11:43 +00005114 /* End of body; start the cleanup. */
Mark Shannon13bc1392020-01-23 09:25:17 +00005115
Mark Shannonfee55262019-11-21 09:11:43 +00005116 /* For successful outcome:
5117 * call __exit__(None, None, None)
5118 */
Mark Shannon5979e812021-04-30 14:32:47 +01005119 SET_LOC(c, s);
Mark Shannonfee55262019-11-21 09:11:43 +00005120 if (!compiler_call_exit_with_nones(c))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005121 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00005122 ADDOP(c, POP_TOP);
Mark Shannon582aaf12020-08-04 17:30:11 +01005123 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005124
Mark Shannonfee55262019-11-21 09:11:43 +00005125 /* For exceptional outcome: */
5126 compiler_use_next_block(c, final);
Mark Shannonfee55262019-11-21 09:11:43 +00005127 ADDOP(c, WITH_EXCEPT_START);
5128 compiler_with_except_finish(c);
5129
5130 compiler_use_next_block(c, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005131 return 1;
5132}
5133
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005134static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005135compiler_visit_expr1(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005137 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07005138 case NamedExpr_kind:
5139 VISIT(c, expr, e->v.NamedExpr.value);
5140 ADDOP(c, DUP_TOP);
5141 VISIT(c, expr, e->v.NamedExpr.target);
5142 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005143 case BoolOp_kind:
5144 return compiler_boolop(c, e);
5145 case BinOp_kind:
5146 VISIT(c, expr, e->v.BinOp.left);
5147 VISIT(c, expr, e->v.BinOp.right);
Andy Lester76d58772020-03-10 21:18:12 -05005148 ADDOP(c, binop(e->v.BinOp.op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005149 break;
5150 case UnaryOp_kind:
5151 VISIT(c, expr, e->v.UnaryOp.operand);
5152 ADDOP(c, unaryop(e->v.UnaryOp.op));
5153 break;
5154 case Lambda_kind:
5155 return compiler_lambda(c, e);
5156 case IfExp_kind:
5157 return compiler_ifexp(c, e);
5158 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005159 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005160 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005161 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005162 case GeneratorExp_kind:
5163 return compiler_genexp(c, e);
5164 case ListComp_kind:
5165 return compiler_listcomp(c, e);
5166 case SetComp_kind:
5167 return compiler_setcomp(c, e);
5168 case DictComp_kind:
5169 return compiler_dictcomp(c, e);
5170 case Yield_kind:
5171 if (c->u->u_ste->ste_type != FunctionBlock)
5172 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005173 if (e->v.Yield.value) {
5174 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005175 }
5176 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005177 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005178 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005179 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005180 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005181 case YieldFrom_kind:
5182 if (c->u->u_ste->ste_type != FunctionBlock)
5183 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04005184
5185 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
5186 return compiler_error(c, "'yield from' inside async function");
5187
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005188 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04005189 ADDOP(c, GET_YIELD_FROM_ITER);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005190 ADDOP_LOAD_CONST(c, Py_None);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005191 ADDOP(c, YIELD_FROM);
5192 break;
Yury Selivanov75445082015-05-11 22:57:16 -04005193 case Await_kind:
Pablo Galindo90235812020-03-15 04:29:22 +00005194 if (!IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005195 if (c->u->u_ste->ste_type != FunctionBlock){
5196 return compiler_error(c, "'await' outside function");
5197 }
Yury Selivanov75445082015-05-11 22:57:16 -04005198
Victor Stinner331a6a52019-05-27 16:39:22 +02005199 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005200 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
5201 return compiler_error(c, "'await' outside async function");
5202 }
5203 }
Yury Selivanov75445082015-05-11 22:57:16 -04005204
5205 VISIT(c, expr, e->v.Await.value);
5206 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005207 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005208 ADDOP(c, YIELD_FROM);
5209 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005210 case Compare_kind:
5211 return compiler_compare(c, e);
5212 case Call_kind:
5213 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005214 case Constant_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005215 ADDOP_LOAD_CONST(c, e->v.Constant.value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005216 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005217 case JoinedStr_kind:
5218 return compiler_joined_str(c, e);
5219 case FormattedValue_kind:
5220 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005221 /* The following exprs can be assignment targets. */
5222 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005223 VISIT(c, expr, e->v.Attribute.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005224 switch (e->v.Attribute.ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005225 case Load:
Mark Shannond48848c2021-03-14 18:01:30 +00005226 {
5227 int old_lineno = c->u->u_lineno;
5228 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005229 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005230 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005231 break;
Mark Shannond48848c2021-03-14 18:01:30 +00005232 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005233 case Store:
Mark Shannond48848c2021-03-14 18:01:30 +00005234 if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005235 return 0;
Mark Shannond48848c2021-03-14 18:01:30 +00005236 }
5237 int old_lineno = c->u->u_lineno;
5238 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005239 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005240 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005241 break;
5242 case Del:
5243 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
5244 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005245 }
5246 break;
5247 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005248 return compiler_subscript(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005249 case Starred_kind:
5250 switch (e->v.Starred.ctx) {
5251 case Store:
5252 /* In all legitimate cases, the Starred node was already replaced
5253 * by compiler_list/compiler_tuple. XXX: is that okay? */
5254 return compiler_error(c,
5255 "starred assignment target must be in a list or tuple");
5256 default:
5257 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005258 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005259 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005260 break;
5261 case Slice_kind:
5262 return compiler_slice(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005263 case Name_kind:
5264 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
5265 /* child nodes of List and Tuple will have expr_context set */
5266 case List_kind:
5267 return compiler_list(c, e);
5268 case Tuple_kind:
5269 return compiler_tuple(c, e);
5270 }
5271 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005272}
5273
5274static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005275compiler_visit_expr(struct compiler *c, expr_ty e)
5276{
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005277 int old_lineno = c->u->u_lineno;
5278 int old_col_offset = c->u->u_col_offset;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005279 SET_LOC(c, e);
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005280 int res = compiler_visit_expr1(c, e);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005281 c->u->u_lineno = old_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005282 c->u->u_col_offset = old_col_offset;
5283 return res;
5284}
5285
5286static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005287compiler_augassign(struct compiler *c, stmt_ty s)
5288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005289 assert(s->kind == AugAssign_kind);
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005290 expr_ty e = s->v.AugAssign.target;
5291
5292 int old_lineno = c->u->u_lineno;
5293 int old_col_offset = c->u->u_col_offset;
5294 SET_LOC(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005296 switch (e->kind) {
5297 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005298 VISIT(c, expr, e->v.Attribute.value);
5299 ADDOP(c, DUP_TOP);
Mark Shannond48848c2021-03-14 18:01:30 +00005300 int old_lineno = c->u->u_lineno;
5301 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005302 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005303 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005304 break;
5305 case Subscript_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005306 VISIT(c, expr, e->v.Subscript.value);
5307 VISIT(c, expr, e->v.Subscript.slice);
5308 ADDOP(c, DUP_TOP_TWO);
5309 ADDOP(c, BINARY_SUBSCR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005310 break;
5311 case Name_kind:
5312 if (!compiler_nameop(c, e->v.Name.id, Load))
5313 return 0;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005314 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005315 default:
5316 PyErr_Format(PyExc_SystemError,
5317 "invalid node type (%d) for augmented assignment",
5318 e->kind);
5319 return 0;
5320 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005321
5322 c->u->u_lineno = old_lineno;
5323 c->u->u_col_offset = old_col_offset;
5324
5325 VISIT(c, expr, s->v.AugAssign.value);
5326 ADDOP(c, inplace_binop(s->v.AugAssign.op));
5327
5328 SET_LOC(c, e);
5329
5330 switch (e->kind) {
5331 case Attribute_kind:
Mark Shannond48848c2021-03-14 18:01:30 +00005332 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005333 ADDOP(c, ROT_TWO);
5334 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5335 break;
5336 case Subscript_kind:
5337 ADDOP(c, ROT_THREE);
5338 ADDOP(c, STORE_SUBSCR);
5339 break;
5340 case Name_kind:
5341 return compiler_nameop(c, e->v.Name.id, Store);
5342 default:
5343 Py_UNREACHABLE();
5344 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005345 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005346}
5347
5348static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005349check_ann_expr(struct compiler *c, expr_ty e)
5350{
5351 VISIT(c, expr, e);
5352 ADDOP(c, POP_TOP);
5353 return 1;
5354}
5355
5356static int
5357check_annotation(struct compiler *c, stmt_ty s)
5358{
Batuhan Taskaya8cc3cfa2021-04-25 05:31:20 +03005359 /* Annotations of complex targets does not produce anything
5360 under annotations future */
5361 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
5362 return 1;
5363 }
5364
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005365 /* Annotations are only evaluated in a module or class. */
5366 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5367 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
5368 return check_ann_expr(c, s->v.AnnAssign.annotation);
5369 }
5370 return 1;
5371}
5372
5373static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005374check_ann_subscr(struct compiler *c, expr_ty e)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005375{
5376 /* We check that everything in a subscript is defined at runtime. */
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005377 switch (e->kind) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005378 case Slice_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005379 if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005380 return 0;
5381 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005382 if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) {
5383 return 0;
5384 }
5385 if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) {
5386 return 0;
5387 }
5388 return 1;
5389 case Tuple_kind: {
5390 /* extended slice */
Pablo Galindoa5634c42020-09-16 19:42:00 +01005391 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005392 Py_ssize_t i, n = asdl_seq_LEN(elts);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005393 for (i = 0; i < n; i++) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005394 if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005395 return 0;
5396 }
5397 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005398 return 1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005399 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005400 default:
5401 return check_ann_expr(c, e);
5402 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005403}
5404
5405static int
5406compiler_annassign(struct compiler *c, stmt_ty s)
5407{
5408 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07005409 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005410
5411 assert(s->kind == AnnAssign_kind);
5412
5413 /* We perform the actual assignment first. */
5414 if (s->v.AnnAssign.value) {
5415 VISIT(c, expr, s->v.AnnAssign.value);
5416 VISIT(c, expr, targ);
5417 }
5418 switch (targ->kind) {
5419 case Name_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005420 if (forbidden_name(c, targ->v.Name.id, Store))
5421 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005422 /* If we have a simple name in a module or class, store annotation. */
5423 if (s->v.AnnAssign.simple &&
5424 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5425 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Pablo Galindob0544ba2021-04-21 12:41:19 +01005426 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
5427 VISIT(c, annexpr, s->v.AnnAssign.annotation)
5428 }
5429 else {
5430 VISIT(c, expr, s->v.AnnAssign.annotation);
5431 }
Mark Shannon332cd5e2018-01-30 00:41:04 +00005432 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02005433 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005434 ADDOP_LOAD_CONST_NEW(c, mangled);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005435 ADDOP(c, STORE_SUBSCR);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005436 }
5437 break;
5438 case Attribute_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005439 if (forbidden_name(c, targ->v.Attribute.attr, Store))
5440 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005441 if (!s->v.AnnAssign.value &&
5442 !check_ann_expr(c, targ->v.Attribute.value)) {
5443 return 0;
5444 }
5445 break;
5446 case Subscript_kind:
5447 if (!s->v.AnnAssign.value &&
5448 (!check_ann_expr(c, targ->v.Subscript.value) ||
5449 !check_ann_subscr(c, targ->v.Subscript.slice))) {
5450 return 0;
5451 }
5452 break;
5453 default:
5454 PyErr_Format(PyExc_SystemError,
5455 "invalid node type (%d) for annotated assignment",
5456 targ->kind);
5457 return 0;
5458 }
5459 /* Annotation is evaluated last. */
5460 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
5461 return 0;
5462 }
5463 return 1;
5464}
5465
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005466/* Raises a SyntaxError and returns 0.
5467 If something goes wrong, a different exception may be raised.
5468*/
5469
5470static int
Brandt Bucher145bf262021-02-26 14:51:55 -08005471compiler_error(struct compiler *c, const char *format, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005472{
Brandt Bucher145bf262021-02-26 14:51:55 -08005473 va_list vargs;
5474#ifdef HAVE_STDARG_PROTOTYPES
5475 va_start(vargs, format);
5476#else
5477 va_start(vargs);
5478#endif
5479 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5480 va_end(vargs);
5481 if (msg == NULL) {
5482 return 0;
5483 }
5484 PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
5485 if (loc == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005486 Py_INCREF(Py_None);
5487 loc = Py_None;
5488 }
Pablo Galindoa77aac42021-04-23 14:27:05 +01005489 PyObject *args = Py_BuildValue("O(OiiOii)", msg, c->c_filename,
5490 c->u->u_lineno, c->u->u_col_offset + 1, loc,
5491 c->u->u_end_lineno, c->u->u_end_col_offset + 1);
Brandt Bucher145bf262021-02-26 14:51:55 -08005492 Py_DECREF(msg);
5493 if (args == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005494 goto exit;
Brandt Bucher145bf262021-02-26 14:51:55 -08005495 }
5496 PyErr_SetObject(PyExc_SyntaxError, args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005497 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005498 Py_DECREF(loc);
Brandt Bucher145bf262021-02-26 14:51:55 -08005499 Py_XDECREF(args);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005500 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005501}
5502
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005503/* Emits a SyntaxWarning and returns 1 on success.
5504 If a SyntaxWarning raised as error, replaces it with a SyntaxError
5505 and returns 0.
5506*/
5507static int
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005508compiler_warn(struct compiler *c, const char *format, ...)
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005509{
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005510 va_list vargs;
5511#ifdef HAVE_STDARG_PROTOTYPES
5512 va_start(vargs, format);
5513#else
5514 va_start(vargs);
5515#endif
5516 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5517 va_end(vargs);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005518 if (msg == NULL) {
5519 return 0;
5520 }
5521 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
5522 c->u->u_lineno, NULL, NULL) < 0)
5523 {
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005524 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005525 /* Replace the SyntaxWarning exception with a SyntaxError
5526 to get a more accurate error report */
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005527 PyErr_Clear();
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005528 assert(PyUnicode_AsUTF8(msg) != NULL);
5529 compiler_error(c, PyUnicode_AsUTF8(msg));
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005530 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005531 Py_DECREF(msg);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005532 return 0;
5533 }
5534 Py_DECREF(msg);
5535 return 1;
5536}
5537
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005538static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005539compiler_subscript(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005540{
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005541 expr_context_ty ctx = e->v.Subscript.ctx;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005542 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005543
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005544 if (ctx == Load) {
5545 if (!check_subscripter(c, e->v.Subscript.value)) {
5546 return 0;
5547 }
5548 if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
5549 return 0;
5550 }
5551 }
5552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005553 switch (ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005554 case Load: op = BINARY_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005555 case Store: op = STORE_SUBSCR; break;
5556 case Del: op = DELETE_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005557 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005558 assert(op);
5559 VISIT(c, expr, e->v.Subscript.value);
5560 VISIT(c, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005561 ADDOP(c, op);
5562 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005563}
5564
5565static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005566compiler_slice(struct compiler *c, expr_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005568 int n = 2;
5569 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005571 /* only handles the cases where BUILD_SLICE is emitted */
5572 if (s->v.Slice.lower) {
5573 VISIT(c, expr, s->v.Slice.lower);
5574 }
5575 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005576 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005577 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005579 if (s->v.Slice.upper) {
5580 VISIT(c, expr, s->v.Slice.upper);
5581 }
5582 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005583 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005584 }
5585
5586 if (s->v.Slice.step) {
5587 n++;
5588 VISIT(c, expr, s->v.Slice.step);
5589 }
5590 ADDOP_I(c, BUILD_SLICE, n);
5591 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005592}
5593
Brandt Bucher145bf262021-02-26 14:51:55 -08005594
5595// PEP 634: Structural Pattern Matching
5596
5597// To keep things simple, all compiler_pattern_* routines follow the convention
5598// of replacing TOS (the subject for the given pattern) with either True (match)
5599// or False (no match). We do this even for irrefutable patterns; the idea is
5600// that it's much easier to smooth out any redundant pushing, popping, and
5601// jumping in the peephole optimizer than to detect or predict it here.
5602
Brandt Bucher145bf262021-02-26 14:51:55 -08005603#define WILDCARD_CHECK(N) \
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005604 ((N)->kind == MatchAs_kind && !(N)->v.MatchAs.name)
Brandt Bucher145bf262021-02-26 14:51:55 -08005605
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005606#define WILDCARD_STAR_CHECK(N) \
5607 ((N)->kind == MatchStar_kind && !(N)->v.MatchStar.name)
5608
5609// Limit permitted subexpressions, even if the parser & AST validator let them through
5610#define MATCH_VALUE_EXPR(N) \
5611 ((N)->kind == Constant_kind || (N)->kind == Attribute_kind)
Brandt Bucher145bf262021-02-26 14:51:55 -08005612
5613static int
5614pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc)
5615{
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005616 if (n == NULL) {
5617 ADDOP(c, POP_TOP);
5618 return 1;
5619 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005620 if (forbidden_name(c, n, Store)) {
5621 return 0;
5622 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005623 // Can't assign to the same name twice:
5624 if (pc->stores == NULL) {
5625 RETURN_IF_FALSE(pc->stores = PySet_New(NULL));
5626 }
5627 else {
5628 int duplicate = PySet_Contains(pc->stores, n);
5629 if (duplicate < 0) {
5630 return 0;
5631 }
5632 if (duplicate) {
5633 const char *e = "multiple assignments to name %R in pattern";
5634 return compiler_error(c, e, n);
5635 }
5636 }
5637 RETURN_IF_FALSE(!PySet_Add(pc->stores, n));
5638 RETURN_IF_FALSE(compiler_nameop(c, n, Store));
5639 return 1;
5640}
5641
5642
5643static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005644pattern_unpack_helper(struct compiler *c, asdl_pattern_seq *elts)
5645{
5646 Py_ssize_t n = asdl_seq_LEN(elts);
5647 int seen_star = 0;
5648 for (Py_ssize_t i = 0; i < n; i++) {
5649 pattern_ty elt = asdl_seq_GET(elts, i);
5650 if (elt->kind == MatchStar_kind && !seen_star) {
5651 if ((i >= (1 << 8)) ||
5652 (n-i-1 >= (INT_MAX >> 8)))
5653 return compiler_error(c,
5654 "too many expressions in "
5655 "star-unpacking sequence pattern");
5656 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
5657 seen_star = 1;
5658 }
5659 else if (elt->kind == MatchStar_kind) {
5660 return compiler_error(c,
5661 "multiple starred expressions in sequence pattern");
5662 }
5663 }
5664 if (!seen_star) {
5665 ADDOP_I(c, UNPACK_SEQUENCE, n);
5666 }
5667 return 1;
5668}
5669
5670static int
5671pattern_helper_sequence_unpack(struct compiler *c, asdl_pattern_seq *patterns,
Brandt Bucher145bf262021-02-26 14:51:55 -08005672 Py_ssize_t star, pattern_context *pc)
5673{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005674 RETURN_IF_FALSE(pattern_unpack_helper(c, patterns));
Brandt Bucher145bf262021-02-26 14:51:55 -08005675 // We've now got a bunch of new subjects on the stack. If any of them fail
5676 // to match, we need to pop everything else off, then finally push False.
5677 // fails is an array of blocks that correspond to the necessary amount of
5678 // popping for each element:
5679 basicblock **fails;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005680 Py_ssize_t size = asdl_seq_LEN(patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08005681 fails = (basicblock **)PyObject_Malloc(sizeof(basicblock*) * size);
5682 if (fails == NULL) {
5683 PyErr_NoMemory();
5684 return 0;
5685 }
5686 // NOTE: Can't use our nice returning macros anymore: they'll leak memory!
5687 // goto error on error.
5688 for (Py_ssize_t i = 0; i < size; i++) {
5689 fails[i] = compiler_new_block(c);
5690 if (fails[i] == NULL) {
5691 goto error;
5692 }
5693 }
5694 for (Py_ssize_t i = 0; i < size; i++) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005695 pattern_ty pattern = asdl_seq_GET(patterns, i);
5696 assert((i == star) == (pattern->kind == MatchStar_kind));
5697 if (!compiler_pattern_subpattern(c, pattern, pc) ||
Brandt Bucher145bf262021-02-26 14:51:55 -08005698 !compiler_addop_j(c, POP_JUMP_IF_FALSE, fails[i]) ||
5699 compiler_next_block(c) == NULL)
5700 {
5701 goto error;
5702 }
5703 }
5704 // Success!
5705 basicblock *end = compiler_new_block(c);
5706 if (end == NULL ||
5707 !compiler_addop_load_const(c, Py_True) ||
5708 !compiler_addop_j(c, JUMP_FORWARD, end))
5709 {
5710 goto error;
5711 }
5712 // This is where we handle failed sub-patterns. For a sequence pattern like
5713 // [a, b, c, d], this will look like:
5714 // fails[0]: POP_TOP
5715 // fails[1]: POP_TOP
5716 // fails[2]: POP_TOP
5717 // fails[3]: LOAD_CONST False
5718 for (Py_ssize_t i = 0; i < size - 1; i++) {
5719 compiler_use_next_block(c, fails[i]);
5720 if (!compiler_addop(c, POP_TOP)) {
5721 goto error;
5722 }
5723 }
5724 compiler_use_next_block(c, fails[size - 1]);
5725 if (!compiler_addop_load_const(c, Py_False)) {
5726 goto error;
5727 }
5728 compiler_use_next_block(c, end);
5729 PyObject_Free(fails);
5730 return 1;
5731error:
5732 PyObject_Free(fails);
5733 return 0;
5734}
5735
5736// Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of
5737// UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a
5738// starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc.
5739static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005740pattern_helper_sequence_subscr(struct compiler *c, asdl_pattern_seq *patterns,
Brandt Bucher145bf262021-02-26 14:51:55 -08005741 Py_ssize_t star, pattern_context *pc)
5742{
5743 basicblock *end, *fail_pop_1;
5744 RETURN_IF_FALSE(end = compiler_new_block(c));
5745 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005746 Py_ssize_t size = asdl_seq_LEN(patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08005747 for (Py_ssize_t i = 0; i < size; i++) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005748 pattern_ty pattern = asdl_seq_GET(patterns, i);
5749 if (WILDCARD_CHECK(pattern)) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005750 continue;
5751 }
5752 if (i == star) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005753 assert(WILDCARD_STAR_CHECK(pattern));
Brandt Bucher145bf262021-02-26 14:51:55 -08005754 continue;
5755 }
5756 ADDOP(c, DUP_TOP);
5757 if (i < star) {
5758 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5759 }
5760 else {
5761 // The subject may not support negative indexing! Compute a
5762 // nonnegative index:
5763 ADDOP(c, GET_LEN);
5764 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i));
5765 ADDOP(c, BINARY_SUBTRACT);
5766 }
5767 ADDOP(c, BINARY_SUBSCR);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005768 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08005769 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5770 NEXT_BLOCK(c);
5771 }
5772 ADDOP(c, POP_TOP);
5773 ADDOP_LOAD_CONST(c, Py_True);
5774 ADDOP_JUMP(c, JUMP_FORWARD, end);
5775 compiler_use_next_block(c, fail_pop_1);
5776 ADDOP(c, POP_TOP);
5777 ADDOP_LOAD_CONST(c, Py_False);
5778 compiler_use_next_block(c, end);
5779 return 1;
5780}
5781
Brandt Bucher145bf262021-02-26 14:51:55 -08005782// Like compiler_pattern, but turn off checks for irrefutability.
5783static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005784compiler_pattern_subpattern(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005785{
5786 int allow_irrefutable = pc->allow_irrefutable;
5787 pc->allow_irrefutable = 1;
5788 RETURN_IF_FALSE(compiler_pattern(c, p, pc));
5789 pc->allow_irrefutable = allow_irrefutable;
5790 return 1;
5791}
5792
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005793static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005794compiler_pattern_as(struct compiler *c, pattern_ty p, pattern_context *pc)
5795{
5796 assert(p->kind == MatchAs_kind);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005797 if (p->v.MatchAs.pattern == NULL) {
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005798 // An irrefutable match:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005799 if (!pc->allow_irrefutable) {
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005800 if (p->v.MatchAs.name) {
5801 const char *e = "name capture %R makes remaining patterns unreachable";
5802 return compiler_error(c, e, p->v.MatchAs.name);
5803 }
5804 const char *e = "wildcard makes remaining patterns unreachable";
5805 return compiler_error(c, e);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005806 }
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005807 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc));
5808 ADDOP_LOAD_CONST(c, Py_True);
5809 return 1;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005810 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005811 basicblock *end, *fail_pop_1;
5812 RETURN_IF_FALSE(end = compiler_new_block(c));
5813 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5814 // Need to make a copy for (possibly) storing later:
5815 ADDOP(c, DUP_TOP);
5816 RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc));
5817 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5818 NEXT_BLOCK(c);
5819 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc));
5820 ADDOP_LOAD_CONST(c, Py_True);
5821 ADDOP_JUMP(c, JUMP_FORWARD, end);
5822 compiler_use_next_block(c, fail_pop_1);
5823 // Need to pop that unused copy from before:
5824 ADDOP(c, POP_TOP);
5825 ADDOP_LOAD_CONST(c, Py_False);
5826 compiler_use_next_block(c, end);
5827 return 1;
5828}
5829
Brandt Bucher145bf262021-02-26 14:51:55 -08005830static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005831compiler_pattern_star(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005832{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005833 assert(p->kind == MatchStar_kind);
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07005834 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchStar.name, pc));
5835 ADDOP_LOAD_CONST(c, Py_True);
5836 return 1;
Brandt Bucher145bf262021-02-26 14:51:55 -08005837}
5838
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005839static int
5840validate_kwd_attrs(struct compiler *c, asdl_identifier_seq *attrs, asdl_pattern_seq* patterns)
5841{
5842 // Any errors will point to the pattern rather than the arg name as the
5843 // parser is only supplying identifiers rather than Name or keyword nodes
5844 Py_ssize_t nattrs = asdl_seq_LEN(attrs);
5845 for (Py_ssize_t i = 0; i < nattrs; i++) {
5846 identifier attr = ((identifier)asdl_seq_GET(attrs, i));
5847 c->u->u_col_offset = ((pattern_ty) asdl_seq_GET(patterns, i))->col_offset;
5848 if (forbidden_name(c, attr, Store)) {
5849 return -1;
5850 }
5851 for (Py_ssize_t j = i + 1; j < nattrs; j++) {
5852 identifier other = ((identifier)asdl_seq_GET(attrs, j));
5853 if (!PyUnicode_Compare(attr, other)) {
5854 c->u->u_col_offset = ((pattern_ty) asdl_seq_GET(patterns, j))->col_offset;
5855 compiler_error(c, "attribute name repeated in class pattern: %U", attr);
5856 return -1;
5857 }
5858 }
5859 }
5860 return 0;
5861}
Brandt Bucher145bf262021-02-26 14:51:55 -08005862
5863static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005864compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005865{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005866 assert(p->kind == MatchClass_kind);
5867 asdl_pattern_seq *patterns = p->v.MatchClass.patterns;
5868 asdl_identifier_seq *kwd_attrs = p->v.MatchClass.kwd_attrs;
5869 asdl_pattern_seq *kwd_patterns = p->v.MatchClass.kwd_patterns;
5870 Py_ssize_t nargs = asdl_seq_LEN(patterns);
5871 Py_ssize_t nattrs = asdl_seq_LEN(kwd_attrs);
5872 Py_ssize_t nkwd_patterns = asdl_seq_LEN(kwd_patterns);
5873 if (nattrs != nkwd_patterns) {
5874 // AST validator shouldn't let this happen, but if it does,
5875 // just fail, don't crash out of the interpreter
5876 const char * e = "kwd_attrs (%d) / kwd_patterns (%d) length mismatch in class pattern";
5877 return compiler_error(c, e, nattrs, nkwd_patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08005878 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005879 if (INT_MAX < nargs || INT_MAX < nargs + nattrs - 1) {
5880 const char *e = "too many sub-patterns in class pattern %R";
5881 return compiler_error(c, e, p->v.MatchClass.cls);
5882 }
5883 if (nattrs) {
5884 RETURN_IF_FALSE(!validate_kwd_attrs(c, kwd_attrs, kwd_patterns));
5885 c->u->u_col_offset = p->col_offset; // validate_kwd_attrs moves this
5886 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005887 basicblock *end, *fail_pop_1;
5888 RETURN_IF_FALSE(end = compiler_new_block(c));
5889 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005890 VISIT(c, expr, p->v.MatchClass.cls);
5891 PyObject *attr_names;
5892 RETURN_IF_FALSE(attr_names = PyTuple_New(nattrs));
Brandt Bucher145bf262021-02-26 14:51:55 -08005893 Py_ssize_t i;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005894 for (i = 0; i < nattrs; i++) {
5895 PyObject *name = asdl_seq_GET(kwd_attrs, i);
Brandt Bucher145bf262021-02-26 14:51:55 -08005896 Py_INCREF(name);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005897 PyTuple_SET_ITEM(attr_names, i, name);
Brandt Bucher145bf262021-02-26 14:51:55 -08005898 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005899 ADDOP_LOAD_CONST_NEW(c, attr_names);
Brandt Bucher145bf262021-02-26 14:51:55 -08005900 ADDOP_I(c, MATCH_CLASS, nargs);
5901 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5902 NEXT_BLOCK(c);
5903 // TOS is now a tuple of (nargs + nkwargs) attributes.
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005904 for (i = 0; i < nargs + nattrs; i++) {
5905 pattern_ty pattern;
Brandt Bucher145bf262021-02-26 14:51:55 -08005906 if (i < nargs) {
5907 // Positional:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005908 pattern = asdl_seq_GET(patterns, i);
Brandt Bucher145bf262021-02-26 14:51:55 -08005909 }
5910 else {
5911 // Keyword:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005912 pattern = asdl_seq_GET(kwd_patterns, i - nargs);
Brandt Bucher145bf262021-02-26 14:51:55 -08005913 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005914 if (WILDCARD_CHECK(pattern)) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005915 continue;
5916 }
5917 // Get the i-th attribute, and match it against the i-th pattern:
5918 ADDOP(c, DUP_TOP);
5919 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5920 ADDOP(c, BINARY_SUBSCR);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005921 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08005922 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5923 NEXT_BLOCK(c);
5924 }
5925 // Success! Pop the tuple of attributes:
5926 ADDOP(c, POP_TOP);
5927 ADDOP_LOAD_CONST(c, Py_True);
5928 ADDOP_JUMP(c, JUMP_FORWARD, end);
5929 compiler_use_next_block(c, fail_pop_1);
5930 ADDOP(c, POP_TOP);
5931 ADDOP_LOAD_CONST(c, Py_False);
5932 compiler_use_next_block(c, end);
5933 return 1;
5934}
5935
Brandt Bucher145bf262021-02-26 14:51:55 -08005936static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005937compiler_pattern_mapping(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08005938{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005939 assert(p->kind == MatchMapping_kind);
Brandt Bucher145bf262021-02-26 14:51:55 -08005940 basicblock *end, *fail_pop_1, *fail_pop_3;
5941 RETURN_IF_FALSE(end = compiler_new_block(c));
5942 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5943 RETURN_IF_FALSE(fail_pop_3 = compiler_new_block(c));
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005944 asdl_expr_seq *keys = p->v.MatchMapping.keys;
5945 asdl_pattern_seq *patterns = p->v.MatchMapping.patterns;
5946 Py_ssize_t size = asdl_seq_LEN(keys);
5947 Py_ssize_t npatterns = asdl_seq_LEN(patterns);
5948 if (size != npatterns) {
5949 // AST validator shouldn't let this happen, but if it does,
5950 // just fail, don't crash out of the interpreter
5951 const char * e = "keys (%d) / patterns (%d) length mismatch in mapping pattern";
5952 return compiler_error(c, e, size, npatterns);
5953 }
5954 // We have a double-star target if "rest" is set
5955 PyObject *star_target = p->v.MatchMapping.rest;
Brandt Bucher145bf262021-02-26 14:51:55 -08005956 ADDOP(c, MATCH_MAPPING);
5957 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5958 NEXT_BLOCK(c);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005959 if (!size && !star_target) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005960 // If the pattern is just "{}", we're done!
5961 ADDOP(c, POP_TOP);
5962 ADDOP_LOAD_CONST(c, Py_True);
5963 ADDOP_JUMP(c, JUMP_FORWARD, end);
5964 compiler_use_next_block(c, fail_pop_1);
5965 ADDOP(c, POP_TOP);
5966 ADDOP_LOAD_CONST(c, Py_False);
5967 compiler_use_next_block(c, end);
5968 return 1;
5969 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005970 if (size) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005971 // If the pattern has any keys in it, perform a length check:
5972 ADDOP(c, GET_LEN);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005973 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
Brandt Bucher145bf262021-02-26 14:51:55 -08005974 ADDOP_COMPARE(c, GtE);
5975 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5976 NEXT_BLOCK(c);
5977 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005978 if (INT_MAX < size - 1) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005979 return compiler_error(c, "too many sub-patterns in mapping pattern");
5980 }
5981 // Collect all of the keys into a tuple for MATCH_KEYS and
5982 // COPY_DICT_WITHOUT_KEYS. They can either be dotted names or literals:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005983 for (Py_ssize_t i = 0; i < size; i++) {
Brandt Bucher145bf262021-02-26 14:51:55 -08005984 expr_ty key = asdl_seq_GET(keys, i);
5985 if (key == NULL) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005986 const char *e = "can't use NULL keys in MatchMapping "
5987 "(set 'rest' parameter instead)";
5988 c->u->u_col_offset = ((pattern_ty) asdl_seq_GET(patterns, i))->col_offset;
5989 return compiler_error(c, e);
5990 }
5991 if (!MATCH_VALUE_EXPR(key)) {
5992 const char *e = "mapping pattern keys may only match literals and attribute lookups";
Brandt Bucher145bf262021-02-26 14:51:55 -08005993 return compiler_error(c, e);
5994 }
5995 VISIT(c, expr, key);
5996 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10005997 ADDOP_I(c, BUILD_TUPLE, size);
Brandt Bucher145bf262021-02-26 14:51:55 -08005998 ADDOP(c, MATCH_KEYS);
5999 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3);
6000 NEXT_BLOCK(c);
6001 // So far so good. There's now a tuple of values on the stack to match
6002 // sub-patterns against:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006003 for (Py_ssize_t i = 0; i < size; i++) {
6004 pattern_ty pattern = asdl_seq_GET(patterns, i);
6005 if (WILDCARD_CHECK(pattern)) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006006 continue;
6007 }
6008 ADDOP(c, DUP_TOP);
6009 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
6010 ADDOP(c, BINARY_SUBSCR);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006011 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006012 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3);
6013 NEXT_BLOCK(c);
6014 }
6015 // If we get this far, it's a match! We're done with that tuple of values.
6016 ADDOP(c, POP_TOP);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006017 if (star_target) {
6018 // If we have a starred name, bind a dict of remaining items to it:
Brandt Bucher145bf262021-02-26 14:51:55 -08006019 ADDOP(c, COPY_DICT_WITHOUT_KEYS);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006020 RETURN_IF_FALSE(pattern_helper_store_name(c, star_target, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006021 }
6022 else {
6023 // Otherwise, we don't care about this tuple of keys anymore:
6024 ADDOP(c, POP_TOP);
6025 }
6026 // Pop the subject:
6027 ADDOP(c, POP_TOP);
6028 ADDOP_LOAD_CONST(c, Py_True);
6029 ADDOP_JUMP(c, JUMP_FORWARD, end);
6030 // The top two items are a tuple of values or None, followed by a tuple of
6031 // keys. Pop them both:
6032 compiler_use_next_block(c, fail_pop_3);
6033 ADDOP(c, POP_TOP);
6034 ADDOP(c, POP_TOP);
6035 compiler_use_next_block(c, fail_pop_1);
6036 // Pop the subject:
6037 ADDOP(c, POP_TOP);
6038 ADDOP_LOAD_CONST(c, Py_False);
6039 compiler_use_next_block(c, end);
6040 return 1;
6041}
6042
Brandt Bucher145bf262021-02-26 14:51:55 -08006043static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006044compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006045{
6046 assert(p->kind == MatchOr_kind);
6047 // control is the set of names bound by the first alternative. If all of the
6048 // others bind the same names (they should), then this becomes pc->stores.
6049 PyObject *control = NULL;
6050 basicblock *end, *pass_pop_1;
6051 RETURN_IF_FALSE(end = compiler_new_block(c));
6052 RETURN_IF_FALSE(pass_pop_1 = compiler_new_block(c));
6053 Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns);
6054 assert(size > 1);
6055 // We're going to be messing with pc. Keep the original info handy:
6056 PyObject *stores_init = pc->stores;
6057 int allow_irrefutable = pc->allow_irrefutable;
6058 for (Py_ssize_t i = 0; i < size; i++) {
6059 // NOTE: Can't use our nice returning macros in here: they'll leak sets!
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006060 pattern_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i);
Brandt Bucher145bf262021-02-26 14:51:55 -08006061 pc->stores = PySet_New(stores_init);
6062 // An irrefutable sub-pattern must be last, if it is allowed at all:
6063 int is_last = i == size - 1;
6064 pc->allow_irrefutable = allow_irrefutable && is_last;
6065 SET_LOC(c, alt);
6066 if (pc->stores == NULL ||
6067 // Only copy the subject if we're *not* on the last alternative:
6068 (!is_last && !compiler_addop(c, DUP_TOP)) ||
6069 !compiler_pattern(c, alt, pc) ||
6070 // Only jump if we're *not* on the last alternative:
6071 (!is_last && !compiler_addop_j(c, POP_JUMP_IF_TRUE, pass_pop_1)) ||
6072 !compiler_next_block(c))
6073 {
6074 goto fail;
6075 }
6076 if (!i) {
6077 // If this is the first alternative, save its stores as a "control"
6078 // for the others (they can't bind a different set of names):
6079 control = pc->stores;
6080 continue;
6081 }
6082 if (PySet_GET_SIZE(pc->stores) || PySet_GET_SIZE(control)) {
6083 // Otherwise, check to see if we differ from the control set:
6084 PyObject *diff = PyNumber_InPlaceXor(pc->stores, control);
6085 if (diff == NULL) {
6086 goto fail;
6087 }
6088 if (PySet_GET_SIZE(diff)) {
6089 // The names differ! Raise.
6090 Py_DECREF(diff);
6091 compiler_error(c, "alternative patterns bind different names");
6092 goto fail;
6093 }
6094 Py_DECREF(diff);
6095 }
6096 Py_DECREF(pc->stores);
6097 }
6098 Py_XDECREF(stores_init);
6099 // Update pc->stores and restore pc->allow_irrefutable:
6100 pc->stores = control;
6101 pc->allow_irrefutable = allow_irrefutable;
6102 ADDOP_JUMP(c, JUMP_FORWARD, end);
6103 compiler_use_next_block(c, pass_pop_1);
6104 ADDOP(c, POP_TOP);
6105 ADDOP_LOAD_CONST(c, Py_True);
6106 compiler_use_next_block(c, end);
6107 return 1;
6108fail:
6109 Py_XDECREF(stores_init);
6110 Py_XDECREF(control);
6111 return 0;
6112}
6113
6114
6115static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006116compiler_pattern_sequence(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006117{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006118 assert(p->kind == MatchSequence_kind);
6119 asdl_pattern_seq *patterns = p->v.MatchSequence.patterns;
6120 Py_ssize_t size = asdl_seq_LEN(patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08006121 Py_ssize_t star = -1;
6122 int only_wildcard = 1;
6123 int star_wildcard = 0;
6124 // Find a starred name, if it exists. There may be at most one:
6125 for (Py_ssize_t i = 0; i < size; i++) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006126 pattern_ty pattern = asdl_seq_GET(patterns, i);
6127 if (pattern->kind == MatchStar_kind) {
Brandt Bucher145bf262021-02-26 14:51:55 -08006128 if (star >= 0) {
6129 const char *e = "multiple starred names in sequence pattern";
6130 return compiler_error(c, e);
6131 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006132 star_wildcard = WILDCARD_STAR_CHECK(pattern);
6133 only_wildcard &= star_wildcard;
Brandt Bucher145bf262021-02-26 14:51:55 -08006134 star = i;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006135 continue;
Brandt Bucher145bf262021-02-26 14:51:55 -08006136 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006137 only_wildcard &= WILDCARD_CHECK(pattern);
Brandt Bucher145bf262021-02-26 14:51:55 -08006138 }
6139 basicblock *end, *fail_pop_1;
6140 RETURN_IF_FALSE(end = compiler_new_block(c));
6141 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
6142 ADDOP(c, MATCH_SEQUENCE);
6143 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
6144 NEXT_BLOCK(c);
6145 if (star < 0) {
6146 // No star: len(subject) == size
6147 ADDOP(c, GET_LEN);
6148 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
6149 ADDOP_COMPARE(c, Eq);
6150 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
6151 NEXT_BLOCK(c);
6152 }
6153 else if (size > 1) {
6154 // Star: len(subject) >= size - 1
6155 ADDOP(c, GET_LEN);
6156 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1));
6157 ADDOP_COMPARE(c, GtE);
6158 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
6159 NEXT_BLOCK(c);
6160 }
6161 if (only_wildcard) {
6162 // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc.
6163 ADDOP(c, POP_TOP);
6164 ADDOP_LOAD_CONST(c, Py_True);
6165 }
6166 else if (star_wildcard) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006167 RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, patterns, star, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006168 }
6169 else {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006170 RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, patterns, star, pc));
Brandt Bucher145bf262021-02-26 14:51:55 -08006171 }
6172 ADDOP_JUMP(c, JUMP_FORWARD, end);
6173 compiler_use_next_block(c, fail_pop_1);
6174 ADDOP(c, POP_TOP)
6175 ADDOP_LOAD_CONST(c, Py_False);
6176 compiler_use_next_block(c, end);
6177 return 1;
6178}
6179
Brandt Bucher145bf262021-02-26 14:51:55 -08006180static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006181compiler_pattern_value(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006182{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006183 assert(p->kind == MatchValue_kind);
6184 expr_ty value = p->v.MatchValue.value;
6185 if (!MATCH_VALUE_EXPR(value)) {
6186 const char *e = "patterns may only match literals and attribute lookups";
6187 return compiler_error(c, e);
6188 }
6189 VISIT(c, expr, value);
Brandt Bucher145bf262021-02-26 14:51:55 -08006190 ADDOP_COMPARE(c, Eq);
6191 return 1;
6192}
6193
Brandt Bucher145bf262021-02-26 14:51:55 -08006194static int
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07006195compiler_pattern_singleton(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 == MatchSingleton_kind);
6198 ADDOP_LOAD_CONST(c, p->v.MatchSingleton.value);
6199 ADDOP_COMPARE(c, Is);
Brandt Bucher145bf262021-02-26 14:51:55 -08006200 return 1;
6201}
6202
Brandt Bucher145bf262021-02-26 14:51:55 -08006203static int
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006204compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc)
Brandt Bucher145bf262021-02-26 14:51:55 -08006205{
6206 SET_LOC(c, p);
6207 switch (p->kind) {
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006208 case MatchValue_kind:
Brandt Bucher145bf262021-02-26 14:51:55 -08006209 return compiler_pattern_value(c, p, pc);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006210 case MatchSingleton_kind:
Brandt Bucherdbe60ee2021-04-29 17:19:28 -07006211 return compiler_pattern_singleton(c, p, pc);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006212 case MatchSequence_kind:
Brandt Bucher145bf262021-02-26 14:51:55 -08006213 return compiler_pattern_sequence(c, p, pc);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006214 case MatchMapping_kind:
6215 return compiler_pattern_mapping(c, p, pc);
6216 case MatchClass_kind:
6217 return compiler_pattern_class(c, p, pc);
6218 case MatchStar_kind:
6219 return compiler_pattern_star(c, p, pc);
Brandt Bucher145bf262021-02-26 14:51:55 -08006220 case MatchAs_kind:
6221 return compiler_pattern_as(c, p, pc);
6222 case MatchOr_kind:
6223 return compiler_pattern_or(c, p, pc);
Brandt Bucher145bf262021-02-26 14:51:55 -08006224 }
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006225 // AST validator shouldn't let this happen, but if it does,
6226 // just fail, don't crash out of the interpreter
6227 const char *e = "invalid match pattern node in AST (kind=%d)";
6228 return compiler_error(c, e, p->kind);
Brandt Bucher145bf262021-02-26 14:51:55 -08006229}
6230
Brandt Bucher145bf262021-02-26 14:51:55 -08006231static int
6232compiler_match(struct compiler *c, stmt_ty s)
6233{
6234 VISIT(c, expr, s->v.Match.subject);
6235 basicblock *next, *end;
6236 RETURN_IF_FALSE(end = compiler_new_block(c));
6237 Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases);
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006238 assert(cases > 0);
Brandt Bucher145bf262021-02-26 14:51:55 -08006239 pattern_context pc;
6240 // We use pc.stores to track:
6241 // - Repeated name assignments in the same pattern.
6242 // - Different name assignments in alternatives.
6243 // It's a set of names, but we don't create it until it's needed:
6244 pc.stores = NULL;
6245 match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6246 int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases;
6247 for (Py_ssize_t i = 0; i < cases - has_default; i++) {
6248 m = asdl_seq_GET(s->v.Match.cases, i);
6249 SET_LOC(c, m->pattern);
6250 RETURN_IF_FALSE(next = compiler_new_block(c));
6251 // If pc.allow_irrefutable is 0, any name captures against our subject
6252 // will raise. Irrefutable cases must be either guarded, last, or both:
6253 pc.allow_irrefutable = m->guard != NULL || i == cases - 1;
6254 // Only copy the subject if we're *not* on the last case:
6255 if (i != cases - has_default - 1) {
6256 ADDOP(c, DUP_TOP);
6257 }
6258 int result = compiler_pattern(c, m->pattern, &pc);
6259 Py_CLEAR(pc.stores);
6260 RETURN_IF_FALSE(result);
6261 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, next);
6262 NEXT_BLOCK(c);
6263 if (m->guard) {
6264 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, next, 0));
6265 }
6266 // Success! Pop the subject off, we're done with it:
6267 if (i != cases - has_default - 1) {
6268 ADDOP(c, POP_TOP);
6269 }
6270 VISIT_SEQ(c, stmt, m->body);
6271 ADDOP_JUMP(c, JUMP_FORWARD, end);
6272 compiler_use_next_block(c, next);
6273 }
6274 if (has_default) {
6275 if (cases == 1) {
6276 // No matches. Done with the subject:
6277 ADDOP(c, POP_TOP);
6278 }
6279 // A trailing "case _" is common, and lets us save a bit of redundant
6280 // pushing and popping in the loop above:
6281 m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6282 SET_LOC(c, m->pattern);
6283 if (m->guard) {
6284 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0));
6285 }
6286 VISIT_SEQ(c, stmt, m->body);
6287 }
6288 compiler_use_next_block(c, end);
6289 return 1;
6290}
6291
Brandt Bucher145bf262021-02-26 14:51:55 -08006292#undef WILDCARD_CHECK
Nick Coghlan1e7b8582021-04-29 15:58:44 +10006293#undef WILDCARD_STAR_CHECK
Brandt Bucher145bf262021-02-26 14:51:55 -08006294
Thomas Wouters89f507f2006-12-13 04:49:30 +00006295/* End of the compiler section, beginning of the assembler section */
6296
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006297/* do depth-first search of basic block graph, starting with block.
T. Wouters99b54d62019-09-12 07:05:33 -07006298 post records the block indices in post-order.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006299
6300 XXX must handle implicit jumps from one block to next
6301*/
6302
Thomas Wouters89f507f2006-12-13 04:49:30 +00006303struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006304 PyObject *a_bytecode; /* string containing bytecode */
6305 int a_offset; /* offset into bytecode */
6306 int a_nblocks; /* number of reachable blocks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006307 PyObject *a_lnotab; /* string containing lnotab */
6308 int a_lnotab_off; /* offset into lnotab */
Mark Shannon877df852020-11-12 09:43:29 +00006309 int a_prevlineno; /* lineno of last emitted line in line table */
6310 int a_lineno; /* lineno of last emitted instruction */
6311 int a_lineno_start; /* bytecode start offset of current lineno */
Mark Shannoncc75ab72020-11-12 19:49:33 +00006312 basicblock *a_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006313};
6314
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006315Py_LOCAL_INLINE(void)
6316stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006317{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02006318 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
Mark Shannonfee55262019-11-21 09:11:43 +00006319 if (b->b_startdepth < depth && b->b_startdepth < 100) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006320 assert(b->b_startdepth < 0);
6321 b->b_startdepth = depth;
6322 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02006323 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006324}
6325
6326/* Find the flow path that needs the largest stack. We assume that
6327 * cycles in the flow graph have no net effect on the stack depth.
6328 */
6329static int
6330stackdepth(struct compiler *c)
6331{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006332 basicblock *b, *entryblock = NULL;
6333 basicblock **stack, **sp;
6334 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006335 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006336 b->b_startdepth = INT_MIN;
6337 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006338 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006339 }
Mark Shannon67969f52021-04-07 10:52:07 +01006340 assert(entryblock!= NULL);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006341 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
6342 if (!stack) {
6343 PyErr_NoMemory();
6344 return -1;
6345 }
6346
6347 sp = stack;
Mark Shannonb37181e2021-04-06 11:48:59 +01006348 if (c->u->u_ste->ste_generator || c->u->u_ste->ste_coroutine) {
6349 stackdepth_push(&sp, entryblock, 1);
6350 } else {
6351 stackdepth_push(&sp, entryblock, 0);
6352 }
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006353 while (sp != stack) {
6354 b = *--sp;
6355 int depth = b->b_startdepth;
6356 assert(depth >= 0);
6357 basicblock *next = b->b_next;
6358 for (int i = 0; i < b->b_iused; i++) {
6359 struct instr *instr = &b->b_instr[i];
6360 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
6361 if (effect == PY_INVALID_STACK_EFFECT) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01006362 PyErr_Format(PyExc_SystemError,
6363 "compiler stack_effect(opcode=%d, arg=%i) failed",
6364 instr->i_opcode, instr->i_oparg);
6365 return -1;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006366 }
6367 int new_depth = depth + effect;
6368 if (new_depth > maxdepth) {
6369 maxdepth = new_depth;
6370 }
6371 assert(depth >= 0); /* invalid code or bug in stackdepth() */
Mark Shannon582aaf12020-08-04 17:30:11 +01006372 if (is_jump(instr)) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006373 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
6374 assert(effect != PY_INVALID_STACK_EFFECT);
6375 int target_depth = depth + effect;
6376 if (target_depth > maxdepth) {
6377 maxdepth = target_depth;
6378 }
6379 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006380 stackdepth_push(&sp, instr->i_target, target_depth);
6381 }
6382 depth = new_depth;
6383 if (instr->i_opcode == JUMP_ABSOLUTE ||
6384 instr->i_opcode == JUMP_FORWARD ||
6385 instr->i_opcode == RETURN_VALUE ||
Mark Shannonfee55262019-11-21 09:11:43 +00006386 instr->i_opcode == RAISE_VARARGS ||
6387 instr->i_opcode == RERAISE)
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006388 {
6389 /* remaining code is dead */
6390 next = NULL;
6391 break;
6392 }
6393 }
6394 if (next != NULL) {
Mark Shannon266b4622020-11-17 19:30:14 +00006395 assert(b->b_nofallthrough == 0);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006396 stackdepth_push(&sp, next, depth);
6397 }
6398 }
6399 PyObject_Free(stack);
6400 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006401}
6402
6403static int
6404assemble_init(struct assembler *a, int nblocks, int firstlineno)
6405{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006406 memset(a, 0, sizeof(struct assembler));
Mark Shannon877df852020-11-12 09:43:29 +00006407 a->a_prevlineno = a->a_lineno = firstlineno;
Mark Shannonfd009e62020-11-13 12:53:53 +00006408 a->a_lnotab = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006409 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006410 if (a->a_bytecode == NULL) {
6411 goto error;
6412 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006413 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006414 if (a->a_lnotab == NULL) {
6415 goto error;
6416 }
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07006417 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006418 PyErr_NoMemory();
Mark Shannonfd009e62020-11-13 12:53:53 +00006419 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006420 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006421 return 1;
Mark Shannonfd009e62020-11-13 12:53:53 +00006422error:
6423 Py_XDECREF(a->a_bytecode);
6424 Py_XDECREF(a->a_lnotab);
6425 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006426}
6427
6428static void
6429assemble_free(struct assembler *a)
6430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006431 Py_XDECREF(a->a_bytecode);
6432 Py_XDECREF(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006433}
6434
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006435static int
6436blocksize(basicblock *b)
6437{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006438 int i;
6439 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006441 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006442 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006443 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006444}
6445
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006446static int
Mark Shannon877df852020-11-12 09:43:29 +00006447assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006448{
Mark Shannon877df852020-11-12 09:43:29 +00006449 Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006450 if (a->a_lnotab_off + 2 >= len) {
6451 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
6452 return 0;
6453 }
Pablo Galindo86e322f2021-01-30 13:54:22 +00006454 unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab);
6455 lnotab += a->a_lnotab_off;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006456 a->a_lnotab_off += 2;
Mark Shannon877df852020-11-12 09:43:29 +00006457 *lnotab++ = bdelta;
6458 *lnotab++ = ldelta;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006459 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006460}
6461
Mark Shannon877df852020-11-12 09:43:29 +00006462/* Appends a range to the end of the line number table. See
6463 * Objects/lnotab_notes.txt for the description of the line number table. */
6464
6465static int
6466assemble_line_range(struct assembler *a)
6467{
6468 int ldelta, bdelta;
6469 bdelta = (a->a_offset - a->a_lineno_start) * 2;
6470 if (bdelta == 0) {
6471 return 1;
6472 }
6473 if (a->a_lineno < 0) {
6474 ldelta = -128;
6475 }
6476 else {
6477 ldelta = a->a_lineno - a->a_prevlineno;
6478 a->a_prevlineno = a->a_lineno;
6479 while (ldelta > 127) {
6480 if (!assemble_emit_linetable_pair(a, 0, 127)) {
6481 return 0;
6482 }
6483 ldelta -= 127;
6484 }
6485 while (ldelta < -127) {
6486 if (!assemble_emit_linetable_pair(a, 0, -127)) {
6487 return 0;
6488 }
6489 ldelta += 127;
6490 }
6491 }
6492 assert(-128 <= ldelta && ldelta < 128);
6493 while (bdelta > 254) {
6494 if (!assemble_emit_linetable_pair(a, 254, ldelta)) {
6495 return 0;
6496 }
6497 ldelta = a->a_lineno < 0 ? -128 : 0;
6498 bdelta -= 254;
6499 }
6500 if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) {
6501 return 0;
6502 }
6503 a->a_lineno_start = a->a_offset;
6504 return 1;
6505}
6506
6507static int
6508assemble_lnotab(struct assembler *a, struct instr *i)
6509{
6510 if (i->i_lineno == a->a_lineno) {
6511 return 1;
6512 }
6513 if (!assemble_line_range(a)) {
6514 return 0;
6515 }
6516 a->a_lineno = i->i_lineno;
6517 return 1;
6518}
6519
6520
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006521/* assemble_emit()
6522 Extend the bytecode with a new instruction.
6523 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00006524*/
6525
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006526static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006527assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006528{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006529 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006530 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03006531 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006532
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006533 arg = i->i_oparg;
6534 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006535 if (i->i_lineno && !assemble_lnotab(a, i))
6536 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006537 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006538 if (len > PY_SSIZE_T_MAX / 2)
6539 return 0;
6540 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
6541 return 0;
6542 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03006543 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006544 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006545 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006546 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006547}
6548
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00006549static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006550assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006551{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006552 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006553 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006554 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00006555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006556 /* Compute the size of each block and fixup jump args.
6557 Replace block pointer with position in bytecode. */
6558 do {
6559 totsize = 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006560 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006561 bsize = blocksize(b);
6562 b->b_offset = totsize;
6563 totsize += bsize;
6564 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006565 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006566 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6567 bsize = b->b_offset;
6568 for (i = 0; i < b->b_iused; i++) {
6569 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006570 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006571 /* Relative jumps are computed relative to
6572 the instruction pointer after fetching
6573 the jump instruction.
6574 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006575 bsize += isize;
Mark Shannon582aaf12020-08-04 17:30:11 +01006576 if (is_jump(instr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006577 instr->i_oparg = instr->i_target->b_offset;
Mark Shannon582aaf12020-08-04 17:30:11 +01006578 if (is_relative_jump(instr)) {
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006579 instr->i_oparg -= bsize;
6580 }
6581 if (instrsize(instr->i_oparg) != isize) {
6582 extended_arg_recompile = 1;
6583 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006584 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006585 }
6586 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00006587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006588 /* XXX: This is an awful hack that could hurt performance, but
6589 on the bright side it should work until we come up
6590 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006592 The issue is that in the first loop blocksize() is called
6593 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006594 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006595 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006597 So we loop until we stop seeing new EXTENDED_ARGs.
6598 The only EXTENDED_ARGs that could be popping up are
6599 ones in jump instructions. So this should converge
6600 fairly quickly.
6601 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006602 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00006603}
6604
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006605static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01006606dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006608 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006609 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006611 tuple = PyTuple_New(size);
6612 if (tuple == NULL)
6613 return NULL;
6614 while (PyDict_Next(dict, &pos, &k, &v)) {
6615 i = PyLong_AS_LONG(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006616 Py_INCREF(k);
6617 assert((i - offset) < size);
6618 assert((i - offset) >= 0);
6619 PyTuple_SET_ITEM(tuple, i - offset, k);
6620 }
6621 return tuple;
6622}
6623
6624static PyObject *
6625consts_dict_keys_inorder(PyObject *dict)
6626{
6627 PyObject *consts, *k, *v;
6628 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
6629
6630 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
6631 if (consts == NULL)
6632 return NULL;
6633 while (PyDict_Next(dict, &pos, &k, &v)) {
6634 i = PyLong_AS_LONG(v);
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +03006635 /* The keys of the dictionary can be tuples wrapping a contant.
6636 * (see compiler_add_o and _PyCode_ConstantKey). In that case
6637 * the object we want is always second. */
6638 if (PyTuple_CheckExact(k)) {
6639 k = PyTuple_GET_ITEM(k, 1);
6640 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006641 Py_INCREF(k);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006642 assert(i < size);
6643 assert(i >= 0);
6644 PyList_SET_ITEM(consts, i, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006645 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006646 return consts;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006647}
6648
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006649static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006650compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006651{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006652 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006653 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006654 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04006655 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006656 if (ste->ste_nested)
6657 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07006658 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006659 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07006660 if (!ste->ste_generator && ste->ste_coroutine)
6661 flags |= CO_COROUTINE;
6662 if (ste->ste_generator && ste->ste_coroutine)
6663 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006664 if (ste->ste_varargs)
6665 flags |= CO_VARARGS;
6666 if (ste->ste_varkeywords)
6667 flags |= CO_VARKEYWORDS;
6668 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006670 /* (Only) inherit compilerflags in PyCF_MASK */
6671 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006672
Pablo Galindo90235812020-03-15 04:29:22 +00006673 if ((IS_TOP_LEVEL_AWAIT(c)) &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07006674 ste->ste_coroutine &&
6675 !ste->ste_generator) {
6676 flags |= CO_COROUTINE;
6677 }
6678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006679 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00006680}
6681
Inada Naokibdb941b2021-02-10 09:20:42 +09006682// Merge *obj* with constant cache.
INADA Naokic2e16072018-11-26 21:23:22 +09006683// Unlike merge_consts_recursive(), this function doesn't work recursively.
6684static int
Inada Naokibdb941b2021-02-10 09:20:42 +09006685merge_const_one(struct compiler *c, PyObject **obj)
INADA Naokic2e16072018-11-26 21:23:22 +09006686{
Inada Naokibdb941b2021-02-10 09:20:42 +09006687 PyObject *key = _PyCode_ConstantKey(*obj);
INADA Naokic2e16072018-11-26 21:23:22 +09006688 if (key == NULL) {
6689 return 0;
6690 }
6691
6692 // t is borrowed reference
6693 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
6694 Py_DECREF(key);
6695 if (t == NULL) {
6696 return 0;
6697 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006698 if (t == key) { // obj is new constant.
INADA Naokic2e16072018-11-26 21:23:22 +09006699 return 1;
6700 }
6701
Inada Naokibdb941b2021-02-10 09:20:42 +09006702 if (PyTuple_CheckExact(t)) {
6703 // t is still borrowed reference
6704 t = PyTuple_GET_ITEM(t, 1);
6705 }
6706
6707 Py_INCREF(t);
6708 Py_DECREF(*obj);
6709 *obj = t;
INADA Naokic2e16072018-11-26 21:23:22 +09006710 return 1;
6711}
6712
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006713static PyCodeObject *
Mark Shannon6e8128f2020-07-30 10:03:00 +01006714makecode(struct compiler *c, struct assembler *a, PyObject *consts)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006716 PyCodeObject *co = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006717 PyObject *names = NULL;
6718 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006719 PyObject *name = NULL;
6720 PyObject *freevars = NULL;
6721 PyObject *cellvars = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006722 Py_ssize_t nlocals;
6723 int nlocals_int;
6724 int flags;
Pablo Galindocd74e662019-06-01 18:08:04 +01006725 int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006727 names = dict_keys_inorder(c->u->u_names, 0);
6728 varnames = dict_keys_inorder(c->u->u_varnames, 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006729 if (!names || !varnames) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006730 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006731 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006732 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
6733 if (!cellvars)
6734 goto error;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006735 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006736 if (!freevars)
6737 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006738
Inada Naokibdb941b2021-02-10 09:20:42 +09006739 if (!merge_const_one(c, &names) ||
6740 !merge_const_one(c, &varnames) ||
6741 !merge_const_one(c, &cellvars) ||
6742 !merge_const_one(c, &freevars))
INADA Naokic2e16072018-11-26 21:23:22 +09006743 {
6744 goto error;
6745 }
6746
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006747 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01006748 assert(nlocals < INT_MAX);
6749 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
6750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006751 flags = compute_code_flags(c);
6752 if (flags < 0)
6753 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006754
Mark Shannon6e8128f2020-07-30 10:03:00 +01006755 consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
6756 if (consts == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006757 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006758 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006759 if (!merge_const_one(c, &consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006760 Py_DECREF(consts);
INADA Naokic2e16072018-11-26 21:23:22 +09006761 goto error;
6762 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006763
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01006764 posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int);
Pablo Galindocd74e662019-06-01 18:08:04 +01006765 posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +01006766 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006767 maxdepth = stackdepth(c);
6768 if (maxdepth < 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006769 Py_DECREF(consts);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006770 goto error;
6771 }
Mark Shannon11e0b292021-04-15 14:28:56 +01006772 if (maxdepth > MAX_ALLOWED_STACK_USE) {
6773 PyErr_Format(PyExc_SystemError,
6774 "excessive stack use: stack is %d deep",
6775 maxdepth);
6776 Py_DECREF(consts);
6777 goto error;
6778 }
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006779 co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
Mark Shannon13bc1392020-01-23 09:25:17 +00006780 posonlyargcount, kwonlyargcount, nlocals_int,
Mark Shannon6e8128f2020-07-30 10:03:00 +01006781 maxdepth, flags, a->a_bytecode, consts, names,
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006782 varnames, freevars, cellvars, c->c_filename,
6783 c->u->u_name, c->u->u_firstlineno, a->a_lnotab);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006784 Py_DECREF(consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006785 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006786 Py_XDECREF(names);
6787 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006788 Py_XDECREF(name);
6789 Py_XDECREF(freevars);
6790 Py_XDECREF(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006791 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006792}
6793
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006794
6795/* For debugging purposes only */
6796#if 0
6797static void
6798dump_instr(const struct instr *i)
6799{
Mark Shannon582aaf12020-08-04 17:30:11 +01006800 const char *jrel = (is_relative_jump(instr)) ? "jrel " : "";
6801 const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : "";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006802 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006804 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006805 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006806 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006807 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006808 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
6809 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006810}
6811
6812static void
6813dump_basicblock(const basicblock *b)
6814{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006815 const char *b_return = b->b_return ? "return " : "";
Pablo Galindo60eb9f12020-06-28 01:55:47 +01006816 fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n",
6817 b->b_iused, b->b_startdepth, b->b_offset, b_return);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006818 if (b->b_instr) {
6819 int i;
6820 for (i = 0; i < b->b_iused; i++) {
6821 fprintf(stderr, " [%02d] ", i);
6822 dump_instr(b->b_instr + i);
6823 }
6824 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006825}
6826#endif
6827
Mark Shannon5977a792020-12-02 13:31:40 +00006828
6829static int
6830normalize_basic_block(basicblock *bb);
6831
Mark Shannon6e8128f2020-07-30 10:03:00 +01006832static int
Inada Naoki8a232c72021-04-16 14:01:04 +09006833optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006834
Mark Shannon5977a792020-12-02 13:31:40 +00006835static int
6836ensure_exits_have_lineno(struct compiler *c);
6837
Mark Shannonb37181e2021-04-06 11:48:59 +01006838static int
6839insert_generator_prefix(struct compiler *c, basicblock *entryblock) {
6840
6841 int flags = compute_code_flags(c);
6842 if (flags < 0) {
6843 return -1;
6844 }
6845 int kind;
6846 if (flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
6847 if (flags & CO_COROUTINE) {
6848 kind = 1;
6849 }
6850 else if (flags & CO_ASYNC_GENERATOR) {
6851 kind = 2;
6852 }
6853 else {
6854 kind = 0;
6855 }
6856 }
6857 else {
6858 return 0;
6859 }
6860 if (compiler_next_instr(entryblock) < 0) {
6861 return -1;
6862 }
6863 for (int i = entryblock->b_iused-1; i > 0; i--) {
6864 entryblock->b_instr[i] = entryblock->b_instr[i-1];
6865 }
6866 entryblock->b_instr[0].i_opcode = GEN_START;
6867 entryblock->b_instr[0].i_oparg = kind;
6868 entryblock->b_instr[0].i_lineno = -1;
6869 entryblock->b_instr[0].i_target = NULL;
6870 return 0;
6871}
6872
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006873static PyCodeObject *
6874assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006876 basicblock *b, *entryblock;
6877 struct assembler a;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006878 int j, nblocks;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006879 PyCodeObject *co = NULL;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006880 PyObject *consts = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006882 /* Make sure every block that falls off the end returns None.
6883 XXX NEXT_BLOCK() isn't quite right, because if the last
6884 block ends with a jump or return b_next shouldn't set.
6885 */
6886 if (!c->u->u_curblock->b_return) {
Mark Shannon877df852020-11-12 09:43:29 +00006887 c->u->u_lineno = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006888 if (addNone)
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006889 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006890 ADDOP(c, RETURN_VALUE);
6891 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006892
Mark Shannon5977a792020-12-02 13:31:40 +00006893 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
6894 if (normalize_basic_block(b)) {
Alex Henrie503627f2021-03-02 03:20:25 -07006895 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00006896 }
6897 }
6898
6899 if (ensure_exits_have_lineno(c)) {
Alex Henrie503627f2021-03-02 03:20:25 -07006900 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00006901 }
6902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006903 nblocks = 0;
6904 entryblock = NULL;
6905 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6906 nblocks++;
6907 entryblock = b;
6908 }
Mark Shannon67969f52021-04-07 10:52:07 +01006909 assert(entryblock != NULL);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006910
Mark Shannonb37181e2021-04-06 11:48:59 +01006911 if (insert_generator_prefix(c, entryblock)) {
6912 goto error;
6913 }
6914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006915 /* Set firstlineno if it wasn't explicitly set. */
6916 if (!c->u->u_firstlineno) {
Mark Shannon67969f52021-04-07 10:52:07 +01006917 if (entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006918 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
Mark Shannon877df852020-11-12 09:43:29 +00006919 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006920 c->u->u_firstlineno = 1;
6921 }
Mark Shannon5977a792020-12-02 13:31:40 +00006922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006923 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
6924 goto error;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006925 a.a_entry = entryblock;
6926 a.a_nblocks = nblocks;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006927
Mark Shannon6e8128f2020-07-30 10:03:00 +01006928 consts = consts_dict_keys_inorder(c->u->u_consts);
6929 if (consts == NULL) {
6930 goto error;
6931 }
Inada Naoki8a232c72021-04-16 14:01:04 +09006932 if (optimize_cfg(c, &a, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006933 goto error;
6934 }
6935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006936 /* Can't modify the bytecode after computing jump offsets. */
6937 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00006938
Mark Shannoncc75ab72020-11-12 19:49:33 +00006939 /* Emit code. */
6940 for(b = entryblock; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006941 for (j = 0; j < b->b_iused; j++)
6942 if (!assemble_emit(&a, &b->b_instr[j]))
6943 goto error;
6944 }
Mark Shannon877df852020-11-12 09:43:29 +00006945 if (!assemble_line_range(&a)) {
6946 return 0;
6947 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00006948
Inada Naokibdb941b2021-02-10 09:20:42 +09006949 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006950 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09006951 }
6952 if (!merge_const_one(c, &a.a_lnotab)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006953 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09006954 }
6955 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) {
6956 goto error;
6957 }
6958 if (!merge_const_one(c, &a.a_bytecode)) {
6959 goto error;
6960 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006961
Mark Shannon6e8128f2020-07-30 10:03:00 +01006962 co = makecode(c, &a, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006963 error:
Mark Shannon6e8128f2020-07-30 10:03:00 +01006964 Py_XDECREF(consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006965 assemble_free(&a);
6966 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006967}
Georg Brandl8334fd92010-12-04 10:26:46 +00006968
Mark Shannon6e8128f2020-07-30 10:03:00 +01006969/* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
6970 with LOAD_CONST (c1, c2, ... cn).
6971 The consts table must still be in list form so that the
6972 new constant (c1, c2, ... cn) can be appended.
6973 Called with codestr pointing to the first LOAD_CONST.
6974*/
6975static int
Inada Naoki8a232c72021-04-16 14:01:04 +09006976fold_tuple_on_constants(struct compiler *c,
6977 struct instr *inst,
Mark Shannon6e8128f2020-07-30 10:03:00 +01006978 int n, PyObject *consts)
6979{
6980 /* Pre-conditions */
6981 assert(PyList_CheckExact(consts));
6982 assert(inst[n].i_opcode == BUILD_TUPLE);
6983 assert(inst[n].i_oparg == n);
6984
6985 for (int i = 0; i < n; i++) {
6986 if (inst[i].i_opcode != LOAD_CONST) {
6987 return 0;
6988 }
6989 }
6990
6991 /* Buildup new tuple of constants */
6992 PyObject *newconst = PyTuple_New(n);
6993 if (newconst == NULL) {
6994 return -1;
6995 }
6996 for (int i = 0; i < n; i++) {
6997 int arg = inst[i].i_oparg;
6998 PyObject *constant = PyList_GET_ITEM(consts, arg);
6999 Py_INCREF(constant);
7000 PyTuple_SET_ITEM(newconst, i, constant);
7001 }
Inada Naoki8a232c72021-04-16 14:01:04 +09007002 if (merge_const_one(c, &newconst) == 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007003 Py_DECREF(newconst);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007004 return -1;
7005 }
Inada Naoki8a232c72021-04-16 14:01:04 +09007006
7007 Py_ssize_t index;
7008 for (index = 0; index < PyList_GET_SIZE(consts); index++) {
7009 if (PyList_GET_ITEM(consts, index) == newconst) {
7010 break;
7011 }
7012 }
7013 if (index == PyList_GET_SIZE(consts)) {
7014 if ((size_t)index >= (size_t)INT_MAX - 1) {
7015 Py_DECREF(newconst);
7016 PyErr_SetString(PyExc_OverflowError, "too many constants");
7017 return -1;
7018 }
7019 if (PyList_Append(consts, newconst)) {
7020 Py_DECREF(newconst);
7021 return -1;
7022 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007023 }
7024 Py_DECREF(newconst);
7025 for (int i = 0; i < n; i++) {
7026 inst[i].i_opcode = NOP;
7027 }
7028 inst[n].i_opcode = LOAD_CONST;
Victor Stinner71f2ff42020-09-23 14:06:55 +02007029 inst[n].i_oparg = (int)index;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007030 return 0;
7031}
7032
Mark Shannon28b75c82020-12-23 11:43:10 +00007033
7034static int
7035eliminate_jump_to_jump(basicblock *bb, int opcode) {
7036 assert (bb->b_iused > 0);
7037 struct instr *inst = &bb->b_instr[bb->b_iused-1];
7038 assert (is_jump(inst));
7039 assert (inst->i_target->b_iused > 0);
7040 struct instr *target = &inst->i_target->b_instr[0];
7041 if (inst->i_target == target->i_target) {
7042 /* Nothing to do */
7043 return 0;
7044 }
7045 int lineno = target->i_lineno;
7046 if (add_jump_to_block(bb, opcode, lineno, target->i_target) == 0) {
7047 return -1;
7048 }
7049 assert (bb->b_iused >= 2);
7050 bb->b_instr[bb->b_iused-2].i_opcode = NOP;
7051 return 0;
7052}
7053
Mark Shannoncc75ab72020-11-12 19:49:33 +00007054/* Maximum size of basic block that should be copied in optimizer */
7055#define MAX_COPY_SIZE 4
Mark Shannon6e8128f2020-07-30 10:03:00 +01007056
7057/* Optimization */
7058static int
Inada Naoki8a232c72021-04-16 14:01:04 +09007059optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
Mark Shannon6e8128f2020-07-30 10:03:00 +01007060{
7061 assert(PyList_CheckExact(consts));
7062 struct instr nop;
7063 nop.i_opcode = NOP;
7064 struct instr *target;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007065 for (int i = 0; i < bb->b_iused; i++) {
7066 struct instr *inst = &bb->b_instr[i];
7067 int oparg = inst->i_oparg;
7068 int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01007069 if (is_jump(inst)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007070 /* Skip over empty basic blocks. */
7071 while (inst->i_target->b_iused == 0) {
7072 inst->i_target = inst->i_target->b_next;
7073 }
7074 target = &inst->i_target->b_instr[0];
7075 }
7076 else {
7077 target = &nop;
7078 }
7079 switch (inst->i_opcode) {
Mark Shannon266b4622020-11-17 19:30:14 +00007080 /* Remove LOAD_CONST const; conditional jump */
Mark Shannon6e8128f2020-07-30 10:03:00 +01007081 case LOAD_CONST:
Mark Shannon266b4622020-11-17 19:30:14 +00007082 {
7083 PyObject* cnt;
7084 int is_true;
7085 int jump_if_true;
7086 switch(nextop) {
7087 case POP_JUMP_IF_FALSE:
7088 case POP_JUMP_IF_TRUE:
7089 cnt = PyList_GET_ITEM(consts, oparg);
7090 is_true = PyObject_IsTrue(cnt);
7091 if (is_true == -1) {
7092 goto error;
7093 }
7094 inst->i_opcode = NOP;
7095 jump_if_true = nextop == POP_JUMP_IF_TRUE;
7096 if (is_true == jump_if_true) {
7097 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
7098 bb->b_nofallthrough = 1;
7099 }
7100 else {
7101 bb->b_instr[i+1].i_opcode = NOP;
7102 }
7103 break;
7104 case JUMP_IF_FALSE_OR_POP:
7105 case JUMP_IF_TRUE_OR_POP:
7106 cnt = PyList_GET_ITEM(consts, oparg);
7107 is_true = PyObject_IsTrue(cnt);
7108 if (is_true == -1) {
7109 goto error;
7110 }
7111 jump_if_true = nextop == JUMP_IF_TRUE_OR_POP;
7112 if (is_true == jump_if_true) {
7113 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
7114 bb->b_nofallthrough = 1;
7115 }
7116 else {
7117 inst->i_opcode = NOP;
7118 bb->b_instr[i+1].i_opcode = NOP;
7119 }
7120 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007121 }
7122 break;
Mark Shannon266b4622020-11-17 19:30:14 +00007123 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007124
7125 /* Try to fold tuples of constants.
7126 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
7127 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
7128 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
7129 case BUILD_TUPLE:
7130 if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
7131 switch(oparg) {
7132 case 1:
7133 inst->i_opcode = NOP;
7134 bb->b_instr[i+1].i_opcode = NOP;
7135 break;
7136 case 2:
7137 inst->i_opcode = ROT_TWO;
7138 bb->b_instr[i+1].i_opcode = NOP;
7139 break;
7140 case 3:
7141 inst->i_opcode = ROT_THREE;
7142 bb->b_instr[i+1].i_opcode = ROT_TWO;
7143 }
7144 break;
7145 }
7146 if (i >= oparg) {
Inada Naoki8a232c72021-04-16 14:01:04 +09007147 if (fold_tuple_on_constants(c, inst-oparg, oparg, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007148 goto error;
7149 }
7150 }
7151 break;
7152
7153 /* Simplify conditional jump to conditional jump where the
7154 result of the first test implies the success of a similar
7155 test or the failure of the opposite test.
7156 Arises in code like:
7157 "a and b or c"
7158 "(a and b) and c"
7159 "(a or b) or c"
7160 "(a or b) and c"
7161 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
7162 --> x:JUMP_IF_FALSE_OR_POP z
7163 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
7164 --> x:POP_JUMP_IF_FALSE y+1
7165 where y+1 is the instruction following the second test.
7166 */
7167 case JUMP_IF_FALSE_OR_POP:
7168 switch(target->i_opcode) {
7169 case POP_JUMP_IF_FALSE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007170 if (inst->i_lineno == target->i_lineno) {
7171 *inst = *target;
7172 i--;
7173 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007174 break;
7175 case JUMP_ABSOLUTE:
7176 case JUMP_FORWARD:
7177 case JUMP_IF_FALSE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00007178 if (inst->i_lineno == target->i_lineno &&
7179 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00007180 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007181 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007182 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007183 break;
7184 case JUMP_IF_TRUE_OR_POP:
7185 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00007186 if (inst->i_lineno == target->i_lineno) {
7187 inst->i_opcode = POP_JUMP_IF_FALSE;
7188 inst->i_target = inst->i_target->b_next;
7189 --i;
7190 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007191 break;
7192 }
7193 break;
7194
7195 case JUMP_IF_TRUE_OR_POP:
7196 switch(target->i_opcode) {
7197 case POP_JUMP_IF_TRUE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007198 if (inst->i_lineno == target->i_lineno) {
7199 *inst = *target;
7200 i--;
7201 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007202 break;
7203 case JUMP_ABSOLUTE:
7204 case JUMP_FORWARD:
7205 case JUMP_IF_TRUE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00007206 if (inst->i_lineno == target->i_lineno &&
7207 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00007208 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007209 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007210 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007211 break;
7212 case JUMP_IF_FALSE_OR_POP:
7213 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00007214 if (inst->i_lineno == target->i_lineno) {
7215 inst->i_opcode = POP_JUMP_IF_TRUE;
7216 inst->i_target = inst->i_target->b_next;
7217 --i;
7218 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007219 break;
7220 }
7221 break;
7222
7223 case POP_JUMP_IF_FALSE:
7224 switch(target->i_opcode) {
7225 case JUMP_ABSOLUTE:
7226 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007227 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00007228 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007229 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007230 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007231 break;
7232 }
7233 break;
7234
7235 case POP_JUMP_IF_TRUE:
7236 switch(target->i_opcode) {
7237 case JUMP_ABSOLUTE:
7238 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007239 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00007240 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007241 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007242 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007243 break;
7244 }
7245 break;
7246
7247 case JUMP_ABSOLUTE:
7248 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007249 assert (i == bb->b_iused-1);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007250 switch(target->i_opcode) {
7251 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007252 if (eliminate_jump_to_jump(bb, inst->i_opcode)) {
7253 goto error;
Mark Shannon266b4622020-11-17 19:30:14 +00007254 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007255 break;
Mark Shannon28b75c82020-12-23 11:43:10 +00007256
Mark Shannon6e8128f2020-07-30 10:03:00 +01007257 case JUMP_ABSOLUTE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007258 if (eliminate_jump_to_jump(bb, JUMP_ABSOLUTE)) {
7259 goto error;
Mark Shannon266b4622020-11-17 19:30:14 +00007260 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007261 break;
Mark Shannon28b75c82020-12-23 11:43:10 +00007262 default:
7263 if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) {
7264 basicblock *to_copy = inst->i_target;
7265 inst->i_opcode = NOP;
7266 for (i = 0; i < to_copy->b_iused; i++) {
7267 int index = compiler_next_instr(bb);
7268 if (index < 0) {
7269 return -1;
7270 }
7271 bb->b_instr[index] = to_copy->b_instr[i];
7272 }
7273 bb->b_exit = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007274 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007275 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007276 }
7277 }
7278 return 0;
7279error:
7280 return -1;
7281}
7282
7283
7284static void
Mark Shannon1659ad12021-01-13 15:05:04 +00007285clean_basic_block(basicblock *bb, int prev_lineno) {
7286 /* Remove NOPs when legal to do so. */
Mark Shannon6e8128f2020-07-30 10:03:00 +01007287 int dest = 0;
7288 for (int src = 0; src < bb->b_iused; src++) {
Mark Shannon877df852020-11-12 09:43:29 +00007289 int lineno = bb->b_instr[src].i_lineno;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007290 if (bb->b_instr[src].i_opcode == NOP) {
Mark Shannon266b4622020-11-17 19:30:14 +00007291 /* Eliminate no-op if it doesn't have a line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007292 if (lineno < 0) {
7293 continue;
7294 }
Mark Shannon266b4622020-11-17 19:30:14 +00007295 /* or, if the previous instruction had the same line number. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007296 if (prev_lineno == lineno) {
7297 continue;
7298 }
Mark Shannon266b4622020-11-17 19:30:14 +00007299 /* or, if the next instruction has same line number or no line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007300 if (src < bb->b_iused - 1) {
7301 int next_lineno = bb->b_instr[src+1].i_lineno;
7302 if (next_lineno < 0 || next_lineno == lineno) {
7303 bb->b_instr[src+1].i_lineno = lineno;
7304 continue;
Mark Shannon877df852020-11-12 09:43:29 +00007305 }
7306 }
Mark Shannon266b4622020-11-17 19:30:14 +00007307 else {
7308 basicblock* next = bb->b_next;
7309 while (next && next->b_iused == 0) {
7310 next = next->b_next;
7311 }
7312 /* or if last instruction in BB and next BB has same line number */
7313 if (next) {
7314 if (lineno == next->b_instr[0].i_lineno) {
7315 continue;
7316 }
7317 }
7318 }
7319
Mark Shannon6e8128f2020-07-30 10:03:00 +01007320 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007321 if (dest != src) {
7322 bb->b_instr[dest] = bb->b_instr[src];
7323 }
7324 dest++;
7325 prev_lineno = lineno;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007326 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007327 assert(dest <= bb->b_iused);
7328 bb->b_iused = dest;
7329}
7330
Mark Shannon266b4622020-11-17 19:30:14 +00007331static int
7332normalize_basic_block(basicblock *bb) {
7333 /* Mark blocks as exit and/or nofallthrough.
7334 Raise SystemError if CFG is malformed. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007335 for (int i = 0; i < bb->b_iused; i++) {
7336 switch(bb->b_instr[i].i_opcode) {
7337 case RETURN_VALUE:
7338 case RAISE_VARARGS:
7339 case RERAISE:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007340 bb->b_exit = 1;
Mark Shannon5977a792020-12-02 13:31:40 +00007341 bb->b_nofallthrough = 1;
7342 break;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007343 case JUMP_ABSOLUTE:
7344 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007345 bb->b_nofallthrough = 1;
Mark Shannon266b4622020-11-17 19:30:14 +00007346 /* fall through */
7347 case POP_JUMP_IF_FALSE:
7348 case POP_JUMP_IF_TRUE:
7349 case JUMP_IF_FALSE_OR_POP:
7350 case JUMP_IF_TRUE_OR_POP:
Mark Shannon5977a792020-12-02 13:31:40 +00007351 case FOR_ITER:
Mark Shannon266b4622020-11-17 19:30:14 +00007352 if (i != bb->b_iused-1) {
7353 PyErr_SetString(PyExc_SystemError, "malformed control flow graph.");
7354 return -1;
7355 }
Mark Shannon5977a792020-12-02 13:31:40 +00007356 /* Skip over empty basic blocks. */
7357 while (bb->b_instr[i].i_target->b_iused == 0) {
7358 bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next;
7359 }
7360
Mark Shannoncc75ab72020-11-12 19:49:33 +00007361 }
7362 }
Mark Shannon266b4622020-11-17 19:30:14 +00007363 return 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007364}
7365
Mark Shannon6e8128f2020-07-30 10:03:00 +01007366static int
7367mark_reachable(struct assembler *a) {
7368 basicblock **stack, **sp;
7369 sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks);
7370 if (stack == NULL) {
7371 return -1;
7372 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007373 a->a_entry->b_predecessors = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007374 *sp++ = a->a_entry;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007375 while (sp > stack) {
7376 basicblock *b = *(--sp);
Mark Shannon3bd60352021-01-13 12:05:43 +00007377 if (b->b_next && !b->b_nofallthrough) {
7378 if (b->b_next->b_predecessors == 0) {
7379 *sp++ = b->b_next;
7380 }
7381 b->b_next->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007382 }
7383 for (int i = 0; i < b->b_iused; i++) {
7384 basicblock *target;
Mark Shannon582aaf12020-08-04 17:30:11 +01007385 if (is_jump(&b->b_instr[i])) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007386 target = b->b_instr[i].i_target;
Mark Shannon3bd60352021-01-13 12:05:43 +00007387 if (target->b_predecessors == 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007388 *sp++ = target;
7389 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007390 target->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007391 }
7392 }
7393 }
7394 PyObject_Free(stack);
7395 return 0;
7396}
7397
Mark Shannon3bd60352021-01-13 12:05:43 +00007398static void
7399eliminate_empty_basic_blocks(basicblock *entry) {
7400 /* Eliminate empty blocks */
7401 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7402 basicblock *next = b->b_next;
7403 if (next) {
7404 while (next->b_iused == 0 && next->b_next) {
7405 next = next->b_next;
7406 }
7407 b->b_next = next;
7408 }
7409 }
7410 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7411 if (b->b_iused == 0) {
7412 continue;
7413 }
7414 if (is_jump(&b->b_instr[b->b_iused-1])) {
7415 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7416 while (target->b_iused == 0) {
7417 target = target->b_next;
7418 }
7419 b->b_instr[b->b_iused-1].i_target = target;
7420 }
7421 }
7422}
7423
7424
Mark Shannon5977a792020-12-02 13:31:40 +00007425/* If an instruction has no line number, but it's predecessor in the BB does,
Mark Shannon3bd60352021-01-13 12:05:43 +00007426 * then copy the line number. If a successor block has no line number, and only
7427 * one predecessor, then inherit the line number.
7428 * This ensures that all exit blocks (with one predecessor) receive a line number.
7429 * Also reduces the size of the line number table,
Mark Shannon5977a792020-12-02 13:31:40 +00007430 * but has no impact on the generated line number events.
7431 */
7432static void
Mark Shannon3bd60352021-01-13 12:05:43 +00007433propogate_line_numbers(struct assembler *a) {
Mark Shannon5977a792020-12-02 13:31:40 +00007434 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007435 if (b->b_iused == 0) {
7436 continue;
7437 }
Mark Shannon5977a792020-12-02 13:31:40 +00007438 int prev_lineno = -1;
7439 for (int i = 0; i < b->b_iused; i++) {
7440 if (b->b_instr[i].i_lineno < 0) {
7441 b->b_instr[i].i_lineno = prev_lineno;
7442 }
7443 else {
7444 prev_lineno = b->b_instr[i].i_lineno;
7445 }
7446 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007447 if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) {
7448 assert(b->b_next->b_iused);
7449 if (b->b_next->b_instr[0].i_lineno < 0) {
7450 b->b_next->b_instr[0].i_lineno = prev_lineno;
7451 }
7452 }
7453 if (is_jump(&b->b_instr[b->b_iused-1])) {
7454 switch (b->b_instr[b->b_iused-1].i_opcode) {
7455 /* Note: Only actual jumps, not exception handlers */
7456 case SETUP_ASYNC_WITH:
7457 case SETUP_WITH:
7458 case SETUP_FINALLY:
7459 continue;
7460 }
7461 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7462 if (target->b_predecessors == 1) {
7463 if (target->b_instr[0].i_lineno < 0) {
7464 target->b_instr[0].i_lineno = prev_lineno;
7465 }
7466 }
7467 }
Mark Shannon5977a792020-12-02 13:31:40 +00007468 }
7469}
7470
7471/* Perform optimizations on a control flow graph.
Mark Shannon6e8128f2020-07-30 10:03:00 +01007472 The consts object should still be in list form to allow new constants
7473 to be appended.
7474
7475 All transformations keep the code size the same or smaller.
7476 For those that reduce size, the gaps are initially filled with
7477 NOPs. Later those NOPs are removed.
7478*/
7479
7480static int
Inada Naoki8a232c72021-04-16 14:01:04 +09007481optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts)
Mark Shannon6e8128f2020-07-30 10:03:00 +01007482{
Mark Shannoncc75ab72020-11-12 19:49:33 +00007483 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Inada Naoki8a232c72021-04-16 14:01:04 +09007484 if (optimize_basic_block(c, b, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007485 return -1;
7486 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007487 clean_basic_block(b, -1);
Mark Shannon3bd60352021-01-13 12:05:43 +00007488 assert(b->b_predecessors == 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007489 }
7490 if (mark_reachable(a)) {
7491 return -1;
7492 }
7493 /* Delete unreachable instructions */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007494 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007495 if (b->b_predecessors == 0) {
Mark Shannoncc75ab72020-11-12 19:49:33 +00007496 b->b_iused = 0;
Om Gc71581c2020-12-16 17:48:05 +05307497 b->b_nofallthrough = 0;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007498 }
7499 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007500 basicblock *pred = NULL;
7501 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7502 int prev_lineno = -1;
7503 if (pred && pred->b_iused) {
7504 prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno;
7505 }
7506 clean_basic_block(b, prev_lineno);
7507 pred = b->b_nofallthrough ? NULL : b;
7508 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007509 eliminate_empty_basic_blocks(a->a_entry);
Om Gc71581c2020-12-16 17:48:05 +05307510 /* Delete jump instructions made redundant by previous step. If a non-empty
7511 block ends with a jump instruction, check if the next non-empty block
7512 reached through normal flow control is the target of that jump. If it
7513 is, then the jump instruction is redundant and can be deleted.
7514 */
Mark Shannon3bd60352021-01-13 12:05:43 +00007515 int maybe_empty_blocks = 0;
Om Gc71581c2020-12-16 17:48:05 +05307516 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7517 if (b->b_iused > 0) {
7518 struct instr *b_last_instr = &b->b_instr[b->b_iused - 1];
Mark Shannon802b6452021-02-02 14:59:15 +00007519 if (b_last_instr->i_opcode == JUMP_ABSOLUTE ||
Om Gc71581c2020-12-16 17:48:05 +05307520 b_last_instr->i_opcode == JUMP_FORWARD) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007521 if (b_last_instr->i_target == b->b_next) {
7522 assert(b->b_next->b_iused);
Om Gc71581c2020-12-16 17:48:05 +05307523 b->b_nofallthrough = 0;
Mark Shannon802b6452021-02-02 14:59:15 +00007524 b_last_instr->i_opcode = NOP;
7525 clean_basic_block(b, -1);
7526 maybe_empty_blocks = 1;
Om Gc71581c2020-12-16 17:48:05 +05307527 }
7528 }
7529 }
7530 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007531 if (maybe_empty_blocks) {
7532 eliminate_empty_basic_blocks(a->a_entry);
7533 }
7534 propogate_line_numbers(a);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007535 return 0;
7536}
7537
Mark Shannon5977a792020-12-02 13:31:40 +00007538static inline int
7539is_exit_without_lineno(basicblock *b) {
7540 return b->b_exit && b->b_instr[0].i_lineno < 0;
7541}
7542
7543/* PEP 626 mandates that the f_lineno of a frame is correct
7544 * after a frame terminates. It would be prohibitively expensive
7545 * to continuously update the f_lineno field at runtime,
7546 * so we make sure that all exiting instruction (raises and returns)
7547 * have a valid line number, allowing us to compute f_lineno lazily.
7548 * We can do this by duplicating the exit blocks without line number
7549 * so that none have more than one predecessor. We can then safely
7550 * copy the line number from the sole predecessor block.
7551 */
7552static int
7553ensure_exits_have_lineno(struct compiler *c)
7554{
Mark Shannoneaccc122020-12-04 15:22:12 +00007555 basicblock *entry = NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00007556 /* Copy all exit blocks without line number that are targets of a jump.
7557 */
7558 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7559 if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) {
7560 switch (b->b_instr[b->b_iused-1].i_opcode) {
7561 /* Note: Only actual jumps, not exception handlers */
7562 case SETUP_ASYNC_WITH:
7563 case SETUP_WITH:
7564 case SETUP_FINALLY:
7565 continue;
7566 }
7567 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7568 if (is_exit_without_lineno(target)) {
7569 basicblock *new_target = compiler_copy_block(c, target);
7570 if (new_target == NULL) {
7571 return -1;
7572 }
7573 new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7574 b->b_instr[b->b_iused-1].i_target = new_target;
7575 }
7576 }
Mark Shannoneaccc122020-12-04 15:22:12 +00007577 entry = b;
7578 }
7579 assert(entry != NULL);
7580 if (is_exit_without_lineno(entry)) {
7581 entry->b_instr[0].i_lineno = c->u->u_firstlineno;
Mark Shannon5977a792020-12-02 13:31:40 +00007582 }
Mark Shannonee9f98d2021-01-05 12:04:10 +00007583 /* Eliminate empty blocks */
7584 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7585 while (b->b_next && b->b_next->b_iused == 0) {
7586 b->b_next = b->b_next->b_next;
7587 }
7588 }
Mark Shannon5977a792020-12-02 13:31:40 +00007589 /* Any remaining reachable exit blocks without line number can only be reached by
7590 * fall through, and thus can only have a single predecessor */
7591 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7592 if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) {
7593 if (is_exit_without_lineno(b->b_next)) {
7594 assert(b->b_next->b_iused > 0);
7595 b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7596 }
7597 }
7598 }
7599 return 0;
7600}
7601
7602
Mark Shannon6e8128f2020-07-30 10:03:00 +01007603/* Retained for API compatibility.
7604 * Optimization is now done in optimize_cfg */
7605
7606PyObject *
7607PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
7608 PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
7609{
7610 Py_INCREF(code);
7611 return code;
7612}