blob: 2c5326686f8663c519e5d3f1d86bb1ec5ca3c97b [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 Shannonfee55262019-11-21 09:11:43 +0000111enum fblocktype { WHILE_LOOP, FOR_LOOP, EXCEPT, FINALLY_TRY, FINALLY_END,
112 WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE };
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);
Brandt Bucher6dd9b642019-11-25 22:16:53 -0800225static int are_all_items_const(asdl_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,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 asdl_seq *args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400233 asdl_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,
239 asdl_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,
245 asdl_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);
492 v = PyDict_GetItem(src, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 assert(PyLong_Check(v));
494 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; \
1528 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1529 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; \
1538 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1539 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
1562find_ann(asdl_seq *stmts)
1563{
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) {
1626 PyErr_SetString(PyExc_SyntaxError,
1627 "too many statically nested blocks");
1628 return 0;
1629 }
1630 f = &c->u->u_fblock[c->u->u_nfblocks++];
1631 f->fb_type = t;
1632 f->fb_block = b;
1633 f->fb_exit = exit;
Mark Shannonfee55262019-11-21 09:11:43 +00001634 f->fb_datum = datum;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001635 return 1;
1636}
1637
1638static void
1639compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
1640{
1641 struct compiler_unit *u = c->u;
1642 assert(u->u_nfblocks > 0);
1643 u->u_nfblocks--;
1644 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
1645 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
1646}
1647
Mark Shannonfee55262019-11-21 09:11:43 +00001648static int
1649compiler_call_exit_with_nones(struct compiler *c) {
1650 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1651 ADDOP(c, DUP_TOP);
1652 ADDOP(c, DUP_TOP);
1653 ADDOP_I(c, CALL_FUNCTION, 3);
1654 return 1;
1655}
1656
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001657/* Unwind a frame block. If preserve_tos is true, the TOS before
Mark Shannonfee55262019-11-21 09:11:43 +00001658 * popping the blocks will be restored afterwards, unless another
1659 * return, break or continue is found. In which case, the TOS will
1660 * be popped.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001661 */
1662static int
1663compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
1664 int preserve_tos)
1665{
1666 switch (info->fb_type) {
1667 case WHILE_LOOP:
1668 return 1;
1669
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001670 case FOR_LOOP:
1671 /* Pop the iterator */
1672 if (preserve_tos) {
1673 ADDOP(c, ROT_TWO);
1674 }
1675 ADDOP(c, POP_TOP);
1676 return 1;
1677
1678 case EXCEPT:
1679 ADDOP(c, POP_BLOCK);
1680 return 1;
1681
1682 case FINALLY_TRY:
1683 ADDOP(c, POP_BLOCK);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001684 if (preserve_tos) {
Mark Shannonfee55262019-11-21 09:11:43 +00001685 if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) {
1686 return 0;
1687 }
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001688 }
Mark Shannon88dce262019-12-30 09:53:36 +00001689 /* Emit the finally block, restoring the line number when done */
1690 int saved_lineno = c->u->u_lineno;
Mark Shannonfee55262019-11-21 09:11:43 +00001691 VISIT_SEQ(c, stmt, info->fb_datum);
Mark Shannon88dce262019-12-30 09:53:36 +00001692 c->u->u_lineno = saved_lineno;
Mark Shannonfee55262019-11-21 09:11:43 +00001693 if (preserve_tos) {
1694 compiler_pop_fblock(c, POP_VALUE, NULL);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001695 }
1696 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001697
Mark Shannonfee55262019-11-21 09:11:43 +00001698 case FINALLY_END:
1699 if (preserve_tos) {
1700 ADDOP(c, ROT_FOUR);
1701 }
1702 ADDOP(c, POP_TOP);
1703 ADDOP(c, POP_TOP);
1704 ADDOP(c, POP_TOP);
1705 if (preserve_tos) {
1706 ADDOP(c, ROT_FOUR);
1707 }
1708 ADDOP(c, POP_EXCEPT);
1709 return 1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001710
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001711 case WITH:
1712 case ASYNC_WITH:
1713 ADDOP(c, POP_BLOCK);
1714 if (preserve_tos) {
1715 ADDOP(c, ROT_TWO);
1716 }
Mark Shannonfee55262019-11-21 09:11:43 +00001717 if(!compiler_call_exit_with_nones(c)) {
1718 return 0;
1719 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001720 if (info->fb_type == ASYNC_WITH) {
1721 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001722 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001723 ADDOP(c, YIELD_FROM);
1724 }
Mark Shannonfee55262019-11-21 09:11:43 +00001725 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001726 return 1;
1727
1728 case HANDLER_CLEANUP:
Mark Shannonfee55262019-11-21 09:11:43 +00001729 if (info->fb_datum) {
1730 ADDOP(c, POP_BLOCK);
1731 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001732 if (preserve_tos) {
1733 ADDOP(c, ROT_FOUR);
1734 }
Mark Shannonfee55262019-11-21 09:11:43 +00001735 ADDOP(c, POP_EXCEPT);
1736 if (info->fb_datum) {
1737 ADDOP_LOAD_CONST(c, Py_None);
1738 compiler_nameop(c, info->fb_datum, Store);
1739 compiler_nameop(c, info->fb_datum, Del);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001740 }
Mark Shannonfee55262019-11-21 09:11:43 +00001741 return 1;
1742
1743 case POP_VALUE:
1744 if (preserve_tos) {
1745 ADDOP(c, ROT_TWO);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001746 }
Mark Shannonfee55262019-11-21 09:11:43 +00001747 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001748 return 1;
1749 }
1750 Py_UNREACHABLE();
1751}
1752
Mark Shannonfee55262019-11-21 09:11:43 +00001753/** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */
1754static int
1755compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) {
1756 if (c->u->u_nfblocks == 0) {
1757 return 1;
1758 }
1759 struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1];
1760 if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) {
1761 *loop = top;
1762 return 1;
1763 }
1764 struct fblockinfo copy = *top;
1765 c->u->u_nfblocks--;
1766 if (!compiler_unwind_fblock(c, &copy, preserve_tos)) {
1767 return 0;
1768 }
1769 if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) {
1770 return 0;
1771 }
1772 c->u->u_fblock[c->u->u_nfblocks] = copy;
1773 c->u->u_nfblocks++;
1774 return 1;
1775}
1776
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001777/* Compile a sequence of statements, checking for a docstring
1778 and for annotations. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001779
1780static int
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001781compiler_body(struct compiler *c, asdl_seq *stmts)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001783 int i = 0;
1784 stmt_ty st;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001785 PyObject *docstring;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001786
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001787 /* Set current line number to the line number of first statement.
1788 This way line number for SETUP_ANNOTATIONS will always
1789 coincide with the line number of first "real" statement in module.
Hansraj Das01171eb2019-10-09 07:54:02 +05301790 If body is empty, then lineno will be set later in assemble. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001791 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) {
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001792 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001793 SET_LOC(c, st);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001794 }
1795 /* Every annotated class and module should have __annotations__. */
1796 if (find_ann(stmts)) {
1797 ADDOP(c, SETUP_ANNOTATIONS);
1798 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001799 if (!asdl_seq_LEN(stmts))
1800 return 1;
INADA Naokicb41b272017-02-23 00:31:59 +09001801 /* if not -OO mode, set docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001802 if (c->c_optimize < 2) {
1803 docstring = _PyAST_GetDocString(stmts);
1804 if (docstring) {
1805 i = 1;
1806 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1807 assert(st->kind == Expr_kind);
1808 VISIT(c, expr, st->v.Expr.value);
1809 if (!compiler_nameop(c, __doc__, Store))
1810 return 0;
1811 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001813 for (; i < asdl_seq_LEN(stmts); i++)
1814 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001816}
1817
1818static PyCodeObject *
1819compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001820{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 PyCodeObject *co;
1822 int addNone = 1;
1823 static PyObject *module;
1824 if (!module) {
1825 module = PyUnicode_InternFromString("<module>");
1826 if (!module)
1827 return NULL;
1828 }
1829 /* Use 0 for firstlineno initially, will fixup in assemble(). */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001830 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 return NULL;
1832 switch (mod->kind) {
1833 case Module_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001834 if (!compiler_body(c, mod->v.Module.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 compiler_exit_scope(c);
1836 return 0;
1837 }
1838 break;
1839 case Interactive_kind:
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001840 if (find_ann(mod->v.Interactive.body)) {
1841 ADDOP(c, SETUP_ANNOTATIONS);
1842 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 c->c_interactive = 1;
1844 VISIT_SEQ_IN_SCOPE(c, stmt,
1845 mod->v.Interactive.body);
1846 break;
1847 case Expression_kind:
1848 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1849 addNone = 0;
1850 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 default:
1852 PyErr_Format(PyExc_SystemError,
1853 "module kind %d should not be possible",
1854 mod->kind);
1855 return 0;
1856 }
1857 co = assemble(c, addNone);
1858 compiler_exit_scope(c);
1859 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001860}
1861
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001862/* The test for LOCAL must come before the test for FREE in order to
1863 handle classes where name is both local and free. The local var is
1864 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001865*/
1866
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001867static int
1868get_ref_type(struct compiler *c, PyObject *name)
1869{
Victor Stinner0b1bc562013-05-16 22:17:17 +02001870 int scope;
Benjamin Peterson312595c2013-05-15 15:26:42 -05001871 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001872 _PyUnicode_EqualToASCIIString(name, "__class__"))
Benjamin Peterson312595c2013-05-15 15:26:42 -05001873 return CELL;
Victor Stinner0b1bc562013-05-16 22:17:17 +02001874 scope = PyST_GetScope(c->u->u_ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 if (scope == 0) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01001876 _Py_FatalErrorFormat(__func__,
1877 "unknown scope for %.100s in %.100s(%s)\n"
1878 "symbols: %s\nlocals: %s\nglobals: %s",
1879 PyUnicode_AsUTF8(name),
1880 PyUnicode_AsUTF8(c->u->u_name),
1881 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)),
1882 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)),
1883 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)),
1884 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001888}
1889
1890static int
1891compiler_lookup_arg(PyObject *dict, PyObject *name)
1892{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001893 PyObject *v;
1894 v = PyDict_GetItem(dict, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001895 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001896 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001897 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001898}
1899
1900static int
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001901compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001902{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001903 Py_ssize_t i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001904 if (qualname == NULL)
1905 qualname = co->co_name;
1906
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001907 if (free) {
1908 for (i = 0; i < free; ++i) {
1909 /* Bypass com_addop_varname because it will generate
1910 LOAD_DEREF but LOAD_CLOSURE is needed.
1911 */
1912 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1913 int arg, reftype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001914
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001915 /* Special case: If a class contains a method with a
1916 free variable that has the same name as a method,
1917 the name will be considered free *and* local in the
1918 class. It should be handled by the closure, as
Min ho Kimc4cacc82019-07-31 08:16:13 +10001919 well as by the normal name lookup logic.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001920 */
1921 reftype = get_ref_type(c, name);
1922 if (reftype == CELL)
1923 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1924 else /* (reftype == FREE) */
1925 arg = compiler_lookup_arg(c->u->u_freevars, name);
1926 if (arg == -1) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01001927 _Py_FatalErrorFormat(__func__,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001928 "lookup %s in %s %d %d\n"
1929 "freevars of %s: %s\n",
1930 PyUnicode_AsUTF8(PyObject_Repr(name)),
1931 PyUnicode_AsUTF8(c->u->u_name),
1932 reftype, arg,
1933 PyUnicode_AsUTF8(co->co_name),
1934 PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars)));
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001935 }
1936 ADDOP_I(c, LOAD_CLOSURE, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001938 flags |= 0x08;
1939 ADDOP_I(c, BUILD_TUPLE, free);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001941 ADDOP_LOAD_CONST(c, (PyObject*)co);
1942 ADDOP_LOAD_CONST(c, qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001943 ADDOP_I(c, MAKE_FUNCTION, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001945}
1946
1947static int
1948compiler_decorators(struct compiler *c, asdl_seq* decos)
1949{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 if (!decos)
1953 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1956 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1957 }
1958 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001959}
1960
1961static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001962compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 asdl_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001964{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001965 /* Push a dict of keyword-only default values.
1966
1967 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
1968 */
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001969 int i;
1970 PyObject *keys = NULL;
1971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1973 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1974 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1975 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04001976 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001977 if (!mangled) {
1978 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001980 if (keys == NULL) {
1981 keys = PyList_New(1);
1982 if (keys == NULL) {
1983 Py_DECREF(mangled);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001984 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001985 }
1986 PyList_SET_ITEM(keys, 0, mangled);
1987 }
1988 else {
1989 int res = PyList_Append(keys, mangled);
1990 Py_DECREF(mangled);
1991 if (res == -1) {
1992 goto error;
1993 }
1994 }
1995 if (!compiler_visit_expr(c, default_)) {
1996 goto error;
1997 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998 }
1999 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002000 if (keys != NULL) {
2001 Py_ssize_t default_count = PyList_GET_SIZE(keys);
2002 PyObject *keys_tuple = PyList_AsTuple(keys);
2003 Py_DECREF(keys);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002004 ADDOP_LOAD_CONST_NEW(c, keys_tuple);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002005 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002006 assert(default_count > 0);
2007 return 1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002008 }
2009 else {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002010 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002011 }
2012
2013error:
2014 Py_XDECREF(keys);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002015 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002016}
2017
2018static int
Guido van Rossum95e4d582018-01-26 08:20:18 -08002019compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
2020{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +03002021 ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation));
Guido van Rossum95e4d582018-01-26 08:20:18 -08002022 return 1;
2023}
2024
2025static int
Neal Norwitzc1505362006-12-28 06:47:50 +00002026compiler_visit_argannotation(struct compiler *c, identifier id,
2027 expr_ty annotation, PyObject *names)
2028{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 if (annotation) {
Victor Stinner065efc32014-02-18 22:07:56 +01002030 PyObject *mangled;
Guido van Rossum95e4d582018-01-26 08:20:18 -08002031 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
2032 VISIT(c, annexpr, annotation)
2033 }
2034 else {
2035 VISIT(c, expr, annotation);
2036 }
Victor Stinner065efc32014-02-18 22:07:56 +01002037 mangled = _Py_Mangle(c->u->u_private, id);
Yury Selivanov34ce99f2014-02-18 12:49:41 -05002038 if (!mangled)
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002039 return 0;
Yury Selivanov34ce99f2014-02-18 12:49:41 -05002040 if (PyList_Append(names, mangled) < 0) {
2041 Py_DECREF(mangled);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002042 return 0;
Yury Selivanov34ce99f2014-02-18 12:49:41 -05002043 }
2044 Py_DECREF(mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002046 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002047}
2048
2049static int
2050compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
2051 PyObject *names)
2052{
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002053 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054 for (i = 0; i < asdl_seq_LEN(args); i++) {
2055 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002056 if (!compiler_visit_argannotation(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 c,
2058 arg->arg,
2059 arg->annotation,
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002060 names))
2061 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002063 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002064}
2065
2066static int
2067compiler_visit_annotations(struct compiler *c, arguments_ty args,
2068 expr_ty returns)
2069{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002070 /* Push arg annotation dict.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002071 The expressions are evaluated out-of-order wrt the source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00002072
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002073 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 */
2075 static identifier return_str;
2076 PyObject *names;
Victor Stinnerad9a0662013-11-19 22:23:20 +01002077 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002078 names = PyList_New(0);
2079 if (!names)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03002080 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002081
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002082 if (!compiler_visit_argannotations(c, args->args, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 goto error;
Pablo Galindoa0c01bf2019-05-31 15:19:50 +01002084 if (!compiler_visit_argannotations(c, args->posonlyargs, names))
2085 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002086 if (args->vararg && args->vararg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002087 !compiler_visit_argannotation(c, args->vararg->arg,
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002088 args->vararg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 goto error;
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002090 if (!compiler_visit_argannotations(c, args->kwonlyargs, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002092 if (args->kwarg && args->kwarg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002093 !compiler_visit_argannotation(c, args->kwarg->arg,
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002094 args->kwarg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00002096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 if (!return_str) {
2098 return_str = PyUnicode_InternFromString("return");
2099 if (!return_str)
2100 goto error;
2101 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002102 if (!compiler_visit_argannotation(c, return_str, returns, names)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 goto error;
2104 }
2105
2106 len = PyList_GET_SIZE(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107 if (len) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002108 PyObject *keytuple = PyList_AsTuple(names);
2109 Py_DECREF(names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002110 ADDOP_LOAD_CONST_NEW(c, keytuple);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002111 ADDOP_I(c, BUILD_CONST_KEY_MAP, len);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002112 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002114 else {
2115 Py_DECREF(names);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002116 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002117 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002118
2119error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002120 Py_DECREF(names);
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03002121 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002122}
2123
2124static int
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002125compiler_visit_defaults(struct compiler *c, arguments_ty args)
2126{
2127 VISIT_SEQ(c, expr, args->defaults);
2128 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
2129 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002130}
2131
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002132static Py_ssize_t
2133compiler_default_arguments(struct compiler *c, arguments_ty args)
2134{
2135 Py_ssize_t funcflags = 0;
2136 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002137 if (!compiler_visit_defaults(c, args))
2138 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002139 funcflags |= 0x01;
2140 }
2141 if (args->kwonlyargs) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002142 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002143 args->kw_defaults);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002144 if (res == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002145 return -1;
2146 }
2147 else if (res > 0) {
2148 funcflags |= 0x02;
2149 }
2150 }
2151 return funcflags;
2152}
2153
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002154static int
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002155forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx)
2156{
2157
2158 if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2159 compiler_error(c, "cannot assign to __debug__");
2160 return 1;
2161 }
2162 return 0;
2163}
2164
2165static int
2166compiler_check_debug_one_arg(struct compiler *c, arg_ty arg)
2167{
2168 if (arg != NULL) {
2169 if (forbidden_name(c, arg->arg, Store))
2170 return 0;
2171 }
2172 return 1;
2173}
2174
2175static int
2176compiler_check_debug_args_seq(struct compiler *c, asdl_seq *args)
2177{
2178 if (args != NULL) {
Pablo Galindoee40e4b2020-04-23 03:43:08 +01002179 for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002180 if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i)))
2181 return 0;
2182 }
2183 }
2184 return 1;
2185}
2186
2187static int
2188compiler_check_debug_args(struct compiler *c, arguments_ty args)
2189{
2190 if (!compiler_check_debug_args_seq(c, args->posonlyargs))
2191 return 0;
2192 if (!compiler_check_debug_args_seq(c, args->args))
2193 return 0;
2194 if (!compiler_check_debug_one_arg(c, args->vararg))
2195 return 0;
2196 if (!compiler_check_debug_args_seq(c, args->kwonlyargs))
2197 return 0;
2198 if (!compiler_check_debug_one_arg(c, args->kwarg))
2199 return 0;
2200 return 1;
2201}
2202
2203static int
Yury Selivanov75445082015-05-11 22:57:16 -04002204compiler_function(struct compiler *c, stmt_ty s, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 PyCodeObject *co;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002207 PyObject *qualname, *docstring = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002208 arguments_ty args;
2209 expr_ty returns;
2210 identifier name;
2211 asdl_seq* decos;
2212 asdl_seq *body;
INADA Naokicb41b272017-02-23 00:31:59 +09002213 Py_ssize_t i, funcflags;
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002214 int annotations;
Yury Selivanov75445082015-05-11 22:57:16 -04002215 int scope_type;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002216 int firstlineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002217
Yury Selivanov75445082015-05-11 22:57:16 -04002218 if (is_async) {
2219 assert(s->kind == AsyncFunctionDef_kind);
2220
2221 args = s->v.AsyncFunctionDef.args;
2222 returns = s->v.AsyncFunctionDef.returns;
2223 decos = s->v.AsyncFunctionDef.decorator_list;
2224 name = s->v.AsyncFunctionDef.name;
2225 body = s->v.AsyncFunctionDef.body;
2226
2227 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
2228 } else {
2229 assert(s->kind == FunctionDef_kind);
2230
2231 args = s->v.FunctionDef.args;
2232 returns = s->v.FunctionDef.returns;
2233 decos = s->v.FunctionDef.decorator_list;
2234 name = s->v.FunctionDef.name;
2235 body = s->v.FunctionDef.body;
2236
2237 scope_type = COMPILER_SCOPE_FUNCTION;
2238 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002239
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002240 if (!compiler_check_debug_args(c, args))
2241 return 0;
2242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 if (!compiler_decorators(c, decos))
2244 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002245
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002246 firstlineno = s->lineno;
2247 if (asdl_seq_LEN(decos)) {
2248 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2249 }
2250
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002251 funcflags = compiler_default_arguments(c, args);
2252 if (funcflags == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002254 }
2255
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002256 annotations = compiler_visit_annotations(c, args, returns);
2257 if (annotations == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002258 return 0;
2259 }
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002260 else if (annotations > 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002261 funcflags |= 0x04;
2262 }
2263
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002264 if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002265 return 0;
2266 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002267
INADA Naokicb41b272017-02-23 00:31:59 +09002268 /* if not -OO mode, add docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002269 if (c->c_optimize < 2) {
2270 docstring = _PyAST_GetDocString(body);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002271 }
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002272 if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 compiler_exit_scope(c);
2274 return 0;
2275 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002278 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
INADA Naokicb41b272017-02-23 00:31:59 +09002280 VISIT_SEQ_IN_SCOPE(c, stmt, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002282 qualname = c->u->u_qualname;
2283 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002285 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002286 Py_XDECREF(qualname);
2287 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002289 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002291 compiler_make_closure(c, co, funcflags, qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002292 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 /* decorators */
2296 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2297 ADDOP_I(c, CALL_FUNCTION, 1);
2298 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002299
Yury Selivanov75445082015-05-11 22:57:16 -04002300 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301}
2302
2303static int
2304compiler_class(struct compiler *c, stmt_ty s)
2305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 PyCodeObject *co;
2307 PyObject *str;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002308 int i, firstlineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002309 asdl_seq* decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 if (!compiler_decorators(c, decos))
2312 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002313
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002314 firstlineno = s->lineno;
2315 if (asdl_seq_LEN(decos)) {
2316 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2317 }
2318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 /* ultimately generate code for:
2320 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
2321 where:
2322 <func> is a function/closure created from the class body;
2323 it has a single argument (__locals__) where the dict
2324 (or MutableSequence) representing the locals is passed
2325 <name> is the class name
2326 <bases> is the positional arguments and *varargs argument
2327 <keywords> is the keyword arguments and **kwds argument
2328 This borrows from compiler_call.
2329 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002332 if (!compiler_enter_scope(c, s->v.ClassDef.name,
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002333 COMPILER_SCOPE_CLASS, (void *)s, firstlineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 return 0;
2335 /* this block represents what we do in the new scope */
2336 {
2337 /* use the class name for name mangling */
2338 Py_INCREF(s->v.ClassDef.name);
Serhiy Storchaka48842712016-04-06 09:45:48 +03002339 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 /* load (global) __name__ ... */
2341 str = PyUnicode_InternFromString("__name__");
2342 if (!str || !compiler_nameop(c, str, Load)) {
2343 Py_XDECREF(str);
2344 compiler_exit_scope(c);
2345 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002346 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 Py_DECREF(str);
2348 /* ... and store it as __module__ */
2349 str = PyUnicode_InternFromString("__module__");
2350 if (!str || !compiler_nameop(c, str, Store)) {
2351 Py_XDECREF(str);
2352 compiler_exit_scope(c);
2353 return 0;
2354 }
2355 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002356 assert(c->u->u_qualname);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002357 ADDOP_LOAD_CONST(c, c->u->u_qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002358 str = PyUnicode_InternFromString("__qualname__");
2359 if (!str || !compiler_nameop(c, str, Store)) {
2360 Py_XDECREF(str);
2361 compiler_exit_scope(c);
2362 return 0;
2363 }
2364 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 /* compile the body proper */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002366 if (!compiler_body(c, s->v.ClassDef.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 compiler_exit_scope(c);
2368 return 0;
2369 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002370 /* Return __classcell__ if it is referenced, otherwise return None */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002371 if (c->u->u_ste->ste_needs_class_closure) {
Nick Coghlan19d24672016-12-05 16:47:55 +10002372 /* Store __classcell__ into class namespace & return it */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002373 str = PyUnicode_InternFromString("__class__");
2374 if (str == NULL) {
2375 compiler_exit_scope(c);
2376 return 0;
2377 }
2378 i = compiler_lookup_arg(c->u->u_cellvars, str);
2379 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01002380 if (i < 0) {
2381 compiler_exit_scope(c);
2382 return 0;
2383 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002384 assert(i == 0);
Nick Coghlan944368e2016-09-11 14:45:49 +10002385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 ADDOP_I(c, LOAD_CLOSURE, i);
Nick Coghlan19d24672016-12-05 16:47:55 +10002387 ADDOP(c, DUP_TOP);
Nick Coghlan944368e2016-09-11 14:45:49 +10002388 str = PyUnicode_InternFromString("__classcell__");
2389 if (!str || !compiler_nameop(c, str, Store)) {
2390 Py_XDECREF(str);
2391 compiler_exit_scope(c);
2392 return 0;
2393 }
2394 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002396 else {
Nick Coghlan19d24672016-12-05 16:47:55 +10002397 /* No methods referenced __class__, so just return None */
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002398 assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002399 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson312595c2013-05-15 15:26:42 -05002400 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002401 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 /* create the code object */
2403 co = assemble(c, 1);
2404 }
2405 /* leave the new scope */
2406 compiler_exit_scope(c);
2407 if (co == NULL)
2408 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 /* 2. load the 'build_class' function */
2411 ADDOP(c, LOAD_BUILD_CLASS);
2412
2413 /* 3. load a function (or closure) made from the code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002414 compiler_make_closure(c, co, 0, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 Py_DECREF(co);
2416
2417 /* 4. load class name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002418 ADDOP_LOAD_CONST(c, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419
2420 /* 5. generate the rest of the code for the call */
2421 if (!compiler_call_helper(c, 2,
2422 s->v.ClassDef.bases,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002423 s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 return 0;
2425
2426 /* 6. apply decorators */
2427 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2428 ADDOP_I(c, CALL_FUNCTION, 1);
2429 }
2430
2431 /* 7. store into <name> */
2432 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2433 return 0;
2434 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002435}
2436
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02002437/* Return 0 if the expression is a constant value except named singletons.
2438 Return 1 otherwise. */
2439static int
2440check_is_arg(expr_ty e)
2441{
2442 if (e->kind != Constant_kind) {
2443 return 1;
2444 }
2445 PyObject *value = e->v.Constant.value;
2446 return (value == Py_None
2447 || value == Py_False
2448 || value == Py_True
2449 || value == Py_Ellipsis);
2450}
2451
2452/* Check operands of identity chacks ("is" and "is not").
2453 Emit a warning if any operand is a constant except named singletons.
2454 Return 0 on error.
2455 */
2456static int
2457check_compare(struct compiler *c, expr_ty e)
2458{
2459 Py_ssize_t i, n;
2460 int left = check_is_arg(e->v.Compare.left);
2461 n = asdl_seq_LEN(e->v.Compare.ops);
2462 for (i = 0; i < n; i++) {
2463 cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i);
2464 int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2465 if (op == Is || op == IsNot) {
2466 if (!right || !left) {
2467 const char *msg = (op == Is)
2468 ? "\"is\" with a literal. Did you mean \"==\"?"
2469 : "\"is not\" with a literal. Did you mean \"!=\"?";
2470 return compiler_warn(c, msg);
2471 }
2472 }
2473 left = right;
2474 }
2475 return 1;
2476}
2477
Mark Shannon9af0e472020-01-14 10:12:45 +00002478static int compiler_addcompare(struct compiler *c, cmpop_ty op)
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002479{
Mark Shannon9af0e472020-01-14 10:12:45 +00002480 int cmp;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002481 switch (op) {
2482 case Eq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002483 cmp = Py_EQ;
2484 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002485 case NotEq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002486 cmp = Py_NE;
2487 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002488 case Lt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002489 cmp = Py_LT;
2490 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002491 case LtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002492 cmp = Py_LE;
2493 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002494 case Gt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002495 cmp = Py_GT;
2496 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002497 case GtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002498 cmp = Py_GE;
2499 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002500 case Is:
Mark Shannon9af0e472020-01-14 10:12:45 +00002501 ADDOP_I(c, IS_OP, 0);
2502 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002503 case IsNot:
Mark Shannon9af0e472020-01-14 10:12:45 +00002504 ADDOP_I(c, IS_OP, 1);
2505 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002506 case In:
Mark Shannon9af0e472020-01-14 10:12:45 +00002507 ADDOP_I(c, CONTAINS_OP, 0);
2508 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002509 case NotIn:
Mark Shannon9af0e472020-01-14 10:12:45 +00002510 ADDOP_I(c, CONTAINS_OP, 1);
2511 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002512 default:
Mark Shannon9af0e472020-01-14 10:12:45 +00002513 Py_UNREACHABLE();
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002514 }
Mark Shannon9af0e472020-01-14 10:12:45 +00002515 ADDOP_I(c, COMPARE_OP, cmp);
2516 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002517}
2518
Mark Shannon9af0e472020-01-14 10:12:45 +00002519
2520
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002521static int
2522compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2523{
2524 switch (e->kind) {
2525 case UnaryOp_kind:
2526 if (e->v.UnaryOp.op == Not)
2527 return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2528 /* fallback to general implementation */
2529 break;
2530 case BoolOp_kind: {
2531 asdl_seq *s = e->v.BoolOp.values;
2532 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2533 assert(n >= 0);
2534 int cond2 = e->v.BoolOp.op == Or;
2535 basicblock *next2 = next;
2536 if (!cond2 != !cond) {
2537 next2 = compiler_new_block(c);
2538 if (next2 == NULL)
2539 return 0;
2540 }
2541 for (i = 0; i < n; ++i) {
2542 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2543 return 0;
2544 }
2545 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2546 return 0;
2547 if (next2 != next)
2548 compiler_use_next_block(c, next2);
2549 return 1;
2550 }
2551 case IfExp_kind: {
2552 basicblock *end, *next2;
2553 end = compiler_new_block(c);
2554 if (end == NULL)
2555 return 0;
2556 next2 = compiler_new_block(c);
2557 if (next2 == NULL)
2558 return 0;
2559 if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2560 return 0;
2561 if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2562 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01002563 ADDOP_JUMP(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002564 compiler_use_next_block(c, next2);
2565 if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2566 return 0;
2567 compiler_use_next_block(c, end);
2568 return 1;
2569 }
2570 case Compare_kind: {
2571 Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2572 if (n > 0) {
Serhiy Storchaka45835252019-02-16 08:29:46 +02002573 if (!check_compare(c, e)) {
2574 return 0;
2575 }
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002576 basicblock *cleanup = compiler_new_block(c);
2577 if (cleanup == NULL)
2578 return 0;
2579 VISIT(c, expr, e->v.Compare.left);
2580 for (i = 0; i < n; i++) {
2581 VISIT(c, expr,
2582 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2583 ADDOP(c, DUP_TOP);
2584 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00002585 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01002586 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002587 NEXT_BLOCK(c);
2588 }
2589 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00002590 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Mark Shannon582aaf12020-08-04 17:30:11 +01002591 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002592 basicblock *end = compiler_new_block(c);
2593 if (end == NULL)
2594 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01002595 ADDOP_JUMP(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002596 compiler_use_next_block(c, cleanup);
2597 ADDOP(c, POP_TOP);
2598 if (!cond) {
Mark Shannon582aaf12020-08-04 17:30:11 +01002599 ADDOP_JUMP(c, JUMP_FORWARD, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002600 }
2601 compiler_use_next_block(c, end);
2602 return 1;
2603 }
2604 /* fallback to general implementation */
2605 break;
2606 }
2607 default:
2608 /* fallback to general implementation */
2609 break;
2610 }
2611
2612 /* general implementation */
2613 VISIT(c, expr, e);
Mark Shannon582aaf12020-08-04 17:30:11 +01002614 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002615 return 1;
2616}
2617
2618static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002619compiler_ifexp(struct compiler *c, expr_ty e)
2620{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 basicblock *end, *next;
2622
2623 assert(e->kind == IfExp_kind);
2624 end = compiler_new_block(c);
2625 if (end == NULL)
2626 return 0;
2627 next = compiler_new_block(c);
2628 if (next == NULL)
2629 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002630 if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2631 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002632 VISIT(c, expr, e->v.IfExp.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002633 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002634 compiler_use_next_block(c, next);
2635 VISIT(c, expr, e->v.IfExp.orelse);
2636 compiler_use_next_block(c, end);
2637 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002638}
2639
2640static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641compiler_lambda(struct compiler *c, expr_ty e)
2642{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002643 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002644 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 static identifier name;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002646 Py_ssize_t funcflags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 arguments_ty args = e->v.Lambda.args;
2648 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002649
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002650 if (!compiler_check_debug_args(c, args))
2651 return 0;
2652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 if (!name) {
2654 name = PyUnicode_InternFromString("<lambda>");
2655 if (!name)
2656 return 0;
2657 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002658
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002659 funcflags = compiler_default_arguments(c, args);
2660 if (funcflags == -1) {
2661 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002662 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002663
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002664 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002665 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 /* Make None the first constant, so the lambda can't have a
2669 docstring. */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002670 if (compiler_add_const(c, Py_None) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002674 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2676 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2677 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002678 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 }
2680 else {
2681 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002682 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002684 qualname = c->u->u_qualname;
2685 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002686 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002687 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002689
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002690 compiler_make_closure(c, co, funcflags, qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002691 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002692 Py_DECREF(co);
2693
2694 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002695}
2696
2697static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002698compiler_if(struct compiler *c, stmt_ty s)
2699{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 basicblock *end, *next;
2701 int constant;
2702 assert(s->kind == If_kind);
2703 end = compiler_new_block(c);
2704 if (end == NULL)
2705 return 0;
2706
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02002707 constant = expr_constant(s->v.If.test);
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002708 /* constant = 0: "if 0"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002709 * constant = 1: "if 1", "if 2", ...
2710 * constant = -1: rest */
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002711 if (constant == 0) {
2712 BEGIN_DO_NOT_EMIT_BYTECODE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002713 VISIT_SEQ(c, stmt, s->v.If.body);
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002714 END_DO_NOT_EMIT_BYTECODE
2715 if (s->v.If.orelse) {
2716 VISIT_SEQ(c, stmt, s->v.If.orelse);
2717 }
2718 } else if (constant == 1) {
2719 VISIT_SEQ(c, stmt, s->v.If.body);
2720 if (s->v.If.orelse) {
2721 BEGIN_DO_NOT_EMIT_BYTECODE
2722 VISIT_SEQ(c, stmt, s->v.If.orelse);
2723 END_DO_NOT_EMIT_BYTECODE
2724 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002725 } else {
Antoine Pitroue7811fc2014-09-18 03:06:50 +02002726 if (asdl_seq_LEN(s->v.If.orelse)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002727 next = compiler_new_block(c);
2728 if (next == NULL)
2729 return 0;
2730 }
Mark Shannonfee55262019-11-21 09:11:43 +00002731 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002732 next = end;
Mark Shannonfee55262019-11-21 09:11:43 +00002733 }
2734 if (!compiler_jump_if(c, s->v.If.test, next, 0)) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002735 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002736 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002737 VISIT_SEQ(c, stmt, s->v.If.body);
Antoine Pitroue7811fc2014-09-18 03:06:50 +02002738 if (asdl_seq_LEN(s->v.If.orelse)) {
Mark Shannon582aaf12020-08-04 17:30:11 +01002739 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 compiler_use_next_block(c, next);
2741 VISIT_SEQ(c, stmt, s->v.If.orelse);
2742 }
2743 }
2744 compiler_use_next_block(c, end);
2745 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002746}
2747
2748static int
2749compiler_for(struct compiler *c, stmt_ty s)
2750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 basicblock *start, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002753 start = compiler_new_block(c);
2754 cleanup = compiler_new_block(c);
2755 end = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00002756 if (start == NULL || end == NULL || cleanup == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002758 }
2759 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002761 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 VISIT(c, expr, s->v.For.iter);
2763 ADDOP(c, GET_ITER);
2764 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01002765 ADDOP_JUMP(c, FOR_ITER, cleanup);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 VISIT(c, expr, s->v.For.target);
2767 VISIT_SEQ(c, stmt, s->v.For.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002768 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002769 compiler_use_next_block(c, cleanup);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002770
2771 compiler_pop_fblock(c, FOR_LOOP, start);
2772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 VISIT_SEQ(c, stmt, s->v.For.orelse);
2774 compiler_use_next_block(c, end);
2775 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776}
2777
Yury Selivanov75445082015-05-11 22:57:16 -04002778
2779static int
2780compiler_async_for(struct compiler *c, stmt_ty s)
2781{
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002782 basicblock *start, *except, *end;
Pablo Galindo90235812020-03-15 04:29:22 +00002783 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07002784 c->u->u_ste->ste_coroutine = 1;
2785 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
Zsolt Dollensteine2396502018-04-27 08:58:56 -07002786 return compiler_error(c, "'async for' outside async function");
2787 }
2788
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002789 start = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002790 except = compiler_new_block(c);
2791 end = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002792
Mark Shannonfee55262019-11-21 09:11:43 +00002793 if (start == NULL || except == NULL || end == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002794 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002795 }
Yury Selivanov75445082015-05-11 22:57:16 -04002796 VISIT(c, expr, s->v.AsyncFor.iter);
2797 ADDOP(c, GET_AITER);
Yury Selivanov75445082015-05-11 22:57:16 -04002798
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002799 compiler_use_next_block(c, start);
Mark Shannonfee55262019-11-21 09:11:43 +00002800 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002801 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002802 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002803 /* SETUP_FINALLY to guard the __anext__ call */
Mark Shannon582aaf12020-08-04 17:30:11 +01002804 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov75445082015-05-11 22:57:16 -04002805 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002806 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04002807 ADDOP(c, YIELD_FROM);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002808 ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */
Yury Selivanov75445082015-05-11 22:57:16 -04002809
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002810 /* Success block for __anext__ */
2811 VISIT(c, expr, s->v.AsyncFor.target);
2812 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002813 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002814
2815 compiler_pop_fblock(c, FOR_LOOP, start);
Yury Selivanov75445082015-05-11 22:57:16 -04002816
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002817 /* Except block for __anext__ */
Yury Selivanov75445082015-05-11 22:57:16 -04002818 compiler_use_next_block(c, except);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002819 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov75445082015-05-11 22:57:16 -04002820
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002821 /* `else` block */
Yury Selivanov75445082015-05-11 22:57:16 -04002822 VISIT_SEQ(c, stmt, s->v.For.orelse);
2823
2824 compiler_use_next_block(c, end);
2825
2826 return 1;
2827}
2828
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002829static int
2830compiler_while(struct compiler *c, stmt_ty s)
2831{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 basicblock *loop, *orelse, *end, *anchor = NULL;
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02002833 int constant = expr_constant(s->v.While.test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002835 if (constant == 0) {
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002836 BEGIN_DO_NOT_EMIT_BYTECODE
Pablo Galindo6c3e66a2019-10-30 11:53:26 +00002837 // Push a dummy block so the VISIT_SEQ knows that we are
2838 // inside a while loop so it can correctly evaluate syntax
2839 // errors.
Mark Shannonfee55262019-11-21 09:11:43 +00002840 if (!compiler_push_fblock(c, WHILE_LOOP, NULL, NULL, NULL)) {
Pablo Galindo6c3e66a2019-10-30 11:53:26 +00002841 return 0;
2842 }
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002843 VISIT_SEQ(c, stmt, s->v.While.body);
Pablo Galindo6c3e66a2019-10-30 11:53:26 +00002844 // Remove the dummy block now that is not needed.
2845 compiler_pop_fblock(c, WHILE_LOOP, NULL);
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002846 END_DO_NOT_EMIT_BYTECODE
2847 if (s->v.While.orelse) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002848 VISIT_SEQ(c, stmt, s->v.While.orelse);
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002849 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002850 return 1;
2851 }
2852 loop = compiler_new_block(c);
2853 end = compiler_new_block(c);
2854 if (constant == -1) {
2855 anchor = compiler_new_block(c);
2856 if (anchor == NULL)
2857 return 0;
2858 }
2859 if (loop == NULL || end == NULL)
2860 return 0;
2861 if (s->v.While.orelse) {
2862 orelse = compiler_new_block(c);
2863 if (orelse == NULL)
2864 return 0;
2865 }
2866 else
2867 orelse = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 compiler_use_next_block(c, loop);
Mark Shannonfee55262019-11-21 09:11:43 +00002870 if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 return 0;
2872 if (constant == -1) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002873 if (!compiler_jump_if(c, s->v.While.test, anchor, 0))
2874 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 }
2876 VISIT_SEQ(c, stmt, s->v.While.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002877 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 /* XXX should the two POP instructions be in a separate block
2880 if there is no else clause ?
2881 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882
Benjamin Peterson3cda0ed2014-12-13 16:06:19 -05002883 if (constant == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 compiler_use_next_block(c, anchor);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002885 compiler_pop_fblock(c, WHILE_LOOP, loop);
2886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 if (orelse != NULL) /* what if orelse is just pass? */
2888 VISIT_SEQ(c, stmt, s->v.While.orelse);
2889 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002892}
2893
2894static int
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002895compiler_return(struct compiler *c, stmt_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002896{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002897 int preserve_tos = ((s->v.Return.value != NULL) &&
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002898 (s->v.Return.value->kind != Constant_kind));
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002899 if (c->u->u_ste->ste_type != FunctionBlock)
2900 return compiler_error(c, "'return' outside function");
2901 if (s->v.Return.value != NULL &&
2902 c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2903 {
2904 return compiler_error(
2905 c, "'return' with value in async generator");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002907 if (preserve_tos) {
2908 VISIT(c, expr, s->v.Return.value);
2909 }
Mark Shannonfee55262019-11-21 09:11:43 +00002910 if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL))
2911 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002912 if (s->v.Return.value == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002913 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002914 }
2915 else if (!preserve_tos) {
2916 VISIT(c, expr, s->v.Return.value);
2917 }
2918 ADDOP(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002921}
2922
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002923static int
2924compiler_break(struct compiler *c)
2925{
Mark Shannonfee55262019-11-21 09:11:43 +00002926 struct fblockinfo *loop = NULL;
2927 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
2928 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002929 }
Mark Shannonfee55262019-11-21 09:11:43 +00002930 if (loop == NULL) {
2931 return compiler_error(c, "'break' outside loop");
2932 }
2933 if (!compiler_unwind_fblock(c, loop, 0)) {
2934 return 0;
2935 }
Mark Shannon582aaf12020-08-04 17:30:11 +01002936 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit);
Mark Shannonfee55262019-11-21 09:11:43 +00002937 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002938}
2939
2940static int
2941compiler_continue(struct compiler *c)
2942{
Mark Shannonfee55262019-11-21 09:11:43 +00002943 struct fblockinfo *loop = NULL;
2944 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
2945 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002946 }
Mark Shannonfee55262019-11-21 09:11:43 +00002947 if (loop == NULL) {
2948 return compiler_error(c, "'continue' not properly in loop");
2949 }
Mark Shannon582aaf12020-08-04 17:30:11 +01002950 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block);
Mark Shannonfee55262019-11-21 09:11:43 +00002951 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002952}
2953
2954
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002955/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002956
2957 SETUP_FINALLY L
2958 <code for body>
2959 POP_BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00002960 <code for finalbody>
2961 JUMP E
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002962 L:
2963 <code for finalbody>
Mark Shannonfee55262019-11-21 09:11:43 +00002964 E:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002965
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002966 The special instructions use the block stack. Each block
2967 stack entry contains the instruction that created it (here
2968 SETUP_FINALLY), the level of the value stack at the time the
2969 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002970
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002971 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002972 Pushes the current value stack level and the label
2973 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002974 POP_BLOCK:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002975 Pops en entry from the block stack.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002976
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002977 The block stack is unwound when an exception is raised:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002978 when a SETUP_FINALLY entry is found, the raised and the caught
2979 exceptions are pushed onto the value stack (and the exception
2980 condition is cleared), and the interpreter jumps to the label
2981 gotten from the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002982*/
2983
2984static int
2985compiler_try_finally(struct compiler *c, stmt_ty s)
2986{
Mark Shannonfee55262019-11-21 09:11:43 +00002987 basicblock *body, *end, *exit;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 body = compiler_new_block(c);
2990 end = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00002991 exit = compiler_new_block(c);
2992 if (body == NULL || end == NULL || exit == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002993 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002994
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002995 /* `try` block */
Mark Shannon582aaf12020-08-04 17:30:11 +01002996 ADDOP_JUMP(c, SETUP_FINALLY, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002997 compiler_use_next_block(c, body);
Mark Shannonfee55262019-11-21 09:11:43 +00002998 if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002999 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003000 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
3001 if (!compiler_try_except(c, s))
3002 return 0;
3003 }
3004 else {
3005 VISIT_SEQ(c, stmt, s->v.Try.body);
3006 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003007 ADDOP(c, POP_BLOCK);
Mark Shannonfee55262019-11-21 09:11:43 +00003008 compiler_pop_fblock(c, FINALLY_TRY, body);
3009 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Mark Shannon582aaf12020-08-04 17:30:11 +01003010 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00003011 /* `finally` block */
3012 compiler_use_next_block(c, end);
3013 if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL))
3014 return 0;
3015 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3016 compiler_pop_fblock(c, FINALLY_END, end);
3017 ADDOP(c, RERAISE);
3018 compiler_use_next_block(c, exit);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003019 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003020}
3021
3022/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003023 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003024 (The contents of the value stack is shown in [], with the top
3025 at the right; 'tb' is trace-back info, 'val' the exception's
3026 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003027
3028 Value stack Label Instruction Argument
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003029 [] SETUP_FINALLY L1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003030 [] <code for S>
3031 [] POP_BLOCK
3032 [] JUMP_FORWARD L0
3033
3034 [tb, val, exc] L1: DUP )
3035 [tb, val, exc, exc] <evaluate E1> )
Mark Shannon9af0e472020-01-14 10:12:45 +00003036 [tb, val, exc, exc, E1] JUMP_IF_NOT_EXC_MATCH L2 ) only if E1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 [tb, val, exc] POP
3038 [tb, val] <assign to V1> (or POP if no V1)
3039 [tb] POP
3040 [] <code for S1>
3041 JUMP_FORWARD L0
3042
3043 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003044 .............................etc.......................
3045
Mark Shannonfee55262019-11-21 09:11:43 +00003046 [tb, val, exc] Ln+1: RERAISE # re-raise exception
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003047
3048 [] L0: <next statement>
3049
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003050 Of course, parts are not generated if Vi or Ei is not present.
3051*/
3052static int
3053compiler_try_except(struct compiler *c, stmt_ty s)
3054{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003056 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058 body = compiler_new_block(c);
3059 except = compiler_new_block(c);
3060 orelse = compiler_new_block(c);
3061 end = compiler_new_block(c);
3062 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
3063 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01003064 ADDOP_JUMP(c, SETUP_FINALLY, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 compiler_use_next_block(c, body);
Mark Shannonfee55262019-11-21 09:11:43 +00003066 if (!compiler_push_fblock(c, EXCEPT, body, NULL, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003068 VISIT_SEQ(c, stmt, s->v.Try.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003069 ADDOP(c, POP_BLOCK);
3070 compiler_pop_fblock(c, EXCEPT, body);
Mark Shannon582aaf12020-08-04 17:30:11 +01003071 ADDOP_JUMP(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003072 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 compiler_use_next_block(c, except);
3074 for (i = 0; i < n; i++) {
3075 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003076 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003077 if (!handler->v.ExceptHandler.type && i < n-1)
3078 return compiler_error(c, "default 'except:' must be last");
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003079 SET_LOC(c, handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003080 except = compiler_new_block(c);
3081 if (except == NULL)
3082 return 0;
3083 if (handler->v.ExceptHandler.type) {
3084 ADDOP(c, DUP_TOP);
3085 VISIT(c, expr, handler->v.ExceptHandler.type);
Mark Shannon582aaf12020-08-04 17:30:11 +01003086 ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087 }
3088 ADDOP(c, POP_TOP);
3089 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003090 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00003091
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003092 cleanup_end = compiler_new_block(c);
3093 cleanup_body = compiler_new_block(c);
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003094 if (cleanup_end == NULL || cleanup_body == NULL) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003095 return 0;
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003096 }
Guido van Rossumb940e112007-01-10 16:19:56 +00003097
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003098 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3099 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003100
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003101 /*
3102 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003103 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003104 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003105 try:
3106 # body
3107 finally:
Chris Angelicoad098b62019-05-21 23:34:19 +10003108 name = None # in case body contains "del name"
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003109 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003110 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003111
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003112 /* second try: */
Mark Shannon582aaf12020-08-04 17:30:11 +01003113 ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003114 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003115 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003116 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003117
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003118 /* second # body */
3119 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003120 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003121 ADDOP(c, POP_BLOCK);
3122 ADDOP(c, POP_EXCEPT);
3123 /* name = None; del name */
3124 ADDOP_LOAD_CONST(c, Py_None);
3125 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3126 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Mark Shannon582aaf12020-08-04 17:30:11 +01003127 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003128
Mark Shannonfee55262019-11-21 09:11:43 +00003129 /* except: */
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003130 compiler_use_next_block(c, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003132 /* name = None; del name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003133 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003134 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003135 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136
Mark Shannonfee55262019-11-21 09:11:43 +00003137 ADDOP(c, RERAISE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 }
3139 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003140 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003141
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003142 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05003143 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003144 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145
Guido van Rossumb940e112007-01-10 16:19:56 +00003146 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003147 ADDOP(c, POP_TOP);
3148 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003149 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003150 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003152 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003153 ADDOP(c, POP_EXCEPT);
Mark Shannon582aaf12020-08-04 17:30:11 +01003154 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003155 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003156 compiler_use_next_block(c, except);
3157 }
Mark Shannonfee55262019-11-21 09:11:43 +00003158 ADDOP(c, RERAISE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003159 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003160 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 compiler_use_next_block(c, end);
3162 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003163}
3164
3165static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003166compiler_try(struct compiler *c, stmt_ty s) {
3167 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
3168 return compiler_try_finally(c, s);
3169 else
3170 return compiler_try_except(c, s);
3171}
3172
3173
3174static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003175compiler_import_as(struct compiler *c, identifier name, identifier asname)
3176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003177 /* The IMPORT_NAME opcode was already generated. This function
3178 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003180 If there is a dot in name, we need to split it and emit a
Serhiy Storchakaf93234b2017-05-09 22:31:05 +03003181 IMPORT_FROM for each name.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003182 */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003183 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
3184 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003185 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003186 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003187 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 /* Consume the base module name to get the first attribute */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003189 while (1) {
3190 Py_ssize_t pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003191 PyObject *attr;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003192 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003193 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003194 return 0;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003195 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003196 if (!attr)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003197 return 0;
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003198 ADDOP_N(c, IMPORT_FROM, attr, names);
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003199 if (dot == -1) {
3200 break;
3201 }
3202 ADDOP(c, ROT_TWO);
3203 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003204 }
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003205 if (!compiler_nameop(c, asname, Store)) {
3206 return 0;
3207 }
3208 ADDOP(c, POP_TOP);
3209 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 }
3211 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003212}
3213
3214static int
3215compiler_import(struct compiler *c, stmt_ty s)
3216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217 /* The Import node stores a module name like a.b.c as a single
3218 string. This is convenient for all cases except
3219 import a.b.c as d
3220 where we need to parse that string to extract the individual
3221 module names.
3222 XXX Perhaps change the representation to make this case simpler?
3223 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01003224 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003226 for (i = 0; i < n; i++) {
3227 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
3228 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003229
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003230 ADDOP_LOAD_CONST(c, _PyLong_Zero);
3231 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003232 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003234 if (alias->asname) {
3235 r = compiler_import_as(c, alias->name, alias->asname);
3236 if (!r)
3237 return r;
3238 }
3239 else {
3240 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003241 Py_ssize_t dot = PyUnicode_FindChar(
3242 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02003243 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003244 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02003245 if (tmp == NULL)
3246 return 0;
3247 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003248 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003249 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250 Py_DECREF(tmp);
3251 }
3252 if (!r)
3253 return r;
3254 }
3255 }
3256 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003257}
3258
3259static int
3260compiler_from_import(struct compiler *c, stmt_ty s)
3261{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003262 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003263 PyObject *names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003264 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003266 if (!empty_string) {
3267 empty_string = PyUnicode_FromString("");
3268 if (!empty_string)
3269 return 0;
3270 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003271
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003272 ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02003273
3274 names = PyTuple_New(n);
3275 if (!names)
3276 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003278 /* build up the names */
3279 for (i = 0; i < n; i++) {
3280 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3281 Py_INCREF(alias->name);
3282 PyTuple_SET_ITEM(names, i, alias->name);
3283 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003285 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003286 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003287 Py_DECREF(names);
3288 return compiler_error(c, "from __future__ imports must occur "
3289 "at the beginning of the file");
3290 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003291 ADDOP_LOAD_CONST_NEW(c, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 if (s->v.ImportFrom.module) {
3294 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
3295 }
3296 else {
3297 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
3298 }
3299 for (i = 0; i < n; i++) {
3300 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3301 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003302
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003303 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003304 assert(n == 1);
3305 ADDOP(c, IMPORT_STAR);
3306 return 1;
3307 }
3308
3309 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
3310 store_name = alias->name;
3311 if (alias->asname)
3312 store_name = alias->asname;
3313
3314 if (!compiler_nameop(c, store_name, Store)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 return 0;
3316 }
3317 }
3318 /* remove imported module */
3319 ADDOP(c, POP_TOP);
3320 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003321}
3322
3323static int
3324compiler_assert(struct compiler *c, stmt_ty s)
3325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003327
Georg Brandl8334fd92010-12-04 10:26:46 +00003328 if (c->c_optimize)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003330 if (s->v.Assert.test->kind == Tuple_kind &&
Serhiy Storchakad31e7732018-10-21 10:09:39 +03003331 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0)
3332 {
3333 if (!compiler_warn(c, "assertion is always true, "
3334 "perhaps remove parentheses?"))
3335 {
Victor Stinner14e461d2013-08-26 22:28:21 +02003336 return 0;
3337 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003338 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003339 end = compiler_new_block(c);
3340 if (end == NULL)
3341 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003342 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
3343 return 0;
Zackery Spytzce6a0702019-08-25 03:44:09 -06003344 ADDOP(c, LOAD_ASSERTION_ERROR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003345 if (s->v.Assert.msg) {
3346 VISIT(c, expr, s->v.Assert.msg);
3347 ADDOP_I(c, CALL_FUNCTION, 1);
3348 }
3349 ADDOP_I(c, RAISE_VARARGS, 1);
3350 compiler_use_next_block(c, end);
3351 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352}
3353
3354static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003355compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3356{
3357 if (c->c_interactive && c->c_nestlevel <= 1) {
3358 VISIT(c, expr, value);
3359 ADDOP(c, PRINT_EXPR);
3360 return 1;
3361 }
3362
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003363 if (value->kind == Constant_kind) {
Victor Stinner15a30952016-02-08 22:45:06 +01003364 /* ignore constant statement */
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003365 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003366 }
3367
3368 VISIT(c, expr, value);
3369 ADDOP(c, POP_TOP);
3370 return 1;
3371}
3372
3373static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003374compiler_visit_stmt(struct compiler *c, stmt_ty s)
3375{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003376 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003378 /* Always assign a lineno to the next instruction for a stmt. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003379 SET_LOC(c, s);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003381 switch (s->kind) {
3382 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04003383 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003384 case ClassDef_kind:
3385 return compiler_class(c, s);
3386 case Return_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003387 return compiler_return(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003388 case Delete_kind:
3389 VISIT_SEQ(c, expr, s->v.Delete.targets)
3390 break;
3391 case Assign_kind:
3392 n = asdl_seq_LEN(s->v.Assign.targets);
3393 VISIT(c, expr, s->v.Assign.value);
3394 for (i = 0; i < n; i++) {
3395 if (i < n - 1)
3396 ADDOP(c, DUP_TOP);
3397 VISIT(c, expr,
3398 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3399 }
3400 break;
3401 case AugAssign_kind:
3402 return compiler_augassign(c, s);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003403 case AnnAssign_kind:
3404 return compiler_annassign(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003405 case For_kind:
3406 return compiler_for(c, s);
3407 case While_kind:
3408 return compiler_while(c, s);
3409 case If_kind:
3410 return compiler_if(c, s);
3411 case Raise_kind:
3412 n = 0;
3413 if (s->v.Raise.exc) {
3414 VISIT(c, expr, s->v.Raise.exc);
3415 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003416 if (s->v.Raise.cause) {
3417 VISIT(c, expr, s->v.Raise.cause);
3418 n++;
3419 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003420 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01003421 ADDOP_I(c, RAISE_VARARGS, (int)n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003422 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003423 case Try_kind:
3424 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 case Assert_kind:
3426 return compiler_assert(c, s);
3427 case Import_kind:
3428 return compiler_import(c, s);
3429 case ImportFrom_kind:
3430 return compiler_from_import(c, s);
3431 case Global_kind:
3432 case Nonlocal_kind:
3433 break;
3434 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003435 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436 case Pass_kind:
3437 break;
3438 case Break_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003439 return compiler_break(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003440 case Continue_kind:
3441 return compiler_continue(c);
3442 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003443 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04003444 case AsyncFunctionDef_kind:
3445 return compiler_function(c, s, 1);
3446 case AsyncWith_kind:
3447 return compiler_async_with(c, s, 0);
3448 case AsyncFor_kind:
3449 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 }
Yury Selivanov75445082015-05-11 22:57:16 -04003451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003452 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003453}
3454
3455static int
3456unaryop(unaryop_ty op)
3457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003458 switch (op) {
3459 case Invert:
3460 return UNARY_INVERT;
3461 case Not:
3462 return UNARY_NOT;
3463 case UAdd:
3464 return UNARY_POSITIVE;
3465 case USub:
3466 return UNARY_NEGATIVE;
3467 default:
3468 PyErr_Format(PyExc_SystemError,
3469 "unary op %d should not be possible", op);
3470 return 0;
3471 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003472}
3473
3474static int
Andy Lester76d58772020-03-10 21:18:12 -05003475binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 switch (op) {
3478 case Add:
3479 return BINARY_ADD;
3480 case Sub:
3481 return BINARY_SUBTRACT;
3482 case Mult:
3483 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003484 case MatMult:
3485 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003486 case Div:
3487 return BINARY_TRUE_DIVIDE;
3488 case Mod:
3489 return BINARY_MODULO;
3490 case Pow:
3491 return BINARY_POWER;
3492 case LShift:
3493 return BINARY_LSHIFT;
3494 case RShift:
3495 return BINARY_RSHIFT;
3496 case BitOr:
3497 return BINARY_OR;
3498 case BitXor:
3499 return BINARY_XOR;
3500 case BitAnd:
3501 return BINARY_AND;
3502 case FloorDiv:
3503 return BINARY_FLOOR_DIVIDE;
3504 default:
3505 PyErr_Format(PyExc_SystemError,
3506 "binary op %d should not be possible", op);
3507 return 0;
3508 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003509}
3510
3511static int
Andy Lester76d58772020-03-10 21:18:12 -05003512inplace_binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003514 switch (op) {
3515 case Add:
3516 return INPLACE_ADD;
3517 case Sub:
3518 return INPLACE_SUBTRACT;
3519 case Mult:
3520 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003521 case MatMult:
3522 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003523 case Div:
3524 return INPLACE_TRUE_DIVIDE;
3525 case Mod:
3526 return INPLACE_MODULO;
3527 case Pow:
3528 return INPLACE_POWER;
3529 case LShift:
3530 return INPLACE_LSHIFT;
3531 case RShift:
3532 return INPLACE_RSHIFT;
3533 case BitOr:
3534 return INPLACE_OR;
3535 case BitXor:
3536 return INPLACE_XOR;
3537 case BitAnd:
3538 return INPLACE_AND;
3539 case FloorDiv:
3540 return INPLACE_FLOOR_DIVIDE;
3541 default:
3542 PyErr_Format(PyExc_SystemError,
3543 "inplace binary op %d should not be possible", op);
3544 return 0;
3545 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003546}
3547
3548static int
3549compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3550{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003551 int op, scope;
3552 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003553 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003555 PyObject *dict = c->u->u_names;
3556 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003557
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003558 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3559 !_PyUnicode_EqualToASCIIString(name, "True") &&
3560 !_PyUnicode_EqualToASCIIString(name, "False"));
Benjamin Peterson70b224d2012-12-06 17:49:58 -05003561
Pablo Galindoc5fc1562020-04-22 23:29:27 +01003562 if (forbidden_name(c, name, ctx))
3563 return 0;
3564
Serhiy Storchakabd6ec4d2017-12-18 14:29:12 +02003565 mangled = _Py_Mangle(c->u->u_private, name);
3566 if (!mangled)
3567 return 0;
3568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003569 op = 0;
3570 optype = OP_NAME;
3571 scope = PyST_GetScope(c->u->u_ste, mangled);
3572 switch (scope) {
3573 case FREE:
3574 dict = c->u->u_freevars;
3575 optype = OP_DEREF;
3576 break;
3577 case CELL:
3578 dict = c->u->u_cellvars;
3579 optype = OP_DEREF;
3580 break;
3581 case LOCAL:
3582 if (c->u->u_ste->ste_type == FunctionBlock)
3583 optype = OP_FAST;
3584 break;
3585 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04003586 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003587 optype = OP_GLOBAL;
3588 break;
3589 case GLOBAL_EXPLICIT:
3590 optype = OP_GLOBAL;
3591 break;
3592 default:
3593 /* scope can be 0 */
3594 break;
3595 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003597 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003598 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003600 switch (optype) {
3601 case OP_DEREF:
3602 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003603 case Load:
3604 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3605 break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003606 case Store: op = STORE_DEREF; break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003607 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 }
3609 break;
3610 case OP_FAST:
3611 switch (ctx) {
3612 case Load: op = LOAD_FAST; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003613 case Store: op = STORE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003614 case Del: op = DELETE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003615 }
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003616 ADDOP_N(c, op, mangled, varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003617 return 1;
3618 case OP_GLOBAL:
3619 switch (ctx) {
3620 case Load: op = LOAD_GLOBAL; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003621 case Store: op = STORE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003622 case Del: op = DELETE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003623 }
3624 break;
3625 case OP_NAME:
3626 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00003627 case Load: op = LOAD_NAME; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003628 case Store: op = STORE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003629 case Del: op = DELETE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003630 }
3631 break;
3632 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003634 assert(op);
Andy Lester76d58772020-03-10 21:18:12 -05003635 arg = compiler_add_o(dict, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003636 Py_DECREF(mangled);
3637 if (arg < 0)
3638 return 0;
3639 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003640}
3641
3642static int
3643compiler_boolop(struct compiler *c, expr_ty e)
3644{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003645 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003646 int jumpi;
3647 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003648 asdl_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003650 assert(e->kind == BoolOp_kind);
3651 if (e->v.BoolOp.op == And)
3652 jumpi = JUMP_IF_FALSE_OR_POP;
3653 else
3654 jumpi = JUMP_IF_TRUE_OR_POP;
3655 end = compiler_new_block(c);
3656 if (end == NULL)
3657 return 0;
3658 s = e->v.BoolOp.values;
3659 n = asdl_seq_LEN(s) - 1;
3660 assert(n >= 0);
3661 for (i = 0; i < n; ++i) {
3662 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003663 ADDOP_JUMP(c, jumpi, end);
Mark Shannon6e8128f2020-07-30 10:03:00 +01003664 basicblock *next = compiler_new_block(c);
3665 if (next == NULL) {
3666 return 0;
3667 }
3668 compiler_use_next_block(c, next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003669 }
3670 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3671 compiler_use_next_block(c, end);
3672 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003673}
3674
3675static int
Mark Shannon13bc1392020-01-23 09:25:17 +00003676starunpack_helper(struct compiler *c, asdl_seq *elts, int pushed,
3677 int build, int add, int extend, int tuple)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003678{
3679 Py_ssize_t n = asdl_seq_LEN(elts);
Mark Shannon13bc1392020-01-23 09:25:17 +00003680 Py_ssize_t i, seen_star = 0;
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003681 if (n > 2 && are_all_items_const(elts, 0, n)) {
3682 PyObject *folded = PyTuple_New(n);
3683 if (folded == NULL) {
3684 return 0;
3685 }
3686 PyObject *val;
3687 for (i = 0; i < n; i++) {
3688 val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
3689 Py_INCREF(val);
3690 PyTuple_SET_ITEM(folded, i, val);
3691 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003692 if (tuple) {
3693 ADDOP_LOAD_CONST_NEW(c, folded);
3694 } else {
3695 if (add == SET_ADD) {
3696 Py_SETREF(folded, PyFrozenSet_New(folded));
3697 if (folded == NULL) {
3698 return 0;
3699 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003700 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003701 ADDOP_I(c, build, pushed);
3702 ADDOP_LOAD_CONST_NEW(c, folded);
3703 ADDOP_I(c, extend, 1);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003704 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003705 return 1;
3706 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003707
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003708 for (i = 0; i < n; i++) {
3709 expr_ty elt = asdl_seq_GET(elts, i);
3710 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003711 seen_star = 1;
3712 }
3713 }
3714 if (seen_star) {
3715 seen_star = 0;
3716 for (i = 0; i < n; i++) {
3717 expr_ty elt = asdl_seq_GET(elts, i);
3718 if (elt->kind == Starred_kind) {
3719 if (seen_star == 0) {
3720 ADDOP_I(c, build, i+pushed);
3721 seen_star = 1;
3722 }
3723 VISIT(c, expr, elt->v.Starred.value);
3724 ADDOP_I(c, extend, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003725 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003726 else {
3727 VISIT(c, expr, elt);
3728 if (seen_star) {
3729 ADDOP_I(c, add, 1);
3730 }
3731 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003732 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003733 assert(seen_star);
3734 if (tuple) {
3735 ADDOP(c, LIST_TO_TUPLE);
3736 }
3737 }
3738 else {
3739 for (i = 0; i < n; i++) {
3740 expr_ty elt = asdl_seq_GET(elts, i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003741 VISIT(c, expr, elt);
Mark Shannon13bc1392020-01-23 09:25:17 +00003742 }
3743 if (tuple) {
3744 ADDOP_I(c, BUILD_TUPLE, n+pushed);
3745 } else {
3746 ADDOP_I(c, build, n+pushed);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003747 }
3748 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003749 return 1;
3750}
3751
3752static int
3753assignment_helper(struct compiler *c, asdl_seq *elts)
3754{
3755 Py_ssize_t n = asdl_seq_LEN(elts);
3756 Py_ssize_t i;
3757 int seen_star = 0;
3758 for (i = 0; i < n; i++) {
3759 expr_ty elt = asdl_seq_GET(elts, i);
3760 if (elt->kind == Starred_kind && !seen_star) {
3761 if ((i >= (1 << 8)) ||
3762 (n-i-1 >= (INT_MAX >> 8)))
3763 return compiler_error(c,
3764 "too many expressions in "
3765 "star-unpacking assignment");
3766 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3767 seen_star = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003768 }
3769 else if (elt->kind == Starred_kind) {
3770 return compiler_error(c,
Furkan Öndercb6534e2020-03-26 04:54:31 +03003771 "multiple starred expressions in assignment");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003772 }
3773 }
3774 if (!seen_star) {
3775 ADDOP_I(c, UNPACK_SEQUENCE, n);
3776 }
Brandt Bucherd5aa2e92020-03-07 19:44:18 -08003777 for (i = 0; i < n; i++) {
3778 expr_ty elt = asdl_seq_GET(elts, i);
3779 VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value);
3780 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003781 return 1;
3782}
3783
3784static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003785compiler_list(struct compiler *c, expr_ty e)
3786{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003787 asdl_seq *elts = e->v.List.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003788 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003789 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003790 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003791 else if (e->v.List.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003792 return starunpack_helper(c, elts, 0, BUILD_LIST,
3793 LIST_APPEND, LIST_EXTEND, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003795 else
3796 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003797 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003798}
3799
3800static int
3801compiler_tuple(struct compiler *c, expr_ty e)
3802{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003803 asdl_seq *elts = e->v.Tuple.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003804 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003805 return assignment_helper(c, elts);
3806 }
3807 else if (e->v.Tuple.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003808 return starunpack_helper(c, elts, 0, BUILD_LIST,
3809 LIST_APPEND, LIST_EXTEND, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003810 }
3811 else
3812 VISIT_SEQ(c, expr, elts);
3813 return 1;
3814}
3815
3816static int
3817compiler_set(struct compiler *c, expr_ty e)
3818{
Mark Shannon13bc1392020-01-23 09:25:17 +00003819 return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET,
3820 SET_ADD, SET_UPDATE, 0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003821}
3822
3823static int
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003824are_all_items_const(asdl_seq *seq, Py_ssize_t begin, Py_ssize_t end)
3825{
3826 Py_ssize_t i;
3827 for (i = begin; i < end; i++) {
3828 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003829 if (key == NULL || key->kind != Constant_kind)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003830 return 0;
3831 }
3832 return 1;
3833}
3834
3835static int
3836compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3837{
3838 Py_ssize_t i, n = end - begin;
3839 PyObject *keys, *key;
3840 if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) {
3841 for (i = begin; i < end; i++) {
3842 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3843 }
3844 keys = PyTuple_New(n);
3845 if (keys == NULL) {
3846 return 0;
3847 }
3848 for (i = begin; i < end; i++) {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003849 key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003850 Py_INCREF(key);
3851 PyTuple_SET_ITEM(keys, i - begin, key);
3852 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003853 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003854 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3855 }
3856 else {
3857 for (i = begin; i < end; i++) {
3858 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3859 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3860 }
3861 ADDOP_I(c, BUILD_MAP, n);
3862 }
3863 return 1;
3864}
3865
3866static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003867compiler_dict(struct compiler *c, expr_ty e)
3868{
Victor Stinner976bb402016-03-23 11:36:19 +01003869 Py_ssize_t i, n, elements;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003870 int have_dict;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003871 int is_unpacking = 0;
3872 n = asdl_seq_LEN(e->v.Dict.values);
Mark Shannon8a4cd702020-01-27 09:57:45 +00003873 have_dict = 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003874 elements = 0;
3875 for (i = 0; i < n; i++) {
3876 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003877 if (is_unpacking) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003878 if (elements) {
3879 if (!compiler_subdict(c, e, i - elements, i)) {
3880 return 0;
3881 }
3882 if (have_dict) {
3883 ADDOP_I(c, DICT_UPDATE, 1);
3884 }
3885 have_dict = 1;
3886 elements = 0;
3887 }
3888 if (have_dict == 0) {
3889 ADDOP_I(c, BUILD_MAP, 0);
3890 have_dict = 1;
3891 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003892 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Mark Shannon8a4cd702020-01-27 09:57:45 +00003893 ADDOP_I(c, DICT_UPDATE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003894 }
3895 else {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003896 if (elements == 0xFFFF) {
Pablo Galindoc51db0e2020-08-13 09:48:41 +01003897 if (!compiler_subdict(c, e, i - elements, i + 1)) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003898 return 0;
3899 }
3900 if (have_dict) {
3901 ADDOP_I(c, DICT_UPDATE, 1);
3902 }
3903 have_dict = 1;
3904 elements = 0;
3905 }
3906 else {
3907 elements++;
3908 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003909 }
3910 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003911 if (elements) {
3912 if (!compiler_subdict(c, e, n - elements, n)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003913 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003914 }
3915 if (have_dict) {
3916 ADDOP_I(c, DICT_UPDATE, 1);
3917 }
3918 have_dict = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003919 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003920 if (!have_dict) {
3921 ADDOP_I(c, BUILD_MAP, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003922 }
3923 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003924}
3925
3926static int
3927compiler_compare(struct compiler *c, expr_ty e)
3928{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003929 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003930
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02003931 if (!check_compare(c, e)) {
3932 return 0;
3933 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003934 VISIT(c, expr, e->v.Compare.left);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003935 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
3936 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
3937 if (n == 0) {
3938 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Mark Shannon9af0e472020-01-14 10:12:45 +00003939 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003940 }
3941 else {
3942 basicblock *cleanup = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003943 if (cleanup == NULL)
3944 return 0;
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003945 for (i = 0; i < n; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003946 VISIT(c, expr,
3947 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003948 ADDOP(c, DUP_TOP);
3949 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00003950 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003951 ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003952 NEXT_BLOCK(c);
3953 }
3954 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00003955 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003956 basicblock *end = compiler_new_block(c);
3957 if (end == NULL)
3958 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01003959 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003960 compiler_use_next_block(c, cleanup);
3961 ADDOP(c, ROT_TWO);
3962 ADDOP(c, POP_TOP);
3963 compiler_use_next_block(c, end);
3964 }
3965 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003966}
3967
Serhiy Storchaka62e44812019-02-16 08:12:19 +02003968static PyTypeObject *
3969infer_type(expr_ty e)
3970{
3971 switch (e->kind) {
3972 case Tuple_kind:
3973 return &PyTuple_Type;
3974 case List_kind:
3975 case ListComp_kind:
3976 return &PyList_Type;
3977 case Dict_kind:
3978 case DictComp_kind:
3979 return &PyDict_Type;
3980 case Set_kind:
3981 case SetComp_kind:
3982 return &PySet_Type;
3983 case GeneratorExp_kind:
3984 return &PyGen_Type;
3985 case Lambda_kind:
3986 return &PyFunction_Type;
3987 case JoinedStr_kind:
3988 case FormattedValue_kind:
3989 return &PyUnicode_Type;
3990 case Constant_kind:
Victor Stinnera102ed72020-02-07 02:24:48 +01003991 return Py_TYPE(e->v.Constant.value);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02003992 default:
3993 return NULL;
3994 }
3995}
3996
3997static int
3998check_caller(struct compiler *c, expr_ty e)
3999{
4000 switch (e->kind) {
4001 case Constant_kind:
4002 case Tuple_kind:
4003 case List_kind:
4004 case ListComp_kind:
4005 case Dict_kind:
4006 case DictComp_kind:
4007 case Set_kind:
4008 case SetComp_kind:
4009 case GeneratorExp_kind:
4010 case JoinedStr_kind:
4011 case FormattedValue_kind:
4012 return compiler_warn(c, "'%.200s' object is not callable; "
4013 "perhaps you missed a comma?",
4014 infer_type(e)->tp_name);
4015 default:
4016 return 1;
4017 }
4018}
4019
4020static int
4021check_subscripter(struct compiler *c, expr_ty e)
4022{
4023 PyObject *v;
4024
4025 switch (e->kind) {
4026 case Constant_kind:
4027 v = e->v.Constant.value;
4028 if (!(v == Py_None || v == Py_Ellipsis ||
4029 PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
4030 PyAnySet_Check(v)))
4031 {
4032 return 1;
4033 }
4034 /* fall through */
4035 case Set_kind:
4036 case SetComp_kind:
4037 case GeneratorExp_kind:
4038 case Lambda_kind:
4039 return compiler_warn(c, "'%.200s' object is not subscriptable; "
4040 "perhaps you missed a comma?",
4041 infer_type(e)->tp_name);
4042 default:
4043 return 1;
4044 }
4045}
4046
4047static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004048check_index(struct compiler *c, expr_ty e, expr_ty s)
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004049{
4050 PyObject *v;
4051
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004052 PyTypeObject *index_type = infer_type(s);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004053 if (index_type == NULL
4054 || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
4055 || index_type == &PySlice_Type) {
4056 return 1;
4057 }
4058
4059 switch (e->kind) {
4060 case Constant_kind:
4061 v = e->v.Constant.value;
4062 if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
4063 return 1;
4064 }
4065 /* fall through */
4066 case Tuple_kind:
4067 case List_kind:
4068 case ListComp_kind:
4069 case JoinedStr_kind:
4070 case FormattedValue_kind:
4071 return compiler_warn(c, "%.200s indices must be integers or slices, "
4072 "not %.200s; "
4073 "perhaps you missed a comma?",
4074 infer_type(e)->tp_name,
4075 index_type->tp_name);
4076 default:
4077 return 1;
4078 }
4079}
4080
Zackery Spytz97f5de02019-03-22 01:30:32 -06004081// Return 1 if the method call was optimized, -1 if not, and 0 on error.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004082static int
Yury Selivanovf2392132016-12-13 19:03:51 -05004083maybe_optimize_method_call(struct compiler *c, expr_ty e)
4084{
4085 Py_ssize_t argsl, i;
4086 expr_ty meth = e->v.Call.func;
4087 asdl_seq *args = e->v.Call.args;
4088
4089 /* Check that the call node is an attribute access, and that
4090 the call doesn't have keyword parameters. */
4091 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
4092 asdl_seq_LEN(e->v.Call.keywords))
4093 return -1;
4094
4095 /* Check that there are no *varargs types of arguments. */
4096 argsl = asdl_seq_LEN(args);
4097 for (i = 0; i < argsl; i++) {
4098 expr_ty elt = asdl_seq_GET(args, i);
4099 if (elt->kind == Starred_kind) {
4100 return -1;
4101 }
4102 }
4103
4104 /* Alright, we can optimize the code. */
4105 VISIT(c, expr, meth->v.Attribute.value);
4106 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
4107 VISIT_SEQ(c, expr, e->v.Call.args);
4108 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
4109 return 1;
4110}
4111
4112static int
Zackery Spytz08050e92020-04-06 00:47:47 -06004113validate_keywords(struct compiler *c, asdl_seq *keywords)
4114{
4115 Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
4116 for (Py_ssize_t i = 0; i < nkeywords; i++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004117 keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
4118 if (key->arg == NULL) {
4119 continue;
4120 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +01004121 if (forbidden_name(c, key->arg, Store)) {
4122 return -1;
4123 }
Zackery Spytz08050e92020-04-06 00:47:47 -06004124 for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004125 keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
4126 if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
4127 PyObject *msg = PyUnicode_FromFormat("keyword argument repeated: %U", key->arg);
4128 if (msg == NULL) {
4129 return -1;
4130 }
4131 c->u->u_col_offset = other->col_offset;
4132 compiler_error(c, PyUnicode_AsUTF8(msg));
4133 Py_DECREF(msg);
4134 return -1;
4135 }
4136 }
4137 }
4138 return 0;
4139}
4140
4141static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004142compiler_call(struct compiler *c, expr_ty e)
4143{
Zackery Spytz97f5de02019-03-22 01:30:32 -06004144 int ret = maybe_optimize_method_call(c, e);
4145 if (ret >= 0) {
4146 return ret;
4147 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004148 if (!check_caller(c, e->v.Call.func)) {
4149 return 0;
4150 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004151 VISIT(c, expr, e->v.Call.func);
4152 return compiler_call_helper(c, 0,
4153 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004154 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004155}
4156
Eric V. Smith235a6f02015-09-19 14:51:32 -04004157static int
4158compiler_joined_str(struct compiler *c, expr_ty e)
4159{
Eric V. Smith235a6f02015-09-19 14:51:32 -04004160 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004161 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1)
4162 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
Eric V. Smith235a6f02015-09-19 14:51:32 -04004163 return 1;
4164}
4165
Eric V. Smitha78c7952015-11-03 12:45:05 -05004166/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004167static int
4168compiler_formatted_value(struct compiler *c, expr_ty e)
4169{
Eric V. Smitha78c7952015-11-03 12:45:05 -05004170 /* Our oparg encodes 2 pieces of information: the conversion
4171 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04004172
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004173 Convert the conversion char to 3 bits:
4174 : 000 0x0 FVC_NONE The default if nothing specified.
Eric V. Smitha78c7952015-11-03 12:45:05 -05004175 !s : 001 0x1 FVC_STR
4176 !r : 010 0x2 FVC_REPR
4177 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04004178
Eric V. Smitha78c7952015-11-03 12:45:05 -05004179 next bit is whether or not we have a format spec:
4180 yes : 100 0x4
4181 no : 000 0x0
4182 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004183
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004184 int conversion = e->v.FormattedValue.conversion;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004185 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004186
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004187 /* The expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004188 VISIT(c, expr, e->v.FormattedValue.value);
4189
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004190 switch (conversion) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004191 case 's': oparg = FVC_STR; break;
4192 case 'r': oparg = FVC_REPR; break;
4193 case 'a': oparg = FVC_ASCII; break;
4194 case -1: oparg = FVC_NONE; break;
4195 default:
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004196 PyErr_Format(PyExc_SystemError,
4197 "Unrecognized conversion character %d", conversion);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004198 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004199 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004200 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004201 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004202 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004203 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004204 }
4205
Eric V. Smitha78c7952015-11-03 12:45:05 -05004206 /* And push our opcode and oparg */
4207 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004208
Eric V. Smith235a6f02015-09-19 14:51:32 -04004209 return 1;
4210}
4211
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004212static int
4213compiler_subkwargs(struct compiler *c, asdl_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
4214{
4215 Py_ssize_t i, n = end - begin;
4216 keyword_ty kw;
4217 PyObject *keys, *key;
4218 assert(n > 0);
4219 if (n > 1) {
4220 for (i = begin; i < end; i++) {
4221 kw = asdl_seq_GET(keywords, i);
4222 VISIT(c, expr, kw->value);
4223 }
4224 keys = PyTuple_New(n);
4225 if (keys == NULL) {
4226 return 0;
4227 }
4228 for (i = begin; i < end; i++) {
4229 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
4230 Py_INCREF(key);
4231 PyTuple_SET_ITEM(keys, i - begin, key);
4232 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004233 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004234 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
4235 }
4236 else {
4237 /* a for loop only executes once */
4238 for (i = begin; i < end; i++) {
4239 kw = asdl_seq_GET(keywords, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004240 ADDOP_LOAD_CONST(c, kw->arg);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004241 VISIT(c, expr, kw->value);
4242 }
4243 ADDOP_I(c, BUILD_MAP, n);
4244 }
4245 return 1;
4246}
4247
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004248/* shared code between compiler_call and compiler_class */
4249static int
4250compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01004251 int n, /* Args already pushed */
Victor Stinnerad9a0662013-11-19 22:23:20 +01004252 asdl_seq *args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004253 asdl_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004254{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004255 Py_ssize_t i, nseen, nelts, nkwelts;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004256
Pablo Galindo254ec782020-04-03 20:37:13 +01004257 if (validate_keywords(c, keywords) == -1) {
4258 return 0;
4259 }
4260
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004261 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004262 nkwelts = asdl_seq_LEN(keywords);
4263
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004264 for (i = 0; i < nelts; i++) {
4265 expr_ty elt = asdl_seq_GET(args, i);
4266 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00004267 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004268 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004269 }
4270 for (i = 0; i < nkwelts; i++) {
4271 keyword_ty kw = asdl_seq_GET(keywords, i);
4272 if (kw->arg == NULL) {
4273 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004274 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004275 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004276
Mark Shannon13bc1392020-01-23 09:25:17 +00004277 /* No * or ** args, so can use faster calling sequence */
4278 for (i = 0; i < nelts; i++) {
4279 expr_ty elt = asdl_seq_GET(args, i);
4280 assert(elt->kind != Starred_kind);
4281 VISIT(c, expr, elt);
4282 }
4283 if (nkwelts) {
4284 PyObject *names;
4285 VISIT_SEQ(c, keyword, keywords);
4286 names = PyTuple_New(nkwelts);
4287 if (names == NULL) {
4288 return 0;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004289 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004290 for (i = 0; i < nkwelts; i++) {
4291 keyword_ty kw = asdl_seq_GET(keywords, i);
4292 Py_INCREF(kw->arg);
4293 PyTuple_SET_ITEM(names, i, kw->arg);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004294 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004295 ADDOP_LOAD_CONST_NEW(c, names);
4296 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
4297 return 1;
4298 }
4299 else {
4300 ADDOP_I(c, CALL_FUNCTION, n + nelts);
4301 return 1;
4302 }
4303
4304ex_call:
4305
4306 /* Do positional arguments. */
4307 if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
4308 VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
4309 }
4310 else if (starunpack_helper(c, args, n, BUILD_LIST,
4311 LIST_APPEND, LIST_EXTEND, 1) == 0) {
4312 return 0;
4313 }
4314 /* Then keyword arguments */
4315 if (nkwelts) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004316 /* Has a new dict been pushed */
4317 int have_dict = 0;
Mark Shannon13bc1392020-01-23 09:25:17 +00004318
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004319 nseen = 0; /* the number of keyword arguments on the stack following */
4320 for (i = 0; i < nkwelts; i++) {
4321 keyword_ty kw = asdl_seq_GET(keywords, i);
4322 if (kw->arg == NULL) {
4323 /* A keyword argument unpacking. */
4324 if (nseen) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004325 if (!compiler_subkwargs(c, keywords, i - nseen, i)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004326 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004327 }
Mark Shannondb64f122020-06-01 10:42:42 +01004328 if (have_dict) {
4329 ADDOP_I(c, DICT_MERGE, 1);
4330 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004331 have_dict = 1;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004332 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004333 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004334 if (!have_dict) {
4335 ADDOP_I(c, BUILD_MAP, 0);
4336 have_dict = 1;
4337 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004338 VISIT(c, expr, kw->value);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004339 ADDOP_I(c, DICT_MERGE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004340 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004341 else {
4342 nseen++;
4343 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004344 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004345 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004346 /* Pack up any trailing keyword arguments. */
Mark Shannon8a4cd702020-01-27 09:57:45 +00004347 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004348 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004349 }
4350 if (have_dict) {
4351 ADDOP_I(c, DICT_MERGE, 1);
4352 }
4353 have_dict = 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004354 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004355 assert(have_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004356 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004357 ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0);
4358 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004359}
4360
Nick Coghlan650f0d02007-04-15 12:05:43 +00004361
4362/* List and set comprehensions and generator expressions work by creating a
4363 nested function to perform the actual iteration. This means that the
4364 iteration variables don't leak into the current scope.
4365 The defined function is called immediately following its definition, with the
4366 result of that call being the result of the expression.
4367 The LC/SC version returns the populated container, while the GE version is
4368 flagged in symtable.c as a generator, so it returns the generator object
4369 when the function is called.
Nick Coghlan650f0d02007-04-15 12:05:43 +00004370
4371 Possible cleanups:
4372 - iterate over the generator sequence instead of using recursion
4373*/
4374
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004375
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004376static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004377compiler_comprehension_generator(struct compiler *c,
4378 asdl_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004379 int depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004380 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004381{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004382 comprehension_ty gen;
4383 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4384 if (gen->is_async) {
4385 return compiler_async_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004386 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004387 } else {
4388 return compiler_sync_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004389 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004390 }
4391}
4392
4393static int
4394compiler_sync_comprehension_generator(struct compiler *c,
4395 asdl_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004396 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004397 expr_ty elt, expr_ty val, int type)
4398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004399 /* generate code for the iterator, then each of the ifs,
4400 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004402 comprehension_ty gen;
4403 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004404 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004406 start = compiler_new_block(c);
4407 skip = compiler_new_block(c);
4408 if_cleanup = compiler_new_block(c);
4409 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004411 if (start == NULL || skip == NULL || if_cleanup == NULL ||
4412 anchor == NULL)
4413 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004415 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004417 if (gen_index == 0) {
4418 /* Receive outermost iter as an implicit argument */
4419 c->u->u_argcount = 1;
4420 ADDOP_I(c, LOAD_FAST, 0);
4421 }
4422 else {
4423 /* Sub-iter - calculate on the fly */
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004424 /* Fast path for the temporary variable assignment idiom:
4425 for y in [f(x)]
4426 */
4427 asdl_seq *elts;
4428 switch (gen->iter->kind) {
4429 case List_kind:
4430 elts = gen->iter->v.List.elts;
4431 break;
4432 case Tuple_kind:
4433 elts = gen->iter->v.Tuple.elts;
4434 break;
4435 default:
4436 elts = NULL;
4437 }
4438 if (asdl_seq_LEN(elts) == 1) {
4439 expr_ty elt = asdl_seq_GET(elts, 0);
4440 if (elt->kind != Starred_kind) {
4441 VISIT(c, expr, elt);
4442 start = NULL;
4443 }
4444 }
4445 if (start) {
4446 VISIT(c, expr, gen->iter);
4447 ADDOP(c, GET_ITER);
4448 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004449 }
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004450 if (start) {
4451 depth++;
4452 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01004453 ADDOP_JUMP(c, FOR_ITER, anchor);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004454 NEXT_BLOCK(c);
4455 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004456 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004458 /* XXX this needs to be cleaned up...a lot! */
4459 n = asdl_seq_LEN(gen->ifs);
4460 for (i = 0; i < n; i++) {
4461 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004462 if (!compiler_jump_if(c, e, if_cleanup, 0))
4463 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004464 NEXT_BLOCK(c);
4465 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004467 if (++gen_index < asdl_seq_LEN(generators))
4468 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004469 generators, gen_index, depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004470 elt, val, type))
4471 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004473 /* only append after the last for generator */
4474 if (gen_index >= asdl_seq_LEN(generators)) {
4475 /* comprehension specific code */
4476 switch (type) {
4477 case COMP_GENEXP:
4478 VISIT(c, expr, elt);
4479 ADDOP(c, YIELD_VALUE);
4480 ADDOP(c, POP_TOP);
4481 break;
4482 case COMP_LISTCOMP:
4483 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004484 ADDOP_I(c, LIST_APPEND, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004485 break;
4486 case COMP_SETCOMP:
4487 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004488 ADDOP_I(c, SET_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 break;
4490 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004491 /* With '{k: v}', k is evaluated before v, so we do
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004492 the same. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004493 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004494 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004495 ADDOP_I(c, MAP_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004496 break;
4497 default:
4498 return 0;
4499 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 compiler_use_next_block(c, skip);
4502 }
4503 compiler_use_next_block(c, if_cleanup);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004504 if (start) {
Mark Shannon582aaf12020-08-04 17:30:11 +01004505 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004506 compiler_use_next_block(c, anchor);
4507 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004508
4509 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004510}
4511
4512static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004513compiler_async_comprehension_generator(struct compiler *c,
4514 asdl_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004515 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004516 expr_ty elt, expr_ty val, int type)
4517{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004518 comprehension_ty gen;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004519 basicblock *start, *if_cleanup, *except;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004520 Py_ssize_t i, n;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004521 start = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004522 except = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004523 if_cleanup = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004524
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004525 if (start == NULL || if_cleanup == NULL || except == NULL) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004526 return 0;
4527 }
4528
4529 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4530
4531 if (gen_index == 0) {
4532 /* Receive outermost iter as an implicit argument */
4533 c->u->u_argcount = 1;
4534 ADDOP_I(c, LOAD_FAST, 0);
4535 }
4536 else {
4537 /* Sub-iter - calculate on the fly */
4538 VISIT(c, expr, gen->iter);
4539 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004540 }
4541
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004542 compiler_use_next_block(c, start);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004543
Mark Shannon582aaf12020-08-04 17:30:11 +01004544 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004545 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004546 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004547 ADDOP(c, YIELD_FROM);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004548 ADDOP(c, POP_BLOCK);
Serhiy Storchaka24d32012018-03-10 18:22:34 +02004549 VISIT(c, expr, gen->target);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004550
4551 n = asdl_seq_LEN(gen->ifs);
4552 for (i = 0; i < n; i++) {
4553 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004554 if (!compiler_jump_if(c, e, if_cleanup, 0))
4555 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004556 NEXT_BLOCK(c);
4557 }
4558
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004559 depth++;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004560 if (++gen_index < asdl_seq_LEN(generators))
4561 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004562 generators, gen_index, depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004563 elt, val, type))
4564 return 0;
4565
4566 /* only append after the last for generator */
4567 if (gen_index >= asdl_seq_LEN(generators)) {
4568 /* comprehension specific code */
4569 switch (type) {
4570 case COMP_GENEXP:
4571 VISIT(c, expr, elt);
4572 ADDOP(c, YIELD_VALUE);
4573 ADDOP(c, POP_TOP);
4574 break;
4575 case COMP_LISTCOMP:
4576 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004577 ADDOP_I(c, LIST_APPEND, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004578 break;
4579 case COMP_SETCOMP:
4580 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004581 ADDOP_I(c, SET_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004582 break;
4583 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004584 /* With '{k: v}', k is evaluated before v, so we do
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004585 the same. */
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004586 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004587 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004588 ADDOP_I(c, MAP_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004589 break;
4590 default:
4591 return 0;
4592 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004593 }
4594 compiler_use_next_block(c, if_cleanup);
Mark Shannon582aaf12020-08-04 17:30:11 +01004595 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004596
4597 compiler_use_next_block(c, except);
4598 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004599
4600 return 1;
4601}
4602
4603static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004604compiler_comprehension(struct compiler *c, expr_ty e, int type,
4605 identifier name, asdl_seq *generators, expr_ty elt,
4606 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004608 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004609 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004610 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004611 int is_async_generator = 0;
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004612 int top_level_await = IS_TOP_LEVEL_AWAIT(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004613
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004614
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004615 int is_async_function = c->u->u_ste->ste_coroutine;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004616
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004617 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004618 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4619 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004620 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004621 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004622 }
4623
4624 is_async_generator = c->u->u_ste->ste_coroutine;
4625
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004626 if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004627 compiler_error(c, "asynchronous comprehension outside of "
4628 "an asynchronous function");
4629 goto error_in_scope;
4630 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004632 if (type != COMP_GENEXP) {
4633 int op;
4634 switch (type) {
4635 case COMP_LISTCOMP:
4636 op = BUILD_LIST;
4637 break;
4638 case COMP_SETCOMP:
4639 op = BUILD_SET;
4640 break;
4641 case COMP_DICTCOMP:
4642 op = BUILD_MAP;
4643 break;
4644 default:
4645 PyErr_Format(PyExc_SystemError,
4646 "unknown comprehension type %d", type);
4647 goto error_in_scope;
4648 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004650 ADDOP_I(c, op, 0);
4651 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004652
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004653 if (!compiler_comprehension_generator(c, generators, 0, 0, elt,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004654 val, type))
4655 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004657 if (type != COMP_GENEXP) {
4658 ADDOP(c, RETURN_VALUE);
4659 }
4660
4661 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004662 qualname = c->u->u_qualname;
4663 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004664 compiler_exit_scope(c);
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004665 if (top_level_await && is_async_generator){
4666 c->u->u_ste->ste_coroutine = 1;
4667 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004668 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004669 goto error;
4670
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004671 if (!compiler_make_closure(c, co, 0, qualname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004672 goto error;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004673 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004674 Py_DECREF(co);
4675
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004676 VISIT(c, expr, outermost->iter);
4677
4678 if (outermost->is_async) {
4679 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004680 } else {
4681 ADDOP(c, GET_ITER);
4682 }
4683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004684 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004685
4686 if (is_async_generator && type != COMP_GENEXP) {
4687 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004688 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004689 ADDOP(c, YIELD_FROM);
4690 }
4691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004692 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004693error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004694 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004695error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004696 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004697 Py_XDECREF(co);
4698 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004699}
4700
4701static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004702compiler_genexp(struct compiler *c, expr_ty e)
4703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004704 static identifier name;
4705 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004706 name = PyUnicode_InternFromString("<genexpr>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004707 if (!name)
4708 return 0;
4709 }
4710 assert(e->kind == GeneratorExp_kind);
4711 return compiler_comprehension(c, e, COMP_GENEXP, name,
4712 e->v.GeneratorExp.generators,
4713 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004714}
4715
4716static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004717compiler_listcomp(struct compiler *c, expr_ty e)
4718{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004719 static identifier name;
4720 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004721 name = PyUnicode_InternFromString("<listcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004722 if (!name)
4723 return 0;
4724 }
4725 assert(e->kind == ListComp_kind);
4726 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4727 e->v.ListComp.generators,
4728 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004729}
4730
4731static int
4732compiler_setcomp(struct compiler *c, expr_ty e)
4733{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004734 static identifier name;
4735 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004736 name = PyUnicode_InternFromString("<setcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004737 if (!name)
4738 return 0;
4739 }
4740 assert(e->kind == SetComp_kind);
4741 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4742 e->v.SetComp.generators,
4743 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004744}
4745
4746
4747static int
4748compiler_dictcomp(struct compiler *c, expr_ty e)
4749{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004750 static identifier name;
4751 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004752 name = PyUnicode_InternFromString("<dictcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004753 if (!name)
4754 return 0;
4755 }
4756 assert(e->kind == DictComp_kind);
4757 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4758 e->v.DictComp.generators,
4759 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004760}
4761
4762
4763static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004764compiler_visit_keyword(struct compiler *c, keyword_ty k)
4765{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004766 VISIT(c, expr, k->value);
4767 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004768}
4769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004770/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004771 whether they are true or false.
4772
4773 Return values: 1 for true, 0 for false, -1 for non-constant.
4774 */
4775
4776static int
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02004777expr_constant(expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004778{
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004779 if (e->kind == Constant_kind) {
4780 return PyObject_IsTrue(e->v.Constant.value);
Benjamin Peterson442f2092012-12-06 17:41:04 -05004781 }
Serhiy Storchaka3325a672017-12-15 12:35:48 +02004782 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004783}
4784
Mark Shannonfee55262019-11-21 09:11:43 +00004785static int
4786compiler_with_except_finish(struct compiler *c) {
4787 basicblock *exit;
4788 exit = compiler_new_block(c);
4789 if (exit == NULL)
4790 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01004791 ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00004792 ADDOP(c, RERAISE);
4793 compiler_use_next_block(c, exit);
4794 ADDOP(c, POP_TOP);
4795 ADDOP(c, POP_TOP);
4796 ADDOP(c, POP_TOP);
4797 ADDOP(c, POP_EXCEPT);
4798 ADDOP(c, POP_TOP);
4799 return 1;
4800}
Yury Selivanov75445082015-05-11 22:57:16 -04004801
4802/*
4803 Implements the async with statement.
4804
4805 The semantics outlined in that PEP are as follows:
4806
4807 async with EXPR as VAR:
4808 BLOCK
4809
4810 It is implemented roughly as:
4811
4812 context = EXPR
4813 exit = context.__aexit__ # not calling it
4814 value = await context.__aenter__()
4815 try:
4816 VAR = value # if VAR present in the syntax
4817 BLOCK
4818 finally:
4819 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004820 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04004821 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004822 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04004823 if not (await exit(*exc)):
4824 raise
4825 */
4826static int
4827compiler_async_with(struct compiler *c, stmt_ty s, int pos)
4828{
Mark Shannonfee55262019-11-21 09:11:43 +00004829 basicblock *block, *final, *exit;
Yury Selivanov75445082015-05-11 22:57:16 -04004830 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
4831
4832 assert(s->kind == AsyncWith_kind);
Pablo Galindo90235812020-03-15 04:29:22 +00004833 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07004834 c->u->u_ste->ste_coroutine = 1;
4835 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
Zsolt Dollensteine2396502018-04-27 08:58:56 -07004836 return compiler_error(c, "'async with' outside async function");
4837 }
Yury Selivanov75445082015-05-11 22:57:16 -04004838
4839 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004840 final = compiler_new_block(c);
4841 exit = compiler_new_block(c);
4842 if (!block || !final || !exit)
Yury Selivanov75445082015-05-11 22:57:16 -04004843 return 0;
4844
4845 /* Evaluate EXPR */
4846 VISIT(c, expr, item->context_expr);
4847
4848 ADDOP(c, BEFORE_ASYNC_WITH);
4849 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004850 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004851 ADDOP(c, YIELD_FROM);
4852
Mark Shannon582aaf12020-08-04 17:30:11 +01004853 ADDOP_JUMP(c, SETUP_ASYNC_WITH, final);
Yury Selivanov75445082015-05-11 22:57:16 -04004854
4855 /* SETUP_ASYNC_WITH pushes a finally block. */
4856 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004857 if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004858 return 0;
4859 }
4860
4861 if (item->optional_vars) {
4862 VISIT(c, expr, item->optional_vars);
4863 }
4864 else {
4865 /* Discard result from context.__aenter__() */
4866 ADDOP(c, POP_TOP);
4867 }
4868
4869 pos++;
4870 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
4871 /* BLOCK code */
4872 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
4873 else if (!compiler_async_with(c, s, pos))
4874 return 0;
4875
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004876 compiler_pop_fblock(c, ASYNC_WITH, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004877 ADDOP(c, POP_BLOCK);
4878 /* End of body; start the cleanup */
Yury Selivanov75445082015-05-11 22:57:16 -04004879
Mark Shannonfee55262019-11-21 09:11:43 +00004880 /* For successful outcome:
4881 * call __exit__(None, None, None)
4882 */
4883 if(!compiler_call_exit_with_nones(c))
Yury Selivanov75445082015-05-11 22:57:16 -04004884 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00004885 ADDOP(c, GET_AWAITABLE);
4886 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4887 ADDOP(c, YIELD_FROM);
Yury Selivanov75445082015-05-11 22:57:16 -04004888
Mark Shannonfee55262019-11-21 09:11:43 +00004889 ADDOP(c, POP_TOP);
Yury Selivanov75445082015-05-11 22:57:16 -04004890
Mark Shannon582aaf12020-08-04 17:30:11 +01004891 ADDOP_JUMP(c, JUMP_ABSOLUTE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00004892
4893 /* For exceptional outcome: */
4894 compiler_use_next_block(c, final);
4895
4896 ADDOP(c, WITH_EXCEPT_START);
Yury Selivanov75445082015-05-11 22:57:16 -04004897 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004898 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004899 ADDOP(c, YIELD_FROM);
Mark Shannonfee55262019-11-21 09:11:43 +00004900 compiler_with_except_finish(c);
Yury Selivanov75445082015-05-11 22:57:16 -04004901
Mark Shannonfee55262019-11-21 09:11:43 +00004902compiler_use_next_block(c, exit);
Yury Selivanov75445082015-05-11 22:57:16 -04004903 return 1;
4904}
4905
4906
Guido van Rossumc2e20742006-02-27 22:32:47 +00004907/*
4908 Implements the with statement from PEP 343.
Guido van Rossumc2e20742006-02-27 22:32:47 +00004909 with EXPR as VAR:
4910 BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00004911 is implemented as:
4912 <code for EXPR>
4913 SETUP_WITH E
4914 <code to store to VAR> or POP_TOP
4915 <code for BLOCK>
4916 LOAD_CONST (None, None, None)
4917 CALL_FUNCTION_EX 0
4918 JUMP_FORWARD EXIT
4919 E: WITH_EXCEPT_START (calls EXPR.__exit__)
4920 POP_JUMP_IF_TRUE T:
4921 RERAISE
4922 T: POP_TOP * 3 (remove exception from stack)
4923 POP_EXCEPT
4924 POP_TOP
4925 EXIT:
Guido van Rossumc2e20742006-02-27 22:32:47 +00004926 */
Mark Shannonfee55262019-11-21 09:11:43 +00004927
Guido van Rossumc2e20742006-02-27 22:32:47 +00004928static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004929compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004930{
Mark Shannonfee55262019-11-21 09:11:43 +00004931 basicblock *block, *final, *exit;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004932 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004933
4934 assert(s->kind == With_kind);
4935
Guido van Rossumc2e20742006-02-27 22:32:47 +00004936 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004937 final = compiler_new_block(c);
4938 exit = compiler_new_block(c);
4939 if (!block || !final || !exit)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004940 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004941
Thomas Wouters477c8d52006-05-27 19:21:47 +00004942 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004943 VISIT(c, expr, item->context_expr);
Mark Shannonfee55262019-11-21 09:11:43 +00004944 /* Will push bound __exit__ */
Mark Shannon582aaf12020-08-04 17:30:11 +01004945 ADDOP_JUMP(c, SETUP_WITH, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004946
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004947 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00004948 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004949 if (!compiler_push_fblock(c, WITH, block, final, NULL)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004950 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004951 }
4952
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004953 if (item->optional_vars) {
4954 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004955 }
4956 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004957 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004958 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004959 }
4960
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004961 pos++;
4962 if (pos == asdl_seq_LEN(s->v.With.items))
4963 /* BLOCK code */
4964 VISIT_SEQ(c, stmt, s->v.With.body)
4965 else if (!compiler_with(c, s, pos))
4966 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004967
Guido van Rossumc2e20742006-02-27 22:32:47 +00004968 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004969 compiler_pop_fblock(c, WITH, block);
Mark Shannon13bc1392020-01-23 09:25:17 +00004970
Mark Shannonfee55262019-11-21 09:11:43 +00004971 /* End of body; start the cleanup. */
Mark Shannon13bc1392020-01-23 09:25:17 +00004972
Mark Shannonfee55262019-11-21 09:11:43 +00004973 /* For successful outcome:
4974 * call __exit__(None, None, None)
4975 */
4976 if (!compiler_call_exit_with_nones(c))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004977 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00004978 ADDOP(c, POP_TOP);
Mark Shannon582aaf12020-08-04 17:30:11 +01004979 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004980
Mark Shannonfee55262019-11-21 09:11:43 +00004981 /* For exceptional outcome: */
4982 compiler_use_next_block(c, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004983
Mark Shannonfee55262019-11-21 09:11:43 +00004984 ADDOP(c, WITH_EXCEPT_START);
4985 compiler_with_except_finish(c);
4986
4987 compiler_use_next_block(c, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004988 return 1;
4989}
4990
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004991static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03004992compiler_visit_expr1(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004993{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004994 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07004995 case NamedExpr_kind:
4996 VISIT(c, expr, e->v.NamedExpr.value);
4997 ADDOP(c, DUP_TOP);
4998 VISIT(c, expr, e->v.NamedExpr.target);
4999 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005000 case BoolOp_kind:
5001 return compiler_boolop(c, e);
5002 case BinOp_kind:
5003 VISIT(c, expr, e->v.BinOp.left);
5004 VISIT(c, expr, e->v.BinOp.right);
Andy Lester76d58772020-03-10 21:18:12 -05005005 ADDOP(c, binop(e->v.BinOp.op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005006 break;
5007 case UnaryOp_kind:
5008 VISIT(c, expr, e->v.UnaryOp.operand);
5009 ADDOP(c, unaryop(e->v.UnaryOp.op));
5010 break;
5011 case Lambda_kind:
5012 return compiler_lambda(c, e);
5013 case IfExp_kind:
5014 return compiler_ifexp(c, e);
5015 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005016 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005017 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005018 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005019 case GeneratorExp_kind:
5020 return compiler_genexp(c, e);
5021 case ListComp_kind:
5022 return compiler_listcomp(c, e);
5023 case SetComp_kind:
5024 return compiler_setcomp(c, e);
5025 case DictComp_kind:
5026 return compiler_dictcomp(c, e);
5027 case Yield_kind:
5028 if (c->u->u_ste->ste_type != FunctionBlock)
5029 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005030 if (e->v.Yield.value) {
5031 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005032 }
5033 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005034 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005035 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005036 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005037 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005038 case YieldFrom_kind:
5039 if (c->u->u_ste->ste_type != FunctionBlock)
5040 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04005041
5042 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
5043 return compiler_error(c, "'yield from' inside async function");
5044
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005045 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04005046 ADDOP(c, GET_YIELD_FROM_ITER);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005047 ADDOP_LOAD_CONST(c, Py_None);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005048 ADDOP(c, YIELD_FROM);
5049 break;
Yury Selivanov75445082015-05-11 22:57:16 -04005050 case Await_kind:
Pablo Galindo90235812020-03-15 04:29:22 +00005051 if (!IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005052 if (c->u->u_ste->ste_type != FunctionBlock){
5053 return compiler_error(c, "'await' outside function");
5054 }
Yury Selivanov75445082015-05-11 22:57:16 -04005055
Victor Stinner331a6a52019-05-27 16:39:22 +02005056 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005057 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
5058 return compiler_error(c, "'await' outside async function");
5059 }
5060 }
Yury Selivanov75445082015-05-11 22:57:16 -04005061
5062 VISIT(c, expr, e->v.Await.value);
5063 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005064 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005065 ADDOP(c, YIELD_FROM);
5066 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005067 case Compare_kind:
5068 return compiler_compare(c, e);
5069 case Call_kind:
5070 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005071 case Constant_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005072 ADDOP_LOAD_CONST(c, e->v.Constant.value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005073 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005074 case JoinedStr_kind:
5075 return compiler_joined_str(c, e);
5076 case FormattedValue_kind:
5077 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005078 /* The following exprs can be assignment targets. */
5079 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005080 VISIT(c, expr, e->v.Attribute.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005081 switch (e->v.Attribute.ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005082 case Load:
5083 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
5084 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005085 case Store:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005086 if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx))
5087 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005088 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5089 break;
5090 case Del:
5091 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
5092 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005093 }
5094 break;
5095 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005096 return compiler_subscript(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005097 case Starred_kind:
5098 switch (e->v.Starred.ctx) {
5099 case Store:
5100 /* In all legitimate cases, the Starred node was already replaced
5101 * by compiler_list/compiler_tuple. XXX: is that okay? */
5102 return compiler_error(c,
5103 "starred assignment target must be in a list or tuple");
5104 default:
5105 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005106 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005107 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005108 break;
5109 case Slice_kind:
5110 return compiler_slice(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005111 case Name_kind:
5112 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
5113 /* child nodes of List and Tuple will have expr_context set */
5114 case List_kind:
5115 return compiler_list(c, e);
5116 case Tuple_kind:
5117 return compiler_tuple(c, e);
5118 }
5119 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005120}
5121
5122static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005123compiler_visit_expr(struct compiler *c, expr_ty e)
5124{
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005125 int old_lineno = c->u->u_lineno;
5126 int old_col_offset = c->u->u_col_offset;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005127 SET_LOC(c, e);
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005128 int res = compiler_visit_expr1(c, e);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005129 c->u->u_lineno = old_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005130 c->u->u_col_offset = old_col_offset;
5131 return res;
5132}
5133
5134static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005135compiler_augassign(struct compiler *c, stmt_ty s)
5136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005137 assert(s->kind == AugAssign_kind);
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005138 expr_ty e = s->v.AugAssign.target;
5139
5140 int old_lineno = c->u->u_lineno;
5141 int old_col_offset = c->u->u_col_offset;
5142 SET_LOC(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005144 switch (e->kind) {
5145 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005146 VISIT(c, expr, e->v.Attribute.value);
5147 ADDOP(c, DUP_TOP);
5148 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005149 break;
5150 case Subscript_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005151 VISIT(c, expr, e->v.Subscript.value);
5152 VISIT(c, expr, e->v.Subscript.slice);
5153 ADDOP(c, DUP_TOP_TWO);
5154 ADDOP(c, BINARY_SUBSCR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005155 break;
5156 case Name_kind:
5157 if (!compiler_nameop(c, e->v.Name.id, Load))
5158 return 0;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005159 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005160 default:
5161 PyErr_Format(PyExc_SystemError,
5162 "invalid node type (%d) for augmented assignment",
5163 e->kind);
5164 return 0;
5165 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005166
5167 c->u->u_lineno = old_lineno;
5168 c->u->u_col_offset = old_col_offset;
5169
5170 VISIT(c, expr, s->v.AugAssign.value);
5171 ADDOP(c, inplace_binop(s->v.AugAssign.op));
5172
5173 SET_LOC(c, e);
5174
5175 switch (e->kind) {
5176 case Attribute_kind:
5177 ADDOP(c, ROT_TWO);
5178 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5179 break;
5180 case Subscript_kind:
5181 ADDOP(c, ROT_THREE);
5182 ADDOP(c, STORE_SUBSCR);
5183 break;
5184 case Name_kind:
5185 return compiler_nameop(c, e->v.Name.id, Store);
5186 default:
5187 Py_UNREACHABLE();
5188 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005189 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005190}
5191
5192static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005193check_ann_expr(struct compiler *c, expr_ty e)
5194{
5195 VISIT(c, expr, e);
5196 ADDOP(c, POP_TOP);
5197 return 1;
5198}
5199
5200static int
5201check_annotation(struct compiler *c, stmt_ty s)
5202{
5203 /* Annotations are only evaluated in a module or class. */
5204 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5205 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
5206 return check_ann_expr(c, s->v.AnnAssign.annotation);
5207 }
5208 return 1;
5209}
5210
5211static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005212check_ann_subscr(struct compiler *c, expr_ty e)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005213{
5214 /* We check that everything in a subscript is defined at runtime. */
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005215 switch (e->kind) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005216 case Slice_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005217 if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005218 return 0;
5219 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005220 if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) {
5221 return 0;
5222 }
5223 if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) {
5224 return 0;
5225 }
5226 return 1;
5227 case Tuple_kind: {
5228 /* extended slice */
5229 asdl_seq *elts = e->v.Tuple.elts;
5230 Py_ssize_t i, n = asdl_seq_LEN(elts);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005231 for (i = 0; i < n; i++) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005232 if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005233 return 0;
5234 }
5235 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005236 return 1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005237 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005238 default:
5239 return check_ann_expr(c, e);
5240 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005241}
5242
5243static int
5244compiler_annassign(struct compiler *c, stmt_ty s)
5245{
5246 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07005247 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005248
5249 assert(s->kind == AnnAssign_kind);
5250
5251 /* We perform the actual assignment first. */
5252 if (s->v.AnnAssign.value) {
5253 VISIT(c, expr, s->v.AnnAssign.value);
5254 VISIT(c, expr, targ);
5255 }
5256 switch (targ->kind) {
5257 case Name_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005258 if (forbidden_name(c, targ->v.Name.id, Store))
5259 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005260 /* If we have a simple name in a module or class, store annotation. */
5261 if (s->v.AnnAssign.simple &&
5262 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5263 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Guido van Rossum95e4d582018-01-26 08:20:18 -08005264 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
5265 VISIT(c, annexpr, s->v.AnnAssign.annotation)
5266 }
5267 else {
5268 VISIT(c, expr, s->v.AnnAssign.annotation);
5269 }
Mark Shannon332cd5e2018-01-30 00:41:04 +00005270 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02005271 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005272 ADDOP_LOAD_CONST_NEW(c, mangled);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005273 ADDOP(c, STORE_SUBSCR);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005274 }
5275 break;
5276 case Attribute_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005277 if (forbidden_name(c, targ->v.Attribute.attr, Store))
5278 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005279 if (!s->v.AnnAssign.value &&
5280 !check_ann_expr(c, targ->v.Attribute.value)) {
5281 return 0;
5282 }
5283 break;
5284 case Subscript_kind:
5285 if (!s->v.AnnAssign.value &&
5286 (!check_ann_expr(c, targ->v.Subscript.value) ||
5287 !check_ann_subscr(c, targ->v.Subscript.slice))) {
5288 return 0;
5289 }
5290 break;
5291 default:
5292 PyErr_Format(PyExc_SystemError,
5293 "invalid node type (%d) for annotated assignment",
5294 targ->kind);
5295 return 0;
5296 }
5297 /* Annotation is evaluated last. */
5298 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
5299 return 0;
5300 }
5301 return 1;
5302}
5303
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005304/* Raises a SyntaxError and returns 0.
5305 If something goes wrong, a different exception may be raised.
5306*/
5307
5308static int
5309compiler_error(struct compiler *c, const char *errstr)
5310{
Benjamin Peterson43b06862011-05-27 09:08:01 -05005311 PyObject *loc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005312 PyObject *u = NULL, *v = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005313
Victor Stinner14e461d2013-08-26 22:28:21 +02005314 loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005315 if (!loc) {
5316 Py_INCREF(Py_None);
5317 loc = Py_None;
5318 }
Victor Stinner14e461d2013-08-26 22:28:21 +02005319 u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04005320 c->u->u_col_offset + 1, loc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005321 if (!u)
5322 goto exit;
5323 v = Py_BuildValue("(zO)", errstr, u);
5324 if (!v)
5325 goto exit;
5326 PyErr_SetObject(PyExc_SyntaxError, v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005327 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005328 Py_DECREF(loc);
5329 Py_XDECREF(u);
5330 Py_XDECREF(v);
5331 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005332}
5333
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005334/* Emits a SyntaxWarning and returns 1 on success.
5335 If a SyntaxWarning raised as error, replaces it with a SyntaxError
5336 and returns 0.
5337*/
5338static int
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005339compiler_warn(struct compiler *c, const char *format, ...)
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005340{
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005341 va_list vargs;
5342#ifdef HAVE_STDARG_PROTOTYPES
5343 va_start(vargs, format);
5344#else
5345 va_start(vargs);
5346#endif
5347 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5348 va_end(vargs);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005349 if (msg == NULL) {
5350 return 0;
5351 }
5352 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
5353 c->u->u_lineno, NULL, NULL) < 0)
5354 {
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005355 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005356 /* Replace the SyntaxWarning exception with a SyntaxError
5357 to get a more accurate error report */
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005358 PyErr_Clear();
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005359 assert(PyUnicode_AsUTF8(msg) != NULL);
5360 compiler_error(c, PyUnicode_AsUTF8(msg));
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005361 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005362 Py_DECREF(msg);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005363 return 0;
5364 }
5365 Py_DECREF(msg);
5366 return 1;
5367}
5368
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005369static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005370compiler_subscript(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005371{
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005372 expr_context_ty ctx = e->v.Subscript.ctx;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005373 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005374
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005375 if (ctx == Load) {
5376 if (!check_subscripter(c, e->v.Subscript.value)) {
5377 return 0;
5378 }
5379 if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
5380 return 0;
5381 }
5382 }
5383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005384 switch (ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005385 case Load: op = BINARY_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005386 case Store: op = STORE_SUBSCR; break;
5387 case Del: op = DELETE_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005388 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005389 assert(op);
5390 VISIT(c, expr, e->v.Subscript.value);
5391 VISIT(c, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005392 ADDOP(c, op);
5393 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005394}
5395
5396static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005397compiler_slice(struct compiler *c, expr_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005399 int n = 2;
5400 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005402 /* only handles the cases where BUILD_SLICE is emitted */
5403 if (s->v.Slice.lower) {
5404 VISIT(c, expr, s->v.Slice.lower);
5405 }
5406 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005407 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005408 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005410 if (s->v.Slice.upper) {
5411 VISIT(c, expr, s->v.Slice.upper);
5412 }
5413 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005414 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005415 }
5416
5417 if (s->v.Slice.step) {
5418 n++;
5419 VISIT(c, expr, s->v.Slice.step);
5420 }
5421 ADDOP_I(c, BUILD_SLICE, n);
5422 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005423}
5424
Thomas Wouters89f507f2006-12-13 04:49:30 +00005425/* End of the compiler section, beginning of the assembler section */
5426
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005427/* do depth-first search of basic block graph, starting with block.
T. Wouters99b54d62019-09-12 07:05:33 -07005428 post records the block indices in post-order.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005429
5430 XXX must handle implicit jumps from one block to next
5431*/
5432
Thomas Wouters89f507f2006-12-13 04:49:30 +00005433struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005434 PyObject *a_bytecode; /* string containing bytecode */
5435 int a_offset; /* offset into bytecode */
5436 int a_nblocks; /* number of reachable blocks */
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005437 basicblock **a_reverse_postorder; /* list of blocks in dfs postorder */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005438 PyObject *a_lnotab; /* string containing lnotab */
5439 int a_lnotab_off; /* offset into lnotab */
5440 int a_lineno; /* last lineno of emitted instruction */
5441 int a_lineno_off; /* bytecode offset of last lineno */
Thomas Wouters89f507f2006-12-13 04:49:30 +00005442};
5443
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005444static void
T. Wouters99b54d62019-09-12 07:05:33 -07005445dfs(struct compiler *c, basicblock *b, struct assembler *a, int end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005446{
T. Wouters99b54d62019-09-12 07:05:33 -07005447
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005448 /* There is no real depth-first-search to do here because all the
5449 * blocks are emitted in topological order already, so we just need to
5450 * follow the b_next pointers and place them in a->a_reverse_postorder in
5451 * reverse order and make sure that the first one starts at 0. */
5452
5453 for (a->a_nblocks = 0; b != NULL; b = b->b_next) {
5454 a->a_reverse_postorder[a->a_nblocks++] = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005455 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005456}
5457
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005458Py_LOCAL_INLINE(void)
5459stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005460{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005461 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
Mark Shannonfee55262019-11-21 09:11:43 +00005462 if (b->b_startdepth < depth && b->b_startdepth < 100) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005463 assert(b->b_startdepth < 0);
5464 b->b_startdepth = depth;
5465 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02005466 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005467}
5468
5469/* Find the flow path that needs the largest stack. We assume that
5470 * cycles in the flow graph have no net effect on the stack depth.
5471 */
5472static int
5473stackdepth(struct compiler *c)
5474{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005475 basicblock *b, *entryblock = NULL;
5476 basicblock **stack, **sp;
5477 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005478 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005479 b->b_startdepth = INT_MIN;
5480 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005481 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005482 }
5483 if (!entryblock)
5484 return 0;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005485 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
5486 if (!stack) {
5487 PyErr_NoMemory();
5488 return -1;
5489 }
5490
5491 sp = stack;
5492 stackdepth_push(&sp, entryblock, 0);
5493 while (sp != stack) {
5494 b = *--sp;
5495 int depth = b->b_startdepth;
5496 assert(depth >= 0);
5497 basicblock *next = b->b_next;
5498 for (int i = 0; i < b->b_iused; i++) {
5499 struct instr *instr = &b->b_instr[i];
5500 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
5501 if (effect == PY_INVALID_STACK_EFFECT) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01005502 _Py_FatalErrorFormat(__func__,
5503 "opcode = %d", instr->i_opcode);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005504 }
5505 int new_depth = depth + effect;
5506 if (new_depth > maxdepth) {
5507 maxdepth = new_depth;
5508 }
5509 assert(depth >= 0); /* invalid code or bug in stackdepth() */
Mark Shannon582aaf12020-08-04 17:30:11 +01005510 if (is_jump(instr)) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005511 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
5512 assert(effect != PY_INVALID_STACK_EFFECT);
5513 int target_depth = depth + effect;
5514 if (target_depth > maxdepth) {
5515 maxdepth = target_depth;
5516 }
5517 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005518 stackdepth_push(&sp, instr->i_target, target_depth);
5519 }
5520 depth = new_depth;
5521 if (instr->i_opcode == JUMP_ABSOLUTE ||
5522 instr->i_opcode == JUMP_FORWARD ||
5523 instr->i_opcode == RETURN_VALUE ||
Mark Shannonfee55262019-11-21 09:11:43 +00005524 instr->i_opcode == RAISE_VARARGS ||
5525 instr->i_opcode == RERAISE)
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005526 {
5527 /* remaining code is dead */
5528 next = NULL;
5529 break;
5530 }
5531 }
5532 if (next != NULL) {
5533 stackdepth_push(&sp, next, depth);
5534 }
5535 }
5536 PyObject_Free(stack);
5537 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005538}
5539
5540static int
5541assemble_init(struct assembler *a, int nblocks, int firstlineno)
5542{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005543 memset(a, 0, sizeof(struct assembler));
5544 a->a_lineno = firstlineno;
5545 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
5546 if (!a->a_bytecode)
5547 return 0;
5548 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
5549 if (!a->a_lnotab)
5550 return 0;
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07005551 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005552 PyErr_NoMemory();
5553 return 0;
5554 }
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005555 a->a_reverse_postorder = (basicblock **)PyObject_Malloc(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005556 sizeof(basicblock *) * nblocks);
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005557 if (!a->a_reverse_postorder) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005558 PyErr_NoMemory();
5559 return 0;
5560 }
5561 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005562}
5563
5564static void
5565assemble_free(struct assembler *a)
5566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005567 Py_XDECREF(a->a_bytecode);
5568 Py_XDECREF(a->a_lnotab);
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005569 if (a->a_reverse_postorder)
5570 PyObject_Free(a->a_reverse_postorder);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005571}
5572
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005573static int
5574blocksize(basicblock *b)
5575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005576 int i;
5577 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005579 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005580 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005581 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005582}
5583
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00005584/* Appends a pair to the end of the line number table, a_lnotab, representing
5585 the instruction's bytecode offset and line number. See
5586 Objects/lnotab_notes.txt for the description of the line number table. */
Tim Peters2a7f3842001-06-09 09:26:21 +00005587
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005588static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005589assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005591 int d_bytecode, d_lineno;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005592 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005593 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005595 d_lineno = i->i_lineno - a->a_lineno;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005596 if (d_lineno == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005597 return 1;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005598 }
5599
5600 d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT);
5601 assert(d_bytecode >= 0);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00005602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005603 if (d_bytecode > 255) {
5604 int j, nbytes, ncodes = d_bytecode / 255;
5605 nbytes = a->a_lnotab_off + 2 * ncodes;
5606 len = PyBytes_GET_SIZE(a->a_lnotab);
5607 if (nbytes >= len) {
5608 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
5609 len = nbytes;
5610 else if (len <= INT_MAX / 2)
5611 len *= 2;
5612 else {
5613 PyErr_NoMemory();
5614 return 0;
5615 }
5616 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
5617 return 0;
5618 }
5619 lnotab = (unsigned char *)
5620 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
5621 for (j = 0; j < ncodes; j++) {
5622 *lnotab++ = 255;
5623 *lnotab++ = 0;
5624 }
5625 d_bytecode -= ncodes * 255;
5626 a->a_lnotab_off += ncodes * 2;
5627 }
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005628 assert(0 <= d_bytecode && d_bytecode <= 255);
5629
5630 if (d_lineno < -128 || 127 < d_lineno) {
5631 int j, nbytes, ncodes, k;
5632 if (d_lineno < 0) {
5633 k = -128;
5634 /* use division on positive numbers */
5635 ncodes = (-d_lineno) / 128;
5636 }
5637 else {
5638 k = 127;
5639 ncodes = d_lineno / 127;
5640 }
5641 d_lineno -= ncodes * k;
5642 assert(ncodes >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005643 nbytes = a->a_lnotab_off + 2 * ncodes;
5644 len = PyBytes_GET_SIZE(a->a_lnotab);
5645 if (nbytes >= len) {
5646 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
5647 len = nbytes;
5648 else if (len <= INT_MAX / 2)
5649 len *= 2;
5650 else {
5651 PyErr_NoMemory();
5652 return 0;
5653 }
5654 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
5655 return 0;
5656 }
5657 lnotab = (unsigned char *)
5658 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
5659 *lnotab++ = d_bytecode;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005660 *lnotab++ = k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005661 d_bytecode = 0;
5662 for (j = 1; j < ncodes; j++) {
5663 *lnotab++ = 0;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005664 *lnotab++ = k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005665 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005666 a->a_lnotab_off += ncodes * 2;
5667 }
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005668 assert(-128 <= d_lineno && d_lineno <= 127);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005670 len = PyBytes_GET_SIZE(a->a_lnotab);
5671 if (a->a_lnotab_off + 2 >= len) {
5672 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
5673 return 0;
5674 }
5675 lnotab = (unsigned char *)
5676 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00005677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005678 a->a_lnotab_off += 2;
5679 if (d_bytecode) {
5680 *lnotab++ = d_bytecode;
Victor Stinner4f2dab52011-05-27 16:46:51 +02005681 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005682 }
5683 else { /* First line of a block; def stmt, etc. */
5684 *lnotab++ = 0;
Victor Stinner4f2dab52011-05-27 16:46:51 +02005685 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005686 }
5687 a->a_lineno = i->i_lineno;
5688 a->a_lineno_off = a->a_offset;
5689 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005690}
5691
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005692/* assemble_emit()
5693 Extend the bytecode with a new instruction.
5694 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00005695*/
5696
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00005697static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005698assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00005699{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005700 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005701 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03005702 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005703
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005704 arg = i->i_oparg;
5705 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005706 if (i->i_lineno && !assemble_lnotab(a, i))
5707 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005708 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005709 if (len > PY_SSIZE_T_MAX / 2)
5710 return 0;
5711 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
5712 return 0;
5713 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03005714 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005715 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005716 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005717 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00005718}
5719
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00005720static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005721assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00005722{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005723 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005724 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005725 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00005726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005727 /* Compute the size of each block and fixup jump args.
5728 Replace block pointer with position in bytecode. */
5729 do {
5730 totsize = 0;
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005731 for (i = 0; i < a->a_nblocks; i++) {
5732 b = a->a_reverse_postorder[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005733 bsize = blocksize(b);
5734 b->b_offset = totsize;
5735 totsize += bsize;
5736 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005737 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005738 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
5739 bsize = b->b_offset;
5740 for (i = 0; i < b->b_iused; i++) {
5741 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005742 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005743 /* Relative jumps are computed relative to
5744 the instruction pointer after fetching
5745 the jump instruction.
5746 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005747 bsize += isize;
Mark Shannon582aaf12020-08-04 17:30:11 +01005748 if (is_jump(instr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005749 instr->i_oparg = instr->i_target->b_offset;
Mark Shannon582aaf12020-08-04 17:30:11 +01005750 if (is_relative_jump(instr)) {
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005751 instr->i_oparg -= bsize;
5752 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03005753 instr->i_oparg *= sizeof(_Py_CODEUNIT);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005754 if (instrsize(instr->i_oparg) != isize) {
5755 extended_arg_recompile = 1;
5756 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005757 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005758 }
5759 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00005760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005761 /* XXX: This is an awful hack that could hurt performance, but
5762 on the bright side it should work until we come up
5763 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00005764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005765 The issue is that in the first loop blocksize() is called
5766 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005767 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005768 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00005769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005770 So we loop until we stop seeing new EXTENDED_ARGs.
5771 The only EXTENDED_ARGs that could be popping up are
5772 ones in jump instructions. So this should converge
5773 fairly quickly.
5774 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005775 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005776}
5777
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005778static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01005779dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005780{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005781 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02005782 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005784 tuple = PyTuple_New(size);
5785 if (tuple == NULL)
5786 return NULL;
5787 while (PyDict_Next(dict, &pos, &k, &v)) {
5788 i = PyLong_AS_LONG(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005789 Py_INCREF(k);
5790 assert((i - offset) < size);
5791 assert((i - offset) >= 0);
5792 PyTuple_SET_ITEM(tuple, i - offset, k);
5793 }
5794 return tuple;
5795}
5796
5797static PyObject *
5798consts_dict_keys_inorder(PyObject *dict)
5799{
5800 PyObject *consts, *k, *v;
5801 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
5802
5803 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
5804 if (consts == NULL)
5805 return NULL;
5806 while (PyDict_Next(dict, &pos, &k, &v)) {
5807 i = PyLong_AS_LONG(v);
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +03005808 /* The keys of the dictionary can be tuples wrapping a contant.
5809 * (see compiler_add_o and _PyCode_ConstantKey). In that case
5810 * the object we want is always second. */
5811 if (PyTuple_CheckExact(k)) {
5812 k = PyTuple_GET_ITEM(k, 1);
5813 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005814 Py_INCREF(k);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005815 assert(i < size);
5816 assert(i >= 0);
5817 PyList_SET_ITEM(consts, i, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005818 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005819 return consts;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005820}
5821
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005822static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005823compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005824{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005825 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005826 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005827 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04005828 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005829 if (ste->ste_nested)
5830 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07005831 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005832 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07005833 if (!ste->ste_generator && ste->ste_coroutine)
5834 flags |= CO_COROUTINE;
5835 if (ste->ste_generator && ste->ste_coroutine)
5836 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005837 if (ste->ste_varargs)
5838 flags |= CO_VARARGS;
5839 if (ste->ste_varkeywords)
5840 flags |= CO_VARKEYWORDS;
5841 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00005842
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005843 /* (Only) inherit compilerflags in PyCF_MASK */
5844 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00005845
Pablo Galindo90235812020-03-15 04:29:22 +00005846 if ((IS_TOP_LEVEL_AWAIT(c)) &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005847 ste->ste_coroutine &&
5848 !ste->ste_generator) {
5849 flags |= CO_COROUTINE;
5850 }
5851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005852 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005853}
5854
INADA Naokic2e16072018-11-26 21:23:22 +09005855// Merge *tuple* with constant cache.
5856// Unlike merge_consts_recursive(), this function doesn't work recursively.
5857static int
5858merge_const_tuple(struct compiler *c, PyObject **tuple)
5859{
5860 assert(PyTuple_CheckExact(*tuple));
5861
5862 PyObject *key = _PyCode_ConstantKey(*tuple);
5863 if (key == NULL) {
5864 return 0;
5865 }
5866
5867 // t is borrowed reference
5868 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
5869 Py_DECREF(key);
5870 if (t == NULL) {
5871 return 0;
5872 }
5873 if (t == key) { // tuple is new constant.
5874 return 1;
5875 }
5876
5877 PyObject *u = PyTuple_GET_ITEM(t, 1);
5878 Py_INCREF(u);
5879 Py_DECREF(*tuple);
5880 *tuple = u;
5881 return 1;
5882}
5883
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005884static PyCodeObject *
Mark Shannon6e8128f2020-07-30 10:03:00 +01005885makecode(struct compiler *c, struct assembler *a, PyObject *consts)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005887 PyCodeObject *co = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005888 PyObject *names = NULL;
5889 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005890 PyObject *name = NULL;
5891 PyObject *freevars = NULL;
5892 PyObject *cellvars = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005893 Py_ssize_t nlocals;
5894 int nlocals_int;
5895 int flags;
Pablo Galindocd74e662019-06-01 18:08:04 +01005896 int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005898 names = dict_keys_inorder(c->u->u_names, 0);
5899 varnames = dict_keys_inorder(c->u->u_varnames, 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01005900 if (!names || !varnames) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005901 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01005902 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005903 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
5904 if (!cellvars)
5905 goto error;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005906 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005907 if (!freevars)
5908 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005909
INADA Naokic2e16072018-11-26 21:23:22 +09005910 if (!merge_const_tuple(c, &names) ||
5911 !merge_const_tuple(c, &varnames) ||
5912 !merge_const_tuple(c, &cellvars) ||
5913 !merge_const_tuple(c, &freevars))
5914 {
5915 goto error;
5916 }
5917
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02005918 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01005919 assert(nlocals < INT_MAX);
5920 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
5921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005922 flags = compute_code_flags(c);
5923 if (flags < 0)
5924 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005925
Mark Shannon6e8128f2020-07-30 10:03:00 +01005926 consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
5927 if (consts == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005928 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01005929 }
INADA Naokic2e16072018-11-26 21:23:22 +09005930 if (!merge_const_tuple(c, &consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01005931 Py_DECREF(consts);
INADA Naokic2e16072018-11-26 21:23:22 +09005932 goto error;
5933 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005934
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01005935 posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int);
Pablo Galindocd74e662019-06-01 18:08:04 +01005936 posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +01005937 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005938 maxdepth = stackdepth(c);
5939 if (maxdepth < 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01005940 Py_DECREF(consts);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005941 goto error;
5942 }
Pablo Galindo4a2edc32019-07-01 11:35:05 +01005943 co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
Mark Shannon13bc1392020-01-23 09:25:17 +00005944 posonlyargcount, kwonlyargcount, nlocals_int,
Mark Shannon6e8128f2020-07-30 10:03:00 +01005945 maxdepth, flags, a->a_bytecode, consts, names,
Pablo Galindo4a2edc32019-07-01 11:35:05 +01005946 varnames, freevars, cellvars, c->c_filename,
5947 c->u->u_name, c->u->u_firstlineno, a->a_lnotab);
Mark Shannon6e8128f2020-07-30 10:03:00 +01005948 Py_DECREF(consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005949 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005950 Py_XDECREF(names);
5951 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005952 Py_XDECREF(name);
5953 Py_XDECREF(freevars);
5954 Py_XDECREF(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005955 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005956}
5957
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005958
5959/* For debugging purposes only */
5960#if 0
5961static void
5962dump_instr(const struct instr *i)
5963{
Mark Shannon582aaf12020-08-04 17:30:11 +01005964 const char *jrel = (is_relative_jump(instr)) ? "jrel " : "";
5965 const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : "";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005966 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005968 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005969 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005970 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005971 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005972 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
5973 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005974}
5975
5976static void
5977dump_basicblock(const basicblock *b)
5978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005979 const char *b_return = b->b_return ? "return " : "";
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005980 fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n",
5981 b->b_iused, b->b_startdepth, b->b_offset, b_return);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005982 if (b->b_instr) {
5983 int i;
5984 for (i = 0; i < b->b_iused; i++) {
5985 fprintf(stderr, " [%02d] ", i);
5986 dump_instr(b->b_instr + i);
5987 }
5988 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005989}
5990#endif
5991
Mark Shannon6e8128f2020-07-30 10:03:00 +01005992static int
5993optimize_cfg(struct assembler *a, PyObject *consts);
5994
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005995static PyCodeObject *
5996assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005997{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005998 basicblock *b, *entryblock;
5999 struct assembler a;
6000 int i, j, nblocks;
6001 PyCodeObject *co = NULL;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006002 PyObject *consts = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006004 /* Make sure every block that falls off the end returns None.
6005 XXX NEXT_BLOCK() isn't quite right, because if the last
6006 block ends with a jump or return b_next shouldn't set.
6007 */
6008 if (!c->u->u_curblock->b_return) {
6009 NEXT_BLOCK(c);
6010 if (addNone)
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006011 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006012 ADDOP(c, RETURN_VALUE);
6013 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006015 nblocks = 0;
6016 entryblock = NULL;
6017 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6018 nblocks++;
6019 entryblock = b;
6020 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006022 /* Set firstlineno if it wasn't explicitly set. */
6023 if (!c->u->u_firstlineno) {
Ned Deilydc35cda2016-08-17 17:18:33 -04006024 if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006025 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
6026 else
6027 c->u->u_firstlineno = 1;
6028 }
6029 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
6030 goto error;
T. Wouters99b54d62019-09-12 07:05:33 -07006031 dfs(c, entryblock, &a, nblocks);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006032
Mark Shannon6e8128f2020-07-30 10:03:00 +01006033 consts = consts_dict_keys_inorder(c->u->u_consts);
6034 if (consts == NULL) {
6035 goto error;
6036 }
6037 if (optimize_cfg(&a, consts)) {
6038 goto error;
6039 }
6040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006041 /* Can't modify the bytecode after computing jump offsets. */
6042 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00006043
T. Wouters99b54d62019-09-12 07:05:33 -07006044 /* Emit code in reverse postorder from dfs. */
Pablo Galindo60eb9f12020-06-28 01:55:47 +01006045 for (i = 0; i < a.a_nblocks; i++) {
6046 b = a.a_reverse_postorder[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006047 for (j = 0; j < b->b_iused; j++)
6048 if (!assemble_emit(&a, &b->b_instr[j]))
6049 goto error;
6050 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00006051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006052 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
6053 goto error;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006054 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006055 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006056
Mark Shannon6e8128f2020-07-30 10:03:00 +01006057 co = makecode(c, &a, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006058 error:
Mark Shannon6e8128f2020-07-30 10:03:00 +01006059 Py_XDECREF(consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006060 assemble_free(&a);
6061 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006062}
Georg Brandl8334fd92010-12-04 10:26:46 +00006063
6064#undef PyAST_Compile
Benjamin Petersone5024512018-09-12 12:06:42 -07006065PyCodeObject *
Georg Brandl8334fd92010-12-04 10:26:46 +00006066PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
6067 PyArena *arena)
6068{
6069 return PyAST_CompileEx(mod, filename, flags, -1, arena);
6070}
Mark Shannon6e8128f2020-07-30 10:03:00 +01006071
6072
6073/* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
6074 with LOAD_CONST (c1, c2, ... cn).
6075 The consts table must still be in list form so that the
6076 new constant (c1, c2, ... cn) can be appended.
6077 Called with codestr pointing to the first LOAD_CONST.
6078*/
6079static int
6080fold_tuple_on_constants(struct instr *inst,
6081 int n, PyObject *consts)
6082{
6083 /* Pre-conditions */
6084 assert(PyList_CheckExact(consts));
6085 assert(inst[n].i_opcode == BUILD_TUPLE);
6086 assert(inst[n].i_oparg == n);
6087
6088 for (int i = 0; i < n; i++) {
6089 if (inst[i].i_opcode != LOAD_CONST) {
6090 return 0;
6091 }
6092 }
6093
6094 /* Buildup new tuple of constants */
6095 PyObject *newconst = PyTuple_New(n);
6096 if (newconst == NULL) {
6097 return -1;
6098 }
6099 for (int i = 0; i < n; i++) {
6100 int arg = inst[i].i_oparg;
6101 PyObject *constant = PyList_GET_ITEM(consts, arg);
6102 Py_INCREF(constant);
6103 PyTuple_SET_ITEM(newconst, i, constant);
6104 }
6105 Py_ssize_t index = PyList_GET_SIZE(consts);
6106#if SIZEOF_SIZE_T > SIZEOF_INT
6107 if ((size_t)index >= UINT_MAX - 1) {
6108 Py_DECREF(newconst);
6109 PyErr_SetString(PyExc_OverflowError, "too many constants");
6110 return -1;
6111 }
6112#endif
6113 if (PyList_Append(consts, newconst)) {
6114 Py_DECREF(newconst);
6115 return -1;
6116 }
6117 Py_DECREF(newconst);
6118 for (int i = 0; i < n; i++) {
6119 inst[i].i_opcode = NOP;
6120 }
6121 inst[n].i_opcode = LOAD_CONST;
6122 inst[n].i_oparg = index;
6123 return 0;
6124}
6125
6126
6127/* Optimization */
6128static int
6129optimize_basic_block(basicblock *bb, PyObject *consts)
6130{
6131 assert(PyList_CheckExact(consts));
6132 struct instr nop;
6133 nop.i_opcode = NOP;
6134 struct instr *target;
6135 int lineno;
6136 for (int i = 0; i < bb->b_iused; i++) {
6137 struct instr *inst = &bb->b_instr[i];
6138 int oparg = inst->i_oparg;
6139 int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01006140 if (is_jump(inst)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006141 /* Skip over empty basic blocks. */
6142 while (inst->i_target->b_iused == 0) {
6143 inst->i_target = inst->i_target->b_next;
6144 }
6145 target = &inst->i_target->b_instr[0];
6146 }
6147 else {
6148 target = &nop;
6149 }
6150 switch (inst->i_opcode) {
6151 /* Skip over LOAD_CONST trueconst
6152 POP_JUMP_IF_FALSE xx. This improves
6153 "while 1" performance. */
6154 case LOAD_CONST:
6155 if (nextop != POP_JUMP_IF_FALSE) {
6156 break;
6157 }
6158 PyObject* cnt = PyList_GET_ITEM(consts, oparg);
6159 int is_true = PyObject_IsTrue(cnt);
6160 if (is_true == -1) {
6161 goto error;
6162 }
6163 if (is_true == 1) {
6164 inst->i_opcode = NOP;
6165 bb->b_instr[i+1].i_opcode = NOP;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006166 }
6167 break;
6168
6169 /* Try to fold tuples of constants.
6170 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
6171 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
6172 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
6173 case BUILD_TUPLE:
6174 if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
6175 switch(oparg) {
6176 case 1:
6177 inst->i_opcode = NOP;
6178 bb->b_instr[i+1].i_opcode = NOP;
6179 break;
6180 case 2:
6181 inst->i_opcode = ROT_TWO;
6182 bb->b_instr[i+1].i_opcode = NOP;
6183 break;
6184 case 3:
6185 inst->i_opcode = ROT_THREE;
6186 bb->b_instr[i+1].i_opcode = ROT_TWO;
6187 }
6188 break;
6189 }
6190 if (i >= oparg) {
6191 if (fold_tuple_on_constants(inst-oparg, oparg, consts)) {
6192 goto error;
6193 }
6194 }
6195 break;
6196
6197 /* Simplify conditional jump to conditional jump where the
6198 result of the first test implies the success of a similar
6199 test or the failure of the opposite test.
6200 Arises in code like:
6201 "a and b or c"
6202 "(a and b) and c"
6203 "(a or b) or c"
6204 "(a or b) and c"
6205 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
6206 --> x:JUMP_IF_FALSE_OR_POP z
6207 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
6208 --> x:POP_JUMP_IF_FALSE y+1
6209 where y+1 is the instruction following the second test.
6210 */
6211 case JUMP_IF_FALSE_OR_POP:
6212 switch(target->i_opcode) {
6213 case POP_JUMP_IF_FALSE:
6214 *inst = *target;
6215 break;
6216 case JUMP_ABSOLUTE:
6217 case JUMP_FORWARD:
6218 case JUMP_IF_FALSE_OR_POP:
6219 inst->i_target = target->i_target;
6220 break;
6221 case JUMP_IF_TRUE_OR_POP:
6222 assert (inst->i_target->b_iused == 1);
6223 inst->i_opcode = POP_JUMP_IF_FALSE;
6224 inst->i_target = inst->i_target->b_next;
6225 break;
6226 }
6227 break;
6228
6229 case JUMP_IF_TRUE_OR_POP:
6230 switch(target->i_opcode) {
6231 case POP_JUMP_IF_TRUE:
6232 *inst = *target;
6233 break;
6234 case JUMP_ABSOLUTE:
6235 case JUMP_FORWARD:
6236 case JUMP_IF_TRUE_OR_POP:
6237 inst->i_target = target->i_target;
6238 break;
6239 case JUMP_IF_FALSE_OR_POP:
6240 assert (inst->i_target->b_iused == 1);
6241 inst->i_opcode = POP_JUMP_IF_TRUE;
6242 inst->i_target = inst->i_target->b_next;
6243 break;
6244 }
6245 break;
6246
6247 case POP_JUMP_IF_FALSE:
6248 switch(target->i_opcode) {
6249 case JUMP_ABSOLUTE:
6250 case JUMP_FORWARD:
6251 inst->i_target = target->i_target;
6252 break;
6253 }
6254 break;
6255
6256 case POP_JUMP_IF_TRUE:
6257 switch(target->i_opcode) {
6258 case JUMP_ABSOLUTE:
6259 case JUMP_FORWARD:
6260 inst->i_target = target->i_target;
6261 break;
6262 }
6263 break;
6264
6265 case JUMP_ABSOLUTE:
6266 case JUMP_FORWARD:
6267 switch(target->i_opcode) {
6268 case JUMP_FORWARD:
6269 inst->i_target = target->i_target;
6270 break;
6271 case JUMP_ABSOLUTE:
6272 case RETURN_VALUE:
6273 case RERAISE:
6274 case RAISE_VARARGS:
6275 lineno = inst->i_lineno;
6276 *inst = *target;
6277 inst->i_lineno = lineno;
6278 break;
6279 }
6280 break;
6281 }
6282 }
6283 return 0;
6284error:
6285 return -1;
6286}
6287
6288
6289static void
6290clean_basic_block(basicblock *bb) {
6291 /* Remove NOPs and any code following a return or re-raise. */
6292 int dest = 0;
6293 for (int src = 0; src < bb->b_iused; src++) {
6294 switch(bb->b_instr[src].i_opcode) {
6295 case NOP:
6296 /* skip */
6297 break;
6298 case RETURN_VALUE:
6299 case RERAISE:
6300 bb->b_next = NULL;
6301 bb->b_instr[dest] = bb->b_instr[src];
6302 dest++;
6303 goto end;
6304 default:
6305 if (dest != src) {
6306 bb->b_instr[dest] = bb->b_instr[src];
6307 }
6308 dest++;
6309 break;
6310 }
6311 }
6312end:
6313 assert(dest <= bb->b_iused);
6314 bb->b_iused = dest;
6315}
6316
6317static int
6318mark_reachable(struct assembler *a) {
6319 basicblock **stack, **sp;
6320 sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks);
6321 if (stack == NULL) {
6322 return -1;
6323 }
6324 basicblock *entry = a->a_reverse_postorder[0];
6325 entry->b_reachable = 1;
6326 *sp++ = entry;
6327 while (sp > stack) {
6328 basicblock *b = *(--sp);
6329 if (b->b_next && b->b_next->b_reachable == 0) {
6330 b->b_next->b_reachable = 1;
6331 *sp++ = b->b_next;
6332 }
6333 for (int i = 0; i < b->b_iused; i++) {
6334 basicblock *target;
Mark Shannon582aaf12020-08-04 17:30:11 +01006335 if (is_jump(&b->b_instr[i])) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006336 target = b->b_instr[i].i_target;
6337 if (target->b_reachable == 0) {
6338 target->b_reachable = 1;
6339 *sp++ = target;
6340 }
6341 }
6342 }
6343 }
6344 PyObject_Free(stack);
6345 return 0;
6346}
6347
6348
6349/* Perform basic peephole optimizations on a control flow graph.
6350 The consts object should still be in list form to allow new constants
6351 to be appended.
6352
6353 All transformations keep the code size the same or smaller.
6354 For those that reduce size, the gaps are initially filled with
6355 NOPs. Later those NOPs are removed.
6356*/
6357
6358static int
6359optimize_cfg(struct assembler *a, PyObject *consts)
6360{
6361 for (int i = 0; i < a->a_nblocks; i++) {
6362 if (optimize_basic_block(a->a_reverse_postorder[i], consts)) {
6363 return -1;
6364 }
6365 clean_basic_block(a->a_reverse_postorder[i]);
6366 assert(a->a_reverse_postorder[i]->b_reachable == 0);
6367 }
6368 if (mark_reachable(a)) {
6369 return -1;
6370 }
6371 /* Delete unreachable instructions */
6372 for (int i = 0; i < a->a_nblocks; i++) {
6373 if (a->a_reverse_postorder[i]->b_reachable == 0) {
6374 a->a_reverse_postorder[i]->b_iused = 0;
6375 }
6376 }
6377 return 0;
6378}
6379
6380/* Retained for API compatibility.
6381 * Optimization is now done in optimize_cfg */
6382
6383PyObject *
6384PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
6385 PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
6386{
6387 Py_INCREF(code);
6388 return code;
6389}
6390