blob: 906772289dde1cb205d562f42b3a357f277a1ca9 [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"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000032
Guido van Rossum8e793d91997-03-03 19:13:14 +000033int Py_OptimizeFlag = 0;
34
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000035#define DEFAULT_BLOCK_SIZE 16
36#define DEFAULT_BLOCKS 8
37#define DEFAULT_CODE_SIZE 128
38#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000039
Nick Coghlan650f0d02007-04-15 12:05:43 +000040#define COMP_GENEXP 0
41#define COMP_LISTCOMP 1
42#define COMP_SETCOMP 2
Guido van Rossum992d4a32007-07-11 13:09:30 +000043#define COMP_DICTCOMP 3
Nick Coghlan650f0d02007-04-15 12:05:43 +000044
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000045struct instr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 unsigned i_jabs : 1;
47 unsigned i_jrel : 1;
48 unsigned i_hasarg : 1;
49 unsigned char i_opcode;
50 int i_oparg;
51 struct basicblock_ *i_target; /* target block (if jump instruction) */
52 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000053};
54
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000055typedef struct basicblock_ {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000056 /* Each basicblock in a compilation unit is linked via b_list in the
57 reverse order that the block are allocated. b_list points to the next
58 block, not to be confused with b_next, which is next by control flow. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 struct basicblock_ *b_list;
60 /* number of instructions used */
61 int b_iused;
62 /* length of instruction array (b_instr) */
63 int b_ialloc;
64 /* pointer to an array of instructions, initially NULL */
65 struct instr *b_instr;
66 /* If b_next is non-NULL, it is a pointer to the next
67 block reached by normal control flow. */
68 struct basicblock_ *b_next;
69 /* b_seen is used to perform a DFS of basicblocks. */
70 unsigned b_seen : 1;
71 /* b_return is true if a RETURN_VALUE opcode is inserted. */
72 unsigned b_return : 1;
73 /* depth of stack upon entry of block, computed by stackdepth() */
74 int b_startdepth;
75 /* instruction offset for block, computed by assemble_jump_offsets() */
76 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000077} basicblock;
78
79/* fblockinfo tracks the current frame block.
80
Jeremy Hyltone9357b22006-03-01 15:47:05 +000081A frame block is used to handle loops, try/except, and try/finally.
82It's called a frame block to distinguish it from a basic block in the
83compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000084*/
85
86enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
87
88struct fblockinfo {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 enum fblocktype fb_type;
90 basicblock *fb_block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000091};
92
93/* The following items change on entry and exit of code blocks.
94 They must be saved and restored when returning to a block.
95*/
96struct compiler_unit {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 PySTEntryObject *u_ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 PyObject *u_name;
100 /* The following fields are dicts that map objects to
101 the index of them in co_XXX. The index is used as
102 the argument for opcodes that refer to those collections.
103 */
104 PyObject *u_consts; /* all constants */
105 PyObject *u_names; /* all names */
106 PyObject *u_varnames; /* local variables */
107 PyObject *u_cellvars; /* cell variables */
108 PyObject *u_freevars; /* free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 PyObject *u_private; /* for private name mangling */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 int u_argcount; /* number of arguments for block */
113 int u_kwonlyargcount; /* number of keyword only arguments for block */
114 /* Pointer to the most recently allocated block. By following b_list
115 members, you can reach all early allocated blocks. */
116 basicblock *u_blocks;
117 basicblock *u_curblock; /* pointer to current block */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 int u_nfblocks;
120 struct fblockinfo u_fblock[CO_MAXBLOCKS];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 int u_firstlineno; /* the first lineno of the block */
123 int u_lineno; /* the lineno for the current stmt */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000124 int u_col_offset; /* the offset of the current stmt */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 int u_lineno_set; /* boolean to indicate whether instr
126 has been generated with current lineno */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000127};
128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129/* This struct captures the global state of a compilation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000130
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000131The u pointer points to the current compilation unit, while units
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132for enclosing blocks are stored in c_stack. The u and c_stack are
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000133managed by compiler_enter_scope() and compiler_exit_scope().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000134*/
135
136struct compiler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 const char *c_filename;
Benjamin Peterson43b06862011-05-27 09:08:01 -0500138 PyObject *c_filename_obj;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 struct symtable *c_st;
140 PyFutureFeatures *c_future; /* pointer to module's __future__ */
141 PyCompilerFlags *c_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000142
Georg Brandl8334fd92010-12-04 10:26:46 +0000143 int c_optimize; /* optimization level */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 int c_interactive; /* true if in interactive mode */
145 int c_nestlevel;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 struct compiler_unit *u; /* compiler state for current block */
148 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
149 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000150};
151
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000152static int compiler_enter_scope(struct compiler *, identifier, void *, int);
153static void compiler_free(struct compiler *);
154static basicblock *compiler_new_block(struct compiler *);
155static int compiler_next_instr(struct compiler *, basicblock *);
156static int compiler_addop(struct compiler *, int);
157static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
158static int compiler_addop_i(struct compiler *, int, int);
159static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000160static basicblock *compiler_use_new_block(struct compiler *);
161static int compiler_error(struct compiler *, const char *);
162static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
163
164static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
165static int compiler_visit_stmt(struct compiler *, stmt_ty);
166static int compiler_visit_keyword(struct compiler *, keyword_ty);
167static int compiler_visit_expr(struct compiler *, expr_ty);
168static int compiler_augassign(struct compiler *, stmt_ty);
169static int compiler_visit_slice(struct compiler *, slice_ty,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 expr_context_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000171
172static int compiler_push_fblock(struct compiler *, enum fblocktype,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000174static void compiler_pop_fblock(struct compiler *, enum fblocktype,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 basicblock *);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000176/* Returns true if there is a loop on the fblock stack. */
177static int compiler_in_loop(struct compiler *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000178
179static int inplace_binop(struct compiler *, operator_ty);
Georg Brandl8334fd92010-12-04 10:26:46 +0000180static int expr_constant(struct compiler *, expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000181
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500182static int compiler_with(struct compiler *, stmt_ty, int);
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000183static int compiler_call_helper(struct compiler *c, int n,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 asdl_seq *args,
185 asdl_seq *keywords,
186 expr_ty starargs,
187 expr_ty kwargs);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500188static int compiler_try_except(struct compiler *, stmt_ty);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000189
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000190static PyCodeObject *assemble(struct compiler *, int addNone);
191static PyObject *__doc__;
192
Benjamin Petersonb173f782009-05-05 22:31:58 +0000193#define COMPILER_CAPSULE_NAME_COMPILER_UNIT "compile.c compiler unit"
194
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000196_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 /* Name mangling: __private becomes _classname__private.
199 This is independent from how the name is used. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200200 PyObject *result;
201 size_t nlen, plen, ipriv;
202 Py_UCS4 maxchar;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200204 PyUnicode_READ_CHAR(ident, 0) != '_' ||
205 PyUnicode_READ_CHAR(ident, 1) != '_') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 Py_INCREF(ident);
207 return ident;
208 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200209 nlen = PyUnicode_GET_LENGTH(ident);
210 plen = PyUnicode_GET_LENGTH(privateobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 /* Don't mangle __id__ or names with dots.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 The only time a name with a dot can occur is when
214 we are compiling an import statement that has a
215 package name.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 TODO(jhylton): Decide whether we want to support
218 mangling of the module name, e.g. __M.X.
219 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200220 if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
221 PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
222 PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 Py_INCREF(ident);
224 return ident; /* Don't mangle __whatever__ */
225 }
226 /* Strip leading underscores from class name */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200227 ipriv = 0;
228 while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
229 ipriv++;
230 if (ipriv == plen) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 Py_INCREF(ident);
232 return ident; /* Don't mangle if class is just underscores */
233 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200234 plen -= ipriv;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 assert(1 <= PY_SSIZE_T_MAX - nlen);
237 assert(1 + nlen <= PY_SSIZE_T_MAX - plen);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000238
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200239 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
240 if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
241 maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
242
243 result = PyUnicode_New(1 + nlen + plen, maxchar);
244 if (!result)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200246 /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
247 PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
Victor Stinner6c7a52a2011-09-28 21:39:17 +0200248 if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
249 Py_DECREF(result);
250 return NULL;
251 }
252 if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
253 Py_DECREF(result);
254 return NULL;
255 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200256 return result;
Michael W. Hudson60934622004-08-12 17:56:29 +0000257}
258
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000259static int
260compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 c->c_stack = PyList_New(0);
265 if (!c->c_stack)
266 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000269}
270
271PyCodeObject *
Georg Brandl8334fd92010-12-04 10:26:46 +0000272PyAST_CompileEx(mod_ty mod, const char *filename, PyCompilerFlags *flags,
273 int optimize, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 struct compiler c;
276 PyCodeObject *co = NULL;
277 PyCompilerFlags local_flags;
278 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 if (!__doc__) {
281 __doc__ = PyUnicode_InternFromString("__doc__");
282 if (!__doc__)
283 return NULL;
284 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 if (!compiler_init(&c))
287 return NULL;
288 c.c_filename = filename;
Benjamin Peterson43b06862011-05-27 09:08:01 -0500289 c.c_filename_obj = PyUnicode_DecodeFSDefault(filename);
290 if (!c.c_filename_obj)
291 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 c.c_arena = arena;
293 c.c_future = PyFuture_FromAST(mod, filename);
294 if (c.c_future == NULL)
295 goto finally;
296 if (!flags) {
297 local_flags.cf_flags = 0;
298 flags = &local_flags;
299 }
300 merged = c.c_future->ff_features | flags->cf_flags;
301 c.c_future->ff_features = merged;
302 flags->cf_flags = merged;
303 c.c_flags = flags;
Georg Brandl8334fd92010-12-04 10:26:46 +0000304 c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 c.c_st = PySymtable_Build(mod, filename, c.c_future);
308 if (c.c_st == NULL) {
309 if (!PyErr_Occurred())
310 PyErr_SetString(PyExc_SystemError, "no symtable");
311 goto finally;
312 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000315
Thomas Wouters1175c432006-02-27 22:49:54 +0000316 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 compiler_free(&c);
318 assert(co || PyErr_Occurred());
319 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000320}
321
322PyCodeObject *
323PyNode_Compile(struct _node *n, const char *filename)
324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 PyCodeObject *co = NULL;
326 mod_ty mod;
327 PyArena *arena = PyArena_New();
328 if (!arena)
329 return NULL;
330 mod = PyAST_FromNode(n, NULL, filename, arena);
331 if (mod)
332 co = PyAST_Compile(mod, filename, NULL, arena);
333 PyArena_Free(arena);
334 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000335}
336
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000337static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000338compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 if (c->c_st)
341 PySymtable_Free(c->c_st);
342 if (c->c_future)
343 PyObject_Free(c->c_future);
Benjamin Peterson43b06862011-05-27 09:08:01 -0500344 if (c->c_filename_obj)
345 Py_DECREF(c->c_filename_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000347}
348
Guido van Rossum79f25d91997-04-29 20:08:16 +0000349static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000350list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 Py_ssize_t i, n;
353 PyObject *v, *k;
354 PyObject *dict = PyDict_New();
355 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 n = PyList_Size(list);
358 for (i = 0; i < n; i++) {
359 v = PyLong_FromLong(i);
360 if (!v) {
361 Py_DECREF(dict);
362 return NULL;
363 }
364 k = PyList_GET_ITEM(list, i);
365 k = PyTuple_Pack(2, k, k->ob_type);
366 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
367 Py_XDECREF(k);
368 Py_DECREF(v);
369 Py_DECREF(dict);
370 return NULL;
371 }
372 Py_DECREF(k);
373 Py_DECREF(v);
374 }
375 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000376}
377
378/* Return new dict containing names from src that match scope(s).
379
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000380src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000382values are integers, starting at offset and increasing by one for
383each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000384*/
385
386static PyObject *
387dictbytype(PyObject *src, int scope_type, int flag, int offset)
388{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 Py_ssize_t pos = 0, i = offset, scope;
390 PyObject *k, *v, *dest = PyDict_New();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 assert(offset >= 0);
393 if (dest == NULL)
394 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 while (PyDict_Next(src, &pos, &k, &v)) {
397 /* XXX this should probably be a macro in symtable.h */
398 long vi;
399 assert(PyLong_Check(v));
400 vi = PyLong_AS_LONG(v);
401 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 if (scope == scope_type || vi & flag) {
404 PyObject *tuple, *item = PyLong_FromLong(i);
405 if (item == NULL) {
406 Py_DECREF(dest);
407 return NULL;
408 }
409 i++;
410 tuple = PyTuple_Pack(2, k, k->ob_type);
411 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
412 Py_DECREF(item);
413 Py_DECREF(dest);
414 Py_XDECREF(tuple);
415 return NULL;
416 }
417 Py_DECREF(item);
418 Py_DECREF(tuple);
419 }
420 }
421 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000422}
423
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000424static void
425compiler_unit_check(struct compiler_unit *u)
426{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 basicblock *block;
428 for (block = u->u_blocks; block != NULL; block = block->b_list) {
429 assert((void *)block != (void *)0xcbcbcbcb);
430 assert((void *)block != (void *)0xfbfbfbfb);
431 assert((void *)block != (void *)0xdbdbdbdb);
432 if (block->b_instr != NULL) {
433 assert(block->b_ialloc > 0);
434 assert(block->b_iused > 0);
435 assert(block->b_ialloc >= block->b_iused);
436 }
437 else {
438 assert (block->b_iused == 0);
439 assert (block->b_ialloc == 0);
440 }
441 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000442}
443
444static void
445compiler_unit_free(struct compiler_unit *u)
446{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 compiler_unit_check(u);
450 b = u->u_blocks;
451 while (b != NULL) {
452 if (b->b_instr)
453 PyObject_Free((void *)b->b_instr);
454 next = b->b_list;
455 PyObject_Free((void *)b);
456 b = next;
457 }
458 Py_CLEAR(u->u_ste);
459 Py_CLEAR(u->u_name);
460 Py_CLEAR(u->u_consts);
461 Py_CLEAR(u->u_names);
462 Py_CLEAR(u->u_varnames);
463 Py_CLEAR(u->u_freevars);
464 Py_CLEAR(u->u_cellvars);
465 Py_CLEAR(u->u_private);
466 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000467}
468
469static int
470compiler_enter_scope(struct compiler *c, identifier name, void *key,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
476 struct compiler_unit));
477 if (!u) {
478 PyErr_NoMemory();
479 return 0;
480 }
481 memset(u, 0, sizeof(struct compiler_unit));
482 u->u_argcount = 0;
483 u->u_kwonlyargcount = 0;
484 u->u_ste = PySymtable_Lookup(c->c_st, key);
485 if (!u->u_ste) {
486 compiler_unit_free(u);
487 return 0;
488 }
489 Py_INCREF(name);
490 u->u_name = name;
491 u->u_varnames = list2dict(u->u_ste->ste_varnames);
492 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
493 if (!u->u_varnames || !u->u_cellvars) {
494 compiler_unit_free(u);
495 return 0;
496 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
499 PyDict_Size(u->u_cellvars));
500 if (!u->u_freevars) {
501 compiler_unit_free(u);
502 return 0;
503 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 u->u_blocks = NULL;
506 u->u_nfblocks = 0;
507 u->u_firstlineno = lineno;
508 u->u_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000509 u->u_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 u->u_lineno_set = 0;
511 u->u_consts = PyDict_New();
512 if (!u->u_consts) {
513 compiler_unit_free(u);
514 return 0;
515 }
516 u->u_names = PyDict_New();
517 if (!u->u_names) {
518 compiler_unit_free(u);
519 return 0;
520 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 /* Push the old compiler_unit on the stack. */
525 if (c->u) {
526 PyObject *capsule = PyCapsule_New(c->u, COMPILER_CAPSULE_NAME_COMPILER_UNIT, NULL);
527 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
528 Py_XDECREF(capsule);
529 compiler_unit_free(u);
530 return 0;
531 }
532 Py_DECREF(capsule);
533 u->u_private = c->u->u_private;
534 Py_XINCREF(u->u_private);
535 }
536 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 c->c_nestlevel++;
539 if (compiler_use_new_block(c) == NULL)
540 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543}
544
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000545static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546compiler_exit_scope(struct compiler *c)
547{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 int n;
549 PyObject *capsule;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 c->c_nestlevel--;
552 compiler_unit_free(c->u);
553 /* Restore c->u to the parent unit. */
554 n = PyList_GET_SIZE(c->c_stack) - 1;
555 if (n >= 0) {
556 capsule = PyList_GET_ITEM(c->c_stack, n);
557 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_CAPSULE_NAME_COMPILER_UNIT);
558 assert(c->u);
559 /* we are deleting from a list so this really shouldn't fail */
560 if (PySequence_DelItem(c->c_stack, n) < 0)
561 Py_FatalError("compiler_exit_scope()");
562 compiler_unit_check(c->u);
563 }
564 else
565 c->u = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000566
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567}
568
569/* Allocate a new block and return a pointer to it.
570 Returns NULL on error.
571*/
572
573static basicblock *
574compiler_new_block(struct compiler *c)
575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 basicblock *b;
577 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 u = c->u;
580 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
581 if (b == NULL) {
582 PyErr_NoMemory();
583 return NULL;
584 }
585 memset((void *)b, 0, sizeof(basicblock));
586 /* Extend the singly linked list of blocks with new block. */
587 b->b_list = u->u_blocks;
588 u->u_blocks = b;
589 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000590}
591
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592static basicblock *
593compiler_use_new_block(struct compiler *c)
594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 basicblock *block = compiler_new_block(c);
596 if (block == NULL)
597 return NULL;
598 c->u->u_curblock = block;
599 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600}
601
602static basicblock *
603compiler_next_block(struct compiler *c)
604{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 basicblock *block = compiler_new_block(c);
606 if (block == NULL)
607 return NULL;
608 c->u->u_curblock->b_next = block;
609 c->u->u_curblock = block;
610 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000611}
612
613static basicblock *
614compiler_use_next_block(struct compiler *c, basicblock *block)
615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 assert(block != NULL);
617 c->u->u_curblock->b_next = block;
618 c->u->u_curblock = block;
619 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000620}
621
622/* Returns the offset of the next instruction in the current block's
623 b_instr array. Resizes the b_instr as necessary.
624 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000625*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000626
627static int
628compiler_next_instr(struct compiler *c, basicblock *b)
629{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 assert(b != NULL);
631 if (b->b_instr == NULL) {
632 b->b_instr = (struct instr *)PyObject_Malloc(
633 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
634 if (b->b_instr == NULL) {
635 PyErr_NoMemory();
636 return -1;
637 }
638 b->b_ialloc = DEFAULT_BLOCK_SIZE;
639 memset((char *)b->b_instr, 0,
640 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
641 }
642 else if (b->b_iused == b->b_ialloc) {
643 struct instr *tmp;
644 size_t oldsize, newsize;
645 oldsize = b->b_ialloc * sizeof(struct instr);
646 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 if (oldsize > (PY_SIZE_MAX >> 1)) {
649 PyErr_NoMemory();
650 return -1;
651 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 if (newsize == 0) {
654 PyErr_NoMemory();
655 return -1;
656 }
657 b->b_ialloc <<= 1;
658 tmp = (struct instr *)PyObject_Realloc(
659 (void *)b->b_instr, newsize);
660 if (tmp == NULL) {
661 PyErr_NoMemory();
662 return -1;
663 }
664 b->b_instr = tmp;
665 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
666 }
667 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668}
669
Christian Heimes2202f872008-02-06 14:31:34 +0000670/* Set the i_lineno member of the instruction at offset off if the
671 line number for the current expression/statement has not
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000672 already been set. If it has been set, the call has no effect.
673
Christian Heimes2202f872008-02-06 14:31:34 +0000674 The line number is reset in the following cases:
675 - when entering a new scope
676 - on each statement
677 - on each expression that start a new line
678 - before the "except" clause
679 - before the "for" and "while" expressions
Thomas Wouters89f507f2006-12-13 04:49:30 +0000680*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000681
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000682static void
683compiler_set_lineno(struct compiler *c, int off)
684{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 basicblock *b;
686 if (c->u->u_lineno_set)
687 return;
688 c->u->u_lineno_set = 1;
689 b = c->u->u_curblock;
690 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000691}
692
693static int
694opcode_stack_effect(int opcode, int oparg)
695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 switch (opcode) {
697 case POP_TOP:
698 return -1;
699 case ROT_TWO:
700 case ROT_THREE:
701 return 0;
702 case DUP_TOP:
703 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000704 case DUP_TOP_TWO:
705 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 case UNARY_POSITIVE:
708 case UNARY_NEGATIVE:
709 case UNARY_NOT:
710 case UNARY_INVERT:
711 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 case SET_ADD:
714 case LIST_APPEND:
715 return -1;
716 case MAP_ADD:
717 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 case BINARY_POWER:
720 case BINARY_MULTIPLY:
721 case BINARY_MODULO:
722 case BINARY_ADD:
723 case BINARY_SUBTRACT:
724 case BINARY_SUBSCR:
725 case BINARY_FLOOR_DIVIDE:
726 case BINARY_TRUE_DIVIDE:
727 return -1;
728 case INPLACE_FLOOR_DIVIDE:
729 case INPLACE_TRUE_DIVIDE:
730 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 case INPLACE_ADD:
733 case INPLACE_SUBTRACT:
734 case INPLACE_MULTIPLY:
735 case INPLACE_MODULO:
736 return -1;
737 case STORE_SUBSCR:
738 return -3;
739 case STORE_MAP:
740 return -2;
741 case DELETE_SUBSCR:
742 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 case BINARY_LSHIFT:
745 case BINARY_RSHIFT:
746 case BINARY_AND:
747 case BINARY_XOR:
748 case BINARY_OR:
749 return -1;
750 case INPLACE_POWER:
751 return -1;
752 case GET_ITER:
753 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 case PRINT_EXPR:
756 return -1;
757 case LOAD_BUILD_CLASS:
758 return 1;
759 case INPLACE_LSHIFT:
760 case INPLACE_RSHIFT:
761 case INPLACE_AND:
762 case INPLACE_XOR:
763 case INPLACE_OR:
764 return -1;
765 case BREAK_LOOP:
766 return 0;
767 case SETUP_WITH:
768 return 7;
769 case WITH_CLEANUP:
770 return -1; /* XXX Sometimes more */
771 case STORE_LOCALS:
772 return -1;
773 case RETURN_VALUE:
774 return -1;
775 case IMPORT_STAR:
776 return -1;
777 case YIELD_VALUE:
778 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 case POP_BLOCK:
781 return 0;
782 case POP_EXCEPT:
783 return 0; /* -3 except if bad bytecode */
784 case END_FINALLY:
785 return -1; /* or -2 or -3 if exception occurred */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 case STORE_NAME:
788 return -1;
789 case DELETE_NAME:
790 return 0;
791 case UNPACK_SEQUENCE:
792 return oparg-1;
793 case UNPACK_EX:
794 return (oparg&0xFF) + (oparg>>8);
795 case FOR_ITER:
796 return 1; /* or -1, at end of iterator */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 case STORE_ATTR:
799 return -2;
800 case DELETE_ATTR:
801 return -1;
802 case STORE_GLOBAL:
803 return -1;
804 case DELETE_GLOBAL:
805 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 case LOAD_CONST:
807 return 1;
808 case LOAD_NAME:
809 return 1;
810 case BUILD_TUPLE:
811 case BUILD_LIST:
812 case BUILD_SET:
813 return 1-oparg;
814 case BUILD_MAP:
815 return 1;
816 case LOAD_ATTR:
817 return 0;
818 case COMPARE_OP:
819 return -1;
820 case IMPORT_NAME:
821 return -1;
822 case IMPORT_FROM:
823 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 case JUMP_FORWARD:
826 case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */
827 case JUMP_IF_FALSE_OR_POP: /* "" */
828 case JUMP_ABSOLUTE:
829 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 case POP_JUMP_IF_FALSE:
832 case POP_JUMP_IF_TRUE:
833 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 case LOAD_GLOBAL:
836 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 case CONTINUE_LOOP:
839 return 0;
840 case SETUP_LOOP:
841 return 0;
842 case SETUP_EXCEPT:
843 case SETUP_FINALLY:
844 return 6; /* can push 3 values for the new exception
845 + 3 others for the previous exception state */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 case LOAD_FAST:
848 return 1;
849 case STORE_FAST:
850 return -1;
851 case DELETE_FAST:
852 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 case RAISE_VARARGS:
855 return -oparg;
Neal Norwitzc1505362006-12-28 06:47:50 +0000856#define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 case CALL_FUNCTION:
858 return -NARGS(oparg);
859 case CALL_FUNCTION_VAR:
860 case CALL_FUNCTION_KW:
861 return -NARGS(oparg)-1;
862 case CALL_FUNCTION_VAR_KW:
863 return -NARGS(oparg)-2;
864 case MAKE_FUNCTION:
865 return -NARGS(oparg) - ((oparg >> 16) & 0xffff);
866 case MAKE_CLOSURE:
867 return -1 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000868#undef NARGS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 case BUILD_SLICE:
870 if (oparg == 3)
871 return -2;
872 else
873 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 case LOAD_CLOSURE:
876 return 1;
877 case LOAD_DEREF:
878 return 1;
879 case STORE_DEREF:
880 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000881 case DELETE_DEREF:
882 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 default:
884 fprintf(stderr, "opcode = %d\n", opcode);
885 Py_FatalError("opcode_stack_effect()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 }
888 return 0; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000889}
890
891/* Add an opcode with no argument.
892 Returns 0 on failure, 1 on success.
893*/
894
895static int
896compiler_addop(struct compiler *c, int opcode)
897{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 basicblock *b;
899 struct instr *i;
900 int off;
901 off = compiler_next_instr(c, c->u->u_curblock);
902 if (off < 0)
903 return 0;
904 b = c->u->u_curblock;
905 i = &b->b_instr[off];
906 i->i_opcode = opcode;
907 i->i_hasarg = 0;
908 if (opcode == RETURN_VALUE)
909 b->b_return = 1;
910 compiler_set_lineno(c, off);
911 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000912}
913
914static int
915compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 PyObject *t, *v;
918 Py_ssize_t arg;
919 double d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 /* necessary to make sure types aren't coerced (e.g., int and long) */
922 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
923 if (PyFloat_Check(o)) {
924 d = PyFloat_AS_DOUBLE(o);
925 /* all we need is to make the tuple different in either the 0.0
926 * or -0.0 case from all others, just to avoid the "coercion".
927 */
928 if (d == 0.0 && copysign(1.0, d) < 0.0)
929 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
930 else
931 t = PyTuple_Pack(2, o, o->ob_type);
932 }
933 else if (PyComplex_Check(o)) {
934 Py_complex z;
935 int real_negzero, imag_negzero;
936 /* For the complex case we must make complex(x, 0.)
937 different from complex(x, -0.) and complex(0., y)
938 different from complex(-0., y), for any x and y.
939 All four complex zeros must be distinguished.*/
940 z = PyComplex_AsCComplex(o);
941 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
942 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
943 if (real_negzero && imag_negzero) {
944 t = PyTuple_Pack(5, o, o->ob_type,
945 Py_None, Py_None, Py_None);
Christian Heimes400adb02008-02-01 08:12:03 +0000946 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 else if (imag_negzero) {
948 t = PyTuple_Pack(4, o, o->ob_type, Py_None, Py_None);
Guido van Rossum04110fb2007-08-24 16:32:05 +0000949 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 else if (real_negzero) {
951 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
952 }
953 else {
954 t = PyTuple_Pack(2, o, o->ob_type);
955 }
956 }
957 else {
958 t = PyTuple_Pack(2, o, o->ob_type);
959 }
960 if (t == NULL)
961 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 v = PyDict_GetItem(dict, t);
964 if (!v) {
965 if (PyErr_Occurred())
966 return -1;
967 arg = PyDict_Size(dict);
968 v = PyLong_FromLong(arg);
969 if (!v) {
970 Py_DECREF(t);
971 return -1;
972 }
973 if (PyDict_SetItem(dict, t, v) < 0) {
974 Py_DECREF(t);
975 Py_DECREF(v);
976 return -1;
977 }
978 Py_DECREF(v);
979 }
980 else
981 arg = PyLong_AsLong(v);
982 Py_DECREF(t);
983 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984}
985
986static int
987compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989{
990 int arg = compiler_add_o(c, dict, o);
991 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +0000992 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000993 return compiler_addop_i(c, opcode, arg);
994}
995
996static int
997compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999{
1000 int arg;
1001 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1002 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001003 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004 arg = compiler_add_o(c, dict, mangled);
1005 Py_DECREF(mangled);
1006 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001007 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008 return compiler_addop_i(c, opcode, arg);
1009}
1010
1011/* Add an opcode with an integer argument.
1012 Returns 0 on failure, 1 on success.
1013*/
1014
1015static int
1016compiler_addop_i(struct compiler *c, int opcode, int oparg)
1017{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 struct instr *i;
1019 int off;
1020 off = compiler_next_instr(c, c->u->u_curblock);
1021 if (off < 0)
1022 return 0;
1023 i = &c->u->u_curblock->b_instr[off];
1024 i->i_opcode = opcode;
1025 i->i_oparg = oparg;
1026 i->i_hasarg = 1;
1027 compiler_set_lineno(c, off);
1028 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029}
1030
1031static int
1032compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1033{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 struct instr *i;
1035 int off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 assert(b != NULL);
1038 off = compiler_next_instr(c, c->u->u_curblock);
1039 if (off < 0)
1040 return 0;
1041 i = &c->u->u_curblock->b_instr[off];
1042 i->i_opcode = opcode;
1043 i->i_target = b;
1044 i->i_hasarg = 1;
1045 if (absolute)
1046 i->i_jabs = 1;
1047 else
1048 i->i_jrel = 1;
1049 compiler_set_lineno(c, off);
1050 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001051}
1052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1054 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055 it as the current block. NEXT_BLOCK() also creates an implicit jump
1056 from the current block to the new block.
1057*/
1058
Thomas Wouters89f507f2006-12-13 04:49:30 +00001059/* The returns inside these macros make it impossible to decref objects
1060 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061*/
1062
1063
1064#define NEW_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 if (compiler_use_new_block((C)) == NULL) \
1066 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001067}
1068
1069#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 if (compiler_next_block((C)) == NULL) \
1071 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072}
1073
1074#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 if (!compiler_addop((C), (OP))) \
1076 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077}
1078
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001079#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 if (!compiler_addop((C), (OP))) { \
1081 compiler_exit_scope(c); \
1082 return 0; \
1083 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001084}
1085
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1088 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089}
1090
1091#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1093 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001094}
1095
1096#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 if (!compiler_addop_i((C), (OP), (O))) \
1098 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099}
1100
1101#define ADDOP_JABS(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 if (!compiler_addop_j((C), (OP), (O), 1)) \
1103 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001104}
1105
1106#define ADDOP_JREL(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 if (!compiler_addop_j((C), (OP), (O), 0)) \
1108 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109}
1110
1111/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1112 the ASDL name to synthesize the name of the C type and the visit function.
1113*/
1114
1115#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 if (!compiler_visit_ ## TYPE((C), (V))) \
1117 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001118}
1119
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001120#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 if (!compiler_visit_ ## TYPE((C), (V))) { \
1122 compiler_exit_scope(c); \
1123 return 0; \
1124 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001125}
1126
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 if (!compiler_visit_slice((C), (V), (CTX))) \
1129 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130}
1131
1132#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 int _i; \
1134 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1135 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1136 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1137 if (!compiler_visit_ ## TYPE((C), elt)) \
1138 return 0; \
1139 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001140}
1141
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001142#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 int _i; \
1144 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1145 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1146 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1147 if (!compiler_visit_ ## TYPE((C), elt)) { \
1148 compiler_exit_scope(c); \
1149 return 0; \
1150 } \
1151 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001152}
1153
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154static int
1155compiler_isdocstring(stmt_ty s)
1156{
1157 if (s->kind != Expr_kind)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001158 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159 return s->v.Expr.value->kind == Str_kind;
1160}
1161
1162/* Compile a sequence of statements, checking for a docstring. */
1163
1164static int
1165compiler_body(struct compiler *c, asdl_seq *stmts)
1166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 int i = 0;
1168 stmt_ty st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 if (!asdl_seq_LEN(stmts))
1171 return 1;
1172 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Georg Brandl8334fd92010-12-04 10:26:46 +00001173 if (compiler_isdocstring(st) && c->c_optimize < 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 /* don't generate docstrings if -OO */
1175 i = 1;
1176 VISIT(c, expr, st->v.Expr.value);
1177 if (!compiler_nameop(c, __doc__, Store))
1178 return 0;
1179 }
1180 for (; i < asdl_seq_LEN(stmts); i++)
1181 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
1182 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001183}
1184
1185static PyCodeObject *
1186compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 PyCodeObject *co;
1189 int addNone = 1;
1190 static PyObject *module;
1191 if (!module) {
1192 module = PyUnicode_InternFromString("<module>");
1193 if (!module)
1194 return NULL;
1195 }
1196 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1197 if (!compiler_enter_scope(c, module, mod, 0))
1198 return NULL;
1199 switch (mod->kind) {
1200 case Module_kind:
1201 if (!compiler_body(c, mod->v.Module.body)) {
1202 compiler_exit_scope(c);
1203 return 0;
1204 }
1205 break;
1206 case Interactive_kind:
1207 c->c_interactive = 1;
1208 VISIT_SEQ_IN_SCOPE(c, stmt,
1209 mod->v.Interactive.body);
1210 break;
1211 case Expression_kind:
1212 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1213 addNone = 0;
1214 break;
1215 case Suite_kind:
1216 PyErr_SetString(PyExc_SystemError,
1217 "suite should not be possible");
1218 return 0;
1219 default:
1220 PyErr_Format(PyExc_SystemError,
1221 "module kind %d should not be possible",
1222 mod->kind);
1223 return 0;
1224 }
1225 co = assemble(c, addNone);
1226 compiler_exit_scope(c);
1227 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001228}
1229
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230/* The test for LOCAL must come before the test for FREE in order to
1231 handle classes where name is both local and free. The local var is
1232 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001233*/
1234
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235static int
1236get_ref_type(struct compiler *c, PyObject *name)
1237{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 int scope = PyST_GetScope(c->u->u_ste, name);
1239 if (scope == 0) {
1240 char buf[350];
1241 PyOS_snprintf(buf, sizeof(buf),
1242 "unknown scope for %.100s in %.100s(%s) in %s\n"
1243 "symbols: %s\nlocals: %s\nglobals: %s",
1244 PyBytes_AS_STRING(name),
1245 PyBytes_AS_STRING(c->u->u_name),
1246 PyObject_REPR(c->u->u_ste->ste_id),
1247 c->c_filename,
1248 PyObject_REPR(c->u->u_ste->ste_symbols),
1249 PyObject_REPR(c->u->u_varnames),
1250 PyObject_REPR(c->u->u_names)
1251 );
1252 Py_FatalError(buf);
1253 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001256}
1257
1258static int
1259compiler_lookup_arg(PyObject *dict, PyObject *name)
1260{
1261 PyObject *k, *v;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001262 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001263 if (k == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001264 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001266 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001268 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001269 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270}
1271
1272static int
1273compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 int i, free = PyCode_GetNumFree(co);
1276 if (free == 0) {
1277 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1278 ADDOP_I(c, MAKE_FUNCTION, args);
1279 return 1;
1280 }
1281 for (i = 0; i < free; ++i) {
1282 /* Bypass com_addop_varname because it will generate
1283 LOAD_DEREF but LOAD_CLOSURE is needed.
1284 */
1285 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1286 int arg, reftype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 /* Special case: If a class contains a method with a
1289 free variable that has the same name as a method,
1290 the name will be considered free *and* local in the
1291 class. It should be handled by the closure, as
1292 well as by the normal name loookup logic.
1293 */
1294 reftype = get_ref_type(c, name);
1295 if (reftype == CELL)
1296 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1297 else /* (reftype == FREE) */
1298 arg = compiler_lookup_arg(c->u->u_freevars, name);
1299 if (arg == -1) {
1300 fprintf(stderr,
1301 "lookup %s in %s %d %d\n"
1302 "freevars of %s: %s\n",
1303 PyObject_REPR(name),
1304 PyBytes_AS_STRING(c->u->u_name),
1305 reftype, arg,
1306 _PyUnicode_AsString(co->co_name),
1307 PyObject_REPR(co->co_freevars));
1308 Py_FatalError("compiler_make_closure()");
1309 }
1310 ADDOP_I(c, LOAD_CLOSURE, arg);
1311 }
1312 ADDOP_I(c, BUILD_TUPLE, free);
1313 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1314 ADDOP_I(c, MAKE_CLOSURE, args);
1315 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316}
1317
1318static int
1319compiler_decorators(struct compiler *c, asdl_seq* decos)
1320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 if (!decos)
1324 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1327 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1328 }
1329 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001330}
1331
1332static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001333compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 asdl_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 int i, default_count = 0;
1337 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1338 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1339 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1340 if (default_) {
1341 ADDOP_O(c, LOAD_CONST, arg->arg, consts);
1342 if (!compiler_visit_expr(c, default_)) {
1343 return -1;
1344 }
1345 default_count++;
1346 }
1347 }
1348 return default_count;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001349}
1350
1351static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001352compiler_visit_argannotation(struct compiler *c, identifier id,
1353 expr_ty annotation, PyObject *names)
1354{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 if (annotation) {
1356 VISIT(c, expr, annotation);
1357 if (PyList_Append(names, id))
1358 return -1;
1359 }
1360 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001361}
1362
1363static int
1364compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1365 PyObject *names)
1366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 int i, error;
1368 for (i = 0; i < asdl_seq_LEN(args); i++) {
1369 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1370 error = compiler_visit_argannotation(
1371 c,
1372 arg->arg,
1373 arg->annotation,
1374 names);
1375 if (error)
1376 return error;
1377 }
1378 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001379}
1380
1381static int
1382compiler_visit_annotations(struct compiler *c, arguments_ty args,
1383 expr_ty returns)
1384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 /* Push arg annotations and a list of the argument names. Return the #
1386 of items pushed. The expressions are evaluated out-of-order wrt the
1387 source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00001388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1390 */
1391 static identifier return_str;
1392 PyObject *names;
1393 int len;
1394 names = PyList_New(0);
1395 if (!names)
1396 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 if (compiler_visit_argannotations(c, args->args, names))
1399 goto error;
1400 if (args->varargannotation &&
1401 compiler_visit_argannotation(c, args->vararg,
1402 args->varargannotation, names))
1403 goto error;
1404 if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1405 goto error;
1406 if (args->kwargannotation &&
1407 compiler_visit_argannotation(c, args->kwarg,
1408 args->kwargannotation, names))
1409 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 if (!return_str) {
1412 return_str = PyUnicode_InternFromString("return");
1413 if (!return_str)
1414 goto error;
1415 }
1416 if (compiler_visit_argannotation(c, return_str, returns, names)) {
1417 goto error;
1418 }
1419
1420 len = PyList_GET_SIZE(names);
1421 if (len > 65534) {
1422 /* len must fit in 16 bits, and len is incremented below */
1423 PyErr_SetString(PyExc_SyntaxError,
1424 "too many annotations");
1425 goto error;
1426 }
1427 if (len) {
1428 /* convert names to a tuple and place on stack */
1429 PyObject *elt;
1430 int i;
1431 PyObject *s = PyTuple_New(len);
1432 if (!s)
1433 goto error;
1434 for (i = 0; i < len; i++) {
1435 elt = PyList_GET_ITEM(names, i);
1436 Py_INCREF(elt);
1437 PyTuple_SET_ITEM(s, i, elt);
1438 }
1439 ADDOP_O(c, LOAD_CONST, s, consts);
1440 Py_DECREF(s);
1441 len++; /* include the just-pushed tuple */
1442 }
1443 Py_DECREF(names);
1444 return len;
Neal Norwitzc1505362006-12-28 06:47:50 +00001445
1446error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 Py_DECREF(names);
1448 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001449}
1450
1451static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452compiler_function(struct compiler *c, stmt_ty s)
1453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 PyCodeObject *co;
1455 PyObject *first_const = Py_None;
1456 arguments_ty args = s->v.FunctionDef.args;
1457 expr_ty returns = s->v.FunctionDef.returns;
1458 asdl_seq* decos = s->v.FunctionDef.decorator_list;
1459 stmt_ty st;
1460 int i, n, docstring, kw_default_count = 0, arglength;
1461 int num_annotations;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 assert(s->kind == FunctionDef_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 if (!compiler_decorators(c, decos))
1466 return 0;
1467 if (args->kwonlyargs) {
1468 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1469 args->kw_defaults);
1470 if (res < 0)
1471 return 0;
1472 kw_default_count = res;
1473 }
1474 if (args->defaults)
1475 VISIT_SEQ(c, expr, args->defaults);
1476 num_annotations = compiler_visit_annotations(c, args, returns);
1477 if (num_annotations < 0)
1478 return 0;
1479 assert((num_annotations & 0xFFFF) == num_annotations);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1482 s->lineno))
1483 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
1486 docstring = compiler_isdocstring(st);
Georg Brandl8334fd92010-12-04 10:26:46 +00001487 if (docstring && c->c_optimize < 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 first_const = st->v.Expr.value->v.Str.s;
1489 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1490 compiler_exit_scope(c);
1491 return 0;
1492 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 c->u->u_argcount = asdl_seq_LEN(args->args);
1495 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1496 n = asdl_seq_LEN(s->v.FunctionDef.body);
1497 /* if there was a docstring, we need to skip the first statement */
1498 for (i = docstring; i < n; i++) {
1499 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1500 VISIT_IN_SCOPE(c, stmt, st);
1501 }
1502 co = assemble(c, 1);
1503 compiler_exit_scope(c);
1504 if (co == NULL)
1505 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 arglength = asdl_seq_LEN(args->defaults);
1508 arglength |= kw_default_count << 8;
1509 arglength |= num_annotations << 16;
1510 compiler_make_closure(c, co, arglength);
1511 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 /* decorators */
1514 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1515 ADDOP_I(c, CALL_FUNCTION, 1);
1516 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 return compiler_nameop(c, s->v.FunctionDef.name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001519}
1520
1521static int
1522compiler_class(struct compiler *c, stmt_ty s)
1523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 PyCodeObject *co;
1525 PyObject *str;
1526 int i;
1527 asdl_seq* decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 if (!compiler_decorators(c, decos))
1530 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 /* ultimately generate code for:
1533 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1534 where:
1535 <func> is a function/closure created from the class body;
1536 it has a single argument (__locals__) where the dict
1537 (or MutableSequence) representing the locals is passed
1538 <name> is the class name
1539 <bases> is the positional arguments and *varargs argument
1540 <keywords> is the keyword arguments and **kwds argument
1541 This borrows from compiler_call.
1542 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 /* 1. compile the class body into a code object */
1545 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, s->lineno))
1546 return 0;
1547 /* this block represents what we do in the new scope */
1548 {
1549 /* use the class name for name mangling */
1550 Py_INCREF(s->v.ClassDef.name);
1551 Py_XDECREF(c->u->u_private);
1552 c->u->u_private = s->v.ClassDef.name;
1553 /* force it to have one mandatory argument */
1554 c->u->u_argcount = 1;
1555 /* load the first argument (__locals__) ... */
1556 ADDOP_I(c, LOAD_FAST, 0);
1557 /* ... and store it into f_locals */
1558 ADDOP_IN_SCOPE(c, STORE_LOCALS);
1559 /* load (global) __name__ ... */
1560 str = PyUnicode_InternFromString("__name__");
1561 if (!str || !compiler_nameop(c, str, Load)) {
1562 Py_XDECREF(str);
1563 compiler_exit_scope(c);
1564 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001565 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 Py_DECREF(str);
1567 /* ... and store it as __module__ */
1568 str = PyUnicode_InternFromString("__module__");
1569 if (!str || !compiler_nameop(c, str, Store)) {
1570 Py_XDECREF(str);
1571 compiler_exit_scope(c);
1572 return 0;
1573 }
1574 Py_DECREF(str);
1575 /* compile the body proper */
1576 if (!compiler_body(c, s->v.ClassDef.body)) {
1577 compiler_exit_scope(c);
1578 return 0;
1579 }
1580 /* return the (empty) __class__ cell */
Benjamin Petersonf5ff2232011-06-19 19:42:22 -05001581 str = PyUnicode_InternFromString("@__class__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 if (str == NULL) {
1583 compiler_exit_scope(c);
1584 return 0;
1585 }
1586 i = compiler_lookup_arg(c->u->u_cellvars, str);
1587 Py_DECREF(str);
1588 if (i == -1) {
1589 /* This happens when nobody references the cell */
1590 PyErr_Clear();
1591 /* Return None */
1592 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1593 }
1594 else {
1595 /* Return the cell where to store __class__ */
1596 ADDOP_I(c, LOAD_CLOSURE, i);
1597 }
1598 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1599 /* create the code object */
1600 co = assemble(c, 1);
1601 }
1602 /* leave the new scope */
1603 compiler_exit_scope(c);
1604 if (co == NULL)
1605 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 /* 2. load the 'build_class' function */
1608 ADDOP(c, LOAD_BUILD_CLASS);
1609
1610 /* 3. load a function (or closure) made from the code object */
1611 compiler_make_closure(c, co, 0);
1612 Py_DECREF(co);
1613
1614 /* 4. load class name */
1615 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1616
1617 /* 5. generate the rest of the code for the call */
1618 if (!compiler_call_helper(c, 2,
1619 s->v.ClassDef.bases,
1620 s->v.ClassDef.keywords,
1621 s->v.ClassDef.starargs,
1622 s->v.ClassDef.kwargs))
1623 return 0;
1624
1625 /* 6. apply decorators */
1626 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1627 ADDOP_I(c, CALL_FUNCTION, 1);
1628 }
1629
1630 /* 7. store into <name> */
1631 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1632 return 0;
1633 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001634}
1635
1636static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001637compiler_ifexp(struct compiler *c, expr_ty e)
1638{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 basicblock *end, *next;
1640
1641 assert(e->kind == IfExp_kind);
1642 end = compiler_new_block(c);
1643 if (end == NULL)
1644 return 0;
1645 next = compiler_new_block(c);
1646 if (next == NULL)
1647 return 0;
1648 VISIT(c, expr, e->v.IfExp.test);
1649 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1650 VISIT(c, expr, e->v.IfExp.body);
1651 ADDOP_JREL(c, JUMP_FORWARD, end);
1652 compiler_use_next_block(c, next);
1653 VISIT(c, expr, e->v.IfExp.orelse);
1654 compiler_use_next_block(c, end);
1655 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001656}
1657
1658static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659compiler_lambda(struct compiler *c, expr_ty e)
1660{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 PyCodeObject *co;
1662 static identifier name;
1663 int kw_default_count = 0, arglength;
1664 arguments_ty args = e->v.Lambda.args;
1665 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 if (!name) {
1668 name = PyUnicode_InternFromString("<lambda>");
1669 if (!name)
1670 return 0;
1671 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 if (args->kwonlyargs) {
1674 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1675 args->kw_defaults);
1676 if (res < 0) return 0;
1677 kw_default_count = res;
1678 }
1679 if (args->defaults)
1680 VISIT_SEQ(c, expr, args->defaults);
1681 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1682 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 /* Make None the first constant, so the lambda can't have a
1685 docstring. */
1686 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0)
1687 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 c->u->u_argcount = asdl_seq_LEN(args->args);
1690 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1691 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1692 if (c->u->u_ste->ste_generator) {
1693 ADDOP_IN_SCOPE(c, POP_TOP);
1694 }
1695 else {
1696 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1697 }
1698 co = assemble(c, 1);
1699 compiler_exit_scope(c);
1700 if (co == NULL)
1701 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 arglength = asdl_seq_LEN(args->defaults);
1704 arglength |= kw_default_count << 8;
1705 compiler_make_closure(c, co, arglength);
1706 Py_DECREF(co);
1707
1708 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001709}
1710
1711static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001712compiler_if(struct compiler *c, stmt_ty s)
1713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 basicblock *end, *next;
1715 int constant;
1716 assert(s->kind == If_kind);
1717 end = compiler_new_block(c);
1718 if (end == NULL)
1719 return 0;
1720
Georg Brandl8334fd92010-12-04 10:26:46 +00001721 constant = expr_constant(c, s->v.If.test);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 /* constant = 0: "if 0"
1723 * constant = 1: "if 1", "if 2", ...
1724 * constant = -1: rest */
1725 if (constant == 0) {
1726 if (s->v.If.orelse)
1727 VISIT_SEQ(c, stmt, s->v.If.orelse);
1728 } else if (constant == 1) {
1729 VISIT_SEQ(c, stmt, s->v.If.body);
1730 } else {
1731 if (s->v.If.orelse) {
1732 next = compiler_new_block(c);
1733 if (next == NULL)
1734 return 0;
1735 }
1736 else
1737 next = end;
1738 VISIT(c, expr, s->v.If.test);
1739 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1740 VISIT_SEQ(c, stmt, s->v.If.body);
1741 ADDOP_JREL(c, JUMP_FORWARD, end);
1742 if (s->v.If.orelse) {
1743 compiler_use_next_block(c, next);
1744 VISIT_SEQ(c, stmt, s->v.If.orelse);
1745 }
1746 }
1747 compiler_use_next_block(c, end);
1748 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749}
1750
1751static int
1752compiler_for(struct compiler *c, stmt_ty s)
1753{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 basicblock *start, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 start = compiler_new_block(c);
1757 cleanup = compiler_new_block(c);
1758 end = compiler_new_block(c);
1759 if (start == NULL || end == NULL || cleanup == NULL)
1760 return 0;
1761 ADDOP_JREL(c, SETUP_LOOP, end);
1762 if (!compiler_push_fblock(c, LOOP, start))
1763 return 0;
1764 VISIT(c, expr, s->v.For.iter);
1765 ADDOP(c, GET_ITER);
1766 compiler_use_next_block(c, start);
1767 ADDOP_JREL(c, FOR_ITER, cleanup);
1768 VISIT(c, expr, s->v.For.target);
1769 VISIT_SEQ(c, stmt, s->v.For.body);
1770 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1771 compiler_use_next_block(c, cleanup);
1772 ADDOP(c, POP_BLOCK);
1773 compiler_pop_fblock(c, LOOP, start);
1774 VISIT_SEQ(c, stmt, s->v.For.orelse);
1775 compiler_use_next_block(c, end);
1776 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001777}
1778
1779static int
1780compiler_while(struct compiler *c, stmt_ty s)
1781{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 basicblock *loop, *orelse, *end, *anchor = NULL;
Georg Brandl8334fd92010-12-04 10:26:46 +00001783 int constant = expr_constant(c, s->v.While.test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 if (constant == 0) {
1786 if (s->v.While.orelse)
1787 VISIT_SEQ(c, stmt, s->v.While.orelse);
1788 return 1;
1789 }
1790 loop = compiler_new_block(c);
1791 end = compiler_new_block(c);
1792 if (constant == -1) {
1793 anchor = compiler_new_block(c);
1794 if (anchor == NULL)
1795 return 0;
1796 }
1797 if (loop == NULL || end == NULL)
1798 return 0;
1799 if (s->v.While.orelse) {
1800 orelse = compiler_new_block(c);
1801 if (orelse == NULL)
1802 return 0;
1803 }
1804 else
1805 orelse = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 ADDOP_JREL(c, SETUP_LOOP, end);
1808 compiler_use_next_block(c, loop);
1809 if (!compiler_push_fblock(c, LOOP, loop))
1810 return 0;
1811 if (constant == -1) {
1812 VISIT(c, expr, s->v.While.test);
1813 ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);
1814 }
1815 VISIT_SEQ(c, stmt, s->v.While.body);
1816 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 /* XXX should the two POP instructions be in a separate block
1819 if there is no else clause ?
1820 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 if (constant == -1) {
1823 compiler_use_next_block(c, anchor);
1824 ADDOP(c, POP_BLOCK);
1825 }
1826 compiler_pop_fblock(c, LOOP, loop);
1827 if (orelse != NULL) /* what if orelse is just pass? */
1828 VISIT_SEQ(c, stmt, s->v.While.orelse);
1829 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001832}
1833
1834static int
1835compiler_continue(struct compiler *c)
1836{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
1838 static const char IN_FINALLY_ERROR_MSG[] =
1839 "'continue' not supported inside 'finally' clause";
1840 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842 if (!c->u->u_nfblocks)
1843 return compiler_error(c, LOOP_ERROR_MSG);
1844 i = c->u->u_nfblocks - 1;
1845 switch (c->u->u_fblock[i].fb_type) {
1846 case LOOP:
1847 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1848 break;
1849 case EXCEPT:
1850 case FINALLY_TRY:
1851 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1852 /* Prevent continue anywhere under a finally
1853 even if hidden in a sub-try or except. */
1854 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1855 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1856 }
1857 if (i == -1)
1858 return compiler_error(c, LOOP_ERROR_MSG);
1859 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1860 break;
1861 case FINALLY_END:
1862 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1863 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001866}
1867
1868/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869
1870 SETUP_FINALLY L
1871 <code for body>
1872 POP_BLOCK
1873 LOAD_CONST <None>
1874 L: <code for finalbody>
1875 END_FINALLY
1876
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001877 The special instructions use the block stack. Each block
1878 stack entry contains the instruction that created it (here
1879 SETUP_FINALLY), the level of the value stack at the time the
1880 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001882 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 Pushes the current value stack level and the label
1884 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885 POP_BLOCK:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 Pops en entry from the block stack, and pops the value
1887 stack until its level is the same as indicated on the
1888 block stack. (The label is ignored.)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001889 END_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 Pops a variable number of entries from the *value* stack
1891 and re-raises the exception they specify. The number of
1892 entries popped depends on the (pseudo) exception type.
1893
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001894 The block stack is unwound when an exception is raised:
1895 when a SETUP_FINALLY entry is found, the exception is pushed
1896 onto the value stack (and the exception condition is cleared),
1897 and the interpreter jumps to the label gotten from the block
1898 stack.
1899*/
1900
1901static int
1902compiler_try_finally(struct compiler *c, stmt_ty s)
1903{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 basicblock *body, *end;
1905 body = compiler_new_block(c);
1906 end = compiler_new_block(c);
1907 if (body == NULL || end == NULL)
1908 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 ADDOP_JREL(c, SETUP_FINALLY, end);
1911 compiler_use_next_block(c, body);
1912 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1913 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001914 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
1915 if (!compiler_try_except(c, s))
1916 return 0;
1917 }
1918 else {
1919 VISIT_SEQ(c, stmt, s->v.Try.body);
1920 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 ADDOP(c, POP_BLOCK);
1922 compiler_pop_fblock(c, FINALLY_TRY, body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1925 compiler_use_next_block(c, end);
1926 if (!compiler_push_fblock(c, FINALLY_END, end))
1927 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001928 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 ADDOP(c, END_FINALLY);
1930 compiler_pop_fblock(c, FINALLY_END, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001933}
1934
1935/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001936 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001937 (The contents of the value stack is shown in [], with the top
1938 at the right; 'tb' is trace-back info, 'val' the exception's
1939 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940
1941 Value stack Label Instruction Argument
1942 [] SETUP_EXCEPT L1
1943 [] <code for S>
1944 [] POP_BLOCK
1945 [] JUMP_FORWARD L0
1946
1947 [tb, val, exc] L1: DUP )
1948 [tb, val, exc, exc] <evaluate E1> )
1949 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1950 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
1951 [tb, val, exc] POP
1952 [tb, val] <assign to V1> (or POP if no V1)
1953 [tb] POP
1954 [] <code for S1>
1955 JUMP_FORWARD L0
1956
1957 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001958 .............................etc.......................
1959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
1961
1962 [] L0: <next statement>
1963
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001964 Of course, parts are not generated if Vi or Ei is not present.
1965*/
1966static int
1967compiler_try_except(struct compiler *c, stmt_ty s)
1968{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 basicblock *body, *orelse, *except, *end;
1970 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 body = compiler_new_block(c);
1973 except = compiler_new_block(c);
1974 orelse = compiler_new_block(c);
1975 end = compiler_new_block(c);
1976 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1977 return 0;
1978 ADDOP_JREL(c, SETUP_EXCEPT, except);
1979 compiler_use_next_block(c, body);
1980 if (!compiler_push_fblock(c, EXCEPT, body))
1981 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001982 VISIT_SEQ(c, stmt, s->v.Try.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 ADDOP(c, POP_BLOCK);
1984 compiler_pop_fblock(c, EXCEPT, body);
1985 ADDOP_JREL(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001986 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 compiler_use_next_block(c, except);
1988 for (i = 0; i < n; i++) {
1989 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001990 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001991 if (!handler->v.ExceptHandler.type && i < n-1)
1992 return compiler_error(c, "default 'except:' must be last");
1993 c->u->u_lineno_set = 0;
1994 c->u->u_lineno = handler->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001995 c->u->u_col_offset = handler->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 except = compiler_new_block(c);
1997 if (except == NULL)
1998 return 0;
1999 if (handler->v.ExceptHandler.type) {
2000 ADDOP(c, DUP_TOP);
2001 VISIT(c, expr, handler->v.ExceptHandler.type);
2002 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2003 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
2004 }
2005 ADDOP(c, POP_TOP);
2006 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002007 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00002008
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002009 cleanup_end = compiler_new_block(c);
2010 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05002011 if (!(cleanup_end || cleanup_body))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002012 return 0;
Guido van Rossumb940e112007-01-10 16:19:56 +00002013
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002014 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
2015 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002017 /*
2018 try:
2019 # body
2020 except type as name:
2021 try:
2022 # body
2023 finally:
2024 name = None
2025 del name
2026 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002028 /* second try: */
2029 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2030 compiler_use_next_block(c, cleanup_body);
2031 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2032 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002034 /* second # body */
2035 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2036 ADDOP(c, POP_BLOCK);
2037 ADDOP(c, POP_EXCEPT);
2038 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002039
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002040 /* finally: */
2041 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2042 compiler_use_next_block(c, cleanup_end);
2043 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
2044 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002046 /* name = None */
2047 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2048 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002049
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002050 /* del name */
2051 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002053 ADDOP(c, END_FINALLY);
2054 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 }
2056 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002057 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002059 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05002060 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002061 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062
Guido van Rossumb940e112007-01-10 16:19:56 +00002063 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002064 ADDOP(c, POP_TOP);
2065 compiler_use_next_block(c, cleanup_body);
2066 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2067 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002069 ADDOP(c, POP_EXCEPT);
2070 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 }
2072 ADDOP_JREL(c, JUMP_FORWARD, end);
2073 compiler_use_next_block(c, except);
2074 }
2075 ADDOP(c, END_FINALLY);
2076 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002077 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002078 compiler_use_next_block(c, end);
2079 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002080}
2081
2082static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002083compiler_try(struct compiler *c, stmt_ty s) {
2084 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
2085 return compiler_try_finally(c, s);
2086 else
2087 return compiler_try_except(c, s);
2088}
2089
2090
2091static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002092compiler_import_as(struct compiler *c, identifier name, identifier asname)
2093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 /* The IMPORT_NAME opcode was already generated. This function
2095 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 If there is a dot in name, we need to split it and emit a
2098 LOAD_ATTR for each name.
2099 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002100 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
2101 PyUnicode_GET_LENGTH(name), 1);
2102 if (dot == -2)
2103 return -1;
2104 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 /* Consume the base module name to get the first attribute */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002106 Py_ssize_t pos = dot + 1;
2107 while (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 PyObject *attr;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002109 dot = PyUnicode_FindChar(name, '.', pos,
2110 PyUnicode_GET_LENGTH(name), 1);
2111 if (dot == -2)
2112 return -1;
2113 attr = PyUnicode_Substring(name, pos,
2114 (dot != -1) ? dot :
2115 PyUnicode_GET_LENGTH(name));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116 if (!attr)
2117 return -1;
2118 ADDOP_O(c, LOAD_ATTR, attr, names);
2119 Py_DECREF(attr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002120 pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 }
2122 }
2123 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002124}
2125
2126static int
2127compiler_import(struct compiler *c, stmt_ty s)
2128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 /* The Import node stores a module name like a.b.c as a single
2130 string. This is convenient for all cases except
2131 import a.b.c as d
2132 where we need to parse that string to extract the individual
2133 module names.
2134 XXX Perhaps change the representation to make this case simpler?
2135 */
2136 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 for (i = 0; i < n; i++) {
2139 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
2140 int r;
2141 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143 level = PyLong_FromLong(0);
2144 if (level == NULL)
2145 return 0;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 ADDOP_O(c, LOAD_CONST, level, consts);
2148 Py_DECREF(level);
2149 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2150 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152 if (alias->asname) {
2153 r = compiler_import_as(c, alias->name, alias->asname);
2154 if (!r)
2155 return r;
2156 }
2157 else {
2158 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002159 Py_ssize_t dot = PyUnicode_FindChar(
2160 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
2161 if (dot != -1)
2162 tmp = PyUnicode_Substring(alias->name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002163 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002164 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 Py_DECREF(tmp);
2166 }
2167 if (!r)
2168 return r;
2169 }
2170 }
2171 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002172}
2173
2174static int
2175compiler_from_import(struct compiler *c, stmt_ty s)
2176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 PyObject *names = PyTuple_New(n);
2180 PyObject *level;
2181 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00002182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 if (!empty_string) {
2184 empty_string = PyUnicode_FromString("");
2185 if (!empty_string)
2186 return 0;
2187 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 if (!names)
2190 return 0;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 level = PyLong_FromLong(s->v.ImportFrom.level);
2193 if (!level) {
2194 Py_DECREF(names);
2195 return 0;
2196 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 /* build up the names */
2199 for (i = 0; i < n; i++) {
2200 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2201 Py_INCREF(alias->name);
2202 PyTuple_SET_ITEM(names, i, alias->name);
2203 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
2206 !PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module, "__future__")) {
2207 Py_DECREF(level);
2208 Py_DECREF(names);
2209 return compiler_error(c, "from __future__ imports must occur "
2210 "at the beginning of the file");
2211 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 ADDOP_O(c, LOAD_CONST, level, consts);
2214 Py_DECREF(level);
2215 ADDOP_O(c, LOAD_CONST, names, consts);
2216 Py_DECREF(names);
2217 if (s->v.ImportFrom.module) {
2218 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2219 }
2220 else {
2221 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
2222 }
2223 for (i = 0; i < n; i++) {
2224 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2225 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002226
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002227 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 assert(n == 1);
2229 ADDOP(c, IMPORT_STAR);
2230 return 1;
2231 }
2232
2233 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2234 store_name = alias->name;
2235 if (alias->asname)
2236 store_name = alias->asname;
2237
2238 if (!compiler_nameop(c, store_name, Store)) {
2239 Py_DECREF(names);
2240 return 0;
2241 }
2242 }
2243 /* remove imported module */
2244 ADDOP(c, POP_TOP);
2245 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002246}
2247
2248static int
2249compiler_assert(struct compiler *c, stmt_ty s)
2250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 static PyObject *assertion_error = NULL;
2252 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002253
Georg Brandl8334fd92010-12-04 10:26:46 +00002254 if (c->c_optimize)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 return 1;
2256 if (assertion_error == NULL) {
2257 assertion_error = PyUnicode_InternFromString("AssertionError");
2258 if (assertion_error == NULL)
2259 return 0;
2260 }
2261 if (s->v.Assert.test->kind == Tuple_kind &&
2262 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2263 const char* msg =
2264 "assertion is always true, perhaps remove parentheses?";
2265 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,
2266 c->u->u_lineno, NULL, NULL) == -1)
2267 return 0;
2268 }
2269 VISIT(c, expr, s->v.Assert.test);
2270 end = compiler_new_block(c);
2271 if (end == NULL)
2272 return 0;
2273 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
2274 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2275 if (s->v.Assert.msg) {
2276 VISIT(c, expr, s->v.Assert.msg);
2277 ADDOP_I(c, CALL_FUNCTION, 1);
2278 }
2279 ADDOP_I(c, RAISE_VARARGS, 1);
2280 compiler_use_next_block(c, end);
2281 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002282}
2283
2284static int
2285compiler_visit_stmt(struct compiler *c, stmt_ty s)
2286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 /* Always assign a lineno to the next instruction for a stmt. */
2290 c->u->u_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00002291 c->u->u_col_offset = s->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 switch (s->kind) {
2295 case FunctionDef_kind:
2296 return compiler_function(c, s);
2297 case ClassDef_kind:
2298 return compiler_class(c, s);
2299 case Return_kind:
2300 if (c->u->u_ste->ste_type != FunctionBlock)
2301 return compiler_error(c, "'return' outside function");
2302 if (s->v.Return.value) {
2303 VISIT(c, expr, s->v.Return.value);
2304 }
2305 else
2306 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2307 ADDOP(c, RETURN_VALUE);
2308 break;
2309 case Delete_kind:
2310 VISIT_SEQ(c, expr, s->v.Delete.targets)
2311 break;
2312 case Assign_kind:
2313 n = asdl_seq_LEN(s->v.Assign.targets);
2314 VISIT(c, expr, s->v.Assign.value);
2315 for (i = 0; i < n; i++) {
2316 if (i < n - 1)
2317 ADDOP(c, DUP_TOP);
2318 VISIT(c, expr,
2319 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2320 }
2321 break;
2322 case AugAssign_kind:
2323 return compiler_augassign(c, s);
2324 case For_kind:
2325 return compiler_for(c, s);
2326 case While_kind:
2327 return compiler_while(c, s);
2328 case If_kind:
2329 return compiler_if(c, s);
2330 case Raise_kind:
2331 n = 0;
2332 if (s->v.Raise.exc) {
2333 VISIT(c, expr, s->v.Raise.exc);
2334 n++;
2335 if (s->v.Raise.cause) {
2336 VISIT(c, expr, s->v.Raise.cause);
2337 n++;
2338 }
2339 }
2340 ADDOP_I(c, RAISE_VARARGS, n);
2341 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002342 case Try_kind:
2343 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 case Assert_kind:
2345 return compiler_assert(c, s);
2346 case Import_kind:
2347 return compiler_import(c, s);
2348 case ImportFrom_kind:
2349 return compiler_from_import(c, s);
2350 case Global_kind:
2351 case Nonlocal_kind:
2352 break;
2353 case Expr_kind:
2354 if (c->c_interactive && c->c_nestlevel <= 1) {
2355 VISIT(c, expr, s->v.Expr.value);
2356 ADDOP(c, PRINT_EXPR);
2357 }
2358 else if (s->v.Expr.value->kind != Str_kind &&
2359 s->v.Expr.value->kind != Num_kind) {
2360 VISIT(c, expr, s->v.Expr.value);
2361 ADDOP(c, POP_TOP);
2362 }
2363 break;
2364 case Pass_kind:
2365 break;
2366 case Break_kind:
2367 if (!compiler_in_loop(c))
2368 return compiler_error(c, "'break' outside loop");
2369 ADDOP(c, BREAK_LOOP);
2370 break;
2371 case Continue_kind:
2372 return compiler_continue(c);
2373 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002374 return compiler_with(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 }
2376 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002377}
2378
2379static int
2380unaryop(unaryop_ty op)
2381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 switch (op) {
2383 case Invert:
2384 return UNARY_INVERT;
2385 case Not:
2386 return UNARY_NOT;
2387 case UAdd:
2388 return UNARY_POSITIVE;
2389 case USub:
2390 return UNARY_NEGATIVE;
2391 default:
2392 PyErr_Format(PyExc_SystemError,
2393 "unary op %d should not be possible", op);
2394 return 0;
2395 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002396}
2397
2398static int
2399binop(struct compiler *c, operator_ty op)
2400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 switch (op) {
2402 case Add:
2403 return BINARY_ADD;
2404 case Sub:
2405 return BINARY_SUBTRACT;
2406 case Mult:
2407 return BINARY_MULTIPLY;
2408 case Div:
2409 return BINARY_TRUE_DIVIDE;
2410 case Mod:
2411 return BINARY_MODULO;
2412 case Pow:
2413 return BINARY_POWER;
2414 case LShift:
2415 return BINARY_LSHIFT;
2416 case RShift:
2417 return BINARY_RSHIFT;
2418 case BitOr:
2419 return BINARY_OR;
2420 case BitXor:
2421 return BINARY_XOR;
2422 case BitAnd:
2423 return BINARY_AND;
2424 case FloorDiv:
2425 return BINARY_FLOOR_DIVIDE;
2426 default:
2427 PyErr_Format(PyExc_SystemError,
2428 "binary op %d should not be possible", op);
2429 return 0;
2430 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002431}
2432
2433static int
2434cmpop(cmpop_ty op)
2435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 switch (op) {
2437 case Eq:
2438 return PyCmp_EQ;
2439 case NotEq:
2440 return PyCmp_NE;
2441 case Lt:
2442 return PyCmp_LT;
2443 case LtE:
2444 return PyCmp_LE;
2445 case Gt:
2446 return PyCmp_GT;
2447 case GtE:
2448 return PyCmp_GE;
2449 case Is:
2450 return PyCmp_IS;
2451 case IsNot:
2452 return PyCmp_IS_NOT;
2453 case In:
2454 return PyCmp_IN;
2455 case NotIn:
2456 return PyCmp_NOT_IN;
2457 default:
2458 return PyCmp_BAD;
2459 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002460}
2461
2462static int
2463inplace_binop(struct compiler *c, operator_ty op)
2464{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 switch (op) {
2466 case Add:
2467 return INPLACE_ADD;
2468 case Sub:
2469 return INPLACE_SUBTRACT;
2470 case Mult:
2471 return INPLACE_MULTIPLY;
2472 case Div:
2473 return INPLACE_TRUE_DIVIDE;
2474 case Mod:
2475 return INPLACE_MODULO;
2476 case Pow:
2477 return INPLACE_POWER;
2478 case LShift:
2479 return INPLACE_LSHIFT;
2480 case RShift:
2481 return INPLACE_RSHIFT;
2482 case BitOr:
2483 return INPLACE_OR;
2484 case BitXor:
2485 return INPLACE_XOR;
2486 case BitAnd:
2487 return INPLACE_AND;
2488 case FloorDiv:
2489 return INPLACE_FLOOR_DIVIDE;
2490 default:
2491 PyErr_Format(PyExc_SystemError,
2492 "inplace binary op %d should not be possible", op);
2493 return 0;
2494 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002495}
2496
2497static int
2498compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500 int op, scope, arg;
2501 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002503 PyObject *dict = c->u->u_names;
2504 PyObject *mangled;
2505 /* XXX AugStore isn't used anywhere! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 mangled = _Py_Mangle(c->u->u_private, name);
2508 if (!mangled)
2509 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002511 op = 0;
2512 optype = OP_NAME;
2513 scope = PyST_GetScope(c->u->u_ste, mangled);
2514 switch (scope) {
2515 case FREE:
2516 dict = c->u->u_freevars;
2517 optype = OP_DEREF;
2518 break;
2519 case CELL:
2520 dict = c->u->u_cellvars;
2521 optype = OP_DEREF;
2522 break;
2523 case LOCAL:
2524 if (c->u->u_ste->ste_type == FunctionBlock)
2525 optype = OP_FAST;
2526 break;
2527 case GLOBAL_IMPLICIT:
2528 if (c->u->u_ste->ste_type == FunctionBlock &&
2529 !c->u->u_ste->ste_unoptimized)
2530 optype = OP_GLOBAL;
2531 break;
2532 case GLOBAL_EXPLICIT:
2533 optype = OP_GLOBAL;
2534 break;
2535 default:
2536 /* scope can be 0 */
2537 break;
2538 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002541 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 switch (optype) {
2544 case OP_DEREF:
2545 switch (ctx) {
2546 case Load: op = LOAD_DEREF; break;
2547 case Store: op = STORE_DEREF; break;
2548 case AugLoad:
2549 case AugStore:
2550 break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002551 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 case Param:
2553 default:
2554 PyErr_SetString(PyExc_SystemError,
2555 "param invalid for deref variable");
2556 return 0;
2557 }
2558 break;
2559 case OP_FAST:
2560 switch (ctx) {
2561 case Load: op = LOAD_FAST; break;
2562 case Store: op = STORE_FAST; break;
2563 case Del: op = DELETE_FAST; break;
2564 case AugLoad:
2565 case AugStore:
2566 break;
2567 case Param:
2568 default:
2569 PyErr_SetString(PyExc_SystemError,
2570 "param invalid for local variable");
2571 return 0;
2572 }
2573 ADDOP_O(c, op, mangled, varnames);
2574 Py_DECREF(mangled);
2575 return 1;
2576 case OP_GLOBAL:
2577 switch (ctx) {
2578 case Load: op = LOAD_GLOBAL; break;
2579 case Store: op = STORE_GLOBAL; break;
2580 case Del: op = DELETE_GLOBAL; break;
2581 case AugLoad:
2582 case AugStore:
2583 break;
2584 case Param:
2585 default:
2586 PyErr_SetString(PyExc_SystemError,
2587 "param invalid for global variable");
2588 return 0;
2589 }
2590 break;
2591 case OP_NAME:
2592 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002593 case Load: op = LOAD_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002594 case Store: op = STORE_NAME; break;
2595 case Del: op = DELETE_NAME; break;
2596 case AugLoad:
2597 case AugStore:
2598 break;
2599 case Param:
2600 default:
2601 PyErr_SetString(PyExc_SystemError,
2602 "param invalid for name variable");
2603 return 0;
2604 }
2605 break;
2606 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002608 assert(op);
2609 arg = compiler_add_o(c, dict, mangled);
2610 Py_DECREF(mangled);
2611 if (arg < 0)
2612 return 0;
2613 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002614}
2615
2616static int
2617compiler_boolop(struct compiler *c, expr_ty e)
2618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002619 basicblock *end;
2620 int jumpi, i, n;
2621 asdl_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002623 assert(e->kind == BoolOp_kind);
2624 if (e->v.BoolOp.op == And)
2625 jumpi = JUMP_IF_FALSE_OR_POP;
2626 else
2627 jumpi = JUMP_IF_TRUE_OR_POP;
2628 end = compiler_new_block(c);
2629 if (end == NULL)
2630 return 0;
2631 s = e->v.BoolOp.values;
2632 n = asdl_seq_LEN(s) - 1;
2633 assert(n >= 0);
2634 for (i = 0; i < n; ++i) {
2635 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
2636 ADDOP_JABS(c, jumpi, end);
2637 }
2638 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
2639 compiler_use_next_block(c, end);
2640 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641}
2642
2643static int
2644compiler_list(struct compiler *c, expr_ty e)
2645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646 int n = asdl_seq_LEN(e->v.List.elts);
2647 if (e->v.List.ctx == Store) {
2648 int i, seen_star = 0;
2649 for (i = 0; i < n; i++) {
2650 expr_ty elt = asdl_seq_GET(e->v.List.elts, i);
2651 if (elt->kind == Starred_kind && !seen_star) {
2652 if ((i >= (1 << 8)) ||
2653 (n-i-1 >= (INT_MAX >> 8)))
2654 return compiler_error(c,
2655 "too many expressions in "
2656 "star-unpacking assignment");
2657 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2658 seen_star = 1;
2659 asdl_seq_SET(e->v.List.elts, i, elt->v.Starred.value);
2660 } else if (elt->kind == Starred_kind) {
2661 return compiler_error(c,
2662 "two starred expressions in assignment");
2663 }
2664 }
2665 if (!seen_star) {
2666 ADDOP_I(c, UNPACK_SEQUENCE, n);
2667 }
2668 }
2669 VISIT_SEQ(c, expr, e->v.List.elts);
2670 if (e->v.List.ctx == Load) {
2671 ADDOP_I(c, BUILD_LIST, n);
2672 }
2673 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002674}
2675
2676static int
2677compiler_tuple(struct compiler *c, expr_ty e)
2678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 int n = asdl_seq_LEN(e->v.Tuple.elts);
2680 if (e->v.Tuple.ctx == Store) {
2681 int i, seen_star = 0;
2682 for (i = 0; i < n; i++) {
2683 expr_ty elt = asdl_seq_GET(e->v.Tuple.elts, i);
2684 if (elt->kind == Starred_kind && !seen_star) {
2685 if ((i >= (1 << 8)) ||
2686 (n-i-1 >= (INT_MAX >> 8)))
2687 return compiler_error(c,
2688 "too many expressions in "
2689 "star-unpacking assignment");
2690 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2691 seen_star = 1;
2692 asdl_seq_SET(e->v.Tuple.elts, i, elt->v.Starred.value);
2693 } else if (elt->kind == Starred_kind) {
2694 return compiler_error(c,
2695 "two starred expressions in assignment");
2696 }
2697 }
2698 if (!seen_star) {
2699 ADDOP_I(c, UNPACK_SEQUENCE, n);
2700 }
2701 }
2702 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2703 if (e->v.Tuple.ctx == Load) {
2704 ADDOP_I(c, BUILD_TUPLE, n);
2705 }
2706 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002707}
2708
2709static int
2710compiler_compare(struct compiler *c, expr_ty e)
2711{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002712 int i, n;
2713 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002715 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2716 VISIT(c, expr, e->v.Compare.left);
2717 n = asdl_seq_LEN(e->v.Compare.ops);
2718 assert(n > 0);
2719 if (n > 1) {
2720 cleanup = compiler_new_block(c);
2721 if (cleanup == NULL)
2722 return 0;
2723 VISIT(c, expr,
2724 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
2725 }
2726 for (i = 1; i < n; i++) {
2727 ADDOP(c, DUP_TOP);
2728 ADDOP(c, ROT_THREE);
2729 ADDOP_I(c, COMPARE_OP,
2730 cmpop((cmpop_ty)(asdl_seq_GET(
2731 e->v.Compare.ops, i - 1))));
2732 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
2733 NEXT_BLOCK(c);
2734 if (i < (n - 1))
2735 VISIT(c, expr,
2736 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2737 }
2738 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
2739 ADDOP_I(c, COMPARE_OP,
2740 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
2741 if (n > 1) {
2742 basicblock *end = compiler_new_block(c);
2743 if (end == NULL)
2744 return 0;
2745 ADDOP_JREL(c, JUMP_FORWARD, end);
2746 compiler_use_next_block(c, cleanup);
2747 ADDOP(c, ROT_TWO);
2748 ADDOP(c, POP_TOP);
2749 compiler_use_next_block(c, end);
2750 }
2751 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002752}
2753
2754static int
2755compiler_call(struct compiler *c, expr_ty e)
2756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 VISIT(c, expr, e->v.Call.func);
2758 return compiler_call_helper(c, 0,
2759 e->v.Call.args,
2760 e->v.Call.keywords,
2761 e->v.Call.starargs,
2762 e->v.Call.kwargs);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002763}
2764
2765/* shared code between compiler_call and compiler_class */
2766static int
2767compiler_call_helper(struct compiler *c,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002768 int n, /* Args already pushed */
2769 asdl_seq *args,
2770 asdl_seq *keywords,
2771 expr_ty starargs,
2772 expr_ty kwargs)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002773{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 int code = 0;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002776 n += asdl_seq_LEN(args);
2777 VISIT_SEQ(c, expr, args);
2778 if (keywords) {
2779 VISIT_SEQ(c, keyword, keywords);
2780 n |= asdl_seq_LEN(keywords) << 8;
2781 }
2782 if (starargs) {
2783 VISIT(c, expr, starargs);
2784 code |= 1;
2785 }
2786 if (kwargs) {
2787 VISIT(c, expr, kwargs);
2788 code |= 2;
2789 }
2790 switch (code) {
2791 case 0:
2792 ADDOP_I(c, CALL_FUNCTION, n);
2793 break;
2794 case 1:
2795 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2796 break;
2797 case 2:
2798 ADDOP_I(c, CALL_FUNCTION_KW, n);
2799 break;
2800 case 3:
2801 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2802 break;
2803 }
2804 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002805}
2806
Nick Coghlan650f0d02007-04-15 12:05:43 +00002807
2808/* List and set comprehensions and generator expressions work by creating a
2809 nested function to perform the actual iteration. This means that the
2810 iteration variables don't leak into the current scope.
2811 The defined function is called immediately following its definition, with the
2812 result of that call being the result of the expression.
2813 The LC/SC version returns the populated container, while the GE version is
2814 flagged in symtable.c as a generator, so it returns the generator object
2815 when the function is called.
2816 This code *knows* that the loop cannot contain break, continue, or return,
2817 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2818
2819 Possible cleanups:
2820 - iterate over the generator sequence instead of using recursion
2821*/
2822
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824compiler_comprehension_generator(struct compiler *c,
2825 asdl_seq *generators, int gen_index,
2826 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 /* generate code for the iterator, then each of the ifs,
2829 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 comprehension_ty gen;
2832 basicblock *start, *anchor, *skip, *if_cleanup;
2833 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002835 start = compiler_new_block(c);
2836 skip = compiler_new_block(c);
2837 if_cleanup = compiler_new_block(c);
2838 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2841 anchor == NULL)
2842 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 if (gen_index == 0) {
2847 /* Receive outermost iter as an implicit argument */
2848 c->u->u_argcount = 1;
2849 ADDOP_I(c, LOAD_FAST, 0);
2850 }
2851 else {
2852 /* Sub-iter - calculate on the fly */
2853 VISIT(c, expr, gen->iter);
2854 ADDOP(c, GET_ITER);
2855 }
2856 compiler_use_next_block(c, start);
2857 ADDOP_JREL(c, FOR_ITER, anchor);
2858 NEXT_BLOCK(c);
2859 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 /* XXX this needs to be cleaned up...a lot! */
2862 n = asdl_seq_LEN(gen->ifs);
2863 for (i = 0; i < n; i++) {
2864 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
2865 VISIT(c, expr, e);
2866 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
2867 NEXT_BLOCK(c);
2868 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 if (++gen_index < asdl_seq_LEN(generators))
2871 if (!compiler_comprehension_generator(c,
2872 generators, gen_index,
2873 elt, val, type))
2874 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 /* only append after the last for generator */
2877 if (gen_index >= asdl_seq_LEN(generators)) {
2878 /* comprehension specific code */
2879 switch (type) {
2880 case COMP_GENEXP:
2881 VISIT(c, expr, elt);
2882 ADDOP(c, YIELD_VALUE);
2883 ADDOP(c, POP_TOP);
2884 break;
2885 case COMP_LISTCOMP:
2886 VISIT(c, expr, elt);
2887 ADDOP_I(c, LIST_APPEND, gen_index + 1);
2888 break;
2889 case COMP_SETCOMP:
2890 VISIT(c, expr, elt);
2891 ADDOP_I(c, SET_ADD, gen_index + 1);
2892 break;
2893 case COMP_DICTCOMP:
2894 /* With 'd[k] = v', v is evaluated before k, so we do
2895 the same. */
2896 VISIT(c, expr, val);
2897 VISIT(c, expr, elt);
2898 ADDOP_I(c, MAP_ADD, gen_index + 1);
2899 break;
2900 default:
2901 return 0;
2902 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 compiler_use_next_block(c, skip);
2905 }
2906 compiler_use_next_block(c, if_cleanup);
2907 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2908 compiler_use_next_block(c, anchor);
2909
2910 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002911}
2912
2913static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002914compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 asdl_seq *generators, expr_ty elt, expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 PyCodeObject *co = NULL;
2918 expr_ty outermost_iter;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 outermost_iter = ((comprehension_ty)
2921 asdl_seq_GET(generators, 0))->iter;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2924 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002926 if (type != COMP_GENEXP) {
2927 int op;
2928 switch (type) {
2929 case COMP_LISTCOMP:
2930 op = BUILD_LIST;
2931 break;
2932 case COMP_SETCOMP:
2933 op = BUILD_SET;
2934 break;
2935 case COMP_DICTCOMP:
2936 op = BUILD_MAP;
2937 break;
2938 default:
2939 PyErr_Format(PyExc_SystemError,
2940 "unknown comprehension type %d", type);
2941 goto error_in_scope;
2942 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002944 ADDOP_I(c, op, 0);
2945 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002947 if (!compiler_comprehension_generator(c, generators, 0, elt,
2948 val, type))
2949 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002951 if (type != COMP_GENEXP) {
2952 ADDOP(c, RETURN_VALUE);
2953 }
2954
2955 co = assemble(c, 1);
2956 compiler_exit_scope(c);
2957 if (co == NULL)
2958 goto error;
2959
2960 if (!compiler_make_closure(c, co, 0))
2961 goto error;
2962 Py_DECREF(co);
2963
2964 VISIT(c, expr, outermost_iter);
2965 ADDOP(c, GET_ITER);
2966 ADDOP_I(c, CALL_FUNCTION, 1);
2967 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002968error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002969 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002970error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002971 Py_XDECREF(co);
2972 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002973}
2974
2975static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002976compiler_genexp(struct compiler *c, expr_ty e)
2977{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 static identifier name;
2979 if (!name) {
2980 name = PyUnicode_FromString("<genexpr>");
2981 if (!name)
2982 return 0;
2983 }
2984 assert(e->kind == GeneratorExp_kind);
2985 return compiler_comprehension(c, e, COMP_GENEXP, name,
2986 e->v.GeneratorExp.generators,
2987 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002988}
2989
2990static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002991compiler_listcomp(struct compiler *c, expr_ty e)
2992{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002993 static identifier name;
2994 if (!name) {
2995 name = PyUnicode_FromString("<listcomp>");
2996 if (!name)
2997 return 0;
2998 }
2999 assert(e->kind == ListComp_kind);
3000 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
3001 e->v.ListComp.generators,
3002 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003003}
3004
3005static int
3006compiler_setcomp(struct compiler *c, expr_ty e)
3007{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003008 static identifier name;
3009 if (!name) {
3010 name = PyUnicode_FromString("<setcomp>");
3011 if (!name)
3012 return 0;
3013 }
3014 assert(e->kind == SetComp_kind);
3015 return compiler_comprehension(c, e, COMP_SETCOMP, name,
3016 e->v.SetComp.generators,
3017 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00003018}
3019
3020
3021static int
3022compiler_dictcomp(struct compiler *c, expr_ty e)
3023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 static identifier name;
3025 if (!name) {
3026 name = PyUnicode_FromString("<dictcomp>");
3027 if (!name)
3028 return 0;
3029 }
3030 assert(e->kind == DictComp_kind);
3031 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
3032 e->v.DictComp.generators,
3033 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003034}
3035
3036
3037static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003038compiler_visit_keyword(struct compiler *c, keyword_ty k)
3039{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003040 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3041 VISIT(c, expr, k->value);
3042 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003043}
3044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003045/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003046 whether they are true or false.
3047
3048 Return values: 1 for true, 0 for false, -1 for non-constant.
3049 */
3050
3051static int
Georg Brandl8334fd92010-12-04 10:26:46 +00003052expr_constant(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054 char *id;
3055 switch (e->kind) {
3056 case Ellipsis_kind:
3057 return 1;
3058 case Num_kind:
3059 return PyObject_IsTrue(e->v.Num.n);
3060 case Str_kind:
3061 return PyObject_IsTrue(e->v.Str.s);
3062 case Name_kind:
3063 /* optimize away names that can't be reassigned */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003064 id = PyUnicode_AsUTF8(e->v.Name.id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 if (strcmp(id, "True") == 0) return 1;
3066 if (strcmp(id, "False") == 0) return 0;
3067 if (strcmp(id, "None") == 0) return 0;
3068 if (strcmp(id, "__debug__") == 0)
Georg Brandl8334fd92010-12-04 10:26:46 +00003069 return ! c->c_optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070 /* fall through */
3071 default:
3072 return -1;
3073 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003074}
3075
Guido van Rossumc2e20742006-02-27 22:32:47 +00003076/*
3077 Implements the with statement from PEP 343.
3078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079 The semantics outlined in that PEP are as follows:
Guido van Rossumc2e20742006-02-27 22:32:47 +00003080
3081 with EXPR as VAR:
3082 BLOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083
Guido van Rossumc2e20742006-02-27 22:32:47 +00003084 It is implemented roughly as:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003085
Thomas Wouters477c8d52006-05-27 19:21:47 +00003086 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00003087 exit = context.__exit__ # not calling it
3088 value = context.__enter__()
3089 try:
3090 VAR = value # if VAR present in the syntax
3091 BLOCK
3092 finally:
3093 if an exception was raised:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003095 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003096 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003097 exit(*exc)
3098 */
3099static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003100compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003101{
Guido van Rossumc2e20742006-02-27 22:32:47 +00003102 basicblock *block, *finally;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003103 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003104
3105 assert(s->kind == With_kind);
3106
Guido van Rossumc2e20742006-02-27 22:32:47 +00003107 block = compiler_new_block(c);
3108 finally = compiler_new_block(c);
3109 if (!block || !finally)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003110 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003111
Thomas Wouters477c8d52006-05-27 19:21:47 +00003112 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003113 VISIT(c, expr, item->context_expr);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003114 ADDOP_JREL(c, SETUP_WITH, finally);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003115
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003116 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003117 compiler_use_next_block(c, block);
3118 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003119 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003120 }
3121
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003122 if (item->optional_vars) {
3123 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003124 }
3125 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003126 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003127 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003128 }
3129
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003130 pos++;
3131 if (pos == asdl_seq_LEN(s->v.With.items))
3132 /* BLOCK code */
3133 VISIT_SEQ(c, stmt, s->v.With.body)
3134 else if (!compiler_with(c, s, pos))
3135 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003136
3137 /* End of try block; start the finally block */
3138 ADDOP(c, POP_BLOCK);
3139 compiler_pop_fblock(c, FINALLY_TRY, block);
3140
3141 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3142 compiler_use_next_block(c, finally);
3143 if (!compiler_push_fblock(c, FINALLY_END, finally))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003144 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003145
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003146 /* Finally block starts; context.__exit__ is on the stack under
3147 the exception or return information. Just issue our magic
3148 opcode. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003149 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003150
3151 /* Finally block ends. */
3152 ADDOP(c, END_FINALLY);
3153 compiler_pop_fblock(c, FINALLY_END, finally);
3154 return 1;
3155}
3156
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003157static int
3158compiler_visit_expr(struct compiler *c, expr_ty e)
3159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003160 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003162 /* If expr e has a different line number than the last expr/stmt,
3163 set a new line number for the next instruction.
3164 */
3165 if (e->lineno > c->u->u_lineno) {
3166 c->u->u_lineno = e->lineno;
3167 c->u->u_lineno_set = 0;
3168 }
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00003169 /* Updating the column offset is always harmless. */
3170 c->u->u_col_offset = e->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003171 switch (e->kind) {
3172 case BoolOp_kind:
3173 return compiler_boolop(c, e);
3174 case BinOp_kind:
3175 VISIT(c, expr, e->v.BinOp.left);
3176 VISIT(c, expr, e->v.BinOp.right);
3177 ADDOP(c, binop(c, e->v.BinOp.op));
3178 break;
3179 case UnaryOp_kind:
3180 VISIT(c, expr, e->v.UnaryOp.operand);
3181 ADDOP(c, unaryop(e->v.UnaryOp.op));
3182 break;
3183 case Lambda_kind:
3184 return compiler_lambda(c, e);
3185 case IfExp_kind:
3186 return compiler_ifexp(c, e);
3187 case Dict_kind:
3188 n = asdl_seq_LEN(e->v.Dict.values);
3189 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
3190 for (i = 0; i < n; i++) {
3191 VISIT(c, expr,
3192 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3193 VISIT(c, expr,
3194 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3195 ADDOP(c, STORE_MAP);
3196 }
3197 break;
3198 case Set_kind:
3199 n = asdl_seq_LEN(e->v.Set.elts);
3200 VISIT_SEQ(c, expr, e->v.Set.elts);
3201 ADDOP_I(c, BUILD_SET, n);
3202 break;
3203 case GeneratorExp_kind:
3204 return compiler_genexp(c, e);
3205 case ListComp_kind:
3206 return compiler_listcomp(c, e);
3207 case SetComp_kind:
3208 return compiler_setcomp(c, e);
3209 case DictComp_kind:
3210 return compiler_dictcomp(c, e);
3211 case Yield_kind:
3212 if (c->u->u_ste->ste_type != FunctionBlock)
3213 return compiler_error(c, "'yield' outside function");
3214 if (e->v.Yield.value) {
3215 VISIT(c, expr, e->v.Yield.value);
3216 }
3217 else {
3218 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3219 }
3220 ADDOP(c, YIELD_VALUE);
3221 break;
3222 case Compare_kind:
3223 return compiler_compare(c, e);
3224 case Call_kind:
3225 return compiler_call(c, e);
3226 case Num_kind:
3227 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3228 break;
3229 case Str_kind:
3230 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3231 break;
3232 case Bytes_kind:
3233 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
3234 break;
3235 case Ellipsis_kind:
3236 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3237 break;
3238 /* The following exprs can be assignment targets. */
3239 case Attribute_kind:
3240 if (e->v.Attribute.ctx != AugStore)
3241 VISIT(c, expr, e->v.Attribute.value);
3242 switch (e->v.Attribute.ctx) {
3243 case AugLoad:
3244 ADDOP(c, DUP_TOP);
3245 /* Fall through to load */
3246 case Load:
3247 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3248 break;
3249 case AugStore:
3250 ADDOP(c, ROT_TWO);
3251 /* Fall through to save */
3252 case Store:
3253 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3254 break;
3255 case Del:
3256 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3257 break;
3258 case Param:
3259 default:
3260 PyErr_SetString(PyExc_SystemError,
3261 "param invalid in attribute expression");
3262 return 0;
3263 }
3264 break;
3265 case Subscript_kind:
3266 switch (e->v.Subscript.ctx) {
3267 case AugLoad:
3268 VISIT(c, expr, e->v.Subscript.value);
3269 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3270 break;
3271 case Load:
3272 VISIT(c, expr, e->v.Subscript.value);
3273 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3274 break;
3275 case AugStore:
3276 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3277 break;
3278 case Store:
3279 VISIT(c, expr, e->v.Subscript.value);
3280 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3281 break;
3282 case Del:
3283 VISIT(c, expr, e->v.Subscript.value);
3284 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3285 break;
3286 case Param:
3287 default:
3288 PyErr_SetString(PyExc_SystemError,
3289 "param invalid in subscript expression");
3290 return 0;
3291 }
3292 break;
3293 case Starred_kind:
3294 switch (e->v.Starred.ctx) {
3295 case Store:
3296 /* In all legitimate cases, the Starred node was already replaced
3297 * by compiler_list/compiler_tuple. XXX: is that okay? */
3298 return compiler_error(c,
3299 "starred assignment target must be in a list or tuple");
3300 default:
3301 return compiler_error(c,
3302 "can use starred expression only as assignment target");
3303 }
3304 break;
3305 case Name_kind:
3306 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3307 /* child nodes of List and Tuple will have expr_context set */
3308 case List_kind:
3309 return compiler_list(c, e);
3310 case Tuple_kind:
3311 return compiler_tuple(c, e);
3312 }
3313 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003314}
3315
3316static int
3317compiler_augassign(struct compiler *c, stmt_ty s)
3318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003319 expr_ty e = s->v.AugAssign.target;
3320 expr_ty auge;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003322 assert(s->kind == AugAssign_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 switch (e->kind) {
3325 case Attribute_kind:
3326 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
3327 AugLoad, e->lineno, e->col_offset, c->c_arena);
3328 if (auge == NULL)
3329 return 0;
3330 VISIT(c, expr, auge);
3331 VISIT(c, expr, s->v.AugAssign.value);
3332 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3333 auge->v.Attribute.ctx = AugStore;
3334 VISIT(c, expr, auge);
3335 break;
3336 case Subscript_kind:
3337 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
3338 AugLoad, e->lineno, e->col_offset, c->c_arena);
3339 if (auge == NULL)
3340 return 0;
3341 VISIT(c, expr, auge);
3342 VISIT(c, expr, s->v.AugAssign.value);
3343 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3344 auge->v.Subscript.ctx = AugStore;
3345 VISIT(c, expr, auge);
3346 break;
3347 case Name_kind:
3348 if (!compiler_nameop(c, e->v.Name.id, Load))
3349 return 0;
3350 VISIT(c, expr, s->v.AugAssign.value);
3351 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3352 return compiler_nameop(c, e->v.Name.id, Store);
3353 default:
3354 PyErr_Format(PyExc_SystemError,
3355 "invalid node type (%d) for augmented assignment",
3356 e->kind);
3357 return 0;
3358 }
3359 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003360}
3361
3362static int
3363compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003365 struct fblockinfo *f;
3366 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3367 PyErr_SetString(PyExc_SystemError,
3368 "too many statically nested blocks");
3369 return 0;
3370 }
3371 f = &c->u->u_fblock[c->u->u_nfblocks++];
3372 f->fb_type = t;
3373 f->fb_block = b;
3374 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003375}
3376
3377static void
3378compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003380 struct compiler_unit *u = c->u;
3381 assert(u->u_nfblocks > 0);
3382 u->u_nfblocks--;
3383 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3384 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003385}
3386
Thomas Wouters89f507f2006-12-13 04:49:30 +00003387static int
3388compiler_in_loop(struct compiler *c) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003389 int i;
3390 struct compiler_unit *u = c->u;
3391 for (i = 0; i < u->u_nfblocks; ++i) {
3392 if (u->u_fblock[i].fb_type == LOOP)
3393 return 1;
3394 }
3395 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003396}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003397/* Raises a SyntaxError and returns 0.
3398 If something goes wrong, a different exception may be raised.
3399*/
3400
3401static int
3402compiler_error(struct compiler *c, const char *errstr)
3403{
Benjamin Peterson43b06862011-05-27 09:08:01 -05003404 PyObject *loc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003405 PyObject *u = NULL, *v = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003407 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3408 if (!loc) {
3409 Py_INCREF(Py_None);
3410 loc = Py_None;
3411 }
Benjamin Peterson43b06862011-05-27 09:08:01 -05003412 u = Py_BuildValue("(OiiO)", c->c_filename_obj, c->u->u_lineno,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00003413 c->u->u_col_offset, loc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003414 if (!u)
3415 goto exit;
3416 v = Py_BuildValue("(zO)", errstr, u);
3417 if (!v)
3418 goto exit;
3419 PyErr_SetObject(PyExc_SyntaxError, v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003420 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003421 Py_DECREF(loc);
3422 Py_XDECREF(u);
3423 Py_XDECREF(v);
3424 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003425}
3426
3427static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428compiler_handle_subscr(struct compiler *c, const char *kind,
3429 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 /* XXX this code is duplicated */
3434 switch (ctx) {
3435 case AugLoad: /* fall through to Load */
3436 case Load: op = BINARY_SUBSCR; break;
3437 case AugStore:/* fall through to Store */
3438 case Store: op = STORE_SUBSCR; break;
3439 case Del: op = DELETE_SUBSCR; break;
3440 case Param:
3441 PyErr_Format(PyExc_SystemError,
3442 "invalid %s kind %d in subscript\n",
3443 kind, ctx);
3444 return 0;
3445 }
3446 if (ctx == AugLoad) {
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00003447 ADDOP(c, DUP_TOP_TWO);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003448 }
3449 else if (ctx == AugStore) {
3450 ADDOP(c, ROT_THREE);
3451 }
3452 ADDOP(c, op);
3453 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003454}
3455
3456static int
3457compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003459 int n = 2;
3460 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003462 /* only handles the cases where BUILD_SLICE is emitted */
3463 if (s->v.Slice.lower) {
3464 VISIT(c, expr, s->v.Slice.lower);
3465 }
3466 else {
3467 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3468 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003470 if (s->v.Slice.upper) {
3471 VISIT(c, expr, s->v.Slice.upper);
3472 }
3473 else {
3474 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3475 }
3476
3477 if (s->v.Slice.step) {
3478 n++;
3479 VISIT(c, expr, s->v.Slice.step);
3480 }
3481 ADDOP_I(c, BUILD_SLICE, n);
3482 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003483}
3484
3485static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003486compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3487 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 switch (s->kind) {
3490 case Slice_kind:
3491 return compiler_slice(c, s, ctx);
3492 case Index_kind:
3493 VISIT(c, expr, s->v.Index.value);
3494 break;
3495 case ExtSlice_kind:
3496 default:
3497 PyErr_SetString(PyExc_SystemError,
3498 "extended slice invalid in nested slice");
3499 return 0;
3500 }
3501 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003502}
3503
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003504static int
3505compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003507 char * kindname = NULL;
3508 switch (s->kind) {
3509 case Index_kind:
3510 kindname = "index";
3511 if (ctx != AugStore) {
3512 VISIT(c, expr, s->v.Index.value);
3513 }
3514 break;
3515 case Slice_kind:
3516 kindname = "slice";
3517 if (ctx != AugStore) {
3518 if (!compiler_slice(c, s, ctx))
3519 return 0;
3520 }
3521 break;
3522 case ExtSlice_kind:
3523 kindname = "extended slice";
3524 if (ctx != AugStore) {
3525 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3526 for (i = 0; i < n; i++) {
3527 slice_ty sub = (slice_ty)asdl_seq_GET(
3528 s->v.ExtSlice.dims, i);
3529 if (!compiler_visit_nested_slice(c, sub, ctx))
3530 return 0;
3531 }
3532 ADDOP_I(c, BUILD_TUPLE, n);
3533 }
3534 break;
3535 default:
3536 PyErr_Format(PyExc_SystemError,
3537 "invalid subscript kind %d", s->kind);
3538 return 0;
3539 }
3540 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003541}
3542
Thomas Wouters89f507f2006-12-13 04:49:30 +00003543/* End of the compiler section, beginning of the assembler section */
3544
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003545/* do depth-first search of basic block graph, starting with block.
3546 post records the block indices in post-order.
3547
3548 XXX must handle implicit jumps from one block to next
3549*/
3550
Thomas Wouters89f507f2006-12-13 04:49:30 +00003551struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003552 PyObject *a_bytecode; /* string containing bytecode */
3553 int a_offset; /* offset into bytecode */
3554 int a_nblocks; /* number of reachable blocks */
3555 basicblock **a_postorder; /* list of blocks in dfs postorder */
3556 PyObject *a_lnotab; /* string containing lnotab */
3557 int a_lnotab_off; /* offset into lnotab */
3558 int a_lineno; /* last lineno of emitted instruction */
3559 int a_lineno_off; /* bytecode offset of last lineno */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003560};
3561
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003562static void
3563dfs(struct compiler *c, basicblock *b, struct assembler *a)
3564{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003565 int i;
3566 struct instr *instr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003568 if (b->b_seen)
3569 return;
3570 b->b_seen = 1;
3571 if (b->b_next != NULL)
3572 dfs(c, b->b_next, a);
3573 for (i = 0; i < b->b_iused; i++) {
3574 instr = &b->b_instr[i];
3575 if (instr->i_jrel || instr->i_jabs)
3576 dfs(c, instr->i_target, a);
3577 }
3578 a->a_postorder[a->a_nblocks++] = b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003579}
3580
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003581static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003582stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3583{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 int i, target_depth;
3585 struct instr *instr;
3586 if (b->b_seen || b->b_startdepth >= depth)
3587 return maxdepth;
3588 b->b_seen = 1;
3589 b->b_startdepth = depth;
3590 for (i = 0; i < b->b_iused; i++) {
3591 instr = &b->b_instr[i];
3592 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3593 if (depth > maxdepth)
3594 maxdepth = depth;
3595 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3596 if (instr->i_jrel || instr->i_jabs) {
3597 target_depth = depth;
3598 if (instr->i_opcode == FOR_ITER) {
3599 target_depth = depth-2;
3600 } else if (instr->i_opcode == SETUP_FINALLY ||
3601 instr->i_opcode == SETUP_EXCEPT) {
3602 target_depth = depth+3;
3603 if (target_depth > maxdepth)
3604 maxdepth = target_depth;
3605 }
3606 maxdepth = stackdepth_walk(c, instr->i_target,
3607 target_depth, maxdepth);
3608 if (instr->i_opcode == JUMP_ABSOLUTE ||
3609 instr->i_opcode == JUMP_FORWARD) {
3610 goto out; /* remaining code is dead */
3611 }
3612 }
3613 }
3614 if (b->b_next)
3615 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003616out:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003617 b->b_seen = 0;
3618 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003619}
3620
3621/* Find the flow path that needs the largest stack. We assume that
3622 * cycles in the flow graph have no net effect on the stack depth.
3623 */
3624static int
3625stackdepth(struct compiler *c)
3626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 basicblock *b, *entryblock;
3628 entryblock = NULL;
3629 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3630 b->b_seen = 0;
3631 b->b_startdepth = INT_MIN;
3632 entryblock = b;
3633 }
3634 if (!entryblock)
3635 return 0;
3636 return stackdepth_walk(c, entryblock, 0, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003637}
3638
3639static int
3640assemble_init(struct assembler *a, int nblocks, int firstlineno)
3641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003642 memset(a, 0, sizeof(struct assembler));
3643 a->a_lineno = firstlineno;
3644 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3645 if (!a->a_bytecode)
3646 return 0;
3647 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3648 if (!a->a_lnotab)
3649 return 0;
3650 if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
3651 PyErr_NoMemory();
3652 return 0;
3653 }
3654 a->a_postorder = (basicblock **)PyObject_Malloc(
3655 sizeof(basicblock *) * nblocks);
3656 if (!a->a_postorder) {
3657 PyErr_NoMemory();
3658 return 0;
3659 }
3660 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003661}
3662
3663static void
3664assemble_free(struct assembler *a)
3665{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003666 Py_XDECREF(a->a_bytecode);
3667 Py_XDECREF(a->a_lnotab);
3668 if (a->a_postorder)
3669 PyObject_Free(a->a_postorder);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003670}
3671
3672/* Return the size of a basic block in bytes. */
3673
3674static int
3675instrsize(struct instr *instr)
3676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003677 if (!instr->i_hasarg)
3678 return 1; /* 1 byte for the opcode*/
3679 if (instr->i_oparg > 0xffff)
3680 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3681 return 3; /* 1 (opcode) + 2 (oparg) */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003682}
3683
3684static int
3685blocksize(basicblock *b)
3686{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003687 int i;
3688 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003690 for (i = 0; i < b->b_iused; i++)
3691 size += instrsize(&b->b_instr[i]);
3692 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003693}
3694
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003695/* Appends a pair to the end of the line number table, a_lnotab, representing
3696 the instruction's bytecode offset and line number. See
3697 Objects/lnotab_notes.txt for the description of the line number table. */
Tim Peters2a7f3842001-06-09 09:26:21 +00003698
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003699static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003700assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003701{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003702 int d_bytecode, d_lineno;
3703 int len;
3704 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003706 d_bytecode = a->a_offset - a->a_lineno_off;
3707 d_lineno = i->i_lineno - a->a_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003709 assert(d_bytecode >= 0);
3710 assert(d_lineno >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003712 if(d_bytecode == 0 && d_lineno == 0)
3713 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003715 if (d_bytecode > 255) {
3716 int j, nbytes, ncodes = d_bytecode / 255;
3717 nbytes = a->a_lnotab_off + 2 * ncodes;
3718 len = PyBytes_GET_SIZE(a->a_lnotab);
3719 if (nbytes >= len) {
3720 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
3721 len = nbytes;
3722 else if (len <= INT_MAX / 2)
3723 len *= 2;
3724 else {
3725 PyErr_NoMemory();
3726 return 0;
3727 }
3728 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
3729 return 0;
3730 }
3731 lnotab = (unsigned char *)
3732 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3733 for (j = 0; j < ncodes; j++) {
3734 *lnotab++ = 255;
3735 *lnotab++ = 0;
3736 }
3737 d_bytecode -= ncodes * 255;
3738 a->a_lnotab_off += ncodes * 2;
3739 }
3740 assert(d_bytecode <= 255);
3741 if (d_lineno > 255) {
3742 int j, nbytes, ncodes = d_lineno / 255;
3743 nbytes = a->a_lnotab_off + 2 * ncodes;
3744 len = PyBytes_GET_SIZE(a->a_lnotab);
3745 if (nbytes >= len) {
3746 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
3747 len = nbytes;
3748 else if (len <= INT_MAX / 2)
3749 len *= 2;
3750 else {
3751 PyErr_NoMemory();
3752 return 0;
3753 }
3754 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
3755 return 0;
3756 }
3757 lnotab = (unsigned char *)
3758 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3759 *lnotab++ = d_bytecode;
3760 *lnotab++ = 255;
3761 d_bytecode = 0;
3762 for (j = 1; j < ncodes; j++) {
3763 *lnotab++ = 0;
3764 *lnotab++ = 255;
3765 }
3766 d_lineno -= ncodes * 255;
3767 a->a_lnotab_off += ncodes * 2;
3768 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003770 len = PyBytes_GET_SIZE(a->a_lnotab);
3771 if (a->a_lnotab_off + 2 >= len) {
3772 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
3773 return 0;
3774 }
3775 lnotab = (unsigned char *)
3776 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003778 a->a_lnotab_off += 2;
3779 if (d_bytecode) {
3780 *lnotab++ = d_bytecode;
Victor Stinner4f2dab52011-05-27 16:46:51 +02003781 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003782 }
3783 else { /* First line of a block; def stmt, etc. */
3784 *lnotab++ = 0;
Victor Stinner4f2dab52011-05-27 16:46:51 +02003785 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003786 }
3787 a->a_lineno = i->i_lineno;
3788 a->a_lineno_off = a->a_offset;
3789 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003790}
3791
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003792/* assemble_emit()
3793 Extend the bytecode with a new instruction.
3794 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003795*/
3796
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003797static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003798assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003799{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003800 int size, arg = 0, ext = 0;
3801 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
3802 char *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003804 size = instrsize(i);
3805 if (i->i_hasarg) {
3806 arg = i->i_oparg;
3807 ext = arg >> 16;
3808 }
3809 if (i->i_lineno && !assemble_lnotab(a, i))
3810 return 0;
3811 if (a->a_offset + size >= len) {
3812 if (len > PY_SSIZE_T_MAX / 2)
3813 return 0;
3814 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
3815 return 0;
3816 }
3817 code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
3818 a->a_offset += size;
3819 if (size == 6) {
3820 assert(i->i_hasarg);
3821 *code++ = (char)EXTENDED_ARG;
3822 *code++ = ext & 0xff;
3823 *code++ = ext >> 8;
3824 arg &= 0xffff;
3825 }
3826 *code++ = i->i_opcode;
3827 if (i->i_hasarg) {
3828 assert(size == 3 || size == 6);
3829 *code++ = arg & 0xff;
Victor Stinner4f2dab52011-05-27 16:46:51 +02003830 *code++ = arg >> 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003831 }
3832 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003833}
3834
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003835static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003836assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003837{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003838 basicblock *b;
3839 int bsize, totsize, extended_arg_count = 0, last_extended_arg_count;
3840 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003842 /* Compute the size of each block and fixup jump args.
3843 Replace block pointer with position in bytecode. */
3844 do {
3845 totsize = 0;
3846 for (i = a->a_nblocks - 1; i >= 0; i--) {
3847 b = a->a_postorder[i];
3848 bsize = blocksize(b);
3849 b->b_offset = totsize;
3850 totsize += bsize;
3851 }
3852 last_extended_arg_count = extended_arg_count;
3853 extended_arg_count = 0;
3854 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3855 bsize = b->b_offset;
3856 for (i = 0; i < b->b_iused; i++) {
3857 struct instr *instr = &b->b_instr[i];
3858 /* Relative jumps are computed relative to
3859 the instruction pointer after fetching
3860 the jump instruction.
3861 */
3862 bsize += instrsize(instr);
3863 if (instr->i_jabs)
3864 instr->i_oparg = instr->i_target->b_offset;
3865 else if (instr->i_jrel) {
3866 int delta = instr->i_target->b_offset - bsize;
3867 instr->i_oparg = delta;
3868 }
3869 else
3870 continue;
3871 if (instr->i_oparg > 0xffff)
3872 extended_arg_count++;
3873 }
3874 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003876 /* XXX: This is an awful hack that could hurt performance, but
3877 on the bright side it should work until we come up
3878 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00003879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003880 The issue is that in the first loop blocksize() is called
3881 which calls instrsize() which requires i_oparg be set
3882 appropriately. There is a bootstrap problem because
3883 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00003884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003885 So we loop until we stop seeing new EXTENDED_ARGs.
3886 The only EXTENDED_ARGs that could be popping up are
3887 ones in jump instructions. So this should converge
3888 fairly quickly.
3889 */
3890 } while (last_extended_arg_count != extended_arg_count);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003891}
3892
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003893static PyObject *
3894dict_keys_inorder(PyObject *dict, int offset)
3895{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003896 PyObject *tuple, *k, *v;
3897 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003899 tuple = PyTuple_New(size);
3900 if (tuple == NULL)
3901 return NULL;
3902 while (PyDict_Next(dict, &pos, &k, &v)) {
3903 i = PyLong_AS_LONG(v);
3904 /* The keys of the dictionary are tuples. (see compiler_add_o)
3905 The object we want is always first, though. */
3906 k = PyTuple_GET_ITEM(k, 0);
3907 Py_INCREF(k);
3908 assert((i - offset) < size);
3909 assert((i - offset) >= 0);
3910 PyTuple_SET_ITEM(tuple, i - offset, k);
3911 }
3912 return tuple;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003913}
3914
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003915static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003916compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003917{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003918 PySTEntryObject *ste = c->u->u_ste;
3919 int flags = 0, n;
3920 if (ste->ste_type != ModuleBlock)
3921 flags |= CO_NEWLOCALS;
3922 if (ste->ste_type == FunctionBlock) {
3923 if (!ste->ste_unoptimized)
3924 flags |= CO_OPTIMIZED;
3925 if (ste->ste_nested)
3926 flags |= CO_NESTED;
3927 if (ste->ste_generator)
3928 flags |= CO_GENERATOR;
3929 if (ste->ste_varargs)
3930 flags |= CO_VARARGS;
3931 if (ste->ste_varkeywords)
3932 flags |= CO_VARKEYWORDS;
3933 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003935 /* (Only) inherit compilerflags in PyCF_MASK */
3936 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003938 n = PyDict_Size(c->u->u_freevars);
3939 if (n < 0)
3940 return -1;
3941 if (n == 0) {
3942 n = PyDict_Size(c->u->u_cellvars);
3943 if (n < 0)
3944 return -1;
3945 if (n == 0) {
3946 flags |= CO_NOFREE;
3947 }
3948 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003950 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003951}
3952
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003953static PyCodeObject *
3954makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003955{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003956 PyObject *tmp;
3957 PyCodeObject *co = NULL;
3958 PyObject *consts = NULL;
3959 PyObject *names = NULL;
3960 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003961 PyObject *name = NULL;
3962 PyObject *freevars = NULL;
3963 PyObject *cellvars = NULL;
3964 PyObject *bytecode = NULL;
3965 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003967 tmp = dict_keys_inorder(c->u->u_consts, 0);
3968 if (!tmp)
3969 goto error;
3970 consts = PySequence_List(tmp); /* optimize_code requires a list */
3971 Py_DECREF(tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003973 names = dict_keys_inorder(c->u->u_names, 0);
3974 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3975 if (!consts || !names || !varnames)
3976 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003978 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3979 if (!cellvars)
3980 goto error;
3981 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3982 if (!freevars)
3983 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003984 nlocals = PyDict_Size(c->u->u_varnames);
3985 flags = compute_code_flags(c);
3986 if (flags < 0)
3987 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003989 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
3990 if (!bytecode)
3991 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003993 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
3994 if (!tmp)
3995 goto error;
3996 Py_DECREF(consts);
3997 consts = tmp;
3998
3999 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
4000 nlocals, stackdepth(c), flags,
4001 bytecode, consts, names, varnames,
4002 freevars, cellvars,
Benjamin Peterson43b06862011-05-27 09:08:01 -05004003 c->c_filename_obj, c->u->u_name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004004 c->u->u_firstlineno,
4005 a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004006 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004007 Py_XDECREF(consts);
4008 Py_XDECREF(names);
4009 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004010 Py_XDECREF(name);
4011 Py_XDECREF(freevars);
4012 Py_XDECREF(cellvars);
4013 Py_XDECREF(bytecode);
4014 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004015}
4016
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004017
4018/* For debugging purposes only */
4019#if 0
4020static void
4021dump_instr(const struct instr *i)
4022{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004023 const char *jrel = i->i_jrel ? "jrel " : "";
4024 const char *jabs = i->i_jabs ? "jabs " : "";
4025 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004027 *arg = '\0';
4028 if (i->i_hasarg)
4029 sprintf(arg, "arg: %d ", i->i_oparg);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004031 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4032 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004033}
4034
4035static void
4036dump_basicblock(const basicblock *b)
4037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004038 const char *seen = b->b_seen ? "seen " : "";
4039 const char *b_return = b->b_return ? "return " : "";
4040 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4041 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4042 if (b->b_instr) {
4043 int i;
4044 for (i = 0; i < b->b_iused; i++) {
4045 fprintf(stderr, " [%02d] ", i);
4046 dump_instr(b->b_instr + i);
4047 }
4048 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004049}
4050#endif
4051
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004052static PyCodeObject *
4053assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004054{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004055 basicblock *b, *entryblock;
4056 struct assembler a;
4057 int i, j, nblocks;
4058 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004060 /* Make sure every block that falls off the end returns None.
4061 XXX NEXT_BLOCK() isn't quite right, because if the last
4062 block ends with a jump or return b_next shouldn't set.
4063 */
4064 if (!c->u->u_curblock->b_return) {
4065 NEXT_BLOCK(c);
4066 if (addNone)
4067 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4068 ADDOP(c, RETURN_VALUE);
4069 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004071 nblocks = 0;
4072 entryblock = NULL;
4073 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4074 nblocks++;
4075 entryblock = b;
4076 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004078 /* Set firstlineno if it wasn't explicitly set. */
4079 if (!c->u->u_firstlineno) {
4080 if (entryblock && entryblock->b_instr)
4081 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4082 else
4083 c->u->u_firstlineno = 1;
4084 }
4085 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4086 goto error;
4087 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004089 /* Can't modify the bytecode after computing jump offsets. */
4090 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004092 /* Emit code in reverse postorder from dfs. */
4093 for (i = a.a_nblocks - 1; i >= 0; i--) {
4094 b = a.a_postorder[i];
4095 for (j = 0; j < b->b_iused; j++)
4096 if (!assemble_emit(&a, &b->b_instr[j]))
4097 goto error;
4098 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004100 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4101 goto error;
4102 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset) < 0)
4103 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004105 co = makecode(c, &a);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004106 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004107 assemble_free(&a);
4108 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004109}
Georg Brandl8334fd92010-12-04 10:26:46 +00004110
4111#undef PyAST_Compile
4112PyAPI_FUNC(PyCodeObject *)
4113PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
4114 PyArena *arena)
4115{
4116 return PyAST_CompileEx(mod, filename, flags, -1, arena);
4117}
4118
4119