blob: 496b4b0371f1df375d57582b1fbf7aedb9a38ed6 [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 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200};
201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202/* This struct captures the global state of a compilation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000203
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000204The u pointer points to the current compilation unit, while units
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205for enclosing blocks are stored in c_stack. The u and c_stack are
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000206managed by compiler_enter_scope() and compiler_exit_scope().
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000207
208Note that we don't track recursion levels during compilation - the
209task of detecting and rejecting excessive levels of nesting is
210handled by the symbol analysis pass.
211
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000212*/
213
214struct compiler {
Victor Stinner14e461d2013-08-26 22:28:21 +0200215 PyObject *c_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 struct symtable *c_st;
217 PyFutureFeatures *c_future; /* pointer to module's __future__ */
218 PyCompilerFlags *c_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000219
Georg Brandl8334fd92010-12-04 10:26:46 +0000220 int c_optimize; /* optimization level */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 int c_interactive; /* true if in interactive mode */
222 int c_nestlevel;
INADA Naokic2e16072018-11-26 21:23:22 +0900223 PyObject *c_const_cache; /* Python dict holding all constants,
224 including names tuple */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 struct compiler_unit *u; /* compiler state for current block */
226 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
227 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000228};
229
Brandt Bucher145bf262021-02-26 14:51:55 -0800230typedef struct {
231 PyObject *stores;
232 int allow_irrefutable;
233} pattern_context;
234
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100235static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000236static void compiler_free(struct compiler *);
237static basicblock *compiler_new_block(struct compiler *);
Andy Lester76d58772020-03-10 21:18:12 -0500238static int compiler_next_instr(basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000239static int compiler_addop(struct compiler *, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +0100240static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
Mark Shannon582aaf12020-08-04 17:30:11 +0100241static int compiler_addop_j(struct compiler *, int, basicblock *);
Mark Shannon127dde52021-01-04 18:06:55 +0000242static int compiler_addop_j_noline(struct compiler *, int, basicblock *);
Brandt Bucher145bf262021-02-26 14:51:55 -0800243static int compiler_error(struct compiler *, const char *, ...);
Serhiy Storchaka62e44812019-02-16 08:12:19 +0200244static int compiler_warn(struct compiler *, const char *, ...);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000245static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
246
247static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
248static int compiler_visit_stmt(struct compiler *, stmt_ty);
249static int compiler_visit_keyword(struct compiler *, keyword_ty);
250static int compiler_visit_expr(struct compiler *, expr_ty);
251static int compiler_augassign(struct compiler *, stmt_ty);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700252static int compiler_annassign(struct compiler *, stmt_ty);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +0200253static int compiler_subscript(struct compiler *, expr_ty);
254static int compiler_slice(struct compiler *, expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000255
Andy Lester76d58772020-03-10 21:18:12 -0500256static int inplace_binop(operator_ty);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100257static int are_all_items_const(asdl_expr_seq *, Py_ssize_t, Py_ssize_t);
Mark Shannon8473cf82020-12-15 11:07:50 +0000258
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000259
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500260static int compiler_with(struct compiler *, stmt_ty, int);
Yury Selivanov75445082015-05-11 22:57:16 -0400261static int compiler_async_with(struct compiler *, stmt_ty, int);
262static int compiler_async_for(struct compiler *, stmt_ty);
Victor Stinner976bb402016-03-23 11:36:19 +0100263static int compiler_call_helper(struct compiler *c, int n,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100264 asdl_expr_seq *args,
265 asdl_keyword_seq *keywords);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500266static int compiler_try_except(struct compiler *, stmt_ty);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400267static int compiler_set_qualname(struct compiler *);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000268
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700269static int compiler_sync_comprehension_generator(
270 struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100271 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +0200272 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700273 expr_ty elt, expr_ty val, int type);
274
275static int compiler_async_comprehension_generator(
276 struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100277 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +0200278 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700279 expr_ty elt, expr_ty val, int type);
280
Brandt Bucher145bf262021-02-26 14:51:55 -0800281static int compiler_pattern(struct compiler *, expr_ty, pattern_context *);
282static int compiler_match(struct compiler *, stmt_ty);
283static int compiler_pattern_subpattern(struct compiler *, expr_ty,
284 pattern_context *);
285
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000286static PyCodeObject *assemble(struct compiler *, int addNone);
Mark Shannon332cd5e2018-01-30 00:41:04 +0000287static PyObject *__doc__, *__annotations__;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000288
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400289#define CAPSULE_NAME "compile.c compiler unit"
Benjamin Petersonb173f782009-05-05 22:31:58 +0000290
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000291PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000292_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 /* Name mangling: __private becomes _classname__private.
295 This is independent from how the name is used. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200296 PyObject *result;
297 size_t nlen, plen, ipriv;
298 Py_UCS4 maxchar;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200300 PyUnicode_READ_CHAR(ident, 0) != '_' ||
301 PyUnicode_READ_CHAR(ident, 1) != '_') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 Py_INCREF(ident);
303 return ident;
304 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200305 nlen = PyUnicode_GET_LENGTH(ident);
306 plen = PyUnicode_GET_LENGTH(privateobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 /* Don't mangle __id__ or names with dots.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 The only time a name with a dot can occur is when
310 we are compiling an import statement that has a
311 package name.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 TODO(jhylton): Decide whether we want to support
314 mangling of the module name, e.g. __M.X.
315 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200316 if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
317 PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
318 PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 Py_INCREF(ident);
320 return ident; /* Don't mangle __whatever__ */
321 }
322 /* Strip leading underscores from class name */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200323 ipriv = 0;
324 while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
325 ipriv++;
326 if (ipriv == plen) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 Py_INCREF(ident);
328 return ident; /* Don't mangle if class is just underscores */
329 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200330 plen -= ipriv;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000331
Antoine Pitrou55bff892013-04-06 21:21:04 +0200332 if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
333 PyErr_SetString(PyExc_OverflowError,
334 "private identifier too large to be mangled");
335 return NULL;
336 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000337
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200338 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
339 if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
340 maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
341
342 result = PyUnicode_New(1 + nlen + plen, maxchar);
343 if (!result)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200345 /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
346 PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
Victor Stinner6c7a52a2011-09-28 21:39:17 +0200347 if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
348 Py_DECREF(result);
349 return NULL;
350 }
351 if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
352 Py_DECREF(result);
353 return NULL;
354 }
Victor Stinner8f825062012-04-27 13:55:39 +0200355 assert(_PyUnicode_CheckConsistency(result, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200356 return result;
Michael W. Hudson60934622004-08-12 17:56:29 +0000357}
358
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000359static int
360compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000363
INADA Naokic2e16072018-11-26 21:23:22 +0900364 c->c_const_cache = PyDict_New();
365 if (!c->c_const_cache) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 return 0;
INADA Naokic2e16072018-11-26 21:23:22 +0900367 }
368
369 c->c_stack = PyList_New(0);
370 if (!c->c_stack) {
371 Py_CLEAR(c->c_const_cache);
372 return 0;
373 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000376}
377
378PyCodeObject *
Victor Stinnera81fca62021-03-24 00:51:50 +0100379_PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
380 int optimize, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 struct compiler c;
383 PyCodeObject *co = NULL;
Victor Stinner37d66d72019-06-13 02:16:41 +0200384 PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 if (!__doc__) {
388 __doc__ = PyUnicode_InternFromString("__doc__");
389 if (!__doc__)
390 return NULL;
391 }
Mark Shannon332cd5e2018-01-30 00:41:04 +0000392 if (!__annotations__) {
393 __annotations__ = PyUnicode_InternFromString("__annotations__");
394 if (!__annotations__)
395 return NULL;
396 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 if (!compiler_init(&c))
398 return NULL;
Victor Stinner14e461d2013-08-26 22:28:21 +0200399 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 c.c_filename = filename;
401 c.c_arena = arena;
Victor Stinnera81fca62021-03-24 00:51:50 +0100402 c.c_future = _PyFuture_FromAST(mod, filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 if (c.c_future == NULL)
404 goto finally;
405 if (!flags) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 flags = &local_flags;
407 }
408 merged = c.c_future->ff_features | flags->cf_flags;
409 c.c_future->ff_features = merged;
410 flags->cf_flags = merged;
411 c.c_flags = flags;
Victor Stinnerda7933e2020-04-13 03:04:28 +0200412 c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000414
Pablo Galindod112c602020-03-18 23:02:09 +0000415 _PyASTOptimizeState state;
416 state.optimize = c.c_optimize;
417 state.ff_features = merged;
418
419 if (!_PyAST_Optimize(mod, arena, &state)) {
INADA Naoki7ea143a2017-12-14 16:47:20 +0900420 goto finally;
421 }
422
Victor Stinner28ad12f2021-03-19 12:41:49 +0100423 c.c_st = _PySymtable_Build(mod, filename, c.c_future);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 if (c.c_st == NULL) {
425 if (!PyErr_Occurred())
426 PyErr_SetString(PyExc_SystemError, "no symtable");
427 goto finally;
428 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000431
Thomas Wouters1175c432006-02-27 22:49:54 +0000432 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 compiler_free(&c);
434 assert(co || PyErr_Occurred());
435 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000436}
437
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000438static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000439compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 if (c->c_st)
Victor Stinner28ad12f2021-03-19 12:41:49 +0100442 _PySymtable_Free(c->c_st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 if (c->c_future)
444 PyObject_Free(c->c_future);
Victor Stinner14e461d2013-08-26 22:28:21 +0200445 Py_XDECREF(c->c_filename);
INADA Naokic2e16072018-11-26 21:23:22 +0900446 Py_DECREF(c->c_const_cache);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000448}
449
Guido van Rossum79f25d91997-04-29 20:08:16 +0000450static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000451list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 Py_ssize_t i, n;
454 PyObject *v, *k;
455 PyObject *dict = PyDict_New();
456 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 n = PyList_Size(list);
459 for (i = 0; i < n; i++) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100460 v = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 if (!v) {
462 Py_DECREF(dict);
463 return NULL;
464 }
465 k = PyList_GET_ITEM(list, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300466 if (PyDict_SetItem(dict, k, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 Py_DECREF(v);
468 Py_DECREF(dict);
469 return NULL;
470 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 Py_DECREF(v);
472 }
473 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000474}
475
476/* Return new dict containing names from src that match scope(s).
477
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000478src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000480values are integers, starting at offset and increasing by one for
481each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482*/
483
484static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +0100485dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000486{
Benjamin Peterson51ab2832012-07-18 15:12:47 -0700487 Py_ssize_t i = offset, scope, num_keys, key_i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 PyObject *k, *v, *dest = PyDict_New();
Meador Inge2ca63152012-07-18 14:20:11 -0500489 PyObject *sorted_keys;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 assert(offset >= 0);
492 if (dest == NULL)
493 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000494
Meador Inge2ca63152012-07-18 14:20:11 -0500495 /* Sort the keys so that we have a deterministic order on the indexes
496 saved in the returned dictionary. These indexes are used as indexes
497 into the free and cell var storage. Therefore if they aren't
498 deterministic, then the generated bytecode is not deterministic.
499 */
500 sorted_keys = PyDict_Keys(src);
501 if (sorted_keys == NULL)
502 return NULL;
503 if (PyList_Sort(sorted_keys) != 0) {
504 Py_DECREF(sorted_keys);
505 return NULL;
506 }
Meador Ingef69e24e2012-07-18 16:41:03 -0500507 num_keys = PyList_GET_SIZE(sorted_keys);
Meador Inge2ca63152012-07-18 14:20:11 -0500508
509 for (key_i = 0; key_i < num_keys; key_i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 /* XXX this should probably be a macro in symtable.h */
511 long vi;
Meador Inge2ca63152012-07-18 14:20:11 -0500512 k = PyList_GET_ITEM(sorted_keys, key_i);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200513 v = PyDict_GetItemWithError(src, k);
514 assert(v && PyLong_Check(v));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 vi = PyLong_AS_LONG(v);
516 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 if (scope == scope_type || vi & flag) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300519 PyObject *item = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 if (item == NULL) {
Meador Inge2ca63152012-07-18 14:20:11 -0500521 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 Py_DECREF(dest);
523 return NULL;
524 }
525 i++;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300526 if (PyDict_SetItem(dest, k, item) < 0) {
Meador Inge2ca63152012-07-18 14:20:11 -0500527 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 Py_DECREF(item);
529 Py_DECREF(dest);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 return NULL;
531 }
532 Py_DECREF(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 }
534 }
Meador Inge2ca63152012-07-18 14:20:11 -0500535 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000537}
538
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000539static void
540compiler_unit_check(struct compiler_unit *u)
541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 basicblock *block;
543 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +0100544 assert(!_PyMem_IsPtrFreed(block));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 if (block->b_instr != NULL) {
546 assert(block->b_ialloc > 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +0100547 assert(block->b_iused >= 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 assert(block->b_ialloc >= block->b_iused);
549 }
550 else {
551 assert (block->b_iused == 0);
552 assert (block->b_ialloc == 0);
553 }
554 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000555}
556
557static void
558compiler_unit_free(struct compiler_unit *u)
559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 compiler_unit_check(u);
563 b = u->u_blocks;
564 while (b != NULL) {
565 if (b->b_instr)
566 PyObject_Free((void *)b->b_instr);
567 next = b->b_list;
568 PyObject_Free((void *)b);
569 b = next;
570 }
571 Py_CLEAR(u->u_ste);
572 Py_CLEAR(u->u_name);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400573 Py_CLEAR(u->u_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 Py_CLEAR(u->u_consts);
575 Py_CLEAR(u->u_names);
576 Py_CLEAR(u->u_varnames);
577 Py_CLEAR(u->u_freevars);
578 Py_CLEAR(u->u_cellvars);
579 Py_CLEAR(u->u_private);
580 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581}
582
583static int
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100584compiler_enter_scope(struct compiler *c, identifier name,
585 int scope_type, void *key, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 struct compiler_unit *u;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100588 basicblock *block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000589
Andy Lester7668a8b2020-03-24 23:26:44 -0500590 u = (struct compiler_unit *)PyObject_Calloc(1, sizeof(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 struct compiler_unit));
592 if (!u) {
593 PyErr_NoMemory();
594 return 0;
595 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100596 u->u_scope_type = scope_type;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 u->u_argcount = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100598 u->u_posonlyargcount = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 u->u_kwonlyargcount = 0;
600 u->u_ste = PySymtable_Lookup(c->c_st, key);
601 if (!u->u_ste) {
602 compiler_unit_free(u);
603 return 0;
604 }
605 Py_INCREF(name);
606 u->u_name = name;
607 u->u_varnames = list2dict(u->u_ste->ste_varnames);
608 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
609 if (!u->u_varnames || !u->u_cellvars) {
610 compiler_unit_free(u);
611 return 0;
612 }
Benjamin Peterson312595c2013-05-15 15:26:42 -0500613 if (u->u_ste->ste_needs_class_closure) {
Martin Panter7462b6492015-11-02 03:37:02 +0000614 /* Cook up an implicit __class__ cell. */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500615 _Py_IDENTIFIER(__class__);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300616 PyObject *name;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500617 int res;
618 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200619 assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500620 name = _PyUnicode_FromId(&PyId___class__);
621 if (!name) {
622 compiler_unit_free(u);
623 return 0;
624 }
Victor Stinnerc9bc2902020-10-27 02:24:34 +0100625 res = PyDict_SetItem(u->u_cellvars, name, _PyLong_GetZero());
Benjamin Peterson312595c2013-05-15 15:26:42 -0500626 if (res < 0) {
627 compiler_unit_free(u);
628 return 0;
629 }
630 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200633 PyDict_GET_SIZE(u->u_cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 if (!u->u_freevars) {
635 compiler_unit_free(u);
636 return 0;
637 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 u->u_blocks = NULL;
640 u->u_nfblocks = 0;
641 u->u_firstlineno = lineno;
642 u->u_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000643 u->u_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 u->u_consts = PyDict_New();
645 if (!u->u_consts) {
646 compiler_unit_free(u);
647 return 0;
648 }
649 u->u_names = PyDict_New();
650 if (!u->u_names) {
651 compiler_unit_free(u);
652 return 0;
653 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 /* Push the old compiler_unit on the stack. */
658 if (c->u) {
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400659 PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
661 Py_XDECREF(capsule);
662 compiler_unit_free(u);
663 return 0;
664 }
665 Py_DECREF(capsule);
666 u->u_private = c->u->u_private;
667 Py_XINCREF(u->u_private);
668 }
669 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 c->c_nestlevel++;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100672
673 block = compiler_new_block(c);
674 if (block == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 return 0;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100676 c->u->u_curblock = block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400678 if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
679 if (!compiler_set_qualname(c))
680 return 0;
681 }
682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000684}
685
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000686static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000687compiler_exit_scope(struct compiler *c)
688{
Victor Stinnera6192632021-01-29 16:53:03 +0100689 // Don't call PySequence_DelItem() with an exception raised
690 PyObject *exc_type, *exc_val, *exc_tb;
691 PyErr_Fetch(&exc_type, &exc_val, &exc_tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 c->c_nestlevel--;
694 compiler_unit_free(c->u);
695 /* Restore c->u to the parent unit. */
Victor Stinnera6192632021-01-29 16:53:03 +0100696 Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 if (n >= 0) {
Victor Stinnera6192632021-01-29 16:53:03 +0100698 PyObject *capsule = PyList_GET_ITEM(c->c_stack, n);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400699 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 assert(c->u);
701 /* we are deleting from a list so this really shouldn't fail */
Victor Stinnera6192632021-01-29 16:53:03 +0100702 if (PySequence_DelItem(c->c_stack, n) < 0) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +0100703 _PyErr_WriteUnraisableMsg("on removing the last compiler "
704 "stack item", NULL);
Victor Stinnera6192632021-01-29 16:53:03 +0100705 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 compiler_unit_check(c->u);
707 }
Victor Stinnera6192632021-01-29 16:53:03 +0100708 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 c->u = NULL;
Victor Stinnera6192632021-01-29 16:53:03 +0100710 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000711
Victor Stinnera6192632021-01-29 16:53:03 +0100712 PyErr_Restore(exc_type, exc_val, exc_tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000713}
714
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400715static int
716compiler_set_qualname(struct compiler *c)
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100717{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100718 _Py_static_string(dot, ".");
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400719 _Py_static_string(dot_locals, ".<locals>");
720 Py_ssize_t stack_size;
721 struct compiler_unit *u = c->u;
722 PyObject *name, *base, *dot_str, *dot_locals_str;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100723
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400724 base = NULL;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100725 stack_size = PyList_GET_SIZE(c->c_stack);
Benjamin Petersona8a38b82013-10-19 16:14:39 -0400726 assert(stack_size >= 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400727 if (stack_size > 1) {
728 int scope, force_global = 0;
729 struct compiler_unit *parent;
730 PyObject *mangled, *capsule;
731
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400732 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400733 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400734 assert(parent);
735
Yury Selivanov75445082015-05-11 22:57:16 -0400736 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
737 || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
738 || u->u_scope_type == COMPILER_SCOPE_CLASS) {
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400739 assert(u->u_name);
740 mangled = _Py_Mangle(parent->u_private, u->u_name);
741 if (!mangled)
742 return 0;
Victor Stinner28ad12f2021-03-19 12:41:49 +0100743 scope = _PyST_GetScope(parent->u_ste, mangled);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400744 Py_DECREF(mangled);
745 assert(scope != GLOBAL_IMPLICIT);
746 if (scope == GLOBAL_EXPLICIT)
747 force_global = 1;
748 }
749
750 if (!force_global) {
751 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
Yury Selivanov75445082015-05-11 22:57:16 -0400752 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400753 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
754 dot_locals_str = _PyUnicode_FromId(&dot_locals);
755 if (dot_locals_str == NULL)
756 return 0;
757 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
758 if (base == NULL)
759 return 0;
760 }
761 else {
762 Py_INCREF(parent->u_qualname);
763 base = parent->u_qualname;
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400764 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100765 }
766 }
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400767
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400768 if (base != NULL) {
769 dot_str = _PyUnicode_FromId(&dot);
770 if (dot_str == NULL) {
771 Py_DECREF(base);
772 return 0;
773 }
774 name = PyUnicode_Concat(base, dot_str);
775 Py_DECREF(base);
776 if (name == NULL)
777 return 0;
778 PyUnicode_Append(&name, u->u_name);
779 if (name == NULL)
780 return 0;
781 }
782 else {
783 Py_INCREF(u->u_name);
784 name = u->u_name;
785 }
786 u->u_qualname = name;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100787
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400788 return 1;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100789}
790
Eric V. Smith235a6f02015-09-19 14:51:32 -0400791
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792/* Allocate a new block and return a pointer to it.
793 Returns NULL on error.
794*/
795
796static basicblock *
797compiler_new_block(struct compiler *c)
798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 basicblock *b;
800 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 u = c->u;
Andy Lester7668a8b2020-03-24 23:26:44 -0500803 b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 if (b == NULL) {
805 PyErr_NoMemory();
806 return NULL;
807 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 /* Extend the singly linked list of blocks with new block. */
809 b->b_list = u->u_blocks;
810 u->u_blocks = b;
811 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812}
813
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814static basicblock *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815compiler_next_block(struct compiler *c)
816{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 basicblock *block = compiler_new_block(c);
818 if (block == NULL)
819 return NULL;
820 c->u->u_curblock->b_next = block;
821 c->u->u_curblock = block;
822 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000823}
824
825static basicblock *
826compiler_use_next_block(struct compiler *c, basicblock *block)
827{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 assert(block != NULL);
829 c->u->u_curblock->b_next = block;
830 c->u->u_curblock = block;
831 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832}
833
Mark Shannon5977a792020-12-02 13:31:40 +0000834static basicblock *
835compiler_copy_block(struct compiler *c, basicblock *block)
836{
837 /* Cannot copy a block if it has a fallthrough, since
838 * a block can only have one fallthrough predecessor.
839 */
840 assert(block->b_nofallthrough);
841 basicblock *result = compiler_next_block(c);
842 if (result == NULL) {
843 return NULL;
844 }
845 for (int i = 0; i < block->b_iused; i++) {
846 int n = compiler_next_instr(result);
847 if (n < 0) {
848 return NULL;
849 }
850 result->b_instr[n] = block->b_instr[i];
851 }
852 result->b_exit = block->b_exit;
Mark Shannon3bd60352021-01-13 12:05:43 +0000853 result->b_nofallthrough = 1;
Mark Shannon5977a792020-12-02 13:31:40 +0000854 return result;
855}
856
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857/* Returns the offset of the next instruction in the current block's
858 b_instr array. Resizes the b_instr as necessary.
859 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000860*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861
862static int
Andy Lester76d58772020-03-10 21:18:12 -0500863compiler_next_instr(basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 assert(b != NULL);
866 if (b->b_instr == NULL) {
Andy Lester7668a8b2020-03-24 23:26:44 -0500867 b->b_instr = (struct instr *)PyObject_Calloc(
868 DEFAULT_BLOCK_SIZE, sizeof(struct instr));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 if (b->b_instr == NULL) {
870 PyErr_NoMemory();
871 return -1;
872 }
873 b->b_ialloc = DEFAULT_BLOCK_SIZE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 }
875 else if (b->b_iused == b->b_ialloc) {
876 struct instr *tmp;
877 size_t oldsize, newsize;
878 oldsize = b->b_ialloc * sizeof(struct instr);
879 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000880
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -0700881 if (oldsize > (SIZE_MAX >> 1)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 PyErr_NoMemory();
883 return -1;
884 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 if (newsize == 0) {
887 PyErr_NoMemory();
888 return -1;
889 }
890 b->b_ialloc <<= 1;
891 tmp = (struct instr *)PyObject_Realloc(
892 (void *)b->b_instr, newsize);
893 if (tmp == NULL) {
894 PyErr_NoMemory();
895 return -1;
896 }
897 b->b_instr = tmp;
898 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
899 }
900 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000901}
902
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200903/* Set the line number and column offset for the following instructions.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000904
Christian Heimes2202f872008-02-06 14:31:34 +0000905 The line number is reset in the following cases:
906 - when entering a new scope
907 - on each statement
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200908 - on each expression and sub-expression
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200909 - before the "except" and "finally" clauses
Thomas Wouters89f507f2006-12-13 04:49:30 +0000910*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000911
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200912#define SET_LOC(c, x) \
913 (c)->u->u_lineno = (x)->lineno; \
914 (c)->u->u_col_offset = (x)->col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000915
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200916/* Return the stack effect of opcode with argument oparg.
917
918 Some opcodes have different stack effect when jump to the target and
919 when not jump. The 'jump' parameter specifies the case:
920
921 * 0 -- when not jump
922 * 1 -- when jump
923 * -1 -- maximal
924 */
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200925static int
926stack_effect(int opcode, int oparg, int jump)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000927{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 switch (opcode) {
Serhiy Storchaka57faf342018-04-25 22:04:06 +0300929 case NOP:
930 case EXTENDED_ARG:
931 return 0;
932
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200933 /* Stack manipulation */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 case POP_TOP:
935 return -1;
936 case ROT_TWO:
937 case ROT_THREE:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200938 case ROT_FOUR:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 return 0;
940 case DUP_TOP:
941 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000942 case DUP_TOP_TWO:
943 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000944
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200945 /* Unary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 case UNARY_POSITIVE:
947 case UNARY_NEGATIVE:
948 case UNARY_NOT:
949 case UNARY_INVERT:
950 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 case SET_ADD:
953 case LIST_APPEND:
954 return -1;
955 case MAP_ADD:
956 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000957
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200958 /* Binary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 case BINARY_POWER:
960 case BINARY_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400961 case BINARY_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 case BINARY_MODULO:
963 case BINARY_ADD:
964 case BINARY_SUBTRACT:
965 case BINARY_SUBSCR:
966 case BINARY_FLOOR_DIVIDE:
967 case BINARY_TRUE_DIVIDE:
968 return -1;
969 case INPLACE_FLOOR_DIVIDE:
970 case INPLACE_TRUE_DIVIDE:
971 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 case INPLACE_ADD:
974 case INPLACE_SUBTRACT:
975 case INPLACE_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400976 case INPLACE_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 case INPLACE_MODULO:
978 return -1;
979 case STORE_SUBSCR:
980 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 case DELETE_SUBSCR:
982 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 case BINARY_LSHIFT:
985 case BINARY_RSHIFT:
986 case BINARY_AND:
987 case BINARY_XOR:
988 case BINARY_OR:
989 return -1;
990 case INPLACE_POWER:
991 return -1;
992 case GET_ITER:
993 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 case PRINT_EXPR:
996 return -1;
997 case LOAD_BUILD_CLASS:
998 return 1;
999 case INPLACE_LSHIFT:
1000 case INPLACE_RSHIFT:
1001 case INPLACE_AND:
1002 case INPLACE_XOR:
1003 case INPLACE_OR:
1004 return -1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 case SETUP_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001007 /* 1 in the normal flow.
1008 * Restore the stack position and push 6 values before jumping to
1009 * the handler if an exception be raised. */
1010 return jump ? 6 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 case RETURN_VALUE:
1012 return -1;
1013 case IMPORT_STAR:
1014 return -1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001015 case SETUP_ANNOTATIONS:
1016 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 case YIELD_VALUE:
1018 return 0;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001019 case YIELD_FROM:
1020 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 case POP_BLOCK:
1022 return 0;
1023 case POP_EXCEPT:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001024 return -3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 case STORE_NAME:
1027 return -1;
1028 case DELETE_NAME:
1029 return 0;
1030 case UNPACK_SEQUENCE:
1031 return oparg-1;
1032 case UNPACK_EX:
1033 return (oparg&0xFF) + (oparg>>8);
1034 case FOR_ITER:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001035 /* -1 at end of iterator, 1 if continue iterating. */
1036 return jump > 0 ? -1 : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 case STORE_ATTR:
1039 return -2;
1040 case DELETE_ATTR:
1041 return -1;
1042 case STORE_GLOBAL:
1043 return -1;
1044 case DELETE_GLOBAL:
1045 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 case LOAD_CONST:
1047 return 1;
1048 case LOAD_NAME:
1049 return 1;
1050 case BUILD_TUPLE:
1051 case BUILD_LIST:
1052 case BUILD_SET:
Serhiy Storchakaea525a22016-09-06 22:07:53 +03001053 case BUILD_STRING:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 return 1-oparg;
1055 case BUILD_MAP:
Benjamin Petersonb6855152015-09-10 21:02:39 -07001056 return 1 - 2*oparg;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001057 case BUILD_CONST_KEY_MAP:
1058 return -oparg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 case LOAD_ATTR:
1060 return 0;
1061 case COMPARE_OP:
Mark Shannon9af0e472020-01-14 10:12:45 +00001062 case IS_OP:
1063 case CONTAINS_OP:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 return -1;
Mark Shannon9af0e472020-01-14 10:12:45 +00001065 case JUMP_IF_NOT_EXC_MATCH:
1066 return -2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 case IMPORT_NAME:
1068 return -1;
1069 case IMPORT_FROM:
1070 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001072 /* Jumps */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 case JUMP_FORWARD:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 case JUMP_ABSOLUTE:
1075 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001077 case JUMP_IF_TRUE_OR_POP:
1078 case JUMP_IF_FALSE_OR_POP:
1079 return jump ? 0 : -1;
1080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 case POP_JUMP_IF_FALSE:
1082 case POP_JUMP_IF_TRUE:
1083 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 case LOAD_GLOBAL:
1086 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001088 /* Exception handling */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 case SETUP_FINALLY:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001090 /* 0 in the normal flow.
1091 * Restore the stack position and push 6 values before jumping to
1092 * the handler if an exception be raised. */
1093 return jump ? 6 : 0;
Mark Shannonfee55262019-11-21 09:11:43 +00001094 case RERAISE:
1095 return -3;
1096
1097 case WITH_EXCEPT_START:
1098 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 case LOAD_FAST:
1101 return 1;
1102 case STORE_FAST:
1103 return -1;
1104 case DELETE_FAST:
1105 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 case RAISE_VARARGS:
1108 return -oparg;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001109
1110 /* Functions and calls */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 case CALL_FUNCTION:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001112 return -oparg;
Yury Selivanovf2392132016-12-13 19:03:51 -05001113 case CALL_METHOD:
1114 return -oparg-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 case CALL_FUNCTION_KW:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001116 return -oparg-1;
1117 case CALL_FUNCTION_EX:
Matthieu Dartiailh3a9ac822017-02-21 14:25:22 +01001118 return -1 - ((oparg & 0x01) != 0);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001119 case MAKE_FUNCTION:
1120 return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1121 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 case BUILD_SLICE:
1123 if (oparg == 3)
1124 return -2;
1125 else
1126 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001128 /* Closures */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 case LOAD_CLOSURE:
1130 return 1;
1131 case LOAD_DEREF:
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001132 case LOAD_CLASSDEREF:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 return 1;
1134 case STORE_DEREF:
1135 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001136 case DELETE_DEREF:
1137 return 0;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001138
1139 /* Iterators and generators */
Yury Selivanov75445082015-05-11 22:57:16 -04001140 case GET_AWAITABLE:
1141 return 0;
1142 case SETUP_ASYNC_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001143 /* 0 in the normal flow.
1144 * Restore the stack position to the position before the result
1145 * of __aenter__ and push 6 values before jumping to the handler
1146 * if an exception be raised. */
1147 return jump ? -1 + 6 : 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001148 case BEFORE_ASYNC_WITH:
1149 return 1;
1150 case GET_AITER:
1151 return 0;
1152 case GET_ANEXT:
1153 return 1;
Yury Selivanov5376ba92015-06-22 12:19:30 -04001154 case GET_YIELD_FROM_ITER:
1155 return 0;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02001156 case END_ASYNC_FOR:
1157 return -7;
Eric V. Smitha78c7952015-11-03 12:45:05 -05001158 case FORMAT_VALUE:
1159 /* If there's a fmt_spec on the stack, we go from 2->1,
1160 else 1->1. */
1161 return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
Yury Selivanovf2392132016-12-13 19:03:51 -05001162 case LOAD_METHOD:
1163 return 1;
Zackery Spytzce6a0702019-08-25 03:44:09 -06001164 case LOAD_ASSERTION_ERROR:
1165 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001166 case LIST_TO_TUPLE:
1167 return 0;
Mark Shannonb37181e2021-04-06 11:48:59 +01001168 case GEN_START:
1169 return -1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001170 case LIST_EXTEND:
1171 case SET_UPDATE:
Mark Shannon8a4cd702020-01-27 09:57:45 +00001172 case DICT_MERGE:
1173 case DICT_UPDATE:
Mark Shannon13bc1392020-01-23 09:25:17 +00001174 return -1;
Brandt Bucher145bf262021-02-26 14:51:55 -08001175 case COPY_DICT_WITHOUT_KEYS:
1176 return 0;
1177 case MATCH_CLASS:
1178 return -1;
1179 case GET_LEN:
1180 case MATCH_MAPPING:
1181 case MATCH_SEQUENCE:
1182 return 1;
1183 case MATCH_KEYS:
1184 return 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 default:
Larry Hastings3a907972013-11-23 14:49:22 -08001186 return PY_INVALID_STACK_EFFECT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 }
Larry Hastings3a907972013-11-23 14:49:22 -08001188 return PY_INVALID_STACK_EFFECT; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189}
1190
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001191int
Serhiy Storchaka7bdf2822018-09-18 09:54:26 +03001192PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump)
1193{
1194 return stack_effect(opcode, oparg, jump);
1195}
1196
1197int
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001198PyCompile_OpcodeStackEffect(int opcode, int oparg)
1199{
1200 return stack_effect(opcode, oparg, -1);
1201}
1202
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203/* Add an opcode with no argument.
1204 Returns 0 on failure, 1 on success.
1205*/
1206
1207static int
Mark Shannon3bd60352021-01-13 12:05:43 +00001208compiler_addop_line(struct compiler *c, int opcode, int line)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 basicblock *b;
1211 struct instr *i;
1212 int off;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001213 assert(!HAS_ARG(opcode));
Andy Lester76d58772020-03-10 21:18:12 -05001214 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 if (off < 0)
1216 return 0;
1217 b = c->u->u_curblock;
1218 i = &b->b_instr[off];
1219 i->i_opcode = opcode;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001220 i->i_oparg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 if (opcode == RETURN_VALUE)
1222 b->b_return = 1;
Mark Shannon3bd60352021-01-13 12:05:43 +00001223 i->i_lineno = line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225}
1226
Mark Shannon3bd60352021-01-13 12:05:43 +00001227static int
1228compiler_addop(struct compiler *c, int opcode)
1229{
1230 return compiler_addop_line(c, opcode, c->u->u_lineno);
1231}
1232
1233static int
1234compiler_addop_noline(struct compiler *c, int opcode)
1235{
1236 return compiler_addop_line(c, opcode, -1);
1237}
1238
1239
Victor Stinnerf8e32212013-11-19 23:56:34 +01001240static Py_ssize_t
Andy Lester76d58772020-03-10 21:18:12 -05001241compiler_add_o(PyObject *dict, PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001243 PyObject *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001245
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001246 v = PyDict_GetItemWithError(dict, o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 if (!v) {
Stefan Krahc0cbed12015-07-27 12:56:49 +02001248 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 return -1;
Stefan Krahc0cbed12015-07-27 12:56:49 +02001250 }
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001251 arg = PyDict_GET_SIZE(dict);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001252 v = PyLong_FromSsize_t(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 if (!v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 return -1;
1255 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001256 if (PyDict_SetItem(dict, o, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 Py_DECREF(v);
1258 return -1;
1259 }
1260 Py_DECREF(v);
1261 }
1262 else
1263 arg = PyLong_AsLong(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001264 return arg;
1265}
1266
INADA Naokic2e16072018-11-26 21:23:22 +09001267// Merge const *o* recursively and return constant key object.
1268static PyObject*
1269merge_consts_recursive(struct compiler *c, PyObject *o)
1270{
1271 // None and Ellipsis are singleton, and key is the singleton.
1272 // No need to merge object and key.
1273 if (o == Py_None || o == Py_Ellipsis) {
1274 Py_INCREF(o);
1275 return o;
1276 }
1277
1278 PyObject *key = _PyCode_ConstantKey(o);
1279 if (key == NULL) {
1280 return NULL;
1281 }
1282
1283 // t is borrowed reference
1284 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
1285 if (t != key) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001286 // o is registered in c_const_cache. Just use it.
Zackery Spytz9b4a1b12019-03-20 03:16:25 -06001287 Py_XINCREF(t);
INADA Naokic2e16072018-11-26 21:23:22 +09001288 Py_DECREF(key);
1289 return t;
1290 }
1291
INADA Naokif7e4d362018-11-29 00:58:46 +09001292 // We registered o in c_const_cache.
Simeon63b5fc52019-04-09 19:36:57 -04001293 // When o is a tuple or frozenset, we want to merge its
INADA Naokif7e4d362018-11-29 00:58:46 +09001294 // items too.
INADA Naokic2e16072018-11-26 21:23:22 +09001295 if (PyTuple_CheckExact(o)) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001296 Py_ssize_t len = PyTuple_GET_SIZE(o);
1297 for (Py_ssize_t i = 0; i < len; i++) {
INADA Naokic2e16072018-11-26 21:23:22 +09001298 PyObject *item = PyTuple_GET_ITEM(o, i);
1299 PyObject *u = merge_consts_recursive(c, item);
1300 if (u == NULL) {
1301 Py_DECREF(key);
1302 return NULL;
1303 }
1304
1305 // See _PyCode_ConstantKey()
1306 PyObject *v; // borrowed
1307 if (PyTuple_CheckExact(u)) {
1308 v = PyTuple_GET_ITEM(u, 1);
1309 }
1310 else {
1311 v = u;
1312 }
1313 if (v != item) {
1314 Py_INCREF(v);
1315 PyTuple_SET_ITEM(o, i, v);
1316 Py_DECREF(item);
1317 }
1318
1319 Py_DECREF(u);
1320 }
1321 }
INADA Naokif7e4d362018-11-29 00:58:46 +09001322 else if (PyFrozenSet_CheckExact(o)) {
Simeon63b5fc52019-04-09 19:36:57 -04001323 // *key* is tuple. And its first item is frozenset of
INADA Naokif7e4d362018-11-29 00:58:46 +09001324 // constant keys.
1325 // See _PyCode_ConstantKey() for detail.
1326 assert(PyTuple_CheckExact(key));
1327 assert(PyTuple_GET_SIZE(key) == 2);
1328
1329 Py_ssize_t len = PySet_GET_SIZE(o);
1330 if (len == 0) { // empty frozenset should not be re-created.
1331 return key;
1332 }
1333 PyObject *tuple = PyTuple_New(len);
1334 if (tuple == NULL) {
1335 Py_DECREF(key);
1336 return NULL;
1337 }
1338 Py_ssize_t i = 0, pos = 0;
1339 PyObject *item;
1340 Py_hash_t hash;
1341 while (_PySet_NextEntry(o, &pos, &item, &hash)) {
1342 PyObject *k = merge_consts_recursive(c, item);
1343 if (k == NULL) {
1344 Py_DECREF(tuple);
1345 Py_DECREF(key);
1346 return NULL;
1347 }
1348 PyObject *u;
1349 if (PyTuple_CheckExact(k)) {
1350 u = PyTuple_GET_ITEM(k, 1);
1351 Py_INCREF(u);
1352 Py_DECREF(k);
1353 }
1354 else {
1355 u = k;
1356 }
1357 PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u.
1358 i++;
1359 }
1360
1361 // Instead of rewriting o, we create new frozenset and embed in the
1362 // key tuple. Caller should get merged frozenset from the key tuple.
1363 PyObject *new = PyFrozenSet_New(tuple);
1364 Py_DECREF(tuple);
1365 if (new == NULL) {
1366 Py_DECREF(key);
1367 return NULL;
1368 }
1369 assert(PyTuple_GET_ITEM(key, 1) == o);
1370 Py_DECREF(o);
1371 PyTuple_SET_ITEM(key, 1, new);
1372 }
INADA Naokic2e16072018-11-26 21:23:22 +09001373
1374 return key;
1375}
1376
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001377static Py_ssize_t
1378compiler_add_const(struct compiler *c, PyObject *o)
1379{
INADA Naokic2e16072018-11-26 21:23:22 +09001380 PyObject *key = merge_consts_recursive(c, o);
1381 if (key == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001382 return -1;
INADA Naokic2e16072018-11-26 21:23:22 +09001383 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001384
Andy Lester76d58772020-03-10 21:18:12 -05001385 Py_ssize_t arg = compiler_add_o(c->u->u_consts, key);
INADA Naokic2e16072018-11-26 21:23:22 +09001386 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388}
1389
1390static int
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001391compiler_addop_load_const(struct compiler *c, PyObject *o)
1392{
1393 Py_ssize_t arg = compiler_add_const(c, o);
1394 if (arg < 0)
1395 return 0;
1396 return compiler_addop_i(c, LOAD_CONST, arg);
1397}
1398
1399static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001402{
Andy Lester76d58772020-03-10 21:18:12 -05001403 Py_ssize_t arg = compiler_add_o(dict, o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001404 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001405 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001406 return compiler_addop_i(c, opcode, arg);
1407}
1408
1409static int
1410compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001413 Py_ssize_t arg;
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001414
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001415 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1416 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001417 return 0;
Andy Lester76d58772020-03-10 21:18:12 -05001418 arg = compiler_add_o(dict, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419 Py_DECREF(mangled);
1420 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001421 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422 return compiler_addop_i(c, opcode, arg);
1423}
1424
1425/* Add an opcode with an integer argument.
1426 Returns 0 on failure, 1 on success.
1427*/
1428
1429static int
Mark Shannon11e0b292021-04-15 14:28:56 +01001430compiler_addop_i_line(struct compiler *c, int opcode, Py_ssize_t oparg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 struct instr *i;
1433 int off;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001434
Victor Stinner2ad474b2016-03-01 23:34:47 +01001435 /* oparg value is unsigned, but a signed C int is usually used to store
1436 it in the C code (like Python/ceval.c).
1437
1438 Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1439
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001440 The argument of a concrete bytecode instruction is limited to 8-bit.
1441 EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1442 assert(HAS_ARG(opcode));
Victor Stinner2ad474b2016-03-01 23:34:47 +01001443 assert(0 <= oparg && oparg <= 2147483647);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001444
Andy Lester76d58772020-03-10 21:18:12 -05001445 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 if (off < 0)
1447 return 0;
1448 i = &c->u->u_curblock->b_instr[off];
Victor Stinnerf8e32212013-11-19 23:56:34 +01001449 i->i_opcode = opcode;
1450 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
Mark Shannon11e0b292021-04-15 14:28:56 +01001451 i->i_lineno = lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453}
1454
Mark Shannon11e0b292021-04-15 14:28:56 +01001455static int
1456compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
1457{
1458 return compiler_addop_i_line(c, opcode, oparg, c->u->u_lineno);
1459}
1460
1461static int
1462compiler_addop_i_noline(struct compiler *c, int opcode, Py_ssize_t oparg)
1463{
1464 return compiler_addop_i_line(c, opcode, oparg, -1);
1465}
1466
Mark Shannon28b75c82020-12-23 11:43:10 +00001467static int add_jump_to_block(basicblock *b, int opcode, int lineno, basicblock *target)
1468{
1469 assert(HAS_ARG(opcode));
1470 assert(b != NULL);
1471 assert(target != NULL);
1472
1473 int off = compiler_next_instr(b);
1474 struct instr *i = &b->b_instr[off];
1475 if (off < 0) {
1476 return 0;
1477 }
1478 i->i_opcode = opcode;
1479 i->i_target = target;
1480 i->i_lineno = lineno;
1481 return 1;
1482}
1483
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001484static int
Mark Shannon582aaf12020-08-04 17:30:11 +01001485compiler_addop_j(struct compiler *c, int opcode, basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001486{
Mark Shannon28b75c82020-12-23 11:43:10 +00001487 return add_jump_to_block(c->u->u_curblock, opcode, c->u->u_lineno, b);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488}
1489
Mark Shannon127dde52021-01-04 18:06:55 +00001490static int
1491compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b)
1492{
1493 return add_jump_to_block(c->u->u_curblock, opcode, -1, b);
1494}
1495
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +01001496/* NEXT_BLOCK() creates an implicit jump from the current block
1497 to the new block.
1498
1499 The returns inside this macro make it impossible to decref objects
1500 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001501*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 if (compiler_next_block((C)) == NULL) \
1504 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505}
1506
1507#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 if (!compiler_addop((C), (OP))) \
1509 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510}
1511
Mark Shannon3bd60352021-01-13 12:05:43 +00001512#define ADDOP_NOLINE(C, OP) { \
1513 if (!compiler_addop_noline((C), (OP))) \
1514 return 0; \
1515}
1516
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001517#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 if (!compiler_addop((C), (OP))) { \
1519 compiler_exit_scope(c); \
1520 return 0; \
1521 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001522}
1523
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001524#define ADDOP_LOAD_CONST(C, O) { \
1525 if (!compiler_addop_load_const((C), (O))) \
1526 return 0; \
1527}
1528
1529/* Same as ADDOP_LOAD_CONST, but steals a reference. */
1530#define ADDOP_LOAD_CONST_NEW(C, O) { \
1531 PyObject *__new_const = (O); \
1532 if (__new_const == NULL) { \
1533 return 0; \
1534 } \
1535 if (!compiler_addop_load_const((C), __new_const)) { \
1536 Py_DECREF(__new_const); \
1537 return 0; \
1538 } \
1539 Py_DECREF(__new_const); \
1540}
1541
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001542#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1544 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545}
1546
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001547/* Same as ADDOP_O, but steals a reference. */
1548#define ADDOP_N(C, OP, O, TYPE) { \
1549 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1550 Py_DECREF((O)); \
1551 return 0; \
1552 } \
1553 Py_DECREF((O)); \
1554}
1555
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001556#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1558 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559}
1560
1561#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 if (!compiler_addop_i((C), (OP), (O))) \
1563 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564}
1565
Mark Shannon11e0b292021-04-15 14:28:56 +01001566#define ADDOP_I_NOLINE(C, OP, O) { \
1567 if (!compiler_addop_i_noline((C), (OP), (O))) \
1568 return 0; \
1569}
1570
Mark Shannon582aaf12020-08-04 17:30:11 +01001571#define ADDOP_JUMP(C, OP, O) { \
1572 if (!compiler_addop_j((C), (OP), (O))) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001574}
1575
Mark Shannon127dde52021-01-04 18:06:55 +00001576/* Add a jump with no line number.
1577 * Used for artificial jumps that have no corresponding
1578 * token in the source code. */
1579#define ADDOP_JUMP_NOLINE(C, OP, O) { \
1580 if (!compiler_addop_j_noline((C), (OP), (O))) \
1581 return 0; \
1582}
1583
Mark Shannon9af0e472020-01-14 10:12:45 +00001584#define ADDOP_COMPARE(C, CMP) { \
1585 if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \
1586 return 0; \
1587}
1588
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1590 the ASDL name to synthesize the name of the C type and the visit function.
1591*/
1592
1593#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 if (!compiler_visit_ ## TYPE((C), (V))) \
1595 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596}
1597
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001598#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 if (!compiler_visit_ ## TYPE((C), (V))) { \
1600 compiler_exit_scope(c); \
1601 return 0; \
1602 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001603}
1604
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001605#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 if (!compiler_visit_slice((C), (V), (CTX))) \
1607 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001608}
1609
1610#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001612 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1614 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1615 if (!compiler_visit_ ## TYPE((C), elt)) \
1616 return 0; \
1617 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001618}
1619
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001620#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001622 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1624 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1625 if (!compiler_visit_ ## TYPE((C), elt)) { \
1626 compiler_exit_scope(c); \
1627 return 0; \
1628 } \
1629 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001630}
1631
Brandt Bucher145bf262021-02-26 14:51:55 -08001632#define RETURN_IF_FALSE(X) \
1633 if (!(X)) { \
1634 return 0; \
1635 }
1636
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001637/* Search if variable annotations are present statically in a block. */
1638
1639static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001640find_ann(asdl_stmt_seq *stmts)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001641{
1642 int i, j, res = 0;
1643 stmt_ty st;
1644
1645 for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1646 st = (stmt_ty)asdl_seq_GET(stmts, i);
1647 switch (st->kind) {
1648 case AnnAssign_kind:
1649 return 1;
1650 case For_kind:
1651 res = find_ann(st->v.For.body) ||
1652 find_ann(st->v.For.orelse);
1653 break;
1654 case AsyncFor_kind:
1655 res = find_ann(st->v.AsyncFor.body) ||
1656 find_ann(st->v.AsyncFor.orelse);
1657 break;
1658 case While_kind:
1659 res = find_ann(st->v.While.body) ||
1660 find_ann(st->v.While.orelse);
1661 break;
1662 case If_kind:
1663 res = find_ann(st->v.If.body) ||
1664 find_ann(st->v.If.orelse);
1665 break;
1666 case With_kind:
1667 res = find_ann(st->v.With.body);
1668 break;
1669 case AsyncWith_kind:
1670 res = find_ann(st->v.AsyncWith.body);
1671 break;
1672 case Try_kind:
1673 for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1674 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1675 st->v.Try.handlers, j);
1676 if (find_ann(handler->v.ExceptHandler.body)) {
1677 return 1;
1678 }
1679 }
1680 res = find_ann(st->v.Try.body) ||
1681 find_ann(st->v.Try.finalbody) ||
1682 find_ann(st->v.Try.orelse);
1683 break;
1684 default:
1685 res = 0;
1686 }
1687 if (res) {
1688 break;
1689 }
1690 }
1691 return res;
1692}
1693
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001694/*
1695 * Frame block handling functions
1696 */
1697
1698static int
1699compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b,
Mark Shannonfee55262019-11-21 09:11:43 +00001700 basicblock *exit, void *datum)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001701{
1702 struct fblockinfo *f;
1703 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
Mark Shannon02d126a2020-09-25 14:04:19 +01001704 return compiler_error(c, "too many statically nested blocks");
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001705 }
1706 f = &c->u->u_fblock[c->u->u_nfblocks++];
1707 f->fb_type = t;
1708 f->fb_block = b;
1709 f->fb_exit = exit;
Mark Shannonfee55262019-11-21 09:11:43 +00001710 f->fb_datum = datum;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001711 return 1;
1712}
1713
1714static void
1715compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
1716{
1717 struct compiler_unit *u = c->u;
1718 assert(u->u_nfblocks > 0);
1719 u->u_nfblocks--;
1720 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
1721 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
1722}
1723
Mark Shannonfee55262019-11-21 09:11:43 +00001724static int
1725compiler_call_exit_with_nones(struct compiler *c) {
1726 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1727 ADDOP(c, DUP_TOP);
1728 ADDOP(c, DUP_TOP);
1729 ADDOP_I(c, CALL_FUNCTION, 3);
1730 return 1;
1731}
1732
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001733/* Unwind a frame block. If preserve_tos is true, the TOS before
Mark Shannonfee55262019-11-21 09:11:43 +00001734 * popping the blocks will be restored afterwards, unless another
1735 * return, break or continue is found. In which case, the TOS will
1736 * be popped.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001737 */
1738static int
1739compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
1740 int preserve_tos)
1741{
1742 switch (info->fb_type) {
1743 case WHILE_LOOP:
Mark Shannon02d126a2020-09-25 14:04:19 +01001744 case EXCEPTION_HANDLER:
tomKPZ7a7ba3d2021-04-07 07:43:45 -07001745 case ASYNC_COMPREHENSION_GENERATOR:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001746 return 1;
1747
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001748 case FOR_LOOP:
1749 /* Pop the iterator */
1750 if (preserve_tos) {
1751 ADDOP(c, ROT_TWO);
1752 }
1753 ADDOP(c, POP_TOP);
1754 return 1;
1755
Mark Shannon02d126a2020-09-25 14:04:19 +01001756 case TRY_EXCEPT:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001757 ADDOP(c, POP_BLOCK);
1758 return 1;
1759
1760 case FINALLY_TRY:
Mark Shannon5274b682020-12-16 13:07:01 +00001761 /* This POP_BLOCK gets the line number of the unwinding statement */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001762 ADDOP(c, POP_BLOCK);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001763 if (preserve_tos) {
Mark Shannonfee55262019-11-21 09:11:43 +00001764 if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) {
1765 return 0;
1766 }
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001767 }
Mark Shannon5274b682020-12-16 13:07:01 +00001768 /* Emit the finally block */
Mark Shannonfee55262019-11-21 09:11:43 +00001769 VISIT_SEQ(c, stmt, info->fb_datum);
1770 if (preserve_tos) {
1771 compiler_pop_fblock(c, POP_VALUE, NULL);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001772 }
Mark Shannon5274b682020-12-16 13:07:01 +00001773 /* The finally block should appear to execute after the
1774 * statement causing the unwinding, so make the unwinding
1775 * instruction artificial */
1776 c->u->u_lineno = -1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001777 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001778
Mark Shannonfee55262019-11-21 09:11:43 +00001779 case FINALLY_END:
1780 if (preserve_tos) {
1781 ADDOP(c, ROT_FOUR);
1782 }
1783 ADDOP(c, POP_TOP);
1784 ADDOP(c, POP_TOP);
1785 ADDOP(c, POP_TOP);
1786 if (preserve_tos) {
1787 ADDOP(c, ROT_FOUR);
1788 }
1789 ADDOP(c, POP_EXCEPT);
1790 return 1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001791
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001792 case WITH:
1793 case ASYNC_WITH:
1794 ADDOP(c, POP_BLOCK);
1795 if (preserve_tos) {
1796 ADDOP(c, ROT_TWO);
1797 }
Mark Shannonfee55262019-11-21 09:11:43 +00001798 if(!compiler_call_exit_with_nones(c)) {
1799 return 0;
1800 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001801 if (info->fb_type == ASYNC_WITH) {
1802 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001803 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001804 ADDOP(c, YIELD_FROM);
1805 }
Mark Shannonfee55262019-11-21 09:11:43 +00001806 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001807 return 1;
1808
1809 case HANDLER_CLEANUP:
Mark Shannonfee55262019-11-21 09:11:43 +00001810 if (info->fb_datum) {
1811 ADDOP(c, POP_BLOCK);
1812 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001813 if (preserve_tos) {
1814 ADDOP(c, ROT_FOUR);
1815 }
Mark Shannonfee55262019-11-21 09:11:43 +00001816 ADDOP(c, POP_EXCEPT);
1817 if (info->fb_datum) {
1818 ADDOP_LOAD_CONST(c, Py_None);
1819 compiler_nameop(c, info->fb_datum, Store);
1820 compiler_nameop(c, info->fb_datum, Del);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001821 }
Mark Shannonfee55262019-11-21 09:11:43 +00001822 return 1;
1823
1824 case POP_VALUE:
1825 if (preserve_tos) {
1826 ADDOP(c, ROT_TWO);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001827 }
Mark Shannonfee55262019-11-21 09:11:43 +00001828 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001829 return 1;
1830 }
1831 Py_UNREACHABLE();
1832}
1833
Mark Shannonfee55262019-11-21 09:11:43 +00001834/** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */
1835static int
1836compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) {
1837 if (c->u->u_nfblocks == 0) {
1838 return 1;
1839 }
1840 struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1];
1841 if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) {
1842 *loop = top;
1843 return 1;
1844 }
1845 struct fblockinfo copy = *top;
1846 c->u->u_nfblocks--;
1847 if (!compiler_unwind_fblock(c, &copy, preserve_tos)) {
1848 return 0;
1849 }
1850 if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) {
1851 return 0;
1852 }
1853 c->u->u_fblock[c->u->u_nfblocks] = copy;
1854 c->u->u_nfblocks++;
1855 return 1;
1856}
1857
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001858/* Compile a sequence of statements, checking for a docstring
1859 and for annotations. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860
1861static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001862compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001863{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001864 int i = 0;
1865 stmt_ty st;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001866 PyObject *docstring;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001867
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001868 /* Set current line number to the line number of first statement.
1869 This way line number for SETUP_ANNOTATIONS will always
1870 coincide with the line number of first "real" statement in module.
Hansraj Das01171eb2019-10-09 07:54:02 +05301871 If body is empty, then lineno will be set later in assemble. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001872 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) {
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001873 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001874 SET_LOC(c, st);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001875 }
1876 /* Every annotated class and module should have __annotations__. */
1877 if (find_ann(stmts)) {
1878 ADDOP(c, SETUP_ANNOTATIONS);
1879 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001880 if (!asdl_seq_LEN(stmts))
1881 return 1;
INADA Naokicb41b272017-02-23 00:31:59 +09001882 /* if not -OO mode, set docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001883 if (c->c_optimize < 2) {
1884 docstring = _PyAST_GetDocString(stmts);
1885 if (docstring) {
1886 i = 1;
1887 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1888 assert(st->kind == Expr_kind);
1889 VISIT(c, expr, st->v.Expr.value);
1890 if (!compiler_nameop(c, __doc__, Store))
1891 return 0;
1892 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001894 for (; i < asdl_seq_LEN(stmts); i++)
1895 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897}
1898
1899static PyCodeObject *
1900compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001901{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 PyCodeObject *co;
1903 int addNone = 1;
1904 static PyObject *module;
1905 if (!module) {
1906 module = PyUnicode_InternFromString("<module>");
1907 if (!module)
1908 return NULL;
1909 }
1910 /* Use 0 for firstlineno initially, will fixup in assemble(). */
Mark Shannon877df852020-11-12 09:43:29 +00001911 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 return NULL;
1913 switch (mod->kind) {
1914 case Module_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001915 if (!compiler_body(c, mod->v.Module.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 compiler_exit_scope(c);
1917 return 0;
1918 }
1919 break;
1920 case Interactive_kind:
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001921 if (find_ann(mod->v.Interactive.body)) {
1922 ADDOP(c, SETUP_ANNOTATIONS);
1923 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 c->c_interactive = 1;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001925 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 break;
1927 case Expression_kind:
1928 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1929 addNone = 0;
1930 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 default:
1932 PyErr_Format(PyExc_SystemError,
1933 "module kind %d should not be possible",
1934 mod->kind);
1935 return 0;
1936 }
1937 co = assemble(c, addNone);
1938 compiler_exit_scope(c);
1939 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001940}
1941
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942/* The test for LOCAL must come before the test for FREE in order to
1943 handle classes where name is both local and free. The local var is
1944 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001945*/
1946
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001947static int
1948get_ref_type(struct compiler *c, PyObject *name)
1949{
Victor Stinner0b1bc562013-05-16 22:17:17 +02001950 int scope;
Benjamin Peterson312595c2013-05-15 15:26:42 -05001951 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001952 _PyUnicode_EqualToASCIIString(name, "__class__"))
Benjamin Peterson312595c2013-05-15 15:26:42 -05001953 return CELL;
Victor Stinner28ad12f2021-03-19 12:41:49 +01001954 scope = _PyST_GetScope(c->u->u_ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 if (scope == 0) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001956 PyErr_Format(PyExc_SystemError,
Victor Stinner28ad12f2021-03-19 12:41:49 +01001957 "_PyST_GetScope(name=%R) failed: "
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001958 "unknown scope in unit %S (%R); "
1959 "symbols: %R; locals: %R; globals: %R",
1960 name,
1961 c->u->u_name, c->u->u_ste->ste_id,
1962 c->u->u_ste->ste_symbols, c->u->u_varnames, c->u->u_names);
1963 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001966}
1967
1968static int
1969compiler_lookup_arg(PyObject *dict, PyObject *name)
1970{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001971 PyObject *v;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001972 v = PyDict_GetItemWithError(dict, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001973 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001974 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001975 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001976}
1977
1978static int
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001979compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags,
1980 PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001981{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001982 Py_ssize_t i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001983 if (qualname == NULL)
1984 qualname = co->co_name;
1985
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001986 if (free) {
1987 for (i = 0; i < free; ++i) {
1988 /* Bypass com_addop_varname because it will generate
1989 LOAD_DEREF but LOAD_CLOSURE is needed.
1990 */
1991 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001993 /* Special case: If a class contains a method with a
1994 free variable that has the same name as a method,
1995 the name will be considered free *and* local in the
1996 class. It should be handled by the closure, as
Min ho Kimc4cacc82019-07-31 08:16:13 +10001997 well as by the normal name lookup logic.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001998 */
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001999 int reftype = get_ref_type(c, name);
2000 if (reftype == -1) {
2001 return 0;
2002 }
2003 int arg;
2004 if (reftype == CELL) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002005 arg = compiler_lookup_arg(c->u->u_cellvars, name);
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002006 }
2007 else {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002008 arg = compiler_lookup_arg(c->u->u_freevars, name);
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002009 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002010 if (arg == -1) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002011 PyErr_Format(PyExc_SystemError,
2012 "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; "
2013 "freevars of code %S: %R",
2014 name,
2015 reftype,
2016 c->u->u_name,
2017 co->co_name,
2018 co->co_freevars);
2019 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002020 }
2021 ADDOP_I(c, LOAD_CLOSURE, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002023 flags |= 0x08;
2024 ADDOP_I(c, BUILD_TUPLE, free);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002026 ADDOP_LOAD_CONST(c, (PyObject*)co);
2027 ADDOP_LOAD_CONST(c, qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002028 ADDOP_I(c, MAKE_FUNCTION, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002030}
2031
2032static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002033compiler_decorators(struct compiler *c, asdl_expr_seq* decos)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 if (!decos)
2038 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002040 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2041 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
2042 }
2043 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002044}
2045
2046static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002047compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs,
2048 asdl_expr_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00002049{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002050 /* Push a dict of keyword-only default values.
2051
2052 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
2053 */
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002054 int i;
2055 PyObject *keys = NULL;
2056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
2058 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
2059 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
2060 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04002061 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002062 if (!mangled) {
2063 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002065 if (keys == NULL) {
2066 keys = PyList_New(1);
2067 if (keys == NULL) {
2068 Py_DECREF(mangled);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002069 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002070 }
2071 PyList_SET_ITEM(keys, 0, mangled);
2072 }
2073 else {
2074 int res = PyList_Append(keys, mangled);
2075 Py_DECREF(mangled);
2076 if (res == -1) {
2077 goto error;
2078 }
2079 }
2080 if (!compiler_visit_expr(c, default_)) {
2081 goto error;
2082 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 }
2084 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002085 if (keys != NULL) {
2086 Py_ssize_t default_count = PyList_GET_SIZE(keys);
2087 PyObject *keys_tuple = PyList_AsTuple(keys);
2088 Py_DECREF(keys);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002089 ADDOP_LOAD_CONST_NEW(c, keys_tuple);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002090 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002091 assert(default_count > 0);
2092 return 1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002093 }
2094 else {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002095 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002096 }
2097
2098error:
2099 Py_XDECREF(keys);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002100 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002101}
2102
2103static int
Guido van Rossum95e4d582018-01-26 08:20:18 -08002104compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
2105{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +03002106 ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation));
Guido van Rossum95e4d582018-01-26 08:20:18 -08002107 return 1;
2108}
2109
2110static int
Neal Norwitzc1505362006-12-28 06:47:50 +00002111compiler_visit_argannotation(struct compiler *c, identifier id,
Yurii Karabas73019792020-11-25 12:43:18 +02002112 expr_ty annotation, Py_ssize_t *annotations_len)
Neal Norwitzc1505362006-12-28 06:47:50 +00002113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 if (annotation) {
Yurii Karabas73019792020-11-25 12:43:18 +02002115 PyObject *mangled = _Py_Mangle(c->u->u_private, id);
Yury Selivanov34ce99f2014-02-18 12:49:41 -05002116 if (!mangled)
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002117 return 0;
Yurii Karabas73019792020-11-25 12:43:18 +02002118
2119 ADDOP_LOAD_CONST(c, mangled);
Yury Selivanov34ce99f2014-02-18 12:49:41 -05002120 Py_DECREF(mangled);
Yurii Karabas73019792020-11-25 12:43:18 +02002121 VISIT(c, annexpr, annotation);
2122 *annotations_len += 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002124 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002125}
2126
2127static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002128compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args,
Yurii Karabas73019792020-11-25 12:43:18 +02002129 Py_ssize_t *annotations_len)
Neal Norwitzc1505362006-12-28 06:47:50 +00002130{
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002131 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132 for (i = 0; i < asdl_seq_LEN(args); i++) {
2133 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002134 if (!compiler_visit_argannotation(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135 c,
2136 arg->arg,
2137 arg->annotation,
Yurii Karabas73019792020-11-25 12:43:18 +02002138 annotations_len))
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002139 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002141 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002142}
2143
2144static int
2145compiler_visit_annotations(struct compiler *c, arguments_ty args,
2146 expr_ty returns)
2147{
Yurii Karabas73019792020-11-25 12:43:18 +02002148 /* Push arg annotation names and values.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002149 The expressions are evaluated out-of-order wrt the source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00002150
Yurii Karabas73019792020-11-25 12:43:18 +02002151 Return 0 on error, -1 if no annotations pushed, 1 if a annotations is pushed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152 */
2153 static identifier return_str;
Yurii Karabas73019792020-11-25 12:43:18 +02002154 Py_ssize_t annotations_len = 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002155
Yurii Karabas73019792020-11-25 12:43:18 +02002156 if (!compiler_visit_argannotations(c, args->args, &annotations_len))
2157 return 0;
2158 if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len))
2159 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002160 if (args->vararg && args->vararg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002161 !compiler_visit_argannotation(c, args->vararg->arg,
Yurii Karabas73019792020-11-25 12:43:18 +02002162 args->vararg->annotation, &annotations_len))
2163 return 0;
2164 if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len))
2165 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002166 if (args->kwarg && args->kwarg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002167 !compiler_visit_argannotation(c, args->kwarg->arg,
Yurii Karabas73019792020-11-25 12:43:18 +02002168 args->kwarg->annotation, &annotations_len))
2169 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 if (!return_str) {
2172 return_str = PyUnicode_InternFromString("return");
2173 if (!return_str)
Yurii Karabas73019792020-11-25 12:43:18 +02002174 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 }
Yurii Karabas73019792020-11-25 12:43:18 +02002176 if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) {
2177 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 }
2179
Yurii Karabas73019792020-11-25 12:43:18 +02002180 if (annotations_len) {
2181 ADDOP_I(c, BUILD_TUPLE, annotations_len);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002182 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002184
Yurii Karabas73019792020-11-25 12:43:18 +02002185 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002186}
2187
2188static int
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002189compiler_visit_defaults(struct compiler *c, arguments_ty args)
2190{
2191 VISIT_SEQ(c, expr, args->defaults);
2192 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
2193 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002194}
2195
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002196static Py_ssize_t
2197compiler_default_arguments(struct compiler *c, arguments_ty args)
2198{
2199 Py_ssize_t funcflags = 0;
2200 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002201 if (!compiler_visit_defaults(c, args))
2202 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002203 funcflags |= 0x01;
2204 }
2205 if (args->kwonlyargs) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002206 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002207 args->kw_defaults);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002208 if (res == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002209 return -1;
2210 }
2211 else if (res > 0) {
2212 funcflags |= 0x02;
2213 }
2214 }
2215 return funcflags;
2216}
2217
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002218static int
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002219forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx)
2220{
2221
2222 if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2223 compiler_error(c, "cannot assign to __debug__");
2224 return 1;
2225 }
2226 return 0;
2227}
2228
2229static int
2230compiler_check_debug_one_arg(struct compiler *c, arg_ty arg)
2231{
2232 if (arg != NULL) {
2233 if (forbidden_name(c, arg->arg, Store))
2234 return 0;
2235 }
2236 return 1;
2237}
2238
2239static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002240compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args)
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002241{
2242 if (args != NULL) {
Pablo Galindoee40e4b2020-04-23 03:43:08 +01002243 for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002244 if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i)))
2245 return 0;
2246 }
2247 }
2248 return 1;
2249}
2250
2251static int
2252compiler_check_debug_args(struct compiler *c, arguments_ty args)
2253{
2254 if (!compiler_check_debug_args_seq(c, args->posonlyargs))
2255 return 0;
2256 if (!compiler_check_debug_args_seq(c, args->args))
2257 return 0;
2258 if (!compiler_check_debug_one_arg(c, args->vararg))
2259 return 0;
2260 if (!compiler_check_debug_args_seq(c, args->kwonlyargs))
2261 return 0;
2262 if (!compiler_check_debug_one_arg(c, args->kwarg))
2263 return 0;
2264 return 1;
2265}
2266
2267static int
Yury Selivanov75445082015-05-11 22:57:16 -04002268compiler_function(struct compiler *c, stmt_ty s, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 PyCodeObject *co;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002271 PyObject *qualname, *docstring = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002272 arguments_ty args;
2273 expr_ty returns;
2274 identifier name;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002275 asdl_expr_seq* decos;
2276 asdl_stmt_seq *body;
INADA Naokicb41b272017-02-23 00:31:59 +09002277 Py_ssize_t i, funcflags;
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002278 int annotations;
Yury Selivanov75445082015-05-11 22:57:16 -04002279 int scope_type;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002280 int firstlineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002281
Yury Selivanov75445082015-05-11 22:57:16 -04002282 if (is_async) {
2283 assert(s->kind == AsyncFunctionDef_kind);
2284
2285 args = s->v.AsyncFunctionDef.args;
2286 returns = s->v.AsyncFunctionDef.returns;
2287 decos = s->v.AsyncFunctionDef.decorator_list;
2288 name = s->v.AsyncFunctionDef.name;
2289 body = s->v.AsyncFunctionDef.body;
2290
2291 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
2292 } else {
2293 assert(s->kind == FunctionDef_kind);
2294
2295 args = s->v.FunctionDef.args;
2296 returns = s->v.FunctionDef.returns;
2297 decos = s->v.FunctionDef.decorator_list;
2298 name = s->v.FunctionDef.name;
2299 body = s->v.FunctionDef.body;
2300
2301 scope_type = COMPILER_SCOPE_FUNCTION;
2302 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002303
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002304 if (!compiler_check_debug_args(c, args))
2305 return 0;
2306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 if (!compiler_decorators(c, decos))
2308 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002309
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002310 firstlineno = s->lineno;
2311 if (asdl_seq_LEN(decos)) {
2312 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2313 }
2314
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002315 funcflags = compiler_default_arguments(c, args);
2316 if (funcflags == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002318 }
2319
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002320 annotations = compiler_visit_annotations(c, args, returns);
2321 if (annotations == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002322 return 0;
2323 }
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002324 else if (annotations > 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002325 funcflags |= 0x04;
2326 }
2327
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002328 if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002329 return 0;
2330 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002331
INADA Naokicb41b272017-02-23 00:31:59 +09002332 /* if not -OO mode, add docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002333 if (c->c_optimize < 2) {
2334 docstring = _PyAST_GetDocString(body);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002335 }
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002336 if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 compiler_exit_scope(c);
2338 return 0;
2339 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002341 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002342 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Mark Shannon877df852020-11-12 09:43:29 +00002344 for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) {
Mark Shannonfd009e62020-11-13 12:53:53 +00002345 VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i));
Mark Shannon877df852020-11-12 09:43:29 +00002346 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002348 qualname = c->u->u_qualname;
2349 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002351 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002352 Py_XDECREF(qualname);
2353 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002355 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002356
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002357 if (!compiler_make_closure(c, co, funcflags, qualname)) {
2358 Py_DECREF(qualname);
2359 Py_DECREF(co);
2360 return 0;
2361 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002362 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 /* decorators */
2366 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2367 ADDOP_I(c, CALL_FUNCTION, 1);
2368 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002369
Yury Selivanov75445082015-05-11 22:57:16 -04002370 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002371}
2372
2373static int
2374compiler_class(struct compiler *c, stmt_ty s)
2375{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 PyCodeObject *co;
2377 PyObject *str;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002378 int i, firstlineno;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002379 asdl_expr_seq *decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 if (!compiler_decorators(c, decos))
2382 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002383
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002384 firstlineno = s->lineno;
2385 if (asdl_seq_LEN(decos)) {
2386 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2387 }
2388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 /* ultimately generate code for:
2390 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
2391 where:
2392 <func> is a function/closure created from the class body;
2393 it has a single argument (__locals__) where the dict
2394 (or MutableSequence) representing the locals is passed
2395 <name> is the class name
2396 <bases> is the positional arguments and *varargs argument
2397 <keywords> is the keyword arguments and **kwds argument
2398 This borrows from compiler_call.
2399 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002402 if (!compiler_enter_scope(c, s->v.ClassDef.name,
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002403 COMPILER_SCOPE_CLASS, (void *)s, firstlineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 return 0;
2405 /* this block represents what we do in the new scope */
2406 {
2407 /* use the class name for name mangling */
2408 Py_INCREF(s->v.ClassDef.name);
Serhiy Storchaka48842712016-04-06 09:45:48 +03002409 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 /* load (global) __name__ ... */
2411 str = PyUnicode_InternFromString("__name__");
2412 if (!str || !compiler_nameop(c, str, Load)) {
2413 Py_XDECREF(str);
2414 compiler_exit_scope(c);
2415 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002416 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 Py_DECREF(str);
2418 /* ... and store it as __module__ */
2419 str = PyUnicode_InternFromString("__module__");
2420 if (!str || !compiler_nameop(c, str, Store)) {
2421 Py_XDECREF(str);
2422 compiler_exit_scope(c);
2423 return 0;
2424 }
2425 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002426 assert(c->u->u_qualname);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002427 ADDOP_LOAD_CONST(c, c->u->u_qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002428 str = PyUnicode_InternFromString("__qualname__");
2429 if (!str || !compiler_nameop(c, str, Store)) {
2430 Py_XDECREF(str);
2431 compiler_exit_scope(c);
2432 return 0;
2433 }
2434 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 /* compile the body proper */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002436 if (!compiler_body(c, s->v.ClassDef.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 compiler_exit_scope(c);
2438 return 0;
2439 }
Mark Shannone56d54e2021-01-15 13:52:00 +00002440 /* The following code is artificial */
2441 c->u->u_lineno = -1;
Nick Coghlan19d24672016-12-05 16:47:55 +10002442 /* Return __classcell__ if it is referenced, otherwise return None */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002443 if (c->u->u_ste->ste_needs_class_closure) {
Nick Coghlan19d24672016-12-05 16:47:55 +10002444 /* Store __classcell__ into class namespace & return it */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002445 str = PyUnicode_InternFromString("__class__");
2446 if (str == NULL) {
2447 compiler_exit_scope(c);
2448 return 0;
2449 }
2450 i = compiler_lookup_arg(c->u->u_cellvars, str);
2451 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01002452 if (i < 0) {
2453 compiler_exit_scope(c);
2454 return 0;
2455 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002456 assert(i == 0);
Nick Coghlan944368e2016-09-11 14:45:49 +10002457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 ADDOP_I(c, LOAD_CLOSURE, i);
Nick Coghlan19d24672016-12-05 16:47:55 +10002459 ADDOP(c, DUP_TOP);
Nick Coghlan944368e2016-09-11 14:45:49 +10002460 str = PyUnicode_InternFromString("__classcell__");
2461 if (!str || !compiler_nameop(c, str, Store)) {
2462 Py_XDECREF(str);
2463 compiler_exit_scope(c);
2464 return 0;
2465 }
2466 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002467 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002468 else {
Nick Coghlan19d24672016-12-05 16:47:55 +10002469 /* No methods referenced __class__, so just return None */
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002470 assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002471 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson312595c2013-05-15 15:26:42 -05002472 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002473 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002474 /* create the code object */
2475 co = assemble(c, 1);
2476 }
2477 /* leave the new scope */
2478 compiler_exit_scope(c);
2479 if (co == NULL)
2480 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 /* 2. load the 'build_class' function */
2483 ADDOP(c, LOAD_BUILD_CLASS);
2484
2485 /* 3. load a function (or closure) made from the code object */
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002486 if (!compiler_make_closure(c, co, 0, NULL)) {
2487 Py_DECREF(co);
2488 return 0;
2489 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 Py_DECREF(co);
2491
2492 /* 4. load class name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002493 ADDOP_LOAD_CONST(c, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494
2495 /* 5. generate the rest of the code for the call */
Pablo Galindoa5634c42020-09-16 19:42:00 +01002496 if (!compiler_call_helper(c, 2, s->v.ClassDef.bases, s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002497 return 0;
2498
2499 /* 6. apply decorators */
2500 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2501 ADDOP_I(c, CALL_FUNCTION, 1);
2502 }
2503
2504 /* 7. store into <name> */
2505 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2506 return 0;
2507 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002508}
2509
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02002510/* Return 0 if the expression is a constant value except named singletons.
2511 Return 1 otherwise. */
2512static int
2513check_is_arg(expr_ty e)
2514{
2515 if (e->kind != Constant_kind) {
2516 return 1;
2517 }
2518 PyObject *value = e->v.Constant.value;
2519 return (value == Py_None
2520 || value == Py_False
2521 || value == Py_True
2522 || value == Py_Ellipsis);
2523}
2524
2525/* Check operands of identity chacks ("is" and "is not").
2526 Emit a warning if any operand is a constant except named singletons.
2527 Return 0 on error.
2528 */
2529static int
2530check_compare(struct compiler *c, expr_ty e)
2531{
2532 Py_ssize_t i, n;
2533 int left = check_is_arg(e->v.Compare.left);
2534 n = asdl_seq_LEN(e->v.Compare.ops);
2535 for (i = 0; i < n; i++) {
2536 cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i);
2537 int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2538 if (op == Is || op == IsNot) {
2539 if (!right || !left) {
2540 const char *msg = (op == Is)
2541 ? "\"is\" with a literal. Did you mean \"==\"?"
2542 : "\"is not\" with a literal. Did you mean \"!=\"?";
2543 return compiler_warn(c, msg);
2544 }
2545 }
2546 left = right;
2547 }
2548 return 1;
2549}
2550
Mark Shannon9af0e472020-01-14 10:12:45 +00002551static int compiler_addcompare(struct compiler *c, cmpop_ty op)
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002552{
Mark Shannon9af0e472020-01-14 10:12:45 +00002553 int cmp;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002554 switch (op) {
2555 case Eq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002556 cmp = Py_EQ;
2557 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002558 case NotEq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002559 cmp = Py_NE;
2560 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002561 case Lt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002562 cmp = Py_LT;
2563 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002564 case LtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002565 cmp = Py_LE;
2566 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002567 case Gt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002568 cmp = Py_GT;
2569 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002570 case GtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002571 cmp = Py_GE;
2572 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002573 case Is:
Mark Shannon9af0e472020-01-14 10:12:45 +00002574 ADDOP_I(c, IS_OP, 0);
2575 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002576 case IsNot:
Mark Shannon9af0e472020-01-14 10:12:45 +00002577 ADDOP_I(c, IS_OP, 1);
2578 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002579 case In:
Mark Shannon9af0e472020-01-14 10:12:45 +00002580 ADDOP_I(c, CONTAINS_OP, 0);
2581 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002582 case NotIn:
Mark Shannon9af0e472020-01-14 10:12:45 +00002583 ADDOP_I(c, CONTAINS_OP, 1);
2584 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002585 default:
Mark Shannon9af0e472020-01-14 10:12:45 +00002586 Py_UNREACHABLE();
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002587 }
Mark Shannon9af0e472020-01-14 10:12:45 +00002588 ADDOP_I(c, COMPARE_OP, cmp);
2589 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002590}
2591
Mark Shannon9af0e472020-01-14 10:12:45 +00002592
2593
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002594static int
2595compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2596{
2597 switch (e->kind) {
2598 case UnaryOp_kind:
2599 if (e->v.UnaryOp.op == Not)
2600 return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2601 /* fallback to general implementation */
2602 break;
2603 case BoolOp_kind: {
Pablo Galindoa5634c42020-09-16 19:42:00 +01002604 asdl_expr_seq *s = e->v.BoolOp.values;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002605 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2606 assert(n >= 0);
2607 int cond2 = e->v.BoolOp.op == Or;
2608 basicblock *next2 = next;
2609 if (!cond2 != !cond) {
2610 next2 = compiler_new_block(c);
2611 if (next2 == NULL)
2612 return 0;
2613 }
2614 for (i = 0; i < n; ++i) {
2615 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2616 return 0;
2617 }
2618 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2619 return 0;
2620 if (next2 != next)
2621 compiler_use_next_block(c, next2);
2622 return 1;
2623 }
2624 case IfExp_kind: {
2625 basicblock *end, *next2;
2626 end = compiler_new_block(c);
2627 if (end == NULL)
2628 return 0;
2629 next2 = compiler_new_block(c);
2630 if (next2 == NULL)
2631 return 0;
2632 if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2633 return 0;
2634 if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2635 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002636 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002637 compiler_use_next_block(c, next2);
2638 if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2639 return 0;
2640 compiler_use_next_block(c, end);
2641 return 1;
2642 }
2643 case Compare_kind: {
2644 Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2645 if (n > 0) {
Serhiy Storchaka45835252019-02-16 08:29:46 +02002646 if (!check_compare(c, e)) {
2647 return 0;
2648 }
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002649 basicblock *cleanup = compiler_new_block(c);
2650 if (cleanup == NULL)
2651 return 0;
2652 VISIT(c, expr, e->v.Compare.left);
2653 for (i = 0; i < n; i++) {
2654 VISIT(c, expr,
2655 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2656 ADDOP(c, DUP_TOP);
2657 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00002658 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01002659 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002660 NEXT_BLOCK(c);
2661 }
2662 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00002663 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Mark Shannon582aaf12020-08-04 17:30:11 +01002664 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Mark Shannon266b4622020-11-17 19:30:14 +00002665 NEXT_BLOCK(c);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002666 basicblock *end = compiler_new_block(c);
2667 if (end == NULL)
2668 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002669 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002670 compiler_use_next_block(c, cleanup);
2671 ADDOP(c, POP_TOP);
2672 if (!cond) {
Mark Shannon127dde52021-01-04 18:06:55 +00002673 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002674 }
2675 compiler_use_next_block(c, end);
2676 return 1;
2677 }
2678 /* fallback to general implementation */
2679 break;
2680 }
2681 default:
2682 /* fallback to general implementation */
2683 break;
2684 }
2685
2686 /* general implementation */
2687 VISIT(c, expr, e);
Mark Shannon582aaf12020-08-04 17:30:11 +01002688 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Mark Shannon266b4622020-11-17 19:30:14 +00002689 NEXT_BLOCK(c);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002690 return 1;
2691}
2692
2693static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002694compiler_ifexp(struct compiler *c, expr_ty e)
2695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002696 basicblock *end, *next;
2697
2698 assert(e->kind == IfExp_kind);
2699 end = compiler_new_block(c);
2700 if (end == NULL)
2701 return 0;
2702 next = compiler_new_block(c);
2703 if (next == NULL)
2704 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002705 if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2706 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002707 VISIT(c, expr, e->v.IfExp.body);
Mark Shannon127dde52021-01-04 18:06:55 +00002708 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002709 compiler_use_next_block(c, next);
2710 VISIT(c, expr, e->v.IfExp.orelse);
2711 compiler_use_next_block(c, end);
2712 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002713}
2714
2715static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002716compiler_lambda(struct compiler *c, expr_ty e)
2717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002718 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002719 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002720 static identifier name;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002721 Py_ssize_t funcflags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002722 arguments_ty args = e->v.Lambda.args;
2723 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002724
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002725 if (!compiler_check_debug_args(c, args))
2726 return 0;
2727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002728 if (!name) {
2729 name = PyUnicode_InternFromString("<lambda>");
2730 if (!name)
2731 return 0;
2732 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002733
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002734 funcflags = compiler_default_arguments(c, args);
2735 if (funcflags == -1) {
2736 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002737 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002738
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002739 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002740 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002741 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 /* Make None the first constant, so the lambda can't have a
2744 docstring. */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002745 if (compiler_add_const(c, Py_None) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002748 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002749 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002750 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2751 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2752 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002753 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 }
2755 else {
2756 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002757 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002758 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002759 qualname = c->u->u_qualname;
2760 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 compiler_exit_scope(c);
Pablo Galindo7fdab832021-01-29 22:40:59 +00002762 if (co == NULL) {
2763 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002764 return 0;
Pablo Galindo7fdab832021-01-29 22:40:59 +00002765 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002766
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002767 if (!compiler_make_closure(c, co, funcflags, qualname)) {
2768 Py_DECREF(qualname);
2769 Py_DECREF(co);
2770 return 0;
2771 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002772 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 Py_DECREF(co);
2774
2775 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776}
2777
2778static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002779compiler_if(struct compiler *c, stmt_ty s)
2780{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 basicblock *end, *next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002782 assert(s->kind == If_kind);
2783 end = compiler_new_block(c);
Mark Shannon8473cf82020-12-15 11:07:50 +00002784 if (end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 return 0;
Mark Shannon8473cf82020-12-15 11:07:50 +00002786 }
2787 if (asdl_seq_LEN(s->v.If.orelse)) {
2788 next = compiler_new_block(c);
2789 if (next == NULL) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002790 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002791 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002792 }
2793 else {
2794 next = end;
2795 }
2796 if (!compiler_jump_if(c, s->v.If.test, next, 0)) {
2797 return 0;
2798 }
2799 VISIT_SEQ(c, stmt, s->v.If.body);
2800 if (asdl_seq_LEN(s->v.If.orelse)) {
Mark Shannon127dde52021-01-04 18:06:55 +00002801 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Mark Shannon8473cf82020-12-15 11:07:50 +00002802 compiler_use_next_block(c, next);
2803 VISIT_SEQ(c, stmt, s->v.If.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 }
2805 compiler_use_next_block(c, end);
2806 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002807}
2808
2809static int
2810compiler_for(struct compiler *c, stmt_ty s)
2811{
Mark Shannon5977a792020-12-02 13:31:40 +00002812 basicblock *start, *body, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 start = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002815 body = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816 cleanup = compiler_new_block(c);
2817 end = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002818 if (start == NULL || body == NULL || end == NULL || cleanup == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002819 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002820 }
2821 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002822 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002823 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 VISIT(c, expr, s->v.For.iter);
2825 ADDOP(c, GET_ITER);
2826 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01002827 ADDOP_JUMP(c, FOR_ITER, cleanup);
Mark Shannon5977a792020-12-02 13:31:40 +00002828 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 VISIT(c, expr, s->v.For.target);
2830 VISIT_SEQ(c, stmt, s->v.For.body);
Mark Shannonf5e97b72020-12-14 11:28:39 +00002831 /* Mark jump as artificial */
2832 c->u->u_lineno = -1;
Mark Shannon582aaf12020-08-04 17:30:11 +01002833 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 compiler_use_next_block(c, cleanup);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002835
2836 compiler_pop_fblock(c, FOR_LOOP, start);
2837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002838 VISIT_SEQ(c, stmt, s->v.For.orelse);
2839 compiler_use_next_block(c, end);
2840 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002841}
2842
Yury Selivanov75445082015-05-11 22:57:16 -04002843
2844static int
2845compiler_async_for(struct compiler *c, stmt_ty s)
2846{
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002847 basicblock *start, *except, *end;
Pablo Galindo90235812020-03-15 04:29:22 +00002848 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07002849 c->u->u_ste->ste_coroutine = 1;
2850 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
Zsolt Dollensteine2396502018-04-27 08:58:56 -07002851 return compiler_error(c, "'async for' outside async function");
2852 }
2853
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002854 start = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002855 except = compiler_new_block(c);
2856 end = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002857
Mark Shannonfee55262019-11-21 09:11:43 +00002858 if (start == NULL || except == NULL || end == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002859 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002860 }
Yury Selivanov75445082015-05-11 22:57:16 -04002861 VISIT(c, expr, s->v.AsyncFor.iter);
2862 ADDOP(c, GET_AITER);
Yury Selivanov75445082015-05-11 22:57:16 -04002863
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002864 compiler_use_next_block(c, start);
Mark Shannonfee55262019-11-21 09:11:43 +00002865 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002866 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002867 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002868 /* SETUP_FINALLY to guard the __anext__ call */
Mark Shannon582aaf12020-08-04 17:30:11 +01002869 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov75445082015-05-11 22:57:16 -04002870 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002871 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04002872 ADDOP(c, YIELD_FROM);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002873 ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */
Yury Selivanov75445082015-05-11 22:57:16 -04002874
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002875 /* Success block for __anext__ */
2876 VISIT(c, expr, s->v.AsyncFor.target);
2877 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002878 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002879
2880 compiler_pop_fblock(c, FOR_LOOP, start);
Yury Selivanov75445082015-05-11 22:57:16 -04002881
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002882 /* Except block for __anext__ */
Yury Selivanov75445082015-05-11 22:57:16 -04002883 compiler_use_next_block(c, except);
Mark Shannon877df852020-11-12 09:43:29 +00002884
2885 c->u->u_lineno = -1;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002886 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov75445082015-05-11 22:57:16 -04002887
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002888 /* `else` block */
Yury Selivanov75445082015-05-11 22:57:16 -04002889 VISIT_SEQ(c, stmt, s->v.For.orelse);
2890
2891 compiler_use_next_block(c, end);
2892
2893 return 1;
2894}
2895
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002896static int
2897compiler_while(struct compiler *c, stmt_ty s)
2898{
Mark Shannon266b4622020-11-17 19:30:14 +00002899 basicblock *loop, *body, *end, *anchor = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 loop = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002901 body = compiler_new_block(c);
2902 anchor = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 end = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002904 if (loop == NULL || body == NULL || anchor == NULL || end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 compiler_use_next_block(c, loop);
Mark Shannon266b4622020-11-17 19:30:14 +00002908 if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002911 if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) {
2912 return 0;
Mark Shannon266b4622020-11-17 19:30:14 +00002913 }
2914
2915 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002916 VISIT_SEQ(c, stmt, s->v.While.body);
Mark Shannon8473cf82020-12-15 11:07:50 +00002917 SET_LOC(c, s);
2918 if (!compiler_jump_if(c, s->v.While.test, body, 1)) {
2919 return 0;
2920 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002921
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002922 compiler_pop_fblock(c, WHILE_LOOP, loop);
2923
Mark Shannon266b4622020-11-17 19:30:14 +00002924 compiler_use_next_block(c, anchor);
2925 if (s->v.While.orelse) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002926 VISIT_SEQ(c, stmt, s->v.While.orelse);
Mark Shannon266b4622020-11-17 19:30:14 +00002927 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002928 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002930 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002931}
2932
2933static int
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002934compiler_return(struct compiler *c, stmt_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002935{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002936 int preserve_tos = ((s->v.Return.value != NULL) &&
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002937 (s->v.Return.value->kind != Constant_kind));
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002938 if (c->u->u_ste->ste_type != FunctionBlock)
2939 return compiler_error(c, "'return' outside function");
2940 if (s->v.Return.value != NULL &&
2941 c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2942 {
2943 return compiler_error(
2944 c, "'return' with value in async generator");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002945 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002946 if (preserve_tos) {
2947 VISIT(c, expr, s->v.Return.value);
Mark Shannon5274b682020-12-16 13:07:01 +00002948 } else {
2949 /* Emit instruction with line number for expression */
2950 if (s->v.Return.value != NULL) {
2951 SET_LOC(c, s->v.Return.value);
2952 ADDOP(c, NOP);
2953 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002954 }
Mark Shannonfee55262019-11-21 09:11:43 +00002955 if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL))
2956 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002957 if (s->v.Return.value == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002958 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002959 }
2960 else if (!preserve_tos) {
Mark Shannon5274b682020-12-16 13:07:01 +00002961 ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002962 }
2963 ADDOP(c, RETURN_VALUE);
Mark Shannon266b4622020-11-17 19:30:14 +00002964 NEXT_BLOCK(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002966 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002967}
2968
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002969static int
2970compiler_break(struct compiler *c)
2971{
Mark Shannonfee55262019-11-21 09:11:43 +00002972 struct fblockinfo *loop = NULL;
2973 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
2974 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002975 }
Mark Shannonfee55262019-11-21 09:11:43 +00002976 if (loop == NULL) {
2977 return compiler_error(c, "'break' outside loop");
2978 }
2979 if (!compiler_unwind_fblock(c, loop, 0)) {
2980 return 0;
2981 }
Mark Shannon582aaf12020-08-04 17:30:11 +01002982 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit);
Mark Shannon266b4622020-11-17 19:30:14 +00002983 NEXT_BLOCK(c);
Mark Shannonfee55262019-11-21 09:11:43 +00002984 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002985}
2986
2987static int
2988compiler_continue(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, "'continue' not properly in loop");
2996 }
Mark Shannon582aaf12020-08-04 17:30:11 +01002997 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block);
Mark Shannon266b4622020-11-17 19:30:14 +00002998 NEXT_BLOCK(c)
Mark Shannonfee55262019-11-21 09:11:43 +00002999 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003000}
3001
3002
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003003/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003004
3005 SETUP_FINALLY L
3006 <code for body>
3007 POP_BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00003008 <code for finalbody>
3009 JUMP E
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003010 L:
3011 <code for finalbody>
Mark Shannonfee55262019-11-21 09:11:43 +00003012 E:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003014 The special instructions use the block stack. Each block
3015 stack entry contains the instruction that created it (here
3016 SETUP_FINALLY), the level of the value stack at the time the
3017 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003019 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 Pushes the current value stack level and the label
3021 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003022 POP_BLOCK:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003023 Pops en entry from the block stack.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003025 The block stack is unwound when an exception is raised:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003026 when a SETUP_FINALLY entry is found, the raised and the caught
3027 exceptions are pushed onto the value stack (and the exception
3028 condition is cleared), and the interpreter jumps to the label
3029 gotten from the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003030*/
3031
3032static int
3033compiler_try_finally(struct compiler *c, stmt_ty s)
3034{
Mark Shannonfee55262019-11-21 09:11:43 +00003035 basicblock *body, *end, *exit;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 body = compiler_new_block(c);
3038 end = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00003039 exit = compiler_new_block(c);
3040 if (body == NULL || end == NULL || exit == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003042
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003043 /* `try` block */
Mark Shannon582aaf12020-08-04 17:30:11 +01003044 ADDOP_JUMP(c, SETUP_FINALLY, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003045 compiler_use_next_block(c, body);
Mark Shannonfee55262019-11-21 09:11:43 +00003046 if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003047 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003048 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
3049 if (!compiler_try_except(c, s))
3050 return 0;
3051 }
3052 else {
3053 VISIT_SEQ(c, stmt, s->v.Try.body);
3054 }
Mark Shannon3bd60352021-01-13 12:05:43 +00003055 ADDOP_NOLINE(c, POP_BLOCK);
Mark Shannonfee55262019-11-21 09:11:43 +00003056 compiler_pop_fblock(c, FINALLY_TRY, body);
3057 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Mark Shannon127dde52021-01-04 18:06:55 +00003058 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00003059 /* `finally` block */
3060 compiler_use_next_block(c, end);
3061 if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL))
3062 return 0;
3063 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3064 compiler_pop_fblock(c, FINALLY_END, end);
Mark Shannonbf353f32020-12-17 13:55:28 +00003065 ADDOP_I(c, RERAISE, 0);
Mark Shannonfee55262019-11-21 09:11:43 +00003066 compiler_use_next_block(c, exit);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003068}
3069
3070/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003071 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003072 (The contents of the value stack is shown in [], with the top
3073 at the right; 'tb' is trace-back info, 'val' the exception's
3074 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003075
3076 Value stack Label Instruction Argument
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003077 [] SETUP_FINALLY L1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003078 [] <code for S>
3079 [] POP_BLOCK
3080 [] JUMP_FORWARD L0
3081
3082 [tb, val, exc] L1: DUP )
3083 [tb, val, exc, exc] <evaluate E1> )
Mark Shannon9af0e472020-01-14 10:12:45 +00003084 [tb, val, exc, exc, E1] JUMP_IF_NOT_EXC_MATCH L2 ) only if E1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003085 [tb, val, exc] POP
3086 [tb, val] <assign to V1> (or POP if no V1)
3087 [tb] POP
3088 [] <code for S1>
3089 JUMP_FORWARD L0
3090
3091 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003092 .............................etc.......................
3093
Mark Shannonfee55262019-11-21 09:11:43 +00003094 [tb, val, exc] Ln+1: RERAISE # re-raise exception
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003095
3096 [] L0: <next statement>
3097
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003098 Of course, parts are not generated if Vi or Ei is not present.
3099*/
3100static int
3101compiler_try_except(struct compiler *c, stmt_ty s)
3102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003104 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 body = compiler_new_block(c);
3107 except = compiler_new_block(c);
3108 orelse = compiler_new_block(c);
3109 end = compiler_new_block(c);
3110 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
3111 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01003112 ADDOP_JUMP(c, SETUP_FINALLY, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113 compiler_use_next_block(c, body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003114 if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003115 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003116 VISIT_SEQ(c, stmt, s->v.Try.body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003117 compiler_pop_fblock(c, TRY_EXCEPT, body);
Mark Shannon3bd60352021-01-13 12:05:43 +00003118 ADDOP_NOLINE(c, POP_BLOCK);
3119 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003120 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 compiler_use_next_block(c, except);
Mark Shannon02d126a2020-09-25 14:04:19 +01003122 /* Runtime will push a block here, so we need to account for that */
3123 if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL))
3124 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003125 for (i = 0; i < n; i++) {
3126 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003127 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003128 if (!handler->v.ExceptHandler.type && i < n-1)
3129 return compiler_error(c, "default 'except:' must be last");
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003130 SET_LOC(c, handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131 except = compiler_new_block(c);
3132 if (except == NULL)
3133 return 0;
3134 if (handler->v.ExceptHandler.type) {
3135 ADDOP(c, DUP_TOP);
3136 VISIT(c, expr, handler->v.ExceptHandler.type);
Mark Shannon582aaf12020-08-04 17:30:11 +01003137 ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except);
Mark Shannon266b4622020-11-17 19:30:14 +00003138 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 }
3140 ADDOP(c, POP_TOP);
3141 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003142 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00003143
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003144 cleanup_end = compiler_new_block(c);
3145 cleanup_body = compiler_new_block(c);
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003146 if (cleanup_end == NULL || cleanup_body == NULL) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003147 return 0;
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003148 }
Guido van Rossumb940e112007-01-10 16:19:56 +00003149
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003150 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3151 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003152
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003153 /*
3154 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003155 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003156 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003157 try:
3158 # body
3159 finally:
Chris Angelicoad098b62019-05-21 23:34:19 +10003160 name = None # in case body contains "del name"
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003161 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003162 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003163
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003164 /* second try: */
Mark Shannon582aaf12020-08-04 17:30:11 +01003165 ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003166 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003167 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003168 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003169
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003170 /* second # body */
3171 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003172 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003173 ADDOP(c, POP_BLOCK);
3174 ADDOP(c, POP_EXCEPT);
Mark Shannon877df852020-11-12 09:43:29 +00003175 /* name = None; del name; # Mark as artificial */
3176 c->u->u_lineno = -1;
Mark Shannonfee55262019-11-21 09:11:43 +00003177 ADDOP_LOAD_CONST(c, Py_None);
3178 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3179 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Mark Shannon582aaf12020-08-04 17:30:11 +01003180 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181
Mark Shannonfee55262019-11-21 09:11:43 +00003182 /* except: */
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003183 compiler_use_next_block(c, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003184
Mark Shannon877df852020-11-12 09:43:29 +00003185 /* name = None; del name; # Mark as artificial */
3186 c->u->u_lineno = -1;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003187 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003188 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003189 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190
Mark Shannonbf353f32020-12-17 13:55:28 +00003191 ADDOP_I(c, RERAISE, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003192 }
3193 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003194 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003195
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003196 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05003197 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003198 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003199
Guido van Rossumb940e112007-01-10 16:19:56 +00003200 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003201 ADDOP(c, POP_TOP);
3202 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003203 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003204 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003205 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003206 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannon127dde52021-01-04 18:06:55 +00003207 /* name = None; del name; # Mark as artificial */
3208 c->u->u_lineno = -1;
Mark Shannonfee55262019-11-21 09:11:43 +00003209 ADDOP(c, POP_EXCEPT);
Mark Shannon582aaf12020-08-04 17:30:11 +01003210 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003212 compiler_use_next_block(c, except);
3213 }
Mark Shannon02d126a2020-09-25 14:04:19 +01003214 compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL);
Mark Shannonf2dbfd72020-12-21 13:53:50 +00003215 /* Mark as artificial */
3216 c->u->u_lineno = -1;
Mark Shannonbf353f32020-12-17 13:55:28 +00003217 ADDOP_I(c, RERAISE, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003218 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003219 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003220 compiler_use_next_block(c, end);
3221 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003222}
3223
3224static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003225compiler_try(struct compiler *c, stmt_ty s) {
3226 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
3227 return compiler_try_finally(c, s);
3228 else
3229 return compiler_try_except(c, s);
3230}
3231
3232
3233static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003234compiler_import_as(struct compiler *c, identifier name, identifier asname)
3235{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003236 /* The IMPORT_NAME opcode was already generated. This function
3237 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003239 If there is a dot in name, we need to split it and emit a
Serhiy Storchakaf93234b2017-05-09 22:31:05 +03003240 IMPORT_FROM for each name.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003241 */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003242 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
3243 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003244 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003245 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003246 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003247 /* Consume the base module name to get the first attribute */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003248 while (1) {
3249 Py_ssize_t pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250 PyObject *attr;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003251 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003252 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003253 return 0;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003254 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003255 if (!attr)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003256 return 0;
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003257 ADDOP_N(c, IMPORT_FROM, attr, names);
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003258 if (dot == -1) {
3259 break;
3260 }
3261 ADDOP(c, ROT_TWO);
3262 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 }
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003264 if (!compiler_nameop(c, asname, Store)) {
3265 return 0;
3266 }
3267 ADDOP(c, POP_TOP);
3268 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003269 }
3270 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003271}
3272
3273static int
3274compiler_import(struct compiler *c, stmt_ty s)
3275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003276 /* The Import node stores a module name like a.b.c as a single
3277 string. This is convenient for all cases except
3278 import a.b.c as d
3279 where we need to parse that string to extract the individual
3280 module names.
3281 XXX Perhaps change the representation to make this case simpler?
3282 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01003283 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003284
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003285 PyObject *zero = _PyLong_GetZero(); // borrowed reference
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003286 for (i = 0; i < n; i++) {
3287 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
3288 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003289
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003290 ADDOP_LOAD_CONST(c, zero);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003291 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003292 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003294 if (alias->asname) {
3295 r = compiler_import_as(c, alias->name, alias->asname);
3296 if (!r)
3297 return r;
3298 }
3299 else {
3300 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003301 Py_ssize_t dot = PyUnicode_FindChar(
3302 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02003303 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003304 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02003305 if (tmp == NULL)
3306 return 0;
3307 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003308 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003309 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003310 Py_DECREF(tmp);
3311 }
3312 if (!r)
3313 return r;
3314 }
3315 }
3316 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003317}
3318
3319static int
3320compiler_from_import(struct compiler *c, stmt_ty s)
3321{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003322 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003323 PyObject *names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 if (!empty_string) {
3327 empty_string = PyUnicode_FromString("");
3328 if (!empty_string)
3329 return 0;
3330 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003331
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003332 ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02003333
3334 names = PyTuple_New(n);
3335 if (!names)
3336 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003338 /* build up the names */
3339 for (i = 0; i < n; i++) {
3340 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3341 Py_INCREF(alias->name);
3342 PyTuple_SET_ITEM(names, i, alias->name);
3343 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003345 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003346 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003347 Py_DECREF(names);
3348 return compiler_error(c, "from __future__ imports must occur "
3349 "at the beginning of the file");
3350 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003351 ADDOP_LOAD_CONST_NEW(c, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003353 if (s->v.ImportFrom.module) {
3354 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
3355 }
3356 else {
3357 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
3358 }
3359 for (i = 0; i < n; i++) {
3360 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3361 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003362
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003363 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003364 assert(n == 1);
3365 ADDOP(c, IMPORT_STAR);
3366 return 1;
3367 }
3368
3369 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
3370 store_name = alias->name;
3371 if (alias->asname)
3372 store_name = alias->asname;
3373
3374 if (!compiler_nameop(c, store_name, Store)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003375 return 0;
3376 }
3377 }
3378 /* remove imported module */
3379 ADDOP(c, POP_TOP);
3380 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003381}
3382
3383static int
3384compiler_assert(struct compiler *c, stmt_ty s)
3385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003386 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003387
tsukasa-aua8ef4572021-03-16 22:14:41 +11003388 /* Always emit a warning if the test is a non-zero length tuple */
3389 if ((s->v.Assert.test->kind == Tuple_kind &&
3390 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) ||
3391 (s->v.Assert.test->kind == Constant_kind &&
3392 PyTuple_Check(s->v.Assert.test->v.Constant.value) &&
3393 PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0))
Serhiy Storchakad31e7732018-10-21 10:09:39 +03003394 {
3395 if (!compiler_warn(c, "assertion is always true, "
3396 "perhaps remove parentheses?"))
3397 {
Victor Stinner14e461d2013-08-26 22:28:21 +02003398 return 0;
3399 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003400 }
tsukasa-aua8ef4572021-03-16 22:14:41 +11003401 if (c->c_optimize)
3402 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003403 end = compiler_new_block(c);
3404 if (end == NULL)
3405 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003406 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
3407 return 0;
Zackery Spytzce6a0702019-08-25 03:44:09 -06003408 ADDOP(c, LOAD_ASSERTION_ERROR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003409 if (s->v.Assert.msg) {
3410 VISIT(c, expr, s->v.Assert.msg);
3411 ADDOP_I(c, CALL_FUNCTION, 1);
3412 }
3413 ADDOP_I(c, RAISE_VARARGS, 1);
3414 compiler_use_next_block(c, end);
3415 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003416}
3417
3418static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003419compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3420{
3421 if (c->c_interactive && c->c_nestlevel <= 1) {
3422 VISIT(c, expr, value);
3423 ADDOP(c, PRINT_EXPR);
3424 return 1;
3425 }
3426
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003427 if (value->kind == Constant_kind) {
Victor Stinner15a30952016-02-08 22:45:06 +01003428 /* ignore constant statement */
Mark Shannon877df852020-11-12 09:43:29 +00003429 ADDOP(c, NOP);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003430 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003431 }
3432
3433 VISIT(c, expr, value);
Mark Shannonc5440932021-03-15 14:24:25 +00003434 /* Mark POP_TOP as artificial */
3435 c->u->u_lineno = -1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003436 ADDOP(c, POP_TOP);
3437 return 1;
3438}
3439
3440static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003441compiler_visit_stmt(struct compiler *c, stmt_ty s)
3442{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003443 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445 /* Always assign a lineno to the next instruction for a stmt. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003446 SET_LOC(c, s);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003448 switch (s->kind) {
3449 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04003450 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003451 case ClassDef_kind:
3452 return compiler_class(c, s);
3453 case Return_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003454 return compiler_return(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003455 case Delete_kind:
3456 VISIT_SEQ(c, expr, s->v.Delete.targets)
3457 break;
3458 case Assign_kind:
3459 n = asdl_seq_LEN(s->v.Assign.targets);
3460 VISIT(c, expr, s->v.Assign.value);
3461 for (i = 0; i < n; i++) {
3462 if (i < n - 1)
3463 ADDOP(c, DUP_TOP);
3464 VISIT(c, expr,
3465 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3466 }
3467 break;
3468 case AugAssign_kind:
3469 return compiler_augassign(c, s);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003470 case AnnAssign_kind:
3471 return compiler_annassign(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003472 case For_kind:
3473 return compiler_for(c, s);
3474 case While_kind:
3475 return compiler_while(c, s);
3476 case If_kind:
3477 return compiler_if(c, s);
Brandt Bucher145bf262021-02-26 14:51:55 -08003478 case Match_kind:
3479 return compiler_match(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 case Raise_kind:
3481 n = 0;
3482 if (s->v.Raise.exc) {
3483 VISIT(c, expr, s->v.Raise.exc);
3484 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003485 if (s->v.Raise.cause) {
3486 VISIT(c, expr, s->v.Raise.cause);
3487 n++;
3488 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01003490 ADDOP_I(c, RAISE_VARARGS, (int)n);
Mark Shannon266b4622020-11-17 19:30:14 +00003491 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003492 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003493 case Try_kind:
3494 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003495 case Assert_kind:
3496 return compiler_assert(c, s);
3497 case Import_kind:
3498 return compiler_import(c, s);
3499 case ImportFrom_kind:
3500 return compiler_from_import(c, s);
3501 case Global_kind:
3502 case Nonlocal_kind:
3503 break;
3504 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003505 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003506 case Pass_kind:
Mark Shannon877df852020-11-12 09:43:29 +00003507 ADDOP(c, NOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 break;
3509 case Break_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003510 return compiler_break(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003511 case Continue_kind:
3512 return compiler_continue(c);
3513 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003514 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04003515 case AsyncFunctionDef_kind:
3516 return compiler_function(c, s, 1);
3517 case AsyncWith_kind:
3518 return compiler_async_with(c, s, 0);
3519 case AsyncFor_kind:
3520 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003521 }
Yury Selivanov75445082015-05-11 22:57:16 -04003522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003523 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003524}
3525
3526static int
3527unaryop(unaryop_ty op)
3528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003529 switch (op) {
3530 case Invert:
3531 return UNARY_INVERT;
3532 case Not:
3533 return UNARY_NOT;
3534 case UAdd:
3535 return UNARY_POSITIVE;
3536 case USub:
3537 return UNARY_NEGATIVE;
3538 default:
3539 PyErr_Format(PyExc_SystemError,
3540 "unary op %d should not be possible", op);
3541 return 0;
3542 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003543}
3544
3545static int
Andy Lester76d58772020-03-10 21:18:12 -05003546binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003547{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 switch (op) {
3549 case Add:
3550 return BINARY_ADD;
3551 case Sub:
3552 return BINARY_SUBTRACT;
3553 case Mult:
3554 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003555 case MatMult:
3556 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003557 case Div:
3558 return BINARY_TRUE_DIVIDE;
3559 case Mod:
3560 return BINARY_MODULO;
3561 case Pow:
3562 return BINARY_POWER;
3563 case LShift:
3564 return BINARY_LSHIFT;
3565 case RShift:
3566 return BINARY_RSHIFT;
3567 case BitOr:
3568 return BINARY_OR;
3569 case BitXor:
3570 return BINARY_XOR;
3571 case BitAnd:
3572 return BINARY_AND;
3573 case FloorDiv:
3574 return BINARY_FLOOR_DIVIDE;
3575 default:
3576 PyErr_Format(PyExc_SystemError,
3577 "binary op %d should not be possible", op);
3578 return 0;
3579 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003580}
3581
3582static int
Andy Lester76d58772020-03-10 21:18:12 -05003583inplace_binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003584{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003585 switch (op) {
3586 case Add:
3587 return INPLACE_ADD;
3588 case Sub:
3589 return INPLACE_SUBTRACT;
3590 case Mult:
3591 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003592 case MatMult:
3593 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003594 case Div:
3595 return INPLACE_TRUE_DIVIDE;
3596 case Mod:
3597 return INPLACE_MODULO;
3598 case Pow:
3599 return INPLACE_POWER;
3600 case LShift:
3601 return INPLACE_LSHIFT;
3602 case RShift:
3603 return INPLACE_RSHIFT;
3604 case BitOr:
3605 return INPLACE_OR;
3606 case BitXor:
3607 return INPLACE_XOR;
3608 case BitAnd:
3609 return INPLACE_AND;
3610 case FloorDiv:
3611 return INPLACE_FLOOR_DIVIDE;
3612 default:
3613 PyErr_Format(PyExc_SystemError,
3614 "inplace binary op %d should not be possible", op);
3615 return 0;
3616 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003617}
3618
3619static int
3620compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3621{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003622 int op, scope;
3623 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003624 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003626 PyObject *dict = c->u->u_names;
3627 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003628
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003629 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3630 !_PyUnicode_EqualToASCIIString(name, "True") &&
3631 !_PyUnicode_EqualToASCIIString(name, "False"));
Benjamin Peterson70b224d2012-12-06 17:49:58 -05003632
Pablo Galindoc5fc1562020-04-22 23:29:27 +01003633 if (forbidden_name(c, name, ctx))
3634 return 0;
3635
Serhiy Storchakabd6ec4d2017-12-18 14:29:12 +02003636 mangled = _Py_Mangle(c->u->u_private, name);
3637 if (!mangled)
3638 return 0;
3639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003640 op = 0;
3641 optype = OP_NAME;
Victor Stinner28ad12f2021-03-19 12:41:49 +01003642 scope = _PyST_GetScope(c->u->u_ste, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003643 switch (scope) {
3644 case FREE:
3645 dict = c->u->u_freevars;
3646 optype = OP_DEREF;
3647 break;
3648 case CELL:
3649 dict = c->u->u_cellvars;
3650 optype = OP_DEREF;
3651 break;
3652 case LOCAL:
3653 if (c->u->u_ste->ste_type == FunctionBlock)
3654 optype = OP_FAST;
3655 break;
3656 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04003657 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003658 optype = OP_GLOBAL;
3659 break;
3660 case GLOBAL_EXPLICIT:
3661 optype = OP_GLOBAL;
3662 break;
3663 default:
3664 /* scope can be 0 */
3665 break;
3666 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003668 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003669 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003671 switch (optype) {
3672 case OP_DEREF:
3673 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003674 case Load:
3675 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3676 break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003677 case Store: op = STORE_DEREF; break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003678 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003679 }
3680 break;
3681 case OP_FAST:
3682 switch (ctx) {
3683 case Load: op = LOAD_FAST; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003684 case Store: op = STORE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003685 case Del: op = DELETE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003686 }
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003687 ADDOP_N(c, op, mangled, varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003688 return 1;
3689 case OP_GLOBAL:
3690 switch (ctx) {
3691 case Load: op = LOAD_GLOBAL; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003692 case Store: op = STORE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003693 case Del: op = DELETE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003694 }
3695 break;
3696 case OP_NAME:
3697 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00003698 case Load: op = LOAD_NAME; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003699 case Store: op = STORE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003700 case Del: op = DELETE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003701 }
3702 break;
3703 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003705 assert(op);
Andy Lester76d58772020-03-10 21:18:12 -05003706 arg = compiler_add_o(dict, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003707 Py_DECREF(mangled);
3708 if (arg < 0)
3709 return 0;
3710 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003711}
3712
3713static int
3714compiler_boolop(struct compiler *c, expr_ty e)
3715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003716 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003717 int jumpi;
3718 Py_ssize_t i, n;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003719 asdl_expr_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003721 assert(e->kind == BoolOp_kind);
3722 if (e->v.BoolOp.op == And)
3723 jumpi = JUMP_IF_FALSE_OR_POP;
3724 else
3725 jumpi = JUMP_IF_TRUE_OR_POP;
3726 end = compiler_new_block(c);
3727 if (end == NULL)
3728 return 0;
3729 s = e->v.BoolOp.values;
3730 n = asdl_seq_LEN(s) - 1;
3731 assert(n >= 0);
3732 for (i = 0; i < n; ++i) {
3733 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003734 ADDOP_JUMP(c, jumpi, end);
Mark Shannon6e8128f2020-07-30 10:03:00 +01003735 basicblock *next = compiler_new_block(c);
3736 if (next == NULL) {
3737 return 0;
3738 }
3739 compiler_use_next_block(c, next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003740 }
3741 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3742 compiler_use_next_block(c, end);
3743 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003744}
3745
3746static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003747starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
Mark Shannon13bc1392020-01-23 09:25:17 +00003748 int build, int add, int extend, int tuple)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003749{
3750 Py_ssize_t n = asdl_seq_LEN(elts);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003751 if (n > 2 && are_all_items_const(elts, 0, n)) {
3752 PyObject *folded = PyTuple_New(n);
3753 if (folded == NULL) {
3754 return 0;
3755 }
3756 PyObject *val;
Mark Shannon11e0b292021-04-15 14:28:56 +01003757 for (Py_ssize_t i = 0; i < n; i++) {
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003758 val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
3759 Py_INCREF(val);
3760 PyTuple_SET_ITEM(folded, i, val);
3761 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003762 if (tuple) {
3763 ADDOP_LOAD_CONST_NEW(c, folded);
3764 } else {
3765 if (add == SET_ADD) {
3766 Py_SETREF(folded, PyFrozenSet_New(folded));
3767 if (folded == NULL) {
3768 return 0;
3769 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003770 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003771 ADDOP_I(c, build, pushed);
3772 ADDOP_LOAD_CONST_NEW(c, folded);
3773 ADDOP_I(c, extend, 1);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003774 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003775 return 1;
3776 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003777
Mark Shannon11e0b292021-04-15 14:28:56 +01003778 int big = n+pushed > STACK_USE_GUIDELINE;
3779 int seen_star = 0;
3780 for (Py_ssize_t i = 0; i < n; i++) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003781 expr_ty elt = asdl_seq_GET(elts, i);
3782 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003783 seen_star = 1;
3784 }
3785 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003786 if (!seen_star && !big) {
3787 for (Py_ssize_t i = 0; i < n; i++) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003788 expr_ty elt = asdl_seq_GET(elts, i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003789 VISIT(c, expr, elt);
Mark Shannon13bc1392020-01-23 09:25:17 +00003790 }
3791 if (tuple) {
3792 ADDOP_I(c, BUILD_TUPLE, n+pushed);
3793 } else {
3794 ADDOP_I(c, build, n+pushed);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003795 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003796 return 1;
3797 }
3798 int sequence_built = 0;
3799 if (big) {
3800 ADDOP_I(c, build, pushed);
3801 sequence_built = 1;
3802 }
3803 for (Py_ssize_t i = 0; i < n; i++) {
3804 expr_ty elt = asdl_seq_GET(elts, i);
3805 if (elt->kind == Starred_kind) {
3806 if (sequence_built == 0) {
3807 ADDOP_I(c, build, i+pushed);
3808 sequence_built = 1;
3809 }
3810 VISIT(c, expr, elt->v.Starred.value);
3811 ADDOP_I(c, extend, 1);
3812 }
3813 else {
3814 VISIT(c, expr, elt);
3815 if (sequence_built) {
3816 ADDOP_I(c, add, 1);
3817 }
3818 }
3819 }
3820 assert(sequence_built);
3821 if (tuple) {
3822 ADDOP(c, LIST_TO_TUPLE);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003823 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003824 return 1;
3825}
3826
3827static int
Brandt Bucher145bf262021-02-26 14:51:55 -08003828unpack_helper(struct compiler *c, asdl_expr_seq *elts)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003829{
3830 Py_ssize_t n = asdl_seq_LEN(elts);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003831 int seen_star = 0;
Brandt Bucher145bf262021-02-26 14:51:55 -08003832 for (Py_ssize_t i = 0; i < n; i++) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003833 expr_ty elt = asdl_seq_GET(elts, i);
3834 if (elt->kind == Starred_kind && !seen_star) {
3835 if ((i >= (1 << 8)) ||
3836 (n-i-1 >= (INT_MAX >> 8)))
3837 return compiler_error(c,
3838 "too many expressions in "
3839 "star-unpacking assignment");
3840 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3841 seen_star = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003842 }
3843 else if (elt->kind == Starred_kind) {
3844 return compiler_error(c,
Furkan Öndercb6534e2020-03-26 04:54:31 +03003845 "multiple starred expressions in assignment");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003846 }
3847 }
3848 if (!seen_star) {
3849 ADDOP_I(c, UNPACK_SEQUENCE, n);
3850 }
Brandt Bucher145bf262021-02-26 14:51:55 -08003851 return 1;
3852}
3853
3854static int
3855assignment_helper(struct compiler *c, asdl_expr_seq *elts)
3856{
3857 Py_ssize_t n = asdl_seq_LEN(elts);
3858 RETURN_IF_FALSE(unpack_helper(c, elts));
3859 for (Py_ssize_t i = 0; i < n; i++) {
Brandt Bucherd5aa2e92020-03-07 19:44:18 -08003860 expr_ty elt = asdl_seq_GET(elts, i);
3861 VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value);
3862 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003863 return 1;
3864}
3865
3866static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003867compiler_list(struct compiler *c, expr_ty e)
3868{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003869 asdl_expr_seq *elts = e->v.List.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003870 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003871 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003872 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003873 else if (e->v.List.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003874 return starunpack_helper(c, elts, 0, BUILD_LIST,
3875 LIST_APPEND, LIST_EXTEND, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003876 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003877 else
3878 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003879 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003880}
3881
3882static int
3883compiler_tuple(struct compiler *c, expr_ty e)
3884{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003885 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003886 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003887 return assignment_helper(c, elts);
3888 }
3889 else if (e->v.Tuple.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003890 return starunpack_helper(c, elts, 0, BUILD_LIST,
3891 LIST_APPEND, LIST_EXTEND, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003892 }
3893 else
3894 VISIT_SEQ(c, expr, elts);
3895 return 1;
3896}
3897
3898static int
3899compiler_set(struct compiler *c, expr_ty e)
3900{
Mark Shannon13bc1392020-01-23 09:25:17 +00003901 return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET,
3902 SET_ADD, SET_UPDATE, 0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003903}
3904
3905static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003906are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003907{
3908 Py_ssize_t i;
3909 for (i = begin; i < end; i++) {
3910 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003911 if (key == NULL || key->kind != Constant_kind)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003912 return 0;
3913 }
3914 return 1;
3915}
3916
3917static int
3918compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3919{
3920 Py_ssize_t i, n = end - begin;
3921 PyObject *keys, *key;
Mark Shannon11e0b292021-04-15 14:28:56 +01003922 int big = n*2 > STACK_USE_GUIDELINE;
3923 if (n > 1 && !big && are_all_items_const(e->v.Dict.keys, begin, end)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003924 for (i = begin; i < end; i++) {
3925 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3926 }
3927 keys = PyTuple_New(n);
3928 if (keys == NULL) {
3929 return 0;
3930 }
3931 for (i = begin; i < end; i++) {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003932 key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003933 Py_INCREF(key);
3934 PyTuple_SET_ITEM(keys, i - begin, key);
3935 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003936 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003937 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
Mark Shannon11e0b292021-04-15 14:28:56 +01003938 return 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003939 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003940 if (big) {
3941 ADDOP_I(c, BUILD_MAP, 0);
3942 }
3943 for (i = begin; i < end; i++) {
3944 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3945 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3946 if (big) {
3947 ADDOP_I(c, MAP_ADD, 1);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003948 }
Mark Shannon11e0b292021-04-15 14:28:56 +01003949 }
3950 if (!big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003951 ADDOP_I(c, BUILD_MAP, n);
3952 }
3953 return 1;
3954}
3955
3956static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003957compiler_dict(struct compiler *c, expr_ty e)
3958{
Victor Stinner976bb402016-03-23 11:36:19 +01003959 Py_ssize_t i, n, elements;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003960 int have_dict;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003961 int is_unpacking = 0;
3962 n = asdl_seq_LEN(e->v.Dict.values);
Mark Shannon8a4cd702020-01-27 09:57:45 +00003963 have_dict = 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003964 elements = 0;
3965 for (i = 0; i < n; i++) {
3966 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003967 if (is_unpacking) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003968 if (elements) {
3969 if (!compiler_subdict(c, e, i - elements, i)) {
3970 return 0;
3971 }
3972 if (have_dict) {
3973 ADDOP_I(c, DICT_UPDATE, 1);
3974 }
3975 have_dict = 1;
3976 elements = 0;
3977 }
3978 if (have_dict == 0) {
3979 ADDOP_I(c, BUILD_MAP, 0);
3980 have_dict = 1;
3981 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003982 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Mark Shannon8a4cd702020-01-27 09:57:45 +00003983 ADDOP_I(c, DICT_UPDATE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003984 }
3985 else {
Mark Shannon11e0b292021-04-15 14:28:56 +01003986 if (elements*2 > STACK_USE_GUIDELINE) {
Pablo Galindoc51db0e2020-08-13 09:48:41 +01003987 if (!compiler_subdict(c, e, i - elements, i + 1)) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003988 return 0;
3989 }
3990 if (have_dict) {
3991 ADDOP_I(c, DICT_UPDATE, 1);
3992 }
3993 have_dict = 1;
3994 elements = 0;
3995 }
3996 else {
3997 elements++;
3998 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003999 }
4000 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004001 if (elements) {
4002 if (!compiler_subdict(c, e, n - elements, n)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004003 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004004 }
4005 if (have_dict) {
4006 ADDOP_I(c, DICT_UPDATE, 1);
4007 }
4008 have_dict = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004009 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004010 if (!have_dict) {
4011 ADDOP_I(c, BUILD_MAP, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004012 }
4013 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004014}
4015
4016static int
4017compiler_compare(struct compiler *c, expr_ty e)
4018{
Victor Stinnerad9a0662013-11-19 22:23:20 +01004019 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004020
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02004021 if (!check_compare(c, e)) {
4022 return 0;
4023 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004024 VISIT(c, expr, e->v.Compare.left);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004025 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
4026 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
4027 if (n == 0) {
4028 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Mark Shannon9af0e472020-01-14 10:12:45 +00004029 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004030 }
4031 else {
4032 basicblock *cleanup = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004033 if (cleanup == NULL)
4034 return 0;
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004035 for (i = 0; i < n; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004036 VISIT(c, expr,
4037 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004038 ADDOP(c, DUP_TOP);
4039 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00004040 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01004041 ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02004042 NEXT_BLOCK(c);
4043 }
4044 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00004045 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004046 basicblock *end = compiler_new_block(c);
4047 if (end == NULL)
4048 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00004049 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004050 compiler_use_next_block(c, cleanup);
4051 ADDOP(c, ROT_TWO);
4052 ADDOP(c, POP_TOP);
4053 compiler_use_next_block(c, end);
4054 }
4055 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004056}
4057
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004058static PyTypeObject *
4059infer_type(expr_ty e)
4060{
4061 switch (e->kind) {
4062 case Tuple_kind:
4063 return &PyTuple_Type;
4064 case List_kind:
4065 case ListComp_kind:
4066 return &PyList_Type;
4067 case Dict_kind:
4068 case DictComp_kind:
4069 return &PyDict_Type;
4070 case Set_kind:
4071 case SetComp_kind:
4072 return &PySet_Type;
4073 case GeneratorExp_kind:
4074 return &PyGen_Type;
4075 case Lambda_kind:
4076 return &PyFunction_Type;
4077 case JoinedStr_kind:
4078 case FormattedValue_kind:
4079 return &PyUnicode_Type;
4080 case Constant_kind:
Victor Stinnera102ed72020-02-07 02:24:48 +01004081 return Py_TYPE(e->v.Constant.value);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004082 default:
4083 return NULL;
4084 }
4085}
4086
4087static int
4088check_caller(struct compiler *c, expr_ty e)
4089{
4090 switch (e->kind) {
4091 case Constant_kind:
4092 case Tuple_kind:
4093 case List_kind:
4094 case ListComp_kind:
4095 case Dict_kind:
4096 case DictComp_kind:
4097 case Set_kind:
4098 case SetComp_kind:
4099 case GeneratorExp_kind:
4100 case JoinedStr_kind:
4101 case FormattedValue_kind:
4102 return compiler_warn(c, "'%.200s' object is not callable; "
4103 "perhaps you missed a comma?",
4104 infer_type(e)->tp_name);
4105 default:
4106 return 1;
4107 }
4108}
4109
4110static int
4111check_subscripter(struct compiler *c, expr_ty e)
4112{
4113 PyObject *v;
4114
4115 switch (e->kind) {
4116 case Constant_kind:
4117 v = e->v.Constant.value;
4118 if (!(v == Py_None || v == Py_Ellipsis ||
4119 PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
4120 PyAnySet_Check(v)))
4121 {
4122 return 1;
4123 }
4124 /* fall through */
4125 case Set_kind:
4126 case SetComp_kind:
4127 case GeneratorExp_kind:
4128 case Lambda_kind:
4129 return compiler_warn(c, "'%.200s' object is not subscriptable; "
4130 "perhaps you missed a comma?",
4131 infer_type(e)->tp_name);
4132 default:
4133 return 1;
4134 }
4135}
4136
4137static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004138check_index(struct compiler *c, expr_ty e, expr_ty s)
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004139{
4140 PyObject *v;
4141
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004142 PyTypeObject *index_type = infer_type(s);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004143 if (index_type == NULL
4144 || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
4145 || index_type == &PySlice_Type) {
4146 return 1;
4147 }
4148
4149 switch (e->kind) {
4150 case Constant_kind:
4151 v = e->v.Constant.value;
4152 if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
4153 return 1;
4154 }
4155 /* fall through */
4156 case Tuple_kind:
4157 case List_kind:
4158 case ListComp_kind:
4159 case JoinedStr_kind:
4160 case FormattedValue_kind:
4161 return compiler_warn(c, "%.200s indices must be integers or slices, "
4162 "not %.200s; "
4163 "perhaps you missed a comma?",
4164 infer_type(e)->tp_name,
4165 index_type->tp_name);
4166 default:
4167 return 1;
4168 }
4169}
4170
Zackery Spytz97f5de02019-03-22 01:30:32 -06004171// Return 1 if the method call was optimized, -1 if not, and 0 on error.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004172static int
Yury Selivanovf2392132016-12-13 19:03:51 -05004173maybe_optimize_method_call(struct compiler *c, expr_ty e)
4174{
4175 Py_ssize_t argsl, i;
4176 expr_ty meth = e->v.Call.func;
Pablo Galindoa5634c42020-09-16 19:42:00 +01004177 asdl_expr_seq *args = e->v.Call.args;
Yury Selivanovf2392132016-12-13 19:03:51 -05004178
4179 /* Check that the call node is an attribute access, and that
4180 the call doesn't have keyword parameters. */
4181 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
Mark Shannon11e0b292021-04-15 14:28:56 +01004182 asdl_seq_LEN(e->v.Call.keywords)) {
Yury Selivanovf2392132016-12-13 19:03:51 -05004183 return -1;
Mark Shannon11e0b292021-04-15 14:28:56 +01004184 }
4185 /* Check that there aren't too many arguments */
Yury Selivanovf2392132016-12-13 19:03:51 -05004186 argsl = asdl_seq_LEN(args);
Mark Shannon11e0b292021-04-15 14:28:56 +01004187 if (argsl >= STACK_USE_GUIDELINE) {
4188 return -1;
4189 }
4190 /* Check that there are no *varargs types of arguments. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004191 for (i = 0; i < argsl; i++) {
4192 expr_ty elt = asdl_seq_GET(args, i);
4193 if (elt->kind == Starred_kind) {
4194 return -1;
4195 }
4196 }
4197
4198 /* Alright, we can optimize the code. */
4199 VISIT(c, expr, meth->v.Attribute.value);
Mark Shannond48848c2021-03-14 18:01:30 +00004200 int old_lineno = c->u->u_lineno;
4201 c->u->u_lineno = meth->end_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004202 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
4203 VISIT_SEQ(c, expr, e->v.Call.args);
4204 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
Mark Shannond48848c2021-03-14 18:01:30 +00004205 c->u->u_lineno = old_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004206 return 1;
4207}
4208
4209static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004210validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
Zackery Spytz08050e92020-04-06 00:47:47 -06004211{
4212 Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
4213 for (Py_ssize_t i = 0; i < nkeywords; i++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004214 keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
4215 if (key->arg == NULL) {
4216 continue;
4217 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +01004218 if (forbidden_name(c, key->arg, Store)) {
4219 return -1;
4220 }
Zackery Spytz08050e92020-04-06 00:47:47 -06004221 for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004222 keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
4223 if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004224 c->u->u_col_offset = other->col_offset;
Brandt Bucher145bf262021-02-26 14:51:55 -08004225 compiler_error(c, "keyword argument repeated: %U", key->arg);
Pablo Galindo254ec782020-04-03 20:37:13 +01004226 return -1;
4227 }
4228 }
4229 }
4230 return 0;
4231}
4232
4233static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004234compiler_call(struct compiler *c, expr_ty e)
4235{
Zackery Spytz97f5de02019-03-22 01:30:32 -06004236 int ret = maybe_optimize_method_call(c, e);
4237 if (ret >= 0) {
4238 return ret;
4239 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004240 if (!check_caller(c, e->v.Call.func)) {
4241 return 0;
4242 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004243 VISIT(c, expr, e->v.Call.func);
4244 return compiler_call_helper(c, 0,
4245 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004246 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004247}
4248
Eric V. Smith235a6f02015-09-19 14:51:32 -04004249static int
4250compiler_joined_str(struct compiler *c, expr_ty e)
4251{
Mark Shannon11e0b292021-04-15 14:28:56 +01004252
4253 Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values);
4254 if (value_count > STACK_USE_GUIDELINE) {
4255 ADDOP_LOAD_CONST_NEW(c, _PyUnicode_FromASCII("", 0));
4256 PyObject *join = _PyUnicode_FromASCII("join", 4);
4257 if (join == NULL) {
4258 return 0;
4259 }
4260 ADDOP_NAME(c, LOAD_METHOD, join, names);
4261 Py_DECREF(join);
4262 ADDOP_I(c, BUILD_LIST, 0);
4263 for (Py_ssize_t i = 0; i < asdl_seq_LEN(e->v.JoinedStr.values); i++) {
4264 VISIT(c, expr, asdl_seq_GET(e->v.JoinedStr.values, i));
4265 ADDOP_I(c, LIST_APPEND, 1);
4266 }
4267 ADDOP_I(c, CALL_METHOD, 1);
4268 }
4269 else {
4270 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
4271 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) {
4272 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
4273 }
4274 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004275 return 1;
4276}
4277
Eric V. Smitha78c7952015-11-03 12:45:05 -05004278/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004279static int
4280compiler_formatted_value(struct compiler *c, expr_ty e)
4281{
Eric V. Smitha78c7952015-11-03 12:45:05 -05004282 /* Our oparg encodes 2 pieces of information: the conversion
4283 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04004284
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004285 Convert the conversion char to 3 bits:
4286 : 000 0x0 FVC_NONE The default if nothing specified.
Eric V. Smitha78c7952015-11-03 12:45:05 -05004287 !s : 001 0x1 FVC_STR
4288 !r : 010 0x2 FVC_REPR
4289 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04004290
Eric V. Smitha78c7952015-11-03 12:45:05 -05004291 next bit is whether or not we have a format spec:
4292 yes : 100 0x4
4293 no : 000 0x0
4294 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004295
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004296 int conversion = e->v.FormattedValue.conversion;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004297 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004298
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004299 /* The expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004300 VISIT(c, expr, e->v.FormattedValue.value);
4301
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004302 switch (conversion) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004303 case 's': oparg = FVC_STR; break;
4304 case 'r': oparg = FVC_REPR; break;
4305 case 'a': oparg = FVC_ASCII; break;
4306 case -1: oparg = FVC_NONE; break;
4307 default:
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004308 PyErr_Format(PyExc_SystemError,
4309 "Unrecognized conversion character %d", conversion);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004310 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004311 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004312 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004313 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004314 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004315 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004316 }
4317
Eric V. Smitha78c7952015-11-03 12:45:05 -05004318 /* And push our opcode and oparg */
4319 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004320
Eric V. Smith235a6f02015-09-19 14:51:32 -04004321 return 1;
4322}
4323
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004324static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004325compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004326{
4327 Py_ssize_t i, n = end - begin;
4328 keyword_ty kw;
4329 PyObject *keys, *key;
4330 assert(n > 0);
Mark Shannon11e0b292021-04-15 14:28:56 +01004331 int big = n*2 > STACK_USE_GUIDELINE;
4332 if (n > 1 && !big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004333 for (i = begin; i < end; i++) {
4334 kw = asdl_seq_GET(keywords, i);
4335 VISIT(c, expr, kw->value);
4336 }
4337 keys = PyTuple_New(n);
4338 if (keys == NULL) {
4339 return 0;
4340 }
4341 for (i = begin; i < end; i++) {
4342 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
4343 Py_INCREF(key);
4344 PyTuple_SET_ITEM(keys, i - begin, key);
4345 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004346 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004347 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
Mark Shannon11e0b292021-04-15 14:28:56 +01004348 return 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004349 }
Mark Shannon11e0b292021-04-15 14:28:56 +01004350 if (big) {
4351 ADDOP_I_NOLINE(c, BUILD_MAP, 0);
4352 }
4353 for (i = begin; i < end; i++) {
4354 kw = asdl_seq_GET(keywords, i);
4355 ADDOP_LOAD_CONST(c, kw->arg);
4356 VISIT(c, expr, kw->value);
4357 if (big) {
4358 ADDOP_I_NOLINE(c, MAP_ADD, 1);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004359 }
Mark Shannon11e0b292021-04-15 14:28:56 +01004360 }
4361 if (!big) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004362 ADDOP_I(c, BUILD_MAP, n);
4363 }
4364 return 1;
4365}
4366
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004367/* shared code between compiler_call and compiler_class */
4368static int
4369compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01004370 int n, /* Args already pushed */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004371 asdl_expr_seq *args,
4372 asdl_keyword_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004373{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004374 Py_ssize_t i, nseen, nelts, nkwelts;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004375
Pablo Galindo254ec782020-04-03 20:37:13 +01004376 if (validate_keywords(c, keywords) == -1) {
4377 return 0;
4378 }
4379
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004380 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004381 nkwelts = asdl_seq_LEN(keywords);
4382
Mark Shannon11e0b292021-04-15 14:28:56 +01004383 if (nelts + nkwelts*2 > STACK_USE_GUIDELINE) {
4384 goto ex_call;
4385 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004386 for (i = 0; i < nelts; i++) {
4387 expr_ty elt = asdl_seq_GET(args, i);
4388 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00004389 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004390 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004391 }
4392 for (i = 0; i < nkwelts; i++) {
4393 keyword_ty kw = asdl_seq_GET(keywords, i);
4394 if (kw->arg == NULL) {
4395 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004396 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004397 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004398
Mark Shannon13bc1392020-01-23 09:25:17 +00004399 /* No * or ** args, so can use faster calling sequence */
4400 for (i = 0; i < nelts; i++) {
4401 expr_ty elt = asdl_seq_GET(args, i);
4402 assert(elt->kind != Starred_kind);
4403 VISIT(c, expr, elt);
4404 }
4405 if (nkwelts) {
4406 PyObject *names;
4407 VISIT_SEQ(c, keyword, keywords);
4408 names = PyTuple_New(nkwelts);
4409 if (names == NULL) {
4410 return 0;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004411 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004412 for (i = 0; i < nkwelts; i++) {
4413 keyword_ty kw = asdl_seq_GET(keywords, i);
4414 Py_INCREF(kw->arg);
4415 PyTuple_SET_ITEM(names, i, kw->arg);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004416 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004417 ADDOP_LOAD_CONST_NEW(c, names);
4418 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
4419 return 1;
4420 }
4421 else {
4422 ADDOP_I(c, CALL_FUNCTION, n + nelts);
4423 return 1;
4424 }
4425
4426ex_call:
4427
4428 /* Do positional arguments. */
4429 if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
4430 VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
4431 }
4432 else if (starunpack_helper(c, args, n, BUILD_LIST,
4433 LIST_APPEND, LIST_EXTEND, 1) == 0) {
4434 return 0;
4435 }
4436 /* Then keyword arguments */
4437 if (nkwelts) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004438 /* Has a new dict been pushed */
4439 int have_dict = 0;
Mark Shannon13bc1392020-01-23 09:25:17 +00004440
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004441 nseen = 0; /* the number of keyword arguments on the stack following */
4442 for (i = 0; i < nkwelts; i++) {
4443 keyword_ty kw = asdl_seq_GET(keywords, i);
4444 if (kw->arg == NULL) {
4445 /* A keyword argument unpacking. */
4446 if (nseen) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004447 if (!compiler_subkwargs(c, keywords, i - nseen, i)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004448 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004449 }
Mark Shannondb64f122020-06-01 10:42:42 +01004450 if (have_dict) {
4451 ADDOP_I(c, DICT_MERGE, 1);
4452 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004453 have_dict = 1;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004454 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004455 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004456 if (!have_dict) {
4457 ADDOP_I(c, BUILD_MAP, 0);
4458 have_dict = 1;
4459 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004460 VISIT(c, expr, kw->value);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004461 ADDOP_I(c, DICT_MERGE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004462 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004463 else {
4464 nseen++;
4465 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004466 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004467 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004468 /* Pack up any trailing keyword arguments. */
Mark Shannon8a4cd702020-01-27 09:57:45 +00004469 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004470 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004471 }
4472 if (have_dict) {
4473 ADDOP_I(c, DICT_MERGE, 1);
4474 }
4475 have_dict = 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004476 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004477 assert(have_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004478 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004479 ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0);
4480 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004481}
4482
Nick Coghlan650f0d02007-04-15 12:05:43 +00004483
4484/* List and set comprehensions and generator expressions work by creating a
4485 nested function to perform the actual iteration. This means that the
4486 iteration variables don't leak into the current scope.
4487 The defined function is called immediately following its definition, with the
4488 result of that call being the result of the expression.
4489 The LC/SC version returns the populated container, while the GE version is
4490 flagged in symtable.c as a generator, so it returns the generator object
4491 when the function is called.
Nick Coghlan650f0d02007-04-15 12:05:43 +00004492
4493 Possible cleanups:
4494 - iterate over the generator sequence instead of using recursion
4495*/
4496
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004497
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004498static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004499compiler_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004500 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004501 int depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004502 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004503{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004504 comprehension_ty gen;
4505 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4506 if (gen->is_async) {
4507 return compiler_async_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004508 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004509 } else {
4510 return compiler_sync_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004511 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004512 }
4513}
4514
4515static int
4516compiler_sync_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004517 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004518 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004519 expr_ty elt, expr_ty val, int type)
4520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004521 /* generate code for the iterator, then each of the ifs,
4522 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004524 comprehension_ty gen;
4525 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004526 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004528 start = compiler_new_block(c);
4529 skip = compiler_new_block(c);
4530 if_cleanup = compiler_new_block(c);
4531 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004533 if (start == NULL || skip == NULL || if_cleanup == NULL ||
4534 anchor == NULL)
4535 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004537 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004539 if (gen_index == 0) {
4540 /* Receive outermost iter as an implicit argument */
4541 c->u->u_argcount = 1;
4542 ADDOP_I(c, LOAD_FAST, 0);
4543 }
4544 else {
4545 /* Sub-iter - calculate on the fly */
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004546 /* Fast path for the temporary variable assignment idiom:
4547 for y in [f(x)]
4548 */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004549 asdl_expr_seq *elts;
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004550 switch (gen->iter->kind) {
4551 case List_kind:
4552 elts = gen->iter->v.List.elts;
4553 break;
4554 case Tuple_kind:
4555 elts = gen->iter->v.Tuple.elts;
4556 break;
4557 default:
4558 elts = NULL;
4559 }
4560 if (asdl_seq_LEN(elts) == 1) {
4561 expr_ty elt = asdl_seq_GET(elts, 0);
4562 if (elt->kind != Starred_kind) {
4563 VISIT(c, expr, elt);
4564 start = NULL;
4565 }
4566 }
4567 if (start) {
4568 VISIT(c, expr, gen->iter);
4569 ADDOP(c, GET_ITER);
4570 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004571 }
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004572 if (start) {
4573 depth++;
4574 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01004575 ADDOP_JUMP(c, FOR_ITER, anchor);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004576 NEXT_BLOCK(c);
4577 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004578 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004580 /* XXX this needs to be cleaned up...a lot! */
4581 n = asdl_seq_LEN(gen->ifs);
4582 for (i = 0; i < n; i++) {
4583 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004584 if (!compiler_jump_if(c, e, if_cleanup, 0))
4585 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004586 NEXT_BLOCK(c);
4587 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004589 if (++gen_index < asdl_seq_LEN(generators))
4590 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004591 generators, gen_index, depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004592 elt, val, type))
4593 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004595 /* only append after the last for generator */
4596 if (gen_index >= asdl_seq_LEN(generators)) {
4597 /* comprehension specific code */
4598 switch (type) {
4599 case COMP_GENEXP:
4600 VISIT(c, expr, elt);
4601 ADDOP(c, YIELD_VALUE);
4602 ADDOP(c, POP_TOP);
4603 break;
4604 case COMP_LISTCOMP:
4605 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004606 ADDOP_I(c, LIST_APPEND, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004607 break;
4608 case COMP_SETCOMP:
4609 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004610 ADDOP_I(c, SET_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004611 break;
4612 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004613 /* With '{k: v}', k is evaluated before v, so we do
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004614 the same. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004615 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004616 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004617 ADDOP_I(c, MAP_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004618 break;
4619 default:
4620 return 0;
4621 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004623 compiler_use_next_block(c, skip);
4624 }
4625 compiler_use_next_block(c, if_cleanup);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004626 if (start) {
Mark Shannon582aaf12020-08-04 17:30:11 +01004627 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004628 compiler_use_next_block(c, anchor);
4629 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004630
4631 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004632}
4633
4634static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004635compiler_async_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004636 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004637 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004638 expr_ty elt, expr_ty val, int type)
4639{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004640 comprehension_ty gen;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004641 basicblock *start, *if_cleanup, *except;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004642 Py_ssize_t i, n;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004643 start = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004644 except = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004645 if_cleanup = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004646
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004647 if (start == NULL || if_cleanup == NULL || except == NULL) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004648 return 0;
4649 }
4650
4651 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4652
4653 if (gen_index == 0) {
4654 /* Receive outermost iter as an implicit argument */
4655 c->u->u_argcount = 1;
4656 ADDOP_I(c, LOAD_FAST, 0);
4657 }
4658 else {
4659 /* Sub-iter - calculate on the fly */
4660 VISIT(c, expr, gen->iter);
4661 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004662 }
4663
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004664 compiler_use_next_block(c, start);
tomKPZ7a7ba3d2021-04-07 07:43:45 -07004665 /* Runtime will push a block here, so we need to account for that */
4666 if (!compiler_push_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start,
4667 NULL, NULL)) {
4668 return 0;
4669 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004670
Mark Shannon582aaf12020-08-04 17:30:11 +01004671 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004672 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004673 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004674 ADDOP(c, YIELD_FROM);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004675 ADDOP(c, POP_BLOCK);
Serhiy Storchaka24d32012018-03-10 18:22:34 +02004676 VISIT(c, expr, gen->target);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004677
4678 n = asdl_seq_LEN(gen->ifs);
4679 for (i = 0; i < n; i++) {
4680 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004681 if (!compiler_jump_if(c, e, if_cleanup, 0))
4682 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004683 NEXT_BLOCK(c);
4684 }
4685
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004686 depth++;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004687 if (++gen_index < asdl_seq_LEN(generators))
4688 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004689 generators, gen_index, depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004690 elt, val, type))
4691 return 0;
4692
4693 /* only append after the last for generator */
4694 if (gen_index >= asdl_seq_LEN(generators)) {
4695 /* comprehension specific code */
4696 switch (type) {
4697 case COMP_GENEXP:
4698 VISIT(c, expr, elt);
4699 ADDOP(c, YIELD_VALUE);
4700 ADDOP(c, POP_TOP);
4701 break;
4702 case COMP_LISTCOMP:
4703 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004704 ADDOP_I(c, LIST_APPEND, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004705 break;
4706 case COMP_SETCOMP:
4707 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004708 ADDOP_I(c, SET_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004709 break;
4710 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004711 /* With '{k: v}', k is evaluated before v, so we do
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004712 the same. */
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004713 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004714 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004715 ADDOP_I(c, MAP_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004716 break;
4717 default:
4718 return 0;
4719 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004720 }
4721 compiler_use_next_block(c, if_cleanup);
Mark Shannon582aaf12020-08-04 17:30:11 +01004722 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004723
tomKPZ7a7ba3d2021-04-07 07:43:45 -07004724 compiler_pop_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start);
4725
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004726 compiler_use_next_block(c, except);
4727 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004728
4729 return 1;
4730}
4731
4732static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004733compiler_comprehension(struct compiler *c, expr_ty e, int type,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004734 identifier name, asdl_comprehension_seq *generators, expr_ty elt,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004735 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004736{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004737 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004738 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004739 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004740 int is_async_generator = 0;
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004741 int top_level_await = IS_TOP_LEVEL_AWAIT(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004742
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004743
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004744 int is_async_function = c->u->u_ste->ste_coroutine;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004745
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004746 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004747 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4748 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004749 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004750 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004751 }
4752
4753 is_async_generator = c->u->u_ste->ste_coroutine;
4754
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004755 if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004756 compiler_error(c, "asynchronous comprehension outside of "
4757 "an asynchronous function");
4758 goto error_in_scope;
4759 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004761 if (type != COMP_GENEXP) {
4762 int op;
4763 switch (type) {
4764 case COMP_LISTCOMP:
4765 op = BUILD_LIST;
4766 break;
4767 case COMP_SETCOMP:
4768 op = BUILD_SET;
4769 break;
4770 case COMP_DICTCOMP:
4771 op = BUILD_MAP;
4772 break;
4773 default:
4774 PyErr_Format(PyExc_SystemError,
4775 "unknown comprehension type %d", type);
4776 goto error_in_scope;
4777 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004779 ADDOP_I(c, op, 0);
4780 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004781
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004782 if (!compiler_comprehension_generator(c, generators, 0, 0, elt,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004783 val, type))
4784 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004786 if (type != COMP_GENEXP) {
4787 ADDOP(c, RETURN_VALUE);
4788 }
4789
4790 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004791 qualname = c->u->u_qualname;
4792 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004793 compiler_exit_scope(c);
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004794 if (top_level_await && is_async_generator){
4795 c->u->u_ste->ste_coroutine = 1;
4796 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004797 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004798 goto error;
4799
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004800 if (!compiler_make_closure(c, co, 0, qualname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004801 goto error;
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004802 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004803 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004804 Py_DECREF(co);
4805
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004806 VISIT(c, expr, outermost->iter);
4807
4808 if (outermost->is_async) {
4809 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004810 } else {
4811 ADDOP(c, GET_ITER);
4812 }
4813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004814 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004815
4816 if (is_async_generator && type != COMP_GENEXP) {
4817 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004818 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004819 ADDOP(c, YIELD_FROM);
4820 }
4821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004822 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004823error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004824 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004825error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004826 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004827 Py_XDECREF(co);
4828 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004829}
4830
4831static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004832compiler_genexp(struct compiler *c, expr_ty e)
4833{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004834 static identifier name;
4835 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004836 name = PyUnicode_InternFromString("<genexpr>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004837 if (!name)
4838 return 0;
4839 }
4840 assert(e->kind == GeneratorExp_kind);
4841 return compiler_comprehension(c, e, COMP_GENEXP, name,
4842 e->v.GeneratorExp.generators,
4843 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004844}
4845
4846static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004847compiler_listcomp(struct compiler *c, expr_ty e)
4848{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004849 static identifier name;
4850 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004851 name = PyUnicode_InternFromString("<listcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004852 if (!name)
4853 return 0;
4854 }
4855 assert(e->kind == ListComp_kind);
4856 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4857 e->v.ListComp.generators,
4858 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004859}
4860
4861static int
4862compiler_setcomp(struct compiler *c, expr_ty e)
4863{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004864 static identifier name;
4865 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004866 name = PyUnicode_InternFromString("<setcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004867 if (!name)
4868 return 0;
4869 }
4870 assert(e->kind == SetComp_kind);
4871 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4872 e->v.SetComp.generators,
4873 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004874}
4875
4876
4877static int
4878compiler_dictcomp(struct compiler *c, expr_ty e)
4879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004880 static identifier name;
4881 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004882 name = PyUnicode_InternFromString("<dictcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004883 if (!name)
4884 return 0;
4885 }
4886 assert(e->kind == DictComp_kind);
4887 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4888 e->v.DictComp.generators,
4889 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004890}
4891
4892
4893static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004894compiler_visit_keyword(struct compiler *c, keyword_ty k)
4895{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004896 VISIT(c, expr, k->value);
4897 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004898}
4899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004900/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004901 whether they are true or false.
4902
4903 Return values: 1 for true, 0 for false, -1 for non-constant.
4904 */
4905
4906static int
Mark Shannonfee55262019-11-21 09:11:43 +00004907compiler_with_except_finish(struct compiler *c) {
4908 basicblock *exit;
4909 exit = compiler_new_block(c);
4910 if (exit == NULL)
4911 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01004912 ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit);
Mark Shannon266b4622020-11-17 19:30:14 +00004913 NEXT_BLOCK(c);
Mark Shannonbf353f32020-12-17 13:55:28 +00004914 ADDOP_I(c, RERAISE, 1);
Mark Shannonfee55262019-11-21 09:11:43 +00004915 compiler_use_next_block(c, exit);
4916 ADDOP(c, POP_TOP);
4917 ADDOP(c, POP_TOP);
4918 ADDOP(c, POP_TOP);
4919 ADDOP(c, POP_EXCEPT);
4920 ADDOP(c, POP_TOP);
4921 return 1;
4922}
Yury Selivanov75445082015-05-11 22:57:16 -04004923
4924/*
4925 Implements the async with statement.
4926
4927 The semantics outlined in that PEP are as follows:
4928
4929 async with EXPR as VAR:
4930 BLOCK
4931
4932 It is implemented roughly as:
4933
4934 context = EXPR
4935 exit = context.__aexit__ # not calling it
4936 value = await context.__aenter__()
4937 try:
4938 VAR = value # if VAR present in the syntax
4939 BLOCK
4940 finally:
4941 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004942 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04004943 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004944 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04004945 if not (await exit(*exc)):
4946 raise
4947 */
4948static int
4949compiler_async_with(struct compiler *c, stmt_ty s, int pos)
4950{
Mark Shannonfee55262019-11-21 09:11:43 +00004951 basicblock *block, *final, *exit;
Yury Selivanov75445082015-05-11 22:57:16 -04004952 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
4953
4954 assert(s->kind == AsyncWith_kind);
Pablo Galindo90235812020-03-15 04:29:22 +00004955 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07004956 c->u->u_ste->ste_coroutine = 1;
4957 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
Zsolt Dollensteine2396502018-04-27 08:58:56 -07004958 return compiler_error(c, "'async with' outside async function");
4959 }
Yury Selivanov75445082015-05-11 22:57:16 -04004960
4961 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004962 final = compiler_new_block(c);
4963 exit = compiler_new_block(c);
4964 if (!block || !final || !exit)
Yury Selivanov75445082015-05-11 22:57:16 -04004965 return 0;
4966
4967 /* Evaluate EXPR */
4968 VISIT(c, expr, item->context_expr);
4969
4970 ADDOP(c, BEFORE_ASYNC_WITH);
4971 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004972 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004973 ADDOP(c, YIELD_FROM);
4974
Mark Shannon582aaf12020-08-04 17:30:11 +01004975 ADDOP_JUMP(c, SETUP_ASYNC_WITH, final);
Yury Selivanov75445082015-05-11 22:57:16 -04004976
4977 /* SETUP_ASYNC_WITH pushes a finally block. */
4978 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004979 if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004980 return 0;
4981 }
4982
4983 if (item->optional_vars) {
4984 VISIT(c, expr, item->optional_vars);
4985 }
4986 else {
4987 /* Discard result from context.__aenter__() */
4988 ADDOP(c, POP_TOP);
4989 }
4990
4991 pos++;
4992 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
4993 /* BLOCK code */
4994 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
4995 else if (!compiler_async_with(c, s, pos))
4996 return 0;
4997
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004998 compiler_pop_fblock(c, ASYNC_WITH, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004999 ADDOP(c, POP_BLOCK);
5000 /* End of body; start the cleanup */
Yury Selivanov75445082015-05-11 22:57:16 -04005001
Mark Shannonfee55262019-11-21 09:11:43 +00005002 /* For successful outcome:
5003 * call __exit__(None, None, None)
5004 */
5005 if(!compiler_call_exit_with_nones(c))
Yury Selivanov75445082015-05-11 22:57:16 -04005006 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00005007 ADDOP(c, GET_AWAITABLE);
5008 ADDOP_O(c, LOAD_CONST, Py_None, consts);
5009 ADDOP(c, YIELD_FROM);
Yury Selivanov75445082015-05-11 22:57:16 -04005010
Mark Shannonfee55262019-11-21 09:11:43 +00005011 ADDOP(c, POP_TOP);
Yury Selivanov75445082015-05-11 22:57:16 -04005012
Mark Shannon582aaf12020-08-04 17:30:11 +01005013 ADDOP_JUMP(c, JUMP_ABSOLUTE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00005014
5015 /* For exceptional outcome: */
5016 compiler_use_next_block(c, final);
5017
5018 ADDOP(c, WITH_EXCEPT_START);
Yury Selivanov75445082015-05-11 22:57:16 -04005019 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005020 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005021 ADDOP(c, YIELD_FROM);
Mark Shannonfee55262019-11-21 09:11:43 +00005022 compiler_with_except_finish(c);
Yury Selivanov75445082015-05-11 22:57:16 -04005023
Mark Shannonfee55262019-11-21 09:11:43 +00005024compiler_use_next_block(c, exit);
Yury Selivanov75445082015-05-11 22:57:16 -04005025 return 1;
5026}
5027
5028
Guido van Rossumc2e20742006-02-27 22:32:47 +00005029/*
5030 Implements the with statement from PEP 343.
Guido van Rossumc2e20742006-02-27 22:32:47 +00005031 with EXPR as VAR:
5032 BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00005033 is implemented as:
5034 <code for EXPR>
5035 SETUP_WITH E
5036 <code to store to VAR> or POP_TOP
5037 <code for BLOCK>
5038 LOAD_CONST (None, None, None)
5039 CALL_FUNCTION_EX 0
5040 JUMP_FORWARD EXIT
5041 E: WITH_EXCEPT_START (calls EXPR.__exit__)
5042 POP_JUMP_IF_TRUE T:
5043 RERAISE
5044 T: POP_TOP * 3 (remove exception from stack)
5045 POP_EXCEPT
5046 POP_TOP
5047 EXIT:
Guido van Rossumc2e20742006-02-27 22:32:47 +00005048 */
Mark Shannonfee55262019-11-21 09:11:43 +00005049
Guido van Rossumc2e20742006-02-27 22:32:47 +00005050static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005051compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00005052{
Mark Shannonfee55262019-11-21 09:11:43 +00005053 basicblock *block, *final, *exit;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005054 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005055
5056 assert(s->kind == With_kind);
5057
Guido van Rossumc2e20742006-02-27 22:32:47 +00005058 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00005059 final = compiler_new_block(c);
5060 exit = compiler_new_block(c);
5061 if (!block || !final || !exit)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005062 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005063
Thomas Wouters477c8d52006-05-27 19:21:47 +00005064 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005065 VISIT(c, expr, item->context_expr);
Mark Shannonfee55262019-11-21 09:11:43 +00005066 /* Will push bound __exit__ */
Mark Shannon582aaf12020-08-04 17:30:11 +01005067 ADDOP_JUMP(c, SETUP_WITH, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005068
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005069 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00005070 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00005071 if (!compiler_push_fblock(c, WITH, block, final, NULL)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005072 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005073 }
5074
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005075 if (item->optional_vars) {
5076 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005077 }
5078 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005079 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005080 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005081 }
5082
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05005083 pos++;
5084 if (pos == asdl_seq_LEN(s->v.With.items))
5085 /* BLOCK code */
5086 VISIT_SEQ(c, stmt, s->v.With.body)
5087 else if (!compiler_with(c, s, pos))
5088 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005089
Mark Shannon3bd60352021-01-13 12:05:43 +00005090
5091 /* Mark all following code as artificial */
5092 c->u->u_lineno = -1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005093 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005094 compiler_pop_fblock(c, WITH, block);
Mark Shannon13bc1392020-01-23 09:25:17 +00005095
Mark Shannonfee55262019-11-21 09:11:43 +00005096 /* End of body; start the cleanup. */
Mark Shannon13bc1392020-01-23 09:25:17 +00005097
Mark Shannonfee55262019-11-21 09:11:43 +00005098 /* For successful outcome:
5099 * call __exit__(None, None, None)
5100 */
5101 if (!compiler_call_exit_with_nones(c))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005102 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00005103 ADDOP(c, POP_TOP);
Mark Shannon582aaf12020-08-04 17:30:11 +01005104 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005105
Mark Shannonfee55262019-11-21 09:11:43 +00005106 /* For exceptional outcome: */
5107 compiler_use_next_block(c, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005108
Mark Shannonfee55262019-11-21 09:11:43 +00005109 ADDOP(c, WITH_EXCEPT_START);
5110 compiler_with_except_finish(c);
5111
5112 compiler_use_next_block(c, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005113 return 1;
5114}
5115
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005116static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005117compiler_visit_expr1(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005118{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005119 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07005120 case NamedExpr_kind:
5121 VISIT(c, expr, e->v.NamedExpr.value);
5122 ADDOP(c, DUP_TOP);
5123 VISIT(c, expr, e->v.NamedExpr.target);
5124 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005125 case BoolOp_kind:
5126 return compiler_boolop(c, e);
5127 case BinOp_kind:
5128 VISIT(c, expr, e->v.BinOp.left);
5129 VISIT(c, expr, e->v.BinOp.right);
Andy Lester76d58772020-03-10 21:18:12 -05005130 ADDOP(c, binop(e->v.BinOp.op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005131 break;
5132 case UnaryOp_kind:
5133 VISIT(c, expr, e->v.UnaryOp.operand);
5134 ADDOP(c, unaryop(e->v.UnaryOp.op));
5135 break;
5136 case Lambda_kind:
5137 return compiler_lambda(c, e);
5138 case IfExp_kind:
5139 return compiler_ifexp(c, e);
5140 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005141 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005142 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005143 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005144 case GeneratorExp_kind:
5145 return compiler_genexp(c, e);
5146 case ListComp_kind:
5147 return compiler_listcomp(c, e);
5148 case SetComp_kind:
5149 return compiler_setcomp(c, e);
5150 case DictComp_kind:
5151 return compiler_dictcomp(c, e);
5152 case Yield_kind:
5153 if (c->u->u_ste->ste_type != FunctionBlock)
5154 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005155 if (e->v.Yield.value) {
5156 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005157 }
5158 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005159 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005160 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005161 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005162 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005163 case YieldFrom_kind:
5164 if (c->u->u_ste->ste_type != FunctionBlock)
5165 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04005166
5167 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
5168 return compiler_error(c, "'yield from' inside async function");
5169
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005170 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04005171 ADDOP(c, GET_YIELD_FROM_ITER);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005172 ADDOP_LOAD_CONST(c, Py_None);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005173 ADDOP(c, YIELD_FROM);
5174 break;
Yury Selivanov75445082015-05-11 22:57:16 -04005175 case Await_kind:
Pablo Galindo90235812020-03-15 04:29:22 +00005176 if (!IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005177 if (c->u->u_ste->ste_type != FunctionBlock){
5178 return compiler_error(c, "'await' outside function");
5179 }
Yury Selivanov75445082015-05-11 22:57:16 -04005180
Victor Stinner331a6a52019-05-27 16:39:22 +02005181 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005182 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
5183 return compiler_error(c, "'await' outside async function");
5184 }
5185 }
Yury Selivanov75445082015-05-11 22:57:16 -04005186
5187 VISIT(c, expr, e->v.Await.value);
5188 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005189 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005190 ADDOP(c, YIELD_FROM);
5191 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005192 case Compare_kind:
5193 return compiler_compare(c, e);
5194 case Call_kind:
5195 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005196 case Constant_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005197 ADDOP_LOAD_CONST(c, e->v.Constant.value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005198 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005199 case JoinedStr_kind:
5200 return compiler_joined_str(c, e);
5201 case FormattedValue_kind:
5202 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005203 /* The following exprs can be assignment targets. */
5204 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005205 VISIT(c, expr, e->v.Attribute.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005206 switch (e->v.Attribute.ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005207 case Load:
Mark Shannond48848c2021-03-14 18:01:30 +00005208 {
5209 int old_lineno = c->u->u_lineno;
5210 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005211 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005212 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005213 break;
Mark Shannond48848c2021-03-14 18:01:30 +00005214 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005215 case Store:
Mark Shannond48848c2021-03-14 18:01:30 +00005216 if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005217 return 0;
Mark Shannond48848c2021-03-14 18:01:30 +00005218 }
5219 int old_lineno = c->u->u_lineno;
5220 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005221 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005222 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005223 break;
5224 case Del:
5225 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
5226 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005227 }
5228 break;
5229 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005230 return compiler_subscript(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005231 case Starred_kind:
5232 switch (e->v.Starred.ctx) {
5233 case Store:
5234 /* In all legitimate cases, the Starred node was already replaced
5235 * by compiler_list/compiler_tuple. XXX: is that okay? */
5236 return compiler_error(c,
5237 "starred assignment target must be in a list or tuple");
5238 default:
5239 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005240 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005241 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005242 break;
5243 case Slice_kind:
5244 return compiler_slice(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005245 case Name_kind:
5246 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
5247 /* child nodes of List and Tuple will have expr_context set */
5248 case List_kind:
5249 return compiler_list(c, e);
5250 case Tuple_kind:
5251 return compiler_tuple(c, e);
Brandt Bucher145bf262021-02-26 14:51:55 -08005252 case MatchAs_kind:
5253 case MatchOr_kind:
5254 // Can only occur in patterns, which are handled elsewhere.
5255 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005256 }
5257 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005258}
5259
5260static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005261compiler_visit_expr(struct compiler *c, expr_ty e)
5262{
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005263 int old_lineno = c->u->u_lineno;
5264 int old_col_offset = c->u->u_col_offset;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005265 SET_LOC(c, e);
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005266 int res = compiler_visit_expr1(c, e);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005267 c->u->u_lineno = old_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005268 c->u->u_col_offset = old_col_offset;
5269 return res;
5270}
5271
5272static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005273compiler_augassign(struct compiler *c, stmt_ty s)
5274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005275 assert(s->kind == AugAssign_kind);
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005276 expr_ty e = s->v.AugAssign.target;
5277
5278 int old_lineno = c->u->u_lineno;
5279 int old_col_offset = c->u->u_col_offset;
5280 SET_LOC(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005282 switch (e->kind) {
5283 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005284 VISIT(c, expr, e->v.Attribute.value);
5285 ADDOP(c, DUP_TOP);
Mark Shannond48848c2021-03-14 18:01:30 +00005286 int old_lineno = c->u->u_lineno;
5287 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005288 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005289 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005290 break;
5291 case Subscript_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005292 VISIT(c, expr, e->v.Subscript.value);
5293 VISIT(c, expr, e->v.Subscript.slice);
5294 ADDOP(c, DUP_TOP_TWO);
5295 ADDOP(c, BINARY_SUBSCR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005296 break;
5297 case Name_kind:
5298 if (!compiler_nameop(c, e->v.Name.id, Load))
5299 return 0;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005300 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005301 default:
5302 PyErr_Format(PyExc_SystemError,
5303 "invalid node type (%d) for augmented assignment",
5304 e->kind);
5305 return 0;
5306 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005307
5308 c->u->u_lineno = old_lineno;
5309 c->u->u_col_offset = old_col_offset;
5310
5311 VISIT(c, expr, s->v.AugAssign.value);
5312 ADDOP(c, inplace_binop(s->v.AugAssign.op));
5313
5314 SET_LOC(c, e);
5315
5316 switch (e->kind) {
5317 case Attribute_kind:
Mark Shannond48848c2021-03-14 18:01:30 +00005318 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005319 ADDOP(c, ROT_TWO);
5320 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5321 break;
5322 case Subscript_kind:
5323 ADDOP(c, ROT_THREE);
5324 ADDOP(c, STORE_SUBSCR);
5325 break;
5326 case Name_kind:
5327 return compiler_nameop(c, e->v.Name.id, Store);
5328 default:
5329 Py_UNREACHABLE();
5330 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005331 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005332}
5333
5334static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005335check_ann_expr(struct compiler *c, expr_ty e)
5336{
5337 VISIT(c, expr, e);
5338 ADDOP(c, POP_TOP);
5339 return 1;
5340}
5341
5342static int
5343check_annotation(struct compiler *c, stmt_ty s)
5344{
5345 /* Annotations are only evaluated in a module or class. */
5346 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5347 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
5348 return check_ann_expr(c, s->v.AnnAssign.annotation);
5349 }
5350 return 1;
5351}
5352
5353static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005354check_ann_subscr(struct compiler *c, expr_ty e)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005355{
5356 /* We check that everything in a subscript is defined at runtime. */
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005357 switch (e->kind) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005358 case Slice_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005359 if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005360 return 0;
5361 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005362 if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) {
5363 return 0;
5364 }
5365 if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) {
5366 return 0;
5367 }
5368 return 1;
5369 case Tuple_kind: {
5370 /* extended slice */
Pablo Galindoa5634c42020-09-16 19:42:00 +01005371 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005372 Py_ssize_t i, n = asdl_seq_LEN(elts);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005373 for (i = 0; i < n; i++) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005374 if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005375 return 0;
5376 }
5377 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005378 return 1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005379 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005380 default:
5381 return check_ann_expr(c, e);
5382 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005383}
5384
5385static int
5386compiler_annassign(struct compiler *c, stmt_ty s)
5387{
5388 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07005389 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005390
5391 assert(s->kind == AnnAssign_kind);
5392
5393 /* We perform the actual assignment first. */
5394 if (s->v.AnnAssign.value) {
5395 VISIT(c, expr, s->v.AnnAssign.value);
5396 VISIT(c, expr, targ);
5397 }
5398 switch (targ->kind) {
5399 case Name_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005400 if (forbidden_name(c, targ->v.Name.id, Store))
5401 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005402 /* If we have a simple name in a module or class, store annotation. */
5403 if (s->v.AnnAssign.simple &&
5404 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5405 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Batuhan Taskaya044a1042020-10-06 23:03:02 +03005406 VISIT(c, annexpr, s->v.AnnAssign.annotation);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005407 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02005408 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005409 ADDOP_LOAD_CONST_NEW(c, mangled);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005410 ADDOP(c, STORE_SUBSCR);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005411 }
5412 break;
5413 case Attribute_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005414 if (forbidden_name(c, targ->v.Attribute.attr, Store))
5415 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005416 if (!s->v.AnnAssign.value &&
5417 !check_ann_expr(c, targ->v.Attribute.value)) {
5418 return 0;
5419 }
5420 break;
5421 case Subscript_kind:
5422 if (!s->v.AnnAssign.value &&
5423 (!check_ann_expr(c, targ->v.Subscript.value) ||
5424 !check_ann_subscr(c, targ->v.Subscript.slice))) {
5425 return 0;
5426 }
5427 break;
5428 default:
5429 PyErr_Format(PyExc_SystemError,
5430 "invalid node type (%d) for annotated assignment",
5431 targ->kind);
5432 return 0;
5433 }
5434 /* Annotation is evaluated last. */
5435 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
5436 return 0;
5437 }
5438 return 1;
5439}
5440
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005441/* Raises a SyntaxError and returns 0.
5442 If something goes wrong, a different exception may be raised.
5443*/
5444
5445static int
Brandt Bucher145bf262021-02-26 14:51:55 -08005446compiler_error(struct compiler *c, const char *format, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005447{
Brandt Bucher145bf262021-02-26 14:51:55 -08005448 va_list vargs;
5449#ifdef HAVE_STDARG_PROTOTYPES
5450 va_start(vargs, format);
5451#else
5452 va_start(vargs);
5453#endif
5454 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5455 va_end(vargs);
5456 if (msg == NULL) {
5457 return 0;
5458 }
5459 PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
5460 if (loc == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005461 Py_INCREF(Py_None);
5462 loc = Py_None;
5463 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005464 PyObject *args = Py_BuildValue("O(OiiO)", msg, c->c_filename,
5465 c->u->u_lineno, c->u->u_col_offset + 1, loc);
5466 Py_DECREF(msg);
5467 if (args == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005468 goto exit;
Brandt Bucher145bf262021-02-26 14:51:55 -08005469 }
5470 PyErr_SetObject(PyExc_SyntaxError, args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005471 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005472 Py_DECREF(loc);
Brandt Bucher145bf262021-02-26 14:51:55 -08005473 Py_XDECREF(args);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005474 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005475}
5476
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005477/* Emits a SyntaxWarning and returns 1 on success.
5478 If a SyntaxWarning raised as error, replaces it with a SyntaxError
5479 and returns 0.
5480*/
5481static int
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005482compiler_warn(struct compiler *c, const char *format, ...)
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005483{
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005484 va_list vargs;
5485#ifdef HAVE_STDARG_PROTOTYPES
5486 va_start(vargs, format);
5487#else
5488 va_start(vargs);
5489#endif
5490 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5491 va_end(vargs);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005492 if (msg == NULL) {
5493 return 0;
5494 }
5495 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
5496 c->u->u_lineno, NULL, NULL) < 0)
5497 {
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005498 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005499 /* Replace the SyntaxWarning exception with a SyntaxError
5500 to get a more accurate error report */
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005501 PyErr_Clear();
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005502 assert(PyUnicode_AsUTF8(msg) != NULL);
5503 compiler_error(c, PyUnicode_AsUTF8(msg));
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005504 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005505 Py_DECREF(msg);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005506 return 0;
5507 }
5508 Py_DECREF(msg);
5509 return 1;
5510}
5511
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005512static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005513compiler_subscript(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005514{
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005515 expr_context_ty ctx = e->v.Subscript.ctx;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005516 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005517
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005518 if (ctx == Load) {
5519 if (!check_subscripter(c, e->v.Subscript.value)) {
5520 return 0;
5521 }
5522 if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
5523 return 0;
5524 }
5525 }
5526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005527 switch (ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005528 case Load: op = BINARY_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005529 case Store: op = STORE_SUBSCR; break;
5530 case Del: op = DELETE_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005531 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005532 assert(op);
5533 VISIT(c, expr, e->v.Subscript.value);
5534 VISIT(c, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005535 ADDOP(c, op);
5536 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005537}
5538
5539static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005540compiler_slice(struct compiler *c, expr_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005542 int n = 2;
5543 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005545 /* only handles the cases where BUILD_SLICE is emitted */
5546 if (s->v.Slice.lower) {
5547 VISIT(c, expr, s->v.Slice.lower);
5548 }
5549 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005550 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005551 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005553 if (s->v.Slice.upper) {
5554 VISIT(c, expr, s->v.Slice.upper);
5555 }
5556 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005557 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005558 }
5559
5560 if (s->v.Slice.step) {
5561 n++;
5562 VISIT(c, expr, s->v.Slice.step);
5563 }
5564 ADDOP_I(c, BUILD_SLICE, n);
5565 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005566}
5567
Brandt Bucher145bf262021-02-26 14:51:55 -08005568
5569// PEP 634: Structural Pattern Matching
5570
5571// To keep things simple, all compiler_pattern_* routines follow the convention
5572// of replacing TOS (the subject for the given pattern) with either True (match)
5573// or False (no match). We do this even for irrefutable patterns; the idea is
5574// that it's much easier to smooth out any redundant pushing, popping, and
5575// jumping in the peephole optimizer than to detect or predict it here.
5576
5577
5578#define WILDCARD_CHECK(N) \
5579 ((N)->kind == Name_kind && \
5580 _PyUnicode_EqualToASCIIString((N)->v.Name.id, "_"))
5581
5582
5583static int
5584pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc)
5585{
5586 assert(!_PyUnicode_EqualToASCIIString(n, "_"));
5587 // Can't assign to the same name twice:
5588 if (pc->stores == NULL) {
5589 RETURN_IF_FALSE(pc->stores = PySet_New(NULL));
5590 }
5591 else {
5592 int duplicate = PySet_Contains(pc->stores, n);
5593 if (duplicate < 0) {
5594 return 0;
5595 }
5596 if (duplicate) {
5597 const char *e = "multiple assignments to name %R in pattern";
5598 return compiler_error(c, e, n);
5599 }
5600 }
5601 RETURN_IF_FALSE(!PySet_Add(pc->stores, n));
5602 RETURN_IF_FALSE(compiler_nameop(c, n, Store));
5603 return 1;
5604}
5605
5606
5607static int
5608pattern_helper_sequence_unpack(struct compiler *c, asdl_expr_seq *values,
5609 Py_ssize_t star, pattern_context *pc)
5610{
5611 RETURN_IF_FALSE(unpack_helper(c, values));
5612 // We've now got a bunch of new subjects on the stack. If any of them fail
5613 // to match, we need to pop everything else off, then finally push False.
5614 // fails is an array of blocks that correspond to the necessary amount of
5615 // popping for each element:
5616 basicblock **fails;
5617 Py_ssize_t size = asdl_seq_LEN(values);
5618 fails = (basicblock **)PyObject_Malloc(sizeof(basicblock*) * size);
5619 if (fails == NULL) {
5620 PyErr_NoMemory();
5621 return 0;
5622 }
5623 // NOTE: Can't use our nice returning macros anymore: they'll leak memory!
5624 // goto error on error.
5625 for (Py_ssize_t i = 0; i < size; i++) {
5626 fails[i] = compiler_new_block(c);
5627 if (fails[i] == NULL) {
5628 goto error;
5629 }
5630 }
5631 for (Py_ssize_t i = 0; i < size; i++) {
5632 expr_ty value = asdl_seq_GET(values, i);
5633 if (i == star) {
5634 assert(value->kind == Starred_kind);
5635 value = value->v.Starred.value;
5636 }
5637 if (!compiler_pattern_subpattern(c, value, pc) ||
5638 !compiler_addop_j(c, POP_JUMP_IF_FALSE, fails[i]) ||
5639 compiler_next_block(c) == NULL)
5640 {
5641 goto error;
5642 }
5643 }
5644 // Success!
5645 basicblock *end = compiler_new_block(c);
5646 if (end == NULL ||
5647 !compiler_addop_load_const(c, Py_True) ||
5648 !compiler_addop_j(c, JUMP_FORWARD, end))
5649 {
5650 goto error;
5651 }
5652 // This is where we handle failed sub-patterns. For a sequence pattern like
5653 // [a, b, c, d], this will look like:
5654 // fails[0]: POP_TOP
5655 // fails[1]: POP_TOP
5656 // fails[2]: POP_TOP
5657 // fails[3]: LOAD_CONST False
5658 for (Py_ssize_t i = 0; i < size - 1; i++) {
5659 compiler_use_next_block(c, fails[i]);
5660 if (!compiler_addop(c, POP_TOP)) {
5661 goto error;
5662 }
5663 }
5664 compiler_use_next_block(c, fails[size - 1]);
5665 if (!compiler_addop_load_const(c, Py_False)) {
5666 goto error;
5667 }
5668 compiler_use_next_block(c, end);
5669 PyObject_Free(fails);
5670 return 1;
5671error:
5672 PyObject_Free(fails);
5673 return 0;
5674}
5675
5676// Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of
5677// UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a
5678// starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc.
5679static int
5680pattern_helper_sequence_subscr(struct compiler *c, asdl_expr_seq *values,
5681 Py_ssize_t star, pattern_context *pc)
5682{
5683 basicblock *end, *fail_pop_1;
5684 RETURN_IF_FALSE(end = compiler_new_block(c));
5685 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5686 Py_ssize_t size = asdl_seq_LEN(values);
5687 for (Py_ssize_t i = 0; i < size; i++) {
5688 expr_ty value = asdl_seq_GET(values, i);
5689 if (WILDCARD_CHECK(value)) {
5690 continue;
5691 }
5692 if (i == star) {
5693 assert(value->kind == Starred_kind);
5694 assert(WILDCARD_CHECK(value->v.Starred.value));
5695 continue;
5696 }
5697 ADDOP(c, DUP_TOP);
5698 if (i < star) {
5699 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5700 }
5701 else {
5702 // The subject may not support negative indexing! Compute a
5703 // nonnegative index:
5704 ADDOP(c, GET_LEN);
5705 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i));
5706 ADDOP(c, BINARY_SUBTRACT);
5707 }
5708 ADDOP(c, BINARY_SUBSCR);
5709 RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc));
5710 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5711 NEXT_BLOCK(c);
5712 }
5713 ADDOP(c, POP_TOP);
5714 ADDOP_LOAD_CONST(c, Py_True);
5715 ADDOP_JUMP(c, JUMP_FORWARD, end);
5716 compiler_use_next_block(c, fail_pop_1);
5717 ADDOP(c, POP_TOP);
5718 ADDOP_LOAD_CONST(c, Py_False);
5719 compiler_use_next_block(c, end);
5720 return 1;
5721}
5722
5723
5724// Like compiler_pattern, but turn off checks for irrefutability.
5725static int
5726compiler_pattern_subpattern(struct compiler *c, expr_ty p, pattern_context *pc)
5727{
5728 int allow_irrefutable = pc->allow_irrefutable;
5729 pc->allow_irrefutable = 1;
5730 RETURN_IF_FALSE(compiler_pattern(c, p, pc));
5731 pc->allow_irrefutable = allow_irrefutable;
5732 return 1;
5733}
5734
5735
5736static int
5737compiler_pattern_as(struct compiler *c, expr_ty p, pattern_context *pc)
5738{
5739 assert(p->kind == MatchAs_kind);
5740 basicblock *end, *fail_pop_1;
5741 RETURN_IF_FALSE(end = compiler_new_block(c));
5742 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5743 // Need to make a copy for (possibly) storing later:
5744 ADDOP(c, DUP_TOP);
5745 RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc));
5746 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5747 NEXT_BLOCK(c);
5748 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc));
5749 ADDOP_LOAD_CONST(c, Py_True);
5750 ADDOP_JUMP(c, JUMP_FORWARD, end);
5751 compiler_use_next_block(c, fail_pop_1);
5752 // Need to pop that unused copy from before:
5753 ADDOP(c, POP_TOP);
5754 ADDOP_LOAD_CONST(c, Py_False);
5755 compiler_use_next_block(c, end);
5756 return 1;
5757}
5758
5759
5760static int
5761compiler_pattern_capture(struct compiler *c, expr_ty p, pattern_context *pc)
5762{
5763 assert(p->kind == Name_kind);
5764 assert(p->v.Name.ctx == Store);
5765 assert(!WILDCARD_CHECK(p));
5766 if (!pc->allow_irrefutable) {
5767 // Whoops, can't have a name capture here!
5768 const char *e = "name capture %R makes remaining patterns unreachable";
5769 return compiler_error(c, e, p->v.Name.id);
5770 }
5771 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.Name.id, pc));
5772 ADDOP_LOAD_CONST(c, Py_True);
5773 return 1;
5774}
5775
5776
5777static int
5778compiler_pattern_class(struct compiler *c, expr_ty p, pattern_context *pc)
5779{
5780 asdl_expr_seq *args = p->v.Call.args;
5781 asdl_keyword_seq *kwargs = p->v.Call.keywords;
5782 Py_ssize_t nargs = asdl_seq_LEN(args);
5783 Py_ssize_t nkwargs = asdl_seq_LEN(kwargs);
5784 if (INT_MAX < nargs || INT_MAX < nargs + nkwargs - 1) {
5785 const char *e = "too many sub-patterns in class pattern %R";
5786 return compiler_error(c, e, p->v.Call.func);
5787 }
5788 RETURN_IF_FALSE(!validate_keywords(c, kwargs));
5789 basicblock *end, *fail_pop_1;
5790 RETURN_IF_FALSE(end = compiler_new_block(c));
5791 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5792 VISIT(c, expr, p->v.Call.func);
5793 PyObject *kwnames;
5794 RETURN_IF_FALSE(kwnames = PyTuple_New(nkwargs));
5795 Py_ssize_t i;
5796 for (i = 0; i < nkwargs; i++) {
5797 PyObject *name = ((keyword_ty) asdl_seq_GET(kwargs, i))->arg;
5798 Py_INCREF(name);
5799 PyTuple_SET_ITEM(kwnames, i, name);
5800 }
5801 ADDOP_LOAD_CONST_NEW(c, kwnames);
5802 ADDOP_I(c, MATCH_CLASS, nargs);
5803 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5804 NEXT_BLOCK(c);
5805 // TOS is now a tuple of (nargs + nkwargs) attributes.
5806 for (i = 0; i < nargs + nkwargs; i++) {
5807 expr_ty arg;
5808 if (i < nargs) {
5809 // Positional:
5810 arg = asdl_seq_GET(args, i);
5811 }
5812 else {
5813 // Keyword:
5814 arg = ((keyword_ty) asdl_seq_GET(kwargs, i - nargs))->value;
5815 }
5816 if (WILDCARD_CHECK(arg)) {
5817 continue;
5818 }
5819 // Get the i-th attribute, and match it against the i-th pattern:
5820 ADDOP(c, DUP_TOP);
5821 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5822 ADDOP(c, BINARY_SUBSCR);
5823 RETURN_IF_FALSE(compiler_pattern_subpattern(c, arg, pc));
5824 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5825 NEXT_BLOCK(c);
5826 }
5827 // Success! Pop the tuple of attributes:
5828 ADDOP(c, POP_TOP);
5829 ADDOP_LOAD_CONST(c, Py_True);
5830 ADDOP_JUMP(c, JUMP_FORWARD, end);
5831 compiler_use_next_block(c, fail_pop_1);
5832 ADDOP(c, POP_TOP);
5833 ADDOP_LOAD_CONST(c, Py_False);
5834 compiler_use_next_block(c, end);
5835 return 1;
5836}
5837
5838
5839static int
5840compiler_pattern_literal(struct compiler *c, expr_ty p, pattern_context *pc)
5841{
5842 assert(p->kind == Constant_kind);
5843 PyObject *v = p->v.Constant.value;
5844 ADDOP_LOAD_CONST(c, v);
5845 // Literal True, False, and None are compared by identity. All others use
5846 // equality:
5847 ADDOP_COMPARE(c, (v == Py_None || PyBool_Check(v)) ? Is : Eq);
5848 return 1;
5849}
5850
5851
5852static int
5853compiler_pattern_mapping(struct compiler *c, expr_ty p, pattern_context *pc)
5854{
5855 basicblock *end, *fail_pop_1, *fail_pop_3;
5856 RETURN_IF_FALSE(end = compiler_new_block(c));
5857 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5858 RETURN_IF_FALSE(fail_pop_3 = compiler_new_block(c));
5859 asdl_expr_seq *keys = p->v.Dict.keys;
5860 asdl_expr_seq *values = p->v.Dict.values;
5861 Py_ssize_t size = asdl_seq_LEN(values);
Ikko Ashimine57827f82021-03-10 19:39:51 +09005862 // A starred pattern will be a keyless value. It is guaranteed to be last:
Brandt Bucher145bf262021-02-26 14:51:55 -08005863 int star = size ? !asdl_seq_GET(keys, size - 1) : 0;
5864 ADDOP(c, MATCH_MAPPING);
5865 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5866 NEXT_BLOCK(c);
5867 if (!size) {
5868 // If the pattern is just "{}", we're done!
5869 ADDOP(c, POP_TOP);
5870 ADDOP_LOAD_CONST(c, Py_True);
5871 ADDOP_JUMP(c, JUMP_FORWARD, end);
5872 compiler_use_next_block(c, fail_pop_1);
5873 ADDOP(c, POP_TOP);
5874 ADDOP_LOAD_CONST(c, Py_False);
5875 compiler_use_next_block(c, end);
5876 return 1;
5877 }
5878 if (size - star) {
5879 // If the pattern has any keys in it, perform a length check:
5880 ADDOP(c, GET_LEN);
5881 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - star));
5882 ADDOP_COMPARE(c, GtE);
5883 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5884 NEXT_BLOCK(c);
5885 }
5886 if (INT_MAX < size - star - 1) {
5887 return compiler_error(c, "too many sub-patterns in mapping pattern");
5888 }
5889 // Collect all of the keys into a tuple for MATCH_KEYS and
5890 // COPY_DICT_WITHOUT_KEYS. They can either be dotted names or literals:
5891 for (Py_ssize_t i = 0; i < size - star; i++) {
5892 expr_ty key = asdl_seq_GET(keys, i);
5893 if (key == NULL) {
5894 const char *e = "can't use starred name here "
5895 "(consider moving to end)";
5896 return compiler_error(c, e);
5897 }
5898 VISIT(c, expr, key);
5899 }
5900 ADDOP_I(c, BUILD_TUPLE, size - star);
5901 ADDOP(c, MATCH_KEYS);
5902 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3);
5903 NEXT_BLOCK(c);
5904 // So far so good. There's now a tuple of values on the stack to match
5905 // sub-patterns against:
5906 for (Py_ssize_t i = 0; i < size - star; i++) {
5907 expr_ty value = asdl_seq_GET(values, i);
5908 if (WILDCARD_CHECK(value)) {
5909 continue;
5910 }
5911 ADDOP(c, DUP_TOP);
5912 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5913 ADDOP(c, BINARY_SUBSCR);
5914 RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc));
5915 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3);
5916 NEXT_BLOCK(c);
5917 }
5918 // If we get this far, it's a match! We're done with that tuple of values.
5919 ADDOP(c, POP_TOP);
5920 if (star) {
5921 // If we had a starred name, bind a dict of remaining items to it:
5922 ADDOP(c, COPY_DICT_WITHOUT_KEYS);
5923 PyObject *id = asdl_seq_GET(values, size - 1)->v.Name.id;
5924 RETURN_IF_FALSE(pattern_helper_store_name(c, id, pc));
5925 }
5926 else {
5927 // Otherwise, we don't care about this tuple of keys anymore:
5928 ADDOP(c, POP_TOP);
5929 }
5930 // Pop the subject:
5931 ADDOP(c, POP_TOP);
5932 ADDOP_LOAD_CONST(c, Py_True);
5933 ADDOP_JUMP(c, JUMP_FORWARD, end);
5934 // The top two items are a tuple of values or None, followed by a tuple of
5935 // keys. Pop them both:
5936 compiler_use_next_block(c, fail_pop_3);
5937 ADDOP(c, POP_TOP);
5938 ADDOP(c, POP_TOP);
5939 compiler_use_next_block(c, fail_pop_1);
5940 // Pop the subject:
5941 ADDOP(c, POP_TOP);
5942 ADDOP_LOAD_CONST(c, Py_False);
5943 compiler_use_next_block(c, end);
5944 return 1;
5945}
5946
5947
5948static int
5949compiler_pattern_or(struct compiler *c, expr_ty p, pattern_context *pc)
5950{
5951 assert(p->kind == MatchOr_kind);
5952 // control is the set of names bound by the first alternative. If all of the
5953 // others bind the same names (they should), then this becomes pc->stores.
5954 PyObject *control = NULL;
5955 basicblock *end, *pass_pop_1;
5956 RETURN_IF_FALSE(end = compiler_new_block(c));
5957 RETURN_IF_FALSE(pass_pop_1 = compiler_new_block(c));
5958 Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns);
5959 assert(size > 1);
5960 // We're going to be messing with pc. Keep the original info handy:
5961 PyObject *stores_init = pc->stores;
5962 int allow_irrefutable = pc->allow_irrefutable;
5963 for (Py_ssize_t i = 0; i < size; i++) {
5964 // NOTE: Can't use our nice returning macros in here: they'll leak sets!
5965 expr_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i);
5966 pc->stores = PySet_New(stores_init);
5967 // An irrefutable sub-pattern must be last, if it is allowed at all:
5968 int is_last = i == size - 1;
5969 pc->allow_irrefutable = allow_irrefutable && is_last;
5970 SET_LOC(c, alt);
5971 if (pc->stores == NULL ||
5972 // Only copy the subject if we're *not* on the last alternative:
5973 (!is_last && !compiler_addop(c, DUP_TOP)) ||
5974 !compiler_pattern(c, alt, pc) ||
5975 // Only jump if we're *not* on the last alternative:
5976 (!is_last && !compiler_addop_j(c, POP_JUMP_IF_TRUE, pass_pop_1)) ||
5977 !compiler_next_block(c))
5978 {
5979 goto fail;
5980 }
5981 if (!i) {
5982 // If this is the first alternative, save its stores as a "control"
5983 // for the others (they can't bind a different set of names):
5984 control = pc->stores;
5985 continue;
5986 }
5987 if (PySet_GET_SIZE(pc->stores) || PySet_GET_SIZE(control)) {
5988 // Otherwise, check to see if we differ from the control set:
5989 PyObject *diff = PyNumber_InPlaceXor(pc->stores, control);
5990 if (diff == NULL) {
5991 goto fail;
5992 }
5993 if (PySet_GET_SIZE(diff)) {
5994 // The names differ! Raise.
5995 Py_DECREF(diff);
5996 compiler_error(c, "alternative patterns bind different names");
5997 goto fail;
5998 }
5999 Py_DECREF(diff);
6000 }
6001 Py_DECREF(pc->stores);
6002 }
6003 Py_XDECREF(stores_init);
6004 // Update pc->stores and restore pc->allow_irrefutable:
6005 pc->stores = control;
6006 pc->allow_irrefutable = allow_irrefutable;
6007 ADDOP_JUMP(c, JUMP_FORWARD, end);
6008 compiler_use_next_block(c, pass_pop_1);
6009 ADDOP(c, POP_TOP);
6010 ADDOP_LOAD_CONST(c, Py_True);
6011 compiler_use_next_block(c, end);
6012 return 1;
6013fail:
6014 Py_XDECREF(stores_init);
6015 Py_XDECREF(control);
6016 return 0;
6017}
6018
6019
6020static int
6021compiler_pattern_sequence(struct compiler *c, expr_ty p, pattern_context *pc)
6022{
6023 assert(p->kind == List_kind || p->kind == Tuple_kind);
6024 asdl_expr_seq *values = (p->kind == Tuple_kind) ? p->v.Tuple.elts
6025 : p->v.List.elts;
6026 Py_ssize_t size = asdl_seq_LEN(values);
6027 Py_ssize_t star = -1;
6028 int only_wildcard = 1;
6029 int star_wildcard = 0;
6030 // Find a starred name, if it exists. There may be at most one:
6031 for (Py_ssize_t i = 0; i < size; i++) {
6032 expr_ty value = asdl_seq_GET(values, i);
6033 if (value->kind == Starred_kind) {
6034 value = value->v.Starred.value;
6035 if (star >= 0) {
6036 const char *e = "multiple starred names in sequence pattern";
6037 return compiler_error(c, e);
6038 }
6039 star_wildcard = WILDCARD_CHECK(value);
6040 star = i;
6041 }
6042 only_wildcard &= WILDCARD_CHECK(value);
6043 }
6044 basicblock *end, *fail_pop_1;
6045 RETURN_IF_FALSE(end = compiler_new_block(c));
6046 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
6047 ADDOP(c, MATCH_SEQUENCE);
6048 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
6049 NEXT_BLOCK(c);
6050 if (star < 0) {
6051 // No star: len(subject) == size
6052 ADDOP(c, GET_LEN);
6053 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
6054 ADDOP_COMPARE(c, Eq);
6055 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
6056 NEXT_BLOCK(c);
6057 }
6058 else if (size > 1) {
6059 // Star: len(subject) >= size - 1
6060 ADDOP(c, GET_LEN);
6061 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1));
6062 ADDOP_COMPARE(c, GtE);
6063 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
6064 NEXT_BLOCK(c);
6065 }
6066 if (only_wildcard) {
6067 // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc.
6068 ADDOP(c, POP_TOP);
6069 ADDOP_LOAD_CONST(c, Py_True);
6070 }
6071 else if (star_wildcard) {
6072 RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, values, star, pc));
6073 }
6074 else {
6075 RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, values, star, pc));
6076 }
6077 ADDOP_JUMP(c, JUMP_FORWARD, end);
6078 compiler_use_next_block(c, fail_pop_1);
6079 ADDOP(c, POP_TOP)
6080 ADDOP_LOAD_CONST(c, Py_False);
6081 compiler_use_next_block(c, end);
6082 return 1;
6083}
6084
6085
6086static int
6087compiler_pattern_value(struct compiler *c, expr_ty p, pattern_context *pc)
6088{
6089 assert(p->kind == Attribute_kind);
6090 assert(p->v.Attribute.ctx == Load);
6091 VISIT(c, expr, p);
6092 ADDOP_COMPARE(c, Eq);
6093 return 1;
6094}
6095
6096
6097static int
6098compiler_pattern_wildcard(struct compiler *c, expr_ty p, pattern_context *pc)
6099{
6100 assert(p->kind == Name_kind);
6101 assert(p->v.Name.ctx == Store);
6102 assert(WILDCARD_CHECK(p));
6103 if (!pc->allow_irrefutable) {
6104 // Whoops, can't have a wildcard here!
6105 const char *e = "wildcard makes remaining patterns unreachable";
6106 return compiler_error(c, e);
6107 }
6108 ADDOP(c, POP_TOP);
6109 ADDOP_LOAD_CONST(c, Py_True);
6110 return 1;
6111}
6112
6113
6114static int
6115compiler_pattern(struct compiler *c, expr_ty p, pattern_context *pc)
6116{
6117 SET_LOC(c, p);
6118 switch (p->kind) {
6119 case Attribute_kind:
6120 return compiler_pattern_value(c, p, pc);
6121 case BinOp_kind:
6122 // Because we allow "2+2j", things like "2+2" make it this far:
6123 return compiler_error(c, "patterns cannot include operators");
6124 case Call_kind:
6125 return compiler_pattern_class(c, p, pc);
6126 case Constant_kind:
6127 return compiler_pattern_literal(c, p, pc);
6128 case Dict_kind:
6129 return compiler_pattern_mapping(c, p, pc);
6130 case JoinedStr_kind:
6131 // Because we allow strings, f-strings make it this far:
6132 return compiler_error(c, "patterns cannot include f-strings");
6133 case List_kind:
6134 case Tuple_kind:
6135 return compiler_pattern_sequence(c, p, pc);
6136 case MatchAs_kind:
6137 return compiler_pattern_as(c, p, pc);
6138 case MatchOr_kind:
6139 return compiler_pattern_or(c, p, pc);
6140 case Name_kind:
6141 if (WILDCARD_CHECK(p)) {
6142 return compiler_pattern_wildcard(c, p, pc);
6143 }
6144 return compiler_pattern_capture(c, p, pc);
6145 default:
6146 Py_UNREACHABLE();
6147 }
6148}
6149
6150
6151static int
6152compiler_match(struct compiler *c, stmt_ty s)
6153{
6154 VISIT(c, expr, s->v.Match.subject);
6155 basicblock *next, *end;
6156 RETURN_IF_FALSE(end = compiler_new_block(c));
6157 Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases);
6158 assert(cases);
6159 pattern_context pc;
6160 // We use pc.stores to track:
6161 // - Repeated name assignments in the same pattern.
6162 // - Different name assignments in alternatives.
6163 // It's a set of names, but we don't create it until it's needed:
6164 pc.stores = NULL;
6165 match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6166 int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases;
6167 for (Py_ssize_t i = 0; i < cases - has_default; i++) {
6168 m = asdl_seq_GET(s->v.Match.cases, i);
6169 SET_LOC(c, m->pattern);
6170 RETURN_IF_FALSE(next = compiler_new_block(c));
6171 // If pc.allow_irrefutable is 0, any name captures against our subject
6172 // will raise. Irrefutable cases must be either guarded, last, or both:
6173 pc.allow_irrefutable = m->guard != NULL || i == cases - 1;
6174 // Only copy the subject if we're *not* on the last case:
6175 if (i != cases - has_default - 1) {
6176 ADDOP(c, DUP_TOP);
6177 }
6178 int result = compiler_pattern(c, m->pattern, &pc);
6179 Py_CLEAR(pc.stores);
6180 RETURN_IF_FALSE(result);
6181 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, next);
6182 NEXT_BLOCK(c);
6183 if (m->guard) {
6184 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, next, 0));
6185 }
6186 // Success! Pop the subject off, we're done with it:
6187 if (i != cases - has_default - 1) {
6188 ADDOP(c, POP_TOP);
6189 }
6190 VISIT_SEQ(c, stmt, m->body);
6191 ADDOP_JUMP(c, JUMP_FORWARD, end);
6192 compiler_use_next_block(c, next);
6193 }
6194 if (has_default) {
6195 if (cases == 1) {
6196 // No matches. Done with the subject:
6197 ADDOP(c, POP_TOP);
6198 }
6199 // A trailing "case _" is common, and lets us save a bit of redundant
6200 // pushing and popping in the loop above:
6201 m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6202 SET_LOC(c, m->pattern);
6203 if (m->guard) {
6204 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0));
6205 }
6206 VISIT_SEQ(c, stmt, m->body);
6207 }
6208 compiler_use_next_block(c, end);
6209 return 1;
6210}
6211
6212
6213#undef WILDCARD_CHECK
6214
6215
Thomas Wouters89f507f2006-12-13 04:49:30 +00006216/* End of the compiler section, beginning of the assembler section */
6217
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006218/* do depth-first search of basic block graph, starting with block.
T. Wouters99b54d62019-09-12 07:05:33 -07006219 post records the block indices in post-order.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006220
6221 XXX must handle implicit jumps from one block to next
6222*/
6223
Thomas Wouters89f507f2006-12-13 04:49:30 +00006224struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006225 PyObject *a_bytecode; /* string containing bytecode */
6226 int a_offset; /* offset into bytecode */
6227 int a_nblocks; /* number of reachable blocks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006228 PyObject *a_lnotab; /* string containing lnotab */
6229 int a_lnotab_off; /* offset into lnotab */
Mark Shannon877df852020-11-12 09:43:29 +00006230 int a_prevlineno; /* lineno of last emitted line in line table */
6231 int a_lineno; /* lineno of last emitted instruction */
6232 int a_lineno_start; /* bytecode start offset of current lineno */
Mark Shannoncc75ab72020-11-12 19:49:33 +00006233 basicblock *a_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006234};
6235
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006236Py_LOCAL_INLINE(void)
6237stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006238{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02006239 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
Mark Shannonfee55262019-11-21 09:11:43 +00006240 if (b->b_startdepth < depth && b->b_startdepth < 100) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006241 assert(b->b_startdepth < 0);
6242 b->b_startdepth = depth;
6243 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02006244 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006245}
6246
6247/* Find the flow path that needs the largest stack. We assume that
6248 * cycles in the flow graph have no net effect on the stack depth.
6249 */
6250static int
6251stackdepth(struct compiler *c)
6252{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006253 basicblock *b, *entryblock = NULL;
6254 basicblock **stack, **sp;
6255 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006256 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006257 b->b_startdepth = INT_MIN;
6258 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006259 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006260 }
Mark Shannon67969f52021-04-07 10:52:07 +01006261 assert(entryblock!= NULL);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006262 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
6263 if (!stack) {
6264 PyErr_NoMemory();
6265 return -1;
6266 }
6267
6268 sp = stack;
Mark Shannonb37181e2021-04-06 11:48:59 +01006269 if (c->u->u_ste->ste_generator || c->u->u_ste->ste_coroutine) {
6270 stackdepth_push(&sp, entryblock, 1);
6271 } else {
6272 stackdepth_push(&sp, entryblock, 0);
6273 }
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006274 while (sp != stack) {
6275 b = *--sp;
6276 int depth = b->b_startdepth;
6277 assert(depth >= 0);
6278 basicblock *next = b->b_next;
6279 for (int i = 0; i < b->b_iused; i++) {
6280 struct instr *instr = &b->b_instr[i];
6281 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
6282 if (effect == PY_INVALID_STACK_EFFECT) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01006283 PyErr_Format(PyExc_SystemError,
6284 "compiler stack_effect(opcode=%d, arg=%i) failed",
6285 instr->i_opcode, instr->i_oparg);
6286 return -1;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006287 }
6288 int new_depth = depth + effect;
6289 if (new_depth > maxdepth) {
6290 maxdepth = new_depth;
6291 }
6292 assert(depth >= 0); /* invalid code or bug in stackdepth() */
Mark Shannon582aaf12020-08-04 17:30:11 +01006293 if (is_jump(instr)) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006294 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
6295 assert(effect != PY_INVALID_STACK_EFFECT);
6296 int target_depth = depth + effect;
6297 if (target_depth > maxdepth) {
6298 maxdepth = target_depth;
6299 }
6300 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006301 stackdepth_push(&sp, instr->i_target, target_depth);
6302 }
6303 depth = new_depth;
6304 if (instr->i_opcode == JUMP_ABSOLUTE ||
6305 instr->i_opcode == JUMP_FORWARD ||
6306 instr->i_opcode == RETURN_VALUE ||
Mark Shannonfee55262019-11-21 09:11:43 +00006307 instr->i_opcode == RAISE_VARARGS ||
6308 instr->i_opcode == RERAISE)
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006309 {
6310 /* remaining code is dead */
6311 next = NULL;
6312 break;
6313 }
6314 }
6315 if (next != NULL) {
Mark Shannon266b4622020-11-17 19:30:14 +00006316 assert(b->b_nofallthrough == 0);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006317 stackdepth_push(&sp, next, depth);
6318 }
6319 }
6320 PyObject_Free(stack);
6321 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006322}
6323
6324static int
6325assemble_init(struct assembler *a, int nblocks, int firstlineno)
6326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006327 memset(a, 0, sizeof(struct assembler));
Mark Shannon877df852020-11-12 09:43:29 +00006328 a->a_prevlineno = a->a_lineno = firstlineno;
Mark Shannonfd009e62020-11-13 12:53:53 +00006329 a->a_lnotab = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006330 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006331 if (a->a_bytecode == NULL) {
6332 goto error;
6333 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006334 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006335 if (a->a_lnotab == NULL) {
6336 goto error;
6337 }
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07006338 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006339 PyErr_NoMemory();
Mark Shannonfd009e62020-11-13 12:53:53 +00006340 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006341 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006342 return 1;
Mark Shannonfd009e62020-11-13 12:53:53 +00006343error:
6344 Py_XDECREF(a->a_bytecode);
6345 Py_XDECREF(a->a_lnotab);
6346 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006347}
6348
6349static void
6350assemble_free(struct assembler *a)
6351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006352 Py_XDECREF(a->a_bytecode);
6353 Py_XDECREF(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006354}
6355
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006356static int
6357blocksize(basicblock *b)
6358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006359 int i;
6360 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006362 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006363 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006364 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006365}
6366
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006367static int
Mark Shannon877df852020-11-12 09:43:29 +00006368assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006369{
Mark Shannon877df852020-11-12 09:43:29 +00006370 Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006371 if (a->a_lnotab_off + 2 >= len) {
6372 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
6373 return 0;
6374 }
Pablo Galindo86e322f2021-01-30 13:54:22 +00006375 unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab);
6376 lnotab += a->a_lnotab_off;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006377 a->a_lnotab_off += 2;
Mark Shannon877df852020-11-12 09:43:29 +00006378 *lnotab++ = bdelta;
6379 *lnotab++ = ldelta;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006380 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006381}
6382
Mark Shannon877df852020-11-12 09:43:29 +00006383/* Appends a range to the end of the line number table. See
6384 * Objects/lnotab_notes.txt for the description of the line number table. */
6385
6386static int
6387assemble_line_range(struct assembler *a)
6388{
6389 int ldelta, bdelta;
6390 bdelta = (a->a_offset - a->a_lineno_start) * 2;
6391 if (bdelta == 0) {
6392 return 1;
6393 }
6394 if (a->a_lineno < 0) {
6395 ldelta = -128;
6396 }
6397 else {
6398 ldelta = a->a_lineno - a->a_prevlineno;
6399 a->a_prevlineno = a->a_lineno;
6400 while (ldelta > 127) {
6401 if (!assemble_emit_linetable_pair(a, 0, 127)) {
6402 return 0;
6403 }
6404 ldelta -= 127;
6405 }
6406 while (ldelta < -127) {
6407 if (!assemble_emit_linetable_pair(a, 0, -127)) {
6408 return 0;
6409 }
6410 ldelta += 127;
6411 }
6412 }
6413 assert(-128 <= ldelta && ldelta < 128);
6414 while (bdelta > 254) {
6415 if (!assemble_emit_linetable_pair(a, 254, ldelta)) {
6416 return 0;
6417 }
6418 ldelta = a->a_lineno < 0 ? -128 : 0;
6419 bdelta -= 254;
6420 }
6421 if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) {
6422 return 0;
6423 }
6424 a->a_lineno_start = a->a_offset;
6425 return 1;
6426}
6427
6428static int
6429assemble_lnotab(struct assembler *a, struct instr *i)
6430{
6431 if (i->i_lineno == a->a_lineno) {
6432 return 1;
6433 }
6434 if (!assemble_line_range(a)) {
6435 return 0;
6436 }
6437 a->a_lineno = i->i_lineno;
6438 return 1;
6439}
6440
6441
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006442/* assemble_emit()
6443 Extend the bytecode with a new instruction.
6444 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00006445*/
6446
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006447static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006448assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006449{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006450 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006451 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03006452 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006453
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006454 arg = i->i_oparg;
6455 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006456 if (i->i_lineno && !assemble_lnotab(a, i))
6457 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006458 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006459 if (len > PY_SSIZE_T_MAX / 2)
6460 return 0;
6461 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
6462 return 0;
6463 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03006464 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006465 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006466 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006467 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006468}
6469
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00006470static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006471assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006473 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006474 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006475 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00006476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006477 /* Compute the size of each block and fixup jump args.
6478 Replace block pointer with position in bytecode. */
6479 do {
6480 totsize = 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006481 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006482 bsize = blocksize(b);
6483 b->b_offset = totsize;
6484 totsize += bsize;
6485 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006486 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006487 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6488 bsize = b->b_offset;
6489 for (i = 0; i < b->b_iused; i++) {
6490 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006491 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006492 /* Relative jumps are computed relative to
6493 the instruction pointer after fetching
6494 the jump instruction.
6495 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006496 bsize += isize;
Mark Shannon582aaf12020-08-04 17:30:11 +01006497 if (is_jump(instr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006498 instr->i_oparg = instr->i_target->b_offset;
Mark Shannon582aaf12020-08-04 17:30:11 +01006499 if (is_relative_jump(instr)) {
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006500 instr->i_oparg -= bsize;
6501 }
6502 if (instrsize(instr->i_oparg) != isize) {
6503 extended_arg_recompile = 1;
6504 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006505 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006506 }
6507 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00006508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006509 /* XXX: This is an awful hack that could hurt performance, but
6510 on the bright side it should work until we come up
6511 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006513 The issue is that in the first loop blocksize() is called
6514 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006515 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006516 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006518 So we loop until we stop seeing new EXTENDED_ARGs.
6519 The only EXTENDED_ARGs that could be popping up are
6520 ones in jump instructions. So this should converge
6521 fairly quickly.
6522 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006523 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00006524}
6525
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006526static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01006527dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006529 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006530 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006532 tuple = PyTuple_New(size);
6533 if (tuple == NULL)
6534 return NULL;
6535 while (PyDict_Next(dict, &pos, &k, &v)) {
6536 i = PyLong_AS_LONG(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006537 Py_INCREF(k);
6538 assert((i - offset) < size);
6539 assert((i - offset) >= 0);
6540 PyTuple_SET_ITEM(tuple, i - offset, k);
6541 }
6542 return tuple;
6543}
6544
6545static PyObject *
6546consts_dict_keys_inorder(PyObject *dict)
6547{
6548 PyObject *consts, *k, *v;
6549 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
6550
6551 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
6552 if (consts == NULL)
6553 return NULL;
6554 while (PyDict_Next(dict, &pos, &k, &v)) {
6555 i = PyLong_AS_LONG(v);
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +03006556 /* The keys of the dictionary can be tuples wrapping a contant.
6557 * (see compiler_add_o and _PyCode_ConstantKey). In that case
6558 * the object we want is always second. */
6559 if (PyTuple_CheckExact(k)) {
6560 k = PyTuple_GET_ITEM(k, 1);
6561 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006562 Py_INCREF(k);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006563 assert(i < size);
6564 assert(i >= 0);
6565 PyList_SET_ITEM(consts, i, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006566 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006567 return consts;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006568}
6569
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006570static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006571compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006572{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006573 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006574 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006575 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04006576 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006577 if (ste->ste_nested)
6578 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07006579 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006580 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07006581 if (!ste->ste_generator && ste->ste_coroutine)
6582 flags |= CO_COROUTINE;
6583 if (ste->ste_generator && ste->ste_coroutine)
6584 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006585 if (ste->ste_varargs)
6586 flags |= CO_VARARGS;
6587 if (ste->ste_varkeywords)
6588 flags |= CO_VARKEYWORDS;
6589 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006591 /* (Only) inherit compilerflags in PyCF_MASK */
6592 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006593
Pablo Galindo90235812020-03-15 04:29:22 +00006594 if ((IS_TOP_LEVEL_AWAIT(c)) &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07006595 ste->ste_coroutine &&
6596 !ste->ste_generator) {
6597 flags |= CO_COROUTINE;
6598 }
6599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006600 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00006601}
6602
Inada Naokibdb941b2021-02-10 09:20:42 +09006603// Merge *obj* with constant cache.
INADA Naokic2e16072018-11-26 21:23:22 +09006604// Unlike merge_consts_recursive(), this function doesn't work recursively.
6605static int
Inada Naokibdb941b2021-02-10 09:20:42 +09006606merge_const_one(struct compiler *c, PyObject **obj)
INADA Naokic2e16072018-11-26 21:23:22 +09006607{
Inada Naokibdb941b2021-02-10 09:20:42 +09006608 PyObject *key = _PyCode_ConstantKey(*obj);
INADA Naokic2e16072018-11-26 21:23:22 +09006609 if (key == NULL) {
6610 return 0;
6611 }
6612
6613 // t is borrowed reference
6614 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
6615 Py_DECREF(key);
6616 if (t == NULL) {
6617 return 0;
6618 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006619 if (t == key) { // obj is new constant.
INADA Naokic2e16072018-11-26 21:23:22 +09006620 return 1;
6621 }
6622
Inada Naokibdb941b2021-02-10 09:20:42 +09006623 if (PyTuple_CheckExact(t)) {
6624 // t is still borrowed reference
6625 t = PyTuple_GET_ITEM(t, 1);
6626 }
6627
6628 Py_INCREF(t);
6629 Py_DECREF(*obj);
6630 *obj = t;
INADA Naokic2e16072018-11-26 21:23:22 +09006631 return 1;
6632}
6633
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006634static PyCodeObject *
Mark Shannon6e8128f2020-07-30 10:03:00 +01006635makecode(struct compiler *c, struct assembler *a, PyObject *consts)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006636{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006637 PyCodeObject *co = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006638 PyObject *names = NULL;
6639 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006640 PyObject *name = NULL;
6641 PyObject *freevars = NULL;
6642 PyObject *cellvars = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006643 Py_ssize_t nlocals;
6644 int nlocals_int;
6645 int flags;
Pablo Galindocd74e662019-06-01 18:08:04 +01006646 int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006648 names = dict_keys_inorder(c->u->u_names, 0);
6649 varnames = dict_keys_inorder(c->u->u_varnames, 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006650 if (!names || !varnames) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006651 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006652 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006653 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
6654 if (!cellvars)
6655 goto error;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006656 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006657 if (!freevars)
6658 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006659
Inada Naokibdb941b2021-02-10 09:20:42 +09006660 if (!merge_const_one(c, &names) ||
6661 !merge_const_one(c, &varnames) ||
6662 !merge_const_one(c, &cellvars) ||
6663 !merge_const_one(c, &freevars))
INADA Naokic2e16072018-11-26 21:23:22 +09006664 {
6665 goto error;
6666 }
6667
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006668 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01006669 assert(nlocals < INT_MAX);
6670 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
6671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006672 flags = compute_code_flags(c);
6673 if (flags < 0)
6674 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006675
Mark Shannon6e8128f2020-07-30 10:03:00 +01006676 consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
6677 if (consts == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006678 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006679 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006680 if (!merge_const_one(c, &consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006681 Py_DECREF(consts);
INADA Naokic2e16072018-11-26 21:23:22 +09006682 goto error;
6683 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006684
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01006685 posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int);
Pablo Galindocd74e662019-06-01 18:08:04 +01006686 posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +01006687 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006688 maxdepth = stackdepth(c);
6689 if (maxdepth < 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006690 Py_DECREF(consts);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006691 goto error;
6692 }
Mark Shannon11e0b292021-04-15 14:28:56 +01006693 if (maxdepth > MAX_ALLOWED_STACK_USE) {
6694 PyErr_Format(PyExc_SystemError,
6695 "excessive stack use: stack is %d deep",
6696 maxdepth);
6697 Py_DECREF(consts);
6698 goto error;
6699 }
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006700 co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
Mark Shannon13bc1392020-01-23 09:25:17 +00006701 posonlyargcount, kwonlyargcount, nlocals_int,
Mark Shannon6e8128f2020-07-30 10:03:00 +01006702 maxdepth, flags, a->a_bytecode, consts, names,
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006703 varnames, freevars, cellvars, c->c_filename,
6704 c->u->u_name, c->u->u_firstlineno, a->a_lnotab);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006705 Py_DECREF(consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006706 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006707 Py_XDECREF(names);
6708 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006709 Py_XDECREF(name);
6710 Py_XDECREF(freevars);
6711 Py_XDECREF(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006712 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006713}
6714
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006715
6716/* For debugging purposes only */
6717#if 0
6718static void
6719dump_instr(const struct instr *i)
6720{
Mark Shannon582aaf12020-08-04 17:30:11 +01006721 const char *jrel = (is_relative_jump(instr)) ? "jrel " : "";
6722 const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : "";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006723 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006725 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006726 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006727 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006728 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006729 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
6730 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006731}
6732
6733static void
6734dump_basicblock(const basicblock *b)
6735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006736 const char *b_return = b->b_return ? "return " : "";
Pablo Galindo60eb9f12020-06-28 01:55:47 +01006737 fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n",
6738 b->b_iused, b->b_startdepth, b->b_offset, b_return);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006739 if (b->b_instr) {
6740 int i;
6741 for (i = 0; i < b->b_iused; i++) {
6742 fprintf(stderr, " [%02d] ", i);
6743 dump_instr(b->b_instr + i);
6744 }
6745 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006746}
6747#endif
6748
Mark Shannon5977a792020-12-02 13:31:40 +00006749
6750static int
6751normalize_basic_block(basicblock *bb);
6752
Mark Shannon6e8128f2020-07-30 10:03:00 +01006753static int
Inada Naoki8a232c72021-04-16 14:01:04 +09006754optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006755
Mark Shannon5977a792020-12-02 13:31:40 +00006756static int
6757ensure_exits_have_lineno(struct compiler *c);
6758
Mark Shannonb37181e2021-04-06 11:48:59 +01006759static int
6760insert_generator_prefix(struct compiler *c, basicblock *entryblock) {
6761
6762 int flags = compute_code_flags(c);
6763 if (flags < 0) {
6764 return -1;
6765 }
6766 int kind;
6767 if (flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
6768 if (flags & CO_COROUTINE) {
6769 kind = 1;
6770 }
6771 else if (flags & CO_ASYNC_GENERATOR) {
6772 kind = 2;
6773 }
6774 else {
6775 kind = 0;
6776 }
6777 }
6778 else {
6779 return 0;
6780 }
6781 if (compiler_next_instr(entryblock) < 0) {
6782 return -1;
6783 }
6784 for (int i = entryblock->b_iused-1; i > 0; i--) {
6785 entryblock->b_instr[i] = entryblock->b_instr[i-1];
6786 }
6787 entryblock->b_instr[0].i_opcode = GEN_START;
6788 entryblock->b_instr[0].i_oparg = kind;
6789 entryblock->b_instr[0].i_lineno = -1;
6790 entryblock->b_instr[0].i_target = NULL;
6791 return 0;
6792}
6793
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006794static PyCodeObject *
6795assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006796{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006797 basicblock *b, *entryblock;
6798 struct assembler a;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006799 int j, nblocks;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006800 PyCodeObject *co = NULL;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006801 PyObject *consts = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006803 /* Make sure every block that falls off the end returns None.
6804 XXX NEXT_BLOCK() isn't quite right, because if the last
6805 block ends with a jump or return b_next shouldn't set.
6806 */
6807 if (!c->u->u_curblock->b_return) {
Mark Shannon877df852020-11-12 09:43:29 +00006808 c->u->u_lineno = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006809 if (addNone)
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006810 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006811 ADDOP(c, RETURN_VALUE);
6812 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006813
Mark Shannon5977a792020-12-02 13:31:40 +00006814 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
6815 if (normalize_basic_block(b)) {
Alex Henrie503627f2021-03-02 03:20:25 -07006816 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00006817 }
6818 }
6819
6820 if (ensure_exits_have_lineno(c)) {
Alex Henrie503627f2021-03-02 03:20:25 -07006821 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00006822 }
6823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006824 nblocks = 0;
6825 entryblock = NULL;
6826 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6827 nblocks++;
6828 entryblock = b;
6829 }
Mark Shannon67969f52021-04-07 10:52:07 +01006830 assert(entryblock != NULL);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006831
Mark Shannonb37181e2021-04-06 11:48:59 +01006832 if (insert_generator_prefix(c, entryblock)) {
6833 goto error;
6834 }
6835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006836 /* Set firstlineno if it wasn't explicitly set. */
6837 if (!c->u->u_firstlineno) {
Mark Shannon67969f52021-04-07 10:52:07 +01006838 if (entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006839 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
Mark Shannon877df852020-11-12 09:43:29 +00006840 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006841 c->u->u_firstlineno = 1;
6842 }
Mark Shannon5977a792020-12-02 13:31:40 +00006843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006844 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
6845 goto error;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006846 a.a_entry = entryblock;
6847 a.a_nblocks = nblocks;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006848
Mark Shannon6e8128f2020-07-30 10:03:00 +01006849 consts = consts_dict_keys_inorder(c->u->u_consts);
6850 if (consts == NULL) {
6851 goto error;
6852 }
Inada Naoki8a232c72021-04-16 14:01:04 +09006853 if (optimize_cfg(c, &a, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006854 goto error;
6855 }
6856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006857 /* Can't modify the bytecode after computing jump offsets. */
6858 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00006859
Mark Shannoncc75ab72020-11-12 19:49:33 +00006860 /* Emit code. */
6861 for(b = entryblock; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006862 for (j = 0; j < b->b_iused; j++)
6863 if (!assemble_emit(&a, &b->b_instr[j]))
6864 goto error;
6865 }
Mark Shannon877df852020-11-12 09:43:29 +00006866 if (!assemble_line_range(&a)) {
6867 return 0;
6868 }
6869 /* Emit sentinel at end of line number table */
6870 if (!assemble_emit_linetable_pair(&a, 255, -128)) {
6871 goto error;
6872 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00006873
Inada Naokibdb941b2021-02-10 09:20:42 +09006874 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006875 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09006876 }
6877 if (!merge_const_one(c, &a.a_lnotab)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006878 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09006879 }
6880 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) {
6881 goto error;
6882 }
6883 if (!merge_const_one(c, &a.a_bytecode)) {
6884 goto error;
6885 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006886
Mark Shannon6e8128f2020-07-30 10:03:00 +01006887 co = makecode(c, &a, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006888 error:
Mark Shannon6e8128f2020-07-30 10:03:00 +01006889 Py_XDECREF(consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006890 assemble_free(&a);
6891 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006892}
Georg Brandl8334fd92010-12-04 10:26:46 +00006893
Mark Shannon6e8128f2020-07-30 10:03:00 +01006894/* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
6895 with LOAD_CONST (c1, c2, ... cn).
6896 The consts table must still be in list form so that the
6897 new constant (c1, c2, ... cn) can be appended.
6898 Called with codestr pointing to the first LOAD_CONST.
6899*/
6900static int
Inada Naoki8a232c72021-04-16 14:01:04 +09006901fold_tuple_on_constants(struct compiler *c,
6902 struct instr *inst,
Mark Shannon6e8128f2020-07-30 10:03:00 +01006903 int n, PyObject *consts)
6904{
6905 /* Pre-conditions */
6906 assert(PyList_CheckExact(consts));
6907 assert(inst[n].i_opcode == BUILD_TUPLE);
6908 assert(inst[n].i_oparg == n);
6909
6910 for (int i = 0; i < n; i++) {
6911 if (inst[i].i_opcode != LOAD_CONST) {
6912 return 0;
6913 }
6914 }
6915
6916 /* Buildup new tuple of constants */
6917 PyObject *newconst = PyTuple_New(n);
6918 if (newconst == NULL) {
6919 return -1;
6920 }
6921 for (int i = 0; i < n; i++) {
6922 int arg = inst[i].i_oparg;
6923 PyObject *constant = PyList_GET_ITEM(consts, arg);
6924 Py_INCREF(constant);
6925 PyTuple_SET_ITEM(newconst, i, constant);
6926 }
Inada Naoki8a232c72021-04-16 14:01:04 +09006927 if (merge_const_one(c, &newconst) == 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006928 Py_DECREF(newconst);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006929 return -1;
6930 }
Inada Naoki8a232c72021-04-16 14:01:04 +09006931
6932 Py_ssize_t index;
6933 for (index = 0; index < PyList_GET_SIZE(consts); index++) {
6934 if (PyList_GET_ITEM(consts, index) == newconst) {
6935 break;
6936 }
6937 }
6938 if (index == PyList_GET_SIZE(consts)) {
6939 if ((size_t)index >= (size_t)INT_MAX - 1) {
6940 Py_DECREF(newconst);
6941 PyErr_SetString(PyExc_OverflowError, "too many constants");
6942 return -1;
6943 }
6944 if (PyList_Append(consts, newconst)) {
6945 Py_DECREF(newconst);
6946 return -1;
6947 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006948 }
6949 Py_DECREF(newconst);
6950 for (int i = 0; i < n; i++) {
6951 inst[i].i_opcode = NOP;
6952 }
6953 inst[n].i_opcode = LOAD_CONST;
Victor Stinner71f2ff42020-09-23 14:06:55 +02006954 inst[n].i_oparg = (int)index;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006955 return 0;
6956}
6957
Mark Shannon28b75c82020-12-23 11:43:10 +00006958
6959static int
6960eliminate_jump_to_jump(basicblock *bb, int opcode) {
6961 assert (bb->b_iused > 0);
6962 struct instr *inst = &bb->b_instr[bb->b_iused-1];
6963 assert (is_jump(inst));
6964 assert (inst->i_target->b_iused > 0);
6965 struct instr *target = &inst->i_target->b_instr[0];
6966 if (inst->i_target == target->i_target) {
6967 /* Nothing to do */
6968 return 0;
6969 }
6970 int lineno = target->i_lineno;
6971 if (add_jump_to_block(bb, opcode, lineno, target->i_target) == 0) {
6972 return -1;
6973 }
6974 assert (bb->b_iused >= 2);
6975 bb->b_instr[bb->b_iused-2].i_opcode = NOP;
6976 return 0;
6977}
6978
Mark Shannoncc75ab72020-11-12 19:49:33 +00006979/* Maximum size of basic block that should be copied in optimizer */
6980#define MAX_COPY_SIZE 4
Mark Shannon6e8128f2020-07-30 10:03:00 +01006981
6982/* Optimization */
6983static int
Inada Naoki8a232c72021-04-16 14:01:04 +09006984optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
Mark Shannon6e8128f2020-07-30 10:03:00 +01006985{
6986 assert(PyList_CheckExact(consts));
6987 struct instr nop;
6988 nop.i_opcode = NOP;
6989 struct instr *target;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006990 for (int i = 0; i < bb->b_iused; i++) {
6991 struct instr *inst = &bb->b_instr[i];
6992 int oparg = inst->i_oparg;
6993 int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01006994 if (is_jump(inst)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006995 /* Skip over empty basic blocks. */
6996 while (inst->i_target->b_iused == 0) {
6997 inst->i_target = inst->i_target->b_next;
6998 }
6999 target = &inst->i_target->b_instr[0];
7000 }
7001 else {
7002 target = &nop;
7003 }
7004 switch (inst->i_opcode) {
Mark Shannon266b4622020-11-17 19:30:14 +00007005 /* Remove LOAD_CONST const; conditional jump */
Mark Shannon6e8128f2020-07-30 10:03:00 +01007006 case LOAD_CONST:
Mark Shannon266b4622020-11-17 19:30:14 +00007007 {
7008 PyObject* cnt;
7009 int is_true;
7010 int jump_if_true;
7011 switch(nextop) {
7012 case POP_JUMP_IF_FALSE:
7013 case POP_JUMP_IF_TRUE:
7014 cnt = PyList_GET_ITEM(consts, oparg);
7015 is_true = PyObject_IsTrue(cnt);
7016 if (is_true == -1) {
7017 goto error;
7018 }
7019 inst->i_opcode = NOP;
7020 jump_if_true = nextop == POP_JUMP_IF_TRUE;
7021 if (is_true == jump_if_true) {
7022 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
7023 bb->b_nofallthrough = 1;
7024 }
7025 else {
7026 bb->b_instr[i+1].i_opcode = NOP;
7027 }
7028 break;
7029 case JUMP_IF_FALSE_OR_POP:
7030 case JUMP_IF_TRUE_OR_POP:
7031 cnt = PyList_GET_ITEM(consts, oparg);
7032 is_true = PyObject_IsTrue(cnt);
7033 if (is_true == -1) {
7034 goto error;
7035 }
7036 jump_if_true = nextop == JUMP_IF_TRUE_OR_POP;
7037 if (is_true == jump_if_true) {
7038 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
7039 bb->b_nofallthrough = 1;
7040 }
7041 else {
7042 inst->i_opcode = NOP;
7043 bb->b_instr[i+1].i_opcode = NOP;
7044 }
7045 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007046 }
7047 break;
Mark Shannon266b4622020-11-17 19:30:14 +00007048 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007049
7050 /* Try to fold tuples of constants.
7051 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
7052 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
7053 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
7054 case BUILD_TUPLE:
7055 if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
7056 switch(oparg) {
7057 case 1:
7058 inst->i_opcode = NOP;
7059 bb->b_instr[i+1].i_opcode = NOP;
7060 break;
7061 case 2:
7062 inst->i_opcode = ROT_TWO;
7063 bb->b_instr[i+1].i_opcode = NOP;
7064 break;
7065 case 3:
7066 inst->i_opcode = ROT_THREE;
7067 bb->b_instr[i+1].i_opcode = ROT_TWO;
7068 }
7069 break;
7070 }
7071 if (i >= oparg) {
Inada Naoki8a232c72021-04-16 14:01:04 +09007072 if (fold_tuple_on_constants(c, inst-oparg, oparg, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007073 goto error;
7074 }
7075 }
7076 break;
7077
7078 /* Simplify conditional jump to conditional jump where the
7079 result of the first test implies the success of a similar
7080 test or the failure of the opposite test.
7081 Arises in code like:
7082 "a and b or c"
7083 "(a and b) and c"
7084 "(a or b) or c"
7085 "(a or b) and c"
7086 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
7087 --> x:JUMP_IF_FALSE_OR_POP z
7088 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
7089 --> x:POP_JUMP_IF_FALSE y+1
7090 where y+1 is the instruction following the second test.
7091 */
7092 case JUMP_IF_FALSE_OR_POP:
7093 switch(target->i_opcode) {
7094 case POP_JUMP_IF_FALSE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007095 if (inst->i_lineno == target->i_lineno) {
7096 *inst = *target;
7097 i--;
7098 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007099 break;
7100 case JUMP_ABSOLUTE:
7101 case JUMP_FORWARD:
7102 case JUMP_IF_FALSE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00007103 if (inst->i_lineno == target->i_lineno &&
7104 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00007105 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007106 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007107 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007108 break;
7109 case JUMP_IF_TRUE_OR_POP:
7110 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00007111 if (inst->i_lineno == target->i_lineno) {
7112 inst->i_opcode = POP_JUMP_IF_FALSE;
7113 inst->i_target = inst->i_target->b_next;
7114 --i;
7115 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007116 break;
7117 }
7118 break;
7119
7120 case JUMP_IF_TRUE_OR_POP:
7121 switch(target->i_opcode) {
7122 case POP_JUMP_IF_TRUE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007123 if (inst->i_lineno == target->i_lineno) {
7124 *inst = *target;
7125 i--;
7126 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007127 break;
7128 case JUMP_ABSOLUTE:
7129 case JUMP_FORWARD:
7130 case JUMP_IF_TRUE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00007131 if (inst->i_lineno == target->i_lineno &&
7132 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00007133 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007134 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007135 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007136 break;
7137 case JUMP_IF_FALSE_OR_POP:
7138 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00007139 if (inst->i_lineno == target->i_lineno) {
7140 inst->i_opcode = POP_JUMP_IF_TRUE;
7141 inst->i_target = inst->i_target->b_next;
7142 --i;
7143 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007144 break;
7145 }
7146 break;
7147
7148 case POP_JUMP_IF_FALSE:
7149 switch(target->i_opcode) {
7150 case JUMP_ABSOLUTE:
7151 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007152 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00007153 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007154 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007155 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007156 break;
7157 }
7158 break;
7159
7160 case POP_JUMP_IF_TRUE:
7161 switch(target->i_opcode) {
7162 case JUMP_ABSOLUTE:
7163 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007164 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00007165 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007166 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007167 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007168 break;
7169 }
7170 break;
7171
7172 case JUMP_ABSOLUTE:
7173 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007174 assert (i == bb->b_iused-1);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007175 switch(target->i_opcode) {
7176 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007177 if (eliminate_jump_to_jump(bb, inst->i_opcode)) {
7178 goto error;
Mark Shannon266b4622020-11-17 19:30:14 +00007179 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007180 break;
Mark Shannon28b75c82020-12-23 11:43:10 +00007181
Mark Shannon6e8128f2020-07-30 10:03:00 +01007182 case JUMP_ABSOLUTE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007183 if (eliminate_jump_to_jump(bb, JUMP_ABSOLUTE)) {
7184 goto error;
Mark Shannon266b4622020-11-17 19:30:14 +00007185 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007186 break;
Mark Shannon28b75c82020-12-23 11:43:10 +00007187 default:
7188 if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) {
7189 basicblock *to_copy = inst->i_target;
7190 inst->i_opcode = NOP;
7191 for (i = 0; i < to_copy->b_iused; i++) {
7192 int index = compiler_next_instr(bb);
7193 if (index < 0) {
7194 return -1;
7195 }
7196 bb->b_instr[index] = to_copy->b_instr[i];
7197 }
7198 bb->b_exit = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007199 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007200 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007201 }
7202 }
7203 return 0;
7204error:
7205 return -1;
7206}
7207
7208
7209static void
Mark Shannon1659ad12021-01-13 15:05:04 +00007210clean_basic_block(basicblock *bb, int prev_lineno) {
7211 /* Remove NOPs when legal to do so. */
Mark Shannon6e8128f2020-07-30 10:03:00 +01007212 int dest = 0;
7213 for (int src = 0; src < bb->b_iused; src++) {
Mark Shannon877df852020-11-12 09:43:29 +00007214 int lineno = bb->b_instr[src].i_lineno;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007215 if (bb->b_instr[src].i_opcode == NOP) {
Mark Shannon266b4622020-11-17 19:30:14 +00007216 /* Eliminate no-op if it doesn't have a line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007217 if (lineno < 0) {
7218 continue;
7219 }
Mark Shannon266b4622020-11-17 19:30:14 +00007220 /* or, if the previous instruction had the same line number. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007221 if (prev_lineno == lineno) {
7222 continue;
7223 }
Mark Shannon266b4622020-11-17 19:30:14 +00007224 /* or, if the next instruction has same line number or no line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007225 if (src < bb->b_iused - 1) {
7226 int next_lineno = bb->b_instr[src+1].i_lineno;
7227 if (next_lineno < 0 || next_lineno == lineno) {
7228 bb->b_instr[src+1].i_lineno = lineno;
7229 continue;
Mark Shannon877df852020-11-12 09:43:29 +00007230 }
7231 }
Mark Shannon266b4622020-11-17 19:30:14 +00007232 else {
7233 basicblock* next = bb->b_next;
7234 while (next && next->b_iused == 0) {
7235 next = next->b_next;
7236 }
7237 /* or if last instruction in BB and next BB has same line number */
7238 if (next) {
7239 if (lineno == next->b_instr[0].i_lineno) {
7240 continue;
7241 }
7242 }
7243 }
7244
Mark Shannon6e8128f2020-07-30 10:03:00 +01007245 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007246 if (dest != src) {
7247 bb->b_instr[dest] = bb->b_instr[src];
7248 }
7249 dest++;
7250 prev_lineno = lineno;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007251 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007252 assert(dest <= bb->b_iused);
7253 bb->b_iused = dest;
7254}
7255
Mark Shannon266b4622020-11-17 19:30:14 +00007256static int
7257normalize_basic_block(basicblock *bb) {
7258 /* Mark blocks as exit and/or nofallthrough.
7259 Raise SystemError if CFG is malformed. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007260 for (int i = 0; i < bb->b_iused; i++) {
7261 switch(bb->b_instr[i].i_opcode) {
7262 case RETURN_VALUE:
7263 case RAISE_VARARGS:
7264 case RERAISE:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007265 bb->b_exit = 1;
Mark Shannon5977a792020-12-02 13:31:40 +00007266 bb->b_nofallthrough = 1;
7267 break;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007268 case JUMP_ABSOLUTE:
7269 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007270 bb->b_nofallthrough = 1;
Mark Shannon266b4622020-11-17 19:30:14 +00007271 /* fall through */
7272 case POP_JUMP_IF_FALSE:
7273 case POP_JUMP_IF_TRUE:
7274 case JUMP_IF_FALSE_OR_POP:
7275 case JUMP_IF_TRUE_OR_POP:
Mark Shannon5977a792020-12-02 13:31:40 +00007276 case FOR_ITER:
Mark Shannon266b4622020-11-17 19:30:14 +00007277 if (i != bb->b_iused-1) {
7278 PyErr_SetString(PyExc_SystemError, "malformed control flow graph.");
7279 return -1;
7280 }
Mark Shannon5977a792020-12-02 13:31:40 +00007281 /* Skip over empty basic blocks. */
7282 while (bb->b_instr[i].i_target->b_iused == 0) {
7283 bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next;
7284 }
7285
Mark Shannoncc75ab72020-11-12 19:49:33 +00007286 }
7287 }
Mark Shannon266b4622020-11-17 19:30:14 +00007288 return 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007289}
7290
Mark Shannon6e8128f2020-07-30 10:03:00 +01007291static int
7292mark_reachable(struct assembler *a) {
7293 basicblock **stack, **sp;
7294 sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks);
7295 if (stack == NULL) {
7296 return -1;
7297 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007298 a->a_entry->b_predecessors = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007299 *sp++ = a->a_entry;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007300 while (sp > stack) {
7301 basicblock *b = *(--sp);
Mark Shannon3bd60352021-01-13 12:05:43 +00007302 if (b->b_next && !b->b_nofallthrough) {
7303 if (b->b_next->b_predecessors == 0) {
7304 *sp++ = b->b_next;
7305 }
7306 b->b_next->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007307 }
7308 for (int i = 0; i < b->b_iused; i++) {
7309 basicblock *target;
Mark Shannon582aaf12020-08-04 17:30:11 +01007310 if (is_jump(&b->b_instr[i])) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007311 target = b->b_instr[i].i_target;
Mark Shannon3bd60352021-01-13 12:05:43 +00007312 if (target->b_predecessors == 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007313 *sp++ = target;
7314 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007315 target->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007316 }
7317 }
7318 }
7319 PyObject_Free(stack);
7320 return 0;
7321}
7322
Mark Shannon3bd60352021-01-13 12:05:43 +00007323static void
7324eliminate_empty_basic_blocks(basicblock *entry) {
7325 /* Eliminate empty blocks */
7326 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7327 basicblock *next = b->b_next;
7328 if (next) {
7329 while (next->b_iused == 0 && next->b_next) {
7330 next = next->b_next;
7331 }
7332 b->b_next = next;
7333 }
7334 }
7335 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7336 if (b->b_iused == 0) {
7337 continue;
7338 }
7339 if (is_jump(&b->b_instr[b->b_iused-1])) {
7340 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7341 while (target->b_iused == 0) {
7342 target = target->b_next;
7343 }
7344 b->b_instr[b->b_iused-1].i_target = target;
7345 }
7346 }
7347}
7348
7349
Mark Shannon5977a792020-12-02 13:31:40 +00007350/* If an instruction has no line number, but it's predecessor in the BB does,
Mark Shannon3bd60352021-01-13 12:05:43 +00007351 * then copy the line number. If a successor block has no line number, and only
7352 * one predecessor, then inherit the line number.
7353 * This ensures that all exit blocks (with one predecessor) receive a line number.
7354 * Also reduces the size of the line number table,
Mark Shannon5977a792020-12-02 13:31:40 +00007355 * but has no impact on the generated line number events.
7356 */
7357static void
Mark Shannon3bd60352021-01-13 12:05:43 +00007358propogate_line_numbers(struct assembler *a) {
Mark Shannon5977a792020-12-02 13:31:40 +00007359 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007360 if (b->b_iused == 0) {
7361 continue;
7362 }
Mark Shannon5977a792020-12-02 13:31:40 +00007363 int prev_lineno = -1;
7364 for (int i = 0; i < b->b_iused; i++) {
7365 if (b->b_instr[i].i_lineno < 0) {
7366 b->b_instr[i].i_lineno = prev_lineno;
7367 }
7368 else {
7369 prev_lineno = b->b_instr[i].i_lineno;
7370 }
7371 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007372 if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) {
7373 assert(b->b_next->b_iused);
7374 if (b->b_next->b_instr[0].i_lineno < 0) {
7375 b->b_next->b_instr[0].i_lineno = prev_lineno;
7376 }
7377 }
7378 if (is_jump(&b->b_instr[b->b_iused-1])) {
7379 switch (b->b_instr[b->b_iused-1].i_opcode) {
7380 /* Note: Only actual jumps, not exception handlers */
7381 case SETUP_ASYNC_WITH:
7382 case SETUP_WITH:
7383 case SETUP_FINALLY:
7384 continue;
7385 }
7386 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7387 if (target->b_predecessors == 1) {
7388 if (target->b_instr[0].i_lineno < 0) {
7389 target->b_instr[0].i_lineno = prev_lineno;
7390 }
7391 }
7392 }
Mark Shannon5977a792020-12-02 13:31:40 +00007393 }
7394}
7395
7396/* Perform optimizations on a control flow graph.
Mark Shannon6e8128f2020-07-30 10:03:00 +01007397 The consts object should still be in list form to allow new constants
7398 to be appended.
7399
7400 All transformations keep the code size the same or smaller.
7401 For those that reduce size, the gaps are initially filled with
7402 NOPs. Later those NOPs are removed.
7403*/
7404
7405static int
Inada Naoki8a232c72021-04-16 14:01:04 +09007406optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts)
Mark Shannon6e8128f2020-07-30 10:03:00 +01007407{
Mark Shannoncc75ab72020-11-12 19:49:33 +00007408 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Inada Naoki8a232c72021-04-16 14:01:04 +09007409 if (optimize_basic_block(c, b, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007410 return -1;
7411 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007412 clean_basic_block(b, -1);
Mark Shannon3bd60352021-01-13 12:05:43 +00007413 assert(b->b_predecessors == 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007414 }
7415 if (mark_reachable(a)) {
7416 return -1;
7417 }
7418 /* Delete unreachable instructions */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007419 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007420 if (b->b_predecessors == 0) {
Mark Shannoncc75ab72020-11-12 19:49:33 +00007421 b->b_iused = 0;
Om Gc71581c2020-12-16 17:48:05 +05307422 b->b_nofallthrough = 0;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007423 }
7424 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007425 basicblock *pred = NULL;
7426 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7427 int prev_lineno = -1;
7428 if (pred && pred->b_iused) {
7429 prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno;
7430 }
7431 clean_basic_block(b, prev_lineno);
7432 pred = b->b_nofallthrough ? NULL : b;
7433 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007434 eliminate_empty_basic_blocks(a->a_entry);
Om Gc71581c2020-12-16 17:48:05 +05307435 /* Delete jump instructions made redundant by previous step. If a non-empty
7436 block ends with a jump instruction, check if the next non-empty block
7437 reached through normal flow control is the target of that jump. If it
7438 is, then the jump instruction is redundant and can be deleted.
7439 */
Mark Shannon3bd60352021-01-13 12:05:43 +00007440 int maybe_empty_blocks = 0;
Om Gc71581c2020-12-16 17:48:05 +05307441 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7442 if (b->b_iused > 0) {
7443 struct instr *b_last_instr = &b->b_instr[b->b_iused - 1];
Mark Shannon802b6452021-02-02 14:59:15 +00007444 if (b_last_instr->i_opcode == JUMP_ABSOLUTE ||
Om Gc71581c2020-12-16 17:48:05 +05307445 b_last_instr->i_opcode == JUMP_FORWARD) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007446 if (b_last_instr->i_target == b->b_next) {
7447 assert(b->b_next->b_iused);
Om Gc71581c2020-12-16 17:48:05 +05307448 b->b_nofallthrough = 0;
Mark Shannon802b6452021-02-02 14:59:15 +00007449 b_last_instr->i_opcode = NOP;
7450 clean_basic_block(b, -1);
7451 maybe_empty_blocks = 1;
Om Gc71581c2020-12-16 17:48:05 +05307452 }
7453 }
7454 }
7455 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007456 if (maybe_empty_blocks) {
7457 eliminate_empty_basic_blocks(a->a_entry);
7458 }
7459 propogate_line_numbers(a);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007460 return 0;
7461}
7462
Mark Shannon5977a792020-12-02 13:31:40 +00007463static inline int
7464is_exit_without_lineno(basicblock *b) {
7465 return b->b_exit && b->b_instr[0].i_lineno < 0;
7466}
7467
7468/* PEP 626 mandates that the f_lineno of a frame is correct
7469 * after a frame terminates. It would be prohibitively expensive
7470 * to continuously update the f_lineno field at runtime,
7471 * so we make sure that all exiting instruction (raises and returns)
7472 * have a valid line number, allowing us to compute f_lineno lazily.
7473 * We can do this by duplicating the exit blocks without line number
7474 * so that none have more than one predecessor. We can then safely
7475 * copy the line number from the sole predecessor block.
7476 */
7477static int
7478ensure_exits_have_lineno(struct compiler *c)
7479{
Mark Shannoneaccc122020-12-04 15:22:12 +00007480 basicblock *entry = NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00007481 /* Copy all exit blocks without line number that are targets of a jump.
7482 */
7483 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7484 if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) {
7485 switch (b->b_instr[b->b_iused-1].i_opcode) {
7486 /* Note: Only actual jumps, not exception handlers */
7487 case SETUP_ASYNC_WITH:
7488 case SETUP_WITH:
7489 case SETUP_FINALLY:
7490 continue;
7491 }
7492 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7493 if (is_exit_without_lineno(target)) {
7494 basicblock *new_target = compiler_copy_block(c, target);
7495 if (new_target == NULL) {
7496 return -1;
7497 }
7498 new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7499 b->b_instr[b->b_iused-1].i_target = new_target;
7500 }
7501 }
Mark Shannoneaccc122020-12-04 15:22:12 +00007502 entry = b;
7503 }
7504 assert(entry != NULL);
7505 if (is_exit_without_lineno(entry)) {
7506 entry->b_instr[0].i_lineno = c->u->u_firstlineno;
Mark Shannon5977a792020-12-02 13:31:40 +00007507 }
Mark Shannonee9f98d2021-01-05 12:04:10 +00007508 /* Eliminate empty blocks */
7509 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7510 while (b->b_next && b->b_next->b_iused == 0) {
7511 b->b_next = b->b_next->b_next;
7512 }
7513 }
Mark Shannon5977a792020-12-02 13:31:40 +00007514 /* Any remaining reachable exit blocks without line number can only be reached by
7515 * fall through, and thus can only have a single predecessor */
7516 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7517 if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) {
7518 if (is_exit_without_lineno(b->b_next)) {
7519 assert(b->b_next->b_iused > 0);
7520 b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7521 }
7522 }
7523 }
7524 return 0;
7525}
7526
7527
Mark Shannon6e8128f2020-07-30 10:03:00 +01007528/* Retained for API compatibility.
7529 * Optimization is now done in optimize_cfg */
7530
7531PyObject *
7532PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
7533 PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
7534{
7535 Py_INCREF(code);
7536 return code;
7537}