blob: b4f2ceb59a3f10a90a54e9028f2efa7b66329530 [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 Stinnerc9bc2902020-10-27 02:24:34 +010025#include "pycore_long.h" // _PyLong_GetZero()
Guido van Rossum3f5da241990-12-20 15:06:42 +000026
Ammar Askare92d3932020-01-15 11:48:40 -050027#include "Python-ast.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000028#include "ast.h"
29#include "code.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000030#include "symtable.h"
Mark Shannon582aaf12020-08-04 17:30:11 +010031#define NEED_OPCODE_JUMP_TABLES
Guido van Rossum10dc2e81990-11-18 17:27:39 +000032#include "opcode.h"
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030033#include "wordcode_helpers.h"
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 Shannon6e8128f2020-07-30 10:03:00 +010098 unsigned b_reachable : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 /* depth of stack upon entry of block, computed by stackdepth() */
100 int b_startdepth;
101 /* instruction offset for block, computed by assemble_jump_offsets() */
102 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000103} basicblock;
104
105/* fblockinfo tracks the current frame block.
106
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000107A frame block is used to handle loops, try/except, and try/finally.
108It's called a frame block to distinguish it from a basic block in the
109compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000110*/
111
Mark Shannon02d126a2020-09-25 14:04:19 +0100112enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END,
113 WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER };
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000114
115struct fblockinfo {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 enum fblocktype fb_type;
117 basicblock *fb_block;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200118 /* (optional) type-specific exit or cleanup block */
119 basicblock *fb_exit;
Mark Shannonfee55262019-11-21 09:11:43 +0000120 /* (optional) additional information required for unwinding */
121 void *fb_datum;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000122};
123
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100124enum {
125 COMPILER_SCOPE_MODULE,
126 COMPILER_SCOPE_CLASS,
127 COMPILER_SCOPE_FUNCTION,
Yury Selivanov75445082015-05-11 22:57:16 -0400128 COMPILER_SCOPE_ASYNC_FUNCTION,
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400129 COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100130 COMPILER_SCOPE_COMPREHENSION,
131};
132
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000133/* The following items change on entry and exit of code blocks.
134 They must be saved and restored when returning to a block.
135*/
136struct compiler_unit {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 PySTEntryObject *u_ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 PyObject *u_name;
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400140 PyObject *u_qualname; /* dot-separated qualified name (lazy) */
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100141 int u_scope_type;
142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 /* The following fields are dicts that map objects to
144 the index of them in co_XXX. The index is used as
145 the argument for opcodes that refer to those collections.
146 */
147 PyObject *u_consts; /* all constants */
148 PyObject *u_names; /* all names */
149 PyObject *u_varnames; /* local variables */
150 PyObject *u_cellvars; /* cell variables */
151 PyObject *u_freevars; /* free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 PyObject *u_private; /* for private name mangling */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000154
Victor Stinnerf8e32212013-11-19 23:56:34 +0100155 Py_ssize_t u_argcount; /* number of arguments for block */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100156 Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */
Victor Stinnerf8e32212013-11-19 23:56:34 +0100157 Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 /* Pointer to the most recently allocated block. By following b_list
159 members, you can reach all early allocated blocks. */
160 basicblock *u_blocks;
161 basicblock *u_curblock; /* pointer to current block */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 int u_nfblocks;
164 struct fblockinfo u_fblock[CO_MAXBLOCKS];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 int u_firstlineno; /* the first lineno of the block */
167 int u_lineno; /* the lineno for the current stmt */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000168 int u_col_offset; /* the offset of the current stmt */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000169};
170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171/* This struct captures the global state of a compilation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000172
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000173The u pointer points to the current compilation unit, while units
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174for enclosing blocks are stored in c_stack. The u and c_stack are
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000175managed by compiler_enter_scope() and compiler_exit_scope().
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000176
177Note that we don't track recursion levels during compilation - the
178task of detecting and rejecting excessive levels of nesting is
179handled by the symbol analysis pass.
180
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000181*/
182
183struct compiler {
Victor Stinner14e461d2013-08-26 22:28:21 +0200184 PyObject *c_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 struct symtable *c_st;
186 PyFutureFeatures *c_future; /* pointer to module's __future__ */
187 PyCompilerFlags *c_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000188
Georg Brandl8334fd92010-12-04 10:26:46 +0000189 int c_optimize; /* optimization level */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 int c_interactive; /* true if in interactive mode */
191 int c_nestlevel;
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100192 int c_do_not_emit_bytecode; /* The compiler won't emit any bytecode
193 if this value is different from zero.
194 This can be used to temporarily visit
195 nodes without emitting bytecode to
196 check only errors. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000197
INADA Naokic2e16072018-11-26 21:23:22 +0900198 PyObject *c_const_cache; /* Python dict holding all constants,
199 including names tuple */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 struct compiler_unit *u; /* compiler state for current block */
201 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
202 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000203};
204
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100205static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000206static void compiler_free(struct compiler *);
207static basicblock *compiler_new_block(struct compiler *);
Andy Lester76d58772020-03-10 21:18:12 -0500208static int compiler_next_instr(basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000209static int compiler_addop(struct compiler *, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +0100210static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
Mark Shannon582aaf12020-08-04 17:30:11 +0100211static int compiler_addop_j(struct compiler *, int, basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000212static int compiler_error(struct compiler *, const char *);
Serhiy Storchaka62e44812019-02-16 08:12:19 +0200213static int compiler_warn(struct compiler *, const char *, ...);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000214static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
215
216static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
217static int compiler_visit_stmt(struct compiler *, stmt_ty);
218static int compiler_visit_keyword(struct compiler *, keyword_ty);
219static int compiler_visit_expr(struct compiler *, expr_ty);
220static int compiler_augassign(struct compiler *, stmt_ty);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700221static int compiler_annassign(struct compiler *, stmt_ty);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +0200222static int compiler_subscript(struct compiler *, expr_ty);
223static int compiler_slice(struct compiler *, expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000224
Andy Lester76d58772020-03-10 21:18:12 -0500225static int inplace_binop(operator_ty);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100226static int are_all_items_const(asdl_expr_seq *, Py_ssize_t, Py_ssize_t);
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +0200227static int expr_constant(expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000228
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500229static int compiler_with(struct compiler *, stmt_ty, int);
Yury Selivanov75445082015-05-11 22:57:16 -0400230static int compiler_async_with(struct compiler *, stmt_ty, int);
231static int compiler_async_for(struct compiler *, stmt_ty);
Victor Stinner976bb402016-03-23 11:36:19 +0100232static int compiler_call_helper(struct compiler *c, int n,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100233 asdl_expr_seq *args,
234 asdl_keyword_seq *keywords);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500235static int compiler_try_except(struct compiler *, stmt_ty);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400236static int compiler_set_qualname(struct compiler *);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000237
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700238static int compiler_sync_comprehension_generator(
239 struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100240 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +0200241 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700242 expr_ty elt, expr_ty val, int type);
243
244static int compiler_async_comprehension_generator(
245 struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100246 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +0200247 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700248 expr_ty elt, expr_ty val, int type);
249
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000250static PyCodeObject *assemble(struct compiler *, int addNone);
Mark Shannon332cd5e2018-01-30 00:41:04 +0000251static PyObject *__doc__, *__annotations__;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000252
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400253#define CAPSULE_NAME "compile.c compiler unit"
Benjamin Petersonb173f782009-05-05 22:31:58 +0000254
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000255PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000256_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 /* Name mangling: __private becomes _classname__private.
259 This is independent from how the name is used. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200260 PyObject *result;
261 size_t nlen, plen, ipriv;
262 Py_UCS4 maxchar;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200264 PyUnicode_READ_CHAR(ident, 0) != '_' ||
265 PyUnicode_READ_CHAR(ident, 1) != '_') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 Py_INCREF(ident);
267 return ident;
268 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200269 nlen = PyUnicode_GET_LENGTH(ident);
270 plen = PyUnicode_GET_LENGTH(privateobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 /* Don't mangle __id__ or names with dots.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 The only time a name with a dot can occur is when
274 we are compiling an import statement that has a
275 package name.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 TODO(jhylton): Decide whether we want to support
278 mangling of the module name, e.g. __M.X.
279 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200280 if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
281 PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
282 PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 Py_INCREF(ident);
284 return ident; /* Don't mangle __whatever__ */
285 }
286 /* Strip leading underscores from class name */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200287 ipriv = 0;
288 while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
289 ipriv++;
290 if (ipriv == plen) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 Py_INCREF(ident);
292 return ident; /* Don't mangle if class is just underscores */
293 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200294 plen -= ipriv;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000295
Antoine Pitrou55bff892013-04-06 21:21:04 +0200296 if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
297 PyErr_SetString(PyExc_OverflowError,
298 "private identifier too large to be mangled");
299 return NULL;
300 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000301
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200302 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
303 if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
304 maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
305
306 result = PyUnicode_New(1 + nlen + plen, maxchar);
307 if (!result)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200309 /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
310 PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
Victor Stinner6c7a52a2011-09-28 21:39:17 +0200311 if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
312 Py_DECREF(result);
313 return NULL;
314 }
315 if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
316 Py_DECREF(result);
317 return NULL;
318 }
Victor Stinner8f825062012-04-27 13:55:39 +0200319 assert(_PyUnicode_CheckConsistency(result, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200320 return result;
Michael W. Hudson60934622004-08-12 17:56:29 +0000321}
322
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000323static int
324compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000327
INADA Naokic2e16072018-11-26 21:23:22 +0900328 c->c_const_cache = PyDict_New();
329 if (!c->c_const_cache) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 return 0;
INADA Naokic2e16072018-11-26 21:23:22 +0900331 }
332
333 c->c_stack = PyList_New(0);
334 if (!c->c_stack) {
335 Py_CLEAR(c->c_const_cache);
336 return 0;
337 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000340}
341
342PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200343PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
344 int optimize, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 struct compiler c;
347 PyCodeObject *co = NULL;
Victor Stinner37d66d72019-06-13 02:16:41 +0200348 PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 if (!__doc__) {
352 __doc__ = PyUnicode_InternFromString("__doc__");
353 if (!__doc__)
354 return NULL;
355 }
Mark Shannon332cd5e2018-01-30 00:41:04 +0000356 if (!__annotations__) {
357 __annotations__ = PyUnicode_InternFromString("__annotations__");
358 if (!__annotations__)
359 return NULL;
360 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 if (!compiler_init(&c))
362 return NULL;
Victor Stinner14e461d2013-08-26 22:28:21 +0200363 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 c.c_filename = filename;
365 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200366 c.c_future = PyFuture_FromASTObject(mod, filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 if (c.c_future == NULL)
368 goto finally;
369 if (!flags) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 flags = &local_flags;
371 }
372 merged = c.c_future->ff_features | flags->cf_flags;
373 c.c_future->ff_features = merged;
374 flags->cf_flags = merged;
375 c.c_flags = flags;
Victor Stinnerda7933e2020-04-13 03:04:28 +0200376 c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 c.c_nestlevel = 0;
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100378 c.c_do_not_emit_bytecode = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000379
Pablo Galindod112c602020-03-18 23:02:09 +0000380 _PyASTOptimizeState state;
381 state.optimize = c.c_optimize;
382 state.ff_features = merged;
383
384 if (!_PyAST_Optimize(mod, arena, &state)) {
INADA Naoki7ea143a2017-12-14 16:47:20 +0900385 goto finally;
386 }
387
Victor Stinner14e461d2013-08-26 22:28:21 +0200388 c.c_st = PySymtable_BuildObject(mod, filename, c.c_future);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 if (c.c_st == NULL) {
390 if (!PyErr_Occurred())
391 PyErr_SetString(PyExc_SystemError, "no symtable");
392 goto finally;
393 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000396
Thomas Wouters1175c432006-02-27 22:49:54 +0000397 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 compiler_free(&c);
399 assert(co || PyErr_Occurred());
400 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000401}
402
403PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200404PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags,
405 int optimize, PyArena *arena)
406{
407 PyObject *filename;
408 PyCodeObject *co;
409 filename = PyUnicode_DecodeFSDefault(filename_str);
410 if (filename == NULL)
411 return NULL;
412 co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
413 Py_DECREF(filename);
414 return co;
415
416}
417
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000418static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000419compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000420{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 if (c->c_st)
422 PySymtable_Free(c->c_st);
423 if (c->c_future)
424 PyObject_Free(c->c_future);
Victor Stinner14e461d2013-08-26 22:28:21 +0200425 Py_XDECREF(c->c_filename);
INADA Naokic2e16072018-11-26 21:23:22 +0900426 Py_DECREF(c->c_const_cache);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000428}
429
Guido van Rossum79f25d91997-04-29 20:08:16 +0000430static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000431list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 Py_ssize_t i, n;
434 PyObject *v, *k;
435 PyObject *dict = PyDict_New();
436 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 n = PyList_Size(list);
439 for (i = 0; i < n; i++) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100440 v = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 if (!v) {
442 Py_DECREF(dict);
443 return NULL;
444 }
445 k = PyList_GET_ITEM(list, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300446 if (PyDict_SetItem(dict, k, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 Py_DECREF(v);
448 Py_DECREF(dict);
449 return NULL;
450 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 Py_DECREF(v);
452 }
453 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000454}
455
456/* Return new dict containing names from src that match scope(s).
457
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000458src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000460values are integers, starting at offset and increasing by one for
461each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000462*/
463
464static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +0100465dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000466{
Benjamin Peterson51ab2832012-07-18 15:12:47 -0700467 Py_ssize_t i = offset, scope, num_keys, key_i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 PyObject *k, *v, *dest = PyDict_New();
Meador Inge2ca63152012-07-18 14:20:11 -0500469 PyObject *sorted_keys;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 assert(offset >= 0);
472 if (dest == NULL)
473 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000474
Meador Inge2ca63152012-07-18 14:20:11 -0500475 /* Sort the keys so that we have a deterministic order on the indexes
476 saved in the returned dictionary. These indexes are used as indexes
477 into the free and cell var storage. Therefore if they aren't
478 deterministic, then the generated bytecode is not deterministic.
479 */
480 sorted_keys = PyDict_Keys(src);
481 if (sorted_keys == NULL)
482 return NULL;
483 if (PyList_Sort(sorted_keys) != 0) {
484 Py_DECREF(sorted_keys);
485 return NULL;
486 }
Meador Ingef69e24e2012-07-18 16:41:03 -0500487 num_keys = PyList_GET_SIZE(sorted_keys);
Meador Inge2ca63152012-07-18 14:20:11 -0500488
489 for (key_i = 0; key_i < num_keys; key_i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 /* XXX this should probably be a macro in symtable.h */
491 long vi;
Meador Inge2ca63152012-07-18 14:20:11 -0500492 k = PyList_GET_ITEM(sorted_keys, key_i);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200493 v = PyDict_GetItemWithError(src, k);
494 assert(v && PyLong_Check(v));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 vi = PyLong_AS_LONG(v);
496 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 if (scope == scope_type || vi & flag) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300499 PyObject *item = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 if (item == NULL) {
Meador Inge2ca63152012-07-18 14:20:11 -0500501 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 Py_DECREF(dest);
503 return NULL;
504 }
505 i++;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300506 if (PyDict_SetItem(dest, k, item) < 0) {
Meador Inge2ca63152012-07-18 14:20:11 -0500507 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 Py_DECREF(item);
509 Py_DECREF(dest);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 return NULL;
511 }
512 Py_DECREF(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 }
514 }
Meador Inge2ca63152012-07-18 14:20:11 -0500515 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000517}
518
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519static void
520compiler_unit_check(struct compiler_unit *u)
521{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 basicblock *block;
523 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Benjamin Petersonca470632016-09-06 13:47:26 -0700524 assert((uintptr_t)block != 0xcbcbcbcbU);
525 assert((uintptr_t)block != 0xfbfbfbfbU);
526 assert((uintptr_t)block != 0xdbdbdbdbU);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 if (block->b_instr != NULL) {
528 assert(block->b_ialloc > 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +0100529 assert(block->b_iused >= 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 assert(block->b_ialloc >= block->b_iused);
531 }
532 else {
533 assert (block->b_iused == 0);
534 assert (block->b_ialloc == 0);
535 }
536 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000537}
538
539static void
540compiler_unit_free(struct compiler_unit *u)
541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 compiler_unit_check(u);
545 b = u->u_blocks;
546 while (b != NULL) {
547 if (b->b_instr)
548 PyObject_Free((void *)b->b_instr);
549 next = b->b_list;
550 PyObject_Free((void *)b);
551 b = next;
552 }
553 Py_CLEAR(u->u_ste);
554 Py_CLEAR(u->u_name);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400555 Py_CLEAR(u->u_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 Py_CLEAR(u->u_consts);
557 Py_CLEAR(u->u_names);
558 Py_CLEAR(u->u_varnames);
559 Py_CLEAR(u->u_freevars);
560 Py_CLEAR(u->u_cellvars);
561 Py_CLEAR(u->u_private);
562 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563}
564
565static int
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100566compiler_enter_scope(struct compiler *c, identifier name,
567 int scope_type, void *key, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000568{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 struct compiler_unit *u;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100570 basicblock *block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000571
Andy Lester7668a8b2020-03-24 23:26:44 -0500572 u = (struct compiler_unit *)PyObject_Calloc(1, sizeof(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 struct compiler_unit));
574 if (!u) {
575 PyErr_NoMemory();
576 return 0;
577 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100578 u->u_scope_type = scope_type;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 u->u_argcount = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100580 u->u_posonlyargcount = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 u->u_kwonlyargcount = 0;
582 u->u_ste = PySymtable_Lookup(c->c_st, key);
583 if (!u->u_ste) {
584 compiler_unit_free(u);
585 return 0;
586 }
587 Py_INCREF(name);
588 u->u_name = name;
589 u->u_varnames = list2dict(u->u_ste->ste_varnames);
590 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
591 if (!u->u_varnames || !u->u_cellvars) {
592 compiler_unit_free(u);
593 return 0;
594 }
Benjamin Peterson312595c2013-05-15 15:26:42 -0500595 if (u->u_ste->ste_needs_class_closure) {
Martin Panter7462b6492015-11-02 03:37:02 +0000596 /* Cook up an implicit __class__ cell. */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500597 _Py_IDENTIFIER(__class__);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300598 PyObject *name;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500599 int res;
600 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200601 assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500602 name = _PyUnicode_FromId(&PyId___class__);
603 if (!name) {
604 compiler_unit_free(u);
605 return 0;
606 }
Victor Stinnerc9bc2902020-10-27 02:24:34 +0100607 res = PyDict_SetItem(u->u_cellvars, name, _PyLong_GetZero());
Benjamin Peterson312595c2013-05-15 15:26:42 -0500608 if (res < 0) {
609 compiler_unit_free(u);
610 return 0;
611 }
612 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200615 PyDict_GET_SIZE(u->u_cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 if (!u->u_freevars) {
617 compiler_unit_free(u);
618 return 0;
619 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 u->u_blocks = NULL;
622 u->u_nfblocks = 0;
623 u->u_firstlineno = lineno;
624 u->u_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000625 u->u_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 u->u_consts = PyDict_New();
627 if (!u->u_consts) {
628 compiler_unit_free(u);
629 return 0;
630 }
631 u->u_names = PyDict_New();
632 if (!u->u_names) {
633 compiler_unit_free(u);
634 return 0;
635 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 /* Push the old compiler_unit on the stack. */
640 if (c->u) {
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400641 PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
643 Py_XDECREF(capsule);
644 compiler_unit_free(u);
645 return 0;
646 }
647 Py_DECREF(capsule);
648 u->u_private = c->u->u_private;
649 Py_XINCREF(u->u_private);
650 }
651 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 c->c_nestlevel++;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100654
655 block = compiler_new_block(c);
656 if (block == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 return 0;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100658 c->u->u_curblock = block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000659
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400660 if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
661 if (!compiler_set_qualname(c))
662 return 0;
663 }
664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000666}
667
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000668static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000669compiler_exit_scope(struct compiler *c)
670{
Victor Stinnerad9a0662013-11-19 22:23:20 +0100671 Py_ssize_t n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 PyObject *capsule;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 c->c_nestlevel--;
675 compiler_unit_free(c->u);
676 /* Restore c->u to the parent unit. */
677 n = PyList_GET_SIZE(c->c_stack) - 1;
678 if (n >= 0) {
679 capsule = PyList_GET_ITEM(c->c_stack, n);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400680 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 assert(c->u);
682 /* we are deleting from a list so this really shouldn't fail */
683 if (PySequence_DelItem(c->c_stack, n) < 0)
684 Py_FatalError("compiler_exit_scope()");
685 compiler_unit_check(c->u);
686 }
687 else
688 c->u = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000689
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000690}
691
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400692static int
693compiler_set_qualname(struct compiler *c)
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100694{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100695 _Py_static_string(dot, ".");
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400696 _Py_static_string(dot_locals, ".<locals>");
697 Py_ssize_t stack_size;
698 struct compiler_unit *u = c->u;
699 PyObject *name, *base, *dot_str, *dot_locals_str;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100700
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400701 base = NULL;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100702 stack_size = PyList_GET_SIZE(c->c_stack);
Benjamin Petersona8a38b82013-10-19 16:14:39 -0400703 assert(stack_size >= 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400704 if (stack_size > 1) {
705 int scope, force_global = 0;
706 struct compiler_unit *parent;
707 PyObject *mangled, *capsule;
708
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400709 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400710 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400711 assert(parent);
712
Yury Selivanov75445082015-05-11 22:57:16 -0400713 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
714 || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
715 || u->u_scope_type == COMPILER_SCOPE_CLASS) {
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400716 assert(u->u_name);
717 mangled = _Py_Mangle(parent->u_private, u->u_name);
718 if (!mangled)
719 return 0;
720 scope = PyST_GetScope(parent->u_ste, mangled);
721 Py_DECREF(mangled);
722 assert(scope != GLOBAL_IMPLICIT);
723 if (scope == GLOBAL_EXPLICIT)
724 force_global = 1;
725 }
726
727 if (!force_global) {
728 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
Yury Selivanov75445082015-05-11 22:57:16 -0400729 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400730 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
731 dot_locals_str = _PyUnicode_FromId(&dot_locals);
732 if (dot_locals_str == NULL)
733 return 0;
734 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
735 if (base == NULL)
736 return 0;
737 }
738 else {
739 Py_INCREF(parent->u_qualname);
740 base = parent->u_qualname;
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400741 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100742 }
743 }
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400744
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400745 if (base != NULL) {
746 dot_str = _PyUnicode_FromId(&dot);
747 if (dot_str == NULL) {
748 Py_DECREF(base);
749 return 0;
750 }
751 name = PyUnicode_Concat(base, dot_str);
752 Py_DECREF(base);
753 if (name == NULL)
754 return 0;
755 PyUnicode_Append(&name, u->u_name);
756 if (name == NULL)
757 return 0;
758 }
759 else {
760 Py_INCREF(u->u_name);
761 name = u->u_name;
762 }
763 u->u_qualname = name;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100764
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400765 return 1;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100766}
767
Eric V. Smith235a6f02015-09-19 14:51:32 -0400768
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000769/* Allocate a new block and return a pointer to it.
770 Returns NULL on error.
771*/
772
773static basicblock *
774compiler_new_block(struct compiler *c)
775{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 basicblock *b;
777 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 u = c->u;
Andy Lester7668a8b2020-03-24 23:26:44 -0500780 b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 if (b == NULL) {
782 PyErr_NoMemory();
783 return NULL;
784 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 /* Extend the singly linked list of blocks with new block. */
786 b->b_list = u->u_blocks;
787 u->u_blocks = b;
788 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789}
790
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791static basicblock *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792compiler_next_block(struct compiler *c)
793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 basicblock *block = compiler_new_block(c);
795 if (block == NULL)
796 return NULL;
797 c->u->u_curblock->b_next = block;
798 c->u->u_curblock = block;
799 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800}
801
802static basicblock *
803compiler_use_next_block(struct compiler *c, basicblock *block)
804{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 assert(block != NULL);
806 c->u->u_curblock->b_next = block;
807 c->u->u_curblock = block;
808 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000809}
810
811/* Returns the offset of the next instruction in the current block's
812 b_instr array. Resizes the b_instr as necessary.
813 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000814*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815
816static int
Andy Lester76d58772020-03-10 21:18:12 -0500817compiler_next_instr(basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 assert(b != NULL);
820 if (b->b_instr == NULL) {
Andy Lester7668a8b2020-03-24 23:26:44 -0500821 b->b_instr = (struct instr *)PyObject_Calloc(
822 DEFAULT_BLOCK_SIZE, sizeof(struct instr));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 if (b->b_instr == NULL) {
824 PyErr_NoMemory();
825 return -1;
826 }
827 b->b_ialloc = DEFAULT_BLOCK_SIZE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 }
829 else if (b->b_iused == b->b_ialloc) {
830 struct instr *tmp;
831 size_t oldsize, newsize;
832 oldsize = b->b_ialloc * sizeof(struct instr);
833 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000834
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -0700835 if (oldsize > (SIZE_MAX >> 1)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 PyErr_NoMemory();
837 return -1;
838 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 if (newsize == 0) {
841 PyErr_NoMemory();
842 return -1;
843 }
844 b->b_ialloc <<= 1;
845 tmp = (struct instr *)PyObject_Realloc(
846 (void *)b->b_instr, newsize);
847 if (tmp == NULL) {
848 PyErr_NoMemory();
849 return -1;
850 }
851 b->b_instr = tmp;
852 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
853 }
854 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000855}
856
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200857/* Set the line number and column offset for the following instructions.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000858
Christian Heimes2202f872008-02-06 14:31:34 +0000859 The line number is reset in the following cases:
860 - when entering a new scope
861 - on each statement
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200862 - on each expression and sub-expression
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200863 - before the "except" and "finally" clauses
Thomas Wouters89f507f2006-12-13 04:49:30 +0000864*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000865
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200866#define SET_LOC(c, x) \
867 (c)->u->u_lineno = (x)->lineno; \
868 (c)->u->u_col_offset = (x)->col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000869
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200870/* Return the stack effect of opcode with argument oparg.
871
872 Some opcodes have different stack effect when jump to the target and
873 when not jump. The 'jump' parameter specifies the case:
874
875 * 0 -- when not jump
876 * 1 -- when jump
877 * -1 -- maximal
878 */
879/* XXX Make the stack effect of WITH_CLEANUP_START and
880 WITH_CLEANUP_FINISH deterministic. */
881static int
882stack_effect(int opcode, int oparg, int jump)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000883{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 switch (opcode) {
Serhiy Storchaka57faf342018-04-25 22:04:06 +0300885 case NOP:
886 case EXTENDED_ARG:
887 return 0;
888
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200889 /* Stack manipulation */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 case POP_TOP:
891 return -1;
892 case ROT_TWO:
893 case ROT_THREE:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200894 case ROT_FOUR:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 return 0;
896 case DUP_TOP:
897 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000898 case DUP_TOP_TWO:
899 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000900
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200901 /* Unary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 case UNARY_POSITIVE:
903 case UNARY_NEGATIVE:
904 case UNARY_NOT:
905 case UNARY_INVERT:
906 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 case SET_ADD:
909 case LIST_APPEND:
910 return -1;
911 case MAP_ADD:
912 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000913
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200914 /* Binary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 case BINARY_POWER:
916 case BINARY_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400917 case BINARY_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 case BINARY_MODULO:
919 case BINARY_ADD:
920 case BINARY_SUBTRACT:
921 case BINARY_SUBSCR:
922 case BINARY_FLOOR_DIVIDE:
923 case BINARY_TRUE_DIVIDE:
924 return -1;
925 case INPLACE_FLOOR_DIVIDE:
926 case INPLACE_TRUE_DIVIDE:
927 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 case INPLACE_ADD:
930 case INPLACE_SUBTRACT:
931 case INPLACE_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400932 case INPLACE_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 case INPLACE_MODULO:
934 return -1;
935 case STORE_SUBSCR:
936 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 case DELETE_SUBSCR:
938 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 case BINARY_LSHIFT:
941 case BINARY_RSHIFT:
942 case BINARY_AND:
943 case BINARY_XOR:
944 case BINARY_OR:
945 return -1;
946 case INPLACE_POWER:
947 return -1;
948 case GET_ITER:
949 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 case PRINT_EXPR:
952 return -1;
953 case LOAD_BUILD_CLASS:
954 return 1;
955 case INPLACE_LSHIFT:
956 case INPLACE_RSHIFT:
957 case INPLACE_AND:
958 case INPLACE_XOR:
959 case INPLACE_OR:
960 return -1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 case SETUP_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200963 /* 1 in the normal flow.
964 * Restore the stack position and push 6 values before jumping to
965 * the handler if an exception be raised. */
966 return jump ? 6 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 case RETURN_VALUE:
968 return -1;
969 case IMPORT_STAR:
970 return -1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700971 case SETUP_ANNOTATIONS:
972 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 case YIELD_VALUE:
974 return 0;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500975 case YIELD_FROM:
976 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 case POP_BLOCK:
978 return 0;
979 case POP_EXCEPT:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200980 return -3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 case STORE_NAME:
983 return -1;
984 case DELETE_NAME:
985 return 0;
986 case UNPACK_SEQUENCE:
987 return oparg-1;
988 case UNPACK_EX:
989 return (oparg&0xFF) + (oparg>>8);
990 case FOR_ITER:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200991 /* -1 at end of iterator, 1 if continue iterating. */
992 return jump > 0 ? -1 : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 case STORE_ATTR:
995 return -2;
996 case DELETE_ATTR:
997 return -1;
998 case STORE_GLOBAL:
999 return -1;
1000 case DELETE_GLOBAL:
1001 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 case LOAD_CONST:
1003 return 1;
1004 case LOAD_NAME:
1005 return 1;
1006 case BUILD_TUPLE:
1007 case BUILD_LIST:
1008 case BUILD_SET:
Serhiy Storchakaea525a22016-09-06 22:07:53 +03001009 case BUILD_STRING:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 return 1-oparg;
1011 case BUILD_MAP:
Benjamin Petersonb6855152015-09-10 21:02:39 -07001012 return 1 - 2*oparg;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001013 case BUILD_CONST_KEY_MAP:
1014 return -oparg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 case LOAD_ATTR:
1016 return 0;
1017 case COMPARE_OP:
Mark Shannon9af0e472020-01-14 10:12:45 +00001018 case IS_OP:
1019 case CONTAINS_OP:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 return -1;
Mark Shannon9af0e472020-01-14 10:12:45 +00001021 case JUMP_IF_NOT_EXC_MATCH:
1022 return -2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 case IMPORT_NAME:
1024 return -1;
1025 case IMPORT_FROM:
1026 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001027
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001028 /* Jumps */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 case JUMP_FORWARD:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 case JUMP_ABSOLUTE:
1031 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001033 case JUMP_IF_TRUE_OR_POP:
1034 case JUMP_IF_FALSE_OR_POP:
1035 return jump ? 0 : -1;
1036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 case POP_JUMP_IF_FALSE:
1038 case POP_JUMP_IF_TRUE:
1039 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 case LOAD_GLOBAL:
1042 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001044 /* Exception handling */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 case SETUP_FINALLY:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001046 /* 0 in the normal flow.
1047 * Restore the stack position and push 6 values before jumping to
1048 * the handler if an exception be raised. */
1049 return jump ? 6 : 0;
Mark Shannonfee55262019-11-21 09:11:43 +00001050 case RERAISE:
1051 return -3;
1052
1053 case WITH_EXCEPT_START:
1054 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 case LOAD_FAST:
1057 return 1;
1058 case STORE_FAST:
1059 return -1;
1060 case DELETE_FAST:
1061 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 case RAISE_VARARGS:
1064 return -oparg;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001065
1066 /* Functions and calls */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 case CALL_FUNCTION:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001068 return -oparg;
Yury Selivanovf2392132016-12-13 19:03:51 -05001069 case CALL_METHOD:
1070 return -oparg-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 case CALL_FUNCTION_KW:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001072 return -oparg-1;
1073 case CALL_FUNCTION_EX:
Matthieu Dartiailh3a9ac822017-02-21 14:25:22 +01001074 return -1 - ((oparg & 0x01) != 0);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001075 case MAKE_FUNCTION:
1076 return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1077 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 case BUILD_SLICE:
1079 if (oparg == 3)
1080 return -2;
1081 else
1082 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001083
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001084 /* Closures */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 case LOAD_CLOSURE:
1086 return 1;
1087 case LOAD_DEREF:
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001088 case LOAD_CLASSDEREF:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 return 1;
1090 case STORE_DEREF:
1091 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001092 case DELETE_DEREF:
1093 return 0;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001094
1095 /* Iterators and generators */
Yury Selivanov75445082015-05-11 22:57:16 -04001096 case GET_AWAITABLE:
1097 return 0;
1098 case SETUP_ASYNC_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001099 /* 0 in the normal flow.
1100 * Restore the stack position to the position before the result
1101 * of __aenter__ and push 6 values before jumping to the handler
1102 * if an exception be raised. */
1103 return jump ? -1 + 6 : 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001104 case BEFORE_ASYNC_WITH:
1105 return 1;
1106 case GET_AITER:
1107 return 0;
1108 case GET_ANEXT:
1109 return 1;
Yury Selivanov5376ba92015-06-22 12:19:30 -04001110 case GET_YIELD_FROM_ITER:
1111 return 0;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02001112 case END_ASYNC_FOR:
1113 return -7;
Eric V. Smitha78c7952015-11-03 12:45:05 -05001114 case FORMAT_VALUE:
1115 /* If there's a fmt_spec on the stack, we go from 2->1,
1116 else 1->1. */
1117 return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
Yury Selivanovf2392132016-12-13 19:03:51 -05001118 case LOAD_METHOD:
1119 return 1;
Zackery Spytzce6a0702019-08-25 03:44:09 -06001120 case LOAD_ASSERTION_ERROR:
1121 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001122 case LIST_TO_TUPLE:
1123 return 0;
1124 case LIST_EXTEND:
1125 case SET_UPDATE:
Mark Shannon8a4cd702020-01-27 09:57:45 +00001126 case DICT_MERGE:
1127 case DICT_UPDATE:
Mark Shannon13bc1392020-01-23 09:25:17 +00001128 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 default:
Larry Hastings3a907972013-11-23 14:49:22 -08001130 return PY_INVALID_STACK_EFFECT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 }
Larry Hastings3a907972013-11-23 14:49:22 -08001132 return PY_INVALID_STACK_EFFECT; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133}
1134
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001135int
Serhiy Storchaka7bdf2822018-09-18 09:54:26 +03001136PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump)
1137{
1138 return stack_effect(opcode, oparg, jump);
1139}
1140
1141int
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001142PyCompile_OpcodeStackEffect(int opcode, int oparg)
1143{
1144 return stack_effect(opcode, oparg, -1);
1145}
1146
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147/* Add an opcode with no argument.
1148 Returns 0 on failure, 1 on success.
1149*/
1150
1151static int
1152compiler_addop(struct compiler *c, int opcode)
1153{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 basicblock *b;
1155 struct instr *i;
1156 int off;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001157 assert(!HAS_ARG(opcode));
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001158 if (c->c_do_not_emit_bytecode) {
1159 return 1;
1160 }
Andy Lester76d58772020-03-10 21:18:12 -05001161 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 if (off < 0)
1163 return 0;
1164 b = c->u->u_curblock;
1165 i = &b->b_instr[off];
1166 i->i_opcode = opcode;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001167 i->i_oparg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 if (opcode == RETURN_VALUE)
1169 b->b_return = 1;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001170 i->i_lineno = c->u->u_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172}
1173
Victor Stinnerf8e32212013-11-19 23:56:34 +01001174static Py_ssize_t
Andy Lester76d58772020-03-10 21:18:12 -05001175compiler_add_o(PyObject *dict, PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001177 PyObject *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001180 v = PyDict_GetItemWithError(dict, o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 if (!v) {
Stefan Krahc0cbed12015-07-27 12:56:49 +02001182 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 return -1;
Stefan Krahc0cbed12015-07-27 12:56:49 +02001184 }
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001185 arg = PyDict_GET_SIZE(dict);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001186 v = PyLong_FromSsize_t(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 if (!v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 return -1;
1189 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001190 if (PyDict_SetItem(dict, o, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 Py_DECREF(v);
1192 return -1;
1193 }
1194 Py_DECREF(v);
1195 }
1196 else
1197 arg = PyLong_AsLong(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001198 return arg;
1199}
1200
INADA Naokic2e16072018-11-26 21:23:22 +09001201// Merge const *o* recursively and return constant key object.
1202static PyObject*
1203merge_consts_recursive(struct compiler *c, PyObject *o)
1204{
1205 // None and Ellipsis are singleton, and key is the singleton.
1206 // No need to merge object and key.
1207 if (o == Py_None || o == Py_Ellipsis) {
1208 Py_INCREF(o);
1209 return o;
1210 }
1211
1212 PyObject *key = _PyCode_ConstantKey(o);
1213 if (key == NULL) {
1214 return NULL;
1215 }
1216
1217 // t is borrowed reference
1218 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
1219 if (t != key) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001220 // o is registered in c_const_cache. Just use it.
Zackery Spytz9b4a1b12019-03-20 03:16:25 -06001221 Py_XINCREF(t);
INADA Naokic2e16072018-11-26 21:23:22 +09001222 Py_DECREF(key);
1223 return t;
1224 }
1225
INADA Naokif7e4d362018-11-29 00:58:46 +09001226 // We registered o in c_const_cache.
Simeon63b5fc52019-04-09 19:36:57 -04001227 // When o is a tuple or frozenset, we want to merge its
INADA Naokif7e4d362018-11-29 00:58:46 +09001228 // items too.
INADA Naokic2e16072018-11-26 21:23:22 +09001229 if (PyTuple_CheckExact(o)) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001230 Py_ssize_t len = PyTuple_GET_SIZE(o);
1231 for (Py_ssize_t i = 0; i < len; i++) {
INADA Naokic2e16072018-11-26 21:23:22 +09001232 PyObject *item = PyTuple_GET_ITEM(o, i);
1233 PyObject *u = merge_consts_recursive(c, item);
1234 if (u == NULL) {
1235 Py_DECREF(key);
1236 return NULL;
1237 }
1238
1239 // See _PyCode_ConstantKey()
1240 PyObject *v; // borrowed
1241 if (PyTuple_CheckExact(u)) {
1242 v = PyTuple_GET_ITEM(u, 1);
1243 }
1244 else {
1245 v = u;
1246 }
1247 if (v != item) {
1248 Py_INCREF(v);
1249 PyTuple_SET_ITEM(o, i, v);
1250 Py_DECREF(item);
1251 }
1252
1253 Py_DECREF(u);
1254 }
1255 }
INADA Naokif7e4d362018-11-29 00:58:46 +09001256 else if (PyFrozenSet_CheckExact(o)) {
Simeon63b5fc52019-04-09 19:36:57 -04001257 // *key* is tuple. And its first item is frozenset of
INADA Naokif7e4d362018-11-29 00:58:46 +09001258 // constant keys.
1259 // See _PyCode_ConstantKey() for detail.
1260 assert(PyTuple_CheckExact(key));
1261 assert(PyTuple_GET_SIZE(key) == 2);
1262
1263 Py_ssize_t len = PySet_GET_SIZE(o);
1264 if (len == 0) { // empty frozenset should not be re-created.
1265 return key;
1266 }
1267 PyObject *tuple = PyTuple_New(len);
1268 if (tuple == NULL) {
1269 Py_DECREF(key);
1270 return NULL;
1271 }
1272 Py_ssize_t i = 0, pos = 0;
1273 PyObject *item;
1274 Py_hash_t hash;
1275 while (_PySet_NextEntry(o, &pos, &item, &hash)) {
1276 PyObject *k = merge_consts_recursive(c, item);
1277 if (k == NULL) {
1278 Py_DECREF(tuple);
1279 Py_DECREF(key);
1280 return NULL;
1281 }
1282 PyObject *u;
1283 if (PyTuple_CheckExact(k)) {
1284 u = PyTuple_GET_ITEM(k, 1);
1285 Py_INCREF(u);
1286 Py_DECREF(k);
1287 }
1288 else {
1289 u = k;
1290 }
1291 PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u.
1292 i++;
1293 }
1294
1295 // Instead of rewriting o, we create new frozenset and embed in the
1296 // key tuple. Caller should get merged frozenset from the key tuple.
1297 PyObject *new = PyFrozenSet_New(tuple);
1298 Py_DECREF(tuple);
1299 if (new == NULL) {
1300 Py_DECREF(key);
1301 return NULL;
1302 }
1303 assert(PyTuple_GET_ITEM(key, 1) == o);
1304 Py_DECREF(o);
1305 PyTuple_SET_ITEM(key, 1, new);
1306 }
INADA Naokic2e16072018-11-26 21:23:22 +09001307
1308 return key;
1309}
1310
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001311static Py_ssize_t
1312compiler_add_const(struct compiler *c, PyObject *o)
1313{
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001314 if (c->c_do_not_emit_bytecode) {
1315 return 0;
1316 }
1317
INADA Naokic2e16072018-11-26 21:23:22 +09001318 PyObject *key = merge_consts_recursive(c, o);
1319 if (key == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001320 return -1;
INADA Naokic2e16072018-11-26 21:23:22 +09001321 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001322
Andy Lester76d58772020-03-10 21:18:12 -05001323 Py_ssize_t arg = compiler_add_o(c->u->u_consts, key);
INADA Naokic2e16072018-11-26 21:23:22 +09001324 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001326}
1327
1328static int
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001329compiler_addop_load_const(struct compiler *c, PyObject *o)
1330{
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001331 if (c->c_do_not_emit_bytecode) {
1332 return 1;
1333 }
1334
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001335 Py_ssize_t arg = compiler_add_const(c, o);
1336 if (arg < 0)
1337 return 0;
1338 return compiler_addop_i(c, LOAD_CONST, arg);
1339}
1340
1341static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001342compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344{
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001345 if (c->c_do_not_emit_bytecode) {
1346 return 1;
1347 }
1348
Andy Lester76d58772020-03-10 21:18:12 -05001349 Py_ssize_t arg = compiler_add_o(dict, o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001350 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001351 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001352 return compiler_addop_i(c, opcode, arg);
1353}
1354
1355static int
1356compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001358{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001359 Py_ssize_t arg;
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001360
1361 if (c->c_do_not_emit_bytecode) {
1362 return 1;
1363 }
1364
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1366 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001367 return 0;
Andy Lester76d58772020-03-10 21:18:12 -05001368 arg = compiler_add_o(dict, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001369 Py_DECREF(mangled);
1370 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001371 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001372 return compiler_addop_i(c, opcode, arg);
1373}
1374
1375/* Add an opcode with an integer argument.
1376 Returns 0 on failure, 1 on success.
1377*/
1378
1379static int
Victor Stinnerf8e32212013-11-19 23:56:34 +01001380compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 struct instr *i;
1383 int off;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001384
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001385 if (c->c_do_not_emit_bytecode) {
1386 return 1;
1387 }
1388
Victor Stinner2ad474b2016-03-01 23:34:47 +01001389 /* oparg value is unsigned, but a signed C int is usually used to store
1390 it in the C code (like Python/ceval.c).
1391
1392 Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1393
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001394 The argument of a concrete bytecode instruction is limited to 8-bit.
1395 EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1396 assert(HAS_ARG(opcode));
Victor Stinner2ad474b2016-03-01 23:34:47 +01001397 assert(0 <= oparg && oparg <= 2147483647);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001398
Andy Lester76d58772020-03-10 21:18:12 -05001399 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 if (off < 0)
1401 return 0;
1402 i = &c->u->u_curblock->b_instr[off];
Victor Stinnerf8e32212013-11-19 23:56:34 +01001403 i->i_opcode = opcode;
1404 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001405 i->i_lineno = c->u->u_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407}
1408
1409static int
Mark Shannon582aaf12020-08-04 17:30:11 +01001410compiler_addop_j(struct compiler *c, int opcode, basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 struct instr *i;
1413 int off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001414
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001415 if (c->c_do_not_emit_bytecode) {
1416 return 1;
1417 }
1418
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001419 assert(HAS_ARG(opcode));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 assert(b != NULL);
Andy Lester76d58772020-03-10 21:18:12 -05001421 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 if (off < 0)
1423 return 0;
1424 i = &c->u->u_curblock->b_instr[off];
1425 i->i_opcode = opcode;
1426 i->i_target = b;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001427 i->i_lineno = c->u->u_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429}
1430
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +01001431/* NEXT_BLOCK() creates an implicit jump from the current block
1432 to the new block.
1433
1434 The returns inside this macro make it impossible to decref objects
1435 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 if (compiler_next_block((C)) == NULL) \
1439 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440}
1441
1442#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 if (!compiler_addop((C), (OP))) \
1444 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001445}
1446
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001447#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 if (!compiler_addop((C), (OP))) { \
1449 compiler_exit_scope(c); \
1450 return 0; \
1451 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001452}
1453
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001454#define ADDOP_LOAD_CONST(C, O) { \
1455 if (!compiler_addop_load_const((C), (O))) \
1456 return 0; \
1457}
1458
1459/* Same as ADDOP_LOAD_CONST, but steals a reference. */
1460#define ADDOP_LOAD_CONST_NEW(C, O) { \
1461 PyObject *__new_const = (O); \
1462 if (__new_const == NULL) { \
1463 return 0; \
1464 } \
1465 if (!compiler_addop_load_const((C), __new_const)) { \
1466 Py_DECREF(__new_const); \
1467 return 0; \
1468 } \
1469 Py_DECREF(__new_const); \
1470}
1471
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001472#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1474 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001475}
1476
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001477/* Same as ADDOP_O, but steals a reference. */
1478#define ADDOP_N(C, OP, O, TYPE) { \
1479 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1480 Py_DECREF((O)); \
1481 return 0; \
1482 } \
1483 Py_DECREF((O)); \
1484}
1485
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001486#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1488 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489}
1490
1491#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 if (!compiler_addop_i((C), (OP), (O))) \
1493 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494}
1495
Mark Shannon582aaf12020-08-04 17:30:11 +01001496#define ADDOP_JUMP(C, OP, O) { \
1497 if (!compiler_addop_j((C), (OP), (O))) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001499}
1500
Mark Shannon9af0e472020-01-14 10:12:45 +00001501#define ADDOP_COMPARE(C, CMP) { \
1502 if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \
1503 return 0; \
1504}
1505
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1507 the ASDL name to synthesize the name of the C type and the visit function.
1508*/
1509
1510#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 if (!compiler_visit_ ## TYPE((C), (V))) \
1512 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001513}
1514
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001515#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 if (!compiler_visit_ ## TYPE((C), (V))) { \
1517 compiler_exit_scope(c); \
1518 return 0; \
1519 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001520}
1521
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 if (!compiler_visit_slice((C), (V), (CTX))) \
1524 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525}
1526
1527#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001529 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1531 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1532 if (!compiler_visit_ ## TYPE((C), elt)) \
1533 return 0; \
1534 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535}
1536
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001537#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001539 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1541 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1542 if (!compiler_visit_ ## TYPE((C), elt)) { \
1543 compiler_exit_scope(c); \
1544 return 0; \
1545 } \
1546 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001547}
1548
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001549/* These macros allows to check only for errors and not emmit bytecode
1550 * while visiting nodes.
1551*/
1552
1553#define BEGIN_DO_NOT_EMIT_BYTECODE { \
1554 c->c_do_not_emit_bytecode++;
1555
1556#define END_DO_NOT_EMIT_BYTECODE \
1557 c->c_do_not_emit_bytecode--; \
1558}
1559
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001560/* Search if variable annotations are present statically in a block. */
1561
1562static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001563find_ann(asdl_stmt_seq *stmts)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001564{
1565 int i, j, res = 0;
1566 stmt_ty st;
1567
1568 for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1569 st = (stmt_ty)asdl_seq_GET(stmts, i);
1570 switch (st->kind) {
1571 case AnnAssign_kind:
1572 return 1;
1573 case For_kind:
1574 res = find_ann(st->v.For.body) ||
1575 find_ann(st->v.For.orelse);
1576 break;
1577 case AsyncFor_kind:
1578 res = find_ann(st->v.AsyncFor.body) ||
1579 find_ann(st->v.AsyncFor.orelse);
1580 break;
1581 case While_kind:
1582 res = find_ann(st->v.While.body) ||
1583 find_ann(st->v.While.orelse);
1584 break;
1585 case If_kind:
1586 res = find_ann(st->v.If.body) ||
1587 find_ann(st->v.If.orelse);
1588 break;
1589 case With_kind:
1590 res = find_ann(st->v.With.body);
1591 break;
1592 case AsyncWith_kind:
1593 res = find_ann(st->v.AsyncWith.body);
1594 break;
1595 case Try_kind:
1596 for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1597 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1598 st->v.Try.handlers, j);
1599 if (find_ann(handler->v.ExceptHandler.body)) {
1600 return 1;
1601 }
1602 }
1603 res = find_ann(st->v.Try.body) ||
1604 find_ann(st->v.Try.finalbody) ||
1605 find_ann(st->v.Try.orelse);
1606 break;
1607 default:
1608 res = 0;
1609 }
1610 if (res) {
1611 break;
1612 }
1613 }
1614 return res;
1615}
1616
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001617/*
1618 * Frame block handling functions
1619 */
1620
1621static int
1622compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b,
Mark Shannonfee55262019-11-21 09:11:43 +00001623 basicblock *exit, void *datum)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001624{
1625 struct fblockinfo *f;
1626 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
Mark Shannon02d126a2020-09-25 14:04:19 +01001627 return compiler_error(c, "too many statically nested blocks");
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001628 }
1629 f = &c->u->u_fblock[c->u->u_nfblocks++];
1630 f->fb_type = t;
1631 f->fb_block = b;
1632 f->fb_exit = exit;
Mark Shannonfee55262019-11-21 09:11:43 +00001633 f->fb_datum = datum;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001634 return 1;
1635}
1636
1637static void
1638compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
1639{
1640 struct compiler_unit *u = c->u;
1641 assert(u->u_nfblocks > 0);
1642 u->u_nfblocks--;
1643 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
1644 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
1645}
1646
Mark Shannonfee55262019-11-21 09:11:43 +00001647static int
1648compiler_call_exit_with_nones(struct compiler *c) {
1649 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1650 ADDOP(c, DUP_TOP);
1651 ADDOP(c, DUP_TOP);
1652 ADDOP_I(c, CALL_FUNCTION, 3);
1653 return 1;
1654}
1655
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001656/* Unwind a frame block. If preserve_tos is true, the TOS before
Mark Shannonfee55262019-11-21 09:11:43 +00001657 * popping the blocks will be restored afterwards, unless another
1658 * return, break or continue is found. In which case, the TOS will
1659 * be popped.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001660 */
1661static int
1662compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
1663 int preserve_tos)
1664{
1665 switch (info->fb_type) {
1666 case WHILE_LOOP:
Mark Shannon02d126a2020-09-25 14:04:19 +01001667 case EXCEPTION_HANDLER:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001668 return 1;
1669
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001670 case FOR_LOOP:
1671 /* Pop the iterator */
1672 if (preserve_tos) {
1673 ADDOP(c, ROT_TWO);
1674 }
1675 ADDOP(c, POP_TOP);
1676 return 1;
1677
Mark Shannon02d126a2020-09-25 14:04:19 +01001678 case TRY_EXCEPT:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001679 ADDOP(c, POP_BLOCK);
1680 return 1;
1681
1682 case FINALLY_TRY:
1683 ADDOP(c, POP_BLOCK);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001684 if (preserve_tos) {
Mark Shannonfee55262019-11-21 09:11:43 +00001685 if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) {
1686 return 0;
1687 }
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001688 }
Mark Shannon88dce262019-12-30 09:53:36 +00001689 /* Emit the finally block, restoring the line number when done */
1690 int saved_lineno = c->u->u_lineno;
Mark Shannonfee55262019-11-21 09:11:43 +00001691 VISIT_SEQ(c, stmt, info->fb_datum);
Mark Shannon88dce262019-12-30 09:53:36 +00001692 c->u->u_lineno = saved_lineno;
Mark Shannonfee55262019-11-21 09:11:43 +00001693 if (preserve_tos) {
1694 compiler_pop_fblock(c, POP_VALUE, NULL);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001695 }
1696 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001697
Mark Shannonfee55262019-11-21 09:11:43 +00001698 case FINALLY_END:
1699 if (preserve_tos) {
1700 ADDOP(c, ROT_FOUR);
1701 }
1702 ADDOP(c, POP_TOP);
1703 ADDOP(c, POP_TOP);
1704 ADDOP(c, POP_TOP);
1705 if (preserve_tos) {
1706 ADDOP(c, ROT_FOUR);
1707 }
1708 ADDOP(c, POP_EXCEPT);
1709 return 1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001710
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001711 case WITH:
1712 case ASYNC_WITH:
1713 ADDOP(c, POP_BLOCK);
1714 if (preserve_tos) {
1715 ADDOP(c, ROT_TWO);
1716 }
Mark Shannonfee55262019-11-21 09:11:43 +00001717 if(!compiler_call_exit_with_nones(c)) {
1718 return 0;
1719 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001720 if (info->fb_type == ASYNC_WITH) {
1721 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001722 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001723 ADDOP(c, YIELD_FROM);
1724 }
Mark Shannonfee55262019-11-21 09:11:43 +00001725 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001726 return 1;
1727
1728 case HANDLER_CLEANUP:
Mark Shannonfee55262019-11-21 09:11:43 +00001729 if (info->fb_datum) {
1730 ADDOP(c, POP_BLOCK);
1731 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001732 if (preserve_tos) {
1733 ADDOP(c, ROT_FOUR);
1734 }
Mark Shannonfee55262019-11-21 09:11:43 +00001735 ADDOP(c, POP_EXCEPT);
1736 if (info->fb_datum) {
1737 ADDOP_LOAD_CONST(c, Py_None);
1738 compiler_nameop(c, info->fb_datum, Store);
1739 compiler_nameop(c, info->fb_datum, Del);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001740 }
Mark Shannonfee55262019-11-21 09:11:43 +00001741 return 1;
1742
1743 case POP_VALUE:
1744 if (preserve_tos) {
1745 ADDOP(c, ROT_TWO);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001746 }
Mark Shannonfee55262019-11-21 09:11:43 +00001747 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001748 return 1;
1749 }
1750 Py_UNREACHABLE();
1751}
1752
Mark Shannonfee55262019-11-21 09:11:43 +00001753/** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */
1754static int
1755compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) {
1756 if (c->u->u_nfblocks == 0) {
1757 return 1;
1758 }
1759 struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1];
1760 if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) {
1761 *loop = top;
1762 return 1;
1763 }
1764 struct fblockinfo copy = *top;
1765 c->u->u_nfblocks--;
1766 if (!compiler_unwind_fblock(c, &copy, preserve_tos)) {
1767 return 0;
1768 }
1769 if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) {
1770 return 0;
1771 }
1772 c->u->u_fblock[c->u->u_nfblocks] = copy;
1773 c->u->u_nfblocks++;
1774 return 1;
1775}
1776
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001777/* Compile a sequence of statements, checking for a docstring
1778 and for annotations. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001779
1780static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001781compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001783 int i = 0;
1784 stmt_ty st;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001785 PyObject *docstring;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001786
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001787 /* Set current line number to the line number of first statement.
1788 This way line number for SETUP_ANNOTATIONS will always
1789 coincide with the line number of first "real" statement in module.
Hansraj Das01171eb2019-10-09 07:54:02 +05301790 If body is empty, then lineno will be set later in assemble. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001791 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) {
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001792 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001793 SET_LOC(c, st);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001794 }
1795 /* Every annotated class and module should have __annotations__. */
1796 if (find_ann(stmts)) {
1797 ADDOP(c, SETUP_ANNOTATIONS);
1798 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001799 if (!asdl_seq_LEN(stmts))
1800 return 1;
INADA Naokicb41b272017-02-23 00:31:59 +09001801 /* if not -OO mode, set docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001802 if (c->c_optimize < 2) {
1803 docstring = _PyAST_GetDocString(stmts);
1804 if (docstring) {
1805 i = 1;
1806 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1807 assert(st->kind == Expr_kind);
1808 VISIT(c, expr, st->v.Expr.value);
1809 if (!compiler_nameop(c, __doc__, Store))
1810 return 0;
1811 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001813 for (; i < asdl_seq_LEN(stmts); i++)
1814 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001816}
1817
1818static PyCodeObject *
1819compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001820{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 PyCodeObject *co;
1822 int addNone = 1;
1823 static PyObject *module;
1824 if (!module) {
1825 module = PyUnicode_InternFromString("<module>");
1826 if (!module)
1827 return NULL;
1828 }
1829 /* Use 0 for firstlineno initially, will fixup in assemble(). */
Mark Shannon877df852020-11-12 09:43:29 +00001830 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 return NULL;
1832 switch (mod->kind) {
1833 case Module_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001834 if (!compiler_body(c, mod->v.Module.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 compiler_exit_scope(c);
1836 return 0;
1837 }
1838 break;
1839 case Interactive_kind:
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001840 if (find_ann(mod->v.Interactive.body)) {
1841 ADDOP(c, SETUP_ANNOTATIONS);
1842 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 c->c_interactive = 1;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001844 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 break;
1846 case Expression_kind:
1847 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1848 addNone = 0;
1849 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 default:
1851 PyErr_Format(PyExc_SystemError,
1852 "module kind %d should not be possible",
1853 mod->kind);
1854 return 0;
1855 }
1856 co = assemble(c, addNone);
1857 compiler_exit_scope(c);
1858 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001859}
1860
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861/* The test for LOCAL must come before the test for FREE in order to
1862 handle classes where name is both local and free. The local var is
1863 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001864*/
1865
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001866static int
1867get_ref_type(struct compiler *c, PyObject *name)
1868{
Victor Stinner0b1bc562013-05-16 22:17:17 +02001869 int scope;
Benjamin Peterson312595c2013-05-15 15:26:42 -05001870 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001871 _PyUnicode_EqualToASCIIString(name, "__class__"))
Benjamin Peterson312595c2013-05-15 15:26:42 -05001872 return CELL;
Victor Stinner0b1bc562013-05-16 22:17:17 +02001873 scope = PyST_GetScope(c->u->u_ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 if (scope == 0) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01001875 _Py_FatalErrorFormat(__func__,
1876 "unknown scope for %.100s in %.100s(%s)\n"
1877 "symbols: %s\nlocals: %s\nglobals: %s",
1878 PyUnicode_AsUTF8(name),
1879 PyUnicode_AsUTF8(c->u->u_name),
1880 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)),
1881 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)),
1882 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)),
1883 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887}
1888
1889static int
1890compiler_lookup_arg(PyObject *dict, PyObject *name)
1891{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001892 PyObject *v;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001893 v = PyDict_GetItemWithError(dict, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001894 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001895 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001896 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897}
1898
1899static int
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001900compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001901{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001902 Py_ssize_t i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001903 if (qualname == NULL)
1904 qualname = co->co_name;
1905
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001906 if (free) {
1907 for (i = 0; i < free; ++i) {
1908 /* Bypass com_addop_varname because it will generate
1909 LOAD_DEREF but LOAD_CLOSURE is needed.
1910 */
1911 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1912 int arg, reftype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001914 /* Special case: If a class contains a method with a
1915 free variable that has the same name as a method,
1916 the name will be considered free *and* local in the
1917 class. It should be handled by the closure, as
Min ho Kimc4cacc82019-07-31 08:16:13 +10001918 well as by the normal name lookup logic.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001919 */
1920 reftype = get_ref_type(c, name);
1921 if (reftype == CELL)
1922 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1923 else /* (reftype == FREE) */
1924 arg = compiler_lookup_arg(c->u->u_freevars, name);
1925 if (arg == -1) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01001926 _Py_FatalErrorFormat(__func__,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001927 "lookup %s in %s %d %d\n"
1928 "freevars of %s: %s\n",
1929 PyUnicode_AsUTF8(PyObject_Repr(name)),
1930 PyUnicode_AsUTF8(c->u->u_name),
1931 reftype, arg,
1932 PyUnicode_AsUTF8(co->co_name),
1933 PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars)));
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001934 }
1935 ADDOP_I(c, LOAD_CLOSURE, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001937 flags |= 0x08;
1938 ADDOP_I(c, BUILD_TUPLE, free);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001940 ADDOP_LOAD_CONST(c, (PyObject*)co);
1941 ADDOP_LOAD_CONST(c, qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001942 ADDOP_I(c, MAKE_FUNCTION, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944}
1945
1946static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001947compiler_decorators(struct compiler *c, asdl_expr_seq* decos)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001948{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 if (!decos)
1952 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1955 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1956 }
1957 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001958}
1959
1960static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001961compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs,
1962 asdl_expr_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001963{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001964 /* Push a dict of keyword-only default values.
1965
1966 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
1967 */
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001968 int i;
1969 PyObject *keys = NULL;
1970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1972 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1973 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1974 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04001975 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001976 if (!mangled) {
1977 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001979 if (keys == NULL) {
1980 keys = PyList_New(1);
1981 if (keys == NULL) {
1982 Py_DECREF(mangled);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001983 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001984 }
1985 PyList_SET_ITEM(keys, 0, mangled);
1986 }
1987 else {
1988 int res = PyList_Append(keys, mangled);
1989 Py_DECREF(mangled);
1990 if (res == -1) {
1991 goto error;
1992 }
1993 }
1994 if (!compiler_visit_expr(c, default_)) {
1995 goto error;
1996 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 }
1998 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001999 if (keys != NULL) {
2000 Py_ssize_t default_count = PyList_GET_SIZE(keys);
2001 PyObject *keys_tuple = PyList_AsTuple(keys);
2002 Py_DECREF(keys);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002003 ADDOP_LOAD_CONST_NEW(c, keys_tuple);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002004 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002005 assert(default_count > 0);
2006 return 1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002007 }
2008 else {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002009 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002010 }
2011
2012error:
2013 Py_XDECREF(keys);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002014 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002015}
2016
2017static int
Guido van Rossum95e4d582018-01-26 08:20:18 -08002018compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
2019{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +03002020 ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation));
Guido van Rossum95e4d582018-01-26 08:20:18 -08002021 return 1;
2022}
2023
2024static int
Neal Norwitzc1505362006-12-28 06:47:50 +00002025compiler_visit_argannotation(struct compiler *c, identifier id,
2026 expr_ty annotation, PyObject *names)
2027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 if (annotation) {
Victor Stinner065efc32014-02-18 22:07:56 +01002029 PyObject *mangled;
Batuhan Taskaya044a1042020-10-06 23:03:02 +03002030 VISIT(c, annexpr, annotation);
Victor Stinner065efc32014-02-18 22:07:56 +01002031 mangled = _Py_Mangle(c->u->u_private, id);
Yury Selivanov34ce99f2014-02-18 12:49:41 -05002032 if (!mangled)
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002033 return 0;
Yury Selivanov34ce99f2014-02-18 12:49:41 -05002034 if (PyList_Append(names, mangled) < 0) {
2035 Py_DECREF(mangled);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002036 return 0;
Yury Selivanov34ce99f2014-02-18 12:49:41 -05002037 }
2038 Py_DECREF(mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002039 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002040 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002041}
2042
2043static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002044compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args,
Neal Norwitzc1505362006-12-28 06:47:50 +00002045 PyObject *names)
2046{
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002047 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 for (i = 0; i < asdl_seq_LEN(args); i++) {
2049 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002050 if (!compiler_visit_argannotation(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 c,
2052 arg->arg,
2053 arg->annotation,
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002054 names))
2055 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002057 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002058}
2059
2060static int
2061compiler_visit_annotations(struct compiler *c, arguments_ty args,
2062 expr_ty returns)
2063{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002064 /* Push arg annotation dict.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002065 The expressions are evaluated out-of-order wrt the source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00002066
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002067 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 */
2069 static identifier return_str;
2070 PyObject *names;
Victor Stinnerad9a0662013-11-19 22:23:20 +01002071 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 names = PyList_New(0);
2073 if (!names)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03002074 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002075
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002076 if (!compiler_visit_argannotations(c, args->args, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 goto error;
Pablo Galindoa0c01bf2019-05-31 15:19:50 +01002078 if (!compiler_visit_argannotations(c, args->posonlyargs, names))
2079 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002080 if (args->vararg && args->vararg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002081 !compiler_visit_argannotation(c, args->vararg->arg,
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002082 args->vararg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 goto error;
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002084 if (!compiler_visit_argannotations(c, args->kwonlyargs, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002086 if (args->kwarg && args->kwarg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002087 !compiler_visit_argannotation(c, args->kwarg->arg,
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002088 args->kwarg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00002090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 if (!return_str) {
2092 return_str = PyUnicode_InternFromString("return");
2093 if (!return_str)
2094 goto error;
2095 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002096 if (!compiler_visit_argannotation(c, return_str, returns, names)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 goto error;
2098 }
2099
2100 len = PyList_GET_SIZE(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 if (len) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002102 PyObject *keytuple = PyList_AsTuple(names);
2103 Py_DECREF(names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002104 ADDOP_LOAD_CONST_NEW(c, keytuple);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002105 ADDOP_I(c, BUILD_CONST_KEY_MAP, len);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002106 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002108 else {
2109 Py_DECREF(names);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002110 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002111 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002112
2113error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 Py_DECREF(names);
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03002115 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002116}
2117
2118static int
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002119compiler_visit_defaults(struct compiler *c, arguments_ty args)
2120{
2121 VISIT_SEQ(c, expr, args->defaults);
2122 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
2123 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002124}
2125
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002126static Py_ssize_t
2127compiler_default_arguments(struct compiler *c, arguments_ty args)
2128{
2129 Py_ssize_t funcflags = 0;
2130 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002131 if (!compiler_visit_defaults(c, args))
2132 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002133 funcflags |= 0x01;
2134 }
2135 if (args->kwonlyargs) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002136 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002137 args->kw_defaults);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002138 if (res == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002139 return -1;
2140 }
2141 else if (res > 0) {
2142 funcflags |= 0x02;
2143 }
2144 }
2145 return funcflags;
2146}
2147
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002148static int
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002149forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx)
2150{
2151
2152 if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2153 compiler_error(c, "cannot assign to __debug__");
2154 return 1;
2155 }
2156 return 0;
2157}
2158
2159static int
2160compiler_check_debug_one_arg(struct compiler *c, arg_ty arg)
2161{
2162 if (arg != NULL) {
2163 if (forbidden_name(c, arg->arg, Store))
2164 return 0;
2165 }
2166 return 1;
2167}
2168
2169static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002170compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args)
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002171{
2172 if (args != NULL) {
Pablo Galindoee40e4b2020-04-23 03:43:08 +01002173 for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002174 if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i)))
2175 return 0;
2176 }
2177 }
2178 return 1;
2179}
2180
2181static int
2182compiler_check_debug_args(struct compiler *c, arguments_ty args)
2183{
2184 if (!compiler_check_debug_args_seq(c, args->posonlyargs))
2185 return 0;
2186 if (!compiler_check_debug_args_seq(c, args->args))
2187 return 0;
2188 if (!compiler_check_debug_one_arg(c, args->vararg))
2189 return 0;
2190 if (!compiler_check_debug_args_seq(c, args->kwonlyargs))
2191 return 0;
2192 if (!compiler_check_debug_one_arg(c, args->kwarg))
2193 return 0;
2194 return 1;
2195}
2196
2197static int
Yury Selivanov75445082015-05-11 22:57:16 -04002198compiler_function(struct compiler *c, stmt_ty s, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 PyCodeObject *co;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002201 PyObject *qualname, *docstring = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002202 arguments_ty args;
2203 expr_ty returns;
2204 identifier name;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002205 asdl_expr_seq* decos;
2206 asdl_stmt_seq *body;
INADA Naokicb41b272017-02-23 00:31:59 +09002207 Py_ssize_t i, funcflags;
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002208 int annotations;
Yury Selivanov75445082015-05-11 22:57:16 -04002209 int scope_type;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002210 int firstlineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002211
Yury Selivanov75445082015-05-11 22:57:16 -04002212 if (is_async) {
2213 assert(s->kind == AsyncFunctionDef_kind);
2214
2215 args = s->v.AsyncFunctionDef.args;
2216 returns = s->v.AsyncFunctionDef.returns;
2217 decos = s->v.AsyncFunctionDef.decorator_list;
2218 name = s->v.AsyncFunctionDef.name;
2219 body = s->v.AsyncFunctionDef.body;
2220
2221 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
2222 } else {
2223 assert(s->kind == FunctionDef_kind);
2224
2225 args = s->v.FunctionDef.args;
2226 returns = s->v.FunctionDef.returns;
2227 decos = s->v.FunctionDef.decorator_list;
2228 name = s->v.FunctionDef.name;
2229 body = s->v.FunctionDef.body;
2230
2231 scope_type = COMPILER_SCOPE_FUNCTION;
2232 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002233
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002234 if (!compiler_check_debug_args(c, args))
2235 return 0;
2236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 if (!compiler_decorators(c, decos))
2238 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002239
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002240 firstlineno = s->lineno;
2241 if (asdl_seq_LEN(decos)) {
2242 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2243 }
2244
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002245 funcflags = compiler_default_arguments(c, args);
2246 if (funcflags == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002248 }
2249
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002250 annotations = compiler_visit_annotations(c, args, returns);
2251 if (annotations == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002252 return 0;
2253 }
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002254 else if (annotations > 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002255 funcflags |= 0x04;
2256 }
2257
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002258 if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002259 return 0;
2260 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002261
INADA Naokicb41b272017-02-23 00:31:59 +09002262 /* if not -OO mode, add docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002263 if (c->c_optimize < 2) {
2264 docstring = _PyAST_GetDocString(body);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002265 }
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002266 if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002267 compiler_exit_scope(c);
2268 return 0;
2269 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002272 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Mark Shannon877df852020-11-12 09:43:29 +00002274 for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) {
2275 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(body, i));
2276 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002278 qualname = c->u->u_qualname;
2279 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002281 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002282 Py_XDECREF(qualname);
2283 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002285 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002287 compiler_make_closure(c, co, funcflags, qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002288 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 /* decorators */
2292 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2293 ADDOP_I(c, CALL_FUNCTION, 1);
2294 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002295
Yury Selivanov75445082015-05-11 22:57:16 -04002296 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002297}
2298
2299static int
2300compiler_class(struct compiler *c, stmt_ty s)
2301{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 PyCodeObject *co;
2303 PyObject *str;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002304 int i, firstlineno;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002305 asdl_expr_seq *decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 if (!compiler_decorators(c, decos))
2308 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002309
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002310 firstlineno = s->lineno;
2311 if (asdl_seq_LEN(decos)) {
2312 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2313 }
2314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 /* ultimately generate code for:
2316 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
2317 where:
2318 <func> is a function/closure created from the class body;
2319 it has a single argument (__locals__) where the dict
2320 (or MutableSequence) representing the locals is passed
2321 <name> is the class name
2322 <bases> is the positional arguments and *varargs argument
2323 <keywords> is the keyword arguments and **kwds argument
2324 This borrows from compiler_call.
2325 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002328 if (!compiler_enter_scope(c, s->v.ClassDef.name,
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002329 COMPILER_SCOPE_CLASS, (void *)s, firstlineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 return 0;
2331 /* this block represents what we do in the new scope */
2332 {
2333 /* use the class name for name mangling */
2334 Py_INCREF(s->v.ClassDef.name);
Serhiy Storchaka48842712016-04-06 09:45:48 +03002335 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 /* load (global) __name__ ... */
2337 str = PyUnicode_InternFromString("__name__");
2338 if (!str || !compiler_nameop(c, str, Load)) {
2339 Py_XDECREF(str);
2340 compiler_exit_scope(c);
2341 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002342 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 Py_DECREF(str);
2344 /* ... and store it as __module__ */
2345 str = PyUnicode_InternFromString("__module__");
2346 if (!str || !compiler_nameop(c, str, Store)) {
2347 Py_XDECREF(str);
2348 compiler_exit_scope(c);
2349 return 0;
2350 }
2351 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002352 assert(c->u->u_qualname);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002353 ADDOP_LOAD_CONST(c, c->u->u_qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002354 str = PyUnicode_InternFromString("__qualname__");
2355 if (!str || !compiler_nameop(c, str, Store)) {
2356 Py_XDECREF(str);
2357 compiler_exit_scope(c);
2358 return 0;
2359 }
2360 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 /* compile the body proper */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002362 if (!compiler_body(c, s->v.ClassDef.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 compiler_exit_scope(c);
2364 return 0;
2365 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002366 /* Return __classcell__ if it is referenced, otherwise return None */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002367 if (c->u->u_ste->ste_needs_class_closure) {
Nick Coghlan19d24672016-12-05 16:47:55 +10002368 /* Store __classcell__ into class namespace & return it */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002369 str = PyUnicode_InternFromString("__class__");
2370 if (str == NULL) {
2371 compiler_exit_scope(c);
2372 return 0;
2373 }
2374 i = compiler_lookup_arg(c->u->u_cellvars, str);
2375 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01002376 if (i < 0) {
2377 compiler_exit_scope(c);
2378 return 0;
2379 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002380 assert(i == 0);
Nick Coghlan944368e2016-09-11 14:45:49 +10002381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 ADDOP_I(c, LOAD_CLOSURE, i);
Nick Coghlan19d24672016-12-05 16:47:55 +10002383 ADDOP(c, DUP_TOP);
Nick Coghlan944368e2016-09-11 14:45:49 +10002384 str = PyUnicode_InternFromString("__classcell__");
2385 if (!str || !compiler_nameop(c, str, Store)) {
2386 Py_XDECREF(str);
2387 compiler_exit_scope(c);
2388 return 0;
2389 }
2390 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002392 else {
Nick Coghlan19d24672016-12-05 16:47:55 +10002393 /* No methods referenced __class__, so just return None */
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002394 assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002395 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson312595c2013-05-15 15:26:42 -05002396 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002397 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 /* create the code object */
2399 co = assemble(c, 1);
2400 }
2401 /* leave the new scope */
2402 compiler_exit_scope(c);
2403 if (co == NULL)
2404 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406 /* 2. load the 'build_class' function */
2407 ADDOP(c, LOAD_BUILD_CLASS);
2408
2409 /* 3. load a function (or closure) made from the code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002410 compiler_make_closure(c, co, 0, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 Py_DECREF(co);
2412
2413 /* 4. load class name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002414 ADDOP_LOAD_CONST(c, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415
2416 /* 5. generate the rest of the code for the call */
Pablo Galindoa5634c42020-09-16 19:42:00 +01002417 if (!compiler_call_helper(c, 2, s->v.ClassDef.bases, s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 return 0;
2419
2420 /* 6. apply decorators */
2421 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2422 ADDOP_I(c, CALL_FUNCTION, 1);
2423 }
2424
2425 /* 7. store into <name> */
2426 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2427 return 0;
2428 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002429}
2430
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02002431/* Return 0 if the expression is a constant value except named singletons.
2432 Return 1 otherwise. */
2433static int
2434check_is_arg(expr_ty e)
2435{
2436 if (e->kind != Constant_kind) {
2437 return 1;
2438 }
2439 PyObject *value = e->v.Constant.value;
2440 return (value == Py_None
2441 || value == Py_False
2442 || value == Py_True
2443 || value == Py_Ellipsis);
2444}
2445
2446/* Check operands of identity chacks ("is" and "is not").
2447 Emit a warning if any operand is a constant except named singletons.
2448 Return 0 on error.
2449 */
2450static int
2451check_compare(struct compiler *c, expr_ty e)
2452{
2453 Py_ssize_t i, n;
2454 int left = check_is_arg(e->v.Compare.left);
2455 n = asdl_seq_LEN(e->v.Compare.ops);
2456 for (i = 0; i < n; i++) {
2457 cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i);
2458 int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2459 if (op == Is || op == IsNot) {
2460 if (!right || !left) {
2461 const char *msg = (op == Is)
2462 ? "\"is\" with a literal. Did you mean \"==\"?"
2463 : "\"is not\" with a literal. Did you mean \"!=\"?";
2464 return compiler_warn(c, msg);
2465 }
2466 }
2467 left = right;
2468 }
2469 return 1;
2470}
2471
Mark Shannon9af0e472020-01-14 10:12:45 +00002472static int compiler_addcompare(struct compiler *c, cmpop_ty op)
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002473{
Mark Shannon9af0e472020-01-14 10:12:45 +00002474 int cmp;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002475 switch (op) {
2476 case Eq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002477 cmp = Py_EQ;
2478 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002479 case NotEq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002480 cmp = Py_NE;
2481 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002482 case Lt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002483 cmp = Py_LT;
2484 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002485 case LtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002486 cmp = Py_LE;
2487 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002488 case Gt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002489 cmp = Py_GT;
2490 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002491 case GtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002492 cmp = Py_GE;
2493 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002494 case Is:
Mark Shannon9af0e472020-01-14 10:12:45 +00002495 ADDOP_I(c, IS_OP, 0);
2496 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002497 case IsNot:
Mark Shannon9af0e472020-01-14 10:12:45 +00002498 ADDOP_I(c, IS_OP, 1);
2499 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002500 case In:
Mark Shannon9af0e472020-01-14 10:12:45 +00002501 ADDOP_I(c, CONTAINS_OP, 0);
2502 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002503 case NotIn:
Mark Shannon9af0e472020-01-14 10:12:45 +00002504 ADDOP_I(c, CONTAINS_OP, 1);
2505 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002506 default:
Mark Shannon9af0e472020-01-14 10:12:45 +00002507 Py_UNREACHABLE();
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002508 }
Mark Shannon9af0e472020-01-14 10:12:45 +00002509 ADDOP_I(c, COMPARE_OP, cmp);
2510 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002511}
2512
Mark Shannon9af0e472020-01-14 10:12:45 +00002513
2514
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002515static int
2516compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2517{
2518 switch (e->kind) {
2519 case UnaryOp_kind:
2520 if (e->v.UnaryOp.op == Not)
2521 return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2522 /* fallback to general implementation */
2523 break;
2524 case BoolOp_kind: {
Pablo Galindoa5634c42020-09-16 19:42:00 +01002525 asdl_expr_seq *s = e->v.BoolOp.values;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002526 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2527 assert(n >= 0);
2528 int cond2 = e->v.BoolOp.op == Or;
2529 basicblock *next2 = next;
2530 if (!cond2 != !cond) {
2531 next2 = compiler_new_block(c);
2532 if (next2 == NULL)
2533 return 0;
2534 }
2535 for (i = 0; i < n; ++i) {
2536 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2537 return 0;
2538 }
2539 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2540 return 0;
2541 if (next2 != next)
2542 compiler_use_next_block(c, next2);
2543 return 1;
2544 }
2545 case IfExp_kind: {
2546 basicblock *end, *next2;
2547 end = compiler_new_block(c);
2548 if (end == NULL)
2549 return 0;
2550 next2 = compiler_new_block(c);
2551 if (next2 == NULL)
2552 return 0;
2553 if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2554 return 0;
2555 if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2556 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01002557 ADDOP_JUMP(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002558 compiler_use_next_block(c, next2);
2559 if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2560 return 0;
2561 compiler_use_next_block(c, end);
2562 return 1;
2563 }
2564 case Compare_kind: {
2565 Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2566 if (n > 0) {
Serhiy Storchaka45835252019-02-16 08:29:46 +02002567 if (!check_compare(c, e)) {
2568 return 0;
2569 }
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002570 basicblock *cleanup = compiler_new_block(c);
2571 if (cleanup == NULL)
2572 return 0;
2573 VISIT(c, expr, e->v.Compare.left);
2574 for (i = 0; i < n; i++) {
2575 VISIT(c, expr,
2576 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2577 ADDOP(c, DUP_TOP);
2578 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00002579 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01002580 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002581 NEXT_BLOCK(c);
2582 }
2583 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00002584 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Mark Shannon582aaf12020-08-04 17:30:11 +01002585 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002586 basicblock *end = compiler_new_block(c);
2587 if (end == NULL)
2588 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01002589 ADDOP_JUMP(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002590 compiler_use_next_block(c, cleanup);
2591 ADDOP(c, POP_TOP);
2592 if (!cond) {
Mark Shannon582aaf12020-08-04 17:30:11 +01002593 ADDOP_JUMP(c, JUMP_FORWARD, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002594 }
2595 compiler_use_next_block(c, end);
2596 return 1;
2597 }
2598 /* fallback to general implementation */
2599 break;
2600 }
2601 default:
2602 /* fallback to general implementation */
2603 break;
2604 }
2605
2606 /* general implementation */
2607 VISIT(c, expr, e);
Mark Shannon582aaf12020-08-04 17:30:11 +01002608 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002609 return 1;
2610}
2611
2612static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002613compiler_ifexp(struct compiler *c, expr_ty e)
2614{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002615 basicblock *end, *next;
2616
2617 assert(e->kind == IfExp_kind);
2618 end = compiler_new_block(c);
2619 if (end == NULL)
2620 return 0;
2621 next = compiler_new_block(c);
2622 if (next == NULL)
2623 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002624 if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2625 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002626 VISIT(c, expr, e->v.IfExp.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002627 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002628 compiler_use_next_block(c, next);
2629 VISIT(c, expr, e->v.IfExp.orelse);
2630 compiler_use_next_block(c, end);
2631 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002632}
2633
2634static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002635compiler_lambda(struct compiler *c, expr_ty e)
2636{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002637 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002638 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002639 static identifier name;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002640 Py_ssize_t funcflags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002641 arguments_ty args = e->v.Lambda.args;
2642 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002643
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002644 if (!compiler_check_debug_args(c, args))
2645 return 0;
2646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 if (!name) {
2648 name = PyUnicode_InternFromString("<lambda>");
2649 if (!name)
2650 return 0;
2651 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002653 funcflags = compiler_default_arguments(c, args);
2654 if (funcflags == -1) {
2655 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002656 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002657
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002658 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002659 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002660 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002662 /* Make None the first constant, so the lambda can't have a
2663 docstring. */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002664 if (compiler_add_const(c, Py_None) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002667 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002668 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002669 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2670 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2671 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002672 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 }
2674 else {
2675 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002676 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002678 qualname = c->u->u_qualname;
2679 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002680 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002681 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002682 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002683
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002684 compiler_make_closure(c, co, funcflags, qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002685 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002686 Py_DECREF(co);
2687
2688 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002689}
2690
2691static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692compiler_if(struct compiler *c, stmt_ty s)
2693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002694 basicblock *end, *next;
2695 int constant;
2696 assert(s->kind == If_kind);
2697 end = compiler_new_block(c);
2698 if (end == NULL)
2699 return 0;
2700
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02002701 constant = expr_constant(s->v.If.test);
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002702 /* constant = 0: "if 0"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002703 * constant = 1: "if 1", "if 2", ...
2704 * constant = -1: rest */
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002705 if (constant == 0) {
2706 BEGIN_DO_NOT_EMIT_BYTECODE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002707 VISIT_SEQ(c, stmt, s->v.If.body);
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002708 END_DO_NOT_EMIT_BYTECODE
2709 if (s->v.If.orelse) {
2710 VISIT_SEQ(c, stmt, s->v.If.orelse);
2711 }
2712 } else if (constant == 1) {
2713 VISIT_SEQ(c, stmt, s->v.If.body);
2714 if (s->v.If.orelse) {
2715 BEGIN_DO_NOT_EMIT_BYTECODE
2716 VISIT_SEQ(c, stmt, s->v.If.orelse);
2717 END_DO_NOT_EMIT_BYTECODE
2718 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002719 } else {
Antoine Pitroue7811fc2014-09-18 03:06:50 +02002720 if (asdl_seq_LEN(s->v.If.orelse)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002721 next = compiler_new_block(c);
2722 if (next == NULL)
2723 return 0;
2724 }
Mark Shannonfee55262019-11-21 09:11:43 +00002725 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002726 next = end;
Mark Shannonfee55262019-11-21 09:11:43 +00002727 }
2728 if (!compiler_jump_if(c, s->v.If.test, next, 0)) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002729 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002730 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002731 VISIT_SEQ(c, stmt, s->v.If.body);
Antoine Pitroue7811fc2014-09-18 03:06:50 +02002732 if (asdl_seq_LEN(s->v.If.orelse)) {
Mark Shannon582aaf12020-08-04 17:30:11 +01002733 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002734 compiler_use_next_block(c, next);
2735 VISIT_SEQ(c, stmt, s->v.If.orelse);
2736 }
2737 }
2738 compiler_use_next_block(c, end);
2739 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740}
2741
2742static int
2743compiler_for(struct compiler *c, stmt_ty s)
2744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002745 basicblock *start, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 start = compiler_new_block(c);
2748 cleanup = compiler_new_block(c);
2749 end = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00002750 if (start == NULL || end == NULL || cleanup == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002752 }
2753 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002755 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 VISIT(c, expr, s->v.For.iter);
2757 ADDOP(c, GET_ITER);
2758 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01002759 ADDOP_JUMP(c, FOR_ITER, cleanup);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 VISIT(c, expr, s->v.For.target);
2761 VISIT_SEQ(c, stmt, s->v.For.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002762 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 compiler_use_next_block(c, cleanup);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002764
2765 compiler_pop_fblock(c, FOR_LOOP, start);
2766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 VISIT_SEQ(c, stmt, s->v.For.orelse);
2768 compiler_use_next_block(c, end);
2769 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002770}
2771
Yury Selivanov75445082015-05-11 22:57:16 -04002772
2773static int
2774compiler_async_for(struct compiler *c, stmt_ty s)
2775{
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002776 basicblock *start, *except, *end;
Pablo Galindo90235812020-03-15 04:29:22 +00002777 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07002778 c->u->u_ste->ste_coroutine = 1;
2779 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
Zsolt Dollensteine2396502018-04-27 08:58:56 -07002780 return compiler_error(c, "'async for' outside async function");
2781 }
2782
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002783 start = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002784 except = compiler_new_block(c);
2785 end = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002786
Mark Shannonfee55262019-11-21 09:11:43 +00002787 if (start == NULL || except == NULL || end == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002788 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002789 }
Yury Selivanov75445082015-05-11 22:57:16 -04002790 VISIT(c, expr, s->v.AsyncFor.iter);
2791 ADDOP(c, GET_AITER);
Yury Selivanov75445082015-05-11 22:57:16 -04002792
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002793 compiler_use_next_block(c, start);
Mark Shannonfee55262019-11-21 09:11:43 +00002794 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002795 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002796 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002797 /* SETUP_FINALLY to guard the __anext__ call */
Mark Shannon582aaf12020-08-04 17:30:11 +01002798 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov75445082015-05-11 22:57:16 -04002799 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002800 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04002801 ADDOP(c, YIELD_FROM);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002802 ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */
Yury Selivanov75445082015-05-11 22:57:16 -04002803
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002804 /* Success block for __anext__ */
2805 VISIT(c, expr, s->v.AsyncFor.target);
2806 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002807 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002808
2809 compiler_pop_fblock(c, FOR_LOOP, start);
Yury Selivanov75445082015-05-11 22:57:16 -04002810
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002811 /* Except block for __anext__ */
Yury Selivanov75445082015-05-11 22:57:16 -04002812 compiler_use_next_block(c, except);
Mark Shannon877df852020-11-12 09:43:29 +00002813
2814 c->u->u_lineno = -1;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002815 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov75445082015-05-11 22:57:16 -04002816
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002817 /* `else` block */
Yury Selivanov75445082015-05-11 22:57:16 -04002818 VISIT_SEQ(c, stmt, s->v.For.orelse);
2819
2820 compiler_use_next_block(c, end);
2821
2822 return 1;
2823}
2824
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825static int
2826compiler_while(struct compiler *c, stmt_ty s)
2827{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 basicblock *loop, *orelse, *end, *anchor = NULL;
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02002829 int constant = expr_constant(s->v.While.test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 if (constant == 0) {
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002832 BEGIN_DO_NOT_EMIT_BYTECODE
Pablo Galindo6c3e66a2019-10-30 11:53:26 +00002833 // Push a dummy block so the VISIT_SEQ knows that we are
2834 // inside a while loop so it can correctly evaluate syntax
2835 // errors.
Mark Shannonfee55262019-11-21 09:11:43 +00002836 if (!compiler_push_fblock(c, WHILE_LOOP, NULL, NULL, NULL)) {
Pablo Galindo6c3e66a2019-10-30 11:53:26 +00002837 return 0;
2838 }
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002839 VISIT_SEQ(c, stmt, s->v.While.body);
Pablo Galindo6c3e66a2019-10-30 11:53:26 +00002840 // Remove the dummy block now that is not needed.
2841 compiler_pop_fblock(c, WHILE_LOOP, NULL);
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002842 END_DO_NOT_EMIT_BYTECODE
2843 if (s->v.While.orelse) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 VISIT_SEQ(c, stmt, s->v.While.orelse);
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002845 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 return 1;
2847 }
2848 loop = compiler_new_block(c);
2849 end = compiler_new_block(c);
2850 if (constant == -1) {
2851 anchor = compiler_new_block(c);
2852 if (anchor == NULL)
2853 return 0;
2854 }
2855 if (loop == NULL || end == NULL)
2856 return 0;
2857 if (s->v.While.orelse) {
2858 orelse = compiler_new_block(c);
2859 if (orelse == NULL)
2860 return 0;
2861 }
2862 else
2863 orelse = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 compiler_use_next_block(c, loop);
Mark Shannonfee55262019-11-21 09:11:43 +00002866 if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 return 0;
2868 if (constant == -1) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002869 if (!compiler_jump_if(c, s->v.While.test, anchor, 0))
2870 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 }
2872 VISIT_SEQ(c, stmt, s->v.While.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002873 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 /* XXX should the two POP instructions be in a separate block
2876 if there is no else clause ?
2877 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002878
Benjamin Peterson3cda0ed2014-12-13 16:06:19 -05002879 if (constant == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 compiler_use_next_block(c, anchor);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002881 compiler_pop_fblock(c, WHILE_LOOP, loop);
2882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 if (orelse != NULL) /* what if orelse is just pass? */
2884 VISIT_SEQ(c, stmt, s->v.While.orelse);
2885 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002888}
2889
2890static int
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002891compiler_return(struct compiler *c, stmt_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002892{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002893 int preserve_tos = ((s->v.Return.value != NULL) &&
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002894 (s->v.Return.value->kind != Constant_kind));
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002895 if (c->u->u_ste->ste_type != FunctionBlock)
2896 return compiler_error(c, "'return' outside function");
2897 if (s->v.Return.value != NULL &&
2898 c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2899 {
2900 return compiler_error(
2901 c, "'return' with value in async generator");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002902 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002903 if (preserve_tos) {
2904 VISIT(c, expr, s->v.Return.value);
2905 }
Mark Shannonfee55262019-11-21 09:11:43 +00002906 if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL))
2907 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002908 if (s->v.Return.value == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002909 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002910 }
2911 else if (!preserve_tos) {
2912 VISIT(c, expr, s->v.Return.value);
2913 }
2914 ADDOP(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002916 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002917}
2918
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002919static int
2920compiler_break(struct compiler *c)
2921{
Mark Shannonfee55262019-11-21 09:11:43 +00002922 struct fblockinfo *loop = NULL;
2923 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
2924 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002925 }
Mark Shannonfee55262019-11-21 09:11:43 +00002926 if (loop == NULL) {
2927 return compiler_error(c, "'break' outside loop");
2928 }
2929 if (!compiler_unwind_fblock(c, loop, 0)) {
2930 return 0;
2931 }
Mark Shannon582aaf12020-08-04 17:30:11 +01002932 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit);
Mark Shannonfee55262019-11-21 09:11:43 +00002933 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002934}
2935
2936static int
2937compiler_continue(struct compiler *c)
2938{
Mark Shannonfee55262019-11-21 09:11:43 +00002939 struct fblockinfo *loop = NULL;
2940 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
2941 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002942 }
Mark Shannonfee55262019-11-21 09:11:43 +00002943 if (loop == NULL) {
2944 return compiler_error(c, "'continue' not properly in loop");
2945 }
Mark Shannon582aaf12020-08-04 17:30:11 +01002946 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block);
Mark Shannonfee55262019-11-21 09:11:43 +00002947 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002948}
2949
2950
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002951/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952
2953 SETUP_FINALLY L
2954 <code for body>
2955 POP_BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00002956 <code for finalbody>
2957 JUMP E
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002958 L:
2959 <code for finalbody>
Mark Shannonfee55262019-11-21 09:11:43 +00002960 E:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002962 The special instructions use the block stack. Each block
2963 stack entry contains the instruction that created it (here
2964 SETUP_FINALLY), the level of the value stack at the time the
2965 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002966
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002967 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968 Pushes the current value stack level and the label
2969 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002970 POP_BLOCK:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002971 Pops en entry from the block stack.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002972
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002973 The block stack is unwound when an exception is raised:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002974 when a SETUP_FINALLY entry is found, the raised and the caught
2975 exceptions are pushed onto the value stack (and the exception
2976 condition is cleared), and the interpreter jumps to the label
2977 gotten from the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002978*/
2979
2980static int
2981compiler_try_finally(struct compiler *c, stmt_ty s)
2982{
Mark Shannonfee55262019-11-21 09:11:43 +00002983 basicblock *body, *end, *exit;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002985 body = compiler_new_block(c);
2986 end = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00002987 exit = compiler_new_block(c);
2988 if (body == NULL || end == NULL || exit == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002990
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002991 /* `try` block */
Mark Shannon582aaf12020-08-04 17:30:11 +01002992 ADDOP_JUMP(c, SETUP_FINALLY, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002993 compiler_use_next_block(c, body);
Mark Shannonfee55262019-11-21 09:11:43 +00002994 if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002995 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002996 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
2997 if (!compiler_try_except(c, s))
2998 return 0;
2999 }
3000 else {
3001 VISIT_SEQ(c, stmt, s->v.Try.body);
3002 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 ADDOP(c, POP_BLOCK);
Mark Shannonfee55262019-11-21 09:11:43 +00003004 compiler_pop_fblock(c, FINALLY_TRY, body);
3005 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Mark Shannon582aaf12020-08-04 17:30:11 +01003006 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00003007 /* `finally` block */
3008 compiler_use_next_block(c, end);
3009 if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL))
3010 return 0;
3011 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3012 compiler_pop_fblock(c, FINALLY_END, end);
3013 ADDOP(c, RERAISE);
3014 compiler_use_next_block(c, exit);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003015 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003016}
3017
3018/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003019 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003020 (The contents of the value stack is shown in [], with the top
3021 at the right; 'tb' is trace-back info, 'val' the exception's
3022 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003023
3024 Value stack Label Instruction Argument
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003025 [] SETUP_FINALLY L1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003026 [] <code for S>
3027 [] POP_BLOCK
3028 [] JUMP_FORWARD L0
3029
3030 [tb, val, exc] L1: DUP )
3031 [tb, val, exc, exc] <evaluate E1> )
Mark Shannon9af0e472020-01-14 10:12:45 +00003032 [tb, val, exc, exc, E1] JUMP_IF_NOT_EXC_MATCH L2 ) only if E1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 [tb, val, exc] POP
3034 [tb, val] <assign to V1> (or POP if no V1)
3035 [tb] POP
3036 [] <code for S1>
3037 JUMP_FORWARD L0
3038
3039 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003040 .............................etc.......................
3041
Mark Shannonfee55262019-11-21 09:11:43 +00003042 [tb, val, exc] Ln+1: RERAISE # re-raise exception
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003043
3044 [] L0: <next statement>
3045
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003046 Of course, parts are not generated if Vi or Ei is not present.
3047*/
3048static int
3049compiler_try_except(struct compiler *c, stmt_ty s)
3050{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003051 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003052 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054 body = compiler_new_block(c);
3055 except = compiler_new_block(c);
3056 orelse = compiler_new_block(c);
3057 end = compiler_new_block(c);
3058 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
3059 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01003060 ADDOP_JUMP(c, SETUP_FINALLY, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 compiler_use_next_block(c, body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003062 if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003064 VISIT_SEQ(c, stmt, s->v.Try.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 ADDOP(c, POP_BLOCK);
Mark Shannon02d126a2020-09-25 14:04:19 +01003066 compiler_pop_fblock(c, TRY_EXCEPT, body);
Mark Shannon582aaf12020-08-04 17:30:11 +01003067 ADDOP_JUMP(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003068 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003069 compiler_use_next_block(c, except);
Mark Shannon02d126a2020-09-25 14:04:19 +01003070 /* Runtime will push a block here, so we need to account for that */
3071 if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL))
3072 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 for (i = 0; i < n; i++) {
3074 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003075 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003076 if (!handler->v.ExceptHandler.type && i < n-1)
3077 return compiler_error(c, "default 'except:' must be last");
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003078 SET_LOC(c, handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079 except = compiler_new_block(c);
3080 if (except == NULL)
3081 return 0;
3082 if (handler->v.ExceptHandler.type) {
3083 ADDOP(c, DUP_TOP);
3084 VISIT(c, expr, handler->v.ExceptHandler.type);
Mark Shannon582aaf12020-08-04 17:30:11 +01003085 ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003086 }
3087 ADDOP(c, POP_TOP);
3088 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003089 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00003090
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003091 cleanup_end = compiler_new_block(c);
3092 cleanup_body = compiler_new_block(c);
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003093 if (cleanup_end == NULL || cleanup_body == NULL) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003094 return 0;
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003095 }
Guido van Rossumb940e112007-01-10 16:19:56 +00003096
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003097 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3098 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003099
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003100 /*
3101 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003102 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003103 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003104 try:
3105 # body
3106 finally:
Chris Angelicoad098b62019-05-21 23:34:19 +10003107 name = None # in case body contains "del name"
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003108 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003109 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003110
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003111 /* second try: */
Mark Shannon582aaf12020-08-04 17:30:11 +01003112 ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003113 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003114 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003115 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003116
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003117 /* second # body */
3118 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003119 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003120 ADDOP(c, POP_BLOCK);
3121 ADDOP(c, POP_EXCEPT);
Mark Shannon877df852020-11-12 09:43:29 +00003122 /* name = None; del name; # Mark as artificial */
3123 c->u->u_lineno = -1;
Mark Shannonfee55262019-11-21 09:11:43 +00003124 ADDOP_LOAD_CONST(c, Py_None);
3125 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3126 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Mark Shannon582aaf12020-08-04 17:30:11 +01003127 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003128
Mark Shannonfee55262019-11-21 09:11:43 +00003129 /* except: */
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003130 compiler_use_next_block(c, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131
Mark Shannon877df852020-11-12 09:43:29 +00003132 /* name = None; del name; # Mark as artificial */
3133 c->u->u_lineno = -1;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003134 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003135 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003136 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003137
Mark Shannonfee55262019-11-21 09:11:43 +00003138 ADDOP(c, RERAISE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 }
3140 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003141 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003142
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003143 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05003144 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003145 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003146
Guido van Rossumb940e112007-01-10 16:19:56 +00003147 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003148 ADDOP(c, POP_TOP);
3149 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003150 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003151 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003152 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003153 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003154 ADDOP(c, POP_EXCEPT);
Mark Shannon582aaf12020-08-04 17:30:11 +01003155 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003156 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003157 compiler_use_next_block(c, except);
3158 }
Mark Shannon02d126a2020-09-25 14:04:19 +01003159 compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL);
Mark Shannonfee55262019-11-21 09:11:43 +00003160 ADDOP(c, RERAISE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003162 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003163 compiler_use_next_block(c, end);
3164 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003165}
3166
3167static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003168compiler_try(struct compiler *c, stmt_ty s) {
3169 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
3170 return compiler_try_finally(c, s);
3171 else
3172 return compiler_try_except(c, s);
3173}
3174
3175
3176static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003177compiler_import_as(struct compiler *c, identifier name, identifier asname)
3178{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003179 /* The IMPORT_NAME opcode was already generated. This function
3180 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003182 If there is a dot in name, we need to split it and emit a
Serhiy Storchakaf93234b2017-05-09 22:31:05 +03003183 IMPORT_FROM for each name.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003184 */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003185 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
3186 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003187 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003188 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003189 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190 /* Consume the base module name to get the first attribute */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003191 while (1) {
3192 Py_ssize_t pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003193 PyObject *attr;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003194 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003195 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003196 return 0;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003197 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198 if (!attr)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003199 return 0;
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003200 ADDOP_N(c, IMPORT_FROM, attr, names);
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003201 if (dot == -1) {
3202 break;
3203 }
3204 ADDOP(c, ROT_TWO);
3205 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003206 }
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003207 if (!compiler_nameop(c, asname, Store)) {
3208 return 0;
3209 }
3210 ADDOP(c, POP_TOP);
3211 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003212 }
3213 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003214}
3215
3216static int
3217compiler_import(struct compiler *c, stmt_ty s)
3218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 /* The Import node stores a module name like a.b.c as a single
3220 string. This is convenient for all cases except
3221 import a.b.c as d
3222 where we need to parse that string to extract the individual
3223 module names.
3224 XXX Perhaps change the representation to make this case simpler?
3225 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01003226 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003227
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003228 PyObject *zero = _PyLong_GetZero(); // borrowed reference
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003229 for (i = 0; i < n; i++) {
3230 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
3231 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003232
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003233 ADDOP_LOAD_CONST(c, zero);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003234 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003235 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003237 if (alias->asname) {
3238 r = compiler_import_as(c, alias->name, alias->asname);
3239 if (!r)
3240 return r;
3241 }
3242 else {
3243 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003244 Py_ssize_t dot = PyUnicode_FindChar(
3245 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02003246 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003247 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02003248 if (tmp == NULL)
3249 return 0;
3250 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003251 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003252 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003253 Py_DECREF(tmp);
3254 }
3255 if (!r)
3256 return r;
3257 }
3258 }
3259 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003260}
3261
3262static int
3263compiler_from_import(struct compiler *c, stmt_ty s)
3264{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003265 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003266 PyObject *names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003267 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003269 if (!empty_string) {
3270 empty_string = PyUnicode_FromString("");
3271 if (!empty_string)
3272 return 0;
3273 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003274
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003275 ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02003276
3277 names = PyTuple_New(n);
3278 if (!names)
3279 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003281 /* build up the names */
3282 for (i = 0; i < n; i++) {
3283 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3284 Py_INCREF(alias->name);
3285 PyTuple_SET_ITEM(names, i, alias->name);
3286 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003288 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003289 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290 Py_DECREF(names);
3291 return compiler_error(c, "from __future__ imports must occur "
3292 "at the beginning of the file");
3293 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003294 ADDOP_LOAD_CONST_NEW(c, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003296 if (s->v.ImportFrom.module) {
3297 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
3298 }
3299 else {
3300 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
3301 }
3302 for (i = 0; i < n; i++) {
3303 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3304 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003305
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003306 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 assert(n == 1);
3308 ADDOP(c, IMPORT_STAR);
3309 return 1;
3310 }
3311
3312 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
3313 store_name = alias->name;
3314 if (alias->asname)
3315 store_name = alias->asname;
3316
3317 if (!compiler_nameop(c, store_name, Store)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 return 0;
3319 }
3320 }
3321 /* remove imported module */
3322 ADDOP(c, POP_TOP);
3323 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003324}
3325
3326static int
3327compiler_assert(struct compiler *c, stmt_ty s)
3328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003330
Georg Brandl8334fd92010-12-04 10:26:46 +00003331 if (c->c_optimize)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003332 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003333 if (s->v.Assert.test->kind == Tuple_kind &&
Serhiy Storchakad31e7732018-10-21 10:09:39 +03003334 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0)
3335 {
3336 if (!compiler_warn(c, "assertion is always true, "
3337 "perhaps remove parentheses?"))
3338 {
Victor Stinner14e461d2013-08-26 22:28:21 +02003339 return 0;
3340 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003341 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003342 end = compiler_new_block(c);
3343 if (end == NULL)
3344 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003345 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
3346 return 0;
Zackery Spytzce6a0702019-08-25 03:44:09 -06003347 ADDOP(c, LOAD_ASSERTION_ERROR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003348 if (s->v.Assert.msg) {
3349 VISIT(c, expr, s->v.Assert.msg);
3350 ADDOP_I(c, CALL_FUNCTION, 1);
3351 }
3352 ADDOP_I(c, RAISE_VARARGS, 1);
3353 compiler_use_next_block(c, end);
3354 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003355}
3356
3357static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003358compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3359{
3360 if (c->c_interactive && c->c_nestlevel <= 1) {
3361 VISIT(c, expr, value);
3362 ADDOP(c, PRINT_EXPR);
3363 return 1;
3364 }
3365
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003366 if (value->kind == Constant_kind) {
Victor Stinner15a30952016-02-08 22:45:06 +01003367 /* ignore constant statement */
Mark Shannon877df852020-11-12 09:43:29 +00003368 ADDOP(c, NOP);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003369 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003370 }
3371
3372 VISIT(c, expr, value);
3373 ADDOP(c, POP_TOP);
3374 return 1;
3375}
3376
3377static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003378compiler_visit_stmt(struct compiler *c, stmt_ty s)
3379{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003380 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003382 /* Always assign a lineno to the next instruction for a stmt. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003383 SET_LOC(c, s);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003385 switch (s->kind) {
3386 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04003387 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003388 case ClassDef_kind:
3389 return compiler_class(c, s);
3390 case Return_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003391 return compiler_return(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003392 case Delete_kind:
3393 VISIT_SEQ(c, expr, s->v.Delete.targets)
3394 break;
3395 case Assign_kind:
3396 n = asdl_seq_LEN(s->v.Assign.targets);
3397 VISIT(c, expr, s->v.Assign.value);
3398 for (i = 0; i < n; i++) {
3399 if (i < n - 1)
3400 ADDOP(c, DUP_TOP);
3401 VISIT(c, expr,
3402 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3403 }
3404 break;
3405 case AugAssign_kind:
3406 return compiler_augassign(c, s);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003407 case AnnAssign_kind:
3408 return compiler_annassign(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003409 case For_kind:
3410 return compiler_for(c, s);
3411 case While_kind:
3412 return compiler_while(c, s);
3413 case If_kind:
3414 return compiler_if(c, s);
3415 case Raise_kind:
3416 n = 0;
3417 if (s->v.Raise.exc) {
3418 VISIT(c, expr, s->v.Raise.exc);
3419 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003420 if (s->v.Raise.cause) {
3421 VISIT(c, expr, s->v.Raise.cause);
3422 n++;
3423 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003424 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01003425 ADDOP_I(c, RAISE_VARARGS, (int)n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003427 case Try_kind:
3428 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003429 case Assert_kind:
3430 return compiler_assert(c, s);
3431 case Import_kind:
3432 return compiler_import(c, s);
3433 case ImportFrom_kind:
3434 return compiler_from_import(c, s);
3435 case Global_kind:
3436 case Nonlocal_kind:
3437 break;
3438 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003439 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003440 case Pass_kind:
Mark Shannon877df852020-11-12 09:43:29 +00003441 ADDOP(c, NOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003442 break;
3443 case Break_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003444 return compiler_break(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445 case Continue_kind:
3446 return compiler_continue(c);
3447 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003448 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04003449 case AsyncFunctionDef_kind:
3450 return compiler_function(c, s, 1);
3451 case AsyncWith_kind:
3452 return compiler_async_with(c, s, 0);
3453 case AsyncFor_kind:
3454 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003455 }
Yury Selivanov75445082015-05-11 22:57:16 -04003456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003457 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003458}
3459
3460static int
3461unaryop(unaryop_ty op)
3462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003463 switch (op) {
3464 case Invert:
3465 return UNARY_INVERT;
3466 case Not:
3467 return UNARY_NOT;
3468 case UAdd:
3469 return UNARY_POSITIVE;
3470 case USub:
3471 return UNARY_NEGATIVE;
3472 default:
3473 PyErr_Format(PyExc_SystemError,
3474 "unary op %d should not be possible", op);
3475 return 0;
3476 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003477}
3478
3479static int
Andy Lester76d58772020-03-10 21:18:12 -05003480binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003481{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 switch (op) {
3483 case Add:
3484 return BINARY_ADD;
3485 case Sub:
3486 return BINARY_SUBTRACT;
3487 case Mult:
3488 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003489 case MatMult:
3490 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003491 case Div:
3492 return BINARY_TRUE_DIVIDE;
3493 case Mod:
3494 return BINARY_MODULO;
3495 case Pow:
3496 return BINARY_POWER;
3497 case LShift:
3498 return BINARY_LSHIFT;
3499 case RShift:
3500 return BINARY_RSHIFT;
3501 case BitOr:
3502 return BINARY_OR;
3503 case BitXor:
3504 return BINARY_XOR;
3505 case BitAnd:
3506 return BINARY_AND;
3507 case FloorDiv:
3508 return BINARY_FLOOR_DIVIDE;
3509 default:
3510 PyErr_Format(PyExc_SystemError,
3511 "binary op %d should not be possible", op);
3512 return 0;
3513 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003514}
3515
3516static int
Andy Lester76d58772020-03-10 21:18:12 -05003517inplace_binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003518{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 switch (op) {
3520 case Add:
3521 return INPLACE_ADD;
3522 case Sub:
3523 return INPLACE_SUBTRACT;
3524 case Mult:
3525 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003526 case MatMult:
3527 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003528 case Div:
3529 return INPLACE_TRUE_DIVIDE;
3530 case Mod:
3531 return INPLACE_MODULO;
3532 case Pow:
3533 return INPLACE_POWER;
3534 case LShift:
3535 return INPLACE_LSHIFT;
3536 case RShift:
3537 return INPLACE_RSHIFT;
3538 case BitOr:
3539 return INPLACE_OR;
3540 case BitXor:
3541 return INPLACE_XOR;
3542 case BitAnd:
3543 return INPLACE_AND;
3544 case FloorDiv:
3545 return INPLACE_FLOOR_DIVIDE;
3546 default:
3547 PyErr_Format(PyExc_SystemError,
3548 "inplace binary op %d should not be possible", op);
3549 return 0;
3550 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003551}
3552
3553static int
3554compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3555{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003556 int op, scope;
3557 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003558 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003560 PyObject *dict = c->u->u_names;
3561 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003562
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003563 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3564 !_PyUnicode_EqualToASCIIString(name, "True") &&
3565 !_PyUnicode_EqualToASCIIString(name, "False"));
Benjamin Peterson70b224d2012-12-06 17:49:58 -05003566
Pablo Galindoc5fc1562020-04-22 23:29:27 +01003567 if (forbidden_name(c, name, ctx))
3568 return 0;
3569
Serhiy Storchakabd6ec4d2017-12-18 14:29:12 +02003570 mangled = _Py_Mangle(c->u->u_private, name);
3571 if (!mangled)
3572 return 0;
3573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 op = 0;
3575 optype = OP_NAME;
3576 scope = PyST_GetScope(c->u->u_ste, mangled);
3577 switch (scope) {
3578 case FREE:
3579 dict = c->u->u_freevars;
3580 optype = OP_DEREF;
3581 break;
3582 case CELL:
3583 dict = c->u->u_cellvars;
3584 optype = OP_DEREF;
3585 break;
3586 case LOCAL:
3587 if (c->u->u_ste->ste_type == FunctionBlock)
3588 optype = OP_FAST;
3589 break;
3590 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04003591 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003592 optype = OP_GLOBAL;
3593 break;
3594 case GLOBAL_EXPLICIT:
3595 optype = OP_GLOBAL;
3596 break;
3597 default:
3598 /* scope can be 0 */
3599 break;
3600 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003602 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003603 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 switch (optype) {
3606 case OP_DEREF:
3607 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003608 case Load:
3609 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3610 break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003611 case Store: op = STORE_DEREF; break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003612 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003613 }
3614 break;
3615 case OP_FAST:
3616 switch (ctx) {
3617 case Load: op = LOAD_FAST; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003618 case Store: op = STORE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003619 case Del: op = DELETE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003620 }
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003621 ADDOP_N(c, op, mangled, varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003622 return 1;
3623 case OP_GLOBAL:
3624 switch (ctx) {
3625 case Load: op = LOAD_GLOBAL; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003626 case Store: op = STORE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 case Del: op = DELETE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003628 }
3629 break;
3630 case OP_NAME:
3631 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00003632 case Load: op = LOAD_NAME; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003633 case Store: op = STORE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003634 case Del: op = DELETE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003635 }
3636 break;
3637 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003639 assert(op);
Andy Lester76d58772020-03-10 21:18:12 -05003640 arg = compiler_add_o(dict, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003641 Py_DECREF(mangled);
3642 if (arg < 0)
3643 return 0;
3644 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003645}
3646
3647static int
3648compiler_boolop(struct compiler *c, expr_ty e)
3649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003650 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003651 int jumpi;
3652 Py_ssize_t i, n;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003653 asdl_expr_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003655 assert(e->kind == BoolOp_kind);
3656 if (e->v.BoolOp.op == And)
3657 jumpi = JUMP_IF_FALSE_OR_POP;
3658 else
3659 jumpi = JUMP_IF_TRUE_OR_POP;
3660 end = compiler_new_block(c);
3661 if (end == NULL)
3662 return 0;
3663 s = e->v.BoolOp.values;
3664 n = asdl_seq_LEN(s) - 1;
3665 assert(n >= 0);
3666 for (i = 0; i < n; ++i) {
3667 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003668 ADDOP_JUMP(c, jumpi, end);
Mark Shannon6e8128f2020-07-30 10:03:00 +01003669 basicblock *next = compiler_new_block(c);
3670 if (next == NULL) {
3671 return 0;
3672 }
3673 compiler_use_next_block(c, next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003674 }
3675 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3676 compiler_use_next_block(c, end);
3677 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003678}
3679
3680static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003681starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
Mark Shannon13bc1392020-01-23 09:25:17 +00003682 int build, int add, int extend, int tuple)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003683{
3684 Py_ssize_t n = asdl_seq_LEN(elts);
Mark Shannon13bc1392020-01-23 09:25:17 +00003685 Py_ssize_t i, seen_star = 0;
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003686 if (n > 2 && are_all_items_const(elts, 0, n)) {
3687 PyObject *folded = PyTuple_New(n);
3688 if (folded == NULL) {
3689 return 0;
3690 }
3691 PyObject *val;
3692 for (i = 0; i < n; i++) {
3693 val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
3694 Py_INCREF(val);
3695 PyTuple_SET_ITEM(folded, i, val);
3696 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003697 if (tuple) {
3698 ADDOP_LOAD_CONST_NEW(c, folded);
3699 } else {
3700 if (add == SET_ADD) {
3701 Py_SETREF(folded, PyFrozenSet_New(folded));
3702 if (folded == NULL) {
3703 return 0;
3704 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003705 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003706 ADDOP_I(c, build, pushed);
3707 ADDOP_LOAD_CONST_NEW(c, folded);
3708 ADDOP_I(c, extend, 1);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003709 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003710 return 1;
3711 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003712
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003713 for (i = 0; i < n; i++) {
3714 expr_ty elt = asdl_seq_GET(elts, i);
3715 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003716 seen_star = 1;
3717 }
3718 }
3719 if (seen_star) {
3720 seen_star = 0;
3721 for (i = 0; i < n; i++) {
3722 expr_ty elt = asdl_seq_GET(elts, i);
3723 if (elt->kind == Starred_kind) {
3724 if (seen_star == 0) {
3725 ADDOP_I(c, build, i+pushed);
3726 seen_star = 1;
3727 }
3728 VISIT(c, expr, elt->v.Starred.value);
3729 ADDOP_I(c, extend, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003730 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003731 else {
3732 VISIT(c, expr, elt);
3733 if (seen_star) {
3734 ADDOP_I(c, add, 1);
3735 }
3736 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003737 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003738 assert(seen_star);
3739 if (tuple) {
3740 ADDOP(c, LIST_TO_TUPLE);
3741 }
3742 }
3743 else {
3744 for (i = 0; i < n; i++) {
3745 expr_ty elt = asdl_seq_GET(elts, i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003746 VISIT(c, expr, elt);
Mark Shannon13bc1392020-01-23 09:25:17 +00003747 }
3748 if (tuple) {
3749 ADDOP_I(c, BUILD_TUPLE, n+pushed);
3750 } else {
3751 ADDOP_I(c, build, n+pushed);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003752 }
3753 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003754 return 1;
3755}
3756
3757static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003758assignment_helper(struct compiler *c, asdl_expr_seq *elts)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003759{
3760 Py_ssize_t n = asdl_seq_LEN(elts);
3761 Py_ssize_t i;
3762 int seen_star = 0;
3763 for (i = 0; i < n; i++) {
3764 expr_ty elt = asdl_seq_GET(elts, i);
3765 if (elt->kind == Starred_kind && !seen_star) {
3766 if ((i >= (1 << 8)) ||
3767 (n-i-1 >= (INT_MAX >> 8)))
3768 return compiler_error(c,
3769 "too many expressions in "
3770 "star-unpacking assignment");
3771 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3772 seen_star = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003773 }
3774 else if (elt->kind == Starred_kind) {
3775 return compiler_error(c,
Furkan Öndercb6534e2020-03-26 04:54:31 +03003776 "multiple starred expressions in assignment");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003777 }
3778 }
3779 if (!seen_star) {
3780 ADDOP_I(c, UNPACK_SEQUENCE, n);
3781 }
Brandt Bucherd5aa2e92020-03-07 19:44:18 -08003782 for (i = 0; i < n; i++) {
3783 expr_ty elt = asdl_seq_GET(elts, i);
3784 VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value);
3785 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003786 return 1;
3787}
3788
3789static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003790compiler_list(struct compiler *c, expr_ty e)
3791{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003792 asdl_expr_seq *elts = e->v.List.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003793 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003794 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003795 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003796 else if (e->v.List.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003797 return starunpack_helper(c, elts, 0, BUILD_LIST,
3798 LIST_APPEND, LIST_EXTEND, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003799 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003800 else
3801 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003802 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003803}
3804
3805static int
3806compiler_tuple(struct compiler *c, expr_ty e)
3807{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003808 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003809 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003810 return assignment_helper(c, elts);
3811 }
3812 else if (e->v.Tuple.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003813 return starunpack_helper(c, elts, 0, BUILD_LIST,
3814 LIST_APPEND, LIST_EXTEND, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003815 }
3816 else
3817 VISIT_SEQ(c, expr, elts);
3818 return 1;
3819}
3820
3821static int
3822compiler_set(struct compiler *c, expr_ty e)
3823{
Mark Shannon13bc1392020-01-23 09:25:17 +00003824 return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET,
3825 SET_ADD, SET_UPDATE, 0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003826}
3827
3828static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003829are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003830{
3831 Py_ssize_t i;
3832 for (i = begin; i < end; i++) {
3833 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003834 if (key == NULL || key->kind != Constant_kind)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003835 return 0;
3836 }
3837 return 1;
3838}
3839
3840static int
3841compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3842{
3843 Py_ssize_t i, n = end - begin;
3844 PyObject *keys, *key;
3845 if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) {
3846 for (i = begin; i < end; i++) {
3847 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3848 }
3849 keys = PyTuple_New(n);
3850 if (keys == NULL) {
3851 return 0;
3852 }
3853 for (i = begin; i < end; i++) {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003854 key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003855 Py_INCREF(key);
3856 PyTuple_SET_ITEM(keys, i - begin, key);
3857 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003858 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003859 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3860 }
3861 else {
3862 for (i = begin; i < end; i++) {
3863 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3864 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3865 }
3866 ADDOP_I(c, BUILD_MAP, n);
3867 }
3868 return 1;
3869}
3870
3871static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003872compiler_dict(struct compiler *c, expr_ty e)
3873{
Victor Stinner976bb402016-03-23 11:36:19 +01003874 Py_ssize_t i, n, elements;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003875 int have_dict;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003876 int is_unpacking = 0;
3877 n = asdl_seq_LEN(e->v.Dict.values);
Mark Shannon8a4cd702020-01-27 09:57:45 +00003878 have_dict = 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003879 elements = 0;
3880 for (i = 0; i < n; i++) {
3881 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003882 if (is_unpacking) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003883 if (elements) {
3884 if (!compiler_subdict(c, e, i - elements, i)) {
3885 return 0;
3886 }
3887 if (have_dict) {
3888 ADDOP_I(c, DICT_UPDATE, 1);
3889 }
3890 have_dict = 1;
3891 elements = 0;
3892 }
3893 if (have_dict == 0) {
3894 ADDOP_I(c, BUILD_MAP, 0);
3895 have_dict = 1;
3896 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003897 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Mark Shannon8a4cd702020-01-27 09:57:45 +00003898 ADDOP_I(c, DICT_UPDATE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003899 }
3900 else {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003901 if (elements == 0xFFFF) {
Pablo Galindoc51db0e2020-08-13 09:48:41 +01003902 if (!compiler_subdict(c, e, i - elements, i + 1)) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003903 return 0;
3904 }
3905 if (have_dict) {
3906 ADDOP_I(c, DICT_UPDATE, 1);
3907 }
3908 have_dict = 1;
3909 elements = 0;
3910 }
3911 else {
3912 elements++;
3913 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003914 }
3915 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003916 if (elements) {
3917 if (!compiler_subdict(c, e, n - elements, n)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003918 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003919 }
3920 if (have_dict) {
3921 ADDOP_I(c, DICT_UPDATE, 1);
3922 }
3923 have_dict = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003924 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003925 if (!have_dict) {
3926 ADDOP_I(c, BUILD_MAP, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003927 }
3928 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003929}
3930
3931static int
3932compiler_compare(struct compiler *c, expr_ty e)
3933{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003934 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003935
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02003936 if (!check_compare(c, e)) {
3937 return 0;
3938 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003939 VISIT(c, expr, e->v.Compare.left);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003940 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
3941 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
3942 if (n == 0) {
3943 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Mark Shannon9af0e472020-01-14 10:12:45 +00003944 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003945 }
3946 else {
3947 basicblock *cleanup = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003948 if (cleanup == NULL)
3949 return 0;
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003950 for (i = 0; i < n; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003951 VISIT(c, expr,
3952 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003953 ADDOP(c, DUP_TOP);
3954 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00003955 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003956 ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003957 NEXT_BLOCK(c);
3958 }
3959 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00003960 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003961 basicblock *end = compiler_new_block(c);
3962 if (end == NULL)
3963 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01003964 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003965 compiler_use_next_block(c, cleanup);
3966 ADDOP(c, ROT_TWO);
3967 ADDOP(c, POP_TOP);
3968 compiler_use_next_block(c, end);
3969 }
3970 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003971}
3972
Serhiy Storchaka62e44812019-02-16 08:12:19 +02003973static PyTypeObject *
3974infer_type(expr_ty e)
3975{
3976 switch (e->kind) {
3977 case Tuple_kind:
3978 return &PyTuple_Type;
3979 case List_kind:
3980 case ListComp_kind:
3981 return &PyList_Type;
3982 case Dict_kind:
3983 case DictComp_kind:
3984 return &PyDict_Type;
3985 case Set_kind:
3986 case SetComp_kind:
3987 return &PySet_Type;
3988 case GeneratorExp_kind:
3989 return &PyGen_Type;
3990 case Lambda_kind:
3991 return &PyFunction_Type;
3992 case JoinedStr_kind:
3993 case FormattedValue_kind:
3994 return &PyUnicode_Type;
3995 case Constant_kind:
Victor Stinnera102ed72020-02-07 02:24:48 +01003996 return Py_TYPE(e->v.Constant.value);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02003997 default:
3998 return NULL;
3999 }
4000}
4001
4002static int
4003check_caller(struct compiler *c, expr_ty e)
4004{
4005 switch (e->kind) {
4006 case Constant_kind:
4007 case Tuple_kind:
4008 case List_kind:
4009 case ListComp_kind:
4010 case Dict_kind:
4011 case DictComp_kind:
4012 case Set_kind:
4013 case SetComp_kind:
4014 case GeneratorExp_kind:
4015 case JoinedStr_kind:
4016 case FormattedValue_kind:
4017 return compiler_warn(c, "'%.200s' object is not callable; "
4018 "perhaps you missed a comma?",
4019 infer_type(e)->tp_name);
4020 default:
4021 return 1;
4022 }
4023}
4024
4025static int
4026check_subscripter(struct compiler *c, expr_ty e)
4027{
4028 PyObject *v;
4029
4030 switch (e->kind) {
4031 case Constant_kind:
4032 v = e->v.Constant.value;
4033 if (!(v == Py_None || v == Py_Ellipsis ||
4034 PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
4035 PyAnySet_Check(v)))
4036 {
4037 return 1;
4038 }
4039 /* fall through */
4040 case Set_kind:
4041 case SetComp_kind:
4042 case GeneratorExp_kind:
4043 case Lambda_kind:
4044 return compiler_warn(c, "'%.200s' object is not subscriptable; "
4045 "perhaps you missed a comma?",
4046 infer_type(e)->tp_name);
4047 default:
4048 return 1;
4049 }
4050}
4051
4052static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004053check_index(struct compiler *c, expr_ty e, expr_ty s)
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004054{
4055 PyObject *v;
4056
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004057 PyTypeObject *index_type = infer_type(s);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004058 if (index_type == NULL
4059 || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
4060 || index_type == &PySlice_Type) {
4061 return 1;
4062 }
4063
4064 switch (e->kind) {
4065 case Constant_kind:
4066 v = e->v.Constant.value;
4067 if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
4068 return 1;
4069 }
4070 /* fall through */
4071 case Tuple_kind:
4072 case List_kind:
4073 case ListComp_kind:
4074 case JoinedStr_kind:
4075 case FormattedValue_kind:
4076 return compiler_warn(c, "%.200s indices must be integers or slices, "
4077 "not %.200s; "
4078 "perhaps you missed a comma?",
4079 infer_type(e)->tp_name,
4080 index_type->tp_name);
4081 default:
4082 return 1;
4083 }
4084}
4085
Zackery Spytz97f5de02019-03-22 01:30:32 -06004086// Return 1 if the method call was optimized, -1 if not, and 0 on error.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004087static int
Yury Selivanovf2392132016-12-13 19:03:51 -05004088maybe_optimize_method_call(struct compiler *c, expr_ty e)
4089{
4090 Py_ssize_t argsl, i;
4091 expr_ty meth = e->v.Call.func;
Pablo Galindoa5634c42020-09-16 19:42:00 +01004092 asdl_expr_seq *args = e->v.Call.args;
Yury Selivanovf2392132016-12-13 19:03:51 -05004093
4094 /* Check that the call node is an attribute access, and that
4095 the call doesn't have keyword parameters. */
4096 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
4097 asdl_seq_LEN(e->v.Call.keywords))
4098 return -1;
4099
4100 /* Check that there are no *varargs types of arguments. */
4101 argsl = asdl_seq_LEN(args);
4102 for (i = 0; i < argsl; i++) {
4103 expr_ty elt = asdl_seq_GET(args, i);
4104 if (elt->kind == Starred_kind) {
4105 return -1;
4106 }
4107 }
4108
4109 /* Alright, we can optimize the code. */
4110 VISIT(c, expr, meth->v.Attribute.value);
4111 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
4112 VISIT_SEQ(c, expr, e->v.Call.args);
4113 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
4114 return 1;
4115}
4116
4117static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004118validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
Zackery Spytz08050e92020-04-06 00:47:47 -06004119{
4120 Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
4121 for (Py_ssize_t i = 0; i < nkeywords; i++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004122 keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
4123 if (key->arg == NULL) {
4124 continue;
4125 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +01004126 if (forbidden_name(c, key->arg, Store)) {
4127 return -1;
4128 }
Zackery Spytz08050e92020-04-06 00:47:47 -06004129 for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004130 keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
4131 if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
4132 PyObject *msg = PyUnicode_FromFormat("keyword argument repeated: %U", key->arg);
4133 if (msg == NULL) {
4134 return -1;
4135 }
4136 c->u->u_col_offset = other->col_offset;
4137 compiler_error(c, PyUnicode_AsUTF8(msg));
4138 Py_DECREF(msg);
4139 return -1;
4140 }
4141 }
4142 }
4143 return 0;
4144}
4145
4146static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004147compiler_call(struct compiler *c, expr_ty e)
4148{
Zackery Spytz97f5de02019-03-22 01:30:32 -06004149 int ret = maybe_optimize_method_call(c, e);
4150 if (ret >= 0) {
4151 return ret;
4152 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004153 if (!check_caller(c, e->v.Call.func)) {
4154 return 0;
4155 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004156 VISIT(c, expr, e->v.Call.func);
4157 return compiler_call_helper(c, 0,
4158 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004159 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004160}
4161
Eric V. Smith235a6f02015-09-19 14:51:32 -04004162static int
4163compiler_joined_str(struct compiler *c, expr_ty e)
4164{
Eric V. Smith235a6f02015-09-19 14:51:32 -04004165 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004166 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1)
4167 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
Eric V. Smith235a6f02015-09-19 14:51:32 -04004168 return 1;
4169}
4170
Eric V. Smitha78c7952015-11-03 12:45:05 -05004171/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004172static int
4173compiler_formatted_value(struct compiler *c, expr_ty e)
4174{
Eric V. Smitha78c7952015-11-03 12:45:05 -05004175 /* Our oparg encodes 2 pieces of information: the conversion
4176 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04004177
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004178 Convert the conversion char to 3 bits:
4179 : 000 0x0 FVC_NONE The default if nothing specified.
Eric V. Smitha78c7952015-11-03 12:45:05 -05004180 !s : 001 0x1 FVC_STR
4181 !r : 010 0x2 FVC_REPR
4182 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04004183
Eric V. Smitha78c7952015-11-03 12:45:05 -05004184 next bit is whether or not we have a format spec:
4185 yes : 100 0x4
4186 no : 000 0x0
4187 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004188
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004189 int conversion = e->v.FormattedValue.conversion;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004190 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004191
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004192 /* The expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004193 VISIT(c, expr, e->v.FormattedValue.value);
4194
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004195 switch (conversion) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004196 case 's': oparg = FVC_STR; break;
4197 case 'r': oparg = FVC_REPR; break;
4198 case 'a': oparg = FVC_ASCII; break;
4199 case -1: oparg = FVC_NONE; break;
4200 default:
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004201 PyErr_Format(PyExc_SystemError,
4202 "Unrecognized conversion character %d", conversion);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004203 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004204 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004205 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004206 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004207 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004208 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004209 }
4210
Eric V. Smitha78c7952015-11-03 12:45:05 -05004211 /* And push our opcode and oparg */
4212 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004213
Eric V. Smith235a6f02015-09-19 14:51:32 -04004214 return 1;
4215}
4216
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004217static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004218compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004219{
4220 Py_ssize_t i, n = end - begin;
4221 keyword_ty kw;
4222 PyObject *keys, *key;
4223 assert(n > 0);
4224 if (n > 1) {
4225 for (i = begin; i < end; i++) {
4226 kw = asdl_seq_GET(keywords, i);
4227 VISIT(c, expr, kw->value);
4228 }
4229 keys = PyTuple_New(n);
4230 if (keys == NULL) {
4231 return 0;
4232 }
4233 for (i = begin; i < end; i++) {
4234 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
4235 Py_INCREF(key);
4236 PyTuple_SET_ITEM(keys, i - begin, key);
4237 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004238 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004239 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
4240 }
4241 else {
4242 /* a for loop only executes once */
4243 for (i = begin; i < end; i++) {
4244 kw = asdl_seq_GET(keywords, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004245 ADDOP_LOAD_CONST(c, kw->arg);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004246 VISIT(c, expr, kw->value);
4247 }
4248 ADDOP_I(c, BUILD_MAP, n);
4249 }
4250 return 1;
4251}
4252
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004253/* shared code between compiler_call and compiler_class */
4254static int
4255compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01004256 int n, /* Args already pushed */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004257 asdl_expr_seq *args,
4258 asdl_keyword_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004259{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004260 Py_ssize_t i, nseen, nelts, nkwelts;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004261
Pablo Galindo254ec782020-04-03 20:37:13 +01004262 if (validate_keywords(c, keywords) == -1) {
4263 return 0;
4264 }
4265
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004266 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004267 nkwelts = asdl_seq_LEN(keywords);
4268
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004269 for (i = 0; i < nelts; i++) {
4270 expr_ty elt = asdl_seq_GET(args, i);
4271 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00004272 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004273 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004274 }
4275 for (i = 0; i < nkwelts; i++) {
4276 keyword_ty kw = asdl_seq_GET(keywords, i);
4277 if (kw->arg == NULL) {
4278 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004279 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004280 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004281
Mark Shannon13bc1392020-01-23 09:25:17 +00004282 /* No * or ** args, so can use faster calling sequence */
4283 for (i = 0; i < nelts; i++) {
4284 expr_ty elt = asdl_seq_GET(args, i);
4285 assert(elt->kind != Starred_kind);
4286 VISIT(c, expr, elt);
4287 }
4288 if (nkwelts) {
4289 PyObject *names;
4290 VISIT_SEQ(c, keyword, keywords);
4291 names = PyTuple_New(nkwelts);
4292 if (names == NULL) {
4293 return 0;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004294 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004295 for (i = 0; i < nkwelts; i++) {
4296 keyword_ty kw = asdl_seq_GET(keywords, i);
4297 Py_INCREF(kw->arg);
4298 PyTuple_SET_ITEM(names, i, kw->arg);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004299 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004300 ADDOP_LOAD_CONST_NEW(c, names);
4301 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
4302 return 1;
4303 }
4304 else {
4305 ADDOP_I(c, CALL_FUNCTION, n + nelts);
4306 return 1;
4307 }
4308
4309ex_call:
4310
4311 /* Do positional arguments. */
4312 if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
4313 VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
4314 }
4315 else if (starunpack_helper(c, args, n, BUILD_LIST,
4316 LIST_APPEND, LIST_EXTEND, 1) == 0) {
4317 return 0;
4318 }
4319 /* Then keyword arguments */
4320 if (nkwelts) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004321 /* Has a new dict been pushed */
4322 int have_dict = 0;
Mark Shannon13bc1392020-01-23 09:25:17 +00004323
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004324 nseen = 0; /* the number of keyword arguments on the stack following */
4325 for (i = 0; i < nkwelts; i++) {
4326 keyword_ty kw = asdl_seq_GET(keywords, i);
4327 if (kw->arg == NULL) {
4328 /* A keyword argument unpacking. */
4329 if (nseen) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004330 if (!compiler_subkwargs(c, keywords, i - nseen, i)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004331 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004332 }
Mark Shannondb64f122020-06-01 10:42:42 +01004333 if (have_dict) {
4334 ADDOP_I(c, DICT_MERGE, 1);
4335 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004336 have_dict = 1;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004337 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004338 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004339 if (!have_dict) {
4340 ADDOP_I(c, BUILD_MAP, 0);
4341 have_dict = 1;
4342 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004343 VISIT(c, expr, kw->value);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004344 ADDOP_I(c, DICT_MERGE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004345 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004346 else {
4347 nseen++;
4348 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004349 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004350 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004351 /* Pack up any trailing keyword arguments. */
Mark Shannon8a4cd702020-01-27 09:57:45 +00004352 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004353 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004354 }
4355 if (have_dict) {
4356 ADDOP_I(c, DICT_MERGE, 1);
4357 }
4358 have_dict = 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004359 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004360 assert(have_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004361 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004362 ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0);
4363 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004364}
4365
Nick Coghlan650f0d02007-04-15 12:05:43 +00004366
4367/* List and set comprehensions and generator expressions work by creating a
4368 nested function to perform the actual iteration. This means that the
4369 iteration variables don't leak into the current scope.
4370 The defined function is called immediately following its definition, with the
4371 result of that call being the result of the expression.
4372 The LC/SC version returns the populated container, while the GE version is
4373 flagged in symtable.c as a generator, so it returns the generator object
4374 when the function is called.
Nick Coghlan650f0d02007-04-15 12:05:43 +00004375
4376 Possible cleanups:
4377 - iterate over the generator sequence instead of using recursion
4378*/
4379
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004380
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004381static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004382compiler_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004383 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004384 int depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004385 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004386{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004387 comprehension_ty gen;
4388 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4389 if (gen->is_async) {
4390 return compiler_async_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004391 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004392 } else {
4393 return compiler_sync_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004394 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004395 }
4396}
4397
4398static int
4399compiler_sync_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004400 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004401 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004402 expr_ty elt, expr_ty val, int type)
4403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004404 /* generate code for the iterator, then each of the ifs,
4405 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004407 comprehension_ty gen;
4408 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004409 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004411 start = compiler_new_block(c);
4412 skip = compiler_new_block(c);
4413 if_cleanup = compiler_new_block(c);
4414 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004416 if (start == NULL || skip == NULL || if_cleanup == NULL ||
4417 anchor == NULL)
4418 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004420 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004422 if (gen_index == 0) {
4423 /* Receive outermost iter as an implicit argument */
4424 c->u->u_argcount = 1;
4425 ADDOP_I(c, LOAD_FAST, 0);
4426 }
4427 else {
4428 /* Sub-iter - calculate on the fly */
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004429 /* Fast path for the temporary variable assignment idiom:
4430 for y in [f(x)]
4431 */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004432 asdl_expr_seq *elts;
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004433 switch (gen->iter->kind) {
4434 case List_kind:
4435 elts = gen->iter->v.List.elts;
4436 break;
4437 case Tuple_kind:
4438 elts = gen->iter->v.Tuple.elts;
4439 break;
4440 default:
4441 elts = NULL;
4442 }
4443 if (asdl_seq_LEN(elts) == 1) {
4444 expr_ty elt = asdl_seq_GET(elts, 0);
4445 if (elt->kind != Starred_kind) {
4446 VISIT(c, expr, elt);
4447 start = NULL;
4448 }
4449 }
4450 if (start) {
4451 VISIT(c, expr, gen->iter);
4452 ADDOP(c, GET_ITER);
4453 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004454 }
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004455 if (start) {
4456 depth++;
4457 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01004458 ADDOP_JUMP(c, FOR_ITER, anchor);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004459 NEXT_BLOCK(c);
4460 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004461 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004463 /* XXX this needs to be cleaned up...a lot! */
4464 n = asdl_seq_LEN(gen->ifs);
4465 for (i = 0; i < n; i++) {
4466 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004467 if (!compiler_jump_if(c, e, if_cleanup, 0))
4468 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004469 NEXT_BLOCK(c);
4470 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004472 if (++gen_index < asdl_seq_LEN(generators))
4473 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004474 generators, gen_index, depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004475 elt, val, type))
4476 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004478 /* only append after the last for generator */
4479 if (gen_index >= asdl_seq_LEN(generators)) {
4480 /* comprehension specific code */
4481 switch (type) {
4482 case COMP_GENEXP:
4483 VISIT(c, expr, elt);
4484 ADDOP(c, YIELD_VALUE);
4485 ADDOP(c, POP_TOP);
4486 break;
4487 case COMP_LISTCOMP:
4488 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004489 ADDOP_I(c, LIST_APPEND, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004490 break;
4491 case COMP_SETCOMP:
4492 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004493 ADDOP_I(c, SET_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004494 break;
4495 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004496 /* With '{k: v}', k is evaluated before v, so we do
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004497 the same. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004499 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004500 ADDOP_I(c, MAP_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 break;
4502 default:
4503 return 0;
4504 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004506 compiler_use_next_block(c, skip);
4507 }
4508 compiler_use_next_block(c, if_cleanup);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004509 if (start) {
Mark Shannon582aaf12020-08-04 17:30:11 +01004510 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004511 compiler_use_next_block(c, anchor);
4512 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004513
4514 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004515}
4516
4517static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004518compiler_async_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004519 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004520 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004521 expr_ty elt, expr_ty val, int type)
4522{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004523 comprehension_ty gen;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004524 basicblock *start, *if_cleanup, *except;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004525 Py_ssize_t i, n;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004526 start = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004527 except = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004528 if_cleanup = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004529
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004530 if (start == NULL || if_cleanup == NULL || except == NULL) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004531 return 0;
4532 }
4533
4534 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4535
4536 if (gen_index == 0) {
4537 /* Receive outermost iter as an implicit argument */
4538 c->u->u_argcount = 1;
4539 ADDOP_I(c, LOAD_FAST, 0);
4540 }
4541 else {
4542 /* Sub-iter - calculate on the fly */
4543 VISIT(c, expr, gen->iter);
4544 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004545 }
4546
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004547 compiler_use_next_block(c, start);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004548
Mark Shannon582aaf12020-08-04 17:30:11 +01004549 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004550 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004551 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004552 ADDOP(c, YIELD_FROM);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004553 ADDOP(c, POP_BLOCK);
Serhiy Storchaka24d32012018-03-10 18:22:34 +02004554 VISIT(c, expr, gen->target);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004555
4556 n = asdl_seq_LEN(gen->ifs);
4557 for (i = 0; i < n; i++) {
4558 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004559 if (!compiler_jump_if(c, e, if_cleanup, 0))
4560 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004561 NEXT_BLOCK(c);
4562 }
4563
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004564 depth++;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004565 if (++gen_index < asdl_seq_LEN(generators))
4566 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004567 generators, gen_index, depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004568 elt, val, type))
4569 return 0;
4570
4571 /* only append after the last for generator */
4572 if (gen_index >= asdl_seq_LEN(generators)) {
4573 /* comprehension specific code */
4574 switch (type) {
4575 case COMP_GENEXP:
4576 VISIT(c, expr, elt);
4577 ADDOP(c, YIELD_VALUE);
4578 ADDOP(c, POP_TOP);
4579 break;
4580 case COMP_LISTCOMP:
4581 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004582 ADDOP_I(c, LIST_APPEND, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004583 break;
4584 case COMP_SETCOMP:
4585 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004586 ADDOP_I(c, SET_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004587 break;
4588 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004589 /* With '{k: v}', k is evaluated before v, so we do
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004590 the same. */
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004591 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004592 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004593 ADDOP_I(c, MAP_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004594 break;
4595 default:
4596 return 0;
4597 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004598 }
4599 compiler_use_next_block(c, if_cleanup);
Mark Shannon582aaf12020-08-04 17:30:11 +01004600 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004601
4602 compiler_use_next_block(c, except);
4603 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004604
4605 return 1;
4606}
4607
4608static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004609compiler_comprehension(struct compiler *c, expr_ty e, int type,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004610 identifier name, asdl_comprehension_seq *generators, expr_ty elt,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004611 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004613 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004614 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004615 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004616 int is_async_generator = 0;
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004617 int top_level_await = IS_TOP_LEVEL_AWAIT(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004618
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004619
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004620 int is_async_function = c->u->u_ste->ste_coroutine;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004621
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004622 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004623 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4624 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004625 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004626 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004627 }
4628
4629 is_async_generator = c->u->u_ste->ste_coroutine;
4630
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004631 if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004632 compiler_error(c, "asynchronous comprehension outside of "
4633 "an asynchronous function");
4634 goto error_in_scope;
4635 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004637 if (type != COMP_GENEXP) {
4638 int op;
4639 switch (type) {
4640 case COMP_LISTCOMP:
4641 op = BUILD_LIST;
4642 break;
4643 case COMP_SETCOMP:
4644 op = BUILD_SET;
4645 break;
4646 case COMP_DICTCOMP:
4647 op = BUILD_MAP;
4648 break;
4649 default:
4650 PyErr_Format(PyExc_SystemError,
4651 "unknown comprehension type %d", type);
4652 goto error_in_scope;
4653 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004655 ADDOP_I(c, op, 0);
4656 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004657
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004658 if (!compiler_comprehension_generator(c, generators, 0, 0, elt,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004659 val, type))
4660 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004662 if (type != COMP_GENEXP) {
4663 ADDOP(c, RETURN_VALUE);
4664 }
4665
4666 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004667 qualname = c->u->u_qualname;
4668 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004669 compiler_exit_scope(c);
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004670 if (top_level_await && is_async_generator){
4671 c->u->u_ste->ste_coroutine = 1;
4672 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004673 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004674 goto error;
4675
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004676 if (!compiler_make_closure(c, co, 0, qualname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004677 goto error;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004678 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004679 Py_DECREF(co);
4680
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004681 VISIT(c, expr, outermost->iter);
4682
4683 if (outermost->is_async) {
4684 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004685 } else {
4686 ADDOP(c, GET_ITER);
4687 }
4688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004689 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004690
4691 if (is_async_generator && type != COMP_GENEXP) {
4692 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004693 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004694 ADDOP(c, YIELD_FROM);
4695 }
4696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004697 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004698error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004699 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004700error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004701 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004702 Py_XDECREF(co);
4703 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004704}
4705
4706static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004707compiler_genexp(struct compiler *c, expr_ty e)
4708{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004709 static identifier name;
4710 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004711 name = PyUnicode_InternFromString("<genexpr>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004712 if (!name)
4713 return 0;
4714 }
4715 assert(e->kind == GeneratorExp_kind);
4716 return compiler_comprehension(c, e, COMP_GENEXP, name,
4717 e->v.GeneratorExp.generators,
4718 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004719}
4720
4721static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004722compiler_listcomp(struct compiler *c, expr_ty e)
4723{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004724 static identifier name;
4725 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004726 name = PyUnicode_InternFromString("<listcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004727 if (!name)
4728 return 0;
4729 }
4730 assert(e->kind == ListComp_kind);
4731 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4732 e->v.ListComp.generators,
4733 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004734}
4735
4736static int
4737compiler_setcomp(struct compiler *c, expr_ty e)
4738{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004739 static identifier name;
4740 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004741 name = PyUnicode_InternFromString("<setcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004742 if (!name)
4743 return 0;
4744 }
4745 assert(e->kind == SetComp_kind);
4746 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4747 e->v.SetComp.generators,
4748 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004749}
4750
4751
4752static int
4753compiler_dictcomp(struct compiler *c, expr_ty e)
4754{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004755 static identifier name;
4756 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004757 name = PyUnicode_InternFromString("<dictcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004758 if (!name)
4759 return 0;
4760 }
4761 assert(e->kind == DictComp_kind);
4762 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4763 e->v.DictComp.generators,
4764 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004765}
4766
4767
4768static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004769compiler_visit_keyword(struct compiler *c, keyword_ty k)
4770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004771 VISIT(c, expr, k->value);
4772 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004773}
4774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004775/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004776 whether they are true or false.
4777
4778 Return values: 1 for true, 0 for false, -1 for non-constant.
4779 */
4780
4781static int
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02004782expr_constant(expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004783{
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004784 if (e->kind == Constant_kind) {
4785 return PyObject_IsTrue(e->v.Constant.value);
Benjamin Peterson442f2092012-12-06 17:41:04 -05004786 }
Serhiy Storchaka3325a672017-12-15 12:35:48 +02004787 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004788}
4789
Mark Shannonfee55262019-11-21 09:11:43 +00004790static int
4791compiler_with_except_finish(struct compiler *c) {
4792 basicblock *exit;
4793 exit = compiler_new_block(c);
4794 if (exit == NULL)
4795 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01004796 ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00004797 ADDOP(c, RERAISE);
4798 compiler_use_next_block(c, exit);
4799 ADDOP(c, POP_TOP);
4800 ADDOP(c, POP_TOP);
4801 ADDOP(c, POP_TOP);
4802 ADDOP(c, POP_EXCEPT);
4803 ADDOP(c, POP_TOP);
4804 return 1;
4805}
Yury Selivanov75445082015-05-11 22:57:16 -04004806
4807/*
4808 Implements the async with statement.
4809
4810 The semantics outlined in that PEP are as follows:
4811
4812 async with EXPR as VAR:
4813 BLOCK
4814
4815 It is implemented roughly as:
4816
4817 context = EXPR
4818 exit = context.__aexit__ # not calling it
4819 value = await context.__aenter__()
4820 try:
4821 VAR = value # if VAR present in the syntax
4822 BLOCK
4823 finally:
4824 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004825 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04004826 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004827 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04004828 if not (await exit(*exc)):
4829 raise
4830 */
4831static int
4832compiler_async_with(struct compiler *c, stmt_ty s, int pos)
4833{
Mark Shannonfee55262019-11-21 09:11:43 +00004834 basicblock *block, *final, *exit;
Yury Selivanov75445082015-05-11 22:57:16 -04004835 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
4836
4837 assert(s->kind == AsyncWith_kind);
Pablo Galindo90235812020-03-15 04:29:22 +00004838 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07004839 c->u->u_ste->ste_coroutine = 1;
4840 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
Zsolt Dollensteine2396502018-04-27 08:58:56 -07004841 return compiler_error(c, "'async with' outside async function");
4842 }
Yury Selivanov75445082015-05-11 22:57:16 -04004843
4844 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004845 final = compiler_new_block(c);
4846 exit = compiler_new_block(c);
4847 if (!block || !final || !exit)
Yury Selivanov75445082015-05-11 22:57:16 -04004848 return 0;
4849
4850 /* Evaluate EXPR */
4851 VISIT(c, expr, item->context_expr);
4852
4853 ADDOP(c, BEFORE_ASYNC_WITH);
4854 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004855 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004856 ADDOP(c, YIELD_FROM);
4857
Mark Shannon582aaf12020-08-04 17:30:11 +01004858 ADDOP_JUMP(c, SETUP_ASYNC_WITH, final);
Yury Selivanov75445082015-05-11 22:57:16 -04004859
4860 /* SETUP_ASYNC_WITH pushes a finally block. */
4861 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004862 if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004863 return 0;
4864 }
4865
4866 if (item->optional_vars) {
4867 VISIT(c, expr, item->optional_vars);
4868 }
4869 else {
4870 /* Discard result from context.__aenter__() */
4871 ADDOP(c, POP_TOP);
4872 }
4873
4874 pos++;
4875 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
4876 /* BLOCK code */
4877 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
4878 else if (!compiler_async_with(c, s, pos))
4879 return 0;
4880
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004881 compiler_pop_fblock(c, ASYNC_WITH, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004882 ADDOP(c, POP_BLOCK);
4883 /* End of body; start the cleanup */
Yury Selivanov75445082015-05-11 22:57:16 -04004884
Mark Shannonfee55262019-11-21 09:11:43 +00004885 /* For successful outcome:
4886 * call __exit__(None, None, None)
4887 */
4888 if(!compiler_call_exit_with_nones(c))
Yury Selivanov75445082015-05-11 22:57:16 -04004889 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00004890 ADDOP(c, GET_AWAITABLE);
4891 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4892 ADDOP(c, YIELD_FROM);
Yury Selivanov75445082015-05-11 22:57:16 -04004893
Mark Shannonfee55262019-11-21 09:11:43 +00004894 ADDOP(c, POP_TOP);
Yury Selivanov75445082015-05-11 22:57:16 -04004895
Mark Shannon582aaf12020-08-04 17:30:11 +01004896 ADDOP_JUMP(c, JUMP_ABSOLUTE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00004897
4898 /* For exceptional outcome: */
4899 compiler_use_next_block(c, final);
4900
4901 ADDOP(c, WITH_EXCEPT_START);
Yury Selivanov75445082015-05-11 22:57:16 -04004902 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004903 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004904 ADDOP(c, YIELD_FROM);
Mark Shannonfee55262019-11-21 09:11:43 +00004905 compiler_with_except_finish(c);
Yury Selivanov75445082015-05-11 22:57:16 -04004906
Mark Shannonfee55262019-11-21 09:11:43 +00004907compiler_use_next_block(c, exit);
Yury Selivanov75445082015-05-11 22:57:16 -04004908 return 1;
4909}
4910
4911
Guido van Rossumc2e20742006-02-27 22:32:47 +00004912/*
4913 Implements the with statement from PEP 343.
Guido van Rossumc2e20742006-02-27 22:32:47 +00004914 with EXPR as VAR:
4915 BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00004916 is implemented as:
4917 <code for EXPR>
4918 SETUP_WITH E
4919 <code to store to VAR> or POP_TOP
4920 <code for BLOCK>
4921 LOAD_CONST (None, None, None)
4922 CALL_FUNCTION_EX 0
4923 JUMP_FORWARD EXIT
4924 E: WITH_EXCEPT_START (calls EXPR.__exit__)
4925 POP_JUMP_IF_TRUE T:
4926 RERAISE
4927 T: POP_TOP * 3 (remove exception from stack)
4928 POP_EXCEPT
4929 POP_TOP
4930 EXIT:
Guido van Rossumc2e20742006-02-27 22:32:47 +00004931 */
Mark Shannonfee55262019-11-21 09:11:43 +00004932
Guido van Rossumc2e20742006-02-27 22:32:47 +00004933static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004934compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004935{
Mark Shannonfee55262019-11-21 09:11:43 +00004936 basicblock *block, *final, *exit;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004937 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004938
4939 assert(s->kind == With_kind);
4940
Guido van Rossumc2e20742006-02-27 22:32:47 +00004941 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004942 final = compiler_new_block(c);
4943 exit = compiler_new_block(c);
4944 if (!block || !final || !exit)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004945 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004946
Thomas Wouters477c8d52006-05-27 19:21:47 +00004947 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004948 VISIT(c, expr, item->context_expr);
Mark Shannonfee55262019-11-21 09:11:43 +00004949 /* Will push bound __exit__ */
Mark Shannon582aaf12020-08-04 17:30:11 +01004950 ADDOP_JUMP(c, SETUP_WITH, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004951
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004952 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00004953 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004954 if (!compiler_push_fblock(c, WITH, block, final, NULL)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004955 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004956 }
4957
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004958 if (item->optional_vars) {
4959 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004960 }
4961 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004962 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004963 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004964 }
4965
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004966 pos++;
4967 if (pos == asdl_seq_LEN(s->v.With.items))
4968 /* BLOCK code */
4969 VISIT_SEQ(c, stmt, s->v.With.body)
4970 else if (!compiler_with(c, s, pos))
4971 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004972
Guido van Rossumc2e20742006-02-27 22:32:47 +00004973 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004974 compiler_pop_fblock(c, WITH, block);
Mark Shannon13bc1392020-01-23 09:25:17 +00004975
Mark Shannonfee55262019-11-21 09:11:43 +00004976 /* End of body; start the cleanup. */
Mark Shannon13bc1392020-01-23 09:25:17 +00004977
Mark Shannonfee55262019-11-21 09:11:43 +00004978 /* For successful outcome:
4979 * call __exit__(None, None, None)
4980 */
4981 if (!compiler_call_exit_with_nones(c))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004982 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00004983 ADDOP(c, POP_TOP);
Mark Shannon582aaf12020-08-04 17:30:11 +01004984 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004985
Mark Shannonfee55262019-11-21 09:11:43 +00004986 /* For exceptional outcome: */
4987 compiler_use_next_block(c, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004988
Mark Shannonfee55262019-11-21 09:11:43 +00004989 ADDOP(c, WITH_EXCEPT_START);
4990 compiler_with_except_finish(c);
4991
4992 compiler_use_next_block(c, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004993 return 1;
4994}
4995
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004996static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03004997compiler_visit_expr1(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004998{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004999 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07005000 case NamedExpr_kind:
5001 VISIT(c, expr, e->v.NamedExpr.value);
5002 ADDOP(c, DUP_TOP);
5003 VISIT(c, expr, e->v.NamedExpr.target);
5004 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005005 case BoolOp_kind:
5006 return compiler_boolop(c, e);
5007 case BinOp_kind:
5008 VISIT(c, expr, e->v.BinOp.left);
5009 VISIT(c, expr, e->v.BinOp.right);
Andy Lester76d58772020-03-10 21:18:12 -05005010 ADDOP(c, binop(e->v.BinOp.op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005011 break;
5012 case UnaryOp_kind:
5013 VISIT(c, expr, e->v.UnaryOp.operand);
5014 ADDOP(c, unaryop(e->v.UnaryOp.op));
5015 break;
5016 case Lambda_kind:
5017 return compiler_lambda(c, e);
5018 case IfExp_kind:
5019 return compiler_ifexp(c, e);
5020 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005021 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005022 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005023 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005024 case GeneratorExp_kind:
5025 return compiler_genexp(c, e);
5026 case ListComp_kind:
5027 return compiler_listcomp(c, e);
5028 case SetComp_kind:
5029 return compiler_setcomp(c, e);
5030 case DictComp_kind:
5031 return compiler_dictcomp(c, e);
5032 case Yield_kind:
5033 if (c->u->u_ste->ste_type != FunctionBlock)
5034 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005035 if (e->v.Yield.value) {
5036 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005037 }
5038 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005039 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005040 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005041 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005042 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005043 case YieldFrom_kind:
5044 if (c->u->u_ste->ste_type != FunctionBlock)
5045 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04005046
5047 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
5048 return compiler_error(c, "'yield from' inside async function");
5049
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005050 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04005051 ADDOP(c, GET_YIELD_FROM_ITER);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005052 ADDOP_LOAD_CONST(c, Py_None);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005053 ADDOP(c, YIELD_FROM);
5054 break;
Yury Selivanov75445082015-05-11 22:57:16 -04005055 case Await_kind:
Pablo Galindo90235812020-03-15 04:29:22 +00005056 if (!IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005057 if (c->u->u_ste->ste_type != FunctionBlock){
5058 return compiler_error(c, "'await' outside function");
5059 }
Yury Selivanov75445082015-05-11 22:57:16 -04005060
Victor Stinner331a6a52019-05-27 16:39:22 +02005061 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005062 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
5063 return compiler_error(c, "'await' outside async function");
5064 }
5065 }
Yury Selivanov75445082015-05-11 22:57:16 -04005066
5067 VISIT(c, expr, e->v.Await.value);
5068 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005069 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005070 ADDOP(c, YIELD_FROM);
5071 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005072 case Compare_kind:
5073 return compiler_compare(c, e);
5074 case Call_kind:
5075 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005076 case Constant_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005077 ADDOP_LOAD_CONST(c, e->v.Constant.value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005078 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005079 case JoinedStr_kind:
5080 return compiler_joined_str(c, e);
5081 case FormattedValue_kind:
5082 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005083 /* The following exprs can be assignment targets. */
5084 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005085 VISIT(c, expr, e->v.Attribute.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005086 switch (e->v.Attribute.ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005087 case Load:
5088 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
5089 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005090 case Store:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005091 if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx))
5092 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005093 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5094 break;
5095 case Del:
5096 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
5097 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005098 }
5099 break;
5100 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005101 return compiler_subscript(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005102 case Starred_kind:
5103 switch (e->v.Starred.ctx) {
5104 case Store:
5105 /* In all legitimate cases, the Starred node was already replaced
5106 * by compiler_list/compiler_tuple. XXX: is that okay? */
5107 return compiler_error(c,
5108 "starred assignment target must be in a list or tuple");
5109 default:
5110 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005111 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005112 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005113 break;
5114 case Slice_kind:
5115 return compiler_slice(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005116 case Name_kind:
5117 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
5118 /* child nodes of List and Tuple will have expr_context set */
5119 case List_kind:
5120 return compiler_list(c, e);
5121 case Tuple_kind:
5122 return compiler_tuple(c, e);
5123 }
5124 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005125}
5126
5127static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005128compiler_visit_expr(struct compiler *c, expr_ty e)
5129{
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005130 int old_lineno = c->u->u_lineno;
5131 int old_col_offset = c->u->u_col_offset;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005132 SET_LOC(c, e);
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005133 int res = compiler_visit_expr1(c, e);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005134 c->u->u_lineno = old_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005135 c->u->u_col_offset = old_col_offset;
5136 return res;
5137}
5138
5139static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005140compiler_augassign(struct compiler *c, stmt_ty s)
5141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005142 assert(s->kind == AugAssign_kind);
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005143 expr_ty e = s->v.AugAssign.target;
5144
5145 int old_lineno = c->u->u_lineno;
5146 int old_col_offset = c->u->u_col_offset;
5147 SET_LOC(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005149 switch (e->kind) {
5150 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005151 VISIT(c, expr, e->v.Attribute.value);
5152 ADDOP(c, DUP_TOP);
5153 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005154 break;
5155 case Subscript_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005156 VISIT(c, expr, e->v.Subscript.value);
5157 VISIT(c, expr, e->v.Subscript.slice);
5158 ADDOP(c, DUP_TOP_TWO);
5159 ADDOP(c, BINARY_SUBSCR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005160 break;
5161 case Name_kind:
5162 if (!compiler_nameop(c, e->v.Name.id, Load))
5163 return 0;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005164 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005165 default:
5166 PyErr_Format(PyExc_SystemError,
5167 "invalid node type (%d) for augmented assignment",
5168 e->kind);
5169 return 0;
5170 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005171
5172 c->u->u_lineno = old_lineno;
5173 c->u->u_col_offset = old_col_offset;
5174
5175 VISIT(c, expr, s->v.AugAssign.value);
5176 ADDOP(c, inplace_binop(s->v.AugAssign.op));
5177
5178 SET_LOC(c, e);
5179
5180 switch (e->kind) {
5181 case Attribute_kind:
5182 ADDOP(c, ROT_TWO);
5183 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5184 break;
5185 case Subscript_kind:
5186 ADDOP(c, ROT_THREE);
5187 ADDOP(c, STORE_SUBSCR);
5188 break;
5189 case Name_kind:
5190 return compiler_nameop(c, e->v.Name.id, Store);
5191 default:
5192 Py_UNREACHABLE();
5193 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005194 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005195}
5196
5197static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005198check_ann_expr(struct compiler *c, expr_ty e)
5199{
5200 VISIT(c, expr, e);
5201 ADDOP(c, POP_TOP);
5202 return 1;
5203}
5204
5205static int
5206check_annotation(struct compiler *c, stmt_ty s)
5207{
5208 /* Annotations are only evaluated in a module or class. */
5209 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5210 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
5211 return check_ann_expr(c, s->v.AnnAssign.annotation);
5212 }
5213 return 1;
5214}
5215
5216static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005217check_ann_subscr(struct compiler *c, expr_ty e)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005218{
5219 /* We check that everything in a subscript is defined at runtime. */
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005220 switch (e->kind) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005221 case Slice_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005222 if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005223 return 0;
5224 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005225 if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) {
5226 return 0;
5227 }
5228 if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) {
5229 return 0;
5230 }
5231 return 1;
5232 case Tuple_kind: {
5233 /* extended slice */
Pablo Galindoa5634c42020-09-16 19:42:00 +01005234 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005235 Py_ssize_t i, n = asdl_seq_LEN(elts);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005236 for (i = 0; i < n; i++) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005237 if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005238 return 0;
5239 }
5240 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005241 return 1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005242 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005243 default:
5244 return check_ann_expr(c, e);
5245 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005246}
5247
5248static int
5249compiler_annassign(struct compiler *c, stmt_ty s)
5250{
5251 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07005252 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005253
5254 assert(s->kind == AnnAssign_kind);
5255
5256 /* We perform the actual assignment first. */
5257 if (s->v.AnnAssign.value) {
5258 VISIT(c, expr, s->v.AnnAssign.value);
5259 VISIT(c, expr, targ);
5260 }
5261 switch (targ->kind) {
5262 case Name_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005263 if (forbidden_name(c, targ->v.Name.id, Store))
5264 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005265 /* If we have a simple name in a module or class, store annotation. */
5266 if (s->v.AnnAssign.simple &&
5267 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5268 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Batuhan Taskaya044a1042020-10-06 23:03:02 +03005269 VISIT(c, annexpr, s->v.AnnAssign.annotation);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005270 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02005271 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005272 ADDOP_LOAD_CONST_NEW(c, mangled);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005273 ADDOP(c, STORE_SUBSCR);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005274 }
5275 break;
5276 case Attribute_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005277 if (forbidden_name(c, targ->v.Attribute.attr, Store))
5278 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005279 if (!s->v.AnnAssign.value &&
5280 !check_ann_expr(c, targ->v.Attribute.value)) {
5281 return 0;
5282 }
5283 break;
5284 case Subscript_kind:
5285 if (!s->v.AnnAssign.value &&
5286 (!check_ann_expr(c, targ->v.Subscript.value) ||
5287 !check_ann_subscr(c, targ->v.Subscript.slice))) {
5288 return 0;
5289 }
5290 break;
5291 default:
5292 PyErr_Format(PyExc_SystemError,
5293 "invalid node type (%d) for annotated assignment",
5294 targ->kind);
5295 return 0;
5296 }
5297 /* Annotation is evaluated last. */
5298 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
5299 return 0;
5300 }
5301 return 1;
5302}
5303
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005304/* Raises a SyntaxError and returns 0.
5305 If something goes wrong, a different exception may be raised.
5306*/
5307
5308static int
5309compiler_error(struct compiler *c, const char *errstr)
5310{
Benjamin Peterson43b06862011-05-27 09:08:01 -05005311 PyObject *loc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005312 PyObject *u = NULL, *v = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005313
Victor Stinner14e461d2013-08-26 22:28:21 +02005314 loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005315 if (!loc) {
5316 Py_INCREF(Py_None);
5317 loc = Py_None;
5318 }
Victor Stinner14e461d2013-08-26 22:28:21 +02005319 u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04005320 c->u->u_col_offset + 1, loc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005321 if (!u)
5322 goto exit;
5323 v = Py_BuildValue("(zO)", errstr, u);
5324 if (!v)
5325 goto exit;
5326 PyErr_SetObject(PyExc_SyntaxError, v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005327 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005328 Py_DECREF(loc);
5329 Py_XDECREF(u);
5330 Py_XDECREF(v);
5331 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005332}
5333
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005334/* Emits a SyntaxWarning and returns 1 on success.
5335 If a SyntaxWarning raised as error, replaces it with a SyntaxError
5336 and returns 0.
5337*/
5338static int
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005339compiler_warn(struct compiler *c, const char *format, ...)
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005340{
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005341 va_list vargs;
5342#ifdef HAVE_STDARG_PROTOTYPES
5343 va_start(vargs, format);
5344#else
5345 va_start(vargs);
5346#endif
5347 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5348 va_end(vargs);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005349 if (msg == NULL) {
5350 return 0;
5351 }
5352 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
5353 c->u->u_lineno, NULL, NULL) < 0)
5354 {
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005355 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005356 /* Replace the SyntaxWarning exception with a SyntaxError
5357 to get a more accurate error report */
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005358 PyErr_Clear();
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005359 assert(PyUnicode_AsUTF8(msg) != NULL);
5360 compiler_error(c, PyUnicode_AsUTF8(msg));
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005361 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005362 Py_DECREF(msg);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005363 return 0;
5364 }
5365 Py_DECREF(msg);
5366 return 1;
5367}
5368
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005369static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005370compiler_subscript(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005371{
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005372 expr_context_ty ctx = e->v.Subscript.ctx;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005373 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005374
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005375 if (ctx == Load) {
5376 if (!check_subscripter(c, e->v.Subscript.value)) {
5377 return 0;
5378 }
5379 if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
5380 return 0;
5381 }
5382 }
5383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005384 switch (ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005385 case Load: op = BINARY_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005386 case Store: op = STORE_SUBSCR; break;
5387 case Del: op = DELETE_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005388 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005389 assert(op);
5390 VISIT(c, expr, e->v.Subscript.value);
5391 VISIT(c, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005392 ADDOP(c, op);
5393 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005394}
5395
5396static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005397compiler_slice(struct compiler *c, expr_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005399 int n = 2;
5400 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005402 /* only handles the cases where BUILD_SLICE is emitted */
5403 if (s->v.Slice.lower) {
5404 VISIT(c, expr, s->v.Slice.lower);
5405 }
5406 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005407 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005408 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005410 if (s->v.Slice.upper) {
5411 VISIT(c, expr, s->v.Slice.upper);
5412 }
5413 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005414 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005415 }
5416
5417 if (s->v.Slice.step) {
5418 n++;
5419 VISIT(c, expr, s->v.Slice.step);
5420 }
5421 ADDOP_I(c, BUILD_SLICE, n);
5422 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005423}
5424
Thomas Wouters89f507f2006-12-13 04:49:30 +00005425/* End of the compiler section, beginning of the assembler section */
5426
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005427/* do depth-first search of basic block graph, starting with block.
T. Wouters99b54d62019-09-12 07:05:33 -07005428 post records the block indices in post-order.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005429
5430 XXX must handle implicit jumps from one block to next
5431*/
5432
Thomas Wouters89f507f2006-12-13 04:49:30 +00005433struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005434 PyObject *a_bytecode; /* string containing bytecode */
5435 int a_offset; /* offset into bytecode */
5436 int a_nblocks; /* number of reachable blocks */
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005437 basicblock **a_reverse_postorder; /* list of blocks in dfs postorder */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005438 PyObject *a_lnotab; /* string containing lnotab */
5439 int a_lnotab_off; /* offset into lnotab */
Mark Shannon877df852020-11-12 09:43:29 +00005440 int a_prevlineno; /* lineno of last emitted line in line table */
5441 int a_lineno; /* lineno of last emitted instruction */
5442 int a_lineno_start; /* bytecode start offset of current lineno */
Thomas Wouters89f507f2006-12-13 04:49:30 +00005443};
5444
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005445static void
T. Wouters99b54d62019-09-12 07:05:33 -07005446dfs(struct compiler *c, basicblock *b, struct assembler *a, int end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005447{
T. Wouters99b54d62019-09-12 07:05:33 -07005448
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005449 /* There is no real depth-first-search to do here because all the
5450 * blocks are emitted in topological order already, so we just need to
5451 * follow the b_next pointers and place them in a->a_reverse_postorder in
5452 * reverse order and make sure that the first one starts at 0. */
5453
5454 for (a->a_nblocks = 0; b != NULL; b = b->b_next) {
5455 a->a_reverse_postorder[a->a_nblocks++] = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005456 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005457}
5458
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005459Py_LOCAL_INLINE(void)
5460stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005461{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005462 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
Mark Shannonfee55262019-11-21 09:11:43 +00005463 if (b->b_startdepth < depth && b->b_startdepth < 100) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005464 assert(b->b_startdepth < 0);
5465 b->b_startdepth = depth;
5466 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02005467 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005468}
5469
5470/* Find the flow path that needs the largest stack. We assume that
5471 * cycles in the flow graph have no net effect on the stack depth.
5472 */
5473static int
5474stackdepth(struct compiler *c)
5475{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005476 basicblock *b, *entryblock = NULL;
5477 basicblock **stack, **sp;
5478 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005479 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005480 b->b_startdepth = INT_MIN;
5481 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005482 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005483 }
5484 if (!entryblock)
5485 return 0;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005486 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
5487 if (!stack) {
5488 PyErr_NoMemory();
5489 return -1;
5490 }
5491
5492 sp = stack;
5493 stackdepth_push(&sp, entryblock, 0);
5494 while (sp != stack) {
5495 b = *--sp;
5496 int depth = b->b_startdepth;
5497 assert(depth >= 0);
5498 basicblock *next = b->b_next;
5499 for (int i = 0; i < b->b_iused; i++) {
5500 struct instr *instr = &b->b_instr[i];
5501 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
5502 if (effect == PY_INVALID_STACK_EFFECT) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01005503 _Py_FatalErrorFormat(__func__,
5504 "opcode = %d", instr->i_opcode);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005505 }
5506 int new_depth = depth + effect;
5507 if (new_depth > maxdepth) {
5508 maxdepth = new_depth;
5509 }
5510 assert(depth >= 0); /* invalid code or bug in stackdepth() */
Mark Shannon582aaf12020-08-04 17:30:11 +01005511 if (is_jump(instr)) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005512 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
5513 assert(effect != PY_INVALID_STACK_EFFECT);
5514 int target_depth = depth + effect;
5515 if (target_depth > maxdepth) {
5516 maxdepth = target_depth;
5517 }
5518 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005519 stackdepth_push(&sp, instr->i_target, target_depth);
5520 }
5521 depth = new_depth;
5522 if (instr->i_opcode == JUMP_ABSOLUTE ||
5523 instr->i_opcode == JUMP_FORWARD ||
5524 instr->i_opcode == RETURN_VALUE ||
Mark Shannonfee55262019-11-21 09:11:43 +00005525 instr->i_opcode == RAISE_VARARGS ||
5526 instr->i_opcode == RERAISE)
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005527 {
5528 /* remaining code is dead */
5529 next = NULL;
5530 break;
5531 }
5532 }
5533 if (next != NULL) {
5534 stackdepth_push(&sp, next, depth);
5535 }
5536 }
5537 PyObject_Free(stack);
5538 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005539}
5540
5541static int
5542assemble_init(struct assembler *a, int nblocks, int firstlineno)
5543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005544 memset(a, 0, sizeof(struct assembler));
Mark Shannon877df852020-11-12 09:43:29 +00005545 a->a_prevlineno = a->a_lineno = firstlineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005546 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
5547 if (!a->a_bytecode)
5548 return 0;
5549 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
5550 if (!a->a_lnotab)
5551 return 0;
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07005552 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005553 PyErr_NoMemory();
5554 return 0;
5555 }
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005556 a->a_reverse_postorder = (basicblock **)PyObject_Malloc(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005557 sizeof(basicblock *) * nblocks);
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005558 if (!a->a_reverse_postorder) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005559 PyErr_NoMemory();
5560 return 0;
5561 }
5562 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005563}
5564
5565static void
5566assemble_free(struct assembler *a)
5567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005568 Py_XDECREF(a->a_bytecode);
5569 Py_XDECREF(a->a_lnotab);
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005570 if (a->a_reverse_postorder)
5571 PyObject_Free(a->a_reverse_postorder);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005572}
5573
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005574static int
5575blocksize(basicblock *b)
5576{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005577 int i;
5578 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005580 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005581 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005582 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005583}
5584
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005585static int
Mark Shannon877df852020-11-12 09:43:29 +00005586assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005587{
Mark Shannon877df852020-11-12 09:43:29 +00005588 Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005589 if (a->a_lnotab_off + 2 >= len) {
5590 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
5591 return 0;
5592 }
Mark Shannon877df852020-11-12 09:43:29 +00005593 unsigned char *lnotab = (unsigned char *)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005594 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00005595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005596 a->a_lnotab_off += 2;
Mark Shannon877df852020-11-12 09:43:29 +00005597 *lnotab++ = bdelta;
5598 *lnotab++ = ldelta;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005599 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005600}
5601
Mark Shannon877df852020-11-12 09:43:29 +00005602/* Appends a range to the end of the line number table. See
5603 * Objects/lnotab_notes.txt for the description of the line number table. */
5604
5605static int
5606assemble_line_range(struct assembler *a)
5607{
5608 int ldelta, bdelta;
5609 bdelta = (a->a_offset - a->a_lineno_start) * 2;
5610 if (bdelta == 0) {
5611 return 1;
5612 }
5613 if (a->a_lineno < 0) {
5614 ldelta = -128;
5615 }
5616 else {
5617 ldelta = a->a_lineno - a->a_prevlineno;
5618 a->a_prevlineno = a->a_lineno;
5619 while (ldelta > 127) {
5620 if (!assemble_emit_linetable_pair(a, 0, 127)) {
5621 return 0;
5622 }
5623 ldelta -= 127;
5624 }
5625 while (ldelta < -127) {
5626 if (!assemble_emit_linetable_pair(a, 0, -127)) {
5627 return 0;
5628 }
5629 ldelta += 127;
5630 }
5631 }
5632 assert(-128 <= ldelta && ldelta < 128);
5633 while (bdelta > 254) {
5634 if (!assemble_emit_linetable_pair(a, 254, ldelta)) {
5635 return 0;
5636 }
5637 ldelta = a->a_lineno < 0 ? -128 : 0;
5638 bdelta -= 254;
5639 }
5640 if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) {
5641 return 0;
5642 }
5643 a->a_lineno_start = a->a_offset;
5644 return 1;
5645}
5646
5647static int
5648assemble_lnotab(struct assembler *a, struct instr *i)
5649{
5650 if (i->i_lineno == a->a_lineno) {
5651 return 1;
5652 }
5653 if (!assemble_line_range(a)) {
5654 return 0;
5655 }
5656 a->a_lineno = i->i_lineno;
5657 return 1;
5658}
5659
5660
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005661/* assemble_emit()
5662 Extend the bytecode with a new instruction.
5663 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00005664*/
5665
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00005666static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005667assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00005668{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005669 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005670 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03005671 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005672
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005673 arg = i->i_oparg;
5674 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005675 if (i->i_lineno && !assemble_lnotab(a, i))
5676 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005677 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005678 if (len > PY_SSIZE_T_MAX / 2)
5679 return 0;
5680 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
5681 return 0;
5682 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03005683 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005684 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005685 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005686 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00005687}
5688
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00005689static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005690assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00005691{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005692 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005693 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005694 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00005695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005696 /* Compute the size of each block and fixup jump args.
5697 Replace block pointer with position in bytecode. */
5698 do {
5699 totsize = 0;
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005700 for (i = 0; i < a->a_nblocks; i++) {
5701 b = a->a_reverse_postorder[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005702 bsize = blocksize(b);
5703 b->b_offset = totsize;
5704 totsize += bsize;
5705 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005706 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005707 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
5708 bsize = b->b_offset;
5709 for (i = 0; i < b->b_iused; i++) {
5710 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005711 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005712 /* Relative jumps are computed relative to
5713 the instruction pointer after fetching
5714 the jump instruction.
5715 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005716 bsize += isize;
Mark Shannon582aaf12020-08-04 17:30:11 +01005717 if (is_jump(instr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005718 instr->i_oparg = instr->i_target->b_offset;
Mark Shannon582aaf12020-08-04 17:30:11 +01005719 if (is_relative_jump(instr)) {
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005720 instr->i_oparg -= bsize;
5721 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03005722 instr->i_oparg *= sizeof(_Py_CODEUNIT);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005723 if (instrsize(instr->i_oparg) != isize) {
5724 extended_arg_recompile = 1;
5725 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005726 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005727 }
5728 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00005729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005730 /* XXX: This is an awful hack that could hurt performance, but
5731 on the bright side it should work until we come up
5732 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00005733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005734 The issue is that in the first loop blocksize() is called
5735 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005736 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005737 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00005738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005739 So we loop until we stop seeing new EXTENDED_ARGs.
5740 The only EXTENDED_ARGs that could be popping up are
5741 ones in jump instructions. So this should converge
5742 fairly quickly.
5743 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005744 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005745}
5746
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005747static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01005748dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005749{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005750 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02005751 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005753 tuple = PyTuple_New(size);
5754 if (tuple == NULL)
5755 return NULL;
5756 while (PyDict_Next(dict, &pos, &k, &v)) {
5757 i = PyLong_AS_LONG(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005758 Py_INCREF(k);
5759 assert((i - offset) < size);
5760 assert((i - offset) >= 0);
5761 PyTuple_SET_ITEM(tuple, i - offset, k);
5762 }
5763 return tuple;
5764}
5765
5766static PyObject *
5767consts_dict_keys_inorder(PyObject *dict)
5768{
5769 PyObject *consts, *k, *v;
5770 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
5771
5772 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
5773 if (consts == NULL)
5774 return NULL;
5775 while (PyDict_Next(dict, &pos, &k, &v)) {
5776 i = PyLong_AS_LONG(v);
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +03005777 /* The keys of the dictionary can be tuples wrapping a contant.
5778 * (see compiler_add_o and _PyCode_ConstantKey). In that case
5779 * the object we want is always second. */
5780 if (PyTuple_CheckExact(k)) {
5781 k = PyTuple_GET_ITEM(k, 1);
5782 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005783 Py_INCREF(k);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005784 assert(i < size);
5785 assert(i >= 0);
5786 PyList_SET_ITEM(consts, i, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005787 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005788 return consts;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005789}
5790
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005791static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005792compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005794 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005795 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005796 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04005797 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005798 if (ste->ste_nested)
5799 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07005800 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005801 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07005802 if (!ste->ste_generator && ste->ste_coroutine)
5803 flags |= CO_COROUTINE;
5804 if (ste->ste_generator && ste->ste_coroutine)
5805 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005806 if (ste->ste_varargs)
5807 flags |= CO_VARARGS;
5808 if (ste->ste_varkeywords)
5809 flags |= CO_VARKEYWORDS;
5810 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00005811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005812 /* (Only) inherit compilerflags in PyCF_MASK */
5813 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00005814
Pablo Galindo90235812020-03-15 04:29:22 +00005815 if ((IS_TOP_LEVEL_AWAIT(c)) &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005816 ste->ste_coroutine &&
5817 !ste->ste_generator) {
5818 flags |= CO_COROUTINE;
5819 }
5820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005821 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005822}
5823
INADA Naokic2e16072018-11-26 21:23:22 +09005824// Merge *tuple* with constant cache.
5825// Unlike merge_consts_recursive(), this function doesn't work recursively.
5826static int
5827merge_const_tuple(struct compiler *c, PyObject **tuple)
5828{
5829 assert(PyTuple_CheckExact(*tuple));
5830
5831 PyObject *key = _PyCode_ConstantKey(*tuple);
5832 if (key == NULL) {
5833 return 0;
5834 }
5835
5836 // t is borrowed reference
5837 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
5838 Py_DECREF(key);
5839 if (t == NULL) {
5840 return 0;
5841 }
5842 if (t == key) { // tuple is new constant.
5843 return 1;
5844 }
5845
5846 PyObject *u = PyTuple_GET_ITEM(t, 1);
5847 Py_INCREF(u);
5848 Py_DECREF(*tuple);
5849 *tuple = u;
5850 return 1;
5851}
5852
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005853static PyCodeObject *
Mark Shannon6e8128f2020-07-30 10:03:00 +01005854makecode(struct compiler *c, struct assembler *a, PyObject *consts)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005855{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005856 PyCodeObject *co = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005857 PyObject *names = NULL;
5858 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005859 PyObject *name = NULL;
5860 PyObject *freevars = NULL;
5861 PyObject *cellvars = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005862 Py_ssize_t nlocals;
5863 int nlocals_int;
5864 int flags;
Pablo Galindocd74e662019-06-01 18:08:04 +01005865 int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005867 names = dict_keys_inorder(c->u->u_names, 0);
5868 varnames = dict_keys_inorder(c->u->u_varnames, 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01005869 if (!names || !varnames) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005870 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01005871 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005872 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
5873 if (!cellvars)
5874 goto error;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005875 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005876 if (!freevars)
5877 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005878
INADA Naokic2e16072018-11-26 21:23:22 +09005879 if (!merge_const_tuple(c, &names) ||
5880 !merge_const_tuple(c, &varnames) ||
5881 !merge_const_tuple(c, &cellvars) ||
5882 !merge_const_tuple(c, &freevars))
5883 {
5884 goto error;
5885 }
5886
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02005887 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01005888 assert(nlocals < INT_MAX);
5889 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
5890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005891 flags = compute_code_flags(c);
5892 if (flags < 0)
5893 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005894
Mark Shannon6e8128f2020-07-30 10:03:00 +01005895 consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
5896 if (consts == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005897 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01005898 }
INADA Naokic2e16072018-11-26 21:23:22 +09005899 if (!merge_const_tuple(c, &consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01005900 Py_DECREF(consts);
INADA Naokic2e16072018-11-26 21:23:22 +09005901 goto error;
5902 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005903
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01005904 posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int);
Pablo Galindocd74e662019-06-01 18:08:04 +01005905 posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +01005906 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005907 maxdepth = stackdepth(c);
5908 if (maxdepth < 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01005909 Py_DECREF(consts);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005910 goto error;
5911 }
Pablo Galindo4a2edc32019-07-01 11:35:05 +01005912 co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
Mark Shannon13bc1392020-01-23 09:25:17 +00005913 posonlyargcount, kwonlyargcount, nlocals_int,
Mark Shannon6e8128f2020-07-30 10:03:00 +01005914 maxdepth, flags, a->a_bytecode, consts, names,
Pablo Galindo4a2edc32019-07-01 11:35:05 +01005915 varnames, freevars, cellvars, c->c_filename,
5916 c->u->u_name, c->u->u_firstlineno, a->a_lnotab);
Mark Shannon6e8128f2020-07-30 10:03:00 +01005917 Py_DECREF(consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005918 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005919 Py_XDECREF(names);
5920 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005921 Py_XDECREF(name);
5922 Py_XDECREF(freevars);
5923 Py_XDECREF(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005924 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005925}
5926
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005927
5928/* For debugging purposes only */
5929#if 0
5930static void
5931dump_instr(const struct instr *i)
5932{
Mark Shannon582aaf12020-08-04 17:30:11 +01005933 const char *jrel = (is_relative_jump(instr)) ? "jrel " : "";
5934 const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : "";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005935 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005937 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005938 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005939 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005940 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005941 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
5942 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005943}
5944
5945static void
5946dump_basicblock(const basicblock *b)
5947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005948 const char *b_return = b->b_return ? "return " : "";
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005949 fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n",
5950 b->b_iused, b->b_startdepth, b->b_offset, b_return);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005951 if (b->b_instr) {
5952 int i;
5953 for (i = 0; i < b->b_iused; i++) {
5954 fprintf(stderr, " [%02d] ", i);
5955 dump_instr(b->b_instr + i);
5956 }
5957 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005958}
5959#endif
5960
Mark Shannon6e8128f2020-07-30 10:03:00 +01005961static int
5962optimize_cfg(struct assembler *a, PyObject *consts);
5963
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005964static PyCodeObject *
5965assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005966{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005967 basicblock *b, *entryblock;
5968 struct assembler a;
5969 int i, j, nblocks;
5970 PyCodeObject *co = NULL;
Mark Shannon6e8128f2020-07-30 10:03:00 +01005971 PyObject *consts = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005973 /* Make sure every block that falls off the end returns None.
5974 XXX NEXT_BLOCK() isn't quite right, because if the last
5975 block ends with a jump or return b_next shouldn't set.
5976 */
5977 if (!c->u->u_curblock->b_return) {
Mark Shannon877df852020-11-12 09:43:29 +00005978 c->u->u_lineno = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005979 if (addNone)
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005980 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005981 ADDOP(c, RETURN_VALUE);
5982 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005984 nblocks = 0;
5985 entryblock = NULL;
5986 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
5987 nblocks++;
5988 entryblock = b;
5989 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005991 /* Set firstlineno if it wasn't explicitly set. */
5992 if (!c->u->u_firstlineno) {
Ned Deilydc35cda2016-08-17 17:18:33 -04005993 if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005994 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
Mark Shannon877df852020-11-12 09:43:29 +00005995 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005996 c->u->u_firstlineno = 1;
5997 }
5998 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
5999 goto error;
T. Wouters99b54d62019-09-12 07:05:33 -07006000 dfs(c, entryblock, &a, nblocks);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006001
Mark Shannon6e8128f2020-07-30 10:03:00 +01006002 consts = consts_dict_keys_inorder(c->u->u_consts);
6003 if (consts == NULL) {
6004 goto error;
6005 }
6006 if (optimize_cfg(&a, consts)) {
6007 goto error;
6008 }
6009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006010 /* Can't modify the bytecode after computing jump offsets. */
6011 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00006012
T. Wouters99b54d62019-09-12 07:05:33 -07006013 /* Emit code in reverse postorder from dfs. */
Pablo Galindo60eb9f12020-06-28 01:55:47 +01006014 for (i = 0; i < a.a_nblocks; i++) {
6015 b = a.a_reverse_postorder[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006016 for (j = 0; j < b->b_iused; j++)
6017 if (!assemble_emit(&a, &b->b_instr[j]))
6018 goto error;
6019 }
Mark Shannon877df852020-11-12 09:43:29 +00006020 if (!assemble_line_range(&a)) {
6021 return 0;
6022 }
6023 /* Emit sentinel at end of line number table */
6024 if (!assemble_emit_linetable_pair(&a, 255, -128)) {
6025 goto error;
6026 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00006027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006028 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
6029 goto error;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006030 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006031 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006032
Mark Shannon6e8128f2020-07-30 10:03:00 +01006033 co = makecode(c, &a, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006034 error:
Mark Shannon6e8128f2020-07-30 10:03:00 +01006035 Py_XDECREF(consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006036 assemble_free(&a);
6037 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006038}
Georg Brandl8334fd92010-12-04 10:26:46 +00006039
6040#undef PyAST_Compile
Benjamin Petersone5024512018-09-12 12:06:42 -07006041PyCodeObject *
Georg Brandl8334fd92010-12-04 10:26:46 +00006042PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
6043 PyArena *arena)
6044{
6045 return PyAST_CompileEx(mod, filename, flags, -1, arena);
6046}
Mark Shannon6e8128f2020-07-30 10:03:00 +01006047
6048
6049/* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
6050 with LOAD_CONST (c1, c2, ... cn).
6051 The consts table must still be in list form so that the
6052 new constant (c1, c2, ... cn) can be appended.
6053 Called with codestr pointing to the first LOAD_CONST.
6054*/
6055static int
6056fold_tuple_on_constants(struct instr *inst,
6057 int n, PyObject *consts)
6058{
6059 /* Pre-conditions */
6060 assert(PyList_CheckExact(consts));
6061 assert(inst[n].i_opcode == BUILD_TUPLE);
6062 assert(inst[n].i_oparg == n);
6063
6064 for (int i = 0; i < n; i++) {
6065 if (inst[i].i_opcode != LOAD_CONST) {
6066 return 0;
6067 }
6068 }
6069
6070 /* Buildup new tuple of constants */
6071 PyObject *newconst = PyTuple_New(n);
6072 if (newconst == NULL) {
6073 return -1;
6074 }
6075 for (int i = 0; i < n; i++) {
6076 int arg = inst[i].i_oparg;
6077 PyObject *constant = PyList_GET_ITEM(consts, arg);
6078 Py_INCREF(constant);
6079 PyTuple_SET_ITEM(newconst, i, constant);
6080 }
6081 Py_ssize_t index = PyList_GET_SIZE(consts);
Victor Stinner71f2ff42020-09-23 14:06:55 +02006082 if ((size_t)index >= (size_t)INT_MAX - 1) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006083 Py_DECREF(newconst);
6084 PyErr_SetString(PyExc_OverflowError, "too many constants");
6085 return -1;
6086 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006087 if (PyList_Append(consts, newconst)) {
6088 Py_DECREF(newconst);
6089 return -1;
6090 }
6091 Py_DECREF(newconst);
6092 for (int i = 0; i < n; i++) {
6093 inst[i].i_opcode = NOP;
6094 }
6095 inst[n].i_opcode = LOAD_CONST;
Victor Stinner71f2ff42020-09-23 14:06:55 +02006096 inst[n].i_oparg = (int)index;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006097 return 0;
6098}
6099
6100
6101/* Optimization */
6102static int
6103optimize_basic_block(basicblock *bb, PyObject *consts)
6104{
6105 assert(PyList_CheckExact(consts));
6106 struct instr nop;
6107 nop.i_opcode = NOP;
6108 struct instr *target;
6109 int lineno;
6110 for (int i = 0; i < bb->b_iused; i++) {
6111 struct instr *inst = &bb->b_instr[i];
6112 int oparg = inst->i_oparg;
6113 int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01006114 if (is_jump(inst)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006115 /* Skip over empty basic blocks. */
6116 while (inst->i_target->b_iused == 0) {
6117 inst->i_target = inst->i_target->b_next;
6118 }
6119 target = &inst->i_target->b_instr[0];
6120 }
6121 else {
6122 target = &nop;
6123 }
6124 switch (inst->i_opcode) {
6125 /* Skip over LOAD_CONST trueconst
6126 POP_JUMP_IF_FALSE xx. This improves
6127 "while 1" performance. */
6128 case LOAD_CONST:
6129 if (nextop != POP_JUMP_IF_FALSE) {
6130 break;
6131 }
6132 PyObject* cnt = PyList_GET_ITEM(consts, oparg);
6133 int is_true = PyObject_IsTrue(cnt);
6134 if (is_true == -1) {
6135 goto error;
6136 }
6137 if (is_true == 1) {
6138 inst->i_opcode = NOP;
6139 bb->b_instr[i+1].i_opcode = NOP;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006140 }
6141 break;
6142
6143 /* Try to fold tuples of constants.
6144 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
6145 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
6146 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
6147 case BUILD_TUPLE:
6148 if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
6149 switch(oparg) {
6150 case 1:
6151 inst->i_opcode = NOP;
6152 bb->b_instr[i+1].i_opcode = NOP;
6153 break;
6154 case 2:
6155 inst->i_opcode = ROT_TWO;
6156 bb->b_instr[i+1].i_opcode = NOP;
6157 break;
6158 case 3:
6159 inst->i_opcode = ROT_THREE;
6160 bb->b_instr[i+1].i_opcode = ROT_TWO;
6161 }
6162 break;
6163 }
6164 if (i >= oparg) {
6165 if (fold_tuple_on_constants(inst-oparg, oparg, consts)) {
6166 goto error;
6167 }
6168 }
6169 break;
6170
6171 /* Simplify conditional jump to conditional jump where the
6172 result of the first test implies the success of a similar
6173 test or the failure of the opposite test.
6174 Arises in code like:
6175 "a and b or c"
6176 "(a and b) and c"
6177 "(a or b) or c"
6178 "(a or b) and c"
6179 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
6180 --> x:JUMP_IF_FALSE_OR_POP z
6181 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
6182 --> x:POP_JUMP_IF_FALSE y+1
6183 where y+1 is the instruction following the second test.
6184 */
6185 case JUMP_IF_FALSE_OR_POP:
6186 switch(target->i_opcode) {
6187 case POP_JUMP_IF_FALSE:
6188 *inst = *target;
6189 break;
6190 case JUMP_ABSOLUTE:
6191 case JUMP_FORWARD:
6192 case JUMP_IF_FALSE_OR_POP:
6193 inst->i_target = target->i_target;
6194 break;
6195 case JUMP_IF_TRUE_OR_POP:
6196 assert (inst->i_target->b_iused == 1);
6197 inst->i_opcode = POP_JUMP_IF_FALSE;
6198 inst->i_target = inst->i_target->b_next;
6199 break;
6200 }
6201 break;
6202
6203 case JUMP_IF_TRUE_OR_POP:
6204 switch(target->i_opcode) {
6205 case POP_JUMP_IF_TRUE:
6206 *inst = *target;
6207 break;
6208 case JUMP_ABSOLUTE:
6209 case JUMP_FORWARD:
6210 case JUMP_IF_TRUE_OR_POP:
6211 inst->i_target = target->i_target;
6212 break;
6213 case JUMP_IF_FALSE_OR_POP:
6214 assert (inst->i_target->b_iused == 1);
6215 inst->i_opcode = POP_JUMP_IF_TRUE;
6216 inst->i_target = inst->i_target->b_next;
6217 break;
6218 }
6219 break;
6220
6221 case POP_JUMP_IF_FALSE:
6222 switch(target->i_opcode) {
6223 case JUMP_ABSOLUTE:
6224 case JUMP_FORWARD:
6225 inst->i_target = target->i_target;
6226 break;
6227 }
6228 break;
6229
6230 case POP_JUMP_IF_TRUE:
6231 switch(target->i_opcode) {
6232 case JUMP_ABSOLUTE:
6233 case JUMP_FORWARD:
6234 inst->i_target = target->i_target;
6235 break;
6236 }
6237 break;
6238
6239 case JUMP_ABSOLUTE:
6240 case JUMP_FORWARD:
6241 switch(target->i_opcode) {
6242 case JUMP_FORWARD:
6243 inst->i_target = target->i_target;
6244 break;
6245 case JUMP_ABSOLUTE:
6246 case RETURN_VALUE:
6247 case RERAISE:
6248 case RAISE_VARARGS:
6249 lineno = inst->i_lineno;
6250 *inst = *target;
6251 inst->i_lineno = lineno;
6252 break;
6253 }
6254 break;
6255 }
6256 }
6257 return 0;
6258error:
6259 return -1;
6260}
6261
6262
6263static void
6264clean_basic_block(basicblock *bb) {
6265 /* Remove NOPs and any code following a return or re-raise. */
6266 int dest = 0;
Mark Shannon877df852020-11-12 09:43:29 +00006267 int prev_lineno = -1;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006268 for (int src = 0; src < bb->b_iused; src++) {
Mark Shannon877df852020-11-12 09:43:29 +00006269 int lineno = bb->b_instr[src].i_lineno;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006270 switch(bb->b_instr[src].i_opcode) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006271 case RETURN_VALUE:
6272 case RERAISE:
6273 bb->b_next = NULL;
6274 bb->b_instr[dest] = bb->b_instr[src];
6275 dest++;
6276 goto end;
Mark Shannon877df852020-11-12 09:43:29 +00006277 case NOP:
6278 {
6279 /* Eliminate no-op if it doesn't have a line number, or
6280 * if the next instruction has same line number or no line number, or
6281 * if the previous instruction had the same line number. */
6282 if (lineno < 0) {
6283 break;
6284 }
6285 if (prev_lineno == lineno) {
6286 break;
6287 }
6288 if (src < bb->b_iused - 1) {
6289 int next_lineno = bb->b_instr[src+1].i_lineno;
6290 if (next_lineno < 0 || next_lineno == lineno) {
6291 bb->b_instr[src+1].i_lineno = lineno;
6292 break;
6293 }
6294 }
6295 }
6296 /* fallthrough */
Mark Shannon6e8128f2020-07-30 10:03:00 +01006297 default:
6298 if (dest != src) {
6299 bb->b_instr[dest] = bb->b_instr[src];
6300 }
6301 dest++;
Mark Shannon877df852020-11-12 09:43:29 +00006302 prev_lineno = lineno;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006303 break;
6304 }
6305 }
6306end:
6307 assert(dest <= bb->b_iused);
6308 bb->b_iused = dest;
6309}
6310
6311static int
6312mark_reachable(struct assembler *a) {
6313 basicblock **stack, **sp;
6314 sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks);
6315 if (stack == NULL) {
6316 return -1;
6317 }
6318 basicblock *entry = a->a_reverse_postorder[0];
6319 entry->b_reachable = 1;
6320 *sp++ = entry;
6321 while (sp > stack) {
6322 basicblock *b = *(--sp);
6323 if (b->b_next && b->b_next->b_reachable == 0) {
6324 b->b_next->b_reachable = 1;
6325 *sp++ = b->b_next;
6326 }
6327 for (int i = 0; i < b->b_iused; i++) {
6328 basicblock *target;
Mark Shannon582aaf12020-08-04 17:30:11 +01006329 if (is_jump(&b->b_instr[i])) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006330 target = b->b_instr[i].i_target;
6331 if (target->b_reachable == 0) {
6332 target->b_reachable = 1;
6333 *sp++ = target;
6334 }
6335 }
6336 }
6337 }
6338 PyObject_Free(stack);
6339 return 0;
6340}
6341
6342
6343/* Perform basic peephole optimizations on a control flow graph.
6344 The consts object should still be in list form to allow new constants
6345 to be appended.
6346
6347 All transformations keep the code size the same or smaller.
6348 For those that reduce size, the gaps are initially filled with
6349 NOPs. Later those NOPs are removed.
6350*/
6351
6352static int
6353optimize_cfg(struct assembler *a, PyObject *consts)
6354{
6355 for (int i = 0; i < a->a_nblocks; i++) {
6356 if (optimize_basic_block(a->a_reverse_postorder[i], consts)) {
6357 return -1;
6358 }
6359 clean_basic_block(a->a_reverse_postorder[i]);
6360 assert(a->a_reverse_postorder[i]->b_reachable == 0);
6361 }
6362 if (mark_reachable(a)) {
6363 return -1;
6364 }
6365 /* Delete unreachable instructions */
6366 for (int i = 0; i < a->a_nblocks; i++) {
6367 if (a->a_reverse_postorder[i]->b_reachable == 0) {
6368 a->a_reverse_postorder[i]->b_iused = 0;
6369 }
6370 }
6371 return 0;
6372}
6373
6374/* Retained for API compatibility.
6375 * Optimization is now done in optimize_cfg */
6376
6377PyObject *
6378PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
6379 PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
6380{
6381 Py_INCREF(code);
6382 return code;
6383}
6384