blob: 0e8e50c12d575911371528906104a3bdcc036019 [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{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000320 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000321 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{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000355 Py_ssize_t pos = 0, i = offset, scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000356 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;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000410 Py_ssize_t i, arg, len_consts;
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000411
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;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000461 Py_ssize_t len_consts, size;
462 int opcode;
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000463
464 /* Pre-conditions */
465 assert(PyList_CheckExact(consts));
466 assert(codestr[0] == LOAD_CONST);
467 assert(codestr[3] == LOAD_CONST);
468
469 /* Create new constant */
470 v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
471 w = PyList_GET_ITEM(consts, GETARG(codestr, 3));
472 opcode = codestr[6];
473 switch (opcode) {
474 case BINARY_POWER:
475 newconst = PyNumber_Power(v, w, Py_None);
476 break;
477 case BINARY_MULTIPLY:
478 newconst = PyNumber_Multiply(v, w);
479 break;
480 case BINARY_DIVIDE:
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000481 /* Cannot fold this operation statically since
482 the result can depend on the run-time presence of the -Qnew flag */
Armin Rigo664b43b2005-01-07 18:10:51 +0000483 return 0;
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000484 case BINARY_TRUE_DIVIDE:
485 newconst = PyNumber_TrueDivide(v, w);
486 break;
487 case BINARY_FLOOR_DIVIDE:
488 newconst = PyNumber_FloorDivide(v, w);
489 break;
490 case BINARY_MODULO:
491 newconst = PyNumber_Remainder(v, w);
492 break;
493 case BINARY_ADD:
494 newconst = PyNumber_Add(v, w);
495 break;
496 case BINARY_SUBTRACT:
497 newconst = PyNumber_Subtract(v, w);
498 break;
499 case BINARY_SUBSCR:
500 newconst = PyObject_GetItem(v, w);
501 break;
502 case BINARY_LSHIFT:
503 newconst = PyNumber_Lshift(v, w);
504 break;
505 case BINARY_RSHIFT:
506 newconst = PyNumber_Rshift(v, w);
507 break;
508 case BINARY_AND:
509 newconst = PyNumber_And(v, w);
510 break;
511 case BINARY_XOR:
512 newconst = PyNumber_Xor(v, w);
513 break;
514 case BINARY_OR:
515 newconst = PyNumber_Or(v, w);
516 break;
517 default:
518 /* Called with an unknown opcode */
Neal Norwitz4737b232005-11-19 23:58:29 +0000519 PyErr_Format(PyExc_SystemError,
520 "unexpected binary operation %d on a constant",
521 opcode);
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000522 return 0;
523 }
524 if (newconst == NULL) {
525 PyErr_Clear();
526 return 0;
527 }
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000528 size = PyObject_Size(newconst);
529 if (size == -1)
530 PyErr_Clear();
531 else if (size > 20) {
532 Py_DECREF(newconst);
533 return 0;
534 }
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000535
536 /* Append folded constant into consts table */
537 len_consts = PyList_GET_SIZE(consts);
538 if (PyList_Append(consts, newconst)) {
539 Py_DECREF(newconst);
540 return 0;
541 }
542 Py_DECREF(newconst);
543
544 /* Write NOP NOP NOP NOP LOAD_CONST newconst */
545 memset(codestr, NOP, 4);
546 codestr[4] = LOAD_CONST;
547 SETARG(codestr, 4, len_consts);
548 return 1;
549}
550
Raymond Hettinger80121492005-02-20 12:41:32 +0000551static int
552fold_unaryops_on_constants(unsigned char *codestr, PyObject *consts)
553{
Raymond Hettingere63a0782005-02-23 13:37:55 +0000554 PyObject *newconst=NULL, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000555 Py_ssize_t len_consts;
556 int opcode;
Raymond Hettinger80121492005-02-20 12:41:32 +0000557
558 /* Pre-conditions */
559 assert(PyList_CheckExact(consts));
560 assert(codestr[0] == LOAD_CONST);
561
562 /* Create new constant */
563 v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
564 opcode = codestr[3];
565 switch (opcode) {
566 case UNARY_NEGATIVE:
Raymond Hettingere63a0782005-02-23 13:37:55 +0000567 /* Preserve the sign of -0.0 */
568 if (PyObject_IsTrue(v) == 1)
569 newconst = PyNumber_Negative(v);
Raymond Hettinger80121492005-02-20 12:41:32 +0000570 break;
571 case UNARY_CONVERT:
572 newconst = PyObject_Repr(v);
573 break;
574 case UNARY_INVERT:
575 newconst = PyNumber_Invert(v);
576 break;
577 default:
578 /* Called with an unknown opcode */
Neal Norwitz4737b232005-11-19 23:58:29 +0000579 PyErr_Format(PyExc_SystemError,
580 "unexpected unary operation %d on a constant",
581 opcode);
Raymond Hettinger80121492005-02-20 12:41:32 +0000582 return 0;
583 }
584 if (newconst == NULL) {
585 PyErr_Clear();
586 return 0;
587 }
588
589 /* Append folded constant into consts table */
590 len_consts = PyList_GET_SIZE(consts);
591 if (PyList_Append(consts, newconst)) {
592 Py_DECREF(newconst);
593 return 0;
594 }
595 Py_DECREF(newconst);
596
597 /* Write NOP LOAD_CONST newconst */
598 codestr[0] = NOP;
599 codestr[1] = LOAD_CONST;
600 SETARG(codestr, 1, len_consts);
601 return 1;
602}
603
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000604static unsigned int *
605markblocks(unsigned char *code, int len)
606{
607 unsigned int *blocks = PyMem_Malloc(len*sizeof(int));
Raymond Hettingereffb3932004-10-30 08:55:08 +0000608 int i,j, opcode, blockcnt = 0;
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000609
610 if (blocks == NULL)
611 return NULL;
612 memset(blocks, 0, len*sizeof(int));
Raymond Hettingereffb3932004-10-30 08:55:08 +0000613
614 /* Mark labels in the first pass */
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000615 for (i=0 ; i<len ; i+=CODESIZE(opcode)) {
616 opcode = code[i];
617 switch (opcode) {
618 case FOR_ITER:
619 case JUMP_FORWARD:
620 case JUMP_IF_FALSE:
621 case JUMP_IF_TRUE:
622 case JUMP_ABSOLUTE:
623 case CONTINUE_LOOP:
624 case SETUP_LOOP:
625 case SETUP_EXCEPT:
626 case SETUP_FINALLY:
627 j = GETJUMPTGT(code, i);
Raymond Hettingereffb3932004-10-30 08:55:08 +0000628 blocks[j] = 1;
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000629 break;
630 }
631 }
Raymond Hettingereffb3932004-10-30 08:55:08 +0000632 /* Build block numbers in the second pass */
633 for (i=0 ; i<len ; i++) {
634 blockcnt += blocks[i]; /* increment blockcnt over labels */
635 blocks[i] = blockcnt;
636 }
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000637 return blocks;
638}
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000639
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000640/* Perform basic peephole optimizations to components of a code object.
641 The consts object should still be in list form to allow new constants
642 to be appended.
643
644 To keep the optimizer simple, it bails out (does nothing) for code
645 containing extended arguments or that has a length over 32,700. That
646 allows us to avoid overflow and sign issues. Likewise, it bails when
647 the lineno table has complex encoding for gaps >= 255.
648
649 Optimizations are restricted to simple transformations occuring within a
650 single basic block. All transformations keep the code size the same or
651 smaller. For those that reduce size, the gaps are initially filled with
652 NOPs. Later those NOPs are removed and the jump addresses retargeted in
653 a single pass. Line numbering is adjusted accordingly. */
654
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000655static PyObject *
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000656optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *lineno_obj)
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000657{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000658 Py_ssize_t i, j, codelen;
659 int nops, h, adj;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000660 int tgt, tgttgt, opcode;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000661 unsigned char *codestr = NULL;
662 unsigned char *lineno;
663 int *addrmap = NULL;
664 int new_line, cum_orig_line, last_line, tabsiz;
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000665 int cumlc=0, lastlc=0; /* Count runs of consecutive LOAD_CONST codes */
Raymond Hettingereffb3932004-10-30 08:55:08 +0000666 unsigned int *blocks = NULL;
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000667 char *name;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000668
Raymond Hettingereffb3932004-10-30 08:55:08 +0000669 /* Bail out if an exception is set */
670 if (PyErr_Occurred())
671 goto exitUnchanged;
672
Raymond Hettinger1792bfb2004-08-25 17:19:38 +0000673 /* Bypass optimization when the lineno table is too complex */
674 assert(PyString_Check(lineno_obj));
Brett Cannonc9371d42005-06-25 08:23:41 +0000675 lineno = (unsigned char*)PyString_AS_STRING(lineno_obj);
Raymond Hettinger1792bfb2004-08-25 17:19:38 +0000676 tabsiz = PyString_GET_SIZE(lineno_obj);
677 if (memchr(lineno, 255, tabsiz) != NULL)
678 goto exitUnchanged;
679
Raymond Hettingera12fa142004-08-24 04:34:16 +0000680 /* Avoid situations where jump retargeting could overflow */
Raymond Hettinger06cc9732004-09-28 17:22:12 +0000681 assert(PyString_Check(code));
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000682 codelen = PyString_Size(code);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000683 if (codelen > 32700)
Raymond Hettingera12fa142004-08-24 04:34:16 +0000684 goto exitUnchanged;
685
686 /* Make a modifiable copy of the code string */
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000687 codestr = PyMem_Malloc(codelen);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000688 if (codestr == NULL)
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000689 goto exitUnchanged;
690 codestr = memcpy(codestr, PyString_AS_STRING(code), codelen);
Raymond Hettinger98bd1812004-08-06 19:46:34 +0000691
Raymond Hettinger07359a72005-02-21 20:03:14 +0000692 /* Verify that RETURN_VALUE terminates the codestring. This allows
693 the various transformation patterns to look ahead several
694 instructions without additional checks to make sure they are not
695 looking beyond the end of the code string.
696 */
697 if (codestr[codelen-1] != RETURN_VALUE)
698 goto exitUnchanged;
699
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000700 /* Mapping to new jump targets after NOPs are removed */
701 addrmap = PyMem_Malloc(codelen * sizeof(int));
702 if (addrmap == NULL)
703 goto exitUnchanged;
704
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000705 blocks = markblocks(codestr, codelen);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000706 if (blocks == NULL)
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000707 goto exitUnchanged;
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000708 assert(PyList_Check(consts));
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000709
Raymond Hettinger099ecfb2004-11-01 15:19:11 +0000710 for (i=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000711 opcode = codestr[i];
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000712
713 lastlc = cumlc;
714 cumlc = 0;
715
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000716 switch (opcode) {
717
Raymond Hettinger43ea47f2004-06-24 09:25:39 +0000718 /* Replace UNARY_NOT JUMP_IF_FALSE POP_TOP with
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000719 with JUMP_IF_TRUE POP_TOP */
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000720 case UNARY_NOT:
721 if (codestr[i+1] != JUMP_IF_FALSE ||
722 codestr[i+4] != POP_TOP ||
723 !ISBASICBLOCK(blocks,i,5))
724 continue;
725 tgt = GETJUMPTGT(codestr, (i+1));
726 if (codestr[tgt] != POP_TOP)
727 continue;
Raymond Hettinger43ea47f2004-06-24 09:25:39 +0000728 j = GETARG(codestr, i+1) + 1;
729 codestr[i] = JUMP_IF_TRUE;
730 SETARG(codestr, i, j);
731 codestr[i+3] = POP_TOP;
732 codestr[i+4] = NOP;
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000733 break;
734
735 /* not a is b --> a is not b
736 not a in b --> a not in b
737 not a is not b --> a is b
Raymond Hettingerb615bf02005-02-10 01:42:32 +0000738 not a not in b --> a in b
Raymond Hettingera1645742005-02-06 22:05:42 +0000739 */
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000740 case COMPARE_OP:
741 j = GETARG(codestr, i);
742 if (j < 6 || j > 9 ||
743 codestr[i+3] != UNARY_NOT ||
744 !ISBASICBLOCK(blocks,i,4))
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000745 continue;
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000746 SETARG(codestr, i, (j^1));
747 codestr[i+3] = NOP;
Tim Petersdb5860b2004-07-17 05:00:52 +0000748 break;
749
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000750 /* Replace LOAD_GLOBAL/LOAD_NAME None with LOAD_CONST None */
751 case LOAD_NAME:
752 case LOAD_GLOBAL:
753 j = GETARG(codestr, i);
754 name = PyString_AsString(PyTuple_GET_ITEM(names, j));
755 if (name == NULL || strcmp(name, "None") != 0)
756 continue;
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000757 for (j=0 ; j < PyList_GET_SIZE(consts) ; j++) {
758 if (PyList_GET_ITEM(consts, j) == Py_None) {
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000759 codestr[i] = LOAD_CONST;
760 SETARG(codestr, i, j);
Raymond Hettinger5dec0962004-11-02 04:20:10 +0000761 cumlc = lastlc + 1;
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000762 break;
763 }
764 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000765 break;
766
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000767 /* Skip over LOAD_CONST trueconst JUMP_IF_FALSE xx POP_TOP */
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000768 case LOAD_CONST:
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000769 cumlc = lastlc + 1;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000770 j = GETARG(codestr, i);
771 if (codestr[i+3] != JUMP_IF_FALSE ||
772 codestr[i+6] != POP_TOP ||
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000773 !ISBASICBLOCK(blocks,i,7) ||
774 !PyObject_IsTrue(PyList_GET_ITEM(consts, j)))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000775 continue;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000776 memset(codestr+i, NOP, 7);
Raymond Hettinger5dec0962004-11-02 04:20:10 +0000777 cumlc = 0;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000778 break;
779
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000780 /* Try to fold tuples of constants (includes a case for lists
781 which are only used for "in" and "not in" tests).
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000782 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000783 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
784 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000785 case BUILD_TUPLE:
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000786 case BUILD_LIST:
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000787 j = GETARG(codestr, i);
788 h = i - 3 * j;
789 if (h >= 0 &&
Raymond Hettingereffb3932004-10-30 08:55:08 +0000790 j <= lastlc &&
Brett Cannon5dc8ced2005-03-03 07:01:48 +0000791 ((opcode == BUILD_TUPLE &&
792 ISBASICBLOCK(blocks, h, 3*(j+1))) ||
793 (opcode == BUILD_LIST &&
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000794 codestr[i+3]==COMPARE_OP &&
795 ISBASICBLOCK(blocks, h, 3*(j+2)) &&
Brett Cannon5dc8ced2005-03-03 07:01:48 +0000796 (GETARG(codestr,i+3)==6 ||
797 GETARG(codestr,i+3)==7))) &&
798 tuple_of_constants(&codestr[h], j, consts)) {
Raymond Hettinger5dec0962004-11-02 04:20:10 +0000799 assert(codestr[i] == LOAD_CONST);
800 cumlc = 1;
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000801 break;
802 }
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000803 if (codestr[i+3] != UNPACK_SEQUENCE ||
804 !ISBASICBLOCK(blocks,i,6) ||
805 j != GETARG(codestr, i+3))
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000806 continue;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000807 if (j == 1) {
808 memset(codestr+i, NOP, 6);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000809 } else if (j == 2) {
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000810 codestr[i] = ROT_TWO;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000811 memset(codestr+i+1, NOP, 5);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000812 } else if (j == 3) {
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000813 codestr[i] = ROT_THREE;
814 codestr[i+1] = ROT_TWO;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000815 memset(codestr+i+2, NOP, 4);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000816 }
817 break;
818
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000819 /* Fold binary ops on constants.
820 LOAD_CONST c1 LOAD_CONST c2 BINOP --> LOAD_CONST binop(c1,c2) */
821 case BINARY_POWER:
822 case BINARY_MULTIPLY:
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000823 case BINARY_TRUE_DIVIDE:
824 case BINARY_FLOOR_DIVIDE:
825 case BINARY_MODULO:
826 case BINARY_ADD:
827 case BINARY_SUBTRACT:
828 case BINARY_SUBSCR:
829 case BINARY_LSHIFT:
830 case BINARY_RSHIFT:
831 case BINARY_AND:
832 case BINARY_XOR:
833 case BINARY_OR:
834 if (lastlc >= 2 &&
835 ISBASICBLOCK(blocks, i-6, 7) &&
836 fold_binops_on_constants(&codestr[i-6], consts)) {
837 i -= 2;
838 assert(codestr[i] == LOAD_CONST);
839 cumlc = 1;
840 }
841 break;
842
Raymond Hettinger80121492005-02-20 12:41:32 +0000843 /* Fold unary ops on constants.
844 LOAD_CONST c1 UNARY_OP --> LOAD_CONST unary_op(c) */
845 case UNARY_NEGATIVE:
846 case UNARY_CONVERT:
847 case UNARY_INVERT:
848 if (lastlc >= 1 &&
849 ISBASICBLOCK(blocks, i-3, 4) &&
850 fold_unaryops_on_constants(&codestr[i-3], consts)) {
851 i -= 2;
852 assert(codestr[i] == LOAD_CONST);
853 cumlc = 1;
854 }
855 break;
856
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000857 /* Simplify conditional jump to conditional jump where the
858 result of the first test implies the success of a similar
859 test or the failure of the opposite test.
860 Arises in code like:
Raymond Hettingereffb3932004-10-30 08:55:08 +0000861 "if a and b:"
862 "if a or b:"
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000863 "a and b or c"
Armin Rigod7bcf4d2004-10-30 21:08:59 +0000864 "(a and b) and c"
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000865 x:JUMP_IF_FALSE y y:JUMP_IF_FALSE z --> x:JUMP_IF_FALSE z
Raymond Hettinger65d3c052004-08-25 15:15:56 +0000866 x:JUMP_IF_FALSE y y:JUMP_IF_TRUE z --> x:JUMP_IF_FALSE y+3
867 where y+3 is the instruction following the second test.
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000868 */
869 case JUMP_IF_FALSE:
870 case JUMP_IF_TRUE:
871 tgt = GETJUMPTGT(codestr, i);
872 j = codestr[tgt];
873 if (j == JUMP_IF_FALSE || j == JUMP_IF_TRUE) {
874 if (j == opcode) {
875 tgttgt = GETJUMPTGT(codestr, tgt) - i - 3;
876 SETARG(codestr, i, tgttgt);
877 } else {
878 tgt -= i;
879 SETARG(codestr, i, tgt);
880 }
881 break;
882 }
883 /* Intentional fallthrough */
884
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000885 /* Replace jumps to unconditional jumps */
Raymond Hettinger255a3d02003-04-15 10:35:07 +0000886 case FOR_ITER:
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000887 case JUMP_FORWARD:
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000888 case JUMP_ABSOLUTE:
889 case CONTINUE_LOOP:
890 case SETUP_LOOP:
891 case SETUP_EXCEPT:
892 case SETUP_FINALLY:
893 tgt = GETJUMPTGT(codestr, i);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000894 if (!UNCONDITIONAL_JUMP(codestr[tgt]))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000895 continue;
896 tgttgt = GETJUMPTGT(codestr, tgt);
897 if (opcode == JUMP_FORWARD) /* JMP_ABS can go backwards */
898 opcode = JUMP_ABSOLUTE;
Raymond Hettinger5b75c382003-03-28 12:05:00 +0000899 if (!ABSOLUTE_JUMP(opcode))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000900 tgttgt -= i + 3; /* Calc relative jump addr */
901 if (tgttgt < 0) /* No backward relative jumps */
902 continue;
903 codestr[i] = opcode;
904 SETARG(codestr, i, tgttgt);
905 break;
906
907 case EXTENDED_ARG:
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000908 goto exitUnchanged;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000909
910 /* Replace RETURN LOAD_CONST None RETURN with just RETURN */
911 case RETURN_VALUE:
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000912 if (i+4 >= codelen ||
913 codestr[i+4] != RETURN_VALUE ||
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000914 !ISBASICBLOCK(blocks,i,5))
915 continue;
916 memset(codestr+i+1, NOP, 4);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000917 break;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000918 }
919 }
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000920
921 /* Fixup linenotab */
Raymond Hettinger099ecfb2004-11-01 15:19:11 +0000922 for (i=0, nops=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
923 addrmap[i] = i - nops;
924 if (codestr[i] == NOP)
925 nops++;
926 }
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000927 cum_orig_line = 0;
928 last_line = 0;
929 for (i=0 ; i < tabsiz ; i+=2) {
930 cum_orig_line += lineno[i];
931 new_line = addrmap[cum_orig_line];
Raymond Hettinger1792bfb2004-08-25 17:19:38 +0000932 assert (new_line - last_line < 255);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000933 lineno[i] =((unsigned char)(new_line - last_line));
934 last_line = new_line;
935 }
936
937 /* Remove NOPs and fixup jump targets */
938 for (i=0, h=0 ; i<codelen ; ) {
939 opcode = codestr[i];
940 switch (opcode) {
941 case NOP:
942 i++;
943 continue;
944
945 case JUMP_ABSOLUTE:
946 case CONTINUE_LOOP:
947 j = addrmap[GETARG(codestr, i)];
948 SETARG(codestr, i, j);
949 break;
950
951 case FOR_ITER:
952 case JUMP_FORWARD:
953 case JUMP_IF_FALSE:
954 case JUMP_IF_TRUE:
955 case SETUP_LOOP:
956 case SETUP_EXCEPT:
957 case SETUP_FINALLY:
958 j = addrmap[GETARG(codestr, i) + i + 3] - addrmap[i] - 3;
959 SETARG(codestr, i, j);
960 break;
961 }
962 adj = CODESIZE(opcode);
963 while (adj--)
964 codestr[h++] = codestr[i++];
965 }
Raymond Hettingera12fa142004-08-24 04:34:16 +0000966 assert(h + nops == codelen);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000967
968 code = PyString_FromStringAndSize((char *)codestr, h);
969 PyMem_Free(addrmap);
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000970 PyMem_Free(codestr);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000971 PyMem_Free(blocks);
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000972 return code;
973
974exitUnchanged:
Raymond Hettingereffb3932004-10-30 08:55:08 +0000975 if (blocks != NULL)
976 PyMem_Free(blocks);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000977 if (addrmap != NULL)
978 PyMem_Free(addrmap);
979 if (codestr != NULL)
980 PyMem_Free(codestr);
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000981 Py_INCREF(code);
982 return code;
983}
984
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000985/* End: Peephole optimizations ----------------------------------------- */
986
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987/*
988
989Leave this debugging code for just a little longer.
990
991static void
992compiler_display_symbols(PyObject *name, PyObject *symbols)
993{
994 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000995 int flags;
996 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997
998 fprintf(stderr, "block %s\n", PyString_AS_STRING(name));
999 while (PyDict_Next(symbols, &pos, &key, &value)) {
1000 flags = PyInt_AsLong(value);
1001 fprintf(stderr, "var %s:", PyString_AS_STRING(key));
1002 if (flags & DEF_GLOBAL)
1003 fprintf(stderr, " declared_global");
1004 if (flags & DEF_LOCAL)
1005 fprintf(stderr, " local");
1006 if (flags & DEF_PARAM)
1007 fprintf(stderr, " param");
1008 if (flags & DEF_STAR)
1009 fprintf(stderr, " stararg");
1010 if (flags & DEF_DOUBLESTAR)
1011 fprintf(stderr, " starstar");
1012 if (flags & DEF_INTUPLE)
1013 fprintf(stderr, " tuple");
1014 if (flags & DEF_FREE)
1015 fprintf(stderr, " free");
1016 if (flags & DEF_FREE_GLOBAL)
1017 fprintf(stderr, " global");
1018 if (flags & DEF_FREE_CLASS)
1019 fprintf(stderr, " free/class");
1020 if (flags & DEF_IMPORT)
1021 fprintf(stderr, " import");
1022 fprintf(stderr, "\n");
1023 }
1024 fprintf(stderr, "\n");
1025}
1026*/
1027
1028static void
1029compiler_unit_check(struct compiler_unit *u)
1030{
1031 basicblock *block;
1032 for (block = u->u_blocks; block != NULL; block = block->b_list) {
1033 assert(block != (void *)0xcbcbcbcb);
1034 assert(block != (void *)0xfbfbfbfb);
1035 assert(block != (void *)0xdbdbdbdb);
1036 if (block->b_instr != NULL) {
1037 assert(block->b_ialloc > 0);
1038 assert(block->b_iused > 0);
1039 assert(block->b_ialloc >= block->b_iused);
1040 }
1041 else {
1042 assert (block->b_iused == 0);
1043 assert (block->b_ialloc == 0);
1044 }
1045 }
1046}
1047
1048static void
1049compiler_unit_free(struct compiler_unit *u)
1050{
1051 basicblock *b, *next;
1052
1053 compiler_unit_check(u);
1054 b = u->u_blocks;
1055 while (b != NULL) {
1056 if (b->b_instr)
1057 PyObject_Free((void *)b->b_instr);
1058 next = b->b_list;
1059 PyObject_Free((void *)b);
1060 b = next;
1061 }
1062 Py_XDECREF(u->u_ste);
1063 Py_XDECREF(u->u_name);
1064 Py_XDECREF(u->u_consts);
1065 Py_XDECREF(u->u_names);
1066 Py_XDECREF(u->u_varnames);
1067 Py_XDECREF(u->u_freevars);
1068 Py_XDECREF(u->u_cellvars);
1069 Py_XDECREF(u->u_private);
1070 PyObject_Free(u);
1071}
1072
1073static int
1074compiler_enter_scope(struct compiler *c, identifier name, void *key,
1075 int lineno)
1076{
1077 struct compiler_unit *u;
1078
1079 u = PyObject_Malloc(sizeof(struct compiler_unit));
Neal Norwitz87b801c2005-12-18 04:42:47 +00001080 if (!u) {
1081 PyErr_NoMemory();
1082 return 0;
1083 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084 memset(u, 0, sizeof(struct compiler_unit));
1085 u->u_argcount = 0;
1086 u->u_ste = PySymtable_Lookup(c->c_st, key);
1087 if (!u->u_ste) {
1088 compiler_unit_free(u);
Neal Norwitz87b801c2005-12-18 04:42:47 +00001089 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001090 }
1091 Py_INCREF(name);
1092 u->u_name = name;
1093 u->u_varnames = list2dict(u->u_ste->ste_varnames);
1094 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
1095 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
1096 PyDict_Size(u->u_cellvars));
1097
1098 u->u_blocks = NULL;
1099 u->u_tmpname = 0;
1100 u->u_nfblocks = 0;
1101 u->u_firstlineno = lineno;
1102 u->u_lineno = 0;
1103 u->u_lineno_set = false;
1104 u->u_consts = PyDict_New();
1105 if (!u->u_consts) {
1106 compiler_unit_free(u);
1107 return 0;
1108 }
1109 u->u_names = PyDict_New();
1110 if (!u->u_names) {
1111 compiler_unit_free(u);
1112 return 0;
1113 }
1114
1115 u->u_private = NULL;
1116
1117 /* Push the old compiler_unit on the stack. */
1118 if (c->u) {
1119 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
1120 if (PyList_Append(c->c_stack, wrapper) < 0) {
1121 compiler_unit_free(u);
1122 return 0;
1123 }
1124 Py_DECREF(wrapper);
1125 u->u_private = c->u->u_private;
1126 Py_XINCREF(u->u_private);
1127 }
1128 c->u = u;
1129
1130 c->c_nestlevel++;
Martin v. Löwis94962612006-01-02 21:15:05 +00001131 if (compiler_use_new_block(c) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132 return 0;
1133
1134 return 1;
1135}
1136
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001137static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138compiler_exit_scope(struct compiler *c)
1139{
1140 int n;
1141 PyObject *wrapper;
1142
1143 c->c_nestlevel--;
1144 compiler_unit_free(c->u);
1145 /* Restore c->u to the parent unit. */
1146 n = PyList_GET_SIZE(c->c_stack) - 1;
1147 if (n >= 0) {
1148 wrapper = PyList_GET_ITEM(c->c_stack, n);
1149 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001150 /* we are deleting from a list so this really shouldn't fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151 if (PySequence_DelItem(c->c_stack, n) < 0)
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001152 Py_FatalError("compiler_exit_scope()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153 compiler_unit_check(c->u);
1154 }
1155 else
1156 c->u = NULL;
1157
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158}
1159
1160/* Allocate a new block and return a pointer to it.
1161 Returns NULL on error.
1162*/
1163
1164static basicblock *
1165compiler_new_block(struct compiler *c)
1166{
1167 basicblock *b;
1168 struct compiler_unit *u;
1169
1170 u = c->u;
1171 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
Neal Norwitz87b801c2005-12-18 04:42:47 +00001172 if (b == NULL) {
1173 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174 return NULL;
Neal Norwitz87b801c2005-12-18 04:42:47 +00001175 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176 memset((void *)b, 0, sizeof(basicblock));
1177 assert (b->b_next == NULL);
1178 b->b_list = u->u_blocks;
1179 u->u_blocks = b;
1180 return b;
1181}
1182
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001183static basicblock *
1184compiler_use_new_block(struct compiler *c)
1185{
1186 basicblock *block = compiler_new_block(c);
1187 if (block == NULL)
1188 return NULL;
1189 c->u->u_curblock = block;
1190 return block;
1191}
1192
1193static basicblock *
1194compiler_next_block(struct compiler *c)
1195{
1196 basicblock *block = compiler_new_block(c);
1197 if (block == NULL)
1198 return NULL;
1199 c->u->u_curblock->b_next = block;
1200 c->u->u_curblock = block;
1201 return block;
1202}
1203
1204static basicblock *
1205compiler_use_next_block(struct compiler *c, basicblock *block)
1206{
1207 assert(block != NULL);
1208 c->u->u_curblock->b_next = block;
1209 c->u->u_curblock = block;
1210 return block;
1211}
1212
1213/* Returns the offset of the next instruction in the current block's
1214 b_instr array. Resizes the b_instr as necessary.
1215 Returns -1 on failure.
1216 */
1217
1218static int
1219compiler_next_instr(struct compiler *c, basicblock *b)
1220{
1221 assert(b != NULL);
1222 if (b->b_instr == NULL) {
1223 b->b_instr = PyObject_Malloc(sizeof(struct instr) *
1224 DEFAULT_BLOCK_SIZE);
1225 if (b->b_instr == NULL) {
1226 PyErr_NoMemory();
1227 return -1;
1228 }
1229 b->b_ialloc = DEFAULT_BLOCK_SIZE;
1230 memset((char *)b->b_instr, 0,
1231 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
1232 }
1233 else if (b->b_iused == b->b_ialloc) {
1234 size_t oldsize, newsize;
1235 oldsize = b->b_ialloc * sizeof(struct instr);
1236 newsize = oldsize << 1;
1237 if (newsize == 0) {
1238 PyErr_NoMemory();
1239 return -1;
1240 }
1241 b->b_ialloc <<= 1;
1242 b->b_instr = PyObject_Realloc((void *)b->b_instr, newsize);
1243 if (b->b_instr == NULL)
1244 return -1;
1245 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
1246 }
1247 return b->b_iused++;
1248}
1249
1250static void
1251compiler_set_lineno(struct compiler *c, int off)
1252{
1253 basicblock *b;
1254 if (c->u->u_lineno_set)
1255 return;
1256 c->u->u_lineno_set = true;
1257 b = c->u->u_curblock;
1258 b->b_instr[off].i_lineno = c->u->u_lineno;
1259}
1260
1261static int
1262opcode_stack_effect(int opcode, int oparg)
1263{
1264 switch (opcode) {
1265 case POP_TOP:
1266 return -1;
1267 case ROT_TWO:
1268 case ROT_THREE:
1269 return 0;
1270 case DUP_TOP:
1271 return 1;
1272 case ROT_FOUR:
1273 return 0;
1274
1275 case UNARY_POSITIVE:
1276 case UNARY_NEGATIVE:
1277 case UNARY_NOT:
1278 case UNARY_CONVERT:
1279 case UNARY_INVERT:
1280 return 0;
1281
1282 case BINARY_POWER:
1283 case BINARY_MULTIPLY:
1284 case BINARY_DIVIDE:
1285 case BINARY_MODULO:
1286 case BINARY_ADD:
1287 case BINARY_SUBTRACT:
1288 case BINARY_SUBSCR:
1289 case BINARY_FLOOR_DIVIDE:
1290 case BINARY_TRUE_DIVIDE:
1291 return -1;
1292 case INPLACE_FLOOR_DIVIDE:
1293 case INPLACE_TRUE_DIVIDE:
1294 return -1;
1295
1296 case SLICE+0:
1297 return 1;
1298 case SLICE+1:
1299 return 0;
1300 case SLICE+2:
1301 return 0;
1302 case SLICE+3:
1303 return -1;
1304
1305 case STORE_SLICE+0:
1306 return -2;
1307 case STORE_SLICE+1:
1308 return -3;
1309 case STORE_SLICE+2:
1310 return -3;
1311 case STORE_SLICE+3:
1312 return -4;
1313
1314 case DELETE_SLICE+0:
1315 return -1;
1316 case DELETE_SLICE+1:
1317 return -2;
1318 case DELETE_SLICE+2:
1319 return -2;
1320 case DELETE_SLICE+3:
1321 return -3;
1322
1323 case INPLACE_ADD:
1324 case INPLACE_SUBTRACT:
1325 case INPLACE_MULTIPLY:
1326 case INPLACE_DIVIDE:
1327 case INPLACE_MODULO:
1328 return -1;
1329 case STORE_SUBSCR:
1330 return -3;
1331 case DELETE_SUBSCR:
1332 return -2;
1333
1334 case BINARY_LSHIFT:
1335 case BINARY_RSHIFT:
1336 case BINARY_AND:
1337 case BINARY_XOR:
1338 case BINARY_OR:
1339 return -1;
1340 case INPLACE_POWER:
1341 return -1;
1342 case GET_ITER:
1343 return 0;
1344
1345 case PRINT_EXPR:
1346 return -1;
1347 case PRINT_ITEM:
1348 return -1;
1349 case PRINT_NEWLINE:
1350 return 0;
1351 case PRINT_ITEM_TO:
1352 return -2;
1353 case PRINT_NEWLINE_TO:
1354 return -1;
1355 case INPLACE_LSHIFT:
1356 case INPLACE_RSHIFT:
1357 case INPLACE_AND:
1358 case INPLACE_XOR:
1359 case INPLACE_OR:
1360 return -1;
1361 case BREAK_LOOP:
1362 return 0;
1363
1364 case LOAD_LOCALS:
1365 return 1;
1366 case RETURN_VALUE:
1367 return -1;
1368 case IMPORT_STAR:
1369 return -1;
1370 case EXEC_STMT:
1371 return -3;
1372 case YIELD_VALUE:
1373 return 0;
1374
1375 case POP_BLOCK:
1376 return 0;
1377 case END_FINALLY:
1378 return -1; /* or -2 or -3 if exception occurred */
1379 case BUILD_CLASS:
1380 return -2;
1381
1382 case STORE_NAME:
1383 return -1;
1384 case DELETE_NAME:
1385 return 0;
1386 case UNPACK_SEQUENCE:
1387 return oparg-1;
1388 case FOR_ITER:
1389 return 1;
1390
1391 case STORE_ATTR:
1392 return -2;
1393 case DELETE_ATTR:
1394 return -1;
1395 case STORE_GLOBAL:
1396 return -1;
1397 case DELETE_GLOBAL:
1398 return 0;
1399 case DUP_TOPX:
1400 return oparg;
1401 case LOAD_CONST:
1402 return 1;
1403 case LOAD_NAME:
1404 return 1;
1405 case BUILD_TUPLE:
1406 case BUILD_LIST:
1407 return 1-oparg;
1408 case BUILD_MAP:
1409 return 1;
1410 case LOAD_ATTR:
1411 return 0;
1412 case COMPARE_OP:
1413 return -1;
1414 case IMPORT_NAME:
1415 return 0;
1416 case IMPORT_FROM:
1417 return 1;
1418
1419 case JUMP_FORWARD:
1420 case JUMP_IF_FALSE:
1421 case JUMP_IF_TRUE:
1422 case JUMP_ABSOLUTE:
1423 return 0;
1424
1425 case LOAD_GLOBAL:
1426 return 1;
1427
1428 case CONTINUE_LOOP:
1429 return 0;
1430 case SETUP_LOOP:
1431 return 0;
1432 case SETUP_EXCEPT:
1433 case SETUP_FINALLY:
1434 return 3; /* actually pushed by an exception */
1435
1436 case LOAD_FAST:
1437 return 1;
1438 case STORE_FAST:
1439 return -1;
1440 case DELETE_FAST:
1441 return 0;
1442
1443 case RAISE_VARARGS:
1444 return -oparg;
1445#define NARGS(o) (((o) % 256) + 2*((o) / 256))
1446 case CALL_FUNCTION:
1447 return -NARGS(oparg);
1448 case CALL_FUNCTION_VAR:
1449 case CALL_FUNCTION_KW:
1450 return -NARGS(oparg)-1;
1451 case CALL_FUNCTION_VAR_KW:
1452 return -NARGS(oparg)-2;
1453#undef NARGS
1454 case MAKE_FUNCTION:
1455 return -oparg;
1456 case BUILD_SLICE:
1457 if (oparg == 3)
1458 return -2;
1459 else
1460 return -1;
1461
1462 case MAKE_CLOSURE:
1463 return -oparg;
1464 case LOAD_CLOSURE:
1465 return 1;
1466 case LOAD_DEREF:
1467 return 1;
1468 case STORE_DEREF:
1469 return -1;
1470 default:
1471 fprintf(stderr, "opcode = %d\n", opcode);
1472 Py_FatalError("opcode_stack_effect()");
1473
1474 }
1475 return 0; /* not reachable */
1476}
1477
1478/* Add an opcode with no argument.
1479 Returns 0 on failure, 1 on success.
1480*/
1481
1482static int
1483compiler_addop(struct compiler *c, int opcode)
1484{
1485 basicblock *b;
1486 struct instr *i;
1487 int off;
1488 off = compiler_next_instr(c, c->u->u_curblock);
1489 if (off < 0)
1490 return 0;
1491 b = c->u->u_curblock;
1492 i = &b->b_instr[off];
1493 i->i_opcode = opcode;
1494 i->i_hasarg = 0;
1495 if (opcode == RETURN_VALUE)
1496 b->b_return = 1;
1497 compiler_set_lineno(c, off);
1498 return 1;
1499}
1500
1501static int
1502compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
1503{
1504 PyObject *t, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001505 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506
1507 /* necessary to make sure types aren't coerced (e.g., int and long) */
Neil Schemenauer3a44aaa2005-10-23 17:21:54 +00001508 t = PyTuple_Pack(2, o, o->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509 if (t == NULL)
1510 return -1;
1511
1512 v = PyDict_GetItem(dict, t);
1513 if (!v) {
1514 arg = PyDict_Size(dict);
1515 v = PyInt_FromLong(arg);
1516 if (!v) {
1517 Py_DECREF(t);
1518 return -1;
1519 }
1520 if (PyDict_SetItem(dict, t, v) < 0) {
1521 Py_DECREF(t);
1522 Py_DECREF(v);
1523 return -1;
1524 }
1525 Py_DECREF(v);
1526 }
1527 else
1528 arg = PyInt_AsLong(v);
1529 Py_DECREF(t);
1530 return arg;
1531}
1532
1533static int
1534compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
1535 PyObject *o)
1536{
1537 int arg = compiler_add_o(c, dict, o);
1538 if (arg < 0)
1539 return 0;
1540 return compiler_addop_i(c, opcode, arg);
1541}
1542
1543static int
1544compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
1545 PyObject *o)
1546{
1547 int arg;
1548 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1549 if (!mangled)
1550 return 0;
1551 arg = compiler_add_o(c, dict, mangled);
1552 Py_DECREF(mangled);
1553 if (arg < 0)
1554 return 0;
1555 return compiler_addop_i(c, opcode, arg);
1556}
1557
1558/* Add an opcode with an integer argument.
1559 Returns 0 on failure, 1 on success.
1560*/
1561
1562static int
1563compiler_addop_i(struct compiler *c, int opcode, int oparg)
1564{
1565 struct instr *i;
1566 int off;
1567 off = compiler_next_instr(c, c->u->u_curblock);
1568 if (off < 0)
1569 return 0;
1570 i = &c->u->u_curblock->b_instr[off];
1571 i->i_opcode = opcode;
1572 i->i_oparg = oparg;
1573 i->i_hasarg = 1;
1574 compiler_set_lineno(c, off);
1575 return 1;
1576}
1577
1578static int
1579compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1580{
1581 struct instr *i;
1582 int off;
1583
1584 assert(b != NULL);
1585 off = compiler_next_instr(c, c->u->u_curblock);
1586 if (off < 0)
1587 return 0;
1588 compiler_set_lineno(c, off);
1589 i = &c->u->u_curblock->b_instr[off];
1590 i->i_opcode = opcode;
1591 i->i_target = b;
1592 i->i_hasarg = 1;
1593 if (absolute)
1594 i->i_jabs = 1;
1595 else
1596 i->i_jrel = 1;
1597 return 1;
1598}
1599
1600/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1601 like to find better names.) NEW_BLOCK() creates a new block and sets
1602 it as the current block. NEXT_BLOCK() also creates an implicit jump
1603 from the current block to the new block.
1604*/
1605
1606/* XXX The returns inside these macros make it impossible to decref
1607 objects created in the local function.
1608*/
1609
1610
1611#define NEW_BLOCK(C) { \
1612 if (compiler_use_new_block((C)) == NULL) \
1613 return 0; \
1614}
1615
1616#define NEXT_BLOCK(C) { \
1617 if (compiler_next_block((C)) == NULL) \
1618 return 0; \
1619}
1620
1621#define ADDOP(C, OP) { \
1622 if (!compiler_addop((C), (OP))) \
1623 return 0; \
1624}
1625
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001626#define ADDOP_IN_SCOPE(C, OP) { \
1627 if (!compiler_addop((C), (OP))) { \
1628 compiler_exit_scope(c); \
1629 return 0; \
1630 } \
1631}
1632
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001633#define ADDOP_O(C, OP, O, TYPE) { \
1634 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1635 return 0; \
1636}
1637
1638#define ADDOP_NAME(C, OP, O, TYPE) { \
1639 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1640 return 0; \
1641}
1642
1643#define ADDOP_I(C, OP, O) { \
1644 if (!compiler_addop_i((C), (OP), (O))) \
1645 return 0; \
1646}
1647
1648#define ADDOP_JABS(C, OP, O) { \
1649 if (!compiler_addop_j((C), (OP), (O), 1)) \
1650 return 0; \
1651}
1652
1653#define ADDOP_JREL(C, OP, O) { \
1654 if (!compiler_addop_j((C), (OP), (O), 0)) \
1655 return 0; \
1656}
1657
1658/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1659 the ASDL name to synthesize the name of the C type and the visit function.
1660*/
1661
1662#define VISIT(C, TYPE, V) {\
1663 if (!compiler_visit_ ## TYPE((C), (V))) \
1664 return 0; \
1665}
1666
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001667#define VISIT_IN_SCOPE(C, TYPE, V) {\
1668 if (!compiler_visit_ ## TYPE((C), (V))) { \
1669 compiler_exit_scope(c); \
1670 return 0; \
1671 } \
1672}
1673
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001674#define VISIT_SLICE(C, V, CTX) {\
1675 if (!compiler_visit_slice((C), (V), (CTX))) \
1676 return 0; \
1677}
1678
1679#define VISIT_SEQ(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001680 int _i; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001681 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001682 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1683 TYPE ## _ty elt = asdl_seq_GET(seq, _i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684 if (!compiler_visit_ ## TYPE((C), elt)) \
1685 return 0; \
1686 } \
1687}
1688
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001689#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001690 int _i; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001691 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001692 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1693 TYPE ## _ty elt = asdl_seq_GET(seq, _i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001694 if (!compiler_visit_ ## TYPE((C), elt)) { \
1695 compiler_exit_scope(c); \
1696 return 0; \
1697 } \
1698 } \
1699}
1700
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001701static int
1702compiler_isdocstring(stmt_ty s)
1703{
1704 if (s->kind != Expr_kind)
1705 return 0;
1706 return s->v.Expr.value->kind == Str_kind;
1707}
1708
1709/* Compile a sequence of statements, checking for a docstring. */
1710
1711static int
1712compiler_body(struct compiler *c, asdl_seq *stmts)
1713{
1714 int i = 0;
1715 stmt_ty st;
1716
1717 if (!asdl_seq_LEN(stmts))
1718 return 1;
1719 st = asdl_seq_GET(stmts, 0);
1720 if (compiler_isdocstring(st)) {
1721 i = 1;
1722 VISIT(c, expr, st->v.Expr.value);
1723 if (!compiler_nameop(c, __doc__, Store))
1724 return 0;
1725 }
1726 for (; i < asdl_seq_LEN(stmts); i++)
1727 VISIT(c, stmt, asdl_seq_GET(stmts, i));
1728 return 1;
1729}
1730
1731static PyCodeObject *
1732compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001733{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001734 PyCodeObject *co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001735 int addNone = 1;
1736 static PyObject *module;
1737 if (!module) {
1738 module = PyString_FromString("<module>");
1739 if (!module)
1740 return NULL;
1741 }
1742 if (!compiler_enter_scope(c, module, mod, 1))
Guido van Rossumd076c731998-10-07 19:42:25 +00001743 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001744 switch (mod->kind) {
1745 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001746 if (!compiler_body(c, mod->v.Module.body)) {
1747 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001748 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001749 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001750 break;
1751 case Interactive_kind:
1752 c->c_interactive = 1;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001753 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001754 break;
1755 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001756 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001757 addNone = 0;
1758 break;
1759 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001760 PyErr_SetString(PyExc_SystemError,
1761 "suite should not be possible");
1762 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001764 PyErr_Format(PyExc_SystemError,
1765 "module kind %d should not be possible",
1766 mod->kind);
1767 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001768 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001769 co = assemble(c, addNone);
1770 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001771 return co;
1772}
1773
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001774/* The test for LOCAL must come before the test for FREE in order to
1775 handle classes where name is both local and free. The local var is
1776 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001777*/
1778
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001779static int
1780get_ref_type(struct compiler *c, PyObject *name)
1781{
1782 int scope = PyST_GetScope(c->u->u_ste, name);
1783 if (scope == 0) {
1784 char buf[350];
1785 PyOS_snprintf(buf, sizeof(buf),
1786 "unknown scope for %.100s in %.100s(%s) in %s\n"
1787 "symbols: %s\nlocals: %s\nglobals: %s\n",
1788 PyString_AS_STRING(name),
1789 PyString_AS_STRING(c->u->u_name),
1790 PyObject_REPR(c->u->u_ste->ste_id),
1791 c->c_filename,
1792 PyObject_REPR(c->u->u_ste->ste_symbols),
1793 PyObject_REPR(c->u->u_varnames),
1794 PyObject_REPR(c->u->u_names)
1795 );
1796 Py_FatalError(buf);
1797 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001798
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001799 return scope;
1800}
1801
1802static int
1803compiler_lookup_arg(PyObject *dict, PyObject *name)
1804{
1805 PyObject *k, *v;
1806 k = Py_BuildValue("(OO)", name, name->ob_type);
1807 if (k == NULL)
1808 return -1;
1809 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001810 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811 if (v == NULL)
1812 return -1;
1813 return PyInt_AS_LONG(v);
1814}
1815
1816static int
1817compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1818{
1819 int i, free = PyCode_GetNumFree(co);
1820 if (free == 0) {
1821 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1822 ADDOP_I(c, MAKE_FUNCTION, args);
1823 return 1;
1824 }
1825 for (i = 0; i < free; ++i) {
1826 /* Bypass com_addop_varname because it will generate
1827 LOAD_DEREF but LOAD_CLOSURE is needed.
1828 */
1829 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1830 int arg, reftype;
1831
1832 /* Special case: If a class contains a method with a
1833 free variable that has the same name as a method,
1834 the name will be considered free *and* local in the
1835 class. It should be handled by the closure, as
1836 well as by the normal name loookup logic.
1837 */
1838 reftype = get_ref_type(c, name);
1839 if (reftype == CELL)
1840 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1841 else /* (reftype == FREE) */
1842 arg = compiler_lookup_arg(c->u->u_freevars, name);
1843 if (arg == -1) {
1844 printf("lookup %s in %s %d %d\n"
1845 "freevars of %s: %s\n",
1846 PyObject_REPR(name),
1847 PyString_AS_STRING(c->u->u_name),
1848 reftype, arg,
1849 PyString_AS_STRING(co->co_name),
1850 PyObject_REPR(co->co_freevars));
1851 Py_FatalError("compiler_make_closure()");
1852 }
1853 ADDOP_I(c, LOAD_CLOSURE, arg);
1854 }
1855 ADDOP_I(c, BUILD_TUPLE, free);
1856 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1857 ADDOP_I(c, MAKE_CLOSURE, args);
1858 return 1;
1859}
1860
1861static int
1862compiler_decorators(struct compiler *c, asdl_seq* decos)
1863{
1864 int i;
1865
1866 if (!decos)
1867 return 1;
1868
1869 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1870 VISIT(c, expr, asdl_seq_GET(decos, i));
1871 }
1872 return 1;
1873}
1874
1875static int
1876compiler_arguments(struct compiler *c, arguments_ty args)
1877{
1878 int i;
1879 int n = asdl_seq_LEN(args->args);
1880 /* Correctly handle nested argument lists */
1881 for (i = 0; i < n; i++) {
1882 expr_ty arg = asdl_seq_GET(args->args, i);
1883 if (arg->kind == Tuple_kind) {
1884 PyObject *id = PyString_FromFormat(".%d", i);
1885 if (id == NULL) {
1886 return 0;
1887 }
1888 if (!compiler_nameop(c, id, Load)) {
1889 Py_DECREF(id);
1890 return 0;
1891 }
1892 Py_DECREF(id);
1893 VISIT(c, expr, arg);
1894 }
1895 }
1896 return 1;
1897}
1898
1899static int
1900compiler_function(struct compiler *c, stmt_ty s)
1901{
1902 PyCodeObject *co;
1903 PyObject *first_const = Py_None;
1904 arguments_ty args = s->v.FunctionDef.args;
1905 asdl_seq* decos = s->v.FunctionDef.decorators;
1906 stmt_ty st;
1907 int i, n, docstring;
1908
1909 assert(s->kind == FunctionDef_kind);
1910
1911 if (!compiler_decorators(c, decos))
1912 return 0;
1913 if (args->defaults)
1914 VISIT_SEQ(c, expr, args->defaults);
1915 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1916 s->lineno))
1917 return 0;
1918
1919 st = asdl_seq_GET(s->v.FunctionDef.body, 0);
1920 docstring = compiler_isdocstring(st);
1921 if (docstring)
1922 first_const = st->v.Expr.value->v.Str.s;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001923 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1924 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001926 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001927
1928 /* unpack nested arguments */
1929 compiler_arguments(c, args);
1930
1931 c->u->u_argcount = asdl_seq_LEN(args->args);
1932 n = asdl_seq_LEN(s->v.FunctionDef.body);
1933 /* if there was a docstring, we need to skip the first statement */
1934 for (i = docstring; i < n; i++) {
1935 stmt_ty s2 = asdl_seq_GET(s->v.FunctionDef.body, i);
1936 if (i == 0 && s2->kind == Expr_kind &&
1937 s2->v.Expr.value->kind == Str_kind)
1938 continue;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001939 VISIT_IN_SCOPE(c, stmt, s2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001940 }
1941 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001942 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001943 if (co == NULL)
1944 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001945
1946 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00001947 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001948
1949 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1950 ADDOP_I(c, CALL_FUNCTION, 1);
1951 }
1952
1953 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1954}
1955
1956static int
1957compiler_class(struct compiler *c, stmt_ty s)
1958{
1959 int n;
1960 PyCodeObject *co;
1961 PyObject *str;
1962 /* push class name on stack, needed by BUILD_CLASS */
1963 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1964 /* push the tuple of base classes on the stack */
1965 n = asdl_seq_LEN(s->v.ClassDef.bases);
1966 if (n > 0)
1967 VISIT_SEQ(c, expr, s->v.ClassDef.bases);
1968 ADDOP_I(c, BUILD_TUPLE, n);
1969 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s,
1970 s->lineno))
1971 return 0;
1972 c->u->u_private = s->v.ClassDef.name;
1973 Py_INCREF(c->u->u_private);
1974 str = PyString_InternFromString("__name__");
1975 if (!str || !compiler_nameop(c, str, Load)) {
1976 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001977 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001978 return 0;
1979 }
1980
1981 Py_DECREF(str);
1982 str = PyString_InternFromString("__module__");
1983 if (!str || !compiler_nameop(c, str, Store)) {
1984 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001985 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986 return 0;
1987 }
1988 Py_DECREF(str);
1989
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001990 if (!compiler_body(c, s->v.ClassDef.body)) {
1991 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001993 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001994
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001995 ADDOP_IN_SCOPE(c, LOAD_LOCALS);
1996 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001997 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001998 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999 if (co == NULL)
2000 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002001
2002 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00002003 Py_DECREF(co);
2004
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005 ADDOP_I(c, CALL_FUNCTION, 0);
2006 ADDOP(c, BUILD_CLASS);
2007 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2008 return 0;
2009 return 1;
2010}
2011
2012static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002013compiler_ifexp(struct compiler *c, expr_ty e)
2014{
2015 basicblock *end, *next;
2016
2017 assert(e->kind == IfExp_kind);
2018 end = compiler_new_block(c);
2019 if (end == NULL)
2020 return 0;
2021 next = compiler_new_block(c);
2022 if (next == NULL)
2023 return 0;
2024 VISIT(c, expr, e->v.IfExp.test);
2025 ADDOP_JREL(c, JUMP_IF_FALSE, next);
2026 ADDOP(c, POP_TOP);
2027 VISIT(c, expr, e->v.IfExp.body);
2028 ADDOP_JREL(c, JUMP_FORWARD, end);
2029 compiler_use_next_block(c, next);
2030 ADDOP(c, POP_TOP);
2031 VISIT(c, expr, e->v.IfExp.orelse);
2032 compiler_use_next_block(c, end);
2033 return 1;
2034}
2035
2036static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002037compiler_lambda(struct compiler *c, expr_ty e)
2038{
2039 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002040 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002041 arguments_ty args = e->v.Lambda.args;
2042 assert(e->kind == Lambda_kind);
2043
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002044 if (!name) {
2045 name = PyString_InternFromString("<lambda>");
2046 if (!name)
2047 return 0;
2048 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002049
2050 if (args->defaults)
2051 VISIT_SEQ(c, expr, args->defaults);
2052 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2053 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002054
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002055 /* unpack nested arguments */
2056 compiler_arguments(c, args);
2057
2058 c->u->u_argcount = asdl_seq_LEN(args->args);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00002059 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2060 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00002062 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002063 if (co == NULL)
2064 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002065
2066 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00002067 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002068
2069 return 1;
2070}
2071
2072static int
2073compiler_print(struct compiler *c, stmt_ty s)
2074{
2075 int i, n;
2076 bool dest;
2077
2078 assert(s->kind == Print_kind);
2079 n = asdl_seq_LEN(s->v.Print.values);
2080 dest = false;
2081 if (s->v.Print.dest) {
2082 VISIT(c, expr, s->v.Print.dest);
2083 dest = true;
2084 }
2085 for (i = 0; i < n; i++) {
2086 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i);
2087 if (dest) {
2088 ADDOP(c, DUP_TOP);
2089 VISIT(c, expr, e);
2090 ADDOP(c, ROT_TWO);
2091 ADDOP(c, PRINT_ITEM_TO);
2092 }
2093 else {
2094 VISIT(c, expr, e);
2095 ADDOP(c, PRINT_ITEM);
2096 }
2097 }
2098 if (s->v.Print.nl) {
2099 if (dest)
2100 ADDOP(c, PRINT_NEWLINE_TO)
2101 else
2102 ADDOP(c, PRINT_NEWLINE)
2103 }
2104 else if (dest)
2105 ADDOP(c, POP_TOP);
2106 return 1;
2107}
2108
2109static int
2110compiler_if(struct compiler *c, stmt_ty s)
2111{
2112 basicblock *end, *next;
2113
2114 assert(s->kind == If_kind);
2115 end = compiler_new_block(c);
2116 if (end == NULL)
2117 return 0;
2118 next = compiler_new_block(c);
2119 if (next == NULL)
2120 return 0;
2121 VISIT(c, expr, s->v.If.test);
2122 ADDOP_JREL(c, JUMP_IF_FALSE, next);
2123 ADDOP(c, POP_TOP);
2124 VISIT_SEQ(c, stmt, s->v.If.body);
2125 ADDOP_JREL(c, JUMP_FORWARD, end);
2126 compiler_use_next_block(c, next);
2127 ADDOP(c, POP_TOP);
2128 if (s->v.If.orelse)
2129 VISIT_SEQ(c, stmt, s->v.If.orelse);
2130 compiler_use_next_block(c, end);
2131 return 1;
2132}
2133
2134static int
2135compiler_for(struct compiler *c, stmt_ty s)
2136{
2137 basicblock *start, *cleanup, *end;
2138
2139 start = compiler_new_block(c);
2140 cleanup = compiler_new_block(c);
2141 end = compiler_new_block(c);
2142 if (start == NULL || end == NULL || cleanup == NULL)
2143 return 0;
2144 ADDOP_JREL(c, SETUP_LOOP, end);
2145 if (!compiler_push_fblock(c, LOOP, start))
2146 return 0;
2147 VISIT(c, expr, s->v.For.iter);
2148 ADDOP(c, GET_ITER);
2149 compiler_use_next_block(c, start);
2150 ADDOP_JREL(c, FOR_ITER, cleanup);
2151 VISIT(c, expr, s->v.For.target);
2152 VISIT_SEQ(c, stmt, s->v.For.body);
2153 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2154 compiler_use_next_block(c, cleanup);
2155 ADDOP(c, POP_BLOCK);
2156 compiler_pop_fblock(c, LOOP, start);
2157 VISIT_SEQ(c, stmt, s->v.For.orelse);
2158 compiler_use_next_block(c, end);
2159 return 1;
2160}
2161
2162static int
2163compiler_while(struct compiler *c, stmt_ty s)
2164{
2165 basicblock *loop, *orelse, *end, *anchor = NULL;
2166 int constant = expr_constant(s->v.While.test);
2167
2168 if (constant == 0)
2169 return 1;
2170 loop = compiler_new_block(c);
2171 end = compiler_new_block(c);
2172 if (constant == -1) {
2173 anchor = compiler_new_block(c);
2174 if (anchor == NULL)
2175 return 0;
2176 }
2177 if (loop == NULL || end == NULL)
2178 return 0;
2179 if (s->v.While.orelse) {
2180 orelse = compiler_new_block(c);
2181 if (orelse == NULL)
2182 return 0;
2183 }
2184 else
2185 orelse = NULL;
2186
2187 ADDOP_JREL(c, SETUP_LOOP, end);
2188 compiler_use_next_block(c, loop);
2189 if (!compiler_push_fblock(c, LOOP, loop))
2190 return 0;
2191 if (constant == -1) {
2192 VISIT(c, expr, s->v.While.test);
2193 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
2194 ADDOP(c, POP_TOP);
2195 }
2196 VISIT_SEQ(c, stmt, s->v.While.body);
2197 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
2198
2199 /* XXX should the two POP instructions be in a separate block
2200 if there is no else clause ?
2201 */
2202
2203 if (constant == -1) {
2204 compiler_use_next_block(c, anchor);
2205 ADDOP(c, POP_TOP);
2206 ADDOP(c, POP_BLOCK);
2207 }
2208 compiler_pop_fblock(c, LOOP, loop);
2209 if (orelse != NULL)
2210 VISIT_SEQ(c, stmt, s->v.While.orelse);
2211 compiler_use_next_block(c, end);
2212
2213 return 1;
2214}
2215
2216static int
2217compiler_continue(struct compiler *c)
2218{
2219 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
2220 int i;
2221
2222 if (!c->u->u_nfblocks)
2223 return compiler_error(c, LOOP_ERROR_MSG);
2224 i = c->u->u_nfblocks - 1;
2225 switch (c->u->u_fblock[i].fb_type) {
2226 case LOOP:
2227 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
2228 break;
2229 case EXCEPT:
2230 case FINALLY_TRY:
2231 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP)
2232 ;
2233 if (i == -1)
2234 return compiler_error(c, LOOP_ERROR_MSG);
2235 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
2236 break;
2237 case FINALLY_END:
2238 return compiler_error(c,
2239 "'continue' not supported inside 'finally' clause");
2240 }
2241
2242 return 1;
2243}
2244
2245/* Code generated for "try: <body> finally: <finalbody>" is as follows:
2246
2247 SETUP_FINALLY L
2248 <code for body>
2249 POP_BLOCK
2250 LOAD_CONST <None>
2251 L: <code for finalbody>
2252 END_FINALLY
2253
2254 The special instructions use the block stack. Each block
2255 stack entry contains the instruction that created it (here
2256 SETUP_FINALLY), the level of the value stack at the time the
2257 block stack entry was created, and a label (here L).
2258
2259 SETUP_FINALLY:
2260 Pushes the current value stack level and the label
2261 onto the block stack.
2262 POP_BLOCK:
2263 Pops en entry from the block stack, and pops the value
2264 stack until its level is the same as indicated on the
2265 block stack. (The label is ignored.)
2266 END_FINALLY:
2267 Pops a variable number of entries from the *value* stack
2268 and re-raises the exception they specify. The number of
2269 entries popped depends on the (pseudo) exception type.
2270
2271 The block stack is unwound when an exception is raised:
2272 when a SETUP_FINALLY entry is found, the exception is pushed
2273 onto the value stack (and the exception condition is cleared),
2274 and the interpreter jumps to the label gotten from the block
2275 stack.
2276*/
2277
2278static int
2279compiler_try_finally(struct compiler *c, stmt_ty s)
2280{
2281 basicblock *body, *end;
2282 body = compiler_new_block(c);
2283 end = compiler_new_block(c);
2284 if (body == NULL || end == NULL)
2285 return 0;
2286
2287 ADDOP_JREL(c, SETUP_FINALLY, end);
2288 compiler_use_next_block(c, body);
2289 if (!compiler_push_fblock(c, FINALLY_TRY, body))
2290 return 0;
2291 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
2292 ADDOP(c, POP_BLOCK);
2293 compiler_pop_fblock(c, FINALLY_TRY, body);
2294
2295 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2296 compiler_use_next_block(c, end);
2297 if (!compiler_push_fblock(c, FINALLY_END, end))
2298 return 0;
2299 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
2300 ADDOP(c, END_FINALLY);
2301 compiler_pop_fblock(c, FINALLY_END, end);
2302
2303 return 1;
2304}
2305
2306/*
2307 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
2308 (The contents of the value stack is shown in [], with the top
2309 at the right; 'tb' is trace-back info, 'val' the exception's
2310 associated value, and 'exc' the exception.)
2311
2312 Value stack Label Instruction Argument
2313 [] SETUP_EXCEPT L1
2314 [] <code for S>
2315 [] POP_BLOCK
2316 [] JUMP_FORWARD L0
2317
2318 [tb, val, exc] L1: DUP )
2319 [tb, val, exc, exc] <evaluate E1> )
2320 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2321 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
2322 [tb, val, exc, 1] POP )
2323 [tb, val, exc] POP
2324 [tb, val] <assign to V1> (or POP if no V1)
2325 [tb] POP
2326 [] <code for S1>
2327 JUMP_FORWARD L0
2328
2329 [tb, val, exc, 0] L2: POP
2330 [tb, val, exc] DUP
2331 .............................etc.......................
2332
2333 [tb, val, exc, 0] Ln+1: POP
2334 [tb, val, exc] END_FINALLY # re-raise exception
2335
2336 [] L0: <next statement>
2337
2338 Of course, parts are not generated if Vi or Ei is not present.
2339*/
2340static int
2341compiler_try_except(struct compiler *c, stmt_ty s)
2342{
2343 basicblock *body, *orelse, *except, *end;
2344 int i, n;
2345
2346 body = compiler_new_block(c);
2347 except = compiler_new_block(c);
2348 orelse = compiler_new_block(c);
2349 end = compiler_new_block(c);
2350 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
2351 return 0;
2352 ADDOP_JREL(c, SETUP_EXCEPT, except);
2353 compiler_use_next_block(c, body);
2354 if (!compiler_push_fblock(c, EXCEPT, body))
2355 return 0;
2356 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
2357 ADDOP(c, POP_BLOCK);
2358 compiler_pop_fblock(c, EXCEPT, body);
2359 ADDOP_JREL(c, JUMP_FORWARD, orelse);
2360 n = asdl_seq_LEN(s->v.TryExcept.handlers);
2361 compiler_use_next_block(c, except);
2362 for (i = 0; i < n; i++) {
2363 excepthandler_ty handler = asdl_seq_GET(
2364 s->v.TryExcept.handlers, i);
2365 if (!handler->type && i < n-1)
2366 return compiler_error(c, "default 'except:' must be last");
2367 except = compiler_new_block(c);
2368 if (except == NULL)
2369 return 0;
2370 if (handler->type) {
2371 ADDOP(c, DUP_TOP);
2372 VISIT(c, expr, handler->type);
2373 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2374 ADDOP_JREL(c, JUMP_IF_FALSE, except);
2375 ADDOP(c, POP_TOP);
2376 }
2377 ADDOP(c, POP_TOP);
2378 if (handler->name) {
2379 VISIT(c, expr, handler->name);
2380 }
2381 else {
2382 ADDOP(c, POP_TOP);
2383 }
2384 ADDOP(c, POP_TOP);
2385 VISIT_SEQ(c, stmt, handler->body);
2386 ADDOP_JREL(c, JUMP_FORWARD, end);
2387 compiler_use_next_block(c, except);
2388 if (handler->type)
2389 ADDOP(c, POP_TOP);
2390 }
2391 ADDOP(c, END_FINALLY);
2392 compiler_use_next_block(c, orelse);
2393 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2394 compiler_use_next_block(c, end);
2395 return 1;
2396}
2397
2398static int
2399compiler_import_as(struct compiler *c, identifier name, identifier asname)
2400{
2401 /* The IMPORT_NAME opcode was already generated. This function
2402 merely needs to bind the result to a name.
2403
2404 If there is a dot in name, we need to split it and emit a
2405 LOAD_ATTR for each name.
2406 */
2407 const char *src = PyString_AS_STRING(name);
2408 const char *dot = strchr(src, '.');
2409 if (dot) {
2410 /* Consume the base module name to get the first attribute */
2411 src = dot + 1;
2412 while (dot) {
2413 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00002414 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002415 dot = strchr(src, '.');
Armin Rigo31441302005-10-21 12:57:31 +00002416 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002417 dot ? dot - src : strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002418 if (!attr)
2419 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002420 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002421 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002422 src = dot + 1;
2423 }
2424 }
2425 return compiler_nameop(c, asname, Store);
2426}
2427
2428static int
2429compiler_import(struct compiler *c, stmt_ty s)
2430{
2431 /* The Import node stores a module name like a.b.c as a single
2432 string. This is convenient for all cases except
2433 import a.b.c as d
2434 where we need to parse that string to extract the individual
2435 module names.
2436 XXX Perhaps change the representation to make this case simpler?
2437 */
2438 int i, n = asdl_seq_LEN(s->v.Import.names);
2439 for (i = 0; i < n; i++) {
2440 alias_ty alias = asdl_seq_GET(s->v.Import.names, i);
2441 int r;
2442
2443 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2444 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2445
2446 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002447 r = compiler_import_as(c, alias->name, alias->asname);
2448 if (!r)
2449 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002450 }
2451 else {
2452 identifier tmp = alias->name;
2453 const char *base = PyString_AS_STRING(alias->name);
2454 char *dot = strchr(base, '.');
2455 if (dot)
2456 tmp = PyString_FromStringAndSize(base,
2457 dot - base);
2458 r = compiler_nameop(c, tmp, Store);
2459 if (dot) {
2460 Py_DECREF(tmp);
2461 }
2462 if (!r)
2463 return r;
2464 }
2465 }
2466 return 1;
2467}
2468
2469static int
2470compiler_from_import(struct compiler *c, stmt_ty s)
2471{
2472 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002473
2474 PyObject *names = PyTuple_New(n);
2475 if (!names)
2476 return 0;
2477
2478 /* build up the names */
2479 for (i = 0; i < n; i++) {
2480 alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
2481 Py_INCREF(alias->name);
2482 PyTuple_SET_ITEM(names, i, alias->name);
2483 }
2484
2485 if (s->lineno > c->c_future->ff_lineno) {
2486 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
2487 "__future__")) {
2488 Py_DECREF(names);
2489 return compiler_error(c,
2490 "from __future__ imports must occur "
2491 "at the beginning of the file");
2492
2493 }
2494 }
2495
2496 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002497 Py_DECREF(names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002498 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2499 for (i = 0; i < n; i++) {
2500 alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
2501 identifier store_name;
2502
2503 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
2504 assert(n == 1);
2505 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002506 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002507 }
2508
2509 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2510 store_name = alias->name;
2511 if (alias->asname)
2512 store_name = alias->asname;
2513
2514 if (!compiler_nameop(c, store_name, Store)) {
2515 Py_DECREF(names);
2516 return 0;
2517 }
2518 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002519 /* remove imported module */
2520 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521 return 1;
2522}
2523
2524static int
2525compiler_assert(struct compiler *c, stmt_ty s)
2526{
2527 static PyObject *assertion_error = NULL;
2528 basicblock *end;
2529
2530 if (Py_OptimizeFlag)
2531 return 1;
2532 if (assertion_error == NULL) {
2533 assertion_error = PyString_FromString("AssertionError");
2534 if (assertion_error == NULL)
2535 return 0;
2536 }
2537 VISIT(c, expr, s->v.Assert.test);
2538 end = compiler_new_block(c);
2539 if (end == NULL)
2540 return 0;
2541 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2542 ADDOP(c, POP_TOP);
2543 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2544 if (s->v.Assert.msg) {
2545 VISIT(c, expr, s->v.Assert.msg);
2546 ADDOP_I(c, RAISE_VARARGS, 2);
2547 }
2548 else {
2549 ADDOP_I(c, RAISE_VARARGS, 1);
2550 }
Neal Norwitz51abbc72005-12-18 07:06:23 +00002551 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002552 ADDOP(c, POP_TOP);
2553 return 1;
2554}
2555
2556static int
2557compiler_visit_stmt(struct compiler *c, stmt_ty s)
2558{
2559 int i, n;
2560
2561 c->u->u_lineno = s->lineno;
2562 c->u->u_lineno_set = false;
2563 switch (s->kind) {
2564 case FunctionDef_kind:
2565 return compiler_function(c, s);
2566 case ClassDef_kind:
2567 return compiler_class(c, s);
2568 case Return_kind:
2569 if (c->u->u_ste->ste_type != FunctionBlock)
2570 return compiler_error(c, "'return' outside function");
2571 if (s->v.Return.value) {
2572 if (c->u->u_ste->ste_generator) {
2573 return compiler_error(c,
2574 "'return' with argument inside generator");
2575 }
2576 VISIT(c, expr, s->v.Return.value);
2577 }
2578 else
2579 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2580 ADDOP(c, RETURN_VALUE);
2581 break;
2582 case Delete_kind:
2583 VISIT_SEQ(c, expr, s->v.Delete.targets)
2584 break;
2585 case Assign_kind:
2586 n = asdl_seq_LEN(s->v.Assign.targets);
2587 VISIT(c, expr, s->v.Assign.value);
2588 for (i = 0; i < n; i++) {
2589 if (i < n - 1)
2590 ADDOP(c, DUP_TOP);
2591 VISIT(c, expr,
2592 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2593 }
2594 break;
2595 case AugAssign_kind:
2596 return compiler_augassign(c, s);
2597 case Print_kind:
2598 return compiler_print(c, s);
2599 case For_kind:
2600 return compiler_for(c, s);
2601 case While_kind:
2602 return compiler_while(c, s);
2603 case If_kind:
2604 return compiler_if(c, s);
2605 case Raise_kind:
2606 n = 0;
2607 if (s->v.Raise.type) {
2608 VISIT(c, expr, s->v.Raise.type);
2609 n++;
2610 if (s->v.Raise.inst) {
2611 VISIT(c, expr, s->v.Raise.inst);
2612 n++;
2613 if (s->v.Raise.tback) {
2614 VISIT(c, expr, s->v.Raise.tback);
2615 n++;
2616 }
2617 }
2618 }
2619 ADDOP_I(c, RAISE_VARARGS, n);
2620 break;
2621 case TryExcept_kind:
2622 return compiler_try_except(c, s);
2623 case TryFinally_kind:
2624 return compiler_try_finally(c, s);
2625 case Assert_kind:
2626 return compiler_assert(c, s);
2627 case Import_kind:
2628 return compiler_import(c, s);
2629 case ImportFrom_kind:
2630 return compiler_from_import(c, s);
2631 case Exec_kind:
2632 VISIT(c, expr, s->v.Exec.body);
2633 if (s->v.Exec.globals) {
2634 VISIT(c, expr, s->v.Exec.globals);
2635 if (s->v.Exec.locals) {
2636 VISIT(c, expr, s->v.Exec.locals);
2637 } else {
2638 ADDOP(c, DUP_TOP);
2639 }
2640 } else {
2641 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2642 ADDOP(c, DUP_TOP);
2643 }
2644 ADDOP(c, EXEC_STMT);
2645 break;
2646 case Global_kind:
2647 break;
2648 case Expr_kind:
2649 VISIT(c, expr, s->v.Expr.value);
2650 if (c->c_interactive && c->c_nestlevel <= 1) {
2651 ADDOP(c, PRINT_EXPR);
2652 }
2653 else {
2654 ADDOP(c, POP_TOP);
2655 }
2656 break;
2657 case Pass_kind:
2658 break;
2659 case Break_kind:
2660 if (!c->u->u_nfblocks)
2661 return compiler_error(c, "'break' outside loop");
2662 ADDOP(c, BREAK_LOOP);
2663 break;
2664 case Continue_kind:
2665 return compiler_continue(c);
2666 }
2667 return 1;
2668}
2669
2670static int
2671unaryop(unaryop_ty op)
2672{
2673 switch (op) {
2674 case Invert:
2675 return UNARY_INVERT;
2676 case Not:
2677 return UNARY_NOT;
2678 case UAdd:
2679 return UNARY_POSITIVE;
2680 case USub:
2681 return UNARY_NEGATIVE;
2682 }
2683 return 0;
2684}
2685
2686static int
2687binop(struct compiler *c, operator_ty op)
2688{
2689 switch (op) {
2690 case Add:
2691 return BINARY_ADD;
2692 case Sub:
2693 return BINARY_SUBTRACT;
2694 case Mult:
2695 return BINARY_MULTIPLY;
2696 case Div:
2697 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2698 return BINARY_TRUE_DIVIDE;
2699 else
2700 return BINARY_DIVIDE;
2701 case Mod:
2702 return BINARY_MODULO;
2703 case Pow:
2704 return BINARY_POWER;
2705 case LShift:
2706 return BINARY_LSHIFT;
2707 case RShift:
2708 return BINARY_RSHIFT;
2709 case BitOr:
2710 return BINARY_OR;
2711 case BitXor:
2712 return BINARY_XOR;
2713 case BitAnd:
2714 return BINARY_AND;
2715 case FloorDiv:
2716 return BINARY_FLOOR_DIVIDE;
2717 }
2718 return 0;
2719}
2720
2721static int
2722cmpop(cmpop_ty op)
2723{
2724 switch (op) {
2725 case Eq:
2726 return PyCmp_EQ;
2727 case NotEq:
2728 return PyCmp_NE;
2729 case Lt:
2730 return PyCmp_LT;
2731 case LtE:
2732 return PyCmp_LE;
2733 case Gt:
2734 return PyCmp_GT;
2735 case GtE:
2736 return PyCmp_GE;
2737 case Is:
2738 return PyCmp_IS;
2739 case IsNot:
2740 return PyCmp_IS_NOT;
2741 case In:
2742 return PyCmp_IN;
2743 case NotIn:
2744 return PyCmp_NOT_IN;
2745 }
2746 return PyCmp_BAD;
2747}
2748
2749static int
2750inplace_binop(struct compiler *c, operator_ty op)
2751{
2752 switch (op) {
2753 case Add:
2754 return INPLACE_ADD;
2755 case Sub:
2756 return INPLACE_SUBTRACT;
2757 case Mult:
2758 return INPLACE_MULTIPLY;
2759 case Div:
2760 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2761 return INPLACE_TRUE_DIVIDE;
2762 else
2763 return INPLACE_DIVIDE;
2764 case Mod:
2765 return INPLACE_MODULO;
2766 case Pow:
2767 return INPLACE_POWER;
2768 case LShift:
2769 return INPLACE_LSHIFT;
2770 case RShift:
2771 return INPLACE_RSHIFT;
2772 case BitOr:
2773 return INPLACE_OR;
2774 case BitXor:
2775 return INPLACE_XOR;
2776 case BitAnd:
2777 return INPLACE_AND;
2778 case FloorDiv:
2779 return INPLACE_FLOOR_DIVIDE;
2780 }
Neal Norwitz4737b232005-11-19 23:58:29 +00002781 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002782 "inplace binary op %d should not be possible", op);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002783 return 0;
2784}
2785
2786static int
2787compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2788{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002789 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002790 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2791
2792 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002793 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002794 /* XXX AugStore isn't used anywhere! */
2795
2796 /* First check for assignment to __debug__. Param? */
2797 if ((ctx == Store || ctx == AugStore || ctx == Del)
2798 && !strcmp(PyString_AS_STRING(name), "__debug__")) {
2799 return compiler_error(c, "can not assign to __debug__");
2800 }
2801
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002802 mangled = _Py_Mangle(c->u->u_private, name);
2803 if (!mangled)
2804 return 0;
2805
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002806 op = 0;
2807 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002808 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002809 switch (scope) {
2810 case FREE:
2811 dict = c->u->u_freevars;
2812 optype = OP_DEREF;
2813 break;
2814 case CELL:
2815 dict = c->u->u_cellvars;
2816 optype = OP_DEREF;
2817 break;
2818 case LOCAL:
2819 if (c->u->u_ste->ste_type == FunctionBlock)
2820 optype = OP_FAST;
2821 break;
2822 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002823 if (c->u->u_ste->ste_type == FunctionBlock &&
2824 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825 optype = OP_GLOBAL;
2826 break;
2827 case GLOBAL_EXPLICIT:
2828 optype = OP_GLOBAL;
2829 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002830 default:
2831 /* scope can be 0 */
2832 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002833 }
2834
2835 /* XXX Leave assert here, but handle __doc__ and the like better */
2836 assert(scope || PyString_AS_STRING(name)[0] == '_');
2837
2838 switch (optype) {
2839 case OP_DEREF:
2840 switch (ctx) {
2841 case Load: op = LOAD_DEREF; break;
2842 case Store: op = STORE_DEREF; break;
2843 case AugLoad:
2844 case AugStore:
2845 break;
2846 case Del:
2847 PyErr_Format(PyExc_SyntaxError,
2848 "can not delete variable '%s' referenced "
2849 "in nested scope",
2850 PyString_AS_STRING(name));
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002851 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002852 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002854 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002855 PyErr_SetString(PyExc_SystemError,
2856 "param invalid for deref variable");
2857 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002858 }
2859 break;
2860 case OP_FAST:
2861 switch (ctx) {
2862 case Load: op = LOAD_FAST; break;
2863 case Store: op = STORE_FAST; break;
2864 case Del: op = DELETE_FAST; break;
2865 case AugLoad:
2866 case AugStore:
2867 break;
2868 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002869 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002870 PyErr_SetString(PyExc_SystemError,
2871 "param invalid for local variable");
2872 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002873 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002874 ADDOP_O(c, op, mangled, varnames);
2875 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002876 return 1;
2877 case OP_GLOBAL:
2878 switch (ctx) {
2879 case Load: op = LOAD_GLOBAL; break;
2880 case Store: op = STORE_GLOBAL; break;
2881 case Del: op = DELETE_GLOBAL; break;
2882 case AugLoad:
2883 case AugStore:
2884 break;
2885 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002886 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002887 PyErr_SetString(PyExc_SystemError,
2888 "param invalid for global variable");
2889 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002890 }
2891 break;
2892 case OP_NAME:
2893 switch (ctx) {
2894 case Load: op = LOAD_NAME; break;
2895 case Store: op = STORE_NAME; break;
2896 case Del: op = DELETE_NAME; break;
2897 case AugLoad:
2898 case AugStore:
2899 break;
2900 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002901 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002902 PyErr_SetString(PyExc_SystemError,
2903 "param invalid for name variable");
2904 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002905 }
2906 break;
2907 }
2908
2909 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002910 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002911 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002912 if (arg < 0)
2913 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002914 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002915}
2916
2917static int
2918compiler_boolop(struct compiler *c, expr_ty e)
2919{
2920 basicblock *end;
2921 int jumpi, i, n;
2922 asdl_seq *s;
2923
2924 assert(e->kind == BoolOp_kind);
2925 if (e->v.BoolOp.op == And)
2926 jumpi = JUMP_IF_FALSE;
2927 else
2928 jumpi = JUMP_IF_TRUE;
2929 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002930 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002931 return 0;
2932 s = e->v.BoolOp.values;
2933 n = asdl_seq_LEN(s) - 1;
2934 for (i = 0; i < n; ++i) {
2935 VISIT(c, expr, asdl_seq_GET(s, i));
2936 ADDOP_JREL(c, jumpi, end);
2937 ADDOP(c, POP_TOP)
2938 }
2939 VISIT(c, expr, asdl_seq_GET(s, n));
2940 compiler_use_next_block(c, end);
2941 return 1;
2942}
2943
2944static int
2945compiler_list(struct compiler *c, expr_ty e)
2946{
2947 int n = asdl_seq_LEN(e->v.List.elts);
2948 if (e->v.List.ctx == Store) {
2949 ADDOP_I(c, UNPACK_SEQUENCE, n);
2950 }
2951 VISIT_SEQ(c, expr, e->v.List.elts);
2952 if (e->v.List.ctx == Load) {
2953 ADDOP_I(c, BUILD_LIST, n);
2954 }
2955 return 1;
2956}
2957
2958static int
2959compiler_tuple(struct compiler *c, expr_ty e)
2960{
2961 int n = asdl_seq_LEN(e->v.Tuple.elts);
2962 if (e->v.Tuple.ctx == Store) {
2963 ADDOP_I(c, UNPACK_SEQUENCE, n);
2964 }
2965 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2966 if (e->v.Tuple.ctx == Load) {
2967 ADDOP_I(c, BUILD_TUPLE, n);
2968 }
2969 return 1;
2970}
2971
2972static int
2973compiler_compare(struct compiler *c, expr_ty e)
2974{
2975 int i, n;
2976 basicblock *cleanup = NULL;
2977
2978 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2979 VISIT(c, expr, e->v.Compare.left);
2980 n = asdl_seq_LEN(e->v.Compare.ops);
2981 assert(n > 0);
2982 if (n > 1) {
2983 cleanup = compiler_new_block(c);
2984 if (cleanup == NULL)
2985 return 0;
2986 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, 0));
2987 }
2988 for (i = 1; i < n; i++) {
2989 ADDOP(c, DUP_TOP);
2990 ADDOP(c, ROT_THREE);
2991 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
2992 ADDOP_I(c, COMPARE_OP,
2993 cmpop((cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i - 1)));
2994 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2995 NEXT_BLOCK(c);
2996 ADDOP(c, POP_TOP);
2997 if (i < (n - 1))
2998 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, i));
2999 }
3000 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, n - 1));
3001 ADDOP_I(c, COMPARE_OP,
3002 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
3003 cmpop((cmpop_ty)asdl_seq_GET(e->v.Compare.ops, n - 1)));
3004 if (n > 1) {
3005 basicblock *end = compiler_new_block(c);
3006 if (end == NULL)
3007 return 0;
3008 ADDOP_JREL(c, JUMP_FORWARD, end);
3009 compiler_use_next_block(c, cleanup);
3010 ADDOP(c, ROT_TWO);
3011 ADDOP(c, POP_TOP);
3012 compiler_use_next_block(c, end);
3013 }
3014 return 1;
3015}
3016
3017static int
3018compiler_call(struct compiler *c, expr_ty e)
3019{
3020 int n, code = 0;
3021
3022 VISIT(c, expr, e->v.Call.func);
3023 n = asdl_seq_LEN(e->v.Call.args);
3024 VISIT_SEQ(c, expr, e->v.Call.args);
3025 if (e->v.Call.keywords) {
3026 VISIT_SEQ(c, keyword, e->v.Call.keywords);
3027 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
3028 }
3029 if (e->v.Call.starargs) {
3030 VISIT(c, expr, e->v.Call.starargs);
3031 code |= 1;
3032 }
3033 if (e->v.Call.kwargs) {
3034 VISIT(c, expr, e->v.Call.kwargs);
3035 code |= 2;
3036 }
3037 switch (code) {
3038 case 0:
3039 ADDOP_I(c, CALL_FUNCTION, n);
3040 break;
3041 case 1:
3042 ADDOP_I(c, CALL_FUNCTION_VAR, n);
3043 break;
3044 case 2:
3045 ADDOP_I(c, CALL_FUNCTION_KW, n);
3046 break;
3047 case 3:
3048 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
3049 break;
3050 }
3051 return 1;
3052}
3053
3054static int
3055compiler_listcomp_generator(struct compiler *c, PyObject *tmpname,
3056 asdl_seq *generators, int gen_index,
3057 expr_ty elt)
3058{
3059 /* generate code for the iterator, then each of the ifs,
3060 and then write to the element */
3061
3062 comprehension_ty l;
3063 basicblock *start, *anchor, *skip, *if_cleanup;
3064 int i, n;
3065
3066 start = compiler_new_block(c);
3067 skip = compiler_new_block(c);
3068 if_cleanup = compiler_new_block(c);
3069 anchor = compiler_new_block(c);
3070
3071 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3072 anchor == NULL)
3073 return 0;
3074
3075 l = asdl_seq_GET(generators, gen_index);
3076 VISIT(c, expr, l->iter);
3077 ADDOP(c, GET_ITER);
3078 compiler_use_next_block(c, start);
3079 ADDOP_JREL(c, FOR_ITER, anchor);
3080 NEXT_BLOCK(c);
3081 VISIT(c, expr, l->target);
3082
3083 /* XXX this needs to be cleaned up...a lot! */
3084 n = asdl_seq_LEN(l->ifs);
3085 for (i = 0; i < n; i++) {
3086 expr_ty e = asdl_seq_GET(l->ifs, i);
3087 VISIT(c, expr, e);
3088 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
3089 NEXT_BLOCK(c);
3090 ADDOP(c, POP_TOP);
3091 }
3092
3093 if (++gen_index < asdl_seq_LEN(generators))
3094 if (!compiler_listcomp_generator(c, tmpname,
3095 generators, gen_index, elt))
3096 return 0;
3097
3098 /* only append after the last for generator */
3099 if (gen_index >= asdl_seq_LEN(generators)) {
3100 if (!compiler_nameop(c, tmpname, Load))
3101 return 0;
3102 VISIT(c, expr, elt);
3103 ADDOP_I(c, CALL_FUNCTION, 1);
3104 ADDOP(c, POP_TOP);
3105
3106 compiler_use_next_block(c, skip);
3107 }
3108 for (i = 0; i < n; i++) {
3109 ADDOP_I(c, JUMP_FORWARD, 1);
3110 if (i == 0)
3111 compiler_use_next_block(c, if_cleanup);
3112 ADDOP(c, POP_TOP);
3113 }
3114 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3115 compiler_use_next_block(c, anchor);
3116 /* delete the append method added to locals */
3117 if (gen_index == 1)
3118 if (!compiler_nameop(c, tmpname, Del))
3119 return 0;
3120
3121 return 1;
3122}
3123
3124static int
3125compiler_listcomp(struct compiler *c, expr_ty e)
3126{
3127 char tmpname[256];
3128 identifier tmp;
3129 int rc = 0;
3130 static identifier append;
3131 asdl_seq *generators = e->v.ListComp.generators;
3132
3133 assert(e->kind == ListComp_kind);
3134 if (!append) {
3135 append = PyString_InternFromString("append");
3136 if (!append)
3137 return 0;
3138 }
3139 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
3140 tmp = PyString_FromString(tmpname);
3141 if (!tmp)
3142 return 0;
3143 ADDOP_I(c, BUILD_LIST, 0);
3144 ADDOP(c, DUP_TOP);
3145 ADDOP_O(c, LOAD_ATTR, append, names);
3146 if (compiler_nameop(c, tmp, Store))
3147 rc = compiler_listcomp_generator(c, tmp, generators, 0,
3148 e->v.ListComp.elt);
3149 Py_DECREF(tmp);
3150 return rc;
3151}
3152
3153static int
3154compiler_genexp_generator(struct compiler *c,
3155 asdl_seq *generators, int gen_index,
3156 expr_ty elt)
3157{
3158 /* generate code for the iterator, then each of the ifs,
3159 and then write to the element */
3160
3161 comprehension_ty ge;
3162 basicblock *start, *anchor, *skip, *if_cleanup, *end;
3163 int i, n;
3164
3165 start = compiler_new_block(c);
3166 skip = compiler_new_block(c);
3167 if_cleanup = compiler_new_block(c);
3168 anchor = compiler_new_block(c);
3169 end = compiler_new_block(c);
3170
3171 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3172 anchor == NULL || end == NULL)
3173 return 0;
3174
3175 ge = asdl_seq_GET(generators, gen_index);
3176 ADDOP_JREL(c, SETUP_LOOP, end);
3177 if (!compiler_push_fblock(c, LOOP, start))
3178 return 0;
3179
3180 if (gen_index == 0) {
3181 /* Receive outermost iter as an implicit argument */
3182 c->u->u_argcount = 1;
3183 ADDOP_I(c, LOAD_FAST, 0);
3184 }
3185 else {
3186 /* Sub-iter - calculate on the fly */
3187 VISIT(c, expr, ge->iter);
3188 ADDOP(c, GET_ITER);
3189 }
3190 compiler_use_next_block(c, start);
3191 ADDOP_JREL(c, FOR_ITER, anchor);
3192 NEXT_BLOCK(c);
3193 VISIT(c, expr, ge->target);
3194
3195 /* XXX this needs to be cleaned up...a lot! */
3196 n = asdl_seq_LEN(ge->ifs);
3197 for (i = 0; i < n; i++) {
3198 expr_ty e = asdl_seq_GET(ge->ifs, i);
3199 VISIT(c, expr, e);
3200 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
3201 NEXT_BLOCK(c);
3202 ADDOP(c, POP_TOP);
3203 }
3204
3205 if (++gen_index < asdl_seq_LEN(generators))
3206 if (!compiler_genexp_generator(c, generators, gen_index, elt))
3207 return 0;
3208
3209 /* only append after the last 'for' generator */
3210 if (gen_index >= asdl_seq_LEN(generators)) {
3211 VISIT(c, expr, elt);
3212 ADDOP(c, YIELD_VALUE);
3213 ADDOP(c, POP_TOP);
3214
3215 compiler_use_next_block(c, skip);
3216 }
3217 for (i = 0; i < n; i++) {
3218 ADDOP_I(c, JUMP_FORWARD, 1);
3219 if (i == 0)
3220 compiler_use_next_block(c, if_cleanup);
3221
3222 ADDOP(c, POP_TOP);
3223 }
3224 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3225 compiler_use_next_block(c, anchor);
3226 ADDOP(c, POP_BLOCK);
3227 compiler_pop_fblock(c, LOOP, start);
3228 compiler_use_next_block(c, end);
3229
3230 return 1;
3231}
3232
3233static int
3234compiler_genexp(struct compiler *c, expr_ty e)
3235{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00003236 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003237 PyCodeObject *co;
3238 expr_ty outermost_iter = ((comprehension_ty)
3239 (asdl_seq_GET(e->v.GeneratorExp.generators,
3240 0)))->iter;
3241
Nick Coghlan944d3eb2005-11-16 12:46:55 +00003242 if (!name) {
3243 name = PyString_FromString("<genexpr>");
3244 if (!name)
3245 return 0;
3246 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003247
3248 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
3249 return 0;
3250 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
3251 e->v.GeneratorExp.elt);
3252 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00003253 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003254 if (co == NULL)
3255 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003256
3257 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00003258 Py_DECREF(co);
3259
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003260 VISIT(c, expr, outermost_iter);
3261 ADDOP(c, GET_ITER);
3262 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003263
3264 return 1;
3265}
3266
3267static int
3268compiler_visit_keyword(struct compiler *c, keyword_ty k)
3269{
3270 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3271 VISIT(c, expr, k->value);
3272 return 1;
3273}
3274
3275/* Test whether expression is constant. For constants, report
3276 whether they are true or false.
3277
3278 Return values: 1 for true, 0 for false, -1 for non-constant.
3279 */
3280
3281static int
3282expr_constant(expr_ty e)
3283{
3284 switch (e->kind) {
3285 case Num_kind:
3286 return PyObject_IsTrue(e->v.Num.n);
3287 case Str_kind:
3288 return PyObject_IsTrue(e->v.Str.s);
3289 default:
3290 return -1;
3291 }
3292}
3293
3294static int
3295compiler_visit_expr(struct compiler *c, expr_ty e)
3296{
3297 int i, n;
3298
3299 if (e->lineno > c->u->u_lineno) {
3300 c->u->u_lineno = e->lineno;
3301 c->u->u_lineno_set = false;
3302 }
3303 switch (e->kind) {
3304 case BoolOp_kind:
3305 return compiler_boolop(c, e);
3306 case BinOp_kind:
3307 VISIT(c, expr, e->v.BinOp.left);
3308 VISIT(c, expr, e->v.BinOp.right);
3309 ADDOP(c, binop(c, e->v.BinOp.op));
3310 break;
3311 case UnaryOp_kind:
3312 VISIT(c, expr, e->v.UnaryOp.operand);
3313 ADDOP(c, unaryop(e->v.UnaryOp.op));
3314 break;
3315 case Lambda_kind:
3316 return compiler_lambda(c, e);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00003317 case IfExp_kind:
3318 return compiler_ifexp(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003319 case Dict_kind:
3320 /* XXX get rid of arg? */
3321 ADDOP_I(c, BUILD_MAP, 0);
3322 n = asdl_seq_LEN(e->v.Dict.values);
3323 /* We must arrange things just right for STORE_SUBSCR.
3324 It wants the stack to look like (value) (dict) (key) */
3325 for (i = 0; i < n; i++) {
3326 ADDOP(c, DUP_TOP);
3327 VISIT(c, expr, asdl_seq_GET(e->v.Dict.values, i));
3328 ADDOP(c, ROT_TWO);
3329 VISIT(c, expr, asdl_seq_GET(e->v.Dict.keys, i));
3330 ADDOP(c, STORE_SUBSCR);
3331 }
3332 break;
3333 case ListComp_kind:
3334 return compiler_listcomp(c, e);
3335 case GeneratorExp_kind:
3336 return compiler_genexp(c, e);
3337 case Yield_kind:
3338 if (c->u->u_ste->ste_type != FunctionBlock)
3339 return compiler_error(c, "'yield' outside function");
3340 /*
3341 for (i = 0; i < c->u->u_nfblocks; i++) {
3342 if (c->u->u_fblock[i].fb_type == FINALLY_TRY)
3343 return compiler_error(
3344 c, "'yield' not allowed in a 'try' "
3345 "block with a 'finally' clause");
3346 }
3347 */
3348 if (e->v.Yield.value) {
3349 VISIT(c, expr, e->v.Yield.value);
3350 }
3351 else {
3352 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3353 }
3354 ADDOP(c, YIELD_VALUE);
3355 break;
3356 case Compare_kind:
3357 return compiler_compare(c, e);
3358 case Call_kind:
3359 return compiler_call(c, e);
3360 case Repr_kind:
3361 VISIT(c, expr, e->v.Repr.value);
3362 ADDOP(c, UNARY_CONVERT);
3363 break;
3364 case Num_kind:
3365 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3366 break;
3367 case Str_kind:
3368 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3369 break;
3370 /* The following exprs can be assignment targets. */
3371 case Attribute_kind:
3372 if (e->v.Attribute.ctx != AugStore)
3373 VISIT(c, expr, e->v.Attribute.value);
3374 switch (e->v.Attribute.ctx) {
3375 case AugLoad:
3376 ADDOP(c, DUP_TOP);
3377 /* Fall through to load */
3378 case Load:
3379 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3380 break;
3381 case AugStore:
3382 ADDOP(c, ROT_TWO);
3383 /* Fall through to save */
3384 case Store:
3385 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3386 break;
3387 case Del:
3388 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3389 break;
3390 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003391 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003392 PyErr_SetString(PyExc_SystemError,
3393 "param invalid in attribute expression");
3394 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003395 }
3396 break;
3397 case Subscript_kind:
3398 switch (e->v.Subscript.ctx) {
3399 case AugLoad:
3400 VISIT(c, expr, e->v.Subscript.value);
3401 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3402 break;
3403 case Load:
3404 VISIT(c, expr, e->v.Subscript.value);
3405 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3406 break;
3407 case AugStore:
3408 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3409 break;
3410 case Store:
3411 VISIT(c, expr, e->v.Subscript.value);
3412 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3413 break;
3414 case Del:
3415 VISIT(c, expr, e->v.Subscript.value);
3416 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3417 break;
3418 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003419 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003420 PyErr_SetString(PyExc_SystemError,
3421 "param invalid in subscript expression");
3422 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003423 }
3424 break;
3425 case Name_kind:
3426 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3427 /* child nodes of List and Tuple will have expr_context set */
3428 case List_kind:
3429 return compiler_list(c, e);
3430 case Tuple_kind:
3431 return compiler_tuple(c, e);
3432 }
3433 return 1;
3434}
3435
3436static int
3437compiler_augassign(struct compiler *c, stmt_ty s)
3438{
3439 expr_ty e = s->v.AugAssign.target;
3440 expr_ty auge;
3441
3442 assert(s->kind == AugAssign_kind);
3443
3444 switch (e->kind) {
3445 case Attribute_kind:
3446 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003447 AugLoad, e->lineno, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003448 if (auge == NULL)
3449 return 0;
3450 VISIT(c, expr, auge);
3451 VISIT(c, expr, s->v.AugAssign.value);
3452 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3453 auge->v.Attribute.ctx = AugStore;
3454 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003455 break;
3456 case Subscript_kind:
3457 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003458 AugLoad, e->lineno, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003459 if (auge == NULL)
3460 return 0;
3461 VISIT(c, expr, auge);
3462 VISIT(c, expr, s->v.AugAssign.value);
3463 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3464 auge->v.Subscript.ctx = AugStore;
3465 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003466 break;
3467 case Name_kind:
3468 VISIT(c, expr, s->v.AugAssign.target);
3469 VISIT(c, expr, s->v.AugAssign.value);
3470 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3471 return compiler_nameop(c, e->v.Name.id, Store);
3472 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003473 PyErr_Format(PyExc_SystemError,
3474 "invalid node type (%d) for augmented assignment",
3475 e->kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003476 return 0;
3477 }
3478 return 1;
3479}
3480
3481static int
3482compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3483{
3484 struct fblockinfo *f;
3485 if (c->u->u_nfblocks >= CO_MAXBLOCKS)
3486 return 0;
3487 f = &c->u->u_fblock[c->u->u_nfblocks++];
3488 f->fb_type = t;
3489 f->fb_block = b;
3490 return 1;
3491}
3492
3493static void
3494compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3495{
3496 struct compiler_unit *u = c->u;
3497 assert(u->u_nfblocks > 0);
3498 u->u_nfblocks--;
3499 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3500 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3501}
3502
3503/* Raises a SyntaxError and returns 0.
3504 If something goes wrong, a different exception may be raised.
3505*/
3506
3507static int
3508compiler_error(struct compiler *c, const char *errstr)
3509{
3510 PyObject *loc;
3511 PyObject *u = NULL, *v = NULL;
3512
3513 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3514 if (!loc) {
3515 Py_INCREF(Py_None);
3516 loc = Py_None;
3517 }
3518 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3519 Py_None, loc);
3520 if (!u)
3521 goto exit;
3522 v = Py_BuildValue("(zO)", errstr, u);
3523 if (!v)
3524 goto exit;
3525 PyErr_SetObject(PyExc_SyntaxError, v);
3526 exit:
3527 Py_DECREF(loc);
3528 Py_XDECREF(u);
3529 Py_XDECREF(v);
3530 return 0;
3531}
3532
3533static int
3534compiler_handle_subscr(struct compiler *c, const char *kind,
3535 expr_context_ty ctx)
3536{
3537 int op = 0;
3538
3539 /* XXX this code is duplicated */
3540 switch (ctx) {
3541 case AugLoad: /* fall through to Load */
3542 case Load: op = BINARY_SUBSCR; break;
3543 case AugStore:/* fall through to Store */
3544 case Store: op = STORE_SUBSCR; break;
3545 case Del: op = DELETE_SUBSCR; break;
3546 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003547 PyErr_Format(PyExc_SystemError,
3548 "invalid %s kind %d in subscript\n",
3549 kind, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003550 return 0;
3551 }
3552 if (ctx == AugLoad) {
3553 ADDOP_I(c, DUP_TOPX, 2);
3554 }
3555 else if (ctx == AugStore) {
3556 ADDOP(c, ROT_THREE);
3557 }
3558 ADDOP(c, op);
3559 return 1;
3560}
3561
3562static int
3563compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3564{
3565 int n = 2;
3566 assert(s->kind == Slice_kind);
3567
3568 /* only handles the cases where BUILD_SLICE is emitted */
3569 if (s->v.Slice.lower) {
3570 VISIT(c, expr, s->v.Slice.lower);
3571 }
3572 else {
3573 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3574 }
3575
3576 if (s->v.Slice.upper) {
3577 VISIT(c, expr, s->v.Slice.upper);
3578 }
3579 else {
3580 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3581 }
3582
3583 if (s->v.Slice.step) {
3584 n++;
3585 VISIT(c, expr, s->v.Slice.step);
3586 }
3587 ADDOP_I(c, BUILD_SLICE, n);
3588 return 1;
3589}
3590
3591static int
3592compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3593{
3594 int op = 0, slice_offset = 0, stack_count = 0;
3595
3596 assert(s->v.Slice.step == NULL);
3597 if (s->v.Slice.lower) {
3598 slice_offset++;
3599 stack_count++;
3600 if (ctx != AugStore)
3601 VISIT(c, expr, s->v.Slice.lower);
3602 }
3603 if (s->v.Slice.upper) {
3604 slice_offset += 2;
3605 stack_count++;
3606 if (ctx != AugStore)
3607 VISIT(c, expr, s->v.Slice.upper);
3608 }
3609
3610 if (ctx == AugLoad) {
3611 switch (stack_count) {
3612 case 0: ADDOP(c, DUP_TOP); break;
3613 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3614 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3615 }
3616 }
3617 else if (ctx == AugStore) {
3618 switch (stack_count) {
3619 case 0: ADDOP(c, ROT_TWO); break;
3620 case 1: ADDOP(c, ROT_THREE); break;
3621 case 2: ADDOP(c, ROT_FOUR); break;
3622 }
3623 }
3624
3625 switch (ctx) {
3626 case AugLoad: /* fall through to Load */
3627 case Load: op = SLICE; break;
3628 case AugStore:/* fall through to Store */
3629 case Store: op = STORE_SLICE; break;
3630 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003631 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003632 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003633 PyErr_SetString(PyExc_SystemError,
3634 "param invalid in simple slice");
3635 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003636 }
3637
3638 ADDOP(c, op + slice_offset);
3639 return 1;
3640}
3641
3642static int
3643compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3644 expr_context_ty ctx)
3645{
3646 switch (s->kind) {
3647 case Ellipsis_kind:
3648 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3649 break;
3650 case Slice_kind:
3651 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003652 case Index_kind:
3653 VISIT(c, expr, s->v.Index.value);
3654 break;
3655 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003656 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003657 PyErr_SetString(PyExc_SystemError,
3658 "extended slice invalid in nested slice");
3659 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003660 }
3661 return 1;
3662}
3663
3664
3665static int
3666compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3667{
3668 switch (s->kind) {
3669 case Ellipsis_kind:
3670 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3671 break;
3672 case Slice_kind:
3673 if (!s->v.Slice.step)
3674 return compiler_simple_slice(c, s, ctx);
3675 if (!compiler_slice(c, s, ctx))
3676 return 0;
3677 if (ctx == AugLoad) {
3678 ADDOP_I(c, DUP_TOPX, 2);
3679 }
3680 else if (ctx == AugStore) {
3681 ADDOP(c, ROT_THREE);
3682 }
3683 return compiler_handle_subscr(c, "slice", ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003684 case ExtSlice_kind: {
3685 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3686 for (i = 0; i < n; i++) {
3687 slice_ty sub = asdl_seq_GET(s->v.ExtSlice.dims, i);
3688 if (!compiler_visit_nested_slice(c, sub, ctx))
3689 return 0;
3690 }
3691 ADDOP_I(c, BUILD_TUPLE, n);
3692 return compiler_handle_subscr(c, "extended slice", ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003693 }
3694 case Index_kind:
3695 if (ctx != AugStore)
3696 VISIT(c, expr, s->v.Index.value);
3697 return compiler_handle_subscr(c, "index", ctx);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003698 default:
3699 PyErr_Format(PyExc_SystemError,
3700 "invalid slice %d", s->kind);
3701 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003702 }
3703 return 1;
3704}
3705
3706/* do depth-first search of basic block graph, starting with block.
3707 post records the block indices in post-order.
3708
3709 XXX must handle implicit jumps from one block to next
3710*/
3711
3712static void
3713dfs(struct compiler *c, basicblock *b, struct assembler *a)
3714{
3715 int i;
3716 struct instr *instr = NULL;
3717
3718 if (b->b_seen)
3719 return;
3720 b->b_seen = 1;
3721 if (b->b_next != NULL)
3722 dfs(c, b->b_next, a);
3723 for (i = 0; i < b->b_iused; i++) {
3724 instr = &b->b_instr[i];
3725 if (instr->i_jrel || instr->i_jabs)
3726 dfs(c, instr->i_target, a);
3727 }
3728 a->a_postorder[a->a_nblocks++] = b;
3729}
3730
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003731static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003732stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3733{
3734 int i;
3735 struct instr *instr;
3736 if (b->b_seen || b->b_startdepth >= depth)
3737 return maxdepth;
3738 b->b_seen = 1;
3739 b->b_startdepth = depth;
3740 for (i = 0; i < b->b_iused; i++) {
3741 instr = &b->b_instr[i];
3742 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3743 if (depth > maxdepth)
3744 maxdepth = depth;
3745 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3746 if (instr->i_jrel || instr->i_jabs) {
3747 maxdepth = stackdepth_walk(c, instr->i_target,
3748 depth, maxdepth);
3749 if (instr->i_opcode == JUMP_ABSOLUTE ||
3750 instr->i_opcode == JUMP_FORWARD) {
3751 goto out; /* remaining code is dead */
3752 }
3753 }
3754 }
3755 if (b->b_next)
3756 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3757out:
3758 b->b_seen = 0;
3759 return maxdepth;
3760}
3761
3762/* Find the flow path that needs the largest stack. We assume that
3763 * cycles in the flow graph have no net effect on the stack depth.
3764 */
3765static int
3766stackdepth(struct compiler *c)
3767{
3768 basicblock *b, *entryblock;
3769 entryblock = NULL;
3770 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3771 b->b_seen = 0;
3772 b->b_startdepth = INT_MIN;
3773 entryblock = b;
3774 }
3775 return stackdepth_walk(c, entryblock, 0, 0);
3776}
3777
3778static int
3779assemble_init(struct assembler *a, int nblocks, int firstlineno)
3780{
3781 memset(a, 0, sizeof(struct assembler));
3782 a->a_lineno = firstlineno;
3783 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3784 if (!a->a_bytecode)
3785 return 0;
3786 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3787 if (!a->a_lnotab)
3788 return 0;
3789 a->a_postorder = (basicblock **)PyObject_Malloc(
3790 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003791 if (!a->a_postorder) {
3792 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003793 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003794 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003795 return 1;
3796}
3797
3798static void
3799assemble_free(struct assembler *a)
3800{
3801 Py_XDECREF(a->a_bytecode);
3802 Py_XDECREF(a->a_lnotab);
3803 if (a->a_postorder)
3804 PyObject_Free(a->a_postorder);
3805}
3806
3807/* Return the size of a basic block in bytes. */
3808
3809static int
3810instrsize(struct instr *instr)
3811{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003812 if (!instr->i_hasarg)
3813 return 1;
3814 if (instr->i_oparg > 0xffff)
3815 return 6;
3816 return 3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003817}
3818
3819static int
3820blocksize(basicblock *b)
3821{
3822 int i;
3823 int size = 0;
3824
3825 for (i = 0; i < b->b_iused; i++)
3826 size += instrsize(&b->b_instr[i]);
3827 return size;
3828}
3829
3830/* All about a_lnotab.
3831
3832c_lnotab is an array of unsigned bytes disguised as a Python string.
3833It is used to map bytecode offsets to source code line #s (when needed
3834for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003835
Tim Peters2a7f3842001-06-09 09:26:21 +00003836The array is conceptually a list of
3837 (bytecode offset increment, line number increment)
3838pairs. The details are important and delicate, best illustrated by example:
3839
3840 byte code offset source code line number
3841 0 1
3842 6 2
3843 50 7
3844 350 307
3845 361 308
3846
3847The first trick is that these numbers aren't stored, only the increments
3848from one row to the next (this doesn't really work, but it's a start):
3849
3850 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3851
3852The second trick is that an unsigned byte can't hold negative values, or
3853values larger than 255, so (a) there's a deep assumption that byte code
3854offsets and their corresponding line #s both increase monotonically, and (b)
3855if at least one column jumps by more than 255 from one row to the next, more
3856than one pair is written to the table. In case #b, there's no way to know
3857from looking at the table later how many were written. That's the delicate
3858part. A user of c_lnotab desiring to find the source line number
3859corresponding to a bytecode address A should do something like this
3860
3861 lineno = addr = 0
3862 for addr_incr, line_incr in c_lnotab:
3863 addr += addr_incr
3864 if addr > A:
3865 return lineno
3866 lineno += line_incr
3867
3868In order for this to work, when the addr field increments by more than 255,
3869the line # increment in each pair generated must be 0 until the remaining addr
3870increment is < 256. So, in the example above, com_set_lineno should not (as
3871was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
3872255, 0, 45, 255, 0, 45.
3873*/
3874
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003875static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003876assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003877{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003878 int d_bytecode, d_lineno;
3879 int len;
3880 char *lnotab;
3881
3882 d_bytecode = a->a_offset - a->a_lineno_off;
3883 d_lineno = i->i_lineno - a->a_lineno;
3884
3885 assert(d_bytecode >= 0);
3886 assert(d_lineno >= 0);
3887
3888 if (d_lineno == 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003889 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003890
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003891 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003892 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003893 nbytes = a->a_lnotab_off + 2 * ncodes;
3894 len = PyString_GET_SIZE(a->a_lnotab);
3895 if (nbytes >= len) {
3896 if (len * 2 < nbytes)
3897 len = nbytes;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003898 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003899 len *= 2;
3900 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3901 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003902 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003903 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003904 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003905 *lnotab++ = 255;
3906 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003907 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003908 d_bytecode -= ncodes * 255;
3909 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003910 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003911 assert(d_bytecode <= 255);
3912 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003913 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003914 nbytes = a->a_lnotab_off + 2 * ncodes;
3915 len = PyString_GET_SIZE(a->a_lnotab);
3916 if (nbytes >= len) {
3917 if (len * 2 < nbytes)
3918 len = nbytes;
Guido van Rossum635abd21997-01-06 22:56:52 +00003919 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003920 len *= 2;
3921 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3922 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003923 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003924 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3925 *lnotab++ = 255;
3926 *lnotab++ = d_bytecode;
3927 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003928 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003929 *lnotab++ = 255;
3930 *lnotab++ = 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003931 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003932 d_lineno -= ncodes * 255;
3933 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003934 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003935
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003936 len = PyString_GET_SIZE(a->a_lnotab);
3937 if (a->a_lnotab_off + 2 >= len) {
3938 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003939 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003940 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003941 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003942
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003943 a->a_lnotab_off += 2;
3944 if (d_bytecode) {
3945 *lnotab++ = d_bytecode;
3946 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003947 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003948 else { /* First line of a block; def stmt, etc. */
3949 *lnotab++ = 0;
3950 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003951 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003952 a->a_lineno = i->i_lineno;
3953 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003954 return 1;
3955}
3956
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003957/* assemble_emit()
3958 Extend the bytecode with a new instruction.
3959 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003960*/
3961
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003962static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003963assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003964{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003965 int size, arg = 0, ext = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003966 int len = PyString_GET_SIZE(a->a_bytecode);
3967 char *code;
3968
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003969 size = instrsize(i);
3970 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003971 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003972 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003973 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003974 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003975 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003976 if (a->a_offset + size >= len) {
3977 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003978 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003979 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003980 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3981 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003982 if (size == 6) {
3983 assert(i->i_hasarg);
3984 *code++ = (char)EXTENDED_ARG;
3985 *code++ = ext & 0xff;
3986 *code++ = ext >> 8;
3987 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003988 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003989 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003990 if (i->i_hasarg) {
3991 assert(size == 3 || size == 6);
3992 *code++ = arg & 0xff;
3993 *code++ = arg >> 8;
3994 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003995 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003996}
3997
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003998static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003999assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00004000{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004001 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00004002 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00004003 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00004004
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004005 /* Compute the size of each block and fixup jump args.
4006 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00004007start:
4008 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004009 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004010 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004011 bsize = blocksize(b);
4012 b->b_offset = totsize;
4013 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00004014 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00004015 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004016 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4017 bsize = b->b_offset;
4018 for (i = 0; i < b->b_iused; i++) {
4019 struct instr *instr = &b->b_instr[i];
4020 /* Relative jumps are computed relative to
4021 the instruction pointer after fetching
4022 the jump instruction.
4023 */
4024 bsize += instrsize(instr);
4025 if (instr->i_jabs)
4026 instr->i_oparg = instr->i_target->b_offset;
4027 else if (instr->i_jrel) {
4028 int delta = instr->i_target->b_offset - bsize;
4029 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004030 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00004031 else
4032 continue;
4033 if (instr->i_oparg > 0xffff)
4034 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004035 }
4036 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00004037
4038 /* XXX: This is an awful hack that could hurt performance, but
4039 on the bright side it should work until we come up
4040 with a better solution.
4041
4042 In the meantime, should the goto be dropped in favor
4043 of a loop?
4044
4045 The issue is that in the first loop blocksize() is called
4046 which calls instrsize() which requires i_oparg be set
4047 appropriately. There is a bootstrap problem because
4048 i_oparg is calculated in the second loop above.
4049
4050 So we loop until we stop seeing new EXTENDED_ARGs.
4051 The only EXTENDED_ARGs that could be popping up are
4052 ones in jump instructions. So this should converge
4053 fairly quickly.
4054 */
4055 if (last_extended_arg_count != extended_arg_count) {
4056 last_extended_arg_count = extended_arg_count;
4057 goto start;
4058 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004059}
4060
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004061static PyObject *
4062dict_keys_inorder(PyObject *dict, int offset)
4063{
4064 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004065 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004066
4067 tuple = PyTuple_New(size);
4068 if (tuple == NULL)
4069 return NULL;
4070 while (PyDict_Next(dict, &pos, &k, &v)) {
4071 i = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004072 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004073 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004074 assert((i - offset) < size);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004075 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004076 PyTuple_SET_ITEM(tuple, i - offset, k);
4077 }
4078 return tuple;
4079}
4080
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004081static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004082compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004083{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004084 PySTEntryObject *ste = c->u->u_ste;
4085 int flags = 0, n;
4086 if (ste->ste_type != ModuleBlock)
4087 flags |= CO_NEWLOCALS;
4088 if (ste->ste_type == FunctionBlock) {
4089 if (!ste->ste_unoptimized)
4090 flags |= CO_OPTIMIZED;
4091 if (ste->ste_nested)
4092 flags |= CO_NESTED;
4093 if (ste->ste_generator)
4094 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004095 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004096 if (ste->ste_varargs)
4097 flags |= CO_VARARGS;
4098 if (ste->ste_varkeywords)
4099 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00004100 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004101 flags |= CO_GENERATOR;
4102 if (c->c_flags->cf_flags & CO_FUTURE_DIVISION)
4103 flags |= CO_FUTURE_DIVISION;
4104 n = PyDict_Size(c->u->u_freevars);
4105 if (n < 0)
4106 return -1;
4107 if (n == 0) {
4108 n = PyDict_Size(c->u->u_cellvars);
4109 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004110 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004111 if (n == 0) {
4112 flags |= CO_NOFREE;
4113 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004114 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004115
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004116 return flags;
4117}
4118
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004119static PyCodeObject *
4120makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004121{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004122 PyObject *tmp;
4123 PyCodeObject *co = NULL;
4124 PyObject *consts = NULL;
4125 PyObject *names = NULL;
4126 PyObject *varnames = NULL;
4127 PyObject *filename = NULL;
4128 PyObject *name = NULL;
4129 PyObject *freevars = NULL;
4130 PyObject *cellvars = NULL;
4131 PyObject *bytecode = NULL;
4132 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004133
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004134 tmp = dict_keys_inorder(c->u->u_consts, 0);
4135 if (!tmp)
4136 goto error;
4137 consts = PySequence_List(tmp); /* optimize_code requires a list */
4138 Py_DECREF(tmp);
4139
4140 names = dict_keys_inorder(c->u->u_names, 0);
4141 varnames = dict_keys_inorder(c->u->u_varnames, 0);
4142 if (!consts || !names || !varnames)
4143 goto error;
4144
4145 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4146 if (!cellvars)
4147 goto error;
4148 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4149 if (!freevars)
4150 goto error;
4151 filename = PyString_FromString(c->c_filename);
4152 if (!filename)
4153 goto error;
4154
4155 nlocals = PyDict_Size(c->u->u_varnames);
4156 flags = compute_code_flags(c);
4157 if (flags < 0)
4158 goto error;
4159
4160 bytecode = optimize_code(a->a_bytecode, consts, names, a->a_lnotab);
4161 if (!bytecode)
4162 goto error;
4163
4164 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4165 if (!tmp)
4166 goto error;
4167 Py_DECREF(consts);
4168 consts = tmp;
4169
4170 co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags,
4171 bytecode, consts, names, varnames,
4172 freevars, cellvars,
4173 filename, c->u->u_name,
4174 c->u->u_firstlineno,
4175 a->a_lnotab);
4176 error:
4177 Py_XDECREF(consts);
4178 Py_XDECREF(names);
4179 Py_XDECREF(varnames);
4180 Py_XDECREF(filename);
4181 Py_XDECREF(name);
4182 Py_XDECREF(freevars);
4183 Py_XDECREF(cellvars);
4184 Py_XDECREF(bytecode);
4185 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004186}
4187
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004188static PyCodeObject *
4189assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004190{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004191 basicblock *b, *entryblock;
4192 struct assembler a;
4193 int i, j, nblocks;
4194 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004195
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004196 /* Make sure every block that falls off the end returns None.
4197 XXX NEXT_BLOCK() isn't quite right, because if the last
4198 block ends with a jump or return b_next shouldn't set.
4199 */
4200 if (!c->u->u_curblock->b_return) {
4201 NEXT_BLOCK(c);
4202 if (addNone)
4203 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4204 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004205 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004206
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004207 nblocks = 0;
4208 entryblock = NULL;
4209 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4210 nblocks++;
4211 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004212 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004213
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004214 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4215 goto error;
4216 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004217
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004218 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004219 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004220
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004221 /* Emit code in reverse postorder from dfs. */
4222 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00004223 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004224 for (j = 0; j < b->b_iused; j++)
4225 if (!assemble_emit(&a, &b->b_instr[j]))
4226 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004227 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004228
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004229 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4230 goto error;
4231 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
4232 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004233
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004234 co = makecode(c, &a);
4235 error:
4236 assemble_free(&a);
4237 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004238}