blob: 84f52e659ab6970af695df903009b3c02600cc95 [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 {
54 int i_jabs : 1;
55 int i_jrel : 1;
56 int i_hasarg : 1;
57 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. */
77 int b_seen : 1;
78 /* b_return is true if a RETURN_VALUE opcode is inserted. */
79 int b_return : 1;
80 /* 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);
174static void compiler_use_block(struct compiler *, basicblock *);
175static basicblock *compiler_use_new_block(struct compiler *);
176static int compiler_error(struct compiler *, const char *);
177static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
178
179static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
180static int compiler_visit_stmt(struct compiler *, stmt_ty);
181static int compiler_visit_keyword(struct compiler *, keyword_ty);
182static int compiler_visit_expr(struct compiler *, expr_ty);
183static int compiler_augassign(struct compiler *, stmt_ty);
184static int compiler_visit_slice(struct compiler *, slice_ty,
185 expr_context_ty);
186
187static int compiler_push_fblock(struct compiler *, enum fblocktype,
188 basicblock *);
189static void compiler_pop_fblock(struct compiler *, enum fblocktype,
190 basicblock *);
191
192static int inplace_binop(struct compiler *, operator_ty);
193static int expr_constant(expr_ty e);
194
195static PyCodeObject *assemble(struct compiler *, int addNone);
196static PyObject *__doc__;
197
198PyObject *
199_Py_Mangle(PyObject *private, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000200{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000201 /* Name mangling: __private becomes _classname__private.
202 This is independent from how the name is used. */
203 const char *p, *name = PyString_AsString(ident);
204 char *buffer;
205 size_t nlen, plen;
206 if (private == NULL || name == NULL || name[0] != '_' || name[1] != '_') {
207 Py_INCREF(ident);
208 return ident;
209 }
210 p = PyString_AsString(private);
211 nlen = strlen(name);
212 if (name[nlen-1] == '_' && name[nlen-2] == '_') {
213 Py_INCREF(ident);
214 return ident; /* Don't mangle __whatever__ */
215 }
216 /* Strip leading underscores from class name */
217 while (*p == '_')
218 p++;
219 if (*p == '\0') {
220 Py_INCREF(ident);
221 return ident; /* Don't mangle if class is just underscores */
222 }
223 plen = strlen(p);
224 ident = PyString_FromStringAndSize(NULL, 1 + nlen + plen);
225 if (!ident)
226 return 0;
227 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
228 buffer = PyString_AS_STRING(ident);
229 buffer[0] = '_';
230 strncpy(buffer+1, p, plen);
231 strcpy(buffer+1+plen, name);
232 return ident;
Michael W. Hudson60934622004-08-12 17:56:29 +0000233}
234
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000235static int
236compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000237{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000238 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000239
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000240 c->c_stack = PyList_New(0);
241 if (!c->c_stack)
242 return 0;
243
244 return 1;
245}
246
247PyCodeObject *
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000248PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
249 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000250{
251 struct compiler c;
252 PyCodeObject *co = NULL;
253 PyCompilerFlags local_flags;
254 int merged;
255
256 if (!__doc__) {
257 __doc__ = PyString_InternFromString("__doc__");
258 if (!__doc__)
259 goto error;
260 }
261
262 if (!compiler_init(&c))
263 goto error;
264 c.c_filename = filename;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000265 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000266 c.c_future = PyFuture_FromAST(mod, filename);
267 if (c.c_future == NULL)
268 goto error;
269 if (!flags) {
270 local_flags.cf_flags = 0;
271 flags = &local_flags;
272 }
273 merged = c.c_future->ff_features | flags->cf_flags;
274 c.c_future->ff_features = merged;
275 flags->cf_flags = merged;
276 c.c_flags = flags;
277 c.c_nestlevel = 0;
278
279 c.c_st = PySymtable_Build(mod, filename, c.c_future);
280 if (c.c_st == NULL) {
281 if (!PyErr_Occurred())
282 PyErr_SetString(PyExc_SystemError, "no symtable");
283 goto error;
284 }
285
286 /* XXX initialize to NULL for now, need to handle */
287 c.c_encoding = NULL;
288
289 co = compiler_mod(&c, mod);
290
291 error:
292 compiler_free(&c);
293 return co;
294}
295
296PyCodeObject *
297PyNode_Compile(struct _node *n, const char *filename)
298{
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000299 PyCodeObject *co = NULL;
300 PyArena *arena;
301 arena = PyArena_New();
302 mod_ty mod = PyAST_FromNode(n, NULL, filename, arena);
303 if (mod)
304 co = PyAST_Compile(mod, filename, NULL, arena);
305 PyArena_Free(arena);
Raymond Hettinger37a724d2003-09-15 21:43:16 +0000306 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000307}
308
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000309static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000310compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000311{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000312 if (c->c_st)
313 PySymtable_Free(c->c_st);
314 if (c->c_future)
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000315 PyMem_Free(c->c_future);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000316 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000317}
318
Guido van Rossum79f25d91997-04-29 20:08:16 +0000319static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000320list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000321{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000322 int i, n;
323 PyObject *v, *k, *dict = PyDict_New();
Guido van Rossumd076c731998-10-07 19:42:25 +0000324
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000325 n = PyList_Size(list);
326 for (i = 0; i < n; i++) {
327 v = PyInt_FromLong(i);
328 if (!v) {
329 Py_DECREF(dict);
330 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000331 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000332 k = PyList_GET_ITEM(list, i);
333 k = Py_BuildValue("(OO)", k, k->ob_type);
334 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
335 Py_XDECREF(k);
336 Py_DECREF(v);
337 Py_DECREF(dict);
338 return NULL;
339 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000340 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000341 Py_DECREF(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000342 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000343 return dict;
344}
345
346/* Return new dict containing names from src that match scope(s).
347
348 src is a symbol table dictionary. If the scope of a name matches
349 either scope_type or flag is set, insert it into the new dict. The
350 values are integers, starting at offset and increasing by one for
351 each key.
352*/
353
354static PyObject *
355dictbytype(PyObject *src, int scope_type, int flag, int offset)
356{
357 int pos = 0, i = offset, scope;
358 PyObject *k, *v, *dest = PyDict_New();
359
360 assert(offset >= 0);
361 if (dest == NULL)
362 return NULL;
363
364 while (PyDict_Next(src, &pos, &k, &v)) {
365 /* XXX this should probably be a macro in symtable.h */
366 assert(PyInt_Check(v));
367 scope = (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
368
369 if (scope == scope_type || PyInt_AS_LONG(v) & flag) {
370 PyObject *tuple, *item = PyInt_FromLong(i);
371 if (item == NULL) {
372 Py_DECREF(dest);
373 return NULL;
374 }
375 i++;
376 tuple = Py_BuildValue("(OO)", k, k->ob_type);
377 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
378 Py_DECREF(item);
379 Py_DECREF(dest);
380 Py_XDECREF(tuple);
381 return NULL;
382 }
383 Py_DECREF(item);
384 Py_DECREF(tuple);
385 }
386 }
387 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000388}
389
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000390/* Begin: Peephole optimizations ----------------------------------------- */
391
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000392#define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1]))
393#define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD)
Raymond Hettinger5b75c382003-03-28 12:05:00 +0000394#define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP)
395#define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000396#define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000397#define CODESIZE(op) (HAS_ARG(op) ? 3 : 1)
398#define ISBASICBLOCK(blocks, start, bytes) (blocks[start]==blocks[start+bytes-1])
399
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000400/* Replace LOAD_CONST c1. LOAD_CONST c2 ... LOAD_CONST cn BUILD_TUPLE n
401 with LOAD_CONST (c1, c2, ... cn).
402 The consts table must still be in list form so that the
403 new constant (c1, c2, ... cn) can be appended.
404 Called with codestr pointing to the first LOAD_CONST.
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000405 Bails out with no change if one or more of the LOAD_CONSTs is missing.
406 Also works for BUILD_LIST when followed by an "in" or "not in" test.
407*/
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000408static int
409tuple_of_constants(unsigned char *codestr, int n, PyObject *consts)
410{
411 PyObject *newconst, *constant;
412 int i, arg, len_consts;
413
414 /* Pre-conditions */
415 assert(PyList_CheckExact(consts));
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000416 assert(codestr[n*3] == BUILD_TUPLE || codestr[n*3] == BUILD_LIST);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000417 assert(GETARG(codestr, (n*3)) == n);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000418 for (i=0 ; i<n ; i++)
Raymond Hettingereffb3932004-10-30 08:55:08 +0000419 assert(codestr[i*3] == LOAD_CONST);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000420
421 /* Buildup new tuple of constants */
422 newconst = PyTuple_New(n);
423 if (newconst == NULL)
424 return 0;
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000425 len_consts = PyList_GET_SIZE(consts);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000426 for (i=0 ; i<n ; i++) {
427 arg = GETARG(codestr, (i*3));
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000428 assert(arg < len_consts);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000429 constant = PyList_GET_ITEM(consts, arg);
430 Py_INCREF(constant);
431 PyTuple_SET_ITEM(newconst, i, constant);
432 }
433
434 /* Append folded constant onto consts */
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000435 if (PyList_Append(consts, newconst)) {
436 Py_DECREF(newconst);
437 return 0;
438 }
439 Py_DECREF(newconst);
440
441 /* Write NOPs over old LOAD_CONSTS and
442 add a new LOAD_CONST newconst on top of the BUILD_TUPLE n */
443 memset(codestr, NOP, n*3);
444 codestr[n*3] = LOAD_CONST;
445 SETARG(codestr, (n*3), len_consts);
446 return 1;
447}
448
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000449/* Replace LOAD_CONST c1. LOAD_CONST c2 BINOP
450 with LOAD_CONST binop(c1,c2)
451 The consts table must still be in list form so that the
452 new constant can be appended.
453 Called with codestr pointing to the first LOAD_CONST.
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000454 Abandons the transformation if the folding fails (i.e. 1+'a').
455 If the new constant is a sequence, only folds when the size
456 is below a threshold value. That keeps pyc files from
457 becoming large in the presence of code like: (None,)*1000.
458*/
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000459static int
460fold_binops_on_constants(unsigned char *codestr, PyObject *consts)
461{
462 PyObject *newconst, *v, *w;
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000463 int len_consts, opcode, size;
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000464
465 /* Pre-conditions */
466 assert(PyList_CheckExact(consts));
467 assert(codestr[0] == LOAD_CONST);
468 assert(codestr[3] == LOAD_CONST);
469
470 /* Create new constant */
471 v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
472 w = PyList_GET_ITEM(consts, GETARG(codestr, 3));
473 opcode = codestr[6];
474 switch (opcode) {
475 case BINARY_POWER:
476 newconst = PyNumber_Power(v, w, Py_None);
477 break;
478 case BINARY_MULTIPLY:
479 newconst = PyNumber_Multiply(v, w);
480 break;
481 case BINARY_DIVIDE:
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000482 /* Cannot fold this operation statically since
483 the result can depend on the run-time presence of the -Qnew flag */
Armin Rigo664b43b2005-01-07 18:10:51 +0000484 return 0;
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000485 case BINARY_TRUE_DIVIDE:
486 newconst = PyNumber_TrueDivide(v, w);
487 break;
488 case BINARY_FLOOR_DIVIDE:
489 newconst = PyNumber_FloorDivide(v, w);
490 break;
491 case BINARY_MODULO:
492 newconst = PyNumber_Remainder(v, w);
493 break;
494 case BINARY_ADD:
495 newconst = PyNumber_Add(v, w);
496 break;
497 case BINARY_SUBTRACT:
498 newconst = PyNumber_Subtract(v, w);
499 break;
500 case BINARY_SUBSCR:
501 newconst = PyObject_GetItem(v, w);
502 break;
503 case BINARY_LSHIFT:
504 newconst = PyNumber_Lshift(v, w);
505 break;
506 case BINARY_RSHIFT:
507 newconst = PyNumber_Rshift(v, w);
508 break;
509 case BINARY_AND:
510 newconst = PyNumber_And(v, w);
511 break;
512 case BINARY_XOR:
513 newconst = PyNumber_Xor(v, w);
514 break;
515 case BINARY_OR:
516 newconst = PyNumber_Or(v, w);
517 break;
518 default:
519 /* Called with an unknown opcode */
Neal Norwitz4737b232005-11-19 23:58:29 +0000520 PyErr_Format(PyExc_SystemError,
521 "unexpected binary operation %d on a constant",
522 opcode);
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000523 return 0;
524 }
525 if (newconst == NULL) {
526 PyErr_Clear();
527 return 0;
528 }
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000529 size = PyObject_Size(newconst);
530 if (size == -1)
531 PyErr_Clear();
532 else if (size > 20) {
533 Py_DECREF(newconst);
534 return 0;
535 }
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000536
537 /* Append folded constant into consts table */
538 len_consts = PyList_GET_SIZE(consts);
539 if (PyList_Append(consts, newconst)) {
540 Py_DECREF(newconst);
541 return 0;
542 }
543 Py_DECREF(newconst);
544
545 /* Write NOP NOP NOP NOP LOAD_CONST newconst */
546 memset(codestr, NOP, 4);
547 codestr[4] = LOAD_CONST;
548 SETARG(codestr, 4, len_consts);
549 return 1;
550}
551
Raymond Hettinger80121492005-02-20 12:41:32 +0000552static int
553fold_unaryops_on_constants(unsigned char *codestr, PyObject *consts)
554{
Raymond Hettingere63a0782005-02-23 13:37:55 +0000555 PyObject *newconst=NULL, *v;
Raymond Hettinger80121492005-02-20 12:41:32 +0000556 int len_consts, opcode;
557
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{
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000658 int i, j, codelen, nops, h, adj;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000659 int tgt, tgttgt, opcode;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000660 unsigned char *codestr = NULL;
661 unsigned char *lineno;
662 int *addrmap = NULL;
663 int new_line, cum_orig_line, last_line, tabsiz;
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000664 int cumlc=0, lastlc=0; /* Count runs of consecutive LOAD_CONST codes */
Raymond Hettingereffb3932004-10-30 08:55:08 +0000665 unsigned int *blocks = NULL;
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000666 char *name;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000667
Raymond Hettingereffb3932004-10-30 08:55:08 +0000668 /* Bail out if an exception is set */
669 if (PyErr_Occurred())
670 goto exitUnchanged;
671
Raymond Hettinger1792bfb2004-08-25 17:19:38 +0000672 /* Bypass optimization when the lineno table is too complex */
673 assert(PyString_Check(lineno_obj));
Brett Cannonc9371d42005-06-25 08:23:41 +0000674 lineno = (unsigned char*)PyString_AS_STRING(lineno_obj);
Raymond Hettinger1792bfb2004-08-25 17:19:38 +0000675 tabsiz = PyString_GET_SIZE(lineno_obj);
676 if (memchr(lineno, 255, tabsiz) != NULL)
677 goto exitUnchanged;
678
Raymond Hettingera12fa142004-08-24 04:34:16 +0000679 /* Avoid situations where jump retargeting could overflow */
Raymond Hettinger06cc9732004-09-28 17:22:12 +0000680 assert(PyString_Check(code));
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000681 codelen = PyString_Size(code);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000682 if (codelen > 32700)
Raymond Hettingera12fa142004-08-24 04:34:16 +0000683 goto exitUnchanged;
684
685 /* Make a modifiable copy of the code string */
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000686 codestr = PyMem_Malloc(codelen);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000687 if (codestr == NULL)
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000688 goto exitUnchanged;
689 codestr = memcpy(codestr, PyString_AS_STRING(code), codelen);
Raymond Hettinger98bd1812004-08-06 19:46:34 +0000690
Raymond Hettinger07359a72005-02-21 20:03:14 +0000691 /* Verify that RETURN_VALUE terminates the codestring. This allows
692 the various transformation patterns to look ahead several
693 instructions without additional checks to make sure they are not
694 looking beyond the end of the code string.
695 */
696 if (codestr[codelen-1] != RETURN_VALUE)
697 goto exitUnchanged;
698
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000699 /* Mapping to new jump targets after NOPs are removed */
700 addrmap = PyMem_Malloc(codelen * sizeof(int));
701 if (addrmap == NULL)
702 goto exitUnchanged;
703
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000704 blocks = markblocks(codestr, codelen);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000705 if (blocks == NULL)
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000706 goto exitUnchanged;
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000707 assert(PyList_Check(consts));
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000708
Raymond Hettinger099ecfb2004-11-01 15:19:11 +0000709 for (i=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000710 opcode = codestr[i];
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000711
712 lastlc = cumlc;
713 cumlc = 0;
714
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000715 switch (opcode) {
716
Raymond Hettinger43ea47f2004-06-24 09:25:39 +0000717 /* Replace UNARY_NOT JUMP_IF_FALSE POP_TOP with
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000718 with JUMP_IF_TRUE POP_TOP */
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000719 case UNARY_NOT:
720 if (codestr[i+1] != JUMP_IF_FALSE ||
721 codestr[i+4] != POP_TOP ||
722 !ISBASICBLOCK(blocks,i,5))
723 continue;
724 tgt = GETJUMPTGT(codestr, (i+1));
725 if (codestr[tgt] != POP_TOP)
726 continue;
Raymond Hettinger43ea47f2004-06-24 09:25:39 +0000727 j = GETARG(codestr, i+1) + 1;
728 codestr[i] = JUMP_IF_TRUE;
729 SETARG(codestr, i, j);
730 codestr[i+3] = POP_TOP;
731 codestr[i+4] = NOP;
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000732 break;
733
734 /* not a is b --> a is not b
735 not a in b --> a not in b
736 not a is not b --> a is b
Raymond Hettingerb615bf02005-02-10 01:42:32 +0000737 not a not in b --> a in b
Raymond Hettingera1645742005-02-06 22:05:42 +0000738 */
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000739 case COMPARE_OP:
740 j = GETARG(codestr, i);
741 if (j < 6 || j > 9 ||
742 codestr[i+3] != UNARY_NOT ||
743 !ISBASICBLOCK(blocks,i,4))
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000744 continue;
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000745 SETARG(codestr, i, (j^1));
746 codestr[i+3] = NOP;
Tim Petersdb5860b2004-07-17 05:00:52 +0000747 break;
748
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000749 /* Replace LOAD_GLOBAL/LOAD_NAME None with LOAD_CONST None */
750 case LOAD_NAME:
751 case LOAD_GLOBAL:
752 j = GETARG(codestr, i);
753 name = PyString_AsString(PyTuple_GET_ITEM(names, j));
754 if (name == NULL || strcmp(name, "None") != 0)
755 continue;
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000756 for (j=0 ; j < PyList_GET_SIZE(consts) ; j++) {
757 if (PyList_GET_ITEM(consts, j) == Py_None) {
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000758 codestr[i] = LOAD_CONST;
759 SETARG(codestr, i, j);
Raymond Hettinger5dec0962004-11-02 04:20:10 +0000760 cumlc = lastlc + 1;
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000761 break;
762 }
763 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000764 break;
765
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000766 /* Skip over LOAD_CONST trueconst JUMP_IF_FALSE xx POP_TOP */
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000767 case LOAD_CONST:
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000768 cumlc = lastlc + 1;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000769 j = GETARG(codestr, i);
770 if (codestr[i+3] != JUMP_IF_FALSE ||
771 codestr[i+6] != POP_TOP ||
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000772 !ISBASICBLOCK(blocks,i,7) ||
773 !PyObject_IsTrue(PyList_GET_ITEM(consts, j)))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000774 continue;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000775 memset(codestr+i, NOP, 7);
Raymond Hettinger5dec0962004-11-02 04:20:10 +0000776 cumlc = 0;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000777 break;
778
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000779 /* Try to fold tuples of constants (includes a case for lists
780 which are only used for "in" and "not in" tests).
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000781 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000782 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
783 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000784 case BUILD_TUPLE:
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000785 case BUILD_LIST:
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000786 j = GETARG(codestr, i);
787 h = i - 3 * j;
788 if (h >= 0 &&
Raymond Hettingereffb3932004-10-30 08:55:08 +0000789 j <= lastlc &&
Brett Cannon5dc8ced2005-03-03 07:01:48 +0000790 ((opcode == BUILD_TUPLE &&
791 ISBASICBLOCK(blocks, h, 3*(j+1))) ||
792 (opcode == BUILD_LIST &&
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000793 codestr[i+3]==COMPARE_OP &&
794 ISBASICBLOCK(blocks, h, 3*(j+2)) &&
Brett Cannon5dc8ced2005-03-03 07:01:48 +0000795 (GETARG(codestr,i+3)==6 ||
796 GETARG(codestr,i+3)==7))) &&
797 tuple_of_constants(&codestr[h], j, consts)) {
Raymond Hettinger5dec0962004-11-02 04:20:10 +0000798 assert(codestr[i] == LOAD_CONST);
799 cumlc = 1;
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000800 break;
801 }
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000802 if (codestr[i+3] != UNPACK_SEQUENCE ||
803 !ISBASICBLOCK(blocks,i,6) ||
804 j != GETARG(codestr, i+3))
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000805 continue;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000806 if (j == 1) {
807 memset(codestr+i, NOP, 6);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000808 } else if (j == 2) {
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000809 codestr[i] = ROT_TWO;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000810 memset(codestr+i+1, NOP, 5);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000811 } else if (j == 3) {
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000812 codestr[i] = ROT_THREE;
813 codestr[i+1] = ROT_TWO;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000814 memset(codestr+i+2, NOP, 4);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000815 }
816 break;
817
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000818 /* Fold binary ops on constants.
819 LOAD_CONST c1 LOAD_CONST c2 BINOP --> LOAD_CONST binop(c1,c2) */
820 case BINARY_POWER:
821 case BINARY_MULTIPLY:
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000822 case BINARY_TRUE_DIVIDE:
823 case BINARY_FLOOR_DIVIDE:
824 case BINARY_MODULO:
825 case BINARY_ADD:
826 case BINARY_SUBTRACT:
827 case BINARY_SUBSCR:
828 case BINARY_LSHIFT:
829 case BINARY_RSHIFT:
830 case BINARY_AND:
831 case BINARY_XOR:
832 case BINARY_OR:
833 if (lastlc >= 2 &&
834 ISBASICBLOCK(blocks, i-6, 7) &&
835 fold_binops_on_constants(&codestr[i-6], consts)) {
836 i -= 2;
837 assert(codestr[i] == LOAD_CONST);
838 cumlc = 1;
839 }
840 break;
841
Raymond Hettinger80121492005-02-20 12:41:32 +0000842 /* Fold unary ops on constants.
843 LOAD_CONST c1 UNARY_OP --> LOAD_CONST unary_op(c) */
844 case UNARY_NEGATIVE:
845 case UNARY_CONVERT:
846 case UNARY_INVERT:
847 if (lastlc >= 1 &&
848 ISBASICBLOCK(blocks, i-3, 4) &&
849 fold_unaryops_on_constants(&codestr[i-3], consts)) {
850 i -= 2;
851 assert(codestr[i] == LOAD_CONST);
852 cumlc = 1;
853 }
854 break;
855
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000856 /* Simplify conditional jump to conditional jump where the
857 result of the first test implies the success of a similar
858 test or the failure of the opposite test.
859 Arises in code like:
Raymond Hettingereffb3932004-10-30 08:55:08 +0000860 "if a and b:"
861 "if a or b:"
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000862 "a and b or c"
Armin Rigod7bcf4d2004-10-30 21:08:59 +0000863 "(a and b) and c"
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000864 x:JUMP_IF_FALSE y y:JUMP_IF_FALSE z --> x:JUMP_IF_FALSE z
Raymond Hettinger65d3c052004-08-25 15:15:56 +0000865 x:JUMP_IF_FALSE y y:JUMP_IF_TRUE z --> x:JUMP_IF_FALSE y+3
866 where y+3 is the instruction following the second test.
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000867 */
868 case JUMP_IF_FALSE:
869 case JUMP_IF_TRUE:
870 tgt = GETJUMPTGT(codestr, i);
871 j = codestr[tgt];
872 if (j == JUMP_IF_FALSE || j == JUMP_IF_TRUE) {
873 if (j == opcode) {
874 tgttgt = GETJUMPTGT(codestr, tgt) - i - 3;
875 SETARG(codestr, i, tgttgt);
876 } else {
877 tgt -= i;
878 SETARG(codestr, i, tgt);
879 }
880 break;
881 }
882 /* Intentional fallthrough */
883
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000884 /* Replace jumps to unconditional jumps */
Raymond Hettinger255a3d02003-04-15 10:35:07 +0000885 case FOR_ITER:
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000886 case JUMP_FORWARD:
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000887 case JUMP_ABSOLUTE:
888 case CONTINUE_LOOP:
889 case SETUP_LOOP:
890 case SETUP_EXCEPT:
891 case SETUP_FINALLY:
892 tgt = GETJUMPTGT(codestr, i);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000893 if (!UNCONDITIONAL_JUMP(codestr[tgt]))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000894 continue;
895 tgttgt = GETJUMPTGT(codestr, tgt);
896 if (opcode == JUMP_FORWARD) /* JMP_ABS can go backwards */
897 opcode = JUMP_ABSOLUTE;
Raymond Hettinger5b75c382003-03-28 12:05:00 +0000898 if (!ABSOLUTE_JUMP(opcode))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000899 tgttgt -= i + 3; /* Calc relative jump addr */
900 if (tgttgt < 0) /* No backward relative jumps */
901 continue;
902 codestr[i] = opcode;
903 SETARG(codestr, i, tgttgt);
904 break;
905
906 case EXTENDED_ARG:
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000907 goto exitUnchanged;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000908
909 /* Replace RETURN LOAD_CONST None RETURN with just RETURN */
910 case RETURN_VALUE:
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000911 if (i+4 >= codelen ||
912 codestr[i+4] != RETURN_VALUE ||
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000913 !ISBASICBLOCK(blocks,i,5))
914 continue;
915 memset(codestr+i+1, NOP, 4);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000916 break;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000917 }
918 }
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000919
920 /* Fixup linenotab */
Raymond Hettinger099ecfb2004-11-01 15:19:11 +0000921 for (i=0, nops=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
922 addrmap[i] = i - nops;
923 if (codestr[i] == NOP)
924 nops++;
925 }
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000926 cum_orig_line = 0;
927 last_line = 0;
928 for (i=0 ; i < tabsiz ; i+=2) {
929 cum_orig_line += lineno[i];
930 new_line = addrmap[cum_orig_line];
Raymond Hettinger1792bfb2004-08-25 17:19:38 +0000931 assert (new_line - last_line < 255);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000932 lineno[i] =((unsigned char)(new_line - last_line));
933 last_line = new_line;
934 }
935
936 /* Remove NOPs and fixup jump targets */
937 for (i=0, h=0 ; i<codelen ; ) {
938 opcode = codestr[i];
939 switch (opcode) {
940 case NOP:
941 i++;
942 continue;
943
944 case JUMP_ABSOLUTE:
945 case CONTINUE_LOOP:
946 j = addrmap[GETARG(codestr, i)];
947 SETARG(codestr, i, j);
948 break;
949
950 case FOR_ITER:
951 case JUMP_FORWARD:
952 case JUMP_IF_FALSE:
953 case JUMP_IF_TRUE:
954 case SETUP_LOOP:
955 case SETUP_EXCEPT:
956 case SETUP_FINALLY:
957 j = addrmap[GETARG(codestr, i) + i + 3] - addrmap[i] - 3;
958 SETARG(codestr, i, j);
959 break;
960 }
961 adj = CODESIZE(opcode);
962 while (adj--)
963 codestr[h++] = codestr[i++];
964 }
Raymond Hettingera12fa142004-08-24 04:34:16 +0000965 assert(h + nops == codelen);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000966
967 code = PyString_FromStringAndSize((char *)codestr, h);
968 PyMem_Free(addrmap);
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000969 PyMem_Free(codestr);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000970 PyMem_Free(blocks);
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000971 return code;
972
973exitUnchanged:
Raymond Hettingereffb3932004-10-30 08:55:08 +0000974 if (blocks != NULL)
975 PyMem_Free(blocks);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000976 if (addrmap != NULL)
977 PyMem_Free(addrmap);
978 if (codestr != NULL)
979 PyMem_Free(codestr);
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000980 Py_INCREF(code);
981 return code;
982}
983
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000984/* End: Peephole optimizations ----------------------------------------- */
985
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000986/*
987
988Leave this debugging code for just a little longer.
989
990static void
991compiler_display_symbols(PyObject *name, PyObject *symbols)
992{
993 PyObject *key, *value;
994 int flags, pos = 0;
995
996 fprintf(stderr, "block %s\n", PyString_AS_STRING(name));
997 while (PyDict_Next(symbols, &pos, &key, &value)) {
998 flags = PyInt_AsLong(value);
999 fprintf(stderr, "var %s:", PyString_AS_STRING(key));
1000 if (flags & DEF_GLOBAL)
1001 fprintf(stderr, " declared_global");
1002 if (flags & DEF_LOCAL)
1003 fprintf(stderr, " local");
1004 if (flags & DEF_PARAM)
1005 fprintf(stderr, " param");
1006 if (flags & DEF_STAR)
1007 fprintf(stderr, " stararg");
1008 if (flags & DEF_DOUBLESTAR)
1009 fprintf(stderr, " starstar");
1010 if (flags & DEF_INTUPLE)
1011 fprintf(stderr, " tuple");
1012 if (flags & DEF_FREE)
1013 fprintf(stderr, " free");
1014 if (flags & DEF_FREE_GLOBAL)
1015 fprintf(stderr, " global");
1016 if (flags & DEF_FREE_CLASS)
1017 fprintf(stderr, " free/class");
1018 if (flags & DEF_IMPORT)
1019 fprintf(stderr, " import");
1020 fprintf(stderr, "\n");
1021 }
1022 fprintf(stderr, "\n");
1023}
1024*/
1025
1026static void
1027compiler_unit_check(struct compiler_unit *u)
1028{
1029 basicblock *block;
1030 for (block = u->u_blocks; block != NULL; block = block->b_list) {
1031 assert(block != (void *)0xcbcbcbcb);
1032 assert(block != (void *)0xfbfbfbfb);
1033 assert(block != (void *)0xdbdbdbdb);
1034 if (block->b_instr != NULL) {
1035 assert(block->b_ialloc > 0);
1036 assert(block->b_iused > 0);
1037 assert(block->b_ialloc >= block->b_iused);
1038 }
1039 else {
1040 assert (block->b_iused == 0);
1041 assert (block->b_ialloc == 0);
1042 }
1043 }
1044}
1045
1046static void
1047compiler_unit_free(struct compiler_unit *u)
1048{
1049 basicblock *b, *next;
1050
1051 compiler_unit_check(u);
1052 b = u->u_blocks;
1053 while (b != NULL) {
1054 if (b->b_instr)
1055 PyObject_Free((void *)b->b_instr);
1056 next = b->b_list;
1057 PyObject_Free((void *)b);
1058 b = next;
1059 }
1060 Py_XDECREF(u->u_ste);
1061 Py_XDECREF(u->u_name);
1062 Py_XDECREF(u->u_consts);
1063 Py_XDECREF(u->u_names);
1064 Py_XDECREF(u->u_varnames);
1065 Py_XDECREF(u->u_freevars);
1066 Py_XDECREF(u->u_cellvars);
1067 Py_XDECREF(u->u_private);
1068 PyObject_Free(u);
1069}
1070
1071static int
1072compiler_enter_scope(struct compiler *c, identifier name, void *key,
1073 int lineno)
1074{
1075 struct compiler_unit *u;
1076
1077 u = PyObject_Malloc(sizeof(struct compiler_unit));
Neal Norwitz87b801c2005-12-18 04:42:47 +00001078 if (!u) {
1079 PyErr_NoMemory();
1080 return 0;
1081 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001082 memset(u, 0, sizeof(struct compiler_unit));
1083 u->u_argcount = 0;
1084 u->u_ste = PySymtable_Lookup(c->c_st, key);
1085 if (!u->u_ste) {
1086 compiler_unit_free(u);
Neal Norwitz87b801c2005-12-18 04:42:47 +00001087 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001088 }
1089 Py_INCREF(name);
1090 u->u_name = name;
1091 u->u_varnames = list2dict(u->u_ste->ste_varnames);
1092 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
1093 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
1094 PyDict_Size(u->u_cellvars));
1095
1096 u->u_blocks = NULL;
1097 u->u_tmpname = 0;
1098 u->u_nfblocks = 0;
1099 u->u_firstlineno = lineno;
1100 u->u_lineno = 0;
1101 u->u_lineno_set = false;
1102 u->u_consts = PyDict_New();
1103 if (!u->u_consts) {
1104 compiler_unit_free(u);
1105 return 0;
1106 }
1107 u->u_names = PyDict_New();
1108 if (!u->u_names) {
1109 compiler_unit_free(u);
1110 return 0;
1111 }
1112
1113 u->u_private = NULL;
1114
1115 /* Push the old compiler_unit on the stack. */
1116 if (c->u) {
1117 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
1118 if (PyList_Append(c->c_stack, wrapper) < 0) {
1119 compiler_unit_free(u);
1120 return 0;
1121 }
1122 Py_DECREF(wrapper);
1123 u->u_private = c->u->u_private;
1124 Py_XINCREF(u->u_private);
1125 }
1126 c->u = u;
1127
1128 c->c_nestlevel++;
1129 if (compiler_use_new_block(c) < 0)
1130 return 0;
1131
1132 return 1;
1133}
1134
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001135static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136compiler_exit_scope(struct compiler *c)
1137{
1138 int n;
1139 PyObject *wrapper;
1140
1141 c->c_nestlevel--;
1142 compiler_unit_free(c->u);
1143 /* Restore c->u to the parent unit. */
1144 n = PyList_GET_SIZE(c->c_stack) - 1;
1145 if (n >= 0) {
1146 wrapper = PyList_GET_ITEM(c->c_stack, n);
1147 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001148 /* we are deleting from a list so this really shouldn't fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001149 if (PySequence_DelItem(c->c_stack, n) < 0)
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001150 Py_FatalError("compiler_exit_scope()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151 compiler_unit_check(c->u);
1152 }
1153 else
1154 c->u = NULL;
1155
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001156}
1157
1158/* Allocate a new block and return a pointer to it.
1159 Returns NULL on error.
1160*/
1161
1162static basicblock *
1163compiler_new_block(struct compiler *c)
1164{
1165 basicblock *b;
1166 struct compiler_unit *u;
1167
1168 u = c->u;
1169 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
Neal Norwitz87b801c2005-12-18 04:42:47 +00001170 if (b == NULL) {
1171 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172 return NULL;
Neal Norwitz87b801c2005-12-18 04:42:47 +00001173 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174 memset((void *)b, 0, sizeof(basicblock));
1175 assert (b->b_next == NULL);
1176 b->b_list = u->u_blocks;
1177 u->u_blocks = b;
1178 return b;
1179}
1180
1181static void
1182compiler_use_block(struct compiler *c, basicblock *block)
1183{
1184 assert (block != NULL);
1185 c->u->u_curblock = block;
1186}
1187
1188static basicblock *
1189compiler_use_new_block(struct compiler *c)
1190{
1191 basicblock *block = compiler_new_block(c);
1192 if (block == NULL)
1193 return NULL;
1194 c->u->u_curblock = block;
1195 return block;
1196}
1197
1198static basicblock *
1199compiler_next_block(struct compiler *c)
1200{
1201 basicblock *block = compiler_new_block(c);
1202 if (block == NULL)
1203 return NULL;
1204 c->u->u_curblock->b_next = block;
1205 c->u->u_curblock = block;
1206 return block;
1207}
1208
1209static basicblock *
1210compiler_use_next_block(struct compiler *c, basicblock *block)
1211{
1212 assert(block != NULL);
1213 c->u->u_curblock->b_next = block;
1214 c->u->u_curblock = block;
1215 return block;
1216}
1217
1218/* Returns the offset of the next instruction in the current block's
1219 b_instr array. Resizes the b_instr as necessary.
1220 Returns -1 on failure.
1221 */
1222
1223static int
1224compiler_next_instr(struct compiler *c, basicblock *b)
1225{
1226 assert(b != NULL);
1227 if (b->b_instr == NULL) {
1228 b->b_instr = PyObject_Malloc(sizeof(struct instr) *
1229 DEFAULT_BLOCK_SIZE);
1230 if (b->b_instr == NULL) {
1231 PyErr_NoMemory();
1232 return -1;
1233 }
1234 b->b_ialloc = DEFAULT_BLOCK_SIZE;
1235 memset((char *)b->b_instr, 0,
1236 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
1237 }
1238 else if (b->b_iused == b->b_ialloc) {
1239 size_t oldsize, newsize;
1240 oldsize = b->b_ialloc * sizeof(struct instr);
1241 newsize = oldsize << 1;
1242 if (newsize == 0) {
1243 PyErr_NoMemory();
1244 return -1;
1245 }
1246 b->b_ialloc <<= 1;
1247 b->b_instr = PyObject_Realloc((void *)b->b_instr, newsize);
1248 if (b->b_instr == NULL)
1249 return -1;
1250 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
1251 }
1252 return b->b_iused++;
1253}
1254
1255static void
1256compiler_set_lineno(struct compiler *c, int off)
1257{
1258 basicblock *b;
1259 if (c->u->u_lineno_set)
1260 return;
1261 c->u->u_lineno_set = true;
1262 b = c->u->u_curblock;
1263 b->b_instr[off].i_lineno = c->u->u_lineno;
1264}
1265
1266static int
1267opcode_stack_effect(int opcode, int oparg)
1268{
1269 switch (opcode) {
1270 case POP_TOP:
1271 return -1;
1272 case ROT_TWO:
1273 case ROT_THREE:
1274 return 0;
1275 case DUP_TOP:
1276 return 1;
1277 case ROT_FOUR:
1278 return 0;
1279
1280 case UNARY_POSITIVE:
1281 case UNARY_NEGATIVE:
1282 case UNARY_NOT:
1283 case UNARY_CONVERT:
1284 case UNARY_INVERT:
1285 return 0;
1286
1287 case BINARY_POWER:
1288 case BINARY_MULTIPLY:
1289 case BINARY_DIVIDE:
1290 case BINARY_MODULO:
1291 case BINARY_ADD:
1292 case BINARY_SUBTRACT:
1293 case BINARY_SUBSCR:
1294 case BINARY_FLOOR_DIVIDE:
1295 case BINARY_TRUE_DIVIDE:
1296 return -1;
1297 case INPLACE_FLOOR_DIVIDE:
1298 case INPLACE_TRUE_DIVIDE:
1299 return -1;
1300
1301 case SLICE+0:
1302 return 1;
1303 case SLICE+1:
1304 return 0;
1305 case SLICE+2:
1306 return 0;
1307 case SLICE+3:
1308 return -1;
1309
1310 case STORE_SLICE+0:
1311 return -2;
1312 case STORE_SLICE+1:
1313 return -3;
1314 case STORE_SLICE+2:
1315 return -3;
1316 case STORE_SLICE+3:
1317 return -4;
1318
1319 case DELETE_SLICE+0:
1320 return -1;
1321 case DELETE_SLICE+1:
1322 return -2;
1323 case DELETE_SLICE+2:
1324 return -2;
1325 case DELETE_SLICE+3:
1326 return -3;
1327
1328 case INPLACE_ADD:
1329 case INPLACE_SUBTRACT:
1330 case INPLACE_MULTIPLY:
1331 case INPLACE_DIVIDE:
1332 case INPLACE_MODULO:
1333 return -1;
1334 case STORE_SUBSCR:
1335 return -3;
1336 case DELETE_SUBSCR:
1337 return -2;
1338
1339 case BINARY_LSHIFT:
1340 case BINARY_RSHIFT:
1341 case BINARY_AND:
1342 case BINARY_XOR:
1343 case BINARY_OR:
1344 return -1;
1345 case INPLACE_POWER:
1346 return -1;
1347 case GET_ITER:
1348 return 0;
1349
1350 case PRINT_EXPR:
1351 return -1;
1352 case PRINT_ITEM:
1353 return -1;
1354 case PRINT_NEWLINE:
1355 return 0;
1356 case PRINT_ITEM_TO:
1357 return -2;
1358 case PRINT_NEWLINE_TO:
1359 return -1;
1360 case INPLACE_LSHIFT:
1361 case INPLACE_RSHIFT:
1362 case INPLACE_AND:
1363 case INPLACE_XOR:
1364 case INPLACE_OR:
1365 return -1;
1366 case BREAK_LOOP:
1367 return 0;
1368
1369 case LOAD_LOCALS:
1370 return 1;
1371 case RETURN_VALUE:
1372 return -1;
1373 case IMPORT_STAR:
1374 return -1;
1375 case EXEC_STMT:
1376 return -3;
1377 case YIELD_VALUE:
1378 return 0;
1379
1380 case POP_BLOCK:
1381 return 0;
1382 case END_FINALLY:
1383 return -1; /* or -2 or -3 if exception occurred */
1384 case BUILD_CLASS:
1385 return -2;
1386
1387 case STORE_NAME:
1388 return -1;
1389 case DELETE_NAME:
1390 return 0;
1391 case UNPACK_SEQUENCE:
1392 return oparg-1;
1393 case FOR_ITER:
1394 return 1;
1395
1396 case STORE_ATTR:
1397 return -2;
1398 case DELETE_ATTR:
1399 return -1;
1400 case STORE_GLOBAL:
1401 return -1;
1402 case DELETE_GLOBAL:
1403 return 0;
1404 case DUP_TOPX:
1405 return oparg;
1406 case LOAD_CONST:
1407 return 1;
1408 case LOAD_NAME:
1409 return 1;
1410 case BUILD_TUPLE:
1411 case BUILD_LIST:
1412 return 1-oparg;
1413 case BUILD_MAP:
1414 return 1;
1415 case LOAD_ATTR:
1416 return 0;
1417 case COMPARE_OP:
1418 return -1;
1419 case IMPORT_NAME:
1420 return 0;
1421 case IMPORT_FROM:
1422 return 1;
1423
1424 case JUMP_FORWARD:
1425 case JUMP_IF_FALSE:
1426 case JUMP_IF_TRUE:
1427 case JUMP_ABSOLUTE:
1428 return 0;
1429
1430 case LOAD_GLOBAL:
1431 return 1;
1432
1433 case CONTINUE_LOOP:
1434 return 0;
1435 case SETUP_LOOP:
1436 return 0;
1437 case SETUP_EXCEPT:
1438 case SETUP_FINALLY:
1439 return 3; /* actually pushed by an exception */
1440
1441 case LOAD_FAST:
1442 return 1;
1443 case STORE_FAST:
1444 return -1;
1445 case DELETE_FAST:
1446 return 0;
1447
1448 case RAISE_VARARGS:
1449 return -oparg;
1450#define NARGS(o) (((o) % 256) + 2*((o) / 256))
1451 case CALL_FUNCTION:
1452 return -NARGS(oparg);
1453 case CALL_FUNCTION_VAR:
1454 case CALL_FUNCTION_KW:
1455 return -NARGS(oparg)-1;
1456 case CALL_FUNCTION_VAR_KW:
1457 return -NARGS(oparg)-2;
1458#undef NARGS
1459 case MAKE_FUNCTION:
1460 return -oparg;
1461 case BUILD_SLICE:
1462 if (oparg == 3)
1463 return -2;
1464 else
1465 return -1;
1466
1467 case MAKE_CLOSURE:
1468 return -oparg;
1469 case LOAD_CLOSURE:
1470 return 1;
1471 case LOAD_DEREF:
1472 return 1;
1473 case STORE_DEREF:
1474 return -1;
1475 default:
1476 fprintf(stderr, "opcode = %d\n", opcode);
1477 Py_FatalError("opcode_stack_effect()");
1478
1479 }
1480 return 0; /* not reachable */
1481}
1482
1483/* Add an opcode with no argument.
1484 Returns 0 on failure, 1 on success.
1485*/
1486
1487static int
1488compiler_addop(struct compiler *c, int opcode)
1489{
1490 basicblock *b;
1491 struct instr *i;
1492 int off;
1493 off = compiler_next_instr(c, c->u->u_curblock);
1494 if (off < 0)
1495 return 0;
1496 b = c->u->u_curblock;
1497 i = &b->b_instr[off];
1498 i->i_opcode = opcode;
1499 i->i_hasarg = 0;
1500 if (opcode == RETURN_VALUE)
1501 b->b_return = 1;
1502 compiler_set_lineno(c, off);
1503 return 1;
1504}
1505
1506static int
1507compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
1508{
1509 PyObject *t, *v;
1510 int arg;
1511
1512 /* necessary to make sure types aren't coerced (e.g., int and long) */
Neil Schemenauer3a44aaa2005-10-23 17:21:54 +00001513 t = PyTuple_Pack(2, o, o->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514 if (t == NULL)
1515 return -1;
1516
1517 v = PyDict_GetItem(dict, t);
1518 if (!v) {
1519 arg = PyDict_Size(dict);
1520 v = PyInt_FromLong(arg);
1521 if (!v) {
1522 Py_DECREF(t);
1523 return -1;
1524 }
1525 if (PyDict_SetItem(dict, t, v) < 0) {
1526 Py_DECREF(t);
1527 Py_DECREF(v);
1528 return -1;
1529 }
1530 Py_DECREF(v);
1531 }
1532 else
1533 arg = PyInt_AsLong(v);
1534 Py_DECREF(t);
1535 return arg;
1536}
1537
1538static int
1539compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
1540 PyObject *o)
1541{
1542 int arg = compiler_add_o(c, dict, o);
1543 if (arg < 0)
1544 return 0;
1545 return compiler_addop_i(c, opcode, arg);
1546}
1547
1548static int
1549compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
1550 PyObject *o)
1551{
1552 int arg;
1553 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1554 if (!mangled)
1555 return 0;
1556 arg = compiler_add_o(c, dict, mangled);
1557 Py_DECREF(mangled);
1558 if (arg < 0)
1559 return 0;
1560 return compiler_addop_i(c, opcode, arg);
1561}
1562
1563/* Add an opcode with an integer argument.
1564 Returns 0 on failure, 1 on success.
1565*/
1566
1567static int
1568compiler_addop_i(struct compiler *c, int opcode, int oparg)
1569{
1570 struct instr *i;
1571 int off;
1572 off = compiler_next_instr(c, c->u->u_curblock);
1573 if (off < 0)
1574 return 0;
1575 i = &c->u->u_curblock->b_instr[off];
1576 i->i_opcode = opcode;
1577 i->i_oparg = oparg;
1578 i->i_hasarg = 1;
1579 compiler_set_lineno(c, off);
1580 return 1;
1581}
1582
1583static int
1584compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1585{
1586 struct instr *i;
1587 int off;
1588
1589 assert(b != NULL);
1590 off = compiler_next_instr(c, c->u->u_curblock);
1591 if (off < 0)
1592 return 0;
1593 compiler_set_lineno(c, off);
1594 i = &c->u->u_curblock->b_instr[off];
1595 i->i_opcode = opcode;
1596 i->i_target = b;
1597 i->i_hasarg = 1;
1598 if (absolute)
1599 i->i_jabs = 1;
1600 else
1601 i->i_jrel = 1;
1602 return 1;
1603}
1604
1605/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1606 like to find better names.) NEW_BLOCK() creates a new block and sets
1607 it as the current block. NEXT_BLOCK() also creates an implicit jump
1608 from the current block to the new block.
1609*/
1610
1611/* XXX The returns inside these macros make it impossible to decref
1612 objects created in the local function.
1613*/
1614
1615
1616#define NEW_BLOCK(C) { \
1617 if (compiler_use_new_block((C)) == NULL) \
1618 return 0; \
1619}
1620
1621#define NEXT_BLOCK(C) { \
1622 if (compiler_next_block((C)) == NULL) \
1623 return 0; \
1624}
1625
1626#define ADDOP(C, OP) { \
1627 if (!compiler_addop((C), (OP))) \
1628 return 0; \
1629}
1630
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001631#define ADDOP_IN_SCOPE(C, OP) { \
1632 if (!compiler_addop((C), (OP))) { \
1633 compiler_exit_scope(c); \
1634 return 0; \
1635 } \
1636}
1637
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001638#define ADDOP_O(C, OP, O, TYPE) { \
1639 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1640 return 0; \
1641}
1642
1643#define ADDOP_NAME(C, OP, O, TYPE) { \
1644 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1645 return 0; \
1646}
1647
1648#define ADDOP_I(C, OP, O) { \
1649 if (!compiler_addop_i((C), (OP), (O))) \
1650 return 0; \
1651}
1652
1653#define ADDOP_JABS(C, OP, O) { \
1654 if (!compiler_addop_j((C), (OP), (O), 1)) \
1655 return 0; \
1656}
1657
1658#define ADDOP_JREL(C, OP, O) { \
1659 if (!compiler_addop_j((C), (OP), (O), 0)) \
1660 return 0; \
1661}
1662
1663/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1664 the ASDL name to synthesize the name of the C type and the visit function.
1665*/
1666
1667#define VISIT(C, TYPE, V) {\
1668 if (!compiler_visit_ ## TYPE((C), (V))) \
1669 return 0; \
1670}
1671
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001672#define VISIT_IN_SCOPE(C, TYPE, V) {\
1673 if (!compiler_visit_ ## TYPE((C), (V))) { \
1674 compiler_exit_scope(c); \
1675 return 0; \
1676 } \
1677}
1678
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001679#define VISIT_SLICE(C, V, CTX) {\
1680 if (!compiler_visit_slice((C), (V), (CTX))) \
1681 return 0; \
1682}
1683
1684#define VISIT_SEQ(C, TYPE, SEQ) { \
1685 int i; \
1686 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1687 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1688 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
1689 if (!compiler_visit_ ## TYPE((C), elt)) \
1690 return 0; \
1691 } \
1692}
1693
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001694#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
1695 int i; \
1696 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1697 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1698 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
1699 if (!compiler_visit_ ## TYPE((C), elt)) { \
1700 compiler_exit_scope(c); \
1701 return 0; \
1702 } \
1703 } \
1704}
1705
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001706static int
1707compiler_isdocstring(stmt_ty s)
1708{
1709 if (s->kind != Expr_kind)
1710 return 0;
1711 return s->v.Expr.value->kind == Str_kind;
1712}
1713
1714/* Compile a sequence of statements, checking for a docstring. */
1715
1716static int
1717compiler_body(struct compiler *c, asdl_seq *stmts)
1718{
1719 int i = 0;
1720 stmt_ty st;
1721
1722 if (!asdl_seq_LEN(stmts))
1723 return 1;
1724 st = asdl_seq_GET(stmts, 0);
1725 if (compiler_isdocstring(st)) {
1726 i = 1;
1727 VISIT(c, expr, st->v.Expr.value);
1728 if (!compiler_nameop(c, __doc__, Store))
1729 return 0;
1730 }
1731 for (; i < asdl_seq_LEN(stmts); i++)
1732 VISIT(c, stmt, asdl_seq_GET(stmts, i));
1733 return 1;
1734}
1735
1736static PyCodeObject *
1737compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001738{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001739 PyCodeObject *co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740 int addNone = 1;
1741 static PyObject *module;
1742 if (!module) {
1743 module = PyString_FromString("<module>");
1744 if (!module)
1745 return NULL;
1746 }
1747 if (!compiler_enter_scope(c, module, mod, 1))
Guido van Rossumd076c731998-10-07 19:42:25 +00001748 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749 switch (mod->kind) {
1750 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001751 if (!compiler_body(c, mod->v.Module.body)) {
1752 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001753 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001754 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001755 break;
1756 case Interactive_kind:
1757 c->c_interactive = 1;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001758 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001759 break;
1760 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001761 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001762 addNone = 0;
1763 break;
1764 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001765 PyErr_SetString(PyExc_SystemError,
1766 "suite should not be possible");
1767 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001769 PyErr_Format(PyExc_SystemError,
1770 "module kind %d should not be possible",
1771 mod->kind);
1772 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001773 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001774 co = assemble(c, addNone);
1775 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001776 return co;
1777}
1778
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001779/* The test for LOCAL must come before the test for FREE in order to
1780 handle classes where name is both local and free. The local var is
1781 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001782*/
1783
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001784static int
1785get_ref_type(struct compiler *c, PyObject *name)
1786{
1787 int scope = PyST_GetScope(c->u->u_ste, name);
1788 if (scope == 0) {
1789 char buf[350];
1790 PyOS_snprintf(buf, sizeof(buf),
1791 "unknown scope for %.100s in %.100s(%s) in %s\n"
1792 "symbols: %s\nlocals: %s\nglobals: %s\n",
1793 PyString_AS_STRING(name),
1794 PyString_AS_STRING(c->u->u_name),
1795 PyObject_REPR(c->u->u_ste->ste_id),
1796 c->c_filename,
1797 PyObject_REPR(c->u->u_ste->ste_symbols),
1798 PyObject_REPR(c->u->u_varnames),
1799 PyObject_REPR(c->u->u_names)
1800 );
1801 Py_FatalError(buf);
1802 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001803
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001804 return scope;
1805}
1806
1807static int
1808compiler_lookup_arg(PyObject *dict, PyObject *name)
1809{
1810 PyObject *k, *v;
1811 k = Py_BuildValue("(OO)", name, name->ob_type);
1812 if (k == NULL)
1813 return -1;
1814 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001815 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001816 if (v == NULL)
1817 return -1;
1818 return PyInt_AS_LONG(v);
1819}
1820
1821static int
1822compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1823{
1824 int i, free = PyCode_GetNumFree(co);
1825 if (free == 0) {
1826 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1827 ADDOP_I(c, MAKE_FUNCTION, args);
1828 return 1;
1829 }
1830 for (i = 0; i < free; ++i) {
1831 /* Bypass com_addop_varname because it will generate
1832 LOAD_DEREF but LOAD_CLOSURE is needed.
1833 */
1834 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1835 int arg, reftype;
1836
1837 /* Special case: If a class contains a method with a
1838 free variable that has the same name as a method,
1839 the name will be considered free *and* local in the
1840 class. It should be handled by the closure, as
1841 well as by the normal name loookup logic.
1842 */
1843 reftype = get_ref_type(c, name);
1844 if (reftype == CELL)
1845 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1846 else /* (reftype == FREE) */
1847 arg = compiler_lookup_arg(c->u->u_freevars, name);
1848 if (arg == -1) {
1849 printf("lookup %s in %s %d %d\n"
1850 "freevars of %s: %s\n",
1851 PyObject_REPR(name),
1852 PyString_AS_STRING(c->u->u_name),
1853 reftype, arg,
1854 PyString_AS_STRING(co->co_name),
1855 PyObject_REPR(co->co_freevars));
1856 Py_FatalError("compiler_make_closure()");
1857 }
1858 ADDOP_I(c, LOAD_CLOSURE, arg);
1859 }
1860 ADDOP_I(c, BUILD_TUPLE, free);
1861 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1862 ADDOP_I(c, MAKE_CLOSURE, args);
1863 return 1;
1864}
1865
1866static int
1867compiler_decorators(struct compiler *c, asdl_seq* decos)
1868{
1869 int i;
1870
1871 if (!decos)
1872 return 1;
1873
1874 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1875 VISIT(c, expr, asdl_seq_GET(decos, i));
1876 }
1877 return 1;
1878}
1879
1880static int
1881compiler_arguments(struct compiler *c, arguments_ty args)
1882{
1883 int i;
1884 int n = asdl_seq_LEN(args->args);
1885 /* Correctly handle nested argument lists */
1886 for (i = 0; i < n; i++) {
1887 expr_ty arg = asdl_seq_GET(args->args, i);
1888 if (arg->kind == Tuple_kind) {
1889 PyObject *id = PyString_FromFormat(".%d", i);
1890 if (id == NULL) {
1891 return 0;
1892 }
1893 if (!compiler_nameop(c, id, Load)) {
1894 Py_DECREF(id);
1895 return 0;
1896 }
1897 Py_DECREF(id);
1898 VISIT(c, expr, arg);
1899 }
1900 }
1901 return 1;
1902}
1903
1904static int
1905compiler_function(struct compiler *c, stmt_ty s)
1906{
1907 PyCodeObject *co;
1908 PyObject *first_const = Py_None;
1909 arguments_ty args = s->v.FunctionDef.args;
1910 asdl_seq* decos = s->v.FunctionDef.decorators;
1911 stmt_ty st;
1912 int i, n, docstring;
1913
1914 assert(s->kind == FunctionDef_kind);
1915
1916 if (!compiler_decorators(c, decos))
1917 return 0;
1918 if (args->defaults)
1919 VISIT_SEQ(c, expr, args->defaults);
1920 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1921 s->lineno))
1922 return 0;
1923
1924 st = asdl_seq_GET(s->v.FunctionDef.body, 0);
1925 docstring = compiler_isdocstring(st);
1926 if (docstring)
1927 first_const = st->v.Expr.value->v.Str.s;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001928 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1929 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001930 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001931 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001932
1933 /* unpack nested arguments */
1934 compiler_arguments(c, args);
1935
1936 c->u->u_argcount = asdl_seq_LEN(args->args);
1937 n = asdl_seq_LEN(s->v.FunctionDef.body);
1938 /* if there was a docstring, we need to skip the first statement */
1939 for (i = docstring; i < n; i++) {
1940 stmt_ty s2 = asdl_seq_GET(s->v.FunctionDef.body, i);
1941 if (i == 0 && s2->kind == Expr_kind &&
1942 s2->v.Expr.value->kind == Str_kind)
1943 continue;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001944 VISIT_IN_SCOPE(c, stmt, s2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001945 }
1946 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001947 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001948 if (co == NULL)
1949 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001950
1951 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00001952 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001953
1954 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1955 ADDOP_I(c, CALL_FUNCTION, 1);
1956 }
1957
1958 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1959}
1960
1961static int
1962compiler_class(struct compiler *c, stmt_ty s)
1963{
1964 int n;
1965 PyCodeObject *co;
1966 PyObject *str;
1967 /* push class name on stack, needed by BUILD_CLASS */
1968 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1969 /* push the tuple of base classes on the stack */
1970 n = asdl_seq_LEN(s->v.ClassDef.bases);
1971 if (n > 0)
1972 VISIT_SEQ(c, expr, s->v.ClassDef.bases);
1973 ADDOP_I(c, BUILD_TUPLE, n);
1974 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s,
1975 s->lineno))
1976 return 0;
1977 c->u->u_private = s->v.ClassDef.name;
1978 Py_INCREF(c->u->u_private);
1979 str = PyString_InternFromString("__name__");
1980 if (!str || !compiler_nameop(c, str, Load)) {
1981 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001982 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001983 return 0;
1984 }
1985
1986 Py_DECREF(str);
1987 str = PyString_InternFromString("__module__");
1988 if (!str || !compiler_nameop(c, str, Store)) {
1989 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001990 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001991 return 0;
1992 }
1993 Py_DECREF(str);
1994
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001995 if (!compiler_body(c, s->v.ClassDef.body)) {
1996 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001997 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001998 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00002000 ADDOP_IN_SCOPE(c, LOAD_LOCALS);
2001 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002002 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00002003 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004 if (co == NULL)
2005 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002006
2007 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00002008 Py_DECREF(co);
2009
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010 ADDOP_I(c, CALL_FUNCTION, 0);
2011 ADDOP(c, BUILD_CLASS);
2012 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2013 return 0;
2014 return 1;
2015}
2016
2017static int
2018compiler_lambda(struct compiler *c, expr_ty e)
2019{
2020 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002021 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002022 arguments_ty args = e->v.Lambda.args;
2023 assert(e->kind == Lambda_kind);
2024
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002025 if (!name) {
2026 name = PyString_InternFromString("<lambda>");
2027 if (!name)
2028 return 0;
2029 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002030
2031 if (args->defaults)
2032 VISIT_SEQ(c, expr, args->defaults);
2033 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2034 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002035
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002036 /* unpack nested arguments */
2037 compiler_arguments(c, args);
2038
2039 c->u->u_argcount = asdl_seq_LEN(args->args);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00002040 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2041 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002042 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00002043 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002044 if (co == NULL)
2045 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002046
2047 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00002048 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002049
2050 return 1;
2051}
2052
2053static int
2054compiler_print(struct compiler *c, stmt_ty s)
2055{
2056 int i, n;
2057 bool dest;
2058
2059 assert(s->kind == Print_kind);
2060 n = asdl_seq_LEN(s->v.Print.values);
2061 dest = false;
2062 if (s->v.Print.dest) {
2063 VISIT(c, expr, s->v.Print.dest);
2064 dest = true;
2065 }
2066 for (i = 0; i < n; i++) {
2067 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i);
2068 if (dest) {
2069 ADDOP(c, DUP_TOP);
2070 VISIT(c, expr, e);
2071 ADDOP(c, ROT_TWO);
2072 ADDOP(c, PRINT_ITEM_TO);
2073 }
2074 else {
2075 VISIT(c, expr, e);
2076 ADDOP(c, PRINT_ITEM);
2077 }
2078 }
2079 if (s->v.Print.nl) {
2080 if (dest)
2081 ADDOP(c, PRINT_NEWLINE_TO)
2082 else
2083 ADDOP(c, PRINT_NEWLINE)
2084 }
2085 else if (dest)
2086 ADDOP(c, POP_TOP);
2087 return 1;
2088}
2089
2090static int
2091compiler_if(struct compiler *c, stmt_ty s)
2092{
2093 basicblock *end, *next;
2094
2095 assert(s->kind == If_kind);
2096 end = compiler_new_block(c);
2097 if (end == NULL)
2098 return 0;
2099 next = compiler_new_block(c);
2100 if (next == NULL)
2101 return 0;
2102 VISIT(c, expr, s->v.If.test);
2103 ADDOP_JREL(c, JUMP_IF_FALSE, next);
2104 ADDOP(c, POP_TOP);
2105 VISIT_SEQ(c, stmt, s->v.If.body);
2106 ADDOP_JREL(c, JUMP_FORWARD, end);
2107 compiler_use_next_block(c, next);
2108 ADDOP(c, POP_TOP);
2109 if (s->v.If.orelse)
2110 VISIT_SEQ(c, stmt, s->v.If.orelse);
2111 compiler_use_next_block(c, end);
2112 return 1;
2113}
2114
2115static int
2116compiler_for(struct compiler *c, stmt_ty s)
2117{
2118 basicblock *start, *cleanup, *end;
2119
2120 start = compiler_new_block(c);
2121 cleanup = compiler_new_block(c);
2122 end = compiler_new_block(c);
2123 if (start == NULL || end == NULL || cleanup == NULL)
2124 return 0;
2125 ADDOP_JREL(c, SETUP_LOOP, end);
2126 if (!compiler_push_fblock(c, LOOP, start))
2127 return 0;
2128 VISIT(c, expr, s->v.For.iter);
2129 ADDOP(c, GET_ITER);
2130 compiler_use_next_block(c, start);
2131 ADDOP_JREL(c, FOR_ITER, cleanup);
2132 VISIT(c, expr, s->v.For.target);
2133 VISIT_SEQ(c, stmt, s->v.For.body);
2134 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2135 compiler_use_next_block(c, cleanup);
2136 ADDOP(c, POP_BLOCK);
2137 compiler_pop_fblock(c, LOOP, start);
2138 VISIT_SEQ(c, stmt, s->v.For.orelse);
2139 compiler_use_next_block(c, end);
2140 return 1;
2141}
2142
2143static int
2144compiler_while(struct compiler *c, stmt_ty s)
2145{
2146 basicblock *loop, *orelse, *end, *anchor = NULL;
2147 int constant = expr_constant(s->v.While.test);
2148
2149 if (constant == 0)
2150 return 1;
2151 loop = compiler_new_block(c);
2152 end = compiler_new_block(c);
2153 if (constant == -1) {
2154 anchor = compiler_new_block(c);
2155 if (anchor == NULL)
2156 return 0;
2157 }
2158 if (loop == NULL || end == NULL)
2159 return 0;
2160 if (s->v.While.orelse) {
2161 orelse = compiler_new_block(c);
2162 if (orelse == NULL)
2163 return 0;
2164 }
2165 else
2166 orelse = NULL;
2167
2168 ADDOP_JREL(c, SETUP_LOOP, end);
2169 compiler_use_next_block(c, loop);
2170 if (!compiler_push_fblock(c, LOOP, loop))
2171 return 0;
2172 if (constant == -1) {
2173 VISIT(c, expr, s->v.While.test);
2174 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
2175 ADDOP(c, POP_TOP);
2176 }
2177 VISIT_SEQ(c, stmt, s->v.While.body);
2178 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
2179
2180 /* XXX should the two POP instructions be in a separate block
2181 if there is no else clause ?
2182 */
2183
2184 if (constant == -1) {
2185 compiler_use_next_block(c, anchor);
2186 ADDOP(c, POP_TOP);
2187 ADDOP(c, POP_BLOCK);
2188 }
2189 compiler_pop_fblock(c, LOOP, loop);
2190 if (orelse != NULL)
2191 VISIT_SEQ(c, stmt, s->v.While.orelse);
2192 compiler_use_next_block(c, end);
2193
2194 return 1;
2195}
2196
2197static int
2198compiler_continue(struct compiler *c)
2199{
2200 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
2201 int i;
2202
2203 if (!c->u->u_nfblocks)
2204 return compiler_error(c, LOOP_ERROR_MSG);
2205 i = c->u->u_nfblocks - 1;
2206 switch (c->u->u_fblock[i].fb_type) {
2207 case LOOP:
2208 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
2209 break;
2210 case EXCEPT:
2211 case FINALLY_TRY:
2212 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP)
2213 ;
2214 if (i == -1)
2215 return compiler_error(c, LOOP_ERROR_MSG);
2216 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
2217 break;
2218 case FINALLY_END:
2219 return compiler_error(c,
2220 "'continue' not supported inside 'finally' clause");
2221 }
2222
2223 return 1;
2224}
2225
2226/* Code generated for "try: <body> finally: <finalbody>" is as follows:
2227
2228 SETUP_FINALLY L
2229 <code for body>
2230 POP_BLOCK
2231 LOAD_CONST <None>
2232 L: <code for finalbody>
2233 END_FINALLY
2234
2235 The special instructions use the block stack. Each block
2236 stack entry contains the instruction that created it (here
2237 SETUP_FINALLY), the level of the value stack at the time the
2238 block stack entry was created, and a label (here L).
2239
2240 SETUP_FINALLY:
2241 Pushes the current value stack level and the label
2242 onto the block stack.
2243 POP_BLOCK:
2244 Pops en entry from the block stack, and pops the value
2245 stack until its level is the same as indicated on the
2246 block stack. (The label is ignored.)
2247 END_FINALLY:
2248 Pops a variable number of entries from the *value* stack
2249 and re-raises the exception they specify. The number of
2250 entries popped depends on the (pseudo) exception type.
2251
2252 The block stack is unwound when an exception is raised:
2253 when a SETUP_FINALLY entry is found, the exception is pushed
2254 onto the value stack (and the exception condition is cleared),
2255 and the interpreter jumps to the label gotten from the block
2256 stack.
2257*/
2258
2259static int
2260compiler_try_finally(struct compiler *c, stmt_ty s)
2261{
2262 basicblock *body, *end;
2263 body = compiler_new_block(c);
2264 end = compiler_new_block(c);
2265 if (body == NULL || end == NULL)
2266 return 0;
2267
2268 ADDOP_JREL(c, SETUP_FINALLY, end);
2269 compiler_use_next_block(c, body);
2270 if (!compiler_push_fblock(c, FINALLY_TRY, body))
2271 return 0;
2272 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
2273 ADDOP(c, POP_BLOCK);
2274 compiler_pop_fblock(c, FINALLY_TRY, body);
2275
2276 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2277 compiler_use_next_block(c, end);
2278 if (!compiler_push_fblock(c, FINALLY_END, end))
2279 return 0;
2280 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
2281 ADDOP(c, END_FINALLY);
2282 compiler_pop_fblock(c, FINALLY_END, end);
2283
2284 return 1;
2285}
2286
2287/*
2288 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
2289 (The contents of the value stack is shown in [], with the top
2290 at the right; 'tb' is trace-back info, 'val' the exception's
2291 associated value, and 'exc' the exception.)
2292
2293 Value stack Label Instruction Argument
2294 [] SETUP_EXCEPT L1
2295 [] <code for S>
2296 [] POP_BLOCK
2297 [] JUMP_FORWARD L0
2298
2299 [tb, val, exc] L1: DUP )
2300 [tb, val, exc, exc] <evaluate E1> )
2301 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2302 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
2303 [tb, val, exc, 1] POP )
2304 [tb, val, exc] POP
2305 [tb, val] <assign to V1> (or POP if no V1)
2306 [tb] POP
2307 [] <code for S1>
2308 JUMP_FORWARD L0
2309
2310 [tb, val, exc, 0] L2: POP
2311 [tb, val, exc] DUP
2312 .............................etc.......................
2313
2314 [tb, val, exc, 0] Ln+1: POP
2315 [tb, val, exc] END_FINALLY # re-raise exception
2316
2317 [] L0: <next statement>
2318
2319 Of course, parts are not generated if Vi or Ei is not present.
2320*/
2321static int
2322compiler_try_except(struct compiler *c, stmt_ty s)
2323{
2324 basicblock *body, *orelse, *except, *end;
2325 int i, n;
2326
2327 body = compiler_new_block(c);
2328 except = compiler_new_block(c);
2329 orelse = compiler_new_block(c);
2330 end = compiler_new_block(c);
2331 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
2332 return 0;
2333 ADDOP_JREL(c, SETUP_EXCEPT, except);
2334 compiler_use_next_block(c, body);
2335 if (!compiler_push_fblock(c, EXCEPT, body))
2336 return 0;
2337 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
2338 ADDOP(c, POP_BLOCK);
2339 compiler_pop_fblock(c, EXCEPT, body);
2340 ADDOP_JREL(c, JUMP_FORWARD, orelse);
2341 n = asdl_seq_LEN(s->v.TryExcept.handlers);
2342 compiler_use_next_block(c, except);
2343 for (i = 0; i < n; i++) {
2344 excepthandler_ty handler = asdl_seq_GET(
2345 s->v.TryExcept.handlers, i);
2346 if (!handler->type && i < n-1)
2347 return compiler_error(c, "default 'except:' must be last");
2348 except = compiler_new_block(c);
2349 if (except == NULL)
2350 return 0;
2351 if (handler->type) {
2352 ADDOP(c, DUP_TOP);
2353 VISIT(c, expr, handler->type);
2354 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2355 ADDOP_JREL(c, JUMP_IF_FALSE, except);
2356 ADDOP(c, POP_TOP);
2357 }
2358 ADDOP(c, POP_TOP);
2359 if (handler->name) {
2360 VISIT(c, expr, handler->name);
2361 }
2362 else {
2363 ADDOP(c, POP_TOP);
2364 }
2365 ADDOP(c, POP_TOP);
2366 VISIT_SEQ(c, stmt, handler->body);
2367 ADDOP_JREL(c, JUMP_FORWARD, end);
2368 compiler_use_next_block(c, except);
2369 if (handler->type)
2370 ADDOP(c, POP_TOP);
2371 }
2372 ADDOP(c, END_FINALLY);
2373 compiler_use_next_block(c, orelse);
2374 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2375 compiler_use_next_block(c, end);
2376 return 1;
2377}
2378
2379static int
2380compiler_import_as(struct compiler *c, identifier name, identifier asname)
2381{
2382 /* The IMPORT_NAME opcode was already generated. This function
2383 merely needs to bind the result to a name.
2384
2385 If there is a dot in name, we need to split it and emit a
2386 LOAD_ATTR for each name.
2387 */
2388 const char *src = PyString_AS_STRING(name);
2389 const char *dot = strchr(src, '.');
2390 if (dot) {
2391 /* Consume the base module name to get the first attribute */
2392 src = dot + 1;
2393 while (dot) {
2394 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00002395 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002396 dot = strchr(src, '.');
Armin Rigo31441302005-10-21 12:57:31 +00002397 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002398 dot ? dot - src : strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002399 if (!attr)
2400 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002401 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002402 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002403 src = dot + 1;
2404 }
2405 }
2406 return compiler_nameop(c, asname, Store);
2407}
2408
2409static int
2410compiler_import(struct compiler *c, stmt_ty s)
2411{
2412 /* The Import node stores a module name like a.b.c as a single
2413 string. This is convenient for all cases except
2414 import a.b.c as d
2415 where we need to parse that string to extract the individual
2416 module names.
2417 XXX Perhaps change the representation to make this case simpler?
2418 */
2419 int i, n = asdl_seq_LEN(s->v.Import.names);
2420 for (i = 0; i < n; i++) {
2421 alias_ty alias = asdl_seq_GET(s->v.Import.names, i);
2422 int r;
2423
2424 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2425 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2426
2427 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002428 r = compiler_import_as(c, alias->name, alias->asname);
2429 if (!r)
2430 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002431 }
2432 else {
2433 identifier tmp = alias->name;
2434 const char *base = PyString_AS_STRING(alias->name);
2435 char *dot = strchr(base, '.');
2436 if (dot)
2437 tmp = PyString_FromStringAndSize(base,
2438 dot - base);
2439 r = compiler_nameop(c, tmp, Store);
2440 if (dot) {
2441 Py_DECREF(tmp);
2442 }
2443 if (!r)
2444 return r;
2445 }
2446 }
2447 return 1;
2448}
2449
2450static int
2451compiler_from_import(struct compiler *c, stmt_ty s)
2452{
2453 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002454
2455 PyObject *names = PyTuple_New(n);
2456 if (!names)
2457 return 0;
2458
2459 /* build up the names */
2460 for (i = 0; i < n; i++) {
2461 alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
2462 Py_INCREF(alias->name);
2463 PyTuple_SET_ITEM(names, i, alias->name);
2464 }
2465
2466 if (s->lineno > c->c_future->ff_lineno) {
2467 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
2468 "__future__")) {
2469 Py_DECREF(names);
2470 return compiler_error(c,
2471 "from __future__ imports must occur "
2472 "at the beginning of the file");
2473
2474 }
2475 }
2476
2477 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002478 Py_DECREF(names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002479 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2480 for (i = 0; i < n; i++) {
2481 alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
2482 identifier store_name;
2483
2484 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
2485 assert(n == 1);
2486 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002487 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002488 }
2489
2490 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2491 store_name = alias->name;
2492 if (alias->asname)
2493 store_name = alias->asname;
2494
2495 if (!compiler_nameop(c, store_name, Store)) {
2496 Py_DECREF(names);
2497 return 0;
2498 }
2499 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002500 /* remove imported module */
2501 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002502 return 1;
2503}
2504
2505static int
2506compiler_assert(struct compiler *c, stmt_ty s)
2507{
2508 static PyObject *assertion_error = NULL;
2509 basicblock *end;
2510
2511 if (Py_OptimizeFlag)
2512 return 1;
2513 if (assertion_error == NULL) {
2514 assertion_error = PyString_FromString("AssertionError");
2515 if (assertion_error == NULL)
2516 return 0;
2517 }
2518 VISIT(c, expr, s->v.Assert.test);
2519 end = compiler_new_block(c);
2520 if (end == NULL)
2521 return 0;
2522 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2523 ADDOP(c, POP_TOP);
2524 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2525 if (s->v.Assert.msg) {
2526 VISIT(c, expr, s->v.Assert.msg);
2527 ADDOP_I(c, RAISE_VARARGS, 2);
2528 }
2529 else {
2530 ADDOP_I(c, RAISE_VARARGS, 1);
2531 }
2532 compiler_use_block(c, end);
2533 ADDOP(c, POP_TOP);
2534 return 1;
2535}
2536
2537static int
2538compiler_visit_stmt(struct compiler *c, stmt_ty s)
2539{
2540 int i, n;
2541
2542 c->u->u_lineno = s->lineno;
2543 c->u->u_lineno_set = false;
2544 switch (s->kind) {
2545 case FunctionDef_kind:
2546 return compiler_function(c, s);
2547 case ClassDef_kind:
2548 return compiler_class(c, s);
2549 case Return_kind:
2550 if (c->u->u_ste->ste_type != FunctionBlock)
2551 return compiler_error(c, "'return' outside function");
2552 if (s->v.Return.value) {
2553 if (c->u->u_ste->ste_generator) {
2554 return compiler_error(c,
2555 "'return' with argument inside generator");
2556 }
2557 VISIT(c, expr, s->v.Return.value);
2558 }
2559 else
2560 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2561 ADDOP(c, RETURN_VALUE);
2562 break;
2563 case Delete_kind:
2564 VISIT_SEQ(c, expr, s->v.Delete.targets)
2565 break;
2566 case Assign_kind:
2567 n = asdl_seq_LEN(s->v.Assign.targets);
2568 VISIT(c, expr, s->v.Assign.value);
2569 for (i = 0; i < n; i++) {
2570 if (i < n - 1)
2571 ADDOP(c, DUP_TOP);
2572 VISIT(c, expr,
2573 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2574 }
2575 break;
2576 case AugAssign_kind:
2577 return compiler_augassign(c, s);
2578 case Print_kind:
2579 return compiler_print(c, s);
2580 case For_kind:
2581 return compiler_for(c, s);
2582 case While_kind:
2583 return compiler_while(c, s);
2584 case If_kind:
2585 return compiler_if(c, s);
2586 case Raise_kind:
2587 n = 0;
2588 if (s->v.Raise.type) {
2589 VISIT(c, expr, s->v.Raise.type);
2590 n++;
2591 if (s->v.Raise.inst) {
2592 VISIT(c, expr, s->v.Raise.inst);
2593 n++;
2594 if (s->v.Raise.tback) {
2595 VISIT(c, expr, s->v.Raise.tback);
2596 n++;
2597 }
2598 }
2599 }
2600 ADDOP_I(c, RAISE_VARARGS, n);
2601 break;
2602 case TryExcept_kind:
2603 return compiler_try_except(c, s);
2604 case TryFinally_kind:
2605 return compiler_try_finally(c, s);
2606 case Assert_kind:
2607 return compiler_assert(c, s);
2608 case Import_kind:
2609 return compiler_import(c, s);
2610 case ImportFrom_kind:
2611 return compiler_from_import(c, s);
2612 case Exec_kind:
2613 VISIT(c, expr, s->v.Exec.body);
2614 if (s->v.Exec.globals) {
2615 VISIT(c, expr, s->v.Exec.globals);
2616 if (s->v.Exec.locals) {
2617 VISIT(c, expr, s->v.Exec.locals);
2618 } else {
2619 ADDOP(c, DUP_TOP);
2620 }
2621 } else {
2622 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2623 ADDOP(c, DUP_TOP);
2624 }
2625 ADDOP(c, EXEC_STMT);
2626 break;
2627 case Global_kind:
2628 break;
2629 case Expr_kind:
2630 VISIT(c, expr, s->v.Expr.value);
2631 if (c->c_interactive && c->c_nestlevel <= 1) {
2632 ADDOP(c, PRINT_EXPR);
2633 }
2634 else {
2635 ADDOP(c, POP_TOP);
2636 }
2637 break;
2638 case Pass_kind:
2639 break;
2640 case Break_kind:
2641 if (!c->u->u_nfblocks)
2642 return compiler_error(c, "'break' outside loop");
2643 ADDOP(c, BREAK_LOOP);
2644 break;
2645 case Continue_kind:
2646 return compiler_continue(c);
2647 }
2648 return 1;
2649}
2650
2651static int
2652unaryop(unaryop_ty op)
2653{
2654 switch (op) {
2655 case Invert:
2656 return UNARY_INVERT;
2657 case Not:
2658 return UNARY_NOT;
2659 case UAdd:
2660 return UNARY_POSITIVE;
2661 case USub:
2662 return UNARY_NEGATIVE;
2663 }
2664 return 0;
2665}
2666
2667static int
2668binop(struct compiler *c, operator_ty op)
2669{
2670 switch (op) {
2671 case Add:
2672 return BINARY_ADD;
2673 case Sub:
2674 return BINARY_SUBTRACT;
2675 case Mult:
2676 return BINARY_MULTIPLY;
2677 case Div:
2678 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2679 return BINARY_TRUE_DIVIDE;
2680 else
2681 return BINARY_DIVIDE;
2682 case Mod:
2683 return BINARY_MODULO;
2684 case Pow:
2685 return BINARY_POWER;
2686 case LShift:
2687 return BINARY_LSHIFT;
2688 case RShift:
2689 return BINARY_RSHIFT;
2690 case BitOr:
2691 return BINARY_OR;
2692 case BitXor:
2693 return BINARY_XOR;
2694 case BitAnd:
2695 return BINARY_AND;
2696 case FloorDiv:
2697 return BINARY_FLOOR_DIVIDE;
2698 }
2699 return 0;
2700}
2701
2702static int
2703cmpop(cmpop_ty op)
2704{
2705 switch (op) {
2706 case Eq:
2707 return PyCmp_EQ;
2708 case NotEq:
2709 return PyCmp_NE;
2710 case Lt:
2711 return PyCmp_LT;
2712 case LtE:
2713 return PyCmp_LE;
2714 case Gt:
2715 return PyCmp_GT;
2716 case GtE:
2717 return PyCmp_GE;
2718 case Is:
2719 return PyCmp_IS;
2720 case IsNot:
2721 return PyCmp_IS_NOT;
2722 case In:
2723 return PyCmp_IN;
2724 case NotIn:
2725 return PyCmp_NOT_IN;
2726 }
2727 return PyCmp_BAD;
2728}
2729
2730static int
2731inplace_binop(struct compiler *c, operator_ty op)
2732{
2733 switch (op) {
2734 case Add:
2735 return INPLACE_ADD;
2736 case Sub:
2737 return INPLACE_SUBTRACT;
2738 case Mult:
2739 return INPLACE_MULTIPLY;
2740 case Div:
2741 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2742 return INPLACE_TRUE_DIVIDE;
2743 else
2744 return INPLACE_DIVIDE;
2745 case Mod:
2746 return INPLACE_MODULO;
2747 case Pow:
2748 return INPLACE_POWER;
2749 case LShift:
2750 return INPLACE_LSHIFT;
2751 case RShift:
2752 return INPLACE_RSHIFT;
2753 case BitOr:
2754 return INPLACE_OR;
2755 case BitXor:
2756 return INPLACE_XOR;
2757 case BitAnd:
2758 return INPLACE_AND;
2759 case FloorDiv:
2760 return INPLACE_FLOOR_DIVIDE;
2761 }
Neal Norwitz4737b232005-11-19 23:58:29 +00002762 PyErr_Format(PyExc_SystemError,
2763 "inplace binary op %d should not be possible",
2764 op);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002765 return 0;
2766}
2767
2768static int
2769compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2770{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002771 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002772 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2773
2774 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002775 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776 /* XXX AugStore isn't used anywhere! */
2777
2778 /* First check for assignment to __debug__. Param? */
2779 if ((ctx == Store || ctx == AugStore || ctx == Del)
2780 && !strcmp(PyString_AS_STRING(name), "__debug__")) {
2781 return compiler_error(c, "can not assign to __debug__");
2782 }
2783
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002784 mangled = _Py_Mangle(c->u->u_private, name);
2785 if (!mangled)
2786 return 0;
2787
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002788 op = 0;
2789 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002790 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002791 switch (scope) {
2792 case FREE:
2793 dict = c->u->u_freevars;
2794 optype = OP_DEREF;
2795 break;
2796 case CELL:
2797 dict = c->u->u_cellvars;
2798 optype = OP_DEREF;
2799 break;
2800 case LOCAL:
2801 if (c->u->u_ste->ste_type == FunctionBlock)
2802 optype = OP_FAST;
2803 break;
2804 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002805 if (c->u->u_ste->ste_type == FunctionBlock &&
2806 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002807 optype = OP_GLOBAL;
2808 break;
2809 case GLOBAL_EXPLICIT:
2810 optype = OP_GLOBAL;
2811 break;
2812 }
2813
2814 /* XXX Leave assert here, but handle __doc__ and the like better */
2815 assert(scope || PyString_AS_STRING(name)[0] == '_');
2816
2817 switch (optype) {
2818 case OP_DEREF:
2819 switch (ctx) {
2820 case Load: op = LOAD_DEREF; break;
2821 case Store: op = STORE_DEREF; break;
2822 case AugLoad:
2823 case AugStore:
2824 break;
2825 case Del:
2826 PyErr_Format(PyExc_SyntaxError,
2827 "can not delete variable '%s' referenced "
2828 "in nested scope",
2829 PyString_AS_STRING(name));
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002830 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002832 case Param:
Neal Norwitz4737b232005-11-19 23:58:29 +00002833 PyErr_SetString(PyExc_SystemError,
2834 "param invalid for deref variable");
2835 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002836 }
2837 break;
2838 case OP_FAST:
2839 switch (ctx) {
2840 case Load: op = LOAD_FAST; break;
2841 case Store: op = STORE_FAST; break;
2842 case Del: op = DELETE_FAST; break;
2843 case AugLoad:
2844 case AugStore:
2845 break;
2846 case Param:
Neal Norwitz4737b232005-11-19 23:58:29 +00002847 PyErr_SetString(PyExc_SystemError,
2848 "param invalid for local variable");
2849 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002850 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002851 ADDOP_O(c, op, mangled, varnames);
2852 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853 return 1;
2854 case OP_GLOBAL:
2855 switch (ctx) {
2856 case Load: op = LOAD_GLOBAL; break;
2857 case Store: op = STORE_GLOBAL; break;
2858 case Del: op = DELETE_GLOBAL; break;
2859 case AugLoad:
2860 case AugStore:
2861 break;
2862 case Param:
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 Norwitz4737b232005-11-19 23:58:29 +00002877 PyErr_SetString(PyExc_SystemError,
2878 "param invalid for name variable");
2879 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002880 }
2881 break;
2882 }
2883
2884 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002885 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002886 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002887 if (arg < 0)
2888 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002889 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002890}
2891
2892static int
2893compiler_boolop(struct compiler *c, expr_ty e)
2894{
2895 basicblock *end;
2896 int jumpi, i, n;
2897 asdl_seq *s;
2898
2899 assert(e->kind == BoolOp_kind);
2900 if (e->v.BoolOp.op == And)
2901 jumpi = JUMP_IF_FALSE;
2902 else
2903 jumpi = JUMP_IF_TRUE;
2904 end = compiler_new_block(c);
2905 if (end < 0)
2906 return 0;
2907 s = e->v.BoolOp.values;
2908 n = asdl_seq_LEN(s) - 1;
2909 for (i = 0; i < n; ++i) {
2910 VISIT(c, expr, asdl_seq_GET(s, i));
2911 ADDOP_JREL(c, jumpi, end);
2912 ADDOP(c, POP_TOP)
2913 }
2914 VISIT(c, expr, asdl_seq_GET(s, n));
2915 compiler_use_next_block(c, end);
2916 return 1;
2917}
2918
2919static int
2920compiler_list(struct compiler *c, expr_ty e)
2921{
2922 int n = asdl_seq_LEN(e->v.List.elts);
2923 if (e->v.List.ctx == Store) {
2924 ADDOP_I(c, UNPACK_SEQUENCE, n);
2925 }
2926 VISIT_SEQ(c, expr, e->v.List.elts);
2927 if (e->v.List.ctx == Load) {
2928 ADDOP_I(c, BUILD_LIST, n);
2929 }
2930 return 1;
2931}
2932
2933static int
2934compiler_tuple(struct compiler *c, expr_ty e)
2935{
2936 int n = asdl_seq_LEN(e->v.Tuple.elts);
2937 if (e->v.Tuple.ctx == Store) {
2938 ADDOP_I(c, UNPACK_SEQUENCE, n);
2939 }
2940 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2941 if (e->v.Tuple.ctx == Load) {
2942 ADDOP_I(c, BUILD_TUPLE, n);
2943 }
2944 return 1;
2945}
2946
2947static int
2948compiler_compare(struct compiler *c, expr_ty e)
2949{
2950 int i, n;
2951 basicblock *cleanup = NULL;
2952
2953 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2954 VISIT(c, expr, e->v.Compare.left);
2955 n = asdl_seq_LEN(e->v.Compare.ops);
2956 assert(n > 0);
2957 if (n > 1) {
2958 cleanup = compiler_new_block(c);
2959 if (cleanup == NULL)
2960 return 0;
2961 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, 0));
2962 }
2963 for (i = 1; i < n; i++) {
2964 ADDOP(c, DUP_TOP);
2965 ADDOP(c, ROT_THREE);
2966 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
2967 ADDOP_I(c, COMPARE_OP,
2968 cmpop((cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i - 1)));
2969 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2970 NEXT_BLOCK(c);
2971 ADDOP(c, POP_TOP);
2972 if (i < (n - 1))
2973 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, i));
2974 }
2975 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, n - 1));
2976 ADDOP_I(c, COMPARE_OP,
2977 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
2978 cmpop((cmpop_ty)asdl_seq_GET(e->v.Compare.ops, n - 1)));
2979 if (n > 1) {
2980 basicblock *end = compiler_new_block(c);
2981 if (end == NULL)
2982 return 0;
2983 ADDOP_JREL(c, JUMP_FORWARD, end);
2984 compiler_use_next_block(c, cleanup);
2985 ADDOP(c, ROT_TWO);
2986 ADDOP(c, POP_TOP);
2987 compiler_use_next_block(c, end);
2988 }
2989 return 1;
2990}
2991
2992static int
2993compiler_call(struct compiler *c, expr_ty e)
2994{
2995 int n, code = 0;
2996
2997 VISIT(c, expr, e->v.Call.func);
2998 n = asdl_seq_LEN(e->v.Call.args);
2999 VISIT_SEQ(c, expr, e->v.Call.args);
3000 if (e->v.Call.keywords) {
3001 VISIT_SEQ(c, keyword, e->v.Call.keywords);
3002 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
3003 }
3004 if (e->v.Call.starargs) {
3005 VISIT(c, expr, e->v.Call.starargs);
3006 code |= 1;
3007 }
3008 if (e->v.Call.kwargs) {
3009 VISIT(c, expr, e->v.Call.kwargs);
3010 code |= 2;
3011 }
3012 switch (code) {
3013 case 0:
3014 ADDOP_I(c, CALL_FUNCTION, n);
3015 break;
3016 case 1:
3017 ADDOP_I(c, CALL_FUNCTION_VAR, n);
3018 break;
3019 case 2:
3020 ADDOP_I(c, CALL_FUNCTION_KW, n);
3021 break;
3022 case 3:
3023 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
3024 break;
3025 }
3026 return 1;
3027}
3028
3029static int
3030compiler_listcomp_generator(struct compiler *c, PyObject *tmpname,
3031 asdl_seq *generators, int gen_index,
3032 expr_ty elt)
3033{
3034 /* generate code for the iterator, then each of the ifs,
3035 and then write to the element */
3036
3037 comprehension_ty l;
3038 basicblock *start, *anchor, *skip, *if_cleanup;
3039 int i, n;
3040
3041 start = compiler_new_block(c);
3042 skip = compiler_new_block(c);
3043 if_cleanup = compiler_new_block(c);
3044 anchor = compiler_new_block(c);
3045
3046 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3047 anchor == NULL)
3048 return 0;
3049
3050 l = asdl_seq_GET(generators, gen_index);
3051 VISIT(c, expr, l->iter);
3052 ADDOP(c, GET_ITER);
3053 compiler_use_next_block(c, start);
3054 ADDOP_JREL(c, FOR_ITER, anchor);
3055 NEXT_BLOCK(c);
3056 VISIT(c, expr, l->target);
3057
3058 /* XXX this needs to be cleaned up...a lot! */
3059 n = asdl_seq_LEN(l->ifs);
3060 for (i = 0; i < n; i++) {
3061 expr_ty e = asdl_seq_GET(l->ifs, i);
3062 VISIT(c, expr, e);
3063 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
3064 NEXT_BLOCK(c);
3065 ADDOP(c, POP_TOP);
3066 }
3067
3068 if (++gen_index < asdl_seq_LEN(generators))
3069 if (!compiler_listcomp_generator(c, tmpname,
3070 generators, gen_index, elt))
3071 return 0;
3072
3073 /* only append after the last for generator */
3074 if (gen_index >= asdl_seq_LEN(generators)) {
3075 if (!compiler_nameop(c, tmpname, Load))
3076 return 0;
3077 VISIT(c, expr, elt);
3078 ADDOP_I(c, CALL_FUNCTION, 1);
3079 ADDOP(c, POP_TOP);
3080
3081 compiler_use_next_block(c, skip);
3082 }
3083 for (i = 0; i < n; i++) {
3084 ADDOP_I(c, JUMP_FORWARD, 1);
3085 if (i == 0)
3086 compiler_use_next_block(c, if_cleanup);
3087 ADDOP(c, POP_TOP);
3088 }
3089 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3090 compiler_use_next_block(c, anchor);
3091 /* delete the append method added to locals */
3092 if (gen_index == 1)
3093 if (!compiler_nameop(c, tmpname, Del))
3094 return 0;
3095
3096 return 1;
3097}
3098
3099static int
3100compiler_listcomp(struct compiler *c, expr_ty e)
3101{
3102 char tmpname[256];
3103 identifier tmp;
3104 int rc = 0;
3105 static identifier append;
3106 asdl_seq *generators = e->v.ListComp.generators;
3107
3108 assert(e->kind == ListComp_kind);
3109 if (!append) {
3110 append = PyString_InternFromString("append");
3111 if (!append)
3112 return 0;
3113 }
3114 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
3115 tmp = PyString_FromString(tmpname);
3116 if (!tmp)
3117 return 0;
3118 ADDOP_I(c, BUILD_LIST, 0);
3119 ADDOP(c, DUP_TOP);
3120 ADDOP_O(c, LOAD_ATTR, append, names);
3121 if (compiler_nameop(c, tmp, Store))
3122 rc = compiler_listcomp_generator(c, tmp, generators, 0,
3123 e->v.ListComp.elt);
3124 Py_DECREF(tmp);
3125 return rc;
3126}
3127
3128static int
3129compiler_genexp_generator(struct compiler *c,
3130 asdl_seq *generators, int gen_index,
3131 expr_ty elt)
3132{
3133 /* generate code for the iterator, then each of the ifs,
3134 and then write to the element */
3135
3136 comprehension_ty ge;
3137 basicblock *start, *anchor, *skip, *if_cleanup, *end;
3138 int i, n;
3139
3140 start = compiler_new_block(c);
3141 skip = compiler_new_block(c);
3142 if_cleanup = compiler_new_block(c);
3143 anchor = compiler_new_block(c);
3144 end = compiler_new_block(c);
3145
3146 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3147 anchor == NULL || end == NULL)
3148 return 0;
3149
3150 ge = asdl_seq_GET(generators, gen_index);
3151 ADDOP_JREL(c, SETUP_LOOP, end);
3152 if (!compiler_push_fblock(c, LOOP, start))
3153 return 0;
3154
3155 if (gen_index == 0) {
3156 /* Receive outermost iter as an implicit argument */
3157 c->u->u_argcount = 1;
3158 ADDOP_I(c, LOAD_FAST, 0);
3159 }
3160 else {
3161 /* Sub-iter - calculate on the fly */
3162 VISIT(c, expr, ge->iter);
3163 ADDOP(c, GET_ITER);
3164 }
3165 compiler_use_next_block(c, start);
3166 ADDOP_JREL(c, FOR_ITER, anchor);
3167 NEXT_BLOCK(c);
3168 VISIT(c, expr, ge->target);
3169
3170 /* XXX this needs to be cleaned up...a lot! */
3171 n = asdl_seq_LEN(ge->ifs);
3172 for (i = 0; i < n; i++) {
3173 expr_ty e = asdl_seq_GET(ge->ifs, i);
3174 VISIT(c, expr, e);
3175 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
3176 NEXT_BLOCK(c);
3177 ADDOP(c, POP_TOP);
3178 }
3179
3180 if (++gen_index < asdl_seq_LEN(generators))
3181 if (!compiler_genexp_generator(c, generators, gen_index, elt))
3182 return 0;
3183
3184 /* only append after the last 'for' generator */
3185 if (gen_index >= asdl_seq_LEN(generators)) {
3186 VISIT(c, expr, elt);
3187 ADDOP(c, YIELD_VALUE);
3188 ADDOP(c, POP_TOP);
3189
3190 compiler_use_next_block(c, skip);
3191 }
3192 for (i = 0; i < n; i++) {
3193 ADDOP_I(c, JUMP_FORWARD, 1);
3194 if (i == 0)
3195 compiler_use_next_block(c, if_cleanup);
3196
3197 ADDOP(c, POP_TOP);
3198 }
3199 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3200 compiler_use_next_block(c, anchor);
3201 ADDOP(c, POP_BLOCK);
3202 compiler_pop_fblock(c, LOOP, start);
3203 compiler_use_next_block(c, end);
3204
3205 return 1;
3206}
3207
3208static int
3209compiler_genexp(struct compiler *c, expr_ty e)
3210{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00003211 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003212 PyCodeObject *co;
3213 expr_ty outermost_iter = ((comprehension_ty)
3214 (asdl_seq_GET(e->v.GeneratorExp.generators,
3215 0)))->iter;
3216
Nick Coghlan944d3eb2005-11-16 12:46:55 +00003217 if (!name) {
3218 name = PyString_FromString("<genexpr>");
3219 if (!name)
3220 return 0;
3221 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003222
3223 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
3224 return 0;
3225 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
3226 e->v.GeneratorExp.elt);
3227 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00003228 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003229 if (co == NULL)
3230 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003231
3232 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00003233 Py_DECREF(co);
3234
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003235 VISIT(c, expr, outermost_iter);
3236 ADDOP(c, GET_ITER);
3237 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003238
3239 return 1;
3240}
3241
3242static int
3243compiler_visit_keyword(struct compiler *c, keyword_ty k)
3244{
3245 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3246 VISIT(c, expr, k->value);
3247 return 1;
3248}
3249
3250/* Test whether expression is constant. For constants, report
3251 whether they are true or false.
3252
3253 Return values: 1 for true, 0 for false, -1 for non-constant.
3254 */
3255
3256static int
3257expr_constant(expr_ty e)
3258{
3259 switch (e->kind) {
3260 case Num_kind:
3261 return PyObject_IsTrue(e->v.Num.n);
3262 case Str_kind:
3263 return PyObject_IsTrue(e->v.Str.s);
3264 default:
3265 return -1;
3266 }
3267}
3268
3269static int
3270compiler_visit_expr(struct compiler *c, expr_ty e)
3271{
3272 int i, n;
3273
3274 if (e->lineno > c->u->u_lineno) {
3275 c->u->u_lineno = e->lineno;
3276 c->u->u_lineno_set = false;
3277 }
3278 switch (e->kind) {
3279 case BoolOp_kind:
3280 return compiler_boolop(c, e);
3281 case BinOp_kind:
3282 VISIT(c, expr, e->v.BinOp.left);
3283 VISIT(c, expr, e->v.BinOp.right);
3284 ADDOP(c, binop(c, e->v.BinOp.op));
3285 break;
3286 case UnaryOp_kind:
3287 VISIT(c, expr, e->v.UnaryOp.operand);
3288 ADDOP(c, unaryop(e->v.UnaryOp.op));
3289 break;
3290 case Lambda_kind:
3291 return compiler_lambda(c, e);
3292 case Dict_kind:
3293 /* XXX get rid of arg? */
3294 ADDOP_I(c, BUILD_MAP, 0);
3295 n = asdl_seq_LEN(e->v.Dict.values);
3296 /* We must arrange things just right for STORE_SUBSCR.
3297 It wants the stack to look like (value) (dict) (key) */
3298 for (i = 0; i < n; i++) {
3299 ADDOP(c, DUP_TOP);
3300 VISIT(c, expr, asdl_seq_GET(e->v.Dict.values, i));
3301 ADDOP(c, ROT_TWO);
3302 VISIT(c, expr, asdl_seq_GET(e->v.Dict.keys, i));
3303 ADDOP(c, STORE_SUBSCR);
3304 }
3305 break;
3306 case ListComp_kind:
3307 return compiler_listcomp(c, e);
3308 case GeneratorExp_kind:
3309 return compiler_genexp(c, e);
3310 case Yield_kind:
3311 if (c->u->u_ste->ste_type != FunctionBlock)
3312 return compiler_error(c, "'yield' outside function");
3313 /*
3314 for (i = 0; i < c->u->u_nfblocks; i++) {
3315 if (c->u->u_fblock[i].fb_type == FINALLY_TRY)
3316 return compiler_error(
3317 c, "'yield' not allowed in a 'try' "
3318 "block with a 'finally' clause");
3319 }
3320 */
3321 if (e->v.Yield.value) {
3322 VISIT(c, expr, e->v.Yield.value);
3323 }
3324 else {
3325 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3326 }
3327 ADDOP(c, YIELD_VALUE);
3328 break;
3329 case Compare_kind:
3330 return compiler_compare(c, e);
3331 case Call_kind:
3332 return compiler_call(c, e);
3333 case Repr_kind:
3334 VISIT(c, expr, e->v.Repr.value);
3335 ADDOP(c, UNARY_CONVERT);
3336 break;
3337 case Num_kind:
3338 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3339 break;
3340 case Str_kind:
3341 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3342 break;
3343 /* The following exprs can be assignment targets. */
3344 case Attribute_kind:
3345 if (e->v.Attribute.ctx != AugStore)
3346 VISIT(c, expr, e->v.Attribute.value);
3347 switch (e->v.Attribute.ctx) {
3348 case AugLoad:
3349 ADDOP(c, DUP_TOP);
3350 /* Fall through to load */
3351 case Load:
3352 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3353 break;
3354 case AugStore:
3355 ADDOP(c, ROT_TWO);
3356 /* Fall through to save */
3357 case Store:
3358 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3359 break;
3360 case Del:
3361 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3362 break;
3363 case Param:
Neal Norwitz4737b232005-11-19 23:58:29 +00003364 PyErr_SetString(PyExc_SystemError,
3365 "param invalid in attribute expression");
3366 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003367 }
3368 break;
3369 case Subscript_kind:
3370 switch (e->v.Subscript.ctx) {
3371 case AugLoad:
3372 VISIT(c, expr, e->v.Subscript.value);
3373 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3374 break;
3375 case Load:
3376 VISIT(c, expr, e->v.Subscript.value);
3377 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3378 break;
3379 case AugStore:
3380 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3381 break;
3382 case Store:
3383 VISIT(c, expr, e->v.Subscript.value);
3384 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3385 break;
3386 case Del:
3387 VISIT(c, expr, e->v.Subscript.value);
3388 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3389 break;
3390 case Param:
Neal Norwitz4737b232005-11-19 23:58:29 +00003391 PyErr_SetString(PyExc_SystemError,
3392 "param invalid in subscript expression");
3393 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003394 }
3395 break;
3396 case Name_kind:
3397 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3398 /* child nodes of List and Tuple will have expr_context set */
3399 case List_kind:
3400 return compiler_list(c, e);
3401 case Tuple_kind:
3402 return compiler_tuple(c, e);
3403 }
3404 return 1;
3405}
3406
3407static int
3408compiler_augassign(struct compiler *c, stmt_ty s)
3409{
3410 expr_ty e = s->v.AugAssign.target;
3411 expr_ty auge;
3412
3413 assert(s->kind == AugAssign_kind);
3414
3415 switch (e->kind) {
3416 case Attribute_kind:
3417 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003418 AugLoad, e->lineno, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003419 if (auge == NULL)
3420 return 0;
3421 VISIT(c, expr, auge);
3422 VISIT(c, expr, s->v.AugAssign.value);
3423 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3424 auge->v.Attribute.ctx = AugStore;
3425 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003426 break;
3427 case Subscript_kind:
3428 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003429 AugLoad, e->lineno, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003430 if (auge == NULL)
3431 return 0;
3432 VISIT(c, expr, auge);
3433 VISIT(c, expr, s->v.AugAssign.value);
3434 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3435 auge->v.Subscript.ctx = AugStore;
3436 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003437 break;
3438 case Name_kind:
3439 VISIT(c, expr, s->v.AugAssign.target);
3440 VISIT(c, expr, s->v.AugAssign.value);
3441 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3442 return compiler_nameop(c, e->v.Name.id, Store);
3443 default:
3444 fprintf(stderr,
3445 "invalid node type for augmented assignment\n");
3446 return 0;
3447 }
3448 return 1;
3449}
3450
3451static int
3452compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3453{
3454 struct fblockinfo *f;
3455 if (c->u->u_nfblocks >= CO_MAXBLOCKS)
3456 return 0;
3457 f = &c->u->u_fblock[c->u->u_nfblocks++];
3458 f->fb_type = t;
3459 f->fb_block = b;
3460 return 1;
3461}
3462
3463static void
3464compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3465{
3466 struct compiler_unit *u = c->u;
3467 assert(u->u_nfblocks > 0);
3468 u->u_nfblocks--;
3469 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3470 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3471}
3472
3473/* Raises a SyntaxError and returns 0.
3474 If something goes wrong, a different exception may be raised.
3475*/
3476
3477static int
3478compiler_error(struct compiler *c, const char *errstr)
3479{
3480 PyObject *loc;
3481 PyObject *u = NULL, *v = NULL;
3482
3483 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3484 if (!loc) {
3485 Py_INCREF(Py_None);
3486 loc = Py_None;
3487 }
3488 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3489 Py_None, loc);
3490 if (!u)
3491 goto exit;
3492 v = Py_BuildValue("(zO)", errstr, u);
3493 if (!v)
3494 goto exit;
3495 PyErr_SetObject(PyExc_SyntaxError, v);
3496 exit:
3497 Py_DECREF(loc);
3498 Py_XDECREF(u);
3499 Py_XDECREF(v);
3500 return 0;
3501}
3502
3503static int
3504compiler_handle_subscr(struct compiler *c, const char *kind,
3505 expr_context_ty ctx)
3506{
3507 int op = 0;
3508
3509 /* XXX this code is duplicated */
3510 switch (ctx) {
3511 case AugLoad: /* fall through to Load */
3512 case Load: op = BINARY_SUBSCR; break;
3513 case AugStore:/* fall through to Store */
3514 case Store: op = STORE_SUBSCR; break;
3515 case Del: op = DELETE_SUBSCR; break;
3516 case Param:
3517 fprintf(stderr,
3518 "invalid %s kind %d in subscript\n",
3519 kind, ctx);
3520 return 0;
3521 }
3522 if (ctx == AugLoad) {
3523 ADDOP_I(c, DUP_TOPX, 2);
3524 }
3525 else if (ctx == AugStore) {
3526 ADDOP(c, ROT_THREE);
3527 }
3528 ADDOP(c, op);
3529 return 1;
3530}
3531
3532static int
3533compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3534{
3535 int n = 2;
3536 assert(s->kind == Slice_kind);
3537
3538 /* only handles the cases where BUILD_SLICE is emitted */
3539 if (s->v.Slice.lower) {
3540 VISIT(c, expr, s->v.Slice.lower);
3541 }
3542 else {
3543 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3544 }
3545
3546 if (s->v.Slice.upper) {
3547 VISIT(c, expr, s->v.Slice.upper);
3548 }
3549 else {
3550 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3551 }
3552
3553 if (s->v.Slice.step) {
3554 n++;
3555 VISIT(c, expr, s->v.Slice.step);
3556 }
3557 ADDOP_I(c, BUILD_SLICE, n);
3558 return 1;
3559}
3560
3561static int
3562compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3563{
3564 int op = 0, slice_offset = 0, stack_count = 0;
3565
3566 assert(s->v.Slice.step == NULL);
3567 if (s->v.Slice.lower) {
3568 slice_offset++;
3569 stack_count++;
3570 if (ctx != AugStore)
3571 VISIT(c, expr, s->v.Slice.lower);
3572 }
3573 if (s->v.Slice.upper) {
3574 slice_offset += 2;
3575 stack_count++;
3576 if (ctx != AugStore)
3577 VISIT(c, expr, s->v.Slice.upper);
3578 }
3579
3580 if (ctx == AugLoad) {
3581 switch (stack_count) {
3582 case 0: ADDOP(c, DUP_TOP); break;
3583 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3584 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3585 }
3586 }
3587 else if (ctx == AugStore) {
3588 switch (stack_count) {
3589 case 0: ADDOP(c, ROT_TWO); break;
3590 case 1: ADDOP(c, ROT_THREE); break;
3591 case 2: ADDOP(c, ROT_FOUR); break;
3592 }
3593 }
3594
3595 switch (ctx) {
3596 case AugLoad: /* fall through to Load */
3597 case Load: op = SLICE; break;
3598 case AugStore:/* fall through to Store */
3599 case Store: op = STORE_SLICE; break;
3600 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003601 case Param:
3602 PyErr_SetString(PyExc_SystemError,
3603 "param invalid in simple slice");
3604 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003605 }
3606
3607 ADDOP(c, op + slice_offset);
3608 return 1;
3609}
3610
3611static int
3612compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3613 expr_context_ty ctx)
3614{
3615 switch (s->kind) {
3616 case Ellipsis_kind:
3617 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3618 break;
3619 case Slice_kind:
3620 return compiler_slice(c, s, ctx);
3621 break;
3622 case Index_kind:
3623 VISIT(c, expr, s->v.Index.value);
3624 break;
3625 case ExtSlice_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00003626 PyErr_SetString(PyExc_SystemError,
3627 "extended slice invalid in nested slice");
3628 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003629 }
3630 return 1;
3631}
3632
3633
3634static int
3635compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3636{
3637 switch (s->kind) {
3638 case Ellipsis_kind:
3639 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3640 break;
3641 case Slice_kind:
3642 if (!s->v.Slice.step)
3643 return compiler_simple_slice(c, s, ctx);
3644 if (!compiler_slice(c, s, ctx))
3645 return 0;
3646 if (ctx == AugLoad) {
3647 ADDOP_I(c, DUP_TOPX, 2);
3648 }
3649 else if (ctx == AugStore) {
3650 ADDOP(c, ROT_THREE);
3651 }
3652 return compiler_handle_subscr(c, "slice", ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003653 case ExtSlice_kind: {
3654 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3655 for (i = 0; i < n; i++) {
3656 slice_ty sub = asdl_seq_GET(s->v.ExtSlice.dims, i);
3657 if (!compiler_visit_nested_slice(c, sub, ctx))
3658 return 0;
3659 }
3660 ADDOP_I(c, BUILD_TUPLE, n);
3661 return compiler_handle_subscr(c, "extended slice", ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003662 }
3663 case Index_kind:
3664 if (ctx != AugStore)
3665 VISIT(c, expr, s->v.Index.value);
3666 return compiler_handle_subscr(c, "index", ctx);
3667 }
3668 return 1;
3669}
3670
3671/* do depth-first search of basic block graph, starting with block.
3672 post records the block indices in post-order.
3673
3674 XXX must handle implicit jumps from one block to next
3675*/
3676
3677static void
3678dfs(struct compiler *c, basicblock *b, struct assembler *a)
3679{
3680 int i;
3681 struct instr *instr = NULL;
3682
3683 if (b->b_seen)
3684 return;
3685 b->b_seen = 1;
3686 if (b->b_next != NULL)
3687 dfs(c, b->b_next, a);
3688 for (i = 0; i < b->b_iused; i++) {
3689 instr = &b->b_instr[i];
3690 if (instr->i_jrel || instr->i_jabs)
3691 dfs(c, instr->i_target, a);
3692 }
3693 a->a_postorder[a->a_nblocks++] = b;
3694}
3695
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003696static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003697stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3698{
3699 int i;
3700 struct instr *instr;
3701 if (b->b_seen || b->b_startdepth >= depth)
3702 return maxdepth;
3703 b->b_seen = 1;
3704 b->b_startdepth = depth;
3705 for (i = 0; i < b->b_iused; i++) {
3706 instr = &b->b_instr[i];
3707 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3708 if (depth > maxdepth)
3709 maxdepth = depth;
3710 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3711 if (instr->i_jrel || instr->i_jabs) {
3712 maxdepth = stackdepth_walk(c, instr->i_target,
3713 depth, maxdepth);
3714 if (instr->i_opcode == JUMP_ABSOLUTE ||
3715 instr->i_opcode == JUMP_FORWARD) {
3716 goto out; /* remaining code is dead */
3717 }
3718 }
3719 }
3720 if (b->b_next)
3721 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3722out:
3723 b->b_seen = 0;
3724 return maxdepth;
3725}
3726
3727/* Find the flow path that needs the largest stack. We assume that
3728 * cycles in the flow graph have no net effect on the stack depth.
3729 */
3730static int
3731stackdepth(struct compiler *c)
3732{
3733 basicblock *b, *entryblock;
3734 entryblock = NULL;
3735 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3736 b->b_seen = 0;
3737 b->b_startdepth = INT_MIN;
3738 entryblock = b;
3739 }
3740 return stackdepth_walk(c, entryblock, 0, 0);
3741}
3742
3743static int
3744assemble_init(struct assembler *a, int nblocks, int firstlineno)
3745{
3746 memset(a, 0, sizeof(struct assembler));
3747 a->a_lineno = firstlineno;
3748 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3749 if (!a->a_bytecode)
3750 return 0;
3751 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3752 if (!a->a_lnotab)
3753 return 0;
3754 a->a_postorder = (basicblock **)PyObject_Malloc(
3755 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003756 if (!a->a_postorder) {
3757 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003758 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003759 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003760 return 1;
3761}
3762
3763static void
3764assemble_free(struct assembler *a)
3765{
3766 Py_XDECREF(a->a_bytecode);
3767 Py_XDECREF(a->a_lnotab);
3768 if (a->a_postorder)
3769 PyObject_Free(a->a_postorder);
3770}
3771
3772/* Return the size of a basic block in bytes. */
3773
3774static int
3775instrsize(struct instr *instr)
3776{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003777 if (!instr->i_hasarg)
3778 return 1;
3779 if (instr->i_oparg > 0xffff)
3780 return 6;
3781 return 3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003782}
3783
3784static int
3785blocksize(basicblock *b)
3786{
3787 int i;
3788 int size = 0;
3789
3790 for (i = 0; i < b->b_iused; i++)
3791 size += instrsize(&b->b_instr[i]);
3792 return size;
3793}
3794
3795/* All about a_lnotab.
3796
3797c_lnotab is an array of unsigned bytes disguised as a Python string.
3798It is used to map bytecode offsets to source code line #s (when needed
3799for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003800
Tim Peters2a7f3842001-06-09 09:26:21 +00003801The array is conceptually a list of
3802 (bytecode offset increment, line number increment)
3803pairs. The details are important and delicate, best illustrated by example:
3804
3805 byte code offset source code line number
3806 0 1
3807 6 2
3808 50 7
3809 350 307
3810 361 308
3811
3812The first trick is that these numbers aren't stored, only the increments
3813from one row to the next (this doesn't really work, but it's a start):
3814
3815 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3816
3817The second trick is that an unsigned byte can't hold negative values, or
3818values larger than 255, so (a) there's a deep assumption that byte code
3819offsets and their corresponding line #s both increase monotonically, and (b)
3820if at least one column jumps by more than 255 from one row to the next, more
3821than one pair is written to the table. In case #b, there's no way to know
3822from looking at the table later how many were written. That's the delicate
3823part. A user of c_lnotab desiring to find the source line number
3824corresponding to a bytecode address A should do something like this
3825
3826 lineno = addr = 0
3827 for addr_incr, line_incr in c_lnotab:
3828 addr += addr_incr
3829 if addr > A:
3830 return lineno
3831 lineno += line_incr
3832
3833In order for this to work, when the addr field increments by more than 255,
3834the line # increment in each pair generated must be 0 until the remaining addr
3835increment is < 256. So, in the example above, com_set_lineno should not (as
3836was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
3837255, 0, 45, 255, 0, 45.
3838*/
3839
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003840static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003841assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003842{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003843 int d_bytecode, d_lineno;
3844 int len;
3845 char *lnotab;
3846
3847 d_bytecode = a->a_offset - a->a_lineno_off;
3848 d_lineno = i->i_lineno - a->a_lineno;
3849
3850 assert(d_bytecode >= 0);
3851 assert(d_lineno >= 0);
3852
3853 if (d_lineno == 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003854 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003855
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003856 if (d_bytecode > 255) {
3857 int i, nbytes, ncodes = d_bytecode / 255;
3858 nbytes = a->a_lnotab_off + 2 * ncodes;
3859 len = PyString_GET_SIZE(a->a_lnotab);
3860 if (nbytes >= len) {
3861 if (len * 2 < nbytes)
3862 len = nbytes;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003863 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003864 len *= 2;
3865 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3866 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003867 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003868 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3869 for (i = 0; i < ncodes; i++) {
3870 *lnotab++ = 255;
3871 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003872 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003873 d_bytecode -= ncodes * 255;
3874 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003875 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003876 assert(d_bytecode <= 255);
3877 if (d_lineno > 255) {
3878 int i, nbytes, ncodes = d_lineno / 255;
3879 nbytes = a->a_lnotab_off + 2 * ncodes;
3880 len = PyString_GET_SIZE(a->a_lnotab);
3881 if (nbytes >= len) {
3882 if (len * 2 < nbytes)
3883 len = nbytes;
Guido van Rossum635abd21997-01-06 22:56:52 +00003884 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003885 len *= 2;
3886 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3887 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003888 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003889 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3890 *lnotab++ = 255;
3891 *lnotab++ = d_bytecode;
3892 d_bytecode = 0;
3893 for (i = 1; i < ncodes; i++) {
3894 *lnotab++ = 255;
3895 *lnotab++ = 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003896 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003897 d_lineno -= ncodes * 255;
3898 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003899 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003900
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003901 len = PyString_GET_SIZE(a->a_lnotab);
3902 if (a->a_lnotab_off + 2 >= len) {
3903 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003904 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003905 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003906 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003907
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003908 a->a_lnotab_off += 2;
3909 if (d_bytecode) {
3910 *lnotab++ = d_bytecode;
3911 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003912 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003913 else { /* First line of a block; def stmt, etc. */
3914 *lnotab++ = 0;
3915 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003916 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003917 a->a_lineno = i->i_lineno;
3918 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003919 return 1;
3920}
3921
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003922/* assemble_emit()
3923 Extend the bytecode with a new instruction.
3924 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003925*/
3926
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003927static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003928assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003929{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003930 int size, arg = 0, ext = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003931 int len = PyString_GET_SIZE(a->a_bytecode);
3932 char *code;
3933
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003934 size = instrsize(i);
3935 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003936 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003937 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003938 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003939 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003940 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003941 if (a->a_offset + size >= len) {
3942 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003943 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003944 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003945 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3946 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003947 if (size == 6) {
3948 assert(i->i_hasarg);
3949 *code++ = (char)EXTENDED_ARG;
3950 *code++ = ext & 0xff;
3951 *code++ = ext >> 8;
3952 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003953 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003954 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003955 if (i->i_hasarg) {
3956 assert(size == 3 || size == 6);
3957 *code++ = arg & 0xff;
3958 *code++ = arg >> 8;
3959 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003960 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003961}
3962
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003963static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003964assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003965{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003966 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003967 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003968 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003969
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003970 /* Compute the size of each block and fixup jump args.
3971 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003972start:
3973 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003974 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003975 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003976 bsize = blocksize(b);
3977 b->b_offset = totsize;
3978 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003979 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003980 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003981 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3982 bsize = b->b_offset;
3983 for (i = 0; i < b->b_iused; i++) {
3984 struct instr *instr = &b->b_instr[i];
3985 /* Relative jumps are computed relative to
3986 the instruction pointer after fetching
3987 the jump instruction.
3988 */
3989 bsize += instrsize(instr);
3990 if (instr->i_jabs)
3991 instr->i_oparg = instr->i_target->b_offset;
3992 else if (instr->i_jrel) {
3993 int delta = instr->i_target->b_offset - bsize;
3994 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003995 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003996 else
3997 continue;
3998 if (instr->i_oparg > 0xffff)
3999 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004000 }
4001 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00004002
4003 /* XXX: This is an awful hack that could hurt performance, but
4004 on the bright side it should work until we come up
4005 with a better solution.
4006
4007 In the meantime, should the goto be dropped in favor
4008 of a loop?
4009
4010 The issue is that in the first loop blocksize() is called
4011 which calls instrsize() which requires i_oparg be set
4012 appropriately. There is a bootstrap problem because
4013 i_oparg is calculated in the second loop above.
4014
4015 So we loop until we stop seeing new EXTENDED_ARGs.
4016 The only EXTENDED_ARGs that could be popping up are
4017 ones in jump instructions. So this should converge
4018 fairly quickly.
4019 */
4020 if (last_extended_arg_count != extended_arg_count) {
4021 last_extended_arg_count = extended_arg_count;
4022 goto start;
4023 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004024}
4025
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004026static PyObject *
4027dict_keys_inorder(PyObject *dict, int offset)
4028{
4029 PyObject *tuple, *k, *v;
4030 int i, pos = 0, size = PyDict_Size(dict);
4031
4032 tuple = PyTuple_New(size);
4033 if (tuple == NULL)
4034 return NULL;
4035 while (PyDict_Next(dict, &pos, &k, &v)) {
4036 i = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004037 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004038 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004039 assert((i - offset) < size);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004040 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004041 PyTuple_SET_ITEM(tuple, i - offset, k);
4042 }
4043 return tuple;
4044}
4045
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004046static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004047compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004048{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004049 PySTEntryObject *ste = c->u->u_ste;
4050 int flags = 0, n;
4051 if (ste->ste_type != ModuleBlock)
4052 flags |= CO_NEWLOCALS;
4053 if (ste->ste_type == FunctionBlock) {
4054 if (!ste->ste_unoptimized)
4055 flags |= CO_OPTIMIZED;
4056 if (ste->ste_nested)
4057 flags |= CO_NESTED;
4058 if (ste->ste_generator)
4059 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004060 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004061 if (ste->ste_varargs)
4062 flags |= CO_VARARGS;
4063 if (ste->ste_varkeywords)
4064 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00004065 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004066 flags |= CO_GENERATOR;
4067 if (c->c_flags->cf_flags & CO_FUTURE_DIVISION)
4068 flags |= CO_FUTURE_DIVISION;
4069 n = PyDict_Size(c->u->u_freevars);
4070 if (n < 0)
4071 return -1;
4072 if (n == 0) {
4073 n = PyDict_Size(c->u->u_cellvars);
4074 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004075 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004076 if (n == 0) {
4077 flags |= CO_NOFREE;
4078 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004079 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004080
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004081 return flags;
4082}
4083
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004084static PyCodeObject *
4085makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004086{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004087 PyObject *tmp;
4088 PyCodeObject *co = NULL;
4089 PyObject *consts = NULL;
4090 PyObject *names = NULL;
4091 PyObject *varnames = NULL;
4092 PyObject *filename = NULL;
4093 PyObject *name = NULL;
4094 PyObject *freevars = NULL;
4095 PyObject *cellvars = NULL;
4096 PyObject *bytecode = NULL;
4097 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004098
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004099 tmp = dict_keys_inorder(c->u->u_consts, 0);
4100 if (!tmp)
4101 goto error;
4102 consts = PySequence_List(tmp); /* optimize_code requires a list */
4103 Py_DECREF(tmp);
4104
4105 names = dict_keys_inorder(c->u->u_names, 0);
4106 varnames = dict_keys_inorder(c->u->u_varnames, 0);
4107 if (!consts || !names || !varnames)
4108 goto error;
4109
4110 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4111 if (!cellvars)
4112 goto error;
4113 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4114 if (!freevars)
4115 goto error;
4116 filename = PyString_FromString(c->c_filename);
4117 if (!filename)
4118 goto error;
4119
4120 nlocals = PyDict_Size(c->u->u_varnames);
4121 flags = compute_code_flags(c);
4122 if (flags < 0)
4123 goto error;
4124
4125 bytecode = optimize_code(a->a_bytecode, consts, names, a->a_lnotab);
4126 if (!bytecode)
4127 goto error;
4128
4129 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4130 if (!tmp)
4131 goto error;
4132 Py_DECREF(consts);
4133 consts = tmp;
4134
4135 co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags,
4136 bytecode, consts, names, varnames,
4137 freevars, cellvars,
4138 filename, c->u->u_name,
4139 c->u->u_firstlineno,
4140 a->a_lnotab);
4141 error:
4142 Py_XDECREF(consts);
4143 Py_XDECREF(names);
4144 Py_XDECREF(varnames);
4145 Py_XDECREF(filename);
4146 Py_XDECREF(name);
4147 Py_XDECREF(freevars);
4148 Py_XDECREF(cellvars);
4149 Py_XDECREF(bytecode);
4150 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004151}
4152
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004153static PyCodeObject *
4154assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004155{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004156 basicblock *b, *entryblock;
4157 struct assembler a;
4158 int i, j, nblocks;
4159 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004160
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004161 /* Make sure every block that falls off the end returns None.
4162 XXX NEXT_BLOCK() isn't quite right, because if the last
4163 block ends with a jump or return b_next shouldn't set.
4164 */
4165 if (!c->u->u_curblock->b_return) {
4166 NEXT_BLOCK(c);
4167 if (addNone)
4168 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4169 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004170 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004171
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004172 nblocks = 0;
4173 entryblock = NULL;
4174 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4175 nblocks++;
4176 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004177 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004178
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004179 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4180 goto error;
4181 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004182
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004183 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004184 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004185
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004186 /* Emit code in reverse postorder from dfs. */
4187 for (i = a.a_nblocks - 1; i >= 0; i--) {
4188 basicblock *b = a.a_postorder[i];
4189 for (j = 0; j < b->b_iused; j++)
4190 if (!assemble_emit(&a, &b->b_instr[j]))
4191 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004192 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004193
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004194 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4195 goto error;
4196 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
4197 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004198
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004199 co = makecode(c, &a);
4200 error:
4201 assemble_free(&a);
4202 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004203}