blob: e743168517c7cce9dc2f5099fe7ca59485861e0b [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
2013compiler_lambda(struct compiler *c, expr_ty e)
2014{
2015 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002016 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002017 arguments_ty args = e->v.Lambda.args;
2018 assert(e->kind == Lambda_kind);
2019
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002020 if (!name) {
2021 name = PyString_InternFromString("<lambda>");
2022 if (!name)
2023 return 0;
2024 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025
2026 if (args->defaults)
2027 VISIT_SEQ(c, expr, args->defaults);
2028 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2029 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002030
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002031 /* unpack nested arguments */
2032 compiler_arguments(c, args);
2033
2034 c->u->u_argcount = asdl_seq_LEN(args->args);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00002035 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2036 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002037 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00002038 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039 if (co == NULL)
2040 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002041
2042 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00002043 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002044
2045 return 1;
2046}
2047
2048static int
2049compiler_print(struct compiler *c, stmt_ty s)
2050{
2051 int i, n;
2052 bool dest;
2053
2054 assert(s->kind == Print_kind);
2055 n = asdl_seq_LEN(s->v.Print.values);
2056 dest = false;
2057 if (s->v.Print.dest) {
2058 VISIT(c, expr, s->v.Print.dest);
2059 dest = true;
2060 }
2061 for (i = 0; i < n; i++) {
2062 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i);
2063 if (dest) {
2064 ADDOP(c, DUP_TOP);
2065 VISIT(c, expr, e);
2066 ADDOP(c, ROT_TWO);
2067 ADDOP(c, PRINT_ITEM_TO);
2068 }
2069 else {
2070 VISIT(c, expr, e);
2071 ADDOP(c, PRINT_ITEM);
2072 }
2073 }
2074 if (s->v.Print.nl) {
2075 if (dest)
2076 ADDOP(c, PRINT_NEWLINE_TO)
2077 else
2078 ADDOP(c, PRINT_NEWLINE)
2079 }
2080 else if (dest)
2081 ADDOP(c, POP_TOP);
2082 return 1;
2083}
2084
2085static int
2086compiler_if(struct compiler *c, stmt_ty s)
2087{
2088 basicblock *end, *next;
2089
2090 assert(s->kind == If_kind);
2091 end = compiler_new_block(c);
2092 if (end == NULL)
2093 return 0;
2094 next = compiler_new_block(c);
2095 if (next == NULL)
2096 return 0;
2097 VISIT(c, expr, s->v.If.test);
2098 ADDOP_JREL(c, JUMP_IF_FALSE, next);
2099 ADDOP(c, POP_TOP);
2100 VISIT_SEQ(c, stmt, s->v.If.body);
2101 ADDOP_JREL(c, JUMP_FORWARD, end);
2102 compiler_use_next_block(c, next);
2103 ADDOP(c, POP_TOP);
2104 if (s->v.If.orelse)
2105 VISIT_SEQ(c, stmt, s->v.If.orelse);
2106 compiler_use_next_block(c, end);
2107 return 1;
2108}
2109
2110static int
2111compiler_for(struct compiler *c, stmt_ty s)
2112{
2113 basicblock *start, *cleanup, *end;
2114
2115 start = compiler_new_block(c);
2116 cleanup = compiler_new_block(c);
2117 end = compiler_new_block(c);
2118 if (start == NULL || end == NULL || cleanup == NULL)
2119 return 0;
2120 ADDOP_JREL(c, SETUP_LOOP, end);
2121 if (!compiler_push_fblock(c, LOOP, start))
2122 return 0;
2123 VISIT(c, expr, s->v.For.iter);
2124 ADDOP(c, GET_ITER);
2125 compiler_use_next_block(c, start);
2126 ADDOP_JREL(c, FOR_ITER, cleanup);
2127 VISIT(c, expr, s->v.For.target);
2128 VISIT_SEQ(c, stmt, s->v.For.body);
2129 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2130 compiler_use_next_block(c, cleanup);
2131 ADDOP(c, POP_BLOCK);
2132 compiler_pop_fblock(c, LOOP, start);
2133 VISIT_SEQ(c, stmt, s->v.For.orelse);
2134 compiler_use_next_block(c, end);
2135 return 1;
2136}
2137
2138static int
2139compiler_while(struct compiler *c, stmt_ty s)
2140{
2141 basicblock *loop, *orelse, *end, *anchor = NULL;
2142 int constant = expr_constant(s->v.While.test);
2143
2144 if (constant == 0)
2145 return 1;
2146 loop = compiler_new_block(c);
2147 end = compiler_new_block(c);
2148 if (constant == -1) {
2149 anchor = compiler_new_block(c);
2150 if (anchor == NULL)
2151 return 0;
2152 }
2153 if (loop == NULL || end == NULL)
2154 return 0;
2155 if (s->v.While.orelse) {
2156 orelse = compiler_new_block(c);
2157 if (orelse == NULL)
2158 return 0;
2159 }
2160 else
2161 orelse = NULL;
2162
2163 ADDOP_JREL(c, SETUP_LOOP, end);
2164 compiler_use_next_block(c, loop);
2165 if (!compiler_push_fblock(c, LOOP, loop))
2166 return 0;
2167 if (constant == -1) {
2168 VISIT(c, expr, s->v.While.test);
2169 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
2170 ADDOP(c, POP_TOP);
2171 }
2172 VISIT_SEQ(c, stmt, s->v.While.body);
2173 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
2174
2175 /* XXX should the two POP instructions be in a separate block
2176 if there is no else clause ?
2177 */
2178
2179 if (constant == -1) {
2180 compiler_use_next_block(c, anchor);
2181 ADDOP(c, POP_TOP);
2182 ADDOP(c, POP_BLOCK);
2183 }
2184 compiler_pop_fblock(c, LOOP, loop);
2185 if (orelse != NULL)
2186 VISIT_SEQ(c, stmt, s->v.While.orelse);
2187 compiler_use_next_block(c, end);
2188
2189 return 1;
2190}
2191
2192static int
2193compiler_continue(struct compiler *c)
2194{
2195 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
2196 int i;
2197
2198 if (!c->u->u_nfblocks)
2199 return compiler_error(c, LOOP_ERROR_MSG);
2200 i = c->u->u_nfblocks - 1;
2201 switch (c->u->u_fblock[i].fb_type) {
2202 case LOOP:
2203 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
2204 break;
2205 case EXCEPT:
2206 case FINALLY_TRY:
2207 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP)
2208 ;
2209 if (i == -1)
2210 return compiler_error(c, LOOP_ERROR_MSG);
2211 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
2212 break;
2213 case FINALLY_END:
2214 return compiler_error(c,
2215 "'continue' not supported inside 'finally' clause");
2216 }
2217
2218 return 1;
2219}
2220
2221/* Code generated for "try: <body> finally: <finalbody>" is as follows:
2222
2223 SETUP_FINALLY L
2224 <code for body>
2225 POP_BLOCK
2226 LOAD_CONST <None>
2227 L: <code for finalbody>
2228 END_FINALLY
2229
2230 The special instructions use the block stack. Each block
2231 stack entry contains the instruction that created it (here
2232 SETUP_FINALLY), the level of the value stack at the time the
2233 block stack entry was created, and a label (here L).
2234
2235 SETUP_FINALLY:
2236 Pushes the current value stack level and the label
2237 onto the block stack.
2238 POP_BLOCK:
2239 Pops en entry from the block stack, and pops the value
2240 stack until its level is the same as indicated on the
2241 block stack. (The label is ignored.)
2242 END_FINALLY:
2243 Pops a variable number of entries from the *value* stack
2244 and re-raises the exception they specify. The number of
2245 entries popped depends on the (pseudo) exception type.
2246
2247 The block stack is unwound when an exception is raised:
2248 when a SETUP_FINALLY entry is found, the exception is pushed
2249 onto the value stack (and the exception condition is cleared),
2250 and the interpreter jumps to the label gotten from the block
2251 stack.
2252*/
2253
2254static int
2255compiler_try_finally(struct compiler *c, stmt_ty s)
2256{
2257 basicblock *body, *end;
2258 body = compiler_new_block(c);
2259 end = compiler_new_block(c);
2260 if (body == NULL || end == NULL)
2261 return 0;
2262
2263 ADDOP_JREL(c, SETUP_FINALLY, end);
2264 compiler_use_next_block(c, body);
2265 if (!compiler_push_fblock(c, FINALLY_TRY, body))
2266 return 0;
2267 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
2268 ADDOP(c, POP_BLOCK);
2269 compiler_pop_fblock(c, FINALLY_TRY, body);
2270
2271 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2272 compiler_use_next_block(c, end);
2273 if (!compiler_push_fblock(c, FINALLY_END, end))
2274 return 0;
2275 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
2276 ADDOP(c, END_FINALLY);
2277 compiler_pop_fblock(c, FINALLY_END, end);
2278
2279 return 1;
2280}
2281
2282/*
2283 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
2284 (The contents of the value stack is shown in [], with the top
2285 at the right; 'tb' is trace-back info, 'val' the exception's
2286 associated value, and 'exc' the exception.)
2287
2288 Value stack Label Instruction Argument
2289 [] SETUP_EXCEPT L1
2290 [] <code for S>
2291 [] POP_BLOCK
2292 [] JUMP_FORWARD L0
2293
2294 [tb, val, exc] L1: DUP )
2295 [tb, val, exc, exc] <evaluate E1> )
2296 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2297 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
2298 [tb, val, exc, 1] POP )
2299 [tb, val, exc] POP
2300 [tb, val] <assign to V1> (or POP if no V1)
2301 [tb] POP
2302 [] <code for S1>
2303 JUMP_FORWARD L0
2304
2305 [tb, val, exc, 0] L2: POP
2306 [tb, val, exc] DUP
2307 .............................etc.......................
2308
2309 [tb, val, exc, 0] Ln+1: POP
2310 [tb, val, exc] END_FINALLY # re-raise exception
2311
2312 [] L0: <next statement>
2313
2314 Of course, parts are not generated if Vi or Ei is not present.
2315*/
2316static int
2317compiler_try_except(struct compiler *c, stmt_ty s)
2318{
2319 basicblock *body, *orelse, *except, *end;
2320 int i, n;
2321
2322 body = compiler_new_block(c);
2323 except = compiler_new_block(c);
2324 orelse = compiler_new_block(c);
2325 end = compiler_new_block(c);
2326 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
2327 return 0;
2328 ADDOP_JREL(c, SETUP_EXCEPT, except);
2329 compiler_use_next_block(c, body);
2330 if (!compiler_push_fblock(c, EXCEPT, body))
2331 return 0;
2332 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
2333 ADDOP(c, POP_BLOCK);
2334 compiler_pop_fblock(c, EXCEPT, body);
2335 ADDOP_JREL(c, JUMP_FORWARD, orelse);
2336 n = asdl_seq_LEN(s->v.TryExcept.handlers);
2337 compiler_use_next_block(c, except);
2338 for (i = 0; i < n; i++) {
2339 excepthandler_ty handler = asdl_seq_GET(
2340 s->v.TryExcept.handlers, i);
2341 if (!handler->type && i < n-1)
2342 return compiler_error(c, "default 'except:' must be last");
2343 except = compiler_new_block(c);
2344 if (except == NULL)
2345 return 0;
2346 if (handler->type) {
2347 ADDOP(c, DUP_TOP);
2348 VISIT(c, expr, handler->type);
2349 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2350 ADDOP_JREL(c, JUMP_IF_FALSE, except);
2351 ADDOP(c, POP_TOP);
2352 }
2353 ADDOP(c, POP_TOP);
2354 if (handler->name) {
2355 VISIT(c, expr, handler->name);
2356 }
2357 else {
2358 ADDOP(c, POP_TOP);
2359 }
2360 ADDOP(c, POP_TOP);
2361 VISIT_SEQ(c, stmt, handler->body);
2362 ADDOP_JREL(c, JUMP_FORWARD, end);
2363 compiler_use_next_block(c, except);
2364 if (handler->type)
2365 ADDOP(c, POP_TOP);
2366 }
2367 ADDOP(c, END_FINALLY);
2368 compiler_use_next_block(c, orelse);
2369 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2370 compiler_use_next_block(c, end);
2371 return 1;
2372}
2373
2374static int
2375compiler_import_as(struct compiler *c, identifier name, identifier asname)
2376{
2377 /* The IMPORT_NAME opcode was already generated. This function
2378 merely needs to bind the result to a name.
2379
2380 If there is a dot in name, we need to split it and emit a
2381 LOAD_ATTR for each name.
2382 */
2383 const char *src = PyString_AS_STRING(name);
2384 const char *dot = strchr(src, '.');
2385 if (dot) {
2386 /* Consume the base module name to get the first attribute */
2387 src = dot + 1;
2388 while (dot) {
2389 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00002390 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002391 dot = strchr(src, '.');
Armin Rigo31441302005-10-21 12:57:31 +00002392 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002393 dot ? dot - src : strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002394 if (!attr)
2395 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002396 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002397 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002398 src = dot + 1;
2399 }
2400 }
2401 return compiler_nameop(c, asname, Store);
2402}
2403
2404static int
2405compiler_import(struct compiler *c, stmt_ty s)
2406{
2407 /* The Import node stores a module name like a.b.c as a single
2408 string. This is convenient for all cases except
2409 import a.b.c as d
2410 where we need to parse that string to extract the individual
2411 module names.
2412 XXX Perhaps change the representation to make this case simpler?
2413 */
2414 int i, n = asdl_seq_LEN(s->v.Import.names);
2415 for (i = 0; i < n; i++) {
2416 alias_ty alias = asdl_seq_GET(s->v.Import.names, i);
2417 int r;
2418
2419 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2420 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2421
2422 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002423 r = compiler_import_as(c, alias->name, alias->asname);
2424 if (!r)
2425 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002426 }
2427 else {
2428 identifier tmp = alias->name;
2429 const char *base = PyString_AS_STRING(alias->name);
2430 char *dot = strchr(base, '.');
2431 if (dot)
2432 tmp = PyString_FromStringAndSize(base,
2433 dot - base);
2434 r = compiler_nameop(c, tmp, Store);
2435 if (dot) {
2436 Py_DECREF(tmp);
2437 }
2438 if (!r)
2439 return r;
2440 }
2441 }
2442 return 1;
2443}
2444
2445static int
2446compiler_from_import(struct compiler *c, stmt_ty s)
2447{
2448 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002449
2450 PyObject *names = PyTuple_New(n);
2451 if (!names)
2452 return 0;
2453
2454 /* build up the names */
2455 for (i = 0; i < n; i++) {
2456 alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
2457 Py_INCREF(alias->name);
2458 PyTuple_SET_ITEM(names, i, alias->name);
2459 }
2460
2461 if (s->lineno > c->c_future->ff_lineno) {
2462 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
2463 "__future__")) {
2464 Py_DECREF(names);
2465 return compiler_error(c,
2466 "from __future__ imports must occur "
2467 "at the beginning of the file");
2468
2469 }
2470 }
2471
2472 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002473 Py_DECREF(names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002474 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2475 for (i = 0; i < n; i++) {
2476 alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
2477 identifier store_name;
2478
2479 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
2480 assert(n == 1);
2481 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002482 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002483 }
2484
2485 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2486 store_name = alias->name;
2487 if (alias->asname)
2488 store_name = alias->asname;
2489
2490 if (!compiler_nameop(c, store_name, Store)) {
2491 Py_DECREF(names);
2492 return 0;
2493 }
2494 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002495 /* remove imported module */
2496 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002497 return 1;
2498}
2499
2500static int
2501compiler_assert(struct compiler *c, stmt_ty s)
2502{
2503 static PyObject *assertion_error = NULL;
2504 basicblock *end;
2505
2506 if (Py_OptimizeFlag)
2507 return 1;
2508 if (assertion_error == NULL) {
2509 assertion_error = PyString_FromString("AssertionError");
2510 if (assertion_error == NULL)
2511 return 0;
2512 }
2513 VISIT(c, expr, s->v.Assert.test);
2514 end = compiler_new_block(c);
2515 if (end == NULL)
2516 return 0;
2517 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2518 ADDOP(c, POP_TOP);
2519 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2520 if (s->v.Assert.msg) {
2521 VISIT(c, expr, s->v.Assert.msg);
2522 ADDOP_I(c, RAISE_VARARGS, 2);
2523 }
2524 else {
2525 ADDOP_I(c, RAISE_VARARGS, 1);
2526 }
Neal Norwitz51abbc72005-12-18 07:06:23 +00002527 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002528 ADDOP(c, POP_TOP);
2529 return 1;
2530}
2531
2532static int
2533compiler_visit_stmt(struct compiler *c, stmt_ty s)
2534{
2535 int i, n;
2536
2537 c->u->u_lineno = s->lineno;
2538 c->u->u_lineno_set = false;
2539 switch (s->kind) {
2540 case FunctionDef_kind:
2541 return compiler_function(c, s);
2542 case ClassDef_kind:
2543 return compiler_class(c, s);
2544 case Return_kind:
2545 if (c->u->u_ste->ste_type != FunctionBlock)
2546 return compiler_error(c, "'return' outside function");
2547 if (s->v.Return.value) {
2548 if (c->u->u_ste->ste_generator) {
2549 return compiler_error(c,
2550 "'return' with argument inside generator");
2551 }
2552 VISIT(c, expr, s->v.Return.value);
2553 }
2554 else
2555 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2556 ADDOP(c, RETURN_VALUE);
2557 break;
2558 case Delete_kind:
2559 VISIT_SEQ(c, expr, s->v.Delete.targets)
2560 break;
2561 case Assign_kind:
2562 n = asdl_seq_LEN(s->v.Assign.targets);
2563 VISIT(c, expr, s->v.Assign.value);
2564 for (i = 0; i < n; i++) {
2565 if (i < n - 1)
2566 ADDOP(c, DUP_TOP);
2567 VISIT(c, expr,
2568 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2569 }
2570 break;
2571 case AugAssign_kind:
2572 return compiler_augassign(c, s);
2573 case Print_kind:
2574 return compiler_print(c, s);
2575 case For_kind:
2576 return compiler_for(c, s);
2577 case While_kind:
2578 return compiler_while(c, s);
2579 case If_kind:
2580 return compiler_if(c, s);
2581 case Raise_kind:
2582 n = 0;
2583 if (s->v.Raise.type) {
2584 VISIT(c, expr, s->v.Raise.type);
2585 n++;
2586 if (s->v.Raise.inst) {
2587 VISIT(c, expr, s->v.Raise.inst);
2588 n++;
2589 if (s->v.Raise.tback) {
2590 VISIT(c, expr, s->v.Raise.tback);
2591 n++;
2592 }
2593 }
2594 }
2595 ADDOP_I(c, RAISE_VARARGS, n);
2596 break;
2597 case TryExcept_kind:
2598 return compiler_try_except(c, s);
2599 case TryFinally_kind:
2600 return compiler_try_finally(c, s);
2601 case Assert_kind:
2602 return compiler_assert(c, s);
2603 case Import_kind:
2604 return compiler_import(c, s);
2605 case ImportFrom_kind:
2606 return compiler_from_import(c, s);
2607 case Exec_kind:
2608 VISIT(c, expr, s->v.Exec.body);
2609 if (s->v.Exec.globals) {
2610 VISIT(c, expr, s->v.Exec.globals);
2611 if (s->v.Exec.locals) {
2612 VISIT(c, expr, s->v.Exec.locals);
2613 } else {
2614 ADDOP(c, DUP_TOP);
2615 }
2616 } else {
2617 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2618 ADDOP(c, DUP_TOP);
2619 }
2620 ADDOP(c, EXEC_STMT);
2621 break;
2622 case Global_kind:
2623 break;
2624 case Expr_kind:
2625 VISIT(c, expr, s->v.Expr.value);
2626 if (c->c_interactive && c->c_nestlevel <= 1) {
2627 ADDOP(c, PRINT_EXPR);
2628 }
2629 else {
2630 ADDOP(c, POP_TOP);
2631 }
2632 break;
2633 case Pass_kind:
2634 break;
2635 case Break_kind:
2636 if (!c->u->u_nfblocks)
2637 return compiler_error(c, "'break' outside loop");
2638 ADDOP(c, BREAK_LOOP);
2639 break;
2640 case Continue_kind:
2641 return compiler_continue(c);
2642 }
2643 return 1;
2644}
2645
2646static int
2647unaryop(unaryop_ty op)
2648{
2649 switch (op) {
2650 case Invert:
2651 return UNARY_INVERT;
2652 case Not:
2653 return UNARY_NOT;
2654 case UAdd:
2655 return UNARY_POSITIVE;
2656 case USub:
2657 return UNARY_NEGATIVE;
2658 }
2659 return 0;
2660}
2661
2662static int
2663binop(struct compiler *c, operator_ty op)
2664{
2665 switch (op) {
2666 case Add:
2667 return BINARY_ADD;
2668 case Sub:
2669 return BINARY_SUBTRACT;
2670 case Mult:
2671 return BINARY_MULTIPLY;
2672 case Div:
2673 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2674 return BINARY_TRUE_DIVIDE;
2675 else
2676 return BINARY_DIVIDE;
2677 case Mod:
2678 return BINARY_MODULO;
2679 case Pow:
2680 return BINARY_POWER;
2681 case LShift:
2682 return BINARY_LSHIFT;
2683 case RShift:
2684 return BINARY_RSHIFT;
2685 case BitOr:
2686 return BINARY_OR;
2687 case BitXor:
2688 return BINARY_XOR;
2689 case BitAnd:
2690 return BINARY_AND;
2691 case FloorDiv:
2692 return BINARY_FLOOR_DIVIDE;
2693 }
2694 return 0;
2695}
2696
2697static int
2698cmpop(cmpop_ty op)
2699{
2700 switch (op) {
2701 case Eq:
2702 return PyCmp_EQ;
2703 case NotEq:
2704 return PyCmp_NE;
2705 case Lt:
2706 return PyCmp_LT;
2707 case LtE:
2708 return PyCmp_LE;
2709 case Gt:
2710 return PyCmp_GT;
2711 case GtE:
2712 return PyCmp_GE;
2713 case Is:
2714 return PyCmp_IS;
2715 case IsNot:
2716 return PyCmp_IS_NOT;
2717 case In:
2718 return PyCmp_IN;
2719 case NotIn:
2720 return PyCmp_NOT_IN;
2721 }
2722 return PyCmp_BAD;
2723}
2724
2725static int
2726inplace_binop(struct compiler *c, operator_ty op)
2727{
2728 switch (op) {
2729 case Add:
2730 return INPLACE_ADD;
2731 case Sub:
2732 return INPLACE_SUBTRACT;
2733 case Mult:
2734 return INPLACE_MULTIPLY;
2735 case Div:
2736 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2737 return INPLACE_TRUE_DIVIDE;
2738 else
2739 return INPLACE_DIVIDE;
2740 case Mod:
2741 return INPLACE_MODULO;
2742 case Pow:
2743 return INPLACE_POWER;
2744 case LShift:
2745 return INPLACE_LSHIFT;
2746 case RShift:
2747 return INPLACE_RSHIFT;
2748 case BitOr:
2749 return INPLACE_OR;
2750 case BitXor:
2751 return INPLACE_XOR;
2752 case BitAnd:
2753 return INPLACE_AND;
2754 case FloorDiv:
2755 return INPLACE_FLOOR_DIVIDE;
2756 }
Neal Norwitz4737b232005-11-19 23:58:29 +00002757 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002758 "inplace binary op %d should not be possible", op);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002759 return 0;
2760}
2761
2762static int
2763compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2764{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002765 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002766 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2767
2768 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002769 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002770 /* XXX AugStore isn't used anywhere! */
2771
2772 /* First check for assignment to __debug__. Param? */
2773 if ((ctx == Store || ctx == AugStore || ctx == Del)
2774 && !strcmp(PyString_AS_STRING(name), "__debug__")) {
2775 return compiler_error(c, "can not assign to __debug__");
2776 }
2777
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002778 mangled = _Py_Mangle(c->u->u_private, name);
2779 if (!mangled)
2780 return 0;
2781
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002782 op = 0;
2783 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002784 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002785 switch (scope) {
2786 case FREE:
2787 dict = c->u->u_freevars;
2788 optype = OP_DEREF;
2789 break;
2790 case CELL:
2791 dict = c->u->u_cellvars;
2792 optype = OP_DEREF;
2793 break;
2794 case LOCAL:
2795 if (c->u->u_ste->ste_type == FunctionBlock)
2796 optype = OP_FAST;
2797 break;
2798 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002799 if (c->u->u_ste->ste_type == FunctionBlock &&
2800 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002801 optype = OP_GLOBAL;
2802 break;
2803 case GLOBAL_EXPLICIT:
2804 optype = OP_GLOBAL;
2805 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002806 default:
2807 /* scope can be 0 */
2808 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002809 }
2810
2811 /* XXX Leave assert here, but handle __doc__ and the like better */
2812 assert(scope || PyString_AS_STRING(name)[0] == '_');
2813
2814 switch (optype) {
2815 case OP_DEREF:
2816 switch (ctx) {
2817 case Load: op = LOAD_DEREF; break;
2818 case Store: op = STORE_DEREF; break;
2819 case AugLoad:
2820 case AugStore:
2821 break;
2822 case Del:
2823 PyErr_Format(PyExc_SyntaxError,
2824 "can not delete variable '%s' referenced "
2825 "in nested scope",
2826 PyString_AS_STRING(name));
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002827 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002828 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002829 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002830 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002831 PyErr_SetString(PyExc_SystemError,
2832 "param invalid for deref variable");
2833 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834 }
2835 break;
2836 case OP_FAST:
2837 switch (ctx) {
2838 case Load: op = LOAD_FAST; break;
2839 case Store: op = STORE_FAST; break;
2840 case Del: op = DELETE_FAST; break;
2841 case AugLoad:
2842 case AugStore:
2843 break;
2844 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002845 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002846 PyErr_SetString(PyExc_SystemError,
2847 "param invalid for local variable");
2848 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002849 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002850 ADDOP_O(c, op, mangled, varnames);
2851 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002852 return 1;
2853 case OP_GLOBAL:
2854 switch (ctx) {
2855 case Load: op = LOAD_GLOBAL; break;
2856 case Store: op = STORE_GLOBAL; break;
2857 case Del: op = DELETE_GLOBAL; break;
2858 case AugLoad:
2859 case AugStore:
2860 break;
2861 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002862 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002863 PyErr_SetString(PyExc_SystemError,
2864 "param invalid for global variable");
2865 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002866 }
2867 break;
2868 case OP_NAME:
2869 switch (ctx) {
2870 case Load: op = LOAD_NAME; break;
2871 case Store: op = STORE_NAME; break;
2872 case Del: op = DELETE_NAME; break;
2873 case AugLoad:
2874 case AugStore:
2875 break;
2876 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002877 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002878 PyErr_SetString(PyExc_SystemError,
2879 "param invalid for name variable");
2880 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002881 }
2882 break;
2883 }
2884
2885 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002886 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002887 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002888 if (arg < 0)
2889 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002890 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002891}
2892
2893static int
2894compiler_boolop(struct compiler *c, expr_ty e)
2895{
2896 basicblock *end;
2897 int jumpi, i, n;
2898 asdl_seq *s;
2899
2900 assert(e->kind == BoolOp_kind);
2901 if (e->v.BoolOp.op == And)
2902 jumpi = JUMP_IF_FALSE;
2903 else
2904 jumpi = JUMP_IF_TRUE;
2905 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002906 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002907 return 0;
2908 s = e->v.BoolOp.values;
2909 n = asdl_seq_LEN(s) - 1;
2910 for (i = 0; i < n; ++i) {
2911 VISIT(c, expr, asdl_seq_GET(s, i));
2912 ADDOP_JREL(c, jumpi, end);
2913 ADDOP(c, POP_TOP)
2914 }
2915 VISIT(c, expr, asdl_seq_GET(s, n));
2916 compiler_use_next_block(c, end);
2917 return 1;
2918}
2919
2920static int
2921compiler_list(struct compiler *c, expr_ty e)
2922{
2923 int n = asdl_seq_LEN(e->v.List.elts);
2924 if (e->v.List.ctx == Store) {
2925 ADDOP_I(c, UNPACK_SEQUENCE, n);
2926 }
2927 VISIT_SEQ(c, expr, e->v.List.elts);
2928 if (e->v.List.ctx == Load) {
2929 ADDOP_I(c, BUILD_LIST, n);
2930 }
2931 return 1;
2932}
2933
2934static int
2935compiler_tuple(struct compiler *c, expr_ty e)
2936{
2937 int n = asdl_seq_LEN(e->v.Tuple.elts);
2938 if (e->v.Tuple.ctx == Store) {
2939 ADDOP_I(c, UNPACK_SEQUENCE, n);
2940 }
2941 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2942 if (e->v.Tuple.ctx == Load) {
2943 ADDOP_I(c, BUILD_TUPLE, n);
2944 }
2945 return 1;
2946}
2947
2948static int
2949compiler_compare(struct compiler *c, expr_ty e)
2950{
2951 int i, n;
2952 basicblock *cleanup = NULL;
2953
2954 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2955 VISIT(c, expr, e->v.Compare.left);
2956 n = asdl_seq_LEN(e->v.Compare.ops);
2957 assert(n > 0);
2958 if (n > 1) {
2959 cleanup = compiler_new_block(c);
2960 if (cleanup == NULL)
2961 return 0;
2962 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, 0));
2963 }
2964 for (i = 1; i < n; i++) {
2965 ADDOP(c, DUP_TOP);
2966 ADDOP(c, ROT_THREE);
2967 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
2968 ADDOP_I(c, COMPARE_OP,
2969 cmpop((cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i - 1)));
2970 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2971 NEXT_BLOCK(c);
2972 ADDOP(c, POP_TOP);
2973 if (i < (n - 1))
2974 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, i));
2975 }
2976 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, n - 1));
2977 ADDOP_I(c, COMPARE_OP,
2978 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
2979 cmpop((cmpop_ty)asdl_seq_GET(e->v.Compare.ops, n - 1)));
2980 if (n > 1) {
2981 basicblock *end = compiler_new_block(c);
2982 if (end == NULL)
2983 return 0;
2984 ADDOP_JREL(c, JUMP_FORWARD, end);
2985 compiler_use_next_block(c, cleanup);
2986 ADDOP(c, ROT_TWO);
2987 ADDOP(c, POP_TOP);
2988 compiler_use_next_block(c, end);
2989 }
2990 return 1;
2991}
2992
2993static int
2994compiler_call(struct compiler *c, expr_ty e)
2995{
2996 int n, code = 0;
2997
2998 VISIT(c, expr, e->v.Call.func);
2999 n = asdl_seq_LEN(e->v.Call.args);
3000 VISIT_SEQ(c, expr, e->v.Call.args);
3001 if (e->v.Call.keywords) {
3002 VISIT_SEQ(c, keyword, e->v.Call.keywords);
3003 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
3004 }
3005 if (e->v.Call.starargs) {
3006 VISIT(c, expr, e->v.Call.starargs);
3007 code |= 1;
3008 }
3009 if (e->v.Call.kwargs) {
3010 VISIT(c, expr, e->v.Call.kwargs);
3011 code |= 2;
3012 }
3013 switch (code) {
3014 case 0:
3015 ADDOP_I(c, CALL_FUNCTION, n);
3016 break;
3017 case 1:
3018 ADDOP_I(c, CALL_FUNCTION_VAR, n);
3019 break;
3020 case 2:
3021 ADDOP_I(c, CALL_FUNCTION_KW, n);
3022 break;
3023 case 3:
3024 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
3025 break;
3026 }
3027 return 1;
3028}
3029
3030static int
3031compiler_listcomp_generator(struct compiler *c, PyObject *tmpname,
3032 asdl_seq *generators, int gen_index,
3033 expr_ty elt)
3034{
3035 /* generate code for the iterator, then each of the ifs,
3036 and then write to the element */
3037
3038 comprehension_ty l;
3039 basicblock *start, *anchor, *skip, *if_cleanup;
3040 int i, n;
3041
3042 start = compiler_new_block(c);
3043 skip = compiler_new_block(c);
3044 if_cleanup = compiler_new_block(c);
3045 anchor = compiler_new_block(c);
3046
3047 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3048 anchor == NULL)
3049 return 0;
3050
3051 l = asdl_seq_GET(generators, gen_index);
3052 VISIT(c, expr, l->iter);
3053 ADDOP(c, GET_ITER);
3054 compiler_use_next_block(c, start);
3055 ADDOP_JREL(c, FOR_ITER, anchor);
3056 NEXT_BLOCK(c);
3057 VISIT(c, expr, l->target);
3058
3059 /* XXX this needs to be cleaned up...a lot! */
3060 n = asdl_seq_LEN(l->ifs);
3061 for (i = 0; i < n; i++) {
3062 expr_ty e = asdl_seq_GET(l->ifs, i);
3063 VISIT(c, expr, e);
3064 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
3065 NEXT_BLOCK(c);
3066 ADDOP(c, POP_TOP);
3067 }
3068
3069 if (++gen_index < asdl_seq_LEN(generators))
3070 if (!compiler_listcomp_generator(c, tmpname,
3071 generators, gen_index, elt))
3072 return 0;
3073
3074 /* only append after the last for generator */
3075 if (gen_index >= asdl_seq_LEN(generators)) {
3076 if (!compiler_nameop(c, tmpname, Load))
3077 return 0;
3078 VISIT(c, expr, elt);
3079 ADDOP_I(c, CALL_FUNCTION, 1);
3080 ADDOP(c, POP_TOP);
3081
3082 compiler_use_next_block(c, skip);
3083 }
3084 for (i = 0; i < n; i++) {
3085 ADDOP_I(c, JUMP_FORWARD, 1);
3086 if (i == 0)
3087 compiler_use_next_block(c, if_cleanup);
3088 ADDOP(c, POP_TOP);
3089 }
3090 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3091 compiler_use_next_block(c, anchor);
3092 /* delete the append method added to locals */
3093 if (gen_index == 1)
3094 if (!compiler_nameop(c, tmpname, Del))
3095 return 0;
3096
3097 return 1;
3098}
3099
3100static int
3101compiler_listcomp(struct compiler *c, expr_ty e)
3102{
3103 char tmpname[256];
3104 identifier tmp;
3105 int rc = 0;
3106 static identifier append;
3107 asdl_seq *generators = e->v.ListComp.generators;
3108
3109 assert(e->kind == ListComp_kind);
3110 if (!append) {
3111 append = PyString_InternFromString("append");
3112 if (!append)
3113 return 0;
3114 }
3115 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
3116 tmp = PyString_FromString(tmpname);
3117 if (!tmp)
3118 return 0;
3119 ADDOP_I(c, BUILD_LIST, 0);
3120 ADDOP(c, DUP_TOP);
3121 ADDOP_O(c, LOAD_ATTR, append, names);
3122 if (compiler_nameop(c, tmp, Store))
3123 rc = compiler_listcomp_generator(c, tmp, generators, 0,
3124 e->v.ListComp.elt);
3125 Py_DECREF(tmp);
3126 return rc;
3127}
3128
3129static int
3130compiler_genexp_generator(struct compiler *c,
3131 asdl_seq *generators, int gen_index,
3132 expr_ty elt)
3133{
3134 /* generate code for the iterator, then each of the ifs,
3135 and then write to the element */
3136
3137 comprehension_ty ge;
3138 basicblock *start, *anchor, *skip, *if_cleanup, *end;
3139 int i, n;
3140
3141 start = compiler_new_block(c);
3142 skip = compiler_new_block(c);
3143 if_cleanup = compiler_new_block(c);
3144 anchor = compiler_new_block(c);
3145 end = compiler_new_block(c);
3146
3147 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3148 anchor == NULL || end == NULL)
3149 return 0;
3150
3151 ge = asdl_seq_GET(generators, gen_index);
3152 ADDOP_JREL(c, SETUP_LOOP, end);
3153 if (!compiler_push_fblock(c, LOOP, start))
3154 return 0;
3155
3156 if (gen_index == 0) {
3157 /* Receive outermost iter as an implicit argument */
3158 c->u->u_argcount = 1;
3159 ADDOP_I(c, LOAD_FAST, 0);
3160 }
3161 else {
3162 /* Sub-iter - calculate on the fly */
3163 VISIT(c, expr, ge->iter);
3164 ADDOP(c, GET_ITER);
3165 }
3166 compiler_use_next_block(c, start);
3167 ADDOP_JREL(c, FOR_ITER, anchor);
3168 NEXT_BLOCK(c);
3169 VISIT(c, expr, ge->target);
3170
3171 /* XXX this needs to be cleaned up...a lot! */
3172 n = asdl_seq_LEN(ge->ifs);
3173 for (i = 0; i < n; i++) {
3174 expr_ty e = asdl_seq_GET(ge->ifs, i);
3175 VISIT(c, expr, e);
3176 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
3177 NEXT_BLOCK(c);
3178 ADDOP(c, POP_TOP);
3179 }
3180
3181 if (++gen_index < asdl_seq_LEN(generators))
3182 if (!compiler_genexp_generator(c, generators, gen_index, elt))
3183 return 0;
3184
3185 /* only append after the last 'for' generator */
3186 if (gen_index >= asdl_seq_LEN(generators)) {
3187 VISIT(c, expr, elt);
3188 ADDOP(c, YIELD_VALUE);
3189 ADDOP(c, POP_TOP);
3190
3191 compiler_use_next_block(c, skip);
3192 }
3193 for (i = 0; i < n; i++) {
3194 ADDOP_I(c, JUMP_FORWARD, 1);
3195 if (i == 0)
3196 compiler_use_next_block(c, if_cleanup);
3197
3198 ADDOP(c, POP_TOP);
3199 }
3200 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3201 compiler_use_next_block(c, anchor);
3202 ADDOP(c, POP_BLOCK);
3203 compiler_pop_fblock(c, LOOP, start);
3204 compiler_use_next_block(c, end);
3205
3206 return 1;
3207}
3208
3209static int
3210compiler_genexp(struct compiler *c, expr_ty e)
3211{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00003212 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003213 PyCodeObject *co;
3214 expr_ty outermost_iter = ((comprehension_ty)
3215 (asdl_seq_GET(e->v.GeneratorExp.generators,
3216 0)))->iter;
3217
Nick Coghlan944d3eb2005-11-16 12:46:55 +00003218 if (!name) {
3219 name = PyString_FromString("<genexpr>");
3220 if (!name)
3221 return 0;
3222 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003223
3224 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
3225 return 0;
3226 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
3227 e->v.GeneratorExp.elt);
3228 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00003229 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003230 if (co == NULL)
3231 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003232
3233 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00003234 Py_DECREF(co);
3235
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003236 VISIT(c, expr, outermost_iter);
3237 ADDOP(c, GET_ITER);
3238 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003239
3240 return 1;
3241}
3242
3243static int
3244compiler_visit_keyword(struct compiler *c, keyword_ty k)
3245{
3246 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3247 VISIT(c, expr, k->value);
3248 return 1;
3249}
3250
3251/* Test whether expression is constant. For constants, report
3252 whether they are true or false.
3253
3254 Return values: 1 for true, 0 for false, -1 for non-constant.
3255 */
3256
3257static int
3258expr_constant(expr_ty e)
3259{
3260 switch (e->kind) {
3261 case Num_kind:
3262 return PyObject_IsTrue(e->v.Num.n);
3263 case Str_kind:
3264 return PyObject_IsTrue(e->v.Str.s);
3265 default:
3266 return -1;
3267 }
3268}
3269
3270static int
3271compiler_visit_expr(struct compiler *c, expr_ty e)
3272{
3273 int i, n;
3274
3275 if (e->lineno > c->u->u_lineno) {
3276 c->u->u_lineno = e->lineno;
3277 c->u->u_lineno_set = false;
3278 }
3279 switch (e->kind) {
3280 case BoolOp_kind:
3281 return compiler_boolop(c, e);
3282 case BinOp_kind:
3283 VISIT(c, expr, e->v.BinOp.left);
3284 VISIT(c, expr, e->v.BinOp.right);
3285 ADDOP(c, binop(c, e->v.BinOp.op));
3286 break;
3287 case UnaryOp_kind:
3288 VISIT(c, expr, e->v.UnaryOp.operand);
3289 ADDOP(c, unaryop(e->v.UnaryOp.op));
3290 break;
3291 case Lambda_kind:
3292 return compiler_lambda(c, e);
3293 case Dict_kind:
3294 /* XXX get rid of arg? */
3295 ADDOP_I(c, BUILD_MAP, 0);
3296 n = asdl_seq_LEN(e->v.Dict.values);
3297 /* We must arrange things just right for STORE_SUBSCR.
3298 It wants the stack to look like (value) (dict) (key) */
3299 for (i = 0; i < n; i++) {
3300 ADDOP(c, DUP_TOP);
3301 VISIT(c, expr, asdl_seq_GET(e->v.Dict.values, i));
3302 ADDOP(c, ROT_TWO);
3303 VISIT(c, expr, asdl_seq_GET(e->v.Dict.keys, i));
3304 ADDOP(c, STORE_SUBSCR);
3305 }
3306 break;
3307 case ListComp_kind:
3308 return compiler_listcomp(c, e);
3309 case GeneratorExp_kind:
3310 return compiler_genexp(c, e);
3311 case Yield_kind:
3312 if (c->u->u_ste->ste_type != FunctionBlock)
3313 return compiler_error(c, "'yield' outside function");
3314 /*
3315 for (i = 0; i < c->u->u_nfblocks; i++) {
3316 if (c->u->u_fblock[i].fb_type == FINALLY_TRY)
3317 return compiler_error(
3318 c, "'yield' not allowed in a 'try' "
3319 "block with a 'finally' clause");
3320 }
3321 */
3322 if (e->v.Yield.value) {
3323 VISIT(c, expr, e->v.Yield.value);
3324 }
3325 else {
3326 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3327 }
3328 ADDOP(c, YIELD_VALUE);
3329 break;
3330 case Compare_kind:
3331 return compiler_compare(c, e);
3332 case Call_kind:
3333 return compiler_call(c, e);
3334 case Repr_kind:
3335 VISIT(c, expr, e->v.Repr.value);
3336 ADDOP(c, UNARY_CONVERT);
3337 break;
3338 case Num_kind:
3339 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3340 break;
3341 case Str_kind:
3342 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3343 break;
3344 /* The following exprs can be assignment targets. */
3345 case Attribute_kind:
3346 if (e->v.Attribute.ctx != AugStore)
3347 VISIT(c, expr, e->v.Attribute.value);
3348 switch (e->v.Attribute.ctx) {
3349 case AugLoad:
3350 ADDOP(c, DUP_TOP);
3351 /* Fall through to load */
3352 case Load:
3353 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3354 break;
3355 case AugStore:
3356 ADDOP(c, ROT_TWO);
3357 /* Fall through to save */
3358 case Store:
3359 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3360 break;
3361 case Del:
3362 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3363 break;
3364 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003365 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003366 PyErr_SetString(PyExc_SystemError,
3367 "param invalid in attribute expression");
3368 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003369 }
3370 break;
3371 case Subscript_kind:
3372 switch (e->v.Subscript.ctx) {
3373 case AugLoad:
3374 VISIT(c, expr, e->v.Subscript.value);
3375 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3376 break;
3377 case Load:
3378 VISIT(c, expr, e->v.Subscript.value);
3379 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3380 break;
3381 case AugStore:
3382 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3383 break;
3384 case Store:
3385 VISIT(c, expr, e->v.Subscript.value);
3386 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3387 break;
3388 case Del:
3389 VISIT(c, expr, e->v.Subscript.value);
3390 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3391 break;
3392 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003393 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003394 PyErr_SetString(PyExc_SystemError,
3395 "param invalid in subscript expression");
3396 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003397 }
3398 break;
3399 case Name_kind:
3400 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3401 /* child nodes of List and Tuple will have expr_context set */
3402 case List_kind:
3403 return compiler_list(c, e);
3404 case Tuple_kind:
3405 return compiler_tuple(c, e);
3406 }
3407 return 1;
3408}
3409
3410static int
3411compiler_augassign(struct compiler *c, stmt_ty s)
3412{
3413 expr_ty e = s->v.AugAssign.target;
3414 expr_ty auge;
3415
3416 assert(s->kind == AugAssign_kind);
3417
3418 switch (e->kind) {
3419 case Attribute_kind:
3420 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003421 AugLoad, e->lineno, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003422 if (auge == NULL)
3423 return 0;
3424 VISIT(c, expr, auge);
3425 VISIT(c, expr, s->v.AugAssign.value);
3426 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3427 auge->v.Attribute.ctx = AugStore;
3428 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003429 break;
3430 case Subscript_kind:
3431 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003432 AugLoad, e->lineno, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003433 if (auge == NULL)
3434 return 0;
3435 VISIT(c, expr, auge);
3436 VISIT(c, expr, s->v.AugAssign.value);
3437 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3438 auge->v.Subscript.ctx = AugStore;
3439 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003440 break;
3441 case Name_kind:
3442 VISIT(c, expr, s->v.AugAssign.target);
3443 VISIT(c, expr, s->v.AugAssign.value);
3444 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3445 return compiler_nameop(c, e->v.Name.id, Store);
3446 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003447 PyErr_Format(PyExc_SystemError,
3448 "invalid node type (%d) for augmented assignment",
3449 e->kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003450 return 0;
3451 }
3452 return 1;
3453}
3454
3455static int
3456compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3457{
3458 struct fblockinfo *f;
3459 if (c->u->u_nfblocks >= CO_MAXBLOCKS)
3460 return 0;
3461 f = &c->u->u_fblock[c->u->u_nfblocks++];
3462 f->fb_type = t;
3463 f->fb_block = b;
3464 return 1;
3465}
3466
3467static void
3468compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3469{
3470 struct compiler_unit *u = c->u;
3471 assert(u->u_nfblocks > 0);
3472 u->u_nfblocks--;
3473 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3474 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3475}
3476
3477/* Raises a SyntaxError and returns 0.
3478 If something goes wrong, a different exception may be raised.
3479*/
3480
3481static int
3482compiler_error(struct compiler *c, const char *errstr)
3483{
3484 PyObject *loc;
3485 PyObject *u = NULL, *v = NULL;
3486
3487 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3488 if (!loc) {
3489 Py_INCREF(Py_None);
3490 loc = Py_None;
3491 }
3492 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3493 Py_None, loc);
3494 if (!u)
3495 goto exit;
3496 v = Py_BuildValue("(zO)", errstr, u);
3497 if (!v)
3498 goto exit;
3499 PyErr_SetObject(PyExc_SyntaxError, v);
3500 exit:
3501 Py_DECREF(loc);
3502 Py_XDECREF(u);
3503 Py_XDECREF(v);
3504 return 0;
3505}
3506
3507static int
3508compiler_handle_subscr(struct compiler *c, const char *kind,
3509 expr_context_ty ctx)
3510{
3511 int op = 0;
3512
3513 /* XXX this code is duplicated */
3514 switch (ctx) {
3515 case AugLoad: /* fall through to Load */
3516 case Load: op = BINARY_SUBSCR; break;
3517 case AugStore:/* fall through to Store */
3518 case Store: op = STORE_SUBSCR; break;
3519 case Del: op = DELETE_SUBSCR; break;
3520 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003521 PyErr_Format(PyExc_SystemError,
3522 "invalid %s kind %d in subscript\n",
3523 kind, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003524 return 0;
3525 }
3526 if (ctx == AugLoad) {
3527 ADDOP_I(c, DUP_TOPX, 2);
3528 }
3529 else if (ctx == AugStore) {
3530 ADDOP(c, ROT_THREE);
3531 }
3532 ADDOP(c, op);
3533 return 1;
3534}
3535
3536static int
3537compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3538{
3539 int n = 2;
3540 assert(s->kind == Slice_kind);
3541
3542 /* only handles the cases where BUILD_SLICE is emitted */
3543 if (s->v.Slice.lower) {
3544 VISIT(c, expr, s->v.Slice.lower);
3545 }
3546 else {
3547 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3548 }
3549
3550 if (s->v.Slice.upper) {
3551 VISIT(c, expr, s->v.Slice.upper);
3552 }
3553 else {
3554 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3555 }
3556
3557 if (s->v.Slice.step) {
3558 n++;
3559 VISIT(c, expr, s->v.Slice.step);
3560 }
3561 ADDOP_I(c, BUILD_SLICE, n);
3562 return 1;
3563}
3564
3565static int
3566compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3567{
3568 int op = 0, slice_offset = 0, stack_count = 0;
3569
3570 assert(s->v.Slice.step == NULL);
3571 if (s->v.Slice.lower) {
3572 slice_offset++;
3573 stack_count++;
3574 if (ctx != AugStore)
3575 VISIT(c, expr, s->v.Slice.lower);
3576 }
3577 if (s->v.Slice.upper) {
3578 slice_offset += 2;
3579 stack_count++;
3580 if (ctx != AugStore)
3581 VISIT(c, expr, s->v.Slice.upper);
3582 }
3583
3584 if (ctx == AugLoad) {
3585 switch (stack_count) {
3586 case 0: ADDOP(c, DUP_TOP); break;
3587 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3588 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3589 }
3590 }
3591 else if (ctx == AugStore) {
3592 switch (stack_count) {
3593 case 0: ADDOP(c, ROT_TWO); break;
3594 case 1: ADDOP(c, ROT_THREE); break;
3595 case 2: ADDOP(c, ROT_FOUR); break;
3596 }
3597 }
3598
3599 switch (ctx) {
3600 case AugLoad: /* fall through to Load */
3601 case Load: op = SLICE; break;
3602 case AugStore:/* fall through to Store */
3603 case Store: op = STORE_SLICE; break;
3604 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003605 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003606 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003607 PyErr_SetString(PyExc_SystemError,
3608 "param invalid in simple slice");
3609 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003610 }
3611
3612 ADDOP(c, op + slice_offset);
3613 return 1;
3614}
3615
3616static int
3617compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3618 expr_context_ty ctx)
3619{
3620 switch (s->kind) {
3621 case Ellipsis_kind:
3622 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3623 break;
3624 case Slice_kind:
3625 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003626 case Index_kind:
3627 VISIT(c, expr, s->v.Index.value);
3628 break;
3629 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003630 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003631 PyErr_SetString(PyExc_SystemError,
3632 "extended slice invalid in nested slice");
3633 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003634 }
3635 return 1;
3636}
3637
3638
3639static int
3640compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3641{
3642 switch (s->kind) {
3643 case Ellipsis_kind:
3644 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3645 break;
3646 case Slice_kind:
3647 if (!s->v.Slice.step)
3648 return compiler_simple_slice(c, s, ctx);
3649 if (!compiler_slice(c, s, ctx))
3650 return 0;
3651 if (ctx == AugLoad) {
3652 ADDOP_I(c, DUP_TOPX, 2);
3653 }
3654 else if (ctx == AugStore) {
3655 ADDOP(c, ROT_THREE);
3656 }
3657 return compiler_handle_subscr(c, "slice", ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003658 case ExtSlice_kind: {
3659 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3660 for (i = 0; i < n; i++) {
3661 slice_ty sub = asdl_seq_GET(s->v.ExtSlice.dims, i);
3662 if (!compiler_visit_nested_slice(c, sub, ctx))
3663 return 0;
3664 }
3665 ADDOP_I(c, BUILD_TUPLE, n);
3666 return compiler_handle_subscr(c, "extended slice", ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003667 }
3668 case Index_kind:
3669 if (ctx != AugStore)
3670 VISIT(c, expr, s->v.Index.value);
3671 return compiler_handle_subscr(c, "index", ctx);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003672 default:
3673 PyErr_Format(PyExc_SystemError,
3674 "invalid slice %d", s->kind);
3675 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003676 }
3677 return 1;
3678}
3679
3680/* do depth-first search of basic block graph, starting with block.
3681 post records the block indices in post-order.
3682
3683 XXX must handle implicit jumps from one block to next
3684*/
3685
3686static void
3687dfs(struct compiler *c, basicblock *b, struct assembler *a)
3688{
3689 int i;
3690 struct instr *instr = NULL;
3691
3692 if (b->b_seen)
3693 return;
3694 b->b_seen = 1;
3695 if (b->b_next != NULL)
3696 dfs(c, b->b_next, a);
3697 for (i = 0; i < b->b_iused; i++) {
3698 instr = &b->b_instr[i];
3699 if (instr->i_jrel || instr->i_jabs)
3700 dfs(c, instr->i_target, a);
3701 }
3702 a->a_postorder[a->a_nblocks++] = b;
3703}
3704
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003705static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003706stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3707{
3708 int i;
3709 struct instr *instr;
3710 if (b->b_seen || b->b_startdepth >= depth)
3711 return maxdepth;
3712 b->b_seen = 1;
3713 b->b_startdepth = depth;
3714 for (i = 0; i < b->b_iused; i++) {
3715 instr = &b->b_instr[i];
3716 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3717 if (depth > maxdepth)
3718 maxdepth = depth;
3719 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3720 if (instr->i_jrel || instr->i_jabs) {
3721 maxdepth = stackdepth_walk(c, instr->i_target,
3722 depth, maxdepth);
3723 if (instr->i_opcode == JUMP_ABSOLUTE ||
3724 instr->i_opcode == JUMP_FORWARD) {
3725 goto out; /* remaining code is dead */
3726 }
3727 }
3728 }
3729 if (b->b_next)
3730 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3731out:
3732 b->b_seen = 0;
3733 return maxdepth;
3734}
3735
3736/* Find the flow path that needs the largest stack. We assume that
3737 * cycles in the flow graph have no net effect on the stack depth.
3738 */
3739static int
3740stackdepth(struct compiler *c)
3741{
3742 basicblock *b, *entryblock;
3743 entryblock = NULL;
3744 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3745 b->b_seen = 0;
3746 b->b_startdepth = INT_MIN;
3747 entryblock = b;
3748 }
3749 return stackdepth_walk(c, entryblock, 0, 0);
3750}
3751
3752static int
3753assemble_init(struct assembler *a, int nblocks, int firstlineno)
3754{
3755 memset(a, 0, sizeof(struct assembler));
3756 a->a_lineno = firstlineno;
3757 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3758 if (!a->a_bytecode)
3759 return 0;
3760 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3761 if (!a->a_lnotab)
3762 return 0;
3763 a->a_postorder = (basicblock **)PyObject_Malloc(
3764 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003765 if (!a->a_postorder) {
3766 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003767 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003768 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003769 return 1;
3770}
3771
3772static void
3773assemble_free(struct assembler *a)
3774{
3775 Py_XDECREF(a->a_bytecode);
3776 Py_XDECREF(a->a_lnotab);
3777 if (a->a_postorder)
3778 PyObject_Free(a->a_postorder);
3779}
3780
3781/* Return the size of a basic block in bytes. */
3782
3783static int
3784instrsize(struct instr *instr)
3785{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003786 if (!instr->i_hasarg)
3787 return 1;
3788 if (instr->i_oparg > 0xffff)
3789 return 6;
3790 return 3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003791}
3792
3793static int
3794blocksize(basicblock *b)
3795{
3796 int i;
3797 int size = 0;
3798
3799 for (i = 0; i < b->b_iused; i++)
3800 size += instrsize(&b->b_instr[i]);
3801 return size;
3802}
3803
3804/* All about a_lnotab.
3805
3806c_lnotab is an array of unsigned bytes disguised as a Python string.
3807It is used to map bytecode offsets to source code line #s (when needed
3808for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003809
Tim Peters2a7f3842001-06-09 09:26:21 +00003810The array is conceptually a list of
3811 (bytecode offset increment, line number increment)
3812pairs. The details are important and delicate, best illustrated by example:
3813
3814 byte code offset source code line number
3815 0 1
3816 6 2
3817 50 7
3818 350 307
3819 361 308
3820
3821The first trick is that these numbers aren't stored, only the increments
3822from one row to the next (this doesn't really work, but it's a start):
3823
3824 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3825
3826The second trick is that an unsigned byte can't hold negative values, or
3827values larger than 255, so (a) there's a deep assumption that byte code
3828offsets and their corresponding line #s both increase monotonically, and (b)
3829if at least one column jumps by more than 255 from one row to the next, more
3830than one pair is written to the table. In case #b, there's no way to know
3831from looking at the table later how many were written. That's the delicate
3832part. A user of c_lnotab desiring to find the source line number
3833corresponding to a bytecode address A should do something like this
3834
3835 lineno = addr = 0
3836 for addr_incr, line_incr in c_lnotab:
3837 addr += addr_incr
3838 if addr > A:
3839 return lineno
3840 lineno += line_incr
3841
3842In order for this to work, when the addr field increments by more than 255,
3843the line # increment in each pair generated must be 0 until the remaining addr
3844increment is < 256. So, in the example above, com_set_lineno should not (as
3845was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
3846255, 0, 45, 255, 0, 45.
3847*/
3848
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003849static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003850assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003851{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003852 int d_bytecode, d_lineno;
3853 int len;
3854 char *lnotab;
3855
3856 d_bytecode = a->a_offset - a->a_lineno_off;
3857 d_lineno = i->i_lineno - a->a_lineno;
3858
3859 assert(d_bytecode >= 0);
3860 assert(d_lineno >= 0);
3861
3862 if (d_lineno == 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003863 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003864
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003865 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003866 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003867 nbytes = a->a_lnotab_off + 2 * ncodes;
3868 len = PyString_GET_SIZE(a->a_lnotab);
3869 if (nbytes >= len) {
3870 if (len * 2 < nbytes)
3871 len = nbytes;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003872 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003873 len *= 2;
3874 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3875 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003876 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003877 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003878 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003879 *lnotab++ = 255;
3880 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003881 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003882 d_bytecode -= ncodes * 255;
3883 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003884 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003885 assert(d_bytecode <= 255);
3886 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003887 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003888 nbytes = a->a_lnotab_off + 2 * ncodes;
3889 len = PyString_GET_SIZE(a->a_lnotab);
3890 if (nbytes >= len) {
3891 if (len * 2 < nbytes)
3892 len = nbytes;
Guido van Rossum635abd21997-01-06 22:56:52 +00003893 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003894 len *= 2;
3895 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3896 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003897 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003898 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3899 *lnotab++ = 255;
3900 *lnotab++ = d_bytecode;
3901 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003902 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003903 *lnotab++ = 255;
3904 *lnotab++ = 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003905 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003906 d_lineno -= ncodes * 255;
3907 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003908 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003909
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003910 len = PyString_GET_SIZE(a->a_lnotab);
3911 if (a->a_lnotab_off + 2 >= len) {
3912 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003913 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003914 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003915 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003916
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003917 a->a_lnotab_off += 2;
3918 if (d_bytecode) {
3919 *lnotab++ = d_bytecode;
3920 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003921 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003922 else { /* First line of a block; def stmt, etc. */
3923 *lnotab++ = 0;
3924 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003925 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003926 a->a_lineno = i->i_lineno;
3927 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003928 return 1;
3929}
3930
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003931/* assemble_emit()
3932 Extend the bytecode with a new instruction.
3933 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003934*/
3935
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003936static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003937assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003938{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003939 int size, arg = 0, ext = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003940 int len = PyString_GET_SIZE(a->a_bytecode);
3941 char *code;
3942
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003943 size = instrsize(i);
3944 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003945 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003946 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003947 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003948 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003949 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003950 if (a->a_offset + size >= len) {
3951 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003952 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003953 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003954 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3955 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003956 if (size == 6) {
3957 assert(i->i_hasarg);
3958 *code++ = (char)EXTENDED_ARG;
3959 *code++ = ext & 0xff;
3960 *code++ = ext >> 8;
3961 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003962 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003963 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003964 if (i->i_hasarg) {
3965 assert(size == 3 || size == 6);
3966 *code++ = arg & 0xff;
3967 *code++ = arg >> 8;
3968 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003969 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003970}
3971
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003972static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003973assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003974{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003975 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003976 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003977 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003978
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003979 /* Compute the size of each block and fixup jump args.
3980 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003981start:
3982 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003983 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003984 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003985 bsize = blocksize(b);
3986 b->b_offset = totsize;
3987 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003988 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003989 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003990 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3991 bsize = b->b_offset;
3992 for (i = 0; i < b->b_iused; i++) {
3993 struct instr *instr = &b->b_instr[i];
3994 /* Relative jumps are computed relative to
3995 the instruction pointer after fetching
3996 the jump instruction.
3997 */
3998 bsize += instrsize(instr);
3999 if (instr->i_jabs)
4000 instr->i_oparg = instr->i_target->b_offset;
4001 else if (instr->i_jrel) {
4002 int delta = instr->i_target->b_offset - bsize;
4003 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004004 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00004005 else
4006 continue;
4007 if (instr->i_oparg > 0xffff)
4008 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004009 }
4010 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00004011
4012 /* XXX: This is an awful hack that could hurt performance, but
4013 on the bright side it should work until we come up
4014 with a better solution.
4015
4016 In the meantime, should the goto be dropped in favor
4017 of a loop?
4018
4019 The issue is that in the first loop blocksize() is called
4020 which calls instrsize() which requires i_oparg be set
4021 appropriately. There is a bootstrap problem because
4022 i_oparg is calculated in the second loop above.
4023
4024 So we loop until we stop seeing new EXTENDED_ARGs.
4025 The only EXTENDED_ARGs that could be popping up are
4026 ones in jump instructions. So this should converge
4027 fairly quickly.
4028 */
4029 if (last_extended_arg_count != extended_arg_count) {
4030 last_extended_arg_count = extended_arg_count;
4031 goto start;
4032 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004033}
4034
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004035static PyObject *
4036dict_keys_inorder(PyObject *dict, int offset)
4037{
4038 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004039 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004040
4041 tuple = PyTuple_New(size);
4042 if (tuple == NULL)
4043 return NULL;
4044 while (PyDict_Next(dict, &pos, &k, &v)) {
4045 i = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004046 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004047 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004048 assert((i - offset) < size);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004049 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004050 PyTuple_SET_ITEM(tuple, i - offset, k);
4051 }
4052 return tuple;
4053}
4054
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004055static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004056compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004057{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004058 PySTEntryObject *ste = c->u->u_ste;
4059 int flags = 0, n;
4060 if (ste->ste_type != ModuleBlock)
4061 flags |= CO_NEWLOCALS;
4062 if (ste->ste_type == FunctionBlock) {
4063 if (!ste->ste_unoptimized)
4064 flags |= CO_OPTIMIZED;
4065 if (ste->ste_nested)
4066 flags |= CO_NESTED;
4067 if (ste->ste_generator)
4068 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004069 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004070 if (ste->ste_varargs)
4071 flags |= CO_VARARGS;
4072 if (ste->ste_varkeywords)
4073 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00004074 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004075 flags |= CO_GENERATOR;
4076 if (c->c_flags->cf_flags & CO_FUTURE_DIVISION)
4077 flags |= CO_FUTURE_DIVISION;
4078 n = PyDict_Size(c->u->u_freevars);
4079 if (n < 0)
4080 return -1;
4081 if (n == 0) {
4082 n = PyDict_Size(c->u->u_cellvars);
4083 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004084 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004085 if (n == 0) {
4086 flags |= CO_NOFREE;
4087 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004088 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004089
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004090 return flags;
4091}
4092
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004093static PyCodeObject *
4094makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004095{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004096 PyObject *tmp;
4097 PyCodeObject *co = NULL;
4098 PyObject *consts = NULL;
4099 PyObject *names = NULL;
4100 PyObject *varnames = NULL;
4101 PyObject *filename = NULL;
4102 PyObject *name = NULL;
4103 PyObject *freevars = NULL;
4104 PyObject *cellvars = NULL;
4105 PyObject *bytecode = NULL;
4106 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004107
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004108 tmp = dict_keys_inorder(c->u->u_consts, 0);
4109 if (!tmp)
4110 goto error;
4111 consts = PySequence_List(tmp); /* optimize_code requires a list */
4112 Py_DECREF(tmp);
4113
4114 names = dict_keys_inorder(c->u->u_names, 0);
4115 varnames = dict_keys_inorder(c->u->u_varnames, 0);
4116 if (!consts || !names || !varnames)
4117 goto error;
4118
4119 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4120 if (!cellvars)
4121 goto error;
4122 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4123 if (!freevars)
4124 goto error;
4125 filename = PyString_FromString(c->c_filename);
4126 if (!filename)
4127 goto error;
4128
4129 nlocals = PyDict_Size(c->u->u_varnames);
4130 flags = compute_code_flags(c);
4131 if (flags < 0)
4132 goto error;
4133
4134 bytecode = optimize_code(a->a_bytecode, consts, names, a->a_lnotab);
4135 if (!bytecode)
4136 goto error;
4137
4138 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4139 if (!tmp)
4140 goto error;
4141 Py_DECREF(consts);
4142 consts = tmp;
4143
4144 co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags,
4145 bytecode, consts, names, varnames,
4146 freevars, cellvars,
4147 filename, c->u->u_name,
4148 c->u->u_firstlineno,
4149 a->a_lnotab);
4150 error:
4151 Py_XDECREF(consts);
4152 Py_XDECREF(names);
4153 Py_XDECREF(varnames);
4154 Py_XDECREF(filename);
4155 Py_XDECREF(name);
4156 Py_XDECREF(freevars);
4157 Py_XDECREF(cellvars);
4158 Py_XDECREF(bytecode);
4159 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004160}
4161
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004162static PyCodeObject *
4163assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004164{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004165 basicblock *b, *entryblock;
4166 struct assembler a;
4167 int i, j, nblocks;
4168 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004169
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004170 /* Make sure every block that falls off the end returns None.
4171 XXX NEXT_BLOCK() isn't quite right, because if the last
4172 block ends with a jump or return b_next shouldn't set.
4173 */
4174 if (!c->u->u_curblock->b_return) {
4175 NEXT_BLOCK(c);
4176 if (addNone)
4177 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4178 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004179 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004180
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004181 nblocks = 0;
4182 entryblock = NULL;
4183 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4184 nblocks++;
4185 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004186 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004187
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004188 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4189 goto error;
4190 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004191
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004192 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004193 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004194
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004195 /* Emit code in reverse postorder from dfs. */
4196 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00004197 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004198 for (j = 0; j < b->b_iused; j++)
4199 if (!assemble_emit(&a, &b->b_instr[j]))
4200 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004201 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004202
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004203 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4204 goto error;
4205 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
4206 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004207
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004208 co = makecode(c, &a);
4209 error:
4210 assemble_free(&a);
4211 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004212}