blob: 6c9e7954d688662e221f411e674016f884eb0e6e [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);
174static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
Victor Stinnerf8e32212013-11-19 23:56:34 +0100175static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000176static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000177static int compiler_error(struct compiler *, const char *);
178static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
179
180static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
181static int compiler_visit_stmt(struct compiler *, stmt_ty);
182static int compiler_visit_keyword(struct compiler *, keyword_ty);
183static int compiler_visit_expr(struct compiler *, expr_ty);
184static int compiler_augassign(struct compiler *, stmt_ty);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700185static int compiler_annassign(struct compiler *, stmt_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000186static int compiler_visit_slice(struct compiler *, slice_ty,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 expr_context_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000188
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000189static int inplace_binop(struct compiler *, operator_ty);
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +0200190static int expr_constant(expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000191
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500192static int compiler_with(struct compiler *, stmt_ty, int);
Yury Selivanov75445082015-05-11 22:57:16 -0400193static int compiler_async_with(struct compiler *, stmt_ty, int);
194static int compiler_async_for(struct compiler *, stmt_ty);
Victor Stinner976bb402016-03-23 11:36:19 +0100195static int compiler_call_helper(struct compiler *c, int n,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 asdl_seq *args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400197 asdl_seq *keywords);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500198static int compiler_try_except(struct compiler *, stmt_ty);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400199static int compiler_set_qualname(struct compiler *);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000200
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700201static int compiler_sync_comprehension_generator(
202 struct compiler *c,
203 asdl_seq *generators, int gen_index,
204 expr_ty elt, expr_ty val, int type);
205
206static int compiler_async_comprehension_generator(
207 struct compiler *c,
208 asdl_seq *generators, int gen_index,
209 expr_ty elt, expr_ty val, int type);
210
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000211static PyCodeObject *assemble(struct compiler *, int addNone);
Mark Shannon332cd5e2018-01-30 00:41:04 +0000212static PyObject *__doc__, *__annotations__;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000213
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400214#define CAPSULE_NAME "compile.c compiler unit"
Benjamin Petersonb173f782009-05-05 22:31:58 +0000215
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000216PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000217_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 /* Name mangling: __private becomes _classname__private.
220 This is independent from how the name is used. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200221 PyObject *result;
222 size_t nlen, plen, ipriv;
223 Py_UCS4 maxchar;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200225 PyUnicode_READ_CHAR(ident, 0) != '_' ||
226 PyUnicode_READ_CHAR(ident, 1) != '_') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 Py_INCREF(ident);
228 return ident;
229 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200230 nlen = PyUnicode_GET_LENGTH(ident);
231 plen = PyUnicode_GET_LENGTH(privateobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 /* Don't mangle __id__ or names with dots.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 The only time a name with a dot can occur is when
235 we are compiling an import statement that has a
236 package name.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 TODO(jhylton): Decide whether we want to support
239 mangling of the module name, e.g. __M.X.
240 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200241 if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
242 PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
243 PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 Py_INCREF(ident);
245 return ident; /* Don't mangle __whatever__ */
246 }
247 /* Strip leading underscores from class name */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200248 ipriv = 0;
249 while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
250 ipriv++;
251 if (ipriv == plen) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 Py_INCREF(ident);
253 return ident; /* Don't mangle if class is just underscores */
254 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200255 plen -= ipriv;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000256
Antoine Pitrou55bff892013-04-06 21:21:04 +0200257 if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
258 PyErr_SetString(PyExc_OverflowError,
259 "private identifier too large to be mangled");
260 return NULL;
261 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000262
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200263 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
264 if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
265 maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
266
267 result = PyUnicode_New(1 + nlen + plen, maxchar);
268 if (!result)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200270 /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
271 PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
Victor Stinner6c7a52a2011-09-28 21:39:17 +0200272 if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
273 Py_DECREF(result);
274 return NULL;
275 }
276 if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
277 Py_DECREF(result);
278 return NULL;
279 }
Victor Stinner8f825062012-04-27 13:55:39 +0200280 assert(_PyUnicode_CheckConsistency(result, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200281 return result;
Michael W. Hudson60934622004-08-12 17:56:29 +0000282}
283
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000284static int
285compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 c->c_stack = PyList_New(0);
290 if (!c->c_stack)
291 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000294}
295
296PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200297PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
298 int optimize, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 struct compiler c;
301 PyCodeObject *co = NULL;
302 PyCompilerFlags local_flags;
303 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 if (!__doc__) {
306 __doc__ = PyUnicode_InternFromString("__doc__");
307 if (!__doc__)
308 return NULL;
309 }
Mark Shannon332cd5e2018-01-30 00:41:04 +0000310 if (!__annotations__) {
311 __annotations__ = PyUnicode_InternFromString("__annotations__");
312 if (!__annotations__)
313 return NULL;
314 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 if (!compiler_init(&c))
316 return NULL;
Victor Stinner14e461d2013-08-26 22:28:21 +0200317 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 c.c_filename = filename;
319 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200320 c.c_future = PyFuture_FromASTObject(mod, filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 if (c.c_future == NULL)
322 goto finally;
323 if (!flags) {
324 local_flags.cf_flags = 0;
325 flags = &local_flags;
326 }
327 merged = c.c_future->ff_features | flags->cf_flags;
328 c.c_future->ff_features = merged;
329 flags->cf_flags = merged;
330 c.c_flags = flags;
Georg Brandl8334fd92010-12-04 10:26:46 +0000331 c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000333
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +0200334 if (!_PyAST_Optimize(mod, arena, c.c_optimize)) {
INADA Naoki7ea143a2017-12-14 16:47:20 +0900335 goto finally;
336 }
337
Victor Stinner14e461d2013-08-26 22:28:21 +0200338 c.c_st = PySymtable_BuildObject(mod, filename, c.c_future);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 if (c.c_st == NULL) {
340 if (!PyErr_Occurred())
341 PyErr_SetString(PyExc_SystemError, "no symtable");
342 goto finally;
343 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000346
Thomas Wouters1175c432006-02-27 22:49:54 +0000347 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 compiler_free(&c);
349 assert(co || PyErr_Occurred());
350 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000351}
352
353PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200354PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags,
355 int optimize, PyArena *arena)
356{
357 PyObject *filename;
358 PyCodeObject *co;
359 filename = PyUnicode_DecodeFSDefault(filename_str);
360 if (filename == NULL)
361 return NULL;
362 co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
363 Py_DECREF(filename);
364 return co;
365
366}
367
368PyCodeObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000369PyNode_Compile(struct _node *n, const char *filename)
370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 PyCodeObject *co = NULL;
372 mod_ty mod;
373 PyArena *arena = PyArena_New();
374 if (!arena)
375 return NULL;
376 mod = PyAST_FromNode(n, NULL, filename, arena);
377 if (mod)
378 co = PyAST_Compile(mod, filename, NULL, arena);
379 PyArena_Free(arena);
380 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000381}
382
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000383static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000384compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 if (c->c_st)
387 PySymtable_Free(c->c_st);
388 if (c->c_future)
389 PyObject_Free(c->c_future);
Victor Stinner14e461d2013-08-26 22:28:21 +0200390 Py_XDECREF(c->c_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000392}
393
Guido van Rossum79f25d91997-04-29 20:08:16 +0000394static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000395list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 Py_ssize_t i, n;
398 PyObject *v, *k;
399 PyObject *dict = PyDict_New();
400 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 n = PyList_Size(list);
403 for (i = 0; i < n; i++) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100404 v = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 if (!v) {
406 Py_DECREF(dict);
407 return NULL;
408 }
409 k = PyList_GET_ITEM(list, i);
Victor Stinnerefb24132016-01-22 12:33:12 +0100410 k = _PyCode_ConstantKey(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
412 Py_XDECREF(k);
413 Py_DECREF(v);
414 Py_DECREF(dict);
415 return NULL;
416 }
417 Py_DECREF(k);
418 Py_DECREF(v);
419 }
420 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000421}
422
423/* Return new dict containing names from src that match scope(s).
424
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000425src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000427values are integers, starting at offset and increasing by one for
428each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000429*/
430
431static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +0100432dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000433{
Benjamin Peterson51ab2832012-07-18 15:12:47 -0700434 Py_ssize_t i = offset, scope, num_keys, key_i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 PyObject *k, *v, *dest = PyDict_New();
Meador Inge2ca63152012-07-18 14:20:11 -0500436 PyObject *sorted_keys;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 assert(offset >= 0);
439 if (dest == NULL)
440 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000441
Meador Inge2ca63152012-07-18 14:20:11 -0500442 /* Sort the keys so that we have a deterministic order on the indexes
443 saved in the returned dictionary. These indexes are used as indexes
444 into the free and cell var storage. Therefore if they aren't
445 deterministic, then the generated bytecode is not deterministic.
446 */
447 sorted_keys = PyDict_Keys(src);
448 if (sorted_keys == NULL)
449 return NULL;
450 if (PyList_Sort(sorted_keys) != 0) {
451 Py_DECREF(sorted_keys);
452 return NULL;
453 }
Meador Ingef69e24e2012-07-18 16:41:03 -0500454 num_keys = PyList_GET_SIZE(sorted_keys);
Meador Inge2ca63152012-07-18 14:20:11 -0500455
456 for (key_i = 0; key_i < num_keys; key_i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 /* XXX this should probably be a macro in symtable.h */
458 long vi;
Meador Inge2ca63152012-07-18 14:20:11 -0500459 k = PyList_GET_ITEM(sorted_keys, key_i);
460 v = PyDict_GetItem(src, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 assert(PyLong_Check(v));
462 vi = PyLong_AS_LONG(v);
463 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 if (scope == scope_type || vi & flag) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100466 PyObject *tuple, *item = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 if (item == NULL) {
Meador Inge2ca63152012-07-18 14:20:11 -0500468 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 Py_DECREF(dest);
470 return NULL;
471 }
472 i++;
Victor Stinnerefb24132016-01-22 12:33:12 +0100473 tuple = _PyCode_ConstantKey(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
Meador Inge2ca63152012-07-18 14:20:11 -0500475 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 Py_DECREF(item);
477 Py_DECREF(dest);
478 Py_XDECREF(tuple);
479 return NULL;
480 }
481 Py_DECREF(item);
482 Py_DECREF(tuple);
483 }
484 }
Meador Inge2ca63152012-07-18 14:20:11 -0500485 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000487}
488
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000489static void
490compiler_unit_check(struct compiler_unit *u)
491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 basicblock *block;
493 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Benjamin Petersonca470632016-09-06 13:47:26 -0700494 assert((uintptr_t)block != 0xcbcbcbcbU);
495 assert((uintptr_t)block != 0xfbfbfbfbU);
496 assert((uintptr_t)block != 0xdbdbdbdbU);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 if (block->b_instr != NULL) {
498 assert(block->b_ialloc > 0);
499 assert(block->b_iused > 0);
500 assert(block->b_ialloc >= block->b_iused);
501 }
502 else {
503 assert (block->b_iused == 0);
504 assert (block->b_ialloc == 0);
505 }
506 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000507}
508
509static void
510compiler_unit_free(struct compiler_unit *u)
511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 compiler_unit_check(u);
515 b = u->u_blocks;
516 while (b != NULL) {
517 if (b->b_instr)
518 PyObject_Free((void *)b->b_instr);
519 next = b->b_list;
520 PyObject_Free((void *)b);
521 b = next;
522 }
523 Py_CLEAR(u->u_ste);
524 Py_CLEAR(u->u_name);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400525 Py_CLEAR(u->u_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 Py_CLEAR(u->u_consts);
527 Py_CLEAR(u->u_names);
528 Py_CLEAR(u->u_varnames);
529 Py_CLEAR(u->u_freevars);
530 Py_CLEAR(u->u_cellvars);
531 Py_CLEAR(u->u_private);
532 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000533}
534
535static int
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100536compiler_enter_scope(struct compiler *c, identifier name,
537 int scope_type, void *key, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 struct compiler_unit *u;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100540 basicblock *block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
543 struct compiler_unit));
544 if (!u) {
545 PyErr_NoMemory();
546 return 0;
547 }
548 memset(u, 0, sizeof(struct compiler_unit));
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100549 u->u_scope_type = scope_type;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 u->u_argcount = 0;
551 u->u_kwonlyargcount = 0;
552 u->u_ste = PySymtable_Lookup(c->c_st, key);
553 if (!u->u_ste) {
554 compiler_unit_free(u);
555 return 0;
556 }
557 Py_INCREF(name);
558 u->u_name = name;
559 u->u_varnames = list2dict(u->u_ste->ste_varnames);
560 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
561 if (!u->u_varnames || !u->u_cellvars) {
562 compiler_unit_free(u);
563 return 0;
564 }
Benjamin Peterson312595c2013-05-15 15:26:42 -0500565 if (u->u_ste->ste_needs_class_closure) {
Martin Panter7462b6492015-11-02 03:37:02 +0000566 /* Cook up an implicit __class__ cell. */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500567 _Py_IDENTIFIER(__class__);
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300568 PyObject *tuple, *name;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500569 int res;
570 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200571 assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500572 name = _PyUnicode_FromId(&PyId___class__);
573 if (!name) {
574 compiler_unit_free(u);
575 return 0;
576 }
Victor Stinnerefb24132016-01-22 12:33:12 +0100577 tuple = _PyCode_ConstantKey(name);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500578 if (!tuple) {
579 compiler_unit_free(u);
580 return 0;
581 }
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300582 res = PyDict_SetItem(u->u_cellvars, tuple, _PyLong_Zero);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500583 Py_DECREF(tuple);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500584 if (res < 0) {
585 compiler_unit_free(u);
586 return 0;
587 }
588 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200591 PyDict_GET_SIZE(u->u_cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 if (!u->u_freevars) {
593 compiler_unit_free(u);
594 return 0;
595 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 u->u_blocks = NULL;
598 u->u_nfblocks = 0;
599 u->u_firstlineno = lineno;
600 u->u_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000601 u->u_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 u->u_lineno_set = 0;
603 u->u_consts = PyDict_New();
604 if (!u->u_consts) {
605 compiler_unit_free(u);
606 return 0;
607 }
608 u->u_names = PyDict_New();
609 if (!u->u_names) {
610 compiler_unit_free(u);
611 return 0;
612 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 /* Push the old compiler_unit on the stack. */
617 if (c->u) {
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400618 PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
620 Py_XDECREF(capsule);
621 compiler_unit_free(u);
622 return 0;
623 }
624 Py_DECREF(capsule);
625 u->u_private = c->u->u_private;
626 Py_XINCREF(u->u_private);
627 }
628 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 c->c_nestlevel++;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100631
632 block = compiler_new_block(c);
633 if (block == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 return 0;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100635 c->u->u_curblock = block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000636
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400637 if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
638 if (!compiler_set_qualname(c))
639 return 0;
640 }
641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643}
644
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000645static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000646compiler_exit_scope(struct compiler *c)
647{
Victor Stinnerad9a0662013-11-19 22:23:20 +0100648 Py_ssize_t n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 PyObject *capsule;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 c->c_nestlevel--;
652 compiler_unit_free(c->u);
653 /* Restore c->u to the parent unit. */
654 n = PyList_GET_SIZE(c->c_stack) - 1;
655 if (n >= 0) {
656 capsule = PyList_GET_ITEM(c->c_stack, n);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400657 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 assert(c->u);
659 /* we are deleting from a list so this really shouldn't fail */
660 if (PySequence_DelItem(c->c_stack, n) < 0)
661 Py_FatalError("compiler_exit_scope()");
662 compiler_unit_check(c->u);
663 }
664 else
665 c->u = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000666
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000667}
668
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400669static int
670compiler_set_qualname(struct compiler *c)
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100671{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100672 _Py_static_string(dot, ".");
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400673 _Py_static_string(dot_locals, ".<locals>");
674 Py_ssize_t stack_size;
675 struct compiler_unit *u = c->u;
676 PyObject *name, *base, *dot_str, *dot_locals_str;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100677
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400678 base = NULL;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100679 stack_size = PyList_GET_SIZE(c->c_stack);
Benjamin Petersona8a38b82013-10-19 16:14:39 -0400680 assert(stack_size >= 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400681 if (stack_size > 1) {
682 int scope, force_global = 0;
683 struct compiler_unit *parent;
684 PyObject *mangled, *capsule;
685
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400686 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400687 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400688 assert(parent);
689
Yury Selivanov75445082015-05-11 22:57:16 -0400690 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
691 || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
692 || u->u_scope_type == COMPILER_SCOPE_CLASS) {
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400693 assert(u->u_name);
694 mangled = _Py_Mangle(parent->u_private, u->u_name);
695 if (!mangled)
696 return 0;
697 scope = PyST_GetScope(parent->u_ste, mangled);
698 Py_DECREF(mangled);
699 assert(scope != GLOBAL_IMPLICIT);
700 if (scope == GLOBAL_EXPLICIT)
701 force_global = 1;
702 }
703
704 if (!force_global) {
705 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
Yury Selivanov75445082015-05-11 22:57:16 -0400706 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400707 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
708 dot_locals_str = _PyUnicode_FromId(&dot_locals);
709 if (dot_locals_str == NULL)
710 return 0;
711 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
712 if (base == NULL)
713 return 0;
714 }
715 else {
716 Py_INCREF(parent->u_qualname);
717 base = parent->u_qualname;
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400718 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100719 }
720 }
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400721
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400722 if (base != NULL) {
723 dot_str = _PyUnicode_FromId(&dot);
724 if (dot_str == NULL) {
725 Py_DECREF(base);
726 return 0;
727 }
728 name = PyUnicode_Concat(base, dot_str);
729 Py_DECREF(base);
730 if (name == NULL)
731 return 0;
732 PyUnicode_Append(&name, u->u_name);
733 if (name == NULL)
734 return 0;
735 }
736 else {
737 Py_INCREF(u->u_name);
738 name = u->u_name;
739 }
740 u->u_qualname = name;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100741
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400742 return 1;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100743}
744
Eric V. Smith235a6f02015-09-19 14:51:32 -0400745
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000746/* Allocate a new block and return a pointer to it.
747 Returns NULL on error.
748*/
749
750static basicblock *
751compiler_new_block(struct compiler *c)
752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 basicblock *b;
754 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 u = c->u;
757 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
758 if (b == NULL) {
759 PyErr_NoMemory();
760 return NULL;
761 }
762 memset((void *)b, 0, sizeof(basicblock));
763 /* Extend the singly linked list of blocks with new block. */
764 b->b_list = u->u_blocks;
765 u->u_blocks = b;
766 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767}
768
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000769static basicblock *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770compiler_next_block(struct compiler *c)
771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 basicblock *block = compiler_new_block(c);
773 if (block == NULL)
774 return NULL;
775 c->u->u_curblock->b_next = block;
776 c->u->u_curblock = block;
777 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778}
779
780static basicblock *
781compiler_use_next_block(struct compiler *c, basicblock *block)
782{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 assert(block != NULL);
784 c->u->u_curblock->b_next = block;
785 c->u->u_curblock = block;
786 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000787}
788
789/* Returns the offset of the next instruction in the current block's
790 b_instr array. Resizes the b_instr as necessary.
791 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000792*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793
794static int
795compiler_next_instr(struct compiler *c, basicblock *b)
796{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 assert(b != NULL);
798 if (b->b_instr == NULL) {
799 b->b_instr = (struct instr *)PyObject_Malloc(
800 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
801 if (b->b_instr == NULL) {
802 PyErr_NoMemory();
803 return -1;
804 }
805 b->b_ialloc = DEFAULT_BLOCK_SIZE;
806 memset((char *)b->b_instr, 0,
807 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
808 }
809 else if (b->b_iused == b->b_ialloc) {
810 struct instr *tmp;
811 size_t oldsize, newsize;
812 oldsize = b->b_ialloc * sizeof(struct instr);
813 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000814
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -0700815 if (oldsize > (SIZE_MAX >> 1)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 PyErr_NoMemory();
817 return -1;
818 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 if (newsize == 0) {
821 PyErr_NoMemory();
822 return -1;
823 }
824 b->b_ialloc <<= 1;
825 tmp = (struct instr *)PyObject_Realloc(
826 (void *)b->b_instr, newsize);
827 if (tmp == NULL) {
828 PyErr_NoMemory();
829 return -1;
830 }
831 b->b_instr = tmp;
832 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
833 }
834 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835}
836
Christian Heimes2202f872008-02-06 14:31:34 +0000837/* Set the i_lineno member of the instruction at offset off if the
838 line number for the current expression/statement has not
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000839 already been set. If it has been set, the call has no effect.
840
Christian Heimes2202f872008-02-06 14:31:34 +0000841 The line number is reset in the following cases:
842 - when entering a new scope
843 - on each statement
844 - on each expression that start a new line
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200845 - before the "except" and "finally" clauses
Christian Heimes2202f872008-02-06 14:31:34 +0000846 - before the "for" and "while" expressions
Thomas Wouters89f507f2006-12-13 04:49:30 +0000847*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000848
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849static void
850compiler_set_lineno(struct compiler *c, int off)
851{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 basicblock *b;
853 if (c->u->u_lineno_set)
854 return;
855 c->u->u_lineno_set = 1;
856 b = c->u->u_curblock;
857 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858}
859
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200860/* Return the stack effect of opcode with argument oparg.
861
862 Some opcodes have different stack effect when jump to the target and
863 when not jump. The 'jump' parameter specifies the case:
864
865 * 0 -- when not jump
866 * 1 -- when jump
867 * -1 -- maximal
868 */
869/* XXX Make the stack effect of WITH_CLEANUP_START and
870 WITH_CLEANUP_FINISH deterministic. */
871static int
872stack_effect(int opcode, int oparg, int jump)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 switch (opcode) {
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200875 /* Stack manipulation */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 case POP_TOP:
877 return -1;
878 case ROT_TWO:
879 case ROT_THREE:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200880 case ROT_FOUR:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 return 0;
882 case DUP_TOP:
883 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000884 case DUP_TOP_TWO:
885 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000886
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200887 /* Unary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 case UNARY_POSITIVE:
889 case UNARY_NEGATIVE:
890 case UNARY_NOT:
891 case UNARY_INVERT:
892 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 case SET_ADD:
895 case LIST_APPEND:
896 return -1;
897 case MAP_ADD:
898 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000899
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200900 /* Binary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 case BINARY_POWER:
902 case BINARY_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400903 case BINARY_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 case BINARY_MODULO:
905 case BINARY_ADD:
906 case BINARY_SUBTRACT:
907 case BINARY_SUBSCR:
908 case BINARY_FLOOR_DIVIDE:
909 case BINARY_TRUE_DIVIDE:
910 return -1;
911 case INPLACE_FLOOR_DIVIDE:
912 case INPLACE_TRUE_DIVIDE:
913 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 case INPLACE_ADD:
916 case INPLACE_SUBTRACT:
917 case INPLACE_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400918 case INPLACE_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 case INPLACE_MODULO:
920 return -1;
921 case STORE_SUBSCR:
922 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 case DELETE_SUBSCR:
924 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 case BINARY_LSHIFT:
927 case BINARY_RSHIFT:
928 case BINARY_AND:
929 case BINARY_XOR:
930 case BINARY_OR:
931 return -1;
932 case INPLACE_POWER:
933 return -1;
934 case GET_ITER:
935 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 case PRINT_EXPR:
938 return -1;
939 case LOAD_BUILD_CLASS:
940 return 1;
941 case INPLACE_LSHIFT:
942 case INPLACE_RSHIFT:
943 case INPLACE_AND:
944 case INPLACE_XOR:
945 case INPLACE_OR:
946 return -1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 case SETUP_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200949 /* 1 in the normal flow.
950 * Restore the stack position and push 6 values before jumping to
951 * the handler if an exception be raised. */
952 return jump ? 6 : 1;
Yury Selivanov75445082015-05-11 22:57:16 -0400953 case WITH_CLEANUP_START:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200954 return 2; /* or 1, depending on TOS */
Yury Selivanov75445082015-05-11 22:57:16 -0400955 case WITH_CLEANUP_FINISH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200956 /* Pop a variable number of values pushed by WITH_CLEANUP_START
957 * + __exit__ or __aexit__. */
958 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 case RETURN_VALUE:
960 return -1;
961 case IMPORT_STAR:
962 return -1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700963 case SETUP_ANNOTATIONS:
964 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 case YIELD_VALUE:
966 return 0;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500967 case YIELD_FROM:
968 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 case POP_BLOCK:
970 return 0;
971 case POP_EXCEPT:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200972 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 case END_FINALLY:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200974 case POP_FINALLY:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200975 /* Pop 6 values when an exception was raised. */
976 return -6;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 case STORE_NAME:
979 return -1;
980 case DELETE_NAME:
981 return 0;
982 case UNPACK_SEQUENCE:
983 return oparg-1;
984 case UNPACK_EX:
985 return (oparg&0xFF) + (oparg>>8);
986 case FOR_ITER:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200987 /* -1 at end of iterator, 1 if continue iterating. */
988 return jump > 0 ? -1 : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 case STORE_ATTR:
991 return -2;
992 case DELETE_ATTR:
993 return -1;
994 case STORE_GLOBAL:
995 return -1;
996 case DELETE_GLOBAL:
997 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 case LOAD_CONST:
999 return 1;
1000 case LOAD_NAME:
1001 return 1;
1002 case BUILD_TUPLE:
1003 case BUILD_LIST:
1004 case BUILD_SET:
Serhiy Storchakaea525a22016-09-06 22:07:53 +03001005 case BUILD_STRING:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 return 1-oparg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001007 case BUILD_LIST_UNPACK:
1008 case BUILD_TUPLE_UNPACK:
Serhiy Storchaka73442852016-10-02 10:33:46 +03001009 case BUILD_TUPLE_UNPACK_WITH_CALL:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001010 case BUILD_SET_UNPACK:
1011 case BUILD_MAP_UNPACK:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001012 case BUILD_MAP_UNPACK_WITH_CALL:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001013 return 1 - oparg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 case BUILD_MAP:
Benjamin Petersonb6855152015-09-10 21:02:39 -07001015 return 1 - 2*oparg;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001016 case BUILD_CONST_KEY_MAP:
1017 return -oparg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 case LOAD_ATTR:
1019 return 0;
1020 case COMPARE_OP:
1021 return -1;
1022 case IMPORT_NAME:
1023 return -1;
1024 case IMPORT_FROM:
1025 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001027 /* Jumps */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 case JUMP_FORWARD:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 case JUMP_ABSOLUTE:
1030 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001031
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001032 case JUMP_IF_TRUE_OR_POP:
1033 case JUMP_IF_FALSE_OR_POP:
1034 return jump ? 0 : -1;
1035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 case POP_JUMP_IF_FALSE:
1037 case POP_JUMP_IF_TRUE:
1038 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 case LOAD_GLOBAL:
1041 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001042
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001043 /* Exception handling */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 case SETUP_FINALLY:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001045 /* 0 in the normal flow.
1046 * Restore the stack position and push 6 values before jumping to
1047 * the handler if an exception be raised. */
1048 return jump ? 6 : 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001049 case BEGIN_FINALLY:
1050 /* Actually pushes 1 value, but count 6 for balancing with
1051 * END_FINALLY and POP_FINALLY.
1052 * This is the main reason of using this opcode instead of
1053 * "LOAD_CONST None". */
1054 return 6;
1055 case CALL_FINALLY:
1056 return jump ? 1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 case LOAD_FAST:
1059 return 1;
1060 case STORE_FAST:
1061 return -1;
1062 case DELETE_FAST:
1063 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 case RAISE_VARARGS:
1066 return -oparg;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001067
1068 /* Functions and calls */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 case CALL_FUNCTION:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001070 return -oparg;
Yury Selivanovf2392132016-12-13 19:03:51 -05001071 case CALL_METHOD:
1072 return -oparg-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 case CALL_FUNCTION_KW:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001074 return -oparg-1;
1075 case CALL_FUNCTION_EX:
Matthieu Dartiailh3a9ac822017-02-21 14:25:22 +01001076 return -1 - ((oparg & 0x01) != 0);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001077 case MAKE_FUNCTION:
1078 return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1079 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 case BUILD_SLICE:
1081 if (oparg == 3)
1082 return -2;
1083 else
1084 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001085
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001086 /* Closures */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 case LOAD_CLOSURE:
1088 return 1;
1089 case LOAD_DEREF:
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001090 case LOAD_CLASSDEREF:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 return 1;
1092 case STORE_DEREF:
1093 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001094 case DELETE_DEREF:
1095 return 0;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001096
1097 /* Iterators and generators */
Yury Selivanov75445082015-05-11 22:57:16 -04001098 case GET_AWAITABLE:
1099 return 0;
1100 case SETUP_ASYNC_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001101 /* 0 in the normal flow.
1102 * Restore the stack position to the position before the result
1103 * of __aenter__ and push 6 values before jumping to the handler
1104 * if an exception be raised. */
1105 return jump ? -1 + 6 : 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001106 case BEFORE_ASYNC_WITH:
1107 return 1;
1108 case GET_AITER:
1109 return 0;
1110 case GET_ANEXT:
1111 return 1;
Yury Selivanov5376ba92015-06-22 12:19:30 -04001112 case GET_YIELD_FROM_ITER:
1113 return 0;
Eric V. Smitha78c7952015-11-03 12:45:05 -05001114 case FORMAT_VALUE:
1115 /* If there's a fmt_spec on the stack, we go from 2->1,
1116 else 1->1. */
1117 return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
Yury Selivanovf2392132016-12-13 19:03:51 -05001118 case LOAD_METHOD:
1119 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 default:
Larry Hastings3a907972013-11-23 14:49:22 -08001121 return PY_INVALID_STACK_EFFECT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 }
Larry Hastings3a907972013-11-23 14:49:22 -08001123 return PY_INVALID_STACK_EFFECT; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124}
1125
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001126int
1127PyCompile_OpcodeStackEffect(int opcode, int oparg)
1128{
1129 return stack_effect(opcode, oparg, -1);
1130}
1131
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132/* Add an opcode with no argument.
1133 Returns 0 on failure, 1 on success.
1134*/
1135
1136static int
1137compiler_addop(struct compiler *c, int opcode)
1138{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 basicblock *b;
1140 struct instr *i;
1141 int off;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001142 assert(!HAS_ARG(opcode));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 off = compiler_next_instr(c, c->u->u_curblock);
1144 if (off < 0)
1145 return 0;
1146 b = c->u->u_curblock;
1147 i = &b->b_instr[off];
1148 i->i_opcode = opcode;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001149 i->i_oparg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 if (opcode == RETURN_VALUE)
1151 b->b_return = 1;
1152 compiler_set_lineno(c, off);
1153 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154}
1155
Victor Stinnerf8e32212013-11-19 23:56:34 +01001156static Py_ssize_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
1158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 PyObject *t, *v;
1160 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161
Victor Stinnerefb24132016-01-22 12:33:12 +01001162 t = _PyCode_ConstantKey(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 if (t == NULL)
1164 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 v = PyDict_GetItem(dict, t);
1167 if (!v) {
Stefan Krahc0cbed12015-07-27 12:56:49 +02001168 if (PyErr_Occurred()) {
1169 Py_DECREF(t);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 return -1;
Stefan Krahc0cbed12015-07-27 12:56:49 +02001171 }
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001172 arg = PyDict_GET_SIZE(dict);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001173 v = PyLong_FromSsize_t(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 if (!v) {
1175 Py_DECREF(t);
1176 return -1;
1177 }
1178 if (PyDict_SetItem(dict, t, v) < 0) {
1179 Py_DECREF(t);
1180 Py_DECREF(v);
1181 return -1;
1182 }
1183 Py_DECREF(v);
1184 }
1185 else
1186 arg = PyLong_AsLong(v);
1187 Py_DECREF(t);
1188 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189}
1190
1191static int
1192compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001195 Py_ssize_t arg = compiler_add_o(c, dict, o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001197 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198 return compiler_addop_i(c, opcode, arg);
1199}
1200
1201static int
1202compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001205 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001206 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1207 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001208 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209 arg = compiler_add_o(c, dict, mangled);
1210 Py_DECREF(mangled);
1211 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001212 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213 return compiler_addop_i(c, opcode, arg);
1214}
1215
1216/* Add an opcode with an integer argument.
1217 Returns 0 on failure, 1 on success.
1218*/
1219
1220static int
Victor Stinnerf8e32212013-11-19 23:56:34 +01001221compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 struct instr *i;
1224 int off;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001225
Victor Stinner2ad474b2016-03-01 23:34:47 +01001226 /* oparg value is unsigned, but a signed C int is usually used to store
1227 it in the C code (like Python/ceval.c).
1228
1229 Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1230
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001231 The argument of a concrete bytecode instruction is limited to 8-bit.
1232 EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1233 assert(HAS_ARG(opcode));
Victor Stinner2ad474b2016-03-01 23:34:47 +01001234 assert(0 <= oparg && oparg <= 2147483647);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 off = compiler_next_instr(c, c->u->u_curblock);
1237 if (off < 0)
1238 return 0;
1239 i = &c->u->u_curblock->b_instr[off];
Victor Stinnerf8e32212013-11-19 23:56:34 +01001240 i->i_opcode = opcode;
1241 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 compiler_set_lineno(c, off);
1243 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244}
1245
1246static int
1247compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 struct instr *i;
1250 int off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001252 assert(HAS_ARG(opcode));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 assert(b != NULL);
1254 off = compiler_next_instr(c, c->u->u_curblock);
1255 if (off < 0)
1256 return 0;
1257 i = &c->u->u_curblock->b_instr[off];
1258 i->i_opcode = opcode;
1259 i->i_target = b;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 if (absolute)
1261 i->i_jabs = 1;
1262 else
1263 i->i_jrel = 1;
1264 compiler_set_lineno(c, off);
1265 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001266}
1267
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +01001268/* NEXT_BLOCK() creates an implicit jump from the current block
1269 to the new block.
1270
1271 The returns inside this macro make it impossible to decref objects
1272 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 if (compiler_next_block((C)) == NULL) \
1276 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001277}
1278
1279#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 if (!compiler_addop((C), (OP))) \
1281 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001282}
1283
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001284#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 if (!compiler_addop((C), (OP))) { \
1286 compiler_exit_scope(c); \
1287 return 0; \
1288 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001289}
1290
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1293 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001294}
1295
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001296/* Same as ADDOP_O, but steals a reference. */
1297#define ADDOP_N(C, OP, O, TYPE) { \
1298 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1299 Py_DECREF((O)); \
1300 return 0; \
1301 } \
1302 Py_DECREF((O)); \
1303}
1304
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001305#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1307 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001308}
1309
1310#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 if (!compiler_addop_i((C), (OP), (O))) \
1312 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001313}
1314
1315#define ADDOP_JABS(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 if (!compiler_addop_j((C), (OP), (O), 1)) \
1317 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318}
1319
1320#define ADDOP_JREL(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 if (!compiler_addop_j((C), (OP), (O), 0)) \
1322 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001323}
1324
1325/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1326 the ASDL name to synthesize the name of the C type and the visit function.
1327*/
1328
1329#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 if (!compiler_visit_ ## TYPE((C), (V))) \
1331 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001332}
1333
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001334#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 if (!compiler_visit_ ## TYPE((C), (V))) { \
1336 compiler_exit_scope(c); \
1337 return 0; \
1338 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001339}
1340
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 if (!compiler_visit_slice((C), (V), (CTX))) \
1343 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344}
1345
1346#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 int _i; \
1348 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1349 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1350 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1351 if (!compiler_visit_ ## TYPE((C), elt)) \
1352 return 0; \
1353 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001354}
1355
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001356#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 int _i; \
1358 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1359 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1360 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1361 if (!compiler_visit_ ## TYPE((C), elt)) { \
1362 compiler_exit_scope(c); \
1363 return 0; \
1364 } \
1365 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001366}
1367
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368static int
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001369is_const(expr_ty e)
1370{
1371 switch (e->kind) {
1372 case Constant_kind:
1373 case Num_kind:
1374 case Str_kind:
1375 case Bytes_kind:
1376 case Ellipsis_kind:
1377 case NameConstant_kind:
1378 return 1;
1379 default:
1380 return 0;
1381 }
1382}
1383
1384static PyObject *
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02001385get_const_value(expr_ty e)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001386{
1387 switch (e->kind) {
1388 case Constant_kind:
1389 return e->v.Constant.value;
1390 case Num_kind:
1391 return e->v.Num.n;
1392 case Str_kind:
1393 return e->v.Str.s;
1394 case Bytes_kind:
1395 return e->v.Bytes.s;
1396 case Ellipsis_kind:
1397 return Py_Ellipsis;
1398 case NameConstant_kind:
1399 return e->v.NameConstant.value;
1400 default:
Barry Warsawb2e57942017-09-14 18:13:16 -07001401 Py_UNREACHABLE();
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001402 }
1403}
1404
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001405/* Search if variable annotations are present statically in a block. */
1406
1407static int
1408find_ann(asdl_seq *stmts)
1409{
1410 int i, j, res = 0;
1411 stmt_ty st;
1412
1413 for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1414 st = (stmt_ty)asdl_seq_GET(stmts, i);
1415 switch (st->kind) {
1416 case AnnAssign_kind:
1417 return 1;
1418 case For_kind:
1419 res = find_ann(st->v.For.body) ||
1420 find_ann(st->v.For.orelse);
1421 break;
1422 case AsyncFor_kind:
1423 res = find_ann(st->v.AsyncFor.body) ||
1424 find_ann(st->v.AsyncFor.orelse);
1425 break;
1426 case While_kind:
1427 res = find_ann(st->v.While.body) ||
1428 find_ann(st->v.While.orelse);
1429 break;
1430 case If_kind:
1431 res = find_ann(st->v.If.body) ||
1432 find_ann(st->v.If.orelse);
1433 break;
1434 case With_kind:
1435 res = find_ann(st->v.With.body);
1436 break;
1437 case AsyncWith_kind:
1438 res = find_ann(st->v.AsyncWith.body);
1439 break;
1440 case Try_kind:
1441 for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1442 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1443 st->v.Try.handlers, j);
1444 if (find_ann(handler->v.ExceptHandler.body)) {
1445 return 1;
1446 }
1447 }
1448 res = find_ann(st->v.Try.body) ||
1449 find_ann(st->v.Try.finalbody) ||
1450 find_ann(st->v.Try.orelse);
1451 break;
1452 default:
1453 res = 0;
1454 }
1455 if (res) {
1456 break;
1457 }
1458 }
1459 return res;
1460}
1461
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001462/*
1463 * Frame block handling functions
1464 */
1465
1466static int
1467compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b,
1468 basicblock *exit)
1469{
1470 struct fblockinfo *f;
1471 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
1472 PyErr_SetString(PyExc_SyntaxError,
1473 "too many statically nested blocks");
1474 return 0;
1475 }
1476 f = &c->u->u_fblock[c->u->u_nfblocks++];
1477 f->fb_type = t;
1478 f->fb_block = b;
1479 f->fb_exit = exit;
1480 return 1;
1481}
1482
1483static void
1484compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
1485{
1486 struct compiler_unit *u = c->u;
1487 assert(u->u_nfblocks > 0);
1488 u->u_nfblocks--;
1489 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
1490 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
1491}
1492
1493/* Unwind a frame block. If preserve_tos is true, the TOS before
1494 * popping the blocks will be restored afterwards.
1495 */
1496static int
1497compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
1498 int preserve_tos)
1499{
1500 switch (info->fb_type) {
1501 case WHILE_LOOP:
1502 return 1;
1503
1504 case FINALLY_END:
1505 ADDOP_I(c, POP_FINALLY, preserve_tos);
1506 return 1;
1507
1508 case FOR_LOOP:
1509 /* Pop the iterator */
1510 if (preserve_tos) {
1511 ADDOP(c, ROT_TWO);
1512 }
1513 ADDOP(c, POP_TOP);
1514 return 1;
1515
1516 case EXCEPT:
1517 ADDOP(c, POP_BLOCK);
1518 return 1;
1519
1520 case FINALLY_TRY:
1521 ADDOP(c, POP_BLOCK);
1522 ADDOP_JREL(c, CALL_FINALLY, info->fb_exit);
1523 return 1;
1524
1525 case WITH:
1526 case ASYNC_WITH:
1527 ADDOP(c, POP_BLOCK);
1528 if (preserve_tos) {
1529 ADDOP(c, ROT_TWO);
1530 }
1531 ADDOP(c, BEGIN_FINALLY);
1532 ADDOP(c, WITH_CLEANUP_START);
1533 if (info->fb_type == ASYNC_WITH) {
1534 ADDOP(c, GET_AWAITABLE);
1535 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1536 ADDOP(c, YIELD_FROM);
1537 }
1538 ADDOP(c, WITH_CLEANUP_FINISH);
1539 ADDOP_I(c, POP_FINALLY, 0);
1540 return 1;
1541
1542 case HANDLER_CLEANUP:
1543 if (preserve_tos) {
1544 ADDOP(c, ROT_FOUR);
1545 }
1546 if (info->fb_exit) {
1547 ADDOP(c, POP_BLOCK);
1548 ADDOP(c, POP_EXCEPT);
1549 ADDOP_JREL(c, CALL_FINALLY, info->fb_exit);
1550 }
1551 else {
1552 ADDOP(c, POP_EXCEPT);
1553 }
1554 return 1;
1555 }
1556 Py_UNREACHABLE();
1557}
1558
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001559/* Compile a sequence of statements, checking for a docstring
1560 and for annotations. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001561
1562static int
INADA Naokicb41b272017-02-23 00:31:59 +09001563compiler_body(struct compiler *c, asdl_seq *stmts, string docstring)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564{
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001565 /* Set current line number to the line number of first statement.
1566 This way line number for SETUP_ANNOTATIONS will always
1567 coincide with the line number of first "real" statement in module.
1568 If body is empy, then lineno will be set later in assemble. */
1569 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE &&
1570 !c->u->u_lineno && asdl_seq_LEN(stmts)) {
INADA Naokicb41b272017-02-23 00:31:59 +09001571 stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, 0);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001572 c->u->u_lineno = st->lineno;
1573 }
1574 /* Every annotated class and module should have __annotations__. */
1575 if (find_ann(stmts)) {
1576 ADDOP(c, SETUP_ANNOTATIONS);
1577 }
INADA Naokicb41b272017-02-23 00:31:59 +09001578 /* if not -OO mode, set docstring */
1579 if (c->c_optimize < 2 && docstring) {
1580 ADDOP_O(c, LOAD_CONST, docstring, consts);
1581 ADDOP_NAME(c, STORE_NAME, __doc__, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 }
INADA Naokicb41b272017-02-23 00:31:59 +09001583 VISIT_SEQ(c, stmt, stmts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001585}
1586
1587static PyCodeObject *
1588compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001589{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 PyCodeObject *co;
1591 int addNone = 1;
1592 static PyObject *module;
1593 if (!module) {
1594 module = PyUnicode_InternFromString("<module>");
1595 if (!module)
1596 return NULL;
1597 }
1598 /* Use 0 for firstlineno initially, will fixup in assemble(). */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001599 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 return NULL;
1601 switch (mod->kind) {
1602 case Module_kind:
INADA Naokicb41b272017-02-23 00:31:59 +09001603 if (!compiler_body(c, mod->v.Module.body, mod->v.Module.docstring)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 compiler_exit_scope(c);
1605 return 0;
1606 }
1607 break;
1608 case Interactive_kind:
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001609 if (find_ann(mod->v.Interactive.body)) {
1610 ADDOP(c, SETUP_ANNOTATIONS);
1611 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 c->c_interactive = 1;
1613 VISIT_SEQ_IN_SCOPE(c, stmt,
1614 mod->v.Interactive.body);
1615 break;
1616 case Expression_kind:
1617 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1618 addNone = 0;
1619 break;
1620 case Suite_kind:
1621 PyErr_SetString(PyExc_SystemError,
1622 "suite should not be possible");
1623 return 0;
1624 default:
1625 PyErr_Format(PyExc_SystemError,
1626 "module kind %d should not be possible",
1627 mod->kind);
1628 return 0;
1629 }
1630 co = assemble(c, addNone);
1631 compiler_exit_scope(c);
1632 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001633}
1634
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001635/* The test for LOCAL must come before the test for FREE in order to
1636 handle classes where name is both local and free. The local var is
1637 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001638*/
1639
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001640static int
1641get_ref_type(struct compiler *c, PyObject *name)
1642{
Victor Stinner0b1bc562013-05-16 22:17:17 +02001643 int scope;
Benjamin Peterson312595c2013-05-15 15:26:42 -05001644 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001645 _PyUnicode_EqualToASCIIString(name, "__class__"))
Benjamin Peterson312595c2013-05-15 15:26:42 -05001646 return CELL;
Victor Stinner0b1bc562013-05-16 22:17:17 +02001647 scope = PyST_GetScope(c->u->u_ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 if (scope == 0) {
1649 char buf[350];
1650 PyOS_snprintf(buf, sizeof(buf),
Victor Stinner14e461d2013-08-26 22:28:21 +02001651 "unknown scope for %.100s in %.100s(%s)\n"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 "symbols: %s\nlocals: %s\nglobals: %s",
Serhiy Storchakadf4518c2014-11-18 23:34:33 +02001653 PyUnicode_AsUTF8(name),
1654 PyUnicode_AsUTF8(c->u->u_name),
1655 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)),
1656 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)),
1657 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)),
1658 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 );
1660 Py_FatalError(buf);
1661 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001664}
1665
1666static int
1667compiler_lookup_arg(PyObject *dict, PyObject *name)
1668{
1669 PyObject *k, *v;
Victor Stinnerefb24132016-01-22 12:33:12 +01001670 k = _PyCode_ConstantKey(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001671 if (k == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001672 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001673 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001674 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001675 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001676 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001677 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001678}
1679
1680static int
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001681compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001682{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001683 Py_ssize_t i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001684 if (qualname == NULL)
1685 qualname = co->co_name;
1686
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001687 if (free) {
1688 for (i = 0; i < free; ++i) {
1689 /* Bypass com_addop_varname because it will generate
1690 LOAD_DEREF but LOAD_CLOSURE is needed.
1691 */
1692 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1693 int arg, reftype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001694
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001695 /* Special case: If a class contains a method with a
1696 free variable that has the same name as a method,
1697 the name will be considered free *and* local in the
1698 class. It should be handled by the closure, as
1699 well as by the normal name loookup logic.
1700 */
1701 reftype = get_ref_type(c, name);
1702 if (reftype == CELL)
1703 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1704 else /* (reftype == FREE) */
1705 arg = compiler_lookup_arg(c->u->u_freevars, name);
1706 if (arg == -1) {
1707 fprintf(stderr,
1708 "lookup %s in %s %d %d\n"
1709 "freevars of %s: %s\n",
1710 PyUnicode_AsUTF8(PyObject_Repr(name)),
1711 PyUnicode_AsUTF8(c->u->u_name),
1712 reftype, arg,
1713 PyUnicode_AsUTF8(co->co_name),
1714 PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars)));
1715 Py_FatalError("compiler_make_closure()");
1716 }
1717 ADDOP_I(c, LOAD_CLOSURE, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001719 flags |= 0x08;
1720 ADDOP_I(c, BUILD_TUPLE, free);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001723 ADDOP_O(c, LOAD_CONST, qualname, consts);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001724 ADDOP_I(c, MAKE_FUNCTION, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726}
1727
1728static int
1729compiler_decorators(struct compiler *c, asdl_seq* decos)
1730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 if (!decos)
1734 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1737 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1738 }
1739 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740}
1741
1742static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001743compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 asdl_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001745{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001746 /* Push a dict of keyword-only default values.
1747
1748 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
1749 */
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001750 int i;
1751 PyObject *keys = NULL;
1752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1754 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1755 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1756 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04001757 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001758 if (!mangled) {
1759 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001761 if (keys == NULL) {
1762 keys = PyList_New(1);
1763 if (keys == NULL) {
1764 Py_DECREF(mangled);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001765 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001766 }
1767 PyList_SET_ITEM(keys, 0, mangled);
1768 }
1769 else {
1770 int res = PyList_Append(keys, mangled);
1771 Py_DECREF(mangled);
1772 if (res == -1) {
1773 goto error;
1774 }
1775 }
1776 if (!compiler_visit_expr(c, default_)) {
1777 goto error;
1778 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 }
1780 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001781 if (keys != NULL) {
1782 Py_ssize_t default_count = PyList_GET_SIZE(keys);
1783 PyObject *keys_tuple = PyList_AsTuple(keys);
1784 Py_DECREF(keys);
1785 if (keys_tuple == NULL) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001786 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001787 }
1788 ADDOP_N(c, LOAD_CONST, keys_tuple, consts);
1789 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001790 assert(default_count > 0);
1791 return 1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001792 }
1793 else {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001794 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001795 }
1796
1797error:
1798 Py_XDECREF(keys);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001799 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001800}
1801
1802static int
Guido van Rossum95e4d582018-01-26 08:20:18 -08001803compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
1804{
1805 PyObject *ann_as_str;
1806 ann_as_str = _PyAST_ExprAsUnicode(annotation, 1);
1807 if (!ann_as_str) {
1808 return 0;
1809 }
1810 ADDOP_N(c, LOAD_CONST, ann_as_str, consts);
1811 return 1;
1812}
1813
1814static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001815compiler_visit_argannotation(struct compiler *c, identifier id,
1816 expr_ty annotation, PyObject *names)
1817{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 if (annotation) {
Victor Stinner065efc32014-02-18 22:07:56 +01001819 PyObject *mangled;
Guido van Rossum95e4d582018-01-26 08:20:18 -08001820 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
1821 VISIT(c, annexpr, annotation)
1822 }
1823 else {
1824 VISIT(c, expr, annotation);
1825 }
Victor Stinner065efc32014-02-18 22:07:56 +01001826 mangled = _Py_Mangle(c->u->u_private, id);
Yury Selivanov34ce99f2014-02-18 12:49:41 -05001827 if (!mangled)
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001828 return 0;
Yury Selivanov34ce99f2014-02-18 12:49:41 -05001829 if (PyList_Append(names, mangled) < 0) {
1830 Py_DECREF(mangled);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001831 return 0;
Yury Selivanov34ce99f2014-02-18 12:49:41 -05001832 }
1833 Py_DECREF(mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001835 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001836}
1837
1838static int
1839compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1840 PyObject *names)
1841{
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001842 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 for (i = 0; i < asdl_seq_LEN(args); i++) {
1844 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001845 if (!compiler_visit_argannotation(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846 c,
1847 arg->arg,
1848 arg->annotation,
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001849 names))
1850 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001852 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001853}
1854
1855static int
1856compiler_visit_annotations(struct compiler *c, arguments_ty args,
1857 expr_ty returns)
1858{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001859 /* Push arg annotation dict.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001860 The expressions are evaluated out-of-order wrt the source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00001861
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001862 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 */
1864 static identifier return_str;
1865 PyObject *names;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001866 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 names = PyList_New(0);
1868 if (!names)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03001869 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001870
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001871 if (!compiler_visit_argannotations(c, args->args, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001873 if (args->vararg && args->vararg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001874 !compiler_visit_argannotation(c, args->vararg->arg,
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001875 args->vararg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 goto error;
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001877 if (!compiler_visit_argannotations(c, args->kwonlyargs, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001879 if (args->kwarg && args->kwarg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001880 !compiler_visit_argannotation(c, args->kwarg->arg,
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001881 args->kwarg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 if (!return_str) {
1885 return_str = PyUnicode_InternFromString("return");
1886 if (!return_str)
1887 goto error;
1888 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001889 if (!compiler_visit_argannotation(c, return_str, returns, names)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 goto error;
1891 }
1892
1893 len = PyList_GET_SIZE(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 if (len) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001895 PyObject *keytuple = PyList_AsTuple(names);
1896 Py_DECREF(names);
1897 if (keytuple == NULL) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001898 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001900 ADDOP_N(c, LOAD_CONST, keytuple, consts);
1901 ADDOP_I(c, BUILD_CONST_KEY_MAP, len);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001902 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001904 else {
1905 Py_DECREF(names);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001906 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001907 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001908
1909error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 Py_DECREF(names);
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03001911 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001912}
1913
1914static int
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001915compiler_visit_defaults(struct compiler *c, arguments_ty args)
1916{
1917 VISIT_SEQ(c, expr, args->defaults);
1918 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
1919 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001920}
1921
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001922static Py_ssize_t
1923compiler_default_arguments(struct compiler *c, arguments_ty args)
1924{
1925 Py_ssize_t funcflags = 0;
1926 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001927 if (!compiler_visit_defaults(c, args))
1928 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001929 funcflags |= 0x01;
1930 }
1931 if (args->kwonlyargs) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001932 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001933 args->kw_defaults);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001934 if (res == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001935 return -1;
1936 }
1937 else if (res > 0) {
1938 funcflags |= 0x02;
1939 }
1940 }
1941 return funcflags;
1942}
1943
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944static int
Yury Selivanov75445082015-05-11 22:57:16 -04001945compiler_function(struct compiler *c, stmt_ty s, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001946{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 PyCodeObject *co;
INADA Naokicb41b272017-02-23 00:31:59 +09001948 PyObject *qualname, *docstring = Py_None;
Yury Selivanov75445082015-05-11 22:57:16 -04001949 arguments_ty args;
1950 expr_ty returns;
1951 identifier name;
1952 asdl_seq* decos;
1953 asdl_seq *body;
INADA Naokicb41b272017-02-23 00:31:59 +09001954 Py_ssize_t i, funcflags;
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001955 int annotations;
Yury Selivanov75445082015-05-11 22:57:16 -04001956 int scope_type;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001957
Yury Selivanov75445082015-05-11 22:57:16 -04001958 if (is_async) {
1959 assert(s->kind == AsyncFunctionDef_kind);
1960
1961 args = s->v.AsyncFunctionDef.args;
1962 returns = s->v.AsyncFunctionDef.returns;
1963 decos = s->v.AsyncFunctionDef.decorator_list;
1964 name = s->v.AsyncFunctionDef.name;
1965 body = s->v.AsyncFunctionDef.body;
1966
1967 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
1968 } else {
1969 assert(s->kind == FunctionDef_kind);
1970
1971 args = s->v.FunctionDef.args;
1972 returns = s->v.FunctionDef.returns;
1973 decos = s->v.FunctionDef.decorator_list;
1974 name = s->v.FunctionDef.name;
1975 body = s->v.FunctionDef.body;
1976
1977 scope_type = COMPILER_SCOPE_FUNCTION;
1978 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 if (!compiler_decorators(c, decos))
1981 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001982
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001983 funcflags = compiler_default_arguments(c, args);
1984 if (funcflags == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001986 }
1987
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001988 annotations = compiler_visit_annotations(c, args, returns);
1989 if (annotations == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001990 return 0;
1991 }
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001992 else if (annotations > 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001993 funcflags |= 0x04;
1994 }
1995
1996 if (!compiler_enter_scope(c, name, scope_type, (void *)s, s->lineno)) {
1997 return 0;
1998 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999
INADA Naokicb41b272017-02-23 00:31:59 +09002000 /* if not -OO mode, add docstring */
2001 if (c->c_optimize < 2 && s->v.FunctionDef.docstring)
2002 docstring = s->v.FunctionDef.docstring;
2003 if (compiler_add_o(c, c->u->u_consts, docstring) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 compiler_exit_scope(c);
2005 return 0;
2006 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 c->u->u_argcount = asdl_seq_LEN(args->args);
2009 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 /* if there was a docstring, we need to skip the first statement */
INADA Naokicb41b272017-02-23 00:31:59 +09002011 VISIT_SEQ_IN_SCOPE(c, stmt, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002013 qualname = c->u->u_qualname;
2014 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002016 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002017 Py_XDECREF(qualname);
2018 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002020 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002022 compiler_make_closure(c, co, funcflags, qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002023 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 /* decorators */
2027 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2028 ADDOP_I(c, CALL_FUNCTION, 1);
2029 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002030
Yury Selivanov75445082015-05-11 22:57:16 -04002031 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002032}
2033
2034static int
2035compiler_class(struct compiler *c, stmt_ty s)
2036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 PyCodeObject *co;
2038 PyObject *str;
2039 int i;
2040 asdl_seq* decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 if (!compiler_decorators(c, decos))
2043 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 /* ultimately generate code for:
2046 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
2047 where:
2048 <func> is a function/closure created from the class body;
2049 it has a single argument (__locals__) where the dict
2050 (or MutableSequence) representing the locals is passed
2051 <name> is the class name
2052 <bases> is the positional arguments and *varargs argument
2053 <keywords> is the keyword arguments and **kwds argument
2054 This borrows from compiler_call.
2055 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002058 if (!compiler_enter_scope(c, s->v.ClassDef.name,
2059 COMPILER_SCOPE_CLASS, (void *)s, s->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 return 0;
2061 /* this block represents what we do in the new scope */
2062 {
2063 /* use the class name for name mangling */
2064 Py_INCREF(s->v.ClassDef.name);
Serhiy Storchaka48842712016-04-06 09:45:48 +03002065 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 /* load (global) __name__ ... */
2067 str = PyUnicode_InternFromString("__name__");
2068 if (!str || !compiler_nameop(c, str, Load)) {
2069 Py_XDECREF(str);
2070 compiler_exit_scope(c);
2071 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002072 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 Py_DECREF(str);
2074 /* ... and store it as __module__ */
2075 str = PyUnicode_InternFromString("__module__");
2076 if (!str || !compiler_nameop(c, str, Store)) {
2077 Py_XDECREF(str);
2078 compiler_exit_scope(c);
2079 return 0;
2080 }
2081 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002082 assert(c->u->u_qualname);
2083 ADDOP_O(c, LOAD_CONST, c->u->u_qualname, consts);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002084 str = PyUnicode_InternFromString("__qualname__");
2085 if (!str || !compiler_nameop(c, str, Store)) {
2086 Py_XDECREF(str);
2087 compiler_exit_scope(c);
2088 return 0;
2089 }
2090 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 /* compile the body proper */
INADA Naokicb41b272017-02-23 00:31:59 +09002092 if (!compiler_body(c, s->v.ClassDef.body, s->v.ClassDef.docstring)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 compiler_exit_scope(c);
2094 return 0;
2095 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002096 /* Return __classcell__ if it is referenced, otherwise return None */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002097 if (c->u->u_ste->ste_needs_class_closure) {
Nick Coghlan19d24672016-12-05 16:47:55 +10002098 /* Store __classcell__ into class namespace & return it */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002099 str = PyUnicode_InternFromString("__class__");
2100 if (str == NULL) {
2101 compiler_exit_scope(c);
2102 return 0;
2103 }
2104 i = compiler_lookup_arg(c->u->u_cellvars, str);
2105 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01002106 if (i < 0) {
2107 compiler_exit_scope(c);
2108 return 0;
2109 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002110 assert(i == 0);
Nick Coghlan944368e2016-09-11 14:45:49 +10002111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 ADDOP_I(c, LOAD_CLOSURE, i);
Nick Coghlan19d24672016-12-05 16:47:55 +10002113 ADDOP(c, DUP_TOP);
Nick Coghlan944368e2016-09-11 14:45:49 +10002114 str = PyUnicode_InternFromString("__classcell__");
2115 if (!str || !compiler_nameop(c, str, Store)) {
2116 Py_XDECREF(str);
2117 compiler_exit_scope(c);
2118 return 0;
2119 }
2120 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002122 else {
Nick Coghlan19d24672016-12-05 16:47:55 +10002123 /* No methods referenced __class__, so just return None */
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002124 assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
Nick Coghlan19d24672016-12-05 16:47:55 +10002125 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Benjamin Peterson312595c2013-05-15 15:26:42 -05002126 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002127 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002128 /* create the code object */
2129 co = assemble(c, 1);
2130 }
2131 /* leave the new scope */
2132 compiler_exit_scope(c);
2133 if (co == NULL)
2134 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 /* 2. load the 'build_class' function */
2137 ADDOP(c, LOAD_BUILD_CLASS);
2138
2139 /* 3. load a function (or closure) made from the code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002140 compiler_make_closure(c, co, 0, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002141 Py_DECREF(co);
2142
2143 /* 4. load class name */
2144 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
2145
2146 /* 5. generate the rest of the code for the call */
2147 if (!compiler_call_helper(c, 2,
2148 s->v.ClassDef.bases,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002149 s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 return 0;
2151
2152 /* 6. apply decorators */
2153 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2154 ADDOP_I(c, CALL_FUNCTION, 1);
2155 }
2156
2157 /* 7. store into <name> */
2158 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2159 return 0;
2160 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002161}
2162
2163static int
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002164cmpop(cmpop_ty op)
2165{
2166 switch (op) {
2167 case Eq:
2168 return PyCmp_EQ;
2169 case NotEq:
2170 return PyCmp_NE;
2171 case Lt:
2172 return PyCmp_LT;
2173 case LtE:
2174 return PyCmp_LE;
2175 case Gt:
2176 return PyCmp_GT;
2177 case GtE:
2178 return PyCmp_GE;
2179 case Is:
2180 return PyCmp_IS;
2181 case IsNot:
2182 return PyCmp_IS_NOT;
2183 case In:
2184 return PyCmp_IN;
2185 case NotIn:
2186 return PyCmp_NOT_IN;
2187 default:
2188 return PyCmp_BAD;
2189 }
2190}
2191
2192static int
2193compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2194{
2195 switch (e->kind) {
2196 case UnaryOp_kind:
2197 if (e->v.UnaryOp.op == Not)
2198 return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2199 /* fallback to general implementation */
2200 break;
2201 case BoolOp_kind: {
2202 asdl_seq *s = e->v.BoolOp.values;
2203 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2204 assert(n >= 0);
2205 int cond2 = e->v.BoolOp.op == Or;
2206 basicblock *next2 = next;
2207 if (!cond2 != !cond) {
2208 next2 = compiler_new_block(c);
2209 if (next2 == NULL)
2210 return 0;
2211 }
2212 for (i = 0; i < n; ++i) {
2213 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2214 return 0;
2215 }
2216 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2217 return 0;
2218 if (next2 != next)
2219 compiler_use_next_block(c, next2);
2220 return 1;
2221 }
2222 case IfExp_kind: {
2223 basicblock *end, *next2;
2224 end = compiler_new_block(c);
2225 if (end == NULL)
2226 return 0;
2227 next2 = compiler_new_block(c);
2228 if (next2 == NULL)
2229 return 0;
2230 if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2231 return 0;
2232 if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2233 return 0;
2234 ADDOP_JREL(c, JUMP_FORWARD, end);
2235 compiler_use_next_block(c, next2);
2236 if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2237 return 0;
2238 compiler_use_next_block(c, end);
2239 return 1;
2240 }
2241 case Compare_kind: {
2242 Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2243 if (n > 0) {
2244 basicblock *cleanup = compiler_new_block(c);
2245 if (cleanup == NULL)
2246 return 0;
2247 VISIT(c, expr, e->v.Compare.left);
2248 for (i = 0; i < n; i++) {
2249 VISIT(c, expr,
2250 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2251 ADDOP(c, DUP_TOP);
2252 ADDOP(c, ROT_THREE);
2253 ADDOP_I(c, COMPARE_OP,
2254 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, i))));
2255 ADDOP_JABS(c, POP_JUMP_IF_FALSE, cleanup);
2256 NEXT_BLOCK(c);
2257 }
2258 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
2259 ADDOP_I(c, COMPARE_OP,
2260 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n))));
2261 ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
2262 basicblock *end = compiler_new_block(c);
2263 if (end == NULL)
2264 return 0;
2265 ADDOP_JREL(c, JUMP_FORWARD, end);
2266 compiler_use_next_block(c, cleanup);
2267 ADDOP(c, POP_TOP);
2268 if (!cond) {
2269 ADDOP_JREL(c, JUMP_FORWARD, next);
2270 }
2271 compiler_use_next_block(c, end);
2272 return 1;
2273 }
2274 /* fallback to general implementation */
2275 break;
2276 }
2277 default:
2278 /* fallback to general implementation */
2279 break;
2280 }
2281
2282 /* general implementation */
2283 VISIT(c, expr, e);
2284 ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
2285 return 1;
2286}
2287
2288static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002289compiler_ifexp(struct compiler *c, expr_ty e)
2290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 basicblock *end, *next;
2292
2293 assert(e->kind == IfExp_kind);
2294 end = compiler_new_block(c);
2295 if (end == NULL)
2296 return 0;
2297 next = compiler_new_block(c);
2298 if (next == NULL)
2299 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002300 if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2301 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 VISIT(c, expr, e->v.IfExp.body);
2303 ADDOP_JREL(c, JUMP_FORWARD, end);
2304 compiler_use_next_block(c, next);
2305 VISIT(c, expr, e->v.IfExp.orelse);
2306 compiler_use_next_block(c, end);
2307 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002308}
2309
2310static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002311compiler_lambda(struct compiler *c, expr_ty e)
2312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002314 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 static identifier name;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002316 Py_ssize_t funcflags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 arguments_ty args = e->v.Lambda.args;
2318 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 if (!name) {
2321 name = PyUnicode_InternFromString("<lambda>");
2322 if (!name)
2323 return 0;
2324 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002325
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002326 funcflags = compiler_default_arguments(c, args);
2327 if (funcflags == -1) {
2328 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002329 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002330
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002331 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002332 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002333 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 /* Make None the first constant, so the lambda can't have a
2336 docstring. */
2337 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0)
2338 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 c->u->u_argcount = asdl_seq_LEN(args->args);
2341 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2342 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2343 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002344 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 }
2346 else {
2347 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002348 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002350 qualname = c->u->u_qualname;
2351 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002353 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002355
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002356 compiler_make_closure(c, co, funcflags, qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002357 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 Py_DECREF(co);
2359
2360 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002361}
2362
2363static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002364compiler_if(struct compiler *c, stmt_ty s)
2365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 basicblock *end, *next;
2367 int constant;
2368 assert(s->kind == If_kind);
2369 end = compiler_new_block(c);
2370 if (end == NULL)
2371 return 0;
2372
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02002373 constant = expr_constant(s->v.If.test);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 /* constant = 0: "if 0"
2375 * constant = 1: "if 1", "if 2", ...
2376 * constant = -1: rest */
2377 if (constant == 0) {
2378 if (s->v.If.orelse)
2379 VISIT_SEQ(c, stmt, s->v.If.orelse);
2380 } else if (constant == 1) {
2381 VISIT_SEQ(c, stmt, s->v.If.body);
2382 } else {
Antoine Pitroue7811fc2014-09-18 03:06:50 +02002383 if (asdl_seq_LEN(s->v.If.orelse)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002384 next = compiler_new_block(c);
2385 if (next == NULL)
2386 return 0;
2387 }
2388 else
2389 next = end;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002390 if (!compiler_jump_if(c, s->v.If.test, next, 0))
2391 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 VISIT_SEQ(c, stmt, s->v.If.body);
Antoine Pitroue7811fc2014-09-18 03:06:50 +02002393 if (asdl_seq_LEN(s->v.If.orelse)) {
2394 ADDOP_JREL(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 compiler_use_next_block(c, next);
2396 VISIT_SEQ(c, stmt, s->v.If.orelse);
2397 }
2398 }
2399 compiler_use_next_block(c, end);
2400 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002401}
2402
2403static int
2404compiler_for(struct compiler *c, stmt_ty s)
2405{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406 basicblock *start, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 start = compiler_new_block(c);
2409 cleanup = compiler_new_block(c);
2410 end = compiler_new_block(c);
2411 if (start == NULL || end == NULL || cleanup == NULL)
2412 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002413
2414 if (!compiler_push_fblock(c, FOR_LOOP, start, end))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 VISIT(c, expr, s->v.For.iter);
2418 ADDOP(c, GET_ITER);
2419 compiler_use_next_block(c, start);
2420 ADDOP_JREL(c, FOR_ITER, cleanup);
2421 VISIT(c, expr, s->v.For.target);
2422 VISIT_SEQ(c, stmt, s->v.For.body);
2423 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2424 compiler_use_next_block(c, cleanup);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002425
2426 compiler_pop_fblock(c, FOR_LOOP, start);
2427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 VISIT_SEQ(c, stmt, s->v.For.orelse);
2429 compiler_use_next_block(c, end);
2430 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002431}
2432
Yury Selivanov75445082015-05-11 22:57:16 -04002433
2434static int
2435compiler_async_for(struct compiler *c, stmt_ty s)
2436{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002437 _Py_IDENTIFIER(StopAsyncIteration);
2438
Yury Selivanov75445082015-05-11 22:57:16 -04002439 basicblock *try, *except, *end, *after_try, *try_cleanup,
2440 *after_loop, *after_loop_else;
2441
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002442 PyObject *stop_aiter_error = _PyUnicode_FromId(&PyId_StopAsyncIteration);
2443 if (stop_aiter_error == NULL) {
2444 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04002445 }
2446
2447 try = compiler_new_block(c);
2448 except = compiler_new_block(c);
2449 end = compiler_new_block(c);
2450 after_try = compiler_new_block(c);
2451 try_cleanup = compiler_new_block(c);
2452 after_loop = compiler_new_block(c);
2453 after_loop_else = compiler_new_block(c);
2454
2455 if (try == NULL || except == NULL || end == NULL
2456 || after_try == NULL || try_cleanup == NULL)
2457 return 0;
2458
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002459 if (!compiler_push_fblock(c, FOR_LOOP, try, after_loop))
Yury Selivanov75445082015-05-11 22:57:16 -04002460 return 0;
2461
2462 VISIT(c, expr, s->v.AsyncFor.iter);
2463 ADDOP(c, GET_AITER);
Yury Selivanov75445082015-05-11 22:57:16 -04002464
2465 compiler_use_next_block(c, try);
2466
2467
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002468 /* SETUP_FINALLY to guard the __anext__ call */
2469 ADDOP_JREL(c, SETUP_FINALLY, except);
2470 if (!compiler_push_fblock(c, EXCEPT, try, NULL))
Yury Selivanov75445082015-05-11 22:57:16 -04002471 return 0;
2472
2473 ADDOP(c, GET_ANEXT);
2474 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2475 ADDOP(c, YIELD_FROM);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002476 ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */
Serhiy Storchaka24d32012018-03-10 18:22:34 +02002477 VISIT(c, expr, s->v.AsyncFor.target);
Yury Selivanov75445082015-05-11 22:57:16 -04002478 compiler_pop_fblock(c, EXCEPT, try);
2479 ADDOP_JREL(c, JUMP_FORWARD, after_try);
2480
2481
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002482 /* Except block for __anext__ */
Yury Selivanov75445082015-05-11 22:57:16 -04002483 compiler_use_next_block(c, except);
2484 ADDOP(c, DUP_TOP);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002485 ADDOP_O(c, LOAD_GLOBAL, stop_aiter_error, names);
Yury Selivanov75445082015-05-11 22:57:16 -04002486 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2487 ADDOP_JABS(c, POP_JUMP_IF_FALSE, try_cleanup);
2488
2489 ADDOP(c, POP_TOP);
2490 ADDOP(c, POP_TOP);
2491 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002492 ADDOP(c, POP_EXCEPT); /* for SETUP_FINALLY */
2493 ADDOP(c, POP_TOP); /* pop iterator from stack */
Yury Selivanov75445082015-05-11 22:57:16 -04002494 ADDOP_JABS(c, JUMP_ABSOLUTE, after_loop_else);
2495
2496
2497 compiler_use_next_block(c, try_cleanup);
2498 ADDOP(c, END_FINALLY);
2499
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002500 /* Success block for __anext__ */
Yury Selivanov75445082015-05-11 22:57:16 -04002501 compiler_use_next_block(c, after_try);
2502 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
2503 ADDOP_JABS(c, JUMP_ABSOLUTE, try);
2504
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002505 compiler_pop_fblock(c, FOR_LOOP, try);
Yury Selivanov75445082015-05-11 22:57:16 -04002506
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002507 /* Block reached after `break`ing from loop */
Yury Selivanov75445082015-05-11 22:57:16 -04002508 compiler_use_next_block(c, after_loop);
2509 ADDOP_JABS(c, JUMP_ABSOLUTE, end);
2510
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002511 /* `else` block */
Yury Selivanov75445082015-05-11 22:57:16 -04002512 compiler_use_next_block(c, after_loop_else);
2513 VISIT_SEQ(c, stmt, s->v.For.orelse);
2514
2515 compiler_use_next_block(c, end);
2516
2517 return 1;
2518}
2519
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002520static int
2521compiler_while(struct compiler *c, stmt_ty s)
2522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 basicblock *loop, *orelse, *end, *anchor = NULL;
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02002524 int constant = expr_constant(s->v.While.test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002526 if (constant == 0) {
2527 if (s->v.While.orelse)
2528 VISIT_SEQ(c, stmt, s->v.While.orelse);
2529 return 1;
2530 }
2531 loop = compiler_new_block(c);
2532 end = compiler_new_block(c);
2533 if (constant == -1) {
2534 anchor = compiler_new_block(c);
2535 if (anchor == NULL)
2536 return 0;
2537 }
2538 if (loop == NULL || end == NULL)
2539 return 0;
2540 if (s->v.While.orelse) {
2541 orelse = compiler_new_block(c);
2542 if (orelse == NULL)
2543 return 0;
2544 }
2545 else
2546 orelse = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002548 compiler_use_next_block(c, loop);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002549 if (!compiler_push_fblock(c, WHILE_LOOP, loop, end))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550 return 0;
2551 if (constant == -1) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002552 if (!compiler_jump_if(c, s->v.While.test, anchor, 0))
2553 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002554 }
2555 VISIT_SEQ(c, stmt, s->v.While.body);
2556 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002558 /* XXX should the two POP instructions be in a separate block
2559 if there is no else clause ?
2560 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002561
Benjamin Peterson3cda0ed2014-12-13 16:06:19 -05002562 if (constant == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 compiler_use_next_block(c, anchor);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002564 compiler_pop_fblock(c, WHILE_LOOP, loop);
2565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002566 if (orelse != NULL) /* what if orelse is just pass? */
2567 VISIT_SEQ(c, stmt, s->v.While.orelse);
2568 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002570 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002571}
2572
2573static int
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002574compiler_return(struct compiler *c, stmt_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002575{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002576 int preserve_tos = ((s->v.Return.value != NULL) &&
2577 !is_const(s->v.Return.value));
2578 if (c->u->u_ste->ste_type != FunctionBlock)
2579 return compiler_error(c, "'return' outside function");
2580 if (s->v.Return.value != NULL &&
2581 c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2582 {
2583 return compiler_error(
2584 c, "'return' with value in async generator");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002586 if (preserve_tos) {
2587 VISIT(c, expr, s->v.Return.value);
2588 }
2589 for (int depth = c->u->u_nfblocks; depth--;) {
2590 struct fblockinfo *info = &c->u->u_fblock[depth];
2591
2592 if (!compiler_unwind_fblock(c, info, preserve_tos))
2593 return 0;
2594 }
2595 if (s->v.Return.value == NULL) {
2596 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2597 }
2598 else if (!preserve_tos) {
2599 VISIT(c, expr, s->v.Return.value);
2600 }
2601 ADDOP(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002603 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002604}
2605
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002606static int
2607compiler_break(struct compiler *c)
2608{
2609 for (int depth = c->u->u_nfblocks; depth--;) {
2610 struct fblockinfo *info = &c->u->u_fblock[depth];
2611
2612 if (!compiler_unwind_fblock(c, info, 0))
2613 return 0;
2614 if (info->fb_type == WHILE_LOOP || info->fb_type == FOR_LOOP) {
2615 ADDOP_JABS(c, JUMP_ABSOLUTE, info->fb_exit);
2616 return 1;
2617 }
2618 }
2619 return compiler_error(c, "'break' outside loop");
2620}
2621
2622static int
2623compiler_continue(struct compiler *c)
2624{
2625 for (int depth = c->u->u_nfblocks; depth--;) {
2626 struct fblockinfo *info = &c->u->u_fblock[depth];
2627
2628 if (info->fb_type == WHILE_LOOP || info->fb_type == FOR_LOOP) {
2629 ADDOP_JABS(c, JUMP_ABSOLUTE, info->fb_block);
2630 return 1;
2631 }
2632 if (info->fb_type == FINALLY_END) {
2633 return compiler_error(c,
2634 "'continue' not supported inside 'finally' clause");
2635 }
2636 if (!compiler_unwind_fblock(c, info, 0))
2637 return 0;
2638 }
2639 return compiler_error(c, "'continue' not properly in loop");
2640}
2641
2642
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002643/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002644
2645 SETUP_FINALLY L
2646 <code for body>
2647 POP_BLOCK
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002648 BEGIN_FINALLY
2649 L:
2650 <code for finalbody>
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651 END_FINALLY
2652
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002653 The special instructions use the block stack. Each block
2654 stack entry contains the instruction that created it (here
2655 SETUP_FINALLY), the level of the value stack at the time the
2656 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002657
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002658 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002659 Pushes the current value stack level and the label
2660 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661 POP_BLOCK:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002662 Pops en entry from the block stack.
2663 BEGIN_FINALLY
2664 Pushes NULL onto the value stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002665 END_FINALLY:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002666 Pops 1 (NULL or int) or 6 entries from the *value* stack and restore
2667 the raised and the caught exceptions they specify.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002669 The block stack is unwound when an exception is raised:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002670 when a SETUP_FINALLY entry is found, the raised and the caught
2671 exceptions are pushed onto the value stack (and the exception
2672 condition is cleared), and the interpreter jumps to the label
2673 gotten from the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002674*/
2675
2676static int
2677compiler_try_finally(struct compiler *c, stmt_ty s)
2678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 basicblock *body, *end;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002681 body = compiler_new_block(c);
2682 end = compiler_new_block(c);
2683 if (body == NULL || end == NULL)
2684 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002685
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002686 /* `try` block */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002687 ADDOP_JREL(c, SETUP_FINALLY, end);
2688 compiler_use_next_block(c, body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002689 if (!compiler_push_fblock(c, FINALLY_TRY, body, end))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002690 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002691 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
2692 if (!compiler_try_except(c, s))
2693 return 0;
2694 }
2695 else {
2696 VISIT_SEQ(c, stmt, s->v.Try.body);
2697 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002699 ADDOP(c, BEGIN_FINALLY);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 compiler_pop_fblock(c, FINALLY_TRY, body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002701
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002702 /* `finally` block */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002703 compiler_use_next_block(c, end);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002704 if (!compiler_push_fblock(c, FINALLY_END, end, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002705 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002706 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002707 ADDOP(c, END_FINALLY);
2708 compiler_pop_fblock(c, FINALLY_END, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002709 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002710}
2711
2712/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002713 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002714 (The contents of the value stack is shown in [], with the top
2715 at the right; 'tb' is trace-back info, 'val' the exception's
2716 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002717
2718 Value stack Label Instruction Argument
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002719 [] SETUP_FINALLY L1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002720 [] <code for S>
2721 [] POP_BLOCK
2722 [] JUMP_FORWARD L0
2723
2724 [tb, val, exc] L1: DUP )
2725 [tb, val, exc, exc] <evaluate E1> )
2726 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2727 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
2728 [tb, val, exc] POP
2729 [tb, val] <assign to V1> (or POP if no V1)
2730 [tb] POP
2731 [] <code for S1>
2732 JUMP_FORWARD L0
2733
2734 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002735 .............................etc.......................
2736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002737 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
2738
2739 [] L0: <next statement>
2740
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002741 Of course, parts are not generated if Vi or Ei is not present.
2742*/
2743static int
2744compiler_try_except(struct compiler *c, stmt_ty s)
2745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01002747 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002749 body = compiler_new_block(c);
2750 except = compiler_new_block(c);
2751 orelse = compiler_new_block(c);
2752 end = compiler_new_block(c);
2753 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
2754 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002755 ADDOP_JREL(c, SETUP_FINALLY, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 compiler_use_next_block(c, body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002757 if (!compiler_push_fblock(c, EXCEPT, body, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002758 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002759 VISIT_SEQ(c, stmt, s->v.Try.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 ADDOP(c, POP_BLOCK);
2761 compiler_pop_fblock(c, EXCEPT, body);
2762 ADDOP_JREL(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002763 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002764 compiler_use_next_block(c, except);
2765 for (i = 0; i < n; i++) {
2766 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002767 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002768 if (!handler->v.ExceptHandler.type && i < n-1)
2769 return compiler_error(c, "default 'except:' must be last");
2770 c->u->u_lineno_set = 0;
2771 c->u->u_lineno = handler->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00002772 c->u->u_col_offset = handler->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 except = compiler_new_block(c);
2774 if (except == NULL)
2775 return 0;
2776 if (handler->v.ExceptHandler.type) {
2777 ADDOP(c, DUP_TOP);
2778 VISIT(c, expr, handler->v.ExceptHandler.type);
2779 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2780 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
2781 }
2782 ADDOP(c, POP_TOP);
2783 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002784 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00002785
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002786 cleanup_end = compiler_new_block(c);
2787 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05002788 if (!(cleanup_end || cleanup_body))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002789 return 0;
Guido van Rossumb940e112007-01-10 16:19:56 +00002790
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002791 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
2792 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002794 /*
2795 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03002796 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002797 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03002798 try:
2799 # body
2800 finally:
2801 name = None
2802 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002803 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002805 /* second try: */
2806 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2807 compiler_use_next_block(c, cleanup_body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002808 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, cleanup_end))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002809 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002811 /* second # body */
2812 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2813 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002814 ADDOP(c, BEGIN_FINALLY);
2815 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002817 /* finally: */
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002818 compiler_use_next_block(c, cleanup_end);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002819 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end, NULL))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002820 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002822 /* name = None; del name */
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002823 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2824 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002825 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002827 ADDOP(c, END_FINALLY);
Serhiy Storchakad4864c62018-01-09 21:54:52 +02002828 ADDOP(c, POP_EXCEPT);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002829 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 }
2831 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002832 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002834 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05002835 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002836 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837
Guido van Rossumb940e112007-01-10 16:19:56 +00002838 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002839 ADDOP(c, POP_TOP);
2840 compiler_use_next_block(c, cleanup_body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002841 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002842 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002844 ADDOP(c, POP_EXCEPT);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002845 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 }
2847 ADDOP_JREL(c, JUMP_FORWARD, end);
2848 compiler_use_next_block(c, except);
2849 }
2850 ADDOP(c, END_FINALLY);
2851 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002852 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 compiler_use_next_block(c, end);
2854 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002855}
2856
2857static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002858compiler_try(struct compiler *c, stmt_ty s) {
2859 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
2860 return compiler_try_finally(c, s);
2861 else
2862 return compiler_try_except(c, s);
2863}
2864
2865
2866static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002867compiler_import_as(struct compiler *c, identifier name, identifier asname)
2868{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 /* The IMPORT_NAME opcode was already generated. This function
2870 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 If there is a dot in name, we need to split it and emit a
Serhiy Storchakaf93234b2017-05-09 22:31:05 +03002873 IMPORT_FROM for each name.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03002875 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
2876 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002877 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03002878 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002879 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 /* Consume the base module name to get the first attribute */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03002881 while (1) {
2882 Py_ssize_t pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 PyObject *attr;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03002884 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002885 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03002886 return 0;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03002887 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 if (!attr)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03002889 return 0;
Serhiy Storchakaf93234b2017-05-09 22:31:05 +03002890 ADDOP_O(c, IMPORT_FROM, attr, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 Py_DECREF(attr);
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03002892 if (dot == -1) {
2893 break;
2894 }
2895 ADDOP(c, ROT_TWO);
2896 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 }
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03002898 if (!compiler_nameop(c, asname, Store)) {
2899 return 0;
2900 }
2901 ADDOP(c, POP_TOP);
2902 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 }
2904 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002905}
2906
2907static int
2908compiler_import(struct compiler *c, stmt_ty s)
2909{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 /* The Import node stores a module name like a.b.c as a single
2911 string. This is convenient for all cases except
2912 import a.b.c as d
2913 where we need to parse that string to extract the individual
2914 module names.
2915 XXX Perhaps change the representation to make this case simpler?
2916 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01002917 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002919 for (i = 0; i < n; i++) {
2920 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
2921 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002922
Serhiy Storchakaba85d692017-03-30 09:09:41 +03002923 ADDOP_O(c, LOAD_CONST, _PyLong_Zero, consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2925 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 if (alias->asname) {
2928 r = compiler_import_as(c, alias->name, alias->asname);
2929 if (!r)
2930 return r;
2931 }
2932 else {
2933 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002934 Py_ssize_t dot = PyUnicode_FindChar(
2935 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02002936 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002937 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02002938 if (tmp == NULL)
2939 return 0;
2940 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002941 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002942 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 Py_DECREF(tmp);
2944 }
2945 if (!r)
2946 return r;
2947 }
2948 }
2949 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002950}
2951
2952static int
2953compiler_from_import(struct compiler *c, stmt_ty s)
2954{
Victor Stinnerad9a0662013-11-19 22:23:20 +01002955 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002957 PyObject *names = PyTuple_New(n);
2958 PyObject *level;
2959 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00002960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 if (!empty_string) {
2962 empty_string = PyUnicode_FromString("");
2963 if (!empty_string)
2964 return 0;
2965 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002967 if (!names)
2968 return 0;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002970 level = PyLong_FromLong(s->v.ImportFrom.level);
2971 if (!level) {
2972 Py_DECREF(names);
2973 return 0;
2974 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002976 /* build up the names */
2977 for (i = 0; i < n; i++) {
2978 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2979 Py_INCREF(alias->name);
2980 PyTuple_SET_ITEM(names, i, alias->name);
2981 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002983 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02002984 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002985 Py_DECREF(level);
2986 Py_DECREF(names);
2987 return compiler_error(c, "from __future__ imports must occur "
2988 "at the beginning of the file");
2989 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002991 ADDOP_O(c, LOAD_CONST, level, consts);
2992 Py_DECREF(level);
2993 ADDOP_O(c, LOAD_CONST, names, consts);
2994 Py_DECREF(names);
2995 if (s->v.ImportFrom.module) {
2996 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2997 }
2998 else {
2999 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
3000 }
3001 for (i = 0; i < n; i++) {
3002 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3003 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003004
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003005 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 assert(n == 1);
3007 ADDOP(c, IMPORT_STAR);
3008 return 1;
3009 }
3010
3011 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
3012 store_name = alias->name;
3013 if (alias->asname)
3014 store_name = alias->asname;
3015
3016 if (!compiler_nameop(c, store_name, Store)) {
3017 Py_DECREF(names);
3018 return 0;
3019 }
3020 }
3021 /* remove imported module */
3022 ADDOP(c, POP_TOP);
3023 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003024}
3025
3026static int
3027compiler_assert(struct compiler *c, stmt_ty s)
3028{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029 static PyObject *assertion_error = NULL;
3030 basicblock *end;
Victor Stinner14e461d2013-08-26 22:28:21 +02003031 PyObject* msg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003032
Georg Brandl8334fd92010-12-04 10:26:46 +00003033 if (c->c_optimize)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003034 return 1;
3035 if (assertion_error == NULL) {
3036 assertion_error = PyUnicode_InternFromString("AssertionError");
3037 if (assertion_error == NULL)
3038 return 0;
3039 }
3040 if (s->v.Assert.test->kind == Tuple_kind &&
3041 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
Victor Stinner14e461d2013-08-26 22:28:21 +02003042 msg = PyUnicode_FromString("assertion is always true, "
3043 "perhaps remove parentheses?");
3044 if (msg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003045 return 0;
Victor Stinner14e461d2013-08-26 22:28:21 +02003046 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
3047 c->c_filename, c->u->u_lineno,
3048 NULL, NULL) == -1) {
3049 Py_DECREF(msg);
3050 return 0;
3051 }
3052 Py_DECREF(msg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003053 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054 end = compiler_new_block(c);
3055 if (end == NULL)
3056 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003057 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
3058 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
3060 if (s->v.Assert.msg) {
3061 VISIT(c, expr, s->v.Assert.msg);
3062 ADDOP_I(c, CALL_FUNCTION, 1);
3063 }
3064 ADDOP_I(c, RAISE_VARARGS, 1);
3065 compiler_use_next_block(c, end);
3066 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003067}
3068
3069static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003070compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3071{
3072 if (c->c_interactive && c->c_nestlevel <= 1) {
3073 VISIT(c, expr, value);
3074 ADDOP(c, PRINT_EXPR);
3075 return 1;
3076 }
3077
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003078 if (is_const(value)) {
Victor Stinner15a30952016-02-08 22:45:06 +01003079 /* ignore constant statement */
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003080 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003081 }
3082
3083 VISIT(c, expr, value);
3084 ADDOP(c, POP_TOP);
3085 return 1;
3086}
3087
3088static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003089compiler_visit_stmt(struct compiler *c, stmt_ty s)
3090{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003091 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093 /* Always assign a lineno to the next instruction for a stmt. */
3094 c->u->u_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00003095 c->u->u_col_offset = s->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003096 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003098 switch (s->kind) {
3099 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04003100 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003101 case ClassDef_kind:
3102 return compiler_class(c, s);
3103 case Return_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003104 return compiler_return(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003105 case Delete_kind:
3106 VISIT_SEQ(c, expr, s->v.Delete.targets)
3107 break;
3108 case Assign_kind:
3109 n = asdl_seq_LEN(s->v.Assign.targets);
3110 VISIT(c, expr, s->v.Assign.value);
3111 for (i = 0; i < n; i++) {
3112 if (i < n - 1)
3113 ADDOP(c, DUP_TOP);
3114 VISIT(c, expr,
3115 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3116 }
3117 break;
3118 case AugAssign_kind:
3119 return compiler_augassign(c, s);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003120 case AnnAssign_kind:
3121 return compiler_annassign(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 case For_kind:
3123 return compiler_for(c, s);
3124 case While_kind:
3125 return compiler_while(c, s);
3126 case If_kind:
3127 return compiler_if(c, s);
3128 case Raise_kind:
3129 n = 0;
3130 if (s->v.Raise.exc) {
3131 VISIT(c, expr, s->v.Raise.exc);
3132 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003133 if (s->v.Raise.cause) {
3134 VISIT(c, expr, s->v.Raise.cause);
3135 n++;
3136 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003137 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01003138 ADDOP_I(c, RAISE_VARARGS, (int)n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003140 case Try_kind:
3141 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003142 case Assert_kind:
3143 return compiler_assert(c, s);
3144 case Import_kind:
3145 return compiler_import(c, s);
3146 case ImportFrom_kind:
3147 return compiler_from_import(c, s);
3148 case Global_kind:
3149 case Nonlocal_kind:
3150 break;
3151 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003152 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003153 case Pass_kind:
3154 break;
3155 case Break_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003156 return compiler_break(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003157 case Continue_kind:
3158 return compiler_continue(c);
3159 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003160 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04003161 case AsyncFunctionDef_kind:
3162 return compiler_function(c, s, 1);
3163 case AsyncWith_kind:
3164 return compiler_async_with(c, s, 0);
3165 case AsyncFor_kind:
3166 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003167 }
Yury Selivanov75445082015-05-11 22:57:16 -04003168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003169 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003170}
3171
3172static int
3173unaryop(unaryop_ty op)
3174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003175 switch (op) {
3176 case Invert:
3177 return UNARY_INVERT;
3178 case Not:
3179 return UNARY_NOT;
3180 case UAdd:
3181 return UNARY_POSITIVE;
3182 case USub:
3183 return UNARY_NEGATIVE;
3184 default:
3185 PyErr_Format(PyExc_SystemError,
3186 "unary op %d should not be possible", op);
3187 return 0;
3188 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003189}
3190
3191static int
3192binop(struct compiler *c, operator_ty op)
3193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003194 switch (op) {
3195 case Add:
3196 return BINARY_ADD;
3197 case Sub:
3198 return BINARY_SUBTRACT;
3199 case Mult:
3200 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003201 case MatMult:
3202 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003203 case Div:
3204 return BINARY_TRUE_DIVIDE;
3205 case Mod:
3206 return BINARY_MODULO;
3207 case Pow:
3208 return BINARY_POWER;
3209 case LShift:
3210 return BINARY_LSHIFT;
3211 case RShift:
3212 return BINARY_RSHIFT;
3213 case BitOr:
3214 return BINARY_OR;
3215 case BitXor:
3216 return BINARY_XOR;
3217 case BitAnd:
3218 return BINARY_AND;
3219 case FloorDiv:
3220 return BINARY_FLOOR_DIVIDE;
3221 default:
3222 PyErr_Format(PyExc_SystemError,
3223 "binary op %d should not be possible", op);
3224 return 0;
3225 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003226}
3227
3228static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003229inplace_binop(struct compiler *c, operator_ty op)
3230{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003231 switch (op) {
3232 case Add:
3233 return INPLACE_ADD;
3234 case Sub:
3235 return INPLACE_SUBTRACT;
3236 case Mult:
3237 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003238 case MatMult:
3239 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003240 case Div:
3241 return INPLACE_TRUE_DIVIDE;
3242 case Mod:
3243 return INPLACE_MODULO;
3244 case Pow:
3245 return INPLACE_POWER;
3246 case LShift:
3247 return INPLACE_LSHIFT;
3248 case RShift:
3249 return INPLACE_RSHIFT;
3250 case BitOr:
3251 return INPLACE_OR;
3252 case BitXor:
3253 return INPLACE_XOR;
3254 case BitAnd:
3255 return INPLACE_AND;
3256 case FloorDiv:
3257 return INPLACE_FLOOR_DIVIDE;
3258 default:
3259 PyErr_Format(PyExc_SystemError,
3260 "inplace binary op %d should not be possible", op);
3261 return 0;
3262 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003263}
3264
3265static int
3266compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3267{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003268 int op, scope;
3269 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003270 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 PyObject *dict = c->u->u_names;
3273 PyObject *mangled;
3274 /* XXX AugStore isn't used anywhere! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003275
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003276 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3277 !_PyUnicode_EqualToASCIIString(name, "True") &&
3278 !_PyUnicode_EqualToASCIIString(name, "False"));
Benjamin Peterson70b224d2012-12-06 17:49:58 -05003279
Serhiy Storchakabd6ec4d2017-12-18 14:29:12 +02003280 mangled = _Py_Mangle(c->u->u_private, name);
3281 if (!mangled)
3282 return 0;
3283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003284 op = 0;
3285 optype = OP_NAME;
3286 scope = PyST_GetScope(c->u->u_ste, mangled);
3287 switch (scope) {
3288 case FREE:
3289 dict = c->u->u_freevars;
3290 optype = OP_DEREF;
3291 break;
3292 case CELL:
3293 dict = c->u->u_cellvars;
3294 optype = OP_DEREF;
3295 break;
3296 case LOCAL:
3297 if (c->u->u_ste->ste_type == FunctionBlock)
3298 optype = OP_FAST;
3299 break;
3300 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04003301 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 optype = OP_GLOBAL;
3303 break;
3304 case GLOBAL_EXPLICIT:
3305 optype = OP_GLOBAL;
3306 break;
3307 default:
3308 /* scope can be 0 */
3309 break;
3310 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003312 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003313 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 switch (optype) {
3316 case OP_DEREF:
3317 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003318 case Load:
3319 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3320 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003321 case Store: op = STORE_DEREF; break;
3322 case AugLoad:
3323 case AugStore:
3324 break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003325 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 case Param:
3327 default:
3328 PyErr_SetString(PyExc_SystemError,
3329 "param invalid for deref variable");
3330 return 0;
3331 }
3332 break;
3333 case OP_FAST:
3334 switch (ctx) {
3335 case Load: op = LOAD_FAST; break;
3336 case Store: op = STORE_FAST; break;
3337 case Del: op = DELETE_FAST; break;
3338 case AugLoad:
3339 case AugStore:
3340 break;
3341 case Param:
3342 default:
3343 PyErr_SetString(PyExc_SystemError,
3344 "param invalid for local variable");
3345 return 0;
3346 }
3347 ADDOP_O(c, op, mangled, varnames);
3348 Py_DECREF(mangled);
3349 return 1;
3350 case OP_GLOBAL:
3351 switch (ctx) {
3352 case Load: op = LOAD_GLOBAL; break;
3353 case Store: op = STORE_GLOBAL; break;
3354 case Del: op = DELETE_GLOBAL; break;
3355 case AugLoad:
3356 case AugStore:
3357 break;
3358 case Param:
3359 default:
3360 PyErr_SetString(PyExc_SystemError,
3361 "param invalid for global variable");
3362 return 0;
3363 }
3364 break;
3365 case OP_NAME:
3366 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00003367 case Load: op = LOAD_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003368 case Store: op = STORE_NAME; break;
3369 case Del: op = DELETE_NAME; break;
3370 case AugLoad:
3371 case AugStore:
3372 break;
3373 case Param:
3374 default:
3375 PyErr_SetString(PyExc_SystemError,
3376 "param invalid for name variable");
3377 return 0;
3378 }
3379 break;
3380 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003382 assert(op);
3383 arg = compiler_add_o(c, dict, mangled);
3384 Py_DECREF(mangled);
3385 if (arg < 0)
3386 return 0;
3387 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003388}
3389
3390static int
3391compiler_boolop(struct compiler *c, expr_ty e)
3392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003393 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003394 int jumpi;
3395 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003396 asdl_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003398 assert(e->kind == BoolOp_kind);
3399 if (e->v.BoolOp.op == And)
3400 jumpi = JUMP_IF_FALSE_OR_POP;
3401 else
3402 jumpi = JUMP_IF_TRUE_OR_POP;
3403 end = compiler_new_block(c);
3404 if (end == NULL)
3405 return 0;
3406 s = e->v.BoolOp.values;
3407 n = asdl_seq_LEN(s) - 1;
3408 assert(n >= 0);
3409 for (i = 0; i < n; ++i) {
3410 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
3411 ADDOP_JABS(c, jumpi, end);
3412 }
3413 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3414 compiler_use_next_block(c, end);
3415 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003416}
3417
3418static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003419starunpack_helper(struct compiler *c, asdl_seq *elts,
3420 int single_op, int inner_op, int outer_op)
3421{
3422 Py_ssize_t n = asdl_seq_LEN(elts);
3423 Py_ssize_t i, nsubitems = 0, nseen = 0;
3424 for (i = 0; i < n; i++) {
3425 expr_ty elt = asdl_seq_GET(elts, i);
3426 if (elt->kind == Starred_kind) {
3427 if (nseen) {
3428 ADDOP_I(c, inner_op, nseen);
3429 nseen = 0;
3430 nsubitems++;
3431 }
3432 VISIT(c, expr, elt->v.Starred.value);
3433 nsubitems++;
3434 }
3435 else {
3436 VISIT(c, expr, elt);
3437 nseen++;
3438 }
3439 }
3440 if (nsubitems) {
3441 if (nseen) {
3442 ADDOP_I(c, inner_op, nseen);
3443 nsubitems++;
3444 }
3445 ADDOP_I(c, outer_op, nsubitems);
3446 }
3447 else
3448 ADDOP_I(c, single_op, nseen);
3449 return 1;
3450}
3451
3452static int
3453assignment_helper(struct compiler *c, asdl_seq *elts)
3454{
3455 Py_ssize_t n = asdl_seq_LEN(elts);
3456 Py_ssize_t i;
3457 int seen_star = 0;
3458 for (i = 0; i < n; i++) {
3459 expr_ty elt = asdl_seq_GET(elts, i);
3460 if (elt->kind == Starred_kind && !seen_star) {
3461 if ((i >= (1 << 8)) ||
3462 (n-i-1 >= (INT_MAX >> 8)))
3463 return compiler_error(c,
3464 "too many expressions in "
3465 "star-unpacking assignment");
3466 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3467 seen_star = 1;
3468 asdl_seq_SET(elts, i, elt->v.Starred.value);
3469 }
3470 else if (elt->kind == Starred_kind) {
3471 return compiler_error(c,
3472 "two starred expressions in assignment");
3473 }
3474 }
3475 if (!seen_star) {
3476 ADDOP_I(c, UNPACK_SEQUENCE, n);
3477 }
3478 VISIT_SEQ(c, expr, elts);
3479 return 1;
3480}
3481
3482static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003483compiler_list(struct compiler *c, expr_ty e)
3484{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003485 asdl_seq *elts = e->v.List.elts;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003486 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003487 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003489 else if (e->v.List.ctx == Load) {
3490 return starunpack_helper(c, elts,
3491 BUILD_LIST, BUILD_TUPLE, BUILD_LIST_UNPACK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003492 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003493 else
3494 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003495 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003496}
3497
3498static int
3499compiler_tuple(struct compiler *c, expr_ty e)
3500{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003501 asdl_seq *elts = e->v.Tuple.elts;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003502 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003503 return assignment_helper(c, elts);
3504 }
3505 else if (e->v.Tuple.ctx == Load) {
3506 return starunpack_helper(c, elts,
3507 BUILD_TUPLE, BUILD_TUPLE, BUILD_TUPLE_UNPACK);
3508 }
3509 else
3510 VISIT_SEQ(c, expr, elts);
3511 return 1;
3512}
3513
3514static int
3515compiler_set(struct compiler *c, expr_ty e)
3516{
3517 return starunpack_helper(c, e->v.Set.elts, BUILD_SET,
3518 BUILD_SET, BUILD_SET_UNPACK);
3519}
3520
3521static int
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003522are_all_items_const(asdl_seq *seq, Py_ssize_t begin, Py_ssize_t end)
3523{
3524 Py_ssize_t i;
3525 for (i = begin; i < end; i++) {
3526 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
3527 if (key == NULL || !is_const(key))
3528 return 0;
3529 }
3530 return 1;
3531}
3532
3533static int
3534compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3535{
3536 Py_ssize_t i, n = end - begin;
3537 PyObject *keys, *key;
3538 if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) {
3539 for (i = begin; i < end; i++) {
3540 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3541 }
3542 keys = PyTuple_New(n);
3543 if (keys == NULL) {
3544 return 0;
3545 }
3546 for (i = begin; i < end; i++) {
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02003547 key = get_const_value((expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003548 Py_INCREF(key);
3549 PyTuple_SET_ITEM(keys, i - begin, key);
3550 }
3551 ADDOP_N(c, LOAD_CONST, keys, consts);
3552 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3553 }
3554 else {
3555 for (i = begin; i < end; i++) {
3556 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3557 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3558 }
3559 ADDOP_I(c, BUILD_MAP, n);
3560 }
3561 return 1;
3562}
3563
3564static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003565compiler_dict(struct compiler *c, expr_ty e)
3566{
Victor Stinner976bb402016-03-23 11:36:19 +01003567 Py_ssize_t i, n, elements;
3568 int containers;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003569 int is_unpacking = 0;
3570 n = asdl_seq_LEN(e->v.Dict.values);
3571 containers = 0;
3572 elements = 0;
3573 for (i = 0; i < n; i++) {
3574 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
3575 if (elements == 0xFFFF || (elements && is_unpacking)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003576 if (!compiler_subdict(c, e, i - elements, i))
3577 return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003578 containers++;
3579 elements = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003580 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003581 if (is_unpacking) {
3582 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3583 containers++;
3584 }
3585 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003586 elements++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003587 }
3588 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003589 if (elements || containers == 0) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003590 if (!compiler_subdict(c, e, n - elements, n))
3591 return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003592 containers++;
3593 }
3594 /* If there is more than one dict, they need to be merged into a new
3595 * dict. If there is one dict and it's an unpacking, then it needs
3596 * to be copied into a new dict." */
Serhiy Storchaka3d85fae2016-11-28 20:56:37 +02003597 if (containers > 1 || is_unpacking) {
3598 ADDOP_I(c, BUILD_MAP_UNPACK, containers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003599 }
3600 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003601}
3602
3603static int
3604compiler_compare(struct compiler *c, expr_ty e)
3605{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003606 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 VISIT(c, expr, e->v.Compare.left);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003609 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
3610 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
3611 if (n == 0) {
3612 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
3613 ADDOP_I(c, COMPARE_OP,
3614 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, 0))));
3615 }
3616 else {
3617 basicblock *cleanup = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003618 if (cleanup == NULL)
3619 return 0;
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003620 for (i = 0; i < n; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003621 VISIT(c, expr,
3622 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003623 ADDOP(c, DUP_TOP);
3624 ADDOP(c, ROT_THREE);
3625 ADDOP_I(c, COMPARE_OP,
3626 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, i))));
3627 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
3628 NEXT_BLOCK(c);
3629 }
3630 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
3631 ADDOP_I(c, COMPARE_OP,
3632 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n))));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003633 basicblock *end = compiler_new_block(c);
3634 if (end == NULL)
3635 return 0;
3636 ADDOP_JREL(c, JUMP_FORWARD, end);
3637 compiler_use_next_block(c, cleanup);
3638 ADDOP(c, ROT_TWO);
3639 ADDOP(c, POP_TOP);
3640 compiler_use_next_block(c, end);
3641 }
3642 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003643}
3644
3645static int
Yury Selivanovf2392132016-12-13 19:03:51 -05003646maybe_optimize_method_call(struct compiler *c, expr_ty e)
3647{
3648 Py_ssize_t argsl, i;
3649 expr_ty meth = e->v.Call.func;
3650 asdl_seq *args = e->v.Call.args;
3651
3652 /* Check that the call node is an attribute access, and that
3653 the call doesn't have keyword parameters. */
3654 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
3655 asdl_seq_LEN(e->v.Call.keywords))
3656 return -1;
3657
3658 /* Check that there are no *varargs types of arguments. */
3659 argsl = asdl_seq_LEN(args);
3660 for (i = 0; i < argsl; i++) {
3661 expr_ty elt = asdl_seq_GET(args, i);
3662 if (elt->kind == Starred_kind) {
3663 return -1;
3664 }
3665 }
3666
3667 /* Alright, we can optimize the code. */
3668 VISIT(c, expr, meth->v.Attribute.value);
3669 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
3670 VISIT_SEQ(c, expr, e->v.Call.args);
3671 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
3672 return 1;
3673}
3674
3675static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003676compiler_call(struct compiler *c, expr_ty e)
3677{
Yury Selivanovf2392132016-12-13 19:03:51 -05003678 if (maybe_optimize_method_call(c, e) > 0)
3679 return 1;
3680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003681 VISIT(c, expr, e->v.Call.func);
3682 return compiler_call_helper(c, 0,
3683 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003684 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003685}
3686
Eric V. Smith235a6f02015-09-19 14:51:32 -04003687static int
3688compiler_joined_str(struct compiler *c, expr_ty e)
3689{
Eric V. Smith235a6f02015-09-19 14:51:32 -04003690 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02003691 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1)
3692 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
Eric V. Smith235a6f02015-09-19 14:51:32 -04003693 return 1;
3694}
3695
Eric V. Smitha78c7952015-11-03 12:45:05 -05003696/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04003697static int
3698compiler_formatted_value(struct compiler *c, expr_ty e)
3699{
Eric V. Smitha78c7952015-11-03 12:45:05 -05003700 /* Our oparg encodes 2 pieces of information: the conversion
3701 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04003702
Eric V. Smitha78c7952015-11-03 12:45:05 -05003703 Convert the conversion char to 2 bits:
3704 None: 000 0x0 FVC_NONE
3705 !s : 001 0x1 FVC_STR
3706 !r : 010 0x2 FVC_REPR
3707 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04003708
Eric V. Smitha78c7952015-11-03 12:45:05 -05003709 next bit is whether or not we have a format spec:
3710 yes : 100 0x4
3711 no : 000 0x0
3712 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04003713
Eric V. Smitha78c7952015-11-03 12:45:05 -05003714 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003715
Eric V. Smitha78c7952015-11-03 12:45:05 -05003716 /* Evaluate the expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04003717 VISIT(c, expr, e->v.FormattedValue.value);
3718
Eric V. Smitha78c7952015-11-03 12:45:05 -05003719 switch (e->v.FormattedValue.conversion) {
3720 case 's': oparg = FVC_STR; break;
3721 case 'r': oparg = FVC_REPR; break;
3722 case 'a': oparg = FVC_ASCII; break;
3723 case -1: oparg = FVC_NONE; break;
3724 default:
3725 PyErr_SetString(PyExc_SystemError,
3726 "Unrecognized conversion character");
3727 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003728 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04003729 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003730 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04003731 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003732 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003733 }
3734
Eric V. Smitha78c7952015-11-03 12:45:05 -05003735 /* And push our opcode and oparg */
3736 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith235a6f02015-09-19 14:51:32 -04003737 return 1;
3738}
3739
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003740static int
3741compiler_subkwargs(struct compiler *c, asdl_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
3742{
3743 Py_ssize_t i, n = end - begin;
3744 keyword_ty kw;
3745 PyObject *keys, *key;
3746 assert(n > 0);
3747 if (n > 1) {
3748 for (i = begin; i < end; i++) {
3749 kw = asdl_seq_GET(keywords, i);
3750 VISIT(c, expr, kw->value);
3751 }
3752 keys = PyTuple_New(n);
3753 if (keys == NULL) {
3754 return 0;
3755 }
3756 for (i = begin; i < end; i++) {
3757 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
3758 Py_INCREF(key);
3759 PyTuple_SET_ITEM(keys, i - begin, key);
3760 }
3761 ADDOP_N(c, LOAD_CONST, keys, consts);
3762 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3763 }
3764 else {
3765 /* a for loop only executes once */
3766 for (i = begin; i < end; i++) {
3767 kw = asdl_seq_GET(keywords, i);
3768 ADDOP_O(c, LOAD_CONST, kw->arg, consts);
3769 VISIT(c, expr, kw->value);
3770 }
3771 ADDOP_I(c, BUILD_MAP, n);
3772 }
3773 return 1;
3774}
3775
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003776/* shared code between compiler_call and compiler_class */
3777static int
3778compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01003779 int n, /* Args already pushed */
Victor Stinnerad9a0662013-11-19 22:23:20 +01003780 asdl_seq *args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003781 asdl_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003782{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003783 Py_ssize_t i, nseen, nelts, nkwelts;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003784 int mustdictunpack = 0;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003785
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003786 /* the number of tuples and dictionaries on the stack */
3787 Py_ssize_t nsubargs = 0, nsubkwargs = 0;
3788
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003789 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003790 nkwelts = asdl_seq_LEN(keywords);
3791
3792 for (i = 0; i < nkwelts; i++) {
3793 keyword_ty kw = asdl_seq_GET(keywords, i);
3794 if (kw->arg == NULL) {
3795 mustdictunpack = 1;
3796 break;
3797 }
3798 }
3799
3800 nseen = n; /* the number of positional arguments on the stack */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003801 for (i = 0; i < nelts; i++) {
3802 expr_ty elt = asdl_seq_GET(args, i);
3803 if (elt->kind == Starred_kind) {
3804 /* A star-arg. If we've seen positional arguments,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003805 pack the positional arguments into a tuple. */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003806 if (nseen) {
3807 ADDOP_I(c, BUILD_TUPLE, nseen);
3808 nseen = 0;
3809 nsubargs++;
3810 }
3811 VISIT(c, expr, elt->v.Starred.value);
3812 nsubargs++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003813 }
3814 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003815 VISIT(c, expr, elt);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003816 nseen++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003817 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003818 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003819
3820 /* Same dance again for keyword arguments */
Serhiy Storchakab7281052016-09-12 00:52:40 +03003821 if (nsubargs || mustdictunpack) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003822 if (nseen) {
3823 /* Pack up any trailing positional arguments. */
3824 ADDOP_I(c, BUILD_TUPLE, nseen);
3825 nsubargs++;
3826 }
Serhiy Storchakab7281052016-09-12 00:52:40 +03003827 if (nsubargs > 1) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003828 /* If we ended up with more than one stararg, we need
3829 to concatenate them into a single sequence. */
Serhiy Storchaka73442852016-10-02 10:33:46 +03003830 ADDOP_I(c, BUILD_TUPLE_UNPACK_WITH_CALL, nsubargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003831 }
3832 else if (nsubargs == 0) {
3833 ADDOP_I(c, BUILD_TUPLE, 0);
3834 }
3835 nseen = 0; /* the number of keyword arguments on the stack following */
3836 for (i = 0; i < nkwelts; i++) {
3837 keyword_ty kw = asdl_seq_GET(keywords, i);
3838 if (kw->arg == NULL) {
3839 /* A keyword argument unpacking. */
3840 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003841 if (!compiler_subkwargs(c, keywords, i - nseen, i))
3842 return 0;
3843 nsubkwargs++;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003844 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003845 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003846 VISIT(c, expr, kw->value);
3847 nsubkwargs++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003848 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003849 else {
3850 nseen++;
3851 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003852 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003853 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003854 /* Pack up any trailing keyword arguments. */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003855 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts))
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003856 return 0;
3857 nsubkwargs++;
3858 }
Serhiy Storchakab7281052016-09-12 00:52:40 +03003859 if (nsubkwargs > 1) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003860 /* Pack it all up */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003861 ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003862 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003863 ADDOP_I(c, CALL_FUNCTION_EX, nsubkwargs > 0);
3864 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003865 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003866 else if (nkwelts) {
3867 PyObject *names;
3868 VISIT_SEQ(c, keyword, keywords);
3869 names = PyTuple_New(nkwelts);
3870 if (names == NULL) {
3871 return 0;
3872 }
3873 for (i = 0; i < nkwelts; i++) {
3874 keyword_ty kw = asdl_seq_GET(keywords, i);
3875 Py_INCREF(kw->arg);
3876 PyTuple_SET_ITEM(names, i, kw->arg);
3877 }
3878 ADDOP_N(c, LOAD_CONST, names, consts);
3879 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
3880 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003881 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003882 else {
3883 ADDOP_I(c, CALL_FUNCTION, n + nelts);
3884 return 1;
3885 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003886}
3887
Nick Coghlan650f0d02007-04-15 12:05:43 +00003888
3889/* List and set comprehensions and generator expressions work by creating a
3890 nested function to perform the actual iteration. This means that the
3891 iteration variables don't leak into the current scope.
3892 The defined function is called immediately following its definition, with the
3893 result of that call being the result of the expression.
3894 The LC/SC version returns the populated container, while the GE version is
3895 flagged in symtable.c as a generator, so it returns the generator object
3896 when the function is called.
Nick Coghlan650f0d02007-04-15 12:05:43 +00003897
3898 Possible cleanups:
3899 - iterate over the generator sequence instead of using recursion
3900*/
3901
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003902
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003903static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003904compiler_comprehension_generator(struct compiler *c,
3905 asdl_seq *generators, int gen_index,
3906 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003907{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003908 comprehension_ty gen;
3909 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
3910 if (gen->is_async) {
3911 return compiler_async_comprehension_generator(
3912 c, generators, gen_index, elt, val, type);
3913 } else {
3914 return compiler_sync_comprehension_generator(
3915 c, generators, gen_index, elt, val, type);
3916 }
3917}
3918
3919static int
3920compiler_sync_comprehension_generator(struct compiler *c,
3921 asdl_seq *generators, int gen_index,
3922 expr_ty elt, expr_ty val, int type)
3923{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003924 /* generate code for the iterator, then each of the ifs,
3925 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003927 comprehension_ty gen;
3928 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003929 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003931 start = compiler_new_block(c);
3932 skip = compiler_new_block(c);
3933 if_cleanup = compiler_new_block(c);
3934 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003936 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3937 anchor == NULL)
3938 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003940 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003942 if (gen_index == 0) {
3943 /* Receive outermost iter as an implicit argument */
3944 c->u->u_argcount = 1;
3945 ADDOP_I(c, LOAD_FAST, 0);
3946 }
3947 else {
3948 /* Sub-iter - calculate on the fly */
3949 VISIT(c, expr, gen->iter);
3950 ADDOP(c, GET_ITER);
3951 }
3952 compiler_use_next_block(c, start);
3953 ADDOP_JREL(c, FOR_ITER, anchor);
3954 NEXT_BLOCK(c);
3955 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003957 /* XXX this needs to be cleaned up...a lot! */
3958 n = asdl_seq_LEN(gen->ifs);
3959 for (i = 0; i < n; i++) {
3960 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003961 if (!compiler_jump_if(c, e, if_cleanup, 0))
3962 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003963 NEXT_BLOCK(c);
3964 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003966 if (++gen_index < asdl_seq_LEN(generators))
3967 if (!compiler_comprehension_generator(c,
3968 generators, gen_index,
3969 elt, val, type))
3970 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003972 /* only append after the last for generator */
3973 if (gen_index >= asdl_seq_LEN(generators)) {
3974 /* comprehension specific code */
3975 switch (type) {
3976 case COMP_GENEXP:
3977 VISIT(c, expr, elt);
3978 ADDOP(c, YIELD_VALUE);
3979 ADDOP(c, POP_TOP);
3980 break;
3981 case COMP_LISTCOMP:
3982 VISIT(c, expr, elt);
3983 ADDOP_I(c, LIST_APPEND, gen_index + 1);
3984 break;
3985 case COMP_SETCOMP:
3986 VISIT(c, expr, elt);
3987 ADDOP_I(c, SET_ADD, gen_index + 1);
3988 break;
3989 case COMP_DICTCOMP:
3990 /* With 'd[k] = v', v is evaluated before k, so we do
3991 the same. */
3992 VISIT(c, expr, val);
3993 VISIT(c, expr, elt);
3994 ADDOP_I(c, MAP_ADD, gen_index + 1);
3995 break;
3996 default:
3997 return 0;
3998 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004000 compiler_use_next_block(c, skip);
4001 }
4002 compiler_use_next_block(c, if_cleanup);
4003 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
4004 compiler_use_next_block(c, anchor);
4005
4006 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004007}
4008
4009static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004010compiler_async_comprehension_generator(struct compiler *c,
4011 asdl_seq *generators, int gen_index,
4012 expr_ty elt, expr_ty val, int type)
4013{
4014 _Py_IDENTIFIER(StopAsyncIteration);
4015
4016 comprehension_ty gen;
4017 basicblock *anchor, *skip, *if_cleanup, *try,
4018 *after_try, *except, *try_cleanup;
4019 Py_ssize_t i, n;
4020
4021 PyObject *stop_aiter_error = _PyUnicode_FromId(&PyId_StopAsyncIteration);
4022 if (stop_aiter_error == NULL) {
4023 return 0;
4024 }
4025
4026 try = compiler_new_block(c);
4027 after_try = compiler_new_block(c);
4028 try_cleanup = compiler_new_block(c);
4029 except = compiler_new_block(c);
4030 skip = compiler_new_block(c);
4031 if_cleanup = compiler_new_block(c);
4032 anchor = compiler_new_block(c);
4033
4034 if (skip == NULL || if_cleanup == NULL || anchor == NULL ||
4035 try == NULL || after_try == NULL ||
4036 except == NULL || after_try == NULL) {
4037 return 0;
4038 }
4039
4040 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4041
4042 if (gen_index == 0) {
4043 /* Receive outermost iter as an implicit argument */
4044 c->u->u_argcount = 1;
4045 ADDOP_I(c, LOAD_FAST, 0);
4046 }
4047 else {
4048 /* Sub-iter - calculate on the fly */
4049 VISIT(c, expr, gen->iter);
4050 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004051 }
4052
4053 compiler_use_next_block(c, try);
4054
4055
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004056 ADDOP_JREL(c, SETUP_FINALLY, except);
4057 if (!compiler_push_fblock(c, EXCEPT, try, NULL))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004058 return 0;
4059
4060 ADDOP(c, GET_ANEXT);
4061 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4062 ADDOP(c, YIELD_FROM);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004063 ADDOP(c, POP_BLOCK);
Serhiy Storchaka24d32012018-03-10 18:22:34 +02004064 VISIT(c, expr, gen->target);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004065 compiler_pop_fblock(c, EXCEPT, try);
4066 ADDOP_JREL(c, JUMP_FORWARD, after_try);
4067
4068
4069 compiler_use_next_block(c, except);
4070 ADDOP(c, DUP_TOP);
4071 ADDOP_O(c, LOAD_GLOBAL, stop_aiter_error, names);
4072 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
4073 ADDOP_JABS(c, POP_JUMP_IF_FALSE, try_cleanup);
4074
4075 ADDOP(c, POP_TOP);
4076 ADDOP(c, POP_TOP);
4077 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004078 ADDOP(c, POP_EXCEPT); /* for SETUP_FINALLY */
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004079 ADDOP_JABS(c, JUMP_ABSOLUTE, anchor);
4080
4081
4082 compiler_use_next_block(c, try_cleanup);
4083 ADDOP(c, END_FINALLY);
4084
4085 compiler_use_next_block(c, after_try);
4086
4087 n = asdl_seq_LEN(gen->ifs);
4088 for (i = 0; i < n; i++) {
4089 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004090 if (!compiler_jump_if(c, e, if_cleanup, 0))
4091 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004092 NEXT_BLOCK(c);
4093 }
4094
4095 if (++gen_index < asdl_seq_LEN(generators))
4096 if (!compiler_comprehension_generator(c,
4097 generators, gen_index,
4098 elt, val, type))
4099 return 0;
4100
4101 /* only append after the last for generator */
4102 if (gen_index >= asdl_seq_LEN(generators)) {
4103 /* comprehension specific code */
4104 switch (type) {
4105 case COMP_GENEXP:
4106 VISIT(c, expr, elt);
4107 ADDOP(c, YIELD_VALUE);
4108 ADDOP(c, POP_TOP);
4109 break;
4110 case COMP_LISTCOMP:
4111 VISIT(c, expr, elt);
4112 ADDOP_I(c, LIST_APPEND, gen_index + 1);
4113 break;
4114 case COMP_SETCOMP:
4115 VISIT(c, expr, elt);
4116 ADDOP_I(c, SET_ADD, gen_index + 1);
4117 break;
4118 case COMP_DICTCOMP:
4119 /* With 'd[k] = v', v is evaluated before k, so we do
4120 the same. */
4121 VISIT(c, expr, val);
4122 VISIT(c, expr, elt);
4123 ADDOP_I(c, MAP_ADD, gen_index + 1);
4124 break;
4125 default:
4126 return 0;
4127 }
4128
4129 compiler_use_next_block(c, skip);
4130 }
4131 compiler_use_next_block(c, if_cleanup);
4132 ADDOP_JABS(c, JUMP_ABSOLUTE, try);
4133 compiler_use_next_block(c, anchor);
4134 ADDOP(c, POP_TOP);
4135
4136 return 1;
4137}
4138
4139static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004140compiler_comprehension(struct compiler *c, expr_ty e, int type,
4141 identifier name, asdl_seq *generators, expr_ty elt,
4142 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004144 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004145 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004146 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004147 int is_async_function = c->u->u_ste->ste_coroutine;
4148 int is_async_generator = 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004149
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004150 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004151
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004152 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4153 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004154 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004155 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004156 }
4157
4158 is_async_generator = c->u->u_ste->ste_coroutine;
4159
Yury Selivanovb8ab9d32017-10-06 02:58:28 -04004160 if (is_async_generator && !is_async_function && type != COMP_GENEXP) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004161 if (e->lineno > c->u->u_lineno) {
4162 c->u->u_lineno = e->lineno;
4163 c->u->u_lineno_set = 0;
4164 }
4165 compiler_error(c, "asynchronous comprehension outside of "
4166 "an asynchronous function");
4167 goto error_in_scope;
4168 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004170 if (type != COMP_GENEXP) {
4171 int op;
4172 switch (type) {
4173 case COMP_LISTCOMP:
4174 op = BUILD_LIST;
4175 break;
4176 case COMP_SETCOMP:
4177 op = BUILD_SET;
4178 break;
4179 case COMP_DICTCOMP:
4180 op = BUILD_MAP;
4181 break;
4182 default:
4183 PyErr_Format(PyExc_SystemError,
4184 "unknown comprehension type %d", type);
4185 goto error_in_scope;
4186 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004188 ADDOP_I(c, op, 0);
4189 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004191 if (!compiler_comprehension_generator(c, generators, 0, elt,
4192 val, type))
4193 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004195 if (type != COMP_GENEXP) {
4196 ADDOP(c, RETURN_VALUE);
4197 }
4198
4199 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004200 qualname = c->u->u_qualname;
4201 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004202 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004203 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004204 goto error;
4205
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004206 if (!compiler_make_closure(c, co, 0, qualname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004207 goto error;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004208 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004209 Py_DECREF(co);
4210
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004211 VISIT(c, expr, outermost->iter);
4212
4213 if (outermost->is_async) {
4214 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004215 } else {
4216 ADDOP(c, GET_ITER);
4217 }
4218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004219 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004220
4221 if (is_async_generator && type != COMP_GENEXP) {
4222 ADDOP(c, GET_AWAITABLE);
4223 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4224 ADDOP(c, YIELD_FROM);
4225 }
4226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004227 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004228error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004229 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004230error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004231 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004232 Py_XDECREF(co);
4233 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004234}
4235
4236static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004237compiler_genexp(struct compiler *c, expr_ty e)
4238{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004239 static identifier name;
4240 if (!name) {
4241 name = PyUnicode_FromString("<genexpr>");
4242 if (!name)
4243 return 0;
4244 }
4245 assert(e->kind == GeneratorExp_kind);
4246 return compiler_comprehension(c, e, COMP_GENEXP, name,
4247 e->v.GeneratorExp.generators,
4248 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004249}
4250
4251static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004252compiler_listcomp(struct compiler *c, expr_ty e)
4253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004254 static identifier name;
4255 if (!name) {
4256 name = PyUnicode_FromString("<listcomp>");
4257 if (!name)
4258 return 0;
4259 }
4260 assert(e->kind == ListComp_kind);
4261 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4262 e->v.ListComp.generators,
4263 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004264}
4265
4266static int
4267compiler_setcomp(struct compiler *c, expr_ty e)
4268{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004269 static identifier name;
4270 if (!name) {
4271 name = PyUnicode_FromString("<setcomp>");
4272 if (!name)
4273 return 0;
4274 }
4275 assert(e->kind == SetComp_kind);
4276 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4277 e->v.SetComp.generators,
4278 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004279}
4280
4281
4282static int
4283compiler_dictcomp(struct compiler *c, expr_ty e)
4284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004285 static identifier name;
4286 if (!name) {
4287 name = PyUnicode_FromString("<dictcomp>");
4288 if (!name)
4289 return 0;
4290 }
4291 assert(e->kind == DictComp_kind);
4292 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4293 e->v.DictComp.generators,
4294 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004295}
4296
4297
4298static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004299compiler_visit_keyword(struct compiler *c, keyword_ty k)
4300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004301 VISIT(c, expr, k->value);
4302 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004303}
4304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004305/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004306 whether they are true or false.
4307
4308 Return values: 1 for true, 0 for false, -1 for non-constant.
4309 */
4310
4311static int
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02004312expr_constant(expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004313{
Serhiy Storchaka3325a672017-12-15 12:35:48 +02004314 if (is_const(e)) {
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02004315 return PyObject_IsTrue(get_const_value(e));
Benjamin Peterson442f2092012-12-06 17:41:04 -05004316 }
Serhiy Storchaka3325a672017-12-15 12:35:48 +02004317 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004318}
4319
Yury Selivanov75445082015-05-11 22:57:16 -04004320
4321/*
4322 Implements the async with statement.
4323
4324 The semantics outlined in that PEP are as follows:
4325
4326 async with EXPR as VAR:
4327 BLOCK
4328
4329 It is implemented roughly as:
4330
4331 context = EXPR
4332 exit = context.__aexit__ # not calling it
4333 value = await context.__aenter__()
4334 try:
4335 VAR = value # if VAR present in the syntax
4336 BLOCK
4337 finally:
4338 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004339 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04004340 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004341 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04004342 if not (await exit(*exc)):
4343 raise
4344 */
4345static int
4346compiler_async_with(struct compiler *c, stmt_ty s, int pos)
4347{
4348 basicblock *block, *finally;
4349 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
4350
4351 assert(s->kind == AsyncWith_kind);
4352
4353 block = compiler_new_block(c);
4354 finally = compiler_new_block(c);
4355 if (!block || !finally)
4356 return 0;
4357
4358 /* Evaluate EXPR */
4359 VISIT(c, expr, item->context_expr);
4360
4361 ADDOP(c, BEFORE_ASYNC_WITH);
4362 ADDOP(c, GET_AWAITABLE);
4363 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4364 ADDOP(c, YIELD_FROM);
4365
4366 ADDOP_JREL(c, SETUP_ASYNC_WITH, finally);
4367
4368 /* SETUP_ASYNC_WITH pushes a finally block. */
4369 compiler_use_next_block(c, block);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004370 if (!compiler_push_fblock(c, ASYNC_WITH, block, finally)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004371 return 0;
4372 }
4373
4374 if (item->optional_vars) {
4375 VISIT(c, expr, item->optional_vars);
4376 }
4377 else {
4378 /* Discard result from context.__aenter__() */
4379 ADDOP(c, POP_TOP);
4380 }
4381
4382 pos++;
4383 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
4384 /* BLOCK code */
4385 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
4386 else if (!compiler_async_with(c, s, pos))
4387 return 0;
4388
4389 /* End of try block; start the finally block */
4390 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004391 ADDOP(c, BEGIN_FINALLY);
4392 compiler_pop_fblock(c, ASYNC_WITH, block);
Yury Selivanov75445082015-05-11 22:57:16 -04004393
Yury Selivanov75445082015-05-11 22:57:16 -04004394 compiler_use_next_block(c, finally);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004395 if (!compiler_push_fblock(c, FINALLY_END, finally, NULL))
Yury Selivanov75445082015-05-11 22:57:16 -04004396 return 0;
4397
4398 /* Finally block starts; context.__exit__ is on the stack under
4399 the exception or return information. Just issue our magic
4400 opcode. */
4401 ADDOP(c, WITH_CLEANUP_START);
4402
4403 ADDOP(c, GET_AWAITABLE);
4404 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4405 ADDOP(c, YIELD_FROM);
4406
4407 ADDOP(c, WITH_CLEANUP_FINISH);
4408
4409 /* Finally block ends. */
4410 ADDOP(c, END_FINALLY);
4411 compiler_pop_fblock(c, FINALLY_END, finally);
4412 return 1;
4413}
4414
4415
Guido van Rossumc2e20742006-02-27 22:32:47 +00004416/*
4417 Implements the with statement from PEP 343.
4418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004419 The semantics outlined in that PEP are as follows:
Guido van Rossumc2e20742006-02-27 22:32:47 +00004420
4421 with EXPR as VAR:
4422 BLOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004423
Guido van Rossumc2e20742006-02-27 22:32:47 +00004424 It is implemented roughly as:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004425
Thomas Wouters477c8d52006-05-27 19:21:47 +00004426 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00004427 exit = context.__exit__ # not calling it
4428 value = context.__enter__()
4429 try:
4430 VAR = value # if VAR present in the syntax
4431 BLOCK
4432 finally:
4433 if an exception was raised:
Serhiy Storchakad741a882015-06-11 00:06:39 +03004434 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004435 else:
Serhiy Storchakad741a882015-06-11 00:06:39 +03004436 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004437 exit(*exc)
4438 */
4439static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004440compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004441{
Guido van Rossumc2e20742006-02-27 22:32:47 +00004442 basicblock *block, *finally;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004443 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004444
4445 assert(s->kind == With_kind);
4446
Guido van Rossumc2e20742006-02-27 22:32:47 +00004447 block = compiler_new_block(c);
4448 finally = compiler_new_block(c);
4449 if (!block || !finally)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004450 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004451
Thomas Wouters477c8d52006-05-27 19:21:47 +00004452 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004453 VISIT(c, expr, item->context_expr);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004454 ADDOP_JREL(c, SETUP_WITH, finally);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004455
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004456 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00004457 compiler_use_next_block(c, block);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004458 if (!compiler_push_fblock(c, WITH, block, finally)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004459 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004460 }
4461
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004462 if (item->optional_vars) {
4463 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004464 }
4465 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004466 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004467 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004468 }
4469
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004470 pos++;
4471 if (pos == asdl_seq_LEN(s->v.With.items))
4472 /* BLOCK code */
4473 VISIT_SEQ(c, stmt, s->v.With.body)
4474 else if (!compiler_with(c, s, pos))
4475 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004476
4477 /* End of try block; start the finally block */
4478 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004479 ADDOP(c, BEGIN_FINALLY);
4480 compiler_pop_fblock(c, WITH, block);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004481
Guido van Rossumc2e20742006-02-27 22:32:47 +00004482 compiler_use_next_block(c, finally);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004483 if (!compiler_push_fblock(c, FINALLY_END, finally, NULL))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004484 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004485
Christian Heimesdd15f6c2008-03-16 00:07:10 +00004486 /* Finally block starts; context.__exit__ is on the stack under
4487 the exception or return information. Just issue our magic
4488 opcode. */
Yury Selivanov75445082015-05-11 22:57:16 -04004489 ADDOP(c, WITH_CLEANUP_START);
4490 ADDOP(c, WITH_CLEANUP_FINISH);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004491
4492 /* Finally block ends. */
4493 ADDOP(c, END_FINALLY);
4494 compiler_pop_fblock(c, FINALLY_END, finally);
4495 return 1;
4496}
4497
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004498static int
4499compiler_visit_expr(struct compiler *c, expr_ty e)
4500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 /* If expr e has a different line number than the last expr/stmt,
4502 set a new line number for the next instruction.
4503 */
4504 if (e->lineno > c->u->u_lineno) {
4505 c->u->u_lineno = e->lineno;
4506 c->u->u_lineno_set = 0;
4507 }
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00004508 /* Updating the column offset is always harmless. */
4509 c->u->u_col_offset = e->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004510 switch (e->kind) {
4511 case BoolOp_kind:
4512 return compiler_boolop(c, e);
4513 case BinOp_kind:
4514 VISIT(c, expr, e->v.BinOp.left);
4515 VISIT(c, expr, e->v.BinOp.right);
4516 ADDOP(c, binop(c, e->v.BinOp.op));
4517 break;
4518 case UnaryOp_kind:
4519 VISIT(c, expr, e->v.UnaryOp.operand);
4520 ADDOP(c, unaryop(e->v.UnaryOp.op));
4521 break;
4522 case Lambda_kind:
4523 return compiler_lambda(c, e);
4524 case IfExp_kind:
4525 return compiler_ifexp(c, e);
4526 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004527 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004528 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004529 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004530 case GeneratorExp_kind:
4531 return compiler_genexp(c, e);
4532 case ListComp_kind:
4533 return compiler_listcomp(c, e);
4534 case SetComp_kind:
4535 return compiler_setcomp(c, e);
4536 case DictComp_kind:
4537 return compiler_dictcomp(c, e);
4538 case Yield_kind:
4539 if (c->u->u_ste->ste_type != FunctionBlock)
4540 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00004541 if (e->v.Yield.value) {
4542 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004543 }
4544 else {
4545 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4546 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00004547 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004548 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00004549 case YieldFrom_kind:
4550 if (c->u->u_ste->ste_type != FunctionBlock)
4551 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04004552
4553 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
4554 return compiler_error(c, "'yield from' inside async function");
4555
Mark Dickinsonded35ae2012-11-25 14:36:26 +00004556 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004557 ADDOP(c, GET_YIELD_FROM_ITER);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00004558 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4559 ADDOP(c, YIELD_FROM);
4560 break;
Yury Selivanov75445082015-05-11 22:57:16 -04004561 case Await_kind:
4562 if (c->u->u_ste->ste_type != FunctionBlock)
4563 return compiler_error(c, "'await' outside function");
4564
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004565 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
4566 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION)
Yury Selivanov75445082015-05-11 22:57:16 -04004567 return compiler_error(c, "'await' outside async function");
4568
4569 VISIT(c, expr, e->v.Await.value);
4570 ADDOP(c, GET_AWAITABLE);
4571 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4572 ADDOP(c, YIELD_FROM);
4573 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004574 case Compare_kind:
4575 return compiler_compare(c, e);
4576 case Call_kind:
4577 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01004578 case Constant_kind:
4579 ADDOP_O(c, LOAD_CONST, e->v.Constant.value, consts);
4580 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004581 case Num_kind:
4582 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
4583 break;
4584 case Str_kind:
4585 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
4586 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004587 case JoinedStr_kind:
4588 return compiler_joined_str(c, e);
4589 case FormattedValue_kind:
4590 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004591 case Bytes_kind:
4592 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
4593 break;
4594 case Ellipsis_kind:
4595 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
4596 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05004597 case NameConstant_kind:
4598 ADDOP_O(c, LOAD_CONST, e->v.NameConstant.value, consts);
4599 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004600 /* The following exprs can be assignment targets. */
4601 case Attribute_kind:
4602 if (e->v.Attribute.ctx != AugStore)
4603 VISIT(c, expr, e->v.Attribute.value);
4604 switch (e->v.Attribute.ctx) {
4605 case AugLoad:
4606 ADDOP(c, DUP_TOP);
Stefan Krahf432a322017-08-21 13:09:59 +02004607 /* Fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004608 case Load:
4609 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
4610 break;
4611 case AugStore:
4612 ADDOP(c, ROT_TWO);
Stefan Krahf432a322017-08-21 13:09:59 +02004613 /* Fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004614 case Store:
4615 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
4616 break;
4617 case Del:
4618 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
4619 break;
4620 case Param:
4621 default:
4622 PyErr_SetString(PyExc_SystemError,
4623 "param invalid in attribute expression");
4624 return 0;
4625 }
4626 break;
4627 case Subscript_kind:
4628 switch (e->v.Subscript.ctx) {
4629 case AugLoad:
4630 VISIT(c, expr, e->v.Subscript.value);
4631 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
4632 break;
4633 case Load:
4634 VISIT(c, expr, e->v.Subscript.value);
4635 VISIT_SLICE(c, e->v.Subscript.slice, Load);
4636 break;
4637 case AugStore:
4638 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
4639 break;
4640 case Store:
4641 VISIT(c, expr, e->v.Subscript.value);
4642 VISIT_SLICE(c, e->v.Subscript.slice, Store);
4643 break;
4644 case Del:
4645 VISIT(c, expr, e->v.Subscript.value);
4646 VISIT_SLICE(c, e->v.Subscript.slice, Del);
4647 break;
4648 case Param:
4649 default:
4650 PyErr_SetString(PyExc_SystemError,
4651 "param invalid in subscript expression");
4652 return 0;
4653 }
4654 break;
4655 case Starred_kind:
4656 switch (e->v.Starred.ctx) {
4657 case Store:
4658 /* In all legitimate cases, the Starred node was already replaced
4659 * by compiler_list/compiler_tuple. XXX: is that okay? */
4660 return compiler_error(c,
4661 "starred assignment target must be in a list or tuple");
4662 default:
4663 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004664 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004665 }
4666 break;
4667 case Name_kind:
4668 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
4669 /* child nodes of List and Tuple will have expr_context set */
4670 case List_kind:
4671 return compiler_list(c, e);
4672 case Tuple_kind:
4673 return compiler_tuple(c, e);
4674 }
4675 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004676}
4677
4678static int
4679compiler_augassign(struct compiler *c, stmt_ty s)
4680{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004681 expr_ty e = s->v.AugAssign.target;
4682 expr_ty auge;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004684 assert(s->kind == AugAssign_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004686 switch (e->kind) {
4687 case Attribute_kind:
4688 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
4689 AugLoad, e->lineno, e->col_offset, c->c_arena);
4690 if (auge == NULL)
4691 return 0;
4692 VISIT(c, expr, auge);
4693 VISIT(c, expr, s->v.AugAssign.value);
4694 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
4695 auge->v.Attribute.ctx = AugStore;
4696 VISIT(c, expr, auge);
4697 break;
4698 case Subscript_kind:
4699 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
4700 AugLoad, e->lineno, e->col_offset, c->c_arena);
4701 if (auge == NULL)
4702 return 0;
4703 VISIT(c, expr, auge);
4704 VISIT(c, expr, s->v.AugAssign.value);
4705 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
4706 auge->v.Subscript.ctx = AugStore;
4707 VISIT(c, expr, auge);
4708 break;
4709 case Name_kind:
4710 if (!compiler_nameop(c, e->v.Name.id, Load))
4711 return 0;
4712 VISIT(c, expr, s->v.AugAssign.value);
4713 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
4714 return compiler_nameop(c, e->v.Name.id, Store);
4715 default:
4716 PyErr_Format(PyExc_SystemError,
4717 "invalid node type (%d) for augmented assignment",
4718 e->kind);
4719 return 0;
4720 }
4721 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004722}
4723
4724static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07004725check_ann_expr(struct compiler *c, expr_ty e)
4726{
4727 VISIT(c, expr, e);
4728 ADDOP(c, POP_TOP);
4729 return 1;
4730}
4731
4732static int
4733check_annotation(struct compiler *c, stmt_ty s)
4734{
4735 /* Annotations are only evaluated in a module or class. */
4736 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
4737 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
4738 return check_ann_expr(c, s->v.AnnAssign.annotation);
4739 }
4740 return 1;
4741}
4742
4743static int
4744check_ann_slice(struct compiler *c, slice_ty sl)
4745{
4746 switch(sl->kind) {
4747 case Index_kind:
4748 return check_ann_expr(c, sl->v.Index.value);
4749 case Slice_kind:
4750 if (sl->v.Slice.lower && !check_ann_expr(c, sl->v.Slice.lower)) {
4751 return 0;
4752 }
4753 if (sl->v.Slice.upper && !check_ann_expr(c, sl->v.Slice.upper)) {
4754 return 0;
4755 }
4756 if (sl->v.Slice.step && !check_ann_expr(c, sl->v.Slice.step)) {
4757 return 0;
4758 }
4759 break;
4760 default:
4761 PyErr_SetString(PyExc_SystemError,
4762 "unexpected slice kind");
4763 return 0;
4764 }
4765 return 1;
4766}
4767
4768static int
4769check_ann_subscr(struct compiler *c, slice_ty sl)
4770{
4771 /* We check that everything in a subscript is defined at runtime. */
4772 Py_ssize_t i, n;
4773
4774 switch (sl->kind) {
4775 case Index_kind:
4776 case Slice_kind:
4777 if (!check_ann_slice(c, sl)) {
4778 return 0;
4779 }
4780 break;
4781 case ExtSlice_kind:
4782 n = asdl_seq_LEN(sl->v.ExtSlice.dims);
4783 for (i = 0; i < n; i++) {
4784 slice_ty subsl = (slice_ty)asdl_seq_GET(sl->v.ExtSlice.dims, i);
4785 switch (subsl->kind) {
4786 case Index_kind:
4787 case Slice_kind:
4788 if (!check_ann_slice(c, subsl)) {
4789 return 0;
4790 }
4791 break;
4792 case ExtSlice_kind:
4793 default:
4794 PyErr_SetString(PyExc_SystemError,
4795 "extended slice invalid in nested slice");
4796 return 0;
4797 }
4798 }
4799 break;
4800 default:
4801 PyErr_Format(PyExc_SystemError,
4802 "invalid subscript kind %d", sl->kind);
4803 return 0;
4804 }
4805 return 1;
4806}
4807
4808static int
4809compiler_annassign(struct compiler *c, stmt_ty s)
4810{
4811 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07004812 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07004813
4814 assert(s->kind == AnnAssign_kind);
4815
4816 /* We perform the actual assignment first. */
4817 if (s->v.AnnAssign.value) {
4818 VISIT(c, expr, s->v.AnnAssign.value);
4819 VISIT(c, expr, targ);
4820 }
4821 switch (targ->kind) {
4822 case Name_kind:
4823 /* If we have a simple name in a module or class, store annotation. */
4824 if (s->v.AnnAssign.simple &&
4825 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
4826 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Guido van Rossum015d8742016-09-11 09:45:24 -07004827 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
4828 if (!mangled) {
4829 return 0;
4830 }
Guido van Rossum95e4d582018-01-26 08:20:18 -08004831 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
4832 VISIT(c, annexpr, s->v.AnnAssign.annotation)
4833 }
4834 else {
4835 VISIT(c, expr, s->v.AnnAssign.annotation);
4836 }
Mark Shannon332cd5e2018-01-30 00:41:04 +00004837 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
4838 ADDOP_O(c, LOAD_CONST, mangled, consts);
4839 Py_DECREF(mangled);
4840 ADDOP(c, STORE_SUBSCR);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07004841 }
4842 break;
4843 case Attribute_kind:
4844 if (!s->v.AnnAssign.value &&
4845 !check_ann_expr(c, targ->v.Attribute.value)) {
4846 return 0;
4847 }
4848 break;
4849 case Subscript_kind:
4850 if (!s->v.AnnAssign.value &&
4851 (!check_ann_expr(c, targ->v.Subscript.value) ||
4852 !check_ann_subscr(c, targ->v.Subscript.slice))) {
4853 return 0;
4854 }
4855 break;
4856 default:
4857 PyErr_Format(PyExc_SystemError,
4858 "invalid node type (%d) for annotated assignment",
4859 targ->kind);
4860 return 0;
4861 }
4862 /* Annotation is evaluated last. */
4863 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
4864 return 0;
4865 }
4866 return 1;
4867}
4868
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004869/* Raises a SyntaxError and returns 0.
4870 If something goes wrong, a different exception may be raised.
4871*/
4872
4873static int
4874compiler_error(struct compiler *c, const char *errstr)
4875{
Benjamin Peterson43b06862011-05-27 09:08:01 -05004876 PyObject *loc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004877 PyObject *u = NULL, *v = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004878
Victor Stinner14e461d2013-08-26 22:28:21 +02004879 loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004880 if (!loc) {
4881 Py_INCREF(Py_None);
4882 loc = Py_None;
4883 }
Victor Stinner14e461d2013-08-26 22:28:21 +02004884 u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00004885 c->u->u_col_offset, loc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004886 if (!u)
4887 goto exit;
4888 v = Py_BuildValue("(zO)", errstr, u);
4889 if (!v)
4890 goto exit;
4891 PyErr_SetObject(PyExc_SyntaxError, v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004892 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004893 Py_DECREF(loc);
4894 Py_XDECREF(u);
4895 Py_XDECREF(v);
4896 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004897}
4898
4899static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004900compiler_handle_subscr(struct compiler *c, const char *kind,
4901 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004902{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004903 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004905 /* XXX this code is duplicated */
4906 switch (ctx) {
4907 case AugLoad: /* fall through to Load */
4908 case Load: op = BINARY_SUBSCR; break;
4909 case AugStore:/* fall through to Store */
4910 case Store: op = STORE_SUBSCR; break;
4911 case Del: op = DELETE_SUBSCR; break;
4912 case Param:
4913 PyErr_Format(PyExc_SystemError,
4914 "invalid %s kind %d in subscript\n",
4915 kind, ctx);
4916 return 0;
4917 }
4918 if (ctx == AugLoad) {
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00004919 ADDOP(c, DUP_TOP_TWO);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004920 }
4921 else if (ctx == AugStore) {
4922 ADDOP(c, ROT_THREE);
4923 }
4924 ADDOP(c, op);
4925 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004926}
4927
4928static int
4929compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
4930{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004931 int n = 2;
4932 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004934 /* only handles the cases where BUILD_SLICE is emitted */
4935 if (s->v.Slice.lower) {
4936 VISIT(c, expr, s->v.Slice.lower);
4937 }
4938 else {
4939 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4940 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004942 if (s->v.Slice.upper) {
4943 VISIT(c, expr, s->v.Slice.upper);
4944 }
4945 else {
4946 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4947 }
4948
4949 if (s->v.Slice.step) {
4950 n++;
4951 VISIT(c, expr, s->v.Slice.step);
4952 }
4953 ADDOP_I(c, BUILD_SLICE, n);
4954 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004955}
4956
4957static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004958compiler_visit_nested_slice(struct compiler *c, slice_ty s,
4959 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004960{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004961 switch (s->kind) {
4962 case Slice_kind:
4963 return compiler_slice(c, s, ctx);
4964 case Index_kind:
4965 VISIT(c, expr, s->v.Index.value);
4966 break;
4967 case ExtSlice_kind:
4968 default:
4969 PyErr_SetString(PyExc_SystemError,
4970 "extended slice invalid in nested slice");
4971 return 0;
4972 }
4973 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004974}
4975
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004976static int
4977compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
4978{
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02004979 const char * kindname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004980 switch (s->kind) {
4981 case Index_kind:
4982 kindname = "index";
4983 if (ctx != AugStore) {
4984 VISIT(c, expr, s->v.Index.value);
4985 }
4986 break;
4987 case Slice_kind:
4988 kindname = "slice";
4989 if (ctx != AugStore) {
4990 if (!compiler_slice(c, s, ctx))
4991 return 0;
4992 }
4993 break;
4994 case ExtSlice_kind:
4995 kindname = "extended slice";
4996 if (ctx != AugStore) {
Victor Stinnerad9a0662013-11-19 22:23:20 +01004997 Py_ssize_t i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004998 for (i = 0; i < n; i++) {
4999 slice_ty sub = (slice_ty)asdl_seq_GET(
5000 s->v.ExtSlice.dims, i);
5001 if (!compiler_visit_nested_slice(c, sub, ctx))
5002 return 0;
5003 }
5004 ADDOP_I(c, BUILD_TUPLE, n);
5005 }
5006 break;
5007 default:
5008 PyErr_Format(PyExc_SystemError,
5009 "invalid subscript kind %d", s->kind);
5010 return 0;
5011 }
5012 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005013}
5014
Thomas Wouters89f507f2006-12-13 04:49:30 +00005015/* End of the compiler section, beginning of the assembler section */
5016
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005017/* do depth-first search of basic block graph, starting with block.
5018 post records the block indices in post-order.
5019
5020 XXX must handle implicit jumps from one block to next
5021*/
5022
Thomas Wouters89f507f2006-12-13 04:49:30 +00005023struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005024 PyObject *a_bytecode; /* string containing bytecode */
5025 int a_offset; /* offset into bytecode */
5026 int a_nblocks; /* number of reachable blocks */
5027 basicblock **a_postorder; /* list of blocks in dfs postorder */
5028 PyObject *a_lnotab; /* string containing lnotab */
5029 int a_lnotab_off; /* offset into lnotab */
5030 int a_lineno; /* last lineno of emitted instruction */
5031 int a_lineno_off; /* bytecode offset of last lineno */
Thomas Wouters89f507f2006-12-13 04:49:30 +00005032};
5033
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005034static void
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005035dfs(struct compiler *c, basicblock *b, struct assembler *a, int end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005036{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005037 int i, j;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005038
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005039 /* Get rid of recursion for normal control flow.
5040 Since the number of blocks is limited, unused space in a_postorder
5041 (from a_nblocks to end) can be used as a stack for still not ordered
5042 blocks. */
5043 for (j = end; b && !b->b_seen; b = b->b_next) {
5044 b->b_seen = 1;
5045 assert(a->a_nblocks < j);
5046 a->a_postorder[--j] = b;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005047 }
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005048 while (j < end) {
5049 b = a->a_postorder[j++];
5050 for (i = 0; i < b->b_iused; i++) {
5051 struct instr *instr = &b->b_instr[i];
5052 if (instr->i_jrel || instr->i_jabs)
5053 dfs(c, instr->i_target, a, j);
5054 }
5055 assert(a->a_nblocks < j);
5056 a->a_postorder[a->a_nblocks++] = b;
5057 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005058}
5059
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005060Py_LOCAL_INLINE(void)
5061stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005062{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005063 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005064 if (b->b_startdepth < depth) {
5065 assert(b->b_startdepth < 0);
5066 b->b_startdepth = depth;
5067 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02005068 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005069}
5070
5071/* Find the flow path that needs the largest stack. We assume that
5072 * cycles in the flow graph have no net effect on the stack depth.
5073 */
5074static int
5075stackdepth(struct compiler *c)
5076{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005077 basicblock *b, *entryblock = NULL;
5078 basicblock **stack, **sp;
5079 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005080 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005081 b->b_startdepth = INT_MIN;
5082 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005083 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005084 }
5085 if (!entryblock)
5086 return 0;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005087 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
5088 if (!stack) {
5089 PyErr_NoMemory();
5090 return -1;
5091 }
5092
5093 sp = stack;
5094 stackdepth_push(&sp, entryblock, 0);
5095 while (sp != stack) {
5096 b = *--sp;
5097 int depth = b->b_startdepth;
5098 assert(depth >= 0);
5099 basicblock *next = b->b_next;
5100 for (int i = 0; i < b->b_iused; i++) {
5101 struct instr *instr = &b->b_instr[i];
5102 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
5103 if (effect == PY_INVALID_STACK_EFFECT) {
5104 fprintf(stderr, "opcode = %d\n", instr->i_opcode);
5105 Py_FatalError("PyCompile_OpcodeStackEffect()");
5106 }
5107 int new_depth = depth + effect;
5108 if (new_depth > maxdepth) {
5109 maxdepth = new_depth;
5110 }
5111 assert(depth >= 0); /* invalid code or bug in stackdepth() */
5112 if (instr->i_jrel || instr->i_jabs) {
5113 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
5114 assert(effect != PY_INVALID_STACK_EFFECT);
5115 int target_depth = depth + effect;
5116 if (target_depth > maxdepth) {
5117 maxdepth = target_depth;
5118 }
5119 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005120 if (instr->i_opcode == CALL_FINALLY) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005121 assert(instr->i_target->b_startdepth >= 0);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005122 assert(instr->i_target->b_startdepth >= target_depth);
5123 depth = new_depth;
5124 continue;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005125 }
5126 stackdepth_push(&sp, instr->i_target, target_depth);
5127 }
5128 depth = new_depth;
5129 if (instr->i_opcode == JUMP_ABSOLUTE ||
5130 instr->i_opcode == JUMP_FORWARD ||
5131 instr->i_opcode == RETURN_VALUE ||
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005132 instr->i_opcode == RAISE_VARARGS)
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005133 {
5134 /* remaining code is dead */
5135 next = NULL;
5136 break;
5137 }
5138 }
5139 if (next != NULL) {
5140 stackdepth_push(&sp, next, depth);
5141 }
5142 }
5143 PyObject_Free(stack);
5144 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005145}
5146
5147static int
5148assemble_init(struct assembler *a, int nblocks, int firstlineno)
5149{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005150 memset(a, 0, sizeof(struct assembler));
5151 a->a_lineno = firstlineno;
5152 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
5153 if (!a->a_bytecode)
5154 return 0;
5155 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
5156 if (!a->a_lnotab)
5157 return 0;
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07005158 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005159 PyErr_NoMemory();
5160 return 0;
5161 }
5162 a->a_postorder = (basicblock **)PyObject_Malloc(
5163 sizeof(basicblock *) * nblocks);
5164 if (!a->a_postorder) {
5165 PyErr_NoMemory();
5166 return 0;
5167 }
5168 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005169}
5170
5171static void
5172assemble_free(struct assembler *a)
5173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005174 Py_XDECREF(a->a_bytecode);
5175 Py_XDECREF(a->a_lnotab);
5176 if (a->a_postorder)
5177 PyObject_Free(a->a_postorder);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005178}
5179
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005180static int
5181blocksize(basicblock *b)
5182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005183 int i;
5184 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005186 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005187 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005188 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005189}
5190
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00005191/* Appends a pair to the end of the line number table, a_lnotab, representing
5192 the instruction's bytecode offset and line number. See
5193 Objects/lnotab_notes.txt for the description of the line number table. */
Tim Peters2a7f3842001-06-09 09:26:21 +00005194
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005195static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005196assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005198 int d_bytecode, d_lineno;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005199 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005200 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005201
Serhiy Storchakaab874002016-09-11 13:48:15 +03005202 d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005203 d_lineno = i->i_lineno - a->a_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005205 assert(d_bytecode >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005207 if(d_bytecode == 0 && d_lineno == 0)
5208 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00005209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005210 if (d_bytecode > 255) {
5211 int j, nbytes, ncodes = d_bytecode / 255;
5212 nbytes = a->a_lnotab_off + 2 * ncodes;
5213 len = PyBytes_GET_SIZE(a->a_lnotab);
5214 if (nbytes >= len) {
5215 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
5216 len = nbytes;
5217 else if (len <= INT_MAX / 2)
5218 len *= 2;
5219 else {
5220 PyErr_NoMemory();
5221 return 0;
5222 }
5223 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
5224 return 0;
5225 }
5226 lnotab = (unsigned char *)
5227 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
5228 for (j = 0; j < ncodes; j++) {
5229 *lnotab++ = 255;
5230 *lnotab++ = 0;
5231 }
5232 d_bytecode -= ncodes * 255;
5233 a->a_lnotab_off += ncodes * 2;
5234 }
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005235 assert(0 <= d_bytecode && d_bytecode <= 255);
5236
5237 if (d_lineno < -128 || 127 < d_lineno) {
5238 int j, nbytes, ncodes, k;
5239 if (d_lineno < 0) {
5240 k = -128;
5241 /* use division on positive numbers */
5242 ncodes = (-d_lineno) / 128;
5243 }
5244 else {
5245 k = 127;
5246 ncodes = d_lineno / 127;
5247 }
5248 d_lineno -= ncodes * k;
5249 assert(ncodes >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005250 nbytes = a->a_lnotab_off + 2 * ncodes;
5251 len = PyBytes_GET_SIZE(a->a_lnotab);
5252 if (nbytes >= len) {
5253 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
5254 len = nbytes;
5255 else if (len <= INT_MAX / 2)
5256 len *= 2;
5257 else {
5258 PyErr_NoMemory();
5259 return 0;
5260 }
5261 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
5262 return 0;
5263 }
5264 lnotab = (unsigned char *)
5265 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
5266 *lnotab++ = d_bytecode;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005267 *lnotab++ = k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005268 d_bytecode = 0;
5269 for (j = 1; j < ncodes; j++) {
5270 *lnotab++ = 0;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005271 *lnotab++ = k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005272 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005273 a->a_lnotab_off += ncodes * 2;
5274 }
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005275 assert(-128 <= d_lineno && d_lineno <= 127);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005277 len = PyBytes_GET_SIZE(a->a_lnotab);
5278 if (a->a_lnotab_off + 2 >= len) {
5279 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
5280 return 0;
5281 }
5282 lnotab = (unsigned char *)
5283 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00005284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005285 a->a_lnotab_off += 2;
5286 if (d_bytecode) {
5287 *lnotab++ = d_bytecode;
Victor Stinner4f2dab52011-05-27 16:46:51 +02005288 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005289 }
5290 else { /* First line of a block; def stmt, etc. */
5291 *lnotab++ = 0;
Victor Stinner4f2dab52011-05-27 16:46:51 +02005292 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005293 }
5294 a->a_lineno = i->i_lineno;
5295 a->a_lineno_off = a->a_offset;
5296 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005297}
5298
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005299/* assemble_emit()
5300 Extend the bytecode with a new instruction.
5301 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00005302*/
5303
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00005304static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005305assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00005306{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005307 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005308 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03005309 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005310
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005311 arg = i->i_oparg;
5312 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005313 if (i->i_lineno && !assemble_lnotab(a, i))
5314 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005315 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005316 if (len > PY_SSIZE_T_MAX / 2)
5317 return 0;
5318 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
5319 return 0;
5320 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03005321 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005322 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005323 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005324 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00005325}
5326
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00005327static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005328assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00005329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005330 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005331 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005332 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00005333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005334 /* Compute the size of each block and fixup jump args.
5335 Replace block pointer with position in bytecode. */
5336 do {
5337 totsize = 0;
5338 for (i = a->a_nblocks - 1; i >= 0; i--) {
5339 b = a->a_postorder[i];
5340 bsize = blocksize(b);
5341 b->b_offset = totsize;
5342 totsize += bsize;
5343 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005344 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005345 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
5346 bsize = b->b_offset;
5347 for (i = 0; i < b->b_iused; i++) {
5348 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005349 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005350 /* Relative jumps are computed relative to
5351 the instruction pointer after fetching
5352 the jump instruction.
5353 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005354 bsize += isize;
5355 if (instr->i_jabs || instr->i_jrel) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005356 instr->i_oparg = instr->i_target->b_offset;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005357 if (instr->i_jrel) {
5358 instr->i_oparg -= bsize;
5359 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03005360 instr->i_oparg *= sizeof(_Py_CODEUNIT);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005361 if (instrsize(instr->i_oparg) != isize) {
5362 extended_arg_recompile = 1;
5363 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005364 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005365 }
5366 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00005367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005368 /* XXX: This is an awful hack that could hurt performance, but
5369 on the bright side it should work until we come up
5370 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00005371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005372 The issue is that in the first loop blocksize() is called
5373 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005374 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005375 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00005376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005377 So we loop until we stop seeing new EXTENDED_ARGs.
5378 The only EXTENDED_ARGs that could be popping up are
5379 ones in jump instructions. So this should converge
5380 fairly quickly.
5381 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005382 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005383}
5384
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005385static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01005386dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005387{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005388 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02005389 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005391 tuple = PyTuple_New(size);
5392 if (tuple == NULL)
5393 return NULL;
5394 while (PyDict_Next(dict, &pos, &k, &v)) {
5395 i = PyLong_AS_LONG(v);
Victor Stinnerefb24132016-01-22 12:33:12 +01005396 /* The keys of the dictionary are tuples. (see compiler_add_o
5397 * and _PyCode_ConstantKey). The object we want is always second,
5398 * though. */
5399 k = PyTuple_GET_ITEM(k, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005400 Py_INCREF(k);
5401 assert((i - offset) < size);
5402 assert((i - offset) >= 0);
5403 PyTuple_SET_ITEM(tuple, i - offset, k);
5404 }
5405 return tuple;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005406}
5407
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005408static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005409compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005411 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005412 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005413 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04005414 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005415 if (ste->ste_nested)
5416 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07005417 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005418 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07005419 if (!ste->ste_generator && ste->ste_coroutine)
5420 flags |= CO_COROUTINE;
5421 if (ste->ste_generator && ste->ste_coroutine)
5422 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005423 if (ste->ste_varargs)
5424 flags |= CO_VARARGS;
5425 if (ste->ste_varkeywords)
5426 flags |= CO_VARKEYWORDS;
5427 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00005428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005429 /* (Only) inherit compilerflags in PyCF_MASK */
5430 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00005431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005432 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005433}
5434
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005435static PyCodeObject *
5436makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005437{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005438 PyObject *tmp;
5439 PyCodeObject *co = NULL;
5440 PyObject *consts = NULL;
5441 PyObject *names = NULL;
5442 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005443 PyObject *name = NULL;
5444 PyObject *freevars = NULL;
5445 PyObject *cellvars = NULL;
5446 PyObject *bytecode = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005447 Py_ssize_t nlocals;
5448 int nlocals_int;
5449 int flags;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005450 int argcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005452 tmp = dict_keys_inorder(c->u->u_consts, 0);
5453 if (!tmp)
5454 goto error;
5455 consts = PySequence_List(tmp); /* optimize_code requires a list */
5456 Py_DECREF(tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005458 names = dict_keys_inorder(c->u->u_names, 0);
5459 varnames = dict_keys_inorder(c->u->u_varnames, 0);
5460 if (!consts || !names || !varnames)
5461 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005463 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
5464 if (!cellvars)
5465 goto error;
5466 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
5467 if (!freevars)
5468 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005469
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02005470 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01005471 assert(nlocals < INT_MAX);
5472 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
5473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005474 flags = compute_code_flags(c);
5475 if (flags < 0)
5476 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005478 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
5479 if (!bytecode)
5480 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005482 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
5483 if (!tmp)
5484 goto error;
5485 Py_DECREF(consts);
5486 consts = tmp;
5487
Victor Stinnerf8e32212013-11-19 23:56:34 +01005488 argcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
5489 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005490 maxdepth = stackdepth(c);
5491 if (maxdepth < 0) {
5492 goto error;
5493 }
Victor Stinnerf8e32212013-11-19 23:56:34 +01005494 co = PyCode_New(argcount, kwonlyargcount,
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005495 nlocals_int, maxdepth, flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005496 bytecode, consts, names, varnames,
5497 freevars, cellvars,
Victor Stinner14e461d2013-08-26 22:28:21 +02005498 c->c_filename, c->u->u_name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005499 c->u->u_firstlineno,
5500 a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005501 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005502 Py_XDECREF(consts);
5503 Py_XDECREF(names);
5504 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005505 Py_XDECREF(name);
5506 Py_XDECREF(freevars);
5507 Py_XDECREF(cellvars);
5508 Py_XDECREF(bytecode);
5509 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005510}
5511
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005512
5513/* For debugging purposes only */
5514#if 0
5515static void
5516dump_instr(const struct instr *i)
5517{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005518 const char *jrel = i->i_jrel ? "jrel " : "";
5519 const char *jabs = i->i_jabs ? "jabs " : "";
5520 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005522 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005523 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005524 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005525 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005526 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
5527 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005528}
5529
5530static void
5531dump_basicblock(const basicblock *b)
5532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005533 const char *seen = b->b_seen ? "seen " : "";
5534 const char *b_return = b->b_return ? "return " : "";
5535 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
5536 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
5537 if (b->b_instr) {
5538 int i;
5539 for (i = 0; i < b->b_iused; i++) {
5540 fprintf(stderr, " [%02d] ", i);
5541 dump_instr(b->b_instr + i);
5542 }
5543 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005544}
5545#endif
5546
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005547static PyCodeObject *
5548assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005549{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005550 basicblock *b, *entryblock;
5551 struct assembler a;
5552 int i, j, nblocks;
5553 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005555 /* Make sure every block that falls off the end returns None.
5556 XXX NEXT_BLOCK() isn't quite right, because if the last
5557 block ends with a jump or return b_next shouldn't set.
5558 */
5559 if (!c->u->u_curblock->b_return) {
5560 NEXT_BLOCK(c);
5561 if (addNone)
5562 ADDOP_O(c, LOAD_CONST, Py_None, consts);
5563 ADDOP(c, RETURN_VALUE);
5564 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005566 nblocks = 0;
5567 entryblock = NULL;
5568 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
5569 nblocks++;
5570 entryblock = b;
5571 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005573 /* Set firstlineno if it wasn't explicitly set. */
5574 if (!c->u->u_firstlineno) {
Ned Deilydc35cda2016-08-17 17:18:33 -04005575 if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005576 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
5577 else
5578 c->u->u_firstlineno = 1;
5579 }
5580 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
5581 goto error;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005582 dfs(c, entryblock, &a, nblocks);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005584 /* Can't modify the bytecode after computing jump offsets. */
5585 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00005586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005587 /* Emit code in reverse postorder from dfs. */
5588 for (i = a.a_nblocks - 1; i >= 0; i--) {
5589 b = a.a_postorder[i];
5590 for (j = 0; j < b->b_iused; j++)
5591 if (!assemble_emit(&a, &b->b_instr[j]))
5592 goto error;
5593 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00005594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005595 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
5596 goto error;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005597 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005598 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005600 co = makecode(c, &a);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005601 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005602 assemble_free(&a);
5603 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005604}
Georg Brandl8334fd92010-12-04 10:26:46 +00005605
5606#undef PyAST_Compile
5607PyAPI_FUNC(PyCodeObject *)
5608PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
5609 PyArena *arena)
5610{
5611 return PyAST_CompileEx(mod, filename, flags, -1, arena);
5612}