blob: 6963a151a76b4838a1978fc6b897d6cedd73a590 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file compiles an abstract syntax tree (AST) into Python bytecode.
3 *
4 * The primary entry point is PyAST_Compile(), which returns a
5 * PyCodeObject. The compiler makes several passes to build the code
6 * object:
7 * 1. Checks for future statements. See future.c
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00008 * 2. Builds a symbol table. See symtable.c.
Thomas Wouters89f507f2006-12-13 04:49:30 +00009 * 3. Generate code for basic blocks. See compiler_mod() in this file.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000010 * 4. Assemble the basic blocks into final code. See assemble() in
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000011 * this file.
Thomas Wouters89f507f2006-12-13 04:49:30 +000012 * 5. Optimize the byte code (peephole optimizations). See peephole.c
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013 *
14 * Note that compiler_mod() suggests module, but the module ast type
15 * (mod_ty) has cases for expressions and interactive statements.
Nick Coghlan944d3eb2005-11-16 12:46:55 +000016 *
Jeremy Hyltone9357b22006-03-01 15:47:05 +000017 * CAUTION: The VISIT_* macros abort the current function when they
18 * encounter a problem. So don't invoke them when there is memory
19 * which needs to be released. Code blocks are OK, as the compiler
Thomas Wouters89f507f2006-12-13 04:49:30 +000020 * structure takes care of releasing those. Use the arena to manage
21 * objects.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022 */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000023
Guido van Rossum79f25d91997-04-29 20:08:16 +000024#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000025
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000026#include "Python-ast.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000027#include "node.h"
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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +000099 PySTEntryObject *u_ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000112 PyObject *u_private; /* for private name mangling */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 int u_nfblocks;
122 struct fblockinfo u_fblock[CO_MAXBLOCKS];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 int u_firstlineno; /* the first lineno of the block */
125 int u_lineno; /* the lineno for the current stmt */
126 int u_lineno_set; /* boolean to indicate whether instr
127 has been generated with current lineno */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000128};
129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130/* This struct captures the global state of a compilation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000131
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000132The u pointer points to the current compilation unit, while units
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133for enclosing blocks are stored in c_stack. The u and c_stack are
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000134managed by compiler_enter_scope() and compiler_exit_scope().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000135*/
136
137struct compiler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 const char *c_filename;
139 struct symtable *c_st;
140 PyFutureFeatures *c_future; /* pointer to module's __future__ */
141 PyCompilerFlags *c_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 int c_interactive; /* true if in interactive mode */
144 int c_nestlevel;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 struct compiler_unit *u; /* compiler state for current block */
147 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
148 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000149};
150
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000151static int compiler_enter_scope(struct compiler *, identifier, void *, int);
152static void compiler_free(struct compiler *);
153static basicblock *compiler_new_block(struct compiler *);
154static int compiler_next_instr(struct compiler *, basicblock *);
155static int compiler_addop(struct compiler *, int);
156static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
157static int compiler_addop_i(struct compiler *, int, int);
158static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000159static basicblock *compiler_use_new_block(struct compiler *);
160static int compiler_error(struct compiler *, const char *);
161static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
162
163static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
164static int compiler_visit_stmt(struct compiler *, stmt_ty);
165static int compiler_visit_keyword(struct compiler *, keyword_ty);
166static int compiler_visit_expr(struct compiler *, expr_ty);
167static int compiler_augassign(struct compiler *, stmt_ty);
168static int compiler_visit_slice(struct compiler *, slice_ty,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 expr_context_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000170
171static int compiler_push_fblock(struct compiler *, enum fblocktype,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000173static void compiler_pop_fblock(struct compiler *, enum fblocktype,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 basicblock *);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000175/* Returns true if there is a loop on the fblock stack. */
176static int compiler_in_loop(struct compiler *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000177
178static int inplace_binop(struct compiler *, operator_ty);
179static int expr_constant(expr_ty e);
180
Guido van Rossumc2e20742006-02-27 22:32:47 +0000181static int compiler_with(struct compiler *, stmt_ty);
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000182static int compiler_call_helper(struct compiler *c, int n,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 asdl_seq *args,
184 asdl_seq *keywords,
185 expr_ty starargs,
186 expr_ty kwargs);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000187
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000188static PyCodeObject *assemble(struct compiler *, int addNone);
189static PyObject *__doc__;
190
Benjamin Petersonb173f782009-05-05 22:31:58 +0000191#define COMPILER_CAPSULE_NAME_COMPILER_UNIT "compile.c compiler unit"
192
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000194_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 /* Name mangling: __private becomes _classname__private.
197 This is independent from how the name is used. */
198 const Py_UNICODE *p, *name = PyUnicode_AS_UNICODE(ident);
199 Py_UNICODE *buffer;
200 size_t nlen, plen;
201 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
202 name == NULL || name[0] != '_' || name[1] != '_') {
203 Py_INCREF(ident);
204 return ident;
205 }
206 p = PyUnicode_AS_UNICODE(privateobj);
207 nlen = Py_UNICODE_strlen(name);
208 /* Don't mangle __id__ or names with dots.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 The only time a name with a dot can occur is when
211 we are compiling an import statement that has a
212 package name.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 TODO(jhylton): Decide whether we want to support
215 mangling of the module name, e.g. __M.X.
216 */
217 if ((name[nlen-1] == '_' && name[nlen-2] == '_')
218 || Py_UNICODE_strchr(name, '.')) {
219 Py_INCREF(ident);
220 return ident; /* Don't mangle __whatever__ */
221 }
222 /* Strip leading underscores from class name */
223 while (*p == '_')
224 p++;
225 if (*p == 0) {
226 Py_INCREF(ident);
227 return ident; /* Don't mangle if class is just underscores */
228 }
229 plen = Py_UNICODE_strlen(p);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 assert(1 <= PY_SSIZE_T_MAX - nlen);
232 assert(1 + nlen <= PY_SSIZE_T_MAX - plen);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 ident = PyUnicode_FromStringAndSize(NULL, 1 + nlen + plen);
235 if (!ident)
236 return 0;
237 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
238 buffer = PyUnicode_AS_UNICODE(ident);
239 buffer[0] = '_';
240 Py_UNICODE_strncpy(buffer+1, p, plen);
241 Py_UNICODE_strcpy(buffer+1+plen, name);
242 return ident;
Michael W. Hudson60934622004-08-12 17:56:29 +0000243}
244
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000245static int
246compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000247{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 c->c_stack = PyList_New(0);
251 if (!c->c_stack)
252 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000255}
256
257PyCodeObject *
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000258PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 struct compiler c;
262 PyCodeObject *co = NULL;
263 PyCompilerFlags local_flags;
264 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 if (!__doc__) {
267 __doc__ = PyUnicode_InternFromString("__doc__");
268 if (!__doc__)
269 return NULL;
270 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 if (!compiler_init(&c))
273 return NULL;
274 c.c_filename = filename;
275 c.c_arena = arena;
276 c.c_future = PyFuture_FromAST(mod, filename);
277 if (c.c_future == NULL)
278 goto finally;
279 if (!flags) {
280 local_flags.cf_flags = 0;
281 flags = &local_flags;
282 }
283 merged = c.c_future->ff_features | flags->cf_flags;
284 c.c_future->ff_features = merged;
285 flags->cf_flags = merged;
286 c.c_flags = flags;
287 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 c.c_st = PySymtable_Build(mod, filename, c.c_future);
290 if (c.c_st == NULL) {
291 if (!PyErr_Occurred())
292 PyErr_SetString(PyExc_SystemError, "no symtable");
293 goto finally;
294 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000297
Thomas Wouters1175c432006-02-27 22:49:54 +0000298 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 compiler_free(&c);
300 assert(co || PyErr_Occurred());
301 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000302}
303
304PyCodeObject *
305PyNode_Compile(struct _node *n, const char *filename)
306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 PyCodeObject *co = NULL;
308 mod_ty mod;
309 PyArena *arena = PyArena_New();
310 if (!arena)
311 return NULL;
312 mod = PyAST_FromNode(n, NULL, filename, arena);
313 if (mod)
314 co = PyAST_Compile(mod, filename, NULL, arena);
315 PyArena_Free(arena);
316 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000317}
318
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000319static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000320compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000321{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 if (c->c_st)
323 PySymtable_Free(c->c_st);
324 if (c->c_future)
325 PyObject_Free(c->c_future);
326 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000327}
328
Guido van Rossum79f25d91997-04-29 20:08:16 +0000329static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000330list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 Py_ssize_t i, n;
333 PyObject *v, *k;
334 PyObject *dict = PyDict_New();
335 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 n = PyList_Size(list);
338 for (i = 0; i < n; i++) {
339 v = PyLong_FromLong(i);
340 if (!v) {
341 Py_DECREF(dict);
342 return NULL;
343 }
344 k = PyList_GET_ITEM(list, i);
345 k = PyTuple_Pack(2, k, k->ob_type);
346 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
347 Py_XDECREF(k);
348 Py_DECREF(v);
349 Py_DECREF(dict);
350 return NULL;
351 }
352 Py_DECREF(k);
353 Py_DECREF(v);
354 }
355 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000356}
357
358/* Return new dict containing names from src that match scope(s).
359
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000360src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000362values are integers, starting at offset and increasing by one for
363each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000364*/
365
366static PyObject *
367dictbytype(PyObject *src, int scope_type, int flag, int offset)
368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 Py_ssize_t pos = 0, i = offset, scope;
370 PyObject *k, *v, *dest = PyDict_New();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 assert(offset >= 0);
373 if (dest == NULL)
374 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 while (PyDict_Next(src, &pos, &k, &v)) {
377 /* XXX this should probably be a macro in symtable.h */
378 long vi;
379 assert(PyLong_Check(v));
380 vi = PyLong_AS_LONG(v);
381 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 if (scope == scope_type || vi & flag) {
384 PyObject *tuple, *item = PyLong_FromLong(i);
385 if (item == NULL) {
386 Py_DECREF(dest);
387 return NULL;
388 }
389 i++;
390 tuple = PyTuple_Pack(2, k, k->ob_type);
391 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
392 Py_DECREF(item);
393 Py_DECREF(dest);
394 Py_XDECREF(tuple);
395 return NULL;
396 }
397 Py_DECREF(item);
398 Py_DECREF(tuple);
399 }
400 }
401 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000402}
403
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000404static void
405compiler_unit_check(struct compiler_unit *u)
406{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 basicblock *block;
408 for (block = u->u_blocks; block != NULL; block = block->b_list) {
409 assert((void *)block != (void *)0xcbcbcbcb);
410 assert((void *)block != (void *)0xfbfbfbfb);
411 assert((void *)block != (void *)0xdbdbdbdb);
412 if (block->b_instr != NULL) {
413 assert(block->b_ialloc > 0);
414 assert(block->b_iused > 0);
415 assert(block->b_ialloc >= block->b_iused);
416 }
417 else {
418 assert (block->b_iused == 0);
419 assert (block->b_ialloc == 0);
420 }
421 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000422}
423
424static void
425compiler_unit_free(struct compiler_unit *u)
426{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 compiler_unit_check(u);
430 b = u->u_blocks;
431 while (b != NULL) {
432 if (b->b_instr)
433 PyObject_Free((void *)b->b_instr);
434 next = b->b_list;
435 PyObject_Free((void *)b);
436 b = next;
437 }
438 Py_CLEAR(u->u_ste);
439 Py_CLEAR(u->u_name);
440 Py_CLEAR(u->u_consts);
441 Py_CLEAR(u->u_names);
442 Py_CLEAR(u->u_varnames);
443 Py_CLEAR(u->u_freevars);
444 Py_CLEAR(u->u_cellvars);
445 Py_CLEAR(u->u_private);
446 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000447}
448
449static int
450compiler_enter_scope(struct compiler *c, identifier name, void *key,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
456 struct compiler_unit));
457 if (!u) {
458 PyErr_NoMemory();
459 return 0;
460 }
461 memset(u, 0, sizeof(struct compiler_unit));
462 u->u_argcount = 0;
463 u->u_kwonlyargcount = 0;
464 u->u_ste = PySymtable_Lookup(c->c_st, key);
465 if (!u->u_ste) {
466 compiler_unit_free(u);
467 return 0;
468 }
469 Py_INCREF(name);
470 u->u_name = name;
471 u->u_varnames = list2dict(u->u_ste->ste_varnames);
472 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
473 if (!u->u_varnames || !u->u_cellvars) {
474 compiler_unit_free(u);
475 return 0;
476 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
479 PyDict_Size(u->u_cellvars));
480 if (!u->u_freevars) {
481 compiler_unit_free(u);
482 return 0;
483 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 u->u_blocks = NULL;
486 u->u_nfblocks = 0;
487 u->u_firstlineno = lineno;
488 u->u_lineno = 0;
489 u->u_lineno_set = 0;
490 u->u_consts = PyDict_New();
491 if (!u->u_consts) {
492 compiler_unit_free(u);
493 return 0;
494 }
495 u->u_names = PyDict_New();
496 if (!u->u_names) {
497 compiler_unit_free(u);
498 return 0;
499 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 /* Push the old compiler_unit on the stack. */
504 if (c->u) {
505 PyObject *capsule = PyCapsule_New(c->u, COMPILER_CAPSULE_NAME_COMPILER_UNIT, NULL);
506 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
507 Py_XDECREF(capsule);
508 compiler_unit_free(u);
509 return 0;
510 }
511 Py_DECREF(capsule);
512 u->u_private = c->u->u_private;
513 Py_XINCREF(u->u_private);
514 }
515 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 c->c_nestlevel++;
518 if (compiler_use_new_block(c) == NULL)
519 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000522}
523
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000524static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000525compiler_exit_scope(struct compiler *c)
526{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 int n;
528 PyObject *capsule;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 c->c_nestlevel--;
531 compiler_unit_free(c->u);
532 /* Restore c->u to the parent unit. */
533 n = PyList_GET_SIZE(c->c_stack) - 1;
534 if (n >= 0) {
535 capsule = PyList_GET_ITEM(c->c_stack, n);
536 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_CAPSULE_NAME_COMPILER_UNIT);
537 assert(c->u);
538 /* we are deleting from a list so this really shouldn't fail */
539 if (PySequence_DelItem(c->c_stack, n) < 0)
540 Py_FatalError("compiler_exit_scope()");
541 compiler_unit_check(c->u);
542 }
543 else
544 c->u = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546}
547
548/* Allocate a new block and return a pointer to it.
549 Returns NULL on error.
550*/
551
552static basicblock *
553compiler_new_block(struct compiler *c)
554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 basicblock *b;
556 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 u = c->u;
559 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
560 if (b == NULL) {
561 PyErr_NoMemory();
562 return NULL;
563 }
564 memset((void *)b, 0, sizeof(basicblock));
565 /* Extend the singly linked list of blocks with new block. */
566 b->b_list = u->u_blocks;
567 u->u_blocks = b;
568 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000569}
570
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000571static basicblock *
572compiler_use_new_block(struct compiler *c)
573{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 basicblock *block = compiler_new_block(c);
575 if (block == NULL)
576 return NULL;
577 c->u->u_curblock = block;
578 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579}
580
581static basicblock *
582compiler_next_block(struct compiler *c)
583{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 basicblock *block = compiler_new_block(c);
585 if (block == NULL)
586 return NULL;
587 c->u->u_curblock->b_next = block;
588 c->u->u_curblock = block;
589 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000590}
591
592static basicblock *
593compiler_use_next_block(struct compiler *c, basicblock *block)
594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 assert(block != NULL);
596 c->u->u_curblock->b_next = block;
597 c->u->u_curblock = block;
598 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000599}
600
601/* Returns the offset of the next instruction in the current block's
602 b_instr array. Resizes the b_instr as necessary.
603 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000604*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000605
606static int
607compiler_next_instr(struct compiler *c, basicblock *b)
608{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 assert(b != NULL);
610 if (b->b_instr == NULL) {
611 b->b_instr = (struct instr *)PyObject_Malloc(
612 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
613 if (b->b_instr == NULL) {
614 PyErr_NoMemory();
615 return -1;
616 }
617 b->b_ialloc = DEFAULT_BLOCK_SIZE;
618 memset((char *)b->b_instr, 0,
619 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
620 }
621 else if (b->b_iused == b->b_ialloc) {
622 struct instr *tmp;
623 size_t oldsize, newsize;
624 oldsize = b->b_ialloc * sizeof(struct instr);
625 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 if (oldsize > (PY_SIZE_MAX >> 1)) {
628 PyErr_NoMemory();
629 return -1;
630 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 if (newsize == 0) {
633 PyErr_NoMemory();
634 return -1;
635 }
636 b->b_ialloc <<= 1;
637 tmp = (struct instr *)PyObject_Realloc(
638 (void *)b->b_instr, newsize);
639 if (tmp == NULL) {
640 PyErr_NoMemory();
641 return -1;
642 }
643 b->b_instr = tmp;
644 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
645 }
646 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000647}
648
Christian Heimes2202f872008-02-06 14:31:34 +0000649/* Set the i_lineno member of the instruction at offset off if the
650 line number for the current expression/statement has not
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000651 already been set. If it has been set, the call has no effect.
652
Christian Heimes2202f872008-02-06 14:31:34 +0000653 The line number is reset in the following cases:
654 - when entering a new scope
655 - on each statement
656 - on each expression that start a new line
657 - before the "except" clause
658 - before the "for" and "while" expressions
Thomas Wouters89f507f2006-12-13 04:49:30 +0000659*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000660
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000661static void
662compiler_set_lineno(struct compiler *c, int off)
663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 basicblock *b;
665 if (c->u->u_lineno_set)
666 return;
667 c->u->u_lineno_set = 1;
668 b = c->u->u_curblock;
669 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000670}
671
672static int
673opcode_stack_effect(int opcode, int oparg)
674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 switch (opcode) {
676 case POP_TOP:
677 return -1;
678 case ROT_TWO:
679 case ROT_THREE:
680 return 0;
681 case DUP_TOP:
682 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000683 case DUP_TOP_TWO:
684 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 case UNARY_POSITIVE:
687 case UNARY_NEGATIVE:
688 case UNARY_NOT:
689 case UNARY_INVERT:
690 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 case SET_ADD:
693 case LIST_APPEND:
694 return -1;
695 case MAP_ADD:
696 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 case BINARY_POWER:
699 case BINARY_MULTIPLY:
700 case BINARY_MODULO:
701 case BINARY_ADD:
702 case BINARY_SUBTRACT:
703 case BINARY_SUBSCR:
704 case BINARY_FLOOR_DIVIDE:
705 case BINARY_TRUE_DIVIDE:
706 return -1;
707 case INPLACE_FLOOR_DIVIDE:
708 case INPLACE_TRUE_DIVIDE:
709 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 case INPLACE_ADD:
712 case INPLACE_SUBTRACT:
713 case INPLACE_MULTIPLY:
714 case INPLACE_MODULO:
715 return -1;
716 case STORE_SUBSCR:
717 return -3;
718 case STORE_MAP:
719 return -2;
720 case DELETE_SUBSCR:
721 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 case BINARY_LSHIFT:
724 case BINARY_RSHIFT:
725 case BINARY_AND:
726 case BINARY_XOR:
727 case BINARY_OR:
728 return -1;
729 case INPLACE_POWER:
730 return -1;
731 case GET_ITER:
732 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 case PRINT_EXPR:
735 return -1;
736 case LOAD_BUILD_CLASS:
737 return 1;
738 case INPLACE_LSHIFT:
739 case INPLACE_RSHIFT:
740 case INPLACE_AND:
741 case INPLACE_XOR:
742 case INPLACE_OR:
743 return -1;
744 case BREAK_LOOP:
745 return 0;
746 case SETUP_WITH:
747 return 7;
748 case WITH_CLEANUP:
749 return -1; /* XXX Sometimes more */
750 case STORE_LOCALS:
751 return -1;
752 case RETURN_VALUE:
753 return -1;
754 case IMPORT_STAR:
755 return -1;
756 case YIELD_VALUE:
757 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 case POP_BLOCK:
760 return 0;
761 case POP_EXCEPT:
762 return 0; /* -3 except if bad bytecode */
763 case END_FINALLY:
764 return -1; /* or -2 or -3 if exception occurred */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 case STORE_NAME:
767 return -1;
768 case DELETE_NAME:
769 return 0;
770 case UNPACK_SEQUENCE:
771 return oparg-1;
772 case UNPACK_EX:
773 return (oparg&0xFF) + (oparg>>8);
774 case FOR_ITER:
775 return 1; /* or -1, at end of iterator */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 case STORE_ATTR:
778 return -2;
779 case DELETE_ATTR:
780 return -1;
781 case STORE_GLOBAL:
782 return -1;
783 case DELETE_GLOBAL:
784 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 case LOAD_CONST:
786 return 1;
787 case LOAD_NAME:
788 return 1;
789 case BUILD_TUPLE:
790 case BUILD_LIST:
791 case BUILD_SET:
792 return 1-oparg;
793 case BUILD_MAP:
794 return 1;
795 case LOAD_ATTR:
796 return 0;
797 case COMPARE_OP:
798 return -1;
799 case IMPORT_NAME:
800 return -1;
801 case IMPORT_FROM:
802 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 case JUMP_FORWARD:
805 case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */
806 case JUMP_IF_FALSE_OR_POP: /* "" */
807 case JUMP_ABSOLUTE:
808 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 case POP_JUMP_IF_FALSE:
811 case POP_JUMP_IF_TRUE:
812 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 case LOAD_GLOBAL:
815 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 case CONTINUE_LOOP:
818 return 0;
819 case SETUP_LOOP:
820 return 0;
821 case SETUP_EXCEPT:
822 case SETUP_FINALLY:
823 return 6; /* can push 3 values for the new exception
824 + 3 others for the previous exception state */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 case LOAD_FAST:
827 return 1;
828 case STORE_FAST:
829 return -1;
830 case DELETE_FAST:
831 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 case RAISE_VARARGS:
834 return -oparg;
Neal Norwitzc1505362006-12-28 06:47:50 +0000835#define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 case CALL_FUNCTION:
837 return -NARGS(oparg);
838 case CALL_FUNCTION_VAR:
839 case CALL_FUNCTION_KW:
840 return -NARGS(oparg)-1;
841 case CALL_FUNCTION_VAR_KW:
842 return -NARGS(oparg)-2;
843 case MAKE_FUNCTION:
844 return -NARGS(oparg) - ((oparg >> 16) & 0xffff);
845 case MAKE_CLOSURE:
846 return -1 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000847#undef NARGS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 case BUILD_SLICE:
849 if (oparg == 3)
850 return -2;
851 else
852 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 case LOAD_CLOSURE:
855 return 1;
856 case LOAD_DEREF:
857 return 1;
858 case STORE_DEREF:
859 return -1;
860 default:
861 fprintf(stderr, "opcode = %d\n", opcode);
862 Py_FatalError("opcode_stack_effect()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 }
865 return 0; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866}
867
868/* Add an opcode with no argument.
869 Returns 0 on failure, 1 on success.
870*/
871
872static int
873compiler_addop(struct compiler *c, int opcode)
874{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 basicblock *b;
876 struct instr *i;
877 int off;
878 off = compiler_next_instr(c, c->u->u_curblock);
879 if (off < 0)
880 return 0;
881 b = c->u->u_curblock;
882 i = &b->b_instr[off];
883 i->i_opcode = opcode;
884 i->i_hasarg = 0;
885 if (opcode == RETURN_VALUE)
886 b->b_return = 1;
887 compiler_set_lineno(c, off);
888 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000889}
890
891static int
892compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
893{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 PyObject *t, *v;
895 Py_ssize_t arg;
896 double d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 /* necessary to make sure types aren't coerced (e.g., int and long) */
899 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
900 if (PyFloat_Check(o)) {
901 d = PyFloat_AS_DOUBLE(o);
902 /* all we need is to make the tuple different in either the 0.0
903 * or -0.0 case from all others, just to avoid the "coercion".
904 */
905 if (d == 0.0 && copysign(1.0, d) < 0.0)
906 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
907 else
908 t = PyTuple_Pack(2, o, o->ob_type);
909 }
910 else if (PyComplex_Check(o)) {
911 Py_complex z;
912 int real_negzero, imag_negzero;
913 /* For the complex case we must make complex(x, 0.)
914 different from complex(x, -0.) and complex(0., y)
915 different from complex(-0., y), for any x and y.
916 All four complex zeros must be distinguished.*/
917 z = PyComplex_AsCComplex(o);
918 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
919 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
920 if (real_negzero && imag_negzero) {
921 t = PyTuple_Pack(5, o, o->ob_type,
922 Py_None, Py_None, Py_None);
Christian Heimes400adb02008-02-01 08:12:03 +0000923 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 else if (imag_negzero) {
925 t = PyTuple_Pack(4, o, o->ob_type, Py_None, Py_None);
Guido van Rossum04110fb2007-08-24 16:32:05 +0000926 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 else if (real_negzero) {
928 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
929 }
930 else {
931 t = PyTuple_Pack(2, o, o->ob_type);
932 }
933 }
934 else {
935 t = PyTuple_Pack(2, o, o->ob_type);
936 }
937 if (t == NULL)
938 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 v = PyDict_GetItem(dict, t);
941 if (!v) {
942 if (PyErr_Occurred())
943 return -1;
944 arg = PyDict_Size(dict);
945 v = PyLong_FromLong(arg);
946 if (!v) {
947 Py_DECREF(t);
948 return -1;
949 }
950 if (PyDict_SetItem(dict, t, v) < 0) {
951 Py_DECREF(t);
952 Py_DECREF(v);
953 return -1;
954 }
955 Py_DECREF(v);
956 }
957 else
958 arg = PyLong_AsLong(v);
959 Py_DECREF(t);
960 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000961}
962
963static int
964compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966{
967 int arg = compiler_add_o(c, dict, o);
968 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +0000969 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000970 return compiler_addop_i(c, opcode, arg);
971}
972
973static int
974compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976{
977 int arg;
978 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
979 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +0000980 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981 arg = compiler_add_o(c, dict, mangled);
982 Py_DECREF(mangled);
983 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +0000984 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000985 return compiler_addop_i(c, opcode, arg);
986}
987
988/* Add an opcode with an integer argument.
989 Returns 0 on failure, 1 on success.
990*/
991
992static int
993compiler_addop_i(struct compiler *c, int opcode, int oparg)
994{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 struct instr *i;
996 int off;
997 off = compiler_next_instr(c, c->u->u_curblock);
998 if (off < 0)
999 return 0;
1000 i = &c->u->u_curblock->b_instr[off];
1001 i->i_opcode = opcode;
1002 i->i_oparg = oparg;
1003 i->i_hasarg = 1;
1004 compiler_set_lineno(c, off);
1005 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006}
1007
1008static int
1009compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1010{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 struct instr *i;
1012 int off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 assert(b != NULL);
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_target = b;
1021 i->i_hasarg = 1;
1022 if (absolute)
1023 i->i_jabs = 1;
1024 else
1025 i->i_jrel = 1;
1026 compiler_set_lineno(c, off);
1027 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001028}
1029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1031 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032 it as the current block. NEXT_BLOCK() also creates an implicit jump
1033 from the current block to the new block.
1034*/
1035
Thomas Wouters89f507f2006-12-13 04:49:30 +00001036/* The returns inside these macros make it impossible to decref objects
1037 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001038*/
1039
1040
1041#define NEW_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 if (compiler_use_new_block((C)) == NULL) \
1043 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001044}
1045
1046#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 if (compiler_next_block((C)) == NULL) \
1048 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001049}
1050
1051#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 if (!compiler_addop((C), (OP))) \
1053 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001054}
1055
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001056#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 if (!compiler_addop((C), (OP))) { \
1058 compiler_exit_scope(c); \
1059 return 0; \
1060 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001061}
1062
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001063#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1065 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001066}
1067
1068#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1070 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071}
1072
1073#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 if (!compiler_addop_i((C), (OP), (O))) \
1075 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076}
1077
1078#define ADDOP_JABS(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 if (!compiler_addop_j((C), (OP), (O), 1)) \
1080 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081}
1082
1083#define ADDOP_JREL(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 if (!compiler_addop_j((C), (OP), (O), 0)) \
1085 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086}
1087
1088/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1089 the ASDL name to synthesize the name of the C type and the visit function.
1090*/
1091
1092#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 if (!compiler_visit_ ## TYPE((C), (V))) \
1094 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001095}
1096
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001097#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 if (!compiler_visit_ ## TYPE((C), (V))) { \
1099 compiler_exit_scope(c); \
1100 return 0; \
1101 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001102}
1103
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001104#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 if (!compiler_visit_slice((C), (V), (CTX))) \
1106 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107}
1108
1109#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 int _i; \
1111 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1112 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1113 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1114 if (!compiler_visit_ ## TYPE((C), elt)) \
1115 return 0; \
1116 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001117}
1118
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001119#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 int _i; \
1121 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1122 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1123 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1124 if (!compiler_visit_ ## TYPE((C), elt)) { \
1125 compiler_exit_scope(c); \
1126 return 0; \
1127 } \
1128 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001129}
1130
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131static int
1132compiler_isdocstring(stmt_ty s)
1133{
1134 if (s->kind != Expr_kind)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001135 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136 return s->v.Expr.value->kind == Str_kind;
1137}
1138
1139/* Compile a sequence of statements, checking for a docstring. */
1140
1141static int
1142compiler_body(struct compiler *c, asdl_seq *stmts)
1143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 int i = 0;
1145 stmt_ty st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 if (!asdl_seq_LEN(stmts))
1148 return 1;
1149 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1150 if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) {
1151 /* don't generate docstrings if -OO */
1152 i = 1;
1153 VISIT(c, expr, st->v.Expr.value);
1154 if (!compiler_nameop(c, __doc__, Store))
1155 return 0;
1156 }
1157 for (; i < asdl_seq_LEN(stmts); i++)
1158 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
1159 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160}
1161
1162static PyCodeObject *
1163compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001164{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 PyCodeObject *co;
1166 int addNone = 1;
1167 static PyObject *module;
1168 if (!module) {
1169 module = PyUnicode_InternFromString("<module>");
1170 if (!module)
1171 return NULL;
1172 }
1173 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1174 if (!compiler_enter_scope(c, module, mod, 0))
1175 return NULL;
1176 switch (mod->kind) {
1177 case Module_kind:
1178 if (!compiler_body(c, mod->v.Module.body)) {
1179 compiler_exit_scope(c);
1180 return 0;
1181 }
1182 break;
1183 case Interactive_kind:
1184 c->c_interactive = 1;
1185 VISIT_SEQ_IN_SCOPE(c, stmt,
1186 mod->v.Interactive.body);
1187 break;
1188 case Expression_kind:
1189 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1190 addNone = 0;
1191 break;
1192 case Suite_kind:
1193 PyErr_SetString(PyExc_SystemError,
1194 "suite should not be possible");
1195 return 0;
1196 default:
1197 PyErr_Format(PyExc_SystemError,
1198 "module kind %d should not be possible",
1199 mod->kind);
1200 return 0;
1201 }
1202 co = assemble(c, addNone);
1203 compiler_exit_scope(c);
1204 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001205}
1206
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207/* The test for LOCAL must come before the test for FREE in order to
1208 handle classes where name is both local and free. The local var is
1209 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001210*/
1211
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212static int
1213get_ref_type(struct compiler *c, PyObject *name)
1214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 int scope = PyST_GetScope(c->u->u_ste, name);
1216 if (scope == 0) {
1217 char buf[350];
1218 PyOS_snprintf(buf, sizeof(buf),
1219 "unknown scope for %.100s in %.100s(%s) in %s\n"
1220 "symbols: %s\nlocals: %s\nglobals: %s",
1221 PyBytes_AS_STRING(name),
1222 PyBytes_AS_STRING(c->u->u_name),
1223 PyObject_REPR(c->u->u_ste->ste_id),
1224 c->c_filename,
1225 PyObject_REPR(c->u->u_ste->ste_symbols),
1226 PyObject_REPR(c->u->u_varnames),
1227 PyObject_REPR(c->u->u_names)
1228 );
1229 Py_FatalError(buf);
1230 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233}
1234
1235static int
1236compiler_lookup_arg(PyObject *dict, PyObject *name)
1237{
1238 PyObject *k, *v;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001239 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240 if (k == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001241 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001243 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001245 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001246 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001247}
1248
1249static int
1250compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1251{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 int i, free = PyCode_GetNumFree(co);
1253 if (free == 0) {
1254 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1255 ADDOP_I(c, MAKE_FUNCTION, args);
1256 return 1;
1257 }
1258 for (i = 0; i < free; ++i) {
1259 /* Bypass com_addop_varname because it will generate
1260 LOAD_DEREF but LOAD_CLOSURE is needed.
1261 */
1262 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1263 int arg, reftype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 /* Special case: If a class contains a method with a
1266 free variable that has the same name as a method,
1267 the name will be considered free *and* local in the
1268 class. It should be handled by the closure, as
1269 well as by the normal name loookup logic.
1270 */
1271 reftype = get_ref_type(c, name);
1272 if (reftype == CELL)
1273 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1274 else /* (reftype == FREE) */
1275 arg = compiler_lookup_arg(c->u->u_freevars, name);
1276 if (arg == -1) {
1277 fprintf(stderr,
1278 "lookup %s in %s %d %d\n"
1279 "freevars of %s: %s\n",
1280 PyObject_REPR(name),
1281 PyBytes_AS_STRING(c->u->u_name),
1282 reftype, arg,
1283 _PyUnicode_AsString(co->co_name),
1284 PyObject_REPR(co->co_freevars));
1285 Py_FatalError("compiler_make_closure()");
1286 }
1287 ADDOP_I(c, LOAD_CLOSURE, arg);
1288 }
1289 ADDOP_I(c, BUILD_TUPLE, free);
1290 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1291 ADDOP_I(c, MAKE_CLOSURE, args);
1292 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001293}
1294
1295static int
1296compiler_decorators(struct compiler *c, asdl_seq* decos)
1297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 if (!decos)
1301 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1304 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1305 }
1306 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307}
1308
1309static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001310compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 asdl_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 int i, default_count = 0;
1314 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1315 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1316 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1317 if (default_) {
1318 ADDOP_O(c, LOAD_CONST, arg->arg, consts);
1319 if (!compiler_visit_expr(c, default_)) {
1320 return -1;
1321 }
1322 default_count++;
1323 }
1324 }
1325 return default_count;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001326}
1327
1328static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001329compiler_visit_argannotation(struct compiler *c, identifier id,
1330 expr_ty annotation, PyObject *names)
1331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 if (annotation) {
1333 VISIT(c, expr, annotation);
1334 if (PyList_Append(names, id))
1335 return -1;
1336 }
1337 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001338}
1339
1340static int
1341compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1342 PyObject *names)
1343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 int i, error;
1345 for (i = 0; i < asdl_seq_LEN(args); i++) {
1346 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1347 error = compiler_visit_argannotation(
1348 c,
1349 arg->arg,
1350 arg->annotation,
1351 names);
1352 if (error)
1353 return error;
1354 }
1355 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001356}
1357
1358static int
1359compiler_visit_annotations(struct compiler *c, arguments_ty args,
1360 expr_ty returns)
1361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 /* Push arg annotations and a list of the argument names. Return the #
1363 of items pushed. The expressions are evaluated out-of-order wrt the
1364 source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00001365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1367 */
1368 static identifier return_str;
1369 PyObject *names;
1370 int len;
1371 names = PyList_New(0);
1372 if (!names)
1373 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 if (compiler_visit_argannotations(c, args->args, names))
1376 goto error;
1377 if (args->varargannotation &&
1378 compiler_visit_argannotation(c, args->vararg,
1379 args->varargannotation, names))
1380 goto error;
1381 if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1382 goto error;
1383 if (args->kwargannotation &&
1384 compiler_visit_argannotation(c, args->kwarg,
1385 args->kwargannotation, names))
1386 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 if (!return_str) {
1389 return_str = PyUnicode_InternFromString("return");
1390 if (!return_str)
1391 goto error;
1392 }
1393 if (compiler_visit_argannotation(c, return_str, returns, names)) {
1394 goto error;
1395 }
1396
1397 len = PyList_GET_SIZE(names);
1398 if (len > 65534) {
1399 /* len must fit in 16 bits, and len is incremented below */
1400 PyErr_SetString(PyExc_SyntaxError,
1401 "too many annotations");
1402 goto error;
1403 }
1404 if (len) {
1405 /* convert names to a tuple and place on stack */
1406 PyObject *elt;
1407 int i;
1408 PyObject *s = PyTuple_New(len);
1409 if (!s)
1410 goto error;
1411 for (i = 0; i < len; i++) {
1412 elt = PyList_GET_ITEM(names, i);
1413 Py_INCREF(elt);
1414 PyTuple_SET_ITEM(s, i, elt);
1415 }
1416 ADDOP_O(c, LOAD_CONST, s, consts);
1417 Py_DECREF(s);
1418 len++; /* include the just-pushed tuple */
1419 }
1420 Py_DECREF(names);
1421 return len;
Neal Norwitzc1505362006-12-28 06:47:50 +00001422
1423error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 Py_DECREF(names);
1425 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001426}
1427
1428static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429compiler_function(struct compiler *c, stmt_ty s)
1430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 PyCodeObject *co;
1432 PyObject *first_const = Py_None;
1433 arguments_ty args = s->v.FunctionDef.args;
1434 expr_ty returns = s->v.FunctionDef.returns;
1435 asdl_seq* decos = s->v.FunctionDef.decorator_list;
1436 stmt_ty st;
1437 int i, n, docstring, kw_default_count = 0, arglength;
1438 int num_annotations;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 assert(s->kind == FunctionDef_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 if (!compiler_decorators(c, decos))
1443 return 0;
1444 if (args->kwonlyargs) {
1445 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1446 args->kw_defaults);
1447 if (res < 0)
1448 return 0;
1449 kw_default_count = res;
1450 }
1451 if (args->defaults)
1452 VISIT_SEQ(c, expr, args->defaults);
1453 num_annotations = compiler_visit_annotations(c, args, returns);
1454 if (num_annotations < 0)
1455 return 0;
1456 assert((num_annotations & 0xFFFF) == num_annotations);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1459 s->lineno))
1460 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
1463 docstring = compiler_isdocstring(st);
1464 if (docstring && Py_OptimizeFlag < 2)
1465 first_const = st->v.Expr.value->v.Str.s;
1466 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1467 compiler_exit_scope(c);
1468 return 0;
1469 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 c->u->u_argcount = asdl_seq_LEN(args->args);
1472 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1473 n = asdl_seq_LEN(s->v.FunctionDef.body);
1474 /* if there was a docstring, we need to skip the first statement */
1475 for (i = docstring; i < n; i++) {
1476 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1477 VISIT_IN_SCOPE(c, stmt, st);
1478 }
1479 co = assemble(c, 1);
1480 compiler_exit_scope(c);
1481 if (co == NULL)
1482 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 arglength = asdl_seq_LEN(args->defaults);
1485 arglength |= kw_default_count << 8;
1486 arglength |= num_annotations << 16;
1487 compiler_make_closure(c, co, arglength);
1488 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 /* decorators */
1491 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1492 ADDOP_I(c, CALL_FUNCTION, 1);
1493 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 return compiler_nameop(c, s->v.FunctionDef.name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001496}
1497
1498static int
1499compiler_class(struct compiler *c, stmt_ty s)
1500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 PyCodeObject *co;
1502 PyObject *str;
1503 int i;
1504 asdl_seq* decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 if (!compiler_decorators(c, decos))
1507 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 /* ultimately generate code for:
1510 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1511 where:
1512 <func> is a function/closure created from the class body;
1513 it has a single argument (__locals__) where the dict
1514 (or MutableSequence) representing the locals is passed
1515 <name> is the class name
1516 <bases> is the positional arguments and *varargs argument
1517 <keywords> is the keyword arguments and **kwds argument
1518 This borrows from compiler_call.
1519 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 /* 1. compile the class body into a code object */
1522 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, s->lineno))
1523 return 0;
1524 /* this block represents what we do in the new scope */
1525 {
1526 /* use the class name for name mangling */
1527 Py_INCREF(s->v.ClassDef.name);
1528 Py_XDECREF(c->u->u_private);
1529 c->u->u_private = s->v.ClassDef.name;
1530 /* force it to have one mandatory argument */
1531 c->u->u_argcount = 1;
1532 /* load the first argument (__locals__) ... */
1533 ADDOP_I(c, LOAD_FAST, 0);
1534 /* ... and store it into f_locals */
1535 ADDOP_IN_SCOPE(c, STORE_LOCALS);
1536 /* load (global) __name__ ... */
1537 str = PyUnicode_InternFromString("__name__");
1538 if (!str || !compiler_nameop(c, str, Load)) {
1539 Py_XDECREF(str);
1540 compiler_exit_scope(c);
1541 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001542 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 Py_DECREF(str);
1544 /* ... and store it as __module__ */
1545 str = PyUnicode_InternFromString("__module__");
1546 if (!str || !compiler_nameop(c, str, Store)) {
1547 Py_XDECREF(str);
1548 compiler_exit_scope(c);
1549 return 0;
1550 }
1551 Py_DECREF(str);
1552 /* compile the body proper */
1553 if (!compiler_body(c, s->v.ClassDef.body)) {
1554 compiler_exit_scope(c);
1555 return 0;
1556 }
1557 /* return the (empty) __class__ cell */
1558 str = PyUnicode_InternFromString("__class__");
1559 if (str == NULL) {
1560 compiler_exit_scope(c);
1561 return 0;
1562 }
1563 i = compiler_lookup_arg(c->u->u_cellvars, str);
1564 Py_DECREF(str);
1565 if (i == -1) {
1566 /* This happens when nobody references the cell */
1567 PyErr_Clear();
1568 /* Return None */
1569 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1570 }
1571 else {
1572 /* Return the cell where to store __class__ */
1573 ADDOP_I(c, LOAD_CLOSURE, i);
1574 }
1575 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1576 /* create the code object */
1577 co = assemble(c, 1);
1578 }
1579 /* leave the new scope */
1580 compiler_exit_scope(c);
1581 if (co == NULL)
1582 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 /* 2. load the 'build_class' function */
1585 ADDOP(c, LOAD_BUILD_CLASS);
1586
1587 /* 3. load a function (or closure) made from the code object */
1588 compiler_make_closure(c, co, 0);
1589 Py_DECREF(co);
1590
1591 /* 4. load class name */
1592 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1593
1594 /* 5. generate the rest of the code for the call */
1595 if (!compiler_call_helper(c, 2,
1596 s->v.ClassDef.bases,
1597 s->v.ClassDef.keywords,
1598 s->v.ClassDef.starargs,
1599 s->v.ClassDef.kwargs))
1600 return 0;
1601
1602 /* 6. apply decorators */
1603 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1604 ADDOP_I(c, CALL_FUNCTION, 1);
1605 }
1606
1607 /* 7. store into <name> */
1608 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1609 return 0;
1610 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001611}
1612
1613static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001614compiler_ifexp(struct compiler *c, expr_ty e)
1615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 basicblock *end, *next;
1617
1618 assert(e->kind == IfExp_kind);
1619 end = compiler_new_block(c);
1620 if (end == NULL)
1621 return 0;
1622 next = compiler_new_block(c);
1623 if (next == NULL)
1624 return 0;
1625 VISIT(c, expr, e->v.IfExp.test);
1626 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1627 VISIT(c, expr, e->v.IfExp.body);
1628 ADDOP_JREL(c, JUMP_FORWARD, end);
1629 compiler_use_next_block(c, next);
1630 VISIT(c, expr, e->v.IfExp.orelse);
1631 compiler_use_next_block(c, end);
1632 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001633}
1634
1635static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636compiler_lambda(struct compiler *c, expr_ty e)
1637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 PyCodeObject *co;
1639 static identifier name;
1640 int kw_default_count = 0, arglength;
1641 arguments_ty args = e->v.Lambda.args;
1642 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 if (!name) {
1645 name = PyUnicode_InternFromString("<lambda>");
1646 if (!name)
1647 return 0;
1648 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 if (args->kwonlyargs) {
1651 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1652 args->kw_defaults);
1653 if (res < 0) return 0;
1654 kw_default_count = res;
1655 }
1656 if (args->defaults)
1657 VISIT_SEQ(c, expr, args->defaults);
1658 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1659 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 /* Make None the first constant, so the lambda can't have a
1662 docstring. */
1663 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0)
1664 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 c->u->u_argcount = asdl_seq_LEN(args->args);
1667 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1668 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1669 if (c->u->u_ste->ste_generator) {
1670 ADDOP_IN_SCOPE(c, POP_TOP);
1671 }
1672 else {
1673 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1674 }
1675 co = assemble(c, 1);
1676 compiler_exit_scope(c);
1677 if (co == NULL)
1678 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 arglength = asdl_seq_LEN(args->defaults);
1681 arglength |= kw_default_count << 8;
1682 compiler_make_closure(c, co, arglength);
1683 Py_DECREF(co);
1684
1685 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001686}
1687
1688static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689compiler_if(struct compiler *c, stmt_ty s)
1690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 basicblock *end, *next;
1692 int constant;
1693 assert(s->kind == If_kind);
1694 end = compiler_new_block(c);
1695 if (end == NULL)
1696 return 0;
1697
1698 constant = expr_constant(s->v.If.test);
1699 /* constant = 0: "if 0"
1700 * constant = 1: "if 1", "if 2", ...
1701 * constant = -1: rest */
1702 if (constant == 0) {
1703 if (s->v.If.orelse)
1704 VISIT_SEQ(c, stmt, s->v.If.orelse);
1705 } else if (constant == 1) {
1706 VISIT_SEQ(c, stmt, s->v.If.body);
1707 } else {
1708 if (s->v.If.orelse) {
1709 next = compiler_new_block(c);
1710 if (next == NULL)
1711 return 0;
1712 }
1713 else
1714 next = end;
1715 VISIT(c, expr, s->v.If.test);
1716 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1717 VISIT_SEQ(c, stmt, s->v.If.body);
1718 ADDOP_JREL(c, JUMP_FORWARD, end);
1719 if (s->v.If.orelse) {
1720 compiler_use_next_block(c, next);
1721 VISIT_SEQ(c, stmt, s->v.If.orelse);
1722 }
1723 }
1724 compiler_use_next_block(c, end);
1725 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726}
1727
1728static int
1729compiler_for(struct compiler *c, stmt_ty s)
1730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 basicblock *start, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 start = compiler_new_block(c);
1734 cleanup = compiler_new_block(c);
1735 end = compiler_new_block(c);
1736 if (start == NULL || end == NULL || cleanup == NULL)
1737 return 0;
1738 ADDOP_JREL(c, SETUP_LOOP, end);
1739 if (!compiler_push_fblock(c, LOOP, start))
1740 return 0;
1741 VISIT(c, expr, s->v.For.iter);
1742 ADDOP(c, GET_ITER);
1743 compiler_use_next_block(c, start);
1744 ADDOP_JREL(c, FOR_ITER, cleanup);
1745 VISIT(c, expr, s->v.For.target);
1746 VISIT_SEQ(c, stmt, s->v.For.body);
1747 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1748 compiler_use_next_block(c, cleanup);
1749 ADDOP(c, POP_BLOCK);
1750 compiler_pop_fblock(c, LOOP, start);
1751 VISIT_SEQ(c, stmt, s->v.For.orelse);
1752 compiler_use_next_block(c, end);
1753 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001754}
1755
1756static int
1757compiler_while(struct compiler *c, stmt_ty s)
1758{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 basicblock *loop, *orelse, *end, *anchor = NULL;
1760 int constant = expr_constant(s->v.While.test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 if (constant == 0) {
1763 if (s->v.While.orelse)
1764 VISIT_SEQ(c, stmt, s->v.While.orelse);
1765 return 1;
1766 }
1767 loop = compiler_new_block(c);
1768 end = compiler_new_block(c);
1769 if (constant == -1) {
1770 anchor = compiler_new_block(c);
1771 if (anchor == NULL)
1772 return 0;
1773 }
1774 if (loop == NULL || end == NULL)
1775 return 0;
1776 if (s->v.While.orelse) {
1777 orelse = compiler_new_block(c);
1778 if (orelse == NULL)
1779 return 0;
1780 }
1781 else
1782 orelse = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 ADDOP_JREL(c, SETUP_LOOP, end);
1785 compiler_use_next_block(c, loop);
1786 if (!compiler_push_fblock(c, LOOP, loop))
1787 return 0;
1788 if (constant == -1) {
1789 VISIT(c, expr, s->v.While.test);
1790 ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);
1791 }
1792 VISIT_SEQ(c, stmt, s->v.While.body);
1793 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 /* XXX should the two POP instructions be in a separate block
1796 if there is no else clause ?
1797 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 if (constant == -1) {
1800 compiler_use_next_block(c, anchor);
1801 ADDOP(c, POP_BLOCK);
1802 }
1803 compiler_pop_fblock(c, LOOP, loop);
1804 if (orelse != NULL) /* what if orelse is just pass? */
1805 VISIT_SEQ(c, stmt, s->v.While.orelse);
1806 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809}
1810
1811static int
1812compiler_continue(struct compiler *c)
1813{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
1815 static const char IN_FINALLY_ERROR_MSG[] =
1816 "'continue' not supported inside 'finally' clause";
1817 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 if (!c->u->u_nfblocks)
1820 return compiler_error(c, LOOP_ERROR_MSG);
1821 i = c->u->u_nfblocks - 1;
1822 switch (c->u->u_fblock[i].fb_type) {
1823 case LOOP:
1824 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1825 break;
1826 case EXCEPT:
1827 case FINALLY_TRY:
1828 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1829 /* Prevent continue anywhere under a finally
1830 even if hidden in a sub-try or except. */
1831 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1832 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1833 }
1834 if (i == -1)
1835 return compiler_error(c, LOOP_ERROR_MSG);
1836 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1837 break;
1838 case FINALLY_END:
1839 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1840 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001843}
1844
1845/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846
1847 SETUP_FINALLY L
1848 <code for body>
1849 POP_BLOCK
1850 LOAD_CONST <None>
1851 L: <code for finalbody>
1852 END_FINALLY
1853
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001854 The special instructions use the block stack. Each block
1855 stack entry contains the instruction that created it (here
1856 SETUP_FINALLY), the level of the value stack at the time the
1857 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001859 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 Pushes the current value stack level and the label
1861 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001862 POP_BLOCK:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 Pops en entry from the block stack, and pops the value
1864 stack until its level is the same as indicated on the
1865 block stack. (The label is ignored.)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001866 END_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 Pops a variable number of entries from the *value* stack
1868 and re-raises the exception they specify. The number of
1869 entries popped depends on the (pseudo) exception type.
1870
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001871 The block stack is unwound when an exception is raised:
1872 when a SETUP_FINALLY entry is found, the exception is pushed
1873 onto the value stack (and the exception condition is cleared),
1874 and the interpreter jumps to the label gotten from the block
1875 stack.
1876*/
1877
1878static int
1879compiler_try_finally(struct compiler *c, stmt_ty s)
1880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 basicblock *body, *end;
1882 body = compiler_new_block(c);
1883 end = compiler_new_block(c);
1884 if (body == NULL || end == NULL)
1885 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 ADDOP_JREL(c, SETUP_FINALLY, end);
1888 compiler_use_next_block(c, body);
1889 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1890 return 0;
1891 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
1892 ADDOP(c, POP_BLOCK);
1893 compiler_pop_fblock(c, FINALLY_TRY, body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1896 compiler_use_next_block(c, end);
1897 if (!compiler_push_fblock(c, FINALLY_END, end))
1898 return 0;
1899 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
1900 ADDOP(c, END_FINALLY);
1901 compiler_pop_fblock(c, FINALLY_END, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001904}
1905
1906/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001907 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908 (The contents of the value stack is shown in [], with the top
1909 at the right; 'tb' is trace-back info, 'val' the exception's
1910 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911
1912 Value stack Label Instruction Argument
1913 [] SETUP_EXCEPT L1
1914 [] <code for S>
1915 [] POP_BLOCK
1916 [] JUMP_FORWARD L0
1917
1918 [tb, val, exc] L1: DUP )
1919 [tb, val, exc, exc] <evaluate E1> )
1920 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1921 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
1922 [tb, val, exc] POP
1923 [tb, val] <assign to V1> (or POP if no V1)
1924 [tb] POP
1925 [] <code for S1>
1926 JUMP_FORWARD L0
1927
1928 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001929 .............................etc.......................
1930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
1932
1933 [] L0: <next statement>
1934
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001935 Of course, parts are not generated if Vi or Ei is not present.
1936*/
1937static int
1938compiler_try_except(struct compiler *c, stmt_ty s)
1939{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 basicblock *body, *orelse, *except, *end;
1941 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 body = compiler_new_block(c);
1944 except = compiler_new_block(c);
1945 orelse = compiler_new_block(c);
1946 end = compiler_new_block(c);
1947 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1948 return 0;
1949 ADDOP_JREL(c, SETUP_EXCEPT, except);
1950 compiler_use_next_block(c, body);
1951 if (!compiler_push_fblock(c, EXCEPT, body))
1952 return 0;
1953 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
1954 ADDOP(c, POP_BLOCK);
1955 compiler_pop_fblock(c, EXCEPT, body);
1956 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1957 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1958 compiler_use_next_block(c, except);
1959 for (i = 0; i < n; i++) {
1960 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1961 s->v.TryExcept.handlers, i);
1962 if (!handler->v.ExceptHandler.type && i < n-1)
1963 return compiler_error(c, "default 'except:' must be last");
1964 c->u->u_lineno_set = 0;
1965 c->u->u_lineno = handler->lineno;
1966 except = compiler_new_block(c);
1967 if (except == NULL)
1968 return 0;
1969 if (handler->v.ExceptHandler.type) {
1970 ADDOP(c, DUP_TOP);
1971 VISIT(c, expr, handler->v.ExceptHandler.type);
1972 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1973 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
1974 }
1975 ADDOP(c, POP_TOP);
1976 if (handler->v.ExceptHandler.name) {
1977 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00001978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 cleanup_end = compiler_new_block(c);
1980 cleanup_body = compiler_new_block(c);
1981 if(!(cleanup_end || cleanup_body))
1982 return 0;
Guido van Rossumb940e112007-01-10 16:19:56 +00001983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
1985 ADDOP(c, POP_TOP);
1986
1987 /*
1988 try:
1989 # body
1990 except type as name:
1991 try:
1992 # body
1993 finally:
1994 name = None
1995 del name
1996 */
1997
1998 /* second try: */
1999 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2000 compiler_use_next_block(c, cleanup_body);
2001 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2002 return 0;
2003
2004 /* second # body */
2005 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2006 ADDOP(c, POP_BLOCK);
2007 ADDOP(c, POP_EXCEPT);
2008 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
2009
2010 /* finally: */
2011 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2012 compiler_use_next_block(c, cleanup_end);
2013 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
2014 return 0;
2015
2016 /* name = None */
2017 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2018 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
2019
2020 /* del name */
2021 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
2022
2023 ADDOP(c, END_FINALLY);
2024 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
2025 }
2026 else {
2027 basicblock *cleanup_body;
2028
2029 cleanup_body = compiler_new_block(c);
2030 if(!cleanup_body)
2031 return 0;
2032
Guido van Rossumb940e112007-01-10 16:19:56 +00002033 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 ADDOP(c, POP_TOP);
2035 compiler_use_next_block(c, cleanup_body);
2036 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2037 return 0;
2038 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2039 ADDOP(c, POP_EXCEPT);
2040 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
2041 }
2042 ADDOP_JREL(c, JUMP_FORWARD, end);
2043 compiler_use_next_block(c, except);
2044 }
2045 ADDOP(c, END_FINALLY);
2046 compiler_use_next_block(c, orelse);
2047 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2048 compiler_use_next_block(c, end);
2049 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002050}
2051
2052static int
2053compiler_import_as(struct compiler *c, identifier name, identifier asname)
2054{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 /* The IMPORT_NAME opcode was already generated. This function
2056 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 If there is a dot in name, we need to split it and emit a
2059 LOAD_ATTR for each name.
2060 */
2061 const Py_UNICODE *src = PyUnicode_AS_UNICODE(name);
2062 const Py_UNICODE *dot = Py_UNICODE_strchr(src, '.');
2063 if (dot) {
2064 /* Consume the base module name to get the first attribute */
2065 src = dot + 1;
2066 while (dot) {
2067 /* NB src is only defined when dot != NULL */
2068 PyObject *attr;
2069 dot = Py_UNICODE_strchr(src, '.');
2070 attr = PyUnicode_FromUnicode(src,
2071 dot ? dot - src : Py_UNICODE_strlen(src));
2072 if (!attr)
2073 return -1;
2074 ADDOP_O(c, LOAD_ATTR, attr, names);
2075 Py_DECREF(attr);
2076 src = dot + 1;
2077 }
2078 }
2079 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002080}
2081
2082static int
2083compiler_import(struct compiler *c, stmt_ty s)
2084{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 /* The Import node stores a module name like a.b.c as a single
2086 string. This is convenient for all cases except
2087 import a.b.c as d
2088 where we need to parse that string to extract the individual
2089 module names.
2090 XXX Perhaps change the representation to make this case simpler?
2091 */
2092 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 for (i = 0; i < n; i++) {
2095 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
2096 int r;
2097 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 level = PyLong_FromLong(0);
2100 if (level == NULL)
2101 return 0;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 ADDOP_O(c, LOAD_CONST, level, consts);
2104 Py_DECREF(level);
2105 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2106 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 if (alias->asname) {
2109 r = compiler_import_as(c, alias->name, alias->asname);
2110 if (!r)
2111 return r;
2112 }
2113 else {
2114 identifier tmp = alias->name;
2115 const Py_UNICODE *base = PyUnicode_AS_UNICODE(alias->name);
2116 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
2117 if (dot)
2118 tmp = PyUnicode_FromUnicode(base,
2119 dot - base);
2120 r = compiler_nameop(c, tmp, Store);
2121 if (dot) {
2122 Py_DECREF(tmp);
2123 }
2124 if (!r)
2125 return r;
2126 }
2127 }
2128 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002129}
2130
2131static int
2132compiler_from_import(struct compiler *c, stmt_ty s)
2133{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002134 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 PyObject *names = PyTuple_New(n);
2137 PyObject *level;
2138 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00002139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 if (!empty_string) {
2141 empty_string = PyUnicode_FromString("");
2142 if (!empty_string)
2143 return 0;
2144 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 if (!names)
2147 return 0;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002149 level = PyLong_FromLong(s->v.ImportFrom.level);
2150 if (!level) {
2151 Py_DECREF(names);
2152 return 0;
2153 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 /* build up the names */
2156 for (i = 0; i < n; i++) {
2157 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2158 Py_INCREF(alias->name);
2159 PyTuple_SET_ITEM(names, i, alias->name);
2160 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
2163 !PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module, "__future__")) {
2164 Py_DECREF(level);
2165 Py_DECREF(names);
2166 return compiler_error(c, "from __future__ imports must occur "
2167 "at the beginning of the file");
2168 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 ADDOP_O(c, LOAD_CONST, level, consts);
2171 Py_DECREF(level);
2172 ADDOP_O(c, LOAD_CONST, names, consts);
2173 Py_DECREF(names);
2174 if (s->v.ImportFrom.module) {
2175 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2176 }
2177 else {
2178 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
2179 }
2180 for (i = 0; i < n; i++) {
2181 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2182 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 if (i == 0 && *PyUnicode_AS_UNICODE(alias->name) == '*') {
2185 assert(n == 1);
2186 ADDOP(c, IMPORT_STAR);
2187 return 1;
2188 }
2189
2190 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2191 store_name = alias->name;
2192 if (alias->asname)
2193 store_name = alias->asname;
2194
2195 if (!compiler_nameop(c, store_name, Store)) {
2196 Py_DECREF(names);
2197 return 0;
2198 }
2199 }
2200 /* remove imported module */
2201 ADDOP(c, POP_TOP);
2202 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002203}
2204
2205static int
2206compiler_assert(struct compiler *c, stmt_ty s)
2207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 static PyObject *assertion_error = NULL;
2209 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 if (Py_OptimizeFlag)
2212 return 1;
2213 if (assertion_error == NULL) {
2214 assertion_error = PyUnicode_InternFromString("AssertionError");
2215 if (assertion_error == NULL)
2216 return 0;
2217 }
2218 if (s->v.Assert.test->kind == Tuple_kind &&
2219 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2220 const char* msg =
2221 "assertion is always true, perhaps remove parentheses?";
2222 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,
2223 c->u->u_lineno, NULL, NULL) == -1)
2224 return 0;
2225 }
2226 VISIT(c, expr, s->v.Assert.test);
2227 end = compiler_new_block(c);
2228 if (end == NULL)
2229 return 0;
2230 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
2231 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2232 if (s->v.Assert.msg) {
2233 VISIT(c, expr, s->v.Assert.msg);
2234 ADDOP_I(c, CALL_FUNCTION, 1);
2235 }
2236 ADDOP_I(c, RAISE_VARARGS, 1);
2237 compiler_use_next_block(c, end);
2238 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002239}
2240
2241static int
2242compiler_visit_stmt(struct compiler *c, stmt_ty s)
2243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002246 /* Always assign a lineno to the next instruction for a stmt. */
2247 c->u->u_lineno = s->lineno;
2248 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 switch (s->kind) {
2251 case FunctionDef_kind:
2252 return compiler_function(c, s);
2253 case ClassDef_kind:
2254 return compiler_class(c, s);
2255 case Return_kind:
2256 if (c->u->u_ste->ste_type != FunctionBlock)
2257 return compiler_error(c, "'return' outside function");
2258 if (s->v.Return.value) {
2259 VISIT(c, expr, s->v.Return.value);
2260 }
2261 else
2262 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2263 ADDOP(c, RETURN_VALUE);
2264 break;
2265 case Delete_kind:
2266 VISIT_SEQ(c, expr, s->v.Delete.targets)
2267 break;
2268 case Assign_kind:
2269 n = asdl_seq_LEN(s->v.Assign.targets);
2270 VISIT(c, expr, s->v.Assign.value);
2271 for (i = 0; i < n; i++) {
2272 if (i < n - 1)
2273 ADDOP(c, DUP_TOP);
2274 VISIT(c, expr,
2275 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2276 }
2277 break;
2278 case AugAssign_kind:
2279 return compiler_augassign(c, s);
2280 case For_kind:
2281 return compiler_for(c, s);
2282 case While_kind:
2283 return compiler_while(c, s);
2284 case If_kind:
2285 return compiler_if(c, s);
2286 case Raise_kind:
2287 n = 0;
2288 if (s->v.Raise.exc) {
2289 VISIT(c, expr, s->v.Raise.exc);
2290 n++;
2291 if (s->v.Raise.cause) {
2292 VISIT(c, expr, s->v.Raise.cause);
2293 n++;
2294 }
2295 }
2296 ADDOP_I(c, RAISE_VARARGS, n);
2297 break;
2298 case TryExcept_kind:
2299 return compiler_try_except(c, s);
2300 case TryFinally_kind:
2301 return compiler_try_finally(c, s);
2302 case Assert_kind:
2303 return compiler_assert(c, s);
2304 case Import_kind:
2305 return compiler_import(c, s);
2306 case ImportFrom_kind:
2307 return compiler_from_import(c, s);
2308 case Global_kind:
2309 case Nonlocal_kind:
2310 break;
2311 case Expr_kind:
2312 if (c->c_interactive && c->c_nestlevel <= 1) {
2313 VISIT(c, expr, s->v.Expr.value);
2314 ADDOP(c, PRINT_EXPR);
2315 }
2316 else if (s->v.Expr.value->kind != Str_kind &&
2317 s->v.Expr.value->kind != Num_kind) {
2318 VISIT(c, expr, s->v.Expr.value);
2319 ADDOP(c, POP_TOP);
2320 }
2321 break;
2322 case Pass_kind:
2323 break;
2324 case Break_kind:
2325 if (!compiler_in_loop(c))
2326 return compiler_error(c, "'break' outside loop");
2327 ADDOP(c, BREAK_LOOP);
2328 break;
2329 case Continue_kind:
2330 return compiler_continue(c);
2331 case With_kind:
2332 return compiler_with(c, s);
2333 }
2334 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002335}
2336
2337static int
2338unaryop(unaryop_ty op)
2339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 switch (op) {
2341 case Invert:
2342 return UNARY_INVERT;
2343 case Not:
2344 return UNARY_NOT;
2345 case UAdd:
2346 return UNARY_POSITIVE;
2347 case USub:
2348 return UNARY_NEGATIVE;
2349 default:
2350 PyErr_Format(PyExc_SystemError,
2351 "unary op %d should not be possible", op);
2352 return 0;
2353 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002354}
2355
2356static int
2357binop(struct compiler *c, operator_ty op)
2358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 switch (op) {
2360 case Add:
2361 return BINARY_ADD;
2362 case Sub:
2363 return BINARY_SUBTRACT;
2364 case Mult:
2365 return BINARY_MULTIPLY;
2366 case Div:
2367 return BINARY_TRUE_DIVIDE;
2368 case Mod:
2369 return BINARY_MODULO;
2370 case Pow:
2371 return BINARY_POWER;
2372 case LShift:
2373 return BINARY_LSHIFT;
2374 case RShift:
2375 return BINARY_RSHIFT;
2376 case BitOr:
2377 return BINARY_OR;
2378 case BitXor:
2379 return BINARY_XOR;
2380 case BitAnd:
2381 return BINARY_AND;
2382 case FloorDiv:
2383 return BINARY_FLOOR_DIVIDE;
2384 default:
2385 PyErr_Format(PyExc_SystemError,
2386 "binary op %d should not be possible", op);
2387 return 0;
2388 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002389}
2390
2391static int
2392cmpop(cmpop_ty op)
2393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 switch (op) {
2395 case Eq:
2396 return PyCmp_EQ;
2397 case NotEq:
2398 return PyCmp_NE;
2399 case Lt:
2400 return PyCmp_LT;
2401 case LtE:
2402 return PyCmp_LE;
2403 case Gt:
2404 return PyCmp_GT;
2405 case GtE:
2406 return PyCmp_GE;
2407 case Is:
2408 return PyCmp_IS;
2409 case IsNot:
2410 return PyCmp_IS_NOT;
2411 case In:
2412 return PyCmp_IN;
2413 case NotIn:
2414 return PyCmp_NOT_IN;
2415 default:
2416 return PyCmp_BAD;
2417 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002418}
2419
2420static int
2421inplace_binop(struct compiler *c, operator_ty op)
2422{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002423 switch (op) {
2424 case Add:
2425 return INPLACE_ADD;
2426 case Sub:
2427 return INPLACE_SUBTRACT;
2428 case Mult:
2429 return INPLACE_MULTIPLY;
2430 case Div:
2431 return INPLACE_TRUE_DIVIDE;
2432 case Mod:
2433 return INPLACE_MODULO;
2434 case Pow:
2435 return INPLACE_POWER;
2436 case LShift:
2437 return INPLACE_LSHIFT;
2438 case RShift:
2439 return INPLACE_RSHIFT;
2440 case BitOr:
2441 return INPLACE_OR;
2442 case BitXor:
2443 return INPLACE_XOR;
2444 case BitAnd:
2445 return INPLACE_AND;
2446 case FloorDiv:
2447 return INPLACE_FLOOR_DIVIDE;
2448 default:
2449 PyErr_Format(PyExc_SystemError,
2450 "inplace binary op %d should not be possible", op);
2451 return 0;
2452 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002453}
2454
2455static int
2456compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 int op, scope, arg;
2459 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 PyObject *dict = c->u->u_names;
2462 PyObject *mangled;
2463 /* XXX AugStore isn't used anywhere! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 mangled = _Py_Mangle(c->u->u_private, name);
2466 if (!mangled)
2467 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 op = 0;
2470 optype = OP_NAME;
2471 scope = PyST_GetScope(c->u->u_ste, mangled);
2472 switch (scope) {
2473 case FREE:
2474 dict = c->u->u_freevars;
2475 optype = OP_DEREF;
2476 break;
2477 case CELL:
2478 dict = c->u->u_cellvars;
2479 optype = OP_DEREF;
2480 break;
2481 case LOCAL:
2482 if (c->u->u_ste->ste_type == FunctionBlock)
2483 optype = OP_FAST;
2484 break;
2485 case GLOBAL_IMPLICIT:
2486 if (c->u->u_ste->ste_type == FunctionBlock &&
2487 !c->u->u_ste->ste_unoptimized)
2488 optype = OP_GLOBAL;
2489 break;
2490 case GLOBAL_EXPLICIT:
2491 optype = OP_GLOBAL;
2492 break;
2493 default:
2494 /* scope can be 0 */
2495 break;
2496 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002498 /* XXX Leave assert here, but handle __doc__ and the like better */
2499 assert(scope || PyUnicode_AS_UNICODE(name)[0] == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002501 switch (optype) {
2502 case OP_DEREF:
2503 switch (ctx) {
2504 case Load: op = LOAD_DEREF; break;
2505 case Store: op = STORE_DEREF; break;
2506 case AugLoad:
2507 case AugStore:
2508 break;
2509 case Del:
2510 PyErr_Format(PyExc_SyntaxError,
2511 "can not delete variable '%S' referenced "
2512 "in nested scope",
2513 name);
2514 Py_DECREF(mangled);
2515 return 0;
2516 case Param:
2517 default:
2518 PyErr_SetString(PyExc_SystemError,
2519 "param invalid for deref variable");
2520 return 0;
2521 }
2522 break;
2523 case OP_FAST:
2524 switch (ctx) {
2525 case Load: op = LOAD_FAST; break;
2526 case Store: op = STORE_FAST; break;
2527 case Del: op = DELETE_FAST; break;
2528 case AugLoad:
2529 case AugStore:
2530 break;
2531 case Param:
2532 default:
2533 PyErr_SetString(PyExc_SystemError,
2534 "param invalid for local variable");
2535 return 0;
2536 }
2537 ADDOP_O(c, op, mangled, varnames);
2538 Py_DECREF(mangled);
2539 return 1;
2540 case OP_GLOBAL:
2541 switch (ctx) {
2542 case Load: op = LOAD_GLOBAL; break;
2543 case Store: op = STORE_GLOBAL; break;
2544 case Del: op = DELETE_GLOBAL; break;
2545 case AugLoad:
2546 case AugStore:
2547 break;
2548 case Param:
2549 default:
2550 PyErr_SetString(PyExc_SystemError,
2551 "param invalid for global variable");
2552 return 0;
2553 }
2554 break;
2555 case OP_NAME:
2556 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002557 case Load: op = LOAD_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002558 case Store: op = STORE_NAME; break;
2559 case Del: op = DELETE_NAME; break;
2560 case AugLoad:
2561 case AugStore:
2562 break;
2563 case Param:
2564 default:
2565 PyErr_SetString(PyExc_SystemError,
2566 "param invalid for name variable");
2567 return 0;
2568 }
2569 break;
2570 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 assert(op);
2573 arg = compiler_add_o(c, dict, mangled);
2574 Py_DECREF(mangled);
2575 if (arg < 0)
2576 return 0;
2577 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578}
2579
2580static int
2581compiler_boolop(struct compiler *c, expr_ty e)
2582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002583 basicblock *end;
2584 int jumpi, i, n;
2585 asdl_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002587 assert(e->kind == BoolOp_kind);
2588 if (e->v.BoolOp.op == And)
2589 jumpi = JUMP_IF_FALSE_OR_POP;
2590 else
2591 jumpi = JUMP_IF_TRUE_OR_POP;
2592 end = compiler_new_block(c);
2593 if (end == NULL)
2594 return 0;
2595 s = e->v.BoolOp.values;
2596 n = asdl_seq_LEN(s) - 1;
2597 assert(n >= 0);
2598 for (i = 0; i < n; ++i) {
2599 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
2600 ADDOP_JABS(c, jumpi, end);
2601 }
2602 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
2603 compiler_use_next_block(c, end);
2604 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002605}
2606
2607static int
2608compiler_list(struct compiler *c, expr_ty e)
2609{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002610 int n = asdl_seq_LEN(e->v.List.elts);
2611 if (e->v.List.ctx == Store) {
2612 int i, seen_star = 0;
2613 for (i = 0; i < n; i++) {
2614 expr_ty elt = asdl_seq_GET(e->v.List.elts, i);
2615 if (elt->kind == Starred_kind && !seen_star) {
2616 if ((i >= (1 << 8)) ||
2617 (n-i-1 >= (INT_MAX >> 8)))
2618 return compiler_error(c,
2619 "too many expressions in "
2620 "star-unpacking assignment");
2621 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2622 seen_star = 1;
2623 asdl_seq_SET(e->v.List.elts, i, elt->v.Starred.value);
2624 } else if (elt->kind == Starred_kind) {
2625 return compiler_error(c,
2626 "two starred expressions in assignment");
2627 }
2628 }
2629 if (!seen_star) {
2630 ADDOP_I(c, UNPACK_SEQUENCE, n);
2631 }
2632 }
2633 VISIT_SEQ(c, expr, e->v.List.elts);
2634 if (e->v.List.ctx == Load) {
2635 ADDOP_I(c, BUILD_LIST, n);
2636 }
2637 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002638}
2639
2640static int
2641compiler_tuple(struct compiler *c, expr_ty e)
2642{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002643 int n = asdl_seq_LEN(e->v.Tuple.elts);
2644 if (e->v.Tuple.ctx == Store) {
2645 int i, seen_star = 0;
2646 for (i = 0; i < n; i++) {
2647 expr_ty elt = asdl_seq_GET(e->v.Tuple.elts, i);
2648 if (elt->kind == Starred_kind && !seen_star) {
2649 if ((i >= (1 << 8)) ||
2650 (n-i-1 >= (INT_MAX >> 8)))
2651 return compiler_error(c,
2652 "too many expressions in "
2653 "star-unpacking assignment");
2654 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2655 seen_star = 1;
2656 asdl_seq_SET(e->v.Tuple.elts, i, elt->v.Starred.value);
2657 } else if (elt->kind == Starred_kind) {
2658 return compiler_error(c,
2659 "two starred expressions in assignment");
2660 }
2661 }
2662 if (!seen_star) {
2663 ADDOP_I(c, UNPACK_SEQUENCE, n);
2664 }
2665 }
2666 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2667 if (e->v.Tuple.ctx == Load) {
2668 ADDOP_I(c, BUILD_TUPLE, n);
2669 }
2670 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002671}
2672
2673static int
2674compiler_compare(struct compiler *c, expr_ty e)
2675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002676 int i, n;
2677 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2680 VISIT(c, expr, e->v.Compare.left);
2681 n = asdl_seq_LEN(e->v.Compare.ops);
2682 assert(n > 0);
2683 if (n > 1) {
2684 cleanup = compiler_new_block(c);
2685 if (cleanup == NULL)
2686 return 0;
2687 VISIT(c, expr,
2688 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
2689 }
2690 for (i = 1; i < n; i++) {
2691 ADDOP(c, DUP_TOP);
2692 ADDOP(c, ROT_THREE);
2693 ADDOP_I(c, COMPARE_OP,
2694 cmpop((cmpop_ty)(asdl_seq_GET(
2695 e->v.Compare.ops, i - 1))));
2696 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
2697 NEXT_BLOCK(c);
2698 if (i < (n - 1))
2699 VISIT(c, expr,
2700 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2701 }
2702 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
2703 ADDOP_I(c, COMPARE_OP,
2704 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
2705 if (n > 1) {
2706 basicblock *end = compiler_new_block(c);
2707 if (end == NULL)
2708 return 0;
2709 ADDOP_JREL(c, JUMP_FORWARD, end);
2710 compiler_use_next_block(c, cleanup);
2711 ADDOP(c, ROT_TWO);
2712 ADDOP(c, POP_TOP);
2713 compiler_use_next_block(c, end);
2714 }
2715 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002716}
2717
2718static int
2719compiler_call(struct compiler *c, expr_ty e)
2720{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002721 VISIT(c, expr, e->v.Call.func);
2722 return compiler_call_helper(c, 0,
2723 e->v.Call.args,
2724 e->v.Call.keywords,
2725 e->v.Call.starargs,
2726 e->v.Call.kwargs);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002727}
2728
2729/* shared code between compiler_call and compiler_class */
2730static int
2731compiler_call_helper(struct compiler *c,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002732 int n, /* Args already pushed */
2733 asdl_seq *args,
2734 asdl_seq *keywords,
2735 expr_ty starargs,
2736 expr_ty kwargs)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002737{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002738 int code = 0;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 n += asdl_seq_LEN(args);
2741 VISIT_SEQ(c, expr, args);
2742 if (keywords) {
2743 VISIT_SEQ(c, keyword, keywords);
2744 n |= asdl_seq_LEN(keywords) << 8;
2745 }
2746 if (starargs) {
2747 VISIT(c, expr, starargs);
2748 code |= 1;
2749 }
2750 if (kwargs) {
2751 VISIT(c, expr, kwargs);
2752 code |= 2;
2753 }
2754 switch (code) {
2755 case 0:
2756 ADDOP_I(c, CALL_FUNCTION, n);
2757 break;
2758 case 1:
2759 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2760 break;
2761 case 2:
2762 ADDOP_I(c, CALL_FUNCTION_KW, n);
2763 break;
2764 case 3:
2765 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2766 break;
2767 }
2768 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002769}
2770
Nick Coghlan650f0d02007-04-15 12:05:43 +00002771
2772/* List and set comprehensions and generator expressions work by creating a
2773 nested function to perform the actual iteration. This means that the
2774 iteration variables don't leak into the current scope.
2775 The defined function is called immediately following its definition, with the
2776 result of that call being the result of the expression.
2777 The LC/SC version returns the populated container, while the GE version is
2778 flagged in symtable.c as a generator, so it returns the generator object
2779 when the function is called.
2780 This code *knows* that the loop cannot contain break, continue, or return,
2781 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2782
2783 Possible cleanups:
2784 - iterate over the generator sequence instead of using recursion
2785*/
2786
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002787static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788compiler_comprehension_generator(struct compiler *c,
2789 asdl_seq *generators, int gen_index,
2790 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 /* generate code for the iterator, then each of the ifs,
2793 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002795 comprehension_ty gen;
2796 basicblock *start, *anchor, *skip, *if_cleanup;
2797 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 start = compiler_new_block(c);
2800 skip = compiler_new_block(c);
2801 if_cleanup = compiler_new_block(c);
2802 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2805 anchor == NULL)
2806 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 if (gen_index == 0) {
2811 /* Receive outermost iter as an implicit argument */
2812 c->u->u_argcount = 1;
2813 ADDOP_I(c, LOAD_FAST, 0);
2814 }
2815 else {
2816 /* Sub-iter - calculate on the fly */
2817 VISIT(c, expr, gen->iter);
2818 ADDOP(c, GET_ITER);
2819 }
2820 compiler_use_next_block(c, start);
2821 ADDOP_JREL(c, FOR_ITER, anchor);
2822 NEXT_BLOCK(c);
2823 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002825 /* XXX this needs to be cleaned up...a lot! */
2826 n = asdl_seq_LEN(gen->ifs);
2827 for (i = 0; i < n; i++) {
2828 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
2829 VISIT(c, expr, e);
2830 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
2831 NEXT_BLOCK(c);
2832 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 if (++gen_index < asdl_seq_LEN(generators))
2835 if (!compiler_comprehension_generator(c,
2836 generators, gen_index,
2837 elt, val, type))
2838 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 /* only append after the last for generator */
2841 if (gen_index >= asdl_seq_LEN(generators)) {
2842 /* comprehension specific code */
2843 switch (type) {
2844 case COMP_GENEXP:
2845 VISIT(c, expr, elt);
2846 ADDOP(c, YIELD_VALUE);
2847 ADDOP(c, POP_TOP);
2848 break;
2849 case COMP_LISTCOMP:
2850 VISIT(c, expr, elt);
2851 ADDOP_I(c, LIST_APPEND, gen_index + 1);
2852 break;
2853 case COMP_SETCOMP:
2854 VISIT(c, expr, elt);
2855 ADDOP_I(c, SET_ADD, gen_index + 1);
2856 break;
2857 case COMP_DICTCOMP:
2858 /* With 'd[k] = v', v is evaluated before k, so we do
2859 the same. */
2860 VISIT(c, expr, val);
2861 VISIT(c, expr, elt);
2862 ADDOP_I(c, MAP_ADD, gen_index + 1);
2863 break;
2864 default:
2865 return 0;
2866 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 compiler_use_next_block(c, skip);
2869 }
2870 compiler_use_next_block(c, if_cleanup);
2871 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2872 compiler_use_next_block(c, anchor);
2873
2874 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002875}
2876
2877static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002878compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 asdl_seq *generators, expr_ty elt, expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 PyCodeObject *co = NULL;
2882 expr_ty outermost_iter;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 outermost_iter = ((comprehension_ty)
2885 asdl_seq_GET(generators, 0))->iter;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2888 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002890 if (type != COMP_GENEXP) {
2891 int op;
2892 switch (type) {
2893 case COMP_LISTCOMP:
2894 op = BUILD_LIST;
2895 break;
2896 case COMP_SETCOMP:
2897 op = BUILD_SET;
2898 break;
2899 case COMP_DICTCOMP:
2900 op = BUILD_MAP;
2901 break;
2902 default:
2903 PyErr_Format(PyExc_SystemError,
2904 "unknown comprehension type %d", type);
2905 goto error_in_scope;
2906 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 ADDOP_I(c, op, 0);
2909 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 if (!compiler_comprehension_generator(c, generators, 0, elt,
2912 val, type))
2913 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 if (type != COMP_GENEXP) {
2916 ADDOP(c, RETURN_VALUE);
2917 }
2918
2919 co = assemble(c, 1);
2920 compiler_exit_scope(c);
2921 if (co == NULL)
2922 goto error;
2923
2924 if (!compiler_make_closure(c, co, 0))
2925 goto error;
2926 Py_DECREF(co);
2927
2928 VISIT(c, expr, outermost_iter);
2929 ADDOP(c, GET_ITER);
2930 ADDOP_I(c, CALL_FUNCTION, 1);
2931 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002932error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002933 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002934error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002935 Py_XDECREF(co);
2936 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002937}
2938
2939static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002940compiler_genexp(struct compiler *c, expr_ty e)
2941{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 static identifier name;
2943 if (!name) {
2944 name = PyUnicode_FromString("<genexpr>");
2945 if (!name)
2946 return 0;
2947 }
2948 assert(e->kind == GeneratorExp_kind);
2949 return compiler_comprehension(c, e, COMP_GENEXP, name,
2950 e->v.GeneratorExp.generators,
2951 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002952}
2953
2954static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002955compiler_listcomp(struct compiler *c, expr_ty e)
2956{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002957 static identifier name;
2958 if (!name) {
2959 name = PyUnicode_FromString("<listcomp>");
2960 if (!name)
2961 return 0;
2962 }
2963 assert(e->kind == ListComp_kind);
2964 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
2965 e->v.ListComp.generators,
2966 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002967}
2968
2969static int
2970compiler_setcomp(struct compiler *c, expr_ty e)
2971{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002972 static identifier name;
2973 if (!name) {
2974 name = PyUnicode_FromString("<setcomp>");
2975 if (!name)
2976 return 0;
2977 }
2978 assert(e->kind == SetComp_kind);
2979 return compiler_comprehension(c, e, COMP_SETCOMP, name,
2980 e->v.SetComp.generators,
2981 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002982}
2983
2984
2985static int
2986compiler_dictcomp(struct compiler *c, expr_ty e)
2987{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002988 static identifier name;
2989 if (!name) {
2990 name = PyUnicode_FromString("<dictcomp>");
2991 if (!name)
2992 return 0;
2993 }
2994 assert(e->kind == DictComp_kind);
2995 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
2996 e->v.DictComp.generators,
2997 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002998}
2999
3000
3001static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003002compiler_visit_keyword(struct compiler *c, keyword_ty k)
3003{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003004 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3005 VISIT(c, expr, k->value);
3006 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003007}
3008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003009/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003010 whether they are true or false.
3011
3012 Return values: 1 for true, 0 for false, -1 for non-constant.
3013 */
3014
3015static int
3016expr_constant(expr_ty e)
3017{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018 char *id;
3019 switch (e->kind) {
3020 case Ellipsis_kind:
3021 return 1;
3022 case Num_kind:
3023 return PyObject_IsTrue(e->v.Num.n);
3024 case Str_kind:
3025 return PyObject_IsTrue(e->v.Str.s);
3026 case Name_kind:
3027 /* optimize away names that can't be reassigned */
3028 id = PyBytes_AS_STRING(
3029 _PyUnicode_AsDefaultEncodedString(e->v.Name.id, NULL));
3030 if (strcmp(id, "True") == 0) return 1;
3031 if (strcmp(id, "False") == 0) return 0;
3032 if (strcmp(id, "None") == 0) return 0;
3033 if (strcmp(id, "__debug__") == 0)
3034 return ! Py_OptimizeFlag;
3035 /* fall through */
3036 default:
3037 return -1;
3038 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003039}
3040
Guido van Rossumc2e20742006-02-27 22:32:47 +00003041/*
3042 Implements the with statement from PEP 343.
3043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003044 The semantics outlined in that PEP are as follows:
Guido van Rossumc2e20742006-02-27 22:32:47 +00003045
3046 with EXPR as VAR:
3047 BLOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048
Guido van Rossumc2e20742006-02-27 22:32:47 +00003049 It is implemented roughly as:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003050
Thomas Wouters477c8d52006-05-27 19:21:47 +00003051 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00003052 exit = context.__exit__ # not calling it
3053 value = context.__enter__()
3054 try:
3055 VAR = value # if VAR present in the syntax
3056 BLOCK
3057 finally:
3058 if an exception was raised:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003060 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003062 exit(*exc)
3063 */
3064static int
3065compiler_with(struct compiler *c, stmt_ty s)
3066{
Guido van Rossumc2e20742006-02-27 22:32:47 +00003067 basicblock *block, *finally;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003068
3069 assert(s->kind == With_kind);
3070
Guido van Rossumc2e20742006-02-27 22:32:47 +00003071 block = compiler_new_block(c);
3072 finally = compiler_new_block(c);
3073 if (!block || !finally)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003074 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003075
Thomas Wouters477c8d52006-05-27 19:21:47 +00003076 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003077 VISIT(c, expr, s->v.With.context_expr);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003078 ADDOP_JREL(c, SETUP_WITH, finally);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003079
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003080 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003081 compiler_use_next_block(c, block);
3082 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003083 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003084 }
3085
3086 if (s->v.With.optional_vars) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003087 VISIT(c, expr, s->v.With.optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003088 }
3089 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003091 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003092 }
3093
3094 /* BLOCK code */
3095 VISIT_SEQ(c, stmt, s->v.With.body);
3096
3097 /* End of try block; start the finally block */
3098 ADDOP(c, POP_BLOCK);
3099 compiler_pop_fblock(c, FINALLY_TRY, block);
3100
3101 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3102 compiler_use_next_block(c, finally);
3103 if (!compiler_push_fblock(c, FINALLY_END, finally))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003104 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003105
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003106 /* Finally block starts; context.__exit__ is on the stack under
3107 the exception or return information. Just issue our magic
3108 opcode. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003109 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003110
3111 /* Finally block ends. */
3112 ADDOP(c, END_FINALLY);
3113 compiler_pop_fblock(c, FINALLY_END, finally);
3114 return 1;
3115}
3116
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003117static int
3118compiler_visit_expr(struct compiler *c, expr_ty e)
3119{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003120 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 /* If expr e has a different line number than the last expr/stmt,
3123 set a new line number for the next instruction.
3124 */
3125 if (e->lineno > c->u->u_lineno) {
3126 c->u->u_lineno = e->lineno;
3127 c->u->u_lineno_set = 0;
3128 }
3129 switch (e->kind) {
3130 case BoolOp_kind:
3131 return compiler_boolop(c, e);
3132 case BinOp_kind:
3133 VISIT(c, expr, e->v.BinOp.left);
3134 VISIT(c, expr, e->v.BinOp.right);
3135 ADDOP(c, binop(c, e->v.BinOp.op));
3136 break;
3137 case UnaryOp_kind:
3138 VISIT(c, expr, e->v.UnaryOp.operand);
3139 ADDOP(c, unaryop(e->v.UnaryOp.op));
3140 break;
3141 case Lambda_kind:
3142 return compiler_lambda(c, e);
3143 case IfExp_kind:
3144 return compiler_ifexp(c, e);
3145 case Dict_kind:
3146 n = asdl_seq_LEN(e->v.Dict.values);
3147 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
3148 for (i = 0; i < n; i++) {
3149 VISIT(c, expr,
3150 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3151 VISIT(c, expr,
3152 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3153 ADDOP(c, STORE_MAP);
3154 }
3155 break;
3156 case Set_kind:
3157 n = asdl_seq_LEN(e->v.Set.elts);
3158 VISIT_SEQ(c, expr, e->v.Set.elts);
3159 ADDOP_I(c, BUILD_SET, n);
3160 break;
3161 case GeneratorExp_kind:
3162 return compiler_genexp(c, e);
3163 case ListComp_kind:
3164 return compiler_listcomp(c, e);
3165 case SetComp_kind:
3166 return compiler_setcomp(c, e);
3167 case DictComp_kind:
3168 return compiler_dictcomp(c, e);
3169 case Yield_kind:
3170 if (c->u->u_ste->ste_type != FunctionBlock)
3171 return compiler_error(c, "'yield' outside function");
3172 if (e->v.Yield.value) {
3173 VISIT(c, expr, e->v.Yield.value);
3174 }
3175 else {
3176 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3177 }
3178 ADDOP(c, YIELD_VALUE);
3179 break;
3180 case Compare_kind:
3181 return compiler_compare(c, e);
3182 case Call_kind:
3183 return compiler_call(c, e);
3184 case Num_kind:
3185 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3186 break;
3187 case Str_kind:
3188 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3189 break;
3190 case Bytes_kind:
3191 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
3192 break;
3193 case Ellipsis_kind:
3194 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3195 break;
3196 /* The following exprs can be assignment targets. */
3197 case Attribute_kind:
3198 if (e->v.Attribute.ctx != AugStore)
3199 VISIT(c, expr, e->v.Attribute.value);
3200 switch (e->v.Attribute.ctx) {
3201 case AugLoad:
3202 ADDOP(c, DUP_TOP);
3203 /* Fall through to load */
3204 case Load:
3205 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3206 break;
3207 case AugStore:
3208 ADDOP(c, ROT_TWO);
3209 /* Fall through to save */
3210 case Store:
3211 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3212 break;
3213 case Del:
3214 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3215 break;
3216 case Param:
3217 default:
3218 PyErr_SetString(PyExc_SystemError,
3219 "param invalid in attribute expression");
3220 return 0;
3221 }
3222 break;
3223 case Subscript_kind:
3224 switch (e->v.Subscript.ctx) {
3225 case AugLoad:
3226 VISIT(c, expr, e->v.Subscript.value);
3227 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3228 break;
3229 case Load:
3230 VISIT(c, expr, e->v.Subscript.value);
3231 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3232 break;
3233 case AugStore:
3234 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3235 break;
3236 case Store:
3237 VISIT(c, expr, e->v.Subscript.value);
3238 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3239 break;
3240 case Del:
3241 VISIT(c, expr, e->v.Subscript.value);
3242 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3243 break;
3244 case Param:
3245 default:
3246 PyErr_SetString(PyExc_SystemError,
3247 "param invalid in subscript expression");
3248 return 0;
3249 }
3250 break;
3251 case Starred_kind:
3252 switch (e->v.Starred.ctx) {
3253 case Store:
3254 /* In all legitimate cases, the Starred node was already replaced
3255 * by compiler_list/compiler_tuple. XXX: is that okay? */
3256 return compiler_error(c,
3257 "starred assignment target must be in a list or tuple");
3258 default:
3259 return compiler_error(c,
3260 "can use starred expression only as assignment target");
3261 }
3262 break;
3263 case Name_kind:
3264 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3265 /* child nodes of List and Tuple will have expr_context set */
3266 case List_kind:
3267 return compiler_list(c, e);
3268 case Tuple_kind:
3269 return compiler_tuple(c, e);
3270 }
3271 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003272}
3273
3274static int
3275compiler_augassign(struct compiler *c, stmt_ty s)
3276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003277 expr_ty e = s->v.AugAssign.target;
3278 expr_ty auge;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003280 assert(s->kind == AugAssign_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003282 switch (e->kind) {
3283 case Attribute_kind:
3284 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
3285 AugLoad, e->lineno, e->col_offset, c->c_arena);
3286 if (auge == NULL)
3287 return 0;
3288 VISIT(c, expr, auge);
3289 VISIT(c, expr, s->v.AugAssign.value);
3290 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3291 auge->v.Attribute.ctx = AugStore;
3292 VISIT(c, expr, auge);
3293 break;
3294 case Subscript_kind:
3295 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
3296 AugLoad, e->lineno, e->col_offset, c->c_arena);
3297 if (auge == NULL)
3298 return 0;
3299 VISIT(c, expr, auge);
3300 VISIT(c, expr, s->v.AugAssign.value);
3301 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3302 auge->v.Subscript.ctx = AugStore;
3303 VISIT(c, expr, auge);
3304 break;
3305 case Name_kind:
3306 if (!compiler_nameop(c, e->v.Name.id, Load))
3307 return 0;
3308 VISIT(c, expr, s->v.AugAssign.value);
3309 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3310 return compiler_nameop(c, e->v.Name.id, Store);
3311 default:
3312 PyErr_Format(PyExc_SystemError,
3313 "invalid node type (%d) for augmented assignment",
3314 e->kind);
3315 return 0;
3316 }
3317 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003318}
3319
3320static int
3321compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003323 struct fblockinfo *f;
3324 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3325 PyErr_SetString(PyExc_SystemError,
3326 "too many statically nested blocks");
3327 return 0;
3328 }
3329 f = &c->u->u_fblock[c->u->u_nfblocks++];
3330 f->fb_type = t;
3331 f->fb_block = b;
3332 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003333}
3334
3335static void
3336compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003338 struct compiler_unit *u = c->u;
3339 assert(u->u_nfblocks > 0);
3340 u->u_nfblocks--;
3341 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3342 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003343}
3344
Thomas Wouters89f507f2006-12-13 04:49:30 +00003345static int
3346compiler_in_loop(struct compiler *c) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003347 int i;
3348 struct compiler_unit *u = c->u;
3349 for (i = 0; i < u->u_nfblocks; ++i) {
3350 if (u->u_fblock[i].fb_type == LOOP)
3351 return 1;
3352 }
3353 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003354}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003355/* Raises a SyntaxError and returns 0.
3356 If something goes wrong, a different exception may be raised.
3357*/
3358
3359static int
3360compiler_error(struct compiler *c, const char *errstr)
3361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003362 PyObject *loc;
3363 PyObject *u = NULL, *v = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003365 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3366 if (!loc) {
3367 Py_INCREF(Py_None);
3368 loc = Py_None;
3369 }
3370 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3371 Py_None, loc);
3372 if (!u)
3373 goto exit;
3374 v = Py_BuildValue("(zO)", errstr, u);
3375 if (!v)
3376 goto exit;
3377 PyErr_SetObject(PyExc_SyntaxError, v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003378 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003379 Py_DECREF(loc);
3380 Py_XDECREF(u);
3381 Py_XDECREF(v);
3382 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003383}
3384
3385static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003386compiler_handle_subscr(struct compiler *c, const char *kind,
3387 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003388{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003389 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003391 /* XXX this code is duplicated */
3392 switch (ctx) {
3393 case AugLoad: /* fall through to Load */
3394 case Load: op = BINARY_SUBSCR; break;
3395 case AugStore:/* fall through to Store */
3396 case Store: op = STORE_SUBSCR; break;
3397 case Del: op = DELETE_SUBSCR; break;
3398 case Param:
3399 PyErr_Format(PyExc_SystemError,
3400 "invalid %s kind %d in subscript\n",
3401 kind, ctx);
3402 return 0;
3403 }
3404 if (ctx == AugLoad) {
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00003405 ADDOP(c, DUP_TOP_TWO);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003406 }
3407 else if (ctx == AugStore) {
3408 ADDOP(c, ROT_THREE);
3409 }
3410 ADDOP(c, op);
3411 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003412}
3413
3414static int
3415compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003417 int n = 2;
3418 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003420 /* only handles the cases where BUILD_SLICE is emitted */
3421 if (s->v.Slice.lower) {
3422 VISIT(c, expr, s->v.Slice.lower);
3423 }
3424 else {
3425 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3426 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 if (s->v.Slice.upper) {
3429 VISIT(c, expr, s->v.Slice.upper);
3430 }
3431 else {
3432 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3433 }
3434
3435 if (s->v.Slice.step) {
3436 n++;
3437 VISIT(c, expr, s->v.Slice.step);
3438 }
3439 ADDOP_I(c, BUILD_SLICE, n);
3440 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003441}
3442
3443static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003444compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3445 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003446{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003447 switch (s->kind) {
3448 case Slice_kind:
3449 return compiler_slice(c, s, ctx);
3450 case Index_kind:
3451 VISIT(c, expr, s->v.Index.value);
3452 break;
3453 case ExtSlice_kind:
3454 default:
3455 PyErr_SetString(PyExc_SystemError,
3456 "extended slice invalid in nested slice");
3457 return 0;
3458 }
3459 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003460}
3461
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003462static int
3463compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3464{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003465 char * kindname = NULL;
3466 switch (s->kind) {
3467 case Index_kind:
3468 kindname = "index";
3469 if (ctx != AugStore) {
3470 VISIT(c, expr, s->v.Index.value);
3471 }
3472 break;
3473 case Slice_kind:
3474 kindname = "slice";
3475 if (ctx != AugStore) {
3476 if (!compiler_slice(c, s, ctx))
3477 return 0;
3478 }
3479 break;
3480 case ExtSlice_kind:
3481 kindname = "extended slice";
3482 if (ctx != AugStore) {
3483 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3484 for (i = 0; i < n; i++) {
3485 slice_ty sub = (slice_ty)asdl_seq_GET(
3486 s->v.ExtSlice.dims, i);
3487 if (!compiler_visit_nested_slice(c, sub, ctx))
3488 return 0;
3489 }
3490 ADDOP_I(c, BUILD_TUPLE, n);
3491 }
3492 break;
3493 default:
3494 PyErr_Format(PyExc_SystemError,
3495 "invalid subscript kind %d", s->kind);
3496 return 0;
3497 }
3498 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003499}
3500
Thomas Wouters89f507f2006-12-13 04:49:30 +00003501/* End of the compiler section, beginning of the assembler section */
3502
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003503/* do depth-first search of basic block graph, starting with block.
3504 post records the block indices in post-order.
3505
3506 XXX must handle implicit jumps from one block to next
3507*/
3508
Thomas Wouters89f507f2006-12-13 04:49:30 +00003509struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003510 PyObject *a_bytecode; /* string containing bytecode */
3511 int a_offset; /* offset into bytecode */
3512 int a_nblocks; /* number of reachable blocks */
3513 basicblock **a_postorder; /* list of blocks in dfs postorder */
3514 PyObject *a_lnotab; /* string containing lnotab */
3515 int a_lnotab_off; /* offset into lnotab */
3516 int a_lineno; /* last lineno of emitted instruction */
3517 int a_lineno_off; /* bytecode offset of last lineno */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003518};
3519
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003520static void
3521dfs(struct compiler *c, basicblock *b, struct assembler *a)
3522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003523 int i;
3524 struct instr *instr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003526 if (b->b_seen)
3527 return;
3528 b->b_seen = 1;
3529 if (b->b_next != NULL)
3530 dfs(c, b->b_next, a);
3531 for (i = 0; i < b->b_iused; i++) {
3532 instr = &b->b_instr[i];
3533 if (instr->i_jrel || instr->i_jabs)
3534 dfs(c, instr->i_target, a);
3535 }
3536 a->a_postorder[a->a_nblocks++] = b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003537}
3538
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003539static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003540stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 int i, target_depth;
3543 struct instr *instr;
3544 if (b->b_seen || b->b_startdepth >= depth)
3545 return maxdepth;
3546 b->b_seen = 1;
3547 b->b_startdepth = depth;
3548 for (i = 0; i < b->b_iused; i++) {
3549 instr = &b->b_instr[i];
3550 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3551 if (depth > maxdepth)
3552 maxdepth = depth;
3553 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3554 if (instr->i_jrel || instr->i_jabs) {
3555 target_depth = depth;
3556 if (instr->i_opcode == FOR_ITER) {
3557 target_depth = depth-2;
3558 } else if (instr->i_opcode == SETUP_FINALLY ||
3559 instr->i_opcode == SETUP_EXCEPT) {
3560 target_depth = depth+3;
3561 if (target_depth > maxdepth)
3562 maxdepth = target_depth;
3563 }
3564 maxdepth = stackdepth_walk(c, instr->i_target,
3565 target_depth, maxdepth);
3566 if (instr->i_opcode == JUMP_ABSOLUTE ||
3567 instr->i_opcode == JUMP_FORWARD) {
3568 goto out; /* remaining code is dead */
3569 }
3570 }
3571 }
3572 if (b->b_next)
3573 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003574out:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003575 b->b_seen = 0;
3576 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003577}
3578
3579/* Find the flow path that needs the largest stack. We assume that
3580 * cycles in the flow graph have no net effect on the stack depth.
3581 */
3582static int
3583stackdepth(struct compiler *c)
3584{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003585 basicblock *b, *entryblock;
3586 entryblock = NULL;
3587 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3588 b->b_seen = 0;
3589 b->b_startdepth = INT_MIN;
3590 entryblock = b;
3591 }
3592 if (!entryblock)
3593 return 0;
3594 return stackdepth_walk(c, entryblock, 0, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003595}
3596
3597static int
3598assemble_init(struct assembler *a, int nblocks, int firstlineno)
3599{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003600 memset(a, 0, sizeof(struct assembler));
3601 a->a_lineno = firstlineno;
3602 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3603 if (!a->a_bytecode)
3604 return 0;
3605 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3606 if (!a->a_lnotab)
3607 return 0;
3608 if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
3609 PyErr_NoMemory();
3610 return 0;
3611 }
3612 a->a_postorder = (basicblock **)PyObject_Malloc(
3613 sizeof(basicblock *) * nblocks);
3614 if (!a->a_postorder) {
3615 PyErr_NoMemory();
3616 return 0;
3617 }
3618 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003619}
3620
3621static void
3622assemble_free(struct assembler *a)
3623{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003624 Py_XDECREF(a->a_bytecode);
3625 Py_XDECREF(a->a_lnotab);
3626 if (a->a_postorder)
3627 PyObject_Free(a->a_postorder);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003628}
3629
3630/* Return the size of a basic block in bytes. */
3631
3632static int
3633instrsize(struct instr *instr)
3634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003635 if (!instr->i_hasarg)
3636 return 1; /* 1 byte for the opcode*/
3637 if (instr->i_oparg > 0xffff)
3638 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3639 return 3; /* 1 (opcode) + 2 (oparg) */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003640}
3641
3642static int
3643blocksize(basicblock *b)
3644{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003645 int i;
3646 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003648 for (i = 0; i < b->b_iused; i++)
3649 size += instrsize(&b->b_instr[i]);
3650 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003651}
3652
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003653/* Appends a pair to the end of the line number table, a_lnotab, representing
3654 the instruction's bytecode offset and line number. See
3655 Objects/lnotab_notes.txt for the description of the line number table. */
Tim Peters2a7f3842001-06-09 09:26:21 +00003656
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003657static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003658assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003659{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003660 int d_bytecode, d_lineno;
3661 int len;
3662 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003664 d_bytecode = a->a_offset - a->a_lineno_off;
3665 d_lineno = i->i_lineno - a->a_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003667 assert(d_bytecode >= 0);
3668 assert(d_lineno >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003670 if(d_bytecode == 0 && d_lineno == 0)
3671 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003673 if (d_bytecode > 255) {
3674 int j, nbytes, ncodes = d_bytecode / 255;
3675 nbytes = a->a_lnotab_off + 2 * ncodes;
3676 len = PyBytes_GET_SIZE(a->a_lnotab);
3677 if (nbytes >= len) {
3678 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
3679 len = nbytes;
3680 else if (len <= INT_MAX / 2)
3681 len *= 2;
3682 else {
3683 PyErr_NoMemory();
3684 return 0;
3685 }
3686 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
3687 return 0;
3688 }
3689 lnotab = (unsigned char *)
3690 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3691 for (j = 0; j < ncodes; j++) {
3692 *lnotab++ = 255;
3693 *lnotab++ = 0;
3694 }
3695 d_bytecode -= ncodes * 255;
3696 a->a_lnotab_off += ncodes * 2;
3697 }
3698 assert(d_bytecode <= 255);
3699 if (d_lineno > 255) {
3700 int j, nbytes, ncodes = d_lineno / 255;
3701 nbytes = a->a_lnotab_off + 2 * ncodes;
3702 len = PyBytes_GET_SIZE(a->a_lnotab);
3703 if (nbytes >= len) {
3704 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
3705 len = nbytes;
3706 else if (len <= INT_MAX / 2)
3707 len *= 2;
3708 else {
3709 PyErr_NoMemory();
3710 return 0;
3711 }
3712 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
3713 return 0;
3714 }
3715 lnotab = (unsigned char *)
3716 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3717 *lnotab++ = d_bytecode;
3718 *lnotab++ = 255;
3719 d_bytecode = 0;
3720 for (j = 1; j < ncodes; j++) {
3721 *lnotab++ = 0;
3722 *lnotab++ = 255;
3723 }
3724 d_lineno -= ncodes * 255;
3725 a->a_lnotab_off += ncodes * 2;
3726 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 len = PyBytes_GET_SIZE(a->a_lnotab);
3729 if (a->a_lnotab_off + 2 >= len) {
3730 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
3731 return 0;
3732 }
3733 lnotab = (unsigned char *)
3734 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003736 a->a_lnotab_off += 2;
3737 if (d_bytecode) {
3738 *lnotab++ = d_bytecode;
3739 *lnotab++ = d_lineno;
3740 }
3741 else { /* First line of a block; def stmt, etc. */
3742 *lnotab++ = 0;
3743 *lnotab++ = d_lineno;
3744 }
3745 a->a_lineno = i->i_lineno;
3746 a->a_lineno_off = a->a_offset;
3747 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003748}
3749
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003750/* assemble_emit()
3751 Extend the bytecode with a new instruction.
3752 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003753*/
3754
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003755static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003756assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003757{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 int size, arg = 0, ext = 0;
3759 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
3760 char *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003762 size = instrsize(i);
3763 if (i->i_hasarg) {
3764 arg = i->i_oparg;
3765 ext = arg >> 16;
3766 }
3767 if (i->i_lineno && !assemble_lnotab(a, i))
3768 return 0;
3769 if (a->a_offset + size >= len) {
3770 if (len > PY_SSIZE_T_MAX / 2)
3771 return 0;
3772 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
3773 return 0;
3774 }
3775 code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
3776 a->a_offset += size;
3777 if (size == 6) {
3778 assert(i->i_hasarg);
3779 *code++ = (char)EXTENDED_ARG;
3780 *code++ = ext & 0xff;
3781 *code++ = ext >> 8;
3782 arg &= 0xffff;
3783 }
3784 *code++ = i->i_opcode;
3785 if (i->i_hasarg) {
3786 assert(size == 3 || size == 6);
3787 *code++ = arg & 0xff;
3788 *code++ = arg >> 8;
3789 }
3790 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003791}
3792
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003793static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003794assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003795{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003796 basicblock *b;
3797 int bsize, totsize, extended_arg_count = 0, last_extended_arg_count;
3798 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003800 /* Compute the size of each block and fixup jump args.
3801 Replace block pointer with position in bytecode. */
3802 do {
3803 totsize = 0;
3804 for (i = a->a_nblocks - 1; i >= 0; i--) {
3805 b = a->a_postorder[i];
3806 bsize = blocksize(b);
3807 b->b_offset = totsize;
3808 totsize += bsize;
3809 }
3810 last_extended_arg_count = extended_arg_count;
3811 extended_arg_count = 0;
3812 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3813 bsize = b->b_offset;
3814 for (i = 0; i < b->b_iused; i++) {
3815 struct instr *instr = &b->b_instr[i];
3816 /* Relative jumps are computed relative to
3817 the instruction pointer after fetching
3818 the jump instruction.
3819 */
3820 bsize += instrsize(instr);
3821 if (instr->i_jabs)
3822 instr->i_oparg = instr->i_target->b_offset;
3823 else if (instr->i_jrel) {
3824 int delta = instr->i_target->b_offset - bsize;
3825 instr->i_oparg = delta;
3826 }
3827 else
3828 continue;
3829 if (instr->i_oparg > 0xffff)
3830 extended_arg_count++;
3831 }
3832 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003834 /* XXX: This is an awful hack that could hurt performance, but
3835 on the bright side it should work until we come up
3836 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00003837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003838 The issue is that in the first loop blocksize() is called
3839 which calls instrsize() which requires i_oparg be set
3840 appropriately. There is a bootstrap problem because
3841 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00003842
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003843 So we loop until we stop seeing new EXTENDED_ARGs.
3844 The only EXTENDED_ARGs that could be popping up are
3845 ones in jump instructions. So this should converge
3846 fairly quickly.
3847 */
3848 } while (last_extended_arg_count != extended_arg_count);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003849}
3850
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003851static PyObject *
3852dict_keys_inorder(PyObject *dict, int offset)
3853{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003854 PyObject *tuple, *k, *v;
3855 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003857 tuple = PyTuple_New(size);
3858 if (tuple == NULL)
3859 return NULL;
3860 while (PyDict_Next(dict, &pos, &k, &v)) {
3861 i = PyLong_AS_LONG(v);
3862 /* The keys of the dictionary are tuples. (see compiler_add_o)
3863 The object we want is always first, though. */
3864 k = PyTuple_GET_ITEM(k, 0);
3865 Py_INCREF(k);
3866 assert((i - offset) < size);
3867 assert((i - offset) >= 0);
3868 PyTuple_SET_ITEM(tuple, i - offset, k);
3869 }
3870 return tuple;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003871}
3872
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003873static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003874compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003876 PySTEntryObject *ste = c->u->u_ste;
3877 int flags = 0, n;
3878 if (ste->ste_type != ModuleBlock)
3879 flags |= CO_NEWLOCALS;
3880 if (ste->ste_type == FunctionBlock) {
3881 if (!ste->ste_unoptimized)
3882 flags |= CO_OPTIMIZED;
3883 if (ste->ste_nested)
3884 flags |= CO_NESTED;
3885 if (ste->ste_generator)
3886 flags |= CO_GENERATOR;
3887 if (ste->ste_varargs)
3888 flags |= CO_VARARGS;
3889 if (ste->ste_varkeywords)
3890 flags |= CO_VARKEYWORDS;
3891 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003893 /* (Only) inherit compilerflags in PyCF_MASK */
3894 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003896 n = PyDict_Size(c->u->u_freevars);
3897 if (n < 0)
3898 return -1;
3899 if (n == 0) {
3900 n = PyDict_Size(c->u->u_cellvars);
3901 if (n < 0)
3902 return -1;
3903 if (n == 0) {
3904 flags |= CO_NOFREE;
3905 }
3906 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003908 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003909}
3910
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003911static PyCodeObject *
3912makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003913{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003914 PyObject *tmp;
3915 PyCodeObject *co = NULL;
3916 PyObject *consts = NULL;
3917 PyObject *names = NULL;
3918 PyObject *varnames = NULL;
3919 PyObject *filename = NULL;
3920 PyObject *name = NULL;
3921 PyObject *freevars = NULL;
3922 PyObject *cellvars = NULL;
3923 PyObject *bytecode = NULL;
3924 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003926 tmp = dict_keys_inorder(c->u->u_consts, 0);
3927 if (!tmp)
3928 goto error;
3929 consts = PySequence_List(tmp); /* optimize_code requires a list */
3930 Py_DECREF(tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003932 names = dict_keys_inorder(c->u->u_names, 0);
3933 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3934 if (!consts || !names || !varnames)
3935 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003937 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3938 if (!cellvars)
3939 goto error;
3940 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3941 if (!freevars)
3942 goto error;
Victor Stinner0fe25a42010-06-17 23:08:50 +00003943 filename = PyUnicode_FromString(c->c_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003944 if (!filename)
3945 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003947 nlocals = PyDict_Size(c->u->u_varnames);
3948 flags = compute_code_flags(c);
3949 if (flags < 0)
3950 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003952 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
3953 if (!bytecode)
3954 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003956 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
3957 if (!tmp)
3958 goto error;
3959 Py_DECREF(consts);
3960 consts = tmp;
3961
3962 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
3963 nlocals, stackdepth(c), flags,
3964 bytecode, consts, names, varnames,
3965 freevars, cellvars,
3966 filename, c->u->u_name,
3967 c->u->u_firstlineno,
3968 a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003969 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003970 Py_XDECREF(consts);
3971 Py_XDECREF(names);
3972 Py_XDECREF(varnames);
3973 Py_XDECREF(filename);
3974 Py_XDECREF(name);
3975 Py_XDECREF(freevars);
3976 Py_XDECREF(cellvars);
3977 Py_XDECREF(bytecode);
3978 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003979}
3980
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003981
3982/* For debugging purposes only */
3983#if 0
3984static void
3985dump_instr(const struct instr *i)
3986{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003987 const char *jrel = i->i_jrel ? "jrel " : "";
3988 const char *jabs = i->i_jabs ? "jabs " : "";
3989 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003991 *arg = '\0';
3992 if (i->i_hasarg)
3993 sprintf(arg, "arg: %d ", i->i_oparg);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003995 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
3996 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003997}
3998
3999static void
4000dump_basicblock(const basicblock *b)
4001{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004002 const char *seen = b->b_seen ? "seen " : "";
4003 const char *b_return = b->b_return ? "return " : "";
4004 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4005 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4006 if (b->b_instr) {
4007 int i;
4008 for (i = 0; i < b->b_iused; i++) {
4009 fprintf(stderr, " [%02d] ", i);
4010 dump_instr(b->b_instr + i);
4011 }
4012 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004013}
4014#endif
4015
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004016static PyCodeObject *
4017assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004018{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004019 basicblock *b, *entryblock;
4020 struct assembler a;
4021 int i, j, nblocks;
4022 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004024 /* Make sure every block that falls off the end returns None.
4025 XXX NEXT_BLOCK() isn't quite right, because if the last
4026 block ends with a jump or return b_next shouldn't set.
4027 */
4028 if (!c->u->u_curblock->b_return) {
4029 NEXT_BLOCK(c);
4030 if (addNone)
4031 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4032 ADDOP(c, RETURN_VALUE);
4033 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004035 nblocks = 0;
4036 entryblock = NULL;
4037 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4038 nblocks++;
4039 entryblock = b;
4040 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004042 /* Set firstlineno if it wasn't explicitly set. */
4043 if (!c->u->u_firstlineno) {
4044 if (entryblock && entryblock->b_instr)
4045 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4046 else
4047 c->u->u_firstlineno = 1;
4048 }
4049 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4050 goto error;
4051 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004053 /* Can't modify the bytecode after computing jump offsets. */
4054 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004056 /* Emit code in reverse postorder from dfs. */
4057 for (i = a.a_nblocks - 1; i >= 0; i--) {
4058 b = a.a_postorder[i];
4059 for (j = 0; j < b->b_iused; j++)
4060 if (!assemble_emit(&a, &b->b_instr[j]))
4061 goto error;
4062 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004064 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4065 goto error;
4066 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset) < 0)
4067 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004069 co = makecode(c, &a);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004070 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004071 assemble_free(&a);
4072 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004073}