blob: 42ae5082593c6c240fd9f331389022993fb1ab28 [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.
Thomas Wouters89f507f2006-12-13 04:49:30 +000012 * 5. Optimize the byte code (peephole optimizations). See peephole.c
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
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000026#include "Python-ast.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000027#include "node.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000028#include "ast.h"
29#include "code.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000030#include "symtable.h"
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
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000044struct instr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 unsigned i_jabs : 1;
46 unsigned i_jrel : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 unsigned char i_opcode;
48 int i_oparg;
49 struct basicblock_ *i_target; /* target block (if jump instruction) */
50 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000051};
52
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000053typedef struct basicblock_ {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000054 /* Each basicblock in a compilation unit is linked via b_list in the
55 reverse order that the block are allocated. b_list points to the next
56 block, not to be confused with b_next, which is next by control flow. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 struct basicblock_ *b_list;
58 /* number of instructions used */
59 int b_iused;
60 /* length of instruction array (b_instr) */
61 int b_ialloc;
62 /* pointer to an array of instructions, initially NULL */
63 struct instr *b_instr;
64 /* If b_next is non-NULL, it is a pointer to the next
65 block reached by normal control flow. */
66 struct basicblock_ *b_next;
67 /* b_seen is used to perform a DFS of basicblocks. */
68 unsigned b_seen : 1;
69 /* b_return is true if a RETURN_VALUE opcode is inserted. */
70 unsigned b_return : 1;
71 /* depth of stack upon entry of block, computed by stackdepth() */
72 int b_startdepth;
73 /* instruction offset for block, computed by assemble_jump_offsets() */
74 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000075} basicblock;
76
77/* fblockinfo tracks the current frame block.
78
Jeremy Hyltone9357b22006-03-01 15:47:05 +000079A frame block is used to handle loops, try/except, and try/finally.
80It's called a frame block to distinguish it from a basic block in the
81compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000082*/
83
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +020084enum fblocktype { WHILE_LOOP, FOR_LOOP, EXCEPT, FINALLY_TRY, FINALLY_END,
85 WITH, ASYNC_WITH, HANDLER_CLEANUP };
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000086
87struct fblockinfo {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 enum fblocktype fb_type;
89 basicblock *fb_block;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +020090 /* (optional) type-specific exit or cleanup block */
91 basicblock *fb_exit;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000092};
93
Antoine Pitrou86a36b52011-11-25 18:56:07 +010094enum {
95 COMPILER_SCOPE_MODULE,
96 COMPILER_SCOPE_CLASS,
97 COMPILER_SCOPE_FUNCTION,
Yury Selivanov75445082015-05-11 22:57:16 -040098 COMPILER_SCOPE_ASYNC_FUNCTION,
Benjamin Peterson6b4f7802013-10-20 17:50:28 -040099 COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100100 COMPILER_SCOPE_COMPREHENSION,
101};
102
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000103/* The following items change on entry and exit of code blocks.
104 They must be saved and restored when returning to a block.
105*/
106struct compiler_unit {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 PySTEntryObject *u_ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 PyObject *u_name;
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400110 PyObject *u_qualname; /* dot-separated qualified name (lazy) */
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100111 int u_scope_type;
112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 /* The following fields are dicts that map objects to
114 the index of them in co_XXX. The index is used as
115 the argument for opcodes that refer to those collections.
116 */
117 PyObject *u_consts; /* all constants */
118 PyObject *u_names; /* all names */
119 PyObject *u_varnames; /* local variables */
120 PyObject *u_cellvars; /* cell variables */
121 PyObject *u_freevars; /* free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 PyObject *u_private; /* for private name mangling */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000124
Victor Stinnerf8e32212013-11-19 23:56:34 +0100125 Py_ssize_t u_argcount; /* number of arguments for block */
126 Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 /* Pointer to the most recently allocated block. By following b_list
128 members, you can reach all early allocated blocks. */
129 basicblock *u_blocks;
130 basicblock *u_curblock; /* pointer to current block */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 int u_nfblocks;
133 struct fblockinfo u_fblock[CO_MAXBLOCKS];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 int u_firstlineno; /* the first lineno of the block */
136 int u_lineno; /* the lineno for the current stmt */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000137 int u_col_offset; /* the offset of the current stmt */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 int u_lineno_set; /* boolean to indicate whether instr
139 has been generated with current lineno */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000140};
141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142/* This struct captures the global state of a compilation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000143
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000144The u pointer points to the current compilation unit, while units
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145for enclosing blocks are stored in c_stack. The u and c_stack are
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000146managed by compiler_enter_scope() and compiler_exit_scope().
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000147
148Note that we don't track recursion levels during compilation - the
149task of detecting and rejecting excessive levels of nesting is
150handled by the symbol analysis pass.
151
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000152*/
153
154struct compiler {
Victor Stinner14e461d2013-08-26 22:28:21 +0200155 PyObject *c_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 struct symtable *c_st;
157 PyFutureFeatures *c_future; /* pointer to module's __future__ */
158 PyCompilerFlags *c_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000159
Georg Brandl8334fd92010-12-04 10:26:46 +0000160 int c_optimize; /* optimization level */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 int c_interactive; /* true if in interactive mode */
162 int c_nestlevel;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 struct compiler_unit *u; /* compiler state for current block */
165 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
166 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000167};
168
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100169static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000170static void compiler_free(struct compiler *);
171static basicblock *compiler_new_block(struct compiler *);
172static int compiler_next_instr(struct compiler *, basicblock *);
173static int compiler_addop(struct compiler *, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +0100174static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000175static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000176static int compiler_error(struct compiler *, const char *);
177static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
178
179static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
180static int compiler_visit_stmt(struct compiler *, stmt_ty);
181static int compiler_visit_keyword(struct compiler *, keyword_ty);
182static int compiler_visit_expr(struct compiler *, expr_ty);
183static int compiler_augassign(struct compiler *, stmt_ty);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700184static int compiler_annassign(struct compiler *, stmt_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185static int compiler_visit_slice(struct compiler *, slice_ty,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 expr_context_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000188static int inplace_binop(struct compiler *, operator_ty);
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +0200189static int expr_constant(expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000190
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500191static int compiler_with(struct compiler *, stmt_ty, int);
Yury Selivanov75445082015-05-11 22:57:16 -0400192static int compiler_async_with(struct compiler *, stmt_ty, int);
193static int compiler_async_for(struct compiler *, stmt_ty);
Victor Stinner976bb402016-03-23 11:36:19 +0100194static int compiler_call_helper(struct compiler *c, int n,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 asdl_seq *args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400196 asdl_seq *keywords);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500197static int compiler_try_except(struct compiler *, stmt_ty);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400198static int compiler_set_qualname(struct compiler *);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000199
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700200static int compiler_sync_comprehension_generator(
201 struct compiler *c,
202 asdl_seq *generators, int gen_index,
203 expr_ty elt, expr_ty val, int type);
204
205static int compiler_async_comprehension_generator(
206 struct compiler *c,
207 asdl_seq *generators, int gen_index,
208 expr_ty elt, expr_ty val, int type);
209
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210static PyCodeObject *assemble(struct compiler *, int addNone);
Mark Shannon332cd5e2018-01-30 00:41:04 +0000211static PyObject *__doc__, *__annotations__;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000212
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400213#define CAPSULE_NAME "compile.c compiler unit"
Benjamin Petersonb173f782009-05-05 22:31:58 +0000214
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000215PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000216_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000217{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 /* Name mangling: __private becomes _classname__private.
219 This is independent from how the name is used. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200220 PyObject *result;
221 size_t nlen, plen, ipriv;
222 Py_UCS4 maxchar;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200224 PyUnicode_READ_CHAR(ident, 0) != '_' ||
225 PyUnicode_READ_CHAR(ident, 1) != '_') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 Py_INCREF(ident);
227 return ident;
228 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200229 nlen = PyUnicode_GET_LENGTH(ident);
230 plen = PyUnicode_GET_LENGTH(privateobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 /* Don't mangle __id__ or names with dots.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 The only time a name with a dot can occur is when
234 we are compiling an import statement that has a
235 package name.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 TODO(jhylton): Decide whether we want to support
238 mangling of the module name, e.g. __M.X.
239 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200240 if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
241 PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
242 PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 Py_INCREF(ident);
244 return ident; /* Don't mangle __whatever__ */
245 }
246 /* Strip leading underscores from class name */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200247 ipriv = 0;
248 while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
249 ipriv++;
250 if (ipriv == plen) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 Py_INCREF(ident);
252 return ident; /* Don't mangle if class is just underscores */
253 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200254 plen -= ipriv;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000255
Antoine Pitrou55bff892013-04-06 21:21:04 +0200256 if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
257 PyErr_SetString(PyExc_OverflowError,
258 "private identifier too large to be mangled");
259 return NULL;
260 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000261
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200262 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
263 if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
264 maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
265
266 result = PyUnicode_New(1 + nlen + plen, maxchar);
267 if (!result)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200269 /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
270 PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
Victor Stinner6c7a52a2011-09-28 21:39:17 +0200271 if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
272 Py_DECREF(result);
273 return NULL;
274 }
275 if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
276 Py_DECREF(result);
277 return NULL;
278 }
Victor Stinner8f825062012-04-27 13:55:39 +0200279 assert(_PyUnicode_CheckConsistency(result, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200280 return result;
Michael W. Hudson60934622004-08-12 17:56:29 +0000281}
282
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000283static int
284compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 c->c_stack = PyList_New(0);
289 if (!c->c_stack)
290 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000293}
294
295PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200296PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
297 int optimize, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 struct compiler c;
300 PyCodeObject *co = NULL;
301 PyCompilerFlags local_flags;
302 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 if (!__doc__) {
305 __doc__ = PyUnicode_InternFromString("__doc__");
306 if (!__doc__)
307 return NULL;
308 }
Mark Shannon332cd5e2018-01-30 00:41:04 +0000309 if (!__annotations__) {
310 __annotations__ = PyUnicode_InternFromString("__annotations__");
311 if (!__annotations__)
312 return NULL;
313 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 if (!compiler_init(&c))
315 return NULL;
Victor Stinner14e461d2013-08-26 22:28:21 +0200316 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 c.c_filename = filename;
318 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200319 c.c_future = PyFuture_FromASTObject(mod, filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 if (c.c_future == NULL)
321 goto finally;
322 if (!flags) {
323 local_flags.cf_flags = 0;
324 flags = &local_flags;
325 }
326 merged = c.c_future->ff_features | flags->cf_flags;
327 c.c_future->ff_features = merged;
328 flags->cf_flags = merged;
329 c.c_flags = flags;
Georg Brandl8334fd92010-12-04 10:26:46 +0000330 c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000332
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +0200333 if (!_PyAST_Optimize(mod, arena, c.c_optimize)) {
INADA Naoki7ea143a2017-12-14 16:47:20 +0900334 goto finally;
335 }
336
Victor Stinner14e461d2013-08-26 22:28:21 +0200337 c.c_st = PySymtable_BuildObject(mod, filename, c.c_future);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 if (c.c_st == NULL) {
339 if (!PyErr_Occurred())
340 PyErr_SetString(PyExc_SystemError, "no symtable");
341 goto finally;
342 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000345
Thomas Wouters1175c432006-02-27 22:49:54 +0000346 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 compiler_free(&c);
348 assert(co || PyErr_Occurred());
349 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000350}
351
352PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200353PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags,
354 int optimize, PyArena *arena)
355{
356 PyObject *filename;
357 PyCodeObject *co;
358 filename = PyUnicode_DecodeFSDefault(filename_str);
359 if (filename == NULL)
360 return NULL;
361 co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
362 Py_DECREF(filename);
363 return co;
364
365}
366
367PyCodeObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000368PyNode_Compile(struct _node *n, const char *filename)
369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 PyCodeObject *co = NULL;
371 mod_ty mod;
372 PyArena *arena = PyArena_New();
373 if (!arena)
374 return NULL;
375 mod = PyAST_FromNode(n, NULL, filename, arena);
376 if (mod)
377 co = PyAST_Compile(mod, filename, NULL, arena);
378 PyArena_Free(arena);
379 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000380}
381
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000382static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000383compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 if (c->c_st)
386 PySymtable_Free(c->c_st);
387 if (c->c_future)
388 PyObject_Free(c->c_future);
Victor Stinner14e461d2013-08-26 22:28:21 +0200389 Py_XDECREF(c->c_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000391}
392
Guido van Rossum79f25d91997-04-29 20:08:16 +0000393static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000394list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 Py_ssize_t i, n;
397 PyObject *v, *k;
398 PyObject *dict = PyDict_New();
399 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 n = PyList_Size(list);
402 for (i = 0; i < n; i++) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100403 v = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 if (!v) {
405 Py_DECREF(dict);
406 return NULL;
407 }
408 k = PyList_GET_ITEM(list, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300409 if (PyDict_SetItem(dict, k, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 Py_DECREF(v);
411 Py_DECREF(dict);
412 return NULL;
413 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 Py_DECREF(v);
415 }
416 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000417}
418
419/* Return new dict containing names from src that match scope(s).
420
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000421src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000423values are integers, starting at offset and increasing by one for
424each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000425*/
426
427static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +0100428dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000429{
Benjamin Peterson51ab2832012-07-18 15:12:47 -0700430 Py_ssize_t i = offset, scope, num_keys, key_i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 PyObject *k, *v, *dest = PyDict_New();
Meador Inge2ca63152012-07-18 14:20:11 -0500432 PyObject *sorted_keys;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 assert(offset >= 0);
435 if (dest == NULL)
436 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000437
Meador Inge2ca63152012-07-18 14:20:11 -0500438 /* Sort the keys so that we have a deterministic order on the indexes
439 saved in the returned dictionary. These indexes are used as indexes
440 into the free and cell var storage. Therefore if they aren't
441 deterministic, then the generated bytecode is not deterministic.
442 */
443 sorted_keys = PyDict_Keys(src);
444 if (sorted_keys == NULL)
445 return NULL;
446 if (PyList_Sort(sorted_keys) != 0) {
447 Py_DECREF(sorted_keys);
448 return NULL;
449 }
Meador Ingef69e24e2012-07-18 16:41:03 -0500450 num_keys = PyList_GET_SIZE(sorted_keys);
Meador Inge2ca63152012-07-18 14:20:11 -0500451
452 for (key_i = 0; key_i < num_keys; key_i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 /* XXX this should probably be a macro in symtable.h */
454 long vi;
Meador Inge2ca63152012-07-18 14:20:11 -0500455 k = PyList_GET_ITEM(sorted_keys, key_i);
456 v = PyDict_GetItem(src, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 assert(PyLong_Check(v));
458 vi = PyLong_AS_LONG(v);
459 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 if (scope == scope_type || vi & flag) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300462 PyObject *item = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 if (item == NULL) {
Meador Inge2ca63152012-07-18 14:20:11 -0500464 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 Py_DECREF(dest);
466 return NULL;
467 }
468 i++;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300469 if (PyDict_SetItem(dest, k, item) < 0) {
Meador Inge2ca63152012-07-18 14:20:11 -0500470 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 Py_DECREF(item);
472 Py_DECREF(dest);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 return NULL;
474 }
475 Py_DECREF(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 }
477 }
Meador Inge2ca63152012-07-18 14:20:11 -0500478 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000480}
481
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482static void
483compiler_unit_check(struct compiler_unit *u)
484{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 basicblock *block;
486 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Benjamin Petersonca470632016-09-06 13:47:26 -0700487 assert((uintptr_t)block != 0xcbcbcbcbU);
488 assert((uintptr_t)block != 0xfbfbfbfbU);
489 assert((uintptr_t)block != 0xdbdbdbdbU);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 if (block->b_instr != NULL) {
491 assert(block->b_ialloc > 0);
492 assert(block->b_iused > 0);
493 assert(block->b_ialloc >= block->b_iused);
494 }
495 else {
496 assert (block->b_iused == 0);
497 assert (block->b_ialloc == 0);
498 }
499 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000500}
501
502static void
503compiler_unit_free(struct compiler_unit *u)
504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 compiler_unit_check(u);
508 b = u->u_blocks;
509 while (b != NULL) {
510 if (b->b_instr)
511 PyObject_Free((void *)b->b_instr);
512 next = b->b_list;
513 PyObject_Free((void *)b);
514 b = next;
515 }
516 Py_CLEAR(u->u_ste);
517 Py_CLEAR(u->u_name);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400518 Py_CLEAR(u->u_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 Py_CLEAR(u->u_consts);
520 Py_CLEAR(u->u_names);
521 Py_CLEAR(u->u_varnames);
522 Py_CLEAR(u->u_freevars);
523 Py_CLEAR(u->u_cellvars);
524 Py_CLEAR(u->u_private);
525 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000526}
527
528static int
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100529compiler_enter_scope(struct compiler *c, identifier name,
530 int scope_type, void *key, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 struct compiler_unit *u;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100533 basicblock *block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
536 struct compiler_unit));
537 if (!u) {
538 PyErr_NoMemory();
539 return 0;
540 }
541 memset(u, 0, sizeof(struct compiler_unit));
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100542 u->u_scope_type = scope_type;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 u->u_argcount = 0;
544 u->u_kwonlyargcount = 0;
545 u->u_ste = PySymtable_Lookup(c->c_st, key);
546 if (!u->u_ste) {
547 compiler_unit_free(u);
548 return 0;
549 }
550 Py_INCREF(name);
551 u->u_name = name;
552 u->u_varnames = list2dict(u->u_ste->ste_varnames);
553 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
554 if (!u->u_varnames || !u->u_cellvars) {
555 compiler_unit_free(u);
556 return 0;
557 }
Benjamin Peterson312595c2013-05-15 15:26:42 -0500558 if (u->u_ste->ste_needs_class_closure) {
Martin Panter7462b6492015-11-02 03:37:02 +0000559 /* Cook up an implicit __class__ cell. */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500560 _Py_IDENTIFIER(__class__);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300561 PyObject *name;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500562 int res;
563 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200564 assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500565 name = _PyUnicode_FromId(&PyId___class__);
566 if (!name) {
567 compiler_unit_free(u);
568 return 0;
569 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300570 res = PyDict_SetItem(u->u_cellvars, name, _PyLong_Zero);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500571 if (res < 0) {
572 compiler_unit_free(u);
573 return 0;
574 }
575 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200578 PyDict_GET_SIZE(u->u_cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 if (!u->u_freevars) {
580 compiler_unit_free(u);
581 return 0;
582 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 u->u_blocks = NULL;
585 u->u_nfblocks = 0;
586 u->u_firstlineno = lineno;
587 u->u_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000588 u->u_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 u->u_lineno_set = 0;
590 u->u_consts = PyDict_New();
591 if (!u->u_consts) {
592 compiler_unit_free(u);
593 return 0;
594 }
595 u->u_names = PyDict_New();
596 if (!u->u_names) {
597 compiler_unit_free(u);
598 return 0;
599 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 /* Push the old compiler_unit on the stack. */
604 if (c->u) {
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400605 PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
607 Py_XDECREF(capsule);
608 compiler_unit_free(u);
609 return 0;
610 }
611 Py_DECREF(capsule);
612 u->u_private = c->u->u_private;
613 Py_XINCREF(u->u_private);
614 }
615 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 c->c_nestlevel++;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100618
619 block = compiler_new_block(c);
620 if (block == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 return 0;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100622 c->u->u_curblock = block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000623
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400624 if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
625 if (!compiler_set_qualname(c))
626 return 0;
627 }
628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000630}
631
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000632static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000633compiler_exit_scope(struct compiler *c)
634{
Victor Stinnerad9a0662013-11-19 22:23:20 +0100635 Py_ssize_t n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 PyObject *capsule;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 c->c_nestlevel--;
639 compiler_unit_free(c->u);
640 /* Restore c->u to the parent unit. */
641 n = PyList_GET_SIZE(c->c_stack) - 1;
642 if (n >= 0) {
643 capsule = PyList_GET_ITEM(c->c_stack, n);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400644 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 assert(c->u);
646 /* we are deleting from a list so this really shouldn't fail */
647 if (PySequence_DelItem(c->c_stack, n) < 0)
648 Py_FatalError("compiler_exit_scope()");
649 compiler_unit_check(c->u);
650 }
651 else
652 c->u = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000653
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654}
655
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400656static int
657compiler_set_qualname(struct compiler *c)
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100658{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100659 _Py_static_string(dot, ".");
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400660 _Py_static_string(dot_locals, ".<locals>");
661 Py_ssize_t stack_size;
662 struct compiler_unit *u = c->u;
663 PyObject *name, *base, *dot_str, *dot_locals_str;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100664
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400665 base = NULL;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100666 stack_size = PyList_GET_SIZE(c->c_stack);
Benjamin Petersona8a38b82013-10-19 16:14:39 -0400667 assert(stack_size >= 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400668 if (stack_size > 1) {
669 int scope, force_global = 0;
670 struct compiler_unit *parent;
671 PyObject *mangled, *capsule;
672
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400673 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400674 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400675 assert(parent);
676
Yury Selivanov75445082015-05-11 22:57:16 -0400677 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
678 || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
679 || u->u_scope_type == COMPILER_SCOPE_CLASS) {
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400680 assert(u->u_name);
681 mangled = _Py_Mangle(parent->u_private, u->u_name);
682 if (!mangled)
683 return 0;
684 scope = PyST_GetScope(parent->u_ste, mangled);
685 Py_DECREF(mangled);
686 assert(scope != GLOBAL_IMPLICIT);
687 if (scope == GLOBAL_EXPLICIT)
688 force_global = 1;
689 }
690
691 if (!force_global) {
692 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
Yury Selivanov75445082015-05-11 22:57:16 -0400693 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400694 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
695 dot_locals_str = _PyUnicode_FromId(&dot_locals);
696 if (dot_locals_str == NULL)
697 return 0;
698 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
699 if (base == NULL)
700 return 0;
701 }
702 else {
703 Py_INCREF(parent->u_qualname);
704 base = parent->u_qualname;
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400705 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100706 }
707 }
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400708
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400709 if (base != NULL) {
710 dot_str = _PyUnicode_FromId(&dot);
711 if (dot_str == NULL) {
712 Py_DECREF(base);
713 return 0;
714 }
715 name = PyUnicode_Concat(base, dot_str);
716 Py_DECREF(base);
717 if (name == NULL)
718 return 0;
719 PyUnicode_Append(&name, u->u_name);
720 if (name == NULL)
721 return 0;
722 }
723 else {
724 Py_INCREF(u->u_name);
725 name = u->u_name;
726 }
727 u->u_qualname = name;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100728
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400729 return 1;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100730}
731
Eric V. Smith235a6f02015-09-19 14:51:32 -0400732
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000733/* Allocate a new block and return a pointer to it.
734 Returns NULL on error.
735*/
736
737static basicblock *
738compiler_new_block(struct compiler *c)
739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 basicblock *b;
741 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 u = c->u;
744 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
745 if (b == NULL) {
746 PyErr_NoMemory();
747 return NULL;
748 }
749 memset((void *)b, 0, sizeof(basicblock));
750 /* Extend the singly linked list of blocks with new block. */
751 b->b_list = u->u_blocks;
752 u->u_blocks = b;
753 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754}
755
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756static basicblock *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000757compiler_next_block(struct compiler *c)
758{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 basicblock *block = compiler_new_block(c);
760 if (block == NULL)
761 return NULL;
762 c->u->u_curblock->b_next = block;
763 c->u->u_curblock = block;
764 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765}
766
767static basicblock *
768compiler_use_next_block(struct compiler *c, basicblock *block)
769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 assert(block != NULL);
771 c->u->u_curblock->b_next = block;
772 c->u->u_curblock = block;
773 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774}
775
776/* Returns the offset of the next instruction in the current block's
777 b_instr array. Resizes the b_instr as necessary.
778 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000779*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780
781static int
782compiler_next_instr(struct compiler *c, basicblock *b)
783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 assert(b != NULL);
785 if (b->b_instr == NULL) {
786 b->b_instr = (struct instr *)PyObject_Malloc(
787 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
788 if (b->b_instr == NULL) {
789 PyErr_NoMemory();
790 return -1;
791 }
792 b->b_ialloc = DEFAULT_BLOCK_SIZE;
793 memset((char *)b->b_instr, 0,
794 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
795 }
796 else if (b->b_iused == b->b_ialloc) {
797 struct instr *tmp;
798 size_t oldsize, newsize;
799 oldsize = b->b_ialloc * sizeof(struct instr);
800 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000801
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -0700802 if (oldsize > (SIZE_MAX >> 1)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 PyErr_NoMemory();
804 return -1;
805 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 if (newsize == 0) {
808 PyErr_NoMemory();
809 return -1;
810 }
811 b->b_ialloc <<= 1;
812 tmp = (struct instr *)PyObject_Realloc(
813 (void *)b->b_instr, newsize);
814 if (tmp == NULL) {
815 PyErr_NoMemory();
816 return -1;
817 }
818 b->b_instr = tmp;
819 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
820 }
821 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822}
823
Christian Heimes2202f872008-02-06 14:31:34 +0000824/* Set the i_lineno member of the instruction at offset off if the
825 line number for the current expression/statement has not
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000826 already been set. If it has been set, the call has no effect.
827
Christian Heimes2202f872008-02-06 14:31:34 +0000828 The line number is reset in the following cases:
829 - when entering a new scope
830 - on each statement
831 - on each expression that start a new line
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200832 - before the "except" and "finally" clauses
Christian Heimes2202f872008-02-06 14:31:34 +0000833 - before the "for" and "while" expressions
Thomas Wouters89f507f2006-12-13 04:49:30 +0000834*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000835
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836static void
837compiler_set_lineno(struct compiler *c, int off)
838{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 basicblock *b;
840 if (c->u->u_lineno_set)
841 return;
842 c->u->u_lineno_set = 1;
843 b = c->u->u_curblock;
844 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845}
846
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200847/* Return the stack effect of opcode with argument oparg.
848
849 Some opcodes have different stack effect when jump to the target and
850 when not jump. The 'jump' parameter specifies the case:
851
852 * 0 -- when not jump
853 * 1 -- when jump
854 * -1 -- maximal
855 */
856/* XXX Make the stack effect of WITH_CLEANUP_START and
857 WITH_CLEANUP_FINISH deterministic. */
858static int
859stack_effect(int opcode, int oparg, int jump)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 switch (opcode) {
Serhiy Storchaka57faf342018-04-25 22:04:06 +0300862 case NOP:
863 case EXTENDED_ARG:
864 return 0;
865
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200866 /* Stack manipulation */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 case POP_TOP:
868 return -1;
869 case ROT_TWO:
870 case ROT_THREE:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200871 case ROT_FOUR:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 return 0;
873 case DUP_TOP:
874 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000875 case DUP_TOP_TWO:
876 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000877
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200878 /* Unary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 case UNARY_POSITIVE:
880 case UNARY_NEGATIVE:
881 case UNARY_NOT:
882 case UNARY_INVERT:
883 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 case SET_ADD:
886 case LIST_APPEND:
887 return -1;
888 case MAP_ADD:
889 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000890
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200891 /* Binary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 case BINARY_POWER:
893 case BINARY_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400894 case BINARY_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 case BINARY_MODULO:
896 case BINARY_ADD:
897 case BINARY_SUBTRACT:
898 case BINARY_SUBSCR:
899 case BINARY_FLOOR_DIVIDE:
900 case BINARY_TRUE_DIVIDE:
901 return -1;
902 case INPLACE_FLOOR_DIVIDE:
903 case INPLACE_TRUE_DIVIDE:
904 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 case INPLACE_ADD:
907 case INPLACE_SUBTRACT:
908 case INPLACE_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400909 case INPLACE_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 case INPLACE_MODULO:
911 return -1;
912 case STORE_SUBSCR:
913 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 case DELETE_SUBSCR:
915 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 case BINARY_LSHIFT:
918 case BINARY_RSHIFT:
919 case BINARY_AND:
920 case BINARY_XOR:
921 case BINARY_OR:
922 return -1;
923 case INPLACE_POWER:
924 return -1;
925 case GET_ITER:
926 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 case PRINT_EXPR:
929 return -1;
930 case LOAD_BUILD_CLASS:
931 return 1;
932 case INPLACE_LSHIFT:
933 case INPLACE_RSHIFT:
934 case INPLACE_AND:
935 case INPLACE_XOR:
936 case INPLACE_OR:
937 return -1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 case SETUP_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200940 /* 1 in the normal flow.
941 * Restore the stack position and push 6 values before jumping to
942 * the handler if an exception be raised. */
943 return jump ? 6 : 1;
Yury Selivanov75445082015-05-11 22:57:16 -0400944 case WITH_CLEANUP_START:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200945 return 2; /* or 1, depending on TOS */
Yury Selivanov75445082015-05-11 22:57:16 -0400946 case WITH_CLEANUP_FINISH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200947 /* Pop a variable number of values pushed by WITH_CLEANUP_START
948 * + __exit__ or __aexit__. */
949 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 case RETURN_VALUE:
951 return -1;
952 case IMPORT_STAR:
953 return -1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700954 case SETUP_ANNOTATIONS:
955 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 case YIELD_VALUE:
957 return 0;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500958 case YIELD_FROM:
959 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 case POP_BLOCK:
961 return 0;
962 case POP_EXCEPT:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200963 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 case END_FINALLY:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200965 case POP_FINALLY:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200966 /* Pop 6 values when an exception was raised. */
967 return -6;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 case STORE_NAME:
970 return -1;
971 case DELETE_NAME:
972 return 0;
973 case UNPACK_SEQUENCE:
974 return oparg-1;
975 case UNPACK_EX:
976 return (oparg&0xFF) + (oparg>>8);
977 case FOR_ITER:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200978 /* -1 at end of iterator, 1 if continue iterating. */
979 return jump > 0 ? -1 : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 case STORE_ATTR:
982 return -2;
983 case DELETE_ATTR:
984 return -1;
985 case STORE_GLOBAL:
986 return -1;
987 case DELETE_GLOBAL:
988 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 case LOAD_CONST:
990 return 1;
991 case LOAD_NAME:
992 return 1;
993 case BUILD_TUPLE:
994 case BUILD_LIST:
995 case BUILD_SET:
Serhiy Storchakaea525a22016-09-06 22:07:53 +0300996 case BUILD_STRING:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 return 1-oparg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400998 case BUILD_LIST_UNPACK:
999 case BUILD_TUPLE_UNPACK:
Serhiy Storchaka73442852016-10-02 10:33:46 +03001000 case BUILD_TUPLE_UNPACK_WITH_CALL:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001001 case BUILD_SET_UNPACK:
1002 case BUILD_MAP_UNPACK:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001003 case BUILD_MAP_UNPACK_WITH_CALL:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001004 return 1 - oparg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 case BUILD_MAP:
Benjamin Petersonb6855152015-09-10 21:02:39 -07001006 return 1 - 2*oparg;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001007 case BUILD_CONST_KEY_MAP:
1008 return -oparg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 case LOAD_ATTR:
1010 return 0;
1011 case COMPARE_OP:
1012 return -1;
1013 case IMPORT_NAME:
1014 return -1;
1015 case IMPORT_FROM:
1016 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001018 /* Jumps */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 case JUMP_FORWARD:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 case JUMP_ABSOLUTE:
1021 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001023 case JUMP_IF_TRUE_OR_POP:
1024 case JUMP_IF_FALSE_OR_POP:
1025 return jump ? 0 : -1;
1026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 case POP_JUMP_IF_FALSE:
1028 case POP_JUMP_IF_TRUE:
1029 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 case LOAD_GLOBAL:
1032 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001033
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001034 /* Exception handling */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 case SETUP_FINALLY:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001036 /* 0 in the normal flow.
1037 * Restore the stack position and push 6 values before jumping to
1038 * the handler if an exception be raised. */
1039 return jump ? 6 : 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001040 case BEGIN_FINALLY:
1041 /* Actually pushes 1 value, but count 6 for balancing with
1042 * END_FINALLY and POP_FINALLY.
1043 * This is the main reason of using this opcode instead of
1044 * "LOAD_CONST None". */
1045 return 6;
1046 case CALL_FINALLY:
1047 return jump ? 1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 case LOAD_FAST:
1050 return 1;
1051 case STORE_FAST:
1052 return -1;
1053 case DELETE_FAST:
1054 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 case RAISE_VARARGS:
1057 return -oparg;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001058
1059 /* Functions and calls */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 case CALL_FUNCTION:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001061 return -oparg;
Yury Selivanovf2392132016-12-13 19:03:51 -05001062 case CALL_METHOD:
1063 return -oparg-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 case CALL_FUNCTION_KW:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001065 return -oparg-1;
1066 case CALL_FUNCTION_EX:
Matthieu Dartiailh3a9ac822017-02-21 14:25:22 +01001067 return -1 - ((oparg & 0x01) != 0);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001068 case MAKE_FUNCTION:
1069 return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1070 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 case BUILD_SLICE:
1072 if (oparg == 3)
1073 return -2;
1074 else
1075 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001077 /* Closures */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 case LOAD_CLOSURE:
1079 return 1;
1080 case LOAD_DEREF:
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001081 case LOAD_CLASSDEREF:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 return 1;
1083 case STORE_DEREF:
1084 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001085 case DELETE_DEREF:
1086 return 0;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001087
1088 /* Iterators and generators */
Yury Selivanov75445082015-05-11 22:57:16 -04001089 case GET_AWAITABLE:
1090 return 0;
1091 case SETUP_ASYNC_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001092 /* 0 in the normal flow.
1093 * Restore the stack position to the position before the result
1094 * of __aenter__ and push 6 values before jumping to the handler
1095 * if an exception be raised. */
1096 return jump ? -1 + 6 : 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001097 case BEFORE_ASYNC_WITH:
1098 return 1;
1099 case GET_AITER:
1100 return 0;
1101 case GET_ANEXT:
1102 return 1;
Yury Selivanov5376ba92015-06-22 12:19:30 -04001103 case GET_YIELD_FROM_ITER:
1104 return 0;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02001105 case END_ASYNC_FOR:
1106 return -7;
Eric V. Smitha78c7952015-11-03 12:45:05 -05001107 case FORMAT_VALUE:
1108 /* If there's a fmt_spec on the stack, we go from 2->1,
1109 else 1->1. */
1110 return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
Yury Selivanovf2392132016-12-13 19:03:51 -05001111 case LOAD_METHOD:
1112 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 default:
Larry Hastings3a907972013-11-23 14:49:22 -08001114 return PY_INVALID_STACK_EFFECT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 }
Larry Hastings3a907972013-11-23 14:49:22 -08001116 return PY_INVALID_STACK_EFFECT; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001117}
1118
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001119int
1120PyCompile_OpcodeStackEffect(int opcode, int oparg)
1121{
1122 return stack_effect(opcode, oparg, -1);
1123}
1124
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001125/* Add an opcode with no argument.
1126 Returns 0 on failure, 1 on success.
1127*/
1128
1129static int
1130compiler_addop(struct compiler *c, int opcode)
1131{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 basicblock *b;
1133 struct instr *i;
1134 int off;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001135 assert(!HAS_ARG(opcode));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 off = compiler_next_instr(c, c->u->u_curblock);
1137 if (off < 0)
1138 return 0;
1139 b = c->u->u_curblock;
1140 i = &b->b_instr[off];
1141 i->i_opcode = opcode;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001142 i->i_oparg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 if (opcode == RETURN_VALUE)
1144 b->b_return = 1;
1145 compiler_set_lineno(c, off);
1146 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147}
1148
Victor Stinnerf8e32212013-11-19 23:56:34 +01001149static Py_ssize_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
1151{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001152 PyObject *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001155 v = PyDict_GetItemWithError(dict, o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 if (!v) {
Stefan Krahc0cbed12015-07-27 12:56:49 +02001157 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 return -1;
Stefan Krahc0cbed12015-07-27 12:56:49 +02001159 }
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001160 arg = PyDict_GET_SIZE(dict);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001161 v = PyLong_FromSsize_t(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 if (!v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 return -1;
1164 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001165 if (PyDict_SetItem(dict, o, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 Py_DECREF(v);
1167 return -1;
1168 }
1169 Py_DECREF(v);
1170 }
1171 else
1172 arg = PyLong_AsLong(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001173 return arg;
1174}
1175
1176static Py_ssize_t
1177compiler_add_const(struct compiler *c, PyObject *o)
1178{
1179 PyObject *t;
1180 Py_ssize_t arg;
1181
1182 t = _PyCode_ConstantKey(o);
1183 if (t == NULL)
1184 return -1;
1185
1186 arg = compiler_add_o(c, c->u->u_consts, t);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 Py_DECREF(t);
1188 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189}
1190
1191static int
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001192compiler_addop_load_const(struct compiler *c, PyObject *o)
1193{
1194 Py_ssize_t arg = compiler_add_const(c, o);
1195 if (arg < 0)
1196 return 0;
1197 return compiler_addop_i(c, LOAD_CONST, arg);
1198}
1199
1200static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001204 Py_ssize_t arg = compiler_add_o(c, dict, o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001206 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207 return compiler_addop_i(c, opcode, arg);
1208}
1209
1210static int
1211compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001214 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1216 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001217 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001218 arg = compiler_add_o(c, dict, mangled);
1219 Py_DECREF(mangled);
1220 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001221 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222 return compiler_addop_i(c, opcode, arg);
1223}
1224
1225/* Add an opcode with an integer argument.
1226 Returns 0 on failure, 1 on success.
1227*/
1228
1229static int
Victor Stinnerf8e32212013-11-19 23:56:34 +01001230compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001231{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 struct instr *i;
1233 int off;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001234
Victor Stinner2ad474b2016-03-01 23:34:47 +01001235 /* oparg value is unsigned, but a signed C int is usually used to store
1236 it in the C code (like Python/ceval.c).
1237
1238 Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1239
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001240 The argument of a concrete bytecode instruction is limited to 8-bit.
1241 EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1242 assert(HAS_ARG(opcode));
Victor Stinner2ad474b2016-03-01 23:34:47 +01001243 assert(0 <= oparg && oparg <= 2147483647);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 off = compiler_next_instr(c, c->u->u_curblock);
1246 if (off < 0)
1247 return 0;
1248 i = &c->u->u_curblock->b_instr[off];
Victor Stinnerf8e32212013-11-19 23:56:34 +01001249 i->i_opcode = opcode;
1250 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 compiler_set_lineno(c, off);
1252 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253}
1254
1255static int
1256compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 struct instr *i;
1259 int off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001261 assert(HAS_ARG(opcode));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 assert(b != NULL);
1263 off = compiler_next_instr(c, c->u->u_curblock);
1264 if (off < 0)
1265 return 0;
1266 i = &c->u->u_curblock->b_instr[off];
1267 i->i_opcode = opcode;
1268 i->i_target = b;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 if (absolute)
1270 i->i_jabs = 1;
1271 else
1272 i->i_jrel = 1;
1273 compiler_set_lineno(c, off);
1274 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001275}
1276
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +01001277/* NEXT_BLOCK() creates an implicit jump from the current block
1278 to the new block.
1279
1280 The returns inside this macro make it impossible to decref objects
1281 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001282*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001283#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 if (compiler_next_block((C)) == NULL) \
1285 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001286}
1287
1288#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 if (!compiler_addop((C), (OP))) \
1290 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291}
1292
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001293#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 if (!compiler_addop((C), (OP))) { \
1295 compiler_exit_scope(c); \
1296 return 0; \
1297 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001298}
1299
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001300#define ADDOP_LOAD_CONST(C, O) { \
1301 if (!compiler_addop_load_const((C), (O))) \
1302 return 0; \
1303}
1304
1305/* Same as ADDOP_LOAD_CONST, but steals a reference. */
1306#define ADDOP_LOAD_CONST_NEW(C, O) { \
1307 PyObject *__new_const = (O); \
1308 if (__new_const == NULL) { \
1309 return 0; \
1310 } \
1311 if (!compiler_addop_load_const((C), __new_const)) { \
1312 Py_DECREF(__new_const); \
1313 return 0; \
1314 } \
1315 Py_DECREF(__new_const); \
1316}
1317
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1320 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001321}
1322
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001323/* Same as ADDOP_O, but steals a reference. */
1324#define ADDOP_N(C, OP, O, TYPE) { \
1325 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1326 Py_DECREF((O)); \
1327 return 0; \
1328 } \
1329 Py_DECREF((O)); \
1330}
1331
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001332#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1334 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001335}
1336
1337#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 if (!compiler_addop_i((C), (OP), (O))) \
1339 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001340}
1341
1342#define ADDOP_JABS(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 if (!compiler_addop_j((C), (OP), (O), 1)) \
1344 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345}
1346
1347#define ADDOP_JREL(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 if (!compiler_addop_j((C), (OP), (O), 0)) \
1349 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001350}
1351
1352/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1353 the ASDL name to synthesize the name of the C type and the visit function.
1354*/
1355
1356#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 if (!compiler_visit_ ## TYPE((C), (V))) \
1358 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359}
1360
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001361#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 if (!compiler_visit_ ## TYPE((C), (V))) { \
1363 compiler_exit_scope(c); \
1364 return 0; \
1365 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001366}
1367
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 if (!compiler_visit_slice((C), (V), (CTX))) \
1370 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001371}
1372
1373#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 int _i; \
1375 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1376 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1377 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1378 if (!compiler_visit_ ## TYPE((C), elt)) \
1379 return 0; \
1380 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001381}
1382
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001383#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 int _i; \
1385 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1386 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1387 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1388 if (!compiler_visit_ ## TYPE((C), elt)) { \
1389 compiler_exit_scope(c); \
1390 return 0; \
1391 } \
1392 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001393}
1394
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001395static int
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001396compiler_isdocstring(stmt_ty s)
1397{
1398 if (s->kind != Expr_kind)
1399 return 0;
1400 if (s->v.Expr.value->kind == Str_kind)
1401 return 1;
1402 if (s->v.Expr.value->kind == Constant_kind)
1403 return PyUnicode_CheckExact(s->v.Expr.value->v.Constant.value);
1404 return 0;
1405}
1406
1407static int
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001408is_const(expr_ty e)
1409{
1410 switch (e->kind) {
1411 case Constant_kind:
1412 case Num_kind:
1413 case Str_kind:
1414 case Bytes_kind:
1415 case Ellipsis_kind:
1416 case NameConstant_kind:
1417 return 1;
1418 default:
1419 return 0;
1420 }
1421}
1422
1423static PyObject *
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02001424get_const_value(expr_ty e)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001425{
1426 switch (e->kind) {
1427 case Constant_kind:
1428 return e->v.Constant.value;
1429 case Num_kind:
1430 return e->v.Num.n;
1431 case Str_kind:
1432 return e->v.Str.s;
1433 case Bytes_kind:
1434 return e->v.Bytes.s;
1435 case Ellipsis_kind:
1436 return Py_Ellipsis;
1437 case NameConstant_kind:
1438 return e->v.NameConstant.value;
1439 default:
Barry Warsawb2e57942017-09-14 18:13:16 -07001440 Py_UNREACHABLE();
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001441 }
1442}
1443
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001444/* Search if variable annotations are present statically in a block. */
1445
1446static int
1447find_ann(asdl_seq *stmts)
1448{
1449 int i, j, res = 0;
1450 stmt_ty st;
1451
1452 for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1453 st = (stmt_ty)asdl_seq_GET(stmts, i);
1454 switch (st->kind) {
1455 case AnnAssign_kind:
1456 return 1;
1457 case For_kind:
1458 res = find_ann(st->v.For.body) ||
1459 find_ann(st->v.For.orelse);
1460 break;
1461 case AsyncFor_kind:
1462 res = find_ann(st->v.AsyncFor.body) ||
1463 find_ann(st->v.AsyncFor.orelse);
1464 break;
1465 case While_kind:
1466 res = find_ann(st->v.While.body) ||
1467 find_ann(st->v.While.orelse);
1468 break;
1469 case If_kind:
1470 res = find_ann(st->v.If.body) ||
1471 find_ann(st->v.If.orelse);
1472 break;
1473 case With_kind:
1474 res = find_ann(st->v.With.body);
1475 break;
1476 case AsyncWith_kind:
1477 res = find_ann(st->v.AsyncWith.body);
1478 break;
1479 case Try_kind:
1480 for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1481 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1482 st->v.Try.handlers, j);
1483 if (find_ann(handler->v.ExceptHandler.body)) {
1484 return 1;
1485 }
1486 }
1487 res = find_ann(st->v.Try.body) ||
1488 find_ann(st->v.Try.finalbody) ||
1489 find_ann(st->v.Try.orelse);
1490 break;
1491 default:
1492 res = 0;
1493 }
1494 if (res) {
1495 break;
1496 }
1497 }
1498 return res;
1499}
1500
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001501/*
1502 * Frame block handling functions
1503 */
1504
1505static int
1506compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b,
1507 basicblock *exit)
1508{
1509 struct fblockinfo *f;
1510 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
1511 PyErr_SetString(PyExc_SyntaxError,
1512 "too many statically nested blocks");
1513 return 0;
1514 }
1515 f = &c->u->u_fblock[c->u->u_nfblocks++];
1516 f->fb_type = t;
1517 f->fb_block = b;
1518 f->fb_exit = exit;
1519 return 1;
1520}
1521
1522static void
1523compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
1524{
1525 struct compiler_unit *u = c->u;
1526 assert(u->u_nfblocks > 0);
1527 u->u_nfblocks--;
1528 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
1529 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
1530}
1531
1532/* Unwind a frame block. If preserve_tos is true, the TOS before
1533 * popping the blocks will be restored afterwards.
1534 */
1535static int
1536compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
1537 int preserve_tos)
1538{
1539 switch (info->fb_type) {
1540 case WHILE_LOOP:
1541 return 1;
1542
1543 case FINALLY_END:
1544 ADDOP_I(c, POP_FINALLY, preserve_tos);
1545 return 1;
1546
1547 case FOR_LOOP:
1548 /* Pop the iterator */
1549 if (preserve_tos) {
1550 ADDOP(c, ROT_TWO);
1551 }
1552 ADDOP(c, POP_TOP);
1553 return 1;
1554
1555 case EXCEPT:
1556 ADDOP(c, POP_BLOCK);
1557 return 1;
1558
1559 case FINALLY_TRY:
1560 ADDOP(c, POP_BLOCK);
1561 ADDOP_JREL(c, CALL_FINALLY, info->fb_exit);
1562 return 1;
1563
1564 case WITH:
1565 case ASYNC_WITH:
1566 ADDOP(c, POP_BLOCK);
1567 if (preserve_tos) {
1568 ADDOP(c, ROT_TWO);
1569 }
1570 ADDOP(c, BEGIN_FINALLY);
1571 ADDOP(c, WITH_CLEANUP_START);
1572 if (info->fb_type == ASYNC_WITH) {
1573 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001574 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001575 ADDOP(c, YIELD_FROM);
1576 }
1577 ADDOP(c, WITH_CLEANUP_FINISH);
1578 ADDOP_I(c, POP_FINALLY, 0);
1579 return 1;
1580
1581 case HANDLER_CLEANUP:
1582 if (preserve_tos) {
1583 ADDOP(c, ROT_FOUR);
1584 }
1585 if (info->fb_exit) {
1586 ADDOP(c, POP_BLOCK);
1587 ADDOP(c, POP_EXCEPT);
1588 ADDOP_JREL(c, CALL_FINALLY, info->fb_exit);
1589 }
1590 else {
1591 ADDOP(c, POP_EXCEPT);
1592 }
1593 return 1;
1594 }
1595 Py_UNREACHABLE();
1596}
1597
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001598/* Compile a sequence of statements, checking for a docstring
1599 and for annotations. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600
1601static int
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001602compiler_body(struct compiler *c, asdl_seq *stmts)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001603{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001604 int i = 0;
1605 stmt_ty st;
1606
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001607 /* Set current line number to the line number of first statement.
1608 This way line number for SETUP_ANNOTATIONS will always
1609 coincide with the line number of first "real" statement in module.
1610 If body is empy, then lineno will be set later in assemble. */
1611 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE &&
1612 !c->u->u_lineno && asdl_seq_LEN(stmts)) {
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001613 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001614 c->u->u_lineno = st->lineno;
1615 }
1616 /* Every annotated class and module should have __annotations__. */
1617 if (find_ann(stmts)) {
1618 ADDOP(c, SETUP_ANNOTATIONS);
1619 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001620 if (!asdl_seq_LEN(stmts))
1621 return 1;
1622 st = (stmt_ty)asdl_seq_GET(stmts, 0);
INADA Naokicb41b272017-02-23 00:31:59 +09001623 /* if not -OO mode, set docstring */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001624 if (compiler_isdocstring(st) && c->c_optimize < 2) {
1625 /* don't generate docstrings if -OO */
1626 i = 1;
1627 VISIT(c, expr, st->v.Expr.value);
1628 if (!compiler_nameop(c, __doc__, Store))
1629 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001631 for (; i < asdl_seq_LEN(stmts); i++)
1632 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001634}
1635
1636static PyCodeObject *
1637compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001638{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 PyCodeObject *co;
1640 int addNone = 1;
1641 static PyObject *module;
1642 if (!module) {
1643 module = PyUnicode_InternFromString("<module>");
1644 if (!module)
1645 return NULL;
1646 }
1647 /* Use 0 for firstlineno initially, will fixup in assemble(). */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001648 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 return NULL;
1650 switch (mod->kind) {
1651 case Module_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001652 if (!compiler_body(c, mod->v.Module.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 compiler_exit_scope(c);
1654 return 0;
1655 }
1656 break;
1657 case Interactive_kind:
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001658 if (find_ann(mod->v.Interactive.body)) {
1659 ADDOP(c, SETUP_ANNOTATIONS);
1660 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 c->c_interactive = 1;
1662 VISIT_SEQ_IN_SCOPE(c, stmt,
1663 mod->v.Interactive.body);
1664 break;
1665 case Expression_kind:
1666 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1667 addNone = 0;
1668 break;
1669 case Suite_kind:
1670 PyErr_SetString(PyExc_SystemError,
1671 "suite should not be possible");
1672 return 0;
1673 default:
1674 PyErr_Format(PyExc_SystemError,
1675 "module kind %d should not be possible",
1676 mod->kind);
1677 return 0;
1678 }
1679 co = assemble(c, addNone);
1680 compiler_exit_scope(c);
1681 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001682}
1683
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684/* The test for LOCAL must come before the test for FREE in order to
1685 handle classes where name is both local and free. The local var is
1686 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001687*/
1688
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689static int
1690get_ref_type(struct compiler *c, PyObject *name)
1691{
Victor Stinner0b1bc562013-05-16 22:17:17 +02001692 int scope;
Benjamin Peterson312595c2013-05-15 15:26:42 -05001693 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001694 _PyUnicode_EqualToASCIIString(name, "__class__"))
Benjamin Peterson312595c2013-05-15 15:26:42 -05001695 return CELL;
Victor Stinner0b1bc562013-05-16 22:17:17 +02001696 scope = PyST_GetScope(c->u->u_ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 if (scope == 0) {
1698 char buf[350];
1699 PyOS_snprintf(buf, sizeof(buf),
Victor Stinner14e461d2013-08-26 22:28:21 +02001700 "unknown scope for %.100s in %.100s(%s)\n"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 "symbols: %s\nlocals: %s\nglobals: %s",
Serhiy Storchakadf4518c2014-11-18 23:34:33 +02001702 PyUnicode_AsUTF8(name),
1703 PyUnicode_AsUTF8(c->u->u_name),
1704 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)),
1705 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)),
1706 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)),
1707 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 );
1709 Py_FatalError(buf);
1710 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001713}
1714
1715static int
1716compiler_lookup_arg(PyObject *dict, PyObject *name)
1717{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001718 PyObject *v;
1719 v = PyDict_GetItem(dict, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001721 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001722 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001723}
1724
1725static int
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001726compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001727{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001728 Py_ssize_t i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001729 if (qualname == NULL)
1730 qualname = co->co_name;
1731
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001732 if (free) {
1733 for (i = 0; i < free; ++i) {
1734 /* Bypass com_addop_varname because it will generate
1735 LOAD_DEREF but LOAD_CLOSURE is needed.
1736 */
1737 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1738 int arg, reftype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001739
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001740 /* Special case: If a class contains a method with a
1741 free variable that has the same name as a method,
1742 the name will be considered free *and* local in the
1743 class. It should be handled by the closure, as
1744 well as by the normal name loookup logic.
1745 */
1746 reftype = get_ref_type(c, name);
1747 if (reftype == CELL)
1748 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1749 else /* (reftype == FREE) */
1750 arg = compiler_lookup_arg(c->u->u_freevars, name);
1751 if (arg == -1) {
1752 fprintf(stderr,
1753 "lookup %s in %s %d %d\n"
1754 "freevars of %s: %s\n",
1755 PyUnicode_AsUTF8(PyObject_Repr(name)),
1756 PyUnicode_AsUTF8(c->u->u_name),
1757 reftype, arg,
1758 PyUnicode_AsUTF8(co->co_name),
1759 PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars)));
1760 Py_FatalError("compiler_make_closure()");
1761 }
1762 ADDOP_I(c, LOAD_CLOSURE, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001764 flags |= 0x08;
1765 ADDOP_I(c, BUILD_TUPLE, free);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001767 ADDOP_LOAD_CONST(c, (PyObject*)co);
1768 ADDOP_LOAD_CONST(c, qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001769 ADDOP_I(c, MAKE_FUNCTION, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771}
1772
1773static int
1774compiler_decorators(struct compiler *c, asdl_seq* decos)
1775{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 if (!decos)
1779 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1782 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1783 }
1784 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001785}
1786
1787static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001788compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 asdl_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001790{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001791 /* Push a dict of keyword-only default values.
1792
1793 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
1794 */
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001795 int i;
1796 PyObject *keys = NULL;
1797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1799 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1800 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1801 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04001802 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001803 if (!mangled) {
1804 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001806 if (keys == NULL) {
1807 keys = PyList_New(1);
1808 if (keys == NULL) {
1809 Py_DECREF(mangled);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001810 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001811 }
1812 PyList_SET_ITEM(keys, 0, mangled);
1813 }
1814 else {
1815 int res = PyList_Append(keys, mangled);
1816 Py_DECREF(mangled);
1817 if (res == -1) {
1818 goto error;
1819 }
1820 }
1821 if (!compiler_visit_expr(c, default_)) {
1822 goto error;
1823 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 }
1825 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001826 if (keys != NULL) {
1827 Py_ssize_t default_count = PyList_GET_SIZE(keys);
1828 PyObject *keys_tuple = PyList_AsTuple(keys);
1829 Py_DECREF(keys);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001830 ADDOP_LOAD_CONST_NEW(c, keys_tuple);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001831 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001832 assert(default_count > 0);
1833 return 1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001834 }
1835 else {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001836 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001837 }
1838
1839error:
1840 Py_XDECREF(keys);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001841 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001842}
1843
1844static int
Guido van Rossum95e4d582018-01-26 08:20:18 -08001845compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
1846{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +03001847 ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation));
Guido van Rossum95e4d582018-01-26 08:20:18 -08001848 return 1;
1849}
1850
1851static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001852compiler_visit_argannotation(struct compiler *c, identifier id,
1853 expr_ty annotation, PyObject *names)
1854{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 if (annotation) {
Victor Stinner065efc32014-02-18 22:07:56 +01001856 PyObject *mangled;
Guido van Rossum95e4d582018-01-26 08:20:18 -08001857 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
1858 VISIT(c, annexpr, annotation)
1859 }
1860 else {
1861 VISIT(c, expr, annotation);
1862 }
Victor Stinner065efc32014-02-18 22:07:56 +01001863 mangled = _Py_Mangle(c->u->u_private, id);
Yury Selivanov34ce99f2014-02-18 12:49:41 -05001864 if (!mangled)
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001865 return 0;
Yury Selivanov34ce99f2014-02-18 12:49:41 -05001866 if (PyList_Append(names, mangled) < 0) {
1867 Py_DECREF(mangled);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001868 return 0;
Yury Selivanov34ce99f2014-02-18 12:49:41 -05001869 }
1870 Py_DECREF(mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001872 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001873}
1874
1875static int
1876compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1877 PyObject *names)
1878{
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001879 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 for (i = 0; i < asdl_seq_LEN(args); i++) {
1881 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001882 if (!compiler_visit_argannotation(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 c,
1884 arg->arg,
1885 arg->annotation,
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001886 names))
1887 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001889 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001890}
1891
1892static int
1893compiler_visit_annotations(struct compiler *c, arguments_ty args,
1894 expr_ty returns)
1895{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001896 /* Push arg annotation dict.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001897 The expressions are evaluated out-of-order wrt the source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00001898
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001899 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 */
1901 static identifier return_str;
1902 PyObject *names;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001903 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 names = PyList_New(0);
1905 if (!names)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03001906 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001907
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001908 if (!compiler_visit_argannotations(c, args->args, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001910 if (args->vararg && args->vararg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001911 !compiler_visit_argannotation(c, args->vararg->arg,
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001912 args->vararg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 goto error;
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001914 if (!compiler_visit_argannotations(c, args->kwonlyargs, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001916 if (args->kwarg && args->kwarg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001917 !compiler_visit_argannotation(c, args->kwarg->arg,
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001918 args->kwarg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 if (!return_str) {
1922 return_str = PyUnicode_InternFromString("return");
1923 if (!return_str)
1924 goto error;
1925 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001926 if (!compiler_visit_argannotation(c, return_str, returns, names)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 goto error;
1928 }
1929
1930 len = PyList_GET_SIZE(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 if (len) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001932 PyObject *keytuple = PyList_AsTuple(names);
1933 Py_DECREF(names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001934 ADDOP_LOAD_CONST_NEW(c, keytuple);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001935 ADDOP_I(c, BUILD_CONST_KEY_MAP, len);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001936 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001938 else {
1939 Py_DECREF(names);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001940 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001941 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001942
1943error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 Py_DECREF(names);
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03001945 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001946}
1947
1948static int
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001949compiler_visit_defaults(struct compiler *c, arguments_ty args)
1950{
1951 VISIT_SEQ(c, expr, args->defaults);
1952 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
1953 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001954}
1955
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001956static Py_ssize_t
1957compiler_default_arguments(struct compiler *c, arguments_ty args)
1958{
1959 Py_ssize_t funcflags = 0;
1960 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001961 if (!compiler_visit_defaults(c, args))
1962 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001963 funcflags |= 0x01;
1964 }
1965 if (args->kwonlyargs) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001966 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001967 args->kw_defaults);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001968 if (res == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001969 return -1;
1970 }
1971 else if (res > 0) {
1972 funcflags |= 0x02;
1973 }
1974 }
1975 return funcflags;
1976}
1977
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001978static int
Yury Selivanov75445082015-05-11 22:57:16 -04001979compiler_function(struct compiler *c, stmt_ty s, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001980{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 PyCodeObject *co;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001982 PyObject *qualname, *first_const = Py_None;
Yury Selivanov75445082015-05-11 22:57:16 -04001983 arguments_ty args;
1984 expr_ty returns;
1985 identifier name;
1986 asdl_seq* decos;
1987 asdl_seq *body;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001988 stmt_ty st;
INADA Naokicb41b272017-02-23 00:31:59 +09001989 Py_ssize_t i, funcflags;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001990 int docstring;
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001991 int annotations;
Yury Selivanov75445082015-05-11 22:57:16 -04001992 int scope_type;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001993
Yury Selivanov75445082015-05-11 22:57:16 -04001994 if (is_async) {
1995 assert(s->kind == AsyncFunctionDef_kind);
1996
1997 args = s->v.AsyncFunctionDef.args;
1998 returns = s->v.AsyncFunctionDef.returns;
1999 decos = s->v.AsyncFunctionDef.decorator_list;
2000 name = s->v.AsyncFunctionDef.name;
2001 body = s->v.AsyncFunctionDef.body;
2002
2003 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
2004 } else {
2005 assert(s->kind == FunctionDef_kind);
2006
2007 args = s->v.FunctionDef.args;
2008 returns = s->v.FunctionDef.returns;
2009 decos = s->v.FunctionDef.decorator_list;
2010 name = s->v.FunctionDef.name;
2011 body = s->v.FunctionDef.body;
2012
2013 scope_type = COMPILER_SCOPE_FUNCTION;
2014 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 if (!compiler_decorators(c, decos))
2017 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002018
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002019 funcflags = compiler_default_arguments(c, args);
2020 if (funcflags == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002022 }
2023
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002024 annotations = compiler_visit_annotations(c, args, returns);
2025 if (annotations == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002026 return 0;
2027 }
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002028 else if (annotations > 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002029 funcflags |= 0x04;
2030 }
2031
2032 if (!compiler_enter_scope(c, name, scope_type, (void *)s, s->lineno)) {
2033 return 0;
2034 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035
INADA Naokicb41b272017-02-23 00:31:59 +09002036 /* if not -OO mode, add docstring */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002037 st = (stmt_ty)asdl_seq_GET(body, 0);
2038 docstring = compiler_isdocstring(st);
2039 if (docstring && c->c_optimize < 2) {
2040 if (st->v.Expr.value->kind == Constant_kind)
2041 first_const = st->v.Expr.value->v.Constant.value;
2042 else
2043 first_const = st->v.Expr.value->v.Str.s;
2044 }
2045 if (compiler_add_const(c, first_const) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 compiler_exit_scope(c);
2047 return 0;
2048 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050 c->u->u_argcount = asdl_seq_LEN(args->args);
2051 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
INADA Naokicb41b272017-02-23 00:31:59 +09002052 VISIT_SEQ_IN_SCOPE(c, stmt, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002054 qualname = c->u->u_qualname;
2055 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002057 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002058 Py_XDECREF(qualname);
2059 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002061 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002062
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002063 compiler_make_closure(c, co, funcflags, qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002064 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 /* decorators */
2068 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2069 ADDOP_I(c, CALL_FUNCTION, 1);
2070 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002071
Yury Selivanov75445082015-05-11 22:57:16 -04002072 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073}
2074
2075static int
2076compiler_class(struct compiler *c, stmt_ty s)
2077{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002078 PyCodeObject *co;
2079 PyObject *str;
2080 int i;
2081 asdl_seq* decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 if (!compiler_decorators(c, decos))
2084 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086 /* ultimately generate code for:
2087 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
2088 where:
2089 <func> is a function/closure created from the class body;
2090 it has a single argument (__locals__) where the dict
2091 (or MutableSequence) representing the locals is passed
2092 <name> is the class name
2093 <bases> is the positional arguments and *varargs argument
2094 <keywords> is the keyword arguments and **kwds argument
2095 This borrows from compiler_call.
2096 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002099 if (!compiler_enter_scope(c, s->v.ClassDef.name,
2100 COMPILER_SCOPE_CLASS, (void *)s, s->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 return 0;
2102 /* this block represents what we do in the new scope */
2103 {
2104 /* use the class name for name mangling */
2105 Py_INCREF(s->v.ClassDef.name);
Serhiy Storchaka48842712016-04-06 09:45:48 +03002106 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107 /* load (global) __name__ ... */
2108 str = PyUnicode_InternFromString("__name__");
2109 if (!str || !compiler_nameop(c, str, Load)) {
2110 Py_XDECREF(str);
2111 compiler_exit_scope(c);
2112 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002113 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 Py_DECREF(str);
2115 /* ... and store it as __module__ */
2116 str = PyUnicode_InternFromString("__module__");
2117 if (!str || !compiler_nameop(c, str, Store)) {
2118 Py_XDECREF(str);
2119 compiler_exit_scope(c);
2120 return 0;
2121 }
2122 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002123 assert(c->u->u_qualname);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002124 ADDOP_LOAD_CONST(c, c->u->u_qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002125 str = PyUnicode_InternFromString("__qualname__");
2126 if (!str || !compiler_nameop(c, str, Store)) {
2127 Py_XDECREF(str);
2128 compiler_exit_scope(c);
2129 return 0;
2130 }
2131 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132 /* compile the body proper */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002133 if (!compiler_body(c, s->v.ClassDef.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002134 compiler_exit_scope(c);
2135 return 0;
2136 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002137 /* Return __classcell__ if it is referenced, otherwise return None */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002138 if (c->u->u_ste->ste_needs_class_closure) {
Nick Coghlan19d24672016-12-05 16:47:55 +10002139 /* Store __classcell__ into class namespace & return it */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002140 str = PyUnicode_InternFromString("__class__");
2141 if (str == NULL) {
2142 compiler_exit_scope(c);
2143 return 0;
2144 }
2145 i = compiler_lookup_arg(c->u->u_cellvars, str);
2146 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01002147 if (i < 0) {
2148 compiler_exit_scope(c);
2149 return 0;
2150 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002151 assert(i == 0);
Nick Coghlan944368e2016-09-11 14:45:49 +10002152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153 ADDOP_I(c, LOAD_CLOSURE, i);
Nick Coghlan19d24672016-12-05 16:47:55 +10002154 ADDOP(c, DUP_TOP);
Nick Coghlan944368e2016-09-11 14:45:49 +10002155 str = PyUnicode_InternFromString("__classcell__");
2156 if (!str || !compiler_nameop(c, str, Store)) {
2157 Py_XDECREF(str);
2158 compiler_exit_scope(c);
2159 return 0;
2160 }
2161 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002163 else {
Nick Coghlan19d24672016-12-05 16:47:55 +10002164 /* No methods referenced __class__, so just return None */
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002165 assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002166 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson312595c2013-05-15 15:26:42 -05002167 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002168 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 /* create the code object */
2170 co = assemble(c, 1);
2171 }
2172 /* leave the new scope */
2173 compiler_exit_scope(c);
2174 if (co == NULL)
2175 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177 /* 2. load the 'build_class' function */
2178 ADDOP(c, LOAD_BUILD_CLASS);
2179
2180 /* 3. load a function (or closure) made from the code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002181 compiler_make_closure(c, co, 0, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 Py_DECREF(co);
2183
2184 /* 4. load class name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002185 ADDOP_LOAD_CONST(c, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186
2187 /* 5. generate the rest of the code for the call */
2188 if (!compiler_call_helper(c, 2,
2189 s->v.ClassDef.bases,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002190 s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 return 0;
2192
2193 /* 6. apply decorators */
2194 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2195 ADDOP_I(c, CALL_FUNCTION, 1);
2196 }
2197
2198 /* 7. store into <name> */
2199 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2200 return 0;
2201 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002202}
2203
2204static int
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002205cmpop(cmpop_ty op)
2206{
2207 switch (op) {
2208 case Eq:
2209 return PyCmp_EQ;
2210 case NotEq:
2211 return PyCmp_NE;
2212 case Lt:
2213 return PyCmp_LT;
2214 case LtE:
2215 return PyCmp_LE;
2216 case Gt:
2217 return PyCmp_GT;
2218 case GtE:
2219 return PyCmp_GE;
2220 case Is:
2221 return PyCmp_IS;
2222 case IsNot:
2223 return PyCmp_IS_NOT;
2224 case In:
2225 return PyCmp_IN;
2226 case NotIn:
2227 return PyCmp_NOT_IN;
2228 default:
2229 return PyCmp_BAD;
2230 }
2231}
2232
2233static int
2234compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2235{
2236 switch (e->kind) {
2237 case UnaryOp_kind:
2238 if (e->v.UnaryOp.op == Not)
2239 return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2240 /* fallback to general implementation */
2241 break;
2242 case BoolOp_kind: {
2243 asdl_seq *s = e->v.BoolOp.values;
2244 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2245 assert(n >= 0);
2246 int cond2 = e->v.BoolOp.op == Or;
2247 basicblock *next2 = next;
2248 if (!cond2 != !cond) {
2249 next2 = compiler_new_block(c);
2250 if (next2 == NULL)
2251 return 0;
2252 }
2253 for (i = 0; i < n; ++i) {
2254 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2255 return 0;
2256 }
2257 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2258 return 0;
2259 if (next2 != next)
2260 compiler_use_next_block(c, next2);
2261 return 1;
2262 }
2263 case IfExp_kind: {
2264 basicblock *end, *next2;
2265 end = compiler_new_block(c);
2266 if (end == NULL)
2267 return 0;
2268 next2 = compiler_new_block(c);
2269 if (next2 == NULL)
2270 return 0;
2271 if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2272 return 0;
2273 if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2274 return 0;
2275 ADDOP_JREL(c, JUMP_FORWARD, end);
2276 compiler_use_next_block(c, next2);
2277 if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2278 return 0;
2279 compiler_use_next_block(c, end);
2280 return 1;
2281 }
2282 case Compare_kind: {
2283 Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2284 if (n > 0) {
2285 basicblock *cleanup = compiler_new_block(c);
2286 if (cleanup == NULL)
2287 return 0;
2288 VISIT(c, expr, e->v.Compare.left);
2289 for (i = 0; i < n; i++) {
2290 VISIT(c, expr,
2291 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2292 ADDOP(c, DUP_TOP);
2293 ADDOP(c, ROT_THREE);
2294 ADDOP_I(c, COMPARE_OP,
2295 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, i))));
2296 ADDOP_JABS(c, POP_JUMP_IF_FALSE, cleanup);
2297 NEXT_BLOCK(c);
2298 }
2299 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
2300 ADDOP_I(c, COMPARE_OP,
2301 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n))));
2302 ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
2303 basicblock *end = compiler_new_block(c);
2304 if (end == NULL)
2305 return 0;
2306 ADDOP_JREL(c, JUMP_FORWARD, end);
2307 compiler_use_next_block(c, cleanup);
2308 ADDOP(c, POP_TOP);
2309 if (!cond) {
2310 ADDOP_JREL(c, JUMP_FORWARD, next);
2311 }
2312 compiler_use_next_block(c, end);
2313 return 1;
2314 }
2315 /* fallback to general implementation */
2316 break;
2317 }
2318 default:
2319 /* fallback to general implementation */
2320 break;
2321 }
2322
2323 /* general implementation */
2324 VISIT(c, expr, e);
2325 ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
2326 return 1;
2327}
2328
2329static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002330compiler_ifexp(struct compiler *c, expr_ty e)
2331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 basicblock *end, *next;
2333
2334 assert(e->kind == IfExp_kind);
2335 end = compiler_new_block(c);
2336 if (end == NULL)
2337 return 0;
2338 next = compiler_new_block(c);
2339 if (next == NULL)
2340 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002341 if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2342 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 VISIT(c, expr, e->v.IfExp.body);
2344 ADDOP_JREL(c, JUMP_FORWARD, end);
2345 compiler_use_next_block(c, next);
2346 VISIT(c, expr, e->v.IfExp.orelse);
2347 compiler_use_next_block(c, end);
2348 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002349}
2350
2351static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002352compiler_lambda(struct compiler *c, expr_ty e)
2353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002355 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 static identifier name;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002357 Py_ssize_t funcflags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 arguments_ty args = e->v.Lambda.args;
2359 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 if (!name) {
2362 name = PyUnicode_InternFromString("<lambda>");
2363 if (!name)
2364 return 0;
2365 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002366
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002367 funcflags = compiler_default_arguments(c, args);
2368 if (funcflags == -1) {
2369 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002371
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002372 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002373 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 /* Make None the first constant, so the lambda can't have a
2377 docstring. */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002378 if (compiler_add_const(c, Py_None) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 c->u->u_argcount = asdl_seq_LEN(args->args);
2382 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2383 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2384 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002385 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 }
2387 else {
2388 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002389 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002391 qualname = c->u->u_qualname;
2392 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002394 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002396
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002397 compiler_make_closure(c, co, funcflags, qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002398 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 Py_DECREF(co);
2400
2401 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002402}
2403
2404static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002405compiler_if(struct compiler *c, stmt_ty s)
2406{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 basicblock *end, *next;
2408 int constant;
2409 assert(s->kind == If_kind);
2410 end = compiler_new_block(c);
2411 if (end == NULL)
2412 return 0;
2413
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02002414 constant = expr_constant(s->v.If.test);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 /* constant = 0: "if 0"
2416 * constant = 1: "if 1", "if 2", ...
2417 * constant = -1: rest */
2418 if (constant == 0) {
2419 if (s->v.If.orelse)
2420 VISIT_SEQ(c, stmt, s->v.If.orelse);
2421 } else if (constant == 1) {
2422 VISIT_SEQ(c, stmt, s->v.If.body);
2423 } else {
Antoine Pitroue7811fc2014-09-18 03:06:50 +02002424 if (asdl_seq_LEN(s->v.If.orelse)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 next = compiler_new_block(c);
2426 if (next == NULL)
2427 return 0;
2428 }
2429 else
2430 next = end;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002431 if (!compiler_jump_if(c, s->v.If.test, next, 0))
2432 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 VISIT_SEQ(c, stmt, s->v.If.body);
Antoine Pitroue7811fc2014-09-18 03:06:50 +02002434 if (asdl_seq_LEN(s->v.If.orelse)) {
2435 ADDOP_JREL(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 compiler_use_next_block(c, next);
2437 VISIT_SEQ(c, stmt, s->v.If.orelse);
2438 }
2439 }
2440 compiler_use_next_block(c, end);
2441 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002442}
2443
2444static int
2445compiler_for(struct compiler *c, stmt_ty s)
2446{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 basicblock *start, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 start = compiler_new_block(c);
2450 cleanup = compiler_new_block(c);
2451 end = compiler_new_block(c);
2452 if (start == NULL || end == NULL || cleanup == NULL)
2453 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002454
2455 if (!compiler_push_fblock(c, FOR_LOOP, start, end))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002456 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 VISIT(c, expr, s->v.For.iter);
2459 ADDOP(c, GET_ITER);
2460 compiler_use_next_block(c, start);
2461 ADDOP_JREL(c, FOR_ITER, cleanup);
2462 VISIT(c, expr, s->v.For.target);
2463 VISIT_SEQ(c, stmt, s->v.For.body);
2464 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2465 compiler_use_next_block(c, cleanup);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002466
2467 compiler_pop_fblock(c, FOR_LOOP, start);
2468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 VISIT_SEQ(c, stmt, s->v.For.orelse);
2470 compiler_use_next_block(c, end);
2471 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002472}
2473
Yury Selivanov75445082015-05-11 22:57:16 -04002474
2475static int
2476compiler_async_for(struct compiler *c, stmt_ty s)
2477{
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002478 basicblock *start, *except, *end;
Zsolt Dollensteine2396502018-04-27 08:58:56 -07002479 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
2480 return compiler_error(c, "'async for' outside async function");
2481 }
2482
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002483 start = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002484 except = compiler_new_block(c);
2485 end = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002486
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002487 if (start == NULL || except == NULL || end == NULL)
Yury Selivanov75445082015-05-11 22:57:16 -04002488 return 0;
2489
2490 VISIT(c, expr, s->v.AsyncFor.iter);
2491 ADDOP(c, GET_AITER);
Yury Selivanov75445082015-05-11 22:57:16 -04002492
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002493 compiler_use_next_block(c, start);
2494 if (!compiler_push_fblock(c, FOR_LOOP, start, end))
2495 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04002496
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002497 /* SETUP_FINALLY to guard the __anext__ call */
2498 ADDOP_JREL(c, SETUP_FINALLY, except);
Yury Selivanov75445082015-05-11 22:57:16 -04002499 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002500 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04002501 ADDOP(c, YIELD_FROM);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002502 ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */
Yury Selivanov75445082015-05-11 22:57:16 -04002503
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002504 /* Success block for __anext__ */
2505 VISIT(c, expr, s->v.AsyncFor.target);
2506 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
2507 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2508
2509 compiler_pop_fblock(c, FOR_LOOP, start);
Yury Selivanov75445082015-05-11 22:57:16 -04002510
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002511 /* Except block for __anext__ */
Yury Selivanov75445082015-05-11 22:57:16 -04002512 compiler_use_next_block(c, except);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002513 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov75445082015-05-11 22:57:16 -04002514
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002515 /* `else` block */
Yury Selivanov75445082015-05-11 22:57:16 -04002516 VISIT_SEQ(c, stmt, s->v.For.orelse);
2517
2518 compiler_use_next_block(c, end);
2519
2520 return 1;
2521}
2522
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002523static int
2524compiler_while(struct compiler *c, stmt_ty s)
2525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002526 basicblock *loop, *orelse, *end, *anchor = NULL;
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02002527 int constant = expr_constant(s->v.While.test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 if (constant == 0) {
2530 if (s->v.While.orelse)
2531 VISIT_SEQ(c, stmt, s->v.While.orelse);
2532 return 1;
2533 }
2534 loop = compiler_new_block(c);
2535 end = compiler_new_block(c);
2536 if (constant == -1) {
2537 anchor = compiler_new_block(c);
2538 if (anchor == NULL)
2539 return 0;
2540 }
2541 if (loop == NULL || end == NULL)
2542 return 0;
2543 if (s->v.While.orelse) {
2544 orelse = compiler_new_block(c);
2545 if (orelse == NULL)
2546 return 0;
2547 }
2548 else
2549 orelse = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002551 compiler_use_next_block(c, loop);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002552 if (!compiler_push_fblock(c, WHILE_LOOP, loop, end))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002553 return 0;
2554 if (constant == -1) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002555 if (!compiler_jump_if(c, s->v.While.test, anchor, 0))
2556 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002557 }
2558 VISIT_SEQ(c, stmt, s->v.While.body);
2559 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002561 /* XXX should the two POP instructions be in a separate block
2562 if there is no else clause ?
2563 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002564
Benjamin Peterson3cda0ed2014-12-13 16:06:19 -05002565 if (constant == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002566 compiler_use_next_block(c, anchor);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002567 compiler_pop_fblock(c, WHILE_LOOP, loop);
2568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002569 if (orelse != NULL) /* what if orelse is just pass? */
2570 VISIT_SEQ(c, stmt, s->v.While.orelse);
2571 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002573 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002574}
2575
2576static int
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002577compiler_return(struct compiler *c, stmt_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002579 int preserve_tos = ((s->v.Return.value != NULL) &&
2580 !is_const(s->v.Return.value));
2581 if (c->u->u_ste->ste_type != FunctionBlock)
2582 return compiler_error(c, "'return' outside function");
2583 if (s->v.Return.value != NULL &&
2584 c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2585 {
2586 return compiler_error(
2587 c, "'return' with value in async generator");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002588 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002589 if (preserve_tos) {
2590 VISIT(c, expr, s->v.Return.value);
2591 }
2592 for (int depth = c->u->u_nfblocks; depth--;) {
2593 struct fblockinfo *info = &c->u->u_fblock[depth];
2594
2595 if (!compiler_unwind_fblock(c, info, preserve_tos))
2596 return 0;
2597 }
2598 if (s->v.Return.value == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002599 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002600 }
2601 else if (!preserve_tos) {
2602 VISIT(c, expr, s->v.Return.value);
2603 }
2604 ADDOP(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002606 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002607}
2608
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002609static int
2610compiler_break(struct compiler *c)
2611{
2612 for (int depth = c->u->u_nfblocks; depth--;) {
2613 struct fblockinfo *info = &c->u->u_fblock[depth];
2614
2615 if (!compiler_unwind_fblock(c, info, 0))
2616 return 0;
2617 if (info->fb_type == WHILE_LOOP || info->fb_type == FOR_LOOP) {
2618 ADDOP_JABS(c, JUMP_ABSOLUTE, info->fb_exit);
2619 return 1;
2620 }
2621 }
2622 return compiler_error(c, "'break' outside loop");
2623}
2624
2625static int
2626compiler_continue(struct compiler *c)
2627{
2628 for (int depth = c->u->u_nfblocks; depth--;) {
2629 struct fblockinfo *info = &c->u->u_fblock[depth];
2630
2631 if (info->fb_type == WHILE_LOOP || info->fb_type == FOR_LOOP) {
2632 ADDOP_JABS(c, JUMP_ABSOLUTE, info->fb_block);
2633 return 1;
2634 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002635 if (!compiler_unwind_fblock(c, info, 0))
2636 return 0;
2637 }
2638 return compiler_error(c, "'continue' not properly in loop");
2639}
2640
2641
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002642/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002643
2644 SETUP_FINALLY L
2645 <code for body>
2646 POP_BLOCK
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002647 BEGIN_FINALLY
2648 L:
2649 <code for finalbody>
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 END_FINALLY
2651
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652 The special instructions use the block stack. Each block
2653 stack entry contains the instruction that created it (here
2654 SETUP_FINALLY), the level of the value stack at the time the
2655 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002656
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002657 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002658 Pushes the current value stack level and the label
2659 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002660 POP_BLOCK:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002661 Pops en entry from the block stack.
2662 BEGIN_FINALLY
2663 Pushes NULL onto the value stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002664 END_FINALLY:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002665 Pops 1 (NULL or int) or 6 entries from the *value* stack and restore
2666 the raised and the caught exceptions they specify.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002667
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002668 The block stack is unwound when an exception is raised:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002669 when a SETUP_FINALLY entry is found, the raised and the caught
2670 exceptions are pushed onto the value stack (and the exception
2671 condition is cleared), and the interpreter jumps to the label
2672 gotten from the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002673*/
2674
2675static int
2676compiler_try_finally(struct compiler *c, stmt_ty s)
2677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002678 basicblock *body, *end;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002680 body = compiler_new_block(c);
2681 end = compiler_new_block(c);
2682 if (body == NULL || end == NULL)
2683 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002684
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002685 /* `try` block */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002686 ADDOP_JREL(c, SETUP_FINALLY, end);
2687 compiler_use_next_block(c, body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002688 if (!compiler_push_fblock(c, FINALLY_TRY, body, end))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002689 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002690 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
2691 if (!compiler_try_except(c, s))
2692 return 0;
2693 }
2694 else {
2695 VISIT_SEQ(c, stmt, s->v.Try.body);
2696 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002697 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002698 ADDOP(c, BEGIN_FINALLY);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002699 compiler_pop_fblock(c, FINALLY_TRY, body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002700
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002701 /* `finally` block */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002702 compiler_use_next_block(c, end);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002703 if (!compiler_push_fblock(c, FINALLY_END, end, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002704 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002705 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002706 ADDOP(c, END_FINALLY);
2707 compiler_pop_fblock(c, FINALLY_END, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002708 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002709}
2710
2711/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002712 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002713 (The contents of the value stack is shown in [], with the top
2714 at the right; 'tb' is trace-back info, 'val' the exception's
2715 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716
2717 Value stack Label Instruction Argument
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002718 [] SETUP_FINALLY L1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002719 [] <code for S>
2720 [] POP_BLOCK
2721 [] JUMP_FORWARD L0
2722
2723 [tb, val, exc] L1: DUP )
2724 [tb, val, exc, exc] <evaluate E1> )
2725 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2726 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
2727 [tb, val, exc] POP
2728 [tb, val] <assign to V1> (or POP if no V1)
2729 [tb] POP
2730 [] <code for S1>
2731 JUMP_FORWARD L0
2732
2733 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002734 .............................etc.......................
2735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002736 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
2737
2738 [] L0: <next statement>
2739
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740 Of course, parts are not generated if Vi or Ei is not present.
2741*/
2742static int
2743compiler_try_except(struct compiler *c, stmt_ty s)
2744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002745 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01002746 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002748 body = compiler_new_block(c);
2749 except = compiler_new_block(c);
2750 orelse = compiler_new_block(c);
2751 end = compiler_new_block(c);
2752 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
2753 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002754 ADDOP_JREL(c, SETUP_FINALLY, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 compiler_use_next_block(c, body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002756 if (!compiler_push_fblock(c, EXCEPT, body, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002758 VISIT_SEQ(c, stmt, s->v.Try.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002759 ADDOP(c, POP_BLOCK);
2760 compiler_pop_fblock(c, EXCEPT, body);
2761 ADDOP_JREL(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002762 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 compiler_use_next_block(c, except);
2764 for (i = 0; i < n; i++) {
2765 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002766 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 if (!handler->v.ExceptHandler.type && i < n-1)
2768 return compiler_error(c, "default 'except:' must be last");
2769 c->u->u_lineno_set = 0;
2770 c->u->u_lineno = handler->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00002771 c->u->u_col_offset = handler->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 except = compiler_new_block(c);
2773 if (except == NULL)
2774 return 0;
2775 if (handler->v.ExceptHandler.type) {
2776 ADDOP(c, DUP_TOP);
2777 VISIT(c, expr, handler->v.ExceptHandler.type);
2778 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2779 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
2780 }
2781 ADDOP(c, POP_TOP);
2782 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002783 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00002784
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002785 cleanup_end = compiler_new_block(c);
2786 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05002787 if (!(cleanup_end || cleanup_body))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002788 return 0;
Guido van Rossumb940e112007-01-10 16:19:56 +00002789
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002790 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
2791 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002793 /*
2794 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03002795 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002796 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03002797 try:
2798 # body
2799 finally:
2800 name = None
2801 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002802 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002803
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002804 /* second try: */
2805 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2806 compiler_use_next_block(c, cleanup_body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002807 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, cleanup_end))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002808 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002810 /* second # body */
2811 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2812 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002813 ADDOP(c, BEGIN_FINALLY);
2814 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002815
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002816 /* finally: */
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002817 compiler_use_next_block(c, cleanup_end);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002818 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end, NULL))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002819 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002820
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002821 /* name = None; del name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002822 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002823 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002824 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002825
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002826 ADDOP(c, END_FINALLY);
Serhiy Storchakad4864c62018-01-09 21:54:52 +02002827 ADDOP(c, POP_EXCEPT);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002828 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 }
2830 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002831 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002833 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05002834 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002835 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836
Guido van Rossumb940e112007-01-10 16:19:56 +00002837 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002838 ADDOP(c, POP_TOP);
2839 compiler_use_next_block(c, cleanup_body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002840 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002841 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002843 ADDOP(c, POP_EXCEPT);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002844 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 }
2846 ADDOP_JREL(c, JUMP_FORWARD, end);
2847 compiler_use_next_block(c, except);
2848 }
2849 ADDOP(c, END_FINALLY);
2850 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002851 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 compiler_use_next_block(c, end);
2853 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002854}
2855
2856static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002857compiler_try(struct compiler *c, stmt_ty s) {
2858 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
2859 return compiler_try_finally(c, s);
2860 else
2861 return compiler_try_except(c, s);
2862}
2863
2864
2865static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002866compiler_import_as(struct compiler *c, identifier name, identifier asname)
2867{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 /* The IMPORT_NAME opcode was already generated. This function
2869 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 If there is a dot in name, we need to split it and emit a
Serhiy Storchakaf93234b2017-05-09 22:31:05 +03002872 IMPORT_FROM for each name.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03002874 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
2875 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002876 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03002877 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002878 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 /* Consume the base module name to get the first attribute */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03002880 while (1) {
2881 Py_ssize_t pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 PyObject *attr;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03002883 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002884 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03002885 return 0;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03002886 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 if (!attr)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03002888 return 0;
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03002889 ADDOP_N(c, IMPORT_FROM, attr, names);
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03002890 if (dot == -1) {
2891 break;
2892 }
2893 ADDOP(c, ROT_TWO);
2894 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 }
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03002896 if (!compiler_nameop(c, asname, Store)) {
2897 return 0;
2898 }
2899 ADDOP(c, POP_TOP);
2900 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 }
2902 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002903}
2904
2905static int
2906compiler_import(struct compiler *c, stmt_ty s)
2907{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 /* The Import node stores a module name like a.b.c as a single
2909 string. This is convenient for all cases except
2910 import a.b.c as d
2911 where we need to parse that string to extract the individual
2912 module names.
2913 XXX Perhaps change the representation to make this case simpler?
2914 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01002915 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 for (i = 0; i < n; i++) {
2918 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
2919 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002920
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002921 ADDOP_LOAD_CONST(c, _PyLong_Zero);
2922 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 if (alias->asname) {
2926 r = compiler_import_as(c, alias->name, alias->asname);
2927 if (!r)
2928 return r;
2929 }
2930 else {
2931 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002932 Py_ssize_t dot = PyUnicode_FindChar(
2933 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02002934 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002935 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02002936 if (tmp == NULL)
2937 return 0;
2938 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002940 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002941 Py_DECREF(tmp);
2942 }
2943 if (!r)
2944 return r;
2945 }
2946 }
2947 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002948}
2949
2950static int
2951compiler_from_import(struct compiler *c, stmt_ty s)
2952{
Victor Stinnerad9a0662013-11-19 22:23:20 +01002953 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002954 PyObject *names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00002956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002957 if (!empty_string) {
2958 empty_string = PyUnicode_FromString("");
2959 if (!empty_string)
2960 return 0;
2961 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002962
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002963 ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02002964
2965 names = PyTuple_New(n);
2966 if (!names)
2967 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002969 /* build up the names */
2970 for (i = 0; i < n; i++) {
2971 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2972 Py_INCREF(alias->name);
2973 PyTuple_SET_ITEM(names, i, alias->name);
2974 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002976 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02002977 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 Py_DECREF(names);
2979 return compiler_error(c, "from __future__ imports must occur "
2980 "at the beginning of the file");
2981 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002982 ADDOP_LOAD_CONST_NEW(c, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002984 if (s->v.ImportFrom.module) {
2985 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2986 }
2987 else {
2988 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
2989 }
2990 for (i = 0; i < n; i++) {
2991 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2992 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002993
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002994 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002995 assert(n == 1);
2996 ADDOP(c, IMPORT_STAR);
2997 return 1;
2998 }
2999
3000 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
3001 store_name = alias->name;
3002 if (alias->asname)
3003 store_name = alias->asname;
3004
3005 if (!compiler_nameop(c, store_name, Store)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 return 0;
3007 }
3008 }
3009 /* remove imported module */
3010 ADDOP(c, POP_TOP);
3011 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003012}
3013
3014static int
3015compiler_assert(struct compiler *c, stmt_ty s)
3016{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 static PyObject *assertion_error = NULL;
3018 basicblock *end;
Victor Stinner14e461d2013-08-26 22:28:21 +02003019 PyObject* msg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003020
Georg Brandl8334fd92010-12-04 10:26:46 +00003021 if (c->c_optimize)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 return 1;
3023 if (assertion_error == NULL) {
3024 assertion_error = PyUnicode_InternFromString("AssertionError");
3025 if (assertion_error == NULL)
3026 return 0;
3027 }
3028 if (s->v.Assert.test->kind == Tuple_kind &&
3029 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
Victor Stinner14e461d2013-08-26 22:28:21 +02003030 msg = PyUnicode_FromString("assertion is always true, "
3031 "perhaps remove parentheses?");
3032 if (msg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 return 0;
Victor Stinner14e461d2013-08-26 22:28:21 +02003034 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
3035 c->c_filename, c->u->u_lineno,
3036 NULL, NULL) == -1) {
3037 Py_DECREF(msg);
3038 return 0;
3039 }
3040 Py_DECREF(msg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003042 end = compiler_new_block(c);
3043 if (end == NULL)
3044 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003045 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
3046 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003047 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
3048 if (s->v.Assert.msg) {
3049 VISIT(c, expr, s->v.Assert.msg);
3050 ADDOP_I(c, CALL_FUNCTION, 1);
3051 }
3052 ADDOP_I(c, RAISE_VARARGS, 1);
3053 compiler_use_next_block(c, end);
3054 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003055}
3056
3057static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003058compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3059{
3060 if (c->c_interactive && c->c_nestlevel <= 1) {
3061 VISIT(c, expr, value);
3062 ADDOP(c, PRINT_EXPR);
3063 return 1;
3064 }
3065
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003066 if (is_const(value)) {
Victor Stinner15a30952016-02-08 22:45:06 +01003067 /* ignore constant statement */
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003068 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003069 }
3070
3071 VISIT(c, expr, value);
3072 ADDOP(c, POP_TOP);
3073 return 1;
3074}
3075
3076static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003077compiler_visit_stmt(struct compiler *c, stmt_ty s)
3078{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003079 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081 /* Always assign a lineno to the next instruction for a stmt. */
3082 c->u->u_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00003083 c->u->u_col_offset = s->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003084 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003086 switch (s->kind) {
3087 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04003088 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089 case ClassDef_kind:
3090 return compiler_class(c, s);
3091 case Return_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003092 return compiler_return(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093 case Delete_kind:
3094 VISIT_SEQ(c, expr, s->v.Delete.targets)
3095 break;
3096 case Assign_kind:
3097 n = asdl_seq_LEN(s->v.Assign.targets);
3098 VISIT(c, expr, s->v.Assign.value);
3099 for (i = 0; i < n; i++) {
3100 if (i < n - 1)
3101 ADDOP(c, DUP_TOP);
3102 VISIT(c, expr,
3103 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3104 }
3105 break;
3106 case AugAssign_kind:
3107 return compiler_augassign(c, s);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003108 case AnnAssign_kind:
3109 return compiler_annassign(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003110 case For_kind:
3111 return compiler_for(c, s);
3112 case While_kind:
3113 return compiler_while(c, s);
3114 case If_kind:
3115 return compiler_if(c, s);
3116 case Raise_kind:
3117 n = 0;
3118 if (s->v.Raise.exc) {
3119 VISIT(c, expr, s->v.Raise.exc);
3120 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003121 if (s->v.Raise.cause) {
3122 VISIT(c, expr, s->v.Raise.cause);
3123 n++;
3124 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003125 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01003126 ADDOP_I(c, RAISE_VARARGS, (int)n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003127 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003128 case Try_kind:
3129 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003130 case Assert_kind:
3131 return compiler_assert(c, s);
3132 case Import_kind:
3133 return compiler_import(c, s);
3134 case ImportFrom_kind:
3135 return compiler_from_import(c, s);
3136 case Global_kind:
3137 case Nonlocal_kind:
3138 break;
3139 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003140 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003141 case Pass_kind:
3142 break;
3143 case Break_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003144 return compiler_break(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145 case Continue_kind:
3146 return compiler_continue(c);
3147 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003148 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04003149 case AsyncFunctionDef_kind:
3150 return compiler_function(c, s, 1);
3151 case AsyncWith_kind:
3152 return compiler_async_with(c, s, 0);
3153 case AsyncFor_kind:
3154 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003155 }
Yury Selivanov75445082015-05-11 22:57:16 -04003156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003157 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003158}
3159
3160static int
3161unaryop(unaryop_ty op)
3162{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003163 switch (op) {
3164 case Invert:
3165 return UNARY_INVERT;
3166 case Not:
3167 return UNARY_NOT;
3168 case UAdd:
3169 return UNARY_POSITIVE;
3170 case USub:
3171 return UNARY_NEGATIVE;
3172 default:
3173 PyErr_Format(PyExc_SystemError,
3174 "unary op %d should not be possible", op);
3175 return 0;
3176 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003177}
3178
3179static int
3180binop(struct compiler *c, operator_ty op)
3181{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003182 switch (op) {
3183 case Add:
3184 return BINARY_ADD;
3185 case Sub:
3186 return BINARY_SUBTRACT;
3187 case Mult:
3188 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003189 case MatMult:
3190 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003191 case Div:
3192 return BINARY_TRUE_DIVIDE;
3193 case Mod:
3194 return BINARY_MODULO;
3195 case Pow:
3196 return BINARY_POWER;
3197 case LShift:
3198 return BINARY_LSHIFT;
3199 case RShift:
3200 return BINARY_RSHIFT;
3201 case BitOr:
3202 return BINARY_OR;
3203 case BitXor:
3204 return BINARY_XOR;
3205 case BitAnd:
3206 return BINARY_AND;
3207 case FloorDiv:
3208 return BINARY_FLOOR_DIVIDE;
3209 default:
3210 PyErr_Format(PyExc_SystemError,
3211 "binary op %d should not be possible", op);
3212 return 0;
3213 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003214}
3215
3216static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003217inplace_binop(struct compiler *c, operator_ty op)
3218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 switch (op) {
3220 case Add:
3221 return INPLACE_ADD;
3222 case Sub:
3223 return INPLACE_SUBTRACT;
3224 case Mult:
3225 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003226 case MatMult:
3227 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003228 case Div:
3229 return INPLACE_TRUE_DIVIDE;
3230 case Mod:
3231 return INPLACE_MODULO;
3232 case Pow:
3233 return INPLACE_POWER;
3234 case LShift:
3235 return INPLACE_LSHIFT;
3236 case RShift:
3237 return INPLACE_RSHIFT;
3238 case BitOr:
3239 return INPLACE_OR;
3240 case BitXor:
3241 return INPLACE_XOR;
3242 case BitAnd:
3243 return INPLACE_AND;
3244 case FloorDiv:
3245 return INPLACE_FLOOR_DIVIDE;
3246 default:
3247 PyErr_Format(PyExc_SystemError,
3248 "inplace binary op %d should not be possible", op);
3249 return 0;
3250 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003251}
3252
3253static int
3254compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3255{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003256 int op, scope;
3257 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003258 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003260 PyObject *dict = c->u->u_names;
3261 PyObject *mangled;
3262 /* XXX AugStore isn't used anywhere! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003263
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003264 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3265 !_PyUnicode_EqualToASCIIString(name, "True") &&
3266 !_PyUnicode_EqualToASCIIString(name, "False"));
Benjamin Peterson70b224d2012-12-06 17:49:58 -05003267
Serhiy Storchakabd6ec4d2017-12-18 14:29:12 +02003268 mangled = _Py_Mangle(c->u->u_private, name);
3269 if (!mangled)
3270 return 0;
3271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 op = 0;
3273 optype = OP_NAME;
3274 scope = PyST_GetScope(c->u->u_ste, mangled);
3275 switch (scope) {
3276 case FREE:
3277 dict = c->u->u_freevars;
3278 optype = OP_DEREF;
3279 break;
3280 case CELL:
3281 dict = c->u->u_cellvars;
3282 optype = OP_DEREF;
3283 break;
3284 case LOCAL:
3285 if (c->u->u_ste->ste_type == FunctionBlock)
3286 optype = OP_FAST;
3287 break;
3288 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04003289 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290 optype = OP_GLOBAL;
3291 break;
3292 case GLOBAL_EXPLICIT:
3293 optype = OP_GLOBAL;
3294 break;
3295 default:
3296 /* scope can be 0 */
3297 break;
3298 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003300 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003301 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003303 switch (optype) {
3304 case OP_DEREF:
3305 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003306 case Load:
3307 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3308 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003309 case Store: op = STORE_DEREF; break;
3310 case AugLoad:
3311 case AugStore:
3312 break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003313 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 case Param:
3315 default:
3316 PyErr_SetString(PyExc_SystemError,
3317 "param invalid for deref variable");
3318 return 0;
3319 }
3320 break;
3321 case OP_FAST:
3322 switch (ctx) {
3323 case Load: op = LOAD_FAST; break;
3324 case Store: op = STORE_FAST; break;
3325 case Del: op = DELETE_FAST; break;
3326 case AugLoad:
3327 case AugStore:
3328 break;
3329 case Param:
3330 default:
3331 PyErr_SetString(PyExc_SystemError,
3332 "param invalid for local variable");
3333 return 0;
3334 }
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003335 ADDOP_N(c, op, mangled, varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003336 return 1;
3337 case OP_GLOBAL:
3338 switch (ctx) {
3339 case Load: op = LOAD_GLOBAL; break;
3340 case Store: op = STORE_GLOBAL; break;
3341 case Del: op = DELETE_GLOBAL; break;
3342 case AugLoad:
3343 case AugStore:
3344 break;
3345 case Param:
3346 default:
3347 PyErr_SetString(PyExc_SystemError,
3348 "param invalid for global variable");
3349 return 0;
3350 }
3351 break;
3352 case OP_NAME:
3353 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00003354 case Load: op = LOAD_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 case Store: op = STORE_NAME; break;
3356 case Del: op = DELETE_NAME; break;
3357 case AugLoad:
3358 case AugStore:
3359 break;
3360 case Param:
3361 default:
3362 PyErr_SetString(PyExc_SystemError,
3363 "param invalid for name variable");
3364 return 0;
3365 }
3366 break;
3367 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003369 assert(op);
3370 arg = compiler_add_o(c, dict, mangled);
3371 Py_DECREF(mangled);
3372 if (arg < 0)
3373 return 0;
3374 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003375}
3376
3377static int
3378compiler_boolop(struct compiler *c, expr_ty e)
3379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003380 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003381 int jumpi;
3382 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003383 asdl_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003385 assert(e->kind == BoolOp_kind);
3386 if (e->v.BoolOp.op == And)
3387 jumpi = JUMP_IF_FALSE_OR_POP;
3388 else
3389 jumpi = JUMP_IF_TRUE_OR_POP;
3390 end = compiler_new_block(c);
3391 if (end == NULL)
3392 return 0;
3393 s = e->v.BoolOp.values;
3394 n = asdl_seq_LEN(s) - 1;
3395 assert(n >= 0);
3396 for (i = 0; i < n; ++i) {
3397 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
3398 ADDOP_JABS(c, jumpi, end);
3399 }
3400 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3401 compiler_use_next_block(c, end);
3402 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003403}
3404
3405static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003406starunpack_helper(struct compiler *c, asdl_seq *elts,
3407 int single_op, int inner_op, int outer_op)
3408{
3409 Py_ssize_t n = asdl_seq_LEN(elts);
3410 Py_ssize_t i, nsubitems = 0, nseen = 0;
3411 for (i = 0; i < n; i++) {
3412 expr_ty elt = asdl_seq_GET(elts, i);
3413 if (elt->kind == Starred_kind) {
3414 if (nseen) {
3415 ADDOP_I(c, inner_op, nseen);
3416 nseen = 0;
3417 nsubitems++;
3418 }
3419 VISIT(c, expr, elt->v.Starred.value);
3420 nsubitems++;
3421 }
3422 else {
3423 VISIT(c, expr, elt);
3424 nseen++;
3425 }
3426 }
3427 if (nsubitems) {
3428 if (nseen) {
3429 ADDOP_I(c, inner_op, nseen);
3430 nsubitems++;
3431 }
3432 ADDOP_I(c, outer_op, nsubitems);
3433 }
3434 else
3435 ADDOP_I(c, single_op, nseen);
3436 return 1;
3437}
3438
3439static int
3440assignment_helper(struct compiler *c, asdl_seq *elts)
3441{
3442 Py_ssize_t n = asdl_seq_LEN(elts);
3443 Py_ssize_t i;
3444 int seen_star = 0;
3445 for (i = 0; i < n; i++) {
3446 expr_ty elt = asdl_seq_GET(elts, i);
3447 if (elt->kind == Starred_kind && !seen_star) {
3448 if ((i >= (1 << 8)) ||
3449 (n-i-1 >= (INT_MAX >> 8)))
3450 return compiler_error(c,
3451 "too many expressions in "
3452 "star-unpacking assignment");
3453 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3454 seen_star = 1;
3455 asdl_seq_SET(elts, i, elt->v.Starred.value);
3456 }
3457 else if (elt->kind == Starred_kind) {
3458 return compiler_error(c,
3459 "two starred expressions in assignment");
3460 }
3461 }
3462 if (!seen_star) {
3463 ADDOP_I(c, UNPACK_SEQUENCE, n);
3464 }
3465 VISIT_SEQ(c, expr, elts);
3466 return 1;
3467}
3468
3469static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003470compiler_list(struct compiler *c, expr_ty e)
3471{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003472 asdl_seq *elts = e->v.List.elts;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003473 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003474 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003475 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003476 else if (e->v.List.ctx == Load) {
3477 return starunpack_helper(c, elts,
3478 BUILD_LIST, BUILD_TUPLE, BUILD_LIST_UNPACK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003479 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003480 else
3481 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003483}
3484
3485static int
3486compiler_tuple(struct compiler *c, expr_ty e)
3487{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003488 asdl_seq *elts = e->v.Tuple.elts;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003490 return assignment_helper(c, elts);
3491 }
3492 else if (e->v.Tuple.ctx == Load) {
3493 return starunpack_helper(c, elts,
3494 BUILD_TUPLE, BUILD_TUPLE, BUILD_TUPLE_UNPACK);
3495 }
3496 else
3497 VISIT_SEQ(c, expr, elts);
3498 return 1;
3499}
3500
3501static int
3502compiler_set(struct compiler *c, expr_ty e)
3503{
3504 return starunpack_helper(c, e->v.Set.elts, BUILD_SET,
3505 BUILD_SET, BUILD_SET_UNPACK);
3506}
3507
3508static int
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003509are_all_items_const(asdl_seq *seq, Py_ssize_t begin, Py_ssize_t end)
3510{
3511 Py_ssize_t i;
3512 for (i = begin; i < end; i++) {
3513 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
3514 if (key == NULL || !is_const(key))
3515 return 0;
3516 }
3517 return 1;
3518}
3519
3520static int
3521compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3522{
3523 Py_ssize_t i, n = end - begin;
3524 PyObject *keys, *key;
3525 if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) {
3526 for (i = begin; i < end; i++) {
3527 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3528 }
3529 keys = PyTuple_New(n);
3530 if (keys == NULL) {
3531 return 0;
3532 }
3533 for (i = begin; i < end; i++) {
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02003534 key = get_const_value((expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003535 Py_INCREF(key);
3536 PyTuple_SET_ITEM(keys, i - begin, key);
3537 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003538 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003539 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3540 }
3541 else {
3542 for (i = begin; i < end; i++) {
3543 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3544 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3545 }
3546 ADDOP_I(c, BUILD_MAP, n);
3547 }
3548 return 1;
3549}
3550
3551static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003552compiler_dict(struct compiler *c, expr_ty e)
3553{
Victor Stinner976bb402016-03-23 11:36:19 +01003554 Py_ssize_t i, n, elements;
3555 int containers;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003556 int is_unpacking = 0;
3557 n = asdl_seq_LEN(e->v.Dict.values);
3558 containers = 0;
3559 elements = 0;
3560 for (i = 0; i < n; i++) {
3561 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
3562 if (elements == 0xFFFF || (elements && is_unpacking)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003563 if (!compiler_subdict(c, e, i - elements, i))
3564 return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003565 containers++;
3566 elements = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003567 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003568 if (is_unpacking) {
3569 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3570 containers++;
3571 }
3572 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003573 elements++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 }
3575 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003576 if (elements || containers == 0) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003577 if (!compiler_subdict(c, e, n - elements, n))
3578 return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003579 containers++;
3580 }
3581 /* If there is more than one dict, they need to be merged into a new
3582 * dict. If there is one dict and it's an unpacking, then it needs
3583 * to be copied into a new dict." */
Serhiy Storchaka3d85fae2016-11-28 20:56:37 +02003584 if (containers > 1 || is_unpacking) {
3585 ADDOP_I(c, BUILD_MAP_UNPACK, containers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003586 }
3587 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003588}
3589
3590static int
3591compiler_compare(struct compiler *c, expr_ty e)
3592{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003593 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003595 VISIT(c, expr, e->v.Compare.left);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003596 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
3597 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
3598 if (n == 0) {
3599 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
3600 ADDOP_I(c, COMPARE_OP,
3601 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, 0))));
3602 }
3603 else {
3604 basicblock *cleanup = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 if (cleanup == NULL)
3606 return 0;
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003607 for (i = 0; i < n; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 VISIT(c, expr,
3609 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003610 ADDOP(c, DUP_TOP);
3611 ADDOP(c, ROT_THREE);
3612 ADDOP_I(c, COMPARE_OP,
3613 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, i))));
3614 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
3615 NEXT_BLOCK(c);
3616 }
3617 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
3618 ADDOP_I(c, COMPARE_OP,
3619 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n))));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003620 basicblock *end = compiler_new_block(c);
3621 if (end == NULL)
3622 return 0;
3623 ADDOP_JREL(c, JUMP_FORWARD, end);
3624 compiler_use_next_block(c, cleanup);
3625 ADDOP(c, ROT_TWO);
3626 ADDOP(c, POP_TOP);
3627 compiler_use_next_block(c, end);
3628 }
3629 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003630}
3631
3632static int
Yury Selivanovf2392132016-12-13 19:03:51 -05003633maybe_optimize_method_call(struct compiler *c, expr_ty e)
3634{
3635 Py_ssize_t argsl, i;
3636 expr_ty meth = e->v.Call.func;
3637 asdl_seq *args = e->v.Call.args;
3638
3639 /* Check that the call node is an attribute access, and that
3640 the call doesn't have keyword parameters. */
3641 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
3642 asdl_seq_LEN(e->v.Call.keywords))
3643 return -1;
3644
3645 /* Check that there are no *varargs types of arguments. */
3646 argsl = asdl_seq_LEN(args);
3647 for (i = 0; i < argsl; i++) {
3648 expr_ty elt = asdl_seq_GET(args, i);
3649 if (elt->kind == Starred_kind) {
3650 return -1;
3651 }
3652 }
3653
3654 /* Alright, we can optimize the code. */
3655 VISIT(c, expr, meth->v.Attribute.value);
3656 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
3657 VISIT_SEQ(c, expr, e->v.Call.args);
3658 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
3659 return 1;
3660}
3661
3662static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003663compiler_call(struct compiler *c, expr_ty e)
3664{
Yury Selivanovf2392132016-12-13 19:03:51 -05003665 if (maybe_optimize_method_call(c, e) > 0)
3666 return 1;
3667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003668 VISIT(c, expr, e->v.Call.func);
3669 return compiler_call_helper(c, 0,
3670 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003671 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003672}
3673
Eric V. Smith235a6f02015-09-19 14:51:32 -04003674static int
3675compiler_joined_str(struct compiler *c, expr_ty e)
3676{
Eric V. Smith235a6f02015-09-19 14:51:32 -04003677 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02003678 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1)
3679 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
Eric V. Smith235a6f02015-09-19 14:51:32 -04003680 return 1;
3681}
3682
Eric V. Smitha78c7952015-11-03 12:45:05 -05003683/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04003684static int
3685compiler_formatted_value(struct compiler *c, expr_ty e)
3686{
Eric V. Smitha78c7952015-11-03 12:45:05 -05003687 /* Our oparg encodes 2 pieces of information: the conversion
3688 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04003689
Eric V. Smitha78c7952015-11-03 12:45:05 -05003690 Convert the conversion char to 2 bits:
3691 None: 000 0x0 FVC_NONE
3692 !s : 001 0x1 FVC_STR
3693 !r : 010 0x2 FVC_REPR
3694 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04003695
Eric V. Smitha78c7952015-11-03 12:45:05 -05003696 next bit is whether or not we have a format spec:
3697 yes : 100 0x4
3698 no : 000 0x0
3699 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04003700
Eric V. Smitha78c7952015-11-03 12:45:05 -05003701 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003702
Eric V. Smitha78c7952015-11-03 12:45:05 -05003703 /* Evaluate the expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04003704 VISIT(c, expr, e->v.FormattedValue.value);
3705
Eric V. Smitha78c7952015-11-03 12:45:05 -05003706 switch (e->v.FormattedValue.conversion) {
3707 case 's': oparg = FVC_STR; break;
3708 case 'r': oparg = FVC_REPR; break;
3709 case 'a': oparg = FVC_ASCII; break;
3710 case -1: oparg = FVC_NONE; break;
3711 default:
3712 PyErr_SetString(PyExc_SystemError,
3713 "Unrecognized conversion character");
3714 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003715 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04003716 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003717 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04003718 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003719 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003720 }
3721
Eric V. Smitha78c7952015-11-03 12:45:05 -05003722 /* And push our opcode and oparg */
3723 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith235a6f02015-09-19 14:51:32 -04003724 return 1;
3725}
3726
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003727static int
3728compiler_subkwargs(struct compiler *c, asdl_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
3729{
3730 Py_ssize_t i, n = end - begin;
3731 keyword_ty kw;
3732 PyObject *keys, *key;
3733 assert(n > 0);
3734 if (n > 1) {
3735 for (i = begin; i < end; i++) {
3736 kw = asdl_seq_GET(keywords, i);
3737 VISIT(c, expr, kw->value);
3738 }
3739 keys = PyTuple_New(n);
3740 if (keys == NULL) {
3741 return 0;
3742 }
3743 for (i = begin; i < end; i++) {
3744 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
3745 Py_INCREF(key);
3746 PyTuple_SET_ITEM(keys, i - begin, key);
3747 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003748 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003749 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3750 }
3751 else {
3752 /* a for loop only executes once */
3753 for (i = begin; i < end; i++) {
3754 kw = asdl_seq_GET(keywords, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003755 ADDOP_LOAD_CONST(c, kw->arg);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003756 VISIT(c, expr, kw->value);
3757 }
3758 ADDOP_I(c, BUILD_MAP, n);
3759 }
3760 return 1;
3761}
3762
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003763/* shared code between compiler_call and compiler_class */
3764static int
3765compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01003766 int n, /* Args already pushed */
Victor Stinnerad9a0662013-11-19 22:23:20 +01003767 asdl_seq *args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003768 asdl_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003769{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003770 Py_ssize_t i, nseen, nelts, nkwelts;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003771 int mustdictunpack = 0;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003772
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003773 /* the number of tuples and dictionaries on the stack */
3774 Py_ssize_t nsubargs = 0, nsubkwargs = 0;
3775
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003776 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003777 nkwelts = asdl_seq_LEN(keywords);
3778
3779 for (i = 0; i < nkwelts; i++) {
3780 keyword_ty kw = asdl_seq_GET(keywords, i);
3781 if (kw->arg == NULL) {
3782 mustdictunpack = 1;
3783 break;
3784 }
3785 }
3786
3787 nseen = n; /* the number of positional arguments on the stack */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003788 for (i = 0; i < nelts; i++) {
3789 expr_ty elt = asdl_seq_GET(args, i);
3790 if (elt->kind == Starred_kind) {
3791 /* A star-arg. If we've seen positional arguments,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003792 pack the positional arguments into a tuple. */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003793 if (nseen) {
3794 ADDOP_I(c, BUILD_TUPLE, nseen);
3795 nseen = 0;
3796 nsubargs++;
3797 }
3798 VISIT(c, expr, elt->v.Starred.value);
3799 nsubargs++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003800 }
3801 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003802 VISIT(c, expr, elt);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003803 nseen++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003804 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003805 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003806
3807 /* Same dance again for keyword arguments */
Serhiy Storchakab7281052016-09-12 00:52:40 +03003808 if (nsubargs || mustdictunpack) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003809 if (nseen) {
3810 /* Pack up any trailing positional arguments. */
3811 ADDOP_I(c, BUILD_TUPLE, nseen);
3812 nsubargs++;
3813 }
Serhiy Storchakab7281052016-09-12 00:52:40 +03003814 if (nsubargs > 1) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003815 /* If we ended up with more than one stararg, we need
3816 to concatenate them into a single sequence. */
Serhiy Storchaka73442852016-10-02 10:33:46 +03003817 ADDOP_I(c, BUILD_TUPLE_UNPACK_WITH_CALL, nsubargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003818 }
3819 else if (nsubargs == 0) {
3820 ADDOP_I(c, BUILD_TUPLE, 0);
3821 }
3822 nseen = 0; /* the number of keyword arguments on the stack following */
3823 for (i = 0; i < nkwelts; i++) {
3824 keyword_ty kw = asdl_seq_GET(keywords, i);
3825 if (kw->arg == NULL) {
3826 /* A keyword argument unpacking. */
3827 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003828 if (!compiler_subkwargs(c, keywords, i - nseen, i))
3829 return 0;
3830 nsubkwargs++;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003831 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003832 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003833 VISIT(c, expr, kw->value);
3834 nsubkwargs++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003835 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003836 else {
3837 nseen++;
3838 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003839 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003840 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003841 /* Pack up any trailing keyword arguments. */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003842 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts))
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003843 return 0;
3844 nsubkwargs++;
3845 }
Serhiy Storchakab7281052016-09-12 00:52:40 +03003846 if (nsubkwargs > 1) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003847 /* Pack it all up */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003848 ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003849 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003850 ADDOP_I(c, CALL_FUNCTION_EX, nsubkwargs > 0);
3851 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003852 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003853 else if (nkwelts) {
3854 PyObject *names;
3855 VISIT_SEQ(c, keyword, keywords);
3856 names = PyTuple_New(nkwelts);
3857 if (names == NULL) {
3858 return 0;
3859 }
3860 for (i = 0; i < nkwelts; i++) {
3861 keyword_ty kw = asdl_seq_GET(keywords, i);
3862 Py_INCREF(kw->arg);
3863 PyTuple_SET_ITEM(names, i, kw->arg);
3864 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003865 ADDOP_LOAD_CONST_NEW(c, names);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003866 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
3867 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003868 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003869 else {
3870 ADDOP_I(c, CALL_FUNCTION, n + nelts);
3871 return 1;
3872 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003873}
3874
Nick Coghlan650f0d02007-04-15 12:05:43 +00003875
3876/* List and set comprehensions and generator expressions work by creating a
3877 nested function to perform the actual iteration. This means that the
3878 iteration variables don't leak into the current scope.
3879 The defined function is called immediately following its definition, with the
3880 result of that call being the result of the expression.
3881 The LC/SC version returns the populated container, while the GE version is
3882 flagged in symtable.c as a generator, so it returns the generator object
3883 when the function is called.
Nick Coghlan650f0d02007-04-15 12:05:43 +00003884
3885 Possible cleanups:
3886 - iterate over the generator sequence instead of using recursion
3887*/
3888
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003889
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003890static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003891compiler_comprehension_generator(struct compiler *c,
3892 asdl_seq *generators, int gen_index,
3893 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003894{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003895 comprehension_ty gen;
3896 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
3897 if (gen->is_async) {
3898 return compiler_async_comprehension_generator(
3899 c, generators, gen_index, elt, val, type);
3900 } else {
3901 return compiler_sync_comprehension_generator(
3902 c, generators, gen_index, elt, val, type);
3903 }
3904}
3905
3906static int
3907compiler_sync_comprehension_generator(struct compiler *c,
3908 asdl_seq *generators, int gen_index,
3909 expr_ty elt, expr_ty val, int type)
3910{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003911 /* generate code for the iterator, then each of the ifs,
3912 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003914 comprehension_ty gen;
3915 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003916 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003918 start = compiler_new_block(c);
3919 skip = compiler_new_block(c);
3920 if_cleanup = compiler_new_block(c);
3921 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003923 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3924 anchor == NULL)
3925 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003927 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003929 if (gen_index == 0) {
3930 /* Receive outermost iter as an implicit argument */
3931 c->u->u_argcount = 1;
3932 ADDOP_I(c, LOAD_FAST, 0);
3933 }
3934 else {
3935 /* Sub-iter - calculate on the fly */
3936 VISIT(c, expr, gen->iter);
3937 ADDOP(c, GET_ITER);
3938 }
3939 compiler_use_next_block(c, start);
3940 ADDOP_JREL(c, FOR_ITER, anchor);
3941 NEXT_BLOCK(c);
3942 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003944 /* XXX this needs to be cleaned up...a lot! */
3945 n = asdl_seq_LEN(gen->ifs);
3946 for (i = 0; i < n; i++) {
3947 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003948 if (!compiler_jump_if(c, e, if_cleanup, 0))
3949 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003950 NEXT_BLOCK(c);
3951 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003953 if (++gen_index < asdl_seq_LEN(generators))
3954 if (!compiler_comprehension_generator(c,
3955 generators, gen_index,
3956 elt, val, type))
3957 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003959 /* only append after the last for generator */
3960 if (gen_index >= asdl_seq_LEN(generators)) {
3961 /* comprehension specific code */
3962 switch (type) {
3963 case COMP_GENEXP:
3964 VISIT(c, expr, elt);
3965 ADDOP(c, YIELD_VALUE);
3966 ADDOP(c, POP_TOP);
3967 break;
3968 case COMP_LISTCOMP:
3969 VISIT(c, expr, elt);
3970 ADDOP_I(c, LIST_APPEND, gen_index + 1);
3971 break;
3972 case COMP_SETCOMP:
3973 VISIT(c, expr, elt);
3974 ADDOP_I(c, SET_ADD, gen_index + 1);
3975 break;
3976 case COMP_DICTCOMP:
3977 /* With 'd[k] = v', v is evaluated before k, so we do
3978 the same. */
3979 VISIT(c, expr, val);
3980 VISIT(c, expr, elt);
3981 ADDOP_I(c, MAP_ADD, gen_index + 1);
3982 break;
3983 default:
3984 return 0;
3985 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003987 compiler_use_next_block(c, skip);
3988 }
3989 compiler_use_next_block(c, if_cleanup);
3990 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3991 compiler_use_next_block(c, anchor);
3992
3993 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003994}
3995
3996static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003997compiler_async_comprehension_generator(struct compiler *c,
3998 asdl_seq *generators, int gen_index,
3999 expr_ty elt, expr_ty val, int type)
4000{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004001 comprehension_ty gen;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004002 basicblock *start, *if_cleanup, *except;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004003 Py_ssize_t i, n;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004004 start = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004005 except = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004006 if_cleanup = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004007
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004008 if (start == NULL || if_cleanup == NULL || except == NULL) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004009 return 0;
4010 }
4011
4012 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4013
4014 if (gen_index == 0) {
4015 /* Receive outermost iter as an implicit argument */
4016 c->u->u_argcount = 1;
4017 ADDOP_I(c, LOAD_FAST, 0);
4018 }
4019 else {
4020 /* Sub-iter - calculate on the fly */
4021 VISIT(c, expr, gen->iter);
4022 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004023 }
4024
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004025 compiler_use_next_block(c, start);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004026
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004027 ADDOP_JREL(c, SETUP_FINALLY, except);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004028 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004029 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004030 ADDOP(c, YIELD_FROM);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004031 ADDOP(c, POP_BLOCK);
Serhiy Storchaka24d32012018-03-10 18:22:34 +02004032 VISIT(c, expr, gen->target);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004033
4034 n = asdl_seq_LEN(gen->ifs);
4035 for (i = 0; i < n; i++) {
4036 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004037 if (!compiler_jump_if(c, e, if_cleanup, 0))
4038 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004039 NEXT_BLOCK(c);
4040 }
4041
4042 if (++gen_index < asdl_seq_LEN(generators))
4043 if (!compiler_comprehension_generator(c,
4044 generators, gen_index,
4045 elt, val, type))
4046 return 0;
4047
4048 /* only append after the last for generator */
4049 if (gen_index >= asdl_seq_LEN(generators)) {
4050 /* comprehension specific code */
4051 switch (type) {
4052 case COMP_GENEXP:
4053 VISIT(c, expr, elt);
4054 ADDOP(c, YIELD_VALUE);
4055 ADDOP(c, POP_TOP);
4056 break;
4057 case COMP_LISTCOMP:
4058 VISIT(c, expr, elt);
4059 ADDOP_I(c, LIST_APPEND, gen_index + 1);
4060 break;
4061 case COMP_SETCOMP:
4062 VISIT(c, expr, elt);
4063 ADDOP_I(c, SET_ADD, gen_index + 1);
4064 break;
4065 case COMP_DICTCOMP:
4066 /* With 'd[k] = v', v is evaluated before k, so we do
4067 the same. */
4068 VISIT(c, expr, val);
4069 VISIT(c, expr, elt);
4070 ADDOP_I(c, MAP_ADD, gen_index + 1);
4071 break;
4072 default:
4073 return 0;
4074 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004075 }
4076 compiler_use_next_block(c, if_cleanup);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004077 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
4078
4079 compiler_use_next_block(c, except);
4080 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004081
4082 return 1;
4083}
4084
4085static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004086compiler_comprehension(struct compiler *c, expr_ty e, int type,
4087 identifier name, asdl_seq *generators, expr_ty elt,
4088 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004089{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004090 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004091 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004092 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004093 int is_async_function = c->u->u_ste->ste_coroutine;
4094 int is_async_generator = 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004095
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004096 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004097
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004098 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4099 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004100 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004101 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004102 }
4103
4104 is_async_generator = c->u->u_ste->ste_coroutine;
4105
Yury Selivanovb8ab9d32017-10-06 02:58:28 -04004106 if (is_async_generator && !is_async_function && type != COMP_GENEXP) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004107 if (e->lineno > c->u->u_lineno) {
4108 c->u->u_lineno = e->lineno;
4109 c->u->u_lineno_set = 0;
4110 }
4111 compiler_error(c, "asynchronous comprehension outside of "
4112 "an asynchronous function");
4113 goto error_in_scope;
4114 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004116 if (type != COMP_GENEXP) {
4117 int op;
4118 switch (type) {
4119 case COMP_LISTCOMP:
4120 op = BUILD_LIST;
4121 break;
4122 case COMP_SETCOMP:
4123 op = BUILD_SET;
4124 break;
4125 case COMP_DICTCOMP:
4126 op = BUILD_MAP;
4127 break;
4128 default:
4129 PyErr_Format(PyExc_SystemError,
4130 "unknown comprehension type %d", type);
4131 goto error_in_scope;
4132 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004134 ADDOP_I(c, op, 0);
4135 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004137 if (!compiler_comprehension_generator(c, generators, 0, elt,
4138 val, type))
4139 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004141 if (type != COMP_GENEXP) {
4142 ADDOP(c, RETURN_VALUE);
4143 }
4144
4145 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004146 qualname = c->u->u_qualname;
4147 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004148 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004149 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004150 goto error;
4151
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004152 if (!compiler_make_closure(c, co, 0, qualname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004153 goto error;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004154 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004155 Py_DECREF(co);
4156
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004157 VISIT(c, expr, outermost->iter);
4158
4159 if (outermost->is_async) {
4160 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004161 } else {
4162 ADDOP(c, GET_ITER);
4163 }
4164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004165 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004166
4167 if (is_async_generator && type != COMP_GENEXP) {
4168 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004169 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004170 ADDOP(c, YIELD_FROM);
4171 }
4172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004173 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004174error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004175 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004176error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004177 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004178 Py_XDECREF(co);
4179 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004180}
4181
4182static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004183compiler_genexp(struct compiler *c, expr_ty e)
4184{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004185 static identifier name;
4186 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004187 name = PyUnicode_InternFromString("<genexpr>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004188 if (!name)
4189 return 0;
4190 }
4191 assert(e->kind == GeneratorExp_kind);
4192 return compiler_comprehension(c, e, COMP_GENEXP, name,
4193 e->v.GeneratorExp.generators,
4194 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004195}
4196
4197static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004198compiler_listcomp(struct compiler *c, expr_ty e)
4199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200 static identifier name;
4201 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004202 name = PyUnicode_InternFromString("<listcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004203 if (!name)
4204 return 0;
4205 }
4206 assert(e->kind == ListComp_kind);
4207 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4208 e->v.ListComp.generators,
4209 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004210}
4211
4212static int
4213compiler_setcomp(struct compiler *c, expr_ty e)
4214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004215 static identifier name;
4216 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004217 name = PyUnicode_InternFromString("<setcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004218 if (!name)
4219 return 0;
4220 }
4221 assert(e->kind == SetComp_kind);
4222 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4223 e->v.SetComp.generators,
4224 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004225}
4226
4227
4228static int
4229compiler_dictcomp(struct compiler *c, expr_ty e)
4230{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004231 static identifier name;
4232 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004233 name = PyUnicode_InternFromString("<dictcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004234 if (!name)
4235 return 0;
4236 }
4237 assert(e->kind == DictComp_kind);
4238 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4239 e->v.DictComp.generators,
4240 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004241}
4242
4243
4244static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004245compiler_visit_keyword(struct compiler *c, keyword_ty k)
4246{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004247 VISIT(c, expr, k->value);
4248 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004249}
4250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004251/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004252 whether they are true or false.
4253
4254 Return values: 1 for true, 0 for false, -1 for non-constant.
4255 */
4256
4257static int
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02004258expr_constant(expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004259{
Serhiy Storchaka3325a672017-12-15 12:35:48 +02004260 if (is_const(e)) {
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02004261 return PyObject_IsTrue(get_const_value(e));
Benjamin Peterson442f2092012-12-06 17:41:04 -05004262 }
Serhiy Storchaka3325a672017-12-15 12:35:48 +02004263 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004264}
4265
Yury Selivanov75445082015-05-11 22:57:16 -04004266
4267/*
4268 Implements the async with statement.
4269
4270 The semantics outlined in that PEP are as follows:
4271
4272 async with EXPR as VAR:
4273 BLOCK
4274
4275 It is implemented roughly as:
4276
4277 context = EXPR
4278 exit = context.__aexit__ # not calling it
4279 value = await context.__aenter__()
4280 try:
4281 VAR = value # if VAR present in the syntax
4282 BLOCK
4283 finally:
4284 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004285 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04004286 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004287 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04004288 if not (await exit(*exc)):
4289 raise
4290 */
4291static int
4292compiler_async_with(struct compiler *c, stmt_ty s, int pos)
4293{
4294 basicblock *block, *finally;
4295 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
4296
4297 assert(s->kind == AsyncWith_kind);
Zsolt Dollensteine2396502018-04-27 08:58:56 -07004298 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
4299 return compiler_error(c, "'async with' outside async function");
4300 }
Yury Selivanov75445082015-05-11 22:57:16 -04004301
4302 block = compiler_new_block(c);
4303 finally = compiler_new_block(c);
4304 if (!block || !finally)
4305 return 0;
4306
4307 /* Evaluate EXPR */
4308 VISIT(c, expr, item->context_expr);
4309
4310 ADDOP(c, BEFORE_ASYNC_WITH);
4311 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004312 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004313 ADDOP(c, YIELD_FROM);
4314
4315 ADDOP_JREL(c, SETUP_ASYNC_WITH, finally);
4316
4317 /* SETUP_ASYNC_WITH pushes a finally block. */
4318 compiler_use_next_block(c, block);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004319 if (!compiler_push_fblock(c, ASYNC_WITH, block, finally)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004320 return 0;
4321 }
4322
4323 if (item->optional_vars) {
4324 VISIT(c, expr, item->optional_vars);
4325 }
4326 else {
4327 /* Discard result from context.__aenter__() */
4328 ADDOP(c, POP_TOP);
4329 }
4330
4331 pos++;
4332 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
4333 /* BLOCK code */
4334 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
4335 else if (!compiler_async_with(c, s, pos))
4336 return 0;
4337
4338 /* End of try block; start the finally block */
4339 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004340 ADDOP(c, BEGIN_FINALLY);
4341 compiler_pop_fblock(c, ASYNC_WITH, block);
Yury Selivanov75445082015-05-11 22:57:16 -04004342
Yury Selivanov75445082015-05-11 22:57:16 -04004343 compiler_use_next_block(c, finally);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004344 if (!compiler_push_fblock(c, FINALLY_END, finally, NULL))
Yury Selivanov75445082015-05-11 22:57:16 -04004345 return 0;
4346
4347 /* Finally block starts; context.__exit__ is on the stack under
4348 the exception or return information. Just issue our magic
4349 opcode. */
4350 ADDOP(c, WITH_CLEANUP_START);
4351
4352 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004353 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004354 ADDOP(c, YIELD_FROM);
4355
4356 ADDOP(c, WITH_CLEANUP_FINISH);
4357
4358 /* Finally block ends. */
4359 ADDOP(c, END_FINALLY);
4360 compiler_pop_fblock(c, FINALLY_END, finally);
4361 return 1;
4362}
4363
4364
Guido van Rossumc2e20742006-02-27 22:32:47 +00004365/*
4366 Implements the with statement from PEP 343.
4367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004368 The semantics outlined in that PEP are as follows:
Guido van Rossumc2e20742006-02-27 22:32:47 +00004369
4370 with EXPR as VAR:
4371 BLOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004372
Guido van Rossumc2e20742006-02-27 22:32:47 +00004373 It is implemented roughly as:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374
Thomas Wouters477c8d52006-05-27 19:21:47 +00004375 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00004376 exit = context.__exit__ # not calling it
4377 value = context.__enter__()
4378 try:
4379 VAR = value # if VAR present in the syntax
4380 BLOCK
4381 finally:
4382 if an exception was raised:
Serhiy Storchakad741a882015-06-11 00:06:39 +03004383 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004384 else:
Serhiy Storchakad741a882015-06-11 00:06:39 +03004385 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004386 exit(*exc)
4387 */
4388static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004389compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004390{
Guido van Rossumc2e20742006-02-27 22:32:47 +00004391 basicblock *block, *finally;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004392 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004393
4394 assert(s->kind == With_kind);
4395
Guido van Rossumc2e20742006-02-27 22:32:47 +00004396 block = compiler_new_block(c);
4397 finally = compiler_new_block(c);
4398 if (!block || !finally)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004399 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004400
Thomas Wouters477c8d52006-05-27 19:21:47 +00004401 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004402 VISIT(c, expr, item->context_expr);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004403 ADDOP_JREL(c, SETUP_WITH, finally);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004404
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004405 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00004406 compiler_use_next_block(c, block);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004407 if (!compiler_push_fblock(c, WITH, block, finally)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004408 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004409 }
4410
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004411 if (item->optional_vars) {
4412 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004413 }
4414 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004415 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004416 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004417 }
4418
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004419 pos++;
4420 if (pos == asdl_seq_LEN(s->v.With.items))
4421 /* BLOCK code */
4422 VISIT_SEQ(c, stmt, s->v.With.body)
4423 else if (!compiler_with(c, s, pos))
4424 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004425
4426 /* End of try block; start the finally block */
4427 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004428 ADDOP(c, BEGIN_FINALLY);
4429 compiler_pop_fblock(c, WITH, block);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004430
Guido van Rossumc2e20742006-02-27 22:32:47 +00004431 compiler_use_next_block(c, finally);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004432 if (!compiler_push_fblock(c, FINALLY_END, finally, NULL))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004433 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004434
Christian Heimesdd15f6c2008-03-16 00:07:10 +00004435 /* Finally block starts; context.__exit__ is on the stack under
4436 the exception or return information. Just issue our magic
4437 opcode. */
Yury Selivanov75445082015-05-11 22:57:16 -04004438 ADDOP(c, WITH_CLEANUP_START);
4439 ADDOP(c, WITH_CLEANUP_FINISH);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004440
4441 /* Finally block ends. */
4442 ADDOP(c, END_FINALLY);
4443 compiler_pop_fblock(c, FINALLY_END, finally);
4444 return 1;
4445}
4446
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004447static int
4448compiler_visit_expr(struct compiler *c, expr_ty e)
4449{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004450 /* If expr e has a different line number than the last expr/stmt,
4451 set a new line number for the next instruction.
4452 */
4453 if (e->lineno > c->u->u_lineno) {
4454 c->u->u_lineno = e->lineno;
4455 c->u->u_lineno_set = 0;
4456 }
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00004457 /* Updating the column offset is always harmless. */
4458 c->u->u_col_offset = e->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004459 switch (e->kind) {
4460 case BoolOp_kind:
4461 return compiler_boolop(c, e);
4462 case BinOp_kind:
4463 VISIT(c, expr, e->v.BinOp.left);
4464 VISIT(c, expr, e->v.BinOp.right);
4465 ADDOP(c, binop(c, e->v.BinOp.op));
4466 break;
4467 case UnaryOp_kind:
4468 VISIT(c, expr, e->v.UnaryOp.operand);
4469 ADDOP(c, unaryop(e->v.UnaryOp.op));
4470 break;
4471 case Lambda_kind:
4472 return compiler_lambda(c, e);
4473 case IfExp_kind:
4474 return compiler_ifexp(c, e);
4475 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004476 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004477 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004478 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004479 case GeneratorExp_kind:
4480 return compiler_genexp(c, e);
4481 case ListComp_kind:
4482 return compiler_listcomp(c, e);
4483 case SetComp_kind:
4484 return compiler_setcomp(c, e);
4485 case DictComp_kind:
4486 return compiler_dictcomp(c, e);
4487 case Yield_kind:
4488 if (c->u->u_ste->ste_type != FunctionBlock)
4489 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00004490 if (e->v.Yield.value) {
4491 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004492 }
4493 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004494 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004495 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00004496 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004497 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00004498 case YieldFrom_kind:
4499 if (c->u->u_ste->ste_type != FunctionBlock)
4500 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04004501
4502 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
4503 return compiler_error(c, "'yield from' inside async function");
4504
Mark Dickinsonded35ae2012-11-25 14:36:26 +00004505 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004506 ADDOP(c, GET_YIELD_FROM_ITER);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004507 ADDOP_LOAD_CONST(c, Py_None);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00004508 ADDOP(c, YIELD_FROM);
4509 break;
Yury Selivanov75445082015-05-11 22:57:16 -04004510 case Await_kind:
4511 if (c->u->u_ste->ste_type != FunctionBlock)
4512 return compiler_error(c, "'await' outside function");
4513
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004514 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
4515 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION)
Yury Selivanov75445082015-05-11 22:57:16 -04004516 return compiler_error(c, "'await' outside async function");
4517
4518 VISIT(c, expr, e->v.Await.value);
4519 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004520 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004521 ADDOP(c, YIELD_FROM);
4522 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004523 case Compare_kind:
4524 return compiler_compare(c, e);
4525 case Call_kind:
4526 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01004527 case Constant_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004528 ADDOP_LOAD_CONST(c, e->v.Constant.value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01004529 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004530 case Num_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004531 ADDOP_LOAD_CONST(c, e->v.Num.n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004532 break;
4533 case Str_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004534 ADDOP_LOAD_CONST(c, e->v.Str.s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004535 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004536 case JoinedStr_kind:
4537 return compiler_joined_str(c, e);
4538 case FormattedValue_kind:
4539 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004540 case Bytes_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004541 ADDOP_LOAD_CONST(c, e->v.Bytes.s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004542 break;
4543 case Ellipsis_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004544 ADDOP_LOAD_CONST(c, Py_Ellipsis);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004545 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05004546 case NameConstant_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004547 ADDOP_LOAD_CONST(c, e->v.NameConstant.value);
Benjamin Peterson442f2092012-12-06 17:41:04 -05004548 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004549 /* The following exprs can be assignment targets. */
4550 case Attribute_kind:
4551 if (e->v.Attribute.ctx != AugStore)
4552 VISIT(c, expr, e->v.Attribute.value);
4553 switch (e->v.Attribute.ctx) {
4554 case AugLoad:
4555 ADDOP(c, DUP_TOP);
Stefan Krahf432a322017-08-21 13:09:59 +02004556 /* Fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004557 case Load:
4558 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
4559 break;
4560 case AugStore:
4561 ADDOP(c, ROT_TWO);
Stefan Krahf432a322017-08-21 13:09:59 +02004562 /* Fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004563 case Store:
4564 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
4565 break;
4566 case Del:
4567 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
4568 break;
4569 case Param:
4570 default:
4571 PyErr_SetString(PyExc_SystemError,
4572 "param invalid in attribute expression");
4573 return 0;
4574 }
4575 break;
4576 case Subscript_kind:
4577 switch (e->v.Subscript.ctx) {
4578 case AugLoad:
4579 VISIT(c, expr, e->v.Subscript.value);
4580 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
4581 break;
4582 case Load:
4583 VISIT(c, expr, e->v.Subscript.value);
4584 VISIT_SLICE(c, e->v.Subscript.slice, Load);
4585 break;
4586 case AugStore:
4587 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
4588 break;
4589 case Store:
4590 VISIT(c, expr, e->v.Subscript.value);
4591 VISIT_SLICE(c, e->v.Subscript.slice, Store);
4592 break;
4593 case Del:
4594 VISIT(c, expr, e->v.Subscript.value);
4595 VISIT_SLICE(c, e->v.Subscript.slice, Del);
4596 break;
4597 case Param:
4598 default:
4599 PyErr_SetString(PyExc_SystemError,
4600 "param invalid in subscript expression");
4601 return 0;
4602 }
4603 break;
4604 case Starred_kind:
4605 switch (e->v.Starred.ctx) {
4606 case Store:
4607 /* In all legitimate cases, the Starred node was already replaced
4608 * by compiler_list/compiler_tuple. XXX: is that okay? */
4609 return compiler_error(c,
4610 "starred assignment target must be in a list or tuple");
4611 default:
4612 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004613 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004614 }
4615 break;
4616 case Name_kind:
4617 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
4618 /* child nodes of List and Tuple will have expr_context set */
4619 case List_kind:
4620 return compiler_list(c, e);
4621 case Tuple_kind:
4622 return compiler_tuple(c, e);
4623 }
4624 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004625}
4626
4627static int
4628compiler_augassign(struct compiler *c, stmt_ty s)
4629{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004630 expr_ty e = s->v.AugAssign.target;
4631 expr_ty auge;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004633 assert(s->kind == AugAssign_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004635 switch (e->kind) {
4636 case Attribute_kind:
4637 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
4638 AugLoad, e->lineno, e->col_offset, c->c_arena);
4639 if (auge == NULL)
4640 return 0;
4641 VISIT(c, expr, auge);
4642 VISIT(c, expr, s->v.AugAssign.value);
4643 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
4644 auge->v.Attribute.ctx = AugStore;
4645 VISIT(c, expr, auge);
4646 break;
4647 case Subscript_kind:
4648 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
4649 AugLoad, e->lineno, e->col_offset, c->c_arena);
4650 if (auge == NULL)
4651 return 0;
4652 VISIT(c, expr, auge);
4653 VISIT(c, expr, s->v.AugAssign.value);
4654 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
4655 auge->v.Subscript.ctx = AugStore;
4656 VISIT(c, expr, auge);
4657 break;
4658 case Name_kind:
4659 if (!compiler_nameop(c, e->v.Name.id, Load))
4660 return 0;
4661 VISIT(c, expr, s->v.AugAssign.value);
4662 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
4663 return compiler_nameop(c, e->v.Name.id, Store);
4664 default:
4665 PyErr_Format(PyExc_SystemError,
4666 "invalid node type (%d) for augmented assignment",
4667 e->kind);
4668 return 0;
4669 }
4670 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004671}
4672
4673static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07004674check_ann_expr(struct compiler *c, expr_ty e)
4675{
4676 VISIT(c, expr, e);
4677 ADDOP(c, POP_TOP);
4678 return 1;
4679}
4680
4681static int
4682check_annotation(struct compiler *c, stmt_ty s)
4683{
4684 /* Annotations are only evaluated in a module or class. */
4685 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
4686 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
4687 return check_ann_expr(c, s->v.AnnAssign.annotation);
4688 }
4689 return 1;
4690}
4691
4692static int
4693check_ann_slice(struct compiler *c, slice_ty sl)
4694{
4695 switch(sl->kind) {
4696 case Index_kind:
4697 return check_ann_expr(c, sl->v.Index.value);
4698 case Slice_kind:
4699 if (sl->v.Slice.lower && !check_ann_expr(c, sl->v.Slice.lower)) {
4700 return 0;
4701 }
4702 if (sl->v.Slice.upper && !check_ann_expr(c, sl->v.Slice.upper)) {
4703 return 0;
4704 }
4705 if (sl->v.Slice.step && !check_ann_expr(c, sl->v.Slice.step)) {
4706 return 0;
4707 }
4708 break;
4709 default:
4710 PyErr_SetString(PyExc_SystemError,
4711 "unexpected slice kind");
4712 return 0;
4713 }
4714 return 1;
4715}
4716
4717static int
4718check_ann_subscr(struct compiler *c, slice_ty sl)
4719{
4720 /* We check that everything in a subscript is defined at runtime. */
4721 Py_ssize_t i, n;
4722
4723 switch (sl->kind) {
4724 case Index_kind:
4725 case Slice_kind:
4726 if (!check_ann_slice(c, sl)) {
4727 return 0;
4728 }
4729 break;
4730 case ExtSlice_kind:
4731 n = asdl_seq_LEN(sl->v.ExtSlice.dims);
4732 for (i = 0; i < n; i++) {
4733 slice_ty subsl = (slice_ty)asdl_seq_GET(sl->v.ExtSlice.dims, i);
4734 switch (subsl->kind) {
4735 case Index_kind:
4736 case Slice_kind:
4737 if (!check_ann_slice(c, subsl)) {
4738 return 0;
4739 }
4740 break;
4741 case ExtSlice_kind:
4742 default:
4743 PyErr_SetString(PyExc_SystemError,
4744 "extended slice invalid in nested slice");
4745 return 0;
4746 }
4747 }
4748 break;
4749 default:
4750 PyErr_Format(PyExc_SystemError,
4751 "invalid subscript kind %d", sl->kind);
4752 return 0;
4753 }
4754 return 1;
4755}
4756
4757static int
4758compiler_annassign(struct compiler *c, stmt_ty s)
4759{
4760 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07004761 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07004762
4763 assert(s->kind == AnnAssign_kind);
4764
4765 /* We perform the actual assignment first. */
4766 if (s->v.AnnAssign.value) {
4767 VISIT(c, expr, s->v.AnnAssign.value);
4768 VISIT(c, expr, targ);
4769 }
4770 switch (targ->kind) {
4771 case Name_kind:
4772 /* If we have a simple name in a module or class, store annotation. */
4773 if (s->v.AnnAssign.simple &&
4774 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
4775 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Guido van Rossum95e4d582018-01-26 08:20:18 -08004776 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
4777 VISIT(c, annexpr, s->v.AnnAssign.annotation)
4778 }
4779 else {
4780 VISIT(c, expr, s->v.AnnAssign.annotation);
4781 }
Mark Shannon332cd5e2018-01-30 00:41:04 +00004782 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02004783 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004784 ADDOP_LOAD_CONST_NEW(c, mangled);
Mark Shannon332cd5e2018-01-30 00:41:04 +00004785 ADDOP(c, STORE_SUBSCR);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07004786 }
4787 break;
4788 case Attribute_kind:
4789 if (!s->v.AnnAssign.value &&
4790 !check_ann_expr(c, targ->v.Attribute.value)) {
4791 return 0;
4792 }
4793 break;
4794 case Subscript_kind:
4795 if (!s->v.AnnAssign.value &&
4796 (!check_ann_expr(c, targ->v.Subscript.value) ||
4797 !check_ann_subscr(c, targ->v.Subscript.slice))) {
4798 return 0;
4799 }
4800 break;
4801 default:
4802 PyErr_Format(PyExc_SystemError,
4803 "invalid node type (%d) for annotated assignment",
4804 targ->kind);
4805 return 0;
4806 }
4807 /* Annotation is evaluated last. */
4808 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
4809 return 0;
4810 }
4811 return 1;
4812}
4813
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004814/* Raises a SyntaxError and returns 0.
4815 If something goes wrong, a different exception may be raised.
4816*/
4817
4818static int
4819compiler_error(struct compiler *c, const char *errstr)
4820{
Benjamin Peterson43b06862011-05-27 09:08:01 -05004821 PyObject *loc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004822 PyObject *u = NULL, *v = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004823
Victor Stinner14e461d2013-08-26 22:28:21 +02004824 loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004825 if (!loc) {
4826 Py_INCREF(Py_None);
4827 loc = Py_None;
4828 }
Victor Stinner14e461d2013-08-26 22:28:21 +02004829 u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00004830 c->u->u_col_offset, loc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004831 if (!u)
4832 goto exit;
4833 v = Py_BuildValue("(zO)", errstr, u);
4834 if (!v)
4835 goto exit;
4836 PyErr_SetObject(PyExc_SyntaxError, v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004837 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004838 Py_DECREF(loc);
4839 Py_XDECREF(u);
4840 Py_XDECREF(v);
4841 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004842}
4843
4844static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004845compiler_handle_subscr(struct compiler *c, const char *kind,
4846 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004847{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004848 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004850 /* XXX this code is duplicated */
4851 switch (ctx) {
4852 case AugLoad: /* fall through to Load */
4853 case Load: op = BINARY_SUBSCR; break;
4854 case AugStore:/* fall through to Store */
4855 case Store: op = STORE_SUBSCR; break;
4856 case Del: op = DELETE_SUBSCR; break;
4857 case Param:
4858 PyErr_Format(PyExc_SystemError,
4859 "invalid %s kind %d in subscript\n",
4860 kind, ctx);
4861 return 0;
4862 }
4863 if (ctx == AugLoad) {
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00004864 ADDOP(c, DUP_TOP_TWO);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004865 }
4866 else if (ctx == AugStore) {
4867 ADDOP(c, ROT_THREE);
4868 }
4869 ADDOP(c, op);
4870 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004871}
4872
4873static int
4874compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
4875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004876 int n = 2;
4877 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004879 /* only handles the cases where BUILD_SLICE is emitted */
4880 if (s->v.Slice.lower) {
4881 VISIT(c, expr, s->v.Slice.lower);
4882 }
4883 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004884 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004885 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004887 if (s->v.Slice.upper) {
4888 VISIT(c, expr, s->v.Slice.upper);
4889 }
4890 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004891 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004892 }
4893
4894 if (s->v.Slice.step) {
4895 n++;
4896 VISIT(c, expr, s->v.Slice.step);
4897 }
4898 ADDOP_I(c, BUILD_SLICE, n);
4899 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004900}
4901
4902static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004903compiler_visit_nested_slice(struct compiler *c, slice_ty s,
4904 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004905{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004906 switch (s->kind) {
4907 case Slice_kind:
4908 return compiler_slice(c, s, ctx);
4909 case Index_kind:
4910 VISIT(c, expr, s->v.Index.value);
4911 break;
4912 case ExtSlice_kind:
4913 default:
4914 PyErr_SetString(PyExc_SystemError,
4915 "extended slice invalid in nested slice");
4916 return 0;
4917 }
4918 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004919}
4920
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004921static int
4922compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
4923{
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02004924 const char * kindname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004925 switch (s->kind) {
4926 case Index_kind:
4927 kindname = "index";
4928 if (ctx != AugStore) {
4929 VISIT(c, expr, s->v.Index.value);
4930 }
4931 break;
4932 case Slice_kind:
4933 kindname = "slice";
4934 if (ctx != AugStore) {
4935 if (!compiler_slice(c, s, ctx))
4936 return 0;
4937 }
4938 break;
4939 case ExtSlice_kind:
4940 kindname = "extended slice";
4941 if (ctx != AugStore) {
Victor Stinnerad9a0662013-11-19 22:23:20 +01004942 Py_ssize_t i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004943 for (i = 0; i < n; i++) {
4944 slice_ty sub = (slice_ty)asdl_seq_GET(
4945 s->v.ExtSlice.dims, i);
4946 if (!compiler_visit_nested_slice(c, sub, ctx))
4947 return 0;
4948 }
4949 ADDOP_I(c, BUILD_TUPLE, n);
4950 }
4951 break;
4952 default:
4953 PyErr_Format(PyExc_SystemError,
4954 "invalid subscript kind %d", s->kind);
4955 return 0;
4956 }
4957 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004958}
4959
Thomas Wouters89f507f2006-12-13 04:49:30 +00004960/* End of the compiler section, beginning of the assembler section */
4961
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004962/* do depth-first search of basic block graph, starting with block.
4963 post records the block indices in post-order.
4964
4965 XXX must handle implicit jumps from one block to next
4966*/
4967
Thomas Wouters89f507f2006-12-13 04:49:30 +00004968struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004969 PyObject *a_bytecode; /* string containing bytecode */
4970 int a_offset; /* offset into bytecode */
4971 int a_nblocks; /* number of reachable blocks */
4972 basicblock **a_postorder; /* list of blocks in dfs postorder */
4973 PyObject *a_lnotab; /* string containing lnotab */
4974 int a_lnotab_off; /* offset into lnotab */
4975 int a_lineno; /* last lineno of emitted instruction */
4976 int a_lineno_off; /* bytecode offset of last lineno */
Thomas Wouters89f507f2006-12-13 04:49:30 +00004977};
4978
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004979static void
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02004980dfs(struct compiler *c, basicblock *b, struct assembler *a, int end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004981{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02004982 int i, j;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004983
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02004984 /* Get rid of recursion for normal control flow.
4985 Since the number of blocks is limited, unused space in a_postorder
4986 (from a_nblocks to end) can be used as a stack for still not ordered
4987 blocks. */
4988 for (j = end; b && !b->b_seen; b = b->b_next) {
4989 b->b_seen = 1;
4990 assert(a->a_nblocks < j);
4991 a->a_postorder[--j] = b;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004992 }
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02004993 while (j < end) {
4994 b = a->a_postorder[j++];
4995 for (i = 0; i < b->b_iused; i++) {
4996 struct instr *instr = &b->b_instr[i];
4997 if (instr->i_jrel || instr->i_jabs)
4998 dfs(c, instr->i_target, a, j);
4999 }
5000 assert(a->a_nblocks < j);
5001 a->a_postorder[a->a_nblocks++] = b;
5002 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005003}
5004
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005005Py_LOCAL_INLINE(void)
5006stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005007{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005008 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005009 if (b->b_startdepth < depth) {
5010 assert(b->b_startdepth < 0);
5011 b->b_startdepth = depth;
5012 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02005013 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005014}
5015
5016/* Find the flow path that needs the largest stack. We assume that
5017 * cycles in the flow graph have no net effect on the stack depth.
5018 */
5019static int
5020stackdepth(struct compiler *c)
5021{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005022 basicblock *b, *entryblock = NULL;
5023 basicblock **stack, **sp;
5024 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005025 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005026 b->b_startdepth = INT_MIN;
5027 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005028 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005029 }
5030 if (!entryblock)
5031 return 0;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005032 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
5033 if (!stack) {
5034 PyErr_NoMemory();
5035 return -1;
5036 }
5037
5038 sp = stack;
5039 stackdepth_push(&sp, entryblock, 0);
5040 while (sp != stack) {
5041 b = *--sp;
5042 int depth = b->b_startdepth;
5043 assert(depth >= 0);
5044 basicblock *next = b->b_next;
5045 for (int i = 0; i < b->b_iused; i++) {
5046 struct instr *instr = &b->b_instr[i];
5047 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
5048 if (effect == PY_INVALID_STACK_EFFECT) {
5049 fprintf(stderr, "opcode = %d\n", instr->i_opcode);
5050 Py_FatalError("PyCompile_OpcodeStackEffect()");
5051 }
5052 int new_depth = depth + effect;
5053 if (new_depth > maxdepth) {
5054 maxdepth = new_depth;
5055 }
5056 assert(depth >= 0); /* invalid code or bug in stackdepth() */
5057 if (instr->i_jrel || instr->i_jabs) {
5058 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
5059 assert(effect != PY_INVALID_STACK_EFFECT);
5060 int target_depth = depth + effect;
5061 if (target_depth > maxdepth) {
5062 maxdepth = target_depth;
5063 }
5064 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005065 if (instr->i_opcode == CALL_FINALLY) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005066 assert(instr->i_target->b_startdepth >= 0);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005067 assert(instr->i_target->b_startdepth >= target_depth);
5068 depth = new_depth;
5069 continue;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005070 }
5071 stackdepth_push(&sp, instr->i_target, target_depth);
5072 }
5073 depth = new_depth;
5074 if (instr->i_opcode == JUMP_ABSOLUTE ||
5075 instr->i_opcode == JUMP_FORWARD ||
5076 instr->i_opcode == RETURN_VALUE ||
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005077 instr->i_opcode == RAISE_VARARGS)
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005078 {
5079 /* remaining code is dead */
5080 next = NULL;
5081 break;
5082 }
5083 }
5084 if (next != NULL) {
5085 stackdepth_push(&sp, next, depth);
5086 }
5087 }
5088 PyObject_Free(stack);
5089 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005090}
5091
5092static int
5093assemble_init(struct assembler *a, int nblocks, int firstlineno)
5094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005095 memset(a, 0, sizeof(struct assembler));
5096 a->a_lineno = firstlineno;
5097 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
5098 if (!a->a_bytecode)
5099 return 0;
5100 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
5101 if (!a->a_lnotab)
5102 return 0;
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07005103 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005104 PyErr_NoMemory();
5105 return 0;
5106 }
5107 a->a_postorder = (basicblock **)PyObject_Malloc(
5108 sizeof(basicblock *) * nblocks);
5109 if (!a->a_postorder) {
5110 PyErr_NoMemory();
5111 return 0;
5112 }
5113 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005114}
5115
5116static void
5117assemble_free(struct assembler *a)
5118{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005119 Py_XDECREF(a->a_bytecode);
5120 Py_XDECREF(a->a_lnotab);
5121 if (a->a_postorder)
5122 PyObject_Free(a->a_postorder);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005123}
5124
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005125static int
5126blocksize(basicblock *b)
5127{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005128 int i;
5129 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005131 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005132 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005133 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005134}
5135
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00005136/* Appends a pair to the end of the line number table, a_lnotab, representing
5137 the instruction's bytecode offset and line number. See
5138 Objects/lnotab_notes.txt for the description of the line number table. */
Tim Peters2a7f3842001-06-09 09:26:21 +00005139
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005140static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005141assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005142{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005143 int d_bytecode, d_lineno;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005144 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005145 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005146
Serhiy Storchakaab874002016-09-11 13:48:15 +03005147 d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005148 d_lineno = i->i_lineno - a->a_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005150 assert(d_bytecode >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005152 if(d_bytecode == 0 && d_lineno == 0)
5153 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00005154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005155 if (d_bytecode > 255) {
5156 int j, nbytes, ncodes = d_bytecode / 255;
5157 nbytes = a->a_lnotab_off + 2 * ncodes;
5158 len = PyBytes_GET_SIZE(a->a_lnotab);
5159 if (nbytes >= len) {
5160 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
5161 len = nbytes;
5162 else if (len <= INT_MAX / 2)
5163 len *= 2;
5164 else {
5165 PyErr_NoMemory();
5166 return 0;
5167 }
5168 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
5169 return 0;
5170 }
5171 lnotab = (unsigned char *)
5172 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
5173 for (j = 0; j < ncodes; j++) {
5174 *lnotab++ = 255;
5175 *lnotab++ = 0;
5176 }
5177 d_bytecode -= ncodes * 255;
5178 a->a_lnotab_off += ncodes * 2;
5179 }
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005180 assert(0 <= d_bytecode && d_bytecode <= 255);
5181
5182 if (d_lineno < -128 || 127 < d_lineno) {
5183 int j, nbytes, ncodes, k;
5184 if (d_lineno < 0) {
5185 k = -128;
5186 /* use division on positive numbers */
5187 ncodes = (-d_lineno) / 128;
5188 }
5189 else {
5190 k = 127;
5191 ncodes = d_lineno / 127;
5192 }
5193 d_lineno -= ncodes * k;
5194 assert(ncodes >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005195 nbytes = a->a_lnotab_off + 2 * ncodes;
5196 len = PyBytes_GET_SIZE(a->a_lnotab);
5197 if (nbytes >= len) {
5198 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
5199 len = nbytes;
5200 else if (len <= INT_MAX / 2)
5201 len *= 2;
5202 else {
5203 PyErr_NoMemory();
5204 return 0;
5205 }
5206 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
5207 return 0;
5208 }
5209 lnotab = (unsigned char *)
5210 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
5211 *lnotab++ = d_bytecode;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005212 *lnotab++ = k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005213 d_bytecode = 0;
5214 for (j = 1; j < ncodes; j++) {
5215 *lnotab++ = 0;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005216 *lnotab++ = k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005217 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005218 a->a_lnotab_off += ncodes * 2;
5219 }
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005220 assert(-128 <= d_lineno && d_lineno <= 127);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005222 len = PyBytes_GET_SIZE(a->a_lnotab);
5223 if (a->a_lnotab_off + 2 >= len) {
5224 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
5225 return 0;
5226 }
5227 lnotab = (unsigned char *)
5228 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00005229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005230 a->a_lnotab_off += 2;
5231 if (d_bytecode) {
5232 *lnotab++ = d_bytecode;
Victor Stinner4f2dab52011-05-27 16:46:51 +02005233 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005234 }
5235 else { /* First line of a block; def stmt, etc. */
5236 *lnotab++ = 0;
Victor Stinner4f2dab52011-05-27 16:46:51 +02005237 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005238 }
5239 a->a_lineno = i->i_lineno;
5240 a->a_lineno_off = a->a_offset;
5241 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005242}
5243
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005244/* assemble_emit()
5245 Extend the bytecode with a new instruction.
5246 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00005247*/
5248
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00005249static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005250assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00005251{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005252 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005253 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03005254 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005255
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005256 arg = i->i_oparg;
5257 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005258 if (i->i_lineno && !assemble_lnotab(a, i))
5259 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005260 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005261 if (len > PY_SSIZE_T_MAX / 2)
5262 return 0;
5263 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
5264 return 0;
5265 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03005266 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005267 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005268 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005269 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00005270}
5271
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00005272static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005273assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00005274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005275 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005276 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005277 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00005278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005279 /* Compute the size of each block and fixup jump args.
5280 Replace block pointer with position in bytecode. */
5281 do {
5282 totsize = 0;
5283 for (i = a->a_nblocks - 1; i >= 0; i--) {
5284 b = a->a_postorder[i];
5285 bsize = blocksize(b);
5286 b->b_offset = totsize;
5287 totsize += bsize;
5288 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005289 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005290 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
5291 bsize = b->b_offset;
5292 for (i = 0; i < b->b_iused; i++) {
5293 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005294 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005295 /* Relative jumps are computed relative to
5296 the instruction pointer after fetching
5297 the jump instruction.
5298 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005299 bsize += isize;
5300 if (instr->i_jabs || instr->i_jrel) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005301 instr->i_oparg = instr->i_target->b_offset;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005302 if (instr->i_jrel) {
5303 instr->i_oparg -= bsize;
5304 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03005305 instr->i_oparg *= sizeof(_Py_CODEUNIT);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005306 if (instrsize(instr->i_oparg) != isize) {
5307 extended_arg_recompile = 1;
5308 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005309 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005310 }
5311 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00005312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005313 /* XXX: This is an awful hack that could hurt performance, but
5314 on the bright side it should work until we come up
5315 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00005316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005317 The issue is that in the first loop blocksize() is called
5318 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005319 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005320 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00005321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005322 So we loop until we stop seeing new EXTENDED_ARGs.
5323 The only EXTENDED_ARGs that could be popping up are
5324 ones in jump instructions. So this should converge
5325 fairly quickly.
5326 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005327 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005328}
5329
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005330static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01005331dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005333 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02005334 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005336 tuple = PyTuple_New(size);
5337 if (tuple == NULL)
5338 return NULL;
5339 while (PyDict_Next(dict, &pos, &k, &v)) {
5340 i = PyLong_AS_LONG(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005341 Py_INCREF(k);
5342 assert((i - offset) < size);
5343 assert((i - offset) >= 0);
5344 PyTuple_SET_ITEM(tuple, i - offset, k);
5345 }
5346 return tuple;
5347}
5348
5349static PyObject *
5350consts_dict_keys_inorder(PyObject *dict)
5351{
5352 PyObject *consts, *k, *v;
5353 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
5354
5355 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
5356 if (consts == NULL)
5357 return NULL;
5358 while (PyDict_Next(dict, &pos, &k, &v)) {
5359 i = PyLong_AS_LONG(v);
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +03005360 /* The keys of the dictionary can be tuples wrapping a contant.
5361 * (see compiler_add_o and _PyCode_ConstantKey). In that case
5362 * the object we want is always second. */
5363 if (PyTuple_CheckExact(k)) {
5364 k = PyTuple_GET_ITEM(k, 1);
5365 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005366 Py_INCREF(k);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005367 assert(i < size);
5368 assert(i >= 0);
5369 PyList_SET_ITEM(consts, i, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005370 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005371 return consts;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005372}
5373
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005374static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005375compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005377 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005378 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005379 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04005380 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005381 if (ste->ste_nested)
5382 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07005383 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005384 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07005385 if (!ste->ste_generator && ste->ste_coroutine)
5386 flags |= CO_COROUTINE;
5387 if (ste->ste_generator && ste->ste_coroutine)
5388 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005389 if (ste->ste_varargs)
5390 flags |= CO_VARARGS;
5391 if (ste->ste_varkeywords)
5392 flags |= CO_VARKEYWORDS;
5393 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00005394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005395 /* (Only) inherit compilerflags in PyCF_MASK */
5396 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00005397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005398 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005399}
5400
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005401static PyCodeObject *
5402makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005404 PyObject *tmp;
5405 PyCodeObject *co = NULL;
5406 PyObject *consts = NULL;
5407 PyObject *names = NULL;
5408 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005409 PyObject *name = NULL;
5410 PyObject *freevars = NULL;
5411 PyObject *cellvars = NULL;
5412 PyObject *bytecode = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005413 Py_ssize_t nlocals;
5414 int nlocals_int;
5415 int flags;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005416 int argcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005417
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005418 consts = consts_dict_keys_inorder(c->u->u_consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005419 names = dict_keys_inorder(c->u->u_names, 0);
5420 varnames = dict_keys_inorder(c->u->u_varnames, 0);
5421 if (!consts || !names || !varnames)
5422 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005424 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
5425 if (!cellvars)
5426 goto error;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005427 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005428 if (!freevars)
5429 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005430
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02005431 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01005432 assert(nlocals < INT_MAX);
5433 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
5434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005435 flags = compute_code_flags(c);
5436 if (flags < 0)
5437 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005439 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
5440 if (!bytecode)
5441 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005443 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
5444 if (!tmp)
5445 goto error;
5446 Py_DECREF(consts);
5447 consts = tmp;
5448
Victor Stinnerf8e32212013-11-19 23:56:34 +01005449 argcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
5450 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005451 maxdepth = stackdepth(c);
5452 if (maxdepth < 0) {
5453 goto error;
5454 }
Victor Stinnerf8e32212013-11-19 23:56:34 +01005455 co = PyCode_New(argcount, kwonlyargcount,
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005456 nlocals_int, maxdepth, flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005457 bytecode, consts, names, varnames,
5458 freevars, cellvars,
Victor Stinner14e461d2013-08-26 22:28:21 +02005459 c->c_filename, c->u->u_name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005460 c->u->u_firstlineno,
5461 a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005462 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005463 Py_XDECREF(consts);
5464 Py_XDECREF(names);
5465 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005466 Py_XDECREF(name);
5467 Py_XDECREF(freevars);
5468 Py_XDECREF(cellvars);
5469 Py_XDECREF(bytecode);
5470 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005471}
5472
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005473
5474/* For debugging purposes only */
5475#if 0
5476static void
5477dump_instr(const struct instr *i)
5478{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005479 const char *jrel = i->i_jrel ? "jrel " : "";
5480 const char *jabs = i->i_jabs ? "jabs " : "";
5481 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005483 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005484 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005485 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005486 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005487 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
5488 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005489}
5490
5491static void
5492dump_basicblock(const basicblock *b)
5493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005494 const char *seen = b->b_seen ? "seen " : "";
5495 const char *b_return = b->b_return ? "return " : "";
5496 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
5497 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
5498 if (b->b_instr) {
5499 int i;
5500 for (i = 0; i < b->b_iused; i++) {
5501 fprintf(stderr, " [%02d] ", i);
5502 dump_instr(b->b_instr + i);
5503 }
5504 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005505}
5506#endif
5507
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005508static PyCodeObject *
5509assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005511 basicblock *b, *entryblock;
5512 struct assembler a;
5513 int i, j, nblocks;
5514 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005516 /* Make sure every block that falls off the end returns None.
5517 XXX NEXT_BLOCK() isn't quite right, because if the last
5518 block ends with a jump or return b_next shouldn't set.
5519 */
5520 if (!c->u->u_curblock->b_return) {
5521 NEXT_BLOCK(c);
5522 if (addNone)
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005523 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005524 ADDOP(c, RETURN_VALUE);
5525 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005527 nblocks = 0;
5528 entryblock = NULL;
5529 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
5530 nblocks++;
5531 entryblock = b;
5532 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005534 /* Set firstlineno if it wasn't explicitly set. */
5535 if (!c->u->u_firstlineno) {
Ned Deilydc35cda2016-08-17 17:18:33 -04005536 if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005537 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
5538 else
5539 c->u->u_firstlineno = 1;
5540 }
5541 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
5542 goto error;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005543 dfs(c, entryblock, &a, nblocks);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005545 /* Can't modify the bytecode after computing jump offsets. */
5546 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00005547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005548 /* Emit code in reverse postorder from dfs. */
5549 for (i = a.a_nblocks - 1; i >= 0; i--) {
5550 b = a.a_postorder[i];
5551 for (j = 0; j < b->b_iused; j++)
5552 if (!assemble_emit(&a, &b->b_instr[j]))
5553 goto error;
5554 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00005555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005556 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
5557 goto error;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005558 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005559 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005561 co = makecode(c, &a);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005562 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005563 assemble_free(&a);
5564 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005565}
Georg Brandl8334fd92010-12-04 10:26:46 +00005566
5567#undef PyAST_Compile
5568PyAPI_FUNC(PyCodeObject *)
5569PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
5570 PyArena *arena)
5571{
5572 return PyAST_CompileEx(mod, filename, flags, -1, arena);
5573}