blob: a0089b2d6dc18644781a6057ee418705c7be7b76 [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"
Guido van Rossum3f5da241990-12-20 15:06:42 +000025
Ammar Askare92d3932020-01-15 11:48:40 -050026#include "Python-ast.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000027#include "ast.h"
28#include "code.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000029#include "symtable.h"
Mark Shannon582aaf12020-08-04 17:30:11 +010030#define NEED_OPCODE_JUMP_TABLES
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031#include "opcode.h"
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030032#include "wordcode_helpers.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000033
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000034#define DEFAULT_BLOCK_SIZE 16
35#define DEFAULT_BLOCKS 8
36#define DEFAULT_CODE_SIZE 128
37#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000038
Nick Coghlan650f0d02007-04-15 12:05:43 +000039#define COMP_GENEXP 0
40#define COMP_LISTCOMP 1
41#define COMP_SETCOMP 2
Guido van Rossum992d4a32007-07-11 13:09:30 +000042#define COMP_DICTCOMP 3
Nick Coghlan650f0d02007-04-15 12:05:43 +000043
Pablo Galindo90235812020-03-15 04:29:22 +000044#define IS_TOP_LEVEL_AWAIT(c) ( \
45 (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \
46 && (c->u->u_ste->ste_type == ModuleBlock))
47
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000048struct instr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 unsigned char i_opcode;
50 int i_oparg;
51 struct basicblock_ *i_target; /* target block (if jump instruction) */
52 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000053};
54
Mark Shannon582aaf12020-08-04 17:30:11 +010055#define LOG_BITS_PER_INT 5
56#define MASK_LOW_LOG_BITS 31
57
58static inline int
59is_bit_set_in_table(uint32_t *table, int bitindex) {
60 /* Is the relevant bit set in the relevant word? */
61 /* 256 bits fit into 8 32-bits words.
62 * Word is indexed by (bitindex>>ln(size of int in bits)).
63 * Bit within word is the low bits of bitindex.
64 */
65 uint32_t word = table[bitindex >> LOG_BITS_PER_INT];
66 return (word >> (bitindex & MASK_LOW_LOG_BITS)) & 1;
67}
68
69static inline int
70is_relative_jump(struct instr *i)
71{
72 return is_bit_set_in_table(_PyOpcode_RelativeJump, i->i_opcode);
73}
74
75static inline int
76is_jump(struct instr *i)
77{
78 return is_bit_set_in_table(_PyOpcode_Jump, i->i_opcode);
79}
80
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000081typedef struct basicblock_ {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000082 /* Each basicblock in a compilation unit is linked via b_list in the
83 reverse order that the block are allocated. b_list points to the next
84 block, not to be confused with b_next, which is next by control flow. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 struct basicblock_ *b_list;
86 /* number of instructions used */
87 int b_iused;
88 /* length of instruction array (b_instr) */
89 int b_ialloc;
90 /* pointer to an array of instructions, initially NULL */
91 struct instr *b_instr;
92 /* If b_next is non-NULL, it is a pointer to the next
93 block reached by normal control flow. */
94 struct basicblock_ *b_next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 /* b_return is true if a RETURN_VALUE opcode is inserted. */
96 unsigned b_return : 1;
Mark Shannon6e8128f2020-07-30 10:03:00 +010097 unsigned b_reachable : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 /* depth of stack upon entry of block, computed by stackdepth() */
99 int b_startdepth;
100 /* instruction offset for block, computed by assemble_jump_offsets() */
101 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000102} basicblock;
103
104/* fblockinfo tracks the current frame block.
105
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000106A frame block is used to handle loops, try/except, and try/finally.
107It's called a frame block to distinguish it from a basic block in the
108compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000109*/
110
Mark Shannon02d126a2020-09-25 14:04:19 +0100111enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END,
112 WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER };
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000113
114struct fblockinfo {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 enum fblocktype fb_type;
116 basicblock *fb_block;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200117 /* (optional) type-specific exit or cleanup block */
118 basicblock *fb_exit;
Mark Shannonfee55262019-11-21 09:11:43 +0000119 /* (optional) additional information required for unwinding */
120 void *fb_datum;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000121};
122
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100123enum {
124 COMPILER_SCOPE_MODULE,
125 COMPILER_SCOPE_CLASS,
126 COMPILER_SCOPE_FUNCTION,
Yury Selivanov75445082015-05-11 22:57:16 -0400127 COMPILER_SCOPE_ASYNC_FUNCTION,
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400128 COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100129 COMPILER_SCOPE_COMPREHENSION,
130};
131
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000132/* The following items change on entry and exit of code blocks.
133 They must be saved and restored when returning to a block.
134*/
135struct compiler_unit {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 PySTEntryObject *u_ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 PyObject *u_name;
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400139 PyObject *u_qualname; /* dot-separated qualified name (lazy) */
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100140 int u_scope_type;
141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 /* The following fields are dicts that map objects to
143 the index of them in co_XXX. The index is used as
144 the argument for opcodes that refer to those collections.
145 */
146 PyObject *u_consts; /* all constants */
147 PyObject *u_names; /* all names */
148 PyObject *u_varnames; /* local variables */
149 PyObject *u_cellvars; /* cell variables */
150 PyObject *u_freevars; /* free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 PyObject *u_private; /* for private name mangling */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000153
Victor Stinnerf8e32212013-11-19 23:56:34 +0100154 Py_ssize_t u_argcount; /* number of arguments for block */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100155 Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */
Victor Stinnerf8e32212013-11-19 23:56:34 +0100156 Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 /* Pointer to the most recently allocated block. By following b_list
158 members, you can reach all early allocated blocks. */
159 basicblock *u_blocks;
160 basicblock *u_curblock; /* pointer to current block */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 int u_nfblocks;
163 struct fblockinfo u_fblock[CO_MAXBLOCKS];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 int u_firstlineno; /* the first lineno of the block */
166 int u_lineno; /* the lineno for the current stmt */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000167 int u_col_offset; /* the offset of the current stmt */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000168};
169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170/* This struct captures the global state of a compilation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000171
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000172The u pointer points to the current compilation unit, while units
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173for enclosing blocks are stored in c_stack. The u and c_stack are
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000174managed by compiler_enter_scope() and compiler_exit_scope().
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000175
176Note that we don't track recursion levels during compilation - the
177task of detecting and rejecting excessive levels of nesting is
178handled by the symbol analysis pass.
179
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000180*/
181
182struct compiler {
Victor Stinner14e461d2013-08-26 22:28:21 +0200183 PyObject *c_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 struct symtable *c_st;
185 PyFutureFeatures *c_future; /* pointer to module's __future__ */
186 PyCompilerFlags *c_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187
Georg Brandl8334fd92010-12-04 10:26:46 +0000188 int c_optimize; /* optimization level */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 int c_interactive; /* true if in interactive mode */
190 int c_nestlevel;
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100191 int c_do_not_emit_bytecode; /* The compiler won't emit any bytecode
192 if this value is different from zero.
193 This can be used to temporarily visit
194 nodes without emitting bytecode to
195 check only errors. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196
INADA Naokic2e16072018-11-26 21:23:22 +0900197 PyObject *c_const_cache; /* Python dict holding all constants,
198 including names tuple */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 struct compiler_unit *u; /* compiler state for current block */
200 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
201 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000202};
203
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100204static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205static void compiler_free(struct compiler *);
206static basicblock *compiler_new_block(struct compiler *);
Andy Lester76d58772020-03-10 21:18:12 -0500207static int compiler_next_instr(basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000208static int compiler_addop(struct compiler *, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +0100209static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
Mark Shannon582aaf12020-08-04 17:30:11 +0100210static int compiler_addop_j(struct compiler *, int, basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000211static int compiler_error(struct compiler *, const char *);
Serhiy Storchaka62e44812019-02-16 08:12:19 +0200212static int compiler_warn(struct compiler *, const char *, ...);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000213static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
214
215static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
216static int compiler_visit_stmt(struct compiler *, stmt_ty);
217static int compiler_visit_keyword(struct compiler *, keyword_ty);
218static int compiler_visit_expr(struct compiler *, expr_ty);
219static int compiler_augassign(struct compiler *, stmt_ty);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700220static int compiler_annassign(struct compiler *, stmt_ty);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +0200221static int compiler_subscript(struct compiler *, expr_ty);
222static int compiler_slice(struct compiler *, expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000223
Andy Lester76d58772020-03-10 21:18:12 -0500224static int inplace_binop(operator_ty);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100225static int are_all_items_const(asdl_expr_seq *, Py_ssize_t, Py_ssize_t);
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +0200226static int expr_constant(expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000227
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500228static int compiler_with(struct compiler *, stmt_ty, int);
Yury Selivanov75445082015-05-11 22:57:16 -0400229static int compiler_async_with(struct compiler *, stmt_ty, int);
230static int compiler_async_for(struct compiler *, stmt_ty);
Victor Stinner976bb402016-03-23 11:36:19 +0100231static int compiler_call_helper(struct compiler *c, int n,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100232 asdl_expr_seq *args,
233 asdl_keyword_seq *keywords);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500234static int compiler_try_except(struct compiler *, stmt_ty);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400235static int compiler_set_qualname(struct compiler *);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000236
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700237static int compiler_sync_comprehension_generator(
238 struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100239 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +0200240 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700241 expr_ty elt, expr_ty val, int type);
242
243static int compiler_async_comprehension_generator(
244 struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100245 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +0200246 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700247 expr_ty elt, expr_ty val, int type);
248
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000249static PyCodeObject *assemble(struct compiler *, int addNone);
Mark Shannon332cd5e2018-01-30 00:41:04 +0000250static PyObject *__doc__, *__annotations__;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000251
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400252#define CAPSULE_NAME "compile.c compiler unit"
Benjamin Petersonb173f782009-05-05 22:31:58 +0000253
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000254PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000255_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 /* Name mangling: __private becomes _classname__private.
258 This is independent from how the name is used. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200259 PyObject *result;
260 size_t nlen, plen, ipriv;
261 Py_UCS4 maxchar;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200263 PyUnicode_READ_CHAR(ident, 0) != '_' ||
264 PyUnicode_READ_CHAR(ident, 1) != '_') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 Py_INCREF(ident);
266 return ident;
267 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200268 nlen = PyUnicode_GET_LENGTH(ident);
269 plen = PyUnicode_GET_LENGTH(privateobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 /* Don't mangle __id__ or names with dots.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 The only time a name with a dot can occur is when
273 we are compiling an import statement that has a
274 package name.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 TODO(jhylton): Decide whether we want to support
277 mangling of the module name, e.g. __M.X.
278 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200279 if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
280 PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
281 PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 Py_INCREF(ident);
283 return ident; /* Don't mangle __whatever__ */
284 }
285 /* Strip leading underscores from class name */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200286 ipriv = 0;
287 while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
288 ipriv++;
289 if (ipriv == plen) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 Py_INCREF(ident);
291 return ident; /* Don't mangle if class is just underscores */
292 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200293 plen -= ipriv;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000294
Antoine Pitrou55bff892013-04-06 21:21:04 +0200295 if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
296 PyErr_SetString(PyExc_OverflowError,
297 "private identifier too large to be mangled");
298 return NULL;
299 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000300
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200301 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
302 if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
303 maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
304
305 result = PyUnicode_New(1 + nlen + plen, maxchar);
306 if (!result)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200308 /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
309 PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
Victor Stinner6c7a52a2011-09-28 21:39:17 +0200310 if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
311 Py_DECREF(result);
312 return NULL;
313 }
314 if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
315 Py_DECREF(result);
316 return NULL;
317 }
Victor Stinner8f825062012-04-27 13:55:39 +0200318 assert(_PyUnicode_CheckConsistency(result, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200319 return result;
Michael W. Hudson60934622004-08-12 17:56:29 +0000320}
321
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000322static int
323compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000326
INADA Naokic2e16072018-11-26 21:23:22 +0900327 c->c_const_cache = PyDict_New();
328 if (!c->c_const_cache) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 return 0;
INADA Naokic2e16072018-11-26 21:23:22 +0900330 }
331
332 c->c_stack = PyList_New(0);
333 if (!c->c_stack) {
334 Py_CLEAR(c->c_const_cache);
335 return 0;
336 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000339}
340
341PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200342PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
343 int optimize, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000344{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 struct compiler c;
346 PyCodeObject *co = NULL;
Victor Stinner37d66d72019-06-13 02:16:41 +0200347 PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 if (!__doc__) {
351 __doc__ = PyUnicode_InternFromString("__doc__");
352 if (!__doc__)
353 return NULL;
354 }
Mark Shannon332cd5e2018-01-30 00:41:04 +0000355 if (!__annotations__) {
356 __annotations__ = PyUnicode_InternFromString("__annotations__");
357 if (!__annotations__)
358 return NULL;
359 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 if (!compiler_init(&c))
361 return NULL;
Victor Stinner14e461d2013-08-26 22:28:21 +0200362 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 c.c_filename = filename;
364 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200365 c.c_future = PyFuture_FromASTObject(mod, filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 if (c.c_future == NULL)
367 goto finally;
368 if (!flags) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 flags = &local_flags;
370 }
371 merged = c.c_future->ff_features | flags->cf_flags;
372 c.c_future->ff_features = merged;
373 flags->cf_flags = merged;
374 c.c_flags = flags;
Victor Stinnerda7933e2020-04-13 03:04:28 +0200375 c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 c.c_nestlevel = 0;
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100377 c.c_do_not_emit_bytecode = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000378
Pablo Galindod112c602020-03-18 23:02:09 +0000379 _PyASTOptimizeState state;
380 state.optimize = c.c_optimize;
381 state.ff_features = merged;
382
383 if (!_PyAST_Optimize(mod, arena, &state)) {
INADA Naoki7ea143a2017-12-14 16:47:20 +0900384 goto finally;
385 }
386
Victor Stinner14e461d2013-08-26 22:28:21 +0200387 c.c_st = PySymtable_BuildObject(mod, filename, c.c_future);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 if (c.c_st == NULL) {
389 if (!PyErr_Occurred())
390 PyErr_SetString(PyExc_SystemError, "no symtable");
391 goto finally;
392 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000395
Thomas Wouters1175c432006-02-27 22:49:54 +0000396 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 compiler_free(&c);
398 assert(co || PyErr_Occurred());
399 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000400}
401
402PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200403PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags,
404 int optimize, PyArena *arena)
405{
406 PyObject *filename;
407 PyCodeObject *co;
408 filename = PyUnicode_DecodeFSDefault(filename_str);
409 if (filename == NULL)
410 return NULL;
411 co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
412 Py_DECREF(filename);
413 return co;
414
415}
416
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000417static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000418compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 if (c->c_st)
421 PySymtable_Free(c->c_st);
422 if (c->c_future)
423 PyObject_Free(c->c_future);
Victor Stinner14e461d2013-08-26 22:28:21 +0200424 Py_XDECREF(c->c_filename);
INADA Naokic2e16072018-11-26 21:23:22 +0900425 Py_DECREF(c->c_const_cache);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000427}
428
Guido van Rossum79f25d91997-04-29 20:08:16 +0000429static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000430list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 Py_ssize_t i, n;
433 PyObject *v, *k;
434 PyObject *dict = PyDict_New();
435 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 n = PyList_Size(list);
438 for (i = 0; i < n; i++) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100439 v = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 if (!v) {
441 Py_DECREF(dict);
442 return NULL;
443 }
444 k = PyList_GET_ITEM(list, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300445 if (PyDict_SetItem(dict, k, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 Py_DECREF(v);
447 Py_DECREF(dict);
448 return NULL;
449 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 Py_DECREF(v);
451 }
452 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000453}
454
455/* Return new dict containing names from src that match scope(s).
456
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000457src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000459values are integers, starting at offset and increasing by one for
460each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000461*/
462
463static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +0100464dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000465{
Benjamin Peterson51ab2832012-07-18 15:12:47 -0700466 Py_ssize_t i = offset, scope, num_keys, key_i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 PyObject *k, *v, *dest = PyDict_New();
Meador Inge2ca63152012-07-18 14:20:11 -0500468 PyObject *sorted_keys;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 assert(offset >= 0);
471 if (dest == NULL)
472 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000473
Meador Inge2ca63152012-07-18 14:20:11 -0500474 /* Sort the keys so that we have a deterministic order on the indexes
475 saved in the returned dictionary. These indexes are used as indexes
476 into the free and cell var storage. Therefore if they aren't
477 deterministic, then the generated bytecode is not deterministic.
478 */
479 sorted_keys = PyDict_Keys(src);
480 if (sorted_keys == NULL)
481 return NULL;
482 if (PyList_Sort(sorted_keys) != 0) {
483 Py_DECREF(sorted_keys);
484 return NULL;
485 }
Meador Ingef69e24e2012-07-18 16:41:03 -0500486 num_keys = PyList_GET_SIZE(sorted_keys);
Meador Inge2ca63152012-07-18 14:20:11 -0500487
488 for (key_i = 0; key_i < num_keys; key_i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 /* XXX this should probably be a macro in symtable.h */
490 long vi;
Meador Inge2ca63152012-07-18 14:20:11 -0500491 k = PyList_GET_ITEM(sorted_keys, key_i);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200492 v = PyDict_GetItemWithError(src, k);
493 assert(v && PyLong_Check(v));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 vi = PyLong_AS_LONG(v);
495 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 if (scope == scope_type || vi & flag) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300498 PyObject *item = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 if (item == NULL) {
Meador Inge2ca63152012-07-18 14:20:11 -0500500 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 Py_DECREF(dest);
502 return NULL;
503 }
504 i++;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300505 if (PyDict_SetItem(dest, k, item) < 0) {
Meador Inge2ca63152012-07-18 14:20:11 -0500506 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 Py_DECREF(item);
508 Py_DECREF(dest);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 return NULL;
510 }
511 Py_DECREF(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 }
513 }
Meador Inge2ca63152012-07-18 14:20:11 -0500514 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000516}
517
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000518static void
519compiler_unit_check(struct compiler_unit *u)
520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 basicblock *block;
522 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Benjamin Petersonca470632016-09-06 13:47:26 -0700523 assert((uintptr_t)block != 0xcbcbcbcbU);
524 assert((uintptr_t)block != 0xfbfbfbfbU);
525 assert((uintptr_t)block != 0xdbdbdbdbU);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 if (block->b_instr != NULL) {
527 assert(block->b_ialloc > 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +0100528 assert(block->b_iused >= 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 assert(block->b_ialloc >= block->b_iused);
530 }
531 else {
532 assert (block->b_iused == 0);
533 assert (block->b_ialloc == 0);
534 }
535 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000536}
537
538static void
539compiler_unit_free(struct compiler_unit *u)
540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 compiler_unit_check(u);
544 b = u->u_blocks;
545 while (b != NULL) {
546 if (b->b_instr)
547 PyObject_Free((void *)b->b_instr);
548 next = b->b_list;
549 PyObject_Free((void *)b);
550 b = next;
551 }
552 Py_CLEAR(u->u_ste);
553 Py_CLEAR(u->u_name);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400554 Py_CLEAR(u->u_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 Py_CLEAR(u->u_consts);
556 Py_CLEAR(u->u_names);
557 Py_CLEAR(u->u_varnames);
558 Py_CLEAR(u->u_freevars);
559 Py_CLEAR(u->u_cellvars);
560 Py_CLEAR(u->u_private);
561 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000562}
563
564static int
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100565compiler_enter_scope(struct compiler *c, identifier name,
566 int scope_type, void *key, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 struct compiler_unit *u;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100569 basicblock *block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570
Andy Lester7668a8b2020-03-24 23:26:44 -0500571 u = (struct compiler_unit *)PyObject_Calloc(1, sizeof(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 struct compiler_unit));
573 if (!u) {
574 PyErr_NoMemory();
575 return 0;
576 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100577 u->u_scope_type = scope_type;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 u->u_argcount = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100579 u->u_posonlyargcount = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 u->u_kwonlyargcount = 0;
581 u->u_ste = PySymtable_Lookup(c->c_st, key);
582 if (!u->u_ste) {
583 compiler_unit_free(u);
584 return 0;
585 }
586 Py_INCREF(name);
587 u->u_name = name;
588 u->u_varnames = list2dict(u->u_ste->ste_varnames);
589 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
590 if (!u->u_varnames || !u->u_cellvars) {
591 compiler_unit_free(u);
592 return 0;
593 }
Benjamin Peterson312595c2013-05-15 15:26:42 -0500594 if (u->u_ste->ste_needs_class_closure) {
Martin Panter7462b6492015-11-02 03:37:02 +0000595 /* Cook up an implicit __class__ cell. */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500596 _Py_IDENTIFIER(__class__);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300597 PyObject *name;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500598 int res;
599 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200600 assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500601 name = _PyUnicode_FromId(&PyId___class__);
602 if (!name) {
603 compiler_unit_free(u);
604 return 0;
605 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300606 res = PyDict_SetItem(u->u_cellvars, name, _PyLong_Zero);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500607 if (res < 0) {
608 compiler_unit_free(u);
609 return 0;
610 }
611 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200614 PyDict_GET_SIZE(u->u_cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 if (!u->u_freevars) {
616 compiler_unit_free(u);
617 return 0;
618 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 u->u_blocks = NULL;
621 u->u_nfblocks = 0;
622 u->u_firstlineno = lineno;
623 u->u_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000624 u->u_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 u->u_consts = PyDict_New();
626 if (!u->u_consts) {
627 compiler_unit_free(u);
628 return 0;
629 }
630 u->u_names = PyDict_New();
631 if (!u->u_names) {
632 compiler_unit_free(u);
633 return 0;
634 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 /* Push the old compiler_unit on the stack. */
639 if (c->u) {
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400640 PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
642 Py_XDECREF(capsule);
643 compiler_unit_free(u);
644 return 0;
645 }
646 Py_DECREF(capsule);
647 u->u_private = c->u->u_private;
648 Py_XINCREF(u->u_private);
649 }
650 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 c->c_nestlevel++;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100653
654 block = compiler_new_block(c);
655 if (block == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 return 0;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100657 c->u->u_curblock = block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400659 if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
660 if (!compiler_set_qualname(c))
661 return 0;
662 }
663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000665}
666
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000667static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668compiler_exit_scope(struct compiler *c)
669{
Victor Stinnerad9a0662013-11-19 22:23:20 +0100670 Py_ssize_t n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 PyObject *capsule;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 c->c_nestlevel--;
674 compiler_unit_free(c->u);
675 /* Restore c->u to the parent unit. */
676 n = PyList_GET_SIZE(c->c_stack) - 1;
677 if (n >= 0) {
678 capsule = PyList_GET_ITEM(c->c_stack, n);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400679 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 assert(c->u);
681 /* we are deleting from a list so this really shouldn't fail */
682 if (PySequence_DelItem(c->c_stack, n) < 0)
683 Py_FatalError("compiler_exit_scope()");
684 compiler_unit_check(c->u);
685 }
686 else
687 c->u = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000688
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000689}
690
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400691static int
692compiler_set_qualname(struct compiler *c)
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100693{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100694 _Py_static_string(dot, ".");
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400695 _Py_static_string(dot_locals, ".<locals>");
696 Py_ssize_t stack_size;
697 struct compiler_unit *u = c->u;
698 PyObject *name, *base, *dot_str, *dot_locals_str;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100699
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400700 base = NULL;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100701 stack_size = PyList_GET_SIZE(c->c_stack);
Benjamin Petersona8a38b82013-10-19 16:14:39 -0400702 assert(stack_size >= 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400703 if (stack_size > 1) {
704 int scope, force_global = 0;
705 struct compiler_unit *parent;
706 PyObject *mangled, *capsule;
707
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400708 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400709 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400710 assert(parent);
711
Yury Selivanov75445082015-05-11 22:57:16 -0400712 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
713 || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
714 || u->u_scope_type == COMPILER_SCOPE_CLASS) {
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400715 assert(u->u_name);
716 mangled = _Py_Mangle(parent->u_private, u->u_name);
717 if (!mangled)
718 return 0;
719 scope = PyST_GetScope(parent->u_ste, mangled);
720 Py_DECREF(mangled);
721 assert(scope != GLOBAL_IMPLICIT);
722 if (scope == GLOBAL_EXPLICIT)
723 force_global = 1;
724 }
725
726 if (!force_global) {
727 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
Yury Selivanov75445082015-05-11 22:57:16 -0400728 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400729 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
730 dot_locals_str = _PyUnicode_FromId(&dot_locals);
731 if (dot_locals_str == NULL)
732 return 0;
733 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
734 if (base == NULL)
735 return 0;
736 }
737 else {
738 Py_INCREF(parent->u_qualname);
739 base = parent->u_qualname;
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400740 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100741 }
742 }
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400743
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400744 if (base != NULL) {
745 dot_str = _PyUnicode_FromId(&dot);
746 if (dot_str == NULL) {
747 Py_DECREF(base);
748 return 0;
749 }
750 name = PyUnicode_Concat(base, dot_str);
751 Py_DECREF(base);
752 if (name == NULL)
753 return 0;
754 PyUnicode_Append(&name, u->u_name);
755 if (name == NULL)
756 return 0;
757 }
758 else {
759 Py_INCREF(u->u_name);
760 name = u->u_name;
761 }
762 u->u_qualname = name;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100763
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400764 return 1;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100765}
766
Eric V. Smith235a6f02015-09-19 14:51:32 -0400767
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000768/* Allocate a new block and return a pointer to it.
769 Returns NULL on error.
770*/
771
772static basicblock *
773compiler_new_block(struct compiler *c)
774{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 basicblock *b;
776 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 u = c->u;
Andy Lester7668a8b2020-03-24 23:26:44 -0500779 b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 if (b == NULL) {
781 PyErr_NoMemory();
782 return NULL;
783 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 /* Extend the singly linked list of blocks with new block. */
785 b->b_list = u->u_blocks;
786 u->u_blocks = b;
787 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788}
789
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790static basicblock *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791compiler_next_block(struct compiler *c)
792{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 basicblock *block = compiler_new_block(c);
794 if (block == NULL)
795 return NULL;
796 c->u->u_curblock->b_next = block;
797 c->u->u_curblock = block;
798 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799}
800
801static basicblock *
802compiler_use_next_block(struct compiler *c, basicblock *block)
803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 assert(block != NULL);
805 c->u->u_curblock->b_next = block;
806 c->u->u_curblock = block;
807 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808}
809
810/* Returns the offset of the next instruction in the current block's
811 b_instr array. Resizes the b_instr as necessary.
812 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000813*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814
815static int
Andy Lester76d58772020-03-10 21:18:12 -0500816compiler_next_instr(basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 assert(b != NULL);
819 if (b->b_instr == NULL) {
Andy Lester7668a8b2020-03-24 23:26:44 -0500820 b->b_instr = (struct instr *)PyObject_Calloc(
821 DEFAULT_BLOCK_SIZE, sizeof(struct instr));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 if (b->b_instr == NULL) {
823 PyErr_NoMemory();
824 return -1;
825 }
826 b->b_ialloc = DEFAULT_BLOCK_SIZE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 }
828 else if (b->b_iused == b->b_ialloc) {
829 struct instr *tmp;
830 size_t oldsize, newsize;
831 oldsize = b->b_ialloc * sizeof(struct instr);
832 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000833
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -0700834 if (oldsize > (SIZE_MAX >> 1)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 PyErr_NoMemory();
836 return -1;
837 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 if (newsize == 0) {
840 PyErr_NoMemory();
841 return -1;
842 }
843 b->b_ialloc <<= 1;
844 tmp = (struct instr *)PyObject_Realloc(
845 (void *)b->b_instr, newsize);
846 if (tmp == NULL) {
847 PyErr_NoMemory();
848 return -1;
849 }
850 b->b_instr = tmp;
851 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
852 }
853 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854}
855
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200856/* Set the line number and column offset for the following instructions.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000857
Christian Heimes2202f872008-02-06 14:31:34 +0000858 The line number is reset in the following cases:
859 - when entering a new scope
860 - on each statement
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200861 - on each expression and sub-expression
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200862 - before the "except" and "finally" clauses
Thomas Wouters89f507f2006-12-13 04:49:30 +0000863*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000864
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200865#define SET_LOC(c, x) \
866 (c)->u->u_lineno = (x)->lineno; \
867 (c)->u->u_col_offset = (x)->col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000868
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200869/* Return the stack effect of opcode with argument oparg.
870
871 Some opcodes have different stack effect when jump to the target and
872 when not jump. The 'jump' parameter specifies the case:
873
874 * 0 -- when not jump
875 * 1 -- when jump
876 * -1 -- maximal
877 */
878/* XXX Make the stack effect of WITH_CLEANUP_START and
879 WITH_CLEANUP_FINISH deterministic. */
880static int
881stack_effect(int opcode, int oparg, int jump)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000882{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 switch (opcode) {
Serhiy Storchaka57faf342018-04-25 22:04:06 +0300884 case NOP:
885 case EXTENDED_ARG:
886 return 0;
887
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200888 /* Stack manipulation */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 case POP_TOP:
890 return -1;
891 case ROT_TWO:
892 case ROT_THREE:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200893 case ROT_FOUR:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 return 0;
895 case DUP_TOP:
896 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000897 case DUP_TOP_TWO:
898 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000899
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200900 /* Unary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 case UNARY_POSITIVE:
902 case UNARY_NEGATIVE:
903 case UNARY_NOT:
904 case UNARY_INVERT:
905 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 case SET_ADD:
908 case LIST_APPEND:
909 return -1;
910 case MAP_ADD:
911 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000912
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200913 /* Binary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 case BINARY_POWER:
915 case BINARY_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400916 case BINARY_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 case BINARY_MODULO:
918 case BINARY_ADD:
919 case BINARY_SUBTRACT:
920 case BINARY_SUBSCR:
921 case BINARY_FLOOR_DIVIDE:
922 case BINARY_TRUE_DIVIDE:
923 return -1;
924 case INPLACE_FLOOR_DIVIDE:
925 case INPLACE_TRUE_DIVIDE:
926 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 case INPLACE_ADD:
929 case INPLACE_SUBTRACT:
930 case INPLACE_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400931 case INPLACE_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 case INPLACE_MODULO:
933 return -1;
934 case STORE_SUBSCR:
935 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 case DELETE_SUBSCR:
937 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 case BINARY_LSHIFT:
940 case BINARY_RSHIFT:
941 case BINARY_AND:
942 case BINARY_XOR:
943 case BINARY_OR:
944 return -1;
945 case INPLACE_POWER:
946 return -1;
947 case GET_ITER:
948 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 case PRINT_EXPR:
951 return -1;
952 case LOAD_BUILD_CLASS:
953 return 1;
954 case INPLACE_LSHIFT:
955 case INPLACE_RSHIFT:
956 case INPLACE_AND:
957 case INPLACE_XOR:
958 case INPLACE_OR:
959 return -1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 case SETUP_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200962 /* 1 in the normal flow.
963 * Restore the stack position and push 6 values before jumping to
964 * the handler if an exception be raised. */
965 return jump ? 6 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 case RETURN_VALUE:
967 return -1;
968 case IMPORT_STAR:
969 return -1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700970 case SETUP_ANNOTATIONS:
971 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 case YIELD_VALUE:
973 return 0;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500974 case YIELD_FROM:
975 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 case POP_BLOCK:
977 return 0;
978 case POP_EXCEPT:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200979 return -3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 case STORE_NAME:
982 return -1;
983 case DELETE_NAME:
984 return 0;
985 case UNPACK_SEQUENCE:
986 return oparg-1;
987 case UNPACK_EX:
988 return (oparg&0xFF) + (oparg>>8);
989 case FOR_ITER:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200990 /* -1 at end of iterator, 1 if continue iterating. */
991 return jump > 0 ? -1 : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 case STORE_ATTR:
994 return -2;
995 case DELETE_ATTR:
996 return -1;
997 case STORE_GLOBAL:
998 return -1;
999 case DELETE_GLOBAL:
1000 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 case LOAD_CONST:
1002 return 1;
1003 case LOAD_NAME:
1004 return 1;
1005 case BUILD_TUPLE:
1006 case BUILD_LIST:
1007 case BUILD_SET:
Serhiy Storchakaea525a22016-09-06 22:07:53 +03001008 case BUILD_STRING:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 return 1-oparg;
1010 case BUILD_MAP:
Benjamin Petersonb6855152015-09-10 21:02:39 -07001011 return 1 - 2*oparg;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001012 case BUILD_CONST_KEY_MAP:
1013 return -oparg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 case LOAD_ATTR:
1015 return 0;
1016 case COMPARE_OP:
Mark Shannon9af0e472020-01-14 10:12:45 +00001017 case IS_OP:
1018 case CONTAINS_OP:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 return -1;
Mark Shannon9af0e472020-01-14 10:12:45 +00001020 case JUMP_IF_NOT_EXC_MATCH:
1021 return -2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 case IMPORT_NAME:
1023 return -1;
1024 case IMPORT_FROM:
1025 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001027 /* Jumps */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 case JUMP_FORWARD:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 case JUMP_ABSOLUTE:
1030 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001031
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001032 case JUMP_IF_TRUE_OR_POP:
1033 case JUMP_IF_FALSE_OR_POP:
1034 return jump ? 0 : -1;
1035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 case POP_JUMP_IF_FALSE:
1037 case POP_JUMP_IF_TRUE:
1038 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 case LOAD_GLOBAL:
1041 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001042
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001043 /* Exception handling */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 case SETUP_FINALLY:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001045 /* 0 in the normal flow.
1046 * Restore the stack position and push 6 values before jumping to
1047 * the handler if an exception be raised. */
1048 return jump ? 6 : 0;
Mark Shannonfee55262019-11-21 09:11:43 +00001049 case RERAISE:
1050 return -3;
1051
1052 case WITH_EXCEPT_START:
1053 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 case LOAD_FAST:
1056 return 1;
1057 case STORE_FAST:
1058 return -1;
1059 case DELETE_FAST:
1060 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 case RAISE_VARARGS:
1063 return -oparg;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001064
1065 /* Functions and calls */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 case CALL_FUNCTION:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001067 return -oparg;
Yury Selivanovf2392132016-12-13 19:03:51 -05001068 case CALL_METHOD:
1069 return -oparg-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 case CALL_FUNCTION_KW:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001071 return -oparg-1;
1072 case CALL_FUNCTION_EX:
Matthieu Dartiailh3a9ac822017-02-21 14:25:22 +01001073 return -1 - ((oparg & 0x01) != 0);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001074 case MAKE_FUNCTION:
1075 return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1076 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 case BUILD_SLICE:
1078 if (oparg == 3)
1079 return -2;
1080 else
1081 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001082
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001083 /* Closures */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 case LOAD_CLOSURE:
1085 return 1;
1086 case LOAD_DEREF:
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001087 case LOAD_CLASSDEREF:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 return 1;
1089 case STORE_DEREF:
1090 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001091 case DELETE_DEREF:
1092 return 0;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001093
1094 /* Iterators and generators */
Yury Selivanov75445082015-05-11 22:57:16 -04001095 case GET_AWAITABLE:
1096 return 0;
1097 case SETUP_ASYNC_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001098 /* 0 in the normal flow.
1099 * Restore the stack position to the position before the result
1100 * of __aenter__ and push 6 values before jumping to the handler
1101 * if an exception be raised. */
1102 return jump ? -1 + 6 : 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001103 case BEFORE_ASYNC_WITH:
1104 return 1;
1105 case GET_AITER:
1106 return 0;
1107 case GET_ANEXT:
1108 return 1;
Yury Selivanov5376ba92015-06-22 12:19:30 -04001109 case GET_YIELD_FROM_ITER:
1110 return 0;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02001111 case END_ASYNC_FOR:
1112 return -7;
Eric V. Smitha78c7952015-11-03 12:45:05 -05001113 case FORMAT_VALUE:
1114 /* If there's a fmt_spec on the stack, we go from 2->1,
1115 else 1->1. */
1116 return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
Yury Selivanovf2392132016-12-13 19:03:51 -05001117 case LOAD_METHOD:
1118 return 1;
Zackery Spytzce6a0702019-08-25 03:44:09 -06001119 case LOAD_ASSERTION_ERROR:
1120 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001121 case LIST_TO_TUPLE:
1122 return 0;
1123 case LIST_EXTEND:
1124 case SET_UPDATE:
Mark Shannon8a4cd702020-01-27 09:57:45 +00001125 case DICT_MERGE:
1126 case DICT_UPDATE:
Mark Shannon13bc1392020-01-23 09:25:17 +00001127 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 default:
Larry Hastings3a907972013-11-23 14:49:22 -08001129 return PY_INVALID_STACK_EFFECT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 }
Larry Hastings3a907972013-11-23 14:49:22 -08001131 return PY_INVALID_STACK_EFFECT; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132}
1133
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001134int
Serhiy Storchaka7bdf2822018-09-18 09:54:26 +03001135PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump)
1136{
1137 return stack_effect(opcode, oparg, jump);
1138}
1139
1140int
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001141PyCompile_OpcodeStackEffect(int opcode, int oparg)
1142{
1143 return stack_effect(opcode, oparg, -1);
1144}
1145
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146/* Add an opcode with no argument.
1147 Returns 0 on failure, 1 on success.
1148*/
1149
1150static int
1151compiler_addop(struct compiler *c, int opcode)
1152{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 basicblock *b;
1154 struct instr *i;
1155 int off;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001156 assert(!HAS_ARG(opcode));
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001157 if (c->c_do_not_emit_bytecode) {
1158 return 1;
1159 }
Andy Lester76d58772020-03-10 21:18:12 -05001160 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 if (off < 0)
1162 return 0;
1163 b = c->u->u_curblock;
1164 i = &b->b_instr[off];
1165 i->i_opcode = opcode;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001166 i->i_oparg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 if (opcode == RETURN_VALUE)
1168 b->b_return = 1;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001169 i->i_lineno = c->u->u_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171}
1172
Victor Stinnerf8e32212013-11-19 23:56:34 +01001173static Py_ssize_t
Andy Lester76d58772020-03-10 21:18:12 -05001174compiler_add_o(PyObject *dict, PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001176 PyObject *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001179 v = PyDict_GetItemWithError(dict, o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 if (!v) {
Stefan Krahc0cbed12015-07-27 12:56:49 +02001181 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 return -1;
Stefan Krahc0cbed12015-07-27 12:56:49 +02001183 }
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001184 arg = PyDict_GET_SIZE(dict);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001185 v = PyLong_FromSsize_t(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 if (!v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 return -1;
1188 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001189 if (PyDict_SetItem(dict, o, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 Py_DECREF(v);
1191 return -1;
1192 }
1193 Py_DECREF(v);
1194 }
1195 else
1196 arg = PyLong_AsLong(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001197 return arg;
1198}
1199
INADA Naokic2e16072018-11-26 21:23:22 +09001200// Merge const *o* recursively and return constant key object.
1201static PyObject*
1202merge_consts_recursive(struct compiler *c, PyObject *o)
1203{
1204 // None and Ellipsis are singleton, and key is the singleton.
1205 // No need to merge object and key.
1206 if (o == Py_None || o == Py_Ellipsis) {
1207 Py_INCREF(o);
1208 return o;
1209 }
1210
1211 PyObject *key = _PyCode_ConstantKey(o);
1212 if (key == NULL) {
1213 return NULL;
1214 }
1215
1216 // t is borrowed reference
1217 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
1218 if (t != key) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001219 // o is registered in c_const_cache. Just use it.
Zackery Spytz9b4a1b12019-03-20 03:16:25 -06001220 Py_XINCREF(t);
INADA Naokic2e16072018-11-26 21:23:22 +09001221 Py_DECREF(key);
1222 return t;
1223 }
1224
INADA Naokif7e4d362018-11-29 00:58:46 +09001225 // We registered o in c_const_cache.
Simeon63b5fc52019-04-09 19:36:57 -04001226 // When o is a tuple or frozenset, we want to merge its
INADA Naokif7e4d362018-11-29 00:58:46 +09001227 // items too.
INADA Naokic2e16072018-11-26 21:23:22 +09001228 if (PyTuple_CheckExact(o)) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001229 Py_ssize_t len = PyTuple_GET_SIZE(o);
1230 for (Py_ssize_t i = 0; i < len; i++) {
INADA Naokic2e16072018-11-26 21:23:22 +09001231 PyObject *item = PyTuple_GET_ITEM(o, i);
1232 PyObject *u = merge_consts_recursive(c, item);
1233 if (u == NULL) {
1234 Py_DECREF(key);
1235 return NULL;
1236 }
1237
1238 // See _PyCode_ConstantKey()
1239 PyObject *v; // borrowed
1240 if (PyTuple_CheckExact(u)) {
1241 v = PyTuple_GET_ITEM(u, 1);
1242 }
1243 else {
1244 v = u;
1245 }
1246 if (v != item) {
1247 Py_INCREF(v);
1248 PyTuple_SET_ITEM(o, i, v);
1249 Py_DECREF(item);
1250 }
1251
1252 Py_DECREF(u);
1253 }
1254 }
INADA Naokif7e4d362018-11-29 00:58:46 +09001255 else if (PyFrozenSet_CheckExact(o)) {
Simeon63b5fc52019-04-09 19:36:57 -04001256 // *key* is tuple. And its first item is frozenset of
INADA Naokif7e4d362018-11-29 00:58:46 +09001257 // constant keys.
1258 // See _PyCode_ConstantKey() for detail.
1259 assert(PyTuple_CheckExact(key));
1260 assert(PyTuple_GET_SIZE(key) == 2);
1261
1262 Py_ssize_t len = PySet_GET_SIZE(o);
1263 if (len == 0) { // empty frozenset should not be re-created.
1264 return key;
1265 }
1266 PyObject *tuple = PyTuple_New(len);
1267 if (tuple == NULL) {
1268 Py_DECREF(key);
1269 return NULL;
1270 }
1271 Py_ssize_t i = 0, pos = 0;
1272 PyObject *item;
1273 Py_hash_t hash;
1274 while (_PySet_NextEntry(o, &pos, &item, &hash)) {
1275 PyObject *k = merge_consts_recursive(c, item);
1276 if (k == NULL) {
1277 Py_DECREF(tuple);
1278 Py_DECREF(key);
1279 return NULL;
1280 }
1281 PyObject *u;
1282 if (PyTuple_CheckExact(k)) {
1283 u = PyTuple_GET_ITEM(k, 1);
1284 Py_INCREF(u);
1285 Py_DECREF(k);
1286 }
1287 else {
1288 u = k;
1289 }
1290 PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u.
1291 i++;
1292 }
1293
1294 // Instead of rewriting o, we create new frozenset and embed in the
1295 // key tuple. Caller should get merged frozenset from the key tuple.
1296 PyObject *new = PyFrozenSet_New(tuple);
1297 Py_DECREF(tuple);
1298 if (new == NULL) {
1299 Py_DECREF(key);
1300 return NULL;
1301 }
1302 assert(PyTuple_GET_ITEM(key, 1) == o);
1303 Py_DECREF(o);
1304 PyTuple_SET_ITEM(key, 1, new);
1305 }
INADA Naokic2e16072018-11-26 21:23:22 +09001306
1307 return key;
1308}
1309
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001310static Py_ssize_t
1311compiler_add_const(struct compiler *c, PyObject *o)
1312{
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001313 if (c->c_do_not_emit_bytecode) {
1314 return 0;
1315 }
1316
INADA Naokic2e16072018-11-26 21:23:22 +09001317 PyObject *key = merge_consts_recursive(c, o);
1318 if (key == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001319 return -1;
INADA Naokic2e16072018-11-26 21:23:22 +09001320 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001321
Andy Lester76d58772020-03-10 21:18:12 -05001322 Py_ssize_t arg = compiler_add_o(c->u->u_consts, key);
INADA Naokic2e16072018-11-26 21:23:22 +09001323 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325}
1326
1327static int
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001328compiler_addop_load_const(struct compiler *c, PyObject *o)
1329{
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001330 if (c->c_do_not_emit_bytecode) {
1331 return 1;
1332 }
1333
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001334 Py_ssize_t arg = compiler_add_const(c, o);
1335 if (arg < 0)
1336 return 0;
1337 return compiler_addop_i(c, LOAD_CONST, arg);
1338}
1339
1340static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343{
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001344 if (c->c_do_not_emit_bytecode) {
1345 return 1;
1346 }
1347
Andy Lester76d58772020-03-10 21:18:12 -05001348 Py_ssize_t arg = compiler_add_o(dict, o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001349 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001350 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001351 return compiler_addop_i(c, opcode, arg);
1352}
1353
1354static int
1355compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001358 Py_ssize_t arg;
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001359
1360 if (c->c_do_not_emit_bytecode) {
1361 return 1;
1362 }
1363
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001364 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1365 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001366 return 0;
Andy Lester76d58772020-03-10 21:18:12 -05001367 arg = compiler_add_o(dict, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368 Py_DECREF(mangled);
1369 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001370 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001371 return compiler_addop_i(c, opcode, arg);
1372}
1373
1374/* Add an opcode with an integer argument.
1375 Returns 0 on failure, 1 on success.
1376*/
1377
1378static int
Victor Stinnerf8e32212013-11-19 23:56:34 +01001379compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001380{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 struct instr *i;
1382 int off;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001383
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001384 if (c->c_do_not_emit_bytecode) {
1385 return 1;
1386 }
1387
Victor Stinner2ad474b2016-03-01 23:34:47 +01001388 /* oparg value is unsigned, but a signed C int is usually used to store
1389 it in the C code (like Python/ceval.c).
1390
1391 Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1392
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001393 The argument of a concrete bytecode instruction is limited to 8-bit.
1394 EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1395 assert(HAS_ARG(opcode));
Victor Stinner2ad474b2016-03-01 23:34:47 +01001396 assert(0 <= oparg && oparg <= 2147483647);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001397
Andy Lester76d58772020-03-10 21:18:12 -05001398 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 if (off < 0)
1400 return 0;
1401 i = &c->u->u_curblock->b_instr[off];
Victor Stinnerf8e32212013-11-19 23:56:34 +01001402 i->i_opcode = opcode;
1403 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001404 i->i_lineno = c->u->u_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001406}
1407
1408static int
Mark Shannon582aaf12020-08-04 17:30:11 +01001409compiler_addop_j(struct compiler *c, int opcode, basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 struct instr *i;
1412 int off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001414 if (c->c_do_not_emit_bytecode) {
1415 return 1;
1416 }
1417
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001418 assert(HAS_ARG(opcode));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 assert(b != NULL);
Andy Lester76d58772020-03-10 21:18:12 -05001420 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 if (off < 0)
1422 return 0;
1423 i = &c->u->u_curblock->b_instr[off];
1424 i->i_opcode = opcode;
1425 i->i_target = b;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001426 i->i_lineno = c->u->u_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428}
1429
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +01001430/* NEXT_BLOCK() creates an implicit jump from the current block
1431 to the new block.
1432
1433 The returns inside this macro make it impossible to decref objects
1434 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001435*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 if (compiler_next_block((C)) == NULL) \
1438 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439}
1440
1441#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 if (!compiler_addop((C), (OP))) \
1443 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444}
1445
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001446#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 if (!compiler_addop((C), (OP))) { \
1448 compiler_exit_scope(c); \
1449 return 0; \
1450 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001451}
1452
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001453#define ADDOP_LOAD_CONST(C, O) { \
1454 if (!compiler_addop_load_const((C), (O))) \
1455 return 0; \
1456}
1457
1458/* Same as ADDOP_LOAD_CONST, but steals a reference. */
1459#define ADDOP_LOAD_CONST_NEW(C, O) { \
1460 PyObject *__new_const = (O); \
1461 if (__new_const == NULL) { \
1462 return 0; \
1463 } \
1464 if (!compiler_addop_load_const((C), __new_const)) { \
1465 Py_DECREF(__new_const); \
1466 return 0; \
1467 } \
1468 Py_DECREF(__new_const); \
1469}
1470
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1473 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474}
1475
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001476/* Same as ADDOP_O, but steals a reference. */
1477#define ADDOP_N(C, OP, O, TYPE) { \
1478 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1479 Py_DECREF((O)); \
1480 return 0; \
1481 } \
1482 Py_DECREF((O)); \
1483}
1484
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1487 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488}
1489
1490#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 if (!compiler_addop_i((C), (OP), (O))) \
1492 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493}
1494
Mark Shannon582aaf12020-08-04 17:30:11 +01001495#define ADDOP_JUMP(C, OP, O) { \
1496 if (!compiler_addop_j((C), (OP), (O))) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001498}
1499
Mark Shannon9af0e472020-01-14 10:12:45 +00001500#define ADDOP_COMPARE(C, CMP) { \
1501 if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \
1502 return 0; \
1503}
1504
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1506 the ASDL name to synthesize the name of the C type and the visit function.
1507*/
1508
1509#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 if (!compiler_visit_ ## TYPE((C), (V))) \
1511 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001512}
1513
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001514#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 if (!compiler_visit_ ## TYPE((C), (V))) { \
1516 compiler_exit_scope(c); \
1517 return 0; \
1518 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001519}
1520
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 if (!compiler_visit_slice((C), (V), (CTX))) \
1523 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001524}
1525
1526#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001528 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1530 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1531 if (!compiler_visit_ ## TYPE((C), elt)) \
1532 return 0; \
1533 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534}
1535
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001536#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001538 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1540 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1541 if (!compiler_visit_ ## TYPE((C), elt)) { \
1542 compiler_exit_scope(c); \
1543 return 0; \
1544 } \
1545 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001546}
1547
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001548/* These macros allows to check only for errors and not emmit bytecode
1549 * while visiting nodes.
1550*/
1551
1552#define BEGIN_DO_NOT_EMIT_BYTECODE { \
1553 c->c_do_not_emit_bytecode++;
1554
1555#define END_DO_NOT_EMIT_BYTECODE \
1556 c->c_do_not_emit_bytecode--; \
1557}
1558
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001559/* Search if variable annotations are present statically in a block. */
1560
1561static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001562find_ann(asdl_stmt_seq *stmts)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001563{
1564 int i, j, res = 0;
1565 stmt_ty st;
1566
1567 for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1568 st = (stmt_ty)asdl_seq_GET(stmts, i);
1569 switch (st->kind) {
1570 case AnnAssign_kind:
1571 return 1;
1572 case For_kind:
1573 res = find_ann(st->v.For.body) ||
1574 find_ann(st->v.For.orelse);
1575 break;
1576 case AsyncFor_kind:
1577 res = find_ann(st->v.AsyncFor.body) ||
1578 find_ann(st->v.AsyncFor.orelse);
1579 break;
1580 case While_kind:
1581 res = find_ann(st->v.While.body) ||
1582 find_ann(st->v.While.orelse);
1583 break;
1584 case If_kind:
1585 res = find_ann(st->v.If.body) ||
1586 find_ann(st->v.If.orelse);
1587 break;
1588 case With_kind:
1589 res = find_ann(st->v.With.body);
1590 break;
1591 case AsyncWith_kind:
1592 res = find_ann(st->v.AsyncWith.body);
1593 break;
1594 case Try_kind:
1595 for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1596 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1597 st->v.Try.handlers, j);
1598 if (find_ann(handler->v.ExceptHandler.body)) {
1599 return 1;
1600 }
1601 }
1602 res = find_ann(st->v.Try.body) ||
1603 find_ann(st->v.Try.finalbody) ||
1604 find_ann(st->v.Try.orelse);
1605 break;
1606 default:
1607 res = 0;
1608 }
1609 if (res) {
1610 break;
1611 }
1612 }
1613 return res;
1614}
1615
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001616/*
1617 * Frame block handling functions
1618 */
1619
1620static int
1621compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b,
Mark Shannonfee55262019-11-21 09:11:43 +00001622 basicblock *exit, void *datum)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001623{
1624 struct fblockinfo *f;
1625 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
Mark Shannon02d126a2020-09-25 14:04:19 +01001626 return compiler_error(c, "too many statically nested blocks");
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001627 }
1628 f = &c->u->u_fblock[c->u->u_nfblocks++];
1629 f->fb_type = t;
1630 f->fb_block = b;
1631 f->fb_exit = exit;
Mark Shannonfee55262019-11-21 09:11:43 +00001632 f->fb_datum = datum;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001633 return 1;
1634}
1635
1636static void
1637compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
1638{
1639 struct compiler_unit *u = c->u;
1640 assert(u->u_nfblocks > 0);
1641 u->u_nfblocks--;
1642 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
1643 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
1644}
1645
Mark Shannonfee55262019-11-21 09:11:43 +00001646static int
1647compiler_call_exit_with_nones(struct compiler *c) {
1648 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1649 ADDOP(c, DUP_TOP);
1650 ADDOP(c, DUP_TOP);
1651 ADDOP_I(c, CALL_FUNCTION, 3);
1652 return 1;
1653}
1654
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001655/* Unwind a frame block. If preserve_tos is true, the TOS before
Mark Shannonfee55262019-11-21 09:11:43 +00001656 * popping the blocks will be restored afterwards, unless another
1657 * return, break or continue is found. In which case, the TOS will
1658 * be popped.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001659 */
1660static int
1661compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
1662 int preserve_tos)
1663{
1664 switch (info->fb_type) {
1665 case WHILE_LOOP:
Mark Shannon02d126a2020-09-25 14:04:19 +01001666 case EXCEPTION_HANDLER:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001667 return 1;
1668
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001669 case FOR_LOOP:
1670 /* Pop the iterator */
1671 if (preserve_tos) {
1672 ADDOP(c, ROT_TWO);
1673 }
1674 ADDOP(c, POP_TOP);
1675 return 1;
1676
Mark Shannon02d126a2020-09-25 14:04:19 +01001677 case TRY_EXCEPT:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001678 ADDOP(c, POP_BLOCK);
1679 return 1;
1680
1681 case FINALLY_TRY:
1682 ADDOP(c, POP_BLOCK);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001683 if (preserve_tos) {
Mark Shannonfee55262019-11-21 09:11:43 +00001684 if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) {
1685 return 0;
1686 }
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001687 }
Mark Shannon88dce262019-12-30 09:53:36 +00001688 /* Emit the finally block, restoring the line number when done */
1689 int saved_lineno = c->u->u_lineno;
Mark Shannonfee55262019-11-21 09:11:43 +00001690 VISIT_SEQ(c, stmt, info->fb_datum);
Mark Shannon88dce262019-12-30 09:53:36 +00001691 c->u->u_lineno = saved_lineno;
Mark Shannonfee55262019-11-21 09:11:43 +00001692 if (preserve_tos) {
1693 compiler_pop_fblock(c, POP_VALUE, NULL);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001694 }
1695 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001696
Mark Shannonfee55262019-11-21 09:11:43 +00001697 case FINALLY_END:
1698 if (preserve_tos) {
1699 ADDOP(c, ROT_FOUR);
1700 }
1701 ADDOP(c, POP_TOP);
1702 ADDOP(c, POP_TOP);
1703 ADDOP(c, POP_TOP);
1704 if (preserve_tos) {
1705 ADDOP(c, ROT_FOUR);
1706 }
1707 ADDOP(c, POP_EXCEPT);
1708 return 1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001709
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001710 case WITH:
1711 case ASYNC_WITH:
1712 ADDOP(c, POP_BLOCK);
1713 if (preserve_tos) {
1714 ADDOP(c, ROT_TWO);
1715 }
Mark Shannonfee55262019-11-21 09:11:43 +00001716 if(!compiler_call_exit_with_nones(c)) {
1717 return 0;
1718 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001719 if (info->fb_type == ASYNC_WITH) {
1720 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001721 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001722 ADDOP(c, YIELD_FROM);
1723 }
Mark Shannonfee55262019-11-21 09:11:43 +00001724 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001725 return 1;
1726
1727 case HANDLER_CLEANUP:
Mark Shannonfee55262019-11-21 09:11:43 +00001728 if (info->fb_datum) {
1729 ADDOP(c, POP_BLOCK);
1730 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001731 if (preserve_tos) {
1732 ADDOP(c, ROT_FOUR);
1733 }
Mark Shannonfee55262019-11-21 09:11:43 +00001734 ADDOP(c, POP_EXCEPT);
1735 if (info->fb_datum) {
1736 ADDOP_LOAD_CONST(c, Py_None);
1737 compiler_nameop(c, info->fb_datum, Store);
1738 compiler_nameop(c, info->fb_datum, Del);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001739 }
Mark Shannonfee55262019-11-21 09:11:43 +00001740 return 1;
1741
1742 case POP_VALUE:
1743 if (preserve_tos) {
1744 ADDOP(c, ROT_TWO);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001745 }
Mark Shannonfee55262019-11-21 09:11:43 +00001746 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001747 return 1;
1748 }
1749 Py_UNREACHABLE();
1750}
1751
Mark Shannonfee55262019-11-21 09:11:43 +00001752/** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */
1753static int
1754compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) {
1755 if (c->u->u_nfblocks == 0) {
1756 return 1;
1757 }
1758 struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1];
1759 if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) {
1760 *loop = top;
1761 return 1;
1762 }
1763 struct fblockinfo copy = *top;
1764 c->u->u_nfblocks--;
1765 if (!compiler_unwind_fblock(c, &copy, preserve_tos)) {
1766 return 0;
1767 }
1768 if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) {
1769 return 0;
1770 }
1771 c->u->u_fblock[c->u->u_nfblocks] = copy;
1772 c->u->u_nfblocks++;
1773 return 1;
1774}
1775
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001776/* Compile a sequence of statements, checking for a docstring
1777 and for annotations. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001778
1779static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001780compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001781{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001782 int i = 0;
1783 stmt_ty st;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001784 PyObject *docstring;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001785
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001786 /* Set current line number to the line number of first statement.
1787 This way line number for SETUP_ANNOTATIONS will always
1788 coincide with the line number of first "real" statement in module.
Hansraj Das01171eb2019-10-09 07:54:02 +05301789 If body is empty, then lineno will be set later in assemble. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001790 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) {
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001791 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001792 SET_LOC(c, st);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001793 }
1794 /* Every annotated class and module should have __annotations__. */
1795 if (find_ann(stmts)) {
1796 ADDOP(c, SETUP_ANNOTATIONS);
1797 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001798 if (!asdl_seq_LEN(stmts))
1799 return 1;
INADA Naokicb41b272017-02-23 00:31:59 +09001800 /* if not -OO mode, set docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001801 if (c->c_optimize < 2) {
1802 docstring = _PyAST_GetDocString(stmts);
1803 if (docstring) {
1804 i = 1;
1805 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1806 assert(st->kind == Expr_kind);
1807 VISIT(c, expr, st->v.Expr.value);
1808 if (!compiler_nameop(c, __doc__, Store))
1809 return 0;
1810 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001812 for (; i < asdl_seq_LEN(stmts); i++)
1813 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001815}
1816
1817static PyCodeObject *
1818compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001819{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 PyCodeObject *co;
1821 int addNone = 1;
1822 static PyObject *module;
1823 if (!module) {
1824 module = PyUnicode_InternFromString("<module>");
1825 if (!module)
1826 return NULL;
1827 }
1828 /* Use 0 for firstlineno initially, will fixup in assemble(). */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001829 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 return NULL;
1831 switch (mod->kind) {
1832 case Module_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001833 if (!compiler_body(c, mod->v.Module.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 compiler_exit_scope(c);
1835 return 0;
1836 }
1837 break;
1838 case Interactive_kind:
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001839 if (find_ann(mod->v.Interactive.body)) {
1840 ADDOP(c, SETUP_ANNOTATIONS);
1841 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842 c->c_interactive = 1;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001843 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 break;
1845 case Expression_kind:
1846 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1847 addNone = 0;
1848 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 default:
1850 PyErr_Format(PyExc_SystemError,
1851 "module kind %d should not be possible",
1852 mod->kind);
1853 return 0;
1854 }
1855 co = assemble(c, addNone);
1856 compiler_exit_scope(c);
1857 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001858}
1859
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860/* The test for LOCAL must come before the test for FREE in order to
1861 handle classes where name is both local and free. The local var is
1862 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001863*/
1864
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001865static int
1866get_ref_type(struct compiler *c, PyObject *name)
1867{
Victor Stinner0b1bc562013-05-16 22:17:17 +02001868 int scope;
Benjamin Peterson312595c2013-05-15 15:26:42 -05001869 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001870 _PyUnicode_EqualToASCIIString(name, "__class__"))
Benjamin Peterson312595c2013-05-15 15:26:42 -05001871 return CELL;
Victor Stinner0b1bc562013-05-16 22:17:17 +02001872 scope = PyST_GetScope(c->u->u_ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 if (scope == 0) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01001874 _Py_FatalErrorFormat(__func__,
1875 "unknown scope for %.100s in %.100s(%s)\n"
1876 "symbols: %s\nlocals: %s\nglobals: %s",
1877 PyUnicode_AsUTF8(name),
1878 PyUnicode_AsUTF8(c->u->u_name),
1879 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)),
1880 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)),
1881 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)),
1882 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001886}
1887
1888static int
1889compiler_lookup_arg(PyObject *dict, PyObject *name)
1890{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001891 PyObject *v;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001892 v = PyDict_GetItemWithError(dict, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001893 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001894 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001895 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001896}
1897
1898static int
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001899compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001900{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001901 Py_ssize_t i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001902 if (qualname == NULL)
1903 qualname = co->co_name;
1904
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001905 if (free) {
1906 for (i = 0; i < free; ++i) {
1907 /* Bypass com_addop_varname because it will generate
1908 LOAD_DEREF but LOAD_CLOSURE is needed.
1909 */
1910 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1911 int arg, reftype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001912
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001913 /* Special case: If a class contains a method with a
1914 free variable that has the same name as a method,
1915 the name will be considered free *and* local in the
1916 class. It should be handled by the closure, as
Min ho Kimc4cacc82019-07-31 08:16:13 +10001917 well as by the normal name lookup logic.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001918 */
1919 reftype = get_ref_type(c, name);
1920 if (reftype == CELL)
1921 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1922 else /* (reftype == FREE) */
1923 arg = compiler_lookup_arg(c->u->u_freevars, name);
1924 if (arg == -1) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01001925 _Py_FatalErrorFormat(__func__,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001926 "lookup %s in %s %d %d\n"
1927 "freevars of %s: %s\n",
1928 PyUnicode_AsUTF8(PyObject_Repr(name)),
1929 PyUnicode_AsUTF8(c->u->u_name),
1930 reftype, arg,
1931 PyUnicode_AsUTF8(co->co_name),
1932 PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars)));
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001933 }
1934 ADDOP_I(c, LOAD_CLOSURE, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001936 flags |= 0x08;
1937 ADDOP_I(c, BUILD_TUPLE, free);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001939 ADDOP_LOAD_CONST(c, (PyObject*)co);
1940 ADDOP_LOAD_CONST(c, qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001941 ADDOP_I(c, MAKE_FUNCTION, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001943}
1944
1945static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001946compiler_decorators(struct compiler *c, asdl_expr_seq* decos)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 if (!decos)
1951 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1954 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1955 }
1956 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001957}
1958
1959static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001960compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs,
1961 asdl_expr_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001962{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001963 /* Push a dict of keyword-only default values.
1964
1965 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
1966 */
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001967 int i;
1968 PyObject *keys = NULL;
1969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1971 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1972 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1973 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04001974 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001975 if (!mangled) {
1976 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001978 if (keys == NULL) {
1979 keys = PyList_New(1);
1980 if (keys == NULL) {
1981 Py_DECREF(mangled);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001982 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001983 }
1984 PyList_SET_ITEM(keys, 0, mangled);
1985 }
1986 else {
1987 int res = PyList_Append(keys, mangled);
1988 Py_DECREF(mangled);
1989 if (res == -1) {
1990 goto error;
1991 }
1992 }
1993 if (!compiler_visit_expr(c, default_)) {
1994 goto error;
1995 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 }
1997 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001998 if (keys != NULL) {
1999 Py_ssize_t default_count = PyList_GET_SIZE(keys);
2000 PyObject *keys_tuple = PyList_AsTuple(keys);
2001 Py_DECREF(keys);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002002 ADDOP_LOAD_CONST_NEW(c, keys_tuple);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002003 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002004 assert(default_count > 0);
2005 return 1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002006 }
2007 else {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002008 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002009 }
2010
2011error:
2012 Py_XDECREF(keys);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002013 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002014}
2015
2016static int
Guido van Rossum95e4d582018-01-26 08:20:18 -08002017compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
2018{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +03002019 ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation));
Guido van Rossum95e4d582018-01-26 08:20:18 -08002020 return 1;
2021}
2022
2023static int
Neal Norwitzc1505362006-12-28 06:47:50 +00002024compiler_visit_argannotation(struct compiler *c, identifier id,
2025 expr_ty annotation, PyObject *names)
2026{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 if (annotation) {
Victor Stinner065efc32014-02-18 22:07:56 +01002028 PyObject *mangled;
Batuhan Taskaya044a1042020-10-06 23:03:02 +03002029 VISIT(c, annexpr, annotation);
Victor Stinner065efc32014-02-18 22:07:56 +01002030 mangled = _Py_Mangle(c->u->u_private, id);
Yury Selivanov34ce99f2014-02-18 12:49:41 -05002031 if (!mangled)
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002032 return 0;
Yury Selivanov34ce99f2014-02-18 12:49:41 -05002033 if (PyList_Append(names, mangled) < 0) {
2034 Py_DECREF(mangled);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002035 return 0;
Yury Selivanov34ce99f2014-02-18 12:49:41 -05002036 }
2037 Py_DECREF(mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002039 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002040}
2041
2042static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002043compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args,
Neal Norwitzc1505362006-12-28 06:47:50 +00002044 PyObject *names)
2045{
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002046 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 for (i = 0; i < asdl_seq_LEN(args); i++) {
2048 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002049 if (!compiler_visit_argannotation(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050 c,
2051 arg->arg,
2052 arg->annotation,
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002053 names))
2054 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002056 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002057}
2058
2059static int
2060compiler_visit_annotations(struct compiler *c, arguments_ty args,
2061 expr_ty returns)
2062{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002063 /* Push arg annotation dict.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002064 The expressions are evaluated out-of-order wrt the source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00002065
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002066 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 */
2068 static identifier return_str;
2069 PyObject *names;
Victor Stinnerad9a0662013-11-19 22:23:20 +01002070 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 names = PyList_New(0);
2072 if (!names)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03002073 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002074
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002075 if (!compiler_visit_argannotations(c, args->args, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 goto error;
Pablo Galindoa0c01bf2019-05-31 15:19:50 +01002077 if (!compiler_visit_argannotations(c, args->posonlyargs, names))
2078 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002079 if (args->vararg && args->vararg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002080 !compiler_visit_argannotation(c, args->vararg->arg,
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002081 args->vararg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 goto error;
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002083 if (!compiler_visit_argannotations(c, args->kwonlyargs, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002085 if (args->kwarg && args->kwarg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002086 !compiler_visit_argannotation(c, args->kwarg->arg,
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002087 args->kwarg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00002089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 if (!return_str) {
2091 return_str = PyUnicode_InternFromString("return");
2092 if (!return_str)
2093 goto error;
2094 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002095 if (!compiler_visit_argannotation(c, return_str, returns, names)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 goto error;
2097 }
2098
2099 len = PyList_GET_SIZE(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100 if (len) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002101 PyObject *keytuple = PyList_AsTuple(names);
2102 Py_DECREF(names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002103 ADDOP_LOAD_CONST_NEW(c, keytuple);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002104 ADDOP_I(c, BUILD_CONST_KEY_MAP, len);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002105 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002106 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002107 else {
2108 Py_DECREF(names);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002109 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002110 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002111
2112error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113 Py_DECREF(names);
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03002114 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002115}
2116
2117static int
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002118compiler_visit_defaults(struct compiler *c, arguments_ty args)
2119{
2120 VISIT_SEQ(c, expr, args->defaults);
2121 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
2122 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002123}
2124
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002125static Py_ssize_t
2126compiler_default_arguments(struct compiler *c, arguments_ty args)
2127{
2128 Py_ssize_t funcflags = 0;
2129 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002130 if (!compiler_visit_defaults(c, args))
2131 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002132 funcflags |= 0x01;
2133 }
2134 if (args->kwonlyargs) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002135 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002136 args->kw_defaults);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002137 if (res == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002138 return -1;
2139 }
2140 else if (res > 0) {
2141 funcflags |= 0x02;
2142 }
2143 }
2144 return funcflags;
2145}
2146
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002147static int
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002148forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx)
2149{
2150
2151 if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2152 compiler_error(c, "cannot assign to __debug__");
2153 return 1;
2154 }
2155 return 0;
2156}
2157
2158static int
2159compiler_check_debug_one_arg(struct compiler *c, arg_ty arg)
2160{
2161 if (arg != NULL) {
2162 if (forbidden_name(c, arg->arg, Store))
2163 return 0;
2164 }
2165 return 1;
2166}
2167
2168static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002169compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args)
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002170{
2171 if (args != NULL) {
Pablo Galindoee40e4b2020-04-23 03:43:08 +01002172 for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002173 if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i)))
2174 return 0;
2175 }
2176 }
2177 return 1;
2178}
2179
2180static int
2181compiler_check_debug_args(struct compiler *c, arguments_ty args)
2182{
2183 if (!compiler_check_debug_args_seq(c, args->posonlyargs))
2184 return 0;
2185 if (!compiler_check_debug_args_seq(c, args->args))
2186 return 0;
2187 if (!compiler_check_debug_one_arg(c, args->vararg))
2188 return 0;
2189 if (!compiler_check_debug_args_seq(c, args->kwonlyargs))
2190 return 0;
2191 if (!compiler_check_debug_one_arg(c, args->kwarg))
2192 return 0;
2193 return 1;
2194}
2195
2196static int
Yury Selivanov75445082015-05-11 22:57:16 -04002197compiler_function(struct compiler *c, stmt_ty s, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002198{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 PyCodeObject *co;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002200 PyObject *qualname, *docstring = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002201 arguments_ty args;
2202 expr_ty returns;
2203 identifier name;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002204 asdl_expr_seq* decos;
2205 asdl_stmt_seq *body;
INADA Naokicb41b272017-02-23 00:31:59 +09002206 Py_ssize_t i, funcflags;
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002207 int annotations;
Yury Selivanov75445082015-05-11 22:57:16 -04002208 int scope_type;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002209 int firstlineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002210
Yury Selivanov75445082015-05-11 22:57:16 -04002211 if (is_async) {
2212 assert(s->kind == AsyncFunctionDef_kind);
2213
2214 args = s->v.AsyncFunctionDef.args;
2215 returns = s->v.AsyncFunctionDef.returns;
2216 decos = s->v.AsyncFunctionDef.decorator_list;
2217 name = s->v.AsyncFunctionDef.name;
2218 body = s->v.AsyncFunctionDef.body;
2219
2220 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
2221 } else {
2222 assert(s->kind == FunctionDef_kind);
2223
2224 args = s->v.FunctionDef.args;
2225 returns = s->v.FunctionDef.returns;
2226 decos = s->v.FunctionDef.decorator_list;
2227 name = s->v.FunctionDef.name;
2228 body = s->v.FunctionDef.body;
2229
2230 scope_type = COMPILER_SCOPE_FUNCTION;
2231 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002232
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002233 if (!compiler_check_debug_args(c, args))
2234 return 0;
2235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 if (!compiler_decorators(c, decos))
2237 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002238
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002239 firstlineno = s->lineno;
2240 if (asdl_seq_LEN(decos)) {
2241 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2242 }
2243
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002244 funcflags = compiler_default_arguments(c, args);
2245 if (funcflags == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002246 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002247 }
2248
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002249 annotations = compiler_visit_annotations(c, args, returns);
2250 if (annotations == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002251 return 0;
2252 }
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002253 else if (annotations > 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002254 funcflags |= 0x04;
2255 }
2256
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002257 if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002258 return 0;
2259 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002260
INADA Naokicb41b272017-02-23 00:31:59 +09002261 /* if not -OO mode, add docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002262 if (c->c_optimize < 2) {
2263 docstring = _PyAST_GetDocString(body);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002264 }
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002265 if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 compiler_exit_scope(c);
2267 return 0;
2268 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002271 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
INADA Naokicb41b272017-02-23 00:31:59 +09002273 VISIT_SEQ_IN_SCOPE(c, stmt, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002275 qualname = c->u->u_qualname;
2276 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002278 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002279 Py_XDECREF(qualname);
2280 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002282 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002284 compiler_make_closure(c, co, funcflags, qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002285 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 /* decorators */
2289 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2290 ADDOP_I(c, CALL_FUNCTION, 1);
2291 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002292
Yury Selivanov75445082015-05-11 22:57:16 -04002293 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002294}
2295
2296static int
2297compiler_class(struct compiler *c, stmt_ty s)
2298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 PyCodeObject *co;
2300 PyObject *str;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002301 int i, firstlineno;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002302 asdl_expr_seq *decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 if (!compiler_decorators(c, decos))
2305 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002306
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002307 firstlineno = s->lineno;
2308 if (asdl_seq_LEN(decos)) {
2309 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2310 }
2311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002312 /* ultimately generate code for:
2313 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
2314 where:
2315 <func> is a function/closure created from the class body;
2316 it has a single argument (__locals__) where the dict
2317 (or MutableSequence) representing the locals is passed
2318 <name> is the class name
2319 <bases> is the positional arguments and *varargs argument
2320 <keywords> is the keyword arguments and **kwds argument
2321 This borrows from compiler_call.
2322 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002325 if (!compiler_enter_scope(c, s->v.ClassDef.name,
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002326 COMPILER_SCOPE_CLASS, (void *)s, firstlineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 return 0;
2328 /* this block represents what we do in the new scope */
2329 {
2330 /* use the class name for name mangling */
2331 Py_INCREF(s->v.ClassDef.name);
Serhiy Storchaka48842712016-04-06 09:45:48 +03002332 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002333 /* load (global) __name__ ... */
2334 str = PyUnicode_InternFromString("__name__");
2335 if (!str || !compiler_nameop(c, str, Load)) {
2336 Py_XDECREF(str);
2337 compiler_exit_scope(c);
2338 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002339 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 Py_DECREF(str);
2341 /* ... and store it as __module__ */
2342 str = PyUnicode_InternFromString("__module__");
2343 if (!str || !compiler_nameop(c, str, Store)) {
2344 Py_XDECREF(str);
2345 compiler_exit_scope(c);
2346 return 0;
2347 }
2348 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002349 assert(c->u->u_qualname);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002350 ADDOP_LOAD_CONST(c, c->u->u_qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002351 str = PyUnicode_InternFromString("__qualname__");
2352 if (!str || !compiler_nameop(c, str, Store)) {
2353 Py_XDECREF(str);
2354 compiler_exit_scope(c);
2355 return 0;
2356 }
2357 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 /* compile the body proper */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002359 if (!compiler_body(c, s->v.ClassDef.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 compiler_exit_scope(c);
2361 return 0;
2362 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002363 /* Return __classcell__ if it is referenced, otherwise return None */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002364 if (c->u->u_ste->ste_needs_class_closure) {
Nick Coghlan19d24672016-12-05 16:47:55 +10002365 /* Store __classcell__ into class namespace & return it */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002366 str = PyUnicode_InternFromString("__class__");
2367 if (str == NULL) {
2368 compiler_exit_scope(c);
2369 return 0;
2370 }
2371 i = compiler_lookup_arg(c->u->u_cellvars, str);
2372 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01002373 if (i < 0) {
2374 compiler_exit_scope(c);
2375 return 0;
2376 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002377 assert(i == 0);
Nick Coghlan944368e2016-09-11 14:45:49 +10002378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 ADDOP_I(c, LOAD_CLOSURE, i);
Nick Coghlan19d24672016-12-05 16:47:55 +10002380 ADDOP(c, DUP_TOP);
Nick Coghlan944368e2016-09-11 14:45:49 +10002381 str = PyUnicode_InternFromString("__classcell__");
2382 if (!str || !compiler_nameop(c, str, Store)) {
2383 Py_XDECREF(str);
2384 compiler_exit_scope(c);
2385 return 0;
2386 }
2387 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002389 else {
Nick Coghlan19d24672016-12-05 16:47:55 +10002390 /* No methods referenced __class__, so just return None */
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002391 assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002392 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson312595c2013-05-15 15:26:42 -05002393 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002394 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 /* create the code object */
2396 co = assemble(c, 1);
2397 }
2398 /* leave the new scope */
2399 compiler_exit_scope(c);
2400 if (co == NULL)
2401 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 /* 2. load the 'build_class' function */
2404 ADDOP(c, LOAD_BUILD_CLASS);
2405
2406 /* 3. load a function (or closure) made from the code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002407 compiler_make_closure(c, co, 0, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 Py_DECREF(co);
2409
2410 /* 4. load class name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002411 ADDOP_LOAD_CONST(c, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412
2413 /* 5. generate the rest of the code for the call */
Pablo Galindoa5634c42020-09-16 19:42:00 +01002414 if (!compiler_call_helper(c, 2, s->v.ClassDef.bases, s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 return 0;
2416
2417 /* 6. apply decorators */
2418 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2419 ADDOP_I(c, CALL_FUNCTION, 1);
2420 }
2421
2422 /* 7. store into <name> */
2423 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2424 return 0;
2425 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002426}
2427
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02002428/* Return 0 if the expression is a constant value except named singletons.
2429 Return 1 otherwise. */
2430static int
2431check_is_arg(expr_ty e)
2432{
2433 if (e->kind != Constant_kind) {
2434 return 1;
2435 }
2436 PyObject *value = e->v.Constant.value;
2437 return (value == Py_None
2438 || value == Py_False
2439 || value == Py_True
2440 || value == Py_Ellipsis);
2441}
2442
2443/* Check operands of identity chacks ("is" and "is not").
2444 Emit a warning if any operand is a constant except named singletons.
2445 Return 0 on error.
2446 */
2447static int
2448check_compare(struct compiler *c, expr_ty e)
2449{
2450 Py_ssize_t i, n;
2451 int left = check_is_arg(e->v.Compare.left);
2452 n = asdl_seq_LEN(e->v.Compare.ops);
2453 for (i = 0; i < n; i++) {
2454 cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i);
2455 int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2456 if (op == Is || op == IsNot) {
2457 if (!right || !left) {
2458 const char *msg = (op == Is)
2459 ? "\"is\" with a literal. Did you mean \"==\"?"
2460 : "\"is not\" with a literal. Did you mean \"!=\"?";
2461 return compiler_warn(c, msg);
2462 }
2463 }
2464 left = right;
2465 }
2466 return 1;
2467}
2468
Mark Shannon9af0e472020-01-14 10:12:45 +00002469static int compiler_addcompare(struct compiler *c, cmpop_ty op)
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002470{
Mark Shannon9af0e472020-01-14 10:12:45 +00002471 int cmp;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002472 switch (op) {
2473 case Eq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002474 cmp = Py_EQ;
2475 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002476 case NotEq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002477 cmp = Py_NE;
2478 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002479 case Lt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002480 cmp = Py_LT;
2481 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002482 case LtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002483 cmp = Py_LE;
2484 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002485 case Gt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002486 cmp = Py_GT;
2487 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002488 case GtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002489 cmp = Py_GE;
2490 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002491 case Is:
Mark Shannon9af0e472020-01-14 10:12:45 +00002492 ADDOP_I(c, IS_OP, 0);
2493 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002494 case IsNot:
Mark Shannon9af0e472020-01-14 10:12:45 +00002495 ADDOP_I(c, IS_OP, 1);
2496 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002497 case In:
Mark Shannon9af0e472020-01-14 10:12:45 +00002498 ADDOP_I(c, CONTAINS_OP, 0);
2499 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002500 case NotIn:
Mark Shannon9af0e472020-01-14 10:12:45 +00002501 ADDOP_I(c, CONTAINS_OP, 1);
2502 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002503 default:
Mark Shannon9af0e472020-01-14 10:12:45 +00002504 Py_UNREACHABLE();
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002505 }
Mark Shannon9af0e472020-01-14 10:12:45 +00002506 ADDOP_I(c, COMPARE_OP, cmp);
2507 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002508}
2509
Mark Shannon9af0e472020-01-14 10:12:45 +00002510
2511
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002512static int
2513compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2514{
2515 switch (e->kind) {
2516 case UnaryOp_kind:
2517 if (e->v.UnaryOp.op == Not)
2518 return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2519 /* fallback to general implementation */
2520 break;
2521 case BoolOp_kind: {
Pablo Galindoa5634c42020-09-16 19:42:00 +01002522 asdl_expr_seq *s = e->v.BoolOp.values;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002523 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2524 assert(n >= 0);
2525 int cond2 = e->v.BoolOp.op == Or;
2526 basicblock *next2 = next;
2527 if (!cond2 != !cond) {
2528 next2 = compiler_new_block(c);
2529 if (next2 == NULL)
2530 return 0;
2531 }
2532 for (i = 0; i < n; ++i) {
2533 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2534 return 0;
2535 }
2536 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2537 return 0;
2538 if (next2 != next)
2539 compiler_use_next_block(c, next2);
2540 return 1;
2541 }
2542 case IfExp_kind: {
2543 basicblock *end, *next2;
2544 end = compiler_new_block(c);
2545 if (end == NULL)
2546 return 0;
2547 next2 = compiler_new_block(c);
2548 if (next2 == NULL)
2549 return 0;
2550 if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2551 return 0;
2552 if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2553 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01002554 ADDOP_JUMP(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002555 compiler_use_next_block(c, next2);
2556 if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2557 return 0;
2558 compiler_use_next_block(c, end);
2559 return 1;
2560 }
2561 case Compare_kind: {
2562 Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2563 if (n > 0) {
Serhiy Storchaka45835252019-02-16 08:29:46 +02002564 if (!check_compare(c, e)) {
2565 return 0;
2566 }
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002567 basicblock *cleanup = compiler_new_block(c);
2568 if (cleanup == NULL)
2569 return 0;
2570 VISIT(c, expr, e->v.Compare.left);
2571 for (i = 0; i < n; i++) {
2572 VISIT(c, expr,
2573 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2574 ADDOP(c, DUP_TOP);
2575 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00002576 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01002577 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002578 NEXT_BLOCK(c);
2579 }
2580 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00002581 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Mark Shannon582aaf12020-08-04 17:30:11 +01002582 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002583 basicblock *end = compiler_new_block(c);
2584 if (end == NULL)
2585 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01002586 ADDOP_JUMP(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002587 compiler_use_next_block(c, cleanup);
2588 ADDOP(c, POP_TOP);
2589 if (!cond) {
Mark Shannon582aaf12020-08-04 17:30:11 +01002590 ADDOP_JUMP(c, JUMP_FORWARD, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002591 }
2592 compiler_use_next_block(c, end);
2593 return 1;
2594 }
2595 /* fallback to general implementation */
2596 break;
2597 }
2598 default:
2599 /* fallback to general implementation */
2600 break;
2601 }
2602
2603 /* general implementation */
2604 VISIT(c, expr, e);
Mark Shannon582aaf12020-08-04 17:30:11 +01002605 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002606 return 1;
2607}
2608
2609static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002610compiler_ifexp(struct compiler *c, expr_ty e)
2611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002612 basicblock *end, *next;
2613
2614 assert(e->kind == IfExp_kind);
2615 end = compiler_new_block(c);
2616 if (end == NULL)
2617 return 0;
2618 next = compiler_new_block(c);
2619 if (next == NULL)
2620 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002621 if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2622 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002623 VISIT(c, expr, e->v.IfExp.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002624 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002625 compiler_use_next_block(c, next);
2626 VISIT(c, expr, e->v.IfExp.orelse);
2627 compiler_use_next_block(c, end);
2628 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002629}
2630
2631static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002632compiler_lambda(struct compiler *c, expr_ty e)
2633{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002634 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002635 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002636 static identifier name;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002637 Py_ssize_t funcflags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002638 arguments_ty args = e->v.Lambda.args;
2639 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002640
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002641 if (!compiler_check_debug_args(c, args))
2642 return 0;
2643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002644 if (!name) {
2645 name = PyUnicode_InternFromString("<lambda>");
2646 if (!name)
2647 return 0;
2648 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002649
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002650 funcflags = compiler_default_arguments(c, args);
2651 if (funcflags == -1) {
2652 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002654
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002655 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002656 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002657 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002659 /* Make None the first constant, so the lambda can't have a
2660 docstring. */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002661 if (compiler_add_const(c, Py_None) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002662 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002665 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2667 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2668 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002669 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002670 }
2671 else {
2672 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002673 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002675 qualname = c->u->u_qualname;
2676 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002678 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002680
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002681 compiler_make_closure(c, co, funcflags, qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002682 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 Py_DECREF(co);
2684
2685 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002686}
2687
2688static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002689compiler_if(struct compiler *c, stmt_ty s)
2690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002691 basicblock *end, *next;
2692 int constant;
2693 assert(s->kind == If_kind);
2694 end = compiler_new_block(c);
2695 if (end == NULL)
2696 return 0;
2697
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02002698 constant = expr_constant(s->v.If.test);
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002699 /* constant = 0: "if 0"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 * constant = 1: "if 1", "if 2", ...
2701 * constant = -1: rest */
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002702 if (constant == 0) {
2703 BEGIN_DO_NOT_EMIT_BYTECODE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002704 VISIT_SEQ(c, stmt, s->v.If.body);
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002705 END_DO_NOT_EMIT_BYTECODE
2706 if (s->v.If.orelse) {
2707 VISIT_SEQ(c, stmt, s->v.If.orelse);
2708 }
2709 } else if (constant == 1) {
2710 VISIT_SEQ(c, stmt, s->v.If.body);
2711 if (s->v.If.orelse) {
2712 BEGIN_DO_NOT_EMIT_BYTECODE
2713 VISIT_SEQ(c, stmt, s->v.If.orelse);
2714 END_DO_NOT_EMIT_BYTECODE
2715 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 } else {
Antoine Pitroue7811fc2014-09-18 03:06:50 +02002717 if (asdl_seq_LEN(s->v.If.orelse)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002718 next = compiler_new_block(c);
2719 if (next == NULL)
2720 return 0;
2721 }
Mark Shannonfee55262019-11-21 09:11:43 +00002722 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002723 next = end;
Mark Shannonfee55262019-11-21 09:11:43 +00002724 }
2725 if (!compiler_jump_if(c, s->v.If.test, next, 0)) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002726 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002727 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002728 VISIT_SEQ(c, stmt, s->v.If.body);
Antoine Pitroue7811fc2014-09-18 03:06:50 +02002729 if (asdl_seq_LEN(s->v.If.orelse)) {
Mark Shannon582aaf12020-08-04 17:30:11 +01002730 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002731 compiler_use_next_block(c, next);
2732 VISIT_SEQ(c, stmt, s->v.If.orelse);
2733 }
2734 }
2735 compiler_use_next_block(c, end);
2736 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002737}
2738
2739static int
2740compiler_for(struct compiler *c, stmt_ty s)
2741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 basicblock *start, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 start = compiler_new_block(c);
2745 cleanup = compiler_new_block(c);
2746 end = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00002747 if (start == NULL || end == NULL || cleanup == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002748 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002749 }
2750 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002752 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002753 VISIT(c, expr, s->v.For.iter);
2754 ADDOP(c, GET_ITER);
2755 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01002756 ADDOP_JUMP(c, FOR_ITER, cleanup);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 VISIT(c, expr, s->v.For.target);
2758 VISIT_SEQ(c, stmt, s->v.For.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002759 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 compiler_use_next_block(c, cleanup);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002761
2762 compiler_pop_fblock(c, FOR_LOOP, start);
2763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002764 VISIT_SEQ(c, stmt, s->v.For.orelse);
2765 compiler_use_next_block(c, end);
2766 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002767}
2768
Yury Selivanov75445082015-05-11 22:57:16 -04002769
2770static int
2771compiler_async_for(struct compiler *c, stmt_ty s)
2772{
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002773 basicblock *start, *except, *end;
Pablo Galindo90235812020-03-15 04:29:22 +00002774 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07002775 c->u->u_ste->ste_coroutine = 1;
2776 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
Zsolt Dollensteine2396502018-04-27 08:58:56 -07002777 return compiler_error(c, "'async for' outside async function");
2778 }
2779
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002780 start = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002781 except = compiler_new_block(c);
2782 end = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002783
Mark Shannonfee55262019-11-21 09:11:43 +00002784 if (start == NULL || except == NULL || end == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002785 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002786 }
Yury Selivanov75445082015-05-11 22:57:16 -04002787 VISIT(c, expr, s->v.AsyncFor.iter);
2788 ADDOP(c, GET_AITER);
Yury Selivanov75445082015-05-11 22:57:16 -04002789
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002790 compiler_use_next_block(c, start);
Mark Shannonfee55262019-11-21 09:11:43 +00002791 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002792 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002793 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002794 /* SETUP_FINALLY to guard the __anext__ call */
Mark Shannon582aaf12020-08-04 17:30:11 +01002795 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov75445082015-05-11 22:57:16 -04002796 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002797 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04002798 ADDOP(c, YIELD_FROM);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002799 ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */
Yury Selivanov75445082015-05-11 22:57:16 -04002800
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002801 /* Success block for __anext__ */
2802 VISIT(c, expr, s->v.AsyncFor.target);
2803 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002804 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002805
2806 compiler_pop_fblock(c, FOR_LOOP, start);
Yury Selivanov75445082015-05-11 22:57:16 -04002807
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002808 /* Except block for __anext__ */
Yury Selivanov75445082015-05-11 22:57:16 -04002809 compiler_use_next_block(c, except);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002810 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov75445082015-05-11 22:57:16 -04002811
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002812 /* `else` block */
Yury Selivanov75445082015-05-11 22:57:16 -04002813 VISIT_SEQ(c, stmt, s->v.For.orelse);
2814
2815 compiler_use_next_block(c, end);
2816
2817 return 1;
2818}
2819
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002820static int
2821compiler_while(struct compiler *c, stmt_ty s)
2822{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 basicblock *loop, *orelse, *end, *anchor = NULL;
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02002824 int constant = expr_constant(s->v.While.test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 if (constant == 0) {
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002827 BEGIN_DO_NOT_EMIT_BYTECODE
Pablo Galindo6c3e66a2019-10-30 11:53:26 +00002828 // Push a dummy block so the VISIT_SEQ knows that we are
2829 // inside a while loop so it can correctly evaluate syntax
2830 // errors.
Mark Shannonfee55262019-11-21 09:11:43 +00002831 if (!compiler_push_fblock(c, WHILE_LOOP, NULL, NULL, NULL)) {
Pablo Galindo6c3e66a2019-10-30 11:53:26 +00002832 return 0;
2833 }
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002834 VISIT_SEQ(c, stmt, s->v.While.body);
Pablo Galindo6c3e66a2019-10-30 11:53:26 +00002835 // Remove the dummy block now that is not needed.
2836 compiler_pop_fblock(c, WHILE_LOOP, NULL);
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002837 END_DO_NOT_EMIT_BYTECODE
2838 if (s->v.While.orelse) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002839 VISIT_SEQ(c, stmt, s->v.While.orelse);
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002840 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002841 return 1;
2842 }
2843 loop = compiler_new_block(c);
2844 end = compiler_new_block(c);
2845 if (constant == -1) {
2846 anchor = compiler_new_block(c);
2847 if (anchor == NULL)
2848 return 0;
2849 }
2850 if (loop == NULL || end == NULL)
2851 return 0;
2852 if (s->v.While.orelse) {
2853 orelse = compiler_new_block(c);
2854 if (orelse == NULL)
2855 return 0;
2856 }
2857 else
2858 orelse = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002860 compiler_use_next_block(c, loop);
Mark Shannonfee55262019-11-21 09:11:43 +00002861 if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 return 0;
2863 if (constant == -1) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002864 if (!compiler_jump_if(c, s->v.While.test, anchor, 0))
2865 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 }
2867 VISIT_SEQ(c, stmt, s->v.While.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002868 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 /* XXX should the two POP instructions be in a separate block
2871 if there is no else clause ?
2872 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002873
Benjamin Peterson3cda0ed2014-12-13 16:06:19 -05002874 if (constant == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 compiler_use_next_block(c, anchor);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002876 compiler_pop_fblock(c, WHILE_LOOP, loop);
2877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 if (orelse != NULL) /* what if orelse is just pass? */
2879 VISIT_SEQ(c, stmt, s->v.While.orelse);
2880 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002883}
2884
2885static int
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002886compiler_return(struct compiler *c, stmt_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002887{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002888 int preserve_tos = ((s->v.Return.value != NULL) &&
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002889 (s->v.Return.value->kind != Constant_kind));
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002890 if (c->u->u_ste->ste_type != FunctionBlock)
2891 return compiler_error(c, "'return' outside function");
2892 if (s->v.Return.value != NULL &&
2893 c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2894 {
2895 return compiler_error(
2896 c, "'return' with value in async generator");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002898 if (preserve_tos) {
2899 VISIT(c, expr, s->v.Return.value);
2900 }
Mark Shannonfee55262019-11-21 09:11:43 +00002901 if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL))
2902 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002903 if (s->v.Return.value == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002904 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002905 }
2906 else if (!preserve_tos) {
2907 VISIT(c, expr, s->v.Return.value);
2908 }
2909 ADDOP(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002912}
2913
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002914static int
2915compiler_break(struct compiler *c)
2916{
Mark Shannonfee55262019-11-21 09:11:43 +00002917 struct fblockinfo *loop = NULL;
2918 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
2919 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002920 }
Mark Shannonfee55262019-11-21 09:11:43 +00002921 if (loop == NULL) {
2922 return compiler_error(c, "'break' outside loop");
2923 }
2924 if (!compiler_unwind_fblock(c, loop, 0)) {
2925 return 0;
2926 }
Mark Shannon582aaf12020-08-04 17:30:11 +01002927 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit);
Mark Shannonfee55262019-11-21 09:11:43 +00002928 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002929}
2930
2931static int
2932compiler_continue(struct compiler *c)
2933{
Mark Shannonfee55262019-11-21 09:11:43 +00002934 struct fblockinfo *loop = NULL;
2935 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
2936 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002937 }
Mark Shannonfee55262019-11-21 09:11:43 +00002938 if (loop == NULL) {
2939 return compiler_error(c, "'continue' not properly in loop");
2940 }
Mark Shannon582aaf12020-08-04 17:30:11 +01002941 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block);
Mark Shannonfee55262019-11-21 09:11:43 +00002942 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002943}
2944
2945
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002946/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002947
2948 SETUP_FINALLY L
2949 <code for body>
2950 POP_BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00002951 <code for finalbody>
2952 JUMP E
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002953 L:
2954 <code for finalbody>
Mark Shannonfee55262019-11-21 09:11:43 +00002955 E:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002956
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002957 The special instructions use the block stack. Each block
2958 stack entry contains the instruction that created it (here
2959 SETUP_FINALLY), the level of the value stack at the time the
2960 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002962 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002963 Pushes the current value stack level and the label
2964 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002965 POP_BLOCK:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002966 Pops en entry from the block stack.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002967
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002968 The block stack is unwound when an exception is raised:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002969 when a SETUP_FINALLY entry is found, the raised and the caught
2970 exceptions are pushed onto the value stack (and the exception
2971 condition is cleared), and the interpreter jumps to the label
2972 gotten from the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002973*/
2974
2975static int
2976compiler_try_finally(struct compiler *c, stmt_ty s)
2977{
Mark Shannonfee55262019-11-21 09:11:43 +00002978 basicblock *body, *end, *exit;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002980 body = compiler_new_block(c);
2981 end = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00002982 exit = compiler_new_block(c);
2983 if (body == NULL || end == NULL || exit == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002984 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002985
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002986 /* `try` block */
Mark Shannon582aaf12020-08-04 17:30:11 +01002987 ADDOP_JUMP(c, SETUP_FINALLY, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002988 compiler_use_next_block(c, body);
Mark Shannonfee55262019-11-21 09:11:43 +00002989 if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002991 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
2992 if (!compiler_try_except(c, s))
2993 return 0;
2994 }
2995 else {
2996 VISIT_SEQ(c, stmt, s->v.Try.body);
2997 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 ADDOP(c, POP_BLOCK);
Mark Shannonfee55262019-11-21 09:11:43 +00002999 compiler_pop_fblock(c, FINALLY_TRY, body);
3000 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Mark Shannon582aaf12020-08-04 17:30:11 +01003001 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00003002 /* `finally` block */
3003 compiler_use_next_block(c, end);
3004 if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL))
3005 return 0;
3006 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3007 compiler_pop_fblock(c, FINALLY_END, end);
3008 ADDOP(c, RERAISE);
3009 compiler_use_next_block(c, exit);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003011}
3012
3013/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003014 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003015 (The contents of the value stack is shown in [], with the top
3016 at the right; 'tb' is trace-back info, 'val' the exception's
3017 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018
3019 Value stack Label Instruction Argument
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003020 [] SETUP_FINALLY L1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003021 [] <code for S>
3022 [] POP_BLOCK
3023 [] JUMP_FORWARD L0
3024
3025 [tb, val, exc] L1: DUP )
3026 [tb, val, exc, exc] <evaluate E1> )
Mark Shannon9af0e472020-01-14 10:12:45 +00003027 [tb, val, exc, exc, E1] JUMP_IF_NOT_EXC_MATCH L2 ) only if E1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003028 [tb, val, exc] POP
3029 [tb, val] <assign to V1> (or POP if no V1)
3030 [tb] POP
3031 [] <code for S1>
3032 JUMP_FORWARD L0
3033
3034 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003035 .............................etc.......................
3036
Mark Shannonfee55262019-11-21 09:11:43 +00003037 [tb, val, exc] Ln+1: RERAISE # re-raise exception
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003038
3039 [] L0: <next statement>
3040
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003041 Of course, parts are not generated if Vi or Ei is not present.
3042*/
3043static int
3044compiler_try_except(struct compiler *c, stmt_ty s)
3045{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003046 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003047 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 body = compiler_new_block(c);
3050 except = compiler_new_block(c);
3051 orelse = compiler_new_block(c);
3052 end = compiler_new_block(c);
3053 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
3054 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01003055 ADDOP_JUMP(c, SETUP_FINALLY, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003056 compiler_use_next_block(c, body);
Mark Shannon02d126a2020-09-25 14:04:19 +01003057 if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003059 VISIT_SEQ(c, stmt, s->v.Try.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003060 ADDOP(c, POP_BLOCK);
Mark Shannon02d126a2020-09-25 14:04:19 +01003061 compiler_pop_fblock(c, TRY_EXCEPT, body);
Mark Shannon582aaf12020-08-04 17:30:11 +01003062 ADDOP_JUMP(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003063 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003064 compiler_use_next_block(c, except);
Mark Shannon02d126a2020-09-25 14:04:19 +01003065 /* Runtime will push a block here, so we need to account for that */
3066 if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL))
3067 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003068 for (i = 0; i < n; i++) {
3069 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003070 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003071 if (!handler->v.ExceptHandler.type && i < n-1)
3072 return compiler_error(c, "default 'except:' must be last");
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003073 SET_LOC(c, handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003074 except = compiler_new_block(c);
3075 if (except == NULL)
3076 return 0;
3077 if (handler->v.ExceptHandler.type) {
3078 ADDOP(c, DUP_TOP);
3079 VISIT(c, expr, handler->v.ExceptHandler.type);
Mark Shannon582aaf12020-08-04 17:30:11 +01003080 ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081 }
3082 ADDOP(c, POP_TOP);
3083 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003084 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00003085
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003086 cleanup_end = compiler_new_block(c);
3087 cleanup_body = compiler_new_block(c);
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003088 if (cleanup_end == NULL || cleanup_body == NULL) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003089 return 0;
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003090 }
Guido van Rossumb940e112007-01-10 16:19:56 +00003091
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003092 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3093 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003095 /*
3096 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003097 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003098 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003099 try:
3100 # body
3101 finally:
Chris Angelicoad098b62019-05-21 23:34:19 +10003102 name = None # in case body contains "del name"
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003103 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003104 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003105
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003106 /* second try: */
Mark Shannon582aaf12020-08-04 17:30:11 +01003107 ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003108 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003109 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003110 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003111
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003112 /* second # body */
3113 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003114 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003115 ADDOP(c, POP_BLOCK);
3116 ADDOP(c, POP_EXCEPT);
3117 /* name = None; del name */
3118 ADDOP_LOAD_CONST(c, Py_None);
3119 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3120 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Mark Shannon582aaf12020-08-04 17:30:11 +01003121 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122
Mark Shannonfee55262019-11-21 09:11:43 +00003123 /* except: */
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003124 compiler_use_next_block(c, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003125
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003126 /* name = None; del name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003127 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003128 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003129 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003130
Mark Shannonfee55262019-11-21 09:11:43 +00003131 ADDOP(c, RERAISE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003132 }
3133 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003134 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003135
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003136 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05003137 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003138 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139
Guido van Rossumb940e112007-01-10 16:19:56 +00003140 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003141 ADDOP(c, POP_TOP);
3142 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003143 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003144 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003146 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003147 ADDOP(c, POP_EXCEPT);
Mark Shannon582aaf12020-08-04 17:30:11 +01003148 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003149 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003150 compiler_use_next_block(c, except);
3151 }
Mark Shannon02d126a2020-09-25 14:04:19 +01003152 compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL);
Mark Shannonfee55262019-11-21 09:11:43 +00003153 ADDOP(c, RERAISE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003155 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003156 compiler_use_next_block(c, end);
3157 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003158}
3159
3160static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003161compiler_try(struct compiler *c, stmt_ty s) {
3162 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
3163 return compiler_try_finally(c, s);
3164 else
3165 return compiler_try_except(c, s);
3166}
3167
3168
3169static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003170compiler_import_as(struct compiler *c, identifier name, identifier asname)
3171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003172 /* The IMPORT_NAME opcode was already generated. This function
3173 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003175 If there is a dot in name, we need to split it and emit a
Serhiy Storchakaf93234b2017-05-09 22:31:05 +03003176 IMPORT_FROM for each name.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003177 */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003178 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
3179 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003180 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003181 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003182 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003183 /* Consume the base module name to get the first attribute */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003184 while (1) {
3185 Py_ssize_t pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003186 PyObject *attr;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003187 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003188 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003189 return 0;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003190 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003191 if (!attr)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003192 return 0;
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003193 ADDOP_N(c, IMPORT_FROM, attr, names);
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003194 if (dot == -1) {
3195 break;
3196 }
3197 ADDOP(c, ROT_TWO);
3198 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003199 }
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003200 if (!compiler_nameop(c, asname, Store)) {
3201 return 0;
3202 }
3203 ADDOP(c, POP_TOP);
3204 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003205 }
3206 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003207}
3208
3209static int
3210compiler_import(struct compiler *c, stmt_ty s)
3211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003212 /* The Import node stores a module name like a.b.c as a single
3213 string. This is convenient for all cases except
3214 import a.b.c as d
3215 where we need to parse that string to extract the individual
3216 module names.
3217 XXX Perhaps change the representation to make this case simpler?
3218 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01003219 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003221 for (i = 0; i < n; i++) {
3222 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
3223 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003224
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003225 ADDOP_LOAD_CONST(c, _PyLong_Zero);
3226 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003229 if (alias->asname) {
3230 r = compiler_import_as(c, alias->name, alias->asname);
3231 if (!r)
3232 return r;
3233 }
3234 else {
3235 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003236 Py_ssize_t dot = PyUnicode_FindChar(
3237 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02003238 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003239 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02003240 if (tmp == NULL)
3241 return 0;
3242 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003243 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003244 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003245 Py_DECREF(tmp);
3246 }
3247 if (!r)
3248 return r;
3249 }
3250 }
3251 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003252}
3253
3254static int
3255compiler_from_import(struct compiler *c, stmt_ty s)
3256{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003257 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003258 PyObject *names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003259 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 if (!empty_string) {
3262 empty_string = PyUnicode_FromString("");
3263 if (!empty_string)
3264 return 0;
3265 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003266
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003267 ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02003268
3269 names = PyTuple_New(n);
3270 if (!names)
3271 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003273 /* build up the names */
3274 for (i = 0; i < n; i++) {
3275 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3276 Py_INCREF(alias->name);
3277 PyTuple_SET_ITEM(names, i, alias->name);
3278 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003280 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003281 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003282 Py_DECREF(names);
3283 return compiler_error(c, "from __future__ imports must occur "
3284 "at the beginning of the file");
3285 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003286 ADDOP_LOAD_CONST_NEW(c, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003288 if (s->v.ImportFrom.module) {
3289 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
3290 }
3291 else {
3292 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
3293 }
3294 for (i = 0; i < n; i++) {
3295 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3296 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003297
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003298 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003299 assert(n == 1);
3300 ADDOP(c, IMPORT_STAR);
3301 return 1;
3302 }
3303
3304 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
3305 store_name = alias->name;
3306 if (alias->asname)
3307 store_name = alias->asname;
3308
3309 if (!compiler_nameop(c, store_name, Store)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003310 return 0;
3311 }
3312 }
3313 /* remove imported module */
3314 ADDOP(c, POP_TOP);
3315 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003316}
3317
3318static int
3319compiler_assert(struct compiler *c, stmt_ty s)
3320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003321 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003322
Georg Brandl8334fd92010-12-04 10:26:46 +00003323 if (c->c_optimize)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003325 if (s->v.Assert.test->kind == Tuple_kind &&
Serhiy Storchakad31e7732018-10-21 10:09:39 +03003326 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0)
3327 {
3328 if (!compiler_warn(c, "assertion is always true, "
3329 "perhaps remove parentheses?"))
3330 {
Victor Stinner14e461d2013-08-26 22:28:21 +02003331 return 0;
3332 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003333 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003334 end = compiler_new_block(c);
3335 if (end == NULL)
3336 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003337 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
3338 return 0;
Zackery Spytzce6a0702019-08-25 03:44:09 -06003339 ADDOP(c, LOAD_ASSERTION_ERROR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003340 if (s->v.Assert.msg) {
3341 VISIT(c, expr, s->v.Assert.msg);
3342 ADDOP_I(c, CALL_FUNCTION, 1);
3343 }
3344 ADDOP_I(c, RAISE_VARARGS, 1);
3345 compiler_use_next_block(c, end);
3346 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003347}
3348
3349static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003350compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3351{
3352 if (c->c_interactive && c->c_nestlevel <= 1) {
3353 VISIT(c, expr, value);
3354 ADDOP(c, PRINT_EXPR);
3355 return 1;
3356 }
3357
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003358 if (value->kind == Constant_kind) {
Victor Stinner15a30952016-02-08 22:45:06 +01003359 /* ignore constant statement */
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003360 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003361 }
3362
3363 VISIT(c, expr, value);
3364 ADDOP(c, POP_TOP);
3365 return 1;
3366}
3367
3368static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003369compiler_visit_stmt(struct compiler *c, stmt_ty s)
3370{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003371 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003373 /* Always assign a lineno to the next instruction for a stmt. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003374 SET_LOC(c, s);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003376 switch (s->kind) {
3377 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04003378 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003379 case ClassDef_kind:
3380 return compiler_class(c, s);
3381 case Return_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003382 return compiler_return(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003383 case Delete_kind:
3384 VISIT_SEQ(c, expr, s->v.Delete.targets)
3385 break;
3386 case Assign_kind:
3387 n = asdl_seq_LEN(s->v.Assign.targets);
3388 VISIT(c, expr, s->v.Assign.value);
3389 for (i = 0; i < n; i++) {
3390 if (i < n - 1)
3391 ADDOP(c, DUP_TOP);
3392 VISIT(c, expr,
3393 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3394 }
3395 break;
3396 case AugAssign_kind:
3397 return compiler_augassign(c, s);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003398 case AnnAssign_kind:
3399 return compiler_annassign(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003400 case For_kind:
3401 return compiler_for(c, s);
3402 case While_kind:
3403 return compiler_while(c, s);
3404 case If_kind:
3405 return compiler_if(c, s);
3406 case Raise_kind:
3407 n = 0;
3408 if (s->v.Raise.exc) {
3409 VISIT(c, expr, s->v.Raise.exc);
3410 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003411 if (s->v.Raise.cause) {
3412 VISIT(c, expr, s->v.Raise.cause);
3413 n++;
3414 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003415 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01003416 ADDOP_I(c, RAISE_VARARGS, (int)n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003417 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003418 case Try_kind:
3419 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003420 case Assert_kind:
3421 return compiler_assert(c, s);
3422 case Import_kind:
3423 return compiler_import(c, s);
3424 case ImportFrom_kind:
3425 return compiler_from_import(c, s);
3426 case Global_kind:
3427 case Nonlocal_kind:
3428 break;
3429 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003430 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 case Pass_kind:
3432 break;
3433 case Break_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003434 return compiler_break(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 case Continue_kind:
3436 return compiler_continue(c);
3437 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003438 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04003439 case AsyncFunctionDef_kind:
3440 return compiler_function(c, s, 1);
3441 case AsyncWith_kind:
3442 return compiler_async_with(c, s, 0);
3443 case AsyncFor_kind:
3444 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445 }
Yury Selivanov75445082015-05-11 22:57:16 -04003446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003447 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003448}
3449
3450static int
3451unaryop(unaryop_ty op)
3452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003453 switch (op) {
3454 case Invert:
3455 return UNARY_INVERT;
3456 case Not:
3457 return UNARY_NOT;
3458 case UAdd:
3459 return UNARY_POSITIVE;
3460 case USub:
3461 return UNARY_NEGATIVE;
3462 default:
3463 PyErr_Format(PyExc_SystemError,
3464 "unary op %d should not be possible", op);
3465 return 0;
3466 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003467}
3468
3469static int
Andy Lester76d58772020-03-10 21:18:12 -05003470binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003472 switch (op) {
3473 case Add:
3474 return BINARY_ADD;
3475 case Sub:
3476 return BINARY_SUBTRACT;
3477 case Mult:
3478 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003479 case MatMult:
3480 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 case Div:
3482 return BINARY_TRUE_DIVIDE;
3483 case Mod:
3484 return BINARY_MODULO;
3485 case Pow:
3486 return BINARY_POWER;
3487 case LShift:
3488 return BINARY_LSHIFT;
3489 case RShift:
3490 return BINARY_RSHIFT;
3491 case BitOr:
3492 return BINARY_OR;
3493 case BitXor:
3494 return BINARY_XOR;
3495 case BitAnd:
3496 return BINARY_AND;
3497 case FloorDiv:
3498 return BINARY_FLOOR_DIVIDE;
3499 default:
3500 PyErr_Format(PyExc_SystemError,
3501 "binary op %d should not be possible", op);
3502 return 0;
3503 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003504}
3505
3506static int
Andy Lester76d58772020-03-10 21:18:12 -05003507inplace_binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003509 switch (op) {
3510 case Add:
3511 return INPLACE_ADD;
3512 case Sub:
3513 return INPLACE_SUBTRACT;
3514 case Mult:
3515 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003516 case MatMult:
3517 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003518 case Div:
3519 return INPLACE_TRUE_DIVIDE;
3520 case Mod:
3521 return INPLACE_MODULO;
3522 case Pow:
3523 return INPLACE_POWER;
3524 case LShift:
3525 return INPLACE_LSHIFT;
3526 case RShift:
3527 return INPLACE_RSHIFT;
3528 case BitOr:
3529 return INPLACE_OR;
3530 case BitXor:
3531 return INPLACE_XOR;
3532 case BitAnd:
3533 return INPLACE_AND;
3534 case FloorDiv:
3535 return INPLACE_FLOOR_DIVIDE;
3536 default:
3537 PyErr_Format(PyExc_SystemError,
3538 "inplace binary op %d should not be possible", op);
3539 return 0;
3540 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003541}
3542
3543static int
3544compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3545{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003546 int op, scope;
3547 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003550 PyObject *dict = c->u->u_names;
3551 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003552
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003553 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3554 !_PyUnicode_EqualToASCIIString(name, "True") &&
3555 !_PyUnicode_EqualToASCIIString(name, "False"));
Benjamin Peterson70b224d2012-12-06 17:49:58 -05003556
Pablo Galindoc5fc1562020-04-22 23:29:27 +01003557 if (forbidden_name(c, name, ctx))
3558 return 0;
3559
Serhiy Storchakabd6ec4d2017-12-18 14:29:12 +02003560 mangled = _Py_Mangle(c->u->u_private, name);
3561 if (!mangled)
3562 return 0;
3563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003564 op = 0;
3565 optype = OP_NAME;
3566 scope = PyST_GetScope(c->u->u_ste, mangled);
3567 switch (scope) {
3568 case FREE:
3569 dict = c->u->u_freevars;
3570 optype = OP_DEREF;
3571 break;
3572 case CELL:
3573 dict = c->u->u_cellvars;
3574 optype = OP_DEREF;
3575 break;
3576 case LOCAL:
3577 if (c->u->u_ste->ste_type == FunctionBlock)
3578 optype = OP_FAST;
3579 break;
3580 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04003581 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003582 optype = OP_GLOBAL;
3583 break;
3584 case GLOBAL_EXPLICIT:
3585 optype = OP_GLOBAL;
3586 break;
3587 default:
3588 /* scope can be 0 */
3589 break;
3590 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003592 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003593 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003595 switch (optype) {
3596 case OP_DEREF:
3597 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003598 case Load:
3599 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3600 break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003601 case Store: op = STORE_DEREF; break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003602 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003603 }
3604 break;
3605 case OP_FAST:
3606 switch (ctx) {
3607 case Load: op = LOAD_FAST; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003608 case Store: op = STORE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003609 case Del: op = DELETE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 }
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003611 ADDOP_N(c, op, mangled, varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 return 1;
3613 case OP_GLOBAL:
3614 switch (ctx) {
3615 case Load: op = LOAD_GLOBAL; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003616 case Store: op = STORE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003617 case Del: op = DELETE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003618 }
3619 break;
3620 case OP_NAME:
3621 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00003622 case Load: op = LOAD_NAME; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003623 case Store: op = STORE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003624 case Del: op = DELETE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003625 }
3626 break;
3627 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003629 assert(op);
Andy Lester76d58772020-03-10 21:18:12 -05003630 arg = compiler_add_o(dict, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003631 Py_DECREF(mangled);
3632 if (arg < 0)
3633 return 0;
3634 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003635}
3636
3637static int
3638compiler_boolop(struct compiler *c, expr_ty e)
3639{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003640 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003641 int jumpi;
3642 Py_ssize_t i, n;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003643 asdl_expr_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003645 assert(e->kind == BoolOp_kind);
3646 if (e->v.BoolOp.op == And)
3647 jumpi = JUMP_IF_FALSE_OR_POP;
3648 else
3649 jumpi = JUMP_IF_TRUE_OR_POP;
3650 end = compiler_new_block(c);
3651 if (end == NULL)
3652 return 0;
3653 s = e->v.BoolOp.values;
3654 n = asdl_seq_LEN(s) - 1;
3655 assert(n >= 0);
3656 for (i = 0; i < n; ++i) {
3657 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003658 ADDOP_JUMP(c, jumpi, end);
Mark Shannon6e8128f2020-07-30 10:03:00 +01003659 basicblock *next = compiler_new_block(c);
3660 if (next == NULL) {
3661 return 0;
3662 }
3663 compiler_use_next_block(c, next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003664 }
3665 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3666 compiler_use_next_block(c, end);
3667 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003668}
3669
3670static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003671starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
Mark Shannon13bc1392020-01-23 09:25:17 +00003672 int build, int add, int extend, int tuple)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003673{
3674 Py_ssize_t n = asdl_seq_LEN(elts);
Mark Shannon13bc1392020-01-23 09:25:17 +00003675 Py_ssize_t i, seen_star = 0;
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003676 if (n > 2 && are_all_items_const(elts, 0, n)) {
3677 PyObject *folded = PyTuple_New(n);
3678 if (folded == NULL) {
3679 return 0;
3680 }
3681 PyObject *val;
3682 for (i = 0; i < n; i++) {
3683 val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
3684 Py_INCREF(val);
3685 PyTuple_SET_ITEM(folded, i, val);
3686 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003687 if (tuple) {
3688 ADDOP_LOAD_CONST_NEW(c, folded);
3689 } else {
3690 if (add == SET_ADD) {
3691 Py_SETREF(folded, PyFrozenSet_New(folded));
3692 if (folded == NULL) {
3693 return 0;
3694 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003695 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003696 ADDOP_I(c, build, pushed);
3697 ADDOP_LOAD_CONST_NEW(c, folded);
3698 ADDOP_I(c, extend, 1);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003699 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003700 return 1;
3701 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003702
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003703 for (i = 0; i < n; i++) {
3704 expr_ty elt = asdl_seq_GET(elts, i);
3705 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003706 seen_star = 1;
3707 }
3708 }
3709 if (seen_star) {
3710 seen_star = 0;
3711 for (i = 0; i < n; i++) {
3712 expr_ty elt = asdl_seq_GET(elts, i);
3713 if (elt->kind == Starred_kind) {
3714 if (seen_star == 0) {
3715 ADDOP_I(c, build, i+pushed);
3716 seen_star = 1;
3717 }
3718 VISIT(c, expr, elt->v.Starred.value);
3719 ADDOP_I(c, extend, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003720 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003721 else {
3722 VISIT(c, expr, elt);
3723 if (seen_star) {
3724 ADDOP_I(c, add, 1);
3725 }
3726 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003727 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003728 assert(seen_star);
3729 if (tuple) {
3730 ADDOP(c, LIST_TO_TUPLE);
3731 }
3732 }
3733 else {
3734 for (i = 0; i < n; i++) {
3735 expr_ty elt = asdl_seq_GET(elts, i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003736 VISIT(c, expr, elt);
Mark Shannon13bc1392020-01-23 09:25:17 +00003737 }
3738 if (tuple) {
3739 ADDOP_I(c, BUILD_TUPLE, n+pushed);
3740 } else {
3741 ADDOP_I(c, build, n+pushed);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003742 }
3743 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003744 return 1;
3745}
3746
3747static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003748assignment_helper(struct compiler *c, asdl_expr_seq *elts)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003749{
3750 Py_ssize_t n = asdl_seq_LEN(elts);
3751 Py_ssize_t i;
3752 int seen_star = 0;
3753 for (i = 0; i < n; i++) {
3754 expr_ty elt = asdl_seq_GET(elts, i);
3755 if (elt->kind == Starred_kind && !seen_star) {
3756 if ((i >= (1 << 8)) ||
3757 (n-i-1 >= (INT_MAX >> 8)))
3758 return compiler_error(c,
3759 "too many expressions in "
3760 "star-unpacking assignment");
3761 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3762 seen_star = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003763 }
3764 else if (elt->kind == Starred_kind) {
3765 return compiler_error(c,
Furkan Öndercb6534e2020-03-26 04:54:31 +03003766 "multiple starred expressions in assignment");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003767 }
3768 }
3769 if (!seen_star) {
3770 ADDOP_I(c, UNPACK_SEQUENCE, n);
3771 }
Brandt Bucherd5aa2e92020-03-07 19:44:18 -08003772 for (i = 0; i < n; i++) {
3773 expr_ty elt = asdl_seq_GET(elts, i);
3774 VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value);
3775 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003776 return 1;
3777}
3778
3779static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003780compiler_list(struct compiler *c, expr_ty e)
3781{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003782 asdl_expr_seq *elts = e->v.List.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003783 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003784 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003785 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003786 else if (e->v.List.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003787 return starunpack_helper(c, elts, 0, BUILD_LIST,
3788 LIST_APPEND, LIST_EXTEND, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003789 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003790 else
3791 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003792 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003793}
3794
3795static int
3796compiler_tuple(struct compiler *c, expr_ty e)
3797{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003798 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003799 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003800 return assignment_helper(c, elts);
3801 }
3802 else if (e->v.Tuple.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003803 return starunpack_helper(c, elts, 0, BUILD_LIST,
3804 LIST_APPEND, LIST_EXTEND, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003805 }
3806 else
3807 VISIT_SEQ(c, expr, elts);
3808 return 1;
3809}
3810
3811static int
3812compiler_set(struct compiler *c, expr_ty e)
3813{
Mark Shannon13bc1392020-01-23 09:25:17 +00003814 return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET,
3815 SET_ADD, SET_UPDATE, 0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003816}
3817
3818static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003819are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003820{
3821 Py_ssize_t i;
3822 for (i = begin; i < end; i++) {
3823 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003824 if (key == NULL || key->kind != Constant_kind)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003825 return 0;
3826 }
3827 return 1;
3828}
3829
3830static int
3831compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3832{
3833 Py_ssize_t i, n = end - begin;
3834 PyObject *keys, *key;
3835 if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) {
3836 for (i = begin; i < end; i++) {
3837 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3838 }
3839 keys = PyTuple_New(n);
3840 if (keys == NULL) {
3841 return 0;
3842 }
3843 for (i = begin; i < end; i++) {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003844 key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003845 Py_INCREF(key);
3846 PyTuple_SET_ITEM(keys, i - begin, key);
3847 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003848 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003849 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3850 }
3851 else {
3852 for (i = begin; i < end; i++) {
3853 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3854 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3855 }
3856 ADDOP_I(c, BUILD_MAP, n);
3857 }
3858 return 1;
3859}
3860
3861static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003862compiler_dict(struct compiler *c, expr_ty e)
3863{
Victor Stinner976bb402016-03-23 11:36:19 +01003864 Py_ssize_t i, n, elements;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003865 int have_dict;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003866 int is_unpacking = 0;
3867 n = asdl_seq_LEN(e->v.Dict.values);
Mark Shannon8a4cd702020-01-27 09:57:45 +00003868 have_dict = 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003869 elements = 0;
3870 for (i = 0; i < n; i++) {
3871 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003872 if (is_unpacking) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003873 if (elements) {
3874 if (!compiler_subdict(c, e, i - elements, i)) {
3875 return 0;
3876 }
3877 if (have_dict) {
3878 ADDOP_I(c, DICT_UPDATE, 1);
3879 }
3880 have_dict = 1;
3881 elements = 0;
3882 }
3883 if (have_dict == 0) {
3884 ADDOP_I(c, BUILD_MAP, 0);
3885 have_dict = 1;
3886 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003887 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Mark Shannon8a4cd702020-01-27 09:57:45 +00003888 ADDOP_I(c, DICT_UPDATE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003889 }
3890 else {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003891 if (elements == 0xFFFF) {
Pablo Galindoc51db0e2020-08-13 09:48:41 +01003892 if (!compiler_subdict(c, e, i - elements, i + 1)) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003893 return 0;
3894 }
3895 if (have_dict) {
3896 ADDOP_I(c, DICT_UPDATE, 1);
3897 }
3898 have_dict = 1;
3899 elements = 0;
3900 }
3901 else {
3902 elements++;
3903 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003904 }
3905 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003906 if (elements) {
3907 if (!compiler_subdict(c, e, n - elements, n)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003908 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003909 }
3910 if (have_dict) {
3911 ADDOP_I(c, DICT_UPDATE, 1);
3912 }
3913 have_dict = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003914 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003915 if (!have_dict) {
3916 ADDOP_I(c, BUILD_MAP, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003917 }
3918 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003919}
3920
3921static int
3922compiler_compare(struct compiler *c, expr_ty e)
3923{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003924 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003925
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02003926 if (!check_compare(c, e)) {
3927 return 0;
3928 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003929 VISIT(c, expr, e->v.Compare.left);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003930 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
3931 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
3932 if (n == 0) {
3933 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Mark Shannon9af0e472020-01-14 10:12:45 +00003934 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003935 }
3936 else {
3937 basicblock *cleanup = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003938 if (cleanup == NULL)
3939 return 0;
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003940 for (i = 0; i < n; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003941 VISIT(c, expr,
3942 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003943 ADDOP(c, DUP_TOP);
3944 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00003945 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003946 ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003947 NEXT_BLOCK(c);
3948 }
3949 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00003950 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003951 basicblock *end = compiler_new_block(c);
3952 if (end == NULL)
3953 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01003954 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003955 compiler_use_next_block(c, cleanup);
3956 ADDOP(c, ROT_TWO);
3957 ADDOP(c, POP_TOP);
3958 compiler_use_next_block(c, end);
3959 }
3960 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003961}
3962
Serhiy Storchaka62e44812019-02-16 08:12:19 +02003963static PyTypeObject *
3964infer_type(expr_ty e)
3965{
3966 switch (e->kind) {
3967 case Tuple_kind:
3968 return &PyTuple_Type;
3969 case List_kind:
3970 case ListComp_kind:
3971 return &PyList_Type;
3972 case Dict_kind:
3973 case DictComp_kind:
3974 return &PyDict_Type;
3975 case Set_kind:
3976 case SetComp_kind:
3977 return &PySet_Type;
3978 case GeneratorExp_kind:
3979 return &PyGen_Type;
3980 case Lambda_kind:
3981 return &PyFunction_Type;
3982 case JoinedStr_kind:
3983 case FormattedValue_kind:
3984 return &PyUnicode_Type;
3985 case Constant_kind:
Victor Stinnera102ed72020-02-07 02:24:48 +01003986 return Py_TYPE(e->v.Constant.value);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02003987 default:
3988 return NULL;
3989 }
3990}
3991
3992static int
3993check_caller(struct compiler *c, expr_ty e)
3994{
3995 switch (e->kind) {
3996 case Constant_kind:
3997 case Tuple_kind:
3998 case List_kind:
3999 case ListComp_kind:
4000 case Dict_kind:
4001 case DictComp_kind:
4002 case Set_kind:
4003 case SetComp_kind:
4004 case GeneratorExp_kind:
4005 case JoinedStr_kind:
4006 case FormattedValue_kind:
4007 return compiler_warn(c, "'%.200s' object is not callable; "
4008 "perhaps you missed a comma?",
4009 infer_type(e)->tp_name);
4010 default:
4011 return 1;
4012 }
4013}
4014
4015static int
4016check_subscripter(struct compiler *c, expr_ty e)
4017{
4018 PyObject *v;
4019
4020 switch (e->kind) {
4021 case Constant_kind:
4022 v = e->v.Constant.value;
4023 if (!(v == Py_None || v == Py_Ellipsis ||
4024 PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
4025 PyAnySet_Check(v)))
4026 {
4027 return 1;
4028 }
4029 /* fall through */
4030 case Set_kind:
4031 case SetComp_kind:
4032 case GeneratorExp_kind:
4033 case Lambda_kind:
4034 return compiler_warn(c, "'%.200s' object is not subscriptable; "
4035 "perhaps you missed a comma?",
4036 infer_type(e)->tp_name);
4037 default:
4038 return 1;
4039 }
4040}
4041
4042static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004043check_index(struct compiler *c, expr_ty e, expr_ty s)
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004044{
4045 PyObject *v;
4046
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004047 PyTypeObject *index_type = infer_type(s);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004048 if (index_type == NULL
4049 || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
4050 || index_type == &PySlice_Type) {
4051 return 1;
4052 }
4053
4054 switch (e->kind) {
4055 case Constant_kind:
4056 v = e->v.Constant.value;
4057 if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
4058 return 1;
4059 }
4060 /* fall through */
4061 case Tuple_kind:
4062 case List_kind:
4063 case ListComp_kind:
4064 case JoinedStr_kind:
4065 case FormattedValue_kind:
4066 return compiler_warn(c, "%.200s indices must be integers or slices, "
4067 "not %.200s; "
4068 "perhaps you missed a comma?",
4069 infer_type(e)->tp_name,
4070 index_type->tp_name);
4071 default:
4072 return 1;
4073 }
4074}
4075
Zackery Spytz97f5de02019-03-22 01:30:32 -06004076// Return 1 if the method call was optimized, -1 if not, and 0 on error.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004077static int
Yury Selivanovf2392132016-12-13 19:03:51 -05004078maybe_optimize_method_call(struct compiler *c, expr_ty e)
4079{
4080 Py_ssize_t argsl, i;
4081 expr_ty meth = e->v.Call.func;
Pablo Galindoa5634c42020-09-16 19:42:00 +01004082 asdl_expr_seq *args = e->v.Call.args;
Yury Selivanovf2392132016-12-13 19:03:51 -05004083
4084 /* Check that the call node is an attribute access, and that
4085 the call doesn't have keyword parameters. */
4086 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
4087 asdl_seq_LEN(e->v.Call.keywords))
4088 return -1;
4089
4090 /* Check that there are no *varargs types of arguments. */
4091 argsl = asdl_seq_LEN(args);
4092 for (i = 0; i < argsl; i++) {
4093 expr_ty elt = asdl_seq_GET(args, i);
4094 if (elt->kind == Starred_kind) {
4095 return -1;
4096 }
4097 }
4098
4099 /* Alright, we can optimize the code. */
4100 VISIT(c, expr, meth->v.Attribute.value);
4101 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
4102 VISIT_SEQ(c, expr, e->v.Call.args);
4103 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
4104 return 1;
4105}
4106
4107static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004108validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
Zackery Spytz08050e92020-04-06 00:47:47 -06004109{
4110 Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
4111 for (Py_ssize_t i = 0; i < nkeywords; i++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004112 keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
4113 if (key->arg == NULL) {
4114 continue;
4115 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +01004116 if (forbidden_name(c, key->arg, Store)) {
4117 return -1;
4118 }
Zackery Spytz08050e92020-04-06 00:47:47 -06004119 for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004120 keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
4121 if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
4122 PyObject *msg = PyUnicode_FromFormat("keyword argument repeated: %U", key->arg);
4123 if (msg == NULL) {
4124 return -1;
4125 }
4126 c->u->u_col_offset = other->col_offset;
4127 compiler_error(c, PyUnicode_AsUTF8(msg));
4128 Py_DECREF(msg);
4129 return -1;
4130 }
4131 }
4132 }
4133 return 0;
4134}
4135
4136static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004137compiler_call(struct compiler *c, expr_ty e)
4138{
Zackery Spytz97f5de02019-03-22 01:30:32 -06004139 int ret = maybe_optimize_method_call(c, e);
4140 if (ret >= 0) {
4141 return ret;
4142 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004143 if (!check_caller(c, e->v.Call.func)) {
4144 return 0;
4145 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004146 VISIT(c, expr, e->v.Call.func);
4147 return compiler_call_helper(c, 0,
4148 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004149 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004150}
4151
Eric V. Smith235a6f02015-09-19 14:51:32 -04004152static int
4153compiler_joined_str(struct compiler *c, expr_ty e)
4154{
Eric V. Smith235a6f02015-09-19 14:51:32 -04004155 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004156 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1)
4157 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
Eric V. Smith235a6f02015-09-19 14:51:32 -04004158 return 1;
4159}
4160
Eric V. Smitha78c7952015-11-03 12:45:05 -05004161/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004162static int
4163compiler_formatted_value(struct compiler *c, expr_ty e)
4164{
Eric V. Smitha78c7952015-11-03 12:45:05 -05004165 /* Our oparg encodes 2 pieces of information: the conversion
4166 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04004167
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004168 Convert the conversion char to 3 bits:
4169 : 000 0x0 FVC_NONE The default if nothing specified.
Eric V. Smitha78c7952015-11-03 12:45:05 -05004170 !s : 001 0x1 FVC_STR
4171 !r : 010 0x2 FVC_REPR
4172 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04004173
Eric V. Smitha78c7952015-11-03 12:45:05 -05004174 next bit is whether or not we have a format spec:
4175 yes : 100 0x4
4176 no : 000 0x0
4177 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004178
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004179 int conversion = e->v.FormattedValue.conversion;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004180 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004181
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004182 /* The expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004183 VISIT(c, expr, e->v.FormattedValue.value);
4184
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004185 switch (conversion) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004186 case 's': oparg = FVC_STR; break;
4187 case 'r': oparg = FVC_REPR; break;
4188 case 'a': oparg = FVC_ASCII; break;
4189 case -1: oparg = FVC_NONE; break;
4190 default:
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004191 PyErr_Format(PyExc_SystemError,
4192 "Unrecognized conversion character %d", conversion);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004193 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004194 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004195 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004196 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004197 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004198 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004199 }
4200
Eric V. Smitha78c7952015-11-03 12:45:05 -05004201 /* And push our opcode and oparg */
4202 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004203
Eric V. Smith235a6f02015-09-19 14:51:32 -04004204 return 1;
4205}
4206
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004207static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004208compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004209{
4210 Py_ssize_t i, n = end - begin;
4211 keyword_ty kw;
4212 PyObject *keys, *key;
4213 assert(n > 0);
4214 if (n > 1) {
4215 for (i = begin; i < end; i++) {
4216 kw = asdl_seq_GET(keywords, i);
4217 VISIT(c, expr, kw->value);
4218 }
4219 keys = PyTuple_New(n);
4220 if (keys == NULL) {
4221 return 0;
4222 }
4223 for (i = begin; i < end; i++) {
4224 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
4225 Py_INCREF(key);
4226 PyTuple_SET_ITEM(keys, i - begin, key);
4227 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004228 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004229 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
4230 }
4231 else {
4232 /* a for loop only executes once */
4233 for (i = begin; i < end; i++) {
4234 kw = asdl_seq_GET(keywords, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004235 ADDOP_LOAD_CONST(c, kw->arg);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004236 VISIT(c, expr, kw->value);
4237 }
4238 ADDOP_I(c, BUILD_MAP, n);
4239 }
4240 return 1;
4241}
4242
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004243/* shared code between compiler_call and compiler_class */
4244static int
4245compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01004246 int n, /* Args already pushed */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004247 asdl_expr_seq *args,
4248 asdl_keyword_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004249{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004250 Py_ssize_t i, nseen, nelts, nkwelts;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004251
Pablo Galindo254ec782020-04-03 20:37:13 +01004252 if (validate_keywords(c, keywords) == -1) {
4253 return 0;
4254 }
4255
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004256 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004257 nkwelts = asdl_seq_LEN(keywords);
4258
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004259 for (i = 0; i < nelts; i++) {
4260 expr_ty elt = asdl_seq_GET(args, i);
4261 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00004262 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004263 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004264 }
4265 for (i = 0; i < nkwelts; i++) {
4266 keyword_ty kw = asdl_seq_GET(keywords, i);
4267 if (kw->arg == NULL) {
4268 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004269 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004270 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004271
Mark Shannon13bc1392020-01-23 09:25:17 +00004272 /* No * or ** args, so can use faster calling sequence */
4273 for (i = 0; i < nelts; i++) {
4274 expr_ty elt = asdl_seq_GET(args, i);
4275 assert(elt->kind != Starred_kind);
4276 VISIT(c, expr, elt);
4277 }
4278 if (nkwelts) {
4279 PyObject *names;
4280 VISIT_SEQ(c, keyword, keywords);
4281 names = PyTuple_New(nkwelts);
4282 if (names == NULL) {
4283 return 0;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004284 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004285 for (i = 0; i < nkwelts; i++) {
4286 keyword_ty kw = asdl_seq_GET(keywords, i);
4287 Py_INCREF(kw->arg);
4288 PyTuple_SET_ITEM(names, i, kw->arg);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004289 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004290 ADDOP_LOAD_CONST_NEW(c, names);
4291 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
4292 return 1;
4293 }
4294 else {
4295 ADDOP_I(c, CALL_FUNCTION, n + nelts);
4296 return 1;
4297 }
4298
4299ex_call:
4300
4301 /* Do positional arguments. */
4302 if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
4303 VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
4304 }
4305 else if (starunpack_helper(c, args, n, BUILD_LIST,
4306 LIST_APPEND, LIST_EXTEND, 1) == 0) {
4307 return 0;
4308 }
4309 /* Then keyword arguments */
4310 if (nkwelts) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004311 /* Has a new dict been pushed */
4312 int have_dict = 0;
Mark Shannon13bc1392020-01-23 09:25:17 +00004313
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004314 nseen = 0; /* the number of keyword arguments on the stack following */
4315 for (i = 0; i < nkwelts; i++) {
4316 keyword_ty kw = asdl_seq_GET(keywords, i);
4317 if (kw->arg == NULL) {
4318 /* A keyword argument unpacking. */
4319 if (nseen) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004320 if (!compiler_subkwargs(c, keywords, i - nseen, i)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004321 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004322 }
Mark Shannondb64f122020-06-01 10:42:42 +01004323 if (have_dict) {
4324 ADDOP_I(c, DICT_MERGE, 1);
4325 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004326 have_dict = 1;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004327 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004328 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004329 if (!have_dict) {
4330 ADDOP_I(c, BUILD_MAP, 0);
4331 have_dict = 1;
4332 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004333 VISIT(c, expr, kw->value);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004334 ADDOP_I(c, DICT_MERGE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004335 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004336 else {
4337 nseen++;
4338 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004339 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004340 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004341 /* Pack up any trailing keyword arguments. */
Mark Shannon8a4cd702020-01-27 09:57:45 +00004342 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004343 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004344 }
4345 if (have_dict) {
4346 ADDOP_I(c, DICT_MERGE, 1);
4347 }
4348 have_dict = 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004349 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004350 assert(have_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004351 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004352 ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0);
4353 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004354}
4355
Nick Coghlan650f0d02007-04-15 12:05:43 +00004356
4357/* List and set comprehensions and generator expressions work by creating a
4358 nested function to perform the actual iteration. This means that the
4359 iteration variables don't leak into the current scope.
4360 The defined function is called immediately following its definition, with the
4361 result of that call being the result of the expression.
4362 The LC/SC version returns the populated container, while the GE version is
4363 flagged in symtable.c as a generator, so it returns the generator object
4364 when the function is called.
Nick Coghlan650f0d02007-04-15 12:05:43 +00004365
4366 Possible cleanups:
4367 - iterate over the generator sequence instead of using recursion
4368*/
4369
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004370
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004371static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004372compiler_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004373 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004374 int depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004375 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004376{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004377 comprehension_ty gen;
4378 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4379 if (gen->is_async) {
4380 return compiler_async_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004381 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004382 } else {
4383 return compiler_sync_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004384 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004385 }
4386}
4387
4388static int
4389compiler_sync_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004390 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004391 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004392 expr_ty elt, expr_ty val, int type)
4393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004394 /* generate code for the iterator, then each of the ifs,
4395 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004397 comprehension_ty gen;
4398 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004399 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004401 start = compiler_new_block(c);
4402 skip = compiler_new_block(c);
4403 if_cleanup = compiler_new_block(c);
4404 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004406 if (start == NULL || skip == NULL || if_cleanup == NULL ||
4407 anchor == NULL)
4408 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004410 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004412 if (gen_index == 0) {
4413 /* Receive outermost iter as an implicit argument */
4414 c->u->u_argcount = 1;
4415 ADDOP_I(c, LOAD_FAST, 0);
4416 }
4417 else {
4418 /* Sub-iter - calculate on the fly */
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004419 /* Fast path for the temporary variable assignment idiom:
4420 for y in [f(x)]
4421 */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004422 asdl_expr_seq *elts;
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004423 switch (gen->iter->kind) {
4424 case List_kind:
4425 elts = gen->iter->v.List.elts;
4426 break;
4427 case Tuple_kind:
4428 elts = gen->iter->v.Tuple.elts;
4429 break;
4430 default:
4431 elts = NULL;
4432 }
4433 if (asdl_seq_LEN(elts) == 1) {
4434 expr_ty elt = asdl_seq_GET(elts, 0);
4435 if (elt->kind != Starred_kind) {
4436 VISIT(c, expr, elt);
4437 start = NULL;
4438 }
4439 }
4440 if (start) {
4441 VISIT(c, expr, gen->iter);
4442 ADDOP(c, GET_ITER);
4443 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004444 }
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004445 if (start) {
4446 depth++;
4447 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01004448 ADDOP_JUMP(c, FOR_ITER, anchor);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004449 NEXT_BLOCK(c);
4450 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004451 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004453 /* XXX this needs to be cleaned up...a lot! */
4454 n = asdl_seq_LEN(gen->ifs);
4455 for (i = 0; i < n; i++) {
4456 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004457 if (!compiler_jump_if(c, e, if_cleanup, 0))
4458 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004459 NEXT_BLOCK(c);
4460 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004462 if (++gen_index < asdl_seq_LEN(generators))
4463 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004464 generators, gen_index, depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004465 elt, val, type))
4466 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004468 /* only append after the last for generator */
4469 if (gen_index >= asdl_seq_LEN(generators)) {
4470 /* comprehension specific code */
4471 switch (type) {
4472 case COMP_GENEXP:
4473 VISIT(c, expr, elt);
4474 ADDOP(c, YIELD_VALUE);
4475 ADDOP(c, POP_TOP);
4476 break;
4477 case COMP_LISTCOMP:
4478 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004479 ADDOP_I(c, LIST_APPEND, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004480 break;
4481 case COMP_SETCOMP:
4482 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004483 ADDOP_I(c, SET_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004484 break;
4485 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004486 /* With '{k: v}', k is evaluated before v, so we do
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004487 the same. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004488 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004489 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004490 ADDOP_I(c, MAP_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004491 break;
4492 default:
4493 return 0;
4494 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004496 compiler_use_next_block(c, skip);
4497 }
4498 compiler_use_next_block(c, if_cleanup);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004499 if (start) {
Mark Shannon582aaf12020-08-04 17:30:11 +01004500 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004501 compiler_use_next_block(c, anchor);
4502 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503
4504 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004505}
4506
4507static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004508compiler_async_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004509 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004510 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004511 expr_ty elt, expr_ty val, int type)
4512{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004513 comprehension_ty gen;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004514 basicblock *start, *if_cleanup, *except;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004515 Py_ssize_t i, n;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004516 start = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004517 except = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004518 if_cleanup = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004519
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004520 if (start == NULL || if_cleanup == NULL || except == NULL) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004521 return 0;
4522 }
4523
4524 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4525
4526 if (gen_index == 0) {
4527 /* Receive outermost iter as an implicit argument */
4528 c->u->u_argcount = 1;
4529 ADDOP_I(c, LOAD_FAST, 0);
4530 }
4531 else {
4532 /* Sub-iter - calculate on the fly */
4533 VISIT(c, expr, gen->iter);
4534 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004535 }
4536
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004537 compiler_use_next_block(c, start);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004538
Mark Shannon582aaf12020-08-04 17:30:11 +01004539 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004540 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004541 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004542 ADDOP(c, YIELD_FROM);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004543 ADDOP(c, POP_BLOCK);
Serhiy Storchaka24d32012018-03-10 18:22:34 +02004544 VISIT(c, expr, gen->target);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004545
4546 n = asdl_seq_LEN(gen->ifs);
4547 for (i = 0; i < n; i++) {
4548 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004549 if (!compiler_jump_if(c, e, if_cleanup, 0))
4550 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004551 NEXT_BLOCK(c);
4552 }
4553
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004554 depth++;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004555 if (++gen_index < asdl_seq_LEN(generators))
4556 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004557 generators, gen_index, depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004558 elt, val, type))
4559 return 0;
4560
4561 /* only append after the last for generator */
4562 if (gen_index >= asdl_seq_LEN(generators)) {
4563 /* comprehension specific code */
4564 switch (type) {
4565 case COMP_GENEXP:
4566 VISIT(c, expr, elt);
4567 ADDOP(c, YIELD_VALUE);
4568 ADDOP(c, POP_TOP);
4569 break;
4570 case COMP_LISTCOMP:
4571 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004572 ADDOP_I(c, LIST_APPEND, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004573 break;
4574 case COMP_SETCOMP:
4575 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004576 ADDOP_I(c, SET_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004577 break;
4578 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004579 /* With '{k: v}', k is evaluated before v, so we do
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004580 the same. */
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004581 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004582 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004583 ADDOP_I(c, MAP_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004584 break;
4585 default:
4586 return 0;
4587 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004588 }
4589 compiler_use_next_block(c, if_cleanup);
Mark Shannon582aaf12020-08-04 17:30:11 +01004590 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004591
4592 compiler_use_next_block(c, except);
4593 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004594
4595 return 1;
4596}
4597
4598static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004599compiler_comprehension(struct compiler *c, expr_ty e, int type,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004600 identifier name, asdl_comprehension_seq *generators, expr_ty elt,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004601 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004603 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004604 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004605 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004606 int is_async_generator = 0;
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004607 int top_level_await = IS_TOP_LEVEL_AWAIT(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004608
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004609
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004610 int is_async_function = c->u->u_ste->ste_coroutine;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004611
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004612 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004613 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4614 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004615 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004616 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004617 }
4618
4619 is_async_generator = c->u->u_ste->ste_coroutine;
4620
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004621 if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004622 compiler_error(c, "asynchronous comprehension outside of "
4623 "an asynchronous function");
4624 goto error_in_scope;
4625 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004627 if (type != COMP_GENEXP) {
4628 int op;
4629 switch (type) {
4630 case COMP_LISTCOMP:
4631 op = BUILD_LIST;
4632 break;
4633 case COMP_SETCOMP:
4634 op = BUILD_SET;
4635 break;
4636 case COMP_DICTCOMP:
4637 op = BUILD_MAP;
4638 break;
4639 default:
4640 PyErr_Format(PyExc_SystemError,
4641 "unknown comprehension type %d", type);
4642 goto error_in_scope;
4643 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004645 ADDOP_I(c, op, 0);
4646 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004647
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004648 if (!compiler_comprehension_generator(c, generators, 0, 0, elt,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004649 val, type))
4650 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004652 if (type != COMP_GENEXP) {
4653 ADDOP(c, RETURN_VALUE);
4654 }
4655
4656 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004657 qualname = c->u->u_qualname;
4658 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004659 compiler_exit_scope(c);
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004660 if (top_level_await && is_async_generator){
4661 c->u->u_ste->ste_coroutine = 1;
4662 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004663 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004664 goto error;
4665
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004666 if (!compiler_make_closure(c, co, 0, qualname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004667 goto error;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004668 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004669 Py_DECREF(co);
4670
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004671 VISIT(c, expr, outermost->iter);
4672
4673 if (outermost->is_async) {
4674 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004675 } else {
4676 ADDOP(c, GET_ITER);
4677 }
4678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004679 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004680
4681 if (is_async_generator && type != COMP_GENEXP) {
4682 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004683 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004684 ADDOP(c, YIELD_FROM);
4685 }
4686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004687 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004688error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004689 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004690error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004691 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004692 Py_XDECREF(co);
4693 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004694}
4695
4696static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004697compiler_genexp(struct compiler *c, expr_ty e)
4698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004699 static identifier name;
4700 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004701 name = PyUnicode_InternFromString("<genexpr>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004702 if (!name)
4703 return 0;
4704 }
4705 assert(e->kind == GeneratorExp_kind);
4706 return compiler_comprehension(c, e, COMP_GENEXP, name,
4707 e->v.GeneratorExp.generators,
4708 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004709}
4710
4711static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004712compiler_listcomp(struct compiler *c, expr_ty e)
4713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004714 static identifier name;
4715 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004716 name = PyUnicode_InternFromString("<listcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004717 if (!name)
4718 return 0;
4719 }
4720 assert(e->kind == ListComp_kind);
4721 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4722 e->v.ListComp.generators,
4723 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004724}
4725
4726static int
4727compiler_setcomp(struct compiler *c, expr_ty e)
4728{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004729 static identifier name;
4730 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004731 name = PyUnicode_InternFromString("<setcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004732 if (!name)
4733 return 0;
4734 }
4735 assert(e->kind == SetComp_kind);
4736 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4737 e->v.SetComp.generators,
4738 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004739}
4740
4741
4742static int
4743compiler_dictcomp(struct compiler *c, expr_ty e)
4744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004745 static identifier name;
4746 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004747 name = PyUnicode_InternFromString("<dictcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004748 if (!name)
4749 return 0;
4750 }
4751 assert(e->kind == DictComp_kind);
4752 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4753 e->v.DictComp.generators,
4754 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004755}
4756
4757
4758static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004759compiler_visit_keyword(struct compiler *c, keyword_ty k)
4760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004761 VISIT(c, expr, k->value);
4762 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004763}
4764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004765/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004766 whether they are true or false.
4767
4768 Return values: 1 for true, 0 for false, -1 for non-constant.
4769 */
4770
4771static int
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02004772expr_constant(expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004773{
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004774 if (e->kind == Constant_kind) {
4775 return PyObject_IsTrue(e->v.Constant.value);
Benjamin Peterson442f2092012-12-06 17:41:04 -05004776 }
Serhiy Storchaka3325a672017-12-15 12:35:48 +02004777 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004778}
4779
Mark Shannonfee55262019-11-21 09:11:43 +00004780static int
4781compiler_with_except_finish(struct compiler *c) {
4782 basicblock *exit;
4783 exit = compiler_new_block(c);
4784 if (exit == NULL)
4785 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01004786 ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00004787 ADDOP(c, RERAISE);
4788 compiler_use_next_block(c, exit);
4789 ADDOP(c, POP_TOP);
4790 ADDOP(c, POP_TOP);
4791 ADDOP(c, POP_TOP);
4792 ADDOP(c, POP_EXCEPT);
4793 ADDOP(c, POP_TOP);
4794 return 1;
4795}
Yury Selivanov75445082015-05-11 22:57:16 -04004796
4797/*
4798 Implements the async with statement.
4799
4800 The semantics outlined in that PEP are as follows:
4801
4802 async with EXPR as VAR:
4803 BLOCK
4804
4805 It is implemented roughly as:
4806
4807 context = EXPR
4808 exit = context.__aexit__ # not calling it
4809 value = await context.__aenter__()
4810 try:
4811 VAR = value # if VAR present in the syntax
4812 BLOCK
4813 finally:
4814 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004815 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04004816 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004817 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04004818 if not (await exit(*exc)):
4819 raise
4820 */
4821static int
4822compiler_async_with(struct compiler *c, stmt_ty s, int pos)
4823{
Mark Shannonfee55262019-11-21 09:11:43 +00004824 basicblock *block, *final, *exit;
Yury Selivanov75445082015-05-11 22:57:16 -04004825 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
4826
4827 assert(s->kind == AsyncWith_kind);
Pablo Galindo90235812020-03-15 04:29:22 +00004828 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07004829 c->u->u_ste->ste_coroutine = 1;
4830 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
Zsolt Dollensteine2396502018-04-27 08:58:56 -07004831 return compiler_error(c, "'async with' outside async function");
4832 }
Yury Selivanov75445082015-05-11 22:57:16 -04004833
4834 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004835 final = compiler_new_block(c);
4836 exit = compiler_new_block(c);
4837 if (!block || !final || !exit)
Yury Selivanov75445082015-05-11 22:57:16 -04004838 return 0;
4839
4840 /* Evaluate EXPR */
4841 VISIT(c, expr, item->context_expr);
4842
4843 ADDOP(c, BEFORE_ASYNC_WITH);
4844 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004845 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004846 ADDOP(c, YIELD_FROM);
4847
Mark Shannon582aaf12020-08-04 17:30:11 +01004848 ADDOP_JUMP(c, SETUP_ASYNC_WITH, final);
Yury Selivanov75445082015-05-11 22:57:16 -04004849
4850 /* SETUP_ASYNC_WITH pushes a finally block. */
4851 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004852 if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004853 return 0;
4854 }
4855
4856 if (item->optional_vars) {
4857 VISIT(c, expr, item->optional_vars);
4858 }
4859 else {
4860 /* Discard result from context.__aenter__() */
4861 ADDOP(c, POP_TOP);
4862 }
4863
4864 pos++;
4865 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
4866 /* BLOCK code */
4867 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
4868 else if (!compiler_async_with(c, s, pos))
4869 return 0;
4870
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004871 compiler_pop_fblock(c, ASYNC_WITH, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004872 ADDOP(c, POP_BLOCK);
4873 /* End of body; start the cleanup */
Yury Selivanov75445082015-05-11 22:57:16 -04004874
Mark Shannonfee55262019-11-21 09:11:43 +00004875 /* For successful outcome:
4876 * call __exit__(None, None, None)
4877 */
4878 if(!compiler_call_exit_with_nones(c))
Yury Selivanov75445082015-05-11 22:57:16 -04004879 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00004880 ADDOP(c, GET_AWAITABLE);
4881 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4882 ADDOP(c, YIELD_FROM);
Yury Selivanov75445082015-05-11 22:57:16 -04004883
Mark Shannonfee55262019-11-21 09:11:43 +00004884 ADDOP(c, POP_TOP);
Yury Selivanov75445082015-05-11 22:57:16 -04004885
Mark Shannon582aaf12020-08-04 17:30:11 +01004886 ADDOP_JUMP(c, JUMP_ABSOLUTE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00004887
4888 /* For exceptional outcome: */
4889 compiler_use_next_block(c, final);
4890
4891 ADDOP(c, WITH_EXCEPT_START);
Yury Selivanov75445082015-05-11 22:57:16 -04004892 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004893 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004894 ADDOP(c, YIELD_FROM);
Mark Shannonfee55262019-11-21 09:11:43 +00004895 compiler_with_except_finish(c);
Yury Selivanov75445082015-05-11 22:57:16 -04004896
Mark Shannonfee55262019-11-21 09:11:43 +00004897compiler_use_next_block(c, exit);
Yury Selivanov75445082015-05-11 22:57:16 -04004898 return 1;
4899}
4900
4901
Guido van Rossumc2e20742006-02-27 22:32:47 +00004902/*
4903 Implements the with statement from PEP 343.
Guido van Rossumc2e20742006-02-27 22:32:47 +00004904 with EXPR as VAR:
4905 BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00004906 is implemented as:
4907 <code for EXPR>
4908 SETUP_WITH E
4909 <code to store to VAR> or POP_TOP
4910 <code for BLOCK>
4911 LOAD_CONST (None, None, None)
4912 CALL_FUNCTION_EX 0
4913 JUMP_FORWARD EXIT
4914 E: WITH_EXCEPT_START (calls EXPR.__exit__)
4915 POP_JUMP_IF_TRUE T:
4916 RERAISE
4917 T: POP_TOP * 3 (remove exception from stack)
4918 POP_EXCEPT
4919 POP_TOP
4920 EXIT:
Guido van Rossumc2e20742006-02-27 22:32:47 +00004921 */
Mark Shannonfee55262019-11-21 09:11:43 +00004922
Guido van Rossumc2e20742006-02-27 22:32:47 +00004923static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004924compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004925{
Mark Shannonfee55262019-11-21 09:11:43 +00004926 basicblock *block, *final, *exit;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004927 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004928
4929 assert(s->kind == With_kind);
4930
Guido van Rossumc2e20742006-02-27 22:32:47 +00004931 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004932 final = compiler_new_block(c);
4933 exit = compiler_new_block(c);
4934 if (!block || !final || !exit)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004935 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004936
Thomas Wouters477c8d52006-05-27 19:21:47 +00004937 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004938 VISIT(c, expr, item->context_expr);
Mark Shannonfee55262019-11-21 09:11:43 +00004939 /* Will push bound __exit__ */
Mark Shannon582aaf12020-08-04 17:30:11 +01004940 ADDOP_JUMP(c, SETUP_WITH, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004941
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004942 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00004943 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004944 if (!compiler_push_fblock(c, WITH, block, final, NULL)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004945 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004946 }
4947
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004948 if (item->optional_vars) {
4949 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004950 }
4951 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004952 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004953 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004954 }
4955
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004956 pos++;
4957 if (pos == asdl_seq_LEN(s->v.With.items))
4958 /* BLOCK code */
4959 VISIT_SEQ(c, stmt, s->v.With.body)
4960 else if (!compiler_with(c, s, pos))
4961 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004962
Guido van Rossumc2e20742006-02-27 22:32:47 +00004963 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004964 compiler_pop_fblock(c, WITH, block);
Mark Shannon13bc1392020-01-23 09:25:17 +00004965
Mark Shannonfee55262019-11-21 09:11:43 +00004966 /* End of body; start the cleanup. */
Mark Shannon13bc1392020-01-23 09:25:17 +00004967
Mark Shannonfee55262019-11-21 09:11:43 +00004968 /* For successful outcome:
4969 * call __exit__(None, None, None)
4970 */
4971 if (!compiler_call_exit_with_nones(c))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004972 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00004973 ADDOP(c, POP_TOP);
Mark Shannon582aaf12020-08-04 17:30:11 +01004974 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004975
Mark Shannonfee55262019-11-21 09:11:43 +00004976 /* For exceptional outcome: */
4977 compiler_use_next_block(c, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004978
Mark Shannonfee55262019-11-21 09:11:43 +00004979 ADDOP(c, WITH_EXCEPT_START);
4980 compiler_with_except_finish(c);
4981
4982 compiler_use_next_block(c, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004983 return 1;
4984}
4985
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004986static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03004987compiler_visit_expr1(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004988{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004989 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07004990 case NamedExpr_kind:
4991 VISIT(c, expr, e->v.NamedExpr.value);
4992 ADDOP(c, DUP_TOP);
4993 VISIT(c, expr, e->v.NamedExpr.target);
4994 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004995 case BoolOp_kind:
4996 return compiler_boolop(c, e);
4997 case BinOp_kind:
4998 VISIT(c, expr, e->v.BinOp.left);
4999 VISIT(c, expr, e->v.BinOp.right);
Andy Lester76d58772020-03-10 21:18:12 -05005000 ADDOP(c, binop(e->v.BinOp.op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005001 break;
5002 case UnaryOp_kind:
5003 VISIT(c, expr, e->v.UnaryOp.operand);
5004 ADDOP(c, unaryop(e->v.UnaryOp.op));
5005 break;
5006 case Lambda_kind:
5007 return compiler_lambda(c, e);
5008 case IfExp_kind:
5009 return compiler_ifexp(c, e);
5010 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005011 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005012 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005013 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005014 case GeneratorExp_kind:
5015 return compiler_genexp(c, e);
5016 case ListComp_kind:
5017 return compiler_listcomp(c, e);
5018 case SetComp_kind:
5019 return compiler_setcomp(c, e);
5020 case DictComp_kind:
5021 return compiler_dictcomp(c, e);
5022 case Yield_kind:
5023 if (c->u->u_ste->ste_type != FunctionBlock)
5024 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005025 if (e->v.Yield.value) {
5026 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005027 }
5028 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005029 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005030 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005031 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005032 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005033 case YieldFrom_kind:
5034 if (c->u->u_ste->ste_type != FunctionBlock)
5035 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04005036
5037 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
5038 return compiler_error(c, "'yield from' inside async function");
5039
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005040 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04005041 ADDOP(c, GET_YIELD_FROM_ITER);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005042 ADDOP_LOAD_CONST(c, Py_None);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005043 ADDOP(c, YIELD_FROM);
5044 break;
Yury Selivanov75445082015-05-11 22:57:16 -04005045 case Await_kind:
Pablo Galindo90235812020-03-15 04:29:22 +00005046 if (!IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005047 if (c->u->u_ste->ste_type != FunctionBlock){
5048 return compiler_error(c, "'await' outside function");
5049 }
Yury Selivanov75445082015-05-11 22:57:16 -04005050
Victor Stinner331a6a52019-05-27 16:39:22 +02005051 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005052 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
5053 return compiler_error(c, "'await' outside async function");
5054 }
5055 }
Yury Selivanov75445082015-05-11 22:57:16 -04005056
5057 VISIT(c, expr, e->v.Await.value);
5058 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005059 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005060 ADDOP(c, YIELD_FROM);
5061 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005062 case Compare_kind:
5063 return compiler_compare(c, e);
5064 case Call_kind:
5065 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005066 case Constant_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005067 ADDOP_LOAD_CONST(c, e->v.Constant.value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005068 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005069 case JoinedStr_kind:
5070 return compiler_joined_str(c, e);
5071 case FormattedValue_kind:
5072 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005073 /* The following exprs can be assignment targets. */
5074 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005075 VISIT(c, expr, e->v.Attribute.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005076 switch (e->v.Attribute.ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005077 case Load:
5078 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
5079 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005080 case Store:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005081 if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx))
5082 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005083 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5084 break;
5085 case Del:
5086 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
5087 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005088 }
5089 break;
5090 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005091 return compiler_subscript(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005092 case Starred_kind:
5093 switch (e->v.Starred.ctx) {
5094 case Store:
5095 /* In all legitimate cases, the Starred node was already replaced
5096 * by compiler_list/compiler_tuple. XXX: is that okay? */
5097 return compiler_error(c,
5098 "starred assignment target must be in a list or tuple");
5099 default:
5100 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005101 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005102 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005103 break;
5104 case Slice_kind:
5105 return compiler_slice(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005106 case Name_kind:
5107 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
5108 /* child nodes of List and Tuple will have expr_context set */
5109 case List_kind:
5110 return compiler_list(c, e);
5111 case Tuple_kind:
5112 return compiler_tuple(c, e);
5113 }
5114 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005115}
5116
5117static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005118compiler_visit_expr(struct compiler *c, expr_ty e)
5119{
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005120 int old_lineno = c->u->u_lineno;
5121 int old_col_offset = c->u->u_col_offset;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005122 SET_LOC(c, e);
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005123 int res = compiler_visit_expr1(c, e);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005124 c->u->u_lineno = old_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005125 c->u->u_col_offset = old_col_offset;
5126 return res;
5127}
5128
5129static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005130compiler_augassign(struct compiler *c, stmt_ty s)
5131{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005132 assert(s->kind == AugAssign_kind);
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005133 expr_ty e = s->v.AugAssign.target;
5134
5135 int old_lineno = c->u->u_lineno;
5136 int old_col_offset = c->u->u_col_offset;
5137 SET_LOC(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005139 switch (e->kind) {
5140 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005141 VISIT(c, expr, e->v.Attribute.value);
5142 ADDOP(c, DUP_TOP);
5143 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005144 break;
5145 case Subscript_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005146 VISIT(c, expr, e->v.Subscript.value);
5147 VISIT(c, expr, e->v.Subscript.slice);
5148 ADDOP(c, DUP_TOP_TWO);
5149 ADDOP(c, BINARY_SUBSCR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005150 break;
5151 case Name_kind:
5152 if (!compiler_nameop(c, e->v.Name.id, Load))
5153 return 0;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005154 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005155 default:
5156 PyErr_Format(PyExc_SystemError,
5157 "invalid node type (%d) for augmented assignment",
5158 e->kind);
5159 return 0;
5160 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005161
5162 c->u->u_lineno = old_lineno;
5163 c->u->u_col_offset = old_col_offset;
5164
5165 VISIT(c, expr, s->v.AugAssign.value);
5166 ADDOP(c, inplace_binop(s->v.AugAssign.op));
5167
5168 SET_LOC(c, e);
5169
5170 switch (e->kind) {
5171 case Attribute_kind:
5172 ADDOP(c, ROT_TWO);
5173 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5174 break;
5175 case Subscript_kind:
5176 ADDOP(c, ROT_THREE);
5177 ADDOP(c, STORE_SUBSCR);
5178 break;
5179 case Name_kind:
5180 return compiler_nameop(c, e->v.Name.id, Store);
5181 default:
5182 Py_UNREACHABLE();
5183 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005184 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005185}
5186
5187static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005188check_ann_expr(struct compiler *c, expr_ty e)
5189{
5190 VISIT(c, expr, e);
5191 ADDOP(c, POP_TOP);
5192 return 1;
5193}
5194
5195static int
5196check_annotation(struct compiler *c, stmt_ty s)
5197{
5198 /* Annotations are only evaluated in a module or class. */
5199 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5200 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
5201 return check_ann_expr(c, s->v.AnnAssign.annotation);
5202 }
5203 return 1;
5204}
5205
5206static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005207check_ann_subscr(struct compiler *c, expr_ty e)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005208{
5209 /* We check that everything in a subscript is defined at runtime. */
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005210 switch (e->kind) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005211 case Slice_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005212 if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005213 return 0;
5214 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005215 if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) {
5216 return 0;
5217 }
5218 if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) {
5219 return 0;
5220 }
5221 return 1;
5222 case Tuple_kind: {
5223 /* extended slice */
Pablo Galindoa5634c42020-09-16 19:42:00 +01005224 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005225 Py_ssize_t i, n = asdl_seq_LEN(elts);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005226 for (i = 0; i < n; i++) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005227 if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005228 return 0;
5229 }
5230 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005231 return 1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005232 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005233 default:
5234 return check_ann_expr(c, e);
5235 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005236}
5237
5238static int
5239compiler_annassign(struct compiler *c, stmt_ty s)
5240{
5241 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07005242 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005243
5244 assert(s->kind == AnnAssign_kind);
5245
5246 /* We perform the actual assignment first. */
5247 if (s->v.AnnAssign.value) {
5248 VISIT(c, expr, s->v.AnnAssign.value);
5249 VISIT(c, expr, targ);
5250 }
5251 switch (targ->kind) {
5252 case Name_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005253 if (forbidden_name(c, targ->v.Name.id, Store))
5254 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005255 /* If we have a simple name in a module or class, store annotation. */
5256 if (s->v.AnnAssign.simple &&
5257 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5258 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Batuhan Taskaya044a1042020-10-06 23:03:02 +03005259 VISIT(c, annexpr, s->v.AnnAssign.annotation);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005260 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02005261 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005262 ADDOP_LOAD_CONST_NEW(c, mangled);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005263 ADDOP(c, STORE_SUBSCR);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005264 }
5265 break;
5266 case Attribute_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005267 if (forbidden_name(c, targ->v.Attribute.attr, Store))
5268 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005269 if (!s->v.AnnAssign.value &&
5270 !check_ann_expr(c, targ->v.Attribute.value)) {
5271 return 0;
5272 }
5273 break;
5274 case Subscript_kind:
5275 if (!s->v.AnnAssign.value &&
5276 (!check_ann_expr(c, targ->v.Subscript.value) ||
5277 !check_ann_subscr(c, targ->v.Subscript.slice))) {
5278 return 0;
5279 }
5280 break;
5281 default:
5282 PyErr_Format(PyExc_SystemError,
5283 "invalid node type (%d) for annotated assignment",
5284 targ->kind);
5285 return 0;
5286 }
5287 /* Annotation is evaluated last. */
5288 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
5289 return 0;
5290 }
5291 return 1;
5292}
5293
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005294/* Raises a SyntaxError and returns 0.
5295 If something goes wrong, a different exception may be raised.
5296*/
5297
5298static int
5299compiler_error(struct compiler *c, const char *errstr)
5300{
Benjamin Peterson43b06862011-05-27 09:08:01 -05005301 PyObject *loc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005302 PyObject *u = NULL, *v = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005303
Victor Stinner14e461d2013-08-26 22:28:21 +02005304 loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005305 if (!loc) {
5306 Py_INCREF(Py_None);
5307 loc = Py_None;
5308 }
Victor Stinner14e461d2013-08-26 22:28:21 +02005309 u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04005310 c->u->u_col_offset + 1, loc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005311 if (!u)
5312 goto exit;
5313 v = Py_BuildValue("(zO)", errstr, u);
5314 if (!v)
5315 goto exit;
5316 PyErr_SetObject(PyExc_SyntaxError, v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005317 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005318 Py_DECREF(loc);
5319 Py_XDECREF(u);
5320 Py_XDECREF(v);
5321 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005322}
5323
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005324/* Emits a SyntaxWarning and returns 1 on success.
5325 If a SyntaxWarning raised as error, replaces it with a SyntaxError
5326 and returns 0.
5327*/
5328static int
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005329compiler_warn(struct compiler *c, const char *format, ...)
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005330{
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005331 va_list vargs;
5332#ifdef HAVE_STDARG_PROTOTYPES
5333 va_start(vargs, format);
5334#else
5335 va_start(vargs);
5336#endif
5337 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5338 va_end(vargs);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005339 if (msg == NULL) {
5340 return 0;
5341 }
5342 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
5343 c->u->u_lineno, NULL, NULL) < 0)
5344 {
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005345 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005346 /* Replace the SyntaxWarning exception with a SyntaxError
5347 to get a more accurate error report */
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005348 PyErr_Clear();
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005349 assert(PyUnicode_AsUTF8(msg) != NULL);
5350 compiler_error(c, PyUnicode_AsUTF8(msg));
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005351 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005352 Py_DECREF(msg);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005353 return 0;
5354 }
5355 Py_DECREF(msg);
5356 return 1;
5357}
5358
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005359static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005360compiler_subscript(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005361{
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005362 expr_context_ty ctx = e->v.Subscript.ctx;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005363 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005364
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005365 if (ctx == Load) {
5366 if (!check_subscripter(c, e->v.Subscript.value)) {
5367 return 0;
5368 }
5369 if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
5370 return 0;
5371 }
5372 }
5373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005374 switch (ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005375 case Load: op = BINARY_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005376 case Store: op = STORE_SUBSCR; break;
5377 case Del: op = DELETE_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005378 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005379 assert(op);
5380 VISIT(c, expr, e->v.Subscript.value);
5381 VISIT(c, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005382 ADDOP(c, op);
5383 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005384}
5385
5386static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005387compiler_slice(struct compiler *c, expr_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005388{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005389 int n = 2;
5390 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005392 /* only handles the cases where BUILD_SLICE is emitted */
5393 if (s->v.Slice.lower) {
5394 VISIT(c, expr, s->v.Slice.lower);
5395 }
5396 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005397 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005398 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005400 if (s->v.Slice.upper) {
5401 VISIT(c, expr, s->v.Slice.upper);
5402 }
5403 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005404 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005405 }
5406
5407 if (s->v.Slice.step) {
5408 n++;
5409 VISIT(c, expr, s->v.Slice.step);
5410 }
5411 ADDOP_I(c, BUILD_SLICE, n);
5412 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005413}
5414
Thomas Wouters89f507f2006-12-13 04:49:30 +00005415/* End of the compiler section, beginning of the assembler section */
5416
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005417/* do depth-first search of basic block graph, starting with block.
T. Wouters99b54d62019-09-12 07:05:33 -07005418 post records the block indices in post-order.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005419
5420 XXX must handle implicit jumps from one block to next
5421*/
5422
Thomas Wouters89f507f2006-12-13 04:49:30 +00005423struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005424 PyObject *a_bytecode; /* string containing bytecode */
5425 int a_offset; /* offset into bytecode */
5426 int a_nblocks; /* number of reachable blocks */
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005427 basicblock **a_reverse_postorder; /* list of blocks in dfs postorder */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005428 PyObject *a_lnotab; /* string containing lnotab */
5429 int a_lnotab_off; /* offset into lnotab */
5430 int a_lineno; /* last lineno of emitted instruction */
5431 int a_lineno_off; /* bytecode offset of last lineno */
Thomas Wouters89f507f2006-12-13 04:49:30 +00005432};
5433
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005434static void
T. Wouters99b54d62019-09-12 07:05:33 -07005435dfs(struct compiler *c, basicblock *b, struct assembler *a, int end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005436{
T. Wouters99b54d62019-09-12 07:05:33 -07005437
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005438 /* There is no real depth-first-search to do here because all the
5439 * blocks are emitted in topological order already, so we just need to
5440 * follow the b_next pointers and place them in a->a_reverse_postorder in
5441 * reverse order and make sure that the first one starts at 0. */
5442
5443 for (a->a_nblocks = 0; b != NULL; b = b->b_next) {
5444 a->a_reverse_postorder[a->a_nblocks++] = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005445 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005446}
5447
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005448Py_LOCAL_INLINE(void)
5449stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005450{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005451 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
Mark Shannonfee55262019-11-21 09:11:43 +00005452 if (b->b_startdepth < depth && b->b_startdepth < 100) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005453 assert(b->b_startdepth < 0);
5454 b->b_startdepth = depth;
5455 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02005456 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005457}
5458
5459/* Find the flow path that needs the largest stack. We assume that
5460 * cycles in the flow graph have no net effect on the stack depth.
5461 */
5462static int
5463stackdepth(struct compiler *c)
5464{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005465 basicblock *b, *entryblock = NULL;
5466 basicblock **stack, **sp;
5467 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005468 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005469 b->b_startdepth = INT_MIN;
5470 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005471 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005472 }
5473 if (!entryblock)
5474 return 0;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005475 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
5476 if (!stack) {
5477 PyErr_NoMemory();
5478 return -1;
5479 }
5480
5481 sp = stack;
5482 stackdepth_push(&sp, entryblock, 0);
5483 while (sp != stack) {
5484 b = *--sp;
5485 int depth = b->b_startdepth;
5486 assert(depth >= 0);
5487 basicblock *next = b->b_next;
5488 for (int i = 0; i < b->b_iused; i++) {
5489 struct instr *instr = &b->b_instr[i];
5490 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
5491 if (effect == PY_INVALID_STACK_EFFECT) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01005492 _Py_FatalErrorFormat(__func__,
5493 "opcode = %d", instr->i_opcode);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005494 }
5495 int new_depth = depth + effect;
5496 if (new_depth > maxdepth) {
5497 maxdepth = new_depth;
5498 }
5499 assert(depth >= 0); /* invalid code or bug in stackdepth() */
Mark Shannon582aaf12020-08-04 17:30:11 +01005500 if (is_jump(instr)) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005501 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
5502 assert(effect != PY_INVALID_STACK_EFFECT);
5503 int target_depth = depth + effect;
5504 if (target_depth > maxdepth) {
5505 maxdepth = target_depth;
5506 }
5507 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005508 stackdepth_push(&sp, instr->i_target, target_depth);
5509 }
5510 depth = new_depth;
5511 if (instr->i_opcode == JUMP_ABSOLUTE ||
5512 instr->i_opcode == JUMP_FORWARD ||
5513 instr->i_opcode == RETURN_VALUE ||
Mark Shannonfee55262019-11-21 09:11:43 +00005514 instr->i_opcode == RAISE_VARARGS ||
5515 instr->i_opcode == RERAISE)
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005516 {
5517 /* remaining code is dead */
5518 next = NULL;
5519 break;
5520 }
5521 }
5522 if (next != NULL) {
5523 stackdepth_push(&sp, next, depth);
5524 }
5525 }
5526 PyObject_Free(stack);
5527 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005528}
5529
5530static int
5531assemble_init(struct assembler *a, int nblocks, int firstlineno)
5532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005533 memset(a, 0, sizeof(struct assembler));
5534 a->a_lineno = firstlineno;
5535 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
5536 if (!a->a_bytecode)
5537 return 0;
5538 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
5539 if (!a->a_lnotab)
5540 return 0;
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07005541 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005542 PyErr_NoMemory();
5543 return 0;
5544 }
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005545 a->a_reverse_postorder = (basicblock **)PyObject_Malloc(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005546 sizeof(basicblock *) * nblocks);
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005547 if (!a->a_reverse_postorder) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005548 PyErr_NoMemory();
5549 return 0;
5550 }
5551 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005552}
5553
5554static void
5555assemble_free(struct assembler *a)
5556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005557 Py_XDECREF(a->a_bytecode);
5558 Py_XDECREF(a->a_lnotab);
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005559 if (a->a_reverse_postorder)
5560 PyObject_Free(a->a_reverse_postorder);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005561}
5562
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005563static int
5564blocksize(basicblock *b)
5565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005566 int i;
5567 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005569 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005570 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005571 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005572}
5573
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00005574/* Appends a pair to the end of the line number table, a_lnotab, representing
5575 the instruction's bytecode offset and line number. See
5576 Objects/lnotab_notes.txt for the description of the line number table. */
Tim Peters2a7f3842001-06-09 09:26:21 +00005577
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005578static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005579assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005581 int d_bytecode, d_lineno;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005582 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005583 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005585 d_lineno = i->i_lineno - a->a_lineno;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005586 if (d_lineno == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005587 return 1;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005588 }
5589
5590 d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT);
5591 assert(d_bytecode >= 0);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00005592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005593 if (d_bytecode > 255) {
5594 int j, nbytes, ncodes = d_bytecode / 255;
5595 nbytes = a->a_lnotab_off + 2 * ncodes;
5596 len = PyBytes_GET_SIZE(a->a_lnotab);
5597 if (nbytes >= len) {
5598 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
5599 len = nbytes;
5600 else if (len <= INT_MAX / 2)
5601 len *= 2;
5602 else {
5603 PyErr_NoMemory();
5604 return 0;
5605 }
5606 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
5607 return 0;
5608 }
5609 lnotab = (unsigned char *)
5610 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
5611 for (j = 0; j < ncodes; j++) {
5612 *lnotab++ = 255;
5613 *lnotab++ = 0;
5614 }
5615 d_bytecode -= ncodes * 255;
5616 a->a_lnotab_off += ncodes * 2;
5617 }
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005618 assert(0 <= d_bytecode && d_bytecode <= 255);
5619
5620 if (d_lineno < -128 || 127 < d_lineno) {
5621 int j, nbytes, ncodes, k;
5622 if (d_lineno < 0) {
5623 k = -128;
5624 /* use division on positive numbers */
5625 ncodes = (-d_lineno) / 128;
5626 }
5627 else {
5628 k = 127;
5629 ncodes = d_lineno / 127;
5630 }
5631 d_lineno -= ncodes * k;
5632 assert(ncodes >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005633 nbytes = a->a_lnotab_off + 2 * ncodes;
5634 len = PyBytes_GET_SIZE(a->a_lnotab);
5635 if (nbytes >= len) {
5636 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
5637 len = nbytes;
5638 else if (len <= INT_MAX / 2)
5639 len *= 2;
5640 else {
5641 PyErr_NoMemory();
5642 return 0;
5643 }
5644 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
5645 return 0;
5646 }
5647 lnotab = (unsigned char *)
5648 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
5649 *lnotab++ = d_bytecode;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005650 *lnotab++ = k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005651 d_bytecode = 0;
5652 for (j = 1; j < ncodes; j++) {
5653 *lnotab++ = 0;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005654 *lnotab++ = k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005655 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005656 a->a_lnotab_off += ncodes * 2;
5657 }
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005658 assert(-128 <= d_lineno && d_lineno <= 127);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005660 len = PyBytes_GET_SIZE(a->a_lnotab);
5661 if (a->a_lnotab_off + 2 >= len) {
5662 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
5663 return 0;
5664 }
5665 lnotab = (unsigned char *)
5666 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00005667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005668 a->a_lnotab_off += 2;
5669 if (d_bytecode) {
5670 *lnotab++ = d_bytecode;
Victor Stinner4f2dab52011-05-27 16:46:51 +02005671 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005672 }
5673 else { /* First line of a block; def stmt, etc. */
5674 *lnotab++ = 0;
Victor Stinner4f2dab52011-05-27 16:46:51 +02005675 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005676 }
5677 a->a_lineno = i->i_lineno;
5678 a->a_lineno_off = a->a_offset;
5679 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005680}
5681
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005682/* assemble_emit()
5683 Extend the bytecode with a new instruction.
5684 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00005685*/
5686
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00005687static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005688assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00005689{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005690 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005691 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03005692 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005693
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005694 arg = i->i_oparg;
5695 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005696 if (i->i_lineno && !assemble_lnotab(a, i))
5697 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005698 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005699 if (len > PY_SSIZE_T_MAX / 2)
5700 return 0;
5701 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
5702 return 0;
5703 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03005704 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005705 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005706 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005707 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00005708}
5709
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00005710static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005711assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00005712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005713 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005714 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005715 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00005716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005717 /* Compute the size of each block and fixup jump args.
5718 Replace block pointer with position in bytecode. */
5719 do {
5720 totsize = 0;
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005721 for (i = 0; i < a->a_nblocks; i++) {
5722 b = a->a_reverse_postorder[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005723 bsize = blocksize(b);
5724 b->b_offset = totsize;
5725 totsize += bsize;
5726 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005727 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005728 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
5729 bsize = b->b_offset;
5730 for (i = 0; i < b->b_iused; i++) {
5731 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005732 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005733 /* Relative jumps are computed relative to
5734 the instruction pointer after fetching
5735 the jump instruction.
5736 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005737 bsize += isize;
Mark Shannon582aaf12020-08-04 17:30:11 +01005738 if (is_jump(instr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005739 instr->i_oparg = instr->i_target->b_offset;
Mark Shannon582aaf12020-08-04 17:30:11 +01005740 if (is_relative_jump(instr)) {
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005741 instr->i_oparg -= bsize;
5742 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03005743 instr->i_oparg *= sizeof(_Py_CODEUNIT);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005744 if (instrsize(instr->i_oparg) != isize) {
5745 extended_arg_recompile = 1;
5746 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005747 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005748 }
5749 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00005750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005751 /* XXX: This is an awful hack that could hurt performance, but
5752 on the bright side it should work until we come up
5753 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00005754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005755 The issue is that in the first loop blocksize() is called
5756 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005757 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005758 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00005759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005760 So we loop until we stop seeing new EXTENDED_ARGs.
5761 The only EXTENDED_ARGs that could be popping up are
5762 ones in jump instructions. So this should converge
5763 fairly quickly.
5764 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005765 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005766}
5767
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005768static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01005769dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005771 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02005772 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005774 tuple = PyTuple_New(size);
5775 if (tuple == NULL)
5776 return NULL;
5777 while (PyDict_Next(dict, &pos, &k, &v)) {
5778 i = PyLong_AS_LONG(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005779 Py_INCREF(k);
5780 assert((i - offset) < size);
5781 assert((i - offset) >= 0);
5782 PyTuple_SET_ITEM(tuple, i - offset, k);
5783 }
5784 return tuple;
5785}
5786
5787static PyObject *
5788consts_dict_keys_inorder(PyObject *dict)
5789{
5790 PyObject *consts, *k, *v;
5791 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
5792
5793 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
5794 if (consts == NULL)
5795 return NULL;
5796 while (PyDict_Next(dict, &pos, &k, &v)) {
5797 i = PyLong_AS_LONG(v);
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +03005798 /* The keys of the dictionary can be tuples wrapping a contant.
5799 * (see compiler_add_o and _PyCode_ConstantKey). In that case
5800 * the object we want is always second. */
5801 if (PyTuple_CheckExact(k)) {
5802 k = PyTuple_GET_ITEM(k, 1);
5803 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005804 Py_INCREF(k);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005805 assert(i < size);
5806 assert(i >= 0);
5807 PyList_SET_ITEM(consts, i, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005808 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005809 return consts;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005810}
5811
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005812static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005813compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005814{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005815 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005816 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005817 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04005818 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005819 if (ste->ste_nested)
5820 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07005821 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005822 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07005823 if (!ste->ste_generator && ste->ste_coroutine)
5824 flags |= CO_COROUTINE;
5825 if (ste->ste_generator && ste->ste_coroutine)
5826 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005827 if (ste->ste_varargs)
5828 flags |= CO_VARARGS;
5829 if (ste->ste_varkeywords)
5830 flags |= CO_VARKEYWORDS;
5831 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00005832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005833 /* (Only) inherit compilerflags in PyCF_MASK */
5834 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00005835
Pablo Galindo90235812020-03-15 04:29:22 +00005836 if ((IS_TOP_LEVEL_AWAIT(c)) &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005837 ste->ste_coroutine &&
5838 !ste->ste_generator) {
5839 flags |= CO_COROUTINE;
5840 }
5841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005842 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005843}
5844
INADA Naokic2e16072018-11-26 21:23:22 +09005845// Merge *tuple* with constant cache.
5846// Unlike merge_consts_recursive(), this function doesn't work recursively.
5847static int
5848merge_const_tuple(struct compiler *c, PyObject **tuple)
5849{
5850 assert(PyTuple_CheckExact(*tuple));
5851
5852 PyObject *key = _PyCode_ConstantKey(*tuple);
5853 if (key == NULL) {
5854 return 0;
5855 }
5856
5857 // t is borrowed reference
5858 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
5859 Py_DECREF(key);
5860 if (t == NULL) {
5861 return 0;
5862 }
5863 if (t == key) { // tuple is new constant.
5864 return 1;
5865 }
5866
5867 PyObject *u = PyTuple_GET_ITEM(t, 1);
5868 Py_INCREF(u);
5869 Py_DECREF(*tuple);
5870 *tuple = u;
5871 return 1;
5872}
5873
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005874static PyCodeObject *
Mark Shannon6e8128f2020-07-30 10:03:00 +01005875makecode(struct compiler *c, struct assembler *a, PyObject *consts)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005876{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005877 PyCodeObject *co = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005878 PyObject *names = NULL;
5879 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005880 PyObject *name = NULL;
5881 PyObject *freevars = NULL;
5882 PyObject *cellvars = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005883 Py_ssize_t nlocals;
5884 int nlocals_int;
5885 int flags;
Pablo Galindocd74e662019-06-01 18:08:04 +01005886 int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005888 names = dict_keys_inorder(c->u->u_names, 0);
5889 varnames = dict_keys_inorder(c->u->u_varnames, 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01005890 if (!names || !varnames) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005891 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01005892 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005893 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
5894 if (!cellvars)
5895 goto error;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005896 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005897 if (!freevars)
5898 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005899
INADA Naokic2e16072018-11-26 21:23:22 +09005900 if (!merge_const_tuple(c, &names) ||
5901 !merge_const_tuple(c, &varnames) ||
5902 !merge_const_tuple(c, &cellvars) ||
5903 !merge_const_tuple(c, &freevars))
5904 {
5905 goto error;
5906 }
5907
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02005908 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01005909 assert(nlocals < INT_MAX);
5910 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
5911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005912 flags = compute_code_flags(c);
5913 if (flags < 0)
5914 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005915
Mark Shannon6e8128f2020-07-30 10:03:00 +01005916 consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
5917 if (consts == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005918 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01005919 }
INADA Naokic2e16072018-11-26 21:23:22 +09005920 if (!merge_const_tuple(c, &consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01005921 Py_DECREF(consts);
INADA Naokic2e16072018-11-26 21:23:22 +09005922 goto error;
5923 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005924
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01005925 posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int);
Pablo Galindocd74e662019-06-01 18:08:04 +01005926 posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +01005927 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005928 maxdepth = stackdepth(c);
5929 if (maxdepth < 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01005930 Py_DECREF(consts);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005931 goto error;
5932 }
Pablo Galindo4a2edc32019-07-01 11:35:05 +01005933 co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
Mark Shannon13bc1392020-01-23 09:25:17 +00005934 posonlyargcount, kwonlyargcount, nlocals_int,
Mark Shannon6e8128f2020-07-30 10:03:00 +01005935 maxdepth, flags, a->a_bytecode, consts, names,
Pablo Galindo4a2edc32019-07-01 11:35:05 +01005936 varnames, freevars, cellvars, c->c_filename,
5937 c->u->u_name, c->u->u_firstlineno, a->a_lnotab);
Mark Shannon6e8128f2020-07-30 10:03:00 +01005938 Py_DECREF(consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005939 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005940 Py_XDECREF(names);
5941 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005942 Py_XDECREF(name);
5943 Py_XDECREF(freevars);
5944 Py_XDECREF(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005945 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005946}
5947
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005948
5949/* For debugging purposes only */
5950#if 0
5951static void
5952dump_instr(const struct instr *i)
5953{
Mark Shannon582aaf12020-08-04 17:30:11 +01005954 const char *jrel = (is_relative_jump(instr)) ? "jrel " : "";
5955 const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : "";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005956 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005958 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005959 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005960 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005961 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005962 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
5963 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005964}
5965
5966static void
5967dump_basicblock(const basicblock *b)
5968{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005969 const char *b_return = b->b_return ? "return " : "";
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005970 fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n",
5971 b->b_iused, b->b_startdepth, b->b_offset, b_return);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005972 if (b->b_instr) {
5973 int i;
5974 for (i = 0; i < b->b_iused; i++) {
5975 fprintf(stderr, " [%02d] ", i);
5976 dump_instr(b->b_instr + i);
5977 }
5978 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005979}
5980#endif
5981
Mark Shannon6e8128f2020-07-30 10:03:00 +01005982static int
5983optimize_cfg(struct assembler *a, PyObject *consts);
5984
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005985static PyCodeObject *
5986assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005987{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005988 basicblock *b, *entryblock;
5989 struct assembler a;
5990 int i, j, nblocks;
5991 PyCodeObject *co = NULL;
Mark Shannon6e8128f2020-07-30 10:03:00 +01005992 PyObject *consts = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005994 /* Make sure every block that falls off the end returns None.
5995 XXX NEXT_BLOCK() isn't quite right, because if the last
5996 block ends with a jump or return b_next shouldn't set.
5997 */
5998 if (!c->u->u_curblock->b_return) {
5999 NEXT_BLOCK(c);
6000 if (addNone)
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006001 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006002 ADDOP(c, RETURN_VALUE);
6003 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006005 nblocks = 0;
6006 entryblock = NULL;
6007 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6008 nblocks++;
6009 entryblock = b;
6010 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006012 /* Set firstlineno if it wasn't explicitly set. */
6013 if (!c->u->u_firstlineno) {
Ned Deilydc35cda2016-08-17 17:18:33 -04006014 if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006015 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
6016 else
6017 c->u->u_firstlineno = 1;
6018 }
6019 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
6020 goto error;
T. Wouters99b54d62019-09-12 07:05:33 -07006021 dfs(c, entryblock, &a, nblocks);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006022
Mark Shannon6e8128f2020-07-30 10:03:00 +01006023 consts = consts_dict_keys_inorder(c->u->u_consts);
6024 if (consts == NULL) {
6025 goto error;
6026 }
6027 if (optimize_cfg(&a, consts)) {
6028 goto error;
6029 }
6030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006031 /* Can't modify the bytecode after computing jump offsets. */
6032 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00006033
T. Wouters99b54d62019-09-12 07:05:33 -07006034 /* Emit code in reverse postorder from dfs. */
Pablo Galindo60eb9f12020-06-28 01:55:47 +01006035 for (i = 0; i < a.a_nblocks; i++) {
6036 b = a.a_reverse_postorder[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006037 for (j = 0; j < b->b_iused; j++)
6038 if (!assemble_emit(&a, &b->b_instr[j]))
6039 goto error;
6040 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00006041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006042 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
6043 goto error;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006044 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006045 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006046
Mark Shannon6e8128f2020-07-30 10:03:00 +01006047 co = makecode(c, &a, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006048 error:
Mark Shannon6e8128f2020-07-30 10:03:00 +01006049 Py_XDECREF(consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006050 assemble_free(&a);
6051 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006052}
Georg Brandl8334fd92010-12-04 10:26:46 +00006053
6054#undef PyAST_Compile
Benjamin Petersone5024512018-09-12 12:06:42 -07006055PyCodeObject *
Georg Brandl8334fd92010-12-04 10:26:46 +00006056PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
6057 PyArena *arena)
6058{
6059 return PyAST_CompileEx(mod, filename, flags, -1, arena);
6060}
Mark Shannon6e8128f2020-07-30 10:03:00 +01006061
6062
6063/* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
6064 with LOAD_CONST (c1, c2, ... cn).
6065 The consts table must still be in list form so that the
6066 new constant (c1, c2, ... cn) can be appended.
6067 Called with codestr pointing to the first LOAD_CONST.
6068*/
6069static int
6070fold_tuple_on_constants(struct instr *inst,
6071 int n, PyObject *consts)
6072{
6073 /* Pre-conditions */
6074 assert(PyList_CheckExact(consts));
6075 assert(inst[n].i_opcode == BUILD_TUPLE);
6076 assert(inst[n].i_oparg == n);
6077
6078 for (int i = 0; i < n; i++) {
6079 if (inst[i].i_opcode != LOAD_CONST) {
6080 return 0;
6081 }
6082 }
6083
6084 /* Buildup new tuple of constants */
6085 PyObject *newconst = PyTuple_New(n);
6086 if (newconst == NULL) {
6087 return -1;
6088 }
6089 for (int i = 0; i < n; i++) {
6090 int arg = inst[i].i_oparg;
6091 PyObject *constant = PyList_GET_ITEM(consts, arg);
6092 Py_INCREF(constant);
6093 PyTuple_SET_ITEM(newconst, i, constant);
6094 }
6095 Py_ssize_t index = PyList_GET_SIZE(consts);
Victor Stinner71f2ff42020-09-23 14:06:55 +02006096 if ((size_t)index >= (size_t)INT_MAX - 1) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006097 Py_DECREF(newconst);
6098 PyErr_SetString(PyExc_OverflowError, "too many constants");
6099 return -1;
6100 }
Mark Shannon6e8128f2020-07-30 10:03:00 +01006101 if (PyList_Append(consts, newconst)) {
6102 Py_DECREF(newconst);
6103 return -1;
6104 }
6105 Py_DECREF(newconst);
6106 for (int i = 0; i < n; i++) {
6107 inst[i].i_opcode = NOP;
6108 }
6109 inst[n].i_opcode = LOAD_CONST;
Victor Stinner71f2ff42020-09-23 14:06:55 +02006110 inst[n].i_oparg = (int)index;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006111 return 0;
6112}
6113
6114
6115/* Optimization */
6116static int
6117optimize_basic_block(basicblock *bb, PyObject *consts)
6118{
6119 assert(PyList_CheckExact(consts));
6120 struct instr nop;
6121 nop.i_opcode = NOP;
6122 struct instr *target;
6123 int lineno;
6124 for (int i = 0; i < bb->b_iused; i++) {
6125 struct instr *inst = &bb->b_instr[i];
6126 int oparg = inst->i_oparg;
6127 int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01006128 if (is_jump(inst)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006129 /* Skip over empty basic blocks. */
6130 while (inst->i_target->b_iused == 0) {
6131 inst->i_target = inst->i_target->b_next;
6132 }
6133 target = &inst->i_target->b_instr[0];
6134 }
6135 else {
6136 target = &nop;
6137 }
6138 switch (inst->i_opcode) {
6139 /* Skip over LOAD_CONST trueconst
6140 POP_JUMP_IF_FALSE xx. This improves
6141 "while 1" performance. */
6142 case LOAD_CONST:
6143 if (nextop != POP_JUMP_IF_FALSE) {
6144 break;
6145 }
6146 PyObject* cnt = PyList_GET_ITEM(consts, oparg);
6147 int is_true = PyObject_IsTrue(cnt);
6148 if (is_true == -1) {
6149 goto error;
6150 }
6151 if (is_true == 1) {
6152 inst->i_opcode = NOP;
6153 bb->b_instr[i+1].i_opcode = NOP;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006154 }
6155 break;
6156
6157 /* Try to fold tuples of constants.
6158 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
6159 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
6160 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
6161 case BUILD_TUPLE:
6162 if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
6163 switch(oparg) {
6164 case 1:
6165 inst->i_opcode = NOP;
6166 bb->b_instr[i+1].i_opcode = NOP;
6167 break;
6168 case 2:
6169 inst->i_opcode = ROT_TWO;
6170 bb->b_instr[i+1].i_opcode = NOP;
6171 break;
6172 case 3:
6173 inst->i_opcode = ROT_THREE;
6174 bb->b_instr[i+1].i_opcode = ROT_TWO;
6175 }
6176 break;
6177 }
6178 if (i >= oparg) {
6179 if (fold_tuple_on_constants(inst-oparg, oparg, consts)) {
6180 goto error;
6181 }
6182 }
6183 break;
6184
6185 /* Simplify conditional jump to conditional jump where the
6186 result of the first test implies the success of a similar
6187 test or the failure of the opposite test.
6188 Arises in code like:
6189 "a and b or c"
6190 "(a and b) and c"
6191 "(a or b) or c"
6192 "(a or b) and c"
6193 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
6194 --> x:JUMP_IF_FALSE_OR_POP z
6195 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
6196 --> x:POP_JUMP_IF_FALSE y+1
6197 where y+1 is the instruction following the second test.
6198 */
6199 case JUMP_IF_FALSE_OR_POP:
6200 switch(target->i_opcode) {
6201 case POP_JUMP_IF_FALSE:
6202 *inst = *target;
6203 break;
6204 case JUMP_ABSOLUTE:
6205 case JUMP_FORWARD:
6206 case JUMP_IF_FALSE_OR_POP:
6207 inst->i_target = target->i_target;
6208 break;
6209 case JUMP_IF_TRUE_OR_POP:
6210 assert (inst->i_target->b_iused == 1);
6211 inst->i_opcode = POP_JUMP_IF_FALSE;
6212 inst->i_target = inst->i_target->b_next;
6213 break;
6214 }
6215 break;
6216
6217 case JUMP_IF_TRUE_OR_POP:
6218 switch(target->i_opcode) {
6219 case POP_JUMP_IF_TRUE:
6220 *inst = *target;
6221 break;
6222 case JUMP_ABSOLUTE:
6223 case JUMP_FORWARD:
6224 case JUMP_IF_TRUE_OR_POP:
6225 inst->i_target = target->i_target;
6226 break;
6227 case JUMP_IF_FALSE_OR_POP:
6228 assert (inst->i_target->b_iused == 1);
6229 inst->i_opcode = POP_JUMP_IF_TRUE;
6230 inst->i_target = inst->i_target->b_next;
6231 break;
6232 }
6233 break;
6234
6235 case POP_JUMP_IF_FALSE:
6236 switch(target->i_opcode) {
6237 case JUMP_ABSOLUTE:
6238 case JUMP_FORWARD:
6239 inst->i_target = target->i_target;
6240 break;
6241 }
6242 break;
6243
6244 case POP_JUMP_IF_TRUE:
6245 switch(target->i_opcode) {
6246 case JUMP_ABSOLUTE:
6247 case JUMP_FORWARD:
6248 inst->i_target = target->i_target;
6249 break;
6250 }
6251 break;
6252
6253 case JUMP_ABSOLUTE:
6254 case JUMP_FORWARD:
6255 switch(target->i_opcode) {
6256 case JUMP_FORWARD:
6257 inst->i_target = target->i_target;
6258 break;
6259 case JUMP_ABSOLUTE:
6260 case RETURN_VALUE:
6261 case RERAISE:
6262 case RAISE_VARARGS:
6263 lineno = inst->i_lineno;
6264 *inst = *target;
6265 inst->i_lineno = lineno;
6266 break;
6267 }
6268 break;
6269 }
6270 }
6271 return 0;
6272error:
6273 return -1;
6274}
6275
6276
6277static void
6278clean_basic_block(basicblock *bb) {
6279 /* Remove NOPs and any code following a return or re-raise. */
6280 int dest = 0;
6281 for (int src = 0; src < bb->b_iused; src++) {
6282 switch(bb->b_instr[src].i_opcode) {
6283 case NOP:
6284 /* skip */
6285 break;
6286 case RETURN_VALUE:
6287 case RERAISE:
6288 bb->b_next = NULL;
6289 bb->b_instr[dest] = bb->b_instr[src];
6290 dest++;
6291 goto end;
6292 default:
6293 if (dest != src) {
6294 bb->b_instr[dest] = bb->b_instr[src];
6295 }
6296 dest++;
6297 break;
6298 }
6299 }
6300end:
6301 assert(dest <= bb->b_iused);
6302 bb->b_iused = dest;
6303}
6304
6305static int
6306mark_reachable(struct assembler *a) {
6307 basicblock **stack, **sp;
6308 sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks);
6309 if (stack == NULL) {
6310 return -1;
6311 }
6312 basicblock *entry = a->a_reverse_postorder[0];
6313 entry->b_reachable = 1;
6314 *sp++ = entry;
6315 while (sp > stack) {
6316 basicblock *b = *(--sp);
6317 if (b->b_next && b->b_next->b_reachable == 0) {
6318 b->b_next->b_reachable = 1;
6319 *sp++ = b->b_next;
6320 }
6321 for (int i = 0; i < b->b_iused; i++) {
6322 basicblock *target;
Mark Shannon582aaf12020-08-04 17:30:11 +01006323 if (is_jump(&b->b_instr[i])) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006324 target = b->b_instr[i].i_target;
6325 if (target->b_reachable == 0) {
6326 target->b_reachable = 1;
6327 *sp++ = target;
6328 }
6329 }
6330 }
6331 }
6332 PyObject_Free(stack);
6333 return 0;
6334}
6335
6336
6337/* Perform basic peephole optimizations on a control flow graph.
6338 The consts object should still be in list form to allow new constants
6339 to be appended.
6340
6341 All transformations keep the code size the same or smaller.
6342 For those that reduce size, the gaps are initially filled with
6343 NOPs. Later those NOPs are removed.
6344*/
6345
6346static int
6347optimize_cfg(struct assembler *a, PyObject *consts)
6348{
6349 for (int i = 0; i < a->a_nblocks; i++) {
6350 if (optimize_basic_block(a->a_reverse_postorder[i], consts)) {
6351 return -1;
6352 }
6353 clean_basic_block(a->a_reverse_postorder[i]);
6354 assert(a->a_reverse_postorder[i]->b_reachable == 0);
6355 }
6356 if (mark_reachable(a)) {
6357 return -1;
6358 }
6359 /* Delete unreachable instructions */
6360 for (int i = 0; i < a->a_nblocks; i++) {
6361 if (a->a_reverse_postorder[i]->b_reachable == 0) {
6362 a->a_reverse_postorder[i]->b_iused = 0;
6363 }
6364 }
6365 return 0;
6366}
6367
6368/* Retained for API compatibility.
6369 * Optimization is now done in optimize_cfg */
6370
6371PyObject *
6372PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
6373 PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
6374{
6375 Py_INCREF(code);
6376 return code;
6377}
6378