blob: 19b2addc109afc127b1f87035f361abcf2a6455a [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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000028#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000029#include "ast.h"
30#include "code.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031#include "compile.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000032#include "symtable.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000033#include "opcode.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000034
Guido van Rossum8e793d91997-03-03 19:13:14 +000035int Py_OptimizeFlag = 0;
36
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000037#define DEFAULT_BLOCK_SIZE 16
38#define DEFAULT_BLOCKS 8
39#define DEFAULT_CODE_SIZE 128
40#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000041
Nick Coghlan650f0d02007-04-15 12:05:43 +000042#define COMP_GENEXP 0
43#define COMP_LISTCOMP 1
44#define COMP_SETCOMP 2
Guido van Rossum992d4a32007-07-11 13:09:30 +000045#define COMP_DICTCOMP 3
Nick Coghlan650f0d02007-04-15 12:05:43 +000046
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000047struct instr {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000048 unsigned i_jabs : 1;
49 unsigned i_jrel : 1;
50 unsigned i_hasarg : 1;
51 unsigned char i_opcode;
52 int i_oparg;
53 struct basicblock_ *i_target; /* target block (if jump instruction) */
54 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000055};
56
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000057typedef struct basicblock_ {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000058 /* Each basicblock in a compilation unit is linked via b_list in the
59 reverse order that the block are allocated. b_list points to the next
60 block, not to be confused with b_next, which is next by control flow. */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000061 struct basicblock_ *b_list;
62 /* number of instructions used */
63 int b_iused;
64 /* length of instruction array (b_instr) */
65 int b_ialloc;
66 /* pointer to an array of instructions, initially NULL */
67 struct instr *b_instr;
68 /* If b_next is non-NULL, it is a pointer to the next
69 block reached by normal control flow. */
70 struct basicblock_ *b_next;
71 /* b_seen is used to perform a DFS of basicblocks. */
72 unsigned b_seen : 1;
73 /* b_return is true if a RETURN_VALUE opcode is inserted. */
74 unsigned b_return : 1;
75 /* depth of stack upon entry of block, computed by stackdepth() */
76 int b_startdepth;
77 /* instruction offset for block, computed by assemble_jump_offsets() */
78 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000079} basicblock;
80
81/* fblockinfo tracks the current frame block.
82
Jeremy Hyltone9357b22006-03-01 15:47:05 +000083A frame block is used to handle loops, try/except, and try/finally.
84It's called a frame block to distinguish it from a basic block in the
85compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000086*/
87
88enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
89
90struct fblockinfo {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000091 enum fblocktype fb_type;
92 basicblock *fb_block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000093};
94
95/* The following items change on entry and exit of code blocks.
96 They must be saved and restored when returning to a block.
97*/
98struct compiler_unit {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000099 PySTEntryObject *u_ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000100
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000101 PyObject *u_name;
102 /* The following fields are dicts that map objects to
103 the index of them in co_XXX. The index is used as
104 the argument for opcodes that refer to those collections.
105 */
106 PyObject *u_consts; /* all constants */
107 PyObject *u_names; /* all names */
108 PyObject *u_varnames; /* local variables */
109 PyObject *u_cellvars; /* cell variables */
110 PyObject *u_freevars; /* free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000111
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000112 PyObject *u_private; /* for private name mangling */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000113
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000114 int u_argcount; /* number of arguments for block */
115 int u_kwonlyargcount; /* number of keyword only arguments for block */
116 /* Pointer to the most recently allocated block. By following b_list
117 members, you can reach all early allocated blocks. */
118 basicblock *u_blocks;
119 basicblock *u_curblock; /* pointer to current block */
120 int u_tmpname; /* temporary variables for list comps */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000121
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000122 int u_nfblocks;
123 struct fblockinfo u_fblock[CO_MAXBLOCKS];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000124
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000125 int u_firstlineno; /* the first lineno of the block */
126 int u_lineno; /* the lineno for the current stmt */
127 int u_lineno_set; /* boolean to indicate whether instr
128 has been generated with current lineno */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000129};
130
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000131/* This struct captures the global state of a compilation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000132
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000133The u pointer points to the current compilation unit, while units
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000134for enclosing blocks are stored in c_stack. The u and c_stack are
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000135managed by compiler_enter_scope() and compiler_exit_scope().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000136*/
137
138struct compiler {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000139 const char *c_filename;
140 struct symtable *c_st;
141 PyFutureFeatures *c_future; /* pointer to module's __future__ */
142 PyCompilerFlags *c_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000143
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000144 int c_interactive; /* true if in interactive mode */
145 int c_nestlevel;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000146
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000147 struct compiler_unit *u; /* compiler state for current block */
148 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
149 char *c_encoding; /* source encoding (a borrowed reference) */
150 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000151};
152
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000153static int compiler_enter_scope(struct compiler *, identifier, void *, int);
154static void compiler_free(struct compiler *);
155static basicblock *compiler_new_block(struct compiler *);
156static int compiler_next_instr(struct compiler *, basicblock *);
157static int compiler_addop(struct compiler *, int);
158static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
159static int compiler_addop_i(struct compiler *, int, int);
160static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000161static basicblock *compiler_use_new_block(struct compiler *);
162static int compiler_error(struct compiler *, const char *);
163static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
164
165static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
166static int compiler_visit_stmt(struct compiler *, stmt_ty);
167static int compiler_visit_keyword(struct compiler *, keyword_ty);
168static int compiler_visit_expr(struct compiler *, expr_ty);
169static int compiler_augassign(struct compiler *, stmt_ty);
170static int compiler_visit_slice(struct compiler *, slice_ty,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000171 expr_context_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000172
173static int compiler_push_fblock(struct compiler *, enum fblocktype,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000174 basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000175static void compiler_pop_fblock(struct compiler *, enum fblocktype,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000176 basicblock *);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000177/* Returns true if there is a loop on the fblock stack. */
178static int compiler_in_loop(struct compiler *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000179
180static int inplace_binop(struct compiler *, operator_ty);
181static int expr_constant(expr_ty e);
182
Guido van Rossumc2e20742006-02-27 22:32:47 +0000183static int compiler_with(struct compiler *, stmt_ty);
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000184static int compiler_call_helper(struct compiler *c, int n,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000185 asdl_seq *args,
186 asdl_seq *keywords,
187 expr_ty starargs,
188 expr_ty kwargs);
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 Pitrou7f14f0d2010-05-09 16:14:21 +0000198 /* Name mangling: __private becomes _classname__private.
199 This is independent from how the name is used. */
200 const Py_UNICODE *p, *name = PyUnicode_AS_UNICODE(ident);
201 Py_UNICODE *buffer;
202 size_t nlen, plen;
203 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
204 name == NULL || name[0] != '_' || name[1] != '_') {
205 Py_INCREF(ident);
206 return ident;
207 }
208 p = PyUnicode_AS_UNICODE(privateobj);
209 nlen = Py_UNICODE_strlen(name);
210 /* Don't mangle __id__ or names with dots.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000211
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000212 The only time a name with a dot can occur is when
213 we are compiling an import statement that has a
214 package name.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000215
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000216 TODO(jhylton): Decide whether we want to support
217 mangling of the module name, e.g. __M.X.
218 */
219 if ((name[nlen-1] == '_' && name[nlen-2] == '_')
220 || Py_UNICODE_strchr(name, '.')) {
221 Py_INCREF(ident);
222 return ident; /* Don't mangle __whatever__ */
223 }
224 /* Strip leading underscores from class name */
225 while (*p == '_')
226 p++;
227 if (*p == 0) {
228 Py_INCREF(ident);
229 return ident; /* Don't mangle if class is just underscores */
230 }
231 plen = Py_UNICODE_strlen(p);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000232
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000233 assert(1 <= PY_SSIZE_T_MAX - nlen);
234 assert(1 + nlen <= PY_SSIZE_T_MAX - plen);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000235
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000236 ident = PyUnicode_FromStringAndSize(NULL, 1 + nlen + plen);
237 if (!ident)
238 return 0;
239 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
240 buffer = PyUnicode_AS_UNICODE(ident);
241 buffer[0] = '_';
242 Py_UNICODE_strncpy(buffer+1, p, plen);
243 Py_UNICODE_strcpy(buffer+1+plen, name);
244 return ident;
Michael W. Hudson60934622004-08-12 17:56:29 +0000245}
246
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000247static int
248compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000249{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000250 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000251
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000252 c->c_stack = PyList_New(0);
253 if (!c->c_stack)
254 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000255
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000256 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000257}
258
259PyCodeObject *
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000260PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000261 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000262{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000263 struct compiler c;
264 PyCodeObject *co = NULL;
265 PyCompilerFlags local_flags;
266 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000267
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000268 if (!__doc__) {
269 __doc__ = PyUnicode_InternFromString("__doc__");
270 if (!__doc__)
271 return NULL;
272 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000273
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000274 if (!compiler_init(&c))
275 return NULL;
276 c.c_filename = filename;
277 c.c_arena = arena;
278 c.c_future = PyFuture_FromAST(mod, filename);
279 if (c.c_future == NULL)
280 goto finally;
281 if (!flags) {
282 local_flags.cf_flags = 0;
283 flags = &local_flags;
284 }
285 merged = c.c_future->ff_features | flags->cf_flags;
286 c.c_future->ff_features = merged;
287 flags->cf_flags = merged;
288 c.c_flags = flags;
289 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000290
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000291 c.c_st = PySymtable_Build(mod, filename, c.c_future);
292 if (c.c_st == NULL) {
293 if (!PyErr_Occurred())
294 PyErr_SetString(PyExc_SystemError, "no symtable");
295 goto finally;
296 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000297
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000298 /* XXX initialize to NULL for now, need to handle */
299 c.c_encoding = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000300
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000301 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000302
Thomas Wouters1175c432006-02-27 22:49:54 +0000303 finally:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000304 compiler_free(&c);
305 assert(co || PyErr_Occurred());
306 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000307}
308
309PyCodeObject *
310PyNode_Compile(struct _node *n, const char *filename)
311{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000312 PyCodeObject *co = NULL;
313 mod_ty mod;
314 PyArena *arena = PyArena_New();
315 if (!arena)
316 return NULL;
317 mod = PyAST_FromNode(n, NULL, filename, arena);
318 if (mod)
319 co = PyAST_Compile(mod, filename, NULL, arena);
320 PyArena_Free(arena);
321 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000322}
323
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000324static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000325compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000326{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000327 if (c->c_st)
328 PySymtable_Free(c->c_st);
329 if (c->c_future)
330 PyObject_Free(c->c_future);
331 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000332}
333
Guido van Rossum79f25d91997-04-29 20:08:16 +0000334static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000335list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000336{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000337 Py_ssize_t i, n;
338 PyObject *v, *k;
339 PyObject *dict = PyDict_New();
340 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000341
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000342 n = PyList_Size(list);
343 for (i = 0; i < n; i++) {
344 v = PyLong_FromLong(i);
345 if (!v) {
346 Py_DECREF(dict);
347 return NULL;
348 }
349 k = PyList_GET_ITEM(list, i);
350 k = PyTuple_Pack(2, k, k->ob_type);
351 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
352 Py_XDECREF(k);
353 Py_DECREF(v);
354 Py_DECREF(dict);
355 return NULL;
356 }
357 Py_DECREF(k);
358 Py_DECREF(v);
359 }
360 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000361}
362
363/* Return new dict containing names from src that match scope(s).
364
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000365src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000366either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000367values are integers, starting at offset and increasing by one for
368each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000369*/
370
371static PyObject *
372dictbytype(PyObject *src, int scope_type, int flag, int offset)
373{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000374 Py_ssize_t pos = 0, i = offset, scope;
375 PyObject *k, *v, *dest = PyDict_New();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000376
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000377 assert(offset >= 0);
378 if (dest == NULL)
379 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000380
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000381 while (PyDict_Next(src, &pos, &k, &v)) {
382 /* XXX this should probably be a macro in symtable.h */
383 long vi;
384 assert(PyLong_Check(v));
385 vi = PyLong_AS_LONG(v);
386 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000387
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000388 if (scope == scope_type || vi & flag) {
389 PyObject *tuple, *item = PyLong_FromLong(i);
390 if (item == NULL) {
391 Py_DECREF(dest);
392 return NULL;
393 }
394 i++;
395 tuple = PyTuple_Pack(2, k, k->ob_type);
396 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
397 Py_DECREF(item);
398 Py_DECREF(dest);
399 Py_XDECREF(tuple);
400 return NULL;
401 }
402 Py_DECREF(item);
403 Py_DECREF(tuple);
404 }
405 }
406 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000407}
408
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000409static void
410compiler_unit_check(struct compiler_unit *u)
411{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000412 basicblock *block;
413 for (block = u->u_blocks; block != NULL; block = block->b_list) {
414 assert((void *)block != (void *)0xcbcbcbcb);
415 assert((void *)block != (void *)0xfbfbfbfb);
416 assert((void *)block != (void *)0xdbdbdbdb);
417 if (block->b_instr != NULL) {
418 assert(block->b_ialloc > 0);
419 assert(block->b_iused > 0);
420 assert(block->b_ialloc >= block->b_iused);
421 }
422 else {
423 assert (block->b_iused == 0);
424 assert (block->b_ialloc == 0);
425 }
426 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000427}
428
429static void
430compiler_unit_free(struct compiler_unit *u)
431{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000432 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000433
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000434 compiler_unit_check(u);
435 b = u->u_blocks;
436 while (b != NULL) {
437 if (b->b_instr)
438 PyObject_Free((void *)b->b_instr);
439 next = b->b_list;
440 PyObject_Free((void *)b);
441 b = next;
442 }
443 Py_CLEAR(u->u_ste);
444 Py_CLEAR(u->u_name);
445 Py_CLEAR(u->u_consts);
446 Py_CLEAR(u->u_names);
447 Py_CLEAR(u->u_varnames);
448 Py_CLEAR(u->u_freevars);
449 Py_CLEAR(u->u_cellvars);
450 Py_CLEAR(u->u_private);
451 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000452}
453
454static int
455compiler_enter_scope(struct compiler *c, identifier name, void *key,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000456 int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000457{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000458 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000459
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000460 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
461 struct compiler_unit));
462 if (!u) {
463 PyErr_NoMemory();
464 return 0;
465 }
466 memset(u, 0, sizeof(struct compiler_unit));
467 u->u_argcount = 0;
468 u->u_kwonlyargcount = 0;
469 u->u_ste = PySymtable_Lookup(c->c_st, key);
470 if (!u->u_ste) {
471 compiler_unit_free(u);
472 return 0;
473 }
474 Py_INCREF(name);
475 u->u_name = name;
476 u->u_varnames = list2dict(u->u_ste->ste_varnames);
477 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
478 if (!u->u_varnames || !u->u_cellvars) {
479 compiler_unit_free(u);
480 return 0;
481 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000482
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000483 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
484 PyDict_Size(u->u_cellvars));
485 if (!u->u_freevars) {
486 compiler_unit_free(u);
487 return 0;
488 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000489
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000490 u->u_blocks = NULL;
491 u->u_tmpname = 0;
492 u->u_nfblocks = 0;
493 u->u_firstlineno = lineno;
494 u->u_lineno = 0;
495 u->u_lineno_set = 0;
496 u->u_consts = PyDict_New();
497 if (!u->u_consts) {
498 compiler_unit_free(u);
499 return 0;
500 }
501 u->u_names = PyDict_New();
502 if (!u->u_names) {
503 compiler_unit_free(u);
504 return 0;
505 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000507 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000508
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000509 /* Push the old compiler_unit on the stack. */
510 if (c->u) {
511 PyObject *capsule = PyCapsule_New(c->u, COMPILER_CAPSULE_NAME_COMPILER_UNIT, NULL);
512 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
513 Py_XDECREF(capsule);
514 compiler_unit_free(u);
515 return 0;
516 }
517 Py_DECREF(capsule);
518 u->u_private = c->u->u_private;
519 Py_XINCREF(u->u_private);
520 }
521 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000522
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000523 c->c_nestlevel++;
524 if (compiler_use_new_block(c) == NULL)
525 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000526
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000527 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528}
529
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000530static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531compiler_exit_scope(struct compiler *c)
532{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000533 int n;
534 PyObject *capsule;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000535
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000536 c->c_nestlevel--;
537 compiler_unit_free(c->u);
538 /* Restore c->u to the parent unit. */
539 n = PyList_GET_SIZE(c->c_stack) - 1;
540 if (n >= 0) {
541 capsule = PyList_GET_ITEM(c->c_stack, n);
542 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_CAPSULE_NAME_COMPILER_UNIT);
543 assert(c->u);
544 /* we are deleting from a list so this really shouldn't fail */
545 if (PySequence_DelItem(c->c_stack, n) < 0)
546 Py_FatalError("compiler_exit_scope()");
547 compiler_unit_check(c->u);
548 }
549 else
550 c->u = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000551
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000552}
553
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000554/* Allocate a new "anonymous" local variable. Used by with statements. */
555
556static PyObject *
557compiler_new_tmpname(struct compiler *c)
558{
559 char tmpname[256];
560 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
561 return PyUnicode_FromString(tmpname);
Benjamin Peterson32123372009-06-28 22:36:18 +0000562}
563
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000564/* Allocate a new block and return a pointer to it.
565 Returns NULL on error.
566*/
567
568static basicblock *
569compiler_new_block(struct compiler *c)
570{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000571 basicblock *b;
572 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000574 u = c->u;
575 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
576 if (b == NULL) {
577 PyErr_NoMemory();
578 return NULL;
579 }
580 memset((void *)b, 0, sizeof(basicblock));
581 /* Extend the singly linked list of blocks with new block. */
582 b->b_list = u->u_blocks;
583 u->u_blocks = b;
584 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000585}
586
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000587static basicblock *
588compiler_use_new_block(struct compiler *c)
589{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000590 basicblock *block = compiler_new_block(c);
591 if (block == NULL)
592 return NULL;
593 c->u->u_curblock = block;
594 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000595}
596
597static basicblock *
598compiler_next_block(struct compiler *c)
599{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000600 basicblock *block = compiler_new_block(c);
601 if (block == NULL)
602 return NULL;
603 c->u->u_curblock->b_next = block;
604 c->u->u_curblock = block;
605 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606}
607
608static basicblock *
609compiler_use_next_block(struct compiler *c, basicblock *block)
610{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000611 assert(block != NULL);
612 c->u->u_curblock->b_next = block;
613 c->u->u_curblock = block;
614 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000615}
616
617/* Returns the offset of the next instruction in the current block's
618 b_instr array. Resizes the b_instr as necessary.
619 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000620*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000621
622static int
623compiler_next_instr(struct compiler *c, basicblock *b)
624{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000625 assert(b != NULL);
626 if (b->b_instr == NULL) {
627 b->b_instr = (struct instr *)PyObject_Malloc(
628 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
629 if (b->b_instr == NULL) {
630 PyErr_NoMemory();
631 return -1;
632 }
633 b->b_ialloc = DEFAULT_BLOCK_SIZE;
634 memset((char *)b->b_instr, 0,
635 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
636 }
637 else if (b->b_iused == b->b_ialloc) {
638 struct instr *tmp;
639 size_t oldsize, newsize;
640 oldsize = b->b_ialloc * sizeof(struct instr);
641 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000642
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000643 if (oldsize > (PY_SIZE_MAX >> 1)) {
644 PyErr_NoMemory();
645 return -1;
646 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000647
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000648 if (newsize == 0) {
649 PyErr_NoMemory();
650 return -1;
651 }
652 b->b_ialloc <<= 1;
653 tmp = (struct instr *)PyObject_Realloc(
654 (void *)b->b_instr, newsize);
655 if (tmp == NULL) {
656 PyErr_NoMemory();
657 return -1;
658 }
659 b->b_instr = tmp;
660 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
661 }
662 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000663}
664
Christian Heimes2202f872008-02-06 14:31:34 +0000665/* Set the i_lineno member of the instruction at offset off if the
666 line number for the current expression/statement has not
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000667 already been set. If it has been set, the call has no effect.
668
Christian Heimes2202f872008-02-06 14:31:34 +0000669 The line number is reset in the following cases:
670 - when entering a new scope
671 - on each statement
672 - on each expression that start a new line
673 - before the "except" clause
674 - before the "for" and "while" expressions
Thomas Wouters89f507f2006-12-13 04:49:30 +0000675*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000676
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677static void
678compiler_set_lineno(struct compiler *c, int off)
679{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000680 basicblock *b;
681 if (c->u->u_lineno_set)
682 return;
683 c->u->u_lineno_set = 1;
684 b = c->u->u_curblock;
685 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000686}
687
688static int
689opcode_stack_effect(int opcode, int oparg)
690{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000691 switch (opcode) {
692 case POP_TOP:
693 return -1;
694 case ROT_TWO:
695 case ROT_THREE:
696 return 0;
697 case DUP_TOP:
698 return 1;
699 case ROT_FOUR:
700 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000701
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000702 case UNARY_POSITIVE:
703 case UNARY_NEGATIVE:
704 case UNARY_NOT:
705 case UNARY_INVERT:
706 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000707
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000708 case SET_ADD:
709 case LIST_APPEND:
710 return -1;
711 case MAP_ADD:
712 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000713
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000714 case BINARY_POWER:
715 case BINARY_MULTIPLY:
716 case BINARY_MODULO:
717 case BINARY_ADD:
718 case BINARY_SUBTRACT:
719 case BINARY_SUBSCR:
720 case BINARY_FLOOR_DIVIDE:
721 case BINARY_TRUE_DIVIDE:
722 return -1;
723 case INPLACE_FLOOR_DIVIDE:
724 case INPLACE_TRUE_DIVIDE:
725 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000726
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000727 case INPLACE_ADD:
728 case INPLACE_SUBTRACT:
729 case INPLACE_MULTIPLY:
730 case INPLACE_MODULO:
731 return -1;
732 case STORE_SUBSCR:
733 return -3;
734 case STORE_MAP:
735 return -2;
736 case DELETE_SUBSCR:
737 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000738
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000739 case BINARY_LSHIFT:
740 case BINARY_RSHIFT:
741 case BINARY_AND:
742 case BINARY_XOR:
743 case BINARY_OR:
744 return -1;
745 case INPLACE_POWER:
746 return -1;
747 case GET_ITER:
748 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000750 case PRINT_EXPR:
751 return -1;
752 case LOAD_BUILD_CLASS:
753 return 1;
754 case INPLACE_LSHIFT:
755 case INPLACE_RSHIFT:
756 case INPLACE_AND:
757 case INPLACE_XOR:
758 case INPLACE_OR:
759 return -1;
760 case BREAK_LOOP:
761 return 0;
762 case WITH_CLEANUP:
763 return -1; /* XXX Sometimes more */
764 case STORE_LOCALS:
765 return -1;
766 case RETURN_VALUE:
767 return -1;
768 case IMPORT_STAR:
769 return -1;
770 case YIELD_VALUE:
771 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000773 case POP_BLOCK:
774 return 0;
775 case POP_EXCEPT:
776 return 0; /* -3 except if bad bytecode */
777 case END_FINALLY:
778 return -1; /* or -2 or -3 if exception occurred */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000780 case STORE_NAME:
781 return -1;
782 case DELETE_NAME:
783 return 0;
784 case UNPACK_SEQUENCE:
785 return oparg-1;
786 case UNPACK_EX:
787 return (oparg&0xFF) + (oparg>>8);
788 case FOR_ITER:
789 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000791 case STORE_ATTR:
792 return -2;
793 case DELETE_ATTR:
794 return -1;
795 case STORE_GLOBAL:
796 return -1;
797 case DELETE_GLOBAL:
798 return 0;
799 case DUP_TOPX:
800 return oparg;
801 case LOAD_CONST:
802 return 1;
803 case LOAD_NAME:
804 return 1;
805 case BUILD_TUPLE:
806 case BUILD_LIST:
807 case BUILD_SET:
808 return 1-oparg;
809 case BUILD_MAP:
810 return 1;
811 case LOAD_ATTR:
812 return 0;
813 case COMPARE_OP:
814 return -1;
815 case IMPORT_NAME:
816 return 0;
817 case IMPORT_FROM:
818 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000820 case JUMP_FORWARD:
821 case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */
822 case JUMP_IF_FALSE_OR_POP: /* "" */
823 case JUMP_ABSOLUTE:
824 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000826 case POP_JUMP_IF_FALSE:
827 case POP_JUMP_IF_TRUE:
828 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000829
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000830 case LOAD_GLOBAL:
831 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000833 case CONTINUE_LOOP:
834 return 0;
835 case SETUP_LOOP:
836 return 0;
837 case SETUP_EXCEPT:
838 case SETUP_FINALLY:
839 return 6; /* can push 3 values for the new exception
840 + 3 others for the previous exception state */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000842 case LOAD_FAST:
843 return 1;
844 case STORE_FAST:
845 return -1;
846 case DELETE_FAST:
847 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000849 case RAISE_VARARGS:
850 return -oparg;
Neal Norwitzc1505362006-12-28 06:47:50 +0000851#define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000852 case CALL_FUNCTION:
853 return -NARGS(oparg);
854 case CALL_FUNCTION_VAR:
855 case CALL_FUNCTION_KW:
856 return -NARGS(oparg)-1;
857 case CALL_FUNCTION_VAR_KW:
858 return -NARGS(oparg)-2;
859 case MAKE_FUNCTION:
860 return -NARGS(oparg) - ((oparg >> 16) & 0xffff);
861 case MAKE_CLOSURE:
862 return -1 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000863#undef NARGS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000864 case BUILD_SLICE:
865 if (oparg == 3)
866 return -2;
867 else
868 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000869
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000870 case LOAD_CLOSURE:
871 return 1;
872 case LOAD_DEREF:
873 return 1;
874 case STORE_DEREF:
875 return -1;
876 default:
877 fprintf(stderr, "opcode = %d\n", opcode);
878 Py_FatalError("opcode_stack_effect()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000879
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000880 }
881 return 0; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000882}
883
884/* Add an opcode with no argument.
885 Returns 0 on failure, 1 on success.
886*/
887
888static int
889compiler_addop(struct compiler *c, int opcode)
890{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000891 basicblock *b;
892 struct instr *i;
893 int off;
894 off = compiler_next_instr(c, c->u->u_curblock);
895 if (off < 0)
896 return 0;
897 b = c->u->u_curblock;
898 i = &b->b_instr[off];
899 i->i_opcode = opcode;
900 i->i_hasarg = 0;
901 if (opcode == RETURN_VALUE)
902 b->b_return = 1;
903 compiler_set_lineno(c, off);
904 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000905}
906
907static int
908compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
909{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000910 PyObject *t, *v;
911 Py_ssize_t arg;
912 double d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000913
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000914 /* necessary to make sure types aren't coerced (e.g., int and long) */
915 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
916 if (PyFloat_Check(o)) {
917 d = PyFloat_AS_DOUBLE(o);
918 /* all we need is to make the tuple different in either the 0.0
919 * or -0.0 case from all others, just to avoid the "coercion".
920 */
921 if (d == 0.0 && copysign(1.0, d) < 0.0)
922 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
923 else
924 t = PyTuple_Pack(2, o, o->ob_type);
925 }
Mark Dickinsonb42dab52009-10-27 17:31:26 +0000926#ifndef WITHOUT_COMPLEX
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000927 else if (PyComplex_Check(o)) {
928 Py_complex z;
929 int real_negzero, imag_negzero;
930 /* For the complex case we must make complex(x, 0.)
931 different from complex(x, -0.) and complex(0., y)
932 different from complex(-0., y), for any x and y.
933 All four complex zeros must be distinguished.*/
934 z = PyComplex_AsCComplex(o);
935 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
936 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
937 if (real_negzero && imag_negzero) {
938 t = PyTuple_Pack(5, o, o->ob_type,
939 Py_None, Py_None, Py_None);
Christian Heimes400adb02008-02-01 08:12:03 +0000940 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000941 else if (imag_negzero) {
942 t = PyTuple_Pack(4, o, o->ob_type, Py_None, Py_None);
943 }
944 else if (real_negzero) {
945 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
946 }
947 else {
948 t = PyTuple_Pack(2, o, o->ob_type);
949 }
950 }
Mark Dickinsonb42dab52009-10-27 17:31:26 +0000951#endif /* WITHOUT_COMPLEX */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000952 else {
953 t = PyTuple_Pack(2, o, o->ob_type);
954 }
955 if (t == NULL)
956 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000957
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000958 v = PyDict_GetItem(dict, t);
959 if (!v) {
960 if (PyErr_Occurred())
961 return -1;
962 arg = PyDict_Size(dict);
963 v = PyLong_FromLong(arg);
964 if (!v) {
965 Py_DECREF(t);
966 return -1;
967 }
968 if (PyDict_SetItem(dict, t, v) < 0) {
969 Py_DECREF(t);
970 Py_DECREF(v);
971 return -1;
972 }
973 Py_DECREF(v);
974 }
975 else
976 arg = PyLong_AsLong(v);
977 Py_DECREF(t);
978 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979}
980
981static int
982compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000983 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984{
985 int arg = compiler_add_o(c, dict, o);
986 if (arg < 0)
Antoine Pitrou252e8e22010-06-22 21:53:47 +0000987 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988 return compiler_addop_i(c, opcode, arg);
989}
990
991static int
992compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000993 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994{
995 int arg;
996 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
997 if (!mangled)
Antoine Pitrou252e8e22010-06-22 21:53:47 +0000998 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999 arg = compiler_add_o(c, dict, mangled);
1000 Py_DECREF(mangled);
1001 if (arg < 0)
Antoine Pitrou252e8e22010-06-22 21:53:47 +00001002 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003 return compiler_addop_i(c, opcode, arg);
1004}
1005
1006/* Add an opcode with an integer argument.
1007 Returns 0 on failure, 1 on success.
1008*/
1009
1010static int
1011compiler_addop_i(struct compiler *c, int opcode, int oparg)
1012{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001013 struct instr *i;
1014 int off;
1015 off = compiler_next_instr(c, c->u->u_curblock);
1016 if (off < 0)
1017 return 0;
1018 i = &c->u->u_curblock->b_instr[off];
1019 i->i_opcode = opcode;
1020 i->i_oparg = oparg;
1021 i->i_hasarg = 1;
1022 compiler_set_lineno(c, off);
1023 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001024}
1025
1026static int
1027compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1028{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001029 struct instr *i;
1030 int off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001031
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001032 assert(b != NULL);
1033 off = compiler_next_instr(c, c->u->u_curblock);
1034 if (off < 0)
1035 return 0;
1036 i = &c->u->u_curblock->b_instr[off];
1037 i->i_opcode = opcode;
1038 i->i_target = b;
1039 i->i_hasarg = 1;
1040 if (absolute)
1041 i->i_jabs = 1;
1042 else
1043 i->i_jrel = 1;
1044 compiler_set_lineno(c, off);
1045 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001046}
1047
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001048/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1049 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001050 it as the current block. NEXT_BLOCK() also creates an implicit jump
1051 from the current block to the new block.
1052*/
1053
Thomas Wouters89f507f2006-12-13 04:49:30 +00001054/* The returns inside these macros make it impossible to decref objects
1055 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001056*/
1057
1058
1059#define NEW_BLOCK(C) { \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001060 if (compiler_use_new_block((C)) == NULL) \
1061 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062}
1063
1064#define NEXT_BLOCK(C) { \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001065 if (compiler_next_block((C)) == NULL) \
1066 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001067}
1068
1069#define ADDOP(C, OP) { \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001070 if (!compiler_addop((C), (OP))) \
1071 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072}
1073
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001074#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001075 if (!compiler_addop((C), (OP))) { \
1076 compiler_exit_scope(c); \
1077 return 0; \
1078 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001079}
1080
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001082 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1083 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084}
1085
1086#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001087 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1088 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089}
1090
1091#define ADDOP_I(C, OP, O) { \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001092 if (!compiler_addop_i((C), (OP), (O))) \
1093 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001094}
1095
1096#define ADDOP_JABS(C, OP, O) { \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001097 if (!compiler_addop_j((C), (OP), (O), 1)) \
1098 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099}
1100
1101#define ADDOP_JREL(C, OP, O) { \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001102 if (!compiler_addop_j((C), (OP), (O), 0)) \
1103 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001104}
1105
1106/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1107 the ASDL name to synthesize the name of the C type and the visit function.
1108*/
1109
1110#define VISIT(C, TYPE, V) {\
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001111 if (!compiler_visit_ ## TYPE((C), (V))) \
1112 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113}
1114
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001115#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001116 if (!compiler_visit_ ## TYPE((C), (V))) { \
1117 compiler_exit_scope(c); \
1118 return 0; \
1119 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001120}
1121
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001123 if (!compiler_visit_slice((C), (V), (CTX))) \
1124 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001125}
1126
1127#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001128 int _i; \
1129 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1130 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1131 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1132 if (!compiler_visit_ ## TYPE((C), elt)) \
1133 return 0; \
1134 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135}
1136
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001137#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001138 int _i; \
1139 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1140 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1141 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1142 if (!compiler_visit_ ## TYPE((C), elt)) { \
1143 compiler_exit_scope(c); \
1144 return 0; \
1145 } \
1146 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001147}
1148
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001149static int
1150compiler_isdocstring(stmt_ty s)
1151{
1152 if (s->kind != Expr_kind)
Antoine Pitrou252e8e22010-06-22 21:53:47 +00001153 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 return s->v.Expr.value->kind == Str_kind;
1155}
1156
1157/* Compile a sequence of statements, checking for a docstring. */
1158
1159static int
1160compiler_body(struct compiler *c, asdl_seq *stmts)
1161{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001162 int i = 0;
1163 stmt_ty st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001165 if (!asdl_seq_LEN(stmts))
1166 return 1;
1167 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1168 if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) {
1169 /* don't generate docstrings if -OO */
1170 i = 1;
1171 VISIT(c, expr, st->v.Expr.value);
1172 if (!compiler_nameop(c, __doc__, Store))
1173 return 0;
1174 }
1175 for (; i < asdl_seq_LEN(stmts); i++)
1176 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
1177 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178}
1179
1180static PyCodeObject *
1181compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001182{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001183 PyCodeObject *co;
1184 int addNone = 1;
1185 static PyObject *module;
1186 if (!module) {
1187 module = PyUnicode_InternFromString("<module>");
1188 if (!module)
1189 return NULL;
1190 }
1191 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1192 if (!compiler_enter_scope(c, module, mod, 0))
1193 return NULL;
1194 switch (mod->kind) {
1195 case Module_kind:
1196 if (!compiler_body(c, mod->v.Module.body)) {
1197 compiler_exit_scope(c);
1198 return 0;
1199 }
1200 break;
1201 case Interactive_kind:
1202 c->c_interactive = 1;
1203 VISIT_SEQ_IN_SCOPE(c, stmt,
1204 mod->v.Interactive.body);
1205 break;
1206 case Expression_kind:
1207 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1208 addNone = 0;
1209 break;
1210 case Suite_kind:
1211 PyErr_SetString(PyExc_SystemError,
1212 "suite should not be possible");
1213 return 0;
1214 default:
1215 PyErr_Format(PyExc_SystemError,
1216 "module kind %d should not be possible",
1217 mod->kind);
1218 return 0;
1219 }
1220 co = assemble(c, addNone);
1221 compiler_exit_scope(c);
1222 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001223}
1224
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225/* The test for LOCAL must come before the test for FREE in order to
1226 handle classes where name is both local and free. The local var is
1227 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001228*/
1229
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230static int
1231get_ref_type(struct compiler *c, PyObject *name)
1232{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001233 int scope = PyST_GetScope(c->u->u_ste, name);
1234 if (scope == 0) {
1235 char buf[350];
1236 PyOS_snprintf(buf, sizeof(buf),
1237 "unknown scope for %.100s in %.100s(%s) in %s\n"
1238 "symbols: %s\nlocals: %s\nglobals: %s",
1239 PyBytes_AS_STRING(name),
1240 PyBytes_AS_STRING(c->u->u_name),
1241 PyObject_REPR(c->u->u_ste->ste_id),
1242 c->c_filename,
1243 PyObject_REPR(c->u->u_ste->ste_symbols),
1244 PyObject_REPR(c->u->u_varnames),
1245 PyObject_REPR(c->u->u_names)
1246 );
1247 Py_FatalError(buf);
1248 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001249
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001250 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251}
1252
1253static int
1254compiler_lookup_arg(PyObject *dict, PyObject *name)
1255{
1256 PyObject *k, *v;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001257 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001258 if (k == NULL)
Antoine Pitrou252e8e22010-06-22 21:53:47 +00001259 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001261 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262 if (v == NULL)
Antoine Pitrou252e8e22010-06-22 21:53:47 +00001263 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001264 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265}
1266
1267static int
1268compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1269{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001270 int i, free = PyCode_GetNumFree(co);
1271 if (free == 0) {
1272 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1273 ADDOP_I(c, MAKE_FUNCTION, args);
1274 return 1;
1275 }
1276 for (i = 0; i < free; ++i) {
1277 /* Bypass com_addop_varname because it will generate
1278 LOAD_DEREF but LOAD_CLOSURE is needed.
1279 */
1280 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1281 int arg, reftype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001282
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001283 /* Special case: If a class contains a method with a
1284 free variable that has the same name as a method,
1285 the name will be considered free *and* local in the
1286 class. It should be handled by the closure, as
1287 well as by the normal name loookup logic.
1288 */
1289 reftype = get_ref_type(c, name);
1290 if (reftype == CELL)
1291 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1292 else /* (reftype == FREE) */
1293 arg = compiler_lookup_arg(c->u->u_freevars, name);
1294 if (arg == -1) {
1295 fprintf(stderr,
1296 "lookup %s in %s %d %d\n"
1297 "freevars of %s: %s\n",
1298 PyObject_REPR(name),
1299 PyBytes_AS_STRING(c->u->u_name),
1300 reftype, arg,
1301 _PyUnicode_AsString(co->co_name),
1302 PyObject_REPR(co->co_freevars));
1303 Py_FatalError("compiler_make_closure()");
1304 }
1305 ADDOP_I(c, LOAD_CLOSURE, arg);
1306 }
1307 ADDOP_I(c, BUILD_TUPLE, free);
1308 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1309 ADDOP_I(c, MAKE_CLOSURE, args);
1310 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001311}
1312
1313static int
1314compiler_decorators(struct compiler *c, asdl_seq* decos)
1315{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001316 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001318 if (!decos)
1319 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001321 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1322 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1323 }
1324 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325}
1326
1327static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001328compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001329 asdl_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001330{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001331 int i, default_count = 0;
1332 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1333 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1334 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1335 if (default_) {
1336 ADDOP_O(c, LOAD_CONST, arg->arg, consts);
1337 if (!compiler_visit_expr(c, default_)) {
1338 return -1;
1339 }
1340 default_count++;
1341 }
1342 }
1343 return default_count;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001344}
1345
1346static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001347compiler_visit_argannotation(struct compiler *c, identifier id,
1348 expr_ty annotation, PyObject *names)
1349{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001350 if (annotation) {
1351 VISIT(c, expr, annotation);
1352 if (PyList_Append(names, id))
1353 return -1;
1354 }
1355 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001356}
1357
1358static int
1359compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1360 PyObject *names)
1361{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001362 int i, error;
1363 for (i = 0; i < asdl_seq_LEN(args); i++) {
1364 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1365 error = compiler_visit_argannotation(
1366 c,
1367 arg->arg,
1368 arg->annotation,
1369 names);
1370 if (error)
1371 return error;
1372 }
1373 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001374}
1375
1376static int
1377compiler_visit_annotations(struct compiler *c, arguments_ty args,
1378 expr_ty returns)
1379{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001380 /* Push arg annotations and a list of the argument names. Return the #
1381 of items pushed. The expressions are evaluated out-of-order wrt the
1382 source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00001383
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001384 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1385 */
1386 static identifier return_str;
1387 PyObject *names;
1388 int len;
1389 names = PyList_New(0);
1390 if (!names)
1391 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001392
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001393 if (compiler_visit_argannotations(c, args->args, names))
1394 goto error;
1395 if (args->varargannotation &&
1396 compiler_visit_argannotation(c, args->vararg,
1397 args->varargannotation, names))
1398 goto error;
1399 if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1400 goto error;
1401 if (args->kwargannotation &&
1402 compiler_visit_argannotation(c, args->kwarg,
1403 args->kwargannotation, names))
1404 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001405
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001406 if (!return_str) {
1407 return_str = PyUnicode_InternFromString("return");
1408 if (!return_str)
1409 goto error;
1410 }
1411 if (compiler_visit_argannotation(c, return_str, returns, names)) {
1412 goto error;
1413 }
1414
1415 len = PyList_GET_SIZE(names);
1416 if (len > 65534) {
1417 /* len must fit in 16 bits, and len is incremented below */
1418 PyErr_SetString(PyExc_SyntaxError,
1419 "too many annotations");
1420 goto error;
1421 }
1422 if (len) {
1423 /* convert names to a tuple and place on stack */
1424 PyObject *elt;
1425 int i;
1426 PyObject *s = PyTuple_New(len);
1427 if (!s)
1428 goto error;
1429 for (i = 0; i < len; i++) {
1430 elt = PyList_GET_ITEM(names, i);
1431 Py_INCREF(elt);
1432 PyTuple_SET_ITEM(s, i, elt);
1433 }
1434 ADDOP_O(c, LOAD_CONST, s, consts);
1435 Py_DECREF(s);
1436 len++; /* include the just-pushed tuple */
1437 }
1438 Py_DECREF(names);
1439 return len;
Neal Norwitzc1505362006-12-28 06:47:50 +00001440
1441error:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001442 Py_DECREF(names);
1443 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001444}
1445
1446static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447compiler_function(struct compiler *c, stmt_ty s)
1448{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001449 PyCodeObject *co;
1450 PyObject *first_const = Py_None;
1451 arguments_ty args = s->v.FunctionDef.args;
1452 expr_ty returns = s->v.FunctionDef.returns;
1453 asdl_seq* decos = s->v.FunctionDef.decorator_list;
1454 stmt_ty st;
1455 int i, n, docstring, kw_default_count = 0, arglength;
1456 int num_annotations;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001458 assert(s->kind == FunctionDef_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001460 if (!compiler_decorators(c, decos))
1461 return 0;
1462 if (args->kwonlyargs) {
1463 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1464 args->kw_defaults);
1465 if (res < 0)
1466 return 0;
1467 kw_default_count = res;
1468 }
1469 if (args->defaults)
1470 VISIT_SEQ(c, expr, args->defaults);
1471 num_annotations = compiler_visit_annotations(c, args, returns);
1472 if (num_annotations < 0)
1473 return 0;
1474 assert((num_annotations & 0xFFFF) == num_annotations);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001475
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001476 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1477 s->lineno))
1478 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001480 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
1481 docstring = compiler_isdocstring(st);
1482 if (docstring && Py_OptimizeFlag < 2)
1483 first_const = st->v.Expr.value->v.Str.s;
1484 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1485 compiler_exit_scope(c);
1486 return 0;
1487 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001489 c->u->u_argcount = asdl_seq_LEN(args->args);
1490 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1491 n = asdl_seq_LEN(s->v.FunctionDef.body);
1492 /* if there was a docstring, we need to skip the first statement */
1493 for (i = docstring; i < n; i++) {
1494 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1495 VISIT_IN_SCOPE(c, stmt, st);
1496 }
1497 co = assemble(c, 1);
1498 compiler_exit_scope(c);
1499 if (co == NULL)
1500 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001501
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001502 arglength = asdl_seq_LEN(args->defaults);
1503 arglength |= kw_default_count << 8;
1504 arglength |= num_annotations << 16;
1505 compiler_make_closure(c, co, arglength);
1506 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001508 /* decorators */
1509 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1510 ADDOP_I(c, CALL_FUNCTION, 1);
1511 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001512
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001513 return compiler_nameop(c, s->v.FunctionDef.name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514}
1515
1516static int
1517compiler_class(struct compiler *c, stmt_ty s)
1518{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001519 PyCodeObject *co;
1520 PyObject *str;
1521 int i;
1522 asdl_seq* decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001523
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001524 if (!compiler_decorators(c, decos))
1525 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001526
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001527 /* ultimately generate code for:
1528 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1529 where:
1530 <func> is a function/closure created from the class body;
1531 it has a single argument (__locals__) where the dict
1532 (or MutableSequence) representing the locals is passed
1533 <name> is the class name
1534 <bases> is the positional arguments and *varargs argument
1535 <keywords> is the keyword arguments and **kwds argument
1536 This borrows from compiler_call.
1537 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001538
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001539 /* 1. compile the class body into a code object */
1540 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, s->lineno))
1541 return 0;
1542 /* this block represents what we do in the new scope */
1543 {
1544 /* use the class name for name mangling */
1545 Py_INCREF(s->v.ClassDef.name);
1546 Py_XDECREF(c->u->u_private);
1547 c->u->u_private = s->v.ClassDef.name;
1548 /* force it to have one mandatory argument */
1549 c->u->u_argcount = 1;
1550 /* load the first argument (__locals__) ... */
1551 ADDOP_I(c, LOAD_FAST, 0);
1552 /* ... and store it into f_locals */
1553 ADDOP_IN_SCOPE(c, STORE_LOCALS);
1554 /* load (global) __name__ ... */
1555 str = PyUnicode_InternFromString("__name__");
1556 if (!str || !compiler_nameop(c, str, Load)) {
1557 Py_XDECREF(str);
1558 compiler_exit_scope(c);
1559 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001560 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001561 Py_DECREF(str);
1562 /* ... and store it as __module__ */
1563 str = PyUnicode_InternFromString("__module__");
1564 if (!str || !compiler_nameop(c, str, Store)) {
1565 Py_XDECREF(str);
1566 compiler_exit_scope(c);
1567 return 0;
1568 }
1569 Py_DECREF(str);
1570 /* compile the body proper */
1571 if (!compiler_body(c, s->v.ClassDef.body)) {
1572 compiler_exit_scope(c);
1573 return 0;
1574 }
1575 /* return the (empty) __class__ cell */
1576 str = PyUnicode_InternFromString("__class__");
1577 if (str == NULL) {
1578 compiler_exit_scope(c);
1579 return 0;
1580 }
1581 i = compiler_lookup_arg(c->u->u_cellvars, str);
1582 Py_DECREF(str);
1583 if (i == -1) {
1584 /* This happens when nobody references the cell */
1585 PyErr_Clear();
1586 /* Return None */
1587 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1588 }
1589 else {
1590 /* Return the cell where to store __class__ */
1591 ADDOP_I(c, LOAD_CLOSURE, i);
1592 }
1593 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1594 /* create the code object */
1595 co = assemble(c, 1);
1596 }
1597 /* leave the new scope */
1598 compiler_exit_scope(c);
1599 if (co == NULL)
1600 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001601
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001602 /* 2. load the 'build_class' function */
1603 ADDOP(c, LOAD_BUILD_CLASS);
1604
1605 /* 3. load a function (or closure) made from the code object */
1606 compiler_make_closure(c, co, 0);
1607 Py_DECREF(co);
1608
1609 /* 4. load class name */
1610 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1611
1612 /* 5. generate the rest of the code for the call */
1613 if (!compiler_call_helper(c, 2,
1614 s->v.ClassDef.bases,
1615 s->v.ClassDef.keywords,
1616 s->v.ClassDef.starargs,
1617 s->v.ClassDef.kwargs))
1618 return 0;
1619
1620 /* 6. apply decorators */
1621 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1622 ADDOP_I(c, CALL_FUNCTION, 1);
1623 }
1624
1625 /* 7. store into <name> */
1626 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1627 return 0;
1628 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001629}
1630
1631static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001632compiler_ifexp(struct compiler *c, expr_ty e)
1633{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001634 basicblock *end, *next;
1635
1636 assert(e->kind == IfExp_kind);
1637 end = compiler_new_block(c);
1638 if (end == NULL)
1639 return 0;
1640 next = compiler_new_block(c);
1641 if (next == NULL)
1642 return 0;
1643 VISIT(c, expr, e->v.IfExp.test);
1644 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1645 VISIT(c, expr, e->v.IfExp.body);
1646 ADDOP_JREL(c, JUMP_FORWARD, end);
1647 compiler_use_next_block(c, next);
1648 VISIT(c, expr, e->v.IfExp.orelse);
1649 compiler_use_next_block(c, end);
1650 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001651}
1652
1653static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001654compiler_lambda(struct compiler *c, expr_ty e)
1655{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001656 PyCodeObject *co;
1657 static identifier name;
1658 int kw_default_count = 0, arglength;
1659 arguments_ty args = e->v.Lambda.args;
1660 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001662 if (!name) {
1663 name = PyUnicode_InternFromString("<lambda>");
1664 if (!name)
1665 return 0;
1666 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001667
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001668 if (args->kwonlyargs) {
1669 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1670 args->kw_defaults);
1671 if (res < 0) return 0;
1672 kw_default_count = res;
1673 }
1674 if (args->defaults)
1675 VISIT_SEQ(c, expr, args->defaults);
1676 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1677 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001678
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001679 c->u->u_argcount = asdl_seq_LEN(args->args);
1680 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1681 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1682 if (c->u->u_ste->ste_generator) {
1683 ADDOP_IN_SCOPE(c, POP_TOP);
1684 }
1685 else {
1686 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1687 }
1688 co = assemble(c, 1);
1689 compiler_exit_scope(c);
1690 if (co == NULL)
1691 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001693 arglength = asdl_seq_LEN(args->defaults);
1694 arglength |= kw_default_count << 8;
1695 compiler_make_closure(c, co, arglength);
1696 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001698 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699}
1700
1701static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702compiler_if(struct compiler *c, stmt_ty s)
1703{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001704 basicblock *end, *next;
1705 int constant;
1706 assert(s->kind == If_kind);
1707 end = compiler_new_block(c);
1708 if (end == NULL)
1709 return 0;
1710
1711 constant = expr_constant(s->v.If.test);
1712 /* constant = 0: "if 0"
1713 * constant = 1: "if 1", "if 2", ...
1714 * constant = -1: rest */
1715 if (constant == 0) {
1716 if (s->v.If.orelse)
1717 VISIT_SEQ(c, stmt, s->v.If.orelse);
1718 } else if (constant == 1) {
1719 VISIT_SEQ(c, stmt, s->v.If.body);
1720 } else {
1721 if (s->v.If.orelse) {
1722 next = compiler_new_block(c);
1723 if (next == NULL)
1724 return 0;
1725 }
1726 else
1727 next = end;
1728 VISIT(c, expr, s->v.If.test);
1729 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1730 VISIT_SEQ(c, stmt, s->v.If.body);
1731 ADDOP_JREL(c, JUMP_FORWARD, end);
1732 if (s->v.If.orelse) {
1733 compiler_use_next_block(c, next);
1734 VISIT_SEQ(c, stmt, s->v.If.orelse);
1735 }
1736 }
1737 compiler_use_next_block(c, end);
1738 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001739}
1740
1741static int
1742compiler_for(struct compiler *c, stmt_ty s)
1743{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001744 basicblock *start, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001746 start = compiler_new_block(c);
1747 cleanup = compiler_new_block(c);
1748 end = compiler_new_block(c);
1749 if (start == NULL || end == NULL || cleanup == NULL)
1750 return 0;
1751 ADDOP_JREL(c, SETUP_LOOP, end);
1752 if (!compiler_push_fblock(c, LOOP, start))
1753 return 0;
1754 VISIT(c, expr, s->v.For.iter);
1755 ADDOP(c, GET_ITER);
1756 compiler_use_next_block(c, start);
1757 /* for expressions must be traced on each iteration,
1758 so we need to set an extra line number. */
1759 c->u->u_lineno_set = 0;
1760 ADDOP_JREL(c, FOR_ITER, cleanup);
1761 VISIT(c, expr, s->v.For.target);
1762 VISIT_SEQ(c, stmt, s->v.For.body);
1763 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1764 compiler_use_next_block(c, cleanup);
1765 ADDOP(c, POP_BLOCK);
1766 compiler_pop_fblock(c, LOOP, start);
1767 VISIT_SEQ(c, stmt, s->v.For.orelse);
1768 compiler_use_next_block(c, end);
1769 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001770}
1771
1772static int
1773compiler_while(struct compiler *c, stmt_ty s)
1774{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001775 basicblock *loop, *orelse, *end, *anchor = NULL;
1776 int constant = expr_constant(s->v.While.test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001777
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001778 if (constant == 0) {
1779 if (s->v.While.orelse)
1780 VISIT_SEQ(c, stmt, s->v.While.orelse);
1781 return 1;
1782 }
1783 loop = compiler_new_block(c);
1784 end = compiler_new_block(c);
1785 if (constant == -1) {
1786 anchor = compiler_new_block(c);
1787 if (anchor == NULL)
1788 return 0;
1789 }
1790 if (loop == NULL || end == NULL)
1791 return 0;
1792 if (s->v.While.orelse) {
1793 orelse = compiler_new_block(c);
1794 if (orelse == NULL)
1795 return 0;
1796 }
1797 else
1798 orelse = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001799
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001800 ADDOP_JREL(c, SETUP_LOOP, end);
1801 compiler_use_next_block(c, loop);
1802 if (!compiler_push_fblock(c, LOOP, loop))
1803 return 0;
1804 if (constant == -1) {
1805 /* while expressions must be traced on each iteration,
1806 so we need to set an extra line number. */
1807 c->u->u_lineno_set = 0;
1808 VISIT(c, expr, s->v.While.test);
1809 ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);
1810 }
1811 VISIT_SEQ(c, stmt, s->v.While.body);
1812 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001813
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001814 /* XXX should the two POP instructions be in a separate block
1815 if there is no else clause ?
1816 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001818 if (constant == -1) {
1819 compiler_use_next_block(c, anchor);
1820 ADDOP(c, POP_BLOCK);
1821 }
1822 compiler_pop_fblock(c, LOOP, loop);
1823 if (orelse != NULL) /* what if orelse is just pass? */
1824 VISIT_SEQ(c, stmt, s->v.While.orelse);
1825 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001826
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001827 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001828}
1829
1830static int
1831compiler_continue(struct compiler *c)
1832{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001833 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
1834 static const char IN_FINALLY_ERROR_MSG[] =
1835 "'continue' not supported inside 'finally' clause";
1836 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001837
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001838 if (!c->u->u_nfblocks)
1839 return compiler_error(c, LOOP_ERROR_MSG);
1840 i = c->u->u_nfblocks - 1;
1841 switch (c->u->u_fblock[i].fb_type) {
1842 case LOOP:
1843 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1844 break;
1845 case EXCEPT:
1846 case FINALLY_TRY:
1847 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1848 /* Prevent continue anywhere under a finally
1849 even if hidden in a sub-try or except. */
1850 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1851 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1852 }
1853 if (i == -1)
1854 return compiler_error(c, LOOP_ERROR_MSG);
1855 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1856 break;
1857 case FINALLY_END:
1858 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1859 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001861 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001862}
1863
1864/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001865
1866 SETUP_FINALLY L
1867 <code for body>
1868 POP_BLOCK
1869 LOAD_CONST <None>
1870 L: <code for finalbody>
1871 END_FINALLY
1872
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001873 The special instructions use the block stack. Each block
1874 stack entry contains the instruction that created it (here
1875 SETUP_FINALLY), the level of the value stack at the time the
1876 block stack entry was created, and a label (here L).
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001877
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001878 SETUP_FINALLY:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001879 Pushes the current value stack level and the label
1880 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881 POP_BLOCK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001882 Pops en entry from the block stack, and pops the value
1883 stack until its level is the same as indicated on the
1884 block stack. (The label is ignored.)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885 END_FINALLY:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001886 Pops a variable number of entries from the *value* stack
1887 and re-raises the exception they specify. The number of
1888 entries popped depends on the (pseudo) exception type.
1889
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001890 The block stack is unwound when an exception is raised:
1891 when a SETUP_FINALLY entry is found, the exception is pushed
1892 onto the value stack (and the exception condition is cleared),
1893 and the interpreter jumps to the label gotten from the block
1894 stack.
1895*/
1896
1897static int
1898compiler_try_finally(struct compiler *c, stmt_ty s)
1899{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001900 basicblock *body, *end;
1901 body = compiler_new_block(c);
1902 end = compiler_new_block(c);
1903 if (body == NULL || end == NULL)
1904 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001906 ADDOP_JREL(c, SETUP_FINALLY, end);
1907 compiler_use_next_block(c, body);
1908 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1909 return 0;
1910 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
1911 ADDOP(c, POP_BLOCK);
1912 compiler_pop_fblock(c, FINALLY_TRY, body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001914 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1915 compiler_use_next_block(c, end);
1916 if (!compiler_push_fblock(c, FINALLY_END, end))
1917 return 0;
1918 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
1919 ADDOP(c, END_FINALLY);
1920 compiler_pop_fblock(c, FINALLY_END, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001922 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001923}
1924
1925/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001926 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001927 (The contents of the value stack is shown in [], with the top
1928 at the right; 'tb' is trace-back info, 'val' the exception's
1929 associated value, and 'exc' the exception.)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001930
1931 Value stack Label Instruction Argument
1932 [] SETUP_EXCEPT L1
1933 [] <code for S>
1934 [] POP_BLOCK
1935 [] JUMP_FORWARD L0
1936
1937 [tb, val, exc] L1: DUP )
1938 [tb, val, exc, exc] <evaluate E1> )
1939 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1940 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
1941 [tb, val, exc] POP
1942 [tb, val] <assign to V1> (or POP if no V1)
1943 [tb] POP
1944 [] <code for S1>
1945 JUMP_FORWARD L0
1946
1947 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001948 .............................etc.......................
1949
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001950 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
1951
1952 [] L0: <next statement>
1953
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001954 Of course, parts are not generated if Vi or Ei is not present.
1955*/
1956static int
1957compiler_try_except(struct compiler *c, stmt_ty s)
1958{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001959 basicblock *body, *orelse, *except, *end;
1960 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001961
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001962 body = compiler_new_block(c);
1963 except = compiler_new_block(c);
1964 orelse = compiler_new_block(c);
1965 end = compiler_new_block(c);
1966 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1967 return 0;
1968 ADDOP_JREL(c, SETUP_EXCEPT, except);
1969 compiler_use_next_block(c, body);
1970 if (!compiler_push_fblock(c, EXCEPT, body))
1971 return 0;
1972 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
1973 ADDOP(c, POP_BLOCK);
1974 compiler_pop_fblock(c, EXCEPT, body);
1975 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1976 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1977 compiler_use_next_block(c, except);
1978 for (i = 0; i < n; i++) {
1979 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1980 s->v.TryExcept.handlers, i);
1981 if (!handler->v.ExceptHandler.type && i < n-1)
1982 return compiler_error(c, "default 'except:' must be last");
1983 c->u->u_lineno_set = 0;
1984 c->u->u_lineno = handler->lineno;
1985 except = compiler_new_block(c);
1986 if (except == NULL)
1987 return 0;
1988 if (handler->v.ExceptHandler.type) {
1989 ADDOP(c, DUP_TOP);
1990 VISIT(c, expr, handler->v.ExceptHandler.type);
1991 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1992 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
1993 }
1994 ADDOP(c, POP_TOP);
1995 if (handler->v.ExceptHandler.name) {
1996 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00001997
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001998 cleanup_end = compiler_new_block(c);
1999 cleanup_body = compiler_new_block(c);
2000 if(!(cleanup_end || cleanup_body))
2001 return 0;
Guido van Rossumb940e112007-01-10 16:19:56 +00002002
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002003 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
2004 ADDOP(c, POP_TOP);
2005
2006 /*
2007 try:
2008 # body
2009 except type as name:
2010 try:
2011 # body
2012 finally:
2013 name = None
2014 del name
2015 */
2016
2017 /* second try: */
2018 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2019 compiler_use_next_block(c, cleanup_body);
2020 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2021 return 0;
2022
2023 /* second # body */
2024 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2025 ADDOP(c, POP_BLOCK);
2026 ADDOP(c, POP_EXCEPT);
2027 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
2028
2029 /* finally: */
2030 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2031 compiler_use_next_block(c, cleanup_end);
2032 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
2033 return 0;
2034
2035 /* name = None */
2036 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2037 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
2038
2039 /* del name */
2040 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
2041
2042 ADDOP(c, END_FINALLY);
2043 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
2044 }
2045 else {
2046 basicblock *cleanup_body;
2047
2048 cleanup_body = compiler_new_block(c);
2049 if(!cleanup_body)
2050 return 0;
2051
Guido van Rossumb940e112007-01-10 16:19:56 +00002052 ADDOP(c, POP_TOP);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002053 ADDOP(c, POP_TOP);
2054 compiler_use_next_block(c, cleanup_body);
2055 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2056 return 0;
2057 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2058 ADDOP(c, POP_EXCEPT);
2059 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
2060 }
2061 ADDOP_JREL(c, JUMP_FORWARD, end);
2062 compiler_use_next_block(c, except);
2063 }
2064 ADDOP(c, END_FINALLY);
2065 compiler_use_next_block(c, orelse);
2066 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2067 compiler_use_next_block(c, end);
2068 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069}
2070
2071static int
2072compiler_import_as(struct compiler *c, identifier name, identifier asname)
2073{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002074 /* The IMPORT_NAME opcode was already generated. This function
2075 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002076
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002077 If there is a dot in name, we need to split it and emit a
2078 LOAD_ATTR for each name.
2079 */
2080 const Py_UNICODE *src = PyUnicode_AS_UNICODE(name);
2081 const Py_UNICODE *dot = Py_UNICODE_strchr(src, '.');
2082 if (dot) {
2083 /* Consume the base module name to get the first attribute */
2084 src = dot + 1;
2085 while (dot) {
2086 /* NB src is only defined when dot != NULL */
2087 PyObject *attr;
2088 dot = Py_UNICODE_strchr(src, '.');
2089 attr = PyUnicode_FromUnicode(src,
2090 dot ? dot - src : Py_UNICODE_strlen(src));
2091 if (!attr)
2092 return -1;
2093 ADDOP_O(c, LOAD_ATTR, attr, names);
2094 Py_DECREF(attr);
2095 src = dot + 1;
2096 }
2097 }
2098 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002099}
2100
2101static int
2102compiler_import(struct compiler *c, stmt_ty s)
2103{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002104 /* The Import node stores a module name like a.b.c as a single
2105 string. This is convenient for all cases except
2106 import a.b.c as d
2107 where we need to parse that string to extract the individual
2108 module names.
2109 XXX Perhaps change the representation to make this case simpler?
2110 */
2111 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002112
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002113 for (i = 0; i < n; i++) {
2114 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
2115 int r;
2116 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002117
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002118 level = PyLong_FromLong(0);
2119 if (level == NULL)
2120 return 0;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002121
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002122 ADDOP_O(c, LOAD_CONST, level, consts);
2123 Py_DECREF(level);
2124 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2125 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002126
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002127 if (alias->asname) {
2128 r = compiler_import_as(c, alias->name, alias->asname);
2129 if (!r)
2130 return r;
2131 }
2132 else {
2133 identifier tmp = alias->name;
2134 const Py_UNICODE *base = PyUnicode_AS_UNICODE(alias->name);
2135 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
2136 if (dot)
2137 tmp = PyUnicode_FromUnicode(base,
2138 dot - base);
2139 r = compiler_nameop(c, tmp, Store);
2140 if (dot) {
2141 Py_DECREF(tmp);
2142 }
2143 if (!r)
2144 return r;
2145 }
2146 }
2147 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002148}
2149
2150static int
2151compiler_from_import(struct compiler *c, stmt_ty s)
2152{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002153 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002154
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002155 PyObject *names = PyTuple_New(n);
2156 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002157
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002158 if (!names)
2159 return 0;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002160
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002161 level = PyLong_FromLong(s->v.ImportFrom.level);
2162 if (!level) {
2163 Py_DECREF(names);
2164 return 0;
2165 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002166
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002167 /* build up the names */
2168 for (i = 0; i < n; i++) {
2169 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2170 Py_INCREF(alias->name);
2171 PyTuple_SET_ITEM(names, i, alias->name);
2172 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002174 if (s->lineno > c->c_future->ff_lineno) {
2175 if (!PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module,
2176 "__future__")) {
2177 Py_DECREF(level);
2178 Py_DECREF(names);
2179 return compiler_error(c,
2180 "from __future__ imports must occur "
2181 "at the beginning of the file");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002183 }
2184 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002185
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002186 ADDOP_O(c, LOAD_CONST, level, consts);
2187 Py_DECREF(level);
2188 ADDOP_O(c, LOAD_CONST, names, consts);
2189 Py_DECREF(names);
2190 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2191 for (i = 0; i < n; i++) {
2192 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2193 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002194
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002195 if (i == 0 && *PyUnicode_AS_UNICODE(alias->name) == '*') {
2196 assert(n == 1);
2197 ADDOP(c, IMPORT_STAR);
2198 return 1;
2199 }
2200
2201 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2202 store_name = alias->name;
2203 if (alias->asname)
2204 store_name = alias->asname;
2205
2206 if (!compiler_nameop(c, store_name, Store)) {
2207 Py_DECREF(names);
2208 return 0;
2209 }
2210 }
2211 /* remove imported module */
2212 ADDOP(c, POP_TOP);
2213 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002214}
2215
2216static int
2217compiler_assert(struct compiler *c, stmt_ty s)
2218{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002219 static PyObject *assertion_error = NULL;
2220 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002221
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002222 if (Py_OptimizeFlag)
2223 return 1;
2224 if (assertion_error == NULL) {
2225 assertion_error = PyUnicode_InternFromString("AssertionError");
2226 if (assertion_error == NULL)
2227 return 0;
2228 }
2229 if (s->v.Assert.test->kind == Tuple_kind &&
2230 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2231 const char* msg =
2232 "assertion is always true, perhaps remove parentheses?";
2233 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,
2234 c->u->u_lineno, NULL, NULL) == -1)
2235 return 0;
2236 }
2237 VISIT(c, expr, s->v.Assert.test);
2238 end = compiler_new_block(c);
2239 if (end == NULL)
2240 return 0;
2241 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
2242 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2243 if (s->v.Assert.msg) {
2244 VISIT(c, expr, s->v.Assert.msg);
2245 ADDOP_I(c, CALL_FUNCTION, 1);
2246 }
2247 ADDOP_I(c, RAISE_VARARGS, 1);
2248 compiler_use_next_block(c, end);
2249 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002250}
2251
2252static int
2253compiler_visit_stmt(struct compiler *c, stmt_ty s)
2254{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002255 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002256
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002257 /* Always assign a lineno to the next instruction for a stmt. */
2258 c->u->u_lineno = s->lineno;
2259 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002260
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002261 switch (s->kind) {
2262 case FunctionDef_kind:
2263 return compiler_function(c, s);
2264 case ClassDef_kind:
2265 return compiler_class(c, s);
2266 case Return_kind:
2267 if (c->u->u_ste->ste_type != FunctionBlock)
2268 return compiler_error(c, "'return' outside function");
2269 if (s->v.Return.value) {
2270 VISIT(c, expr, s->v.Return.value);
2271 }
2272 else
2273 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2274 ADDOP(c, RETURN_VALUE);
2275 break;
2276 case Delete_kind:
2277 VISIT_SEQ(c, expr, s->v.Delete.targets)
2278 break;
2279 case Assign_kind:
2280 n = asdl_seq_LEN(s->v.Assign.targets);
2281 VISIT(c, expr, s->v.Assign.value);
2282 for (i = 0; i < n; i++) {
2283 if (i < n - 1)
2284 ADDOP(c, DUP_TOP);
2285 VISIT(c, expr,
2286 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2287 }
2288 break;
2289 case AugAssign_kind:
2290 return compiler_augassign(c, s);
2291 case For_kind:
2292 return compiler_for(c, s);
2293 case While_kind:
2294 return compiler_while(c, s);
2295 case If_kind:
2296 return compiler_if(c, s);
2297 case Raise_kind:
2298 n = 0;
2299 if (s->v.Raise.exc) {
2300 VISIT(c, expr, s->v.Raise.exc);
2301 n++;
2302 if (s->v.Raise.cause) {
2303 VISIT(c, expr, s->v.Raise.cause);
2304 n++;
2305 }
2306 }
2307 ADDOP_I(c, RAISE_VARARGS, n);
2308 break;
2309 case TryExcept_kind:
2310 return compiler_try_except(c, s);
2311 case TryFinally_kind:
2312 return compiler_try_finally(c, s);
2313 case Assert_kind:
2314 return compiler_assert(c, s);
2315 case Import_kind:
2316 return compiler_import(c, s);
2317 case ImportFrom_kind:
2318 return compiler_from_import(c, s);
2319 case Global_kind:
2320 case Nonlocal_kind:
2321 break;
2322 case Expr_kind:
2323 if (c->c_interactive && c->c_nestlevel <= 1) {
2324 VISIT(c, expr, s->v.Expr.value);
2325 ADDOP(c, PRINT_EXPR);
2326 }
2327 else if (s->v.Expr.value->kind != Str_kind &&
2328 s->v.Expr.value->kind != Num_kind) {
2329 VISIT(c, expr, s->v.Expr.value);
2330 ADDOP(c, POP_TOP);
2331 }
2332 break;
2333 case Pass_kind:
2334 break;
2335 case Break_kind:
2336 if (!compiler_in_loop(c))
2337 return compiler_error(c, "'break' outside loop");
2338 ADDOP(c, BREAK_LOOP);
2339 break;
2340 case Continue_kind:
2341 return compiler_continue(c);
2342 case With_kind:
2343 return compiler_with(c, s);
2344 }
2345 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002346}
2347
2348static int
2349unaryop(unaryop_ty op)
2350{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002351 switch (op) {
2352 case Invert:
2353 return UNARY_INVERT;
2354 case Not:
2355 return UNARY_NOT;
2356 case UAdd:
2357 return UNARY_POSITIVE;
2358 case USub:
2359 return UNARY_NEGATIVE;
2360 default:
2361 PyErr_Format(PyExc_SystemError,
2362 "unary op %d should not be possible", op);
2363 return 0;
2364 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002365}
2366
2367static int
2368binop(struct compiler *c, operator_ty op)
2369{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002370 switch (op) {
2371 case Add:
2372 return BINARY_ADD;
2373 case Sub:
2374 return BINARY_SUBTRACT;
2375 case Mult:
2376 return BINARY_MULTIPLY;
2377 case Div:
2378 return BINARY_TRUE_DIVIDE;
2379 case Mod:
2380 return BINARY_MODULO;
2381 case Pow:
2382 return BINARY_POWER;
2383 case LShift:
2384 return BINARY_LSHIFT;
2385 case RShift:
2386 return BINARY_RSHIFT;
2387 case BitOr:
2388 return BINARY_OR;
2389 case BitXor:
2390 return BINARY_XOR;
2391 case BitAnd:
2392 return BINARY_AND;
2393 case FloorDiv:
2394 return BINARY_FLOOR_DIVIDE;
2395 default:
2396 PyErr_Format(PyExc_SystemError,
2397 "binary op %d should not be possible", op);
2398 return 0;
2399 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002400}
2401
2402static int
2403cmpop(cmpop_ty op)
2404{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002405 switch (op) {
2406 case Eq:
2407 return PyCmp_EQ;
2408 case NotEq:
2409 return PyCmp_NE;
2410 case Lt:
2411 return PyCmp_LT;
2412 case LtE:
2413 return PyCmp_LE;
2414 case Gt:
2415 return PyCmp_GT;
2416 case GtE:
2417 return PyCmp_GE;
2418 case Is:
2419 return PyCmp_IS;
2420 case IsNot:
2421 return PyCmp_IS_NOT;
2422 case In:
2423 return PyCmp_IN;
2424 case NotIn:
2425 return PyCmp_NOT_IN;
2426 default:
2427 return PyCmp_BAD;
2428 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002429}
2430
2431static int
2432inplace_binop(struct compiler *c, operator_ty op)
2433{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002434 switch (op) {
2435 case Add:
2436 return INPLACE_ADD;
2437 case Sub:
2438 return INPLACE_SUBTRACT;
2439 case Mult:
2440 return INPLACE_MULTIPLY;
2441 case Div:
2442 return INPLACE_TRUE_DIVIDE;
2443 case Mod:
2444 return INPLACE_MODULO;
2445 case Pow:
2446 return INPLACE_POWER;
2447 case LShift:
2448 return INPLACE_LSHIFT;
2449 case RShift:
2450 return INPLACE_RSHIFT;
2451 case BitOr:
2452 return INPLACE_OR;
2453 case BitXor:
2454 return INPLACE_XOR;
2455 case BitAnd:
2456 return INPLACE_AND;
2457 case FloorDiv:
2458 return INPLACE_FLOOR_DIVIDE;
2459 default:
2460 PyErr_Format(PyExc_SystemError,
2461 "inplace binary op %d should not be possible", op);
2462 return 0;
2463 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002464}
2465
2466static int
2467compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2468{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002469 int op, scope, arg;
2470 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002471
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002472 PyObject *dict = c->u->u_names;
2473 PyObject *mangled;
2474 /* XXX AugStore isn't used anywhere! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002475
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002476 mangled = _Py_Mangle(c->u->u_private, name);
2477 if (!mangled)
2478 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002479
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002480 op = 0;
2481 optype = OP_NAME;
2482 scope = PyST_GetScope(c->u->u_ste, mangled);
2483 switch (scope) {
2484 case FREE:
2485 dict = c->u->u_freevars;
2486 optype = OP_DEREF;
2487 break;
2488 case CELL:
2489 dict = c->u->u_cellvars;
2490 optype = OP_DEREF;
2491 break;
2492 case LOCAL:
2493 if (c->u->u_ste->ste_type == FunctionBlock)
2494 optype = OP_FAST;
2495 break;
2496 case GLOBAL_IMPLICIT:
2497 if (c->u->u_ste->ste_type == FunctionBlock &&
2498 !c->u->u_ste->ste_unoptimized)
2499 optype = OP_GLOBAL;
2500 break;
2501 case GLOBAL_EXPLICIT:
2502 optype = OP_GLOBAL;
2503 break;
2504 default:
2505 /* scope can be 0 */
2506 break;
2507 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002508
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002509 /* XXX Leave assert here, but handle __doc__ and the like better */
2510 assert(scope || PyUnicode_AS_UNICODE(name)[0] == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002511
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002512 switch (optype) {
2513 case OP_DEREF:
2514 switch (ctx) {
2515 case Load: op = LOAD_DEREF; break;
2516 case Store: op = STORE_DEREF; break;
2517 case AugLoad:
2518 case AugStore:
2519 break;
2520 case Del:
2521 PyErr_Format(PyExc_SyntaxError,
2522 "can not delete variable '%S' referenced "
2523 "in nested scope",
2524 name);
2525 Py_DECREF(mangled);
2526 return 0;
2527 case Param:
2528 default:
2529 PyErr_SetString(PyExc_SystemError,
2530 "param invalid for deref variable");
2531 return 0;
2532 }
2533 break;
2534 case OP_FAST:
2535 switch (ctx) {
2536 case Load: op = LOAD_FAST; break;
2537 case Store: op = STORE_FAST; break;
2538 case Del: op = DELETE_FAST; break;
2539 case AugLoad:
2540 case AugStore:
2541 break;
2542 case Param:
2543 default:
2544 PyErr_SetString(PyExc_SystemError,
2545 "param invalid for local variable");
2546 return 0;
2547 }
2548 ADDOP_O(c, op, mangled, varnames);
2549 Py_DECREF(mangled);
2550 return 1;
2551 case OP_GLOBAL:
2552 switch (ctx) {
2553 case Load: op = LOAD_GLOBAL; break;
2554 case Store: op = STORE_GLOBAL; break;
2555 case Del: op = DELETE_GLOBAL; break;
2556 case AugLoad:
2557 case AugStore:
2558 break;
2559 case Param:
2560 default:
2561 PyErr_SetString(PyExc_SystemError,
2562 "param invalid for global variable");
2563 return 0;
2564 }
2565 break;
2566 case OP_NAME:
2567 switch (ctx) {
2568 case Load: op = LOAD_NAME; break;
2569 case Store: op = STORE_NAME; break;
2570 case Del: op = DELETE_NAME; break;
2571 case AugLoad:
2572 case AugStore:
2573 break;
2574 case Param:
2575 default:
2576 PyErr_SetString(PyExc_SystemError,
2577 "param invalid for name variable");
2578 return 0;
2579 }
2580 break;
2581 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002582
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002583 assert(op);
2584 arg = compiler_add_o(c, dict, mangled);
2585 Py_DECREF(mangled);
2586 if (arg < 0)
2587 return 0;
2588 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589}
2590
2591static int
2592compiler_boolop(struct compiler *c, expr_ty e)
2593{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002594 basicblock *end;
2595 int jumpi, i, n;
2596 asdl_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002597
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002598 assert(e->kind == BoolOp_kind);
2599 if (e->v.BoolOp.op == And)
2600 jumpi = JUMP_IF_FALSE_OR_POP;
2601 else
2602 jumpi = JUMP_IF_TRUE_OR_POP;
2603 end = compiler_new_block(c);
2604 if (end == NULL)
2605 return 0;
2606 s = e->v.BoolOp.values;
2607 n = asdl_seq_LEN(s) - 1;
2608 assert(n >= 0);
2609 for (i = 0; i < n; ++i) {
2610 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
2611 ADDOP_JABS(c, jumpi, end);
2612 }
2613 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
2614 compiler_use_next_block(c, end);
2615 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002616}
2617
2618static int
2619compiler_list(struct compiler *c, expr_ty e)
2620{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002621 int n = asdl_seq_LEN(e->v.List.elts);
2622 if (e->v.List.ctx == Store) {
2623 int i, seen_star = 0;
2624 for (i = 0; i < n; i++) {
2625 expr_ty elt = asdl_seq_GET(e->v.List.elts, i);
2626 if (elt->kind == Starred_kind && !seen_star) {
2627 if ((i >= (1 << 8)) ||
2628 (n-i-1 >= (INT_MAX >> 8)))
2629 return compiler_error(c,
2630 "too many expressions in "
2631 "star-unpacking assignment");
2632 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2633 seen_star = 1;
2634 asdl_seq_SET(e->v.List.elts, i, elt->v.Starred.value);
2635 } else if (elt->kind == Starred_kind) {
2636 return compiler_error(c,
2637 "two starred expressions in assignment");
2638 }
2639 }
2640 if (!seen_star) {
2641 ADDOP_I(c, UNPACK_SEQUENCE, n);
2642 }
2643 }
2644 VISIT_SEQ(c, expr, e->v.List.elts);
2645 if (e->v.List.ctx == Load) {
2646 ADDOP_I(c, BUILD_LIST, n);
2647 }
2648 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002649}
2650
2651static int
2652compiler_tuple(struct compiler *c, expr_ty e)
2653{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002654 int n = asdl_seq_LEN(e->v.Tuple.elts);
2655 if (e->v.Tuple.ctx == Store) {
2656 int i, seen_star = 0;
2657 for (i = 0; i < n; i++) {
2658 expr_ty elt = asdl_seq_GET(e->v.Tuple.elts, i);
2659 if (elt->kind == Starred_kind && !seen_star) {
2660 if ((i >= (1 << 8)) ||
2661 (n-i-1 >= (INT_MAX >> 8)))
2662 return compiler_error(c,
2663 "too many expressions in "
2664 "star-unpacking assignment");
2665 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2666 seen_star = 1;
2667 asdl_seq_SET(e->v.Tuple.elts, i, elt->v.Starred.value);
2668 } else if (elt->kind == Starred_kind) {
2669 return compiler_error(c,
2670 "two starred expressions in assignment");
2671 }
2672 }
2673 if (!seen_star) {
2674 ADDOP_I(c, UNPACK_SEQUENCE, n);
2675 }
2676 }
2677 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2678 if (e->v.Tuple.ctx == Load) {
2679 ADDOP_I(c, BUILD_TUPLE, n);
2680 }
2681 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002682}
2683
2684static int
2685compiler_compare(struct compiler *c, expr_ty e)
2686{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002687 int i, n;
2688 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002689
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002690 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2691 VISIT(c, expr, e->v.Compare.left);
2692 n = asdl_seq_LEN(e->v.Compare.ops);
2693 assert(n > 0);
2694 if (n > 1) {
2695 cleanup = compiler_new_block(c);
2696 if (cleanup == NULL)
2697 return 0;
2698 VISIT(c, expr,
2699 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
2700 }
2701 for (i = 1; i < n; i++) {
2702 ADDOP(c, DUP_TOP);
2703 ADDOP(c, ROT_THREE);
2704 ADDOP_I(c, COMPARE_OP,
2705 cmpop((cmpop_ty)(asdl_seq_GET(
2706 e->v.Compare.ops, i - 1))));
2707 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
2708 NEXT_BLOCK(c);
2709 if (i < (n - 1))
2710 VISIT(c, expr,
2711 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2712 }
2713 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
2714 ADDOP_I(c, COMPARE_OP,
2715 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
2716 if (n > 1) {
2717 basicblock *end = compiler_new_block(c);
2718 if (end == NULL)
2719 return 0;
2720 ADDOP_JREL(c, JUMP_FORWARD, end);
2721 compiler_use_next_block(c, cleanup);
2722 ADDOP(c, ROT_TWO);
2723 ADDOP(c, POP_TOP);
2724 compiler_use_next_block(c, end);
2725 }
2726 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002727}
2728
2729static int
2730compiler_call(struct compiler *c, expr_ty e)
2731{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002732 VISIT(c, expr, e->v.Call.func);
2733 return compiler_call_helper(c, 0,
2734 e->v.Call.args,
2735 e->v.Call.keywords,
2736 e->v.Call.starargs,
2737 e->v.Call.kwargs);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002738}
2739
2740/* shared code between compiler_call and compiler_class */
2741static int
2742compiler_call_helper(struct compiler *c,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002743 int n, /* Args already pushed */
2744 asdl_seq *args,
2745 asdl_seq *keywords,
2746 expr_ty starargs,
2747 expr_ty kwargs)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002748{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002749 int code = 0;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002750
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002751 n += asdl_seq_LEN(args);
2752 VISIT_SEQ(c, expr, args);
2753 if (keywords) {
2754 VISIT_SEQ(c, keyword, keywords);
2755 n |= asdl_seq_LEN(keywords) << 8;
2756 }
2757 if (starargs) {
2758 VISIT(c, expr, starargs);
2759 code |= 1;
2760 }
2761 if (kwargs) {
2762 VISIT(c, expr, kwargs);
2763 code |= 2;
2764 }
2765 switch (code) {
2766 case 0:
2767 ADDOP_I(c, CALL_FUNCTION, n);
2768 break;
2769 case 1:
2770 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2771 break;
2772 case 2:
2773 ADDOP_I(c, CALL_FUNCTION_KW, n);
2774 break;
2775 case 3:
2776 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2777 break;
2778 }
2779 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002780}
2781
Nick Coghlan650f0d02007-04-15 12:05:43 +00002782
2783/* List and set comprehensions and generator expressions work by creating a
2784 nested function to perform the actual iteration. This means that the
2785 iteration variables don't leak into the current scope.
2786 The defined function is called immediately following its definition, with the
2787 result of that call being the result of the expression.
2788 The LC/SC version returns the populated container, while the GE version is
2789 flagged in symtable.c as a generator, so it returns the generator object
2790 when the function is called.
2791 This code *knows* that the loop cannot contain break, continue, or return,
2792 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2793
2794 Possible cleanups:
2795 - iterate over the generator sequence instead of using recursion
2796*/
2797
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002798static int
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002799compiler_comprehension_generator(struct compiler *c,
2800 asdl_seq *generators, int gen_index,
2801 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002802{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002803 /* generate code for the iterator, then each of the ifs,
2804 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002805
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002806 comprehension_ty gen;
2807 basicblock *start, *anchor, *skip, *if_cleanup;
2808 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002809
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002810 start = compiler_new_block(c);
2811 skip = compiler_new_block(c);
2812 if_cleanup = compiler_new_block(c);
2813 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002814
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002815 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2816 anchor == NULL)
2817 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002818
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002819 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002820
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002821 if (gen_index == 0) {
2822 /* Receive outermost iter as an implicit argument */
2823 c->u->u_argcount = 1;
2824 ADDOP_I(c, LOAD_FAST, 0);
2825 }
2826 else {
2827 /* Sub-iter - calculate on the fly */
2828 VISIT(c, expr, gen->iter);
2829 ADDOP(c, GET_ITER);
2830 }
2831 compiler_use_next_block(c, start);
2832 ADDOP_JREL(c, FOR_ITER, anchor);
2833 NEXT_BLOCK(c);
2834 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002835
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002836 /* XXX this needs to be cleaned up...a lot! */
2837 n = asdl_seq_LEN(gen->ifs);
2838 for (i = 0; i < n; i++) {
2839 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
2840 VISIT(c, expr, e);
2841 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
2842 NEXT_BLOCK(c);
2843 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002844
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002845 if (++gen_index < asdl_seq_LEN(generators))
2846 if (!compiler_comprehension_generator(c,
2847 generators, gen_index,
2848 elt, val, type))
2849 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002850
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002851 /* only append after the last for generator */
2852 if (gen_index >= asdl_seq_LEN(generators)) {
2853 /* comprehension specific code */
2854 switch (type) {
2855 case COMP_GENEXP:
2856 VISIT(c, expr, elt);
2857 ADDOP(c, YIELD_VALUE);
2858 ADDOP(c, POP_TOP);
2859 break;
2860 case COMP_LISTCOMP:
2861 VISIT(c, expr, elt);
2862 ADDOP_I(c, LIST_APPEND, gen_index + 1);
2863 break;
2864 case COMP_SETCOMP:
2865 VISIT(c, expr, elt);
2866 ADDOP_I(c, SET_ADD, gen_index + 1);
2867 break;
2868 case COMP_DICTCOMP:
2869 /* With 'd[k] = v', v is evaluated before k, so we do
2870 the same. */
2871 VISIT(c, expr, val);
2872 VISIT(c, expr, elt);
2873 ADDOP_I(c, MAP_ADD, gen_index + 1);
2874 break;
2875 default:
2876 return 0;
2877 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002878
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002879 compiler_use_next_block(c, skip);
2880 }
2881 compiler_use_next_block(c, if_cleanup);
2882 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2883 compiler_use_next_block(c, anchor);
2884
2885 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002886}
2887
2888static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002889compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002890 asdl_seq *generators, expr_ty elt, expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002891{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002892 PyCodeObject *co = NULL;
2893 expr_ty outermost_iter;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002894
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002895 outermost_iter = ((comprehension_ty)
2896 asdl_seq_GET(generators, 0))->iter;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002897
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002898 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2899 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002900
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002901 if (type != COMP_GENEXP) {
2902 int op;
2903 switch (type) {
2904 case COMP_LISTCOMP:
2905 op = BUILD_LIST;
2906 break;
2907 case COMP_SETCOMP:
2908 op = BUILD_SET;
2909 break;
2910 case COMP_DICTCOMP:
2911 op = BUILD_MAP;
2912 break;
2913 default:
2914 PyErr_Format(PyExc_SystemError,
2915 "unknown comprehension type %d", type);
2916 goto error_in_scope;
2917 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002918
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002919 ADDOP_I(c, op, 0);
2920 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002921
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002922 if (!compiler_comprehension_generator(c, generators, 0, elt,
2923 val, type))
2924 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002925
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002926 if (type != COMP_GENEXP) {
2927 ADDOP(c, RETURN_VALUE);
2928 }
2929
2930 co = assemble(c, 1);
2931 compiler_exit_scope(c);
2932 if (co == NULL)
2933 goto error;
2934
2935 if (!compiler_make_closure(c, co, 0))
2936 goto error;
2937 Py_DECREF(co);
2938
2939 VISIT(c, expr, outermost_iter);
2940 ADDOP(c, GET_ITER);
2941 ADDOP_I(c, CALL_FUNCTION, 1);
2942 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002943error_in_scope:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002944 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002945error:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002946 Py_XDECREF(co);
2947 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002948}
2949
2950static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002951compiler_genexp(struct compiler *c, expr_ty e)
2952{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002953 static identifier name;
2954 if (!name) {
2955 name = PyUnicode_FromString("<genexpr>");
2956 if (!name)
2957 return 0;
2958 }
2959 assert(e->kind == GeneratorExp_kind);
2960 return compiler_comprehension(c, e, COMP_GENEXP, name,
2961 e->v.GeneratorExp.generators,
2962 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002963}
2964
2965static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002966compiler_listcomp(struct compiler *c, expr_ty e)
2967{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002968 static identifier name;
2969 if (!name) {
2970 name = PyUnicode_FromString("<listcomp>");
2971 if (!name)
2972 return 0;
2973 }
2974 assert(e->kind == ListComp_kind);
2975 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
2976 e->v.ListComp.generators,
2977 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002978}
2979
2980static int
2981compiler_setcomp(struct compiler *c, expr_ty e)
2982{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002983 static identifier name;
2984 if (!name) {
2985 name = PyUnicode_FromString("<setcomp>");
2986 if (!name)
2987 return 0;
2988 }
2989 assert(e->kind == SetComp_kind);
2990 return compiler_comprehension(c, e, COMP_SETCOMP, name,
2991 e->v.SetComp.generators,
2992 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002993}
2994
2995
2996static int
2997compiler_dictcomp(struct compiler *c, expr_ty e)
2998{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002999 static identifier name;
3000 if (!name) {
3001 name = PyUnicode_FromString("<dictcomp>");
3002 if (!name)
3003 return 0;
3004 }
3005 assert(e->kind == DictComp_kind);
3006 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
3007 e->v.DictComp.generators,
3008 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003009}
3010
3011
3012static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003013compiler_visit_keyword(struct compiler *c, keyword_ty k)
3014{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003015 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3016 VISIT(c, expr, k->value);
3017 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003018}
3019
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003020/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003021 whether they are true or false.
3022
3023 Return values: 1 for true, 0 for false, -1 for non-constant.
3024 */
3025
3026static int
3027expr_constant(expr_ty e)
3028{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003029 char *id;
3030 switch (e->kind) {
3031 case Ellipsis_kind:
3032 return 1;
3033 case Num_kind:
3034 return PyObject_IsTrue(e->v.Num.n);
3035 case Str_kind:
3036 return PyObject_IsTrue(e->v.Str.s);
3037 case Name_kind:
3038 /* optimize away names that can't be reassigned */
3039 id = PyBytes_AS_STRING(
3040 _PyUnicode_AsDefaultEncodedString(e->v.Name.id, NULL));
3041 if (strcmp(id, "True") == 0) return 1;
3042 if (strcmp(id, "False") == 0) return 0;
3043 if (strcmp(id, "None") == 0) return 0;
3044 if (strcmp(id, "__debug__") == 0)
3045 return ! Py_OptimizeFlag;
3046 /* fall through */
3047 default:
3048 return -1;
3049 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003050}
3051
Guido van Rossumc2e20742006-02-27 22:32:47 +00003052/*
3053 Implements the with statement from PEP 343.
3054
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003055 The semantics outlined in that PEP are as follows:
Guido van Rossumc2e20742006-02-27 22:32:47 +00003056
3057 with EXPR as VAR:
3058 BLOCK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003059
Guido van Rossumc2e20742006-02-27 22:32:47 +00003060 It is implemented roughly as:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003061
Thomas Wouters477c8d52006-05-27 19:21:47 +00003062 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00003063 exit = context.__exit__ # not calling it
3064 value = context.__enter__()
3065 try:
3066 VAR = value # if VAR present in the syntax
3067 BLOCK
3068 finally:
3069 if an exception was raised:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003070 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003071 else:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003072 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003073 exit(*exc)
3074 */
3075static int
3076compiler_with(struct compiler *c, stmt_ty s)
3077{
Thomas Wouters477c8d52006-05-27 19:21:47 +00003078 static identifier enter_attr, exit_attr;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003079 basicblock *block, *finally;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00003080 identifier tmpvalue = NULL, tmpexit = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003081
3082 assert(s->kind == With_kind);
3083
Guido van Rossumc2e20742006-02-27 22:32:47 +00003084 if (!enter_attr) {
Antoine Pitrou252e8e22010-06-22 21:53:47 +00003085 enter_attr = PyUnicode_InternFromString("__enter__");
3086 if (!enter_attr)
3087 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003088 }
3089 if (!exit_attr) {
Antoine Pitrou252e8e22010-06-22 21:53:47 +00003090 exit_attr = PyUnicode_InternFromString("__exit__");
3091 if (!exit_attr)
3092 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003093 }
3094
3095 block = compiler_new_block(c);
3096 finally = compiler_new_block(c);
3097 if (!block || !finally)
Antoine Pitrou252e8e22010-06-22 21:53:47 +00003098 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003099
Guido van Rossumc2e20742006-02-27 22:32:47 +00003100 if (s->v.With.optional_vars) {
Antoine Pitrou252e8e22010-06-22 21:53:47 +00003101 /* Create a temporary variable to hold context.__enter__().
3102 We need to do this rather than preserving it on the stack
3103 because SETUP_FINALLY remembers the stack level.
3104 We need to do the assignment *inside* the try/finally
3105 so that context.__exit__() is called when the assignment
3106 fails. But we need to call context.__enter__() *before*
3107 the try/finally so that if it fails we won't call
3108 context.__exit__().
3109 */
3110 tmpvalue = compiler_new_tmpname(c);
3111 if (tmpvalue == NULL)
3112 return 0;
3113 PyArena_AddPyObject(c->c_arena, tmpvalue);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003114 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003115 tmpexit = compiler_new_tmpname(c);
3116 if (tmpexit == NULL)
3117 return 0;
3118 PyArena_AddPyObject(c->c_arena, tmpexit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003119
Thomas Wouters477c8d52006-05-27 19:21:47 +00003120 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003121 VISIT(c, expr, s->v.With.context_expr);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003122
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003123 /* Squirrel away context.__exit__ by stuffing it under context */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003124 ADDOP(c, DUP_TOP);
3125 ADDOP_O(c, LOAD_ATTR, exit_attr, names);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003126 if (!compiler_nameop(c, tmpexit, Store))
3127 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003128
3129 /* Call context.__enter__() */
3130 ADDOP_O(c, LOAD_ATTR, enter_attr, names);
3131 ADDOP_I(c, CALL_FUNCTION, 0);
3132
3133 if (s->v.With.optional_vars) {
Antoine Pitrou252e8e22010-06-22 21:53:47 +00003134 /* Store it in tmpvalue */
3135 if (!compiler_nameop(c, tmpvalue, Store))
3136 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003137 }
3138 else {
Antoine Pitrou252e8e22010-06-22 21:53:47 +00003139 /* Discard result from context.__enter__() */
3140 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003141 }
3142
3143 /* Start the try block */
3144 ADDOP_JREL(c, SETUP_FINALLY, finally);
3145
3146 compiler_use_next_block(c, block);
3147 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Antoine Pitrou252e8e22010-06-22 21:53:47 +00003148 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003149 }
3150
3151 if (s->v.With.optional_vars) {
Antoine Pitrou252e8e22010-06-22 21:53:47 +00003152 /* Bind saved result of context.__enter__() to VAR */
3153 if (!compiler_nameop(c, tmpvalue, Load) ||
3154 !compiler_nameop(c, tmpvalue, Del))
3155 return 0;
3156 VISIT(c, expr, s->v.With.optional_vars);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003157 }
3158
3159 /* BLOCK code */
3160 VISIT_SEQ(c, stmt, s->v.With.body);
3161
3162 /* End of try block; start the finally block */
3163 ADDOP(c, POP_BLOCK);
3164 compiler_pop_fblock(c, FINALLY_TRY, block);
3165
3166 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3167 compiler_use_next_block(c, finally);
3168 if (!compiler_push_fblock(c, FINALLY_END, finally))
Antoine Pitrou252e8e22010-06-22 21:53:47 +00003169 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003170
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003171 /* Finally block starts; context.__exit__ is on the stack under
3172 the exception or return information. Just issue our magic
3173 opcode. */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003174 if (!compiler_nameop(c, tmpexit, Load) ||
3175 !compiler_nameop(c, tmpexit, Del))
3176 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003177 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003178
3179 /* Finally block ends. */
3180 ADDOP(c, END_FINALLY);
3181 compiler_pop_fblock(c, FINALLY_END, finally);
3182 return 1;
3183}
3184
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003185static int
3186compiler_visit_expr(struct compiler *c, expr_ty e)
3187{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003188 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003189
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003190 /* If expr e has a different line number than the last expr/stmt,
3191 set a new line number for the next instruction.
3192 */
3193 if (e->lineno > c->u->u_lineno) {
3194 c->u->u_lineno = e->lineno;
3195 c->u->u_lineno_set = 0;
3196 }
3197 switch (e->kind) {
3198 case BoolOp_kind:
3199 return compiler_boolop(c, e);
3200 case BinOp_kind:
3201 VISIT(c, expr, e->v.BinOp.left);
3202 VISIT(c, expr, e->v.BinOp.right);
3203 ADDOP(c, binop(c, e->v.BinOp.op));
3204 break;
3205 case UnaryOp_kind:
3206 VISIT(c, expr, e->v.UnaryOp.operand);
3207 ADDOP(c, unaryop(e->v.UnaryOp.op));
3208 break;
3209 case Lambda_kind:
3210 return compiler_lambda(c, e);
3211 case IfExp_kind:
3212 return compiler_ifexp(c, e);
3213 case Dict_kind:
3214 n = asdl_seq_LEN(e->v.Dict.values);
3215 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
3216 for (i = 0; i < n; i++) {
3217 VISIT(c, expr,
3218 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3219 VISIT(c, expr,
3220 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3221 ADDOP(c, STORE_MAP);
3222 }
3223 break;
3224 case Set_kind:
3225 n = asdl_seq_LEN(e->v.Set.elts);
3226 VISIT_SEQ(c, expr, e->v.Set.elts);
3227 ADDOP_I(c, BUILD_SET, n);
3228 break;
3229 case GeneratorExp_kind:
3230 return compiler_genexp(c, e);
3231 case ListComp_kind:
3232 return compiler_listcomp(c, e);
3233 case SetComp_kind:
3234 return compiler_setcomp(c, e);
3235 case DictComp_kind:
3236 return compiler_dictcomp(c, e);
3237 case Yield_kind:
3238 if (c->u->u_ste->ste_type != FunctionBlock)
3239 return compiler_error(c, "'yield' outside function");
3240 if (e->v.Yield.value) {
3241 VISIT(c, expr, e->v.Yield.value);
3242 }
3243 else {
3244 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3245 }
3246 ADDOP(c, YIELD_VALUE);
3247 break;
3248 case Compare_kind:
3249 return compiler_compare(c, e);
3250 case Call_kind:
3251 return compiler_call(c, e);
3252 case Num_kind:
3253 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3254 break;
3255 case Str_kind:
3256 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3257 break;
3258 case Bytes_kind:
3259 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
3260 break;
3261 case Ellipsis_kind:
3262 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3263 break;
3264 /* The following exprs can be assignment targets. */
3265 case Attribute_kind:
3266 if (e->v.Attribute.ctx != AugStore)
3267 VISIT(c, expr, e->v.Attribute.value);
3268 switch (e->v.Attribute.ctx) {
3269 case AugLoad:
3270 ADDOP(c, DUP_TOP);
3271 /* Fall through to load */
3272 case Load:
3273 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3274 break;
3275 case AugStore:
3276 ADDOP(c, ROT_TWO);
3277 /* Fall through to save */
3278 case Store:
3279 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3280 break;
3281 case Del:
3282 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3283 break;
3284 case Param:
3285 default:
3286 PyErr_SetString(PyExc_SystemError,
3287 "param invalid in attribute expression");
3288 return 0;
3289 }
3290 break;
3291 case Subscript_kind:
3292 switch (e->v.Subscript.ctx) {
3293 case AugLoad:
3294 VISIT(c, expr, e->v.Subscript.value);
3295 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3296 break;
3297 case Load:
3298 VISIT(c, expr, e->v.Subscript.value);
3299 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3300 break;
3301 case AugStore:
3302 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3303 break;
3304 case Store:
3305 VISIT(c, expr, e->v.Subscript.value);
3306 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3307 break;
3308 case Del:
3309 VISIT(c, expr, e->v.Subscript.value);
3310 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3311 break;
3312 case Param:
3313 default:
3314 PyErr_SetString(PyExc_SystemError,
3315 "param invalid in subscript expression");
3316 return 0;
3317 }
3318 break;
3319 case Starred_kind:
3320 switch (e->v.Starred.ctx) {
3321 case Store:
3322 /* In all legitimate cases, the Starred node was already replaced
3323 * by compiler_list/compiler_tuple. XXX: is that okay? */
3324 return compiler_error(c,
3325 "starred assignment target must be in a list or tuple");
3326 default:
3327 return compiler_error(c,
3328 "can use starred expression only as assignment target");
3329 }
3330 break;
3331 case Name_kind:
3332 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3333 /* child nodes of List and Tuple will have expr_context set */
3334 case List_kind:
3335 return compiler_list(c, e);
3336 case Tuple_kind:
3337 return compiler_tuple(c, e);
3338 }
3339 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003340}
3341
3342static int
3343compiler_augassign(struct compiler *c, stmt_ty s)
3344{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003345 expr_ty e = s->v.AugAssign.target;
3346 expr_ty auge;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003347
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003348 assert(s->kind == AugAssign_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003349
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003350 switch (e->kind) {
3351 case Attribute_kind:
3352 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
3353 AugLoad, e->lineno, e->col_offset, c->c_arena);
3354 if (auge == NULL)
3355 return 0;
3356 VISIT(c, expr, auge);
3357 VISIT(c, expr, s->v.AugAssign.value);
3358 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3359 auge->v.Attribute.ctx = AugStore;
3360 VISIT(c, expr, auge);
3361 break;
3362 case Subscript_kind:
3363 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
3364 AugLoad, e->lineno, e->col_offset, c->c_arena);
3365 if (auge == NULL)
3366 return 0;
3367 VISIT(c, expr, auge);
3368 VISIT(c, expr, s->v.AugAssign.value);
3369 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3370 auge->v.Subscript.ctx = AugStore;
3371 VISIT(c, expr, auge);
3372 break;
3373 case Name_kind:
3374 if (!compiler_nameop(c, e->v.Name.id, Load))
3375 return 0;
3376 VISIT(c, expr, s->v.AugAssign.value);
3377 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3378 return compiler_nameop(c, e->v.Name.id, Store);
3379 default:
3380 PyErr_Format(PyExc_SystemError,
3381 "invalid node type (%d) for augmented assignment",
3382 e->kind);
3383 return 0;
3384 }
3385 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003386}
3387
3388static int
3389compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3390{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003391 struct fblockinfo *f;
3392 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3393 PyErr_SetString(PyExc_SystemError,
3394 "too many statically nested blocks");
3395 return 0;
3396 }
3397 f = &c->u->u_fblock[c->u->u_nfblocks++];
3398 f->fb_type = t;
3399 f->fb_block = b;
3400 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003401}
3402
3403static void
3404compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3405{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003406 struct compiler_unit *u = c->u;
3407 assert(u->u_nfblocks > 0);
3408 u->u_nfblocks--;
3409 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3410 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003411}
3412
Thomas Wouters89f507f2006-12-13 04:49:30 +00003413static int
3414compiler_in_loop(struct compiler *c) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003415 int i;
3416 struct compiler_unit *u = c->u;
3417 for (i = 0; i < u->u_nfblocks; ++i) {
3418 if (u->u_fblock[i].fb_type == LOOP)
3419 return 1;
3420 }
3421 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003422}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003423/* Raises a SyntaxError and returns 0.
3424 If something goes wrong, a different exception may be raised.
3425*/
3426
3427static int
3428compiler_error(struct compiler *c, const char *errstr)
3429{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003430 PyObject *loc;
3431 PyObject *u = NULL, *v = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003432
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003433 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3434 if (!loc) {
3435 Py_INCREF(Py_None);
3436 loc = Py_None;
3437 }
3438 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3439 Py_None, loc);
3440 if (!u)
3441 goto exit;
3442 v = Py_BuildValue("(zO)", errstr, u);
3443 if (!v)
3444 goto exit;
3445 PyErr_SetObject(PyExc_SyntaxError, v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003446 exit:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003447 Py_DECREF(loc);
3448 Py_XDECREF(u);
3449 Py_XDECREF(v);
3450 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003451}
3452
3453static int
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003454compiler_handle_subscr(struct compiler *c, const char *kind,
3455 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003456{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003457 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003458
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003459 /* XXX this code is duplicated */
3460 switch (ctx) {
3461 case AugLoad: /* fall through to Load */
3462 case Load: op = BINARY_SUBSCR; break;
3463 case AugStore:/* fall through to Store */
3464 case Store: op = STORE_SUBSCR; break;
3465 case Del: op = DELETE_SUBSCR; break;
3466 case Param:
3467 PyErr_Format(PyExc_SystemError,
3468 "invalid %s kind %d in subscript\n",
3469 kind, ctx);
3470 return 0;
3471 }
3472 if (ctx == AugLoad) {
3473 ADDOP_I(c, DUP_TOPX, 2);
3474 }
3475 else if (ctx == AugStore) {
3476 ADDOP(c, ROT_THREE);
3477 }
3478 ADDOP(c, op);
3479 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003480}
3481
3482static int
3483compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3484{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003485 int n = 2;
3486 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003487
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003488 /* only handles the cases where BUILD_SLICE is emitted */
3489 if (s->v.Slice.lower) {
3490 VISIT(c, expr, s->v.Slice.lower);
3491 }
3492 else {
3493 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3494 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003495
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003496 if (s->v.Slice.upper) {
3497 VISIT(c, expr, s->v.Slice.upper);
3498 }
3499 else {
3500 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3501 }
3502
3503 if (s->v.Slice.step) {
3504 n++;
3505 VISIT(c, expr, s->v.Slice.step);
3506 }
3507 ADDOP_I(c, BUILD_SLICE, n);
3508 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003509}
3510
3511static int
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003512compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3513 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003514{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003515 switch (s->kind) {
3516 case Slice_kind:
3517 return compiler_slice(c, s, ctx);
3518 case Index_kind:
3519 VISIT(c, expr, s->v.Index.value);
3520 break;
3521 case ExtSlice_kind:
3522 default:
3523 PyErr_SetString(PyExc_SystemError,
3524 "extended slice invalid in nested slice");
3525 return 0;
3526 }
3527 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003528}
3529
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003530static int
3531compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3532{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003533 char * kindname = NULL;
3534 switch (s->kind) {
3535 case Index_kind:
3536 kindname = "index";
3537 if (ctx != AugStore) {
3538 VISIT(c, expr, s->v.Index.value);
3539 }
3540 break;
3541 case Slice_kind:
3542 kindname = "slice";
3543 if (ctx != AugStore) {
3544 if (!compiler_slice(c, s, ctx))
3545 return 0;
3546 }
3547 break;
3548 case ExtSlice_kind:
3549 kindname = "extended slice";
3550 if (ctx != AugStore) {
3551 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3552 for (i = 0; i < n; i++) {
3553 slice_ty sub = (slice_ty)asdl_seq_GET(
3554 s->v.ExtSlice.dims, i);
3555 if (!compiler_visit_nested_slice(c, sub, ctx))
3556 return 0;
3557 }
3558 ADDOP_I(c, BUILD_TUPLE, n);
3559 }
3560 break;
3561 default:
3562 PyErr_Format(PyExc_SystemError,
3563 "invalid subscript kind %d", s->kind);
3564 return 0;
3565 }
3566 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003567}
3568
Thomas Wouters89f507f2006-12-13 04:49:30 +00003569/* End of the compiler section, beginning of the assembler section */
3570
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003571/* do depth-first search of basic block graph, starting with block.
3572 post records the block indices in post-order.
3573
3574 XXX must handle implicit jumps from one block to next
3575*/
3576
Thomas Wouters89f507f2006-12-13 04:49:30 +00003577struct assembler {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003578 PyObject *a_bytecode; /* string containing bytecode */
3579 int a_offset; /* offset into bytecode */
3580 int a_nblocks; /* number of reachable blocks */
3581 basicblock **a_postorder; /* list of blocks in dfs postorder */
3582 PyObject *a_lnotab; /* string containing lnotab */
3583 int a_lnotab_off; /* offset into lnotab */
3584 int a_lineno; /* last lineno of emitted instruction */
3585 int a_lineno_off; /* bytecode offset of last lineno */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003586};
3587
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003588static void
3589dfs(struct compiler *c, basicblock *b, struct assembler *a)
3590{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003591 int i;
3592 struct instr *instr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003593
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003594 if (b->b_seen)
3595 return;
3596 b->b_seen = 1;
3597 if (b->b_next != NULL)
3598 dfs(c, b->b_next, a);
3599 for (i = 0; i < b->b_iused; i++) {
3600 instr = &b->b_instr[i];
3601 if (instr->i_jrel || instr->i_jabs)
3602 dfs(c, instr->i_target, a);
3603 }
3604 a->a_postorder[a->a_nblocks++] = b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003605}
3606
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003607static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003608stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3609{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003610 int i;
3611 struct instr *instr;
3612 if (b->b_seen || b->b_startdepth >= depth)
3613 return maxdepth;
3614 b->b_seen = 1;
3615 b->b_startdepth = depth;
3616 for (i = 0; i < b->b_iused; i++) {
3617 instr = &b->b_instr[i];
3618 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3619 if (depth > maxdepth)
3620 maxdepth = depth;
3621 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3622 if (instr->i_jrel || instr->i_jabs) {
3623 maxdepth = stackdepth_walk(c, instr->i_target,
3624 depth, maxdepth);
3625 if (instr->i_opcode == JUMP_ABSOLUTE ||
3626 instr->i_opcode == JUMP_FORWARD) {
3627 goto out; /* remaining code is dead */
3628 }
3629 }
3630 }
3631 if (b->b_next)
3632 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003633out:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003634 b->b_seen = 0;
3635 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003636}
3637
3638/* Find the flow path that needs the largest stack. We assume that
3639 * cycles in the flow graph have no net effect on the stack depth.
3640 */
3641static int
3642stackdepth(struct compiler *c)
3643{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003644 basicblock *b, *entryblock;
3645 entryblock = NULL;
3646 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3647 b->b_seen = 0;
3648 b->b_startdepth = INT_MIN;
3649 entryblock = b;
3650 }
3651 if (!entryblock)
3652 return 0;
3653 return stackdepth_walk(c, entryblock, 0, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003654}
3655
3656static int
3657assemble_init(struct assembler *a, int nblocks, int firstlineno)
3658{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003659 memset(a, 0, sizeof(struct assembler));
3660 a->a_lineno = firstlineno;
3661 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3662 if (!a->a_bytecode)
3663 return 0;
3664 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3665 if (!a->a_lnotab)
3666 return 0;
3667 if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
3668 PyErr_NoMemory();
3669 return 0;
3670 }
3671 a->a_postorder = (basicblock **)PyObject_Malloc(
3672 sizeof(basicblock *) * nblocks);
3673 if (!a->a_postorder) {
3674 PyErr_NoMemory();
3675 return 0;
3676 }
3677 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003678}
3679
3680static void
3681assemble_free(struct assembler *a)
3682{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003683 Py_XDECREF(a->a_bytecode);
3684 Py_XDECREF(a->a_lnotab);
3685 if (a->a_postorder)
3686 PyObject_Free(a->a_postorder);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003687}
3688
3689/* Return the size of a basic block in bytes. */
3690
3691static int
3692instrsize(struct instr *instr)
3693{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003694 if (!instr->i_hasarg)
3695 return 1; /* 1 byte for the opcode*/
3696 if (instr->i_oparg > 0xffff)
3697 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3698 return 3; /* 1 (opcode) + 2 (oparg) */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003699}
3700
3701static int
3702blocksize(basicblock *b)
3703{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003704 int i;
3705 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003706
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003707 for (i = 0; i < b->b_iused; i++)
3708 size += instrsize(&b->b_instr[i]);
3709 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003710}
3711
3712/* All about a_lnotab.
3713
3714c_lnotab is an array of unsigned bytes disguised as a Python string.
3715It is used to map bytecode offsets to source code line #s (when needed
3716for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003717
Tim Peters2a7f3842001-06-09 09:26:21 +00003718The array is conceptually a list of
3719 (bytecode offset increment, line number increment)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003720pairs. The details are important and delicate, best illustrated by example:
Tim Peters2a7f3842001-06-09 09:26:21 +00003721
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003722 byte code offset source code line number
3723 0 1
3724 6 2
3725 50 7
3726 350 307
3727 361 308
Tim Peters2a7f3842001-06-09 09:26:21 +00003728
3729The first trick is that these numbers aren't stored, only the increments
3730from one row to the next (this doesn't really work, but it's a start):
3731
3732 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3733
3734The second trick is that an unsigned byte can't hold negative values, or
3735values larger than 255, so (a) there's a deep assumption that byte code
3736offsets and their corresponding line #s both increase monotonically, and (b)
3737if at least one column jumps by more than 255 from one row to the next, more
3738than one pair is written to the table. In case #b, there's no way to know
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003739from looking at the table later how many were written. That's the delicate
Tim Peters2a7f3842001-06-09 09:26:21 +00003740part. A user of c_lnotab desiring to find the source line number
3741corresponding to a bytecode address A should do something like this
3742
3743 lineno = addr = 0
3744 for addr_incr, line_incr in c_lnotab:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003745 addr += addr_incr
3746 if addr > A:
3747 return lineno
3748 lineno += line_incr
Tim Peters2a7f3842001-06-09 09:26:21 +00003749
3750In order for this to work, when the addr field increments by more than 255,
3751the line # increment in each pair generated must be 0 until the remaining addr
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003752increment is < 256. So, in the example above, assemble_lnotab (it used
3753to be called com_set_lineno) should not (as was actually done until 2.2)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003754expand 300, 300 to 255, 255, 45, 45,
3755 but to 255, 0, 45, 255, 0, 45.
Tim Peters2a7f3842001-06-09 09:26:21 +00003756*/
3757
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003758static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003759assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003760{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003761 int d_bytecode, d_lineno;
3762 int len;
3763 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003764
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003765 d_bytecode = a->a_offset - a->a_lineno_off;
3766 d_lineno = i->i_lineno - a->a_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003767
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003768 assert(d_bytecode >= 0);
3769 assert(d_lineno >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003770
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003771 if(d_bytecode == 0 && d_lineno == 0)
3772 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003773
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003774 if (d_bytecode > 255) {
3775 int j, nbytes, ncodes = d_bytecode / 255;
3776 nbytes = a->a_lnotab_off + 2 * ncodes;
3777 len = PyBytes_GET_SIZE(a->a_lnotab);
3778 if (nbytes >= len) {
3779 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
3780 len = nbytes;
3781 else if (len <= INT_MAX / 2)
3782 len *= 2;
3783 else {
3784 PyErr_NoMemory();
3785 return 0;
3786 }
3787 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
3788 return 0;
3789 }
3790 lnotab = (unsigned char *)
3791 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3792 for (j = 0; j < ncodes; j++) {
3793 *lnotab++ = 255;
3794 *lnotab++ = 0;
3795 }
3796 d_bytecode -= ncodes * 255;
3797 a->a_lnotab_off += ncodes * 2;
3798 }
3799 assert(d_bytecode <= 255);
3800 if (d_lineno > 255) {
3801 int j, nbytes, ncodes = d_lineno / 255;
3802 nbytes = a->a_lnotab_off + 2 * ncodes;
3803 len = PyBytes_GET_SIZE(a->a_lnotab);
3804 if (nbytes >= len) {
3805 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
3806 len = nbytes;
3807 else if (len <= INT_MAX / 2)
3808 len *= 2;
3809 else {
3810 PyErr_NoMemory();
3811 return 0;
3812 }
3813 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
3814 return 0;
3815 }
3816 lnotab = (unsigned char *)
3817 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3818 *lnotab++ = d_bytecode;
3819 *lnotab++ = 255;
3820 d_bytecode = 0;
3821 for (j = 1; j < ncodes; j++) {
3822 *lnotab++ = 0;
3823 *lnotab++ = 255;
3824 }
3825 d_lineno -= ncodes * 255;
3826 a->a_lnotab_off += ncodes * 2;
3827 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003828
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003829 len = PyBytes_GET_SIZE(a->a_lnotab);
3830 if (a->a_lnotab_off + 2 >= len) {
3831 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
3832 return 0;
3833 }
3834 lnotab = (unsigned char *)
3835 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003836
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003837 a->a_lnotab_off += 2;
3838 if (d_bytecode) {
3839 *lnotab++ = d_bytecode;
3840 *lnotab++ = d_lineno;
3841 }
3842 else { /* First line of a block; def stmt, etc. */
3843 *lnotab++ = 0;
3844 *lnotab++ = d_lineno;
3845 }
3846 a->a_lineno = i->i_lineno;
3847 a->a_lineno_off = a->a_offset;
3848 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003849}
3850
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003851/* assemble_emit()
3852 Extend the bytecode with a new instruction.
3853 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003854*/
3855
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003856static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003857assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003858{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003859 int size, arg = 0, ext = 0;
3860 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
3861 char *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003862
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003863 size = instrsize(i);
3864 if (i->i_hasarg) {
3865 arg = i->i_oparg;
3866 ext = arg >> 16;
3867 }
3868 if (i->i_lineno && !assemble_lnotab(a, i))
3869 return 0;
3870 if (a->a_offset + size >= len) {
3871 if (len > PY_SSIZE_T_MAX / 2)
3872 return 0;
3873 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
3874 return 0;
3875 }
3876 code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
3877 a->a_offset += size;
3878 if (size == 6) {
3879 assert(i->i_hasarg);
3880 *code++ = (char)EXTENDED_ARG;
3881 *code++ = ext & 0xff;
3882 *code++ = ext >> 8;
3883 arg &= 0xffff;
3884 }
3885 *code++ = i->i_opcode;
3886 if (i->i_hasarg) {
3887 assert(size == 3 || size == 6);
3888 *code++ = arg & 0xff;
3889 *code++ = arg >> 8;
3890 }
3891 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003892}
3893
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003894static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003895assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003896{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003897 basicblock *b;
3898 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
3899 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003900
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003901 /* Compute the size of each block and fixup jump args.
3902 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003903start:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003904 totsize = 0;
3905 for (i = a->a_nblocks - 1; i >= 0; i--) {
3906 b = a->a_postorder[i];
3907 bsize = blocksize(b);
3908 b->b_offset = totsize;
3909 totsize += bsize;
3910 }
3911 extended_arg_count = 0;
3912 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3913 bsize = b->b_offset;
3914 for (i = 0; i < b->b_iused; i++) {
3915 struct instr *instr = &b->b_instr[i];
3916 /* Relative jumps are computed relative to
3917 the instruction pointer after fetching
3918 the jump instruction.
3919 */
3920 bsize += instrsize(instr);
3921 if (instr->i_jabs)
3922 instr->i_oparg = instr->i_target->b_offset;
3923 else if (instr->i_jrel) {
3924 int delta = instr->i_target->b_offset - bsize;
3925 instr->i_oparg = delta;
3926 }
3927 else
3928 continue;
3929 if (instr->i_oparg > 0xffff)
3930 extended_arg_count++;
3931 }
3932 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003933
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003934 /* XXX: This is an awful hack that could hurt performance, but
3935 on the bright side it should work until we come up
3936 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00003937
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003938 In the meantime, should the goto be dropped in favor
3939 of a loop?
Neal Norwitzf1d50682005-10-23 23:00:41 +00003940
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003941 The issue is that in the first loop blocksize() is called
3942 which calls instrsize() which requires i_oparg be set
3943 appropriately. There is a bootstrap problem because
3944 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00003945
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003946 So we loop until we stop seeing new EXTENDED_ARGs.
3947 The only EXTENDED_ARGs that could be popping up are
3948 ones in jump instructions. So this should converge
3949 fairly quickly.
3950 */
3951 if (last_extended_arg_count != extended_arg_count) {
3952 last_extended_arg_count = extended_arg_count;
3953 goto start;
3954 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003955}
3956
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003957static PyObject *
3958dict_keys_inorder(PyObject *dict, int offset)
3959{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003960 PyObject *tuple, *k, *v;
3961 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003962
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003963 tuple = PyTuple_New(size);
3964 if (tuple == NULL)
3965 return NULL;
3966 while (PyDict_Next(dict, &pos, &k, &v)) {
3967 i = PyLong_AS_LONG(v);
3968 /* The keys of the dictionary are tuples. (see compiler_add_o)
3969 The object we want is always first, though. */
3970 k = PyTuple_GET_ITEM(k, 0);
3971 Py_INCREF(k);
3972 assert((i - offset) < size);
3973 assert((i - offset) >= 0);
3974 PyTuple_SET_ITEM(tuple, i - offset, k);
3975 }
3976 return tuple;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003977}
3978
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003979static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003980compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003981{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003982 PySTEntryObject *ste = c->u->u_ste;
3983 int flags = 0, n;
3984 if (ste->ste_type != ModuleBlock)
3985 flags |= CO_NEWLOCALS;
3986 if (ste->ste_type == FunctionBlock) {
3987 if (!ste->ste_unoptimized)
3988 flags |= CO_OPTIMIZED;
3989 if (ste->ste_nested)
3990 flags |= CO_NESTED;
3991 if (ste->ste_generator)
3992 flags |= CO_GENERATOR;
3993 if (ste->ste_varargs)
3994 flags |= CO_VARARGS;
3995 if (ste->ste_varkeywords)
3996 flags |= CO_VARKEYWORDS;
3997 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003998
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003999 /* (Only) inherit compilerflags in PyCF_MASK */
4000 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00004001
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004002 n = PyDict_Size(c->u->u_freevars);
4003 if (n < 0)
4004 return -1;
4005 if (n == 0) {
4006 n = PyDict_Size(c->u->u_cellvars);
4007 if (n < 0)
4008 return -1;
4009 if (n == 0) {
4010 flags |= CO_NOFREE;
4011 }
4012 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004013
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004014 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004015}
4016
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004017static PyCodeObject *
4018makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004019{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004020 PyObject *tmp;
4021 PyCodeObject *co = NULL;
4022 PyObject *consts = NULL;
4023 PyObject *names = NULL;
4024 PyObject *varnames = NULL;
4025 PyObject *filename = NULL;
4026 PyObject *name = NULL;
4027 PyObject *freevars = NULL;
4028 PyObject *cellvars = NULL;
4029 PyObject *bytecode = NULL;
4030 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004031
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004032 tmp = dict_keys_inorder(c->u->u_consts, 0);
4033 if (!tmp)
4034 goto error;
4035 consts = PySequence_List(tmp); /* optimize_code requires a list */
4036 Py_DECREF(tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004037
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004038 names = dict_keys_inorder(c->u->u_names, 0);
4039 varnames = dict_keys_inorder(c->u->u_varnames, 0);
4040 if (!consts || !names || !varnames)
4041 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004042
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004043 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4044 if (!cellvars)
4045 goto error;
4046 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4047 if (!freevars)
4048 goto error;
Victor Stinner15244f72010-10-19 01:22:07 +00004049 filename = PyUnicode_DecodeFSDefault(c->c_filename);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004050 if (!filename)
4051 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004052
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004053 nlocals = PyDict_Size(c->u->u_varnames);
4054 flags = compute_code_flags(c);
4055 if (flags < 0)
4056 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004057
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004058 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
4059 if (!bytecode)
4060 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004061
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004062 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4063 if (!tmp)
4064 goto error;
4065 Py_DECREF(consts);
4066 consts = tmp;
4067
4068 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
4069 nlocals, stackdepth(c), flags,
4070 bytecode, consts, names, varnames,
4071 freevars, cellvars,
4072 filename, c->u->u_name,
4073 c->u->u_firstlineno,
4074 a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004075 error:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004076 Py_XDECREF(consts);
4077 Py_XDECREF(names);
4078 Py_XDECREF(varnames);
4079 Py_XDECREF(filename);
4080 Py_XDECREF(name);
4081 Py_XDECREF(freevars);
4082 Py_XDECREF(cellvars);
4083 Py_XDECREF(bytecode);
4084 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004085}
4086
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004087
4088/* For debugging purposes only */
4089#if 0
4090static void
4091dump_instr(const struct instr *i)
4092{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004093 const char *jrel = i->i_jrel ? "jrel " : "";
4094 const char *jabs = i->i_jabs ? "jabs " : "";
4095 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004096
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004097 *arg = '\0';
4098 if (i->i_hasarg)
4099 sprintf(arg, "arg: %d ", i->i_oparg);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004100
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004101 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4102 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004103}
4104
4105static void
4106dump_basicblock(const basicblock *b)
4107{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004108 const char *seen = b->b_seen ? "seen " : "";
4109 const char *b_return = b->b_return ? "return " : "";
4110 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4111 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4112 if (b->b_instr) {
4113 int i;
4114 for (i = 0; i < b->b_iused; i++) {
4115 fprintf(stderr, " [%02d] ", i);
4116 dump_instr(b->b_instr + i);
4117 }
4118 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004119}
4120#endif
4121
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004122static PyCodeObject *
4123assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004124{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004125 basicblock *b, *entryblock;
4126 struct assembler a;
4127 int i, j, nblocks;
4128 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004129
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004130 /* Make sure every block that falls off the end returns None.
4131 XXX NEXT_BLOCK() isn't quite right, because if the last
4132 block ends with a jump or return b_next shouldn't set.
4133 */
4134 if (!c->u->u_curblock->b_return) {
4135 NEXT_BLOCK(c);
4136 if (addNone)
4137 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4138 ADDOP(c, RETURN_VALUE);
4139 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004140
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004141 nblocks = 0;
4142 entryblock = NULL;
4143 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4144 nblocks++;
4145 entryblock = b;
4146 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004147
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004148 /* Set firstlineno if it wasn't explicitly set. */
4149 if (!c->u->u_firstlineno) {
4150 if (entryblock && entryblock->b_instr)
4151 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4152 else
4153 c->u->u_firstlineno = 1;
4154 }
4155 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4156 goto error;
4157 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004158
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004159 /* Can't modify the bytecode after computing jump offsets. */
4160 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004161
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004162 /* Emit code in reverse postorder from dfs. */
4163 for (i = a.a_nblocks - 1; i >= 0; i--) {
4164 b = a.a_postorder[i];
4165 for (j = 0; j < b->b_iused; j++)
4166 if (!assemble_emit(&a, &b->b_instr[j]))
4167 goto error;
4168 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004169
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004170 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4171 goto error;
4172 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset) < 0)
4173 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004174
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004175 co = makecode(c, &a);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004176 error:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004177 assemble_free(&a);
4178 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004179}