blob: 27274ec884dc3fcc89a03f569210843f95eb4ae2 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file compiles an abstract syntax tree (AST) into Python bytecode.
3 *
4 * The primary entry point is PyAST_Compile(), which returns a
5 * 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 Stinnerba7a99d2021-01-30 01:46:44 +010026#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
Victor Stinnerc9bc2902020-10-27 02:24:34 +010027#include "pycore_long.h" // _PyLong_GetZero()
Victor Stinner28ad12f2021-03-19 12:41:49 +010028#include "pycore_symtable.h" // PySTEntryObject
Guido van Rossum3f5da241990-12-20 15:06:42 +000029
Mark Shannon582aaf12020-08-04 17:30:11 +010030#define NEED_OPCODE_JUMP_TABLES
Victor Stinner526fdeb2021-03-17 23:50:50 +010031#include "opcode.h" // EXTENDED_ARG
32#include "wordcode_helpers.h" // instrsize()
33
Guido van Rossumb05a5c71997-05-07 17:46:13 +000034
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000035#define DEFAULT_BLOCK_SIZE 16
36#define DEFAULT_BLOCKS 8
37#define DEFAULT_CODE_SIZE 128
38#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000039
Nick Coghlan650f0d02007-04-15 12:05:43 +000040#define COMP_GENEXP 0
41#define COMP_LISTCOMP 1
42#define COMP_SETCOMP 2
Guido van Rossum992d4a32007-07-11 13:09:30 +000043#define COMP_DICTCOMP 3
Nick Coghlan650f0d02007-04-15 12:05:43 +000044
Pablo Galindo90235812020-03-15 04:29:22 +000045#define IS_TOP_LEVEL_AWAIT(c) ( \
46 (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \
47 && (c->u->u_ste->ste_type == ModuleBlock))
48
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000049struct instr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 unsigned char i_opcode;
51 int i_oparg;
52 struct basicblock_ *i_target; /* target block (if jump instruction) */
53 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000054};
55
Mark Shannon582aaf12020-08-04 17:30:11 +010056#define LOG_BITS_PER_INT 5
57#define MASK_LOW_LOG_BITS 31
58
59static inline int
60is_bit_set_in_table(uint32_t *table, int bitindex) {
61 /* Is the relevant bit set in the relevant word? */
62 /* 256 bits fit into 8 32-bits words.
63 * Word is indexed by (bitindex>>ln(size of int in bits)).
64 * Bit within word is the low bits of bitindex.
65 */
66 uint32_t word = table[bitindex >> LOG_BITS_PER_INT];
67 return (word >> (bitindex & MASK_LOW_LOG_BITS)) & 1;
68}
69
70static inline int
71is_relative_jump(struct instr *i)
72{
73 return is_bit_set_in_table(_PyOpcode_RelativeJump, i->i_opcode);
74}
75
76static inline int
77is_jump(struct instr *i)
78{
79 return is_bit_set_in_table(_PyOpcode_Jump, i->i_opcode);
80}
81
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000082typedef struct basicblock_ {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000083 /* Each basicblock in a compilation unit is linked via b_list in the
84 reverse order that the block are allocated. b_list points to the next
85 block, not to be confused with b_next, which is next by control flow. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 struct basicblock_ *b_list;
87 /* number of instructions used */
88 int b_iused;
89 /* length of instruction array (b_instr) */
90 int b_ialloc;
91 /* pointer to an array of instructions, initially NULL */
92 struct instr *b_instr;
93 /* If b_next is non-NULL, it is a pointer to the next
94 block reached by normal control flow. */
95 struct basicblock_ *b_next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 /* b_return is true if a RETURN_VALUE opcode is inserted. */
97 unsigned b_return : 1;
Mark Shannon3bd60352021-01-13 12:05:43 +000098 /* Number of predecssors that a block has. */
99 int b_predecessors;
Mark Shannoncc75ab72020-11-12 19:49:33 +0000100 /* Basic block has no fall through (it ends with a return, raise or jump) */
101 unsigned b_nofallthrough : 1;
102 /* Basic block exits scope (it ends with a return or raise) */
103 unsigned b_exit : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 /* depth of stack upon entry of block, computed by stackdepth() */
105 int b_startdepth;
106 /* instruction offset for block, computed by assemble_jump_offsets() */
107 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000108} basicblock;
109
110/* fblockinfo tracks the current frame block.
111
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000112A frame block is used to handle loops, try/except, and try/finally.
113It's called a frame block to distinguish it from a basic block in the
114compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000115*/
116
Mark Shannon02d126a2020-09-25 14:04:19 +0100117enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END,
118 WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER };
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000119
120struct fblockinfo {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 enum fblocktype fb_type;
122 basicblock *fb_block;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200123 /* (optional) type-specific exit or cleanup block */
124 basicblock *fb_exit;
Mark Shannonfee55262019-11-21 09:11:43 +0000125 /* (optional) additional information required for unwinding */
126 void *fb_datum;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000127};
128
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100129enum {
130 COMPILER_SCOPE_MODULE,
131 COMPILER_SCOPE_CLASS,
132 COMPILER_SCOPE_FUNCTION,
Yury Selivanov75445082015-05-11 22:57:16 -0400133 COMPILER_SCOPE_ASYNC_FUNCTION,
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400134 COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100135 COMPILER_SCOPE_COMPREHENSION,
136};
137
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000138/* The following items change on entry and exit of code blocks.
139 They must be saved and restored when returning to a block.
140*/
141struct compiler_unit {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 PySTEntryObject *u_ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 PyObject *u_name;
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400145 PyObject *u_qualname; /* dot-separated qualified name (lazy) */
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100146 int u_scope_type;
147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 /* The following fields are dicts that map objects to
149 the index of them in co_XXX. The index is used as
150 the argument for opcodes that refer to those collections.
151 */
152 PyObject *u_consts; /* all constants */
153 PyObject *u_names; /* all names */
154 PyObject *u_varnames; /* local variables */
155 PyObject *u_cellvars; /* cell variables */
156 PyObject *u_freevars; /* free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 PyObject *u_private; /* for private name mangling */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000159
Victor Stinnerf8e32212013-11-19 23:56:34 +0100160 Py_ssize_t u_argcount; /* number of arguments for block */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100161 Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */
Victor Stinnerf8e32212013-11-19 23:56:34 +0100162 Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 /* Pointer to the most recently allocated block. By following b_list
164 members, you can reach all early allocated blocks. */
165 basicblock *u_blocks;
166 basicblock *u_curblock; /* pointer to current block */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 int u_nfblocks;
169 struct fblockinfo u_fblock[CO_MAXBLOCKS];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 int u_firstlineno; /* the first lineno of the block */
172 int u_lineno; /* the lineno for the current stmt */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000173 int u_col_offset; /* the offset of the current stmt */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000174};
175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176/* This struct captures the global state of a compilation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000177
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000178The u pointer points to the current compilation unit, while units
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179for enclosing blocks are stored in c_stack. The u and c_stack are
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000180managed by compiler_enter_scope() and compiler_exit_scope().
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000181
182Note that we don't track recursion levels during compilation - the
183task of detecting and rejecting excessive levels of nesting is
184handled by the symbol analysis pass.
185
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000186*/
187
188struct compiler {
Victor Stinner14e461d2013-08-26 22:28:21 +0200189 PyObject *c_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 struct symtable *c_st;
191 PyFutureFeatures *c_future; /* pointer to module's __future__ */
192 PyCompilerFlags *c_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193
Georg Brandl8334fd92010-12-04 10:26:46 +0000194 int c_optimize; /* optimization level */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 int c_interactive; /* true if in interactive mode */
196 int c_nestlevel;
INADA Naokic2e16072018-11-26 21:23:22 +0900197 PyObject *c_const_cache; /* Python dict holding all constants,
198 including names tuple */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 struct compiler_unit *u; /* compiler state for current block */
200 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
201 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000202};
203
Brandt Bucher145bf262021-02-26 14:51:55 -0800204typedef struct {
205 PyObject *stores;
206 int allow_irrefutable;
207} pattern_context;
208
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100209static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210static void compiler_free(struct compiler *);
211static basicblock *compiler_new_block(struct compiler *);
Andy Lester76d58772020-03-10 21:18:12 -0500212static int compiler_next_instr(basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000213static int compiler_addop(struct compiler *, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +0100214static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
Mark Shannon582aaf12020-08-04 17:30:11 +0100215static int compiler_addop_j(struct compiler *, int, basicblock *);
Mark Shannon127dde52021-01-04 18:06:55 +0000216static int compiler_addop_j_noline(struct compiler *, int, basicblock *);
Brandt Bucher145bf262021-02-26 14:51:55 -0800217static int compiler_error(struct compiler *, const char *, ...);
Serhiy Storchaka62e44812019-02-16 08:12:19 +0200218static int compiler_warn(struct compiler *, const char *, ...);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000219static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
220
221static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
222static int compiler_visit_stmt(struct compiler *, stmt_ty);
223static int compiler_visit_keyword(struct compiler *, keyword_ty);
224static int compiler_visit_expr(struct compiler *, expr_ty);
225static int compiler_augassign(struct compiler *, stmt_ty);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700226static int compiler_annassign(struct compiler *, stmt_ty);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +0200227static int compiler_subscript(struct compiler *, expr_ty);
228static int compiler_slice(struct compiler *, expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000229
Andy Lester76d58772020-03-10 21:18:12 -0500230static int inplace_binop(operator_ty);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100231static int are_all_items_const(asdl_expr_seq *, Py_ssize_t, Py_ssize_t);
Mark Shannon8473cf82020-12-15 11:07:50 +0000232
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000233
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500234static int compiler_with(struct compiler *, stmt_ty, int);
Yury Selivanov75445082015-05-11 22:57:16 -0400235static int compiler_async_with(struct compiler *, stmt_ty, int);
236static int compiler_async_for(struct compiler *, stmt_ty);
Victor Stinner976bb402016-03-23 11:36:19 +0100237static int compiler_call_helper(struct compiler *c, int n,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100238 asdl_expr_seq *args,
239 asdl_keyword_seq *keywords);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500240static int compiler_try_except(struct compiler *, stmt_ty);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400241static int compiler_set_qualname(struct compiler *);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000242
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700243static int compiler_sync_comprehension_generator(
244 struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100245 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +0200246 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700247 expr_ty elt, expr_ty val, int type);
248
249static int compiler_async_comprehension_generator(
250 struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100251 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +0200252 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700253 expr_ty elt, expr_ty val, int type);
254
Brandt Bucher145bf262021-02-26 14:51:55 -0800255static int compiler_pattern(struct compiler *, expr_ty, pattern_context *);
256static int compiler_match(struct compiler *, stmt_ty);
257static int compiler_pattern_subpattern(struct compiler *, expr_ty,
258 pattern_context *);
259
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000260static PyCodeObject *assemble(struct compiler *, int addNone);
Mark Shannon332cd5e2018-01-30 00:41:04 +0000261static PyObject *__doc__, *__annotations__;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000262
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400263#define CAPSULE_NAME "compile.c compiler unit"
Benjamin Petersonb173f782009-05-05 22:31:58 +0000264
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000265PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000266_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 /* Name mangling: __private becomes _classname__private.
269 This is independent from how the name is used. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200270 PyObject *result;
271 size_t nlen, plen, ipriv;
272 Py_UCS4 maxchar;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200274 PyUnicode_READ_CHAR(ident, 0) != '_' ||
275 PyUnicode_READ_CHAR(ident, 1) != '_') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 Py_INCREF(ident);
277 return ident;
278 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200279 nlen = PyUnicode_GET_LENGTH(ident);
280 plen = PyUnicode_GET_LENGTH(privateobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 /* Don't mangle __id__ or names with dots.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 The only time a name with a dot can occur is when
284 we are compiling an import statement that has a
285 package name.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 TODO(jhylton): Decide whether we want to support
288 mangling of the module name, e.g. __M.X.
289 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200290 if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
291 PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
292 PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 Py_INCREF(ident);
294 return ident; /* Don't mangle __whatever__ */
295 }
296 /* Strip leading underscores from class name */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200297 ipriv = 0;
298 while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
299 ipriv++;
300 if (ipriv == plen) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 Py_INCREF(ident);
302 return ident; /* Don't mangle if class is just underscores */
303 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200304 plen -= ipriv;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000305
Antoine Pitrou55bff892013-04-06 21:21:04 +0200306 if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
307 PyErr_SetString(PyExc_OverflowError,
308 "private identifier too large to be mangled");
309 return NULL;
310 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000311
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200312 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
313 if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
314 maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
315
316 result = PyUnicode_New(1 + nlen + plen, maxchar);
317 if (!result)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200319 /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
320 PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
Victor Stinner6c7a52a2011-09-28 21:39:17 +0200321 if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
322 Py_DECREF(result);
323 return NULL;
324 }
325 if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
326 Py_DECREF(result);
327 return NULL;
328 }
Victor Stinner8f825062012-04-27 13:55:39 +0200329 assert(_PyUnicode_CheckConsistency(result, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200330 return result;
Michael W. Hudson60934622004-08-12 17:56:29 +0000331}
332
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000333static int
334compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000337
INADA Naokic2e16072018-11-26 21:23:22 +0900338 c->c_const_cache = PyDict_New();
339 if (!c->c_const_cache) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 return 0;
INADA Naokic2e16072018-11-26 21:23:22 +0900341 }
342
343 c->c_stack = PyList_New(0);
344 if (!c->c_stack) {
345 Py_CLEAR(c->c_const_cache);
346 return 0;
347 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000350}
351
352PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200353PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
354 int optimize, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 struct compiler c;
357 PyCodeObject *co = NULL;
Victor Stinner37d66d72019-06-13 02:16:41 +0200358 PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 if (!__doc__) {
362 __doc__ = PyUnicode_InternFromString("__doc__");
363 if (!__doc__)
364 return NULL;
365 }
Mark Shannon332cd5e2018-01-30 00:41:04 +0000366 if (!__annotations__) {
367 __annotations__ = PyUnicode_InternFromString("__annotations__");
368 if (!__annotations__)
369 return NULL;
370 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 if (!compiler_init(&c))
372 return NULL;
Victor Stinner14e461d2013-08-26 22:28:21 +0200373 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 c.c_filename = filename;
375 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200376 c.c_future = PyFuture_FromASTObject(mod, filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 if (c.c_future == NULL)
378 goto finally;
379 if (!flags) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 flags = &local_flags;
381 }
382 merged = c.c_future->ff_features | flags->cf_flags;
383 c.c_future->ff_features = merged;
384 flags->cf_flags = merged;
385 c.c_flags = flags;
Victor Stinnerda7933e2020-04-13 03:04:28 +0200386 c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000388
Pablo Galindod112c602020-03-18 23:02:09 +0000389 _PyASTOptimizeState state;
390 state.optimize = c.c_optimize;
391 state.ff_features = merged;
392
393 if (!_PyAST_Optimize(mod, arena, &state)) {
INADA Naoki7ea143a2017-12-14 16:47:20 +0900394 goto finally;
395 }
396
Victor Stinner28ad12f2021-03-19 12:41:49 +0100397 c.c_st = _PySymtable_Build(mod, filename, c.c_future);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 if (c.c_st == NULL) {
399 if (!PyErr_Occurred())
400 PyErr_SetString(PyExc_SystemError, "no symtable");
401 goto finally;
402 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000405
Thomas Wouters1175c432006-02-27 22:49:54 +0000406 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 compiler_free(&c);
408 assert(co || PyErr_Occurred());
409 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000410}
411
412PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200413PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags,
414 int optimize, PyArena *arena)
415{
416 PyObject *filename;
417 PyCodeObject *co;
418 filename = PyUnicode_DecodeFSDefault(filename_str);
419 if (filename == NULL)
420 return NULL;
421 co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
422 Py_DECREF(filename);
423 return co;
424
425}
426
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000427static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000428compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000429{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 if (c->c_st)
Victor Stinner28ad12f2021-03-19 12:41:49 +0100431 _PySymtable_Free(c->c_st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 if (c->c_future)
433 PyObject_Free(c->c_future);
Victor Stinner14e461d2013-08-26 22:28:21 +0200434 Py_XDECREF(c->c_filename);
INADA Naokic2e16072018-11-26 21:23:22 +0900435 Py_DECREF(c->c_const_cache);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000437}
438
Guido van Rossum79f25d91997-04-29 20:08:16 +0000439static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000440list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 Py_ssize_t i, n;
443 PyObject *v, *k;
444 PyObject *dict = PyDict_New();
445 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 n = PyList_Size(list);
448 for (i = 0; i < n; i++) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100449 v = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 if (!v) {
451 Py_DECREF(dict);
452 return NULL;
453 }
454 k = PyList_GET_ITEM(list, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300455 if (PyDict_SetItem(dict, k, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 Py_DECREF(v);
457 Py_DECREF(dict);
458 return NULL;
459 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 Py_DECREF(v);
461 }
462 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000463}
464
465/* Return new dict containing names from src that match scope(s).
466
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000467src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000469values are integers, starting at offset and increasing by one for
470each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000471*/
472
473static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +0100474dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000475{
Benjamin Peterson51ab2832012-07-18 15:12:47 -0700476 Py_ssize_t i = offset, scope, num_keys, key_i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 PyObject *k, *v, *dest = PyDict_New();
Meador Inge2ca63152012-07-18 14:20:11 -0500478 PyObject *sorted_keys;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 assert(offset >= 0);
481 if (dest == NULL)
482 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000483
Meador Inge2ca63152012-07-18 14:20:11 -0500484 /* Sort the keys so that we have a deterministic order on the indexes
485 saved in the returned dictionary. These indexes are used as indexes
486 into the free and cell var storage. Therefore if they aren't
487 deterministic, then the generated bytecode is not deterministic.
488 */
489 sorted_keys = PyDict_Keys(src);
490 if (sorted_keys == NULL)
491 return NULL;
492 if (PyList_Sort(sorted_keys) != 0) {
493 Py_DECREF(sorted_keys);
494 return NULL;
495 }
Meador Ingef69e24e2012-07-18 16:41:03 -0500496 num_keys = PyList_GET_SIZE(sorted_keys);
Meador Inge2ca63152012-07-18 14:20:11 -0500497
498 for (key_i = 0; key_i < num_keys; key_i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 /* XXX this should probably be a macro in symtable.h */
500 long vi;
Meador Inge2ca63152012-07-18 14:20:11 -0500501 k = PyList_GET_ITEM(sorted_keys, key_i);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200502 v = PyDict_GetItemWithError(src, k);
503 assert(v && PyLong_Check(v));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 vi = PyLong_AS_LONG(v);
505 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 if (scope == scope_type || vi & flag) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300508 PyObject *item = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 if (item == NULL) {
Meador Inge2ca63152012-07-18 14:20:11 -0500510 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 Py_DECREF(dest);
512 return NULL;
513 }
514 i++;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300515 if (PyDict_SetItem(dest, k, item) < 0) {
Meador Inge2ca63152012-07-18 14:20:11 -0500516 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 Py_DECREF(item);
518 Py_DECREF(dest);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 return NULL;
520 }
521 Py_DECREF(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 }
523 }
Meador Inge2ca63152012-07-18 14:20:11 -0500524 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000526}
527
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528static void
529compiler_unit_check(struct compiler_unit *u)
530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 basicblock *block;
532 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +0100533 assert(!_PyMem_IsPtrFreed(block));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 if (block->b_instr != NULL) {
535 assert(block->b_ialloc > 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +0100536 assert(block->b_iused >= 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 assert(block->b_ialloc >= block->b_iused);
538 }
539 else {
540 assert (block->b_iused == 0);
541 assert (block->b_ialloc == 0);
542 }
543 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000544}
545
546static void
547compiler_unit_free(struct compiler_unit *u)
548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 compiler_unit_check(u);
552 b = u->u_blocks;
553 while (b != NULL) {
554 if (b->b_instr)
555 PyObject_Free((void *)b->b_instr);
556 next = b->b_list;
557 PyObject_Free((void *)b);
558 b = next;
559 }
560 Py_CLEAR(u->u_ste);
561 Py_CLEAR(u->u_name);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400562 Py_CLEAR(u->u_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 Py_CLEAR(u->u_consts);
564 Py_CLEAR(u->u_names);
565 Py_CLEAR(u->u_varnames);
566 Py_CLEAR(u->u_freevars);
567 Py_CLEAR(u->u_cellvars);
568 Py_CLEAR(u->u_private);
569 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570}
571
572static int
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100573compiler_enter_scope(struct compiler *c, identifier name,
574 int scope_type, void *key, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 struct compiler_unit *u;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100577 basicblock *block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000578
Andy Lester7668a8b2020-03-24 23:26:44 -0500579 u = (struct compiler_unit *)PyObject_Calloc(1, sizeof(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 struct compiler_unit));
581 if (!u) {
582 PyErr_NoMemory();
583 return 0;
584 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100585 u->u_scope_type = scope_type;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 u->u_argcount = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100587 u->u_posonlyargcount = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 u->u_kwonlyargcount = 0;
589 u->u_ste = PySymtable_Lookup(c->c_st, key);
590 if (!u->u_ste) {
591 compiler_unit_free(u);
592 return 0;
593 }
594 Py_INCREF(name);
595 u->u_name = name;
596 u->u_varnames = list2dict(u->u_ste->ste_varnames);
597 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
598 if (!u->u_varnames || !u->u_cellvars) {
599 compiler_unit_free(u);
600 return 0;
601 }
Benjamin Peterson312595c2013-05-15 15:26:42 -0500602 if (u->u_ste->ste_needs_class_closure) {
Martin Panter7462b6492015-11-02 03:37:02 +0000603 /* Cook up an implicit __class__ cell. */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500604 _Py_IDENTIFIER(__class__);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300605 PyObject *name;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500606 int res;
607 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200608 assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500609 name = _PyUnicode_FromId(&PyId___class__);
610 if (!name) {
611 compiler_unit_free(u);
612 return 0;
613 }
Victor Stinnerc9bc2902020-10-27 02:24:34 +0100614 res = PyDict_SetItem(u->u_cellvars, name, _PyLong_GetZero());
Benjamin Peterson312595c2013-05-15 15:26:42 -0500615 if (res < 0) {
616 compiler_unit_free(u);
617 return 0;
618 }
619 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200622 PyDict_GET_SIZE(u->u_cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 if (!u->u_freevars) {
624 compiler_unit_free(u);
625 return 0;
626 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 u->u_blocks = NULL;
629 u->u_nfblocks = 0;
630 u->u_firstlineno = lineno;
631 u->u_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000632 u->u_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 u->u_consts = PyDict_New();
634 if (!u->u_consts) {
635 compiler_unit_free(u);
636 return 0;
637 }
638 u->u_names = PyDict_New();
639 if (!u->u_names) {
640 compiler_unit_free(u);
641 return 0;
642 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 /* Push the old compiler_unit on the stack. */
647 if (c->u) {
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400648 PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
650 Py_XDECREF(capsule);
651 compiler_unit_free(u);
652 return 0;
653 }
654 Py_DECREF(capsule);
655 u->u_private = c->u->u_private;
656 Py_XINCREF(u->u_private);
657 }
658 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 c->c_nestlevel++;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100661
662 block = compiler_new_block(c);
663 if (block == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 return 0;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100665 c->u->u_curblock = block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000666
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400667 if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
668 if (!compiler_set_qualname(c))
669 return 0;
670 }
671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000673}
674
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000675static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000676compiler_exit_scope(struct compiler *c)
677{
Victor Stinnera6192632021-01-29 16:53:03 +0100678 // Don't call PySequence_DelItem() with an exception raised
679 PyObject *exc_type, *exc_val, *exc_tb;
680 PyErr_Fetch(&exc_type, &exc_val, &exc_tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 c->c_nestlevel--;
683 compiler_unit_free(c->u);
684 /* Restore c->u to the parent unit. */
Victor Stinnera6192632021-01-29 16:53:03 +0100685 Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 if (n >= 0) {
Victor Stinnera6192632021-01-29 16:53:03 +0100687 PyObject *capsule = PyList_GET_ITEM(c->c_stack, n);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400688 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 assert(c->u);
690 /* we are deleting from a list so this really shouldn't fail */
Victor Stinnera6192632021-01-29 16:53:03 +0100691 if (PySequence_DelItem(c->c_stack, n) < 0) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +0100692 _PyErr_WriteUnraisableMsg("on removing the last compiler "
693 "stack item", NULL);
Victor Stinnera6192632021-01-29 16:53:03 +0100694 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 compiler_unit_check(c->u);
696 }
Victor Stinnera6192632021-01-29 16:53:03 +0100697 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 c->u = NULL;
Victor Stinnera6192632021-01-29 16:53:03 +0100699 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000700
Victor Stinnera6192632021-01-29 16:53:03 +0100701 PyErr_Restore(exc_type, exc_val, exc_tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000702}
703
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400704static int
705compiler_set_qualname(struct compiler *c)
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100706{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100707 _Py_static_string(dot, ".");
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400708 _Py_static_string(dot_locals, ".<locals>");
709 Py_ssize_t stack_size;
710 struct compiler_unit *u = c->u;
711 PyObject *name, *base, *dot_str, *dot_locals_str;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100712
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400713 base = NULL;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100714 stack_size = PyList_GET_SIZE(c->c_stack);
Benjamin Petersona8a38b82013-10-19 16:14:39 -0400715 assert(stack_size >= 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400716 if (stack_size > 1) {
717 int scope, force_global = 0;
718 struct compiler_unit *parent;
719 PyObject *mangled, *capsule;
720
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400721 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400722 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400723 assert(parent);
724
Yury Selivanov75445082015-05-11 22:57:16 -0400725 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
726 || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
727 || u->u_scope_type == COMPILER_SCOPE_CLASS) {
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400728 assert(u->u_name);
729 mangled = _Py_Mangle(parent->u_private, u->u_name);
730 if (!mangled)
731 return 0;
Victor Stinner28ad12f2021-03-19 12:41:49 +0100732 scope = _PyST_GetScope(parent->u_ste, mangled);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400733 Py_DECREF(mangled);
734 assert(scope != GLOBAL_IMPLICIT);
735 if (scope == GLOBAL_EXPLICIT)
736 force_global = 1;
737 }
738
739 if (!force_global) {
740 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
Yury Selivanov75445082015-05-11 22:57:16 -0400741 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400742 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
743 dot_locals_str = _PyUnicode_FromId(&dot_locals);
744 if (dot_locals_str == NULL)
745 return 0;
746 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
747 if (base == NULL)
748 return 0;
749 }
750 else {
751 Py_INCREF(parent->u_qualname);
752 base = parent->u_qualname;
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400753 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100754 }
755 }
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400756
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400757 if (base != NULL) {
758 dot_str = _PyUnicode_FromId(&dot);
759 if (dot_str == NULL) {
760 Py_DECREF(base);
761 return 0;
762 }
763 name = PyUnicode_Concat(base, dot_str);
764 Py_DECREF(base);
765 if (name == NULL)
766 return 0;
767 PyUnicode_Append(&name, u->u_name);
768 if (name == NULL)
769 return 0;
770 }
771 else {
772 Py_INCREF(u->u_name);
773 name = u->u_name;
774 }
775 u->u_qualname = name;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100776
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400777 return 1;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100778}
779
Eric V. Smith235a6f02015-09-19 14:51:32 -0400780
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781/* Allocate a new block and return a pointer to it.
782 Returns NULL on error.
783*/
784
785static basicblock *
786compiler_new_block(struct compiler *c)
787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 basicblock *b;
789 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 u = c->u;
Andy Lester7668a8b2020-03-24 23:26:44 -0500792 b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 if (b == NULL) {
794 PyErr_NoMemory();
795 return NULL;
796 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 /* Extend the singly linked list of blocks with new block. */
798 b->b_list = u->u_blocks;
799 u->u_blocks = b;
800 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000801}
802
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000803static basicblock *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804compiler_next_block(struct compiler *c)
805{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 basicblock *block = compiler_new_block(c);
807 if (block == NULL)
808 return NULL;
809 c->u->u_curblock->b_next = block;
810 c->u->u_curblock = block;
811 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812}
813
814static basicblock *
815compiler_use_next_block(struct compiler *c, basicblock *block)
816{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 assert(block != NULL);
818 c->u->u_curblock->b_next = block;
819 c->u->u_curblock = block;
820 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821}
822
Mark Shannon5977a792020-12-02 13:31:40 +0000823static basicblock *
824compiler_copy_block(struct compiler *c, basicblock *block)
825{
826 /* Cannot copy a block if it has a fallthrough, since
827 * a block can only have one fallthrough predecessor.
828 */
829 assert(block->b_nofallthrough);
830 basicblock *result = compiler_next_block(c);
831 if (result == NULL) {
832 return NULL;
833 }
834 for (int i = 0; i < block->b_iused; i++) {
835 int n = compiler_next_instr(result);
836 if (n < 0) {
837 return NULL;
838 }
839 result->b_instr[n] = block->b_instr[i];
840 }
841 result->b_exit = block->b_exit;
Mark Shannon3bd60352021-01-13 12:05:43 +0000842 result->b_nofallthrough = 1;
Mark Shannon5977a792020-12-02 13:31:40 +0000843 return result;
844}
845
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846/* Returns the offset of the next instruction in the current block's
847 b_instr array. Resizes the b_instr as necessary.
848 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000849*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850
851static int
Andy Lester76d58772020-03-10 21:18:12 -0500852compiler_next_instr(basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 assert(b != NULL);
855 if (b->b_instr == NULL) {
Andy Lester7668a8b2020-03-24 23:26:44 -0500856 b->b_instr = (struct instr *)PyObject_Calloc(
857 DEFAULT_BLOCK_SIZE, sizeof(struct instr));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 if (b->b_instr == NULL) {
859 PyErr_NoMemory();
860 return -1;
861 }
862 b->b_ialloc = DEFAULT_BLOCK_SIZE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 }
864 else if (b->b_iused == b->b_ialloc) {
865 struct instr *tmp;
866 size_t oldsize, newsize;
867 oldsize = b->b_ialloc * sizeof(struct instr);
868 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000869
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -0700870 if (oldsize > (SIZE_MAX >> 1)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 PyErr_NoMemory();
872 return -1;
873 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 if (newsize == 0) {
876 PyErr_NoMemory();
877 return -1;
878 }
879 b->b_ialloc <<= 1;
880 tmp = (struct instr *)PyObject_Realloc(
881 (void *)b->b_instr, newsize);
882 if (tmp == NULL) {
883 PyErr_NoMemory();
884 return -1;
885 }
886 b->b_instr = tmp;
887 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
888 }
889 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000890}
891
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200892/* Set the line number and column offset for the following instructions.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000893
Christian Heimes2202f872008-02-06 14:31:34 +0000894 The line number is reset in the following cases:
895 - when entering a new scope
896 - on each statement
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200897 - on each expression and sub-expression
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200898 - before the "except" and "finally" clauses
Thomas Wouters89f507f2006-12-13 04:49:30 +0000899*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000900
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200901#define SET_LOC(c, x) \
902 (c)->u->u_lineno = (x)->lineno; \
903 (c)->u->u_col_offset = (x)->col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000904
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200905/* Return the stack effect of opcode with argument oparg.
906
907 Some opcodes have different stack effect when jump to the target and
908 when not jump. The 'jump' parameter specifies the case:
909
910 * 0 -- when not jump
911 * 1 -- when jump
912 * -1 -- maximal
913 */
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200914static int
915stack_effect(int opcode, int oparg, int jump)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 switch (opcode) {
Serhiy Storchaka57faf342018-04-25 22:04:06 +0300918 case NOP:
919 case EXTENDED_ARG:
920 return 0;
921
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200922 /* Stack manipulation */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 case POP_TOP:
924 return -1;
925 case ROT_TWO:
926 case ROT_THREE:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200927 case ROT_FOUR:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 return 0;
929 case DUP_TOP:
930 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000931 case DUP_TOP_TWO:
932 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000933
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200934 /* Unary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 case UNARY_POSITIVE:
936 case UNARY_NEGATIVE:
937 case UNARY_NOT:
938 case UNARY_INVERT:
939 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 case SET_ADD:
942 case LIST_APPEND:
943 return -1;
944 case MAP_ADD:
945 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000946
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200947 /* Binary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 case BINARY_POWER:
949 case BINARY_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400950 case BINARY_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 case BINARY_MODULO:
952 case BINARY_ADD:
953 case BINARY_SUBTRACT:
954 case BINARY_SUBSCR:
955 case BINARY_FLOOR_DIVIDE:
956 case BINARY_TRUE_DIVIDE:
957 return -1;
958 case INPLACE_FLOOR_DIVIDE:
959 case INPLACE_TRUE_DIVIDE:
960 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 case INPLACE_ADD:
963 case INPLACE_SUBTRACT:
964 case INPLACE_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400965 case INPLACE_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 case INPLACE_MODULO:
967 return -1;
968 case STORE_SUBSCR:
969 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 case DELETE_SUBSCR:
971 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 case BINARY_LSHIFT:
974 case BINARY_RSHIFT:
975 case BINARY_AND:
976 case BINARY_XOR:
977 case BINARY_OR:
978 return -1;
979 case INPLACE_POWER:
980 return -1;
981 case GET_ITER:
982 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 case PRINT_EXPR:
985 return -1;
986 case LOAD_BUILD_CLASS:
987 return 1;
988 case INPLACE_LSHIFT:
989 case INPLACE_RSHIFT:
990 case INPLACE_AND:
991 case INPLACE_XOR:
992 case INPLACE_OR:
993 return -1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 case SETUP_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200996 /* 1 in the normal flow.
997 * Restore the stack position and push 6 values before jumping to
998 * the handler if an exception be raised. */
999 return jump ? 6 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 case RETURN_VALUE:
1001 return -1;
1002 case IMPORT_STAR:
1003 return -1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001004 case SETUP_ANNOTATIONS:
1005 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 case YIELD_VALUE:
1007 return 0;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001008 case YIELD_FROM:
1009 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 case POP_BLOCK:
1011 return 0;
1012 case POP_EXCEPT:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001013 return -3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 case STORE_NAME:
1016 return -1;
1017 case DELETE_NAME:
1018 return 0;
1019 case UNPACK_SEQUENCE:
1020 return oparg-1;
1021 case UNPACK_EX:
1022 return (oparg&0xFF) + (oparg>>8);
1023 case FOR_ITER:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001024 /* -1 at end of iterator, 1 if continue iterating. */
1025 return jump > 0 ? -1 : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 case STORE_ATTR:
1028 return -2;
1029 case DELETE_ATTR:
1030 return -1;
1031 case STORE_GLOBAL:
1032 return -1;
1033 case DELETE_GLOBAL:
1034 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 case LOAD_CONST:
1036 return 1;
1037 case LOAD_NAME:
1038 return 1;
1039 case BUILD_TUPLE:
1040 case BUILD_LIST:
1041 case BUILD_SET:
Serhiy Storchakaea525a22016-09-06 22:07:53 +03001042 case BUILD_STRING:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 return 1-oparg;
1044 case BUILD_MAP:
Benjamin Petersonb6855152015-09-10 21:02:39 -07001045 return 1 - 2*oparg;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001046 case BUILD_CONST_KEY_MAP:
1047 return -oparg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 case LOAD_ATTR:
1049 return 0;
1050 case COMPARE_OP:
Mark Shannon9af0e472020-01-14 10:12:45 +00001051 case IS_OP:
1052 case CONTAINS_OP:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 return -1;
Mark Shannon9af0e472020-01-14 10:12:45 +00001054 case JUMP_IF_NOT_EXC_MATCH:
1055 return -2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 case IMPORT_NAME:
1057 return -1;
1058 case IMPORT_FROM:
1059 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001060
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001061 /* Jumps */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 case JUMP_FORWARD:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 case JUMP_ABSOLUTE:
1064 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001065
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001066 case JUMP_IF_TRUE_OR_POP:
1067 case JUMP_IF_FALSE_OR_POP:
1068 return jump ? 0 : -1;
1069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 case POP_JUMP_IF_FALSE:
1071 case POP_JUMP_IF_TRUE:
1072 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 case LOAD_GLOBAL:
1075 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001077 /* Exception handling */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 case SETUP_FINALLY:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001079 /* 0 in the normal flow.
1080 * Restore the stack position and push 6 values before jumping to
1081 * the handler if an exception be raised. */
1082 return jump ? 6 : 0;
Mark Shannonfee55262019-11-21 09:11:43 +00001083 case RERAISE:
1084 return -3;
1085
1086 case WITH_EXCEPT_START:
1087 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 case LOAD_FAST:
1090 return 1;
1091 case STORE_FAST:
1092 return -1;
1093 case DELETE_FAST:
1094 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 case RAISE_VARARGS:
1097 return -oparg;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001098
1099 /* Functions and calls */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 case CALL_FUNCTION:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001101 return -oparg;
Yury Selivanovf2392132016-12-13 19:03:51 -05001102 case CALL_METHOD:
1103 return -oparg-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 case CALL_FUNCTION_KW:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001105 return -oparg-1;
1106 case CALL_FUNCTION_EX:
Matthieu Dartiailh3a9ac822017-02-21 14:25:22 +01001107 return -1 - ((oparg & 0x01) != 0);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001108 case MAKE_FUNCTION:
1109 return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1110 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 case BUILD_SLICE:
1112 if (oparg == 3)
1113 return -2;
1114 else
1115 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001116
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001117 /* Closures */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 case LOAD_CLOSURE:
1119 return 1;
1120 case LOAD_DEREF:
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001121 case LOAD_CLASSDEREF:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 return 1;
1123 case STORE_DEREF:
1124 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001125 case DELETE_DEREF:
1126 return 0;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001127
1128 /* Iterators and generators */
Yury Selivanov75445082015-05-11 22:57:16 -04001129 case GET_AWAITABLE:
1130 return 0;
1131 case SETUP_ASYNC_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001132 /* 0 in the normal flow.
1133 * Restore the stack position to the position before the result
1134 * of __aenter__ and push 6 values before jumping to the handler
1135 * if an exception be raised. */
1136 return jump ? -1 + 6 : 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001137 case BEFORE_ASYNC_WITH:
1138 return 1;
1139 case GET_AITER:
1140 return 0;
1141 case GET_ANEXT:
1142 return 1;
Yury Selivanov5376ba92015-06-22 12:19:30 -04001143 case GET_YIELD_FROM_ITER:
1144 return 0;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02001145 case END_ASYNC_FOR:
1146 return -7;
Eric V. Smitha78c7952015-11-03 12:45:05 -05001147 case FORMAT_VALUE:
1148 /* If there's a fmt_spec on the stack, we go from 2->1,
1149 else 1->1. */
1150 return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
Yury Selivanovf2392132016-12-13 19:03:51 -05001151 case LOAD_METHOD:
1152 return 1;
Zackery Spytzce6a0702019-08-25 03:44:09 -06001153 case LOAD_ASSERTION_ERROR:
1154 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001155 case LIST_TO_TUPLE:
1156 return 0;
1157 case LIST_EXTEND:
1158 case SET_UPDATE:
Mark Shannon8a4cd702020-01-27 09:57:45 +00001159 case DICT_MERGE:
1160 case DICT_UPDATE:
Mark Shannon13bc1392020-01-23 09:25:17 +00001161 return -1;
Brandt Bucher145bf262021-02-26 14:51:55 -08001162 case COPY_DICT_WITHOUT_KEYS:
1163 return 0;
1164 case MATCH_CLASS:
1165 return -1;
1166 case GET_LEN:
1167 case MATCH_MAPPING:
1168 case MATCH_SEQUENCE:
1169 return 1;
1170 case MATCH_KEYS:
1171 return 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 default:
Larry Hastings3a907972013-11-23 14:49:22 -08001173 return PY_INVALID_STACK_EFFECT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 }
Larry Hastings3a907972013-11-23 14:49:22 -08001175 return PY_INVALID_STACK_EFFECT; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176}
1177
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001178int
Serhiy Storchaka7bdf2822018-09-18 09:54:26 +03001179PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump)
1180{
1181 return stack_effect(opcode, oparg, jump);
1182}
1183
1184int
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001185PyCompile_OpcodeStackEffect(int opcode, int oparg)
1186{
1187 return stack_effect(opcode, oparg, -1);
1188}
1189
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190/* Add an opcode with no argument.
1191 Returns 0 on failure, 1 on success.
1192*/
1193
1194static int
Mark Shannon3bd60352021-01-13 12:05:43 +00001195compiler_addop_line(struct compiler *c, int opcode, int line)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 basicblock *b;
1198 struct instr *i;
1199 int off;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001200 assert(!HAS_ARG(opcode));
Andy Lester76d58772020-03-10 21:18:12 -05001201 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 if (off < 0)
1203 return 0;
1204 b = c->u->u_curblock;
1205 i = &b->b_instr[off];
1206 i->i_opcode = opcode;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001207 i->i_oparg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 if (opcode == RETURN_VALUE)
1209 b->b_return = 1;
Mark Shannon3bd60352021-01-13 12:05:43 +00001210 i->i_lineno = line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212}
1213
Mark Shannon3bd60352021-01-13 12:05:43 +00001214static int
1215compiler_addop(struct compiler *c, int opcode)
1216{
1217 return compiler_addop_line(c, opcode, c->u->u_lineno);
1218}
1219
1220static int
1221compiler_addop_noline(struct compiler *c, int opcode)
1222{
1223 return compiler_addop_line(c, opcode, -1);
1224}
1225
1226
Victor Stinnerf8e32212013-11-19 23:56:34 +01001227static Py_ssize_t
Andy Lester76d58772020-03-10 21:18:12 -05001228compiler_add_o(PyObject *dict, PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001229{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001230 PyObject *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001232
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001233 v = PyDict_GetItemWithError(dict, o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 if (!v) {
Stefan Krahc0cbed12015-07-27 12:56:49 +02001235 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 return -1;
Stefan Krahc0cbed12015-07-27 12:56:49 +02001237 }
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001238 arg = PyDict_GET_SIZE(dict);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001239 v = PyLong_FromSsize_t(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 if (!v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 return -1;
1242 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001243 if (PyDict_SetItem(dict, o, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 Py_DECREF(v);
1245 return -1;
1246 }
1247 Py_DECREF(v);
1248 }
1249 else
1250 arg = PyLong_AsLong(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001251 return arg;
1252}
1253
INADA Naokic2e16072018-11-26 21:23:22 +09001254// Merge const *o* recursively and return constant key object.
1255static PyObject*
1256merge_consts_recursive(struct compiler *c, PyObject *o)
1257{
1258 // None and Ellipsis are singleton, and key is the singleton.
1259 // No need to merge object and key.
1260 if (o == Py_None || o == Py_Ellipsis) {
1261 Py_INCREF(o);
1262 return o;
1263 }
1264
1265 PyObject *key = _PyCode_ConstantKey(o);
1266 if (key == NULL) {
1267 return NULL;
1268 }
1269
1270 // t is borrowed reference
1271 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
1272 if (t != key) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001273 // o is registered in c_const_cache. Just use it.
Zackery Spytz9b4a1b12019-03-20 03:16:25 -06001274 Py_XINCREF(t);
INADA Naokic2e16072018-11-26 21:23:22 +09001275 Py_DECREF(key);
1276 return t;
1277 }
1278
INADA Naokif7e4d362018-11-29 00:58:46 +09001279 // We registered o in c_const_cache.
Simeon63b5fc52019-04-09 19:36:57 -04001280 // When o is a tuple or frozenset, we want to merge its
INADA Naokif7e4d362018-11-29 00:58:46 +09001281 // items too.
INADA Naokic2e16072018-11-26 21:23:22 +09001282 if (PyTuple_CheckExact(o)) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001283 Py_ssize_t len = PyTuple_GET_SIZE(o);
1284 for (Py_ssize_t i = 0; i < len; i++) {
INADA Naokic2e16072018-11-26 21:23:22 +09001285 PyObject *item = PyTuple_GET_ITEM(o, i);
1286 PyObject *u = merge_consts_recursive(c, item);
1287 if (u == NULL) {
1288 Py_DECREF(key);
1289 return NULL;
1290 }
1291
1292 // See _PyCode_ConstantKey()
1293 PyObject *v; // borrowed
1294 if (PyTuple_CheckExact(u)) {
1295 v = PyTuple_GET_ITEM(u, 1);
1296 }
1297 else {
1298 v = u;
1299 }
1300 if (v != item) {
1301 Py_INCREF(v);
1302 PyTuple_SET_ITEM(o, i, v);
1303 Py_DECREF(item);
1304 }
1305
1306 Py_DECREF(u);
1307 }
1308 }
INADA Naokif7e4d362018-11-29 00:58:46 +09001309 else if (PyFrozenSet_CheckExact(o)) {
Simeon63b5fc52019-04-09 19:36:57 -04001310 // *key* is tuple. And its first item is frozenset of
INADA Naokif7e4d362018-11-29 00:58:46 +09001311 // constant keys.
1312 // See _PyCode_ConstantKey() for detail.
1313 assert(PyTuple_CheckExact(key));
1314 assert(PyTuple_GET_SIZE(key) == 2);
1315
1316 Py_ssize_t len = PySet_GET_SIZE(o);
1317 if (len == 0) { // empty frozenset should not be re-created.
1318 return key;
1319 }
1320 PyObject *tuple = PyTuple_New(len);
1321 if (tuple == NULL) {
1322 Py_DECREF(key);
1323 return NULL;
1324 }
1325 Py_ssize_t i = 0, pos = 0;
1326 PyObject *item;
1327 Py_hash_t hash;
1328 while (_PySet_NextEntry(o, &pos, &item, &hash)) {
1329 PyObject *k = merge_consts_recursive(c, item);
1330 if (k == NULL) {
1331 Py_DECREF(tuple);
1332 Py_DECREF(key);
1333 return NULL;
1334 }
1335 PyObject *u;
1336 if (PyTuple_CheckExact(k)) {
1337 u = PyTuple_GET_ITEM(k, 1);
1338 Py_INCREF(u);
1339 Py_DECREF(k);
1340 }
1341 else {
1342 u = k;
1343 }
1344 PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u.
1345 i++;
1346 }
1347
1348 // Instead of rewriting o, we create new frozenset and embed in the
1349 // key tuple. Caller should get merged frozenset from the key tuple.
1350 PyObject *new = PyFrozenSet_New(tuple);
1351 Py_DECREF(tuple);
1352 if (new == NULL) {
1353 Py_DECREF(key);
1354 return NULL;
1355 }
1356 assert(PyTuple_GET_ITEM(key, 1) == o);
1357 Py_DECREF(o);
1358 PyTuple_SET_ITEM(key, 1, new);
1359 }
INADA Naokic2e16072018-11-26 21:23:22 +09001360
1361 return key;
1362}
1363
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001364static Py_ssize_t
1365compiler_add_const(struct compiler *c, PyObject *o)
1366{
INADA Naokic2e16072018-11-26 21:23:22 +09001367 PyObject *key = merge_consts_recursive(c, o);
1368 if (key == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001369 return -1;
INADA Naokic2e16072018-11-26 21:23:22 +09001370 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001371
Andy Lester76d58772020-03-10 21:18:12 -05001372 Py_ssize_t arg = compiler_add_o(c->u->u_consts, key);
INADA Naokic2e16072018-11-26 21:23:22 +09001373 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375}
1376
1377static int
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001378compiler_addop_load_const(struct compiler *c, PyObject *o)
1379{
1380 Py_ssize_t arg = compiler_add_const(c, o);
1381 if (arg < 0)
1382 return 0;
1383 return compiler_addop_i(c, LOAD_CONST, arg);
1384}
1385
1386static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001387compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001389{
Andy Lester76d58772020-03-10 21:18:12 -05001390 Py_ssize_t arg = compiler_add_o(dict, o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001391 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001392 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001393 return compiler_addop_i(c, opcode, arg);
1394}
1395
1396static int
1397compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001399{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001400 Py_ssize_t arg;
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001401
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001402 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1403 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001404 return 0;
Andy Lester76d58772020-03-10 21:18:12 -05001405 arg = compiler_add_o(dict, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001406 Py_DECREF(mangled);
1407 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001408 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409 return compiler_addop_i(c, opcode, arg);
1410}
1411
1412/* Add an opcode with an integer argument.
1413 Returns 0 on failure, 1 on success.
1414*/
1415
1416static int
Victor Stinnerf8e32212013-11-19 23:56:34 +01001417compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 struct instr *i;
1420 int off;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001421
Victor Stinner2ad474b2016-03-01 23:34:47 +01001422 /* oparg value is unsigned, but a signed C int is usually used to store
1423 it in the C code (like Python/ceval.c).
1424
1425 Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1426
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001427 The argument of a concrete bytecode instruction is limited to 8-bit.
1428 EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1429 assert(HAS_ARG(opcode));
Victor Stinner2ad474b2016-03-01 23:34:47 +01001430 assert(0 <= oparg && oparg <= 2147483647);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001431
Andy Lester76d58772020-03-10 21:18:12 -05001432 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 if (off < 0)
1434 return 0;
1435 i = &c->u->u_curblock->b_instr[off];
Victor Stinnerf8e32212013-11-19 23:56:34 +01001436 i->i_opcode = opcode;
1437 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001438 i->i_lineno = c->u->u_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440}
1441
Mark Shannon28b75c82020-12-23 11:43:10 +00001442static int add_jump_to_block(basicblock *b, int opcode, int lineno, basicblock *target)
1443{
1444 assert(HAS_ARG(opcode));
1445 assert(b != NULL);
1446 assert(target != NULL);
1447
1448 int off = compiler_next_instr(b);
1449 struct instr *i = &b->b_instr[off];
1450 if (off < 0) {
1451 return 0;
1452 }
1453 i->i_opcode = opcode;
1454 i->i_target = target;
1455 i->i_lineno = lineno;
1456 return 1;
1457}
1458
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459static int
Mark Shannon582aaf12020-08-04 17:30:11 +01001460compiler_addop_j(struct compiler *c, int opcode, basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461{
Mark Shannon28b75c82020-12-23 11:43:10 +00001462 return add_jump_to_block(c->u->u_curblock, opcode, c->u->u_lineno, b);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463}
1464
Mark Shannon127dde52021-01-04 18:06:55 +00001465static int
1466compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b)
1467{
1468 return add_jump_to_block(c->u->u_curblock, opcode, -1, b);
1469}
1470
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +01001471/* NEXT_BLOCK() creates an implicit jump from the current block
1472 to the new block.
1473
1474 The returns inside this macro make it impossible to decref objects
1475 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001477#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 if (compiler_next_block((C)) == NULL) \
1479 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001480}
1481
1482#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 if (!compiler_addop((C), (OP))) \
1484 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485}
1486
Mark Shannon3bd60352021-01-13 12:05:43 +00001487#define ADDOP_NOLINE(C, OP) { \
1488 if (!compiler_addop_noline((C), (OP))) \
1489 return 0; \
1490}
1491
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001492#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 if (!compiler_addop((C), (OP))) { \
1494 compiler_exit_scope(c); \
1495 return 0; \
1496 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001497}
1498
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001499#define ADDOP_LOAD_CONST(C, O) { \
1500 if (!compiler_addop_load_const((C), (O))) \
1501 return 0; \
1502}
1503
1504/* Same as ADDOP_LOAD_CONST, but steals a reference. */
1505#define ADDOP_LOAD_CONST_NEW(C, O) { \
1506 PyObject *__new_const = (O); \
1507 if (__new_const == NULL) { \
1508 return 0; \
1509 } \
1510 if (!compiler_addop_load_const((C), __new_const)) { \
1511 Py_DECREF(__new_const); \
1512 return 0; \
1513 } \
1514 Py_DECREF(__new_const); \
1515}
1516
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1519 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520}
1521
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001522/* Same as ADDOP_O, but steals a reference. */
1523#define ADDOP_N(C, OP, O, TYPE) { \
1524 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1525 Py_DECREF((O)); \
1526 return 0; \
1527 } \
1528 Py_DECREF((O)); \
1529}
1530
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1533 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534}
1535
1536#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 if (!compiler_addop_i((C), (OP), (O))) \
1538 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001539}
1540
Mark Shannon582aaf12020-08-04 17:30:11 +01001541#define ADDOP_JUMP(C, OP, O) { \
1542 if (!compiler_addop_j((C), (OP), (O))) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001544}
1545
Mark Shannon127dde52021-01-04 18:06:55 +00001546/* Add a jump with no line number.
1547 * Used for artificial jumps that have no corresponding
1548 * token in the source code. */
1549#define ADDOP_JUMP_NOLINE(C, OP, O) { \
1550 if (!compiler_addop_j_noline((C), (OP), (O))) \
1551 return 0; \
1552}
1553
Mark Shannon9af0e472020-01-14 10:12:45 +00001554#define ADDOP_COMPARE(C, CMP) { \
1555 if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \
1556 return 0; \
1557}
1558
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1560 the ASDL name to synthesize the name of the C type and the visit function.
1561*/
1562
1563#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 if (!compiler_visit_ ## TYPE((C), (V))) \
1565 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001566}
1567
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001568#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 if (!compiler_visit_ ## TYPE((C), (V))) { \
1570 compiler_exit_scope(c); \
1571 return 0; \
1572 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001573}
1574
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001575#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 if (!compiler_visit_slice((C), (V), (CTX))) \
1577 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001578}
1579
1580#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001582 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1584 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1585 if (!compiler_visit_ ## TYPE((C), elt)) \
1586 return 0; \
1587 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001588}
1589
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001590#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001592 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1594 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1595 if (!compiler_visit_ ## TYPE((C), elt)) { \
1596 compiler_exit_scope(c); \
1597 return 0; \
1598 } \
1599 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001600}
1601
Brandt Bucher145bf262021-02-26 14:51:55 -08001602#define RETURN_IF_FALSE(X) \
1603 if (!(X)) { \
1604 return 0; \
1605 }
1606
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001607/* Search if variable annotations are present statically in a block. */
1608
1609static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001610find_ann(asdl_stmt_seq *stmts)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001611{
1612 int i, j, res = 0;
1613 stmt_ty st;
1614
1615 for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1616 st = (stmt_ty)asdl_seq_GET(stmts, i);
1617 switch (st->kind) {
1618 case AnnAssign_kind:
1619 return 1;
1620 case For_kind:
1621 res = find_ann(st->v.For.body) ||
1622 find_ann(st->v.For.orelse);
1623 break;
1624 case AsyncFor_kind:
1625 res = find_ann(st->v.AsyncFor.body) ||
1626 find_ann(st->v.AsyncFor.orelse);
1627 break;
1628 case While_kind:
1629 res = find_ann(st->v.While.body) ||
1630 find_ann(st->v.While.orelse);
1631 break;
1632 case If_kind:
1633 res = find_ann(st->v.If.body) ||
1634 find_ann(st->v.If.orelse);
1635 break;
1636 case With_kind:
1637 res = find_ann(st->v.With.body);
1638 break;
1639 case AsyncWith_kind:
1640 res = find_ann(st->v.AsyncWith.body);
1641 break;
1642 case Try_kind:
1643 for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1644 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1645 st->v.Try.handlers, j);
1646 if (find_ann(handler->v.ExceptHandler.body)) {
1647 return 1;
1648 }
1649 }
1650 res = find_ann(st->v.Try.body) ||
1651 find_ann(st->v.Try.finalbody) ||
1652 find_ann(st->v.Try.orelse);
1653 break;
1654 default:
1655 res = 0;
1656 }
1657 if (res) {
1658 break;
1659 }
1660 }
1661 return res;
1662}
1663
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001664/*
1665 * Frame block handling functions
1666 */
1667
1668static int
1669compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b,
Mark Shannonfee55262019-11-21 09:11:43 +00001670 basicblock *exit, void *datum)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001671{
1672 struct fblockinfo *f;
1673 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
Mark Shannon02d126a2020-09-25 14:04:19 +01001674 return compiler_error(c, "too many statically nested blocks");
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001675 }
1676 f = &c->u->u_fblock[c->u->u_nfblocks++];
1677 f->fb_type = t;
1678 f->fb_block = b;
1679 f->fb_exit = exit;
Mark Shannonfee55262019-11-21 09:11:43 +00001680 f->fb_datum = datum;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001681 return 1;
1682}
1683
1684static void
1685compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
1686{
1687 struct compiler_unit *u = c->u;
1688 assert(u->u_nfblocks > 0);
1689 u->u_nfblocks--;
1690 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
1691 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
1692}
1693
Mark Shannonfee55262019-11-21 09:11:43 +00001694static int
1695compiler_call_exit_with_nones(struct compiler *c) {
1696 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1697 ADDOP(c, DUP_TOP);
1698 ADDOP(c, DUP_TOP);
1699 ADDOP_I(c, CALL_FUNCTION, 3);
1700 return 1;
1701}
1702
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001703/* Unwind a frame block. If preserve_tos is true, the TOS before
Mark Shannonfee55262019-11-21 09:11:43 +00001704 * popping the blocks will be restored afterwards, unless another
1705 * return, break or continue is found. In which case, the TOS will
1706 * be popped.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001707 */
1708static int
1709compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
1710 int preserve_tos)
1711{
1712 switch (info->fb_type) {
1713 case WHILE_LOOP:
Mark Shannon02d126a2020-09-25 14:04:19 +01001714 case EXCEPTION_HANDLER:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001715 return 1;
1716
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001717 case FOR_LOOP:
1718 /* Pop the iterator */
1719 if (preserve_tos) {
1720 ADDOP(c, ROT_TWO);
1721 }
1722 ADDOP(c, POP_TOP);
1723 return 1;
1724
Mark Shannon02d126a2020-09-25 14:04:19 +01001725 case TRY_EXCEPT:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001726 ADDOP(c, POP_BLOCK);
1727 return 1;
1728
1729 case FINALLY_TRY:
Mark Shannon5274b682020-12-16 13:07:01 +00001730 /* This POP_BLOCK gets the line number of the unwinding statement */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001731 ADDOP(c, POP_BLOCK);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001732 if (preserve_tos) {
Mark Shannonfee55262019-11-21 09:11:43 +00001733 if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) {
1734 return 0;
1735 }
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001736 }
Mark Shannon5274b682020-12-16 13:07:01 +00001737 /* Emit the finally block */
Mark Shannonfee55262019-11-21 09:11:43 +00001738 VISIT_SEQ(c, stmt, info->fb_datum);
1739 if (preserve_tos) {
1740 compiler_pop_fblock(c, POP_VALUE, NULL);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001741 }
Mark Shannon5274b682020-12-16 13:07:01 +00001742 /* The finally block should appear to execute after the
1743 * statement causing the unwinding, so make the unwinding
1744 * instruction artificial */
1745 c->u->u_lineno = -1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001746 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001747
Mark Shannonfee55262019-11-21 09:11:43 +00001748 case FINALLY_END:
1749 if (preserve_tos) {
1750 ADDOP(c, ROT_FOUR);
1751 }
1752 ADDOP(c, POP_TOP);
1753 ADDOP(c, POP_TOP);
1754 ADDOP(c, POP_TOP);
1755 if (preserve_tos) {
1756 ADDOP(c, ROT_FOUR);
1757 }
1758 ADDOP(c, POP_EXCEPT);
1759 return 1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001760
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001761 case WITH:
1762 case ASYNC_WITH:
1763 ADDOP(c, POP_BLOCK);
1764 if (preserve_tos) {
1765 ADDOP(c, ROT_TWO);
1766 }
Mark Shannonfee55262019-11-21 09:11:43 +00001767 if(!compiler_call_exit_with_nones(c)) {
1768 return 0;
1769 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001770 if (info->fb_type == ASYNC_WITH) {
1771 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001772 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001773 ADDOP(c, YIELD_FROM);
1774 }
Mark Shannonfee55262019-11-21 09:11:43 +00001775 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001776 return 1;
1777
1778 case HANDLER_CLEANUP:
Mark Shannonfee55262019-11-21 09:11:43 +00001779 if (info->fb_datum) {
1780 ADDOP(c, POP_BLOCK);
1781 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001782 if (preserve_tos) {
1783 ADDOP(c, ROT_FOUR);
1784 }
Mark Shannonfee55262019-11-21 09:11:43 +00001785 ADDOP(c, POP_EXCEPT);
1786 if (info->fb_datum) {
1787 ADDOP_LOAD_CONST(c, Py_None);
1788 compiler_nameop(c, info->fb_datum, Store);
1789 compiler_nameop(c, info->fb_datum, Del);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001790 }
Mark Shannonfee55262019-11-21 09:11:43 +00001791 return 1;
1792
1793 case POP_VALUE:
1794 if (preserve_tos) {
1795 ADDOP(c, ROT_TWO);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001796 }
Mark Shannonfee55262019-11-21 09:11:43 +00001797 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001798 return 1;
1799 }
1800 Py_UNREACHABLE();
1801}
1802
Mark Shannonfee55262019-11-21 09:11:43 +00001803/** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */
1804static int
1805compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) {
1806 if (c->u->u_nfblocks == 0) {
1807 return 1;
1808 }
1809 struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1];
1810 if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) {
1811 *loop = top;
1812 return 1;
1813 }
1814 struct fblockinfo copy = *top;
1815 c->u->u_nfblocks--;
1816 if (!compiler_unwind_fblock(c, &copy, preserve_tos)) {
1817 return 0;
1818 }
1819 if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) {
1820 return 0;
1821 }
1822 c->u->u_fblock[c->u->u_nfblocks] = copy;
1823 c->u->u_nfblocks++;
1824 return 1;
1825}
1826
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001827/* Compile a sequence of statements, checking for a docstring
1828 and for annotations. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001829
1830static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001831compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001832{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001833 int i = 0;
1834 stmt_ty st;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001835 PyObject *docstring;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001836
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001837 /* Set current line number to the line number of first statement.
1838 This way line number for SETUP_ANNOTATIONS will always
1839 coincide with the line number of first "real" statement in module.
Hansraj Das01171eb2019-10-09 07:54:02 +05301840 If body is empty, then lineno will be set later in assemble. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001841 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) {
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001842 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001843 SET_LOC(c, st);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001844 }
1845 /* Every annotated class and module should have __annotations__. */
1846 if (find_ann(stmts)) {
1847 ADDOP(c, SETUP_ANNOTATIONS);
1848 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001849 if (!asdl_seq_LEN(stmts))
1850 return 1;
INADA Naokicb41b272017-02-23 00:31:59 +09001851 /* if not -OO mode, set docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001852 if (c->c_optimize < 2) {
1853 docstring = _PyAST_GetDocString(stmts);
1854 if (docstring) {
1855 i = 1;
1856 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1857 assert(st->kind == Expr_kind);
1858 VISIT(c, expr, st->v.Expr.value);
1859 if (!compiler_nameop(c, __doc__, Store))
1860 return 0;
1861 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001863 for (; i < asdl_seq_LEN(stmts); i++)
1864 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001866}
1867
1868static PyCodeObject *
1869compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001870{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 PyCodeObject *co;
1872 int addNone = 1;
1873 static PyObject *module;
1874 if (!module) {
1875 module = PyUnicode_InternFromString("<module>");
1876 if (!module)
1877 return NULL;
1878 }
1879 /* Use 0 for firstlineno initially, will fixup in assemble(). */
Mark Shannon877df852020-11-12 09:43:29 +00001880 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 return NULL;
1882 switch (mod->kind) {
1883 case Module_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001884 if (!compiler_body(c, mod->v.Module.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 compiler_exit_scope(c);
1886 return 0;
1887 }
1888 break;
1889 case Interactive_kind:
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001890 if (find_ann(mod->v.Interactive.body)) {
1891 ADDOP(c, SETUP_ANNOTATIONS);
1892 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 c->c_interactive = 1;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001894 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 break;
1896 case Expression_kind:
1897 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1898 addNone = 0;
1899 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 default:
1901 PyErr_Format(PyExc_SystemError,
1902 "module kind %d should not be possible",
1903 mod->kind);
1904 return 0;
1905 }
1906 co = assemble(c, addNone);
1907 compiler_exit_scope(c);
1908 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001909}
1910
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001911/* The test for LOCAL must come before the test for FREE in order to
1912 handle classes where name is both local and free. The local var is
1913 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001914*/
1915
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001916static int
1917get_ref_type(struct compiler *c, PyObject *name)
1918{
Victor Stinner0b1bc562013-05-16 22:17:17 +02001919 int scope;
Benjamin Peterson312595c2013-05-15 15:26:42 -05001920 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001921 _PyUnicode_EqualToASCIIString(name, "__class__"))
Benjamin Peterson312595c2013-05-15 15:26:42 -05001922 return CELL;
Victor Stinner28ad12f2021-03-19 12:41:49 +01001923 scope = _PyST_GetScope(c->u->u_ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 if (scope == 0) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001925 PyErr_Format(PyExc_SystemError,
Victor Stinner28ad12f2021-03-19 12:41:49 +01001926 "_PyST_GetScope(name=%R) failed: "
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001927 "unknown scope in unit %S (%R); "
1928 "symbols: %R; locals: %R; globals: %R",
1929 name,
1930 c->u->u_name, c->u->u_ste->ste_id,
1931 c->u->u_ste->ste_symbols, c->u->u_varnames, c->u->u_names);
1932 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001935}
1936
1937static int
1938compiler_lookup_arg(PyObject *dict, PyObject *name)
1939{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001940 PyObject *v;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001941 v = PyDict_GetItemWithError(dict, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001943 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001944 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001945}
1946
1947static int
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001948compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags,
1949 PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001950{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001951 Py_ssize_t i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001952 if (qualname == NULL)
1953 qualname = co->co_name;
1954
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001955 if (free) {
1956 for (i = 0; i < free; ++i) {
1957 /* Bypass com_addop_varname because it will generate
1958 LOAD_DEREF but LOAD_CLOSURE is needed.
1959 */
1960 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001961
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001962 /* Special case: If a class contains a method with a
1963 free variable that has the same name as a method,
1964 the name will be considered free *and* local in the
1965 class. It should be handled by the closure, as
Min ho Kimc4cacc82019-07-31 08:16:13 +10001966 well as by the normal name lookup logic.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001967 */
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001968 int reftype = get_ref_type(c, name);
1969 if (reftype == -1) {
1970 return 0;
1971 }
1972 int arg;
1973 if (reftype == CELL) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001974 arg = compiler_lookup_arg(c->u->u_cellvars, name);
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001975 }
1976 else {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001977 arg = compiler_lookup_arg(c->u->u_freevars, name);
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001978 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001979 if (arg == -1) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01001980 PyErr_Format(PyExc_SystemError,
1981 "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; "
1982 "freevars of code %S: %R",
1983 name,
1984 reftype,
1985 c->u->u_name,
1986 co->co_name,
1987 co->co_freevars);
1988 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001989 }
1990 ADDOP_I(c, LOAD_CLOSURE, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001991 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001992 flags |= 0x08;
1993 ADDOP_I(c, BUILD_TUPLE, free);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001995 ADDOP_LOAD_CONST(c, (PyObject*)co);
1996 ADDOP_LOAD_CONST(c, qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001997 ADDOP_I(c, MAKE_FUNCTION, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999}
2000
2001static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002002compiler_decorators(struct compiler *c, asdl_expr_seq* decos)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 if (!decos)
2007 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2010 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
2011 }
2012 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002013}
2014
2015static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002016compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs,
2017 asdl_expr_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00002018{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002019 /* Push a dict of keyword-only default values.
2020
2021 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
2022 */
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002023 int i;
2024 PyObject *keys = NULL;
2025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
2027 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
2028 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
2029 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04002030 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002031 if (!mangled) {
2032 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002034 if (keys == NULL) {
2035 keys = PyList_New(1);
2036 if (keys == NULL) {
2037 Py_DECREF(mangled);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002038 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002039 }
2040 PyList_SET_ITEM(keys, 0, mangled);
2041 }
2042 else {
2043 int res = PyList_Append(keys, mangled);
2044 Py_DECREF(mangled);
2045 if (res == -1) {
2046 goto error;
2047 }
2048 }
2049 if (!compiler_visit_expr(c, default_)) {
2050 goto error;
2051 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 }
2053 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002054 if (keys != NULL) {
2055 Py_ssize_t default_count = PyList_GET_SIZE(keys);
2056 PyObject *keys_tuple = PyList_AsTuple(keys);
2057 Py_DECREF(keys);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002058 ADDOP_LOAD_CONST_NEW(c, keys_tuple);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002059 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002060 assert(default_count > 0);
2061 return 1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002062 }
2063 else {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002064 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002065 }
2066
2067error:
2068 Py_XDECREF(keys);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002069 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002070}
2071
2072static int
Guido van Rossum95e4d582018-01-26 08:20:18 -08002073compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
2074{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +03002075 ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation));
Guido van Rossum95e4d582018-01-26 08:20:18 -08002076 return 1;
2077}
2078
2079static int
Neal Norwitzc1505362006-12-28 06:47:50 +00002080compiler_visit_argannotation(struct compiler *c, identifier id,
Yurii Karabas73019792020-11-25 12:43:18 +02002081 expr_ty annotation, Py_ssize_t *annotations_len)
Neal Norwitzc1505362006-12-28 06:47:50 +00002082{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 if (annotation) {
Yurii Karabas73019792020-11-25 12:43:18 +02002084 PyObject *mangled = _Py_Mangle(c->u->u_private, id);
Yury Selivanov34ce99f2014-02-18 12:49:41 -05002085 if (!mangled)
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002086 return 0;
Yurii Karabas73019792020-11-25 12:43:18 +02002087
2088 ADDOP_LOAD_CONST(c, mangled);
Yury Selivanov34ce99f2014-02-18 12:49:41 -05002089 Py_DECREF(mangled);
Yurii Karabas73019792020-11-25 12:43:18 +02002090 VISIT(c, annexpr, annotation);
2091 *annotations_len += 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002092 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002093 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002094}
2095
2096static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002097compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args,
Yurii Karabas73019792020-11-25 12:43:18 +02002098 Py_ssize_t *annotations_len)
Neal Norwitzc1505362006-12-28 06:47:50 +00002099{
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002100 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 for (i = 0; i < asdl_seq_LEN(args); i++) {
2102 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002103 if (!compiler_visit_argannotation(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104 c,
2105 arg->arg,
2106 arg->annotation,
Yurii Karabas73019792020-11-25 12:43:18 +02002107 annotations_len))
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002108 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002110 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002111}
2112
2113static int
2114compiler_visit_annotations(struct compiler *c, arguments_ty args,
2115 expr_ty returns)
2116{
Yurii Karabas73019792020-11-25 12:43:18 +02002117 /* Push arg annotation names and values.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002118 The expressions are evaluated out-of-order wrt the source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00002119
Yurii Karabas73019792020-11-25 12:43:18 +02002120 Return 0 on error, -1 if no annotations pushed, 1 if a annotations is pushed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 */
2122 static identifier return_str;
Yurii Karabas73019792020-11-25 12:43:18 +02002123 Py_ssize_t annotations_len = 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002124
Yurii Karabas73019792020-11-25 12:43:18 +02002125 if (!compiler_visit_argannotations(c, args->args, &annotations_len))
2126 return 0;
2127 if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len))
2128 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002129 if (args->vararg && args->vararg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002130 !compiler_visit_argannotation(c, args->vararg->arg,
Yurii Karabas73019792020-11-25 12:43:18 +02002131 args->vararg->annotation, &annotations_len))
2132 return 0;
2133 if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len))
2134 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002135 if (args->kwarg && args->kwarg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002136 !compiler_visit_argannotation(c, args->kwarg->arg,
Yurii Karabas73019792020-11-25 12:43:18 +02002137 args->kwarg->annotation, &annotations_len))
2138 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 if (!return_str) {
2141 return_str = PyUnicode_InternFromString("return");
2142 if (!return_str)
Yurii Karabas73019792020-11-25 12:43:18 +02002143 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 }
Yurii Karabas73019792020-11-25 12:43:18 +02002145 if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) {
2146 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 }
2148
Yurii Karabas73019792020-11-25 12:43:18 +02002149 if (annotations_len) {
2150 ADDOP_I(c, BUILD_TUPLE, annotations_len);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002151 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002153
Yurii Karabas73019792020-11-25 12:43:18 +02002154 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002155}
2156
2157static int
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002158compiler_visit_defaults(struct compiler *c, arguments_ty args)
2159{
2160 VISIT_SEQ(c, expr, args->defaults);
2161 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
2162 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002163}
2164
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002165static Py_ssize_t
2166compiler_default_arguments(struct compiler *c, arguments_ty args)
2167{
2168 Py_ssize_t funcflags = 0;
2169 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002170 if (!compiler_visit_defaults(c, args))
2171 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002172 funcflags |= 0x01;
2173 }
2174 if (args->kwonlyargs) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002175 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002176 args->kw_defaults);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002177 if (res == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002178 return -1;
2179 }
2180 else if (res > 0) {
2181 funcflags |= 0x02;
2182 }
2183 }
2184 return funcflags;
2185}
2186
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002187static int
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002188forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx)
2189{
2190
2191 if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2192 compiler_error(c, "cannot assign to __debug__");
2193 return 1;
2194 }
2195 return 0;
2196}
2197
2198static int
2199compiler_check_debug_one_arg(struct compiler *c, arg_ty arg)
2200{
2201 if (arg != NULL) {
2202 if (forbidden_name(c, arg->arg, Store))
2203 return 0;
2204 }
2205 return 1;
2206}
2207
2208static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002209compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args)
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002210{
2211 if (args != NULL) {
Pablo Galindoee40e4b2020-04-23 03:43:08 +01002212 for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002213 if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i)))
2214 return 0;
2215 }
2216 }
2217 return 1;
2218}
2219
2220static int
2221compiler_check_debug_args(struct compiler *c, arguments_ty args)
2222{
2223 if (!compiler_check_debug_args_seq(c, args->posonlyargs))
2224 return 0;
2225 if (!compiler_check_debug_args_seq(c, args->args))
2226 return 0;
2227 if (!compiler_check_debug_one_arg(c, args->vararg))
2228 return 0;
2229 if (!compiler_check_debug_args_seq(c, args->kwonlyargs))
2230 return 0;
2231 if (!compiler_check_debug_one_arg(c, args->kwarg))
2232 return 0;
2233 return 1;
2234}
2235
2236static int
Yury Selivanov75445082015-05-11 22:57:16 -04002237compiler_function(struct compiler *c, stmt_ty s, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002238{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002239 PyCodeObject *co;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002240 PyObject *qualname, *docstring = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002241 arguments_ty args;
2242 expr_ty returns;
2243 identifier name;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002244 asdl_expr_seq* decos;
2245 asdl_stmt_seq *body;
INADA Naokicb41b272017-02-23 00:31:59 +09002246 Py_ssize_t i, funcflags;
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002247 int annotations;
Yury Selivanov75445082015-05-11 22:57:16 -04002248 int scope_type;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002249 int firstlineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002250
Yury Selivanov75445082015-05-11 22:57:16 -04002251 if (is_async) {
2252 assert(s->kind == AsyncFunctionDef_kind);
2253
2254 args = s->v.AsyncFunctionDef.args;
2255 returns = s->v.AsyncFunctionDef.returns;
2256 decos = s->v.AsyncFunctionDef.decorator_list;
2257 name = s->v.AsyncFunctionDef.name;
2258 body = s->v.AsyncFunctionDef.body;
2259
2260 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
2261 } else {
2262 assert(s->kind == FunctionDef_kind);
2263
2264 args = s->v.FunctionDef.args;
2265 returns = s->v.FunctionDef.returns;
2266 decos = s->v.FunctionDef.decorator_list;
2267 name = s->v.FunctionDef.name;
2268 body = s->v.FunctionDef.body;
2269
2270 scope_type = COMPILER_SCOPE_FUNCTION;
2271 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002272
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002273 if (!compiler_check_debug_args(c, args))
2274 return 0;
2275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 if (!compiler_decorators(c, decos))
2277 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002278
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002279 firstlineno = s->lineno;
2280 if (asdl_seq_LEN(decos)) {
2281 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2282 }
2283
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002284 funcflags = compiler_default_arguments(c, args);
2285 if (funcflags == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002287 }
2288
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002289 annotations = compiler_visit_annotations(c, args, returns);
2290 if (annotations == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002291 return 0;
2292 }
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002293 else if (annotations > 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002294 funcflags |= 0x04;
2295 }
2296
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002297 if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002298 return 0;
2299 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002300
INADA Naokicb41b272017-02-23 00:31:59 +09002301 /* if not -OO mode, add docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002302 if (c->c_optimize < 2) {
2303 docstring = _PyAST_GetDocString(body);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002304 }
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002305 if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 compiler_exit_scope(c);
2307 return 0;
2308 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002311 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002312 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Mark Shannon877df852020-11-12 09:43:29 +00002313 for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) {
Mark Shannonfd009e62020-11-13 12:53:53 +00002314 VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i));
Mark Shannon877df852020-11-12 09:43:29 +00002315 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002317 qualname = c->u->u_qualname;
2318 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002320 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002321 Py_XDECREF(qualname);
2322 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002324 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002325
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002326 if (!compiler_make_closure(c, co, funcflags, qualname)) {
2327 Py_DECREF(qualname);
2328 Py_DECREF(co);
2329 return 0;
2330 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002331 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 /* decorators */
2335 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2336 ADDOP_I(c, CALL_FUNCTION, 1);
2337 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002338
Yury Selivanov75445082015-05-11 22:57:16 -04002339 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002340}
2341
2342static int
2343compiler_class(struct compiler *c, stmt_ty s)
2344{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 PyCodeObject *co;
2346 PyObject *str;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002347 int i, firstlineno;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002348 asdl_expr_seq *decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 if (!compiler_decorators(c, decos))
2351 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002352
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002353 firstlineno = s->lineno;
2354 if (asdl_seq_LEN(decos)) {
2355 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2356 }
2357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 /* ultimately generate code for:
2359 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
2360 where:
2361 <func> is a function/closure created from the class body;
2362 it has a single argument (__locals__) where the dict
2363 (or MutableSequence) representing the locals is passed
2364 <name> is the class name
2365 <bases> is the positional arguments and *varargs argument
2366 <keywords> is the keyword arguments and **kwds argument
2367 This borrows from compiler_call.
2368 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002371 if (!compiler_enter_scope(c, s->v.ClassDef.name,
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002372 COMPILER_SCOPE_CLASS, (void *)s, firstlineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 return 0;
2374 /* this block represents what we do in the new scope */
2375 {
2376 /* use the class name for name mangling */
2377 Py_INCREF(s->v.ClassDef.name);
Serhiy Storchaka48842712016-04-06 09:45:48 +03002378 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 /* load (global) __name__ ... */
2380 str = PyUnicode_InternFromString("__name__");
2381 if (!str || !compiler_nameop(c, str, Load)) {
2382 Py_XDECREF(str);
2383 compiler_exit_scope(c);
2384 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002385 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 Py_DECREF(str);
2387 /* ... and store it as __module__ */
2388 str = PyUnicode_InternFromString("__module__");
2389 if (!str || !compiler_nameop(c, str, Store)) {
2390 Py_XDECREF(str);
2391 compiler_exit_scope(c);
2392 return 0;
2393 }
2394 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002395 assert(c->u->u_qualname);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002396 ADDOP_LOAD_CONST(c, c->u->u_qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002397 str = PyUnicode_InternFromString("__qualname__");
2398 if (!str || !compiler_nameop(c, str, Store)) {
2399 Py_XDECREF(str);
2400 compiler_exit_scope(c);
2401 return 0;
2402 }
2403 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 /* compile the body proper */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002405 if (!compiler_body(c, s->v.ClassDef.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406 compiler_exit_scope(c);
2407 return 0;
2408 }
Mark Shannone56d54e2021-01-15 13:52:00 +00002409 /* The following code is artificial */
2410 c->u->u_lineno = -1;
Nick Coghlan19d24672016-12-05 16:47:55 +10002411 /* Return __classcell__ if it is referenced, otherwise return None */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002412 if (c->u->u_ste->ste_needs_class_closure) {
Nick Coghlan19d24672016-12-05 16:47:55 +10002413 /* Store __classcell__ into class namespace & return it */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002414 str = PyUnicode_InternFromString("__class__");
2415 if (str == NULL) {
2416 compiler_exit_scope(c);
2417 return 0;
2418 }
2419 i = compiler_lookup_arg(c->u->u_cellvars, str);
2420 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01002421 if (i < 0) {
2422 compiler_exit_scope(c);
2423 return 0;
2424 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002425 assert(i == 0);
Nick Coghlan944368e2016-09-11 14:45:49 +10002426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002427 ADDOP_I(c, LOAD_CLOSURE, i);
Nick Coghlan19d24672016-12-05 16:47:55 +10002428 ADDOP(c, DUP_TOP);
Nick Coghlan944368e2016-09-11 14:45:49 +10002429 str = PyUnicode_InternFromString("__classcell__");
2430 if (!str || !compiler_nameop(c, str, Store)) {
2431 Py_XDECREF(str);
2432 compiler_exit_scope(c);
2433 return 0;
2434 }
2435 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002437 else {
Nick Coghlan19d24672016-12-05 16:47:55 +10002438 /* No methods referenced __class__, so just return None */
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002439 assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002440 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson312595c2013-05-15 15:26:42 -05002441 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002442 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 /* create the code object */
2444 co = assemble(c, 1);
2445 }
2446 /* leave the new scope */
2447 compiler_exit_scope(c);
2448 if (co == NULL)
2449 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 /* 2. load the 'build_class' function */
2452 ADDOP(c, LOAD_BUILD_CLASS);
2453
2454 /* 3. load a function (or closure) made from the code object */
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002455 if (!compiler_make_closure(c, co, 0, NULL)) {
2456 Py_DECREF(co);
2457 return 0;
2458 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 Py_DECREF(co);
2460
2461 /* 4. load class name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002462 ADDOP_LOAD_CONST(c, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002463
2464 /* 5. generate the rest of the code for the call */
Pablo Galindoa5634c42020-09-16 19:42:00 +01002465 if (!compiler_call_helper(c, 2, s->v.ClassDef.bases, s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 return 0;
2467
2468 /* 6. apply decorators */
2469 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2470 ADDOP_I(c, CALL_FUNCTION, 1);
2471 }
2472
2473 /* 7. store into <name> */
2474 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2475 return 0;
2476 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002477}
2478
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02002479/* Return 0 if the expression is a constant value except named singletons.
2480 Return 1 otherwise. */
2481static int
2482check_is_arg(expr_ty e)
2483{
2484 if (e->kind != Constant_kind) {
2485 return 1;
2486 }
2487 PyObject *value = e->v.Constant.value;
2488 return (value == Py_None
2489 || value == Py_False
2490 || value == Py_True
2491 || value == Py_Ellipsis);
2492}
2493
2494/* Check operands of identity chacks ("is" and "is not").
2495 Emit a warning if any operand is a constant except named singletons.
2496 Return 0 on error.
2497 */
2498static int
2499check_compare(struct compiler *c, expr_ty e)
2500{
2501 Py_ssize_t i, n;
2502 int left = check_is_arg(e->v.Compare.left);
2503 n = asdl_seq_LEN(e->v.Compare.ops);
2504 for (i = 0; i < n; i++) {
2505 cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i);
2506 int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2507 if (op == Is || op == IsNot) {
2508 if (!right || !left) {
2509 const char *msg = (op == Is)
2510 ? "\"is\" with a literal. Did you mean \"==\"?"
2511 : "\"is not\" with a literal. Did you mean \"!=\"?";
2512 return compiler_warn(c, msg);
2513 }
2514 }
2515 left = right;
2516 }
2517 return 1;
2518}
2519
Mark Shannon9af0e472020-01-14 10:12:45 +00002520static int compiler_addcompare(struct compiler *c, cmpop_ty op)
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002521{
Mark Shannon9af0e472020-01-14 10:12:45 +00002522 int cmp;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002523 switch (op) {
2524 case Eq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002525 cmp = Py_EQ;
2526 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002527 case NotEq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002528 cmp = Py_NE;
2529 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002530 case Lt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002531 cmp = Py_LT;
2532 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002533 case LtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002534 cmp = Py_LE;
2535 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002536 case Gt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002537 cmp = Py_GT;
2538 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002539 case GtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002540 cmp = Py_GE;
2541 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002542 case Is:
Mark Shannon9af0e472020-01-14 10:12:45 +00002543 ADDOP_I(c, IS_OP, 0);
2544 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002545 case IsNot:
Mark Shannon9af0e472020-01-14 10:12:45 +00002546 ADDOP_I(c, IS_OP, 1);
2547 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002548 case In:
Mark Shannon9af0e472020-01-14 10:12:45 +00002549 ADDOP_I(c, CONTAINS_OP, 0);
2550 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002551 case NotIn:
Mark Shannon9af0e472020-01-14 10:12:45 +00002552 ADDOP_I(c, CONTAINS_OP, 1);
2553 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002554 default:
Mark Shannon9af0e472020-01-14 10:12:45 +00002555 Py_UNREACHABLE();
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002556 }
Mark Shannon9af0e472020-01-14 10:12:45 +00002557 ADDOP_I(c, COMPARE_OP, cmp);
2558 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002559}
2560
Mark Shannon9af0e472020-01-14 10:12:45 +00002561
2562
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002563static int
2564compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2565{
2566 switch (e->kind) {
2567 case UnaryOp_kind:
2568 if (e->v.UnaryOp.op == Not)
2569 return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2570 /* fallback to general implementation */
2571 break;
2572 case BoolOp_kind: {
Pablo Galindoa5634c42020-09-16 19:42:00 +01002573 asdl_expr_seq *s = e->v.BoolOp.values;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002574 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2575 assert(n >= 0);
2576 int cond2 = e->v.BoolOp.op == Or;
2577 basicblock *next2 = next;
2578 if (!cond2 != !cond) {
2579 next2 = compiler_new_block(c);
2580 if (next2 == NULL)
2581 return 0;
2582 }
2583 for (i = 0; i < n; ++i) {
2584 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2585 return 0;
2586 }
2587 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2588 return 0;
2589 if (next2 != next)
2590 compiler_use_next_block(c, next2);
2591 return 1;
2592 }
2593 case IfExp_kind: {
2594 basicblock *end, *next2;
2595 end = compiler_new_block(c);
2596 if (end == NULL)
2597 return 0;
2598 next2 = compiler_new_block(c);
2599 if (next2 == NULL)
2600 return 0;
2601 if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2602 return 0;
2603 if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2604 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002605 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002606 compiler_use_next_block(c, next2);
2607 if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2608 return 0;
2609 compiler_use_next_block(c, end);
2610 return 1;
2611 }
2612 case Compare_kind: {
2613 Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2614 if (n > 0) {
Serhiy Storchaka45835252019-02-16 08:29:46 +02002615 if (!check_compare(c, e)) {
2616 return 0;
2617 }
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002618 basicblock *cleanup = compiler_new_block(c);
2619 if (cleanup == NULL)
2620 return 0;
2621 VISIT(c, expr, e->v.Compare.left);
2622 for (i = 0; i < n; i++) {
2623 VISIT(c, expr,
2624 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2625 ADDOP(c, DUP_TOP);
2626 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00002627 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01002628 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002629 NEXT_BLOCK(c);
2630 }
2631 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00002632 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Mark Shannon582aaf12020-08-04 17:30:11 +01002633 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Mark Shannon266b4622020-11-17 19:30:14 +00002634 NEXT_BLOCK(c);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002635 basicblock *end = compiler_new_block(c);
2636 if (end == NULL)
2637 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00002638 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002639 compiler_use_next_block(c, cleanup);
2640 ADDOP(c, POP_TOP);
2641 if (!cond) {
Mark Shannon127dde52021-01-04 18:06:55 +00002642 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002643 }
2644 compiler_use_next_block(c, end);
2645 return 1;
2646 }
2647 /* fallback to general implementation */
2648 break;
2649 }
2650 default:
2651 /* fallback to general implementation */
2652 break;
2653 }
2654
2655 /* general implementation */
2656 VISIT(c, expr, e);
Mark Shannon582aaf12020-08-04 17:30:11 +01002657 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Mark Shannon266b4622020-11-17 19:30:14 +00002658 NEXT_BLOCK(c);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002659 return 1;
2660}
2661
2662static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002663compiler_ifexp(struct compiler *c, expr_ty e)
2664{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 basicblock *end, *next;
2666
2667 assert(e->kind == IfExp_kind);
2668 end = compiler_new_block(c);
2669 if (end == NULL)
2670 return 0;
2671 next = compiler_new_block(c);
2672 if (next == NULL)
2673 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002674 if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2675 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002676 VISIT(c, expr, e->v.IfExp.body);
Mark Shannon127dde52021-01-04 18:06:55 +00002677 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002678 compiler_use_next_block(c, next);
2679 VISIT(c, expr, e->v.IfExp.orelse);
2680 compiler_use_next_block(c, end);
2681 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002682}
2683
2684static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002685compiler_lambda(struct compiler *c, expr_ty e)
2686{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002687 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002688 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002689 static identifier name;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002690 Py_ssize_t funcflags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002691 arguments_ty args = e->v.Lambda.args;
2692 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002693
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002694 if (!compiler_check_debug_args(c, args))
2695 return 0;
2696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002697 if (!name) {
2698 name = PyUnicode_InternFromString("<lambda>");
2699 if (!name)
2700 return 0;
2701 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002702
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002703 funcflags = compiler_default_arguments(c, args);
2704 if (funcflags == -1) {
2705 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002706 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002707
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002708 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002709 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002710 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002712 /* Make None the first constant, so the lambda can't have a
2713 docstring. */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002714 if (compiler_add_const(c, Py_None) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002715 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002717 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002718 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002719 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2720 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2721 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002722 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002723 }
2724 else {
2725 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002726 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002727 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002728 qualname = c->u->u_qualname;
2729 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002730 compiler_exit_scope(c);
Pablo Galindo7fdab832021-01-29 22:40:59 +00002731 if (co == NULL) {
2732 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002733 return 0;
Pablo Galindo7fdab832021-01-29 22:40:59 +00002734 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002735
Victor Stinnerba7a99d2021-01-30 01:46:44 +01002736 if (!compiler_make_closure(c, co, funcflags, qualname)) {
2737 Py_DECREF(qualname);
2738 Py_DECREF(co);
2739 return 0;
2740 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002741 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 Py_DECREF(co);
2743
2744 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002745}
2746
2747static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748compiler_if(struct compiler *c, stmt_ty s)
2749{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002750 basicblock *end, *next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 assert(s->kind == If_kind);
2752 end = compiler_new_block(c);
Mark Shannon8473cf82020-12-15 11:07:50 +00002753 if (end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 return 0;
Mark Shannon8473cf82020-12-15 11:07:50 +00002755 }
2756 if (asdl_seq_LEN(s->v.If.orelse)) {
2757 next = compiler_new_block(c);
2758 if (next == NULL) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002759 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002760 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002761 }
2762 else {
2763 next = end;
2764 }
2765 if (!compiler_jump_if(c, s->v.If.test, next, 0)) {
2766 return 0;
2767 }
2768 VISIT_SEQ(c, stmt, s->v.If.body);
2769 if (asdl_seq_LEN(s->v.If.orelse)) {
Mark Shannon127dde52021-01-04 18:06:55 +00002770 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Mark Shannon8473cf82020-12-15 11:07:50 +00002771 compiler_use_next_block(c, next);
2772 VISIT_SEQ(c, stmt, s->v.If.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 }
2774 compiler_use_next_block(c, end);
2775 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776}
2777
2778static int
2779compiler_for(struct compiler *c, stmt_ty s)
2780{
Mark Shannon5977a792020-12-02 13:31:40 +00002781 basicblock *start, *body, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002783 start = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002784 body = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 cleanup = compiler_new_block(c);
2786 end = compiler_new_block(c);
Mark Shannon5977a792020-12-02 13:31:40 +00002787 if (start == NULL || body == NULL || end == NULL || cleanup == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002789 }
2790 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002791 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002792 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 VISIT(c, expr, s->v.For.iter);
2794 ADDOP(c, GET_ITER);
2795 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01002796 ADDOP_JUMP(c, FOR_ITER, cleanup);
Mark Shannon5977a792020-12-02 13:31:40 +00002797 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002798 VISIT(c, expr, s->v.For.target);
2799 VISIT_SEQ(c, stmt, s->v.For.body);
Mark Shannonf5e97b72020-12-14 11:28:39 +00002800 /* Mark jump as artificial */
2801 c->u->u_lineno = -1;
Mark Shannon582aaf12020-08-04 17:30:11 +01002802 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002803 compiler_use_next_block(c, cleanup);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002804
2805 compiler_pop_fblock(c, FOR_LOOP, start);
2806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002807 VISIT_SEQ(c, stmt, s->v.For.orelse);
2808 compiler_use_next_block(c, end);
2809 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002810}
2811
Yury Selivanov75445082015-05-11 22:57:16 -04002812
2813static int
2814compiler_async_for(struct compiler *c, stmt_ty s)
2815{
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002816 basicblock *start, *except, *end;
Pablo Galindo90235812020-03-15 04:29:22 +00002817 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07002818 c->u->u_ste->ste_coroutine = 1;
2819 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
Zsolt Dollensteine2396502018-04-27 08:58:56 -07002820 return compiler_error(c, "'async for' outside async function");
2821 }
2822
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002823 start = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002824 except = compiler_new_block(c);
2825 end = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002826
Mark Shannonfee55262019-11-21 09:11:43 +00002827 if (start == NULL || except == NULL || end == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002828 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002829 }
Yury Selivanov75445082015-05-11 22:57:16 -04002830 VISIT(c, expr, s->v.AsyncFor.iter);
2831 ADDOP(c, GET_AITER);
Yury Selivanov75445082015-05-11 22:57:16 -04002832
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002833 compiler_use_next_block(c, start);
Mark Shannonfee55262019-11-21 09:11:43 +00002834 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002835 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002836 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002837 /* SETUP_FINALLY to guard the __anext__ call */
Mark Shannon582aaf12020-08-04 17:30:11 +01002838 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov75445082015-05-11 22:57:16 -04002839 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002840 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04002841 ADDOP(c, YIELD_FROM);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002842 ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */
Yury Selivanov75445082015-05-11 22:57:16 -04002843
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002844 /* Success block for __anext__ */
2845 VISIT(c, expr, s->v.AsyncFor.target);
2846 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002847 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002848
2849 compiler_pop_fblock(c, FOR_LOOP, start);
Yury Selivanov75445082015-05-11 22:57:16 -04002850
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002851 /* Except block for __anext__ */
Yury Selivanov75445082015-05-11 22:57:16 -04002852 compiler_use_next_block(c, except);
Mark Shannon877df852020-11-12 09:43:29 +00002853
2854 c->u->u_lineno = -1;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002855 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov75445082015-05-11 22:57:16 -04002856
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002857 /* `else` block */
Yury Selivanov75445082015-05-11 22:57:16 -04002858 VISIT_SEQ(c, stmt, s->v.For.orelse);
2859
2860 compiler_use_next_block(c, end);
2861
2862 return 1;
2863}
2864
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002865static int
2866compiler_while(struct compiler *c, stmt_ty s)
2867{
Mark Shannon266b4622020-11-17 19:30:14 +00002868 basicblock *loop, *body, *end, *anchor = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 loop = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002870 body = compiler_new_block(c);
2871 anchor = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 end = compiler_new_block(c);
Mark Shannon266b4622020-11-17 19:30:14 +00002873 if (loop == NULL || body == NULL || anchor == NULL || end == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 compiler_use_next_block(c, loop);
Mark Shannon266b4622020-11-17 19:30:14 +00002877 if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 }
Mark Shannon8473cf82020-12-15 11:07:50 +00002880 if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) {
2881 return 0;
Mark Shannon266b4622020-11-17 19:30:14 +00002882 }
2883
2884 compiler_use_next_block(c, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 VISIT_SEQ(c, stmt, s->v.While.body);
Mark Shannon8473cf82020-12-15 11:07:50 +00002886 SET_LOC(c, s);
2887 if (!compiler_jump_if(c, s->v.While.test, body, 1)) {
2888 return 0;
2889 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002890
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002891 compiler_pop_fblock(c, WHILE_LOOP, loop);
2892
Mark Shannon266b4622020-11-17 19:30:14 +00002893 compiler_use_next_block(c, anchor);
2894 if (s->v.While.orelse) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 VISIT_SEQ(c, stmt, s->v.While.orelse);
Mark Shannon266b4622020-11-17 19:30:14 +00002896 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002899 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002900}
2901
2902static int
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002903compiler_return(struct compiler *c, stmt_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002904{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002905 int preserve_tos = ((s->v.Return.value != NULL) &&
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002906 (s->v.Return.value->kind != Constant_kind));
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002907 if (c->u->u_ste->ste_type != FunctionBlock)
2908 return compiler_error(c, "'return' outside function");
2909 if (s->v.Return.value != NULL &&
2910 c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2911 {
2912 return compiler_error(
2913 c, "'return' with value in async generator");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002914 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002915 if (preserve_tos) {
2916 VISIT(c, expr, s->v.Return.value);
Mark Shannon5274b682020-12-16 13:07:01 +00002917 } else {
2918 /* Emit instruction with line number for expression */
2919 if (s->v.Return.value != NULL) {
2920 SET_LOC(c, s->v.Return.value);
2921 ADDOP(c, NOP);
2922 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002923 }
Mark Shannonfee55262019-11-21 09:11:43 +00002924 if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL))
2925 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002926 if (s->v.Return.value == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002927 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002928 }
2929 else if (!preserve_tos) {
Mark Shannon5274b682020-12-16 13:07:01 +00002930 ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002931 }
2932 ADDOP(c, RETURN_VALUE);
Mark Shannon266b4622020-11-17 19:30:14 +00002933 NEXT_BLOCK(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002935 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002936}
2937
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002938static int
2939compiler_break(struct compiler *c)
2940{
Mark Shannonfee55262019-11-21 09:11:43 +00002941 struct fblockinfo *loop = NULL;
2942 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
2943 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002944 }
Mark Shannonfee55262019-11-21 09:11:43 +00002945 if (loop == NULL) {
2946 return compiler_error(c, "'break' outside loop");
2947 }
2948 if (!compiler_unwind_fblock(c, loop, 0)) {
2949 return 0;
2950 }
Mark Shannon582aaf12020-08-04 17:30:11 +01002951 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit);
Mark Shannon266b4622020-11-17 19:30:14 +00002952 NEXT_BLOCK(c);
Mark Shannonfee55262019-11-21 09:11:43 +00002953 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002954}
2955
2956static int
2957compiler_continue(struct compiler *c)
2958{
Mark Shannonfee55262019-11-21 09:11:43 +00002959 struct fblockinfo *loop = NULL;
2960 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
2961 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002962 }
Mark Shannonfee55262019-11-21 09:11:43 +00002963 if (loop == NULL) {
2964 return compiler_error(c, "'continue' not properly in loop");
2965 }
Mark Shannon582aaf12020-08-04 17:30:11 +01002966 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block);
Mark Shannon266b4622020-11-17 19:30:14 +00002967 NEXT_BLOCK(c)
Mark Shannonfee55262019-11-21 09:11:43 +00002968 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002969}
2970
2971
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002972/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002973
2974 SETUP_FINALLY L
2975 <code for body>
2976 POP_BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00002977 <code for finalbody>
2978 JUMP E
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002979 L:
2980 <code for finalbody>
Mark Shannonfee55262019-11-21 09:11:43 +00002981 E:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002982
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002983 The special instructions use the block stack. Each block
2984 stack entry contains the instruction that created it (here
2985 SETUP_FINALLY), the level of the value stack at the time the
2986 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002988 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 Pushes the current value stack level and the label
2990 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002991 POP_BLOCK:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002992 Pops en entry from the block stack.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002993
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002994 The block stack is unwound when an exception is raised:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002995 when a SETUP_FINALLY entry is found, the raised and the caught
2996 exceptions are pushed onto the value stack (and the exception
2997 condition is cleared), and the interpreter jumps to the label
2998 gotten from the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002999*/
3000
3001static int
3002compiler_try_finally(struct compiler *c, stmt_ty s)
3003{
Mark Shannonfee55262019-11-21 09:11:43 +00003004 basicblock *body, *end, *exit;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 body = compiler_new_block(c);
3007 end = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00003008 exit = compiler_new_block(c);
3009 if (body == NULL || end == NULL || exit == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003011
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003012 /* `try` block */
Mark Shannon582aaf12020-08-04 17:30:11 +01003013 ADDOP_JUMP(c, SETUP_FINALLY, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003014 compiler_use_next_block(c, body);
Mark Shannonfee55262019-11-21 09:11:43 +00003015 if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003016 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003017 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
3018 if (!compiler_try_except(c, s))
3019 return 0;
3020 }
3021 else {
3022 VISIT_SEQ(c, stmt, s->v.Try.body);
3023 }
Mark Shannon3bd60352021-01-13 12:05:43 +00003024 ADDOP_NOLINE(c, POP_BLOCK);
Mark Shannonfee55262019-11-21 09:11:43 +00003025 compiler_pop_fblock(c, FINALLY_TRY, body);
3026 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Mark Shannon127dde52021-01-04 18:06:55 +00003027 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00003028 /* `finally` block */
3029 compiler_use_next_block(c, end);
3030 if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL))
3031 return 0;
3032 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3033 compiler_pop_fblock(c, FINALLY_END, end);
Mark Shannonbf353f32020-12-17 13:55:28 +00003034 ADDOP_I(c, RERAISE, 0);
Mark Shannonfee55262019-11-21 09:11:43 +00003035 compiler_use_next_block(c, exit);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003036 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003037}
3038
3039/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003040 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003041 (The contents of the value stack is shown in [], with the top
3042 at the right; 'tb' is trace-back info, 'val' the exception's
3043 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003044
3045 Value stack Label Instruction Argument
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003046 [] SETUP_FINALLY L1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003047 [] <code for S>
3048 [] POP_BLOCK
3049 [] JUMP_FORWARD L0
3050
3051 [tb, val, exc] L1: DUP )
3052 [tb, val, exc, exc] <evaluate E1> )
Mark Shannon9af0e472020-01-14 10:12:45 +00003053 [tb, val, exc, exc, E1] JUMP_IF_NOT_EXC_MATCH L2 ) only if E1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054 [tb, val, exc] POP
3055 [tb, val] <assign to V1> (or POP if no V1)
3056 [tb] POP
3057 [] <code for S1>
3058 JUMP_FORWARD L0
3059
3060 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003061 .............................etc.......................
3062
Mark Shannonfee55262019-11-21 09:11:43 +00003063 [tb, val, exc] Ln+1: RERAISE # re-raise exception
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003064
3065 [] L0: <next statement>
3066
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003067 Of course, parts are not generated if Vi or Ei is not present.
3068*/
3069static int
3070compiler_try_except(struct compiler *c, stmt_ty s)
3071{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003072 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003073 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003075 body = compiler_new_block(c);
3076 except = compiler_new_block(c);
3077 orelse = compiler_new_block(c);
3078 end = compiler_new_block(c);
3079 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
3080 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01003081 ADDOP_JUMP(c, SETUP_FINALLY, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082 compiler_use_next_block(c, body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003083 if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003084 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003085 VISIT_SEQ(c, stmt, s->v.Try.body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003086 compiler_pop_fblock(c, TRY_EXCEPT, body);
Mark Shannon3bd60352021-01-13 12:05:43 +00003087 ADDOP_NOLINE(c, POP_BLOCK);
3088 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003089 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 compiler_use_next_block(c, except);
Mark Shannon02d126a2020-09-25 14:04:19 +01003091 /* Runtime will push a block here, so we need to account for that */
3092 if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL))
3093 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 for (i = 0; i < n; i++) {
3095 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003096 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003097 if (!handler->v.ExceptHandler.type && i < n-1)
3098 return compiler_error(c, "default 'except:' must be last");
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003099 SET_LOC(c, handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003100 except = compiler_new_block(c);
3101 if (except == NULL)
3102 return 0;
3103 if (handler->v.ExceptHandler.type) {
3104 ADDOP(c, DUP_TOP);
3105 VISIT(c, expr, handler->v.ExceptHandler.type);
Mark Shannon582aaf12020-08-04 17:30:11 +01003106 ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except);
Mark Shannon266b4622020-11-17 19:30:14 +00003107 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003108 }
3109 ADDOP(c, POP_TOP);
3110 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003111 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00003112
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003113 cleanup_end = compiler_new_block(c);
3114 cleanup_body = compiler_new_block(c);
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003115 if (cleanup_end == NULL || cleanup_body == NULL) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003116 return 0;
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003117 }
Guido van Rossumb940e112007-01-10 16:19:56 +00003118
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003119 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3120 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003122 /*
3123 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003124 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003125 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003126 try:
3127 # body
3128 finally:
Chris Angelicoad098b62019-05-21 23:34:19 +10003129 name = None # in case body contains "del name"
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003130 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003131 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003132
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003133 /* second try: */
Mark Shannon582aaf12020-08-04 17:30:11 +01003134 ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003135 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003136 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003137 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003139 /* second # body */
3140 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003141 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003142 ADDOP(c, POP_BLOCK);
3143 ADDOP(c, POP_EXCEPT);
Mark Shannon877df852020-11-12 09:43:29 +00003144 /* name = None; del name; # Mark as artificial */
3145 c->u->u_lineno = -1;
Mark Shannonfee55262019-11-21 09:11:43 +00003146 ADDOP_LOAD_CONST(c, Py_None);
3147 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3148 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Mark Shannon582aaf12020-08-04 17:30:11 +01003149 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003150
Mark Shannonfee55262019-11-21 09:11:43 +00003151 /* except: */
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003152 compiler_use_next_block(c, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003153
Mark Shannon877df852020-11-12 09:43:29 +00003154 /* name = None; del name; # Mark as artificial */
3155 c->u->u_lineno = -1;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003156 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003157 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003158 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003159
Mark Shannonbf353f32020-12-17 13:55:28 +00003160 ADDOP_I(c, RERAISE, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 }
3162 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003163 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003165 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05003166 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003167 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003168
Guido van Rossumb940e112007-01-10 16:19:56 +00003169 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003170 ADDOP(c, POP_TOP);
3171 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003172 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003173 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003174 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003175 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannon127dde52021-01-04 18:06:55 +00003176 /* name = None; del name; # Mark as artificial */
3177 c->u->u_lineno = -1;
Mark Shannonfee55262019-11-21 09:11:43 +00003178 ADDOP(c, POP_EXCEPT);
Mark Shannon582aaf12020-08-04 17:30:11 +01003179 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003180 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 compiler_use_next_block(c, except);
3182 }
Mark Shannon02d126a2020-09-25 14:04:19 +01003183 compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL);
Mark Shannonf2dbfd72020-12-21 13:53:50 +00003184 /* Mark as artificial */
3185 c->u->u_lineno = -1;
Mark Shannonbf353f32020-12-17 13:55:28 +00003186 ADDOP_I(c, RERAISE, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003187 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003188 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 compiler_use_next_block(c, end);
3190 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003191}
3192
3193static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003194compiler_try(struct compiler *c, stmt_ty s) {
3195 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
3196 return compiler_try_finally(c, s);
3197 else
3198 return compiler_try_except(c, s);
3199}
3200
3201
3202static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003203compiler_import_as(struct compiler *c, identifier name, identifier asname)
3204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003205 /* The IMPORT_NAME opcode was already generated. This function
3206 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208 If there is a dot in name, we need to split it and emit a
Serhiy Storchakaf93234b2017-05-09 22:31:05 +03003209 IMPORT_FROM for each name.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003211 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
3212 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003213 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003214 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003215 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003216 /* Consume the base module name to get the first attribute */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003217 while (1) {
3218 Py_ssize_t pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 PyObject *attr;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003220 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003221 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003222 return 0;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003223 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003224 if (!attr)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003225 return 0;
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003226 ADDOP_N(c, IMPORT_FROM, attr, names);
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003227 if (dot == -1) {
3228 break;
3229 }
3230 ADDOP(c, ROT_TWO);
3231 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003232 }
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003233 if (!compiler_nameop(c, asname, Store)) {
3234 return 0;
3235 }
3236 ADDOP(c, POP_TOP);
3237 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003238 }
3239 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003240}
3241
3242static int
3243compiler_import(struct compiler *c, stmt_ty s)
3244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003245 /* The Import node stores a module name like a.b.c as a single
3246 string. This is convenient for all cases except
3247 import a.b.c as d
3248 where we need to parse that string to extract the individual
3249 module names.
3250 XXX Perhaps change the representation to make this case simpler?
3251 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01003252 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003253
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003254 PyObject *zero = _PyLong_GetZero(); // borrowed reference
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003255 for (i = 0; i < n; i++) {
3256 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
3257 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003258
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003259 ADDOP_LOAD_CONST(c, zero);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003260 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 if (alias->asname) {
3264 r = compiler_import_as(c, alias->name, alias->asname);
3265 if (!r)
3266 return r;
3267 }
3268 else {
3269 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003270 Py_ssize_t dot = PyUnicode_FindChar(
3271 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02003272 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003273 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02003274 if (tmp == NULL)
3275 return 0;
3276 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003277 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003278 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 Py_DECREF(tmp);
3280 }
3281 if (!r)
3282 return r;
3283 }
3284 }
3285 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003286}
3287
3288static int
3289compiler_from_import(struct compiler *c, stmt_ty s)
3290{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003291 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003292 PyObject *names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003295 if (!empty_string) {
3296 empty_string = PyUnicode_FromString("");
3297 if (!empty_string)
3298 return 0;
3299 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003300
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003301 ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02003302
3303 names = PyTuple_New(n);
3304 if (!names)
3305 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 /* build up the names */
3308 for (i = 0; i < n; i++) {
3309 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3310 Py_INCREF(alias->name);
3311 PyTuple_SET_ITEM(names, i, alias->name);
3312 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003315 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 Py_DECREF(names);
3317 return compiler_error(c, "from __future__ imports must occur "
3318 "at the beginning of the file");
3319 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003320 ADDOP_LOAD_CONST_NEW(c, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003322 if (s->v.ImportFrom.module) {
3323 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
3324 }
3325 else {
3326 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
3327 }
3328 for (i = 0; i < n; i++) {
3329 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3330 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003331
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003332 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003333 assert(n == 1);
3334 ADDOP(c, IMPORT_STAR);
3335 return 1;
3336 }
3337
3338 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
3339 store_name = alias->name;
3340 if (alias->asname)
3341 store_name = alias->asname;
3342
3343 if (!compiler_nameop(c, store_name, Store)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003344 return 0;
3345 }
3346 }
3347 /* remove imported module */
3348 ADDOP(c, POP_TOP);
3349 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003350}
3351
3352static int
3353compiler_assert(struct compiler *c, stmt_ty s)
3354{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003356
tsukasa-aua8ef4572021-03-16 22:14:41 +11003357 /* Always emit a warning if the test is a non-zero length tuple */
3358 if ((s->v.Assert.test->kind == Tuple_kind &&
3359 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) ||
3360 (s->v.Assert.test->kind == Constant_kind &&
3361 PyTuple_Check(s->v.Assert.test->v.Constant.value) &&
3362 PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0))
Serhiy Storchakad31e7732018-10-21 10:09:39 +03003363 {
3364 if (!compiler_warn(c, "assertion is always true, "
3365 "perhaps remove parentheses?"))
3366 {
Victor Stinner14e461d2013-08-26 22:28:21 +02003367 return 0;
3368 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003369 }
tsukasa-aua8ef4572021-03-16 22:14:41 +11003370 if (c->c_optimize)
3371 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003372 end = compiler_new_block(c);
3373 if (end == NULL)
3374 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003375 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
3376 return 0;
Zackery Spytzce6a0702019-08-25 03:44:09 -06003377 ADDOP(c, LOAD_ASSERTION_ERROR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003378 if (s->v.Assert.msg) {
3379 VISIT(c, expr, s->v.Assert.msg);
3380 ADDOP_I(c, CALL_FUNCTION, 1);
3381 }
3382 ADDOP_I(c, RAISE_VARARGS, 1);
3383 compiler_use_next_block(c, end);
3384 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003385}
3386
3387static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003388compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3389{
3390 if (c->c_interactive && c->c_nestlevel <= 1) {
3391 VISIT(c, expr, value);
3392 ADDOP(c, PRINT_EXPR);
3393 return 1;
3394 }
3395
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003396 if (value->kind == Constant_kind) {
Victor Stinner15a30952016-02-08 22:45:06 +01003397 /* ignore constant statement */
Mark Shannon877df852020-11-12 09:43:29 +00003398 ADDOP(c, NOP);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003399 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003400 }
3401
3402 VISIT(c, expr, value);
Mark Shannonc5440932021-03-15 14:24:25 +00003403 /* Mark POP_TOP as artificial */
3404 c->u->u_lineno = -1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003405 ADDOP(c, POP_TOP);
3406 return 1;
3407}
3408
3409static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003410compiler_visit_stmt(struct compiler *c, stmt_ty s)
3411{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003412 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003414 /* Always assign a lineno to the next instruction for a stmt. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003415 SET_LOC(c, s);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003417 switch (s->kind) {
3418 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04003419 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003420 case ClassDef_kind:
3421 return compiler_class(c, s);
3422 case Return_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003423 return compiler_return(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003424 case Delete_kind:
3425 VISIT_SEQ(c, expr, s->v.Delete.targets)
3426 break;
3427 case Assign_kind:
3428 n = asdl_seq_LEN(s->v.Assign.targets);
3429 VISIT(c, expr, s->v.Assign.value);
3430 for (i = 0; i < n; i++) {
3431 if (i < n - 1)
3432 ADDOP(c, DUP_TOP);
3433 VISIT(c, expr,
3434 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3435 }
3436 break;
3437 case AugAssign_kind:
3438 return compiler_augassign(c, s);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003439 case AnnAssign_kind:
3440 return compiler_annassign(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003441 case For_kind:
3442 return compiler_for(c, s);
3443 case While_kind:
3444 return compiler_while(c, s);
3445 case If_kind:
3446 return compiler_if(c, s);
Brandt Bucher145bf262021-02-26 14:51:55 -08003447 case Match_kind:
3448 return compiler_match(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003449 case Raise_kind:
3450 n = 0;
3451 if (s->v.Raise.exc) {
3452 VISIT(c, expr, s->v.Raise.exc);
3453 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003454 if (s->v.Raise.cause) {
3455 VISIT(c, expr, s->v.Raise.cause);
3456 n++;
3457 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003458 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01003459 ADDOP_I(c, RAISE_VARARGS, (int)n);
Mark Shannon266b4622020-11-17 19:30:14 +00003460 NEXT_BLOCK(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003461 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003462 case Try_kind:
3463 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003464 case Assert_kind:
3465 return compiler_assert(c, s);
3466 case Import_kind:
3467 return compiler_import(c, s);
3468 case ImportFrom_kind:
3469 return compiler_from_import(c, s);
3470 case Global_kind:
3471 case Nonlocal_kind:
3472 break;
3473 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003474 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003475 case Pass_kind:
Mark Shannon877df852020-11-12 09:43:29 +00003476 ADDOP(c, NOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 break;
3478 case Break_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003479 return compiler_break(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 case Continue_kind:
3481 return compiler_continue(c);
3482 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003483 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04003484 case AsyncFunctionDef_kind:
3485 return compiler_function(c, s, 1);
3486 case AsyncWith_kind:
3487 return compiler_async_with(c, s, 0);
3488 case AsyncFor_kind:
3489 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003490 }
Yury Selivanov75445082015-05-11 22:57:16 -04003491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003492 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003493}
3494
3495static int
3496unaryop(unaryop_ty op)
3497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003498 switch (op) {
3499 case Invert:
3500 return UNARY_INVERT;
3501 case Not:
3502 return UNARY_NOT;
3503 case UAdd:
3504 return UNARY_POSITIVE;
3505 case USub:
3506 return UNARY_NEGATIVE;
3507 default:
3508 PyErr_Format(PyExc_SystemError,
3509 "unary op %d should not be possible", op);
3510 return 0;
3511 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003512}
3513
3514static int
Andy Lester76d58772020-03-10 21:18:12 -05003515binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003517 switch (op) {
3518 case Add:
3519 return BINARY_ADD;
3520 case Sub:
3521 return BINARY_SUBTRACT;
3522 case Mult:
3523 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003524 case MatMult:
3525 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003526 case Div:
3527 return BINARY_TRUE_DIVIDE;
3528 case Mod:
3529 return BINARY_MODULO;
3530 case Pow:
3531 return BINARY_POWER;
3532 case LShift:
3533 return BINARY_LSHIFT;
3534 case RShift:
3535 return BINARY_RSHIFT;
3536 case BitOr:
3537 return BINARY_OR;
3538 case BitXor:
3539 return BINARY_XOR;
3540 case BitAnd:
3541 return BINARY_AND;
3542 case FloorDiv:
3543 return BINARY_FLOOR_DIVIDE;
3544 default:
3545 PyErr_Format(PyExc_SystemError,
3546 "binary op %d should not be possible", op);
3547 return 0;
3548 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003549}
3550
3551static int
Andy Lester76d58772020-03-10 21:18:12 -05003552inplace_binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003553{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003554 switch (op) {
3555 case Add:
3556 return INPLACE_ADD;
3557 case Sub:
3558 return INPLACE_SUBTRACT;
3559 case Mult:
3560 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003561 case MatMult:
3562 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003563 case Div:
3564 return INPLACE_TRUE_DIVIDE;
3565 case Mod:
3566 return INPLACE_MODULO;
3567 case Pow:
3568 return INPLACE_POWER;
3569 case LShift:
3570 return INPLACE_LSHIFT;
3571 case RShift:
3572 return INPLACE_RSHIFT;
3573 case BitOr:
3574 return INPLACE_OR;
3575 case BitXor:
3576 return INPLACE_XOR;
3577 case BitAnd:
3578 return INPLACE_AND;
3579 case FloorDiv:
3580 return INPLACE_FLOOR_DIVIDE;
3581 default:
3582 PyErr_Format(PyExc_SystemError,
3583 "inplace binary op %d should not be possible", op);
3584 return 0;
3585 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003586}
3587
3588static int
3589compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3590{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003591 int op, scope;
3592 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003593 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003595 PyObject *dict = c->u->u_names;
3596 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003597
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003598 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3599 !_PyUnicode_EqualToASCIIString(name, "True") &&
3600 !_PyUnicode_EqualToASCIIString(name, "False"));
Benjamin Peterson70b224d2012-12-06 17:49:58 -05003601
Pablo Galindoc5fc1562020-04-22 23:29:27 +01003602 if (forbidden_name(c, name, ctx))
3603 return 0;
3604
Serhiy Storchakabd6ec4d2017-12-18 14:29:12 +02003605 mangled = _Py_Mangle(c->u->u_private, name);
3606 if (!mangled)
3607 return 0;
3608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003609 op = 0;
3610 optype = OP_NAME;
Victor Stinner28ad12f2021-03-19 12:41:49 +01003611 scope = _PyST_GetScope(c->u->u_ste, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 switch (scope) {
3613 case FREE:
3614 dict = c->u->u_freevars;
3615 optype = OP_DEREF;
3616 break;
3617 case CELL:
3618 dict = c->u->u_cellvars;
3619 optype = OP_DEREF;
3620 break;
3621 case LOCAL:
3622 if (c->u->u_ste->ste_type == FunctionBlock)
3623 optype = OP_FAST;
3624 break;
3625 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04003626 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 optype = OP_GLOBAL;
3628 break;
3629 case GLOBAL_EXPLICIT:
3630 optype = OP_GLOBAL;
3631 break;
3632 default:
3633 /* scope can be 0 */
3634 break;
3635 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003637 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003638 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003640 switch (optype) {
3641 case OP_DEREF:
3642 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003643 case Load:
3644 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3645 break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003646 case Store: op = STORE_DEREF; break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003647 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003648 }
3649 break;
3650 case OP_FAST:
3651 switch (ctx) {
3652 case Load: op = LOAD_FAST; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003653 case Store: op = STORE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003654 case Del: op = DELETE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003655 }
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003656 ADDOP_N(c, op, mangled, varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003657 return 1;
3658 case OP_GLOBAL:
3659 switch (ctx) {
3660 case Load: op = LOAD_GLOBAL; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003661 case Store: op = STORE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003662 case Del: op = DELETE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003663 }
3664 break;
3665 case OP_NAME:
3666 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00003667 case Load: op = LOAD_NAME; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003668 case Store: op = STORE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003669 case Del: op = DELETE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003670 }
3671 break;
3672 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003674 assert(op);
Andy Lester76d58772020-03-10 21:18:12 -05003675 arg = compiler_add_o(dict, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003676 Py_DECREF(mangled);
3677 if (arg < 0)
3678 return 0;
3679 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003680}
3681
3682static int
3683compiler_boolop(struct compiler *c, expr_ty e)
3684{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003685 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003686 int jumpi;
3687 Py_ssize_t i, n;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003688 asdl_expr_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003690 assert(e->kind == BoolOp_kind);
3691 if (e->v.BoolOp.op == And)
3692 jumpi = JUMP_IF_FALSE_OR_POP;
3693 else
3694 jumpi = JUMP_IF_TRUE_OR_POP;
3695 end = compiler_new_block(c);
3696 if (end == NULL)
3697 return 0;
3698 s = e->v.BoolOp.values;
3699 n = asdl_seq_LEN(s) - 1;
3700 assert(n >= 0);
3701 for (i = 0; i < n; ++i) {
3702 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003703 ADDOP_JUMP(c, jumpi, end);
Mark Shannon6e8128f2020-07-30 10:03:00 +01003704 basicblock *next = compiler_new_block(c);
3705 if (next == NULL) {
3706 return 0;
3707 }
3708 compiler_use_next_block(c, next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003709 }
3710 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3711 compiler_use_next_block(c, end);
3712 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003713}
3714
3715static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003716starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
Mark Shannon13bc1392020-01-23 09:25:17 +00003717 int build, int add, int extend, int tuple)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003718{
3719 Py_ssize_t n = asdl_seq_LEN(elts);
Mark Shannon13bc1392020-01-23 09:25:17 +00003720 Py_ssize_t i, seen_star = 0;
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003721 if (n > 2 && are_all_items_const(elts, 0, n)) {
3722 PyObject *folded = PyTuple_New(n);
3723 if (folded == NULL) {
3724 return 0;
3725 }
3726 PyObject *val;
3727 for (i = 0; i < n; i++) {
3728 val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
3729 Py_INCREF(val);
3730 PyTuple_SET_ITEM(folded, i, val);
3731 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003732 if (tuple) {
3733 ADDOP_LOAD_CONST_NEW(c, folded);
3734 } else {
3735 if (add == SET_ADD) {
3736 Py_SETREF(folded, PyFrozenSet_New(folded));
3737 if (folded == NULL) {
3738 return 0;
3739 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003740 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003741 ADDOP_I(c, build, pushed);
3742 ADDOP_LOAD_CONST_NEW(c, folded);
3743 ADDOP_I(c, extend, 1);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003744 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003745 return 1;
3746 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003747
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003748 for (i = 0; i < n; i++) {
3749 expr_ty elt = asdl_seq_GET(elts, i);
3750 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003751 seen_star = 1;
3752 }
3753 }
3754 if (seen_star) {
3755 seen_star = 0;
3756 for (i = 0; i < n; i++) {
3757 expr_ty elt = asdl_seq_GET(elts, i);
3758 if (elt->kind == Starred_kind) {
3759 if (seen_star == 0) {
3760 ADDOP_I(c, build, i+pushed);
3761 seen_star = 1;
3762 }
3763 VISIT(c, expr, elt->v.Starred.value);
3764 ADDOP_I(c, extend, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003765 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003766 else {
3767 VISIT(c, expr, elt);
3768 if (seen_star) {
3769 ADDOP_I(c, add, 1);
3770 }
3771 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003772 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003773 assert(seen_star);
3774 if (tuple) {
3775 ADDOP(c, LIST_TO_TUPLE);
3776 }
3777 }
3778 else {
3779 for (i = 0; i < n; i++) {
3780 expr_ty elt = asdl_seq_GET(elts, i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003781 VISIT(c, expr, elt);
Mark Shannon13bc1392020-01-23 09:25:17 +00003782 }
3783 if (tuple) {
3784 ADDOP_I(c, BUILD_TUPLE, n+pushed);
3785 } else {
3786 ADDOP_I(c, build, n+pushed);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003787 }
3788 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003789 return 1;
3790}
3791
3792static int
Brandt Bucher145bf262021-02-26 14:51:55 -08003793unpack_helper(struct compiler *c, asdl_expr_seq *elts)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003794{
3795 Py_ssize_t n = asdl_seq_LEN(elts);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003796 int seen_star = 0;
Brandt Bucher145bf262021-02-26 14:51:55 -08003797 for (Py_ssize_t i = 0; i < n; i++) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003798 expr_ty elt = asdl_seq_GET(elts, i);
3799 if (elt->kind == Starred_kind && !seen_star) {
3800 if ((i >= (1 << 8)) ||
3801 (n-i-1 >= (INT_MAX >> 8)))
3802 return compiler_error(c,
3803 "too many expressions in "
3804 "star-unpacking assignment");
3805 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3806 seen_star = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003807 }
3808 else if (elt->kind == Starred_kind) {
3809 return compiler_error(c,
Furkan Öndercb6534e2020-03-26 04:54:31 +03003810 "multiple starred expressions in assignment");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003811 }
3812 }
3813 if (!seen_star) {
3814 ADDOP_I(c, UNPACK_SEQUENCE, n);
3815 }
Brandt Bucher145bf262021-02-26 14:51:55 -08003816 return 1;
3817}
3818
3819static int
3820assignment_helper(struct compiler *c, asdl_expr_seq *elts)
3821{
3822 Py_ssize_t n = asdl_seq_LEN(elts);
3823 RETURN_IF_FALSE(unpack_helper(c, elts));
3824 for (Py_ssize_t i = 0; i < n; i++) {
Brandt Bucherd5aa2e92020-03-07 19:44:18 -08003825 expr_ty elt = asdl_seq_GET(elts, i);
3826 VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value);
3827 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003828 return 1;
3829}
3830
3831static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003832compiler_list(struct compiler *c, expr_ty e)
3833{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003834 asdl_expr_seq *elts = e->v.List.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003835 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003836 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003837 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003838 else if (e->v.List.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003839 return starunpack_helper(c, elts, 0, BUILD_LIST,
3840 LIST_APPEND, LIST_EXTEND, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003841 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003842 else
3843 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003844 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003845}
3846
3847static int
3848compiler_tuple(struct compiler *c, expr_ty e)
3849{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003850 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003851 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003852 return assignment_helper(c, elts);
3853 }
3854 else if (e->v.Tuple.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003855 return starunpack_helper(c, elts, 0, BUILD_LIST,
3856 LIST_APPEND, LIST_EXTEND, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003857 }
3858 else
3859 VISIT_SEQ(c, expr, elts);
3860 return 1;
3861}
3862
3863static int
3864compiler_set(struct compiler *c, expr_ty e)
3865{
Mark Shannon13bc1392020-01-23 09:25:17 +00003866 return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET,
3867 SET_ADD, SET_UPDATE, 0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003868}
3869
3870static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003871are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003872{
3873 Py_ssize_t i;
3874 for (i = begin; i < end; i++) {
3875 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003876 if (key == NULL || key->kind != Constant_kind)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003877 return 0;
3878 }
3879 return 1;
3880}
3881
3882static int
3883compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3884{
3885 Py_ssize_t i, n = end - begin;
3886 PyObject *keys, *key;
3887 if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) {
3888 for (i = begin; i < end; i++) {
3889 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3890 }
3891 keys = PyTuple_New(n);
3892 if (keys == NULL) {
3893 return 0;
3894 }
3895 for (i = begin; i < end; i++) {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003896 key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003897 Py_INCREF(key);
3898 PyTuple_SET_ITEM(keys, i - begin, key);
3899 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003900 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003901 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3902 }
3903 else {
3904 for (i = begin; i < end; i++) {
3905 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3906 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3907 }
3908 ADDOP_I(c, BUILD_MAP, n);
3909 }
3910 return 1;
3911}
3912
3913static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003914compiler_dict(struct compiler *c, expr_ty e)
3915{
Victor Stinner976bb402016-03-23 11:36:19 +01003916 Py_ssize_t i, n, elements;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003917 int have_dict;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003918 int is_unpacking = 0;
3919 n = asdl_seq_LEN(e->v.Dict.values);
Mark Shannon8a4cd702020-01-27 09:57:45 +00003920 have_dict = 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003921 elements = 0;
3922 for (i = 0; i < n; i++) {
3923 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003924 if (is_unpacking) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003925 if (elements) {
3926 if (!compiler_subdict(c, e, i - elements, i)) {
3927 return 0;
3928 }
3929 if (have_dict) {
3930 ADDOP_I(c, DICT_UPDATE, 1);
3931 }
3932 have_dict = 1;
3933 elements = 0;
3934 }
3935 if (have_dict == 0) {
3936 ADDOP_I(c, BUILD_MAP, 0);
3937 have_dict = 1;
3938 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003939 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Mark Shannon8a4cd702020-01-27 09:57:45 +00003940 ADDOP_I(c, DICT_UPDATE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003941 }
3942 else {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003943 if (elements == 0xFFFF) {
Pablo Galindoc51db0e2020-08-13 09:48:41 +01003944 if (!compiler_subdict(c, e, i - elements, i + 1)) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003945 return 0;
3946 }
3947 if (have_dict) {
3948 ADDOP_I(c, DICT_UPDATE, 1);
3949 }
3950 have_dict = 1;
3951 elements = 0;
3952 }
3953 else {
3954 elements++;
3955 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003956 }
3957 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003958 if (elements) {
3959 if (!compiler_subdict(c, e, n - elements, n)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003960 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003961 }
3962 if (have_dict) {
3963 ADDOP_I(c, DICT_UPDATE, 1);
3964 }
3965 have_dict = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003966 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003967 if (!have_dict) {
3968 ADDOP_I(c, BUILD_MAP, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003969 }
3970 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003971}
3972
3973static int
3974compiler_compare(struct compiler *c, expr_ty e)
3975{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003976 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003977
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02003978 if (!check_compare(c, e)) {
3979 return 0;
3980 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003981 VISIT(c, expr, e->v.Compare.left);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003982 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
3983 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
3984 if (n == 0) {
3985 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Mark Shannon9af0e472020-01-14 10:12:45 +00003986 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003987 }
3988 else {
3989 basicblock *cleanup = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003990 if (cleanup == NULL)
3991 return 0;
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003992 for (i = 0; i < n; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003993 VISIT(c, expr,
3994 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003995 ADDOP(c, DUP_TOP);
3996 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00003997 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003998 ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003999 NEXT_BLOCK(c);
4000 }
4001 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00004002 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004003 basicblock *end = compiler_new_block(c);
4004 if (end == NULL)
4005 return 0;
Mark Shannon127dde52021-01-04 18:06:55 +00004006 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004007 compiler_use_next_block(c, cleanup);
4008 ADDOP(c, ROT_TWO);
4009 ADDOP(c, POP_TOP);
4010 compiler_use_next_block(c, end);
4011 }
4012 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004013}
4014
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004015static PyTypeObject *
4016infer_type(expr_ty e)
4017{
4018 switch (e->kind) {
4019 case Tuple_kind:
4020 return &PyTuple_Type;
4021 case List_kind:
4022 case ListComp_kind:
4023 return &PyList_Type;
4024 case Dict_kind:
4025 case DictComp_kind:
4026 return &PyDict_Type;
4027 case Set_kind:
4028 case SetComp_kind:
4029 return &PySet_Type;
4030 case GeneratorExp_kind:
4031 return &PyGen_Type;
4032 case Lambda_kind:
4033 return &PyFunction_Type;
4034 case JoinedStr_kind:
4035 case FormattedValue_kind:
4036 return &PyUnicode_Type;
4037 case Constant_kind:
Victor Stinnera102ed72020-02-07 02:24:48 +01004038 return Py_TYPE(e->v.Constant.value);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004039 default:
4040 return NULL;
4041 }
4042}
4043
4044static int
4045check_caller(struct compiler *c, expr_ty e)
4046{
4047 switch (e->kind) {
4048 case Constant_kind:
4049 case Tuple_kind:
4050 case List_kind:
4051 case ListComp_kind:
4052 case Dict_kind:
4053 case DictComp_kind:
4054 case Set_kind:
4055 case SetComp_kind:
4056 case GeneratorExp_kind:
4057 case JoinedStr_kind:
4058 case FormattedValue_kind:
4059 return compiler_warn(c, "'%.200s' object is not callable; "
4060 "perhaps you missed a comma?",
4061 infer_type(e)->tp_name);
4062 default:
4063 return 1;
4064 }
4065}
4066
4067static int
4068check_subscripter(struct compiler *c, expr_ty e)
4069{
4070 PyObject *v;
4071
4072 switch (e->kind) {
4073 case Constant_kind:
4074 v = e->v.Constant.value;
4075 if (!(v == Py_None || v == Py_Ellipsis ||
4076 PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
4077 PyAnySet_Check(v)))
4078 {
4079 return 1;
4080 }
4081 /* fall through */
4082 case Set_kind:
4083 case SetComp_kind:
4084 case GeneratorExp_kind:
4085 case Lambda_kind:
4086 return compiler_warn(c, "'%.200s' object is not subscriptable; "
4087 "perhaps you missed a comma?",
4088 infer_type(e)->tp_name);
4089 default:
4090 return 1;
4091 }
4092}
4093
4094static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004095check_index(struct compiler *c, expr_ty e, expr_ty s)
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004096{
4097 PyObject *v;
4098
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004099 PyTypeObject *index_type = infer_type(s);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004100 if (index_type == NULL
4101 || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
4102 || index_type == &PySlice_Type) {
4103 return 1;
4104 }
4105
4106 switch (e->kind) {
4107 case Constant_kind:
4108 v = e->v.Constant.value;
4109 if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
4110 return 1;
4111 }
4112 /* fall through */
4113 case Tuple_kind:
4114 case List_kind:
4115 case ListComp_kind:
4116 case JoinedStr_kind:
4117 case FormattedValue_kind:
4118 return compiler_warn(c, "%.200s indices must be integers or slices, "
4119 "not %.200s; "
4120 "perhaps you missed a comma?",
4121 infer_type(e)->tp_name,
4122 index_type->tp_name);
4123 default:
4124 return 1;
4125 }
4126}
4127
Zackery Spytz97f5de02019-03-22 01:30:32 -06004128// Return 1 if the method call was optimized, -1 if not, and 0 on error.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004129static int
Yury Selivanovf2392132016-12-13 19:03:51 -05004130maybe_optimize_method_call(struct compiler *c, expr_ty e)
4131{
4132 Py_ssize_t argsl, i;
4133 expr_ty meth = e->v.Call.func;
Pablo Galindoa5634c42020-09-16 19:42:00 +01004134 asdl_expr_seq *args = e->v.Call.args;
Yury Selivanovf2392132016-12-13 19:03:51 -05004135
4136 /* Check that the call node is an attribute access, and that
4137 the call doesn't have keyword parameters. */
4138 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
4139 asdl_seq_LEN(e->v.Call.keywords))
4140 return -1;
4141
4142 /* Check that there are no *varargs types of arguments. */
4143 argsl = asdl_seq_LEN(args);
4144 for (i = 0; i < argsl; i++) {
4145 expr_ty elt = asdl_seq_GET(args, i);
4146 if (elt->kind == Starred_kind) {
4147 return -1;
4148 }
4149 }
4150
4151 /* Alright, we can optimize the code. */
4152 VISIT(c, expr, meth->v.Attribute.value);
Mark Shannond48848c2021-03-14 18:01:30 +00004153 int old_lineno = c->u->u_lineno;
4154 c->u->u_lineno = meth->end_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004155 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
4156 VISIT_SEQ(c, expr, e->v.Call.args);
4157 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
Mark Shannond48848c2021-03-14 18:01:30 +00004158 c->u->u_lineno = old_lineno;
Yury Selivanovf2392132016-12-13 19:03:51 -05004159 return 1;
4160}
4161
4162static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004163validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
Zackery Spytz08050e92020-04-06 00:47:47 -06004164{
4165 Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
4166 for (Py_ssize_t i = 0; i < nkeywords; i++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004167 keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
4168 if (key->arg == NULL) {
4169 continue;
4170 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +01004171 if (forbidden_name(c, key->arg, Store)) {
4172 return -1;
4173 }
Zackery Spytz08050e92020-04-06 00:47:47 -06004174 for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004175 keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
4176 if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004177 c->u->u_col_offset = other->col_offset;
Brandt Bucher145bf262021-02-26 14:51:55 -08004178 compiler_error(c, "keyword argument repeated: %U", key->arg);
Pablo Galindo254ec782020-04-03 20:37:13 +01004179 return -1;
4180 }
4181 }
4182 }
4183 return 0;
4184}
4185
4186static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004187compiler_call(struct compiler *c, expr_ty e)
4188{
Zackery Spytz97f5de02019-03-22 01:30:32 -06004189 int ret = maybe_optimize_method_call(c, e);
4190 if (ret >= 0) {
4191 return ret;
4192 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004193 if (!check_caller(c, e->v.Call.func)) {
4194 return 0;
4195 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004196 VISIT(c, expr, e->v.Call.func);
4197 return compiler_call_helper(c, 0,
4198 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004199 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004200}
4201
Eric V. Smith235a6f02015-09-19 14:51:32 -04004202static int
4203compiler_joined_str(struct compiler *c, expr_ty e)
4204{
Eric V. Smith235a6f02015-09-19 14:51:32 -04004205 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004206 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1)
4207 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
Eric V. Smith235a6f02015-09-19 14:51:32 -04004208 return 1;
4209}
4210
Eric V. Smitha78c7952015-11-03 12:45:05 -05004211/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004212static int
4213compiler_formatted_value(struct compiler *c, expr_ty e)
4214{
Eric V. Smitha78c7952015-11-03 12:45:05 -05004215 /* Our oparg encodes 2 pieces of information: the conversion
4216 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04004217
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004218 Convert the conversion char to 3 bits:
4219 : 000 0x0 FVC_NONE The default if nothing specified.
Eric V. Smitha78c7952015-11-03 12:45:05 -05004220 !s : 001 0x1 FVC_STR
4221 !r : 010 0x2 FVC_REPR
4222 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04004223
Eric V. Smitha78c7952015-11-03 12:45:05 -05004224 next bit is whether or not we have a format spec:
4225 yes : 100 0x4
4226 no : 000 0x0
4227 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004228
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004229 int conversion = e->v.FormattedValue.conversion;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004230 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004231
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004232 /* The expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004233 VISIT(c, expr, e->v.FormattedValue.value);
4234
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004235 switch (conversion) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004236 case 's': oparg = FVC_STR; break;
4237 case 'r': oparg = FVC_REPR; break;
4238 case 'a': oparg = FVC_ASCII; break;
4239 case -1: oparg = FVC_NONE; break;
4240 default:
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004241 PyErr_Format(PyExc_SystemError,
4242 "Unrecognized conversion character %d", conversion);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004243 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004244 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004245 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004246 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004247 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004248 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004249 }
4250
Eric V. Smitha78c7952015-11-03 12:45:05 -05004251 /* And push our opcode and oparg */
4252 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004253
Eric V. Smith235a6f02015-09-19 14:51:32 -04004254 return 1;
4255}
4256
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004257static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004258compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004259{
4260 Py_ssize_t i, n = end - begin;
4261 keyword_ty kw;
4262 PyObject *keys, *key;
4263 assert(n > 0);
4264 if (n > 1) {
4265 for (i = begin; i < end; i++) {
4266 kw = asdl_seq_GET(keywords, i);
4267 VISIT(c, expr, kw->value);
4268 }
4269 keys = PyTuple_New(n);
4270 if (keys == NULL) {
4271 return 0;
4272 }
4273 for (i = begin; i < end; i++) {
4274 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
4275 Py_INCREF(key);
4276 PyTuple_SET_ITEM(keys, i - begin, key);
4277 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004278 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004279 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
4280 }
4281 else {
4282 /* a for loop only executes once */
4283 for (i = begin; i < end; i++) {
4284 kw = asdl_seq_GET(keywords, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004285 ADDOP_LOAD_CONST(c, kw->arg);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004286 VISIT(c, expr, kw->value);
4287 }
4288 ADDOP_I(c, BUILD_MAP, n);
4289 }
4290 return 1;
4291}
4292
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004293/* shared code between compiler_call and compiler_class */
4294static int
4295compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01004296 int n, /* Args already pushed */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004297 asdl_expr_seq *args,
4298 asdl_keyword_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004299{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004300 Py_ssize_t i, nseen, nelts, nkwelts;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004301
Pablo Galindo254ec782020-04-03 20:37:13 +01004302 if (validate_keywords(c, keywords) == -1) {
4303 return 0;
4304 }
4305
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004306 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004307 nkwelts = asdl_seq_LEN(keywords);
4308
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004309 for (i = 0; i < nelts; i++) {
4310 expr_ty elt = asdl_seq_GET(args, i);
4311 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00004312 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004313 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004314 }
4315 for (i = 0; i < nkwelts; i++) {
4316 keyword_ty kw = asdl_seq_GET(keywords, i);
4317 if (kw->arg == NULL) {
4318 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004319 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004320 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004321
Mark Shannon13bc1392020-01-23 09:25:17 +00004322 /* No * or ** args, so can use faster calling sequence */
4323 for (i = 0; i < nelts; i++) {
4324 expr_ty elt = asdl_seq_GET(args, i);
4325 assert(elt->kind != Starred_kind);
4326 VISIT(c, expr, elt);
4327 }
4328 if (nkwelts) {
4329 PyObject *names;
4330 VISIT_SEQ(c, keyword, keywords);
4331 names = PyTuple_New(nkwelts);
4332 if (names == NULL) {
4333 return 0;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004334 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004335 for (i = 0; i < nkwelts; i++) {
4336 keyword_ty kw = asdl_seq_GET(keywords, i);
4337 Py_INCREF(kw->arg);
4338 PyTuple_SET_ITEM(names, i, kw->arg);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004339 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004340 ADDOP_LOAD_CONST_NEW(c, names);
4341 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
4342 return 1;
4343 }
4344 else {
4345 ADDOP_I(c, CALL_FUNCTION, n + nelts);
4346 return 1;
4347 }
4348
4349ex_call:
4350
4351 /* Do positional arguments. */
4352 if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
4353 VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
4354 }
4355 else if (starunpack_helper(c, args, n, BUILD_LIST,
4356 LIST_APPEND, LIST_EXTEND, 1) == 0) {
4357 return 0;
4358 }
4359 /* Then keyword arguments */
4360 if (nkwelts) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004361 /* Has a new dict been pushed */
4362 int have_dict = 0;
Mark Shannon13bc1392020-01-23 09:25:17 +00004363
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004364 nseen = 0; /* the number of keyword arguments on the stack following */
4365 for (i = 0; i < nkwelts; i++) {
4366 keyword_ty kw = asdl_seq_GET(keywords, i);
4367 if (kw->arg == NULL) {
4368 /* A keyword argument unpacking. */
4369 if (nseen) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004370 if (!compiler_subkwargs(c, keywords, i - nseen, i)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004371 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004372 }
Mark Shannondb64f122020-06-01 10:42:42 +01004373 if (have_dict) {
4374 ADDOP_I(c, DICT_MERGE, 1);
4375 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004376 have_dict = 1;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004377 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004378 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004379 if (!have_dict) {
4380 ADDOP_I(c, BUILD_MAP, 0);
4381 have_dict = 1;
4382 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004383 VISIT(c, expr, kw->value);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004384 ADDOP_I(c, DICT_MERGE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004385 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004386 else {
4387 nseen++;
4388 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004389 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004390 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004391 /* Pack up any trailing keyword arguments. */
Mark Shannon8a4cd702020-01-27 09:57:45 +00004392 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004393 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004394 }
4395 if (have_dict) {
4396 ADDOP_I(c, DICT_MERGE, 1);
4397 }
4398 have_dict = 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004399 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004400 assert(have_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004401 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004402 ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0);
4403 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004404}
4405
Nick Coghlan650f0d02007-04-15 12:05:43 +00004406
4407/* List and set comprehensions and generator expressions work by creating a
4408 nested function to perform the actual iteration. This means that the
4409 iteration variables don't leak into the current scope.
4410 The defined function is called immediately following its definition, with the
4411 result of that call being the result of the expression.
4412 The LC/SC version returns the populated container, while the GE version is
4413 flagged in symtable.c as a generator, so it returns the generator object
4414 when the function is called.
Nick Coghlan650f0d02007-04-15 12:05:43 +00004415
4416 Possible cleanups:
4417 - iterate over the generator sequence instead of using recursion
4418*/
4419
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004420
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004421static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004422compiler_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004423 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004424 int depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004425 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004426{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004427 comprehension_ty gen;
4428 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4429 if (gen->is_async) {
4430 return compiler_async_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004431 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004432 } else {
4433 return compiler_sync_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004434 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004435 }
4436}
4437
4438static int
4439compiler_sync_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004440 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004441 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004442 expr_ty elt, expr_ty val, int type)
4443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004444 /* generate code for the iterator, then each of the ifs,
4445 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004447 comprehension_ty gen;
4448 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004449 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004451 start = compiler_new_block(c);
4452 skip = compiler_new_block(c);
4453 if_cleanup = compiler_new_block(c);
4454 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004456 if (start == NULL || skip == NULL || if_cleanup == NULL ||
4457 anchor == NULL)
4458 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004460 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004462 if (gen_index == 0) {
4463 /* Receive outermost iter as an implicit argument */
4464 c->u->u_argcount = 1;
4465 ADDOP_I(c, LOAD_FAST, 0);
4466 }
4467 else {
4468 /* Sub-iter - calculate on the fly */
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004469 /* Fast path for the temporary variable assignment idiom:
4470 for y in [f(x)]
4471 */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004472 asdl_expr_seq *elts;
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004473 switch (gen->iter->kind) {
4474 case List_kind:
4475 elts = gen->iter->v.List.elts;
4476 break;
4477 case Tuple_kind:
4478 elts = gen->iter->v.Tuple.elts;
4479 break;
4480 default:
4481 elts = NULL;
4482 }
4483 if (asdl_seq_LEN(elts) == 1) {
4484 expr_ty elt = asdl_seq_GET(elts, 0);
4485 if (elt->kind != Starred_kind) {
4486 VISIT(c, expr, elt);
4487 start = NULL;
4488 }
4489 }
4490 if (start) {
4491 VISIT(c, expr, gen->iter);
4492 ADDOP(c, GET_ITER);
4493 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004494 }
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004495 if (start) {
4496 depth++;
4497 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01004498 ADDOP_JUMP(c, FOR_ITER, anchor);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004499 NEXT_BLOCK(c);
4500 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 /* XXX this needs to be cleaned up...a lot! */
4504 n = asdl_seq_LEN(gen->ifs);
4505 for (i = 0; i < n; i++) {
4506 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004507 if (!compiler_jump_if(c, e, if_cleanup, 0))
4508 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004509 NEXT_BLOCK(c);
4510 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004512 if (++gen_index < asdl_seq_LEN(generators))
4513 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004514 generators, gen_index, depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004515 elt, val, type))
4516 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004518 /* only append after the last for generator */
4519 if (gen_index >= asdl_seq_LEN(generators)) {
4520 /* comprehension specific code */
4521 switch (type) {
4522 case COMP_GENEXP:
4523 VISIT(c, expr, elt);
4524 ADDOP(c, YIELD_VALUE);
4525 ADDOP(c, POP_TOP);
4526 break;
4527 case COMP_LISTCOMP:
4528 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004529 ADDOP_I(c, LIST_APPEND, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004530 break;
4531 case COMP_SETCOMP:
4532 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004533 ADDOP_I(c, SET_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004534 break;
4535 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004536 /* With '{k: v}', k is evaluated before v, so we do
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004537 the same. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004539 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004540 ADDOP_I(c, MAP_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004541 break;
4542 default:
4543 return 0;
4544 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004546 compiler_use_next_block(c, skip);
4547 }
4548 compiler_use_next_block(c, if_cleanup);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004549 if (start) {
Mark Shannon582aaf12020-08-04 17:30:11 +01004550 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004551 compiler_use_next_block(c, anchor);
4552 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553
4554 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004555}
4556
4557static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004558compiler_async_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004559 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004560 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004561 expr_ty elt, expr_ty val, int type)
4562{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004563 comprehension_ty gen;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004564 basicblock *start, *if_cleanup, *except;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004565 Py_ssize_t i, n;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004566 start = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004567 except = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004568 if_cleanup = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004569
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004570 if (start == NULL || if_cleanup == NULL || except == NULL) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004571 return 0;
4572 }
4573
4574 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4575
4576 if (gen_index == 0) {
4577 /* Receive outermost iter as an implicit argument */
4578 c->u->u_argcount = 1;
4579 ADDOP_I(c, LOAD_FAST, 0);
4580 }
4581 else {
4582 /* Sub-iter - calculate on the fly */
4583 VISIT(c, expr, gen->iter);
4584 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004585 }
4586
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004587 compiler_use_next_block(c, start);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004588
Mark Shannon582aaf12020-08-04 17:30:11 +01004589 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004590 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004591 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004592 ADDOP(c, YIELD_FROM);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004593 ADDOP(c, POP_BLOCK);
Serhiy Storchaka24d32012018-03-10 18:22:34 +02004594 VISIT(c, expr, gen->target);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004595
4596 n = asdl_seq_LEN(gen->ifs);
4597 for (i = 0; i < n; i++) {
4598 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004599 if (!compiler_jump_if(c, e, if_cleanup, 0))
4600 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004601 NEXT_BLOCK(c);
4602 }
4603
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004604 depth++;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004605 if (++gen_index < asdl_seq_LEN(generators))
4606 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004607 generators, gen_index, depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004608 elt, val, type))
4609 return 0;
4610
4611 /* only append after the last for generator */
4612 if (gen_index >= asdl_seq_LEN(generators)) {
4613 /* comprehension specific code */
4614 switch (type) {
4615 case COMP_GENEXP:
4616 VISIT(c, expr, elt);
4617 ADDOP(c, YIELD_VALUE);
4618 ADDOP(c, POP_TOP);
4619 break;
4620 case COMP_LISTCOMP:
4621 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004622 ADDOP_I(c, LIST_APPEND, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004623 break;
4624 case COMP_SETCOMP:
4625 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004626 ADDOP_I(c, SET_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004627 break;
4628 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004629 /* With '{k: v}', k is evaluated before v, so we do
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004630 the same. */
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004631 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004632 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004633 ADDOP_I(c, MAP_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004634 break;
4635 default:
4636 return 0;
4637 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004638 }
4639 compiler_use_next_block(c, if_cleanup);
Mark Shannon582aaf12020-08-04 17:30:11 +01004640 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004641
4642 compiler_use_next_block(c, except);
4643 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004644
4645 return 1;
4646}
4647
4648static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004649compiler_comprehension(struct compiler *c, expr_ty e, int type,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004650 identifier name, asdl_comprehension_seq *generators, expr_ty elt,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004651 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004653 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004654 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004655 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004656 int is_async_generator = 0;
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004657 int top_level_await = IS_TOP_LEVEL_AWAIT(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004658
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004659
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004660 int is_async_function = c->u->u_ste->ste_coroutine;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004661
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004662 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004663 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4664 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004665 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004666 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004667 }
4668
4669 is_async_generator = c->u->u_ste->ste_coroutine;
4670
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004671 if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004672 compiler_error(c, "asynchronous comprehension outside of "
4673 "an asynchronous function");
4674 goto error_in_scope;
4675 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004677 if (type != COMP_GENEXP) {
4678 int op;
4679 switch (type) {
4680 case COMP_LISTCOMP:
4681 op = BUILD_LIST;
4682 break;
4683 case COMP_SETCOMP:
4684 op = BUILD_SET;
4685 break;
4686 case COMP_DICTCOMP:
4687 op = BUILD_MAP;
4688 break;
4689 default:
4690 PyErr_Format(PyExc_SystemError,
4691 "unknown comprehension type %d", type);
4692 goto error_in_scope;
4693 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004695 ADDOP_I(c, op, 0);
4696 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004697
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004698 if (!compiler_comprehension_generator(c, generators, 0, 0, elt,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004699 val, type))
4700 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004702 if (type != COMP_GENEXP) {
4703 ADDOP(c, RETURN_VALUE);
4704 }
4705
4706 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004707 qualname = c->u->u_qualname;
4708 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004709 compiler_exit_scope(c);
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004710 if (top_level_await && is_async_generator){
4711 c->u->u_ste->ste_coroutine = 1;
4712 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004713 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004714 goto error;
4715
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004716 if (!compiler_make_closure(c, co, 0, qualname)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004717 goto error;
Victor Stinnerba7a99d2021-01-30 01:46:44 +01004718 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004719 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004720 Py_DECREF(co);
4721
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004722 VISIT(c, expr, outermost->iter);
4723
4724 if (outermost->is_async) {
4725 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004726 } else {
4727 ADDOP(c, GET_ITER);
4728 }
4729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004730 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004731
4732 if (is_async_generator && type != COMP_GENEXP) {
4733 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004734 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004735 ADDOP(c, YIELD_FROM);
4736 }
4737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004738 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004739error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004740 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004741error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004742 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004743 Py_XDECREF(co);
4744 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004745}
4746
4747static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004748compiler_genexp(struct compiler *c, expr_ty e)
4749{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004750 static identifier name;
4751 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004752 name = PyUnicode_InternFromString("<genexpr>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004753 if (!name)
4754 return 0;
4755 }
4756 assert(e->kind == GeneratorExp_kind);
4757 return compiler_comprehension(c, e, COMP_GENEXP, name,
4758 e->v.GeneratorExp.generators,
4759 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004760}
4761
4762static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004763compiler_listcomp(struct compiler *c, expr_ty e)
4764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004765 static identifier name;
4766 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004767 name = PyUnicode_InternFromString("<listcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004768 if (!name)
4769 return 0;
4770 }
4771 assert(e->kind == ListComp_kind);
4772 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4773 e->v.ListComp.generators,
4774 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004775}
4776
4777static int
4778compiler_setcomp(struct compiler *c, expr_ty e)
4779{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004780 static identifier name;
4781 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004782 name = PyUnicode_InternFromString("<setcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004783 if (!name)
4784 return 0;
4785 }
4786 assert(e->kind == SetComp_kind);
4787 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4788 e->v.SetComp.generators,
4789 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004790}
4791
4792
4793static int
4794compiler_dictcomp(struct compiler *c, expr_ty e)
4795{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004796 static identifier name;
4797 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004798 name = PyUnicode_InternFromString("<dictcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004799 if (!name)
4800 return 0;
4801 }
4802 assert(e->kind == DictComp_kind);
4803 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4804 e->v.DictComp.generators,
4805 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004806}
4807
4808
4809static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004810compiler_visit_keyword(struct compiler *c, keyword_ty k)
4811{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004812 VISIT(c, expr, k->value);
4813 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004814}
4815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004816/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004817 whether they are true or false.
4818
4819 Return values: 1 for true, 0 for false, -1 for non-constant.
4820 */
4821
4822static int
Mark Shannonfee55262019-11-21 09:11:43 +00004823compiler_with_except_finish(struct compiler *c) {
4824 basicblock *exit;
4825 exit = compiler_new_block(c);
4826 if (exit == NULL)
4827 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01004828 ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit);
Mark Shannon266b4622020-11-17 19:30:14 +00004829 NEXT_BLOCK(c);
Mark Shannonbf353f32020-12-17 13:55:28 +00004830 ADDOP_I(c, RERAISE, 1);
Mark Shannonfee55262019-11-21 09:11:43 +00004831 compiler_use_next_block(c, exit);
4832 ADDOP(c, POP_TOP);
4833 ADDOP(c, POP_TOP);
4834 ADDOP(c, POP_TOP);
4835 ADDOP(c, POP_EXCEPT);
4836 ADDOP(c, POP_TOP);
4837 return 1;
4838}
Yury Selivanov75445082015-05-11 22:57:16 -04004839
4840/*
4841 Implements the async with statement.
4842
4843 The semantics outlined in that PEP are as follows:
4844
4845 async with EXPR as VAR:
4846 BLOCK
4847
4848 It is implemented roughly as:
4849
4850 context = EXPR
4851 exit = context.__aexit__ # not calling it
4852 value = await context.__aenter__()
4853 try:
4854 VAR = value # if VAR present in the syntax
4855 BLOCK
4856 finally:
4857 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004858 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04004859 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004860 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04004861 if not (await exit(*exc)):
4862 raise
4863 */
4864static int
4865compiler_async_with(struct compiler *c, stmt_ty s, int pos)
4866{
Mark Shannonfee55262019-11-21 09:11:43 +00004867 basicblock *block, *final, *exit;
Yury Selivanov75445082015-05-11 22:57:16 -04004868 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
4869
4870 assert(s->kind == AsyncWith_kind);
Pablo Galindo90235812020-03-15 04:29:22 +00004871 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07004872 c->u->u_ste->ste_coroutine = 1;
4873 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
Zsolt Dollensteine2396502018-04-27 08:58:56 -07004874 return compiler_error(c, "'async with' outside async function");
4875 }
Yury Selivanov75445082015-05-11 22:57:16 -04004876
4877 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004878 final = compiler_new_block(c);
4879 exit = compiler_new_block(c);
4880 if (!block || !final || !exit)
Yury Selivanov75445082015-05-11 22:57:16 -04004881 return 0;
4882
4883 /* Evaluate EXPR */
4884 VISIT(c, expr, item->context_expr);
4885
4886 ADDOP(c, BEFORE_ASYNC_WITH);
4887 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004888 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004889 ADDOP(c, YIELD_FROM);
4890
Mark Shannon582aaf12020-08-04 17:30:11 +01004891 ADDOP_JUMP(c, SETUP_ASYNC_WITH, final);
Yury Selivanov75445082015-05-11 22:57:16 -04004892
4893 /* SETUP_ASYNC_WITH pushes a finally block. */
4894 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004895 if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004896 return 0;
4897 }
4898
4899 if (item->optional_vars) {
4900 VISIT(c, expr, item->optional_vars);
4901 }
4902 else {
4903 /* Discard result from context.__aenter__() */
4904 ADDOP(c, POP_TOP);
4905 }
4906
4907 pos++;
4908 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
4909 /* BLOCK code */
4910 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
4911 else if (!compiler_async_with(c, s, pos))
4912 return 0;
4913
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004914 compiler_pop_fblock(c, ASYNC_WITH, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004915 ADDOP(c, POP_BLOCK);
4916 /* End of body; start the cleanup */
Yury Selivanov75445082015-05-11 22:57:16 -04004917
Mark Shannonfee55262019-11-21 09:11:43 +00004918 /* For successful outcome:
4919 * call __exit__(None, None, None)
4920 */
4921 if(!compiler_call_exit_with_nones(c))
Yury Selivanov75445082015-05-11 22:57:16 -04004922 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00004923 ADDOP(c, GET_AWAITABLE);
4924 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4925 ADDOP(c, YIELD_FROM);
Yury Selivanov75445082015-05-11 22:57:16 -04004926
Mark Shannonfee55262019-11-21 09:11:43 +00004927 ADDOP(c, POP_TOP);
Yury Selivanov75445082015-05-11 22:57:16 -04004928
Mark Shannon582aaf12020-08-04 17:30:11 +01004929 ADDOP_JUMP(c, JUMP_ABSOLUTE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00004930
4931 /* For exceptional outcome: */
4932 compiler_use_next_block(c, final);
4933
4934 ADDOP(c, WITH_EXCEPT_START);
Yury Selivanov75445082015-05-11 22:57:16 -04004935 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004936 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004937 ADDOP(c, YIELD_FROM);
Mark Shannonfee55262019-11-21 09:11:43 +00004938 compiler_with_except_finish(c);
Yury Selivanov75445082015-05-11 22:57:16 -04004939
Mark Shannonfee55262019-11-21 09:11:43 +00004940compiler_use_next_block(c, exit);
Yury Selivanov75445082015-05-11 22:57:16 -04004941 return 1;
4942}
4943
4944
Guido van Rossumc2e20742006-02-27 22:32:47 +00004945/*
4946 Implements the with statement from PEP 343.
Guido van Rossumc2e20742006-02-27 22:32:47 +00004947 with EXPR as VAR:
4948 BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00004949 is implemented as:
4950 <code for EXPR>
4951 SETUP_WITH E
4952 <code to store to VAR> or POP_TOP
4953 <code for BLOCK>
4954 LOAD_CONST (None, None, None)
4955 CALL_FUNCTION_EX 0
4956 JUMP_FORWARD EXIT
4957 E: WITH_EXCEPT_START (calls EXPR.__exit__)
4958 POP_JUMP_IF_TRUE T:
4959 RERAISE
4960 T: POP_TOP * 3 (remove exception from stack)
4961 POP_EXCEPT
4962 POP_TOP
4963 EXIT:
Guido van Rossumc2e20742006-02-27 22:32:47 +00004964 */
Mark Shannonfee55262019-11-21 09:11:43 +00004965
Guido van Rossumc2e20742006-02-27 22:32:47 +00004966static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004967compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004968{
Mark Shannonfee55262019-11-21 09:11:43 +00004969 basicblock *block, *final, *exit;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004970 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004971
4972 assert(s->kind == With_kind);
4973
Guido van Rossumc2e20742006-02-27 22:32:47 +00004974 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004975 final = compiler_new_block(c);
4976 exit = compiler_new_block(c);
4977 if (!block || !final || !exit)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004978 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004979
Thomas Wouters477c8d52006-05-27 19:21:47 +00004980 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004981 VISIT(c, expr, item->context_expr);
Mark Shannonfee55262019-11-21 09:11:43 +00004982 /* Will push bound __exit__ */
Mark Shannon582aaf12020-08-04 17:30:11 +01004983 ADDOP_JUMP(c, SETUP_WITH, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004984
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004985 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00004986 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004987 if (!compiler_push_fblock(c, WITH, block, final, NULL)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004988 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004989 }
4990
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004991 if (item->optional_vars) {
4992 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004993 }
4994 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004995 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004996 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004997 }
4998
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004999 pos++;
5000 if (pos == asdl_seq_LEN(s->v.With.items))
5001 /* BLOCK code */
5002 VISIT_SEQ(c, stmt, s->v.With.body)
5003 else if (!compiler_with(c, s, pos))
5004 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005005
Mark Shannon3bd60352021-01-13 12:05:43 +00005006
5007 /* Mark all following code as artificial */
5008 c->u->u_lineno = -1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00005009 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005010 compiler_pop_fblock(c, WITH, block);
Mark Shannon13bc1392020-01-23 09:25:17 +00005011
Mark Shannonfee55262019-11-21 09:11:43 +00005012 /* End of body; start the cleanup. */
Mark Shannon13bc1392020-01-23 09:25:17 +00005013
Mark Shannonfee55262019-11-21 09:11:43 +00005014 /* For successful outcome:
5015 * call __exit__(None, None, None)
5016 */
5017 if (!compiler_call_exit_with_nones(c))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00005018 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00005019 ADDOP(c, POP_TOP);
Mark Shannon582aaf12020-08-04 17:30:11 +01005020 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005021
Mark Shannonfee55262019-11-21 09:11:43 +00005022 /* For exceptional outcome: */
5023 compiler_use_next_block(c, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005024
Mark Shannonfee55262019-11-21 09:11:43 +00005025 ADDOP(c, WITH_EXCEPT_START);
5026 compiler_with_except_finish(c);
5027
5028 compiler_use_next_block(c, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00005029 return 1;
5030}
5031
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005032static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005033compiler_visit_expr1(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005035 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07005036 case NamedExpr_kind:
5037 VISIT(c, expr, e->v.NamedExpr.value);
5038 ADDOP(c, DUP_TOP);
5039 VISIT(c, expr, e->v.NamedExpr.target);
5040 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005041 case BoolOp_kind:
5042 return compiler_boolop(c, e);
5043 case BinOp_kind:
5044 VISIT(c, expr, e->v.BinOp.left);
5045 VISIT(c, expr, e->v.BinOp.right);
Andy Lester76d58772020-03-10 21:18:12 -05005046 ADDOP(c, binop(e->v.BinOp.op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005047 break;
5048 case UnaryOp_kind:
5049 VISIT(c, expr, e->v.UnaryOp.operand);
5050 ADDOP(c, unaryop(e->v.UnaryOp.op));
5051 break;
5052 case Lambda_kind:
5053 return compiler_lambda(c, e);
5054 case IfExp_kind:
5055 return compiler_ifexp(c, e);
5056 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005057 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005058 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005059 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005060 case GeneratorExp_kind:
5061 return compiler_genexp(c, e);
5062 case ListComp_kind:
5063 return compiler_listcomp(c, e);
5064 case SetComp_kind:
5065 return compiler_setcomp(c, e);
5066 case DictComp_kind:
5067 return compiler_dictcomp(c, e);
5068 case Yield_kind:
5069 if (c->u->u_ste->ste_type != FunctionBlock)
5070 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005071 if (e->v.Yield.value) {
5072 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005073 }
5074 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005075 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005076 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005077 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005078 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005079 case YieldFrom_kind:
5080 if (c->u->u_ste->ste_type != FunctionBlock)
5081 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04005082
5083 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
5084 return compiler_error(c, "'yield from' inside async function");
5085
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005086 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04005087 ADDOP(c, GET_YIELD_FROM_ITER);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005088 ADDOP_LOAD_CONST(c, Py_None);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005089 ADDOP(c, YIELD_FROM);
5090 break;
Yury Selivanov75445082015-05-11 22:57:16 -04005091 case Await_kind:
Pablo Galindo90235812020-03-15 04:29:22 +00005092 if (!IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005093 if (c->u->u_ste->ste_type != FunctionBlock){
5094 return compiler_error(c, "'await' outside function");
5095 }
Yury Selivanov75445082015-05-11 22:57:16 -04005096
Victor Stinner331a6a52019-05-27 16:39:22 +02005097 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005098 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
5099 return compiler_error(c, "'await' outside async function");
5100 }
5101 }
Yury Selivanov75445082015-05-11 22:57:16 -04005102
5103 VISIT(c, expr, e->v.Await.value);
5104 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005105 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005106 ADDOP(c, YIELD_FROM);
5107 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005108 case Compare_kind:
5109 return compiler_compare(c, e);
5110 case Call_kind:
5111 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005112 case Constant_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005113 ADDOP_LOAD_CONST(c, e->v.Constant.value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005114 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005115 case JoinedStr_kind:
5116 return compiler_joined_str(c, e);
5117 case FormattedValue_kind:
5118 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005119 /* The following exprs can be assignment targets. */
5120 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005121 VISIT(c, expr, e->v.Attribute.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005122 switch (e->v.Attribute.ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005123 case Load:
Mark Shannond48848c2021-03-14 18:01:30 +00005124 {
5125 int old_lineno = c->u->u_lineno;
5126 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005127 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005128 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005129 break;
Mark Shannond48848c2021-03-14 18:01:30 +00005130 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005131 case Store:
Mark Shannond48848c2021-03-14 18:01:30 +00005132 if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005133 return 0;
Mark Shannond48848c2021-03-14 18:01:30 +00005134 }
5135 int old_lineno = c->u->u_lineno;
5136 c->u->u_lineno = e->end_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005137 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005138 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005139 break;
5140 case Del:
5141 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
5142 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005143 }
5144 break;
5145 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005146 return compiler_subscript(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005147 case Starred_kind:
5148 switch (e->v.Starred.ctx) {
5149 case Store:
5150 /* In all legitimate cases, the Starred node was already replaced
5151 * by compiler_list/compiler_tuple. XXX: is that okay? */
5152 return compiler_error(c,
5153 "starred assignment target must be in a list or tuple");
5154 default:
5155 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005156 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005157 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005158 break;
5159 case Slice_kind:
5160 return compiler_slice(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005161 case Name_kind:
5162 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
5163 /* child nodes of List and Tuple will have expr_context set */
5164 case List_kind:
5165 return compiler_list(c, e);
5166 case Tuple_kind:
5167 return compiler_tuple(c, e);
Brandt Bucher145bf262021-02-26 14:51:55 -08005168 case MatchAs_kind:
5169 case MatchOr_kind:
5170 // Can only occur in patterns, which are handled elsewhere.
5171 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005172 }
5173 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005174}
5175
5176static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005177compiler_visit_expr(struct compiler *c, expr_ty e)
5178{
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005179 int old_lineno = c->u->u_lineno;
5180 int old_col_offset = c->u->u_col_offset;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005181 SET_LOC(c, e);
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005182 int res = compiler_visit_expr1(c, e);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005183 c->u->u_lineno = old_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005184 c->u->u_col_offset = old_col_offset;
5185 return res;
5186}
5187
5188static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005189compiler_augassign(struct compiler *c, stmt_ty s)
5190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005191 assert(s->kind == AugAssign_kind);
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005192 expr_ty e = s->v.AugAssign.target;
5193
5194 int old_lineno = c->u->u_lineno;
5195 int old_col_offset = c->u->u_col_offset;
5196 SET_LOC(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005198 switch (e->kind) {
5199 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005200 VISIT(c, expr, e->v.Attribute.value);
5201 ADDOP(c, DUP_TOP);
Mark Shannond48848c2021-03-14 18:01:30 +00005202 int old_lineno = c->u->u_lineno;
5203 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005204 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Mark Shannond48848c2021-03-14 18:01:30 +00005205 c->u->u_lineno = old_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005206 break;
5207 case Subscript_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005208 VISIT(c, expr, e->v.Subscript.value);
5209 VISIT(c, expr, e->v.Subscript.slice);
5210 ADDOP(c, DUP_TOP_TWO);
5211 ADDOP(c, BINARY_SUBSCR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005212 break;
5213 case Name_kind:
5214 if (!compiler_nameop(c, e->v.Name.id, Load))
5215 return 0;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005216 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005217 default:
5218 PyErr_Format(PyExc_SystemError,
5219 "invalid node type (%d) for augmented assignment",
5220 e->kind);
5221 return 0;
5222 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005223
5224 c->u->u_lineno = old_lineno;
5225 c->u->u_col_offset = old_col_offset;
5226
5227 VISIT(c, expr, s->v.AugAssign.value);
5228 ADDOP(c, inplace_binop(s->v.AugAssign.op));
5229
5230 SET_LOC(c, e);
5231
5232 switch (e->kind) {
5233 case Attribute_kind:
Mark Shannond48848c2021-03-14 18:01:30 +00005234 c->u->u_lineno = e->end_lineno;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005235 ADDOP(c, ROT_TWO);
5236 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5237 break;
5238 case Subscript_kind:
5239 ADDOP(c, ROT_THREE);
5240 ADDOP(c, STORE_SUBSCR);
5241 break;
5242 case Name_kind:
5243 return compiler_nameop(c, e->v.Name.id, Store);
5244 default:
5245 Py_UNREACHABLE();
5246 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005247 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005248}
5249
5250static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005251check_ann_expr(struct compiler *c, expr_ty e)
5252{
5253 VISIT(c, expr, e);
5254 ADDOP(c, POP_TOP);
5255 return 1;
5256}
5257
5258static int
5259check_annotation(struct compiler *c, stmt_ty s)
5260{
5261 /* Annotations are only evaluated in a module or class. */
5262 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5263 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
5264 return check_ann_expr(c, s->v.AnnAssign.annotation);
5265 }
5266 return 1;
5267}
5268
5269static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005270check_ann_subscr(struct compiler *c, expr_ty e)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005271{
5272 /* We check that everything in a subscript is defined at runtime. */
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005273 switch (e->kind) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005274 case Slice_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005275 if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005276 return 0;
5277 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005278 if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) {
5279 return 0;
5280 }
5281 if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) {
5282 return 0;
5283 }
5284 return 1;
5285 case Tuple_kind: {
5286 /* extended slice */
Pablo Galindoa5634c42020-09-16 19:42:00 +01005287 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005288 Py_ssize_t i, n = asdl_seq_LEN(elts);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005289 for (i = 0; i < n; i++) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005290 if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005291 return 0;
5292 }
5293 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005294 return 1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005295 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005296 default:
5297 return check_ann_expr(c, e);
5298 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005299}
5300
5301static int
5302compiler_annassign(struct compiler *c, stmt_ty s)
5303{
5304 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07005305 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005306
5307 assert(s->kind == AnnAssign_kind);
5308
5309 /* We perform the actual assignment first. */
5310 if (s->v.AnnAssign.value) {
5311 VISIT(c, expr, s->v.AnnAssign.value);
5312 VISIT(c, expr, targ);
5313 }
5314 switch (targ->kind) {
5315 case Name_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005316 if (forbidden_name(c, targ->v.Name.id, Store))
5317 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005318 /* If we have a simple name in a module or class, store annotation. */
5319 if (s->v.AnnAssign.simple &&
5320 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5321 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Batuhan Taskaya044a1042020-10-06 23:03:02 +03005322 VISIT(c, annexpr, s->v.AnnAssign.annotation);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005323 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02005324 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005325 ADDOP_LOAD_CONST_NEW(c, mangled);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005326 ADDOP(c, STORE_SUBSCR);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005327 }
5328 break;
5329 case Attribute_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005330 if (forbidden_name(c, targ->v.Attribute.attr, Store))
5331 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005332 if (!s->v.AnnAssign.value &&
5333 !check_ann_expr(c, targ->v.Attribute.value)) {
5334 return 0;
5335 }
5336 break;
5337 case Subscript_kind:
5338 if (!s->v.AnnAssign.value &&
5339 (!check_ann_expr(c, targ->v.Subscript.value) ||
5340 !check_ann_subscr(c, targ->v.Subscript.slice))) {
5341 return 0;
5342 }
5343 break;
5344 default:
5345 PyErr_Format(PyExc_SystemError,
5346 "invalid node type (%d) for annotated assignment",
5347 targ->kind);
5348 return 0;
5349 }
5350 /* Annotation is evaluated last. */
5351 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
5352 return 0;
5353 }
5354 return 1;
5355}
5356
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005357/* Raises a SyntaxError and returns 0.
5358 If something goes wrong, a different exception may be raised.
5359*/
5360
5361static int
Brandt Bucher145bf262021-02-26 14:51:55 -08005362compiler_error(struct compiler *c, const char *format, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005363{
Brandt Bucher145bf262021-02-26 14:51:55 -08005364 va_list vargs;
5365#ifdef HAVE_STDARG_PROTOTYPES
5366 va_start(vargs, format);
5367#else
5368 va_start(vargs);
5369#endif
5370 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5371 va_end(vargs);
5372 if (msg == NULL) {
5373 return 0;
5374 }
5375 PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
5376 if (loc == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005377 Py_INCREF(Py_None);
5378 loc = Py_None;
5379 }
Brandt Bucher145bf262021-02-26 14:51:55 -08005380 PyObject *args = Py_BuildValue("O(OiiO)", msg, c->c_filename,
5381 c->u->u_lineno, c->u->u_col_offset + 1, loc);
5382 Py_DECREF(msg);
5383 if (args == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005384 goto exit;
Brandt Bucher145bf262021-02-26 14:51:55 -08005385 }
5386 PyErr_SetObject(PyExc_SyntaxError, args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005387 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005388 Py_DECREF(loc);
Brandt Bucher145bf262021-02-26 14:51:55 -08005389 Py_XDECREF(args);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005390 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005391}
5392
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005393/* Emits a SyntaxWarning and returns 1 on success.
5394 If a SyntaxWarning raised as error, replaces it with a SyntaxError
5395 and returns 0.
5396*/
5397static int
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005398compiler_warn(struct compiler *c, const char *format, ...)
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005399{
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005400 va_list vargs;
5401#ifdef HAVE_STDARG_PROTOTYPES
5402 va_start(vargs, format);
5403#else
5404 va_start(vargs);
5405#endif
5406 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5407 va_end(vargs);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005408 if (msg == NULL) {
5409 return 0;
5410 }
5411 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
5412 c->u->u_lineno, NULL, NULL) < 0)
5413 {
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005414 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005415 /* Replace the SyntaxWarning exception with a SyntaxError
5416 to get a more accurate error report */
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005417 PyErr_Clear();
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005418 assert(PyUnicode_AsUTF8(msg) != NULL);
5419 compiler_error(c, PyUnicode_AsUTF8(msg));
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005420 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005421 Py_DECREF(msg);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005422 return 0;
5423 }
5424 Py_DECREF(msg);
5425 return 1;
5426}
5427
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005428static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005429compiler_subscript(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005430{
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005431 expr_context_ty ctx = e->v.Subscript.ctx;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005432 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005433
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005434 if (ctx == Load) {
5435 if (!check_subscripter(c, e->v.Subscript.value)) {
5436 return 0;
5437 }
5438 if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
5439 return 0;
5440 }
5441 }
5442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005443 switch (ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005444 case Load: op = BINARY_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005445 case Store: op = STORE_SUBSCR; break;
5446 case Del: op = DELETE_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005447 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005448 assert(op);
5449 VISIT(c, expr, e->v.Subscript.value);
5450 VISIT(c, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005451 ADDOP(c, op);
5452 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005453}
5454
5455static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005456compiler_slice(struct compiler *c, expr_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005458 int n = 2;
5459 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005461 /* only handles the cases where BUILD_SLICE is emitted */
5462 if (s->v.Slice.lower) {
5463 VISIT(c, expr, s->v.Slice.lower);
5464 }
5465 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005466 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005467 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005469 if (s->v.Slice.upper) {
5470 VISIT(c, expr, s->v.Slice.upper);
5471 }
5472 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005473 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005474 }
5475
5476 if (s->v.Slice.step) {
5477 n++;
5478 VISIT(c, expr, s->v.Slice.step);
5479 }
5480 ADDOP_I(c, BUILD_SLICE, n);
5481 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005482}
5483
Brandt Bucher145bf262021-02-26 14:51:55 -08005484
5485// PEP 634: Structural Pattern Matching
5486
5487// To keep things simple, all compiler_pattern_* routines follow the convention
5488// of replacing TOS (the subject for the given pattern) with either True (match)
5489// or False (no match). We do this even for irrefutable patterns; the idea is
5490// that it's much easier to smooth out any redundant pushing, popping, and
5491// jumping in the peephole optimizer than to detect or predict it here.
5492
5493
5494#define WILDCARD_CHECK(N) \
5495 ((N)->kind == Name_kind && \
5496 _PyUnicode_EqualToASCIIString((N)->v.Name.id, "_"))
5497
5498
5499static int
5500pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc)
5501{
5502 assert(!_PyUnicode_EqualToASCIIString(n, "_"));
5503 // Can't assign to the same name twice:
5504 if (pc->stores == NULL) {
5505 RETURN_IF_FALSE(pc->stores = PySet_New(NULL));
5506 }
5507 else {
5508 int duplicate = PySet_Contains(pc->stores, n);
5509 if (duplicate < 0) {
5510 return 0;
5511 }
5512 if (duplicate) {
5513 const char *e = "multiple assignments to name %R in pattern";
5514 return compiler_error(c, e, n);
5515 }
5516 }
5517 RETURN_IF_FALSE(!PySet_Add(pc->stores, n));
5518 RETURN_IF_FALSE(compiler_nameop(c, n, Store));
5519 return 1;
5520}
5521
5522
5523static int
5524pattern_helper_sequence_unpack(struct compiler *c, asdl_expr_seq *values,
5525 Py_ssize_t star, pattern_context *pc)
5526{
5527 RETURN_IF_FALSE(unpack_helper(c, values));
5528 // We've now got a bunch of new subjects on the stack. If any of them fail
5529 // to match, we need to pop everything else off, then finally push False.
5530 // fails is an array of blocks that correspond to the necessary amount of
5531 // popping for each element:
5532 basicblock **fails;
5533 Py_ssize_t size = asdl_seq_LEN(values);
5534 fails = (basicblock **)PyObject_Malloc(sizeof(basicblock*) * size);
5535 if (fails == NULL) {
5536 PyErr_NoMemory();
5537 return 0;
5538 }
5539 // NOTE: Can't use our nice returning macros anymore: they'll leak memory!
5540 // goto error on error.
5541 for (Py_ssize_t i = 0; i < size; i++) {
5542 fails[i] = compiler_new_block(c);
5543 if (fails[i] == NULL) {
5544 goto error;
5545 }
5546 }
5547 for (Py_ssize_t i = 0; i < size; i++) {
5548 expr_ty value = asdl_seq_GET(values, i);
5549 if (i == star) {
5550 assert(value->kind == Starred_kind);
5551 value = value->v.Starred.value;
5552 }
5553 if (!compiler_pattern_subpattern(c, value, pc) ||
5554 !compiler_addop_j(c, POP_JUMP_IF_FALSE, fails[i]) ||
5555 compiler_next_block(c) == NULL)
5556 {
5557 goto error;
5558 }
5559 }
5560 // Success!
5561 basicblock *end = compiler_new_block(c);
5562 if (end == NULL ||
5563 !compiler_addop_load_const(c, Py_True) ||
5564 !compiler_addop_j(c, JUMP_FORWARD, end))
5565 {
5566 goto error;
5567 }
5568 // This is where we handle failed sub-patterns. For a sequence pattern like
5569 // [a, b, c, d], this will look like:
5570 // fails[0]: POP_TOP
5571 // fails[1]: POP_TOP
5572 // fails[2]: POP_TOP
5573 // fails[3]: LOAD_CONST False
5574 for (Py_ssize_t i = 0; i < size - 1; i++) {
5575 compiler_use_next_block(c, fails[i]);
5576 if (!compiler_addop(c, POP_TOP)) {
5577 goto error;
5578 }
5579 }
5580 compiler_use_next_block(c, fails[size - 1]);
5581 if (!compiler_addop_load_const(c, Py_False)) {
5582 goto error;
5583 }
5584 compiler_use_next_block(c, end);
5585 PyObject_Free(fails);
5586 return 1;
5587error:
5588 PyObject_Free(fails);
5589 return 0;
5590}
5591
5592// Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of
5593// UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a
5594// starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc.
5595static int
5596pattern_helper_sequence_subscr(struct compiler *c, asdl_expr_seq *values,
5597 Py_ssize_t star, pattern_context *pc)
5598{
5599 basicblock *end, *fail_pop_1;
5600 RETURN_IF_FALSE(end = compiler_new_block(c));
5601 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5602 Py_ssize_t size = asdl_seq_LEN(values);
5603 for (Py_ssize_t i = 0; i < size; i++) {
5604 expr_ty value = asdl_seq_GET(values, i);
5605 if (WILDCARD_CHECK(value)) {
5606 continue;
5607 }
5608 if (i == star) {
5609 assert(value->kind == Starred_kind);
5610 assert(WILDCARD_CHECK(value->v.Starred.value));
5611 continue;
5612 }
5613 ADDOP(c, DUP_TOP);
5614 if (i < star) {
5615 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5616 }
5617 else {
5618 // The subject may not support negative indexing! Compute a
5619 // nonnegative index:
5620 ADDOP(c, GET_LEN);
5621 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i));
5622 ADDOP(c, BINARY_SUBTRACT);
5623 }
5624 ADDOP(c, BINARY_SUBSCR);
5625 RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc));
5626 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5627 NEXT_BLOCK(c);
5628 }
5629 ADDOP(c, POP_TOP);
5630 ADDOP_LOAD_CONST(c, Py_True);
5631 ADDOP_JUMP(c, JUMP_FORWARD, end);
5632 compiler_use_next_block(c, fail_pop_1);
5633 ADDOP(c, POP_TOP);
5634 ADDOP_LOAD_CONST(c, Py_False);
5635 compiler_use_next_block(c, end);
5636 return 1;
5637}
5638
5639
5640// Like compiler_pattern, but turn off checks for irrefutability.
5641static int
5642compiler_pattern_subpattern(struct compiler *c, expr_ty p, pattern_context *pc)
5643{
5644 int allow_irrefutable = pc->allow_irrefutable;
5645 pc->allow_irrefutable = 1;
5646 RETURN_IF_FALSE(compiler_pattern(c, p, pc));
5647 pc->allow_irrefutable = allow_irrefutable;
5648 return 1;
5649}
5650
5651
5652static int
5653compiler_pattern_as(struct compiler *c, expr_ty p, pattern_context *pc)
5654{
5655 assert(p->kind == MatchAs_kind);
5656 basicblock *end, *fail_pop_1;
5657 RETURN_IF_FALSE(end = compiler_new_block(c));
5658 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5659 // Need to make a copy for (possibly) storing later:
5660 ADDOP(c, DUP_TOP);
5661 RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc));
5662 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5663 NEXT_BLOCK(c);
5664 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc));
5665 ADDOP_LOAD_CONST(c, Py_True);
5666 ADDOP_JUMP(c, JUMP_FORWARD, end);
5667 compiler_use_next_block(c, fail_pop_1);
5668 // Need to pop that unused copy from before:
5669 ADDOP(c, POP_TOP);
5670 ADDOP_LOAD_CONST(c, Py_False);
5671 compiler_use_next_block(c, end);
5672 return 1;
5673}
5674
5675
5676static int
5677compiler_pattern_capture(struct compiler *c, expr_ty p, pattern_context *pc)
5678{
5679 assert(p->kind == Name_kind);
5680 assert(p->v.Name.ctx == Store);
5681 assert(!WILDCARD_CHECK(p));
5682 if (!pc->allow_irrefutable) {
5683 // Whoops, can't have a name capture here!
5684 const char *e = "name capture %R makes remaining patterns unreachable";
5685 return compiler_error(c, e, p->v.Name.id);
5686 }
5687 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.Name.id, pc));
5688 ADDOP_LOAD_CONST(c, Py_True);
5689 return 1;
5690}
5691
5692
5693static int
5694compiler_pattern_class(struct compiler *c, expr_ty p, pattern_context *pc)
5695{
5696 asdl_expr_seq *args = p->v.Call.args;
5697 asdl_keyword_seq *kwargs = p->v.Call.keywords;
5698 Py_ssize_t nargs = asdl_seq_LEN(args);
5699 Py_ssize_t nkwargs = asdl_seq_LEN(kwargs);
5700 if (INT_MAX < nargs || INT_MAX < nargs + nkwargs - 1) {
5701 const char *e = "too many sub-patterns in class pattern %R";
5702 return compiler_error(c, e, p->v.Call.func);
5703 }
5704 RETURN_IF_FALSE(!validate_keywords(c, kwargs));
5705 basicblock *end, *fail_pop_1;
5706 RETURN_IF_FALSE(end = compiler_new_block(c));
5707 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5708 VISIT(c, expr, p->v.Call.func);
5709 PyObject *kwnames;
5710 RETURN_IF_FALSE(kwnames = PyTuple_New(nkwargs));
5711 Py_ssize_t i;
5712 for (i = 0; i < nkwargs; i++) {
5713 PyObject *name = ((keyword_ty) asdl_seq_GET(kwargs, i))->arg;
5714 Py_INCREF(name);
5715 PyTuple_SET_ITEM(kwnames, i, name);
5716 }
5717 ADDOP_LOAD_CONST_NEW(c, kwnames);
5718 ADDOP_I(c, MATCH_CLASS, nargs);
5719 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5720 NEXT_BLOCK(c);
5721 // TOS is now a tuple of (nargs + nkwargs) attributes.
5722 for (i = 0; i < nargs + nkwargs; i++) {
5723 expr_ty arg;
5724 if (i < nargs) {
5725 // Positional:
5726 arg = asdl_seq_GET(args, i);
5727 }
5728 else {
5729 // Keyword:
5730 arg = ((keyword_ty) asdl_seq_GET(kwargs, i - nargs))->value;
5731 }
5732 if (WILDCARD_CHECK(arg)) {
5733 continue;
5734 }
5735 // Get the i-th attribute, and match it against the i-th pattern:
5736 ADDOP(c, DUP_TOP);
5737 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5738 ADDOP(c, BINARY_SUBSCR);
5739 RETURN_IF_FALSE(compiler_pattern_subpattern(c, arg, pc));
5740 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5741 NEXT_BLOCK(c);
5742 }
5743 // Success! Pop the tuple of attributes:
5744 ADDOP(c, POP_TOP);
5745 ADDOP_LOAD_CONST(c, Py_True);
5746 ADDOP_JUMP(c, JUMP_FORWARD, end);
5747 compiler_use_next_block(c, fail_pop_1);
5748 ADDOP(c, POP_TOP);
5749 ADDOP_LOAD_CONST(c, Py_False);
5750 compiler_use_next_block(c, end);
5751 return 1;
5752}
5753
5754
5755static int
5756compiler_pattern_literal(struct compiler *c, expr_ty p, pattern_context *pc)
5757{
5758 assert(p->kind == Constant_kind);
5759 PyObject *v = p->v.Constant.value;
5760 ADDOP_LOAD_CONST(c, v);
5761 // Literal True, False, and None are compared by identity. All others use
5762 // equality:
5763 ADDOP_COMPARE(c, (v == Py_None || PyBool_Check(v)) ? Is : Eq);
5764 return 1;
5765}
5766
5767
5768static int
5769compiler_pattern_mapping(struct compiler *c, expr_ty p, pattern_context *pc)
5770{
5771 basicblock *end, *fail_pop_1, *fail_pop_3;
5772 RETURN_IF_FALSE(end = compiler_new_block(c));
5773 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5774 RETURN_IF_FALSE(fail_pop_3 = compiler_new_block(c));
5775 asdl_expr_seq *keys = p->v.Dict.keys;
5776 asdl_expr_seq *values = p->v.Dict.values;
5777 Py_ssize_t size = asdl_seq_LEN(values);
Ikko Ashimine57827f82021-03-10 19:39:51 +09005778 // A starred pattern will be a keyless value. It is guaranteed to be last:
Brandt Bucher145bf262021-02-26 14:51:55 -08005779 int star = size ? !asdl_seq_GET(keys, size - 1) : 0;
5780 ADDOP(c, MATCH_MAPPING);
5781 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5782 NEXT_BLOCK(c);
5783 if (!size) {
5784 // If the pattern is just "{}", we're done!
5785 ADDOP(c, POP_TOP);
5786 ADDOP_LOAD_CONST(c, Py_True);
5787 ADDOP_JUMP(c, JUMP_FORWARD, end);
5788 compiler_use_next_block(c, fail_pop_1);
5789 ADDOP(c, POP_TOP);
5790 ADDOP_LOAD_CONST(c, Py_False);
5791 compiler_use_next_block(c, end);
5792 return 1;
5793 }
5794 if (size - star) {
5795 // If the pattern has any keys in it, perform a length check:
5796 ADDOP(c, GET_LEN);
5797 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - star));
5798 ADDOP_COMPARE(c, GtE);
5799 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5800 NEXT_BLOCK(c);
5801 }
5802 if (INT_MAX < size - star - 1) {
5803 return compiler_error(c, "too many sub-patterns in mapping pattern");
5804 }
5805 // Collect all of the keys into a tuple for MATCH_KEYS and
5806 // COPY_DICT_WITHOUT_KEYS. They can either be dotted names or literals:
5807 for (Py_ssize_t i = 0; i < size - star; i++) {
5808 expr_ty key = asdl_seq_GET(keys, i);
5809 if (key == NULL) {
5810 const char *e = "can't use starred name here "
5811 "(consider moving to end)";
5812 return compiler_error(c, e);
5813 }
5814 VISIT(c, expr, key);
5815 }
5816 ADDOP_I(c, BUILD_TUPLE, size - star);
5817 ADDOP(c, MATCH_KEYS);
5818 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3);
5819 NEXT_BLOCK(c);
5820 // So far so good. There's now a tuple of values on the stack to match
5821 // sub-patterns against:
5822 for (Py_ssize_t i = 0; i < size - star; i++) {
5823 expr_ty value = asdl_seq_GET(values, i);
5824 if (WILDCARD_CHECK(value)) {
5825 continue;
5826 }
5827 ADDOP(c, DUP_TOP);
5828 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5829 ADDOP(c, BINARY_SUBSCR);
5830 RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc));
5831 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3);
5832 NEXT_BLOCK(c);
5833 }
5834 // If we get this far, it's a match! We're done with that tuple of values.
5835 ADDOP(c, POP_TOP);
5836 if (star) {
5837 // If we had a starred name, bind a dict of remaining items to it:
5838 ADDOP(c, COPY_DICT_WITHOUT_KEYS);
5839 PyObject *id = asdl_seq_GET(values, size - 1)->v.Name.id;
5840 RETURN_IF_FALSE(pattern_helper_store_name(c, id, pc));
5841 }
5842 else {
5843 // Otherwise, we don't care about this tuple of keys anymore:
5844 ADDOP(c, POP_TOP);
5845 }
5846 // Pop the subject:
5847 ADDOP(c, POP_TOP);
5848 ADDOP_LOAD_CONST(c, Py_True);
5849 ADDOP_JUMP(c, JUMP_FORWARD, end);
5850 // The top two items are a tuple of values or None, followed by a tuple of
5851 // keys. Pop them both:
5852 compiler_use_next_block(c, fail_pop_3);
5853 ADDOP(c, POP_TOP);
5854 ADDOP(c, POP_TOP);
5855 compiler_use_next_block(c, fail_pop_1);
5856 // Pop the subject:
5857 ADDOP(c, POP_TOP);
5858 ADDOP_LOAD_CONST(c, Py_False);
5859 compiler_use_next_block(c, end);
5860 return 1;
5861}
5862
5863
5864static int
5865compiler_pattern_or(struct compiler *c, expr_ty p, pattern_context *pc)
5866{
5867 assert(p->kind == MatchOr_kind);
5868 // control is the set of names bound by the first alternative. If all of the
5869 // others bind the same names (they should), then this becomes pc->stores.
5870 PyObject *control = NULL;
5871 basicblock *end, *pass_pop_1;
5872 RETURN_IF_FALSE(end = compiler_new_block(c));
5873 RETURN_IF_FALSE(pass_pop_1 = compiler_new_block(c));
5874 Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns);
5875 assert(size > 1);
5876 // We're going to be messing with pc. Keep the original info handy:
5877 PyObject *stores_init = pc->stores;
5878 int allow_irrefutable = pc->allow_irrefutable;
5879 for (Py_ssize_t i = 0; i < size; i++) {
5880 // NOTE: Can't use our nice returning macros in here: they'll leak sets!
5881 expr_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i);
5882 pc->stores = PySet_New(stores_init);
5883 // An irrefutable sub-pattern must be last, if it is allowed at all:
5884 int is_last = i == size - 1;
5885 pc->allow_irrefutable = allow_irrefutable && is_last;
5886 SET_LOC(c, alt);
5887 if (pc->stores == NULL ||
5888 // Only copy the subject if we're *not* on the last alternative:
5889 (!is_last && !compiler_addop(c, DUP_TOP)) ||
5890 !compiler_pattern(c, alt, pc) ||
5891 // Only jump if we're *not* on the last alternative:
5892 (!is_last && !compiler_addop_j(c, POP_JUMP_IF_TRUE, pass_pop_1)) ||
5893 !compiler_next_block(c))
5894 {
5895 goto fail;
5896 }
5897 if (!i) {
5898 // If this is the first alternative, save its stores as a "control"
5899 // for the others (they can't bind a different set of names):
5900 control = pc->stores;
5901 continue;
5902 }
5903 if (PySet_GET_SIZE(pc->stores) || PySet_GET_SIZE(control)) {
5904 // Otherwise, check to see if we differ from the control set:
5905 PyObject *diff = PyNumber_InPlaceXor(pc->stores, control);
5906 if (diff == NULL) {
5907 goto fail;
5908 }
5909 if (PySet_GET_SIZE(diff)) {
5910 // The names differ! Raise.
5911 Py_DECREF(diff);
5912 compiler_error(c, "alternative patterns bind different names");
5913 goto fail;
5914 }
5915 Py_DECREF(diff);
5916 }
5917 Py_DECREF(pc->stores);
5918 }
5919 Py_XDECREF(stores_init);
5920 // Update pc->stores and restore pc->allow_irrefutable:
5921 pc->stores = control;
5922 pc->allow_irrefutable = allow_irrefutable;
5923 ADDOP_JUMP(c, JUMP_FORWARD, end);
5924 compiler_use_next_block(c, pass_pop_1);
5925 ADDOP(c, POP_TOP);
5926 ADDOP_LOAD_CONST(c, Py_True);
5927 compiler_use_next_block(c, end);
5928 return 1;
5929fail:
5930 Py_XDECREF(stores_init);
5931 Py_XDECREF(control);
5932 return 0;
5933}
5934
5935
5936static int
5937compiler_pattern_sequence(struct compiler *c, expr_ty p, pattern_context *pc)
5938{
5939 assert(p->kind == List_kind || p->kind == Tuple_kind);
5940 asdl_expr_seq *values = (p->kind == Tuple_kind) ? p->v.Tuple.elts
5941 : p->v.List.elts;
5942 Py_ssize_t size = asdl_seq_LEN(values);
5943 Py_ssize_t star = -1;
5944 int only_wildcard = 1;
5945 int star_wildcard = 0;
5946 // Find a starred name, if it exists. There may be at most one:
5947 for (Py_ssize_t i = 0; i < size; i++) {
5948 expr_ty value = asdl_seq_GET(values, i);
5949 if (value->kind == Starred_kind) {
5950 value = value->v.Starred.value;
5951 if (star >= 0) {
5952 const char *e = "multiple starred names in sequence pattern";
5953 return compiler_error(c, e);
5954 }
5955 star_wildcard = WILDCARD_CHECK(value);
5956 star = i;
5957 }
5958 only_wildcard &= WILDCARD_CHECK(value);
5959 }
5960 basicblock *end, *fail_pop_1;
5961 RETURN_IF_FALSE(end = compiler_new_block(c));
5962 RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c));
5963 ADDOP(c, MATCH_SEQUENCE);
5964 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5965 NEXT_BLOCK(c);
5966 if (star < 0) {
5967 // No star: len(subject) == size
5968 ADDOP(c, GET_LEN);
5969 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
5970 ADDOP_COMPARE(c, Eq);
5971 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5972 NEXT_BLOCK(c);
5973 }
5974 else if (size > 1) {
5975 // Star: len(subject) >= size - 1
5976 ADDOP(c, GET_LEN);
5977 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1));
5978 ADDOP_COMPARE(c, GtE);
5979 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1);
5980 NEXT_BLOCK(c);
5981 }
5982 if (only_wildcard) {
5983 // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc.
5984 ADDOP(c, POP_TOP);
5985 ADDOP_LOAD_CONST(c, Py_True);
5986 }
5987 else if (star_wildcard) {
5988 RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, values, star, pc));
5989 }
5990 else {
5991 RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, values, star, pc));
5992 }
5993 ADDOP_JUMP(c, JUMP_FORWARD, end);
5994 compiler_use_next_block(c, fail_pop_1);
5995 ADDOP(c, POP_TOP)
5996 ADDOP_LOAD_CONST(c, Py_False);
5997 compiler_use_next_block(c, end);
5998 return 1;
5999}
6000
6001
6002static int
6003compiler_pattern_value(struct compiler *c, expr_ty p, pattern_context *pc)
6004{
6005 assert(p->kind == Attribute_kind);
6006 assert(p->v.Attribute.ctx == Load);
6007 VISIT(c, expr, p);
6008 ADDOP_COMPARE(c, Eq);
6009 return 1;
6010}
6011
6012
6013static int
6014compiler_pattern_wildcard(struct compiler *c, expr_ty p, pattern_context *pc)
6015{
6016 assert(p->kind == Name_kind);
6017 assert(p->v.Name.ctx == Store);
6018 assert(WILDCARD_CHECK(p));
6019 if (!pc->allow_irrefutable) {
6020 // Whoops, can't have a wildcard here!
6021 const char *e = "wildcard makes remaining patterns unreachable";
6022 return compiler_error(c, e);
6023 }
6024 ADDOP(c, POP_TOP);
6025 ADDOP_LOAD_CONST(c, Py_True);
6026 return 1;
6027}
6028
6029
6030static int
6031compiler_pattern(struct compiler *c, expr_ty p, pattern_context *pc)
6032{
6033 SET_LOC(c, p);
6034 switch (p->kind) {
6035 case Attribute_kind:
6036 return compiler_pattern_value(c, p, pc);
6037 case BinOp_kind:
6038 // Because we allow "2+2j", things like "2+2" make it this far:
6039 return compiler_error(c, "patterns cannot include operators");
6040 case Call_kind:
6041 return compiler_pattern_class(c, p, pc);
6042 case Constant_kind:
6043 return compiler_pattern_literal(c, p, pc);
6044 case Dict_kind:
6045 return compiler_pattern_mapping(c, p, pc);
6046 case JoinedStr_kind:
6047 // Because we allow strings, f-strings make it this far:
6048 return compiler_error(c, "patterns cannot include f-strings");
6049 case List_kind:
6050 case Tuple_kind:
6051 return compiler_pattern_sequence(c, p, pc);
6052 case MatchAs_kind:
6053 return compiler_pattern_as(c, p, pc);
6054 case MatchOr_kind:
6055 return compiler_pattern_or(c, p, pc);
6056 case Name_kind:
6057 if (WILDCARD_CHECK(p)) {
6058 return compiler_pattern_wildcard(c, p, pc);
6059 }
6060 return compiler_pattern_capture(c, p, pc);
6061 default:
6062 Py_UNREACHABLE();
6063 }
6064}
6065
6066
6067static int
6068compiler_match(struct compiler *c, stmt_ty s)
6069{
6070 VISIT(c, expr, s->v.Match.subject);
6071 basicblock *next, *end;
6072 RETURN_IF_FALSE(end = compiler_new_block(c));
6073 Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases);
6074 assert(cases);
6075 pattern_context pc;
6076 // We use pc.stores to track:
6077 // - Repeated name assignments in the same pattern.
6078 // - Different name assignments in alternatives.
6079 // It's a set of names, but we don't create it until it's needed:
6080 pc.stores = NULL;
6081 match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6082 int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases;
6083 for (Py_ssize_t i = 0; i < cases - has_default; i++) {
6084 m = asdl_seq_GET(s->v.Match.cases, i);
6085 SET_LOC(c, m->pattern);
6086 RETURN_IF_FALSE(next = compiler_new_block(c));
6087 // If pc.allow_irrefutable is 0, any name captures against our subject
6088 // will raise. Irrefutable cases must be either guarded, last, or both:
6089 pc.allow_irrefutable = m->guard != NULL || i == cases - 1;
6090 // Only copy the subject if we're *not* on the last case:
6091 if (i != cases - has_default - 1) {
6092 ADDOP(c, DUP_TOP);
6093 }
6094 int result = compiler_pattern(c, m->pattern, &pc);
6095 Py_CLEAR(pc.stores);
6096 RETURN_IF_FALSE(result);
6097 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, next);
6098 NEXT_BLOCK(c);
6099 if (m->guard) {
6100 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, next, 0));
6101 }
6102 // Success! Pop the subject off, we're done with it:
6103 if (i != cases - has_default - 1) {
6104 ADDOP(c, POP_TOP);
6105 }
6106 VISIT_SEQ(c, stmt, m->body);
6107 ADDOP_JUMP(c, JUMP_FORWARD, end);
6108 compiler_use_next_block(c, next);
6109 }
6110 if (has_default) {
6111 if (cases == 1) {
6112 // No matches. Done with the subject:
6113 ADDOP(c, POP_TOP);
6114 }
6115 // A trailing "case _" is common, and lets us save a bit of redundant
6116 // pushing and popping in the loop above:
6117 m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6118 SET_LOC(c, m->pattern);
6119 if (m->guard) {
6120 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0));
6121 }
6122 VISIT_SEQ(c, stmt, m->body);
6123 }
6124 compiler_use_next_block(c, end);
6125 return 1;
6126}
6127
6128
6129#undef WILDCARD_CHECK
6130
6131
Thomas Wouters89f507f2006-12-13 04:49:30 +00006132/* End of the compiler section, beginning of the assembler section */
6133
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006134/* do depth-first search of basic block graph, starting with block.
T. Wouters99b54d62019-09-12 07:05:33 -07006135 post records the block indices in post-order.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006136
6137 XXX must handle implicit jumps from one block to next
6138*/
6139
Thomas Wouters89f507f2006-12-13 04:49:30 +00006140struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006141 PyObject *a_bytecode; /* string containing bytecode */
6142 int a_offset; /* offset into bytecode */
6143 int a_nblocks; /* number of reachable blocks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006144 PyObject *a_lnotab; /* string containing lnotab */
6145 int a_lnotab_off; /* offset into lnotab */
Mark Shannon877df852020-11-12 09:43:29 +00006146 int a_prevlineno; /* lineno of last emitted line in line table */
6147 int a_lineno; /* lineno of last emitted instruction */
6148 int a_lineno_start; /* bytecode start offset of current lineno */
Mark Shannoncc75ab72020-11-12 19:49:33 +00006149 basicblock *a_entry;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006150};
6151
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006152Py_LOCAL_INLINE(void)
6153stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006154{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02006155 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
Mark Shannonfee55262019-11-21 09:11:43 +00006156 if (b->b_startdepth < depth && b->b_startdepth < 100) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006157 assert(b->b_startdepth < 0);
6158 b->b_startdepth = depth;
6159 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02006160 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006161}
6162
6163/* Find the flow path that needs the largest stack. We assume that
6164 * cycles in the flow graph have no net effect on the stack depth.
6165 */
6166static int
6167stackdepth(struct compiler *c)
6168{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006169 basicblock *b, *entryblock = NULL;
6170 basicblock **stack, **sp;
6171 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006172 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006173 b->b_startdepth = INT_MIN;
6174 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006175 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006176 }
6177 if (!entryblock)
6178 return 0;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006179 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
6180 if (!stack) {
6181 PyErr_NoMemory();
6182 return -1;
6183 }
6184
6185 sp = stack;
6186 stackdepth_push(&sp, entryblock, 0);
6187 while (sp != stack) {
6188 b = *--sp;
6189 int depth = b->b_startdepth;
6190 assert(depth >= 0);
6191 basicblock *next = b->b_next;
6192 for (int i = 0; i < b->b_iused; i++) {
6193 struct instr *instr = &b->b_instr[i];
6194 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
6195 if (effect == PY_INVALID_STACK_EFFECT) {
Victor Stinnerba7a99d2021-01-30 01:46:44 +01006196 PyErr_Format(PyExc_SystemError,
6197 "compiler stack_effect(opcode=%d, arg=%i) failed",
6198 instr->i_opcode, instr->i_oparg);
6199 return -1;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006200 }
6201 int new_depth = depth + effect;
6202 if (new_depth > maxdepth) {
6203 maxdepth = new_depth;
6204 }
6205 assert(depth >= 0); /* invalid code or bug in stackdepth() */
Mark Shannon582aaf12020-08-04 17:30:11 +01006206 if (is_jump(instr)) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006207 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
6208 assert(effect != PY_INVALID_STACK_EFFECT);
6209 int target_depth = depth + effect;
6210 if (target_depth > maxdepth) {
6211 maxdepth = target_depth;
6212 }
6213 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006214 stackdepth_push(&sp, instr->i_target, target_depth);
6215 }
6216 depth = new_depth;
6217 if (instr->i_opcode == JUMP_ABSOLUTE ||
6218 instr->i_opcode == JUMP_FORWARD ||
6219 instr->i_opcode == RETURN_VALUE ||
Mark Shannonfee55262019-11-21 09:11:43 +00006220 instr->i_opcode == RAISE_VARARGS ||
6221 instr->i_opcode == RERAISE)
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006222 {
6223 /* remaining code is dead */
6224 next = NULL;
6225 break;
6226 }
6227 }
6228 if (next != NULL) {
Mark Shannon266b4622020-11-17 19:30:14 +00006229 assert(b->b_nofallthrough == 0);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006230 stackdepth_push(&sp, next, depth);
6231 }
6232 }
6233 PyObject_Free(stack);
6234 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006235}
6236
6237static int
6238assemble_init(struct assembler *a, int nblocks, int firstlineno)
6239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006240 memset(a, 0, sizeof(struct assembler));
Mark Shannon877df852020-11-12 09:43:29 +00006241 a->a_prevlineno = a->a_lineno = firstlineno;
Mark Shannonfd009e62020-11-13 12:53:53 +00006242 a->a_lnotab = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006243 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006244 if (a->a_bytecode == NULL) {
6245 goto error;
6246 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006247 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Mark Shannonfd009e62020-11-13 12:53:53 +00006248 if (a->a_lnotab == NULL) {
6249 goto error;
6250 }
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07006251 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006252 PyErr_NoMemory();
Mark Shannonfd009e62020-11-13 12:53:53 +00006253 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006254 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006255 return 1;
Mark Shannonfd009e62020-11-13 12:53:53 +00006256error:
6257 Py_XDECREF(a->a_bytecode);
6258 Py_XDECREF(a->a_lnotab);
6259 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006260}
6261
6262static void
6263assemble_free(struct assembler *a)
6264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006265 Py_XDECREF(a->a_bytecode);
6266 Py_XDECREF(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006267}
6268
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006269static int
6270blocksize(basicblock *b)
6271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006272 int i;
6273 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006275 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006276 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006277 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006278}
6279
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006280static int
Mark Shannon877df852020-11-12 09:43:29 +00006281assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006282{
Mark Shannon877df852020-11-12 09:43:29 +00006283 Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006284 if (a->a_lnotab_off + 2 >= len) {
6285 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
6286 return 0;
6287 }
Pablo Galindo86e322f2021-01-30 13:54:22 +00006288 unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab);
6289 lnotab += a->a_lnotab_off;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006290 a->a_lnotab_off += 2;
Mark Shannon877df852020-11-12 09:43:29 +00006291 *lnotab++ = bdelta;
6292 *lnotab++ = ldelta;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006293 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006294}
6295
Mark Shannon877df852020-11-12 09:43:29 +00006296/* Appends a range to the end of the line number table. See
6297 * Objects/lnotab_notes.txt for the description of the line number table. */
6298
6299static int
6300assemble_line_range(struct assembler *a)
6301{
6302 int ldelta, bdelta;
6303 bdelta = (a->a_offset - a->a_lineno_start) * 2;
6304 if (bdelta == 0) {
6305 return 1;
6306 }
6307 if (a->a_lineno < 0) {
6308 ldelta = -128;
6309 }
6310 else {
6311 ldelta = a->a_lineno - a->a_prevlineno;
6312 a->a_prevlineno = a->a_lineno;
6313 while (ldelta > 127) {
6314 if (!assemble_emit_linetable_pair(a, 0, 127)) {
6315 return 0;
6316 }
6317 ldelta -= 127;
6318 }
6319 while (ldelta < -127) {
6320 if (!assemble_emit_linetable_pair(a, 0, -127)) {
6321 return 0;
6322 }
6323 ldelta += 127;
6324 }
6325 }
6326 assert(-128 <= ldelta && ldelta < 128);
6327 while (bdelta > 254) {
6328 if (!assemble_emit_linetable_pair(a, 254, ldelta)) {
6329 return 0;
6330 }
6331 ldelta = a->a_lineno < 0 ? -128 : 0;
6332 bdelta -= 254;
6333 }
6334 if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) {
6335 return 0;
6336 }
6337 a->a_lineno_start = a->a_offset;
6338 return 1;
6339}
6340
6341static int
6342assemble_lnotab(struct assembler *a, struct instr *i)
6343{
6344 if (i->i_lineno == a->a_lineno) {
6345 return 1;
6346 }
6347 if (!assemble_line_range(a)) {
6348 return 0;
6349 }
6350 a->a_lineno = i->i_lineno;
6351 return 1;
6352}
6353
6354
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006355/* assemble_emit()
6356 Extend the bytecode with a new instruction.
6357 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00006358*/
6359
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006360static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006361assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00006362{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006363 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006364 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03006365 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006366
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006367 arg = i->i_oparg;
6368 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006369 if (i->i_lineno && !assemble_lnotab(a, i))
6370 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006371 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006372 if (len > PY_SSIZE_T_MAX / 2)
6373 return 0;
6374 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
6375 return 0;
6376 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03006377 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006378 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006379 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006380 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006381}
6382
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00006383static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006384assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00006385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006386 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006387 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006388 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00006389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006390 /* Compute the size of each block and fixup jump args.
6391 Replace block pointer with position in bytecode. */
6392 do {
6393 totsize = 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006394 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006395 bsize = blocksize(b);
6396 b->b_offset = totsize;
6397 totsize += bsize;
6398 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006399 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006400 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6401 bsize = b->b_offset;
6402 for (i = 0; i < b->b_iused; i++) {
6403 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006404 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006405 /* Relative jumps are computed relative to
6406 the instruction pointer after fetching
6407 the jump instruction.
6408 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006409 bsize += isize;
Mark Shannon582aaf12020-08-04 17:30:11 +01006410 if (is_jump(instr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006411 instr->i_oparg = instr->i_target->b_offset;
Mark Shannon582aaf12020-08-04 17:30:11 +01006412 if (is_relative_jump(instr)) {
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006413 instr->i_oparg -= bsize;
6414 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03006415 instr->i_oparg *= sizeof(_Py_CODEUNIT);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006416 if (instrsize(instr->i_oparg) != isize) {
6417 extended_arg_recompile = 1;
6418 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006419 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006420 }
6421 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00006422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006423 /* XXX: This is an awful hack that could hurt performance, but
6424 on the bright side it should work until we come up
6425 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006427 The issue is that in the first loop blocksize() is called
6428 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006429 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006430 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00006431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006432 So we loop until we stop seeing new EXTENDED_ARGs.
6433 The only EXTENDED_ARGs that could be popping up are
6434 ones in jump instructions. So this should converge
6435 fairly quickly.
6436 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006437 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00006438}
6439
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006440static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01006441dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006443 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006444 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006446 tuple = PyTuple_New(size);
6447 if (tuple == NULL)
6448 return NULL;
6449 while (PyDict_Next(dict, &pos, &k, &v)) {
6450 i = PyLong_AS_LONG(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006451 Py_INCREF(k);
6452 assert((i - offset) < size);
6453 assert((i - offset) >= 0);
6454 PyTuple_SET_ITEM(tuple, i - offset, k);
6455 }
6456 return tuple;
6457}
6458
6459static PyObject *
6460consts_dict_keys_inorder(PyObject *dict)
6461{
6462 PyObject *consts, *k, *v;
6463 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
6464
6465 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
6466 if (consts == NULL)
6467 return NULL;
6468 while (PyDict_Next(dict, &pos, &k, &v)) {
6469 i = PyLong_AS_LONG(v);
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +03006470 /* The keys of the dictionary can be tuples wrapping a contant.
6471 * (see compiler_add_o and _PyCode_ConstantKey). In that case
6472 * the object we want is always second. */
6473 if (PyTuple_CheckExact(k)) {
6474 k = PyTuple_GET_ITEM(k, 1);
6475 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006476 Py_INCREF(k);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006477 assert(i < size);
6478 assert(i >= 0);
6479 PyList_SET_ITEM(consts, i, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006480 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006481 return consts;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006482}
6483
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006484static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006485compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006487 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006488 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006489 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04006490 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006491 if (ste->ste_nested)
6492 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07006493 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006494 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07006495 if (!ste->ste_generator && ste->ste_coroutine)
6496 flags |= CO_COROUTINE;
6497 if (ste->ste_generator && ste->ste_coroutine)
6498 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006499 if (ste->ste_varargs)
6500 flags |= CO_VARARGS;
6501 if (ste->ste_varkeywords)
6502 flags |= CO_VARKEYWORDS;
6503 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006505 /* (Only) inherit compilerflags in PyCF_MASK */
6506 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00006507
Pablo Galindo90235812020-03-15 04:29:22 +00006508 if ((IS_TOP_LEVEL_AWAIT(c)) &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07006509 ste->ste_coroutine &&
6510 !ste->ste_generator) {
6511 flags |= CO_COROUTINE;
6512 }
6513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006514 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00006515}
6516
Inada Naokibdb941b2021-02-10 09:20:42 +09006517// Merge *obj* with constant cache.
INADA Naokic2e16072018-11-26 21:23:22 +09006518// Unlike merge_consts_recursive(), this function doesn't work recursively.
6519static int
Inada Naokibdb941b2021-02-10 09:20:42 +09006520merge_const_one(struct compiler *c, PyObject **obj)
INADA Naokic2e16072018-11-26 21:23:22 +09006521{
Inada Naokibdb941b2021-02-10 09:20:42 +09006522 PyObject *key = _PyCode_ConstantKey(*obj);
INADA Naokic2e16072018-11-26 21:23:22 +09006523 if (key == NULL) {
6524 return 0;
6525 }
6526
6527 // t is borrowed reference
6528 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
6529 Py_DECREF(key);
6530 if (t == NULL) {
6531 return 0;
6532 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006533 if (t == key) { // obj is new constant.
INADA Naokic2e16072018-11-26 21:23:22 +09006534 return 1;
6535 }
6536
Inada Naokibdb941b2021-02-10 09:20:42 +09006537 if (PyTuple_CheckExact(t)) {
6538 // t is still borrowed reference
6539 t = PyTuple_GET_ITEM(t, 1);
6540 }
6541
6542 Py_INCREF(t);
6543 Py_DECREF(*obj);
6544 *obj = t;
INADA Naokic2e16072018-11-26 21:23:22 +09006545 return 1;
6546}
6547
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006548static PyCodeObject *
Mark Shannon6e8128f2020-07-30 10:03:00 +01006549makecode(struct compiler *c, struct assembler *a, PyObject *consts)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006551 PyCodeObject *co = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006552 PyObject *names = NULL;
6553 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006554 PyObject *name = NULL;
6555 PyObject *freevars = NULL;
6556 PyObject *cellvars = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006557 Py_ssize_t nlocals;
6558 int nlocals_int;
6559 int flags;
Pablo Galindocd74e662019-06-01 18:08:04 +01006560 int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006562 names = dict_keys_inorder(c->u->u_names, 0);
6563 varnames = dict_keys_inorder(c->u->u_varnames, 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006564 if (!names || !varnames) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006565 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006566 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006567 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
6568 if (!cellvars)
6569 goto error;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006570 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006571 if (!freevars)
6572 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01006573
Inada Naokibdb941b2021-02-10 09:20:42 +09006574 if (!merge_const_one(c, &names) ||
6575 !merge_const_one(c, &varnames) ||
6576 !merge_const_one(c, &cellvars) ||
6577 !merge_const_one(c, &freevars))
INADA Naokic2e16072018-11-26 21:23:22 +09006578 {
6579 goto error;
6580 }
6581
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02006582 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01006583 assert(nlocals < INT_MAX);
6584 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
6585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006586 flags = compute_code_flags(c);
6587 if (flags < 0)
6588 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006589
Mark Shannon6e8128f2020-07-30 10:03:00 +01006590 consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
6591 if (consts == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006592 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006593 }
Inada Naokibdb941b2021-02-10 09:20:42 +09006594 if (!merge_const_one(c, &consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006595 Py_DECREF(consts);
INADA Naokic2e16072018-11-26 21:23:22 +09006596 goto error;
6597 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006598
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01006599 posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int);
Pablo Galindocd74e662019-06-01 18:08:04 +01006600 posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +01006601 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006602 maxdepth = stackdepth(c);
6603 if (maxdepth < 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006604 Py_DECREF(consts);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02006605 goto error;
6606 }
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006607 co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
Mark Shannon13bc1392020-01-23 09:25:17 +00006608 posonlyargcount, kwonlyargcount, nlocals_int,
Mark Shannon6e8128f2020-07-30 10:03:00 +01006609 maxdepth, flags, a->a_bytecode, consts, names,
Pablo Galindo4a2edc32019-07-01 11:35:05 +01006610 varnames, freevars, cellvars, c->c_filename,
6611 c->u->u_name, c->u->u_firstlineno, a->a_lnotab);
Mark Shannon6e8128f2020-07-30 10:03:00 +01006612 Py_DECREF(consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006613 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006614 Py_XDECREF(names);
6615 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006616 Py_XDECREF(name);
6617 Py_XDECREF(freevars);
6618 Py_XDECREF(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006619 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006620}
6621
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006622
6623/* For debugging purposes only */
6624#if 0
6625static void
6626dump_instr(const struct instr *i)
6627{
Mark Shannon582aaf12020-08-04 17:30:11 +01006628 const char *jrel = (is_relative_jump(instr)) ? "jrel " : "";
6629 const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : "";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006630 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006632 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006633 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006634 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03006635 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006636 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
6637 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006638}
6639
6640static void
6641dump_basicblock(const basicblock *b)
6642{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006643 const char *b_return = b->b_return ? "return " : "";
Pablo Galindo60eb9f12020-06-28 01:55:47 +01006644 fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n",
6645 b->b_iused, b->b_startdepth, b->b_offset, b_return);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006646 if (b->b_instr) {
6647 int i;
6648 for (i = 0; i < b->b_iused; i++) {
6649 fprintf(stderr, " [%02d] ", i);
6650 dump_instr(b->b_instr + i);
6651 }
6652 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006653}
6654#endif
6655
Mark Shannon5977a792020-12-02 13:31:40 +00006656
6657static int
6658normalize_basic_block(basicblock *bb);
6659
Mark Shannon6e8128f2020-07-30 10:03:00 +01006660static int
6661optimize_cfg(struct assembler *a, PyObject *consts);
6662
Mark Shannon5977a792020-12-02 13:31:40 +00006663static int
6664ensure_exits_have_lineno(struct compiler *c);
6665
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006666static PyCodeObject *
6667assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006669 basicblock *b, *entryblock;
6670 struct assembler a;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006671 int j, nblocks;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006672 PyCodeObject *co = NULL;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006673 PyObject *consts = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006675 /* Make sure every block that falls off the end returns None.
6676 XXX NEXT_BLOCK() isn't quite right, because if the last
6677 block ends with a jump or return b_next shouldn't set.
6678 */
6679 if (!c->u->u_curblock->b_return) {
Mark Shannon877df852020-11-12 09:43:29 +00006680 c->u->u_lineno = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006681 if (addNone)
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006682 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006683 ADDOP(c, RETURN_VALUE);
6684 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006685
Mark Shannon5977a792020-12-02 13:31:40 +00006686 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
6687 if (normalize_basic_block(b)) {
Alex Henrie503627f2021-03-02 03:20:25 -07006688 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00006689 }
6690 }
6691
6692 if (ensure_exits_have_lineno(c)) {
Alex Henrie503627f2021-03-02 03:20:25 -07006693 return NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00006694 }
6695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006696 nblocks = 0;
6697 entryblock = NULL;
6698 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6699 nblocks++;
6700 entryblock = b;
6701 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006703 /* Set firstlineno if it wasn't explicitly set. */
6704 if (!c->u->u_firstlineno) {
Ned Deilydc35cda2016-08-17 17:18:33 -04006705 if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006706 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
Mark Shannon877df852020-11-12 09:43:29 +00006707 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006708 c->u->u_firstlineno = 1;
6709 }
Mark Shannon5977a792020-12-02 13:31:40 +00006710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006711 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
6712 goto error;
Mark Shannoncc75ab72020-11-12 19:49:33 +00006713 a.a_entry = entryblock;
6714 a.a_nblocks = nblocks;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006715
Mark Shannon6e8128f2020-07-30 10:03:00 +01006716 consts = consts_dict_keys_inorder(c->u->u_consts);
6717 if (consts == NULL) {
6718 goto error;
6719 }
6720 if (optimize_cfg(&a, consts)) {
6721 goto error;
6722 }
6723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006724 /* Can't modify the bytecode after computing jump offsets. */
6725 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00006726
Mark Shannoncc75ab72020-11-12 19:49:33 +00006727 /* Emit code. */
6728 for(b = entryblock; b != NULL; b = b->b_next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006729 for (j = 0; j < b->b_iused; j++)
6730 if (!assemble_emit(&a, &b->b_instr[j]))
6731 goto error;
6732 }
Mark Shannon877df852020-11-12 09:43:29 +00006733 if (!assemble_line_range(&a)) {
6734 return 0;
6735 }
6736 /* Emit sentinel at end of line number table */
6737 if (!assemble_emit_linetable_pair(&a, 255, -128)) {
6738 goto error;
6739 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00006740
Inada Naokibdb941b2021-02-10 09:20:42 +09006741 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006742 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09006743 }
6744 if (!merge_const_one(c, &a.a_lnotab)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006745 goto error;
Inada Naokibdb941b2021-02-10 09:20:42 +09006746 }
6747 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) {
6748 goto error;
6749 }
6750 if (!merge_const_one(c, &a.a_bytecode)) {
6751 goto error;
6752 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006753
Mark Shannon6e8128f2020-07-30 10:03:00 +01006754 co = makecode(c, &a, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006755 error:
Mark Shannon6e8128f2020-07-30 10:03:00 +01006756 Py_XDECREF(consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006757 assemble_free(&a);
6758 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006759}
Georg Brandl8334fd92010-12-04 10:26:46 +00006760
6761#undef PyAST_Compile
Benjamin Petersone5024512018-09-12 12:06:42 -07006762PyCodeObject *
Georg Brandl8334fd92010-12-04 10:26:46 +00006763PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
6764 PyArena *arena)
6765{
6766 return PyAST_CompileEx(mod, filename, flags, -1, arena);
6767}
Mark Shannon6e8128f2020-07-30 10:03:00 +01006768
6769
6770/* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
6771 with LOAD_CONST (c1, c2, ... cn).
6772 The consts table must still be in list form so that the
6773 new constant (c1, c2, ... cn) can be appended.
6774 Called with codestr pointing to the first LOAD_CONST.
6775*/
6776static int
6777fold_tuple_on_constants(struct instr *inst,
6778 int n, PyObject *consts)
6779{
6780 /* Pre-conditions */
6781 assert(PyList_CheckExact(consts));
6782 assert(inst[n].i_opcode == BUILD_TUPLE);
6783 assert(inst[n].i_oparg == n);
6784
6785 for (int i = 0; i < n; i++) {
6786 if (inst[i].i_opcode != LOAD_CONST) {
6787 return 0;
6788 }
6789 }
6790
6791 /* Buildup new tuple of constants */
6792 PyObject *newconst = PyTuple_New(n);
6793 if (newconst == NULL) {
6794 return -1;
6795 }
6796 for (int i = 0; i < n; i++) {
6797 int arg = inst[i].i_oparg;
6798 PyObject *constant = PyList_GET_ITEM(consts, arg);
6799 Py_INCREF(constant);
6800 PyTuple_SET_ITEM(newconst, i, constant);
6801 }
6802 Py_ssize_t index = PyList_GET_SIZE(consts);
Victor Stinner71f2ff42020-09-23 14:06:55 +02006803 if ((size_t)index >= (size_t)INT_MAX - 1) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006804 Py_DECREF(newconst);
6805 PyErr_SetString(PyExc_OverflowError, "too many constants");
6806 return -1;
6807 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006808 if (PyList_Append(consts, newconst)) {
6809 Py_DECREF(newconst);
6810 return -1;
6811 }
6812 Py_DECREF(newconst);
6813 for (int i = 0; i < n; i++) {
6814 inst[i].i_opcode = NOP;
6815 }
6816 inst[n].i_opcode = LOAD_CONST;
Victor Stinner71f2ff42020-09-23 14:06:55 +02006817 inst[n].i_oparg = (int)index;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006818 return 0;
6819}
6820
Mark Shannon28b75c82020-12-23 11:43:10 +00006821
6822static int
6823eliminate_jump_to_jump(basicblock *bb, int opcode) {
6824 assert (bb->b_iused > 0);
6825 struct instr *inst = &bb->b_instr[bb->b_iused-1];
6826 assert (is_jump(inst));
6827 assert (inst->i_target->b_iused > 0);
6828 struct instr *target = &inst->i_target->b_instr[0];
6829 if (inst->i_target == target->i_target) {
6830 /* Nothing to do */
6831 return 0;
6832 }
6833 int lineno = target->i_lineno;
6834 if (add_jump_to_block(bb, opcode, lineno, target->i_target) == 0) {
6835 return -1;
6836 }
6837 assert (bb->b_iused >= 2);
6838 bb->b_instr[bb->b_iused-2].i_opcode = NOP;
6839 return 0;
6840}
6841
Mark Shannoncc75ab72020-11-12 19:49:33 +00006842/* Maximum size of basic block that should be copied in optimizer */
6843#define MAX_COPY_SIZE 4
Mark Shannon6e8128f2020-07-30 10:03:00 +01006844
6845/* Optimization */
6846static int
6847optimize_basic_block(basicblock *bb, PyObject *consts)
6848{
6849 assert(PyList_CheckExact(consts));
6850 struct instr nop;
6851 nop.i_opcode = NOP;
6852 struct instr *target;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006853 for (int i = 0; i < bb->b_iused; i++) {
6854 struct instr *inst = &bb->b_instr[i];
6855 int oparg = inst->i_oparg;
6856 int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01006857 if (is_jump(inst)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006858 /* Skip over empty basic blocks. */
6859 while (inst->i_target->b_iused == 0) {
6860 inst->i_target = inst->i_target->b_next;
6861 }
6862 target = &inst->i_target->b_instr[0];
6863 }
6864 else {
6865 target = &nop;
6866 }
6867 switch (inst->i_opcode) {
Mark Shannon266b4622020-11-17 19:30:14 +00006868 /* Remove LOAD_CONST const; conditional jump */
Mark Shannon6e8128f2020-07-30 10:03:00 +01006869 case LOAD_CONST:
Mark Shannon266b4622020-11-17 19:30:14 +00006870 {
6871 PyObject* cnt;
6872 int is_true;
6873 int jump_if_true;
6874 switch(nextop) {
6875 case POP_JUMP_IF_FALSE:
6876 case POP_JUMP_IF_TRUE:
6877 cnt = PyList_GET_ITEM(consts, oparg);
6878 is_true = PyObject_IsTrue(cnt);
6879 if (is_true == -1) {
6880 goto error;
6881 }
6882 inst->i_opcode = NOP;
6883 jump_if_true = nextop == POP_JUMP_IF_TRUE;
6884 if (is_true == jump_if_true) {
6885 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
6886 bb->b_nofallthrough = 1;
6887 }
6888 else {
6889 bb->b_instr[i+1].i_opcode = NOP;
6890 }
6891 break;
6892 case JUMP_IF_FALSE_OR_POP:
6893 case JUMP_IF_TRUE_OR_POP:
6894 cnt = PyList_GET_ITEM(consts, oparg);
6895 is_true = PyObject_IsTrue(cnt);
6896 if (is_true == -1) {
6897 goto error;
6898 }
6899 jump_if_true = nextop == JUMP_IF_TRUE_OR_POP;
6900 if (is_true == jump_if_true) {
6901 bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
6902 bb->b_nofallthrough = 1;
6903 }
6904 else {
6905 inst->i_opcode = NOP;
6906 bb->b_instr[i+1].i_opcode = NOP;
6907 }
6908 break;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006909 }
6910 break;
Mark Shannon266b4622020-11-17 19:30:14 +00006911 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006912
6913 /* Try to fold tuples of constants.
6914 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
6915 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
6916 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
6917 case BUILD_TUPLE:
6918 if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
6919 switch(oparg) {
6920 case 1:
6921 inst->i_opcode = NOP;
6922 bb->b_instr[i+1].i_opcode = NOP;
6923 break;
6924 case 2:
6925 inst->i_opcode = ROT_TWO;
6926 bb->b_instr[i+1].i_opcode = NOP;
6927 break;
6928 case 3:
6929 inst->i_opcode = ROT_THREE;
6930 bb->b_instr[i+1].i_opcode = ROT_TWO;
6931 }
6932 break;
6933 }
6934 if (i >= oparg) {
6935 if (fold_tuple_on_constants(inst-oparg, oparg, consts)) {
6936 goto error;
6937 }
6938 }
6939 break;
6940
6941 /* Simplify conditional jump to conditional jump where the
6942 result of the first test implies the success of a similar
6943 test or the failure of the opposite test.
6944 Arises in code like:
6945 "a and b or c"
6946 "(a and b) and c"
6947 "(a or b) or c"
6948 "(a or b) and c"
6949 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
6950 --> x:JUMP_IF_FALSE_OR_POP z
6951 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
6952 --> x:POP_JUMP_IF_FALSE y+1
6953 where y+1 is the instruction following the second test.
6954 */
6955 case JUMP_IF_FALSE_OR_POP:
6956 switch(target->i_opcode) {
6957 case POP_JUMP_IF_FALSE:
Mark Shannon28b75c82020-12-23 11:43:10 +00006958 if (inst->i_lineno == target->i_lineno) {
6959 *inst = *target;
6960 i--;
6961 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006962 break;
6963 case JUMP_ABSOLUTE:
6964 case JUMP_FORWARD:
6965 case JUMP_IF_FALSE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00006966 if (inst->i_lineno == target->i_lineno &&
6967 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00006968 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00006969 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00006970 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006971 break;
6972 case JUMP_IF_TRUE_OR_POP:
6973 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00006974 if (inst->i_lineno == target->i_lineno) {
6975 inst->i_opcode = POP_JUMP_IF_FALSE;
6976 inst->i_target = inst->i_target->b_next;
6977 --i;
6978 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006979 break;
6980 }
6981 break;
6982
6983 case JUMP_IF_TRUE_OR_POP:
6984 switch(target->i_opcode) {
6985 case POP_JUMP_IF_TRUE:
Mark Shannon28b75c82020-12-23 11:43:10 +00006986 if (inst->i_lineno == target->i_lineno) {
6987 *inst = *target;
6988 i--;
6989 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006990 break;
6991 case JUMP_ABSOLUTE:
6992 case JUMP_FORWARD:
6993 case JUMP_IF_TRUE_OR_POP:
Mark Shannon28b75c82020-12-23 11:43:10 +00006994 if (inst->i_lineno == target->i_lineno &&
6995 inst->i_target != target->i_target) {
Mark Shannon266b4622020-11-17 19:30:14 +00006996 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00006997 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00006998 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006999 break;
7000 case JUMP_IF_FALSE_OR_POP:
7001 assert (inst->i_target->b_iused == 1);
Mark Shannon28b75c82020-12-23 11:43:10 +00007002 if (inst->i_lineno == target->i_lineno) {
7003 inst->i_opcode = POP_JUMP_IF_TRUE;
7004 inst->i_target = inst->i_target->b_next;
7005 --i;
7006 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007007 break;
7008 }
7009 break;
7010
7011 case POP_JUMP_IF_FALSE:
7012 switch(target->i_opcode) {
7013 case JUMP_ABSOLUTE:
7014 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007015 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00007016 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007017 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007018 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007019 break;
7020 }
7021 break;
7022
7023 case POP_JUMP_IF_TRUE:
7024 switch(target->i_opcode) {
7025 case JUMP_ABSOLUTE:
7026 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007027 if (inst->i_lineno == target->i_lineno) {
Mark Shannon266b4622020-11-17 19:30:14 +00007028 inst->i_target = target->i_target;
Mark Shannon28b75c82020-12-23 11:43:10 +00007029 i--;
Mark Shannon266b4622020-11-17 19:30:14 +00007030 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007031 break;
7032 }
7033 break;
7034
7035 case JUMP_ABSOLUTE:
7036 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007037 assert (i == bb->b_iused-1);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007038 switch(target->i_opcode) {
7039 case JUMP_FORWARD:
Mark Shannon28b75c82020-12-23 11:43:10 +00007040 if (eliminate_jump_to_jump(bb, inst->i_opcode)) {
7041 goto error;
Mark Shannon266b4622020-11-17 19:30:14 +00007042 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007043 break;
Mark Shannon28b75c82020-12-23 11:43:10 +00007044
Mark Shannon6e8128f2020-07-30 10:03:00 +01007045 case JUMP_ABSOLUTE:
Mark Shannon28b75c82020-12-23 11:43:10 +00007046 if (eliminate_jump_to_jump(bb, JUMP_ABSOLUTE)) {
7047 goto error;
Mark Shannon266b4622020-11-17 19:30:14 +00007048 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007049 break;
Mark Shannon28b75c82020-12-23 11:43:10 +00007050 default:
7051 if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) {
7052 basicblock *to_copy = inst->i_target;
7053 inst->i_opcode = NOP;
7054 for (i = 0; i < to_copy->b_iused; i++) {
7055 int index = compiler_next_instr(bb);
7056 if (index < 0) {
7057 return -1;
7058 }
7059 bb->b_instr[index] = to_copy->b_instr[i];
7060 }
7061 bb->b_exit = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007062 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007063 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007064 }
7065 }
7066 return 0;
7067error:
7068 return -1;
7069}
7070
7071
7072static void
Mark Shannon1659ad12021-01-13 15:05:04 +00007073clean_basic_block(basicblock *bb, int prev_lineno) {
7074 /* Remove NOPs when legal to do so. */
Mark Shannon6e8128f2020-07-30 10:03:00 +01007075 int dest = 0;
7076 for (int src = 0; src < bb->b_iused; src++) {
Mark Shannon877df852020-11-12 09:43:29 +00007077 int lineno = bb->b_instr[src].i_lineno;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007078 if (bb->b_instr[src].i_opcode == NOP) {
Mark Shannon266b4622020-11-17 19:30:14 +00007079 /* Eliminate no-op if it doesn't have a line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007080 if (lineno < 0) {
7081 continue;
7082 }
Mark Shannon266b4622020-11-17 19:30:14 +00007083 /* or, if the previous instruction had the same line number. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007084 if (prev_lineno == lineno) {
7085 continue;
7086 }
Mark Shannon266b4622020-11-17 19:30:14 +00007087 /* or, if the next instruction has same line number or no line number */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007088 if (src < bb->b_iused - 1) {
7089 int next_lineno = bb->b_instr[src+1].i_lineno;
7090 if (next_lineno < 0 || next_lineno == lineno) {
7091 bb->b_instr[src+1].i_lineno = lineno;
7092 continue;
Mark Shannon877df852020-11-12 09:43:29 +00007093 }
7094 }
Mark Shannon266b4622020-11-17 19:30:14 +00007095 else {
7096 basicblock* next = bb->b_next;
7097 while (next && next->b_iused == 0) {
7098 next = next->b_next;
7099 }
7100 /* or if last instruction in BB and next BB has same line number */
7101 if (next) {
7102 if (lineno == next->b_instr[0].i_lineno) {
7103 continue;
7104 }
7105 }
7106 }
7107
Mark Shannon6e8128f2020-07-30 10:03:00 +01007108 }
Mark Shannoncc75ab72020-11-12 19:49:33 +00007109 if (dest != src) {
7110 bb->b_instr[dest] = bb->b_instr[src];
7111 }
7112 dest++;
7113 prev_lineno = lineno;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007114 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01007115 assert(dest <= bb->b_iused);
7116 bb->b_iused = dest;
7117}
7118
Mark Shannon266b4622020-11-17 19:30:14 +00007119static int
7120normalize_basic_block(basicblock *bb) {
7121 /* Mark blocks as exit and/or nofallthrough.
7122 Raise SystemError if CFG is malformed. */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007123 for (int i = 0; i < bb->b_iused; i++) {
7124 switch(bb->b_instr[i].i_opcode) {
7125 case RETURN_VALUE:
7126 case RAISE_VARARGS:
7127 case RERAISE:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007128 bb->b_exit = 1;
Mark Shannon5977a792020-12-02 13:31:40 +00007129 bb->b_nofallthrough = 1;
7130 break;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007131 case JUMP_ABSOLUTE:
7132 case JUMP_FORWARD:
Mark Shannoncc75ab72020-11-12 19:49:33 +00007133 bb->b_nofallthrough = 1;
Mark Shannon266b4622020-11-17 19:30:14 +00007134 /* fall through */
7135 case POP_JUMP_IF_FALSE:
7136 case POP_JUMP_IF_TRUE:
7137 case JUMP_IF_FALSE_OR_POP:
7138 case JUMP_IF_TRUE_OR_POP:
Mark Shannon5977a792020-12-02 13:31:40 +00007139 case FOR_ITER:
Mark Shannon266b4622020-11-17 19:30:14 +00007140 if (i != bb->b_iused-1) {
7141 PyErr_SetString(PyExc_SystemError, "malformed control flow graph.");
7142 return -1;
7143 }
Mark Shannon5977a792020-12-02 13:31:40 +00007144 /* Skip over empty basic blocks. */
7145 while (bb->b_instr[i].i_target->b_iused == 0) {
7146 bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next;
7147 }
7148
Mark Shannoncc75ab72020-11-12 19:49:33 +00007149 }
7150 }
Mark Shannon266b4622020-11-17 19:30:14 +00007151 return 0;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007152}
7153
Mark Shannon6e8128f2020-07-30 10:03:00 +01007154static int
7155mark_reachable(struct assembler *a) {
7156 basicblock **stack, **sp;
7157 sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks);
7158 if (stack == NULL) {
7159 return -1;
7160 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007161 a->a_entry->b_predecessors = 1;
Mark Shannoncc75ab72020-11-12 19:49:33 +00007162 *sp++ = a->a_entry;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007163 while (sp > stack) {
7164 basicblock *b = *(--sp);
Mark Shannon3bd60352021-01-13 12:05:43 +00007165 if (b->b_next && !b->b_nofallthrough) {
7166 if (b->b_next->b_predecessors == 0) {
7167 *sp++ = b->b_next;
7168 }
7169 b->b_next->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007170 }
7171 for (int i = 0; i < b->b_iused; i++) {
7172 basicblock *target;
Mark Shannon582aaf12020-08-04 17:30:11 +01007173 if (is_jump(&b->b_instr[i])) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007174 target = b->b_instr[i].i_target;
Mark Shannon3bd60352021-01-13 12:05:43 +00007175 if (target->b_predecessors == 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007176 *sp++ = target;
7177 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007178 target->b_predecessors++;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007179 }
7180 }
7181 }
7182 PyObject_Free(stack);
7183 return 0;
7184}
7185
Mark Shannon3bd60352021-01-13 12:05:43 +00007186static void
7187eliminate_empty_basic_blocks(basicblock *entry) {
7188 /* Eliminate empty blocks */
7189 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7190 basicblock *next = b->b_next;
7191 if (next) {
7192 while (next->b_iused == 0 && next->b_next) {
7193 next = next->b_next;
7194 }
7195 b->b_next = next;
7196 }
7197 }
7198 for (basicblock *b = entry; b != NULL; b = b->b_next) {
7199 if (b->b_iused == 0) {
7200 continue;
7201 }
7202 if (is_jump(&b->b_instr[b->b_iused-1])) {
7203 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7204 while (target->b_iused == 0) {
7205 target = target->b_next;
7206 }
7207 b->b_instr[b->b_iused-1].i_target = target;
7208 }
7209 }
7210}
7211
7212
Mark Shannon5977a792020-12-02 13:31:40 +00007213/* If an instruction has no line number, but it's predecessor in the BB does,
Mark Shannon3bd60352021-01-13 12:05:43 +00007214 * then copy the line number. If a successor block has no line number, and only
7215 * one predecessor, then inherit the line number.
7216 * This ensures that all exit blocks (with one predecessor) receive a line number.
7217 * Also reduces the size of the line number table,
Mark Shannon5977a792020-12-02 13:31:40 +00007218 * but has no impact on the generated line number events.
7219 */
7220static void
Mark Shannon3bd60352021-01-13 12:05:43 +00007221propogate_line_numbers(struct assembler *a) {
Mark Shannon5977a792020-12-02 13:31:40 +00007222 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007223 if (b->b_iused == 0) {
7224 continue;
7225 }
Mark Shannon5977a792020-12-02 13:31:40 +00007226 int prev_lineno = -1;
7227 for (int i = 0; i < b->b_iused; i++) {
7228 if (b->b_instr[i].i_lineno < 0) {
7229 b->b_instr[i].i_lineno = prev_lineno;
7230 }
7231 else {
7232 prev_lineno = b->b_instr[i].i_lineno;
7233 }
7234 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007235 if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) {
7236 assert(b->b_next->b_iused);
7237 if (b->b_next->b_instr[0].i_lineno < 0) {
7238 b->b_next->b_instr[0].i_lineno = prev_lineno;
7239 }
7240 }
7241 if (is_jump(&b->b_instr[b->b_iused-1])) {
7242 switch (b->b_instr[b->b_iused-1].i_opcode) {
7243 /* Note: Only actual jumps, not exception handlers */
7244 case SETUP_ASYNC_WITH:
7245 case SETUP_WITH:
7246 case SETUP_FINALLY:
7247 continue;
7248 }
7249 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7250 if (target->b_predecessors == 1) {
7251 if (target->b_instr[0].i_lineno < 0) {
7252 target->b_instr[0].i_lineno = prev_lineno;
7253 }
7254 }
7255 }
Mark Shannon5977a792020-12-02 13:31:40 +00007256 }
7257}
7258
7259/* Perform optimizations on a control flow graph.
Mark Shannon6e8128f2020-07-30 10:03:00 +01007260 The consts object should still be in list form to allow new constants
7261 to be appended.
7262
7263 All transformations keep the code size the same or smaller.
7264 For those that reduce size, the gaps are initially filled with
7265 NOPs. Later those NOPs are removed.
7266*/
7267
7268static int
7269optimize_cfg(struct assembler *a, PyObject *consts)
7270{
Mark Shannoncc75ab72020-11-12 19:49:33 +00007271 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannoncc75ab72020-11-12 19:49:33 +00007272 if (optimize_basic_block(b, consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01007273 return -1;
7274 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007275 clean_basic_block(b, -1);
Mark Shannon3bd60352021-01-13 12:05:43 +00007276 assert(b->b_predecessors == 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007277 }
7278 if (mark_reachable(a)) {
7279 return -1;
7280 }
7281 /* Delete unreachable instructions */
Mark Shannoncc75ab72020-11-12 19:49:33 +00007282 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007283 if (b->b_predecessors == 0) {
Mark Shannoncc75ab72020-11-12 19:49:33 +00007284 b->b_iused = 0;
Om Gc71581c2020-12-16 17:48:05 +05307285 b->b_nofallthrough = 0;
Mark Shannon6e8128f2020-07-30 10:03:00 +01007286 }
7287 }
Mark Shannon1659ad12021-01-13 15:05:04 +00007288 basicblock *pred = NULL;
7289 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7290 int prev_lineno = -1;
7291 if (pred && pred->b_iused) {
7292 prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno;
7293 }
7294 clean_basic_block(b, prev_lineno);
7295 pred = b->b_nofallthrough ? NULL : b;
7296 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007297 eliminate_empty_basic_blocks(a->a_entry);
Om Gc71581c2020-12-16 17:48:05 +05307298 /* Delete jump instructions made redundant by previous step. If a non-empty
7299 block ends with a jump instruction, check if the next non-empty block
7300 reached through normal flow control is the target of that jump. If it
7301 is, then the jump instruction is redundant and can be deleted.
7302 */
Mark Shannon3bd60352021-01-13 12:05:43 +00007303 int maybe_empty_blocks = 0;
Om Gc71581c2020-12-16 17:48:05 +05307304 for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7305 if (b->b_iused > 0) {
7306 struct instr *b_last_instr = &b->b_instr[b->b_iused - 1];
Mark Shannon802b6452021-02-02 14:59:15 +00007307 if (b_last_instr->i_opcode == JUMP_ABSOLUTE ||
Om Gc71581c2020-12-16 17:48:05 +05307308 b_last_instr->i_opcode == JUMP_FORWARD) {
Mark Shannon3bd60352021-01-13 12:05:43 +00007309 if (b_last_instr->i_target == b->b_next) {
7310 assert(b->b_next->b_iused);
Om Gc71581c2020-12-16 17:48:05 +05307311 b->b_nofallthrough = 0;
Mark Shannon802b6452021-02-02 14:59:15 +00007312 b_last_instr->i_opcode = NOP;
7313 clean_basic_block(b, -1);
7314 maybe_empty_blocks = 1;
Om Gc71581c2020-12-16 17:48:05 +05307315 }
7316 }
7317 }
7318 }
Mark Shannon3bd60352021-01-13 12:05:43 +00007319 if (maybe_empty_blocks) {
7320 eliminate_empty_basic_blocks(a->a_entry);
7321 }
7322 propogate_line_numbers(a);
Mark Shannon6e8128f2020-07-30 10:03:00 +01007323 return 0;
7324}
7325
Mark Shannon5977a792020-12-02 13:31:40 +00007326static inline int
7327is_exit_without_lineno(basicblock *b) {
7328 return b->b_exit && b->b_instr[0].i_lineno < 0;
7329}
7330
7331/* PEP 626 mandates that the f_lineno of a frame is correct
7332 * after a frame terminates. It would be prohibitively expensive
7333 * to continuously update the f_lineno field at runtime,
7334 * so we make sure that all exiting instruction (raises and returns)
7335 * have a valid line number, allowing us to compute f_lineno lazily.
7336 * We can do this by duplicating the exit blocks without line number
7337 * so that none have more than one predecessor. We can then safely
7338 * copy the line number from the sole predecessor block.
7339 */
7340static int
7341ensure_exits_have_lineno(struct compiler *c)
7342{
Mark Shannoneaccc122020-12-04 15:22:12 +00007343 basicblock *entry = NULL;
Mark Shannon5977a792020-12-02 13:31:40 +00007344 /* Copy all exit blocks without line number that are targets of a jump.
7345 */
7346 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7347 if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) {
7348 switch (b->b_instr[b->b_iused-1].i_opcode) {
7349 /* Note: Only actual jumps, not exception handlers */
7350 case SETUP_ASYNC_WITH:
7351 case SETUP_WITH:
7352 case SETUP_FINALLY:
7353 continue;
7354 }
7355 basicblock *target = b->b_instr[b->b_iused-1].i_target;
7356 if (is_exit_without_lineno(target)) {
7357 basicblock *new_target = compiler_copy_block(c, target);
7358 if (new_target == NULL) {
7359 return -1;
7360 }
7361 new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7362 b->b_instr[b->b_iused-1].i_target = new_target;
7363 }
7364 }
Mark Shannoneaccc122020-12-04 15:22:12 +00007365 entry = b;
7366 }
7367 assert(entry != NULL);
7368 if (is_exit_without_lineno(entry)) {
7369 entry->b_instr[0].i_lineno = c->u->u_firstlineno;
Mark Shannon5977a792020-12-02 13:31:40 +00007370 }
Mark Shannonee9f98d2021-01-05 12:04:10 +00007371 /* Eliminate empty blocks */
7372 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7373 while (b->b_next && b->b_next->b_iused == 0) {
7374 b->b_next = b->b_next->b_next;
7375 }
7376 }
Mark Shannon5977a792020-12-02 13:31:40 +00007377 /* Any remaining reachable exit blocks without line number can only be reached by
7378 * fall through, and thus can only have a single predecessor */
7379 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7380 if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) {
7381 if (is_exit_without_lineno(b->b_next)) {
7382 assert(b->b_next->b_iused > 0);
7383 b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7384 }
7385 }
7386 }
7387 return 0;
7388}
7389
7390
Mark Shannon6e8128f2020-07-30 10:03:00 +01007391/* Retained for API compatibility.
7392 * Optimization is now done in optimize_cfg */
7393
7394PyObject *
7395PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
7396 PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
7397{
7398 Py_INCREF(code);
7399 return code;
7400}