blob: 15a9046065b5dfd03c5b281d7faeaa2d4d788b61 [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(). */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001830 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0))
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);
INADA Naokicb41b272017-02-23 00:31:59 +09002274 VISIT_SEQ_IN_SCOPE(c, stmt, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002276 qualname = c->u->u_qualname;
2277 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002279 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002280 Py_XDECREF(qualname);
2281 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002283 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002284
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002285 compiler_make_closure(c, co, funcflags, qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002286 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 /* decorators */
2290 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2291 ADDOP_I(c, CALL_FUNCTION, 1);
2292 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002293
Yury Selivanov75445082015-05-11 22:57:16 -04002294 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002295}
2296
2297static int
2298compiler_class(struct compiler *c, stmt_ty s)
2299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 PyCodeObject *co;
2301 PyObject *str;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002302 int i, firstlineno;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002303 asdl_expr_seq *decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 if (!compiler_decorators(c, decos))
2306 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002307
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002308 firstlineno = s->lineno;
2309 if (asdl_seq_LEN(decos)) {
2310 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2311 }
2312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 /* ultimately generate code for:
2314 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
2315 where:
2316 <func> is a function/closure created from the class body;
2317 it has a single argument (__locals__) where the dict
2318 (or MutableSequence) representing the locals is passed
2319 <name> is the class name
2320 <bases> is the positional arguments and *varargs argument
2321 <keywords> is the keyword arguments and **kwds argument
2322 This borrows from compiler_call.
2323 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002325 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002326 if (!compiler_enter_scope(c, s->v.ClassDef.name,
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002327 COMPILER_SCOPE_CLASS, (void *)s, firstlineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002328 return 0;
2329 /* this block represents what we do in the new scope */
2330 {
2331 /* use the class name for name mangling */
2332 Py_INCREF(s->v.ClassDef.name);
Serhiy Storchaka48842712016-04-06 09:45:48 +03002333 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 /* load (global) __name__ ... */
2335 str = PyUnicode_InternFromString("__name__");
2336 if (!str || !compiler_nameop(c, str, Load)) {
2337 Py_XDECREF(str);
2338 compiler_exit_scope(c);
2339 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002340 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002341 Py_DECREF(str);
2342 /* ... and store it as __module__ */
2343 str = PyUnicode_InternFromString("__module__");
2344 if (!str || !compiler_nameop(c, str, Store)) {
2345 Py_XDECREF(str);
2346 compiler_exit_scope(c);
2347 return 0;
2348 }
2349 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002350 assert(c->u->u_qualname);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002351 ADDOP_LOAD_CONST(c, c->u->u_qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002352 str = PyUnicode_InternFromString("__qualname__");
2353 if (!str || !compiler_nameop(c, str, Store)) {
2354 Py_XDECREF(str);
2355 compiler_exit_scope(c);
2356 return 0;
2357 }
2358 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 /* compile the body proper */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002360 if (!compiler_body(c, s->v.ClassDef.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 compiler_exit_scope(c);
2362 return 0;
2363 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002364 /* Return __classcell__ if it is referenced, otherwise return None */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002365 if (c->u->u_ste->ste_needs_class_closure) {
Nick Coghlan19d24672016-12-05 16:47:55 +10002366 /* Store __classcell__ into class namespace & return it */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002367 str = PyUnicode_InternFromString("__class__");
2368 if (str == NULL) {
2369 compiler_exit_scope(c);
2370 return 0;
2371 }
2372 i = compiler_lookup_arg(c->u->u_cellvars, str);
2373 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01002374 if (i < 0) {
2375 compiler_exit_scope(c);
2376 return 0;
2377 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002378 assert(i == 0);
Nick Coghlan944368e2016-09-11 14:45:49 +10002379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 ADDOP_I(c, LOAD_CLOSURE, i);
Nick Coghlan19d24672016-12-05 16:47:55 +10002381 ADDOP(c, DUP_TOP);
Nick Coghlan944368e2016-09-11 14:45:49 +10002382 str = PyUnicode_InternFromString("__classcell__");
2383 if (!str || !compiler_nameop(c, str, Store)) {
2384 Py_XDECREF(str);
2385 compiler_exit_scope(c);
2386 return 0;
2387 }
2388 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002390 else {
Nick Coghlan19d24672016-12-05 16:47:55 +10002391 /* No methods referenced __class__, so just return None */
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002392 assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002393 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson312595c2013-05-15 15:26:42 -05002394 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002395 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 /* create the code object */
2397 co = assemble(c, 1);
2398 }
2399 /* leave the new scope */
2400 compiler_exit_scope(c);
2401 if (co == NULL)
2402 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 /* 2. load the 'build_class' function */
2405 ADDOP(c, LOAD_BUILD_CLASS);
2406
2407 /* 3. load a function (or closure) made from the code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002408 compiler_make_closure(c, co, 0, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 Py_DECREF(co);
2410
2411 /* 4. load class name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002412 ADDOP_LOAD_CONST(c, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413
2414 /* 5. generate the rest of the code for the call */
Pablo Galindoa5634c42020-09-16 19:42:00 +01002415 if (!compiler_call_helper(c, 2, s->v.ClassDef.bases, s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 return 0;
2417
2418 /* 6. apply decorators */
2419 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2420 ADDOP_I(c, CALL_FUNCTION, 1);
2421 }
2422
2423 /* 7. store into <name> */
2424 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2425 return 0;
2426 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002427}
2428
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02002429/* Return 0 if the expression is a constant value except named singletons.
2430 Return 1 otherwise. */
2431static int
2432check_is_arg(expr_ty e)
2433{
2434 if (e->kind != Constant_kind) {
2435 return 1;
2436 }
2437 PyObject *value = e->v.Constant.value;
2438 return (value == Py_None
2439 || value == Py_False
2440 || value == Py_True
2441 || value == Py_Ellipsis);
2442}
2443
2444/* Check operands of identity chacks ("is" and "is not").
2445 Emit a warning if any operand is a constant except named singletons.
2446 Return 0 on error.
2447 */
2448static int
2449check_compare(struct compiler *c, expr_ty e)
2450{
2451 Py_ssize_t i, n;
2452 int left = check_is_arg(e->v.Compare.left);
2453 n = asdl_seq_LEN(e->v.Compare.ops);
2454 for (i = 0; i < n; i++) {
2455 cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i);
2456 int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2457 if (op == Is || op == IsNot) {
2458 if (!right || !left) {
2459 const char *msg = (op == Is)
2460 ? "\"is\" with a literal. Did you mean \"==\"?"
2461 : "\"is not\" with a literal. Did you mean \"!=\"?";
2462 return compiler_warn(c, msg);
2463 }
2464 }
2465 left = right;
2466 }
2467 return 1;
2468}
2469
Mark Shannon9af0e472020-01-14 10:12:45 +00002470static int compiler_addcompare(struct compiler *c, cmpop_ty op)
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002471{
Mark Shannon9af0e472020-01-14 10:12:45 +00002472 int cmp;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002473 switch (op) {
2474 case Eq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002475 cmp = Py_EQ;
2476 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002477 case NotEq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002478 cmp = Py_NE;
2479 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002480 case Lt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002481 cmp = Py_LT;
2482 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002483 case LtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002484 cmp = Py_LE;
2485 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002486 case Gt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002487 cmp = Py_GT;
2488 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002489 case GtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002490 cmp = Py_GE;
2491 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002492 case Is:
Mark Shannon9af0e472020-01-14 10:12:45 +00002493 ADDOP_I(c, IS_OP, 0);
2494 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002495 case IsNot:
Mark Shannon9af0e472020-01-14 10:12:45 +00002496 ADDOP_I(c, IS_OP, 1);
2497 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002498 case In:
Mark Shannon9af0e472020-01-14 10:12:45 +00002499 ADDOP_I(c, CONTAINS_OP, 0);
2500 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002501 case NotIn:
Mark Shannon9af0e472020-01-14 10:12:45 +00002502 ADDOP_I(c, CONTAINS_OP, 1);
2503 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002504 default:
Mark Shannon9af0e472020-01-14 10:12:45 +00002505 Py_UNREACHABLE();
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002506 }
Mark Shannon9af0e472020-01-14 10:12:45 +00002507 ADDOP_I(c, COMPARE_OP, cmp);
2508 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002509}
2510
Mark Shannon9af0e472020-01-14 10:12:45 +00002511
2512
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002513static int
2514compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2515{
2516 switch (e->kind) {
2517 case UnaryOp_kind:
2518 if (e->v.UnaryOp.op == Not)
2519 return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2520 /* fallback to general implementation */
2521 break;
2522 case BoolOp_kind: {
Pablo Galindoa5634c42020-09-16 19:42:00 +01002523 asdl_expr_seq *s = e->v.BoolOp.values;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002524 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2525 assert(n >= 0);
2526 int cond2 = e->v.BoolOp.op == Or;
2527 basicblock *next2 = next;
2528 if (!cond2 != !cond) {
2529 next2 = compiler_new_block(c);
2530 if (next2 == NULL)
2531 return 0;
2532 }
2533 for (i = 0; i < n; ++i) {
2534 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2535 return 0;
2536 }
2537 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2538 return 0;
2539 if (next2 != next)
2540 compiler_use_next_block(c, next2);
2541 return 1;
2542 }
2543 case IfExp_kind: {
2544 basicblock *end, *next2;
2545 end = compiler_new_block(c);
2546 if (end == NULL)
2547 return 0;
2548 next2 = compiler_new_block(c);
2549 if (next2 == NULL)
2550 return 0;
2551 if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2552 return 0;
2553 if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2554 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01002555 ADDOP_JUMP(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002556 compiler_use_next_block(c, next2);
2557 if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2558 return 0;
2559 compiler_use_next_block(c, end);
2560 return 1;
2561 }
2562 case Compare_kind: {
2563 Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2564 if (n > 0) {
Serhiy Storchaka45835252019-02-16 08:29:46 +02002565 if (!check_compare(c, e)) {
2566 return 0;
2567 }
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002568 basicblock *cleanup = compiler_new_block(c);
2569 if (cleanup == NULL)
2570 return 0;
2571 VISIT(c, expr, e->v.Compare.left);
2572 for (i = 0; i < n; i++) {
2573 VISIT(c, expr,
2574 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2575 ADDOP(c, DUP_TOP);
2576 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00002577 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01002578 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002579 NEXT_BLOCK(c);
2580 }
2581 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00002582 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Mark Shannon582aaf12020-08-04 17:30:11 +01002583 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002584 basicblock *end = compiler_new_block(c);
2585 if (end == NULL)
2586 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01002587 ADDOP_JUMP(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002588 compiler_use_next_block(c, cleanup);
2589 ADDOP(c, POP_TOP);
2590 if (!cond) {
Mark Shannon582aaf12020-08-04 17:30:11 +01002591 ADDOP_JUMP(c, JUMP_FORWARD, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002592 }
2593 compiler_use_next_block(c, end);
2594 return 1;
2595 }
2596 /* fallback to general implementation */
2597 break;
2598 }
2599 default:
2600 /* fallback to general implementation */
2601 break;
2602 }
2603
2604 /* general implementation */
2605 VISIT(c, expr, e);
Mark Shannon582aaf12020-08-04 17:30:11 +01002606 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002607 return 1;
2608}
2609
2610static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002611compiler_ifexp(struct compiler *c, expr_ty e)
2612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002613 basicblock *end, *next;
2614
2615 assert(e->kind == IfExp_kind);
2616 end = compiler_new_block(c);
2617 if (end == NULL)
2618 return 0;
2619 next = compiler_new_block(c);
2620 if (next == NULL)
2621 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002622 if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2623 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002624 VISIT(c, expr, e->v.IfExp.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002625 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002626 compiler_use_next_block(c, next);
2627 VISIT(c, expr, e->v.IfExp.orelse);
2628 compiler_use_next_block(c, end);
2629 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002630}
2631
2632static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002633compiler_lambda(struct compiler *c, expr_ty e)
2634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002635 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002636 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002637 static identifier name;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002638 Py_ssize_t funcflags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002639 arguments_ty args = e->v.Lambda.args;
2640 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002642 if (!compiler_check_debug_args(c, args))
2643 return 0;
2644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 if (!name) {
2646 name = PyUnicode_InternFromString("<lambda>");
2647 if (!name)
2648 return 0;
2649 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002650
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002651 funcflags = compiler_default_arguments(c, args);
2652 if (funcflags == -1) {
2653 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002654 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002655
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002656 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002657 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002658 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002660 /* Make None the first constant, so the lambda can't have a
2661 docstring. */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002662 if (compiler_add_const(c, Py_None) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002666 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002667 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2668 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2669 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002670 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 }
2672 else {
2673 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002674 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002676 qualname = c->u->u_qualname;
2677 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002678 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002679 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002680 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002681
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002682 compiler_make_closure(c, co, funcflags, qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002683 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 Py_DECREF(co);
2685
2686 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002687}
2688
2689static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002690compiler_if(struct compiler *c, stmt_ty s)
2691{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002692 basicblock *end, *next;
2693 int constant;
2694 assert(s->kind == If_kind);
2695 end = compiler_new_block(c);
2696 if (end == NULL)
2697 return 0;
2698
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02002699 constant = expr_constant(s->v.If.test);
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002700 /* constant = 0: "if 0"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002701 * constant = 1: "if 1", "if 2", ...
2702 * constant = -1: rest */
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002703 if (constant == 0) {
2704 BEGIN_DO_NOT_EMIT_BYTECODE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002705 VISIT_SEQ(c, stmt, s->v.If.body);
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002706 END_DO_NOT_EMIT_BYTECODE
2707 if (s->v.If.orelse) {
2708 VISIT_SEQ(c, stmt, s->v.If.orelse);
2709 }
2710 } else if (constant == 1) {
2711 VISIT_SEQ(c, stmt, s->v.If.body);
2712 if (s->v.If.orelse) {
2713 BEGIN_DO_NOT_EMIT_BYTECODE
2714 VISIT_SEQ(c, stmt, s->v.If.orelse);
2715 END_DO_NOT_EMIT_BYTECODE
2716 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002717 } else {
Antoine Pitroue7811fc2014-09-18 03:06:50 +02002718 if (asdl_seq_LEN(s->v.If.orelse)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002719 next = compiler_new_block(c);
2720 if (next == NULL)
2721 return 0;
2722 }
Mark Shannonfee55262019-11-21 09:11:43 +00002723 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002724 next = end;
Mark Shannonfee55262019-11-21 09:11:43 +00002725 }
2726 if (!compiler_jump_if(c, s->v.If.test, next, 0)) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002727 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002728 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002729 VISIT_SEQ(c, stmt, s->v.If.body);
Antoine Pitroue7811fc2014-09-18 03:06:50 +02002730 if (asdl_seq_LEN(s->v.If.orelse)) {
Mark Shannon582aaf12020-08-04 17:30:11 +01002731 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002732 compiler_use_next_block(c, next);
2733 VISIT_SEQ(c, stmt, s->v.If.orelse);
2734 }
2735 }
2736 compiler_use_next_block(c, end);
2737 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002738}
2739
2740static int
2741compiler_for(struct compiler *c, stmt_ty s)
2742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 basicblock *start, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002745 start = compiler_new_block(c);
2746 cleanup = compiler_new_block(c);
2747 end = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00002748 if (start == NULL || end == NULL || cleanup == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002749 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002750 }
2751 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002752 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002753 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 VISIT(c, expr, s->v.For.iter);
2755 ADDOP(c, GET_ITER);
2756 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01002757 ADDOP_JUMP(c, FOR_ITER, cleanup);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002758 VISIT(c, expr, s->v.For.target);
2759 VISIT_SEQ(c, stmt, s->v.For.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002760 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 compiler_use_next_block(c, cleanup);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002762
2763 compiler_pop_fblock(c, FOR_LOOP, start);
2764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002765 VISIT_SEQ(c, stmt, s->v.For.orelse);
2766 compiler_use_next_block(c, end);
2767 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002768}
2769
Yury Selivanov75445082015-05-11 22:57:16 -04002770
2771static int
2772compiler_async_for(struct compiler *c, stmt_ty s)
2773{
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002774 basicblock *start, *except, *end;
Pablo Galindo90235812020-03-15 04:29:22 +00002775 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07002776 c->u->u_ste->ste_coroutine = 1;
2777 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
Zsolt Dollensteine2396502018-04-27 08:58:56 -07002778 return compiler_error(c, "'async for' outside async function");
2779 }
2780
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002781 start = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002782 except = compiler_new_block(c);
2783 end = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002784
Mark Shannonfee55262019-11-21 09:11:43 +00002785 if (start == NULL || except == NULL || end == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002786 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002787 }
Yury Selivanov75445082015-05-11 22:57:16 -04002788 VISIT(c, expr, s->v.AsyncFor.iter);
2789 ADDOP(c, GET_AITER);
Yury Selivanov75445082015-05-11 22:57:16 -04002790
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002791 compiler_use_next_block(c, start);
Mark Shannonfee55262019-11-21 09:11:43 +00002792 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002793 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002794 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002795 /* SETUP_FINALLY to guard the __anext__ call */
Mark Shannon582aaf12020-08-04 17:30:11 +01002796 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov75445082015-05-11 22:57:16 -04002797 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002798 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04002799 ADDOP(c, YIELD_FROM);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002800 ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */
Yury Selivanov75445082015-05-11 22:57:16 -04002801
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002802 /* Success block for __anext__ */
2803 VISIT(c, expr, s->v.AsyncFor.target);
2804 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002805 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002806
2807 compiler_pop_fblock(c, FOR_LOOP, start);
Yury Selivanov75445082015-05-11 22:57:16 -04002808
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002809 /* Except block for __anext__ */
Yury Selivanov75445082015-05-11 22:57:16 -04002810 compiler_use_next_block(c, except);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002811 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov75445082015-05-11 22:57:16 -04002812
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002813 /* `else` block */
Yury Selivanov75445082015-05-11 22:57:16 -04002814 VISIT_SEQ(c, stmt, s->v.For.orelse);
2815
2816 compiler_use_next_block(c, end);
2817
2818 return 1;
2819}
2820
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002821static int
2822compiler_while(struct compiler *c, stmt_ty s)
2823{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 basicblock *loop, *orelse, *end, *anchor = NULL;
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02002825 int constant = expr_constant(s->v.While.test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 if (constant == 0) {
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002828 BEGIN_DO_NOT_EMIT_BYTECODE
Pablo Galindo6c3e66a2019-10-30 11:53:26 +00002829 // Push a dummy block so the VISIT_SEQ knows that we are
2830 // inside a while loop so it can correctly evaluate syntax
2831 // errors.
Mark Shannonfee55262019-11-21 09:11:43 +00002832 if (!compiler_push_fblock(c, WHILE_LOOP, NULL, NULL, NULL)) {
Pablo Galindo6c3e66a2019-10-30 11:53:26 +00002833 return 0;
2834 }
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002835 VISIT_SEQ(c, stmt, s->v.While.body);
Pablo Galindo6c3e66a2019-10-30 11:53:26 +00002836 // Remove the dummy block now that is not needed.
2837 compiler_pop_fblock(c, WHILE_LOOP, NULL);
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002838 END_DO_NOT_EMIT_BYTECODE
2839 if (s->v.While.orelse) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 VISIT_SEQ(c, stmt, s->v.While.orelse);
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002841 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 return 1;
2843 }
2844 loop = compiler_new_block(c);
2845 end = compiler_new_block(c);
2846 if (constant == -1) {
2847 anchor = compiler_new_block(c);
2848 if (anchor == NULL)
2849 return 0;
2850 }
2851 if (loop == NULL || end == NULL)
2852 return 0;
2853 if (s->v.While.orelse) {
2854 orelse = compiler_new_block(c);
2855 if (orelse == NULL)
2856 return 0;
2857 }
2858 else
2859 orelse = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 compiler_use_next_block(c, loop);
Mark Shannonfee55262019-11-21 09:11:43 +00002862 if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 return 0;
2864 if (constant == -1) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002865 if (!compiler_jump_if(c, s->v.While.test, anchor, 0))
2866 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 }
2868 VISIT_SEQ(c, stmt, s->v.While.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002869 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 /* XXX should the two POP instructions be in a separate block
2872 if there is no else clause ?
2873 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002874
Benjamin Peterson3cda0ed2014-12-13 16:06:19 -05002875 if (constant == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 compiler_use_next_block(c, anchor);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002877 compiler_pop_fblock(c, WHILE_LOOP, loop);
2878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 if (orelse != NULL) /* what if orelse is just pass? */
2880 VISIT_SEQ(c, stmt, s->v.While.orelse);
2881 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002884}
2885
2886static int
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002887compiler_return(struct compiler *c, stmt_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002888{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002889 int preserve_tos = ((s->v.Return.value != NULL) &&
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002890 (s->v.Return.value->kind != Constant_kind));
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002891 if (c->u->u_ste->ste_type != FunctionBlock)
2892 return compiler_error(c, "'return' outside function");
2893 if (s->v.Return.value != NULL &&
2894 c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2895 {
2896 return compiler_error(
2897 c, "'return' with value in async generator");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002898 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002899 if (preserve_tos) {
2900 VISIT(c, expr, s->v.Return.value);
2901 }
Mark Shannonfee55262019-11-21 09:11:43 +00002902 if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL))
2903 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002904 if (s->v.Return.value == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002905 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002906 }
2907 else if (!preserve_tos) {
2908 VISIT(c, expr, s->v.Return.value);
2909 }
2910 ADDOP(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002913}
2914
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002915static int
2916compiler_break(struct compiler *c)
2917{
Mark Shannonfee55262019-11-21 09:11:43 +00002918 struct fblockinfo *loop = NULL;
2919 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
2920 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002921 }
Mark Shannonfee55262019-11-21 09:11:43 +00002922 if (loop == NULL) {
2923 return compiler_error(c, "'break' outside loop");
2924 }
2925 if (!compiler_unwind_fblock(c, loop, 0)) {
2926 return 0;
2927 }
Mark Shannon582aaf12020-08-04 17:30:11 +01002928 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit);
Mark Shannonfee55262019-11-21 09:11:43 +00002929 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002930}
2931
2932static int
2933compiler_continue(struct compiler *c)
2934{
Mark Shannonfee55262019-11-21 09:11:43 +00002935 struct fblockinfo *loop = NULL;
2936 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
2937 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002938 }
Mark Shannonfee55262019-11-21 09:11:43 +00002939 if (loop == NULL) {
2940 return compiler_error(c, "'continue' not properly in loop");
2941 }
Mark Shannon582aaf12020-08-04 17:30:11 +01002942 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block);
Mark Shannonfee55262019-11-21 09:11:43 +00002943 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002944}
2945
2946
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002947/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002948
2949 SETUP_FINALLY L
2950 <code for body>
2951 POP_BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00002952 <code for finalbody>
2953 JUMP E
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002954 L:
2955 <code for finalbody>
Mark Shannonfee55262019-11-21 09:11:43 +00002956 E:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002957
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002958 The special instructions use the block stack. Each block
2959 stack entry contains the instruction that created it (here
2960 SETUP_FINALLY), the level of the value stack at the time the
2961 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002962
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002963 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002964 Pushes the current value stack level and the label
2965 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002966 POP_BLOCK:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002967 Pops en entry from the block stack.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002969 The block stack is unwound when an exception is raised:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002970 when a SETUP_FINALLY entry is found, the raised and the caught
2971 exceptions are pushed onto the value stack (and the exception
2972 condition is cleared), and the interpreter jumps to the label
2973 gotten from the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002974*/
2975
2976static int
2977compiler_try_finally(struct compiler *c, stmt_ty s)
2978{
Mark Shannonfee55262019-11-21 09:11:43 +00002979 basicblock *body, *end, *exit;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 body = compiler_new_block(c);
2982 end = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00002983 exit = compiler_new_block(c);
2984 if (body == NULL || end == NULL || exit == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002985 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002986
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002987 /* `try` block */
Mark Shannon582aaf12020-08-04 17:30:11 +01002988 ADDOP_JUMP(c, SETUP_FINALLY, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 compiler_use_next_block(c, body);
Mark Shannonfee55262019-11-21 09:11:43 +00002990 if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002991 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002992 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
2993 if (!compiler_try_except(c, s))
2994 return 0;
2995 }
2996 else {
2997 VISIT_SEQ(c, stmt, s->v.Try.body);
2998 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002999 ADDOP(c, POP_BLOCK);
Mark Shannonfee55262019-11-21 09:11:43 +00003000 compiler_pop_fblock(c, FINALLY_TRY, body);
3001 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Mark Shannon582aaf12020-08-04 17:30:11 +01003002 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00003003 /* `finally` block */
3004 compiler_use_next_block(c, end);
3005 if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL))
3006 return 0;
3007 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3008 compiler_pop_fblock(c, FINALLY_END, end);
3009 ADDOP(c, RERAISE);
3010 compiler_use_next_block(c, exit);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003011 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003012}
3013
3014/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003015 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003016 (The contents of the value stack is shown in [], with the top
3017 at the right; 'tb' is trace-back info, 'val' the exception's
3018 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003019
3020 Value stack Label Instruction Argument
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003021 [] SETUP_FINALLY L1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 [] <code for S>
3023 [] POP_BLOCK
3024 [] JUMP_FORWARD L0
3025
3026 [tb, val, exc] L1: DUP )
3027 [tb, val, exc, exc] <evaluate E1> )
Mark Shannon9af0e472020-01-14 10:12:45 +00003028 [tb, val, exc, exc, E1] JUMP_IF_NOT_EXC_MATCH L2 ) only if E1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029 [tb, val, exc] POP
3030 [tb, val] <assign to V1> (or POP if no V1)
3031 [tb] POP
3032 [] <code for S1>
3033 JUMP_FORWARD L0
3034
3035 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003036 .............................etc.......................
3037
Mark Shannonfee55262019-11-21 09:11:43 +00003038 [tb, val, exc] Ln+1: RERAISE # re-raise exception
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003039
3040 [] L0: <next statement>
3041
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003042 Of course, parts are not generated if Vi or Ei is not present.
3043*/
3044static int
3045compiler_try_except(struct compiler *c, stmt_ty s)
3046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003047 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003048 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003050 body = compiler_new_block(c);
3051 except = compiler_new_block(c);
3052 orelse = compiler_new_block(c);
3053 end = compiler_new_block(c);
3054 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
3055 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01003056 ADDOP_JUMP(c, SETUP_FINALLY, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003057 compiler_use_next_block(c, body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003058 if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003060 VISIT_SEQ(c, stmt, s->v.Try.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 ADDOP(c, POP_BLOCK);
Mark Shannon02d126a2020-09-25 14:04:19 +01003062 compiler_pop_fblock(c, TRY_EXCEPT, body);
Mark Shannon582aaf12020-08-04 17:30:11 +01003063 ADDOP_JUMP(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003064 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 compiler_use_next_block(c, except);
Mark Shannon02d126a2020-09-25 14:04:19 +01003066 /* Runtime will push a block here, so we need to account for that */
3067 if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL))
3068 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003069 for (i = 0; i < n; i++) {
3070 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003071 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003072 if (!handler->v.ExceptHandler.type && i < n-1)
3073 return compiler_error(c, "default 'except:' must be last");
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003074 SET_LOC(c, handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003075 except = compiler_new_block(c);
3076 if (except == NULL)
3077 return 0;
3078 if (handler->v.ExceptHandler.type) {
3079 ADDOP(c, DUP_TOP);
3080 VISIT(c, expr, handler->v.ExceptHandler.type);
Mark Shannon582aaf12020-08-04 17:30:11 +01003081 ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082 }
3083 ADDOP(c, POP_TOP);
3084 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003085 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00003086
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003087 cleanup_end = compiler_new_block(c);
3088 cleanup_body = compiler_new_block(c);
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003089 if (cleanup_end == NULL || cleanup_body == NULL) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003090 return 0;
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003091 }
Guido van Rossumb940e112007-01-10 16:19:56 +00003092
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003093 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3094 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003095
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003096 /*
3097 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003098 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003099 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003100 try:
3101 # body
3102 finally:
Chris Angelicoad098b62019-05-21 23:34:19 +10003103 name = None # in case body contains "del name"
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003104 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003105 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003107 /* second try: */
Mark Shannon582aaf12020-08-04 17:30:11 +01003108 ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003109 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003110 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003111 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003112
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003113 /* second # body */
3114 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003115 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003116 ADDOP(c, POP_BLOCK);
3117 ADDOP(c, POP_EXCEPT);
3118 /* name = None; del name */
3119 ADDOP_LOAD_CONST(c, Py_None);
3120 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3121 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Mark Shannon582aaf12020-08-04 17:30:11 +01003122 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003123
Mark Shannonfee55262019-11-21 09:11:43 +00003124 /* except: */
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003125 compiler_use_next_block(c, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003126
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003127 /* name = None; del name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003128 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003129 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003130 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131
Mark Shannonfee55262019-11-21 09:11:43 +00003132 ADDOP(c, RERAISE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003133 }
3134 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003135 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003137 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05003138 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003139 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003140
Guido van Rossumb940e112007-01-10 16:19:56 +00003141 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003142 ADDOP(c, POP_TOP);
3143 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003144 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003145 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003146 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003147 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003148 ADDOP(c, POP_EXCEPT);
Mark Shannon582aaf12020-08-04 17:30:11 +01003149 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003150 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 compiler_use_next_block(c, except);
3152 }
Mark Shannon02d126a2020-09-25 14:04:19 +01003153 compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL);
Mark Shannonfee55262019-11-21 09:11:43 +00003154 ADDOP(c, RERAISE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003155 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003156 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003157 compiler_use_next_block(c, end);
3158 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003159}
3160
3161static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003162compiler_try(struct compiler *c, stmt_ty s) {
3163 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
3164 return compiler_try_finally(c, s);
3165 else
3166 return compiler_try_except(c, s);
3167}
3168
3169
3170static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003171compiler_import_as(struct compiler *c, identifier name, identifier asname)
3172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003173 /* The IMPORT_NAME opcode was already generated. This function
3174 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003176 If there is a dot in name, we need to split it and emit a
Serhiy Storchakaf93234b2017-05-09 22:31:05 +03003177 IMPORT_FROM for each name.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003178 */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003179 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
3180 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003181 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003182 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003183 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003184 /* Consume the base module name to get the first attribute */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003185 while (1) {
3186 Py_ssize_t pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003187 PyObject *attr;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003188 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003189 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003190 return 0;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003191 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003192 if (!attr)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003193 return 0;
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003194 ADDOP_N(c, IMPORT_FROM, attr, names);
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003195 if (dot == -1) {
3196 break;
3197 }
3198 ADDOP(c, ROT_TWO);
3199 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003200 }
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003201 if (!compiler_nameop(c, asname, Store)) {
3202 return 0;
3203 }
3204 ADDOP(c, POP_TOP);
3205 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003206 }
3207 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003208}
3209
3210static int
3211compiler_import(struct compiler *c, stmt_ty s)
3212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003213 /* The Import node stores a module name like a.b.c as a single
3214 string. This is convenient for all cases except
3215 import a.b.c as d
3216 where we need to parse that string to extract the individual
3217 module names.
3218 XXX Perhaps change the representation to make this case simpler?
3219 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01003220 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003221
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003222 PyObject *zero = _PyLong_GetZero(); // borrowed reference
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 for (i = 0; i < n; i++) {
3224 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
3225 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003226
Victor Stinnerc9bc2902020-10-27 02:24:34 +01003227 ADDOP_LOAD_CONST(c, zero);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003228 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003229 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003231 if (alias->asname) {
3232 r = compiler_import_as(c, alias->name, alias->asname);
3233 if (!r)
3234 return r;
3235 }
3236 else {
3237 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003238 Py_ssize_t dot = PyUnicode_FindChar(
3239 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02003240 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003241 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02003242 if (tmp == NULL)
3243 return 0;
3244 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003245 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003246 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003247 Py_DECREF(tmp);
3248 }
3249 if (!r)
3250 return r;
3251 }
3252 }
3253 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003254}
3255
3256static int
3257compiler_from_import(struct compiler *c, stmt_ty s)
3258{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003259 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003260 PyObject *names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 if (!empty_string) {
3264 empty_string = PyUnicode_FromString("");
3265 if (!empty_string)
3266 return 0;
3267 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003268
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003269 ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02003270
3271 names = PyTuple_New(n);
3272 if (!names)
3273 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003275 /* build up the names */
3276 for (i = 0; i < n; i++) {
3277 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3278 Py_INCREF(alias->name);
3279 PyTuple_SET_ITEM(names, i, alias->name);
3280 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003282 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003283 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003284 Py_DECREF(names);
3285 return compiler_error(c, "from __future__ imports must occur "
3286 "at the beginning of the file");
3287 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003288 ADDOP_LOAD_CONST_NEW(c, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290 if (s->v.ImportFrom.module) {
3291 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
3292 }
3293 else {
3294 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
3295 }
3296 for (i = 0; i < n; i++) {
3297 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3298 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003299
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003300 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003301 assert(n == 1);
3302 ADDOP(c, IMPORT_STAR);
3303 return 1;
3304 }
3305
3306 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
3307 store_name = alias->name;
3308 if (alias->asname)
3309 store_name = alias->asname;
3310
3311 if (!compiler_nameop(c, store_name, Store)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003312 return 0;
3313 }
3314 }
3315 /* remove imported module */
3316 ADDOP(c, POP_TOP);
3317 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003318}
3319
3320static int
3321compiler_assert(struct compiler *c, stmt_ty s)
3322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003323 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003324
Georg Brandl8334fd92010-12-04 10:26:46 +00003325 if (c->c_optimize)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 if (s->v.Assert.test->kind == Tuple_kind &&
Serhiy Storchakad31e7732018-10-21 10:09:39 +03003328 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0)
3329 {
3330 if (!compiler_warn(c, "assertion is always true, "
3331 "perhaps remove parentheses?"))
3332 {
Victor Stinner14e461d2013-08-26 22:28:21 +02003333 return 0;
3334 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003335 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003336 end = compiler_new_block(c);
3337 if (end == NULL)
3338 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003339 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
3340 return 0;
Zackery Spytzce6a0702019-08-25 03:44:09 -06003341 ADDOP(c, LOAD_ASSERTION_ERROR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003342 if (s->v.Assert.msg) {
3343 VISIT(c, expr, s->v.Assert.msg);
3344 ADDOP_I(c, CALL_FUNCTION, 1);
3345 }
3346 ADDOP_I(c, RAISE_VARARGS, 1);
3347 compiler_use_next_block(c, end);
3348 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003349}
3350
3351static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003352compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3353{
3354 if (c->c_interactive && c->c_nestlevel <= 1) {
3355 VISIT(c, expr, value);
3356 ADDOP(c, PRINT_EXPR);
3357 return 1;
3358 }
3359
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003360 if (value->kind == Constant_kind) {
Victor Stinner15a30952016-02-08 22:45:06 +01003361 /* ignore constant statement */
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003362 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003363 }
3364
3365 VISIT(c, expr, value);
3366 ADDOP(c, POP_TOP);
3367 return 1;
3368}
3369
3370static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003371compiler_visit_stmt(struct compiler *c, stmt_ty s)
3372{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003373 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003375 /* Always assign a lineno to the next instruction for a stmt. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003376 SET_LOC(c, s);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003378 switch (s->kind) {
3379 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04003380 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003381 case ClassDef_kind:
3382 return compiler_class(c, s);
3383 case Return_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003384 return compiler_return(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003385 case Delete_kind:
3386 VISIT_SEQ(c, expr, s->v.Delete.targets)
3387 break;
3388 case Assign_kind:
3389 n = asdl_seq_LEN(s->v.Assign.targets);
3390 VISIT(c, expr, s->v.Assign.value);
3391 for (i = 0; i < n; i++) {
3392 if (i < n - 1)
3393 ADDOP(c, DUP_TOP);
3394 VISIT(c, expr,
3395 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3396 }
3397 break;
3398 case AugAssign_kind:
3399 return compiler_augassign(c, s);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003400 case AnnAssign_kind:
3401 return compiler_annassign(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 case For_kind:
3403 return compiler_for(c, s);
3404 case While_kind:
3405 return compiler_while(c, s);
3406 case If_kind:
3407 return compiler_if(c, s);
3408 case Raise_kind:
3409 n = 0;
3410 if (s->v.Raise.exc) {
3411 VISIT(c, expr, s->v.Raise.exc);
3412 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003413 if (s->v.Raise.cause) {
3414 VISIT(c, expr, s->v.Raise.cause);
3415 n++;
3416 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003417 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01003418 ADDOP_I(c, RAISE_VARARGS, (int)n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003419 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003420 case Try_kind:
3421 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003422 case Assert_kind:
3423 return compiler_assert(c, s);
3424 case Import_kind:
3425 return compiler_import(c, s);
3426 case ImportFrom_kind:
3427 return compiler_from_import(c, s);
3428 case Global_kind:
3429 case Nonlocal_kind:
3430 break;
3431 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003432 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 case Pass_kind:
3434 break;
3435 case Break_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003436 return compiler_break(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003437 case Continue_kind:
3438 return compiler_continue(c);
3439 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003440 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04003441 case AsyncFunctionDef_kind:
3442 return compiler_function(c, s, 1);
3443 case AsyncWith_kind:
3444 return compiler_async_with(c, s, 0);
3445 case AsyncFor_kind:
3446 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003447 }
Yury Selivanov75445082015-05-11 22:57:16 -04003448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003449 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003450}
3451
3452static int
3453unaryop(unaryop_ty op)
3454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003455 switch (op) {
3456 case Invert:
3457 return UNARY_INVERT;
3458 case Not:
3459 return UNARY_NOT;
3460 case UAdd:
3461 return UNARY_POSITIVE;
3462 case USub:
3463 return UNARY_NEGATIVE;
3464 default:
3465 PyErr_Format(PyExc_SystemError,
3466 "unary op %d should not be possible", op);
3467 return 0;
3468 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003469}
3470
3471static int
Andy Lester76d58772020-03-10 21:18:12 -05003472binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 switch (op) {
3475 case Add:
3476 return BINARY_ADD;
3477 case Sub:
3478 return BINARY_SUBTRACT;
3479 case Mult:
3480 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003481 case MatMult:
3482 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003483 case Div:
3484 return BINARY_TRUE_DIVIDE;
3485 case Mod:
3486 return BINARY_MODULO;
3487 case Pow:
3488 return BINARY_POWER;
3489 case LShift:
3490 return BINARY_LSHIFT;
3491 case RShift:
3492 return BINARY_RSHIFT;
3493 case BitOr:
3494 return BINARY_OR;
3495 case BitXor:
3496 return BINARY_XOR;
3497 case BitAnd:
3498 return BINARY_AND;
3499 case FloorDiv:
3500 return BINARY_FLOOR_DIVIDE;
3501 default:
3502 PyErr_Format(PyExc_SystemError,
3503 "binary op %d should not be possible", op);
3504 return 0;
3505 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003506}
3507
3508static int
Andy Lester76d58772020-03-10 21:18:12 -05003509inplace_binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003511 switch (op) {
3512 case Add:
3513 return INPLACE_ADD;
3514 case Sub:
3515 return INPLACE_SUBTRACT;
3516 case Mult:
3517 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003518 case MatMult:
3519 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003520 case Div:
3521 return INPLACE_TRUE_DIVIDE;
3522 case Mod:
3523 return INPLACE_MODULO;
3524 case Pow:
3525 return INPLACE_POWER;
3526 case LShift:
3527 return INPLACE_LSHIFT;
3528 case RShift:
3529 return INPLACE_RSHIFT;
3530 case BitOr:
3531 return INPLACE_OR;
3532 case BitXor:
3533 return INPLACE_XOR;
3534 case BitAnd:
3535 return INPLACE_AND;
3536 case FloorDiv:
3537 return INPLACE_FLOOR_DIVIDE;
3538 default:
3539 PyErr_Format(PyExc_SystemError,
3540 "inplace binary op %d should not be possible", op);
3541 return 0;
3542 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003543}
3544
3545static int
3546compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3547{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003548 int op, scope;
3549 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003550 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003552 PyObject *dict = c->u->u_names;
3553 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003554
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003555 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3556 !_PyUnicode_EqualToASCIIString(name, "True") &&
3557 !_PyUnicode_EqualToASCIIString(name, "False"));
Benjamin Peterson70b224d2012-12-06 17:49:58 -05003558
Pablo Galindoc5fc1562020-04-22 23:29:27 +01003559 if (forbidden_name(c, name, ctx))
3560 return 0;
3561
Serhiy Storchakabd6ec4d2017-12-18 14:29:12 +02003562 mangled = _Py_Mangle(c->u->u_private, name);
3563 if (!mangled)
3564 return 0;
3565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003566 op = 0;
3567 optype = OP_NAME;
3568 scope = PyST_GetScope(c->u->u_ste, mangled);
3569 switch (scope) {
3570 case FREE:
3571 dict = c->u->u_freevars;
3572 optype = OP_DEREF;
3573 break;
3574 case CELL:
3575 dict = c->u->u_cellvars;
3576 optype = OP_DEREF;
3577 break;
3578 case LOCAL:
3579 if (c->u->u_ste->ste_type == FunctionBlock)
3580 optype = OP_FAST;
3581 break;
3582 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04003583 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 optype = OP_GLOBAL;
3585 break;
3586 case GLOBAL_EXPLICIT:
3587 optype = OP_GLOBAL;
3588 break;
3589 default:
3590 /* scope can be 0 */
3591 break;
3592 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003594 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003595 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003597 switch (optype) {
3598 case OP_DEREF:
3599 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003600 case Load:
3601 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3602 break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003603 case Store: op = STORE_DEREF; break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003604 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 }
3606 break;
3607 case OP_FAST:
3608 switch (ctx) {
3609 case Load: op = LOAD_FAST; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003610 case Store: op = STORE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003611 case Del: op = DELETE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 }
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003613 ADDOP_N(c, op, mangled, varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003614 return 1;
3615 case OP_GLOBAL:
3616 switch (ctx) {
3617 case Load: op = LOAD_GLOBAL; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003618 case Store: op = STORE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003619 case Del: op = DELETE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003620 }
3621 break;
3622 case OP_NAME:
3623 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00003624 case Load: op = LOAD_NAME; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003625 case Store: op = STORE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003626 case Del: op = DELETE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 }
3628 break;
3629 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003631 assert(op);
Andy Lester76d58772020-03-10 21:18:12 -05003632 arg = compiler_add_o(dict, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003633 Py_DECREF(mangled);
3634 if (arg < 0)
3635 return 0;
3636 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003637}
3638
3639static int
3640compiler_boolop(struct compiler *c, expr_ty e)
3641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003642 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003643 int jumpi;
3644 Py_ssize_t i, n;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003645 asdl_expr_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003647 assert(e->kind == BoolOp_kind);
3648 if (e->v.BoolOp.op == And)
3649 jumpi = JUMP_IF_FALSE_OR_POP;
3650 else
3651 jumpi = JUMP_IF_TRUE_OR_POP;
3652 end = compiler_new_block(c);
3653 if (end == NULL)
3654 return 0;
3655 s = e->v.BoolOp.values;
3656 n = asdl_seq_LEN(s) - 1;
3657 assert(n >= 0);
3658 for (i = 0; i < n; ++i) {
3659 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003660 ADDOP_JUMP(c, jumpi, end);
Mark Shannon6e8128f2020-07-30 10:03:00 +01003661 basicblock *next = compiler_new_block(c);
3662 if (next == NULL) {
3663 return 0;
3664 }
3665 compiler_use_next_block(c, next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003666 }
3667 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3668 compiler_use_next_block(c, end);
3669 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003670}
3671
3672static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003673starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
Mark Shannon13bc1392020-01-23 09:25:17 +00003674 int build, int add, int extend, int tuple)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003675{
3676 Py_ssize_t n = asdl_seq_LEN(elts);
Mark Shannon13bc1392020-01-23 09:25:17 +00003677 Py_ssize_t i, seen_star = 0;
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003678 if (n > 2 && are_all_items_const(elts, 0, n)) {
3679 PyObject *folded = PyTuple_New(n);
3680 if (folded == NULL) {
3681 return 0;
3682 }
3683 PyObject *val;
3684 for (i = 0; i < n; i++) {
3685 val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
3686 Py_INCREF(val);
3687 PyTuple_SET_ITEM(folded, i, val);
3688 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003689 if (tuple) {
3690 ADDOP_LOAD_CONST_NEW(c, folded);
3691 } else {
3692 if (add == SET_ADD) {
3693 Py_SETREF(folded, PyFrozenSet_New(folded));
3694 if (folded == NULL) {
3695 return 0;
3696 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003697 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003698 ADDOP_I(c, build, pushed);
3699 ADDOP_LOAD_CONST_NEW(c, folded);
3700 ADDOP_I(c, extend, 1);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003701 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003702 return 1;
3703 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003704
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003705 for (i = 0; i < n; i++) {
3706 expr_ty elt = asdl_seq_GET(elts, i);
3707 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003708 seen_star = 1;
3709 }
3710 }
3711 if (seen_star) {
3712 seen_star = 0;
3713 for (i = 0; i < n; i++) {
3714 expr_ty elt = asdl_seq_GET(elts, i);
3715 if (elt->kind == Starred_kind) {
3716 if (seen_star == 0) {
3717 ADDOP_I(c, build, i+pushed);
3718 seen_star = 1;
3719 }
3720 VISIT(c, expr, elt->v.Starred.value);
3721 ADDOP_I(c, extend, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003722 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003723 else {
3724 VISIT(c, expr, elt);
3725 if (seen_star) {
3726 ADDOP_I(c, add, 1);
3727 }
3728 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003729 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003730 assert(seen_star);
3731 if (tuple) {
3732 ADDOP(c, LIST_TO_TUPLE);
3733 }
3734 }
3735 else {
3736 for (i = 0; i < n; i++) {
3737 expr_ty elt = asdl_seq_GET(elts, i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003738 VISIT(c, expr, elt);
Mark Shannon13bc1392020-01-23 09:25:17 +00003739 }
3740 if (tuple) {
3741 ADDOP_I(c, BUILD_TUPLE, n+pushed);
3742 } else {
3743 ADDOP_I(c, build, n+pushed);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003744 }
3745 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003746 return 1;
3747}
3748
3749static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003750assignment_helper(struct compiler *c, asdl_expr_seq *elts)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003751{
3752 Py_ssize_t n = asdl_seq_LEN(elts);
3753 Py_ssize_t i;
3754 int seen_star = 0;
3755 for (i = 0; i < n; i++) {
3756 expr_ty elt = asdl_seq_GET(elts, i);
3757 if (elt->kind == Starred_kind && !seen_star) {
3758 if ((i >= (1 << 8)) ||
3759 (n-i-1 >= (INT_MAX >> 8)))
3760 return compiler_error(c,
3761 "too many expressions in "
3762 "star-unpacking assignment");
3763 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3764 seen_star = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003765 }
3766 else if (elt->kind == Starred_kind) {
3767 return compiler_error(c,
Furkan Öndercb6534e2020-03-26 04:54:31 +03003768 "multiple starred expressions in assignment");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003769 }
3770 }
3771 if (!seen_star) {
3772 ADDOP_I(c, UNPACK_SEQUENCE, n);
3773 }
Brandt Bucherd5aa2e92020-03-07 19:44:18 -08003774 for (i = 0; i < n; i++) {
3775 expr_ty elt = asdl_seq_GET(elts, i);
3776 VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value);
3777 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003778 return 1;
3779}
3780
3781static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003782compiler_list(struct compiler *c, expr_ty e)
3783{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003784 asdl_expr_seq *elts = e->v.List.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003785 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003786 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003787 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003788 else if (e->v.List.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003789 return starunpack_helper(c, elts, 0, BUILD_LIST,
3790 LIST_APPEND, LIST_EXTEND, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003791 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003792 else
3793 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003795}
3796
3797static int
3798compiler_tuple(struct compiler *c, expr_ty e)
3799{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003800 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003801 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003802 return assignment_helper(c, elts);
3803 }
3804 else if (e->v.Tuple.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003805 return starunpack_helper(c, elts, 0, BUILD_LIST,
3806 LIST_APPEND, LIST_EXTEND, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003807 }
3808 else
3809 VISIT_SEQ(c, expr, elts);
3810 return 1;
3811}
3812
3813static int
3814compiler_set(struct compiler *c, expr_ty e)
3815{
Mark Shannon13bc1392020-01-23 09:25:17 +00003816 return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET,
3817 SET_ADD, SET_UPDATE, 0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003818}
3819
3820static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003821are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003822{
3823 Py_ssize_t i;
3824 for (i = begin; i < end; i++) {
3825 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003826 if (key == NULL || key->kind != Constant_kind)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003827 return 0;
3828 }
3829 return 1;
3830}
3831
3832static int
3833compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3834{
3835 Py_ssize_t i, n = end - begin;
3836 PyObject *keys, *key;
3837 if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) {
3838 for (i = begin; i < end; i++) {
3839 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3840 }
3841 keys = PyTuple_New(n);
3842 if (keys == NULL) {
3843 return 0;
3844 }
3845 for (i = begin; i < end; i++) {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003846 key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003847 Py_INCREF(key);
3848 PyTuple_SET_ITEM(keys, i - begin, key);
3849 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003850 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003851 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3852 }
3853 else {
3854 for (i = begin; i < end; i++) {
3855 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3856 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3857 }
3858 ADDOP_I(c, BUILD_MAP, n);
3859 }
3860 return 1;
3861}
3862
3863static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003864compiler_dict(struct compiler *c, expr_ty e)
3865{
Victor Stinner976bb402016-03-23 11:36:19 +01003866 Py_ssize_t i, n, elements;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003867 int have_dict;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003868 int is_unpacking = 0;
3869 n = asdl_seq_LEN(e->v.Dict.values);
Mark Shannon8a4cd702020-01-27 09:57:45 +00003870 have_dict = 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003871 elements = 0;
3872 for (i = 0; i < n; i++) {
3873 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003874 if (is_unpacking) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003875 if (elements) {
3876 if (!compiler_subdict(c, e, i - elements, i)) {
3877 return 0;
3878 }
3879 if (have_dict) {
3880 ADDOP_I(c, DICT_UPDATE, 1);
3881 }
3882 have_dict = 1;
3883 elements = 0;
3884 }
3885 if (have_dict == 0) {
3886 ADDOP_I(c, BUILD_MAP, 0);
3887 have_dict = 1;
3888 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003889 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Mark Shannon8a4cd702020-01-27 09:57:45 +00003890 ADDOP_I(c, DICT_UPDATE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003891 }
3892 else {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003893 if (elements == 0xFFFF) {
Pablo Galindoc51db0e2020-08-13 09:48:41 +01003894 if (!compiler_subdict(c, e, i - elements, i + 1)) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003895 return 0;
3896 }
3897 if (have_dict) {
3898 ADDOP_I(c, DICT_UPDATE, 1);
3899 }
3900 have_dict = 1;
3901 elements = 0;
3902 }
3903 else {
3904 elements++;
3905 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003906 }
3907 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003908 if (elements) {
3909 if (!compiler_subdict(c, e, n - elements, n)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003910 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003911 }
3912 if (have_dict) {
3913 ADDOP_I(c, DICT_UPDATE, 1);
3914 }
3915 have_dict = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003916 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003917 if (!have_dict) {
3918 ADDOP_I(c, BUILD_MAP, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003919 }
3920 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003921}
3922
3923static int
3924compiler_compare(struct compiler *c, expr_ty e)
3925{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003926 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003927
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02003928 if (!check_compare(c, e)) {
3929 return 0;
3930 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003931 VISIT(c, expr, e->v.Compare.left);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003932 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
3933 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
3934 if (n == 0) {
3935 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Mark Shannon9af0e472020-01-14 10:12:45 +00003936 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003937 }
3938 else {
3939 basicblock *cleanup = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003940 if (cleanup == NULL)
3941 return 0;
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003942 for (i = 0; i < n; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003943 VISIT(c, expr,
3944 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003945 ADDOP(c, DUP_TOP);
3946 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00003947 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003948 ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003949 NEXT_BLOCK(c);
3950 }
3951 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00003952 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003953 basicblock *end = compiler_new_block(c);
3954 if (end == NULL)
3955 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01003956 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003957 compiler_use_next_block(c, cleanup);
3958 ADDOP(c, ROT_TWO);
3959 ADDOP(c, POP_TOP);
3960 compiler_use_next_block(c, end);
3961 }
3962 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003963}
3964
Serhiy Storchaka62e44812019-02-16 08:12:19 +02003965static PyTypeObject *
3966infer_type(expr_ty e)
3967{
3968 switch (e->kind) {
3969 case Tuple_kind:
3970 return &PyTuple_Type;
3971 case List_kind:
3972 case ListComp_kind:
3973 return &PyList_Type;
3974 case Dict_kind:
3975 case DictComp_kind:
3976 return &PyDict_Type;
3977 case Set_kind:
3978 case SetComp_kind:
3979 return &PySet_Type;
3980 case GeneratorExp_kind:
3981 return &PyGen_Type;
3982 case Lambda_kind:
3983 return &PyFunction_Type;
3984 case JoinedStr_kind:
3985 case FormattedValue_kind:
3986 return &PyUnicode_Type;
3987 case Constant_kind:
Victor Stinnera102ed72020-02-07 02:24:48 +01003988 return Py_TYPE(e->v.Constant.value);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02003989 default:
3990 return NULL;
3991 }
3992}
3993
3994static int
3995check_caller(struct compiler *c, expr_ty e)
3996{
3997 switch (e->kind) {
3998 case Constant_kind:
3999 case Tuple_kind:
4000 case List_kind:
4001 case ListComp_kind:
4002 case Dict_kind:
4003 case DictComp_kind:
4004 case Set_kind:
4005 case SetComp_kind:
4006 case GeneratorExp_kind:
4007 case JoinedStr_kind:
4008 case FormattedValue_kind:
4009 return compiler_warn(c, "'%.200s' object is not callable; "
4010 "perhaps you missed a comma?",
4011 infer_type(e)->tp_name);
4012 default:
4013 return 1;
4014 }
4015}
4016
4017static int
4018check_subscripter(struct compiler *c, expr_ty e)
4019{
4020 PyObject *v;
4021
4022 switch (e->kind) {
4023 case Constant_kind:
4024 v = e->v.Constant.value;
4025 if (!(v == Py_None || v == Py_Ellipsis ||
4026 PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
4027 PyAnySet_Check(v)))
4028 {
4029 return 1;
4030 }
4031 /* fall through */
4032 case Set_kind:
4033 case SetComp_kind:
4034 case GeneratorExp_kind:
4035 case Lambda_kind:
4036 return compiler_warn(c, "'%.200s' object is not subscriptable; "
4037 "perhaps you missed a comma?",
4038 infer_type(e)->tp_name);
4039 default:
4040 return 1;
4041 }
4042}
4043
4044static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004045check_index(struct compiler *c, expr_ty e, expr_ty s)
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004046{
4047 PyObject *v;
4048
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004049 PyTypeObject *index_type = infer_type(s);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004050 if (index_type == NULL
4051 || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
4052 || index_type == &PySlice_Type) {
4053 return 1;
4054 }
4055
4056 switch (e->kind) {
4057 case Constant_kind:
4058 v = e->v.Constant.value;
4059 if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
4060 return 1;
4061 }
4062 /* fall through */
4063 case Tuple_kind:
4064 case List_kind:
4065 case ListComp_kind:
4066 case JoinedStr_kind:
4067 case FormattedValue_kind:
4068 return compiler_warn(c, "%.200s indices must be integers or slices, "
4069 "not %.200s; "
4070 "perhaps you missed a comma?",
4071 infer_type(e)->tp_name,
4072 index_type->tp_name);
4073 default:
4074 return 1;
4075 }
4076}
4077
Zackery Spytz97f5de02019-03-22 01:30:32 -06004078// Return 1 if the method call was optimized, -1 if not, and 0 on error.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004079static int
Yury Selivanovf2392132016-12-13 19:03:51 -05004080maybe_optimize_method_call(struct compiler *c, expr_ty e)
4081{
4082 Py_ssize_t argsl, i;
4083 expr_ty meth = e->v.Call.func;
Pablo Galindoa5634c42020-09-16 19:42:00 +01004084 asdl_expr_seq *args = e->v.Call.args;
Yury Selivanovf2392132016-12-13 19:03:51 -05004085
4086 /* Check that the call node is an attribute access, and that
4087 the call doesn't have keyword parameters. */
4088 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
4089 asdl_seq_LEN(e->v.Call.keywords))
4090 return -1;
4091
4092 /* Check that there are no *varargs types of arguments. */
4093 argsl = asdl_seq_LEN(args);
4094 for (i = 0; i < argsl; i++) {
4095 expr_ty elt = asdl_seq_GET(args, i);
4096 if (elt->kind == Starred_kind) {
4097 return -1;
4098 }
4099 }
4100
4101 /* Alright, we can optimize the code. */
4102 VISIT(c, expr, meth->v.Attribute.value);
4103 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
4104 VISIT_SEQ(c, expr, e->v.Call.args);
4105 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
4106 return 1;
4107}
4108
4109static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004110validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
Zackery Spytz08050e92020-04-06 00:47:47 -06004111{
4112 Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
4113 for (Py_ssize_t i = 0; i < nkeywords; i++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004114 keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
4115 if (key->arg == NULL) {
4116 continue;
4117 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +01004118 if (forbidden_name(c, key->arg, Store)) {
4119 return -1;
4120 }
Zackery Spytz08050e92020-04-06 00:47:47 -06004121 for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004122 keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
4123 if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
4124 PyObject *msg = PyUnicode_FromFormat("keyword argument repeated: %U", key->arg);
4125 if (msg == NULL) {
4126 return -1;
4127 }
4128 c->u->u_col_offset = other->col_offset;
4129 compiler_error(c, PyUnicode_AsUTF8(msg));
4130 Py_DECREF(msg);
4131 return -1;
4132 }
4133 }
4134 }
4135 return 0;
4136}
4137
4138static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004139compiler_call(struct compiler *c, expr_ty e)
4140{
Zackery Spytz97f5de02019-03-22 01:30:32 -06004141 int ret = maybe_optimize_method_call(c, e);
4142 if (ret >= 0) {
4143 return ret;
4144 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004145 if (!check_caller(c, e->v.Call.func)) {
4146 return 0;
4147 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004148 VISIT(c, expr, e->v.Call.func);
4149 return compiler_call_helper(c, 0,
4150 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004151 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004152}
4153
Eric V. Smith235a6f02015-09-19 14:51:32 -04004154static int
4155compiler_joined_str(struct compiler *c, expr_ty e)
4156{
Eric V. Smith235a6f02015-09-19 14:51:32 -04004157 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004158 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1)
4159 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
Eric V. Smith235a6f02015-09-19 14:51:32 -04004160 return 1;
4161}
4162
Eric V. Smitha78c7952015-11-03 12:45:05 -05004163/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004164static int
4165compiler_formatted_value(struct compiler *c, expr_ty e)
4166{
Eric V. Smitha78c7952015-11-03 12:45:05 -05004167 /* Our oparg encodes 2 pieces of information: the conversion
4168 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04004169
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004170 Convert the conversion char to 3 bits:
4171 : 000 0x0 FVC_NONE The default if nothing specified.
Eric V. Smitha78c7952015-11-03 12:45:05 -05004172 !s : 001 0x1 FVC_STR
4173 !r : 010 0x2 FVC_REPR
4174 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04004175
Eric V. Smitha78c7952015-11-03 12:45:05 -05004176 next bit is whether or not we have a format spec:
4177 yes : 100 0x4
4178 no : 000 0x0
4179 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004180
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004181 int conversion = e->v.FormattedValue.conversion;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004182 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004183
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004184 /* The expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004185 VISIT(c, expr, e->v.FormattedValue.value);
4186
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004187 switch (conversion) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004188 case 's': oparg = FVC_STR; break;
4189 case 'r': oparg = FVC_REPR; break;
4190 case 'a': oparg = FVC_ASCII; break;
4191 case -1: oparg = FVC_NONE; break;
4192 default:
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004193 PyErr_Format(PyExc_SystemError,
4194 "Unrecognized conversion character %d", conversion);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004195 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004196 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004197 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004198 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004199 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004200 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004201 }
4202
Eric V. Smitha78c7952015-11-03 12:45:05 -05004203 /* And push our opcode and oparg */
4204 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004205
Eric V. Smith235a6f02015-09-19 14:51:32 -04004206 return 1;
4207}
4208
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004209static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004210compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004211{
4212 Py_ssize_t i, n = end - begin;
4213 keyword_ty kw;
4214 PyObject *keys, *key;
4215 assert(n > 0);
4216 if (n > 1) {
4217 for (i = begin; i < end; i++) {
4218 kw = asdl_seq_GET(keywords, i);
4219 VISIT(c, expr, kw->value);
4220 }
4221 keys = PyTuple_New(n);
4222 if (keys == NULL) {
4223 return 0;
4224 }
4225 for (i = begin; i < end; i++) {
4226 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
4227 Py_INCREF(key);
4228 PyTuple_SET_ITEM(keys, i - begin, key);
4229 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004230 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004231 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
4232 }
4233 else {
4234 /* a for loop only executes once */
4235 for (i = begin; i < end; i++) {
4236 kw = asdl_seq_GET(keywords, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004237 ADDOP_LOAD_CONST(c, kw->arg);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004238 VISIT(c, expr, kw->value);
4239 }
4240 ADDOP_I(c, BUILD_MAP, n);
4241 }
4242 return 1;
4243}
4244
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004245/* shared code between compiler_call and compiler_class */
4246static int
4247compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01004248 int n, /* Args already pushed */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004249 asdl_expr_seq *args,
4250 asdl_keyword_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004251{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004252 Py_ssize_t i, nseen, nelts, nkwelts;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004253
Pablo Galindo254ec782020-04-03 20:37:13 +01004254 if (validate_keywords(c, keywords) == -1) {
4255 return 0;
4256 }
4257
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004258 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004259 nkwelts = asdl_seq_LEN(keywords);
4260
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004261 for (i = 0; i < nelts; i++) {
4262 expr_ty elt = asdl_seq_GET(args, i);
4263 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00004264 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004265 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004266 }
4267 for (i = 0; i < nkwelts; i++) {
4268 keyword_ty kw = asdl_seq_GET(keywords, i);
4269 if (kw->arg == NULL) {
4270 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004271 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004272 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004273
Mark Shannon13bc1392020-01-23 09:25:17 +00004274 /* No * or ** args, so can use faster calling sequence */
4275 for (i = 0; i < nelts; i++) {
4276 expr_ty elt = asdl_seq_GET(args, i);
4277 assert(elt->kind != Starred_kind);
4278 VISIT(c, expr, elt);
4279 }
4280 if (nkwelts) {
4281 PyObject *names;
4282 VISIT_SEQ(c, keyword, keywords);
4283 names = PyTuple_New(nkwelts);
4284 if (names == NULL) {
4285 return 0;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004286 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004287 for (i = 0; i < nkwelts; i++) {
4288 keyword_ty kw = asdl_seq_GET(keywords, i);
4289 Py_INCREF(kw->arg);
4290 PyTuple_SET_ITEM(names, i, kw->arg);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004291 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004292 ADDOP_LOAD_CONST_NEW(c, names);
4293 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
4294 return 1;
4295 }
4296 else {
4297 ADDOP_I(c, CALL_FUNCTION, n + nelts);
4298 return 1;
4299 }
4300
4301ex_call:
4302
4303 /* Do positional arguments. */
4304 if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
4305 VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
4306 }
4307 else if (starunpack_helper(c, args, n, BUILD_LIST,
4308 LIST_APPEND, LIST_EXTEND, 1) == 0) {
4309 return 0;
4310 }
4311 /* Then keyword arguments */
4312 if (nkwelts) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004313 /* Has a new dict been pushed */
4314 int have_dict = 0;
Mark Shannon13bc1392020-01-23 09:25:17 +00004315
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004316 nseen = 0; /* the number of keyword arguments on the stack following */
4317 for (i = 0; i < nkwelts; i++) {
4318 keyword_ty kw = asdl_seq_GET(keywords, i);
4319 if (kw->arg == NULL) {
4320 /* A keyword argument unpacking. */
4321 if (nseen) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004322 if (!compiler_subkwargs(c, keywords, i - nseen, i)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004323 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004324 }
Mark Shannondb64f122020-06-01 10:42:42 +01004325 if (have_dict) {
4326 ADDOP_I(c, DICT_MERGE, 1);
4327 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004328 have_dict = 1;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004329 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004330 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004331 if (!have_dict) {
4332 ADDOP_I(c, BUILD_MAP, 0);
4333 have_dict = 1;
4334 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004335 VISIT(c, expr, kw->value);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004336 ADDOP_I(c, DICT_MERGE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004337 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004338 else {
4339 nseen++;
4340 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004341 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004342 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004343 /* Pack up any trailing keyword arguments. */
Mark Shannon8a4cd702020-01-27 09:57:45 +00004344 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004345 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004346 }
4347 if (have_dict) {
4348 ADDOP_I(c, DICT_MERGE, 1);
4349 }
4350 have_dict = 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004351 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004352 assert(have_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004353 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004354 ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0);
4355 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004356}
4357
Nick Coghlan650f0d02007-04-15 12:05:43 +00004358
4359/* List and set comprehensions and generator expressions work by creating a
4360 nested function to perform the actual iteration. This means that the
4361 iteration variables don't leak into the current scope.
4362 The defined function is called immediately following its definition, with the
4363 result of that call being the result of the expression.
4364 The LC/SC version returns the populated container, while the GE version is
4365 flagged in symtable.c as a generator, so it returns the generator object
4366 when the function is called.
Nick Coghlan650f0d02007-04-15 12:05:43 +00004367
4368 Possible cleanups:
4369 - iterate over the generator sequence instead of using recursion
4370*/
4371
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004372
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004373static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374compiler_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004375 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004376 int depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004377 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004378{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004379 comprehension_ty gen;
4380 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4381 if (gen->is_async) {
4382 return compiler_async_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004383 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004384 } else {
4385 return compiler_sync_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004386 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004387 }
4388}
4389
4390static int
4391compiler_sync_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004392 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004393 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004394 expr_ty elt, expr_ty val, int type)
4395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004396 /* generate code for the iterator, then each of the ifs,
4397 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004399 comprehension_ty gen;
4400 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004401 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004403 start = compiler_new_block(c);
4404 skip = compiler_new_block(c);
4405 if_cleanup = compiler_new_block(c);
4406 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004408 if (start == NULL || skip == NULL || if_cleanup == NULL ||
4409 anchor == NULL)
4410 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004412 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 if (gen_index == 0) {
4415 /* Receive outermost iter as an implicit argument */
4416 c->u->u_argcount = 1;
4417 ADDOP_I(c, LOAD_FAST, 0);
4418 }
4419 else {
4420 /* Sub-iter - calculate on the fly */
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004421 /* Fast path for the temporary variable assignment idiom:
4422 for y in [f(x)]
4423 */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004424 asdl_expr_seq *elts;
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004425 switch (gen->iter->kind) {
4426 case List_kind:
4427 elts = gen->iter->v.List.elts;
4428 break;
4429 case Tuple_kind:
4430 elts = gen->iter->v.Tuple.elts;
4431 break;
4432 default:
4433 elts = NULL;
4434 }
4435 if (asdl_seq_LEN(elts) == 1) {
4436 expr_ty elt = asdl_seq_GET(elts, 0);
4437 if (elt->kind != Starred_kind) {
4438 VISIT(c, expr, elt);
4439 start = NULL;
4440 }
4441 }
4442 if (start) {
4443 VISIT(c, expr, gen->iter);
4444 ADDOP(c, GET_ITER);
4445 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004446 }
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004447 if (start) {
4448 depth++;
4449 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01004450 ADDOP_JUMP(c, FOR_ITER, anchor);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004451 NEXT_BLOCK(c);
4452 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004453 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004455 /* XXX this needs to be cleaned up...a lot! */
4456 n = asdl_seq_LEN(gen->ifs);
4457 for (i = 0; i < n; i++) {
4458 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004459 if (!compiler_jump_if(c, e, if_cleanup, 0))
4460 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004461 NEXT_BLOCK(c);
4462 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004464 if (++gen_index < asdl_seq_LEN(generators))
4465 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004466 generators, gen_index, depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004467 elt, val, type))
4468 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004470 /* only append after the last for generator */
4471 if (gen_index >= asdl_seq_LEN(generators)) {
4472 /* comprehension specific code */
4473 switch (type) {
4474 case COMP_GENEXP:
4475 VISIT(c, expr, elt);
4476 ADDOP(c, YIELD_VALUE);
4477 ADDOP(c, POP_TOP);
4478 break;
4479 case COMP_LISTCOMP:
4480 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004481 ADDOP_I(c, LIST_APPEND, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004482 break;
4483 case COMP_SETCOMP:
4484 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004485 ADDOP_I(c, SET_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004486 break;
4487 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004488 /* With '{k: v}', k is evaluated before v, so we do
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 the same. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004490 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004491 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004492 ADDOP_I(c, MAP_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004493 break;
4494 default:
4495 return 0;
4496 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 compiler_use_next_block(c, skip);
4499 }
4500 compiler_use_next_block(c, if_cleanup);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004501 if (start) {
Mark Shannon582aaf12020-08-04 17:30:11 +01004502 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004503 compiler_use_next_block(c, anchor);
4504 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505
4506 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004507}
4508
4509static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004510compiler_async_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004511 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004512 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004513 expr_ty elt, expr_ty val, int type)
4514{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004515 comprehension_ty gen;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004516 basicblock *start, *if_cleanup, *except;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004517 Py_ssize_t i, n;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004518 start = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004519 except = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004520 if_cleanup = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004521
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004522 if (start == NULL || if_cleanup == NULL || except == NULL) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004523 return 0;
4524 }
4525
4526 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4527
4528 if (gen_index == 0) {
4529 /* Receive outermost iter as an implicit argument */
4530 c->u->u_argcount = 1;
4531 ADDOP_I(c, LOAD_FAST, 0);
4532 }
4533 else {
4534 /* Sub-iter - calculate on the fly */
4535 VISIT(c, expr, gen->iter);
4536 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004537 }
4538
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004539 compiler_use_next_block(c, start);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004540
Mark Shannon582aaf12020-08-04 17:30:11 +01004541 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004542 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004543 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004544 ADDOP(c, YIELD_FROM);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004545 ADDOP(c, POP_BLOCK);
Serhiy Storchaka24d32012018-03-10 18:22:34 +02004546 VISIT(c, expr, gen->target);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004547
4548 n = asdl_seq_LEN(gen->ifs);
4549 for (i = 0; i < n; i++) {
4550 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004551 if (!compiler_jump_if(c, e, if_cleanup, 0))
4552 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004553 NEXT_BLOCK(c);
4554 }
4555
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004556 depth++;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004557 if (++gen_index < asdl_seq_LEN(generators))
4558 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004559 generators, gen_index, depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004560 elt, val, type))
4561 return 0;
4562
4563 /* only append after the last for generator */
4564 if (gen_index >= asdl_seq_LEN(generators)) {
4565 /* comprehension specific code */
4566 switch (type) {
4567 case COMP_GENEXP:
4568 VISIT(c, expr, elt);
4569 ADDOP(c, YIELD_VALUE);
4570 ADDOP(c, POP_TOP);
4571 break;
4572 case COMP_LISTCOMP:
4573 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004574 ADDOP_I(c, LIST_APPEND, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004575 break;
4576 case COMP_SETCOMP:
4577 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004578 ADDOP_I(c, SET_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004579 break;
4580 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004581 /* With '{k: v}', k is evaluated before v, so we do
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004582 the same. */
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004583 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004584 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004585 ADDOP_I(c, MAP_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004586 break;
4587 default:
4588 return 0;
4589 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004590 }
4591 compiler_use_next_block(c, if_cleanup);
Mark Shannon582aaf12020-08-04 17:30:11 +01004592 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004593
4594 compiler_use_next_block(c, except);
4595 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004596
4597 return 1;
4598}
4599
4600static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004601compiler_comprehension(struct compiler *c, expr_ty e, int type,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004602 identifier name, asdl_comprehension_seq *generators, expr_ty elt,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004603 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004604{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004605 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004606 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004607 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004608 int is_async_generator = 0;
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004609 int top_level_await = IS_TOP_LEVEL_AWAIT(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004610
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004611
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004612 int is_async_function = c->u->u_ste->ste_coroutine;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004613
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004614 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004615 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4616 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004617 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004618 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004619 }
4620
4621 is_async_generator = c->u->u_ste->ste_coroutine;
4622
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004623 if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004624 compiler_error(c, "asynchronous comprehension outside of "
4625 "an asynchronous function");
4626 goto error_in_scope;
4627 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004629 if (type != COMP_GENEXP) {
4630 int op;
4631 switch (type) {
4632 case COMP_LISTCOMP:
4633 op = BUILD_LIST;
4634 break;
4635 case COMP_SETCOMP:
4636 op = BUILD_SET;
4637 break;
4638 case COMP_DICTCOMP:
4639 op = BUILD_MAP;
4640 break;
4641 default:
4642 PyErr_Format(PyExc_SystemError,
4643 "unknown comprehension type %d", type);
4644 goto error_in_scope;
4645 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004647 ADDOP_I(c, op, 0);
4648 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004649
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004650 if (!compiler_comprehension_generator(c, generators, 0, 0, elt,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004651 val, type))
4652 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004654 if (type != COMP_GENEXP) {
4655 ADDOP(c, RETURN_VALUE);
4656 }
4657
4658 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004659 qualname = c->u->u_qualname;
4660 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004661 compiler_exit_scope(c);
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004662 if (top_level_await && is_async_generator){
4663 c->u->u_ste->ste_coroutine = 1;
4664 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004665 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004666 goto error;
4667
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004668 if (!compiler_make_closure(c, co, 0, qualname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004669 goto error;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004670 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004671 Py_DECREF(co);
4672
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004673 VISIT(c, expr, outermost->iter);
4674
4675 if (outermost->is_async) {
4676 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004677 } else {
4678 ADDOP(c, GET_ITER);
4679 }
4680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004681 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004682
4683 if (is_async_generator && type != COMP_GENEXP) {
4684 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004685 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004686 ADDOP(c, YIELD_FROM);
4687 }
4688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004689 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004690error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004691 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004692error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004693 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004694 Py_XDECREF(co);
4695 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004696}
4697
4698static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004699compiler_genexp(struct compiler *c, expr_ty e)
4700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004701 static identifier name;
4702 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004703 name = PyUnicode_InternFromString("<genexpr>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004704 if (!name)
4705 return 0;
4706 }
4707 assert(e->kind == GeneratorExp_kind);
4708 return compiler_comprehension(c, e, COMP_GENEXP, name,
4709 e->v.GeneratorExp.generators,
4710 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004711}
4712
4713static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004714compiler_listcomp(struct compiler *c, expr_ty e)
4715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004716 static identifier name;
4717 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004718 name = PyUnicode_InternFromString("<listcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004719 if (!name)
4720 return 0;
4721 }
4722 assert(e->kind == ListComp_kind);
4723 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4724 e->v.ListComp.generators,
4725 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004726}
4727
4728static int
4729compiler_setcomp(struct compiler *c, expr_ty e)
4730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004731 static identifier name;
4732 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004733 name = PyUnicode_InternFromString("<setcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004734 if (!name)
4735 return 0;
4736 }
4737 assert(e->kind == SetComp_kind);
4738 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4739 e->v.SetComp.generators,
4740 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004741}
4742
4743
4744static int
4745compiler_dictcomp(struct compiler *c, expr_ty e)
4746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004747 static identifier name;
4748 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004749 name = PyUnicode_InternFromString("<dictcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004750 if (!name)
4751 return 0;
4752 }
4753 assert(e->kind == DictComp_kind);
4754 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4755 e->v.DictComp.generators,
4756 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004757}
4758
4759
4760static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004761compiler_visit_keyword(struct compiler *c, keyword_ty k)
4762{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004763 VISIT(c, expr, k->value);
4764 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004765}
4766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004767/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004768 whether they are true or false.
4769
4770 Return values: 1 for true, 0 for false, -1 for non-constant.
4771 */
4772
4773static int
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02004774expr_constant(expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004775{
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004776 if (e->kind == Constant_kind) {
4777 return PyObject_IsTrue(e->v.Constant.value);
Benjamin Peterson442f2092012-12-06 17:41:04 -05004778 }
Serhiy Storchaka3325a672017-12-15 12:35:48 +02004779 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004780}
4781
Mark Shannonfee55262019-11-21 09:11:43 +00004782static int
4783compiler_with_except_finish(struct compiler *c) {
4784 basicblock *exit;
4785 exit = compiler_new_block(c);
4786 if (exit == NULL)
4787 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01004788 ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00004789 ADDOP(c, RERAISE);
4790 compiler_use_next_block(c, exit);
4791 ADDOP(c, POP_TOP);
4792 ADDOP(c, POP_TOP);
4793 ADDOP(c, POP_TOP);
4794 ADDOP(c, POP_EXCEPT);
4795 ADDOP(c, POP_TOP);
4796 return 1;
4797}
Yury Selivanov75445082015-05-11 22:57:16 -04004798
4799/*
4800 Implements the async with statement.
4801
4802 The semantics outlined in that PEP are as follows:
4803
4804 async with EXPR as VAR:
4805 BLOCK
4806
4807 It is implemented roughly as:
4808
4809 context = EXPR
4810 exit = context.__aexit__ # not calling it
4811 value = await context.__aenter__()
4812 try:
4813 VAR = value # if VAR present in the syntax
4814 BLOCK
4815 finally:
4816 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004817 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04004818 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004819 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04004820 if not (await exit(*exc)):
4821 raise
4822 */
4823static int
4824compiler_async_with(struct compiler *c, stmt_ty s, int pos)
4825{
Mark Shannonfee55262019-11-21 09:11:43 +00004826 basicblock *block, *final, *exit;
Yury Selivanov75445082015-05-11 22:57:16 -04004827 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
4828
4829 assert(s->kind == AsyncWith_kind);
Pablo Galindo90235812020-03-15 04:29:22 +00004830 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07004831 c->u->u_ste->ste_coroutine = 1;
4832 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
Zsolt Dollensteine2396502018-04-27 08:58:56 -07004833 return compiler_error(c, "'async with' outside async function");
4834 }
Yury Selivanov75445082015-05-11 22:57:16 -04004835
4836 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004837 final = compiler_new_block(c);
4838 exit = compiler_new_block(c);
4839 if (!block || !final || !exit)
Yury Selivanov75445082015-05-11 22:57:16 -04004840 return 0;
4841
4842 /* Evaluate EXPR */
4843 VISIT(c, expr, item->context_expr);
4844
4845 ADDOP(c, BEFORE_ASYNC_WITH);
4846 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004847 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004848 ADDOP(c, YIELD_FROM);
4849
Mark Shannon582aaf12020-08-04 17:30:11 +01004850 ADDOP_JUMP(c, SETUP_ASYNC_WITH, final);
Yury Selivanov75445082015-05-11 22:57:16 -04004851
4852 /* SETUP_ASYNC_WITH pushes a finally block. */
4853 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004854 if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004855 return 0;
4856 }
4857
4858 if (item->optional_vars) {
4859 VISIT(c, expr, item->optional_vars);
4860 }
4861 else {
4862 /* Discard result from context.__aenter__() */
4863 ADDOP(c, POP_TOP);
4864 }
4865
4866 pos++;
4867 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
4868 /* BLOCK code */
4869 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
4870 else if (!compiler_async_with(c, s, pos))
4871 return 0;
4872
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004873 compiler_pop_fblock(c, ASYNC_WITH, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004874 ADDOP(c, POP_BLOCK);
4875 /* End of body; start the cleanup */
Yury Selivanov75445082015-05-11 22:57:16 -04004876
Mark Shannonfee55262019-11-21 09:11:43 +00004877 /* For successful outcome:
4878 * call __exit__(None, None, None)
4879 */
4880 if(!compiler_call_exit_with_nones(c))
Yury Selivanov75445082015-05-11 22:57:16 -04004881 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00004882 ADDOP(c, GET_AWAITABLE);
4883 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4884 ADDOP(c, YIELD_FROM);
Yury Selivanov75445082015-05-11 22:57:16 -04004885
Mark Shannonfee55262019-11-21 09:11:43 +00004886 ADDOP(c, POP_TOP);
Yury Selivanov75445082015-05-11 22:57:16 -04004887
Mark Shannon582aaf12020-08-04 17:30:11 +01004888 ADDOP_JUMP(c, JUMP_ABSOLUTE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00004889
4890 /* For exceptional outcome: */
4891 compiler_use_next_block(c, final);
4892
4893 ADDOP(c, WITH_EXCEPT_START);
Yury Selivanov75445082015-05-11 22:57:16 -04004894 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004895 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004896 ADDOP(c, YIELD_FROM);
Mark Shannonfee55262019-11-21 09:11:43 +00004897 compiler_with_except_finish(c);
Yury Selivanov75445082015-05-11 22:57:16 -04004898
Mark Shannonfee55262019-11-21 09:11:43 +00004899compiler_use_next_block(c, exit);
Yury Selivanov75445082015-05-11 22:57:16 -04004900 return 1;
4901}
4902
4903
Guido van Rossumc2e20742006-02-27 22:32:47 +00004904/*
4905 Implements the with statement from PEP 343.
Guido van Rossumc2e20742006-02-27 22:32:47 +00004906 with EXPR as VAR:
4907 BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00004908 is implemented as:
4909 <code for EXPR>
4910 SETUP_WITH E
4911 <code to store to VAR> or POP_TOP
4912 <code for BLOCK>
4913 LOAD_CONST (None, None, None)
4914 CALL_FUNCTION_EX 0
4915 JUMP_FORWARD EXIT
4916 E: WITH_EXCEPT_START (calls EXPR.__exit__)
4917 POP_JUMP_IF_TRUE T:
4918 RERAISE
4919 T: POP_TOP * 3 (remove exception from stack)
4920 POP_EXCEPT
4921 POP_TOP
4922 EXIT:
Guido van Rossumc2e20742006-02-27 22:32:47 +00004923 */
Mark Shannonfee55262019-11-21 09:11:43 +00004924
Guido van Rossumc2e20742006-02-27 22:32:47 +00004925static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004926compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004927{
Mark Shannonfee55262019-11-21 09:11:43 +00004928 basicblock *block, *final, *exit;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004929 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004930
4931 assert(s->kind == With_kind);
4932
Guido van Rossumc2e20742006-02-27 22:32:47 +00004933 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004934 final = compiler_new_block(c);
4935 exit = compiler_new_block(c);
4936 if (!block || !final || !exit)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004937 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004938
Thomas Wouters477c8d52006-05-27 19:21:47 +00004939 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004940 VISIT(c, expr, item->context_expr);
Mark Shannonfee55262019-11-21 09:11:43 +00004941 /* Will push bound __exit__ */
Mark Shannon582aaf12020-08-04 17:30:11 +01004942 ADDOP_JUMP(c, SETUP_WITH, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004943
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004944 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00004945 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004946 if (!compiler_push_fblock(c, WITH, block, final, NULL)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004947 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004948 }
4949
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004950 if (item->optional_vars) {
4951 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004952 }
4953 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004954 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004955 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004956 }
4957
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004958 pos++;
4959 if (pos == asdl_seq_LEN(s->v.With.items))
4960 /* BLOCK code */
4961 VISIT_SEQ(c, stmt, s->v.With.body)
4962 else if (!compiler_with(c, s, pos))
4963 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004964
Guido van Rossumc2e20742006-02-27 22:32:47 +00004965 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004966 compiler_pop_fblock(c, WITH, block);
Mark Shannon13bc1392020-01-23 09:25:17 +00004967
Mark Shannonfee55262019-11-21 09:11:43 +00004968 /* End of body; start the cleanup. */
Mark Shannon13bc1392020-01-23 09:25:17 +00004969
Mark Shannonfee55262019-11-21 09:11:43 +00004970 /* For successful outcome:
4971 * call __exit__(None, None, None)
4972 */
4973 if (!compiler_call_exit_with_nones(c))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004974 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00004975 ADDOP(c, POP_TOP);
Mark Shannon582aaf12020-08-04 17:30:11 +01004976 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004977
Mark Shannonfee55262019-11-21 09:11:43 +00004978 /* For exceptional outcome: */
4979 compiler_use_next_block(c, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004980
Mark Shannonfee55262019-11-21 09:11:43 +00004981 ADDOP(c, WITH_EXCEPT_START);
4982 compiler_with_except_finish(c);
4983
4984 compiler_use_next_block(c, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004985 return 1;
4986}
4987
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004988static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03004989compiler_visit_expr1(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004990{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004991 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07004992 case NamedExpr_kind:
4993 VISIT(c, expr, e->v.NamedExpr.value);
4994 ADDOP(c, DUP_TOP);
4995 VISIT(c, expr, e->v.NamedExpr.target);
4996 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004997 case BoolOp_kind:
4998 return compiler_boolop(c, e);
4999 case BinOp_kind:
5000 VISIT(c, expr, e->v.BinOp.left);
5001 VISIT(c, expr, e->v.BinOp.right);
Andy Lester76d58772020-03-10 21:18:12 -05005002 ADDOP(c, binop(e->v.BinOp.op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005003 break;
5004 case UnaryOp_kind:
5005 VISIT(c, expr, e->v.UnaryOp.operand);
5006 ADDOP(c, unaryop(e->v.UnaryOp.op));
5007 break;
5008 case Lambda_kind:
5009 return compiler_lambda(c, e);
5010 case IfExp_kind:
5011 return compiler_ifexp(c, e);
5012 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005013 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005014 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005015 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005016 case GeneratorExp_kind:
5017 return compiler_genexp(c, e);
5018 case ListComp_kind:
5019 return compiler_listcomp(c, e);
5020 case SetComp_kind:
5021 return compiler_setcomp(c, e);
5022 case DictComp_kind:
5023 return compiler_dictcomp(c, e);
5024 case Yield_kind:
5025 if (c->u->u_ste->ste_type != FunctionBlock)
5026 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005027 if (e->v.Yield.value) {
5028 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005029 }
5030 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005031 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005032 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005033 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005034 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005035 case YieldFrom_kind:
5036 if (c->u->u_ste->ste_type != FunctionBlock)
5037 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04005038
5039 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
5040 return compiler_error(c, "'yield from' inside async function");
5041
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005042 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04005043 ADDOP(c, GET_YIELD_FROM_ITER);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005044 ADDOP_LOAD_CONST(c, Py_None);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005045 ADDOP(c, YIELD_FROM);
5046 break;
Yury Selivanov75445082015-05-11 22:57:16 -04005047 case Await_kind:
Pablo Galindo90235812020-03-15 04:29:22 +00005048 if (!IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005049 if (c->u->u_ste->ste_type != FunctionBlock){
5050 return compiler_error(c, "'await' outside function");
5051 }
Yury Selivanov75445082015-05-11 22:57:16 -04005052
Victor Stinner331a6a52019-05-27 16:39:22 +02005053 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005054 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
5055 return compiler_error(c, "'await' outside async function");
5056 }
5057 }
Yury Selivanov75445082015-05-11 22:57:16 -04005058
5059 VISIT(c, expr, e->v.Await.value);
5060 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005061 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005062 ADDOP(c, YIELD_FROM);
5063 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005064 case Compare_kind:
5065 return compiler_compare(c, e);
5066 case Call_kind:
5067 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005068 case Constant_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005069 ADDOP_LOAD_CONST(c, e->v.Constant.value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005070 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005071 case JoinedStr_kind:
5072 return compiler_joined_str(c, e);
5073 case FormattedValue_kind:
5074 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005075 /* The following exprs can be assignment targets. */
5076 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005077 VISIT(c, expr, e->v.Attribute.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005078 switch (e->v.Attribute.ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005079 case Load:
5080 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
5081 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005082 case Store:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005083 if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx))
5084 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005085 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5086 break;
5087 case Del:
5088 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
5089 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005090 }
5091 break;
5092 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005093 return compiler_subscript(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005094 case Starred_kind:
5095 switch (e->v.Starred.ctx) {
5096 case Store:
5097 /* In all legitimate cases, the Starred node was already replaced
5098 * by compiler_list/compiler_tuple. XXX: is that okay? */
5099 return compiler_error(c,
5100 "starred assignment target must be in a list or tuple");
5101 default:
5102 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005103 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005104 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005105 break;
5106 case Slice_kind:
5107 return compiler_slice(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005108 case Name_kind:
5109 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
5110 /* child nodes of List and Tuple will have expr_context set */
5111 case List_kind:
5112 return compiler_list(c, e);
5113 case Tuple_kind:
5114 return compiler_tuple(c, e);
5115 }
5116 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005117}
5118
5119static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005120compiler_visit_expr(struct compiler *c, expr_ty e)
5121{
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005122 int old_lineno = c->u->u_lineno;
5123 int old_col_offset = c->u->u_col_offset;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005124 SET_LOC(c, e);
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005125 int res = compiler_visit_expr1(c, e);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005126 c->u->u_lineno = old_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005127 c->u->u_col_offset = old_col_offset;
5128 return res;
5129}
5130
5131static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005132compiler_augassign(struct compiler *c, stmt_ty s)
5133{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005134 assert(s->kind == AugAssign_kind);
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005135 expr_ty e = s->v.AugAssign.target;
5136
5137 int old_lineno = c->u->u_lineno;
5138 int old_col_offset = c->u->u_col_offset;
5139 SET_LOC(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005141 switch (e->kind) {
5142 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005143 VISIT(c, expr, e->v.Attribute.value);
5144 ADDOP(c, DUP_TOP);
5145 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005146 break;
5147 case Subscript_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005148 VISIT(c, expr, e->v.Subscript.value);
5149 VISIT(c, expr, e->v.Subscript.slice);
5150 ADDOP(c, DUP_TOP_TWO);
5151 ADDOP(c, BINARY_SUBSCR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005152 break;
5153 case Name_kind:
5154 if (!compiler_nameop(c, e->v.Name.id, Load))
5155 return 0;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005156 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005157 default:
5158 PyErr_Format(PyExc_SystemError,
5159 "invalid node type (%d) for augmented assignment",
5160 e->kind);
5161 return 0;
5162 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005163
5164 c->u->u_lineno = old_lineno;
5165 c->u->u_col_offset = old_col_offset;
5166
5167 VISIT(c, expr, s->v.AugAssign.value);
5168 ADDOP(c, inplace_binop(s->v.AugAssign.op));
5169
5170 SET_LOC(c, e);
5171
5172 switch (e->kind) {
5173 case Attribute_kind:
5174 ADDOP(c, ROT_TWO);
5175 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5176 break;
5177 case Subscript_kind:
5178 ADDOP(c, ROT_THREE);
5179 ADDOP(c, STORE_SUBSCR);
5180 break;
5181 case Name_kind:
5182 return compiler_nameop(c, e->v.Name.id, Store);
5183 default:
5184 Py_UNREACHABLE();
5185 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005186 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005187}
5188
5189static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005190check_ann_expr(struct compiler *c, expr_ty e)
5191{
5192 VISIT(c, expr, e);
5193 ADDOP(c, POP_TOP);
5194 return 1;
5195}
5196
5197static int
5198check_annotation(struct compiler *c, stmt_ty s)
5199{
5200 /* Annotations are only evaluated in a module or class. */
5201 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5202 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
5203 return check_ann_expr(c, s->v.AnnAssign.annotation);
5204 }
5205 return 1;
5206}
5207
5208static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005209check_ann_subscr(struct compiler *c, expr_ty e)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005210{
5211 /* We check that everything in a subscript is defined at runtime. */
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005212 switch (e->kind) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005213 case Slice_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005214 if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005215 return 0;
5216 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005217 if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) {
5218 return 0;
5219 }
5220 if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) {
5221 return 0;
5222 }
5223 return 1;
5224 case Tuple_kind: {
5225 /* extended slice */
Pablo Galindoa5634c42020-09-16 19:42:00 +01005226 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005227 Py_ssize_t i, n = asdl_seq_LEN(elts);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005228 for (i = 0; i < n; i++) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005229 if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005230 return 0;
5231 }
5232 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005233 return 1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005234 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005235 default:
5236 return check_ann_expr(c, e);
5237 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005238}
5239
5240static int
5241compiler_annassign(struct compiler *c, stmt_ty s)
5242{
5243 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07005244 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005245
5246 assert(s->kind == AnnAssign_kind);
5247
5248 /* We perform the actual assignment first. */
5249 if (s->v.AnnAssign.value) {
5250 VISIT(c, expr, s->v.AnnAssign.value);
5251 VISIT(c, expr, targ);
5252 }
5253 switch (targ->kind) {
5254 case Name_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005255 if (forbidden_name(c, targ->v.Name.id, Store))
5256 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005257 /* If we have a simple name in a module or class, store annotation. */
5258 if (s->v.AnnAssign.simple &&
5259 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5260 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Batuhan Taskaya044a1042020-10-06 23:03:02 +03005261 VISIT(c, annexpr, s->v.AnnAssign.annotation);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005262 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02005263 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005264 ADDOP_LOAD_CONST_NEW(c, mangled);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005265 ADDOP(c, STORE_SUBSCR);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005266 }
5267 break;
5268 case Attribute_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005269 if (forbidden_name(c, targ->v.Attribute.attr, Store))
5270 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005271 if (!s->v.AnnAssign.value &&
5272 !check_ann_expr(c, targ->v.Attribute.value)) {
5273 return 0;
5274 }
5275 break;
5276 case Subscript_kind:
5277 if (!s->v.AnnAssign.value &&
5278 (!check_ann_expr(c, targ->v.Subscript.value) ||
5279 !check_ann_subscr(c, targ->v.Subscript.slice))) {
5280 return 0;
5281 }
5282 break;
5283 default:
5284 PyErr_Format(PyExc_SystemError,
5285 "invalid node type (%d) for annotated assignment",
5286 targ->kind);
5287 return 0;
5288 }
5289 /* Annotation is evaluated last. */
5290 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
5291 return 0;
5292 }
5293 return 1;
5294}
5295
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005296/* Raises a SyntaxError and returns 0.
5297 If something goes wrong, a different exception may be raised.
5298*/
5299
5300static int
5301compiler_error(struct compiler *c, const char *errstr)
5302{
Benjamin Peterson43b06862011-05-27 09:08:01 -05005303 PyObject *loc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005304 PyObject *u = NULL, *v = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005305
Victor Stinner14e461d2013-08-26 22:28:21 +02005306 loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005307 if (!loc) {
5308 Py_INCREF(Py_None);
5309 loc = Py_None;
5310 }
Victor Stinner14e461d2013-08-26 22:28:21 +02005311 u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04005312 c->u->u_col_offset + 1, loc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005313 if (!u)
5314 goto exit;
5315 v = Py_BuildValue("(zO)", errstr, u);
5316 if (!v)
5317 goto exit;
5318 PyErr_SetObject(PyExc_SyntaxError, v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005319 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005320 Py_DECREF(loc);
5321 Py_XDECREF(u);
5322 Py_XDECREF(v);
5323 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005324}
5325
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005326/* Emits a SyntaxWarning and returns 1 on success.
5327 If a SyntaxWarning raised as error, replaces it with a SyntaxError
5328 and returns 0.
5329*/
5330static int
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005331compiler_warn(struct compiler *c, const char *format, ...)
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005332{
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005333 va_list vargs;
5334#ifdef HAVE_STDARG_PROTOTYPES
5335 va_start(vargs, format);
5336#else
5337 va_start(vargs);
5338#endif
5339 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5340 va_end(vargs);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005341 if (msg == NULL) {
5342 return 0;
5343 }
5344 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
5345 c->u->u_lineno, NULL, NULL) < 0)
5346 {
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005347 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005348 /* Replace the SyntaxWarning exception with a SyntaxError
5349 to get a more accurate error report */
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005350 PyErr_Clear();
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005351 assert(PyUnicode_AsUTF8(msg) != NULL);
5352 compiler_error(c, PyUnicode_AsUTF8(msg));
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005353 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005354 Py_DECREF(msg);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005355 return 0;
5356 }
5357 Py_DECREF(msg);
5358 return 1;
5359}
5360
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005361static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005362compiler_subscript(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005363{
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005364 expr_context_ty ctx = e->v.Subscript.ctx;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005365 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005366
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005367 if (ctx == Load) {
5368 if (!check_subscripter(c, e->v.Subscript.value)) {
5369 return 0;
5370 }
5371 if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
5372 return 0;
5373 }
5374 }
5375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005376 switch (ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005377 case Load: op = BINARY_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005378 case Store: op = STORE_SUBSCR; break;
5379 case Del: op = DELETE_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005380 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005381 assert(op);
5382 VISIT(c, expr, e->v.Subscript.value);
5383 VISIT(c, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005384 ADDOP(c, op);
5385 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005386}
5387
5388static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005389compiler_slice(struct compiler *c, expr_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005391 int n = 2;
5392 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005394 /* only handles the cases where BUILD_SLICE is emitted */
5395 if (s->v.Slice.lower) {
5396 VISIT(c, expr, s->v.Slice.lower);
5397 }
5398 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005399 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005400 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005402 if (s->v.Slice.upper) {
5403 VISIT(c, expr, s->v.Slice.upper);
5404 }
5405 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005406 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005407 }
5408
5409 if (s->v.Slice.step) {
5410 n++;
5411 VISIT(c, expr, s->v.Slice.step);
5412 }
5413 ADDOP_I(c, BUILD_SLICE, n);
5414 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005415}
5416
Thomas Wouters89f507f2006-12-13 04:49:30 +00005417/* End of the compiler section, beginning of the assembler section */
5418
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005419/* do depth-first search of basic block graph, starting with block.
T. Wouters99b54d62019-09-12 07:05:33 -07005420 post records the block indices in post-order.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005421
5422 XXX must handle implicit jumps from one block to next
5423*/
5424
Thomas Wouters89f507f2006-12-13 04:49:30 +00005425struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005426 PyObject *a_bytecode; /* string containing bytecode */
5427 int a_offset; /* offset into bytecode */
5428 int a_nblocks; /* number of reachable blocks */
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005429 basicblock **a_reverse_postorder; /* list of blocks in dfs postorder */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005430 PyObject *a_lnotab; /* string containing lnotab */
5431 int a_lnotab_off; /* offset into lnotab */
5432 int a_lineno; /* last lineno of emitted instruction */
5433 int a_lineno_off; /* bytecode offset of last lineno */
Thomas Wouters89f507f2006-12-13 04:49:30 +00005434};
5435
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005436static void
T. Wouters99b54d62019-09-12 07:05:33 -07005437dfs(struct compiler *c, basicblock *b, struct assembler *a, int end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005438{
T. Wouters99b54d62019-09-12 07:05:33 -07005439
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005440 /* There is no real depth-first-search to do here because all the
5441 * blocks are emitted in topological order already, so we just need to
5442 * follow the b_next pointers and place them in a->a_reverse_postorder in
5443 * reverse order and make sure that the first one starts at 0. */
5444
5445 for (a->a_nblocks = 0; b != NULL; b = b->b_next) {
5446 a->a_reverse_postorder[a->a_nblocks++] = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005447 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005448}
5449
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005450Py_LOCAL_INLINE(void)
5451stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005452{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005453 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
Mark Shannonfee55262019-11-21 09:11:43 +00005454 if (b->b_startdepth < depth && b->b_startdepth < 100) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005455 assert(b->b_startdepth < 0);
5456 b->b_startdepth = depth;
5457 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02005458 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005459}
5460
5461/* Find the flow path that needs the largest stack. We assume that
5462 * cycles in the flow graph have no net effect on the stack depth.
5463 */
5464static int
5465stackdepth(struct compiler *c)
5466{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005467 basicblock *b, *entryblock = NULL;
5468 basicblock **stack, **sp;
5469 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005470 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005471 b->b_startdepth = INT_MIN;
5472 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005473 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005474 }
5475 if (!entryblock)
5476 return 0;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005477 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
5478 if (!stack) {
5479 PyErr_NoMemory();
5480 return -1;
5481 }
5482
5483 sp = stack;
5484 stackdepth_push(&sp, entryblock, 0);
5485 while (sp != stack) {
5486 b = *--sp;
5487 int depth = b->b_startdepth;
5488 assert(depth >= 0);
5489 basicblock *next = b->b_next;
5490 for (int i = 0; i < b->b_iused; i++) {
5491 struct instr *instr = &b->b_instr[i];
5492 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
5493 if (effect == PY_INVALID_STACK_EFFECT) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01005494 _Py_FatalErrorFormat(__func__,
5495 "opcode = %d", instr->i_opcode);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005496 }
5497 int new_depth = depth + effect;
5498 if (new_depth > maxdepth) {
5499 maxdepth = new_depth;
5500 }
5501 assert(depth >= 0); /* invalid code or bug in stackdepth() */
Mark Shannon582aaf12020-08-04 17:30:11 +01005502 if (is_jump(instr)) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005503 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
5504 assert(effect != PY_INVALID_STACK_EFFECT);
5505 int target_depth = depth + effect;
5506 if (target_depth > maxdepth) {
5507 maxdepth = target_depth;
5508 }
5509 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005510 stackdepth_push(&sp, instr->i_target, target_depth);
5511 }
5512 depth = new_depth;
5513 if (instr->i_opcode == JUMP_ABSOLUTE ||
5514 instr->i_opcode == JUMP_FORWARD ||
5515 instr->i_opcode == RETURN_VALUE ||
Mark Shannonfee55262019-11-21 09:11:43 +00005516 instr->i_opcode == RAISE_VARARGS ||
5517 instr->i_opcode == RERAISE)
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005518 {
5519 /* remaining code is dead */
5520 next = NULL;
5521 break;
5522 }
5523 }
5524 if (next != NULL) {
5525 stackdepth_push(&sp, next, depth);
5526 }
5527 }
5528 PyObject_Free(stack);
5529 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005530}
5531
5532static int
5533assemble_init(struct assembler *a, int nblocks, int firstlineno)
5534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005535 memset(a, 0, sizeof(struct assembler));
5536 a->a_lineno = firstlineno;
5537 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
5538 if (!a->a_bytecode)
5539 return 0;
5540 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
5541 if (!a->a_lnotab)
5542 return 0;
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07005543 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005544 PyErr_NoMemory();
5545 return 0;
5546 }
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005547 a->a_reverse_postorder = (basicblock **)PyObject_Malloc(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005548 sizeof(basicblock *) * nblocks);
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005549 if (!a->a_reverse_postorder) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005550 PyErr_NoMemory();
5551 return 0;
5552 }
5553 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005554}
5555
5556static void
5557assemble_free(struct assembler *a)
5558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005559 Py_XDECREF(a->a_bytecode);
5560 Py_XDECREF(a->a_lnotab);
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005561 if (a->a_reverse_postorder)
5562 PyObject_Free(a->a_reverse_postorder);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005563}
5564
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005565static int
5566blocksize(basicblock *b)
5567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005568 int i;
5569 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005571 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005572 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005573 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005574}
5575
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00005576/* Appends a pair to the end of the line number table, a_lnotab, representing
5577 the instruction's bytecode offset and line number. See
5578 Objects/lnotab_notes.txt for the description of the line number table. */
Tim Peters2a7f3842001-06-09 09:26:21 +00005579
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005580static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005581assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005583 int d_bytecode, d_lineno;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005584 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005585 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005587 d_lineno = i->i_lineno - a->a_lineno;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005588 if (d_lineno == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005589 return 1;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005590 }
5591
5592 d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT);
5593 assert(d_bytecode >= 0);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00005594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005595 if (d_bytecode > 255) {
5596 int j, nbytes, ncodes = d_bytecode / 255;
5597 nbytes = a->a_lnotab_off + 2 * ncodes;
5598 len = PyBytes_GET_SIZE(a->a_lnotab);
5599 if (nbytes >= len) {
5600 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
5601 len = nbytes;
5602 else if (len <= INT_MAX / 2)
5603 len *= 2;
5604 else {
5605 PyErr_NoMemory();
5606 return 0;
5607 }
5608 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
5609 return 0;
5610 }
5611 lnotab = (unsigned char *)
5612 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
5613 for (j = 0; j < ncodes; j++) {
5614 *lnotab++ = 255;
5615 *lnotab++ = 0;
5616 }
5617 d_bytecode -= ncodes * 255;
5618 a->a_lnotab_off += ncodes * 2;
5619 }
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005620 assert(0 <= d_bytecode && d_bytecode <= 255);
5621
5622 if (d_lineno < -128 || 127 < d_lineno) {
5623 int j, nbytes, ncodes, k;
5624 if (d_lineno < 0) {
5625 k = -128;
5626 /* use division on positive numbers */
5627 ncodes = (-d_lineno) / 128;
5628 }
5629 else {
5630 k = 127;
5631 ncodes = d_lineno / 127;
5632 }
5633 d_lineno -= ncodes * k;
5634 assert(ncodes >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005635 nbytes = a->a_lnotab_off + 2 * ncodes;
5636 len = PyBytes_GET_SIZE(a->a_lnotab);
5637 if (nbytes >= len) {
5638 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
5639 len = nbytes;
5640 else if (len <= INT_MAX / 2)
5641 len *= 2;
5642 else {
5643 PyErr_NoMemory();
5644 return 0;
5645 }
5646 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
5647 return 0;
5648 }
5649 lnotab = (unsigned char *)
5650 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
5651 *lnotab++ = d_bytecode;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005652 *lnotab++ = k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005653 d_bytecode = 0;
5654 for (j = 1; j < ncodes; j++) {
5655 *lnotab++ = 0;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005656 *lnotab++ = k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005657 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005658 a->a_lnotab_off += ncodes * 2;
5659 }
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005660 assert(-128 <= d_lineno && d_lineno <= 127);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005662 len = PyBytes_GET_SIZE(a->a_lnotab);
5663 if (a->a_lnotab_off + 2 >= len) {
5664 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
5665 return 0;
5666 }
5667 lnotab = (unsigned char *)
5668 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00005669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005670 a->a_lnotab_off += 2;
5671 if (d_bytecode) {
5672 *lnotab++ = d_bytecode;
Victor Stinner4f2dab52011-05-27 16:46:51 +02005673 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005674 }
5675 else { /* First line of a block; def stmt, etc. */
5676 *lnotab++ = 0;
Victor Stinner4f2dab52011-05-27 16:46:51 +02005677 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005678 }
5679 a->a_lineno = i->i_lineno;
5680 a->a_lineno_off = a->a_offset;
5681 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005682}
5683
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005684/* assemble_emit()
5685 Extend the bytecode with a new instruction.
5686 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00005687*/
5688
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00005689static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005690assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00005691{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005692 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005693 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03005694 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005695
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005696 arg = i->i_oparg;
5697 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005698 if (i->i_lineno && !assemble_lnotab(a, i))
5699 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005700 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005701 if (len > PY_SSIZE_T_MAX / 2)
5702 return 0;
5703 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
5704 return 0;
5705 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03005706 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005707 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005708 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005709 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00005710}
5711
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00005712static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005713assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00005714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005715 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005716 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005717 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00005718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005719 /* Compute the size of each block and fixup jump args.
5720 Replace block pointer with position in bytecode. */
5721 do {
5722 totsize = 0;
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005723 for (i = 0; i < a->a_nblocks; i++) {
5724 b = a->a_reverse_postorder[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005725 bsize = blocksize(b);
5726 b->b_offset = totsize;
5727 totsize += bsize;
5728 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005729 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005730 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
5731 bsize = b->b_offset;
5732 for (i = 0; i < b->b_iused; i++) {
5733 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005734 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005735 /* Relative jumps are computed relative to
5736 the instruction pointer after fetching
5737 the jump instruction.
5738 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005739 bsize += isize;
Mark Shannon582aaf12020-08-04 17:30:11 +01005740 if (is_jump(instr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005741 instr->i_oparg = instr->i_target->b_offset;
Mark Shannon582aaf12020-08-04 17:30:11 +01005742 if (is_relative_jump(instr)) {
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005743 instr->i_oparg -= bsize;
5744 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03005745 instr->i_oparg *= sizeof(_Py_CODEUNIT);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005746 if (instrsize(instr->i_oparg) != isize) {
5747 extended_arg_recompile = 1;
5748 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005749 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005750 }
5751 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00005752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005753 /* XXX: This is an awful hack that could hurt performance, but
5754 on the bright side it should work until we come up
5755 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00005756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005757 The issue is that in the first loop blocksize() is called
5758 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005759 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005760 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00005761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005762 So we loop until we stop seeing new EXTENDED_ARGs.
5763 The only EXTENDED_ARGs that could be popping up are
5764 ones in jump instructions. So this should converge
5765 fairly quickly.
5766 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005767 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005768}
5769
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005770static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01005771dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005772{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005773 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02005774 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005776 tuple = PyTuple_New(size);
5777 if (tuple == NULL)
5778 return NULL;
5779 while (PyDict_Next(dict, &pos, &k, &v)) {
5780 i = PyLong_AS_LONG(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005781 Py_INCREF(k);
5782 assert((i - offset) < size);
5783 assert((i - offset) >= 0);
5784 PyTuple_SET_ITEM(tuple, i - offset, k);
5785 }
5786 return tuple;
5787}
5788
5789static PyObject *
5790consts_dict_keys_inorder(PyObject *dict)
5791{
5792 PyObject *consts, *k, *v;
5793 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
5794
5795 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
5796 if (consts == NULL)
5797 return NULL;
5798 while (PyDict_Next(dict, &pos, &k, &v)) {
5799 i = PyLong_AS_LONG(v);
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +03005800 /* The keys of the dictionary can be tuples wrapping a contant.
5801 * (see compiler_add_o and _PyCode_ConstantKey). In that case
5802 * the object we want is always second. */
5803 if (PyTuple_CheckExact(k)) {
5804 k = PyTuple_GET_ITEM(k, 1);
5805 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005806 Py_INCREF(k);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005807 assert(i < size);
5808 assert(i >= 0);
5809 PyList_SET_ITEM(consts, i, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005810 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005811 return consts;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005812}
5813
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005814static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005815compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005816{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005817 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005818 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005819 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04005820 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005821 if (ste->ste_nested)
5822 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07005823 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005824 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07005825 if (!ste->ste_generator && ste->ste_coroutine)
5826 flags |= CO_COROUTINE;
5827 if (ste->ste_generator && ste->ste_coroutine)
5828 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005829 if (ste->ste_varargs)
5830 flags |= CO_VARARGS;
5831 if (ste->ste_varkeywords)
5832 flags |= CO_VARKEYWORDS;
5833 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00005834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005835 /* (Only) inherit compilerflags in PyCF_MASK */
5836 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00005837
Pablo Galindo90235812020-03-15 04:29:22 +00005838 if ((IS_TOP_LEVEL_AWAIT(c)) &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005839 ste->ste_coroutine &&
5840 !ste->ste_generator) {
5841 flags |= CO_COROUTINE;
5842 }
5843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005844 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005845}
5846
INADA Naokic2e16072018-11-26 21:23:22 +09005847// Merge *tuple* with constant cache.
5848// Unlike merge_consts_recursive(), this function doesn't work recursively.
5849static int
5850merge_const_tuple(struct compiler *c, PyObject **tuple)
5851{
5852 assert(PyTuple_CheckExact(*tuple));
5853
5854 PyObject *key = _PyCode_ConstantKey(*tuple);
5855 if (key == NULL) {
5856 return 0;
5857 }
5858
5859 // t is borrowed reference
5860 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
5861 Py_DECREF(key);
5862 if (t == NULL) {
5863 return 0;
5864 }
5865 if (t == key) { // tuple is new constant.
5866 return 1;
5867 }
5868
5869 PyObject *u = PyTuple_GET_ITEM(t, 1);
5870 Py_INCREF(u);
5871 Py_DECREF(*tuple);
5872 *tuple = u;
5873 return 1;
5874}
5875
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005876static PyCodeObject *
Mark Shannon6e8128f2020-07-30 10:03:00 +01005877makecode(struct compiler *c, struct assembler *a, PyObject *consts)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005878{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005879 PyCodeObject *co = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005880 PyObject *names = NULL;
5881 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005882 PyObject *name = NULL;
5883 PyObject *freevars = NULL;
5884 PyObject *cellvars = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005885 Py_ssize_t nlocals;
5886 int nlocals_int;
5887 int flags;
Pablo Galindocd74e662019-06-01 18:08:04 +01005888 int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005890 names = dict_keys_inorder(c->u->u_names, 0);
5891 varnames = dict_keys_inorder(c->u->u_varnames, 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01005892 if (!names || !varnames) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005893 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01005894 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005895 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
5896 if (!cellvars)
5897 goto error;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005898 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005899 if (!freevars)
5900 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005901
INADA Naokic2e16072018-11-26 21:23:22 +09005902 if (!merge_const_tuple(c, &names) ||
5903 !merge_const_tuple(c, &varnames) ||
5904 !merge_const_tuple(c, &cellvars) ||
5905 !merge_const_tuple(c, &freevars))
5906 {
5907 goto error;
5908 }
5909
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02005910 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01005911 assert(nlocals < INT_MAX);
5912 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
5913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005914 flags = compute_code_flags(c);
5915 if (flags < 0)
5916 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005917
Mark Shannon6e8128f2020-07-30 10:03:00 +01005918 consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
5919 if (consts == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005920 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01005921 }
INADA Naokic2e16072018-11-26 21:23:22 +09005922 if (!merge_const_tuple(c, &consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01005923 Py_DECREF(consts);
INADA Naokic2e16072018-11-26 21:23:22 +09005924 goto error;
5925 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005926
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01005927 posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int);
Pablo Galindocd74e662019-06-01 18:08:04 +01005928 posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +01005929 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005930 maxdepth = stackdepth(c);
5931 if (maxdepth < 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01005932 Py_DECREF(consts);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005933 goto error;
5934 }
Pablo Galindo4a2edc32019-07-01 11:35:05 +01005935 co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
Mark Shannon13bc1392020-01-23 09:25:17 +00005936 posonlyargcount, kwonlyargcount, nlocals_int,
Mark Shannon6e8128f2020-07-30 10:03:00 +01005937 maxdepth, flags, a->a_bytecode, consts, names,
Pablo Galindo4a2edc32019-07-01 11:35:05 +01005938 varnames, freevars, cellvars, c->c_filename,
5939 c->u->u_name, c->u->u_firstlineno, a->a_lnotab);
Mark Shannon6e8128f2020-07-30 10:03:00 +01005940 Py_DECREF(consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005941 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005942 Py_XDECREF(names);
5943 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005944 Py_XDECREF(name);
5945 Py_XDECREF(freevars);
5946 Py_XDECREF(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005947 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005948}
5949
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005950
5951/* For debugging purposes only */
5952#if 0
5953static void
5954dump_instr(const struct instr *i)
5955{
Mark Shannon582aaf12020-08-04 17:30:11 +01005956 const char *jrel = (is_relative_jump(instr)) ? "jrel " : "";
5957 const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : "";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005958 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005960 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005961 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005962 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005963 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005964 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
5965 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005966}
5967
5968static void
5969dump_basicblock(const basicblock *b)
5970{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005971 const char *b_return = b->b_return ? "return " : "";
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005972 fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n",
5973 b->b_iused, b->b_startdepth, b->b_offset, b_return);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005974 if (b->b_instr) {
5975 int i;
5976 for (i = 0; i < b->b_iused; i++) {
5977 fprintf(stderr, " [%02d] ", i);
5978 dump_instr(b->b_instr + i);
5979 }
5980 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005981}
5982#endif
5983
Mark Shannon6e8128f2020-07-30 10:03:00 +01005984static int
5985optimize_cfg(struct assembler *a, PyObject *consts);
5986
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005987static PyCodeObject *
5988assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005990 basicblock *b, *entryblock;
5991 struct assembler a;
5992 int i, j, nblocks;
5993 PyCodeObject *co = NULL;
Mark Shannon6e8128f2020-07-30 10:03:00 +01005994 PyObject *consts = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005996 /* Make sure every block that falls off the end returns None.
5997 XXX NEXT_BLOCK() isn't quite right, because if the last
5998 block ends with a jump or return b_next shouldn't set.
5999 */
6000 if (!c->u->u_curblock->b_return) {
6001 NEXT_BLOCK(c);
6002 if (addNone)
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006003 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006004 ADDOP(c, RETURN_VALUE);
6005 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006007 nblocks = 0;
6008 entryblock = NULL;
6009 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6010 nblocks++;
6011 entryblock = b;
6012 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006014 /* Set firstlineno if it wasn't explicitly set. */
6015 if (!c->u->u_firstlineno) {
Ned Deilydc35cda2016-08-17 17:18:33 -04006016 if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006017 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
6018 else
6019 c->u->u_firstlineno = 1;
6020 }
6021 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
6022 goto error;
T. Wouters99b54d62019-09-12 07:05:33 -07006023 dfs(c, entryblock, &a, nblocks);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006024
Mark Shannon6e8128f2020-07-30 10:03:00 +01006025 consts = consts_dict_keys_inorder(c->u->u_consts);
6026 if (consts == NULL) {
6027 goto error;
6028 }
6029 if (optimize_cfg(&a, consts)) {
6030 goto error;
6031 }
6032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006033 /* Can't modify the bytecode after computing jump offsets. */
6034 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00006035
T. Wouters99b54d62019-09-12 07:05:33 -07006036 /* Emit code in reverse postorder from dfs. */
Pablo Galindo60eb9f12020-06-28 01:55:47 +01006037 for (i = 0; i < a.a_nblocks; i++) {
6038 b = a.a_reverse_postorder[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006039 for (j = 0; j < b->b_iused; j++)
6040 if (!assemble_emit(&a, &b->b_instr[j]))
6041 goto error;
6042 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00006043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006044 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
6045 goto error;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006046 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006047 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006048
Mark Shannon6e8128f2020-07-30 10:03:00 +01006049 co = makecode(c, &a, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006050 error:
Mark Shannon6e8128f2020-07-30 10:03:00 +01006051 Py_XDECREF(consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006052 assemble_free(&a);
6053 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006054}
Georg Brandl8334fd92010-12-04 10:26:46 +00006055
6056#undef PyAST_Compile
Benjamin Petersone5024512018-09-12 12:06:42 -07006057PyCodeObject *
Georg Brandl8334fd92010-12-04 10:26:46 +00006058PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
6059 PyArena *arena)
6060{
6061 return PyAST_CompileEx(mod, filename, flags, -1, arena);
6062}
Mark Shannon6e8128f2020-07-30 10:03:00 +01006063
6064
6065/* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
6066 with LOAD_CONST (c1, c2, ... cn).
6067 The consts table must still be in list form so that the
6068 new constant (c1, c2, ... cn) can be appended.
6069 Called with codestr pointing to the first LOAD_CONST.
6070*/
6071static int
6072fold_tuple_on_constants(struct instr *inst,
6073 int n, PyObject *consts)
6074{
6075 /* Pre-conditions */
6076 assert(PyList_CheckExact(consts));
6077 assert(inst[n].i_opcode == BUILD_TUPLE);
6078 assert(inst[n].i_oparg == n);
6079
6080 for (int i = 0; i < n; i++) {
6081 if (inst[i].i_opcode != LOAD_CONST) {
6082 return 0;
6083 }
6084 }
6085
6086 /* Buildup new tuple of constants */
6087 PyObject *newconst = PyTuple_New(n);
6088 if (newconst == NULL) {
6089 return -1;
6090 }
6091 for (int i = 0; i < n; i++) {
6092 int arg = inst[i].i_oparg;
6093 PyObject *constant = PyList_GET_ITEM(consts, arg);
6094 Py_INCREF(constant);
6095 PyTuple_SET_ITEM(newconst, i, constant);
6096 }
6097 Py_ssize_t index = PyList_GET_SIZE(consts);
Victor Stinner71f2ff42020-09-23 14:06:55 +02006098 if ((size_t)index >= (size_t)INT_MAX - 1) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006099 Py_DECREF(newconst);
6100 PyErr_SetString(PyExc_OverflowError, "too many constants");
6101 return -1;
6102 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006103 if (PyList_Append(consts, newconst)) {
6104 Py_DECREF(newconst);
6105 return -1;
6106 }
6107 Py_DECREF(newconst);
6108 for (int i = 0; i < n; i++) {
6109 inst[i].i_opcode = NOP;
6110 }
6111 inst[n].i_opcode = LOAD_CONST;
Victor Stinner71f2ff42020-09-23 14:06:55 +02006112 inst[n].i_oparg = (int)index;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006113 return 0;
6114}
6115
6116
6117/* Optimization */
6118static int
6119optimize_basic_block(basicblock *bb, PyObject *consts)
6120{
6121 assert(PyList_CheckExact(consts));
6122 struct instr nop;
6123 nop.i_opcode = NOP;
6124 struct instr *target;
6125 int lineno;
6126 for (int i = 0; i < bb->b_iused; i++) {
6127 struct instr *inst = &bb->b_instr[i];
6128 int oparg = inst->i_oparg;
6129 int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01006130 if (is_jump(inst)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006131 /* Skip over empty basic blocks. */
6132 while (inst->i_target->b_iused == 0) {
6133 inst->i_target = inst->i_target->b_next;
6134 }
6135 target = &inst->i_target->b_instr[0];
6136 }
6137 else {
6138 target = &nop;
6139 }
6140 switch (inst->i_opcode) {
6141 /* Skip over LOAD_CONST trueconst
6142 POP_JUMP_IF_FALSE xx. This improves
6143 "while 1" performance. */
6144 case LOAD_CONST:
6145 if (nextop != POP_JUMP_IF_FALSE) {
6146 break;
6147 }
6148 PyObject* cnt = PyList_GET_ITEM(consts, oparg);
6149 int is_true = PyObject_IsTrue(cnt);
6150 if (is_true == -1) {
6151 goto error;
6152 }
6153 if (is_true == 1) {
6154 inst->i_opcode = NOP;
6155 bb->b_instr[i+1].i_opcode = NOP;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006156 }
6157 break;
6158
6159 /* Try to fold tuples of constants.
6160 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
6161 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
6162 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
6163 case BUILD_TUPLE:
6164 if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
6165 switch(oparg) {
6166 case 1:
6167 inst->i_opcode = NOP;
6168 bb->b_instr[i+1].i_opcode = NOP;
6169 break;
6170 case 2:
6171 inst->i_opcode = ROT_TWO;
6172 bb->b_instr[i+1].i_opcode = NOP;
6173 break;
6174 case 3:
6175 inst->i_opcode = ROT_THREE;
6176 bb->b_instr[i+1].i_opcode = ROT_TWO;
6177 }
6178 break;
6179 }
6180 if (i >= oparg) {
6181 if (fold_tuple_on_constants(inst-oparg, oparg, consts)) {
6182 goto error;
6183 }
6184 }
6185 break;
6186
6187 /* Simplify conditional jump to conditional jump where the
6188 result of the first test implies the success of a similar
6189 test or the failure of the opposite test.
6190 Arises in code like:
6191 "a and b or c"
6192 "(a and b) and c"
6193 "(a or b) or c"
6194 "(a or b) and c"
6195 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
6196 --> x:JUMP_IF_FALSE_OR_POP z
6197 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
6198 --> x:POP_JUMP_IF_FALSE y+1
6199 where y+1 is the instruction following the second test.
6200 */
6201 case JUMP_IF_FALSE_OR_POP:
6202 switch(target->i_opcode) {
6203 case POP_JUMP_IF_FALSE:
6204 *inst = *target;
6205 break;
6206 case JUMP_ABSOLUTE:
6207 case JUMP_FORWARD:
6208 case JUMP_IF_FALSE_OR_POP:
6209 inst->i_target = target->i_target;
6210 break;
6211 case JUMP_IF_TRUE_OR_POP:
6212 assert (inst->i_target->b_iused == 1);
6213 inst->i_opcode = POP_JUMP_IF_FALSE;
6214 inst->i_target = inst->i_target->b_next;
6215 break;
6216 }
6217 break;
6218
6219 case JUMP_IF_TRUE_OR_POP:
6220 switch(target->i_opcode) {
6221 case POP_JUMP_IF_TRUE:
6222 *inst = *target;
6223 break;
6224 case JUMP_ABSOLUTE:
6225 case JUMP_FORWARD:
6226 case JUMP_IF_TRUE_OR_POP:
6227 inst->i_target = target->i_target;
6228 break;
6229 case JUMP_IF_FALSE_OR_POP:
6230 assert (inst->i_target->b_iused == 1);
6231 inst->i_opcode = POP_JUMP_IF_TRUE;
6232 inst->i_target = inst->i_target->b_next;
6233 break;
6234 }
6235 break;
6236
6237 case POP_JUMP_IF_FALSE:
6238 switch(target->i_opcode) {
6239 case JUMP_ABSOLUTE:
6240 case JUMP_FORWARD:
6241 inst->i_target = target->i_target;
6242 break;
6243 }
6244 break;
6245
6246 case POP_JUMP_IF_TRUE:
6247 switch(target->i_opcode) {
6248 case JUMP_ABSOLUTE:
6249 case JUMP_FORWARD:
6250 inst->i_target = target->i_target;
6251 break;
6252 }
6253 break;
6254
6255 case JUMP_ABSOLUTE:
6256 case JUMP_FORWARD:
6257 switch(target->i_opcode) {
6258 case JUMP_FORWARD:
6259 inst->i_target = target->i_target;
6260 break;
6261 case JUMP_ABSOLUTE:
6262 case RETURN_VALUE:
6263 case RERAISE:
6264 case RAISE_VARARGS:
6265 lineno = inst->i_lineno;
6266 *inst = *target;
6267 inst->i_lineno = lineno;
6268 break;
6269 }
6270 break;
6271 }
6272 }
6273 return 0;
6274error:
6275 return -1;
6276}
6277
6278
6279static void
6280clean_basic_block(basicblock *bb) {
6281 /* Remove NOPs and any code following a return or re-raise. */
6282 int dest = 0;
6283 for (int src = 0; src < bb->b_iused; src++) {
6284 switch(bb->b_instr[src].i_opcode) {
6285 case NOP:
6286 /* skip */
6287 break;
6288 case RETURN_VALUE:
6289 case RERAISE:
6290 bb->b_next = NULL;
6291 bb->b_instr[dest] = bb->b_instr[src];
6292 dest++;
6293 goto end;
6294 default:
6295 if (dest != src) {
6296 bb->b_instr[dest] = bb->b_instr[src];
6297 }
6298 dest++;
6299 break;
6300 }
6301 }
6302end:
6303 assert(dest <= bb->b_iused);
6304 bb->b_iused = dest;
6305}
6306
6307static int
6308mark_reachable(struct assembler *a) {
6309 basicblock **stack, **sp;
6310 sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks);
6311 if (stack == NULL) {
6312 return -1;
6313 }
6314 basicblock *entry = a->a_reverse_postorder[0];
6315 entry->b_reachable = 1;
6316 *sp++ = entry;
6317 while (sp > stack) {
6318 basicblock *b = *(--sp);
6319 if (b->b_next && b->b_next->b_reachable == 0) {
6320 b->b_next->b_reachable = 1;
6321 *sp++ = b->b_next;
6322 }
6323 for (int i = 0; i < b->b_iused; i++) {
6324 basicblock *target;
Mark Shannon582aaf12020-08-04 17:30:11 +01006325 if (is_jump(&b->b_instr[i])) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006326 target = b->b_instr[i].i_target;
6327 if (target->b_reachable == 0) {
6328 target->b_reachable = 1;
6329 *sp++ = target;
6330 }
6331 }
6332 }
6333 }
6334 PyObject_Free(stack);
6335 return 0;
6336}
6337
6338
6339/* Perform basic peephole optimizations on a control flow graph.
6340 The consts object should still be in list form to allow new constants
6341 to be appended.
6342
6343 All transformations keep the code size the same or smaller.
6344 For those that reduce size, the gaps are initially filled with
6345 NOPs. Later those NOPs are removed.
6346*/
6347
6348static int
6349optimize_cfg(struct assembler *a, PyObject *consts)
6350{
6351 for (int i = 0; i < a->a_nblocks; i++) {
6352 if (optimize_basic_block(a->a_reverse_postorder[i], consts)) {
6353 return -1;
6354 }
6355 clean_basic_block(a->a_reverse_postorder[i]);
6356 assert(a->a_reverse_postorder[i]->b_reachable == 0);
6357 }
6358 if (mark_reachable(a)) {
6359 return -1;
6360 }
6361 /* Delete unreachable instructions */
6362 for (int i = 0; i < a->a_nblocks; i++) {
6363 if (a->a_reverse_postorder[i]->b_reachable == 0) {
6364 a->a_reverse_postorder[i]->b_iused = 0;
6365 }
6366 }
6367 return 0;
6368}
6369
6370/* Retained for API compatibility.
6371 * Optimization is now done in optimize_cfg */
6372
6373PyObject *
6374PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
6375 PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
6376{
6377 Py_INCREF(code);
6378 return code;
6379}
6380