blob: 0754ea508a7aedab93c6159447c06383b54f5c7b [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
8 * 2. Builds a symbol table. See symtable.c.
9 * 3. Generate code for basic blocks. See compiler_mod() in this file.
10 * 4. Assemble the basic blocks into final code. See assemble() in
11 * this file.
12 *
13 * Note that compiler_mod() suggests module, but the module ast type
14 * (mod_ty) has cases for expressions and interactive statements.
Nick Coghlan944d3eb2005-11-16 12:46:55 +000015 *
16 * CAUTION: The VISIT_* macros abort the current function when they encounter
17 * a problem. So don't invoke them when there is memory which needs to be
18 * released. Code blocks are OK, as the compiler structure takes care of
19 * releasing those.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000020 */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000021
Guido van Rossum79f25d91997-04-29 20:08:16 +000022#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000023
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024#include "Python-ast.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000025#include "node.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000026#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000027#include "ast.h"
28#include "code.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000029#include "compile.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000030#include "symtable.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031#include "opcode.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000032
Guido van Rossum8e793d91997-03-03 19:13:14 +000033int Py_OptimizeFlag = 0;
34
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000035/*
36 ISSUES:
Guido van Rossum8861b741996-07-30 16:49:37 +000037
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000038 character encodings aren't handled
Jeremy Hyltone36f7782001-01-19 03:21:30 +000039
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000040 ref leaks in interpreter when press return on empty line
Jeremy Hyltoneab156f2001-01-30 01:24:43 +000041
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000042 opcode_stack_effect() function should be reviewed since stack depth bugs
43 could be really hard to find later.
Jeremy Hyltoneab156f2001-01-30 01:24:43 +000044
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000045 Dead code is being generated (i.e. after unconditional jumps).
46*/
Jeremy Hylton29906ee2001-02-27 04:23:34 +000047
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000048#define DEFAULT_BLOCK_SIZE 16
49#define DEFAULT_BLOCKS 8
50#define DEFAULT_CODE_SIZE 128
51#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000052
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000053struct instr {
Neal Norwitz08b401f2006-01-07 21:24:09 +000054 unsigned i_jabs : 1;
55 unsigned i_jrel : 1;
56 unsigned i_hasarg : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000057 unsigned char i_opcode;
58 int i_oparg;
59 struct basicblock_ *i_target; /* target block (if jump instruction) */
60 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000061};
62
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000063typedef struct basicblock_ {
64 /* next block in the list of blocks for a unit (don't confuse with
65 * b_next) */
66 struct basicblock_ *b_list;
67 /* number of instructions used */
68 int b_iused;
69 /* length of instruction array (b_instr) */
70 int b_ialloc;
71 /* pointer to an array of instructions, initially NULL */
72 struct instr *b_instr;
73 /* If b_next is non-NULL, it is a pointer to the next
74 block reached by normal control flow. */
75 struct basicblock_ *b_next;
76 /* b_seen is used to perform a DFS of basicblocks. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000077 unsigned b_seen : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000078 /* b_return is true if a RETURN_VALUE opcode is inserted. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000079 unsigned b_return : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000080 /* depth of stack upon entry of block, computed by stackdepth() */
81 int b_startdepth;
82 /* instruction offset for block, computed by assemble_jump_offsets() */
83 int b_offset;
84} basicblock;
85
86/* fblockinfo tracks the current frame block.
87
88 A frame block is used to handle loops, try/except, and try/finally.
89 It's called a frame block to distinguish it from a basic block in the
90 compiler IR.
91*/
92
93enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
94
95struct fblockinfo {
96 enum fblocktype fb_type;
97 basicblock *fb_block;
98};
99
100/* The following items change on entry and exit of code blocks.
101 They must be saved and restored when returning to a block.
102*/
103struct compiler_unit {
104 PySTEntryObject *u_ste;
105
106 PyObject *u_name;
107 /* The following fields are dicts that map objects to
108 the index of them in co_XXX. The index is used as
109 the argument for opcodes that refer to those collections.
110 */
111 PyObject *u_consts; /* all constants */
112 PyObject *u_names; /* all names */
113 PyObject *u_varnames; /* local variables */
114 PyObject *u_cellvars; /* cell variables */
115 PyObject *u_freevars; /* free variables */
116
117 PyObject *u_private; /* for private name mangling */
118
119 int u_argcount; /* number of arguments for block */
120 basicblock *u_blocks; /* pointer to list of blocks */
121 basicblock *u_curblock; /* pointer to current block */
122 int u_tmpname; /* temporary variables for list comps */
123
124 int u_nfblocks;
125 struct fblockinfo u_fblock[CO_MAXBLOCKS];
126
127 int u_firstlineno; /* the first lineno of the block */
128 int u_lineno; /* the lineno for the current stmt */
129 bool u_lineno_set; /* boolean to indicate whether instr
130 has been generated with current lineno */
131};
132
133/* This struct captures the global state of a compilation.
134
135 The u pointer points to the current compilation unit, while units
136 for enclosing blocks are stored in c_stack. The u and c_stack are
137 managed by compiler_enter_scope() and compiler_exit_scope().
138*/
139
140struct compiler {
141 const char *c_filename;
142 struct symtable *c_st;
143 PyFutureFeatures *c_future; /* pointer to module's __future__ */
144 PyCompilerFlags *c_flags;
145
146 int c_interactive;
147 int c_nestlevel;
148
149 struct compiler_unit *u; /* compiler state for current block */
150 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
151 char *c_encoding; /* source encoding (a borrowed reference) */
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000152 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000153};
154
155struct assembler {
156 PyObject *a_bytecode; /* string containing bytecode */
157 int a_offset; /* offset into bytecode */
158 int a_nblocks; /* number of reachable blocks */
159 basicblock **a_postorder; /* list of blocks in dfs postorder */
160 PyObject *a_lnotab; /* string containing lnotab */
161 int a_lnotab_off; /* offset into lnotab */
162 int a_lineno; /* last lineno of emitted instruction */
163 int a_lineno_off; /* bytecode offset of last lineno */
164};
165
166static int compiler_enter_scope(struct compiler *, identifier, void *, int);
167static void compiler_free(struct compiler *);
168static basicblock *compiler_new_block(struct compiler *);
169static int compiler_next_instr(struct compiler *, basicblock *);
170static int compiler_addop(struct compiler *, int);
171static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
172static int compiler_addop_i(struct compiler *, int, int);
173static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000174static basicblock *compiler_use_new_block(struct compiler *);
175static int compiler_error(struct compiler *, const char *);
176static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
177
178static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
179static int compiler_visit_stmt(struct compiler *, stmt_ty);
180static int compiler_visit_keyword(struct compiler *, keyword_ty);
181static int compiler_visit_expr(struct compiler *, expr_ty);
182static int compiler_augassign(struct compiler *, stmt_ty);
183static int compiler_visit_slice(struct compiler *, slice_ty,
184 expr_context_ty);
185
186static int compiler_push_fblock(struct compiler *, enum fblocktype,
187 basicblock *);
188static void compiler_pop_fblock(struct compiler *, enum fblocktype,
189 basicblock *);
190
191static int inplace_binop(struct compiler *, operator_ty);
192static int expr_constant(expr_ty e);
193
194static PyCodeObject *assemble(struct compiler *, int addNone);
195static PyObject *__doc__;
196
197PyObject *
198_Py_Mangle(PyObject *private, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000199{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200 /* Name mangling: __private becomes _classname__private.
201 This is independent from how the name is used. */
202 const char *p, *name = PyString_AsString(ident);
203 char *buffer;
204 size_t nlen, plen;
205 if (private == NULL || name == NULL || name[0] != '_' || name[1] != '_') {
206 Py_INCREF(ident);
207 return ident;
208 }
209 p = PyString_AsString(private);
210 nlen = strlen(name);
211 if (name[nlen-1] == '_' && name[nlen-2] == '_') {
212 Py_INCREF(ident);
213 return ident; /* Don't mangle __whatever__ */
214 }
215 /* Strip leading underscores from class name */
216 while (*p == '_')
217 p++;
218 if (*p == '\0') {
219 Py_INCREF(ident);
220 return ident; /* Don't mangle if class is just underscores */
221 }
222 plen = strlen(p);
223 ident = PyString_FromStringAndSize(NULL, 1 + nlen + plen);
224 if (!ident)
225 return 0;
226 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
227 buffer = PyString_AS_STRING(ident);
228 buffer[0] = '_';
229 strncpy(buffer+1, p, plen);
230 strcpy(buffer+1+plen, name);
231 return ident;
Michael W. Hudson60934622004-08-12 17:56:29 +0000232}
233
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000234static int
235compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000236{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000237 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000238
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000239 c->c_stack = PyList_New(0);
240 if (!c->c_stack)
241 return 0;
242
243 return 1;
244}
245
246PyCodeObject *
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000247PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
248 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000249{
250 struct compiler c;
251 PyCodeObject *co = NULL;
252 PyCompilerFlags local_flags;
253 int merged;
254
255 if (!__doc__) {
256 __doc__ = PyString_InternFromString("__doc__");
257 if (!__doc__)
258 goto error;
259 }
260
261 if (!compiler_init(&c))
262 goto error;
263 c.c_filename = filename;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000264 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000265 c.c_future = PyFuture_FromAST(mod, filename);
266 if (c.c_future == NULL)
267 goto error;
268 if (!flags) {
269 local_flags.cf_flags = 0;
270 flags = &local_flags;
271 }
272 merged = c.c_future->ff_features | flags->cf_flags;
273 c.c_future->ff_features = merged;
274 flags->cf_flags = merged;
275 c.c_flags = flags;
276 c.c_nestlevel = 0;
277
278 c.c_st = PySymtable_Build(mod, filename, c.c_future);
279 if (c.c_st == NULL) {
280 if (!PyErr_Occurred())
281 PyErr_SetString(PyExc_SystemError, "no symtable");
282 goto error;
283 }
284
285 /* XXX initialize to NULL for now, need to handle */
286 c.c_encoding = NULL;
287
288 co = compiler_mod(&c, mod);
289
290 error:
291 compiler_free(&c);
292 return co;
293}
294
295PyCodeObject *
296PyNode_Compile(struct _node *n, const char *filename)
297{
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000298 PyCodeObject *co = NULL;
Fredrik Lundh93d69a72005-12-18 15:44:21 +0000299 PyArena *arena = PyArena_New();
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000300 mod_ty mod = PyAST_FromNode(n, NULL, filename, arena);
301 if (mod)
302 co = PyAST_Compile(mod, filename, NULL, arena);
303 PyArena_Free(arena);
Raymond Hettinger37a724d2003-09-15 21:43:16 +0000304 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000305}
306
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000307static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000308compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000309{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000310 if (c->c_st)
311 PySymtable_Free(c->c_st);
312 if (c->c_future)
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000313 PyMem_Free(c->c_future);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000314 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000315}
316
Guido van Rossum79f25d91997-04-29 20:08:16 +0000317static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000318list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000319{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000320 int i, n;
321 PyObject *v, *k, *dict = PyDict_New();
Guido van Rossumd076c731998-10-07 19:42:25 +0000322
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000323 n = PyList_Size(list);
324 for (i = 0; i < n; i++) {
325 v = PyInt_FromLong(i);
326 if (!v) {
327 Py_DECREF(dict);
328 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000329 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000330 k = PyList_GET_ITEM(list, i);
331 k = Py_BuildValue("(OO)", k, k->ob_type);
332 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
333 Py_XDECREF(k);
334 Py_DECREF(v);
335 Py_DECREF(dict);
336 return NULL;
337 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000338 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000339 Py_DECREF(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000340 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000341 return dict;
342}
343
344/* Return new dict containing names from src that match scope(s).
345
346 src is a symbol table dictionary. If the scope of a name matches
347 either scope_type or flag is set, insert it into the new dict. The
348 values are integers, starting at offset and increasing by one for
349 each key.
350*/
351
352static PyObject *
353dictbytype(PyObject *src, int scope_type, int flag, int offset)
354{
355 int pos = 0, i = offset, scope;
356 PyObject *k, *v, *dest = PyDict_New();
357
358 assert(offset >= 0);
359 if (dest == NULL)
360 return NULL;
361
362 while (PyDict_Next(src, &pos, &k, &v)) {
363 /* XXX this should probably be a macro in symtable.h */
364 assert(PyInt_Check(v));
365 scope = (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
366
367 if (scope == scope_type || PyInt_AS_LONG(v) & flag) {
368 PyObject *tuple, *item = PyInt_FromLong(i);
369 if (item == NULL) {
370 Py_DECREF(dest);
371 return NULL;
372 }
373 i++;
374 tuple = Py_BuildValue("(OO)", k, k->ob_type);
375 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
376 Py_DECREF(item);
377 Py_DECREF(dest);
378 Py_XDECREF(tuple);
379 return NULL;
380 }
381 Py_DECREF(item);
382 Py_DECREF(tuple);
383 }
384 }
385 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000386}
387
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000388/* Begin: Peephole optimizations ----------------------------------------- */
389
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000390#define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1]))
391#define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD)
Raymond Hettinger5b75c382003-03-28 12:05:00 +0000392#define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP)
393#define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000394#define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000395#define CODESIZE(op) (HAS_ARG(op) ? 3 : 1)
396#define ISBASICBLOCK(blocks, start, bytes) (blocks[start]==blocks[start+bytes-1])
397
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000398/* Replace LOAD_CONST c1. LOAD_CONST c2 ... LOAD_CONST cn BUILD_TUPLE n
399 with LOAD_CONST (c1, c2, ... cn).
400 The consts table must still be in list form so that the
401 new constant (c1, c2, ... cn) can be appended.
402 Called with codestr pointing to the first LOAD_CONST.
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000403 Bails out with no change if one or more of the LOAD_CONSTs is missing.
404 Also works for BUILD_LIST when followed by an "in" or "not in" test.
405*/
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000406static int
407tuple_of_constants(unsigned char *codestr, int n, PyObject *consts)
408{
409 PyObject *newconst, *constant;
410 int i, arg, len_consts;
411
412 /* Pre-conditions */
413 assert(PyList_CheckExact(consts));
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000414 assert(codestr[n*3] == BUILD_TUPLE || codestr[n*3] == BUILD_LIST);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000415 assert(GETARG(codestr, (n*3)) == n);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000416 for (i=0 ; i<n ; i++)
Raymond Hettingereffb3932004-10-30 08:55:08 +0000417 assert(codestr[i*3] == LOAD_CONST);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000418
419 /* Buildup new tuple of constants */
420 newconst = PyTuple_New(n);
421 if (newconst == NULL)
422 return 0;
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000423 len_consts = PyList_GET_SIZE(consts);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000424 for (i=0 ; i<n ; i++) {
425 arg = GETARG(codestr, (i*3));
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000426 assert(arg < len_consts);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000427 constant = PyList_GET_ITEM(consts, arg);
428 Py_INCREF(constant);
429 PyTuple_SET_ITEM(newconst, i, constant);
430 }
431
432 /* Append folded constant onto consts */
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000433 if (PyList_Append(consts, newconst)) {
434 Py_DECREF(newconst);
435 return 0;
436 }
437 Py_DECREF(newconst);
438
439 /* Write NOPs over old LOAD_CONSTS and
440 add a new LOAD_CONST newconst on top of the BUILD_TUPLE n */
441 memset(codestr, NOP, n*3);
442 codestr[n*3] = LOAD_CONST;
443 SETARG(codestr, (n*3), len_consts);
444 return 1;
445}
446
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000447/* Replace LOAD_CONST c1. LOAD_CONST c2 BINOP
448 with LOAD_CONST binop(c1,c2)
449 The consts table must still be in list form so that the
450 new constant can be appended.
451 Called with codestr pointing to the first LOAD_CONST.
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000452 Abandons the transformation if the folding fails (i.e. 1+'a').
453 If the new constant is a sequence, only folds when the size
454 is below a threshold value. That keeps pyc files from
455 becoming large in the presence of code like: (None,)*1000.
456*/
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000457static int
458fold_binops_on_constants(unsigned char *codestr, PyObject *consts)
459{
460 PyObject *newconst, *v, *w;
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000461 int len_consts, opcode, size;
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000462
463 /* Pre-conditions */
464 assert(PyList_CheckExact(consts));
465 assert(codestr[0] == LOAD_CONST);
466 assert(codestr[3] == LOAD_CONST);
467
468 /* Create new constant */
469 v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
470 w = PyList_GET_ITEM(consts, GETARG(codestr, 3));
471 opcode = codestr[6];
472 switch (opcode) {
473 case BINARY_POWER:
474 newconst = PyNumber_Power(v, w, Py_None);
475 break;
476 case BINARY_MULTIPLY:
477 newconst = PyNumber_Multiply(v, w);
478 break;
479 case BINARY_DIVIDE:
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000480 /* Cannot fold this operation statically since
481 the result can depend on the run-time presence of the -Qnew flag */
Armin Rigo664b43b2005-01-07 18:10:51 +0000482 return 0;
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000483 case BINARY_TRUE_DIVIDE:
484 newconst = PyNumber_TrueDivide(v, w);
485 break;
486 case BINARY_FLOOR_DIVIDE:
487 newconst = PyNumber_FloorDivide(v, w);
488 break;
489 case BINARY_MODULO:
490 newconst = PyNumber_Remainder(v, w);
491 break;
492 case BINARY_ADD:
493 newconst = PyNumber_Add(v, w);
494 break;
495 case BINARY_SUBTRACT:
496 newconst = PyNumber_Subtract(v, w);
497 break;
498 case BINARY_SUBSCR:
499 newconst = PyObject_GetItem(v, w);
500 break;
501 case BINARY_LSHIFT:
502 newconst = PyNumber_Lshift(v, w);
503 break;
504 case BINARY_RSHIFT:
505 newconst = PyNumber_Rshift(v, w);
506 break;
507 case BINARY_AND:
508 newconst = PyNumber_And(v, w);
509 break;
510 case BINARY_XOR:
511 newconst = PyNumber_Xor(v, w);
512 break;
513 case BINARY_OR:
514 newconst = PyNumber_Or(v, w);
515 break;
516 default:
517 /* Called with an unknown opcode */
Neal Norwitz4737b232005-11-19 23:58:29 +0000518 PyErr_Format(PyExc_SystemError,
519 "unexpected binary operation %d on a constant",
520 opcode);
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000521 return 0;
522 }
523 if (newconst == NULL) {
524 PyErr_Clear();
525 return 0;
526 }
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000527 size = PyObject_Size(newconst);
528 if (size == -1)
529 PyErr_Clear();
530 else if (size > 20) {
531 Py_DECREF(newconst);
532 return 0;
533 }
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000534
535 /* Append folded constant into consts table */
536 len_consts = PyList_GET_SIZE(consts);
537 if (PyList_Append(consts, newconst)) {
538 Py_DECREF(newconst);
539 return 0;
540 }
541 Py_DECREF(newconst);
542
543 /* Write NOP NOP NOP NOP LOAD_CONST newconst */
544 memset(codestr, NOP, 4);
545 codestr[4] = LOAD_CONST;
546 SETARG(codestr, 4, len_consts);
547 return 1;
548}
549
Raymond Hettinger80121492005-02-20 12:41:32 +0000550static int
551fold_unaryops_on_constants(unsigned char *codestr, PyObject *consts)
552{
Raymond Hettingere63a0782005-02-23 13:37:55 +0000553 PyObject *newconst=NULL, *v;
Raymond Hettinger80121492005-02-20 12:41:32 +0000554 int len_consts, opcode;
555
556 /* Pre-conditions */
557 assert(PyList_CheckExact(consts));
558 assert(codestr[0] == LOAD_CONST);
559
560 /* Create new constant */
561 v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
562 opcode = codestr[3];
563 switch (opcode) {
564 case UNARY_NEGATIVE:
Raymond Hettingere63a0782005-02-23 13:37:55 +0000565 /* Preserve the sign of -0.0 */
566 if (PyObject_IsTrue(v) == 1)
567 newconst = PyNumber_Negative(v);
Raymond Hettinger80121492005-02-20 12:41:32 +0000568 break;
569 case UNARY_CONVERT:
570 newconst = PyObject_Repr(v);
571 break;
572 case UNARY_INVERT:
573 newconst = PyNumber_Invert(v);
574 break;
575 default:
576 /* Called with an unknown opcode */
Neal Norwitz4737b232005-11-19 23:58:29 +0000577 PyErr_Format(PyExc_SystemError,
578 "unexpected unary operation %d on a constant",
579 opcode);
Raymond Hettinger80121492005-02-20 12:41:32 +0000580 return 0;
581 }
582 if (newconst == NULL) {
583 PyErr_Clear();
584 return 0;
585 }
586
587 /* Append folded constant into consts table */
588 len_consts = PyList_GET_SIZE(consts);
589 if (PyList_Append(consts, newconst)) {
590 Py_DECREF(newconst);
591 return 0;
592 }
593 Py_DECREF(newconst);
594
595 /* Write NOP LOAD_CONST newconst */
596 codestr[0] = NOP;
597 codestr[1] = LOAD_CONST;
598 SETARG(codestr, 1, len_consts);
599 return 1;
600}
601
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000602static unsigned int *
603markblocks(unsigned char *code, int len)
604{
605 unsigned int *blocks = PyMem_Malloc(len*sizeof(int));
Raymond Hettingereffb3932004-10-30 08:55:08 +0000606 int i,j, opcode, blockcnt = 0;
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000607
608 if (blocks == NULL)
609 return NULL;
610 memset(blocks, 0, len*sizeof(int));
Raymond Hettingereffb3932004-10-30 08:55:08 +0000611
612 /* Mark labels in the first pass */
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000613 for (i=0 ; i<len ; i+=CODESIZE(opcode)) {
614 opcode = code[i];
615 switch (opcode) {
616 case FOR_ITER:
617 case JUMP_FORWARD:
618 case JUMP_IF_FALSE:
619 case JUMP_IF_TRUE:
620 case JUMP_ABSOLUTE:
621 case CONTINUE_LOOP:
622 case SETUP_LOOP:
623 case SETUP_EXCEPT:
624 case SETUP_FINALLY:
625 j = GETJUMPTGT(code, i);
Raymond Hettingereffb3932004-10-30 08:55:08 +0000626 blocks[j] = 1;
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000627 break;
628 }
629 }
Raymond Hettingereffb3932004-10-30 08:55:08 +0000630 /* Build block numbers in the second pass */
631 for (i=0 ; i<len ; i++) {
632 blockcnt += blocks[i]; /* increment blockcnt over labels */
633 blocks[i] = blockcnt;
634 }
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000635 return blocks;
636}
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000637
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000638/* Perform basic peephole optimizations to components of a code object.
639 The consts object should still be in list form to allow new constants
640 to be appended.
641
642 To keep the optimizer simple, it bails out (does nothing) for code
643 containing extended arguments or that has a length over 32,700. That
644 allows us to avoid overflow and sign issues. Likewise, it bails when
645 the lineno table has complex encoding for gaps >= 255.
646
647 Optimizations are restricted to simple transformations occuring within a
648 single basic block. All transformations keep the code size the same or
649 smaller. For those that reduce size, the gaps are initially filled with
650 NOPs. Later those NOPs are removed and the jump addresses retargeted in
651 a single pass. Line numbering is adjusted accordingly. */
652
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000653static PyObject *
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000654optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *lineno_obj)
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000655{
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000656 int i, j, codelen, nops, h, adj;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000657 int tgt, tgttgt, opcode;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000658 unsigned char *codestr = NULL;
659 unsigned char *lineno;
660 int *addrmap = NULL;
661 int new_line, cum_orig_line, last_line, tabsiz;
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000662 int cumlc=0, lastlc=0; /* Count runs of consecutive LOAD_CONST codes */
Raymond Hettingereffb3932004-10-30 08:55:08 +0000663 unsigned int *blocks = NULL;
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000664 char *name;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000665
Raymond Hettingereffb3932004-10-30 08:55:08 +0000666 /* Bail out if an exception is set */
667 if (PyErr_Occurred())
668 goto exitUnchanged;
669
Raymond Hettinger1792bfb2004-08-25 17:19:38 +0000670 /* Bypass optimization when the lineno table is too complex */
671 assert(PyString_Check(lineno_obj));
Brett Cannonc9371d42005-06-25 08:23:41 +0000672 lineno = (unsigned char*)PyString_AS_STRING(lineno_obj);
Raymond Hettinger1792bfb2004-08-25 17:19:38 +0000673 tabsiz = PyString_GET_SIZE(lineno_obj);
674 if (memchr(lineno, 255, tabsiz) != NULL)
675 goto exitUnchanged;
676
Raymond Hettingera12fa142004-08-24 04:34:16 +0000677 /* Avoid situations where jump retargeting could overflow */
Raymond Hettinger06cc9732004-09-28 17:22:12 +0000678 assert(PyString_Check(code));
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000679 codelen = PyString_Size(code);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000680 if (codelen > 32700)
Raymond Hettingera12fa142004-08-24 04:34:16 +0000681 goto exitUnchanged;
682
683 /* Make a modifiable copy of the code string */
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000684 codestr = PyMem_Malloc(codelen);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000685 if (codestr == NULL)
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000686 goto exitUnchanged;
687 codestr = memcpy(codestr, PyString_AS_STRING(code), codelen);
Raymond Hettinger98bd1812004-08-06 19:46:34 +0000688
Raymond Hettinger07359a72005-02-21 20:03:14 +0000689 /* Verify that RETURN_VALUE terminates the codestring. This allows
690 the various transformation patterns to look ahead several
691 instructions without additional checks to make sure they are not
692 looking beyond the end of the code string.
693 */
694 if (codestr[codelen-1] != RETURN_VALUE)
695 goto exitUnchanged;
696
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000697 /* Mapping to new jump targets after NOPs are removed */
698 addrmap = PyMem_Malloc(codelen * sizeof(int));
699 if (addrmap == NULL)
700 goto exitUnchanged;
701
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000702 blocks = markblocks(codestr, codelen);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000703 if (blocks == NULL)
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000704 goto exitUnchanged;
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000705 assert(PyList_Check(consts));
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000706
Raymond Hettinger099ecfb2004-11-01 15:19:11 +0000707 for (i=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000708 opcode = codestr[i];
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000709
710 lastlc = cumlc;
711 cumlc = 0;
712
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000713 switch (opcode) {
714
Raymond Hettinger43ea47f2004-06-24 09:25:39 +0000715 /* Replace UNARY_NOT JUMP_IF_FALSE POP_TOP with
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000716 with JUMP_IF_TRUE POP_TOP */
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000717 case UNARY_NOT:
718 if (codestr[i+1] != JUMP_IF_FALSE ||
719 codestr[i+4] != POP_TOP ||
720 !ISBASICBLOCK(blocks,i,5))
721 continue;
722 tgt = GETJUMPTGT(codestr, (i+1));
723 if (codestr[tgt] != POP_TOP)
724 continue;
Raymond Hettinger43ea47f2004-06-24 09:25:39 +0000725 j = GETARG(codestr, i+1) + 1;
726 codestr[i] = JUMP_IF_TRUE;
727 SETARG(codestr, i, j);
728 codestr[i+3] = POP_TOP;
729 codestr[i+4] = NOP;
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000730 break;
731
732 /* not a is b --> a is not b
733 not a in b --> a not in b
734 not a is not b --> a is b
Raymond Hettingerb615bf02005-02-10 01:42:32 +0000735 not a not in b --> a in b
Raymond Hettingera1645742005-02-06 22:05:42 +0000736 */
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000737 case COMPARE_OP:
738 j = GETARG(codestr, i);
739 if (j < 6 || j > 9 ||
740 codestr[i+3] != UNARY_NOT ||
741 !ISBASICBLOCK(blocks,i,4))
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000742 continue;
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000743 SETARG(codestr, i, (j^1));
744 codestr[i+3] = NOP;
Tim Petersdb5860b2004-07-17 05:00:52 +0000745 break;
746
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000747 /* Replace LOAD_GLOBAL/LOAD_NAME None with LOAD_CONST None */
748 case LOAD_NAME:
749 case LOAD_GLOBAL:
750 j = GETARG(codestr, i);
751 name = PyString_AsString(PyTuple_GET_ITEM(names, j));
752 if (name == NULL || strcmp(name, "None") != 0)
753 continue;
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000754 for (j=0 ; j < PyList_GET_SIZE(consts) ; j++) {
755 if (PyList_GET_ITEM(consts, j) == Py_None) {
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000756 codestr[i] = LOAD_CONST;
757 SETARG(codestr, i, j);
Raymond Hettinger5dec0962004-11-02 04:20:10 +0000758 cumlc = lastlc + 1;
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000759 break;
760 }
761 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000762 break;
763
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000764 /* Skip over LOAD_CONST trueconst JUMP_IF_FALSE xx POP_TOP */
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000765 case LOAD_CONST:
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000766 cumlc = lastlc + 1;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000767 j = GETARG(codestr, i);
768 if (codestr[i+3] != JUMP_IF_FALSE ||
769 codestr[i+6] != POP_TOP ||
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000770 !ISBASICBLOCK(blocks,i,7) ||
771 !PyObject_IsTrue(PyList_GET_ITEM(consts, j)))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000772 continue;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000773 memset(codestr+i, NOP, 7);
Raymond Hettinger5dec0962004-11-02 04:20:10 +0000774 cumlc = 0;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000775 break;
776
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000777 /* Try to fold tuples of constants (includes a case for lists
778 which are only used for "in" and "not in" tests).
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000779 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000780 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
781 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000782 case BUILD_TUPLE:
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000783 case BUILD_LIST:
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000784 j = GETARG(codestr, i);
785 h = i - 3 * j;
786 if (h >= 0 &&
Raymond Hettingereffb3932004-10-30 08:55:08 +0000787 j <= lastlc &&
Brett Cannon5dc8ced2005-03-03 07:01:48 +0000788 ((opcode == BUILD_TUPLE &&
789 ISBASICBLOCK(blocks, h, 3*(j+1))) ||
790 (opcode == BUILD_LIST &&
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000791 codestr[i+3]==COMPARE_OP &&
792 ISBASICBLOCK(blocks, h, 3*(j+2)) &&
Brett Cannon5dc8ced2005-03-03 07:01:48 +0000793 (GETARG(codestr,i+3)==6 ||
794 GETARG(codestr,i+3)==7))) &&
795 tuple_of_constants(&codestr[h], j, consts)) {
Raymond Hettinger5dec0962004-11-02 04:20:10 +0000796 assert(codestr[i] == LOAD_CONST);
797 cumlc = 1;
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000798 break;
799 }
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000800 if (codestr[i+3] != UNPACK_SEQUENCE ||
801 !ISBASICBLOCK(blocks,i,6) ||
802 j != GETARG(codestr, i+3))
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000803 continue;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000804 if (j == 1) {
805 memset(codestr+i, NOP, 6);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000806 } else if (j == 2) {
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000807 codestr[i] = ROT_TWO;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000808 memset(codestr+i+1, NOP, 5);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000809 } else if (j == 3) {
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000810 codestr[i] = ROT_THREE;
811 codestr[i+1] = ROT_TWO;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000812 memset(codestr+i+2, NOP, 4);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000813 }
814 break;
815
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000816 /* Fold binary ops on constants.
817 LOAD_CONST c1 LOAD_CONST c2 BINOP --> LOAD_CONST binop(c1,c2) */
818 case BINARY_POWER:
819 case BINARY_MULTIPLY:
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000820 case BINARY_TRUE_DIVIDE:
821 case BINARY_FLOOR_DIVIDE:
822 case BINARY_MODULO:
823 case BINARY_ADD:
824 case BINARY_SUBTRACT:
825 case BINARY_SUBSCR:
826 case BINARY_LSHIFT:
827 case BINARY_RSHIFT:
828 case BINARY_AND:
829 case BINARY_XOR:
830 case BINARY_OR:
831 if (lastlc >= 2 &&
832 ISBASICBLOCK(blocks, i-6, 7) &&
833 fold_binops_on_constants(&codestr[i-6], consts)) {
834 i -= 2;
835 assert(codestr[i] == LOAD_CONST);
836 cumlc = 1;
837 }
838 break;
839
Raymond Hettinger80121492005-02-20 12:41:32 +0000840 /* Fold unary ops on constants.
841 LOAD_CONST c1 UNARY_OP --> LOAD_CONST unary_op(c) */
842 case UNARY_NEGATIVE:
843 case UNARY_CONVERT:
844 case UNARY_INVERT:
845 if (lastlc >= 1 &&
846 ISBASICBLOCK(blocks, i-3, 4) &&
847 fold_unaryops_on_constants(&codestr[i-3], consts)) {
848 i -= 2;
849 assert(codestr[i] == LOAD_CONST);
850 cumlc = 1;
851 }
852 break;
853
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000854 /* Simplify conditional jump to conditional jump where the
855 result of the first test implies the success of a similar
856 test or the failure of the opposite test.
857 Arises in code like:
Raymond Hettingereffb3932004-10-30 08:55:08 +0000858 "if a and b:"
859 "if a or b:"
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000860 "a and b or c"
Armin Rigod7bcf4d2004-10-30 21:08:59 +0000861 "(a and b) and c"
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000862 x:JUMP_IF_FALSE y y:JUMP_IF_FALSE z --> x:JUMP_IF_FALSE z
Raymond Hettinger65d3c052004-08-25 15:15:56 +0000863 x:JUMP_IF_FALSE y y:JUMP_IF_TRUE z --> x:JUMP_IF_FALSE y+3
864 where y+3 is the instruction following the second test.
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000865 */
866 case JUMP_IF_FALSE:
867 case JUMP_IF_TRUE:
868 tgt = GETJUMPTGT(codestr, i);
869 j = codestr[tgt];
870 if (j == JUMP_IF_FALSE || j == JUMP_IF_TRUE) {
871 if (j == opcode) {
872 tgttgt = GETJUMPTGT(codestr, tgt) - i - 3;
873 SETARG(codestr, i, tgttgt);
874 } else {
875 tgt -= i;
876 SETARG(codestr, i, tgt);
877 }
878 break;
879 }
880 /* Intentional fallthrough */
881
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000882 /* Replace jumps to unconditional jumps */
Raymond Hettinger255a3d02003-04-15 10:35:07 +0000883 case FOR_ITER:
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000884 case JUMP_FORWARD:
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000885 case JUMP_ABSOLUTE:
886 case CONTINUE_LOOP:
887 case SETUP_LOOP:
888 case SETUP_EXCEPT:
889 case SETUP_FINALLY:
890 tgt = GETJUMPTGT(codestr, i);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000891 if (!UNCONDITIONAL_JUMP(codestr[tgt]))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000892 continue;
893 tgttgt = GETJUMPTGT(codestr, tgt);
894 if (opcode == JUMP_FORWARD) /* JMP_ABS can go backwards */
895 opcode = JUMP_ABSOLUTE;
Raymond Hettinger5b75c382003-03-28 12:05:00 +0000896 if (!ABSOLUTE_JUMP(opcode))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000897 tgttgt -= i + 3; /* Calc relative jump addr */
898 if (tgttgt < 0) /* No backward relative jumps */
899 continue;
900 codestr[i] = opcode;
901 SETARG(codestr, i, tgttgt);
902 break;
903
904 case EXTENDED_ARG:
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000905 goto exitUnchanged;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000906
907 /* Replace RETURN LOAD_CONST None RETURN with just RETURN */
908 case RETURN_VALUE:
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000909 if (i+4 >= codelen ||
910 codestr[i+4] != RETURN_VALUE ||
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000911 !ISBASICBLOCK(blocks,i,5))
912 continue;
913 memset(codestr+i+1, NOP, 4);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000914 break;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000915 }
916 }
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000917
918 /* Fixup linenotab */
Raymond Hettinger099ecfb2004-11-01 15:19:11 +0000919 for (i=0, nops=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
920 addrmap[i] = i - nops;
921 if (codestr[i] == NOP)
922 nops++;
923 }
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000924 cum_orig_line = 0;
925 last_line = 0;
926 for (i=0 ; i < tabsiz ; i+=2) {
927 cum_orig_line += lineno[i];
928 new_line = addrmap[cum_orig_line];
Raymond Hettinger1792bfb2004-08-25 17:19:38 +0000929 assert (new_line - last_line < 255);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000930 lineno[i] =((unsigned char)(new_line - last_line));
931 last_line = new_line;
932 }
933
934 /* Remove NOPs and fixup jump targets */
935 for (i=0, h=0 ; i<codelen ; ) {
936 opcode = codestr[i];
937 switch (opcode) {
938 case NOP:
939 i++;
940 continue;
941
942 case JUMP_ABSOLUTE:
943 case CONTINUE_LOOP:
944 j = addrmap[GETARG(codestr, i)];
945 SETARG(codestr, i, j);
946 break;
947
948 case FOR_ITER:
949 case JUMP_FORWARD:
950 case JUMP_IF_FALSE:
951 case JUMP_IF_TRUE:
952 case SETUP_LOOP:
953 case SETUP_EXCEPT:
954 case SETUP_FINALLY:
955 j = addrmap[GETARG(codestr, i) + i + 3] - addrmap[i] - 3;
956 SETARG(codestr, i, j);
957 break;
958 }
959 adj = CODESIZE(opcode);
960 while (adj--)
961 codestr[h++] = codestr[i++];
962 }
Raymond Hettingera12fa142004-08-24 04:34:16 +0000963 assert(h + nops == codelen);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000964
965 code = PyString_FromStringAndSize((char *)codestr, h);
966 PyMem_Free(addrmap);
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000967 PyMem_Free(codestr);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000968 PyMem_Free(blocks);
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000969 return code;
970
971exitUnchanged:
Raymond Hettingereffb3932004-10-30 08:55:08 +0000972 if (blocks != NULL)
973 PyMem_Free(blocks);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000974 if (addrmap != NULL)
975 PyMem_Free(addrmap);
976 if (codestr != NULL)
977 PyMem_Free(codestr);
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000978 Py_INCREF(code);
979 return code;
980}
981
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000982/* End: Peephole optimizations ----------------------------------------- */
983
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984/*
985
986Leave this debugging code for just a little longer.
987
988static void
989compiler_display_symbols(PyObject *name, PyObject *symbols)
990{
991 PyObject *key, *value;
992 int flags, pos = 0;
993
994 fprintf(stderr, "block %s\n", PyString_AS_STRING(name));
995 while (PyDict_Next(symbols, &pos, &key, &value)) {
996 flags = PyInt_AsLong(value);
997 fprintf(stderr, "var %s:", PyString_AS_STRING(key));
998 if (flags & DEF_GLOBAL)
999 fprintf(stderr, " declared_global");
1000 if (flags & DEF_LOCAL)
1001 fprintf(stderr, " local");
1002 if (flags & DEF_PARAM)
1003 fprintf(stderr, " param");
1004 if (flags & DEF_STAR)
1005 fprintf(stderr, " stararg");
1006 if (flags & DEF_DOUBLESTAR)
1007 fprintf(stderr, " starstar");
1008 if (flags & DEF_INTUPLE)
1009 fprintf(stderr, " tuple");
1010 if (flags & DEF_FREE)
1011 fprintf(stderr, " free");
1012 if (flags & DEF_FREE_GLOBAL)
1013 fprintf(stderr, " global");
1014 if (flags & DEF_FREE_CLASS)
1015 fprintf(stderr, " free/class");
1016 if (flags & DEF_IMPORT)
1017 fprintf(stderr, " import");
1018 fprintf(stderr, "\n");
1019 }
1020 fprintf(stderr, "\n");
1021}
1022*/
1023
1024static void
1025compiler_unit_check(struct compiler_unit *u)
1026{
1027 basicblock *block;
1028 for (block = u->u_blocks; block != NULL; block = block->b_list) {
1029 assert(block != (void *)0xcbcbcbcb);
1030 assert(block != (void *)0xfbfbfbfb);
1031 assert(block != (void *)0xdbdbdbdb);
1032 if (block->b_instr != NULL) {
1033 assert(block->b_ialloc > 0);
1034 assert(block->b_iused > 0);
1035 assert(block->b_ialloc >= block->b_iused);
1036 }
1037 else {
1038 assert (block->b_iused == 0);
1039 assert (block->b_ialloc == 0);
1040 }
1041 }
1042}
1043
1044static void
1045compiler_unit_free(struct compiler_unit *u)
1046{
1047 basicblock *b, *next;
1048
1049 compiler_unit_check(u);
1050 b = u->u_blocks;
1051 while (b != NULL) {
1052 if (b->b_instr)
1053 PyObject_Free((void *)b->b_instr);
1054 next = b->b_list;
1055 PyObject_Free((void *)b);
1056 b = next;
1057 }
1058 Py_XDECREF(u->u_ste);
1059 Py_XDECREF(u->u_name);
1060 Py_XDECREF(u->u_consts);
1061 Py_XDECREF(u->u_names);
1062 Py_XDECREF(u->u_varnames);
1063 Py_XDECREF(u->u_freevars);
1064 Py_XDECREF(u->u_cellvars);
1065 Py_XDECREF(u->u_private);
1066 PyObject_Free(u);
1067}
1068
1069static int
1070compiler_enter_scope(struct compiler *c, identifier name, void *key,
1071 int lineno)
1072{
1073 struct compiler_unit *u;
1074
1075 u = PyObject_Malloc(sizeof(struct compiler_unit));
Neal Norwitz87b801c2005-12-18 04:42:47 +00001076 if (!u) {
1077 PyErr_NoMemory();
1078 return 0;
1079 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001080 memset(u, 0, sizeof(struct compiler_unit));
1081 u->u_argcount = 0;
1082 u->u_ste = PySymtable_Lookup(c->c_st, key);
1083 if (!u->u_ste) {
1084 compiler_unit_free(u);
Neal Norwitz87b801c2005-12-18 04:42:47 +00001085 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086 }
1087 Py_INCREF(name);
1088 u->u_name = name;
1089 u->u_varnames = list2dict(u->u_ste->ste_varnames);
1090 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
1091 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
1092 PyDict_Size(u->u_cellvars));
1093
1094 u->u_blocks = NULL;
1095 u->u_tmpname = 0;
1096 u->u_nfblocks = 0;
1097 u->u_firstlineno = lineno;
1098 u->u_lineno = 0;
1099 u->u_lineno_set = false;
1100 u->u_consts = PyDict_New();
1101 if (!u->u_consts) {
1102 compiler_unit_free(u);
1103 return 0;
1104 }
1105 u->u_names = PyDict_New();
1106 if (!u->u_names) {
1107 compiler_unit_free(u);
1108 return 0;
1109 }
1110
1111 u->u_private = NULL;
1112
1113 /* Push the old compiler_unit on the stack. */
1114 if (c->u) {
1115 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
1116 if (PyList_Append(c->c_stack, wrapper) < 0) {
1117 compiler_unit_free(u);
1118 return 0;
1119 }
1120 Py_DECREF(wrapper);
1121 u->u_private = c->u->u_private;
1122 Py_XINCREF(u->u_private);
1123 }
1124 c->u = u;
1125
1126 c->c_nestlevel++;
Martin v. Löwis94962612006-01-02 21:15:05 +00001127 if (compiler_use_new_block(c) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128 return 0;
1129
1130 return 1;
1131}
1132
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001133static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134compiler_exit_scope(struct compiler *c)
1135{
1136 int n;
1137 PyObject *wrapper;
1138
1139 c->c_nestlevel--;
1140 compiler_unit_free(c->u);
1141 /* Restore c->u to the parent unit. */
1142 n = PyList_GET_SIZE(c->c_stack) - 1;
1143 if (n >= 0) {
1144 wrapper = PyList_GET_ITEM(c->c_stack, n);
1145 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001146 /* we are deleting from a list so this really shouldn't fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 if (PySequence_DelItem(c->c_stack, n) < 0)
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001148 Py_FatalError("compiler_exit_scope()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001149 compiler_unit_check(c->u);
1150 }
1151 else
1152 c->u = NULL;
1153
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154}
1155
1156/* Allocate a new block and return a pointer to it.
1157 Returns NULL on error.
1158*/
1159
1160static basicblock *
1161compiler_new_block(struct compiler *c)
1162{
1163 basicblock *b;
1164 struct compiler_unit *u;
1165
1166 u = c->u;
1167 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
Neal Norwitz87b801c2005-12-18 04:42:47 +00001168 if (b == NULL) {
1169 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001170 return NULL;
Neal Norwitz87b801c2005-12-18 04:42:47 +00001171 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172 memset((void *)b, 0, sizeof(basicblock));
1173 assert (b->b_next == NULL);
1174 b->b_list = u->u_blocks;
1175 u->u_blocks = b;
1176 return b;
1177}
1178
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179static basicblock *
1180compiler_use_new_block(struct compiler *c)
1181{
1182 basicblock *block = compiler_new_block(c);
1183 if (block == NULL)
1184 return NULL;
1185 c->u->u_curblock = block;
1186 return block;
1187}
1188
1189static basicblock *
1190compiler_next_block(struct compiler *c)
1191{
1192 basicblock *block = compiler_new_block(c);
1193 if (block == NULL)
1194 return NULL;
1195 c->u->u_curblock->b_next = block;
1196 c->u->u_curblock = block;
1197 return block;
1198}
1199
1200static basicblock *
1201compiler_use_next_block(struct compiler *c, basicblock *block)
1202{
1203 assert(block != NULL);
1204 c->u->u_curblock->b_next = block;
1205 c->u->u_curblock = block;
1206 return block;
1207}
1208
1209/* Returns the offset of the next instruction in the current block's
1210 b_instr array. Resizes the b_instr as necessary.
1211 Returns -1 on failure.
1212 */
1213
1214static int
1215compiler_next_instr(struct compiler *c, basicblock *b)
1216{
1217 assert(b != NULL);
1218 if (b->b_instr == NULL) {
1219 b->b_instr = PyObject_Malloc(sizeof(struct instr) *
1220 DEFAULT_BLOCK_SIZE);
1221 if (b->b_instr == NULL) {
1222 PyErr_NoMemory();
1223 return -1;
1224 }
1225 b->b_ialloc = DEFAULT_BLOCK_SIZE;
1226 memset((char *)b->b_instr, 0,
1227 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
1228 }
1229 else if (b->b_iused == b->b_ialloc) {
1230 size_t oldsize, newsize;
1231 oldsize = b->b_ialloc * sizeof(struct instr);
1232 newsize = oldsize << 1;
1233 if (newsize == 0) {
1234 PyErr_NoMemory();
1235 return -1;
1236 }
1237 b->b_ialloc <<= 1;
1238 b->b_instr = PyObject_Realloc((void *)b->b_instr, newsize);
1239 if (b->b_instr == NULL)
1240 return -1;
1241 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
1242 }
1243 return b->b_iused++;
1244}
1245
1246static void
1247compiler_set_lineno(struct compiler *c, int off)
1248{
1249 basicblock *b;
1250 if (c->u->u_lineno_set)
1251 return;
1252 c->u->u_lineno_set = true;
1253 b = c->u->u_curblock;
1254 b->b_instr[off].i_lineno = c->u->u_lineno;
1255}
1256
1257static int
1258opcode_stack_effect(int opcode, int oparg)
1259{
1260 switch (opcode) {
1261 case POP_TOP:
1262 return -1;
1263 case ROT_TWO:
1264 case ROT_THREE:
1265 return 0;
1266 case DUP_TOP:
1267 return 1;
1268 case ROT_FOUR:
1269 return 0;
1270
1271 case UNARY_POSITIVE:
1272 case UNARY_NEGATIVE:
1273 case UNARY_NOT:
1274 case UNARY_CONVERT:
1275 case UNARY_INVERT:
1276 return 0;
1277
1278 case BINARY_POWER:
1279 case BINARY_MULTIPLY:
1280 case BINARY_DIVIDE:
1281 case BINARY_MODULO:
1282 case BINARY_ADD:
1283 case BINARY_SUBTRACT:
1284 case BINARY_SUBSCR:
1285 case BINARY_FLOOR_DIVIDE:
1286 case BINARY_TRUE_DIVIDE:
1287 return -1;
1288 case INPLACE_FLOOR_DIVIDE:
1289 case INPLACE_TRUE_DIVIDE:
1290 return -1;
1291
1292 case SLICE+0:
1293 return 1;
1294 case SLICE+1:
1295 return 0;
1296 case SLICE+2:
1297 return 0;
1298 case SLICE+3:
1299 return -1;
1300
1301 case STORE_SLICE+0:
1302 return -2;
1303 case STORE_SLICE+1:
1304 return -3;
1305 case STORE_SLICE+2:
1306 return -3;
1307 case STORE_SLICE+3:
1308 return -4;
1309
1310 case DELETE_SLICE+0:
1311 return -1;
1312 case DELETE_SLICE+1:
1313 return -2;
1314 case DELETE_SLICE+2:
1315 return -2;
1316 case DELETE_SLICE+3:
1317 return -3;
1318
1319 case INPLACE_ADD:
1320 case INPLACE_SUBTRACT:
1321 case INPLACE_MULTIPLY:
1322 case INPLACE_DIVIDE:
1323 case INPLACE_MODULO:
1324 return -1;
1325 case STORE_SUBSCR:
1326 return -3;
1327 case DELETE_SUBSCR:
1328 return -2;
1329
1330 case BINARY_LSHIFT:
1331 case BINARY_RSHIFT:
1332 case BINARY_AND:
1333 case BINARY_XOR:
1334 case BINARY_OR:
1335 return -1;
1336 case INPLACE_POWER:
1337 return -1;
1338 case GET_ITER:
1339 return 0;
1340
1341 case PRINT_EXPR:
1342 return -1;
1343 case PRINT_ITEM:
1344 return -1;
1345 case PRINT_NEWLINE:
1346 return 0;
1347 case PRINT_ITEM_TO:
1348 return -2;
1349 case PRINT_NEWLINE_TO:
1350 return -1;
1351 case INPLACE_LSHIFT:
1352 case INPLACE_RSHIFT:
1353 case INPLACE_AND:
1354 case INPLACE_XOR:
1355 case INPLACE_OR:
1356 return -1;
1357 case BREAK_LOOP:
1358 return 0;
1359
1360 case LOAD_LOCALS:
1361 return 1;
1362 case RETURN_VALUE:
1363 return -1;
1364 case IMPORT_STAR:
1365 return -1;
1366 case EXEC_STMT:
1367 return -3;
1368 case YIELD_VALUE:
1369 return 0;
1370
1371 case POP_BLOCK:
1372 return 0;
1373 case END_FINALLY:
1374 return -1; /* or -2 or -3 if exception occurred */
1375 case BUILD_CLASS:
1376 return -2;
1377
1378 case STORE_NAME:
1379 return -1;
1380 case DELETE_NAME:
1381 return 0;
1382 case UNPACK_SEQUENCE:
1383 return oparg-1;
1384 case FOR_ITER:
1385 return 1;
1386
1387 case STORE_ATTR:
1388 return -2;
1389 case DELETE_ATTR:
1390 return -1;
1391 case STORE_GLOBAL:
1392 return -1;
1393 case DELETE_GLOBAL:
1394 return 0;
1395 case DUP_TOPX:
1396 return oparg;
1397 case LOAD_CONST:
1398 return 1;
1399 case LOAD_NAME:
1400 return 1;
1401 case BUILD_TUPLE:
1402 case BUILD_LIST:
1403 return 1-oparg;
1404 case BUILD_MAP:
1405 return 1;
1406 case LOAD_ATTR:
1407 return 0;
1408 case COMPARE_OP:
1409 return -1;
1410 case IMPORT_NAME:
1411 return 0;
1412 case IMPORT_FROM:
1413 return 1;
1414
1415 case JUMP_FORWARD:
1416 case JUMP_IF_FALSE:
1417 case JUMP_IF_TRUE:
1418 case JUMP_ABSOLUTE:
1419 return 0;
1420
1421 case LOAD_GLOBAL:
1422 return 1;
1423
1424 case CONTINUE_LOOP:
1425 return 0;
1426 case SETUP_LOOP:
1427 return 0;
1428 case SETUP_EXCEPT:
1429 case SETUP_FINALLY:
1430 return 3; /* actually pushed by an exception */
1431
1432 case LOAD_FAST:
1433 return 1;
1434 case STORE_FAST:
1435 return -1;
1436 case DELETE_FAST:
1437 return 0;
1438
1439 case RAISE_VARARGS:
1440 return -oparg;
1441#define NARGS(o) (((o) % 256) + 2*((o) / 256))
1442 case CALL_FUNCTION:
1443 return -NARGS(oparg);
1444 case CALL_FUNCTION_VAR:
1445 case CALL_FUNCTION_KW:
1446 return -NARGS(oparg)-1;
1447 case CALL_FUNCTION_VAR_KW:
1448 return -NARGS(oparg)-2;
1449#undef NARGS
1450 case MAKE_FUNCTION:
1451 return -oparg;
1452 case BUILD_SLICE:
1453 if (oparg == 3)
1454 return -2;
1455 else
1456 return -1;
1457
1458 case MAKE_CLOSURE:
1459 return -oparg;
1460 case LOAD_CLOSURE:
1461 return 1;
1462 case LOAD_DEREF:
1463 return 1;
1464 case STORE_DEREF:
1465 return -1;
1466 default:
1467 fprintf(stderr, "opcode = %d\n", opcode);
1468 Py_FatalError("opcode_stack_effect()");
1469
1470 }
1471 return 0; /* not reachable */
1472}
1473
1474/* Add an opcode with no argument.
1475 Returns 0 on failure, 1 on success.
1476*/
1477
1478static int
1479compiler_addop(struct compiler *c, int opcode)
1480{
1481 basicblock *b;
1482 struct instr *i;
1483 int off;
1484 off = compiler_next_instr(c, c->u->u_curblock);
1485 if (off < 0)
1486 return 0;
1487 b = c->u->u_curblock;
1488 i = &b->b_instr[off];
1489 i->i_opcode = opcode;
1490 i->i_hasarg = 0;
1491 if (opcode == RETURN_VALUE)
1492 b->b_return = 1;
1493 compiler_set_lineno(c, off);
1494 return 1;
1495}
1496
1497static int
1498compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
1499{
1500 PyObject *t, *v;
1501 int arg;
1502
1503 /* necessary to make sure types aren't coerced (e.g., int and long) */
Neil Schemenauer3a44aaa2005-10-23 17:21:54 +00001504 t = PyTuple_Pack(2, o, o->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505 if (t == NULL)
1506 return -1;
1507
1508 v = PyDict_GetItem(dict, t);
1509 if (!v) {
1510 arg = PyDict_Size(dict);
1511 v = PyInt_FromLong(arg);
1512 if (!v) {
1513 Py_DECREF(t);
1514 return -1;
1515 }
1516 if (PyDict_SetItem(dict, t, v) < 0) {
1517 Py_DECREF(t);
1518 Py_DECREF(v);
1519 return -1;
1520 }
1521 Py_DECREF(v);
1522 }
1523 else
1524 arg = PyInt_AsLong(v);
1525 Py_DECREF(t);
1526 return arg;
1527}
1528
1529static int
1530compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
1531 PyObject *o)
1532{
1533 int arg = compiler_add_o(c, dict, o);
1534 if (arg < 0)
1535 return 0;
1536 return compiler_addop_i(c, opcode, arg);
1537}
1538
1539static int
1540compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
1541 PyObject *o)
1542{
1543 int arg;
1544 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1545 if (!mangled)
1546 return 0;
1547 arg = compiler_add_o(c, dict, mangled);
1548 Py_DECREF(mangled);
1549 if (arg < 0)
1550 return 0;
1551 return compiler_addop_i(c, opcode, arg);
1552}
1553
1554/* Add an opcode with an integer argument.
1555 Returns 0 on failure, 1 on success.
1556*/
1557
1558static int
1559compiler_addop_i(struct compiler *c, int opcode, int oparg)
1560{
1561 struct instr *i;
1562 int off;
1563 off = compiler_next_instr(c, c->u->u_curblock);
1564 if (off < 0)
1565 return 0;
1566 i = &c->u->u_curblock->b_instr[off];
1567 i->i_opcode = opcode;
1568 i->i_oparg = oparg;
1569 i->i_hasarg = 1;
1570 compiler_set_lineno(c, off);
1571 return 1;
1572}
1573
1574static int
1575compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1576{
1577 struct instr *i;
1578 int off;
1579
1580 assert(b != NULL);
1581 off = compiler_next_instr(c, c->u->u_curblock);
1582 if (off < 0)
1583 return 0;
1584 compiler_set_lineno(c, off);
1585 i = &c->u->u_curblock->b_instr[off];
1586 i->i_opcode = opcode;
1587 i->i_target = b;
1588 i->i_hasarg = 1;
1589 if (absolute)
1590 i->i_jabs = 1;
1591 else
1592 i->i_jrel = 1;
1593 return 1;
1594}
1595
1596/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1597 like to find better names.) NEW_BLOCK() creates a new block and sets
1598 it as the current block. NEXT_BLOCK() also creates an implicit jump
1599 from the current block to the new block.
1600*/
1601
1602/* XXX The returns inside these macros make it impossible to decref
1603 objects created in the local function.
1604*/
1605
1606
1607#define NEW_BLOCK(C) { \
1608 if (compiler_use_new_block((C)) == NULL) \
1609 return 0; \
1610}
1611
1612#define NEXT_BLOCK(C) { \
1613 if (compiler_next_block((C)) == NULL) \
1614 return 0; \
1615}
1616
1617#define ADDOP(C, OP) { \
1618 if (!compiler_addop((C), (OP))) \
1619 return 0; \
1620}
1621
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001622#define ADDOP_IN_SCOPE(C, OP) { \
1623 if (!compiler_addop((C), (OP))) { \
1624 compiler_exit_scope(c); \
1625 return 0; \
1626 } \
1627}
1628
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001629#define ADDOP_O(C, OP, O, TYPE) { \
1630 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1631 return 0; \
1632}
1633
1634#define ADDOP_NAME(C, OP, O, TYPE) { \
1635 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1636 return 0; \
1637}
1638
1639#define ADDOP_I(C, OP, O) { \
1640 if (!compiler_addop_i((C), (OP), (O))) \
1641 return 0; \
1642}
1643
1644#define ADDOP_JABS(C, OP, O) { \
1645 if (!compiler_addop_j((C), (OP), (O), 1)) \
1646 return 0; \
1647}
1648
1649#define ADDOP_JREL(C, OP, O) { \
1650 if (!compiler_addop_j((C), (OP), (O), 0)) \
1651 return 0; \
1652}
1653
1654/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1655 the ASDL name to synthesize the name of the C type and the visit function.
1656*/
1657
1658#define VISIT(C, TYPE, V) {\
1659 if (!compiler_visit_ ## TYPE((C), (V))) \
1660 return 0; \
1661}
1662
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001663#define VISIT_IN_SCOPE(C, TYPE, V) {\
1664 if (!compiler_visit_ ## TYPE((C), (V))) { \
1665 compiler_exit_scope(c); \
1666 return 0; \
1667 } \
1668}
1669
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001670#define VISIT_SLICE(C, V, CTX) {\
1671 if (!compiler_visit_slice((C), (V), (CTX))) \
1672 return 0; \
1673}
1674
1675#define VISIT_SEQ(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001676 int _i; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001678 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1679 TYPE ## _ty elt = asdl_seq_GET(seq, _i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680 if (!compiler_visit_ ## TYPE((C), elt)) \
1681 return 0; \
1682 } \
1683}
1684
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001685#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001686 int _i; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001687 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001688 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1689 TYPE ## _ty elt = asdl_seq_GET(seq, _i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001690 if (!compiler_visit_ ## TYPE((C), elt)) { \
1691 compiler_exit_scope(c); \
1692 return 0; \
1693 } \
1694 } \
1695}
1696
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697static int
1698compiler_isdocstring(stmt_ty s)
1699{
1700 if (s->kind != Expr_kind)
1701 return 0;
1702 return s->v.Expr.value->kind == Str_kind;
1703}
1704
1705/* Compile a sequence of statements, checking for a docstring. */
1706
1707static int
1708compiler_body(struct compiler *c, asdl_seq *stmts)
1709{
1710 int i = 0;
1711 stmt_ty st;
1712
1713 if (!asdl_seq_LEN(stmts))
1714 return 1;
1715 st = asdl_seq_GET(stmts, 0);
1716 if (compiler_isdocstring(st)) {
1717 i = 1;
1718 VISIT(c, expr, st->v.Expr.value);
1719 if (!compiler_nameop(c, __doc__, Store))
1720 return 0;
1721 }
1722 for (; i < asdl_seq_LEN(stmts); i++)
1723 VISIT(c, stmt, asdl_seq_GET(stmts, i));
1724 return 1;
1725}
1726
1727static PyCodeObject *
1728compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001729{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001730 PyCodeObject *co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001731 int addNone = 1;
1732 static PyObject *module;
1733 if (!module) {
1734 module = PyString_FromString("<module>");
1735 if (!module)
1736 return NULL;
1737 }
1738 if (!compiler_enter_scope(c, module, mod, 1))
Guido van Rossumd076c731998-10-07 19:42:25 +00001739 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740 switch (mod->kind) {
1741 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001742 if (!compiler_body(c, mod->v.Module.body)) {
1743 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001744 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001745 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001746 break;
1747 case Interactive_kind:
1748 c->c_interactive = 1;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001749 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001750 break;
1751 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001752 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001753 addNone = 0;
1754 break;
1755 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001756 PyErr_SetString(PyExc_SystemError,
1757 "suite should not be possible");
1758 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001759 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001760 PyErr_Format(PyExc_SystemError,
1761 "module kind %d should not be possible",
1762 mod->kind);
1763 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001764 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001765 co = assemble(c, addNone);
1766 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001767 return co;
1768}
1769
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001770/* The test for LOCAL must come before the test for FREE in order to
1771 handle classes where name is both local and free. The local var is
1772 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001773*/
1774
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001775static int
1776get_ref_type(struct compiler *c, PyObject *name)
1777{
1778 int scope = PyST_GetScope(c->u->u_ste, name);
1779 if (scope == 0) {
1780 char buf[350];
1781 PyOS_snprintf(buf, sizeof(buf),
1782 "unknown scope for %.100s in %.100s(%s) in %s\n"
1783 "symbols: %s\nlocals: %s\nglobals: %s\n",
1784 PyString_AS_STRING(name),
1785 PyString_AS_STRING(c->u->u_name),
1786 PyObject_REPR(c->u->u_ste->ste_id),
1787 c->c_filename,
1788 PyObject_REPR(c->u->u_ste->ste_symbols),
1789 PyObject_REPR(c->u->u_varnames),
1790 PyObject_REPR(c->u->u_names)
1791 );
1792 Py_FatalError(buf);
1793 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001794
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001795 return scope;
1796}
1797
1798static int
1799compiler_lookup_arg(PyObject *dict, PyObject *name)
1800{
1801 PyObject *k, *v;
1802 k = Py_BuildValue("(OO)", name, name->ob_type);
1803 if (k == NULL)
1804 return -1;
1805 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001806 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807 if (v == NULL)
1808 return -1;
1809 return PyInt_AS_LONG(v);
1810}
1811
1812static int
1813compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1814{
1815 int i, free = PyCode_GetNumFree(co);
1816 if (free == 0) {
1817 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1818 ADDOP_I(c, MAKE_FUNCTION, args);
1819 return 1;
1820 }
1821 for (i = 0; i < free; ++i) {
1822 /* Bypass com_addop_varname because it will generate
1823 LOAD_DEREF but LOAD_CLOSURE is needed.
1824 */
1825 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1826 int arg, reftype;
1827
1828 /* Special case: If a class contains a method with a
1829 free variable that has the same name as a method,
1830 the name will be considered free *and* local in the
1831 class. It should be handled by the closure, as
1832 well as by the normal name loookup logic.
1833 */
1834 reftype = get_ref_type(c, name);
1835 if (reftype == CELL)
1836 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1837 else /* (reftype == FREE) */
1838 arg = compiler_lookup_arg(c->u->u_freevars, name);
1839 if (arg == -1) {
1840 printf("lookup %s in %s %d %d\n"
1841 "freevars of %s: %s\n",
1842 PyObject_REPR(name),
1843 PyString_AS_STRING(c->u->u_name),
1844 reftype, arg,
1845 PyString_AS_STRING(co->co_name),
1846 PyObject_REPR(co->co_freevars));
1847 Py_FatalError("compiler_make_closure()");
1848 }
1849 ADDOP_I(c, LOAD_CLOSURE, arg);
1850 }
1851 ADDOP_I(c, BUILD_TUPLE, free);
1852 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1853 ADDOP_I(c, MAKE_CLOSURE, args);
1854 return 1;
1855}
1856
1857static int
1858compiler_decorators(struct compiler *c, asdl_seq* decos)
1859{
1860 int i;
1861
1862 if (!decos)
1863 return 1;
1864
1865 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1866 VISIT(c, expr, asdl_seq_GET(decos, i));
1867 }
1868 return 1;
1869}
1870
1871static int
1872compiler_arguments(struct compiler *c, arguments_ty args)
1873{
1874 int i;
1875 int n = asdl_seq_LEN(args->args);
1876 /* Correctly handle nested argument lists */
1877 for (i = 0; i < n; i++) {
1878 expr_ty arg = asdl_seq_GET(args->args, i);
1879 if (arg->kind == Tuple_kind) {
1880 PyObject *id = PyString_FromFormat(".%d", i);
1881 if (id == NULL) {
1882 return 0;
1883 }
1884 if (!compiler_nameop(c, id, Load)) {
1885 Py_DECREF(id);
1886 return 0;
1887 }
1888 Py_DECREF(id);
1889 VISIT(c, expr, arg);
1890 }
1891 }
1892 return 1;
1893}
1894
1895static int
1896compiler_function(struct compiler *c, stmt_ty s)
1897{
1898 PyCodeObject *co;
1899 PyObject *first_const = Py_None;
1900 arguments_ty args = s->v.FunctionDef.args;
1901 asdl_seq* decos = s->v.FunctionDef.decorators;
1902 stmt_ty st;
1903 int i, n, docstring;
1904
1905 assert(s->kind == FunctionDef_kind);
1906
1907 if (!compiler_decorators(c, decos))
1908 return 0;
1909 if (args->defaults)
1910 VISIT_SEQ(c, expr, args->defaults);
1911 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1912 s->lineno))
1913 return 0;
1914
1915 st = asdl_seq_GET(s->v.FunctionDef.body, 0);
1916 docstring = compiler_isdocstring(st);
1917 if (docstring)
1918 first_const = st->v.Expr.value->v.Str.s;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001919 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1920 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001922 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001923
1924 /* unpack nested arguments */
1925 compiler_arguments(c, args);
1926
1927 c->u->u_argcount = asdl_seq_LEN(args->args);
1928 n = asdl_seq_LEN(s->v.FunctionDef.body);
1929 /* if there was a docstring, we need to skip the first statement */
1930 for (i = docstring; i < n; i++) {
1931 stmt_ty s2 = asdl_seq_GET(s->v.FunctionDef.body, i);
1932 if (i == 0 && s2->kind == Expr_kind &&
1933 s2->v.Expr.value->kind == Str_kind)
1934 continue;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001935 VISIT_IN_SCOPE(c, stmt, s2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001936 }
1937 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001938 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001939 if (co == NULL)
1940 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001941
1942 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00001943 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944
1945 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1946 ADDOP_I(c, CALL_FUNCTION, 1);
1947 }
1948
1949 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1950}
1951
1952static int
1953compiler_class(struct compiler *c, stmt_ty s)
1954{
1955 int n;
1956 PyCodeObject *co;
1957 PyObject *str;
1958 /* push class name on stack, needed by BUILD_CLASS */
1959 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1960 /* push the tuple of base classes on the stack */
1961 n = asdl_seq_LEN(s->v.ClassDef.bases);
1962 if (n > 0)
1963 VISIT_SEQ(c, expr, s->v.ClassDef.bases);
1964 ADDOP_I(c, BUILD_TUPLE, n);
1965 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s,
1966 s->lineno))
1967 return 0;
1968 c->u->u_private = s->v.ClassDef.name;
1969 Py_INCREF(c->u->u_private);
1970 str = PyString_InternFromString("__name__");
1971 if (!str || !compiler_nameop(c, str, Load)) {
1972 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001973 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001974 return 0;
1975 }
1976
1977 Py_DECREF(str);
1978 str = PyString_InternFromString("__module__");
1979 if (!str || !compiler_nameop(c, str, Store)) {
1980 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001981 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001982 return 0;
1983 }
1984 Py_DECREF(str);
1985
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001986 if (!compiler_body(c, s->v.ClassDef.body)) {
1987 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001988 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001989 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001990
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001991 ADDOP_IN_SCOPE(c, LOAD_LOCALS);
1992 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001993 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001994 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001995 if (co == NULL)
1996 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001997
1998 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00001999 Py_DECREF(co);
2000
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002001 ADDOP_I(c, CALL_FUNCTION, 0);
2002 ADDOP(c, BUILD_CLASS);
2003 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2004 return 0;
2005 return 1;
2006}
2007
2008static int
2009compiler_lambda(struct compiler *c, expr_ty e)
2010{
2011 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002012 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002013 arguments_ty args = e->v.Lambda.args;
2014 assert(e->kind == Lambda_kind);
2015
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002016 if (!name) {
2017 name = PyString_InternFromString("<lambda>");
2018 if (!name)
2019 return 0;
2020 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021
2022 if (args->defaults)
2023 VISIT_SEQ(c, expr, args->defaults);
2024 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2025 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002026
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002027 /* unpack nested arguments */
2028 compiler_arguments(c, args);
2029
2030 c->u->u_argcount = asdl_seq_LEN(args->args);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00002031 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2032 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002033 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00002034 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035 if (co == NULL)
2036 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002037
2038 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00002039 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002040
2041 return 1;
2042}
2043
2044static int
2045compiler_print(struct compiler *c, stmt_ty s)
2046{
2047 int i, n;
2048 bool dest;
2049
2050 assert(s->kind == Print_kind);
2051 n = asdl_seq_LEN(s->v.Print.values);
2052 dest = false;
2053 if (s->v.Print.dest) {
2054 VISIT(c, expr, s->v.Print.dest);
2055 dest = true;
2056 }
2057 for (i = 0; i < n; i++) {
2058 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i);
2059 if (dest) {
2060 ADDOP(c, DUP_TOP);
2061 VISIT(c, expr, e);
2062 ADDOP(c, ROT_TWO);
2063 ADDOP(c, PRINT_ITEM_TO);
2064 }
2065 else {
2066 VISIT(c, expr, e);
2067 ADDOP(c, PRINT_ITEM);
2068 }
2069 }
2070 if (s->v.Print.nl) {
2071 if (dest)
2072 ADDOP(c, PRINT_NEWLINE_TO)
2073 else
2074 ADDOP(c, PRINT_NEWLINE)
2075 }
2076 else if (dest)
2077 ADDOP(c, POP_TOP);
2078 return 1;
2079}
2080
2081static int
2082compiler_if(struct compiler *c, stmt_ty s)
2083{
2084 basicblock *end, *next;
2085
2086 assert(s->kind == If_kind);
2087 end = compiler_new_block(c);
2088 if (end == NULL)
2089 return 0;
2090 next = compiler_new_block(c);
2091 if (next == NULL)
2092 return 0;
2093 VISIT(c, expr, s->v.If.test);
2094 ADDOP_JREL(c, JUMP_IF_FALSE, next);
2095 ADDOP(c, POP_TOP);
2096 VISIT_SEQ(c, stmt, s->v.If.body);
2097 ADDOP_JREL(c, JUMP_FORWARD, end);
2098 compiler_use_next_block(c, next);
2099 ADDOP(c, POP_TOP);
2100 if (s->v.If.orelse)
2101 VISIT_SEQ(c, stmt, s->v.If.orelse);
2102 compiler_use_next_block(c, end);
2103 return 1;
2104}
2105
2106static int
2107compiler_for(struct compiler *c, stmt_ty s)
2108{
2109 basicblock *start, *cleanup, *end;
2110
2111 start = compiler_new_block(c);
2112 cleanup = compiler_new_block(c);
2113 end = compiler_new_block(c);
2114 if (start == NULL || end == NULL || cleanup == NULL)
2115 return 0;
2116 ADDOP_JREL(c, SETUP_LOOP, end);
2117 if (!compiler_push_fblock(c, LOOP, start))
2118 return 0;
2119 VISIT(c, expr, s->v.For.iter);
2120 ADDOP(c, GET_ITER);
2121 compiler_use_next_block(c, start);
2122 ADDOP_JREL(c, FOR_ITER, cleanup);
2123 VISIT(c, expr, s->v.For.target);
2124 VISIT_SEQ(c, stmt, s->v.For.body);
2125 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2126 compiler_use_next_block(c, cleanup);
2127 ADDOP(c, POP_BLOCK);
2128 compiler_pop_fblock(c, LOOP, start);
2129 VISIT_SEQ(c, stmt, s->v.For.orelse);
2130 compiler_use_next_block(c, end);
2131 return 1;
2132}
2133
2134static int
2135compiler_while(struct compiler *c, stmt_ty s)
2136{
2137 basicblock *loop, *orelse, *end, *anchor = NULL;
2138 int constant = expr_constant(s->v.While.test);
2139
2140 if (constant == 0)
2141 return 1;
2142 loop = compiler_new_block(c);
2143 end = compiler_new_block(c);
2144 if (constant == -1) {
2145 anchor = compiler_new_block(c);
2146 if (anchor == NULL)
2147 return 0;
2148 }
2149 if (loop == NULL || end == NULL)
2150 return 0;
2151 if (s->v.While.orelse) {
2152 orelse = compiler_new_block(c);
2153 if (orelse == NULL)
2154 return 0;
2155 }
2156 else
2157 orelse = NULL;
2158
2159 ADDOP_JREL(c, SETUP_LOOP, end);
2160 compiler_use_next_block(c, loop);
2161 if (!compiler_push_fblock(c, LOOP, loop))
2162 return 0;
2163 if (constant == -1) {
2164 VISIT(c, expr, s->v.While.test);
2165 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
2166 ADDOP(c, POP_TOP);
2167 }
2168 VISIT_SEQ(c, stmt, s->v.While.body);
2169 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
2170
2171 /* XXX should the two POP instructions be in a separate block
2172 if there is no else clause ?
2173 */
2174
2175 if (constant == -1) {
2176 compiler_use_next_block(c, anchor);
2177 ADDOP(c, POP_TOP);
2178 ADDOP(c, POP_BLOCK);
2179 }
2180 compiler_pop_fblock(c, LOOP, loop);
2181 if (orelse != NULL)
2182 VISIT_SEQ(c, stmt, s->v.While.orelse);
2183 compiler_use_next_block(c, end);
2184
2185 return 1;
2186}
2187
2188static int
2189compiler_continue(struct compiler *c)
2190{
2191 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
2192 int i;
2193
2194 if (!c->u->u_nfblocks)
2195 return compiler_error(c, LOOP_ERROR_MSG);
2196 i = c->u->u_nfblocks - 1;
2197 switch (c->u->u_fblock[i].fb_type) {
2198 case LOOP:
2199 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
2200 break;
2201 case EXCEPT:
2202 case FINALLY_TRY:
2203 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP)
2204 ;
2205 if (i == -1)
2206 return compiler_error(c, LOOP_ERROR_MSG);
2207 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
2208 break;
2209 case FINALLY_END:
2210 return compiler_error(c,
2211 "'continue' not supported inside 'finally' clause");
2212 }
2213
2214 return 1;
2215}
2216
2217/* Code generated for "try: <body> finally: <finalbody>" is as follows:
2218
2219 SETUP_FINALLY L
2220 <code for body>
2221 POP_BLOCK
2222 LOAD_CONST <None>
2223 L: <code for finalbody>
2224 END_FINALLY
2225
2226 The special instructions use the block stack. Each block
2227 stack entry contains the instruction that created it (here
2228 SETUP_FINALLY), the level of the value stack at the time the
2229 block stack entry was created, and a label (here L).
2230
2231 SETUP_FINALLY:
2232 Pushes the current value stack level and the label
2233 onto the block stack.
2234 POP_BLOCK:
2235 Pops en entry from the block stack, and pops the value
2236 stack until its level is the same as indicated on the
2237 block stack. (The label is ignored.)
2238 END_FINALLY:
2239 Pops a variable number of entries from the *value* stack
2240 and re-raises the exception they specify. The number of
2241 entries popped depends on the (pseudo) exception type.
2242
2243 The block stack is unwound when an exception is raised:
2244 when a SETUP_FINALLY entry is found, the exception is pushed
2245 onto the value stack (and the exception condition is cleared),
2246 and the interpreter jumps to the label gotten from the block
2247 stack.
2248*/
2249
2250static int
2251compiler_try_finally(struct compiler *c, stmt_ty s)
2252{
2253 basicblock *body, *end;
2254 body = compiler_new_block(c);
2255 end = compiler_new_block(c);
2256 if (body == NULL || end == NULL)
2257 return 0;
2258
2259 ADDOP_JREL(c, SETUP_FINALLY, end);
2260 compiler_use_next_block(c, body);
2261 if (!compiler_push_fblock(c, FINALLY_TRY, body))
2262 return 0;
2263 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
2264 ADDOP(c, POP_BLOCK);
2265 compiler_pop_fblock(c, FINALLY_TRY, body);
2266
2267 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2268 compiler_use_next_block(c, end);
2269 if (!compiler_push_fblock(c, FINALLY_END, end))
2270 return 0;
2271 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
2272 ADDOP(c, END_FINALLY);
2273 compiler_pop_fblock(c, FINALLY_END, end);
2274
2275 return 1;
2276}
2277
2278/*
2279 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
2280 (The contents of the value stack is shown in [], with the top
2281 at the right; 'tb' is trace-back info, 'val' the exception's
2282 associated value, and 'exc' the exception.)
2283
2284 Value stack Label Instruction Argument
2285 [] SETUP_EXCEPT L1
2286 [] <code for S>
2287 [] POP_BLOCK
2288 [] JUMP_FORWARD L0
2289
2290 [tb, val, exc] L1: DUP )
2291 [tb, val, exc, exc] <evaluate E1> )
2292 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2293 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
2294 [tb, val, exc, 1] POP )
2295 [tb, val, exc] POP
2296 [tb, val] <assign to V1> (or POP if no V1)
2297 [tb] POP
2298 [] <code for S1>
2299 JUMP_FORWARD L0
2300
2301 [tb, val, exc, 0] L2: POP
2302 [tb, val, exc] DUP
2303 .............................etc.......................
2304
2305 [tb, val, exc, 0] Ln+1: POP
2306 [tb, val, exc] END_FINALLY # re-raise exception
2307
2308 [] L0: <next statement>
2309
2310 Of course, parts are not generated if Vi or Ei is not present.
2311*/
2312static int
2313compiler_try_except(struct compiler *c, stmt_ty s)
2314{
2315 basicblock *body, *orelse, *except, *end;
2316 int i, n;
2317
2318 body = compiler_new_block(c);
2319 except = compiler_new_block(c);
2320 orelse = compiler_new_block(c);
2321 end = compiler_new_block(c);
2322 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
2323 return 0;
2324 ADDOP_JREL(c, SETUP_EXCEPT, except);
2325 compiler_use_next_block(c, body);
2326 if (!compiler_push_fblock(c, EXCEPT, body))
2327 return 0;
2328 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
2329 ADDOP(c, POP_BLOCK);
2330 compiler_pop_fblock(c, EXCEPT, body);
2331 ADDOP_JREL(c, JUMP_FORWARD, orelse);
2332 n = asdl_seq_LEN(s->v.TryExcept.handlers);
2333 compiler_use_next_block(c, except);
2334 for (i = 0; i < n; i++) {
2335 excepthandler_ty handler = asdl_seq_GET(
2336 s->v.TryExcept.handlers, i);
2337 if (!handler->type && i < n-1)
2338 return compiler_error(c, "default 'except:' must be last");
2339 except = compiler_new_block(c);
2340 if (except == NULL)
2341 return 0;
2342 if (handler->type) {
2343 ADDOP(c, DUP_TOP);
2344 VISIT(c, expr, handler->type);
2345 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2346 ADDOP_JREL(c, JUMP_IF_FALSE, except);
2347 ADDOP(c, POP_TOP);
2348 }
2349 ADDOP(c, POP_TOP);
2350 if (handler->name) {
2351 VISIT(c, expr, handler->name);
2352 }
2353 else {
2354 ADDOP(c, POP_TOP);
2355 }
2356 ADDOP(c, POP_TOP);
2357 VISIT_SEQ(c, stmt, handler->body);
2358 ADDOP_JREL(c, JUMP_FORWARD, end);
2359 compiler_use_next_block(c, except);
2360 if (handler->type)
2361 ADDOP(c, POP_TOP);
2362 }
2363 ADDOP(c, END_FINALLY);
2364 compiler_use_next_block(c, orelse);
2365 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2366 compiler_use_next_block(c, end);
2367 return 1;
2368}
2369
2370static int
2371compiler_import_as(struct compiler *c, identifier name, identifier asname)
2372{
2373 /* The IMPORT_NAME opcode was already generated. This function
2374 merely needs to bind the result to a name.
2375
2376 If there is a dot in name, we need to split it and emit a
2377 LOAD_ATTR for each name.
2378 */
2379 const char *src = PyString_AS_STRING(name);
2380 const char *dot = strchr(src, '.');
2381 if (dot) {
2382 /* Consume the base module name to get the first attribute */
2383 src = dot + 1;
2384 while (dot) {
2385 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00002386 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002387 dot = strchr(src, '.');
Armin Rigo31441302005-10-21 12:57:31 +00002388 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002389 dot ? dot - src : strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002390 if (!attr)
2391 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002392 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002393 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002394 src = dot + 1;
2395 }
2396 }
2397 return compiler_nameop(c, asname, Store);
2398}
2399
2400static int
2401compiler_import(struct compiler *c, stmt_ty s)
2402{
2403 /* The Import node stores a module name like a.b.c as a single
2404 string. This is convenient for all cases except
2405 import a.b.c as d
2406 where we need to parse that string to extract the individual
2407 module names.
2408 XXX Perhaps change the representation to make this case simpler?
2409 */
2410 int i, n = asdl_seq_LEN(s->v.Import.names);
2411 for (i = 0; i < n; i++) {
2412 alias_ty alias = asdl_seq_GET(s->v.Import.names, i);
2413 int r;
2414
2415 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2416 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2417
2418 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002419 r = compiler_import_as(c, alias->name, alias->asname);
2420 if (!r)
2421 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002422 }
2423 else {
2424 identifier tmp = alias->name;
2425 const char *base = PyString_AS_STRING(alias->name);
2426 char *dot = strchr(base, '.');
2427 if (dot)
2428 tmp = PyString_FromStringAndSize(base,
2429 dot - base);
2430 r = compiler_nameop(c, tmp, Store);
2431 if (dot) {
2432 Py_DECREF(tmp);
2433 }
2434 if (!r)
2435 return r;
2436 }
2437 }
2438 return 1;
2439}
2440
2441static int
2442compiler_from_import(struct compiler *c, stmt_ty s)
2443{
2444 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002445
2446 PyObject *names = PyTuple_New(n);
2447 if (!names)
2448 return 0;
2449
2450 /* build up the names */
2451 for (i = 0; i < n; i++) {
2452 alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
2453 Py_INCREF(alias->name);
2454 PyTuple_SET_ITEM(names, i, alias->name);
2455 }
2456
2457 if (s->lineno > c->c_future->ff_lineno) {
2458 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
2459 "__future__")) {
2460 Py_DECREF(names);
2461 return compiler_error(c,
2462 "from __future__ imports must occur "
2463 "at the beginning of the file");
2464
2465 }
2466 }
2467
2468 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002469 Py_DECREF(names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002470 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2471 for (i = 0; i < n; i++) {
2472 alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
2473 identifier store_name;
2474
2475 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
2476 assert(n == 1);
2477 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002478 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002479 }
2480
2481 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2482 store_name = alias->name;
2483 if (alias->asname)
2484 store_name = alias->asname;
2485
2486 if (!compiler_nameop(c, store_name, Store)) {
2487 Py_DECREF(names);
2488 return 0;
2489 }
2490 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002491 /* remove imported module */
2492 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002493 return 1;
2494}
2495
2496static int
2497compiler_assert(struct compiler *c, stmt_ty s)
2498{
2499 static PyObject *assertion_error = NULL;
2500 basicblock *end;
2501
2502 if (Py_OptimizeFlag)
2503 return 1;
2504 if (assertion_error == NULL) {
2505 assertion_error = PyString_FromString("AssertionError");
2506 if (assertion_error == NULL)
2507 return 0;
2508 }
2509 VISIT(c, expr, s->v.Assert.test);
2510 end = compiler_new_block(c);
2511 if (end == NULL)
2512 return 0;
2513 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2514 ADDOP(c, POP_TOP);
2515 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2516 if (s->v.Assert.msg) {
2517 VISIT(c, expr, s->v.Assert.msg);
2518 ADDOP_I(c, RAISE_VARARGS, 2);
2519 }
2520 else {
2521 ADDOP_I(c, RAISE_VARARGS, 1);
2522 }
Neal Norwitz51abbc72005-12-18 07:06:23 +00002523 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002524 ADDOP(c, POP_TOP);
2525 return 1;
2526}
2527
2528static int
2529compiler_visit_stmt(struct compiler *c, stmt_ty s)
2530{
2531 int i, n;
2532
2533 c->u->u_lineno = s->lineno;
2534 c->u->u_lineno_set = false;
2535 switch (s->kind) {
2536 case FunctionDef_kind:
2537 return compiler_function(c, s);
2538 case ClassDef_kind:
2539 return compiler_class(c, s);
2540 case Return_kind:
2541 if (c->u->u_ste->ste_type != FunctionBlock)
2542 return compiler_error(c, "'return' outside function");
2543 if (s->v.Return.value) {
2544 if (c->u->u_ste->ste_generator) {
2545 return compiler_error(c,
2546 "'return' with argument inside generator");
2547 }
2548 VISIT(c, expr, s->v.Return.value);
2549 }
2550 else
2551 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2552 ADDOP(c, RETURN_VALUE);
2553 break;
2554 case Delete_kind:
2555 VISIT_SEQ(c, expr, s->v.Delete.targets)
2556 break;
2557 case Assign_kind:
2558 n = asdl_seq_LEN(s->v.Assign.targets);
2559 VISIT(c, expr, s->v.Assign.value);
2560 for (i = 0; i < n; i++) {
2561 if (i < n - 1)
2562 ADDOP(c, DUP_TOP);
2563 VISIT(c, expr,
2564 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2565 }
2566 break;
2567 case AugAssign_kind:
2568 return compiler_augassign(c, s);
2569 case Print_kind:
2570 return compiler_print(c, s);
2571 case For_kind:
2572 return compiler_for(c, s);
2573 case While_kind:
2574 return compiler_while(c, s);
2575 case If_kind:
2576 return compiler_if(c, s);
2577 case Raise_kind:
2578 n = 0;
2579 if (s->v.Raise.type) {
2580 VISIT(c, expr, s->v.Raise.type);
2581 n++;
2582 if (s->v.Raise.inst) {
2583 VISIT(c, expr, s->v.Raise.inst);
2584 n++;
2585 if (s->v.Raise.tback) {
2586 VISIT(c, expr, s->v.Raise.tback);
2587 n++;
2588 }
2589 }
2590 }
2591 ADDOP_I(c, RAISE_VARARGS, n);
2592 break;
2593 case TryExcept_kind:
2594 return compiler_try_except(c, s);
2595 case TryFinally_kind:
2596 return compiler_try_finally(c, s);
2597 case Assert_kind:
2598 return compiler_assert(c, s);
2599 case Import_kind:
2600 return compiler_import(c, s);
2601 case ImportFrom_kind:
2602 return compiler_from_import(c, s);
2603 case Exec_kind:
2604 VISIT(c, expr, s->v.Exec.body);
2605 if (s->v.Exec.globals) {
2606 VISIT(c, expr, s->v.Exec.globals);
2607 if (s->v.Exec.locals) {
2608 VISIT(c, expr, s->v.Exec.locals);
2609 } else {
2610 ADDOP(c, DUP_TOP);
2611 }
2612 } else {
2613 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2614 ADDOP(c, DUP_TOP);
2615 }
2616 ADDOP(c, EXEC_STMT);
2617 break;
2618 case Global_kind:
2619 break;
2620 case Expr_kind:
2621 VISIT(c, expr, s->v.Expr.value);
2622 if (c->c_interactive && c->c_nestlevel <= 1) {
2623 ADDOP(c, PRINT_EXPR);
2624 }
2625 else {
2626 ADDOP(c, POP_TOP);
2627 }
2628 break;
2629 case Pass_kind:
2630 break;
2631 case Break_kind:
2632 if (!c->u->u_nfblocks)
2633 return compiler_error(c, "'break' outside loop");
2634 ADDOP(c, BREAK_LOOP);
2635 break;
2636 case Continue_kind:
2637 return compiler_continue(c);
2638 }
2639 return 1;
2640}
2641
2642static int
2643unaryop(unaryop_ty op)
2644{
2645 switch (op) {
2646 case Invert:
2647 return UNARY_INVERT;
2648 case Not:
2649 return UNARY_NOT;
2650 case UAdd:
2651 return UNARY_POSITIVE;
2652 case USub:
2653 return UNARY_NEGATIVE;
2654 }
2655 return 0;
2656}
2657
2658static int
2659binop(struct compiler *c, operator_ty op)
2660{
2661 switch (op) {
2662 case Add:
2663 return BINARY_ADD;
2664 case Sub:
2665 return BINARY_SUBTRACT;
2666 case Mult:
2667 return BINARY_MULTIPLY;
2668 case Div:
2669 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2670 return BINARY_TRUE_DIVIDE;
2671 else
2672 return BINARY_DIVIDE;
2673 case Mod:
2674 return BINARY_MODULO;
2675 case Pow:
2676 return BINARY_POWER;
2677 case LShift:
2678 return BINARY_LSHIFT;
2679 case RShift:
2680 return BINARY_RSHIFT;
2681 case BitOr:
2682 return BINARY_OR;
2683 case BitXor:
2684 return BINARY_XOR;
2685 case BitAnd:
2686 return BINARY_AND;
2687 case FloorDiv:
2688 return BINARY_FLOOR_DIVIDE;
2689 }
2690 return 0;
2691}
2692
2693static int
2694cmpop(cmpop_ty op)
2695{
2696 switch (op) {
2697 case Eq:
2698 return PyCmp_EQ;
2699 case NotEq:
2700 return PyCmp_NE;
2701 case Lt:
2702 return PyCmp_LT;
2703 case LtE:
2704 return PyCmp_LE;
2705 case Gt:
2706 return PyCmp_GT;
2707 case GtE:
2708 return PyCmp_GE;
2709 case Is:
2710 return PyCmp_IS;
2711 case IsNot:
2712 return PyCmp_IS_NOT;
2713 case In:
2714 return PyCmp_IN;
2715 case NotIn:
2716 return PyCmp_NOT_IN;
2717 }
2718 return PyCmp_BAD;
2719}
2720
2721static int
2722inplace_binop(struct compiler *c, operator_ty op)
2723{
2724 switch (op) {
2725 case Add:
2726 return INPLACE_ADD;
2727 case Sub:
2728 return INPLACE_SUBTRACT;
2729 case Mult:
2730 return INPLACE_MULTIPLY;
2731 case Div:
2732 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2733 return INPLACE_TRUE_DIVIDE;
2734 else
2735 return INPLACE_DIVIDE;
2736 case Mod:
2737 return INPLACE_MODULO;
2738 case Pow:
2739 return INPLACE_POWER;
2740 case LShift:
2741 return INPLACE_LSHIFT;
2742 case RShift:
2743 return INPLACE_RSHIFT;
2744 case BitOr:
2745 return INPLACE_OR;
2746 case BitXor:
2747 return INPLACE_XOR;
2748 case BitAnd:
2749 return INPLACE_AND;
2750 case FloorDiv:
2751 return INPLACE_FLOOR_DIVIDE;
2752 }
Neal Norwitz4737b232005-11-19 23:58:29 +00002753 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002754 "inplace binary op %d should not be possible", op);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002755 return 0;
2756}
2757
2758static int
2759compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2760{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002761 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002762 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2763
2764 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002765 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002766 /* XXX AugStore isn't used anywhere! */
2767
2768 /* First check for assignment to __debug__. Param? */
2769 if ((ctx == Store || ctx == AugStore || ctx == Del)
2770 && !strcmp(PyString_AS_STRING(name), "__debug__")) {
2771 return compiler_error(c, "can not assign to __debug__");
2772 }
2773
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002774 mangled = _Py_Mangle(c->u->u_private, name);
2775 if (!mangled)
2776 return 0;
2777
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002778 op = 0;
2779 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002780 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002781 switch (scope) {
2782 case FREE:
2783 dict = c->u->u_freevars;
2784 optype = OP_DEREF;
2785 break;
2786 case CELL:
2787 dict = c->u->u_cellvars;
2788 optype = OP_DEREF;
2789 break;
2790 case LOCAL:
2791 if (c->u->u_ste->ste_type == FunctionBlock)
2792 optype = OP_FAST;
2793 break;
2794 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002795 if (c->u->u_ste->ste_type == FunctionBlock &&
2796 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002797 optype = OP_GLOBAL;
2798 break;
2799 case GLOBAL_EXPLICIT:
2800 optype = OP_GLOBAL;
2801 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002802 default:
2803 /* scope can be 0 */
2804 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002805 }
2806
2807 /* XXX Leave assert here, but handle __doc__ and the like better */
2808 assert(scope || PyString_AS_STRING(name)[0] == '_');
2809
2810 switch (optype) {
2811 case OP_DEREF:
2812 switch (ctx) {
2813 case Load: op = LOAD_DEREF; break;
2814 case Store: op = STORE_DEREF; break;
2815 case AugLoad:
2816 case AugStore:
2817 break;
2818 case Del:
2819 PyErr_Format(PyExc_SyntaxError,
2820 "can not delete variable '%s' referenced "
2821 "in nested scope",
2822 PyString_AS_STRING(name));
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002823 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002824 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002826 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002827 PyErr_SetString(PyExc_SystemError,
2828 "param invalid for deref variable");
2829 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002830 }
2831 break;
2832 case OP_FAST:
2833 switch (ctx) {
2834 case Load: op = LOAD_FAST; break;
2835 case Store: op = STORE_FAST; break;
2836 case Del: op = DELETE_FAST; break;
2837 case AugLoad:
2838 case AugStore:
2839 break;
2840 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002841 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002842 PyErr_SetString(PyExc_SystemError,
2843 "param invalid for local variable");
2844 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002845 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002846 ADDOP_O(c, op, mangled, varnames);
2847 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002848 return 1;
2849 case OP_GLOBAL:
2850 switch (ctx) {
2851 case Load: op = LOAD_GLOBAL; break;
2852 case Store: op = STORE_GLOBAL; break;
2853 case Del: op = DELETE_GLOBAL; break;
2854 case AugLoad:
2855 case AugStore:
2856 break;
2857 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002858 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002859 PyErr_SetString(PyExc_SystemError,
2860 "param invalid for global variable");
2861 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002862 }
2863 break;
2864 case OP_NAME:
2865 switch (ctx) {
2866 case Load: op = LOAD_NAME; break;
2867 case Store: op = STORE_NAME; break;
2868 case Del: op = DELETE_NAME; break;
2869 case AugLoad:
2870 case AugStore:
2871 break;
2872 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002873 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002874 PyErr_SetString(PyExc_SystemError,
2875 "param invalid for name variable");
2876 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002877 }
2878 break;
2879 }
2880
2881 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002882 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002883 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002884 if (arg < 0)
2885 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002886 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002887}
2888
2889static int
2890compiler_boolop(struct compiler *c, expr_ty e)
2891{
2892 basicblock *end;
2893 int jumpi, i, n;
2894 asdl_seq *s;
2895
2896 assert(e->kind == BoolOp_kind);
2897 if (e->v.BoolOp.op == And)
2898 jumpi = JUMP_IF_FALSE;
2899 else
2900 jumpi = JUMP_IF_TRUE;
2901 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002902 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002903 return 0;
2904 s = e->v.BoolOp.values;
2905 n = asdl_seq_LEN(s) - 1;
2906 for (i = 0; i < n; ++i) {
2907 VISIT(c, expr, asdl_seq_GET(s, i));
2908 ADDOP_JREL(c, jumpi, end);
2909 ADDOP(c, POP_TOP)
2910 }
2911 VISIT(c, expr, asdl_seq_GET(s, n));
2912 compiler_use_next_block(c, end);
2913 return 1;
2914}
2915
2916static int
2917compiler_list(struct compiler *c, expr_ty e)
2918{
2919 int n = asdl_seq_LEN(e->v.List.elts);
2920 if (e->v.List.ctx == Store) {
2921 ADDOP_I(c, UNPACK_SEQUENCE, n);
2922 }
2923 VISIT_SEQ(c, expr, e->v.List.elts);
2924 if (e->v.List.ctx == Load) {
2925 ADDOP_I(c, BUILD_LIST, n);
2926 }
2927 return 1;
2928}
2929
2930static int
2931compiler_tuple(struct compiler *c, expr_ty e)
2932{
2933 int n = asdl_seq_LEN(e->v.Tuple.elts);
2934 if (e->v.Tuple.ctx == Store) {
2935 ADDOP_I(c, UNPACK_SEQUENCE, n);
2936 }
2937 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2938 if (e->v.Tuple.ctx == Load) {
2939 ADDOP_I(c, BUILD_TUPLE, n);
2940 }
2941 return 1;
2942}
2943
2944static int
2945compiler_compare(struct compiler *c, expr_ty e)
2946{
2947 int i, n;
2948 basicblock *cleanup = NULL;
2949
2950 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2951 VISIT(c, expr, e->v.Compare.left);
2952 n = asdl_seq_LEN(e->v.Compare.ops);
2953 assert(n > 0);
2954 if (n > 1) {
2955 cleanup = compiler_new_block(c);
2956 if (cleanup == NULL)
2957 return 0;
2958 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, 0));
2959 }
2960 for (i = 1; i < n; i++) {
2961 ADDOP(c, DUP_TOP);
2962 ADDOP(c, ROT_THREE);
2963 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
2964 ADDOP_I(c, COMPARE_OP,
2965 cmpop((cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i - 1)));
2966 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2967 NEXT_BLOCK(c);
2968 ADDOP(c, POP_TOP);
2969 if (i < (n - 1))
2970 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, i));
2971 }
2972 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, n - 1));
2973 ADDOP_I(c, COMPARE_OP,
2974 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
2975 cmpop((cmpop_ty)asdl_seq_GET(e->v.Compare.ops, n - 1)));
2976 if (n > 1) {
2977 basicblock *end = compiler_new_block(c);
2978 if (end == NULL)
2979 return 0;
2980 ADDOP_JREL(c, JUMP_FORWARD, end);
2981 compiler_use_next_block(c, cleanup);
2982 ADDOP(c, ROT_TWO);
2983 ADDOP(c, POP_TOP);
2984 compiler_use_next_block(c, end);
2985 }
2986 return 1;
2987}
2988
2989static int
2990compiler_call(struct compiler *c, expr_ty e)
2991{
2992 int n, code = 0;
2993
2994 VISIT(c, expr, e->v.Call.func);
2995 n = asdl_seq_LEN(e->v.Call.args);
2996 VISIT_SEQ(c, expr, e->v.Call.args);
2997 if (e->v.Call.keywords) {
2998 VISIT_SEQ(c, keyword, e->v.Call.keywords);
2999 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
3000 }
3001 if (e->v.Call.starargs) {
3002 VISIT(c, expr, e->v.Call.starargs);
3003 code |= 1;
3004 }
3005 if (e->v.Call.kwargs) {
3006 VISIT(c, expr, e->v.Call.kwargs);
3007 code |= 2;
3008 }
3009 switch (code) {
3010 case 0:
3011 ADDOP_I(c, CALL_FUNCTION, n);
3012 break;
3013 case 1:
3014 ADDOP_I(c, CALL_FUNCTION_VAR, n);
3015 break;
3016 case 2:
3017 ADDOP_I(c, CALL_FUNCTION_KW, n);
3018 break;
3019 case 3:
3020 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
3021 break;
3022 }
3023 return 1;
3024}
3025
3026static int
3027compiler_listcomp_generator(struct compiler *c, PyObject *tmpname,
3028 asdl_seq *generators, int gen_index,
3029 expr_ty elt)
3030{
3031 /* generate code for the iterator, then each of the ifs,
3032 and then write to the element */
3033
3034 comprehension_ty l;
3035 basicblock *start, *anchor, *skip, *if_cleanup;
3036 int i, n;
3037
3038 start = compiler_new_block(c);
3039 skip = compiler_new_block(c);
3040 if_cleanup = compiler_new_block(c);
3041 anchor = compiler_new_block(c);
3042
3043 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3044 anchor == NULL)
3045 return 0;
3046
3047 l = asdl_seq_GET(generators, gen_index);
3048 VISIT(c, expr, l->iter);
3049 ADDOP(c, GET_ITER);
3050 compiler_use_next_block(c, start);
3051 ADDOP_JREL(c, FOR_ITER, anchor);
3052 NEXT_BLOCK(c);
3053 VISIT(c, expr, l->target);
3054
3055 /* XXX this needs to be cleaned up...a lot! */
3056 n = asdl_seq_LEN(l->ifs);
3057 for (i = 0; i < n; i++) {
3058 expr_ty e = asdl_seq_GET(l->ifs, i);
3059 VISIT(c, expr, e);
3060 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
3061 NEXT_BLOCK(c);
3062 ADDOP(c, POP_TOP);
3063 }
3064
3065 if (++gen_index < asdl_seq_LEN(generators))
3066 if (!compiler_listcomp_generator(c, tmpname,
3067 generators, gen_index, elt))
3068 return 0;
3069
3070 /* only append after the last for generator */
3071 if (gen_index >= asdl_seq_LEN(generators)) {
3072 if (!compiler_nameop(c, tmpname, Load))
3073 return 0;
3074 VISIT(c, expr, elt);
3075 ADDOP_I(c, CALL_FUNCTION, 1);
3076 ADDOP(c, POP_TOP);
3077
3078 compiler_use_next_block(c, skip);
3079 }
3080 for (i = 0; i < n; i++) {
3081 ADDOP_I(c, JUMP_FORWARD, 1);
3082 if (i == 0)
3083 compiler_use_next_block(c, if_cleanup);
3084 ADDOP(c, POP_TOP);
3085 }
3086 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3087 compiler_use_next_block(c, anchor);
3088 /* delete the append method added to locals */
3089 if (gen_index == 1)
3090 if (!compiler_nameop(c, tmpname, Del))
3091 return 0;
3092
3093 return 1;
3094}
3095
3096static int
3097compiler_listcomp(struct compiler *c, expr_ty e)
3098{
3099 char tmpname[256];
3100 identifier tmp;
3101 int rc = 0;
3102 static identifier append;
3103 asdl_seq *generators = e->v.ListComp.generators;
3104
3105 assert(e->kind == ListComp_kind);
3106 if (!append) {
3107 append = PyString_InternFromString("append");
3108 if (!append)
3109 return 0;
3110 }
3111 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
3112 tmp = PyString_FromString(tmpname);
3113 if (!tmp)
3114 return 0;
3115 ADDOP_I(c, BUILD_LIST, 0);
3116 ADDOP(c, DUP_TOP);
3117 ADDOP_O(c, LOAD_ATTR, append, names);
3118 if (compiler_nameop(c, tmp, Store))
3119 rc = compiler_listcomp_generator(c, tmp, generators, 0,
3120 e->v.ListComp.elt);
3121 Py_DECREF(tmp);
3122 return rc;
3123}
3124
3125static int
3126compiler_genexp_generator(struct compiler *c,
3127 asdl_seq *generators, int gen_index,
3128 expr_ty elt)
3129{
3130 /* generate code for the iterator, then each of the ifs,
3131 and then write to the element */
3132
3133 comprehension_ty ge;
3134 basicblock *start, *anchor, *skip, *if_cleanup, *end;
3135 int i, n;
3136
3137 start = compiler_new_block(c);
3138 skip = compiler_new_block(c);
3139 if_cleanup = compiler_new_block(c);
3140 anchor = compiler_new_block(c);
3141 end = compiler_new_block(c);
3142
3143 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3144 anchor == NULL || end == NULL)
3145 return 0;
3146
3147 ge = asdl_seq_GET(generators, gen_index);
3148 ADDOP_JREL(c, SETUP_LOOP, end);
3149 if (!compiler_push_fblock(c, LOOP, start))
3150 return 0;
3151
3152 if (gen_index == 0) {
3153 /* Receive outermost iter as an implicit argument */
3154 c->u->u_argcount = 1;
3155 ADDOP_I(c, LOAD_FAST, 0);
3156 }
3157 else {
3158 /* Sub-iter - calculate on the fly */
3159 VISIT(c, expr, ge->iter);
3160 ADDOP(c, GET_ITER);
3161 }
3162 compiler_use_next_block(c, start);
3163 ADDOP_JREL(c, FOR_ITER, anchor);
3164 NEXT_BLOCK(c);
3165 VISIT(c, expr, ge->target);
3166
3167 /* XXX this needs to be cleaned up...a lot! */
3168 n = asdl_seq_LEN(ge->ifs);
3169 for (i = 0; i < n; i++) {
3170 expr_ty e = asdl_seq_GET(ge->ifs, i);
3171 VISIT(c, expr, e);
3172 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
3173 NEXT_BLOCK(c);
3174 ADDOP(c, POP_TOP);
3175 }
3176
3177 if (++gen_index < asdl_seq_LEN(generators))
3178 if (!compiler_genexp_generator(c, generators, gen_index, elt))
3179 return 0;
3180
3181 /* only append after the last 'for' generator */
3182 if (gen_index >= asdl_seq_LEN(generators)) {
3183 VISIT(c, expr, elt);
3184 ADDOP(c, YIELD_VALUE);
3185 ADDOP(c, POP_TOP);
3186
3187 compiler_use_next_block(c, skip);
3188 }
3189 for (i = 0; i < n; i++) {
3190 ADDOP_I(c, JUMP_FORWARD, 1);
3191 if (i == 0)
3192 compiler_use_next_block(c, if_cleanup);
3193
3194 ADDOP(c, POP_TOP);
3195 }
3196 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3197 compiler_use_next_block(c, anchor);
3198 ADDOP(c, POP_BLOCK);
3199 compiler_pop_fblock(c, LOOP, start);
3200 compiler_use_next_block(c, end);
3201
3202 return 1;
3203}
3204
3205static int
3206compiler_genexp(struct compiler *c, expr_ty e)
3207{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00003208 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003209 PyCodeObject *co;
3210 expr_ty outermost_iter = ((comprehension_ty)
3211 (asdl_seq_GET(e->v.GeneratorExp.generators,
3212 0)))->iter;
3213
Nick Coghlan944d3eb2005-11-16 12:46:55 +00003214 if (!name) {
3215 name = PyString_FromString("<genexpr>");
3216 if (!name)
3217 return 0;
3218 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003219
3220 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
3221 return 0;
3222 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
3223 e->v.GeneratorExp.elt);
3224 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00003225 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003226 if (co == NULL)
3227 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003228
3229 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00003230 Py_DECREF(co);
3231
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003232 VISIT(c, expr, outermost_iter);
3233 ADDOP(c, GET_ITER);
3234 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003235
3236 return 1;
3237}
3238
3239static int
3240compiler_visit_keyword(struct compiler *c, keyword_ty k)
3241{
3242 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3243 VISIT(c, expr, k->value);
3244 return 1;
3245}
3246
3247/* Test whether expression is constant. For constants, report
3248 whether they are true or false.
3249
3250 Return values: 1 for true, 0 for false, -1 for non-constant.
3251 */
3252
3253static int
3254expr_constant(expr_ty e)
3255{
3256 switch (e->kind) {
3257 case Num_kind:
3258 return PyObject_IsTrue(e->v.Num.n);
3259 case Str_kind:
3260 return PyObject_IsTrue(e->v.Str.s);
3261 default:
3262 return -1;
3263 }
3264}
3265
3266static int
3267compiler_visit_expr(struct compiler *c, expr_ty e)
3268{
3269 int i, n;
3270
3271 if (e->lineno > c->u->u_lineno) {
3272 c->u->u_lineno = e->lineno;
3273 c->u->u_lineno_set = false;
3274 }
3275 switch (e->kind) {
3276 case BoolOp_kind:
3277 return compiler_boolop(c, e);
3278 case BinOp_kind:
3279 VISIT(c, expr, e->v.BinOp.left);
3280 VISIT(c, expr, e->v.BinOp.right);
3281 ADDOP(c, binop(c, e->v.BinOp.op));
3282 break;
3283 case UnaryOp_kind:
3284 VISIT(c, expr, e->v.UnaryOp.operand);
3285 ADDOP(c, unaryop(e->v.UnaryOp.op));
3286 break;
3287 case Lambda_kind:
3288 return compiler_lambda(c, e);
3289 case Dict_kind:
3290 /* XXX get rid of arg? */
3291 ADDOP_I(c, BUILD_MAP, 0);
3292 n = asdl_seq_LEN(e->v.Dict.values);
3293 /* We must arrange things just right for STORE_SUBSCR.
3294 It wants the stack to look like (value) (dict) (key) */
3295 for (i = 0; i < n; i++) {
3296 ADDOP(c, DUP_TOP);
3297 VISIT(c, expr, asdl_seq_GET(e->v.Dict.values, i));
3298 ADDOP(c, ROT_TWO);
3299 VISIT(c, expr, asdl_seq_GET(e->v.Dict.keys, i));
3300 ADDOP(c, STORE_SUBSCR);
3301 }
3302 break;
3303 case ListComp_kind:
3304 return compiler_listcomp(c, e);
3305 case GeneratorExp_kind:
3306 return compiler_genexp(c, e);
3307 case Yield_kind:
3308 if (c->u->u_ste->ste_type != FunctionBlock)
3309 return compiler_error(c, "'yield' outside function");
3310 /*
3311 for (i = 0; i < c->u->u_nfblocks; i++) {
3312 if (c->u->u_fblock[i].fb_type == FINALLY_TRY)
3313 return compiler_error(
3314 c, "'yield' not allowed in a 'try' "
3315 "block with a 'finally' clause");
3316 }
3317 */
3318 if (e->v.Yield.value) {
3319 VISIT(c, expr, e->v.Yield.value);
3320 }
3321 else {
3322 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3323 }
3324 ADDOP(c, YIELD_VALUE);
3325 break;
3326 case Compare_kind:
3327 return compiler_compare(c, e);
3328 case Call_kind:
3329 return compiler_call(c, e);
3330 case Repr_kind:
3331 VISIT(c, expr, e->v.Repr.value);
3332 ADDOP(c, UNARY_CONVERT);
3333 break;
3334 case Num_kind:
3335 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3336 break;
3337 case Str_kind:
3338 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3339 break;
3340 /* The following exprs can be assignment targets. */
3341 case Attribute_kind:
3342 if (e->v.Attribute.ctx != AugStore)
3343 VISIT(c, expr, e->v.Attribute.value);
3344 switch (e->v.Attribute.ctx) {
3345 case AugLoad:
3346 ADDOP(c, DUP_TOP);
3347 /* Fall through to load */
3348 case Load:
3349 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3350 break;
3351 case AugStore:
3352 ADDOP(c, ROT_TWO);
3353 /* Fall through to save */
3354 case Store:
3355 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3356 break;
3357 case Del:
3358 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3359 break;
3360 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003361 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003362 PyErr_SetString(PyExc_SystemError,
3363 "param invalid in attribute expression");
3364 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003365 }
3366 break;
3367 case Subscript_kind:
3368 switch (e->v.Subscript.ctx) {
3369 case AugLoad:
3370 VISIT(c, expr, e->v.Subscript.value);
3371 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3372 break;
3373 case Load:
3374 VISIT(c, expr, e->v.Subscript.value);
3375 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3376 break;
3377 case AugStore:
3378 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3379 break;
3380 case Store:
3381 VISIT(c, expr, e->v.Subscript.value);
3382 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3383 break;
3384 case Del:
3385 VISIT(c, expr, e->v.Subscript.value);
3386 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3387 break;
3388 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003389 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003390 PyErr_SetString(PyExc_SystemError,
3391 "param invalid in subscript expression");
3392 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003393 }
3394 break;
3395 case Name_kind:
3396 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3397 /* child nodes of List and Tuple will have expr_context set */
3398 case List_kind:
3399 return compiler_list(c, e);
3400 case Tuple_kind:
3401 return compiler_tuple(c, e);
3402 }
3403 return 1;
3404}
3405
3406static int
3407compiler_augassign(struct compiler *c, stmt_ty s)
3408{
3409 expr_ty e = s->v.AugAssign.target;
3410 expr_ty auge;
3411
3412 assert(s->kind == AugAssign_kind);
3413
3414 switch (e->kind) {
3415 case Attribute_kind:
3416 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003417 AugLoad, e->lineno, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003418 if (auge == NULL)
3419 return 0;
3420 VISIT(c, expr, auge);
3421 VISIT(c, expr, s->v.AugAssign.value);
3422 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3423 auge->v.Attribute.ctx = AugStore;
3424 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003425 break;
3426 case Subscript_kind:
3427 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003428 AugLoad, e->lineno, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003429 if (auge == NULL)
3430 return 0;
3431 VISIT(c, expr, auge);
3432 VISIT(c, expr, s->v.AugAssign.value);
3433 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3434 auge->v.Subscript.ctx = AugStore;
3435 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003436 break;
3437 case Name_kind:
3438 VISIT(c, expr, s->v.AugAssign.target);
3439 VISIT(c, expr, s->v.AugAssign.value);
3440 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3441 return compiler_nameop(c, e->v.Name.id, Store);
3442 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003443 PyErr_Format(PyExc_SystemError,
3444 "invalid node type (%d) for augmented assignment",
3445 e->kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003446 return 0;
3447 }
3448 return 1;
3449}
3450
3451static int
3452compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3453{
3454 struct fblockinfo *f;
3455 if (c->u->u_nfblocks >= CO_MAXBLOCKS)
3456 return 0;
3457 f = &c->u->u_fblock[c->u->u_nfblocks++];
3458 f->fb_type = t;
3459 f->fb_block = b;
3460 return 1;
3461}
3462
3463static void
3464compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3465{
3466 struct compiler_unit *u = c->u;
3467 assert(u->u_nfblocks > 0);
3468 u->u_nfblocks--;
3469 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3470 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3471}
3472
3473/* Raises a SyntaxError and returns 0.
3474 If something goes wrong, a different exception may be raised.
3475*/
3476
3477static int
3478compiler_error(struct compiler *c, const char *errstr)
3479{
3480 PyObject *loc;
3481 PyObject *u = NULL, *v = NULL;
3482
3483 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3484 if (!loc) {
3485 Py_INCREF(Py_None);
3486 loc = Py_None;
3487 }
3488 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3489 Py_None, loc);
3490 if (!u)
3491 goto exit;
3492 v = Py_BuildValue("(zO)", errstr, u);
3493 if (!v)
3494 goto exit;
3495 PyErr_SetObject(PyExc_SyntaxError, v);
3496 exit:
3497 Py_DECREF(loc);
3498 Py_XDECREF(u);
3499 Py_XDECREF(v);
3500 return 0;
3501}
3502
3503static int
3504compiler_handle_subscr(struct compiler *c, const char *kind,
3505 expr_context_ty ctx)
3506{
3507 int op = 0;
3508
3509 /* XXX this code is duplicated */
3510 switch (ctx) {
3511 case AugLoad: /* fall through to Load */
3512 case Load: op = BINARY_SUBSCR; break;
3513 case AugStore:/* fall through to Store */
3514 case Store: op = STORE_SUBSCR; break;
3515 case Del: op = DELETE_SUBSCR; break;
3516 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003517 PyErr_Format(PyExc_SystemError,
3518 "invalid %s kind %d in subscript\n",
3519 kind, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003520 return 0;
3521 }
3522 if (ctx == AugLoad) {
3523 ADDOP_I(c, DUP_TOPX, 2);
3524 }
3525 else if (ctx == AugStore) {
3526 ADDOP(c, ROT_THREE);
3527 }
3528 ADDOP(c, op);
3529 return 1;
3530}
3531
3532static int
3533compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3534{
3535 int n = 2;
3536 assert(s->kind == Slice_kind);
3537
3538 /* only handles the cases where BUILD_SLICE is emitted */
3539 if (s->v.Slice.lower) {
3540 VISIT(c, expr, s->v.Slice.lower);
3541 }
3542 else {
3543 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3544 }
3545
3546 if (s->v.Slice.upper) {
3547 VISIT(c, expr, s->v.Slice.upper);
3548 }
3549 else {
3550 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3551 }
3552
3553 if (s->v.Slice.step) {
3554 n++;
3555 VISIT(c, expr, s->v.Slice.step);
3556 }
3557 ADDOP_I(c, BUILD_SLICE, n);
3558 return 1;
3559}
3560
3561static int
3562compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3563{
3564 int op = 0, slice_offset = 0, stack_count = 0;
3565
3566 assert(s->v.Slice.step == NULL);
3567 if (s->v.Slice.lower) {
3568 slice_offset++;
3569 stack_count++;
3570 if (ctx != AugStore)
3571 VISIT(c, expr, s->v.Slice.lower);
3572 }
3573 if (s->v.Slice.upper) {
3574 slice_offset += 2;
3575 stack_count++;
3576 if (ctx != AugStore)
3577 VISIT(c, expr, s->v.Slice.upper);
3578 }
3579
3580 if (ctx == AugLoad) {
3581 switch (stack_count) {
3582 case 0: ADDOP(c, DUP_TOP); break;
3583 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3584 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3585 }
3586 }
3587 else if (ctx == AugStore) {
3588 switch (stack_count) {
3589 case 0: ADDOP(c, ROT_TWO); break;
3590 case 1: ADDOP(c, ROT_THREE); break;
3591 case 2: ADDOP(c, ROT_FOUR); break;
3592 }
3593 }
3594
3595 switch (ctx) {
3596 case AugLoad: /* fall through to Load */
3597 case Load: op = SLICE; break;
3598 case AugStore:/* fall through to Store */
3599 case Store: op = STORE_SLICE; break;
3600 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003601 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003602 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003603 PyErr_SetString(PyExc_SystemError,
3604 "param invalid in simple slice");
3605 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003606 }
3607
3608 ADDOP(c, op + slice_offset);
3609 return 1;
3610}
3611
3612static int
3613compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3614 expr_context_ty ctx)
3615{
3616 switch (s->kind) {
3617 case Ellipsis_kind:
3618 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3619 break;
3620 case Slice_kind:
3621 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003622 case Index_kind:
3623 VISIT(c, expr, s->v.Index.value);
3624 break;
3625 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003626 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003627 PyErr_SetString(PyExc_SystemError,
3628 "extended slice invalid in nested slice");
3629 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003630 }
3631 return 1;
3632}
3633
3634
3635static int
3636compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3637{
3638 switch (s->kind) {
3639 case Ellipsis_kind:
3640 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3641 break;
3642 case Slice_kind:
3643 if (!s->v.Slice.step)
3644 return compiler_simple_slice(c, s, ctx);
3645 if (!compiler_slice(c, s, ctx))
3646 return 0;
3647 if (ctx == AugLoad) {
3648 ADDOP_I(c, DUP_TOPX, 2);
3649 }
3650 else if (ctx == AugStore) {
3651 ADDOP(c, ROT_THREE);
3652 }
3653 return compiler_handle_subscr(c, "slice", ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003654 case ExtSlice_kind: {
3655 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3656 for (i = 0; i < n; i++) {
3657 slice_ty sub = asdl_seq_GET(s->v.ExtSlice.dims, i);
3658 if (!compiler_visit_nested_slice(c, sub, ctx))
3659 return 0;
3660 }
3661 ADDOP_I(c, BUILD_TUPLE, n);
3662 return compiler_handle_subscr(c, "extended slice", ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003663 }
3664 case Index_kind:
3665 if (ctx != AugStore)
3666 VISIT(c, expr, s->v.Index.value);
3667 return compiler_handle_subscr(c, "index", ctx);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003668 default:
3669 PyErr_Format(PyExc_SystemError,
3670 "invalid slice %d", s->kind);
3671 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003672 }
3673 return 1;
3674}
3675
3676/* do depth-first search of basic block graph, starting with block.
3677 post records the block indices in post-order.
3678
3679 XXX must handle implicit jumps from one block to next
3680*/
3681
3682static void
3683dfs(struct compiler *c, basicblock *b, struct assembler *a)
3684{
3685 int i;
3686 struct instr *instr = NULL;
3687
3688 if (b->b_seen)
3689 return;
3690 b->b_seen = 1;
3691 if (b->b_next != NULL)
3692 dfs(c, b->b_next, a);
3693 for (i = 0; i < b->b_iused; i++) {
3694 instr = &b->b_instr[i];
3695 if (instr->i_jrel || instr->i_jabs)
3696 dfs(c, instr->i_target, a);
3697 }
3698 a->a_postorder[a->a_nblocks++] = b;
3699}
3700
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003701static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003702stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3703{
3704 int i;
3705 struct instr *instr;
3706 if (b->b_seen || b->b_startdepth >= depth)
3707 return maxdepth;
3708 b->b_seen = 1;
3709 b->b_startdepth = depth;
3710 for (i = 0; i < b->b_iused; i++) {
3711 instr = &b->b_instr[i];
3712 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3713 if (depth > maxdepth)
3714 maxdepth = depth;
3715 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3716 if (instr->i_jrel || instr->i_jabs) {
3717 maxdepth = stackdepth_walk(c, instr->i_target,
3718 depth, maxdepth);
3719 if (instr->i_opcode == JUMP_ABSOLUTE ||
3720 instr->i_opcode == JUMP_FORWARD) {
3721 goto out; /* remaining code is dead */
3722 }
3723 }
3724 }
3725 if (b->b_next)
3726 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3727out:
3728 b->b_seen = 0;
3729 return maxdepth;
3730}
3731
3732/* Find the flow path that needs the largest stack. We assume that
3733 * cycles in the flow graph have no net effect on the stack depth.
3734 */
3735static int
3736stackdepth(struct compiler *c)
3737{
3738 basicblock *b, *entryblock;
3739 entryblock = NULL;
3740 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3741 b->b_seen = 0;
3742 b->b_startdepth = INT_MIN;
3743 entryblock = b;
3744 }
3745 return stackdepth_walk(c, entryblock, 0, 0);
3746}
3747
3748static int
3749assemble_init(struct assembler *a, int nblocks, int firstlineno)
3750{
3751 memset(a, 0, sizeof(struct assembler));
3752 a->a_lineno = firstlineno;
3753 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3754 if (!a->a_bytecode)
3755 return 0;
3756 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3757 if (!a->a_lnotab)
3758 return 0;
3759 a->a_postorder = (basicblock **)PyObject_Malloc(
3760 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003761 if (!a->a_postorder) {
3762 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003763 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003764 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003765 return 1;
3766}
3767
3768static void
3769assemble_free(struct assembler *a)
3770{
3771 Py_XDECREF(a->a_bytecode);
3772 Py_XDECREF(a->a_lnotab);
3773 if (a->a_postorder)
3774 PyObject_Free(a->a_postorder);
3775}
3776
3777/* Return the size of a basic block in bytes. */
3778
3779static int
3780instrsize(struct instr *instr)
3781{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003782 if (!instr->i_hasarg)
3783 return 1;
3784 if (instr->i_oparg > 0xffff)
3785 return 6;
3786 return 3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003787}
3788
3789static int
3790blocksize(basicblock *b)
3791{
3792 int i;
3793 int size = 0;
3794
3795 for (i = 0; i < b->b_iused; i++)
3796 size += instrsize(&b->b_instr[i]);
3797 return size;
3798}
3799
3800/* All about a_lnotab.
3801
3802c_lnotab is an array of unsigned bytes disguised as a Python string.
3803It is used to map bytecode offsets to source code line #s (when needed
3804for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003805
Tim Peters2a7f3842001-06-09 09:26:21 +00003806The array is conceptually a list of
3807 (bytecode offset increment, line number increment)
3808pairs. The details are important and delicate, best illustrated by example:
3809
3810 byte code offset source code line number
3811 0 1
3812 6 2
3813 50 7
3814 350 307
3815 361 308
3816
3817The first trick is that these numbers aren't stored, only the increments
3818from one row to the next (this doesn't really work, but it's a start):
3819
3820 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3821
3822The second trick is that an unsigned byte can't hold negative values, or
3823values larger than 255, so (a) there's a deep assumption that byte code
3824offsets and their corresponding line #s both increase monotonically, and (b)
3825if at least one column jumps by more than 255 from one row to the next, more
3826than one pair is written to the table. In case #b, there's no way to know
3827from looking at the table later how many were written. That's the delicate
3828part. A user of c_lnotab desiring to find the source line number
3829corresponding to a bytecode address A should do something like this
3830
3831 lineno = addr = 0
3832 for addr_incr, line_incr in c_lnotab:
3833 addr += addr_incr
3834 if addr > A:
3835 return lineno
3836 lineno += line_incr
3837
3838In order for this to work, when the addr field increments by more than 255,
3839the line # increment in each pair generated must be 0 until the remaining addr
3840increment is < 256. So, in the example above, com_set_lineno should not (as
3841was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
3842255, 0, 45, 255, 0, 45.
3843*/
3844
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003845static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003846assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003847{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003848 int d_bytecode, d_lineno;
3849 int len;
3850 char *lnotab;
3851
3852 d_bytecode = a->a_offset - a->a_lineno_off;
3853 d_lineno = i->i_lineno - a->a_lineno;
3854
3855 assert(d_bytecode >= 0);
3856 assert(d_lineno >= 0);
3857
3858 if (d_lineno == 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003859 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003860
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003861 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003862 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003863 nbytes = a->a_lnotab_off + 2 * ncodes;
3864 len = PyString_GET_SIZE(a->a_lnotab);
3865 if (nbytes >= len) {
3866 if (len * 2 < nbytes)
3867 len = nbytes;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003868 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003869 len *= 2;
3870 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3871 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003872 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003873 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003874 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003875 *lnotab++ = 255;
3876 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003877 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003878 d_bytecode -= ncodes * 255;
3879 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003880 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003881 assert(d_bytecode <= 255);
3882 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003883 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003884 nbytes = a->a_lnotab_off + 2 * ncodes;
3885 len = PyString_GET_SIZE(a->a_lnotab);
3886 if (nbytes >= len) {
3887 if (len * 2 < nbytes)
3888 len = nbytes;
Guido van Rossum635abd21997-01-06 22:56:52 +00003889 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003890 len *= 2;
3891 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3892 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003893 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003894 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3895 *lnotab++ = 255;
3896 *lnotab++ = d_bytecode;
3897 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003898 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003899 *lnotab++ = 255;
3900 *lnotab++ = 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003901 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003902 d_lineno -= ncodes * 255;
3903 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003904 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003905
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003906 len = PyString_GET_SIZE(a->a_lnotab);
3907 if (a->a_lnotab_off + 2 >= len) {
3908 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003909 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003910 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003911 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003912
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003913 a->a_lnotab_off += 2;
3914 if (d_bytecode) {
3915 *lnotab++ = d_bytecode;
3916 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003917 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003918 else { /* First line of a block; def stmt, etc. */
3919 *lnotab++ = 0;
3920 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003921 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003922 a->a_lineno = i->i_lineno;
3923 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003924 return 1;
3925}
3926
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003927/* assemble_emit()
3928 Extend the bytecode with a new instruction.
3929 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003930*/
3931
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003932static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003933assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003934{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003935 int size, arg = 0, ext = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003936 int len = PyString_GET_SIZE(a->a_bytecode);
3937 char *code;
3938
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003939 size = instrsize(i);
3940 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003941 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003942 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003943 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003944 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003945 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003946 if (a->a_offset + size >= len) {
3947 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003948 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003949 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003950 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3951 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003952 if (size == 6) {
3953 assert(i->i_hasarg);
3954 *code++ = (char)EXTENDED_ARG;
3955 *code++ = ext & 0xff;
3956 *code++ = ext >> 8;
3957 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003958 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003959 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003960 if (i->i_hasarg) {
3961 assert(size == 3 || size == 6);
3962 *code++ = arg & 0xff;
3963 *code++ = arg >> 8;
3964 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003965 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003966}
3967
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003968static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003969assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003970{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003971 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003972 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003973 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003974
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003975 /* Compute the size of each block and fixup jump args.
3976 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003977start:
3978 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003979 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003980 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003981 bsize = blocksize(b);
3982 b->b_offset = totsize;
3983 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003984 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003985 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003986 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3987 bsize = b->b_offset;
3988 for (i = 0; i < b->b_iused; i++) {
3989 struct instr *instr = &b->b_instr[i];
3990 /* Relative jumps are computed relative to
3991 the instruction pointer after fetching
3992 the jump instruction.
3993 */
3994 bsize += instrsize(instr);
3995 if (instr->i_jabs)
3996 instr->i_oparg = instr->i_target->b_offset;
3997 else if (instr->i_jrel) {
3998 int delta = instr->i_target->b_offset - bsize;
3999 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004000 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00004001 else
4002 continue;
4003 if (instr->i_oparg > 0xffff)
4004 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004005 }
4006 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00004007
4008 /* XXX: This is an awful hack that could hurt performance, but
4009 on the bright side it should work until we come up
4010 with a better solution.
4011
4012 In the meantime, should the goto be dropped in favor
4013 of a loop?
4014
4015 The issue is that in the first loop blocksize() is called
4016 which calls instrsize() which requires i_oparg be set
4017 appropriately. There is a bootstrap problem because
4018 i_oparg is calculated in the second loop above.
4019
4020 So we loop until we stop seeing new EXTENDED_ARGs.
4021 The only EXTENDED_ARGs that could be popping up are
4022 ones in jump instructions. So this should converge
4023 fairly quickly.
4024 */
4025 if (last_extended_arg_count != extended_arg_count) {
4026 last_extended_arg_count = extended_arg_count;
4027 goto start;
4028 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004029}
4030
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004031static PyObject *
4032dict_keys_inorder(PyObject *dict, int offset)
4033{
4034 PyObject *tuple, *k, *v;
4035 int i, pos = 0, size = PyDict_Size(dict);
4036
4037 tuple = PyTuple_New(size);
4038 if (tuple == NULL)
4039 return NULL;
4040 while (PyDict_Next(dict, &pos, &k, &v)) {
4041 i = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004042 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004043 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004044 assert((i - offset) < size);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004045 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004046 PyTuple_SET_ITEM(tuple, i - offset, k);
4047 }
4048 return tuple;
4049}
4050
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004051static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004052compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004053{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004054 PySTEntryObject *ste = c->u->u_ste;
4055 int flags = 0, n;
4056 if (ste->ste_type != ModuleBlock)
4057 flags |= CO_NEWLOCALS;
4058 if (ste->ste_type == FunctionBlock) {
4059 if (!ste->ste_unoptimized)
4060 flags |= CO_OPTIMIZED;
4061 if (ste->ste_nested)
4062 flags |= CO_NESTED;
4063 if (ste->ste_generator)
4064 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004065 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004066 if (ste->ste_varargs)
4067 flags |= CO_VARARGS;
4068 if (ste->ste_varkeywords)
4069 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00004070 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004071 flags |= CO_GENERATOR;
4072 if (c->c_flags->cf_flags & CO_FUTURE_DIVISION)
4073 flags |= CO_FUTURE_DIVISION;
4074 n = PyDict_Size(c->u->u_freevars);
4075 if (n < 0)
4076 return -1;
4077 if (n == 0) {
4078 n = PyDict_Size(c->u->u_cellvars);
4079 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004080 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004081 if (n == 0) {
4082 flags |= CO_NOFREE;
4083 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004084 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004085
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004086 return flags;
4087}
4088
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004089static PyCodeObject *
4090makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004091{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004092 PyObject *tmp;
4093 PyCodeObject *co = NULL;
4094 PyObject *consts = NULL;
4095 PyObject *names = NULL;
4096 PyObject *varnames = NULL;
4097 PyObject *filename = NULL;
4098 PyObject *name = NULL;
4099 PyObject *freevars = NULL;
4100 PyObject *cellvars = NULL;
4101 PyObject *bytecode = NULL;
4102 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004103
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004104 tmp = dict_keys_inorder(c->u->u_consts, 0);
4105 if (!tmp)
4106 goto error;
4107 consts = PySequence_List(tmp); /* optimize_code requires a list */
4108 Py_DECREF(tmp);
4109
4110 names = dict_keys_inorder(c->u->u_names, 0);
4111 varnames = dict_keys_inorder(c->u->u_varnames, 0);
4112 if (!consts || !names || !varnames)
4113 goto error;
4114
4115 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4116 if (!cellvars)
4117 goto error;
4118 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4119 if (!freevars)
4120 goto error;
4121 filename = PyString_FromString(c->c_filename);
4122 if (!filename)
4123 goto error;
4124
4125 nlocals = PyDict_Size(c->u->u_varnames);
4126 flags = compute_code_flags(c);
4127 if (flags < 0)
4128 goto error;
4129
4130 bytecode = optimize_code(a->a_bytecode, consts, names, a->a_lnotab);
4131 if (!bytecode)
4132 goto error;
4133
4134 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4135 if (!tmp)
4136 goto error;
4137 Py_DECREF(consts);
4138 consts = tmp;
4139
4140 co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags,
4141 bytecode, consts, names, varnames,
4142 freevars, cellvars,
4143 filename, c->u->u_name,
4144 c->u->u_firstlineno,
4145 a->a_lnotab);
4146 error:
4147 Py_XDECREF(consts);
4148 Py_XDECREF(names);
4149 Py_XDECREF(varnames);
4150 Py_XDECREF(filename);
4151 Py_XDECREF(name);
4152 Py_XDECREF(freevars);
4153 Py_XDECREF(cellvars);
4154 Py_XDECREF(bytecode);
4155 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004156}
4157
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004158static PyCodeObject *
4159assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004160{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004161 basicblock *b, *entryblock;
4162 struct assembler a;
4163 int i, j, nblocks;
4164 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004165
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004166 /* Make sure every block that falls off the end returns None.
4167 XXX NEXT_BLOCK() isn't quite right, because if the last
4168 block ends with a jump or return b_next shouldn't set.
4169 */
4170 if (!c->u->u_curblock->b_return) {
4171 NEXT_BLOCK(c);
4172 if (addNone)
4173 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4174 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004175 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004176
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004177 nblocks = 0;
4178 entryblock = NULL;
4179 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4180 nblocks++;
4181 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004182 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004183
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004184 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4185 goto error;
4186 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004187
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004188 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004189 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004190
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004191 /* Emit code in reverse postorder from dfs. */
4192 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00004193 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004194 for (j = 0; j < b->b_iused; j++)
4195 if (!assemble_emit(&a, &b->b_instr[j]))
4196 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004197 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004198
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004199 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4200 goto error;
4201 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
4202 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004203
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004204 co = makecode(c, &a);
4205 error:
4206 assemble_free(&a);
4207 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004208}