blob: accd3b70bc0ce676f3c3e57329d29c788708793c [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));
1078 memset(u, 0, sizeof(struct compiler_unit));
1079 u->u_argcount = 0;
1080 u->u_ste = PySymtable_Lookup(c->c_st, key);
1081 if (!u->u_ste) {
1082 compiler_unit_free(u);
1083 return 0;
1084 }
1085 Py_INCREF(name);
1086 u->u_name = name;
1087 u->u_varnames = list2dict(u->u_ste->ste_varnames);
1088 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
1089 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
1090 PyDict_Size(u->u_cellvars));
1091
1092 u->u_blocks = NULL;
1093 u->u_tmpname = 0;
1094 u->u_nfblocks = 0;
1095 u->u_firstlineno = lineno;
1096 u->u_lineno = 0;
1097 u->u_lineno_set = false;
1098 u->u_consts = PyDict_New();
1099 if (!u->u_consts) {
1100 compiler_unit_free(u);
1101 return 0;
1102 }
1103 u->u_names = PyDict_New();
1104 if (!u->u_names) {
1105 compiler_unit_free(u);
1106 return 0;
1107 }
1108
1109 u->u_private = NULL;
1110
1111 /* Push the old compiler_unit on the stack. */
1112 if (c->u) {
1113 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
1114 if (PyList_Append(c->c_stack, wrapper) < 0) {
1115 compiler_unit_free(u);
1116 return 0;
1117 }
1118 Py_DECREF(wrapper);
1119 u->u_private = c->u->u_private;
1120 Py_XINCREF(u->u_private);
1121 }
1122 c->u = u;
1123
1124 c->c_nestlevel++;
1125 if (compiler_use_new_block(c) < 0)
1126 return 0;
1127
1128 return 1;
1129}
1130
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001131static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132compiler_exit_scope(struct compiler *c)
1133{
1134 int n;
1135 PyObject *wrapper;
1136
1137 c->c_nestlevel--;
1138 compiler_unit_free(c->u);
1139 /* Restore c->u to the parent unit. */
1140 n = PyList_GET_SIZE(c->c_stack) - 1;
1141 if (n >= 0) {
1142 wrapper = PyList_GET_ITEM(c->c_stack, n);
1143 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001144 /* we are deleting from a list so this really shouldn't fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145 if (PySequence_DelItem(c->c_stack, n) < 0)
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001146 Py_FatalError("compiler_exit_scope()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 compiler_unit_check(c->u);
1148 }
1149 else
1150 c->u = NULL;
1151
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152}
1153
1154/* Allocate a new block and return a pointer to it.
1155 Returns NULL on error.
1156*/
1157
1158static basicblock *
1159compiler_new_block(struct compiler *c)
1160{
1161 basicblock *b;
1162 struct compiler_unit *u;
1163
1164 u = c->u;
1165 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
1166 if (b == NULL)
1167 return NULL;
1168 memset((void *)b, 0, sizeof(basicblock));
1169 assert (b->b_next == NULL);
1170 b->b_list = u->u_blocks;
1171 u->u_blocks = b;
1172 return b;
1173}
1174
1175static void
1176compiler_use_block(struct compiler *c, basicblock *block)
1177{
1178 assert (block != NULL);
1179 c->u->u_curblock = block;
1180}
1181
1182static basicblock *
1183compiler_use_new_block(struct compiler *c)
1184{
1185 basicblock *block = compiler_new_block(c);
1186 if (block == NULL)
1187 return NULL;
1188 c->u->u_curblock = block;
1189 return block;
1190}
1191
1192static basicblock *
1193compiler_next_block(struct compiler *c)
1194{
1195 basicblock *block = compiler_new_block(c);
1196 if (block == NULL)
1197 return NULL;
1198 c->u->u_curblock->b_next = block;
1199 c->u->u_curblock = block;
1200 return block;
1201}
1202
1203static basicblock *
1204compiler_use_next_block(struct compiler *c, basicblock *block)
1205{
1206 assert(block != NULL);
1207 c->u->u_curblock->b_next = block;
1208 c->u->u_curblock = block;
1209 return block;
1210}
1211
1212/* Returns the offset of the next instruction in the current block's
1213 b_instr array. Resizes the b_instr as necessary.
1214 Returns -1 on failure.
1215 */
1216
1217static int
1218compiler_next_instr(struct compiler *c, basicblock *b)
1219{
1220 assert(b != NULL);
1221 if (b->b_instr == NULL) {
1222 b->b_instr = PyObject_Malloc(sizeof(struct instr) *
1223 DEFAULT_BLOCK_SIZE);
1224 if (b->b_instr == NULL) {
1225 PyErr_NoMemory();
1226 return -1;
1227 }
1228 b->b_ialloc = DEFAULT_BLOCK_SIZE;
1229 memset((char *)b->b_instr, 0,
1230 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
1231 }
1232 else if (b->b_iused == b->b_ialloc) {
1233 size_t oldsize, newsize;
1234 oldsize = b->b_ialloc * sizeof(struct instr);
1235 newsize = oldsize << 1;
1236 if (newsize == 0) {
1237 PyErr_NoMemory();
1238 return -1;
1239 }
1240 b->b_ialloc <<= 1;
1241 b->b_instr = PyObject_Realloc((void *)b->b_instr, newsize);
1242 if (b->b_instr == NULL)
1243 return -1;
1244 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
1245 }
1246 return b->b_iused++;
1247}
1248
1249static void
1250compiler_set_lineno(struct compiler *c, int off)
1251{
1252 basicblock *b;
1253 if (c->u->u_lineno_set)
1254 return;
1255 c->u->u_lineno_set = true;
1256 b = c->u->u_curblock;
1257 b->b_instr[off].i_lineno = c->u->u_lineno;
1258}
1259
1260static int
1261opcode_stack_effect(int opcode, int oparg)
1262{
1263 switch (opcode) {
1264 case POP_TOP:
1265 return -1;
1266 case ROT_TWO:
1267 case ROT_THREE:
1268 return 0;
1269 case DUP_TOP:
1270 return 1;
1271 case ROT_FOUR:
1272 return 0;
1273
1274 case UNARY_POSITIVE:
1275 case UNARY_NEGATIVE:
1276 case UNARY_NOT:
1277 case UNARY_CONVERT:
1278 case UNARY_INVERT:
1279 return 0;
1280
1281 case BINARY_POWER:
1282 case BINARY_MULTIPLY:
1283 case BINARY_DIVIDE:
1284 case BINARY_MODULO:
1285 case BINARY_ADD:
1286 case BINARY_SUBTRACT:
1287 case BINARY_SUBSCR:
1288 case BINARY_FLOOR_DIVIDE:
1289 case BINARY_TRUE_DIVIDE:
1290 return -1;
1291 case INPLACE_FLOOR_DIVIDE:
1292 case INPLACE_TRUE_DIVIDE:
1293 return -1;
1294
1295 case SLICE+0:
1296 return 1;
1297 case SLICE+1:
1298 return 0;
1299 case SLICE+2:
1300 return 0;
1301 case SLICE+3:
1302 return -1;
1303
1304 case STORE_SLICE+0:
1305 return -2;
1306 case STORE_SLICE+1:
1307 return -3;
1308 case STORE_SLICE+2:
1309 return -3;
1310 case STORE_SLICE+3:
1311 return -4;
1312
1313 case DELETE_SLICE+0:
1314 return -1;
1315 case DELETE_SLICE+1:
1316 return -2;
1317 case DELETE_SLICE+2:
1318 return -2;
1319 case DELETE_SLICE+3:
1320 return -3;
1321
1322 case INPLACE_ADD:
1323 case INPLACE_SUBTRACT:
1324 case INPLACE_MULTIPLY:
1325 case INPLACE_DIVIDE:
1326 case INPLACE_MODULO:
1327 return -1;
1328 case STORE_SUBSCR:
1329 return -3;
1330 case DELETE_SUBSCR:
1331 return -2;
1332
1333 case BINARY_LSHIFT:
1334 case BINARY_RSHIFT:
1335 case BINARY_AND:
1336 case BINARY_XOR:
1337 case BINARY_OR:
1338 return -1;
1339 case INPLACE_POWER:
1340 return -1;
1341 case GET_ITER:
1342 return 0;
1343
1344 case PRINT_EXPR:
1345 return -1;
1346 case PRINT_ITEM:
1347 return -1;
1348 case PRINT_NEWLINE:
1349 return 0;
1350 case PRINT_ITEM_TO:
1351 return -2;
1352 case PRINT_NEWLINE_TO:
1353 return -1;
1354 case INPLACE_LSHIFT:
1355 case INPLACE_RSHIFT:
1356 case INPLACE_AND:
1357 case INPLACE_XOR:
1358 case INPLACE_OR:
1359 return -1;
1360 case BREAK_LOOP:
1361 return 0;
1362
1363 case LOAD_LOCALS:
1364 return 1;
1365 case RETURN_VALUE:
1366 return -1;
1367 case IMPORT_STAR:
1368 return -1;
1369 case EXEC_STMT:
1370 return -3;
1371 case YIELD_VALUE:
1372 return 0;
1373
1374 case POP_BLOCK:
1375 return 0;
1376 case END_FINALLY:
1377 return -1; /* or -2 or -3 if exception occurred */
1378 case BUILD_CLASS:
1379 return -2;
1380
1381 case STORE_NAME:
1382 return -1;
1383 case DELETE_NAME:
1384 return 0;
1385 case UNPACK_SEQUENCE:
1386 return oparg-1;
1387 case FOR_ITER:
1388 return 1;
1389
1390 case STORE_ATTR:
1391 return -2;
1392 case DELETE_ATTR:
1393 return -1;
1394 case STORE_GLOBAL:
1395 return -1;
1396 case DELETE_GLOBAL:
1397 return 0;
1398 case DUP_TOPX:
1399 return oparg;
1400 case LOAD_CONST:
1401 return 1;
1402 case LOAD_NAME:
1403 return 1;
1404 case BUILD_TUPLE:
1405 case BUILD_LIST:
1406 return 1-oparg;
1407 case BUILD_MAP:
1408 return 1;
1409 case LOAD_ATTR:
1410 return 0;
1411 case COMPARE_OP:
1412 return -1;
1413 case IMPORT_NAME:
1414 return 0;
1415 case IMPORT_FROM:
1416 return 1;
1417
1418 case JUMP_FORWARD:
1419 case JUMP_IF_FALSE:
1420 case JUMP_IF_TRUE:
1421 case JUMP_ABSOLUTE:
1422 return 0;
1423
1424 case LOAD_GLOBAL:
1425 return 1;
1426
1427 case CONTINUE_LOOP:
1428 return 0;
1429 case SETUP_LOOP:
1430 return 0;
1431 case SETUP_EXCEPT:
1432 case SETUP_FINALLY:
1433 return 3; /* actually pushed by an exception */
1434
1435 case LOAD_FAST:
1436 return 1;
1437 case STORE_FAST:
1438 return -1;
1439 case DELETE_FAST:
1440 return 0;
1441
1442 case RAISE_VARARGS:
1443 return -oparg;
1444#define NARGS(o) (((o) % 256) + 2*((o) / 256))
1445 case CALL_FUNCTION:
1446 return -NARGS(oparg);
1447 case CALL_FUNCTION_VAR:
1448 case CALL_FUNCTION_KW:
1449 return -NARGS(oparg)-1;
1450 case CALL_FUNCTION_VAR_KW:
1451 return -NARGS(oparg)-2;
1452#undef NARGS
1453 case MAKE_FUNCTION:
1454 return -oparg;
1455 case BUILD_SLICE:
1456 if (oparg == 3)
1457 return -2;
1458 else
1459 return -1;
1460
1461 case MAKE_CLOSURE:
1462 return -oparg;
1463 case LOAD_CLOSURE:
1464 return 1;
1465 case LOAD_DEREF:
1466 return 1;
1467 case STORE_DEREF:
1468 return -1;
1469 default:
1470 fprintf(stderr, "opcode = %d\n", opcode);
1471 Py_FatalError("opcode_stack_effect()");
1472
1473 }
1474 return 0; /* not reachable */
1475}
1476
1477/* Add an opcode with no argument.
1478 Returns 0 on failure, 1 on success.
1479*/
1480
1481static int
1482compiler_addop(struct compiler *c, int opcode)
1483{
1484 basicblock *b;
1485 struct instr *i;
1486 int off;
1487 off = compiler_next_instr(c, c->u->u_curblock);
1488 if (off < 0)
1489 return 0;
1490 b = c->u->u_curblock;
1491 i = &b->b_instr[off];
1492 i->i_opcode = opcode;
1493 i->i_hasarg = 0;
1494 if (opcode == RETURN_VALUE)
1495 b->b_return = 1;
1496 compiler_set_lineno(c, off);
1497 return 1;
1498}
1499
1500static int
1501compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
1502{
1503 PyObject *t, *v;
1504 int arg;
1505
1506 /* necessary to make sure types aren't coerced (e.g., int and long) */
Neil Schemenauer3a44aaa2005-10-23 17:21:54 +00001507 t = PyTuple_Pack(2, o, o->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508 if (t == NULL)
1509 return -1;
1510
1511 v = PyDict_GetItem(dict, t);
1512 if (!v) {
1513 arg = PyDict_Size(dict);
1514 v = PyInt_FromLong(arg);
1515 if (!v) {
1516 Py_DECREF(t);
1517 return -1;
1518 }
1519 if (PyDict_SetItem(dict, t, v) < 0) {
1520 Py_DECREF(t);
1521 Py_DECREF(v);
1522 return -1;
1523 }
1524 Py_DECREF(v);
1525 }
1526 else
1527 arg = PyInt_AsLong(v);
1528 Py_DECREF(t);
1529 return arg;
1530}
1531
1532static int
1533compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
1534 PyObject *o)
1535{
1536 int arg = compiler_add_o(c, dict, o);
1537 if (arg < 0)
1538 return 0;
1539 return compiler_addop_i(c, opcode, arg);
1540}
1541
1542static int
1543compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
1544 PyObject *o)
1545{
1546 int arg;
1547 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1548 if (!mangled)
1549 return 0;
1550 arg = compiler_add_o(c, dict, mangled);
1551 Py_DECREF(mangled);
1552 if (arg < 0)
1553 return 0;
1554 return compiler_addop_i(c, opcode, arg);
1555}
1556
1557/* Add an opcode with an integer argument.
1558 Returns 0 on failure, 1 on success.
1559*/
1560
1561static int
1562compiler_addop_i(struct compiler *c, int opcode, int oparg)
1563{
1564 struct instr *i;
1565 int off;
1566 off = compiler_next_instr(c, c->u->u_curblock);
1567 if (off < 0)
1568 return 0;
1569 i = &c->u->u_curblock->b_instr[off];
1570 i->i_opcode = opcode;
1571 i->i_oparg = oparg;
1572 i->i_hasarg = 1;
1573 compiler_set_lineno(c, off);
1574 return 1;
1575}
1576
1577static int
1578compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1579{
1580 struct instr *i;
1581 int off;
1582
1583 assert(b != NULL);
1584 off = compiler_next_instr(c, c->u->u_curblock);
1585 if (off < 0)
1586 return 0;
1587 compiler_set_lineno(c, off);
1588 i = &c->u->u_curblock->b_instr[off];
1589 i->i_opcode = opcode;
1590 i->i_target = b;
1591 i->i_hasarg = 1;
1592 if (absolute)
1593 i->i_jabs = 1;
1594 else
1595 i->i_jrel = 1;
1596 return 1;
1597}
1598
1599/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1600 like to find better names.) NEW_BLOCK() creates a new block and sets
1601 it as the current block. NEXT_BLOCK() also creates an implicit jump
1602 from the current block to the new block.
1603*/
1604
1605/* XXX The returns inside these macros make it impossible to decref
1606 objects created in the local function.
1607*/
1608
1609
1610#define NEW_BLOCK(C) { \
1611 if (compiler_use_new_block((C)) == NULL) \
1612 return 0; \
1613}
1614
1615#define NEXT_BLOCK(C) { \
1616 if (compiler_next_block((C)) == NULL) \
1617 return 0; \
1618}
1619
1620#define ADDOP(C, OP) { \
1621 if (!compiler_addop((C), (OP))) \
1622 return 0; \
1623}
1624
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001625#define ADDOP_IN_SCOPE(C, OP) { \
1626 if (!compiler_addop((C), (OP))) { \
1627 compiler_exit_scope(c); \
1628 return 0; \
1629 } \
1630}
1631
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001632#define ADDOP_O(C, OP, O, TYPE) { \
1633 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1634 return 0; \
1635}
1636
1637#define ADDOP_NAME(C, OP, O, TYPE) { \
1638 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1639 return 0; \
1640}
1641
1642#define ADDOP_I(C, OP, O) { \
1643 if (!compiler_addop_i((C), (OP), (O))) \
1644 return 0; \
1645}
1646
1647#define ADDOP_JABS(C, OP, O) { \
1648 if (!compiler_addop_j((C), (OP), (O), 1)) \
1649 return 0; \
1650}
1651
1652#define ADDOP_JREL(C, OP, O) { \
1653 if (!compiler_addop_j((C), (OP), (O), 0)) \
1654 return 0; \
1655}
1656
1657/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1658 the ASDL name to synthesize the name of the C type and the visit function.
1659*/
1660
1661#define VISIT(C, TYPE, V) {\
1662 if (!compiler_visit_ ## TYPE((C), (V))) \
1663 return 0; \
1664}
1665
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001666#define VISIT_IN_SCOPE(C, TYPE, V) {\
1667 if (!compiler_visit_ ## TYPE((C), (V))) { \
1668 compiler_exit_scope(c); \
1669 return 0; \
1670 } \
1671}
1672
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001673#define VISIT_SLICE(C, V, CTX) {\
1674 if (!compiler_visit_slice((C), (V), (CTX))) \
1675 return 0; \
1676}
1677
1678#define VISIT_SEQ(C, TYPE, SEQ) { \
1679 int i; \
1680 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1681 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1682 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
1683 if (!compiler_visit_ ## TYPE((C), elt)) \
1684 return 0; \
1685 } \
1686}
1687
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001688#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
1689 int i; \
1690 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1691 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1692 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
1693 if (!compiler_visit_ ## TYPE((C), elt)) { \
1694 compiler_exit_scope(c); \
1695 return 0; \
1696 } \
1697 } \
1698}
1699
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700static int
1701compiler_isdocstring(stmt_ty s)
1702{
1703 if (s->kind != Expr_kind)
1704 return 0;
1705 return s->v.Expr.value->kind == Str_kind;
1706}
1707
1708/* Compile a sequence of statements, checking for a docstring. */
1709
1710static int
1711compiler_body(struct compiler *c, asdl_seq *stmts)
1712{
1713 int i = 0;
1714 stmt_ty st;
1715
1716 if (!asdl_seq_LEN(stmts))
1717 return 1;
1718 st = asdl_seq_GET(stmts, 0);
1719 if (compiler_isdocstring(st)) {
1720 i = 1;
1721 VISIT(c, expr, st->v.Expr.value);
1722 if (!compiler_nameop(c, __doc__, Store))
1723 return 0;
1724 }
1725 for (; i < asdl_seq_LEN(stmts); i++)
1726 VISIT(c, stmt, asdl_seq_GET(stmts, i));
1727 return 1;
1728}
1729
1730static PyCodeObject *
1731compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001732{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001733 PyCodeObject *co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734 int addNone = 1;
1735 static PyObject *module;
1736 if (!module) {
1737 module = PyString_FromString("<module>");
1738 if (!module)
1739 return NULL;
1740 }
1741 if (!compiler_enter_scope(c, module, mod, 1))
Guido van Rossumd076c731998-10-07 19:42:25 +00001742 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001743 switch (mod->kind) {
1744 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001745 if (!compiler_body(c, mod->v.Module.body)) {
1746 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001747 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001748 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749 break;
1750 case Interactive_kind:
1751 c->c_interactive = 1;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001752 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001753 break;
1754 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001755 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001756 addNone = 0;
1757 break;
1758 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001759 PyErr_SetString(PyExc_SystemError,
1760 "suite should not be possible");
1761 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001762 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001763 PyErr_Format(PyExc_SystemError,
1764 "module kind %d should not be possible",
1765 mod->kind);
1766 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001767 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768 co = assemble(c, addNone);
1769 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001770 return co;
1771}
1772
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001773/* The test for LOCAL must come before the test for FREE in order to
1774 handle classes where name is both local and free. The local var is
1775 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001776*/
1777
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001778static int
1779get_ref_type(struct compiler *c, PyObject *name)
1780{
1781 int scope = PyST_GetScope(c->u->u_ste, name);
1782 if (scope == 0) {
1783 char buf[350];
1784 PyOS_snprintf(buf, sizeof(buf),
1785 "unknown scope for %.100s in %.100s(%s) in %s\n"
1786 "symbols: %s\nlocals: %s\nglobals: %s\n",
1787 PyString_AS_STRING(name),
1788 PyString_AS_STRING(c->u->u_name),
1789 PyObject_REPR(c->u->u_ste->ste_id),
1790 c->c_filename,
1791 PyObject_REPR(c->u->u_ste->ste_symbols),
1792 PyObject_REPR(c->u->u_varnames),
1793 PyObject_REPR(c->u->u_names)
1794 );
1795 Py_FatalError(buf);
1796 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001797
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001798 return scope;
1799}
1800
1801static int
1802compiler_lookup_arg(PyObject *dict, PyObject *name)
1803{
1804 PyObject *k, *v;
1805 k = Py_BuildValue("(OO)", name, name->ob_type);
1806 if (k == NULL)
1807 return -1;
1808 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001809 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001810 if (v == NULL)
1811 return -1;
1812 return PyInt_AS_LONG(v);
1813}
1814
1815static int
1816compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1817{
1818 int i, free = PyCode_GetNumFree(co);
1819 if (free == 0) {
1820 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1821 ADDOP_I(c, MAKE_FUNCTION, args);
1822 return 1;
1823 }
1824 for (i = 0; i < free; ++i) {
1825 /* Bypass com_addop_varname because it will generate
1826 LOAD_DEREF but LOAD_CLOSURE is needed.
1827 */
1828 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1829 int arg, reftype;
1830
1831 /* Special case: If a class contains a method with a
1832 free variable that has the same name as a method,
1833 the name will be considered free *and* local in the
1834 class. It should be handled by the closure, as
1835 well as by the normal name loookup logic.
1836 */
1837 reftype = get_ref_type(c, name);
1838 if (reftype == CELL)
1839 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1840 else /* (reftype == FREE) */
1841 arg = compiler_lookup_arg(c->u->u_freevars, name);
1842 if (arg == -1) {
1843 printf("lookup %s in %s %d %d\n"
1844 "freevars of %s: %s\n",
1845 PyObject_REPR(name),
1846 PyString_AS_STRING(c->u->u_name),
1847 reftype, arg,
1848 PyString_AS_STRING(co->co_name),
1849 PyObject_REPR(co->co_freevars));
1850 Py_FatalError("compiler_make_closure()");
1851 }
1852 ADDOP_I(c, LOAD_CLOSURE, arg);
1853 }
1854 ADDOP_I(c, BUILD_TUPLE, free);
1855 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1856 ADDOP_I(c, MAKE_CLOSURE, args);
1857 return 1;
1858}
1859
1860static int
1861compiler_decorators(struct compiler *c, asdl_seq* decos)
1862{
1863 int i;
1864
1865 if (!decos)
1866 return 1;
1867
1868 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1869 VISIT(c, expr, asdl_seq_GET(decos, i));
1870 }
1871 return 1;
1872}
1873
1874static int
1875compiler_arguments(struct compiler *c, arguments_ty args)
1876{
1877 int i;
1878 int n = asdl_seq_LEN(args->args);
1879 /* Correctly handle nested argument lists */
1880 for (i = 0; i < n; i++) {
1881 expr_ty arg = asdl_seq_GET(args->args, i);
1882 if (arg->kind == Tuple_kind) {
1883 PyObject *id = PyString_FromFormat(".%d", i);
1884 if (id == NULL) {
1885 return 0;
1886 }
1887 if (!compiler_nameop(c, id, Load)) {
1888 Py_DECREF(id);
1889 return 0;
1890 }
1891 Py_DECREF(id);
1892 VISIT(c, expr, arg);
1893 }
1894 }
1895 return 1;
1896}
1897
1898static int
1899compiler_function(struct compiler *c, stmt_ty s)
1900{
1901 PyCodeObject *co;
1902 PyObject *first_const = Py_None;
1903 arguments_ty args = s->v.FunctionDef.args;
1904 asdl_seq* decos = s->v.FunctionDef.decorators;
1905 stmt_ty st;
1906 int i, n, docstring;
1907
1908 assert(s->kind == FunctionDef_kind);
1909
1910 if (!compiler_decorators(c, decos))
1911 return 0;
1912 if (args->defaults)
1913 VISIT_SEQ(c, expr, args->defaults);
1914 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1915 s->lineno))
1916 return 0;
1917
1918 st = asdl_seq_GET(s->v.FunctionDef.body, 0);
1919 docstring = compiler_isdocstring(st);
1920 if (docstring)
1921 first_const = st->v.Expr.value->v.Str.s;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001922 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1923 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001924 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001925 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001926
1927 /* unpack nested arguments */
1928 compiler_arguments(c, args);
1929
1930 c->u->u_argcount = asdl_seq_LEN(args->args);
1931 n = asdl_seq_LEN(s->v.FunctionDef.body);
1932 /* if there was a docstring, we need to skip the first statement */
1933 for (i = docstring; i < n; i++) {
1934 stmt_ty s2 = asdl_seq_GET(s->v.FunctionDef.body, i);
1935 if (i == 0 && s2->kind == Expr_kind &&
1936 s2->v.Expr.value->kind == Str_kind)
1937 continue;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001938 VISIT_IN_SCOPE(c, stmt, s2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001939 }
1940 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001941 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942 if (co == NULL)
1943 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944
1945 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00001946 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001947
1948 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1949 ADDOP_I(c, CALL_FUNCTION, 1);
1950 }
1951
1952 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1953}
1954
1955static int
1956compiler_class(struct compiler *c, stmt_ty s)
1957{
1958 int n;
1959 PyCodeObject *co;
1960 PyObject *str;
1961 /* push class name on stack, needed by BUILD_CLASS */
1962 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1963 /* push the tuple of base classes on the stack */
1964 n = asdl_seq_LEN(s->v.ClassDef.bases);
1965 if (n > 0)
1966 VISIT_SEQ(c, expr, s->v.ClassDef.bases);
1967 ADDOP_I(c, BUILD_TUPLE, n);
1968 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s,
1969 s->lineno))
1970 return 0;
1971 c->u->u_private = s->v.ClassDef.name;
1972 Py_INCREF(c->u->u_private);
1973 str = PyString_InternFromString("__name__");
1974 if (!str || !compiler_nameop(c, str, Load)) {
1975 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001976 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001977 return 0;
1978 }
1979
1980 Py_DECREF(str);
1981 str = PyString_InternFromString("__module__");
1982 if (!str || !compiler_nameop(c, str, Store)) {
1983 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001984 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001985 return 0;
1986 }
1987 Py_DECREF(str);
1988
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001989 if (!compiler_body(c, s->v.ClassDef.body)) {
1990 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001991 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001992 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001993
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001994 ADDOP_IN_SCOPE(c, LOAD_LOCALS);
1995 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001996 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001997 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001998 if (co == NULL)
1999 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002000
2001 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00002002 Py_DECREF(co);
2003
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004 ADDOP_I(c, CALL_FUNCTION, 0);
2005 ADDOP(c, BUILD_CLASS);
2006 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2007 return 0;
2008 return 1;
2009}
2010
2011static int
2012compiler_lambda(struct compiler *c, expr_ty e)
2013{
2014 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002015 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002016 arguments_ty args = e->v.Lambda.args;
2017 assert(e->kind == Lambda_kind);
2018
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002019 if (!name) {
2020 name = PyString_InternFromString("<lambda>");
2021 if (!name)
2022 return 0;
2023 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002024
2025 if (args->defaults)
2026 VISIT_SEQ(c, expr, args->defaults);
2027 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2028 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002029
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002030 /* unpack nested arguments */
2031 compiler_arguments(c, args);
2032
2033 c->u->u_argcount = asdl_seq_LEN(args->args);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00002034 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2035 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002036 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00002037 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002038 if (co == NULL)
2039 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002040
2041 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00002042 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002043
2044 return 1;
2045}
2046
2047static int
2048compiler_print(struct compiler *c, stmt_ty s)
2049{
2050 int i, n;
2051 bool dest;
2052
2053 assert(s->kind == Print_kind);
2054 n = asdl_seq_LEN(s->v.Print.values);
2055 dest = false;
2056 if (s->v.Print.dest) {
2057 VISIT(c, expr, s->v.Print.dest);
2058 dest = true;
2059 }
2060 for (i = 0; i < n; i++) {
2061 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i);
2062 if (dest) {
2063 ADDOP(c, DUP_TOP);
2064 VISIT(c, expr, e);
2065 ADDOP(c, ROT_TWO);
2066 ADDOP(c, PRINT_ITEM_TO);
2067 }
2068 else {
2069 VISIT(c, expr, e);
2070 ADDOP(c, PRINT_ITEM);
2071 }
2072 }
2073 if (s->v.Print.nl) {
2074 if (dest)
2075 ADDOP(c, PRINT_NEWLINE_TO)
2076 else
2077 ADDOP(c, PRINT_NEWLINE)
2078 }
2079 else if (dest)
2080 ADDOP(c, POP_TOP);
2081 return 1;
2082}
2083
2084static int
2085compiler_if(struct compiler *c, stmt_ty s)
2086{
2087 basicblock *end, *next;
2088
2089 assert(s->kind == If_kind);
2090 end = compiler_new_block(c);
2091 if (end == NULL)
2092 return 0;
2093 next = compiler_new_block(c);
2094 if (next == NULL)
2095 return 0;
2096 VISIT(c, expr, s->v.If.test);
2097 ADDOP_JREL(c, JUMP_IF_FALSE, next);
2098 ADDOP(c, POP_TOP);
2099 VISIT_SEQ(c, stmt, s->v.If.body);
2100 ADDOP_JREL(c, JUMP_FORWARD, end);
2101 compiler_use_next_block(c, next);
2102 ADDOP(c, POP_TOP);
2103 if (s->v.If.orelse)
2104 VISIT_SEQ(c, stmt, s->v.If.orelse);
2105 compiler_use_next_block(c, end);
2106 return 1;
2107}
2108
2109static int
2110compiler_for(struct compiler *c, stmt_ty s)
2111{
2112 basicblock *start, *cleanup, *end;
2113
2114 start = compiler_new_block(c);
2115 cleanup = compiler_new_block(c);
2116 end = compiler_new_block(c);
2117 if (start == NULL || end == NULL || cleanup == NULL)
2118 return 0;
2119 ADDOP_JREL(c, SETUP_LOOP, end);
2120 if (!compiler_push_fblock(c, LOOP, start))
2121 return 0;
2122 VISIT(c, expr, s->v.For.iter);
2123 ADDOP(c, GET_ITER);
2124 compiler_use_next_block(c, start);
2125 ADDOP_JREL(c, FOR_ITER, cleanup);
2126 VISIT(c, expr, s->v.For.target);
2127 VISIT_SEQ(c, stmt, s->v.For.body);
2128 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2129 compiler_use_next_block(c, cleanup);
2130 ADDOP(c, POP_BLOCK);
2131 compiler_pop_fblock(c, LOOP, start);
2132 VISIT_SEQ(c, stmt, s->v.For.orelse);
2133 compiler_use_next_block(c, end);
2134 return 1;
2135}
2136
2137static int
2138compiler_while(struct compiler *c, stmt_ty s)
2139{
2140 basicblock *loop, *orelse, *end, *anchor = NULL;
2141 int constant = expr_constant(s->v.While.test);
2142
2143 if (constant == 0)
2144 return 1;
2145 loop = compiler_new_block(c);
2146 end = compiler_new_block(c);
2147 if (constant == -1) {
2148 anchor = compiler_new_block(c);
2149 if (anchor == NULL)
2150 return 0;
2151 }
2152 if (loop == NULL || end == NULL)
2153 return 0;
2154 if (s->v.While.orelse) {
2155 orelse = compiler_new_block(c);
2156 if (orelse == NULL)
2157 return 0;
2158 }
2159 else
2160 orelse = NULL;
2161
2162 ADDOP_JREL(c, SETUP_LOOP, end);
2163 compiler_use_next_block(c, loop);
2164 if (!compiler_push_fblock(c, LOOP, loop))
2165 return 0;
2166 if (constant == -1) {
2167 VISIT(c, expr, s->v.While.test);
2168 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
2169 ADDOP(c, POP_TOP);
2170 }
2171 VISIT_SEQ(c, stmt, s->v.While.body);
2172 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
2173
2174 /* XXX should the two POP instructions be in a separate block
2175 if there is no else clause ?
2176 */
2177
2178 if (constant == -1) {
2179 compiler_use_next_block(c, anchor);
2180 ADDOP(c, POP_TOP);
2181 ADDOP(c, POP_BLOCK);
2182 }
2183 compiler_pop_fblock(c, LOOP, loop);
2184 if (orelse != NULL)
2185 VISIT_SEQ(c, stmt, s->v.While.orelse);
2186 compiler_use_next_block(c, end);
2187
2188 return 1;
2189}
2190
2191static int
2192compiler_continue(struct compiler *c)
2193{
2194 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
2195 int i;
2196
2197 if (!c->u->u_nfblocks)
2198 return compiler_error(c, LOOP_ERROR_MSG);
2199 i = c->u->u_nfblocks - 1;
2200 switch (c->u->u_fblock[i].fb_type) {
2201 case LOOP:
2202 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
2203 break;
2204 case EXCEPT:
2205 case FINALLY_TRY:
2206 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP)
2207 ;
2208 if (i == -1)
2209 return compiler_error(c, LOOP_ERROR_MSG);
2210 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
2211 break;
2212 case FINALLY_END:
2213 return compiler_error(c,
2214 "'continue' not supported inside 'finally' clause");
2215 }
2216
2217 return 1;
2218}
2219
2220/* Code generated for "try: <body> finally: <finalbody>" is as follows:
2221
2222 SETUP_FINALLY L
2223 <code for body>
2224 POP_BLOCK
2225 LOAD_CONST <None>
2226 L: <code for finalbody>
2227 END_FINALLY
2228
2229 The special instructions use the block stack. Each block
2230 stack entry contains the instruction that created it (here
2231 SETUP_FINALLY), the level of the value stack at the time the
2232 block stack entry was created, and a label (here L).
2233
2234 SETUP_FINALLY:
2235 Pushes the current value stack level and the label
2236 onto the block stack.
2237 POP_BLOCK:
2238 Pops en entry from the block stack, and pops the value
2239 stack until its level is the same as indicated on the
2240 block stack. (The label is ignored.)
2241 END_FINALLY:
2242 Pops a variable number of entries from the *value* stack
2243 and re-raises the exception they specify. The number of
2244 entries popped depends on the (pseudo) exception type.
2245
2246 The block stack is unwound when an exception is raised:
2247 when a SETUP_FINALLY entry is found, the exception is pushed
2248 onto the value stack (and the exception condition is cleared),
2249 and the interpreter jumps to the label gotten from the block
2250 stack.
2251*/
2252
2253static int
2254compiler_try_finally(struct compiler *c, stmt_ty s)
2255{
2256 basicblock *body, *end;
2257 body = compiler_new_block(c);
2258 end = compiler_new_block(c);
2259 if (body == NULL || end == NULL)
2260 return 0;
2261
2262 ADDOP_JREL(c, SETUP_FINALLY, end);
2263 compiler_use_next_block(c, body);
2264 if (!compiler_push_fblock(c, FINALLY_TRY, body))
2265 return 0;
2266 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
2267 ADDOP(c, POP_BLOCK);
2268 compiler_pop_fblock(c, FINALLY_TRY, body);
2269
2270 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2271 compiler_use_next_block(c, end);
2272 if (!compiler_push_fblock(c, FINALLY_END, end))
2273 return 0;
2274 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
2275 ADDOP(c, END_FINALLY);
2276 compiler_pop_fblock(c, FINALLY_END, end);
2277
2278 return 1;
2279}
2280
2281/*
2282 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
2283 (The contents of the value stack is shown in [], with the top
2284 at the right; 'tb' is trace-back info, 'val' the exception's
2285 associated value, and 'exc' the exception.)
2286
2287 Value stack Label Instruction Argument
2288 [] SETUP_EXCEPT L1
2289 [] <code for S>
2290 [] POP_BLOCK
2291 [] JUMP_FORWARD L0
2292
2293 [tb, val, exc] L1: DUP )
2294 [tb, val, exc, exc] <evaluate E1> )
2295 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2296 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
2297 [tb, val, exc, 1] POP )
2298 [tb, val, exc] POP
2299 [tb, val] <assign to V1> (or POP if no V1)
2300 [tb] POP
2301 [] <code for S1>
2302 JUMP_FORWARD L0
2303
2304 [tb, val, exc, 0] L2: POP
2305 [tb, val, exc] DUP
2306 .............................etc.......................
2307
2308 [tb, val, exc, 0] Ln+1: POP
2309 [tb, val, exc] END_FINALLY # re-raise exception
2310
2311 [] L0: <next statement>
2312
2313 Of course, parts are not generated if Vi or Ei is not present.
2314*/
2315static int
2316compiler_try_except(struct compiler *c, stmt_ty s)
2317{
2318 basicblock *body, *orelse, *except, *end;
2319 int i, n;
2320
2321 body = compiler_new_block(c);
2322 except = compiler_new_block(c);
2323 orelse = compiler_new_block(c);
2324 end = compiler_new_block(c);
2325 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
2326 return 0;
2327 ADDOP_JREL(c, SETUP_EXCEPT, except);
2328 compiler_use_next_block(c, body);
2329 if (!compiler_push_fblock(c, EXCEPT, body))
2330 return 0;
2331 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
2332 ADDOP(c, POP_BLOCK);
2333 compiler_pop_fblock(c, EXCEPT, body);
2334 ADDOP_JREL(c, JUMP_FORWARD, orelse);
2335 n = asdl_seq_LEN(s->v.TryExcept.handlers);
2336 compiler_use_next_block(c, except);
2337 for (i = 0; i < n; i++) {
2338 excepthandler_ty handler = asdl_seq_GET(
2339 s->v.TryExcept.handlers, i);
2340 if (!handler->type && i < n-1)
2341 return compiler_error(c, "default 'except:' must be last");
2342 except = compiler_new_block(c);
2343 if (except == NULL)
2344 return 0;
2345 if (handler->type) {
2346 ADDOP(c, DUP_TOP);
2347 VISIT(c, expr, handler->type);
2348 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2349 ADDOP_JREL(c, JUMP_IF_FALSE, except);
2350 ADDOP(c, POP_TOP);
2351 }
2352 ADDOP(c, POP_TOP);
2353 if (handler->name) {
2354 VISIT(c, expr, handler->name);
2355 }
2356 else {
2357 ADDOP(c, POP_TOP);
2358 }
2359 ADDOP(c, POP_TOP);
2360 VISIT_SEQ(c, stmt, handler->body);
2361 ADDOP_JREL(c, JUMP_FORWARD, end);
2362 compiler_use_next_block(c, except);
2363 if (handler->type)
2364 ADDOP(c, POP_TOP);
2365 }
2366 ADDOP(c, END_FINALLY);
2367 compiler_use_next_block(c, orelse);
2368 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2369 compiler_use_next_block(c, end);
2370 return 1;
2371}
2372
2373static int
2374compiler_import_as(struct compiler *c, identifier name, identifier asname)
2375{
2376 /* The IMPORT_NAME opcode was already generated. This function
2377 merely needs to bind the result to a name.
2378
2379 If there is a dot in name, we need to split it and emit a
2380 LOAD_ATTR for each name.
2381 */
2382 const char *src = PyString_AS_STRING(name);
2383 const char *dot = strchr(src, '.');
2384 if (dot) {
2385 /* Consume the base module name to get the first attribute */
2386 src = dot + 1;
2387 while (dot) {
2388 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00002389 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002390 dot = strchr(src, '.');
Armin Rigo31441302005-10-21 12:57:31 +00002391 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002392 dot ? dot - src : strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002393 if (!attr)
2394 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002395 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002396 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002397 src = dot + 1;
2398 }
2399 }
2400 return compiler_nameop(c, asname, Store);
2401}
2402
2403static int
2404compiler_import(struct compiler *c, stmt_ty s)
2405{
2406 /* The Import node stores a module name like a.b.c as a single
2407 string. This is convenient for all cases except
2408 import a.b.c as d
2409 where we need to parse that string to extract the individual
2410 module names.
2411 XXX Perhaps change the representation to make this case simpler?
2412 */
2413 int i, n = asdl_seq_LEN(s->v.Import.names);
2414 for (i = 0; i < n; i++) {
2415 alias_ty alias = asdl_seq_GET(s->v.Import.names, i);
2416 int r;
2417
2418 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2419 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2420
2421 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002422 r = compiler_import_as(c, alias->name, alias->asname);
2423 if (!r)
2424 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002425 }
2426 else {
2427 identifier tmp = alias->name;
2428 const char *base = PyString_AS_STRING(alias->name);
2429 char *dot = strchr(base, '.');
2430 if (dot)
2431 tmp = PyString_FromStringAndSize(base,
2432 dot - base);
2433 r = compiler_nameop(c, tmp, Store);
2434 if (dot) {
2435 Py_DECREF(tmp);
2436 }
2437 if (!r)
2438 return r;
2439 }
2440 }
2441 return 1;
2442}
2443
2444static int
2445compiler_from_import(struct compiler *c, stmt_ty s)
2446{
2447 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002448
2449 PyObject *names = PyTuple_New(n);
2450 if (!names)
2451 return 0;
2452
2453 /* build up the names */
2454 for (i = 0; i < n; i++) {
2455 alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
2456 Py_INCREF(alias->name);
2457 PyTuple_SET_ITEM(names, i, alias->name);
2458 }
2459
2460 if (s->lineno > c->c_future->ff_lineno) {
2461 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
2462 "__future__")) {
2463 Py_DECREF(names);
2464 return compiler_error(c,
2465 "from __future__ imports must occur "
2466 "at the beginning of the file");
2467
2468 }
2469 }
2470
2471 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002472 Py_DECREF(names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002473 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2474 for (i = 0; i < n; i++) {
2475 alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
2476 identifier store_name;
2477
2478 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
2479 assert(n == 1);
2480 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002481 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002482 }
2483
2484 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2485 store_name = alias->name;
2486 if (alias->asname)
2487 store_name = alias->asname;
2488
2489 if (!compiler_nameop(c, store_name, Store)) {
2490 Py_DECREF(names);
2491 return 0;
2492 }
2493 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002494 /* remove imported module */
2495 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002496 return 1;
2497}
2498
2499static int
2500compiler_assert(struct compiler *c, stmt_ty s)
2501{
2502 static PyObject *assertion_error = NULL;
2503 basicblock *end;
2504
2505 if (Py_OptimizeFlag)
2506 return 1;
2507 if (assertion_error == NULL) {
2508 assertion_error = PyString_FromString("AssertionError");
2509 if (assertion_error == NULL)
2510 return 0;
2511 }
2512 VISIT(c, expr, s->v.Assert.test);
2513 end = compiler_new_block(c);
2514 if (end == NULL)
2515 return 0;
2516 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2517 ADDOP(c, POP_TOP);
2518 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2519 if (s->v.Assert.msg) {
2520 VISIT(c, expr, s->v.Assert.msg);
2521 ADDOP_I(c, RAISE_VARARGS, 2);
2522 }
2523 else {
2524 ADDOP_I(c, RAISE_VARARGS, 1);
2525 }
2526 compiler_use_block(c, end);
2527 ADDOP(c, POP_TOP);
2528 return 1;
2529}
2530
2531static int
2532compiler_visit_stmt(struct compiler *c, stmt_ty s)
2533{
2534 int i, n;
2535
2536 c->u->u_lineno = s->lineno;
2537 c->u->u_lineno_set = false;
2538 switch (s->kind) {
2539 case FunctionDef_kind:
2540 return compiler_function(c, s);
2541 case ClassDef_kind:
2542 return compiler_class(c, s);
2543 case Return_kind:
2544 if (c->u->u_ste->ste_type != FunctionBlock)
2545 return compiler_error(c, "'return' outside function");
2546 if (s->v.Return.value) {
2547 if (c->u->u_ste->ste_generator) {
2548 return compiler_error(c,
2549 "'return' with argument inside generator");
2550 }
2551 VISIT(c, expr, s->v.Return.value);
2552 }
2553 else
2554 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2555 ADDOP(c, RETURN_VALUE);
2556 break;
2557 case Delete_kind:
2558 VISIT_SEQ(c, expr, s->v.Delete.targets)
2559 break;
2560 case Assign_kind:
2561 n = asdl_seq_LEN(s->v.Assign.targets);
2562 VISIT(c, expr, s->v.Assign.value);
2563 for (i = 0; i < n; i++) {
2564 if (i < n - 1)
2565 ADDOP(c, DUP_TOP);
2566 VISIT(c, expr,
2567 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2568 }
2569 break;
2570 case AugAssign_kind:
2571 return compiler_augassign(c, s);
2572 case Print_kind:
2573 return compiler_print(c, s);
2574 case For_kind:
2575 return compiler_for(c, s);
2576 case While_kind:
2577 return compiler_while(c, s);
2578 case If_kind:
2579 return compiler_if(c, s);
2580 case Raise_kind:
2581 n = 0;
2582 if (s->v.Raise.type) {
2583 VISIT(c, expr, s->v.Raise.type);
2584 n++;
2585 if (s->v.Raise.inst) {
2586 VISIT(c, expr, s->v.Raise.inst);
2587 n++;
2588 if (s->v.Raise.tback) {
2589 VISIT(c, expr, s->v.Raise.tback);
2590 n++;
2591 }
2592 }
2593 }
2594 ADDOP_I(c, RAISE_VARARGS, n);
2595 break;
2596 case TryExcept_kind:
2597 return compiler_try_except(c, s);
2598 case TryFinally_kind:
2599 return compiler_try_finally(c, s);
2600 case Assert_kind:
2601 return compiler_assert(c, s);
2602 case Import_kind:
2603 return compiler_import(c, s);
2604 case ImportFrom_kind:
2605 return compiler_from_import(c, s);
2606 case Exec_kind:
2607 VISIT(c, expr, s->v.Exec.body);
2608 if (s->v.Exec.globals) {
2609 VISIT(c, expr, s->v.Exec.globals);
2610 if (s->v.Exec.locals) {
2611 VISIT(c, expr, s->v.Exec.locals);
2612 } else {
2613 ADDOP(c, DUP_TOP);
2614 }
2615 } else {
2616 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2617 ADDOP(c, DUP_TOP);
2618 }
2619 ADDOP(c, EXEC_STMT);
2620 break;
2621 case Global_kind:
2622 break;
2623 case Expr_kind:
2624 VISIT(c, expr, s->v.Expr.value);
2625 if (c->c_interactive && c->c_nestlevel <= 1) {
2626 ADDOP(c, PRINT_EXPR);
2627 }
2628 else {
2629 ADDOP(c, POP_TOP);
2630 }
2631 break;
2632 case Pass_kind:
2633 break;
2634 case Break_kind:
2635 if (!c->u->u_nfblocks)
2636 return compiler_error(c, "'break' outside loop");
2637 ADDOP(c, BREAK_LOOP);
2638 break;
2639 case Continue_kind:
2640 return compiler_continue(c);
2641 }
2642 return 1;
2643}
2644
2645static int
2646unaryop(unaryop_ty op)
2647{
2648 switch (op) {
2649 case Invert:
2650 return UNARY_INVERT;
2651 case Not:
2652 return UNARY_NOT;
2653 case UAdd:
2654 return UNARY_POSITIVE;
2655 case USub:
2656 return UNARY_NEGATIVE;
2657 }
2658 return 0;
2659}
2660
2661static int
2662binop(struct compiler *c, operator_ty op)
2663{
2664 switch (op) {
2665 case Add:
2666 return BINARY_ADD;
2667 case Sub:
2668 return BINARY_SUBTRACT;
2669 case Mult:
2670 return BINARY_MULTIPLY;
2671 case Div:
2672 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2673 return BINARY_TRUE_DIVIDE;
2674 else
2675 return BINARY_DIVIDE;
2676 case Mod:
2677 return BINARY_MODULO;
2678 case Pow:
2679 return BINARY_POWER;
2680 case LShift:
2681 return BINARY_LSHIFT;
2682 case RShift:
2683 return BINARY_RSHIFT;
2684 case BitOr:
2685 return BINARY_OR;
2686 case BitXor:
2687 return BINARY_XOR;
2688 case BitAnd:
2689 return BINARY_AND;
2690 case FloorDiv:
2691 return BINARY_FLOOR_DIVIDE;
2692 }
2693 return 0;
2694}
2695
2696static int
2697cmpop(cmpop_ty op)
2698{
2699 switch (op) {
2700 case Eq:
2701 return PyCmp_EQ;
2702 case NotEq:
2703 return PyCmp_NE;
2704 case Lt:
2705 return PyCmp_LT;
2706 case LtE:
2707 return PyCmp_LE;
2708 case Gt:
2709 return PyCmp_GT;
2710 case GtE:
2711 return PyCmp_GE;
2712 case Is:
2713 return PyCmp_IS;
2714 case IsNot:
2715 return PyCmp_IS_NOT;
2716 case In:
2717 return PyCmp_IN;
2718 case NotIn:
2719 return PyCmp_NOT_IN;
2720 }
2721 return PyCmp_BAD;
2722}
2723
2724static int
2725inplace_binop(struct compiler *c, operator_ty op)
2726{
2727 switch (op) {
2728 case Add:
2729 return INPLACE_ADD;
2730 case Sub:
2731 return INPLACE_SUBTRACT;
2732 case Mult:
2733 return INPLACE_MULTIPLY;
2734 case Div:
2735 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2736 return INPLACE_TRUE_DIVIDE;
2737 else
2738 return INPLACE_DIVIDE;
2739 case Mod:
2740 return INPLACE_MODULO;
2741 case Pow:
2742 return INPLACE_POWER;
2743 case LShift:
2744 return INPLACE_LSHIFT;
2745 case RShift:
2746 return INPLACE_RSHIFT;
2747 case BitOr:
2748 return INPLACE_OR;
2749 case BitXor:
2750 return INPLACE_XOR;
2751 case BitAnd:
2752 return INPLACE_AND;
2753 case FloorDiv:
2754 return INPLACE_FLOOR_DIVIDE;
2755 }
Neal Norwitz4737b232005-11-19 23:58:29 +00002756 PyErr_Format(PyExc_SystemError,
2757 "inplace binary op %d should not be possible",
2758 op);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002759 return 0;
2760}
2761
2762static int
2763compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2764{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002765 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002766 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2767
2768 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002769 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002770 /* XXX AugStore isn't used anywhere! */
2771
2772 /* First check for assignment to __debug__. Param? */
2773 if ((ctx == Store || ctx == AugStore || ctx == Del)
2774 && !strcmp(PyString_AS_STRING(name), "__debug__")) {
2775 return compiler_error(c, "can not assign to __debug__");
2776 }
2777
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002778 mangled = _Py_Mangle(c->u->u_private, name);
2779 if (!mangled)
2780 return 0;
2781
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002782 op = 0;
2783 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002784 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002785 switch (scope) {
2786 case FREE:
2787 dict = c->u->u_freevars;
2788 optype = OP_DEREF;
2789 break;
2790 case CELL:
2791 dict = c->u->u_cellvars;
2792 optype = OP_DEREF;
2793 break;
2794 case LOCAL:
2795 if (c->u->u_ste->ste_type == FunctionBlock)
2796 optype = OP_FAST;
2797 break;
2798 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002799 if (c->u->u_ste->ste_type == FunctionBlock &&
2800 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002801 optype = OP_GLOBAL;
2802 break;
2803 case GLOBAL_EXPLICIT:
2804 optype = OP_GLOBAL;
2805 break;
2806 }
2807
2808 /* XXX Leave assert here, but handle __doc__ and the like better */
2809 assert(scope || PyString_AS_STRING(name)[0] == '_');
2810
2811 switch (optype) {
2812 case OP_DEREF:
2813 switch (ctx) {
2814 case Load: op = LOAD_DEREF; break;
2815 case Store: op = STORE_DEREF; break;
2816 case AugLoad:
2817 case AugStore:
2818 break;
2819 case Del:
2820 PyErr_Format(PyExc_SyntaxError,
2821 "can not delete variable '%s' referenced "
2822 "in nested scope",
2823 PyString_AS_STRING(name));
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002824 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002826 case Param:
Neal Norwitz4737b232005-11-19 23:58:29 +00002827 PyErr_SetString(PyExc_SystemError,
2828 "param invalid for deref variable");
2829 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002830 }
2831 break;
2832 case OP_FAST:
2833 switch (ctx) {
2834 case Load: op = LOAD_FAST; break;
2835 case Store: op = STORE_FAST; break;
2836 case Del: op = DELETE_FAST; break;
2837 case AugLoad:
2838 case AugStore:
2839 break;
2840 case Param:
Neal Norwitz4737b232005-11-19 23:58:29 +00002841 PyErr_SetString(PyExc_SystemError,
2842 "param invalid for local variable");
2843 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002844 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002845 ADDOP_O(c, op, mangled, varnames);
2846 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002847 return 1;
2848 case OP_GLOBAL:
2849 switch (ctx) {
2850 case Load: op = LOAD_GLOBAL; break;
2851 case Store: op = STORE_GLOBAL; break;
2852 case Del: op = DELETE_GLOBAL; break;
2853 case AugLoad:
2854 case AugStore:
2855 break;
2856 case Param:
Neal Norwitz4737b232005-11-19 23:58:29 +00002857 PyErr_SetString(PyExc_SystemError,
2858 "param invalid for global variable");
2859 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002860 }
2861 break;
2862 case OP_NAME:
2863 switch (ctx) {
2864 case Load: op = LOAD_NAME; break;
2865 case Store: op = STORE_NAME; break;
2866 case Del: op = DELETE_NAME; break;
2867 case AugLoad:
2868 case AugStore:
2869 break;
2870 case Param:
Neal Norwitz4737b232005-11-19 23:58:29 +00002871 PyErr_SetString(PyExc_SystemError,
2872 "param invalid for name variable");
2873 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002874 }
2875 break;
2876 }
2877
2878 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002879 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002880 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002881 if (arg < 0)
2882 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002883 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002884}
2885
2886static int
2887compiler_boolop(struct compiler *c, expr_ty e)
2888{
2889 basicblock *end;
2890 int jumpi, i, n;
2891 asdl_seq *s;
2892
2893 assert(e->kind == BoolOp_kind);
2894 if (e->v.BoolOp.op == And)
2895 jumpi = JUMP_IF_FALSE;
2896 else
2897 jumpi = JUMP_IF_TRUE;
2898 end = compiler_new_block(c);
2899 if (end < 0)
2900 return 0;
2901 s = e->v.BoolOp.values;
2902 n = asdl_seq_LEN(s) - 1;
2903 for (i = 0; i < n; ++i) {
2904 VISIT(c, expr, asdl_seq_GET(s, i));
2905 ADDOP_JREL(c, jumpi, end);
2906 ADDOP(c, POP_TOP)
2907 }
2908 VISIT(c, expr, asdl_seq_GET(s, n));
2909 compiler_use_next_block(c, end);
2910 return 1;
2911}
2912
2913static int
2914compiler_list(struct compiler *c, expr_ty e)
2915{
2916 int n = asdl_seq_LEN(e->v.List.elts);
2917 if (e->v.List.ctx == Store) {
2918 ADDOP_I(c, UNPACK_SEQUENCE, n);
2919 }
2920 VISIT_SEQ(c, expr, e->v.List.elts);
2921 if (e->v.List.ctx == Load) {
2922 ADDOP_I(c, BUILD_LIST, n);
2923 }
2924 return 1;
2925}
2926
2927static int
2928compiler_tuple(struct compiler *c, expr_ty e)
2929{
2930 int n = asdl_seq_LEN(e->v.Tuple.elts);
2931 if (e->v.Tuple.ctx == Store) {
2932 ADDOP_I(c, UNPACK_SEQUENCE, n);
2933 }
2934 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2935 if (e->v.Tuple.ctx == Load) {
2936 ADDOP_I(c, BUILD_TUPLE, n);
2937 }
2938 return 1;
2939}
2940
2941static int
2942compiler_compare(struct compiler *c, expr_ty e)
2943{
2944 int i, n;
2945 basicblock *cleanup = NULL;
2946
2947 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2948 VISIT(c, expr, e->v.Compare.left);
2949 n = asdl_seq_LEN(e->v.Compare.ops);
2950 assert(n > 0);
2951 if (n > 1) {
2952 cleanup = compiler_new_block(c);
2953 if (cleanup == NULL)
2954 return 0;
2955 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, 0));
2956 }
2957 for (i = 1; i < n; i++) {
2958 ADDOP(c, DUP_TOP);
2959 ADDOP(c, ROT_THREE);
2960 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
2961 ADDOP_I(c, COMPARE_OP,
2962 cmpop((cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i - 1)));
2963 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2964 NEXT_BLOCK(c);
2965 ADDOP(c, POP_TOP);
2966 if (i < (n - 1))
2967 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, i));
2968 }
2969 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, n - 1));
2970 ADDOP_I(c, COMPARE_OP,
2971 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
2972 cmpop((cmpop_ty)asdl_seq_GET(e->v.Compare.ops, n - 1)));
2973 if (n > 1) {
2974 basicblock *end = compiler_new_block(c);
2975 if (end == NULL)
2976 return 0;
2977 ADDOP_JREL(c, JUMP_FORWARD, end);
2978 compiler_use_next_block(c, cleanup);
2979 ADDOP(c, ROT_TWO);
2980 ADDOP(c, POP_TOP);
2981 compiler_use_next_block(c, end);
2982 }
2983 return 1;
2984}
2985
2986static int
2987compiler_call(struct compiler *c, expr_ty e)
2988{
2989 int n, code = 0;
2990
2991 VISIT(c, expr, e->v.Call.func);
2992 n = asdl_seq_LEN(e->v.Call.args);
2993 VISIT_SEQ(c, expr, e->v.Call.args);
2994 if (e->v.Call.keywords) {
2995 VISIT_SEQ(c, keyword, e->v.Call.keywords);
2996 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
2997 }
2998 if (e->v.Call.starargs) {
2999 VISIT(c, expr, e->v.Call.starargs);
3000 code |= 1;
3001 }
3002 if (e->v.Call.kwargs) {
3003 VISIT(c, expr, e->v.Call.kwargs);
3004 code |= 2;
3005 }
3006 switch (code) {
3007 case 0:
3008 ADDOP_I(c, CALL_FUNCTION, n);
3009 break;
3010 case 1:
3011 ADDOP_I(c, CALL_FUNCTION_VAR, n);
3012 break;
3013 case 2:
3014 ADDOP_I(c, CALL_FUNCTION_KW, n);
3015 break;
3016 case 3:
3017 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
3018 break;
3019 }
3020 return 1;
3021}
3022
3023static int
3024compiler_listcomp_generator(struct compiler *c, PyObject *tmpname,
3025 asdl_seq *generators, int gen_index,
3026 expr_ty elt)
3027{
3028 /* generate code for the iterator, then each of the ifs,
3029 and then write to the element */
3030
3031 comprehension_ty l;
3032 basicblock *start, *anchor, *skip, *if_cleanup;
3033 int i, n;
3034
3035 start = compiler_new_block(c);
3036 skip = compiler_new_block(c);
3037 if_cleanup = compiler_new_block(c);
3038 anchor = compiler_new_block(c);
3039
3040 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3041 anchor == NULL)
3042 return 0;
3043
3044 l = asdl_seq_GET(generators, gen_index);
3045 VISIT(c, expr, l->iter);
3046 ADDOP(c, GET_ITER);
3047 compiler_use_next_block(c, start);
3048 ADDOP_JREL(c, FOR_ITER, anchor);
3049 NEXT_BLOCK(c);
3050 VISIT(c, expr, l->target);
3051
3052 /* XXX this needs to be cleaned up...a lot! */
3053 n = asdl_seq_LEN(l->ifs);
3054 for (i = 0; i < n; i++) {
3055 expr_ty e = asdl_seq_GET(l->ifs, i);
3056 VISIT(c, expr, e);
3057 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
3058 NEXT_BLOCK(c);
3059 ADDOP(c, POP_TOP);
3060 }
3061
3062 if (++gen_index < asdl_seq_LEN(generators))
3063 if (!compiler_listcomp_generator(c, tmpname,
3064 generators, gen_index, elt))
3065 return 0;
3066
3067 /* only append after the last for generator */
3068 if (gen_index >= asdl_seq_LEN(generators)) {
3069 if (!compiler_nameop(c, tmpname, Load))
3070 return 0;
3071 VISIT(c, expr, elt);
3072 ADDOP_I(c, CALL_FUNCTION, 1);
3073 ADDOP(c, POP_TOP);
3074
3075 compiler_use_next_block(c, skip);
3076 }
3077 for (i = 0; i < n; i++) {
3078 ADDOP_I(c, JUMP_FORWARD, 1);
3079 if (i == 0)
3080 compiler_use_next_block(c, if_cleanup);
3081 ADDOP(c, POP_TOP);
3082 }
3083 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3084 compiler_use_next_block(c, anchor);
3085 /* delete the append method added to locals */
3086 if (gen_index == 1)
3087 if (!compiler_nameop(c, tmpname, Del))
3088 return 0;
3089
3090 return 1;
3091}
3092
3093static int
3094compiler_listcomp(struct compiler *c, expr_ty e)
3095{
3096 char tmpname[256];
3097 identifier tmp;
3098 int rc = 0;
3099 static identifier append;
3100 asdl_seq *generators = e->v.ListComp.generators;
3101
3102 assert(e->kind == ListComp_kind);
3103 if (!append) {
3104 append = PyString_InternFromString("append");
3105 if (!append)
3106 return 0;
3107 }
3108 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
3109 tmp = PyString_FromString(tmpname);
3110 if (!tmp)
3111 return 0;
3112 ADDOP_I(c, BUILD_LIST, 0);
3113 ADDOP(c, DUP_TOP);
3114 ADDOP_O(c, LOAD_ATTR, append, names);
3115 if (compiler_nameop(c, tmp, Store))
3116 rc = compiler_listcomp_generator(c, tmp, generators, 0,
3117 e->v.ListComp.elt);
3118 Py_DECREF(tmp);
3119 return rc;
3120}
3121
3122static int
3123compiler_genexp_generator(struct compiler *c,
3124 asdl_seq *generators, int gen_index,
3125 expr_ty elt)
3126{
3127 /* generate code for the iterator, then each of the ifs,
3128 and then write to the element */
3129
3130 comprehension_ty ge;
3131 basicblock *start, *anchor, *skip, *if_cleanup, *end;
3132 int i, n;
3133
3134 start = compiler_new_block(c);
3135 skip = compiler_new_block(c);
3136 if_cleanup = compiler_new_block(c);
3137 anchor = compiler_new_block(c);
3138 end = compiler_new_block(c);
3139
3140 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3141 anchor == NULL || end == NULL)
3142 return 0;
3143
3144 ge = asdl_seq_GET(generators, gen_index);
3145 ADDOP_JREL(c, SETUP_LOOP, end);
3146 if (!compiler_push_fblock(c, LOOP, start))
3147 return 0;
3148
3149 if (gen_index == 0) {
3150 /* Receive outermost iter as an implicit argument */
3151 c->u->u_argcount = 1;
3152 ADDOP_I(c, LOAD_FAST, 0);
3153 }
3154 else {
3155 /* Sub-iter - calculate on the fly */
3156 VISIT(c, expr, ge->iter);
3157 ADDOP(c, GET_ITER);
3158 }
3159 compiler_use_next_block(c, start);
3160 ADDOP_JREL(c, FOR_ITER, anchor);
3161 NEXT_BLOCK(c);
3162 VISIT(c, expr, ge->target);
3163
3164 /* XXX this needs to be cleaned up...a lot! */
3165 n = asdl_seq_LEN(ge->ifs);
3166 for (i = 0; i < n; i++) {
3167 expr_ty e = asdl_seq_GET(ge->ifs, i);
3168 VISIT(c, expr, e);
3169 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
3170 NEXT_BLOCK(c);
3171 ADDOP(c, POP_TOP);
3172 }
3173
3174 if (++gen_index < asdl_seq_LEN(generators))
3175 if (!compiler_genexp_generator(c, generators, gen_index, elt))
3176 return 0;
3177
3178 /* only append after the last 'for' generator */
3179 if (gen_index >= asdl_seq_LEN(generators)) {
3180 VISIT(c, expr, elt);
3181 ADDOP(c, YIELD_VALUE);
3182 ADDOP(c, POP_TOP);
3183
3184 compiler_use_next_block(c, skip);
3185 }
3186 for (i = 0; i < n; i++) {
3187 ADDOP_I(c, JUMP_FORWARD, 1);
3188 if (i == 0)
3189 compiler_use_next_block(c, if_cleanup);
3190
3191 ADDOP(c, POP_TOP);
3192 }
3193 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3194 compiler_use_next_block(c, anchor);
3195 ADDOP(c, POP_BLOCK);
3196 compiler_pop_fblock(c, LOOP, start);
3197 compiler_use_next_block(c, end);
3198
3199 return 1;
3200}
3201
3202static int
3203compiler_genexp(struct compiler *c, expr_ty e)
3204{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00003205 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003206 PyCodeObject *co;
3207 expr_ty outermost_iter = ((comprehension_ty)
3208 (asdl_seq_GET(e->v.GeneratorExp.generators,
3209 0)))->iter;
3210
Nick Coghlan944d3eb2005-11-16 12:46:55 +00003211 if (!name) {
3212 name = PyString_FromString("<genexpr>");
3213 if (!name)
3214 return 0;
3215 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003216
3217 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
3218 return 0;
3219 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
3220 e->v.GeneratorExp.elt);
3221 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00003222 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003223 if (co == NULL)
3224 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003225
3226 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00003227 Py_DECREF(co);
3228
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003229 VISIT(c, expr, outermost_iter);
3230 ADDOP(c, GET_ITER);
3231 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003232
3233 return 1;
3234}
3235
3236static int
3237compiler_visit_keyword(struct compiler *c, keyword_ty k)
3238{
3239 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3240 VISIT(c, expr, k->value);
3241 return 1;
3242}
3243
3244/* Test whether expression is constant. For constants, report
3245 whether they are true or false.
3246
3247 Return values: 1 for true, 0 for false, -1 for non-constant.
3248 */
3249
3250static int
3251expr_constant(expr_ty e)
3252{
3253 switch (e->kind) {
3254 case Num_kind:
3255 return PyObject_IsTrue(e->v.Num.n);
3256 case Str_kind:
3257 return PyObject_IsTrue(e->v.Str.s);
3258 default:
3259 return -1;
3260 }
3261}
3262
3263static int
3264compiler_visit_expr(struct compiler *c, expr_ty e)
3265{
3266 int i, n;
3267
3268 if (e->lineno > c->u->u_lineno) {
3269 c->u->u_lineno = e->lineno;
3270 c->u->u_lineno_set = false;
3271 }
3272 switch (e->kind) {
3273 case BoolOp_kind:
3274 return compiler_boolop(c, e);
3275 case BinOp_kind:
3276 VISIT(c, expr, e->v.BinOp.left);
3277 VISIT(c, expr, e->v.BinOp.right);
3278 ADDOP(c, binop(c, e->v.BinOp.op));
3279 break;
3280 case UnaryOp_kind:
3281 VISIT(c, expr, e->v.UnaryOp.operand);
3282 ADDOP(c, unaryop(e->v.UnaryOp.op));
3283 break;
3284 case Lambda_kind:
3285 return compiler_lambda(c, e);
3286 case Dict_kind:
3287 /* XXX get rid of arg? */
3288 ADDOP_I(c, BUILD_MAP, 0);
3289 n = asdl_seq_LEN(e->v.Dict.values);
3290 /* We must arrange things just right for STORE_SUBSCR.
3291 It wants the stack to look like (value) (dict) (key) */
3292 for (i = 0; i < n; i++) {
3293 ADDOP(c, DUP_TOP);
3294 VISIT(c, expr, asdl_seq_GET(e->v.Dict.values, i));
3295 ADDOP(c, ROT_TWO);
3296 VISIT(c, expr, asdl_seq_GET(e->v.Dict.keys, i));
3297 ADDOP(c, STORE_SUBSCR);
3298 }
3299 break;
3300 case ListComp_kind:
3301 return compiler_listcomp(c, e);
3302 case GeneratorExp_kind:
3303 return compiler_genexp(c, e);
3304 case Yield_kind:
3305 if (c->u->u_ste->ste_type != FunctionBlock)
3306 return compiler_error(c, "'yield' outside function");
3307 /*
3308 for (i = 0; i < c->u->u_nfblocks; i++) {
3309 if (c->u->u_fblock[i].fb_type == FINALLY_TRY)
3310 return compiler_error(
3311 c, "'yield' not allowed in a 'try' "
3312 "block with a 'finally' clause");
3313 }
3314 */
3315 if (e->v.Yield.value) {
3316 VISIT(c, expr, e->v.Yield.value);
3317 }
3318 else {
3319 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3320 }
3321 ADDOP(c, YIELD_VALUE);
3322 break;
3323 case Compare_kind:
3324 return compiler_compare(c, e);
3325 case Call_kind:
3326 return compiler_call(c, e);
3327 case Repr_kind:
3328 VISIT(c, expr, e->v.Repr.value);
3329 ADDOP(c, UNARY_CONVERT);
3330 break;
3331 case Num_kind:
3332 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3333 break;
3334 case Str_kind:
3335 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3336 break;
3337 /* The following exprs can be assignment targets. */
3338 case Attribute_kind:
3339 if (e->v.Attribute.ctx != AugStore)
3340 VISIT(c, expr, e->v.Attribute.value);
3341 switch (e->v.Attribute.ctx) {
3342 case AugLoad:
3343 ADDOP(c, DUP_TOP);
3344 /* Fall through to load */
3345 case Load:
3346 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3347 break;
3348 case AugStore:
3349 ADDOP(c, ROT_TWO);
3350 /* Fall through to save */
3351 case Store:
3352 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3353 break;
3354 case Del:
3355 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3356 break;
3357 case Param:
Neal Norwitz4737b232005-11-19 23:58:29 +00003358 PyErr_SetString(PyExc_SystemError,
3359 "param invalid in attribute expression");
3360 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003361 }
3362 break;
3363 case Subscript_kind:
3364 switch (e->v.Subscript.ctx) {
3365 case AugLoad:
3366 VISIT(c, expr, e->v.Subscript.value);
3367 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3368 break;
3369 case Load:
3370 VISIT(c, expr, e->v.Subscript.value);
3371 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3372 break;
3373 case AugStore:
3374 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3375 break;
3376 case Store:
3377 VISIT(c, expr, e->v.Subscript.value);
3378 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3379 break;
3380 case Del:
3381 VISIT(c, expr, e->v.Subscript.value);
3382 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3383 break;
3384 case Param:
Neal Norwitz4737b232005-11-19 23:58:29 +00003385 PyErr_SetString(PyExc_SystemError,
3386 "param invalid in subscript expression");
3387 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003388 }
3389 break;
3390 case Name_kind:
3391 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3392 /* child nodes of List and Tuple will have expr_context set */
3393 case List_kind:
3394 return compiler_list(c, e);
3395 case Tuple_kind:
3396 return compiler_tuple(c, e);
3397 }
3398 return 1;
3399}
3400
3401static int
3402compiler_augassign(struct compiler *c, stmt_ty s)
3403{
3404 expr_ty e = s->v.AugAssign.target;
3405 expr_ty auge;
3406
3407 assert(s->kind == AugAssign_kind);
3408
3409 switch (e->kind) {
3410 case Attribute_kind:
3411 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003412 AugLoad, e->lineno, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003413 if (auge == NULL)
3414 return 0;
3415 VISIT(c, expr, auge);
3416 VISIT(c, expr, s->v.AugAssign.value);
3417 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3418 auge->v.Attribute.ctx = AugStore;
3419 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003420 break;
3421 case Subscript_kind:
3422 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003423 AugLoad, e->lineno, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003424 if (auge == NULL)
3425 return 0;
3426 VISIT(c, expr, auge);
3427 VISIT(c, expr, s->v.AugAssign.value);
3428 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3429 auge->v.Subscript.ctx = AugStore;
3430 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003431 break;
3432 case Name_kind:
3433 VISIT(c, expr, s->v.AugAssign.target);
3434 VISIT(c, expr, s->v.AugAssign.value);
3435 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3436 return compiler_nameop(c, e->v.Name.id, Store);
3437 default:
3438 fprintf(stderr,
3439 "invalid node type for augmented assignment\n");
3440 return 0;
3441 }
3442 return 1;
3443}
3444
3445static int
3446compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3447{
3448 struct fblockinfo *f;
3449 if (c->u->u_nfblocks >= CO_MAXBLOCKS)
3450 return 0;
3451 f = &c->u->u_fblock[c->u->u_nfblocks++];
3452 f->fb_type = t;
3453 f->fb_block = b;
3454 return 1;
3455}
3456
3457static void
3458compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3459{
3460 struct compiler_unit *u = c->u;
3461 assert(u->u_nfblocks > 0);
3462 u->u_nfblocks--;
3463 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3464 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3465}
3466
3467/* Raises a SyntaxError and returns 0.
3468 If something goes wrong, a different exception may be raised.
3469*/
3470
3471static int
3472compiler_error(struct compiler *c, const char *errstr)
3473{
3474 PyObject *loc;
3475 PyObject *u = NULL, *v = NULL;
3476
3477 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3478 if (!loc) {
3479 Py_INCREF(Py_None);
3480 loc = Py_None;
3481 }
3482 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3483 Py_None, loc);
3484 if (!u)
3485 goto exit;
3486 v = Py_BuildValue("(zO)", errstr, u);
3487 if (!v)
3488 goto exit;
3489 PyErr_SetObject(PyExc_SyntaxError, v);
3490 exit:
3491 Py_DECREF(loc);
3492 Py_XDECREF(u);
3493 Py_XDECREF(v);
3494 return 0;
3495}
3496
3497static int
3498compiler_handle_subscr(struct compiler *c, const char *kind,
3499 expr_context_ty ctx)
3500{
3501 int op = 0;
3502
3503 /* XXX this code is duplicated */
3504 switch (ctx) {
3505 case AugLoad: /* fall through to Load */
3506 case Load: op = BINARY_SUBSCR; break;
3507 case AugStore:/* fall through to Store */
3508 case Store: op = STORE_SUBSCR; break;
3509 case Del: op = DELETE_SUBSCR; break;
3510 case Param:
3511 fprintf(stderr,
3512 "invalid %s kind %d in subscript\n",
3513 kind, ctx);
3514 return 0;
3515 }
3516 if (ctx == AugLoad) {
3517 ADDOP_I(c, DUP_TOPX, 2);
3518 }
3519 else if (ctx == AugStore) {
3520 ADDOP(c, ROT_THREE);
3521 }
3522 ADDOP(c, op);
3523 return 1;
3524}
3525
3526static int
3527compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3528{
3529 int n = 2;
3530 assert(s->kind == Slice_kind);
3531
3532 /* only handles the cases where BUILD_SLICE is emitted */
3533 if (s->v.Slice.lower) {
3534 VISIT(c, expr, s->v.Slice.lower);
3535 }
3536 else {
3537 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3538 }
3539
3540 if (s->v.Slice.upper) {
3541 VISIT(c, expr, s->v.Slice.upper);
3542 }
3543 else {
3544 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3545 }
3546
3547 if (s->v.Slice.step) {
3548 n++;
3549 VISIT(c, expr, s->v.Slice.step);
3550 }
3551 ADDOP_I(c, BUILD_SLICE, n);
3552 return 1;
3553}
3554
3555static int
3556compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3557{
3558 int op = 0, slice_offset = 0, stack_count = 0;
3559
3560 assert(s->v.Slice.step == NULL);
3561 if (s->v.Slice.lower) {
3562 slice_offset++;
3563 stack_count++;
3564 if (ctx != AugStore)
3565 VISIT(c, expr, s->v.Slice.lower);
3566 }
3567 if (s->v.Slice.upper) {
3568 slice_offset += 2;
3569 stack_count++;
3570 if (ctx != AugStore)
3571 VISIT(c, expr, s->v.Slice.upper);
3572 }
3573
3574 if (ctx == AugLoad) {
3575 switch (stack_count) {
3576 case 0: ADDOP(c, DUP_TOP); break;
3577 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3578 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3579 }
3580 }
3581 else if (ctx == AugStore) {
3582 switch (stack_count) {
3583 case 0: ADDOP(c, ROT_TWO); break;
3584 case 1: ADDOP(c, ROT_THREE); break;
3585 case 2: ADDOP(c, ROT_FOUR); break;
3586 }
3587 }
3588
3589 switch (ctx) {
3590 case AugLoad: /* fall through to Load */
3591 case Load: op = SLICE; break;
3592 case AugStore:/* fall through to Store */
3593 case Store: op = STORE_SLICE; break;
3594 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003595 case Param:
3596 PyErr_SetString(PyExc_SystemError,
3597 "param invalid in simple slice");
3598 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003599 }
3600
3601 ADDOP(c, op + slice_offset);
3602 return 1;
3603}
3604
3605static int
3606compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3607 expr_context_ty ctx)
3608{
3609 switch (s->kind) {
3610 case Ellipsis_kind:
3611 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3612 break;
3613 case Slice_kind:
3614 return compiler_slice(c, s, ctx);
3615 break;
3616 case Index_kind:
3617 VISIT(c, expr, s->v.Index.value);
3618 break;
3619 case ExtSlice_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00003620 PyErr_SetString(PyExc_SystemError,
3621 "extended slice invalid in nested slice");
3622 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003623 }
3624 return 1;
3625}
3626
3627
3628static int
3629compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3630{
3631 switch (s->kind) {
3632 case Ellipsis_kind:
3633 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3634 break;
3635 case Slice_kind:
3636 if (!s->v.Slice.step)
3637 return compiler_simple_slice(c, s, ctx);
3638 if (!compiler_slice(c, s, ctx))
3639 return 0;
3640 if (ctx == AugLoad) {
3641 ADDOP_I(c, DUP_TOPX, 2);
3642 }
3643 else if (ctx == AugStore) {
3644 ADDOP(c, ROT_THREE);
3645 }
3646 return compiler_handle_subscr(c, "slice", ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003647 case ExtSlice_kind: {
3648 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3649 for (i = 0; i < n; i++) {
3650 slice_ty sub = asdl_seq_GET(s->v.ExtSlice.dims, i);
3651 if (!compiler_visit_nested_slice(c, sub, ctx))
3652 return 0;
3653 }
3654 ADDOP_I(c, BUILD_TUPLE, n);
3655 return compiler_handle_subscr(c, "extended slice", ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003656 }
3657 case Index_kind:
3658 if (ctx != AugStore)
3659 VISIT(c, expr, s->v.Index.value);
3660 return compiler_handle_subscr(c, "index", ctx);
3661 }
3662 return 1;
3663}
3664
3665/* do depth-first search of basic block graph, starting with block.
3666 post records the block indices in post-order.
3667
3668 XXX must handle implicit jumps from one block to next
3669*/
3670
3671static void
3672dfs(struct compiler *c, basicblock *b, struct assembler *a)
3673{
3674 int i;
3675 struct instr *instr = NULL;
3676
3677 if (b->b_seen)
3678 return;
3679 b->b_seen = 1;
3680 if (b->b_next != NULL)
3681 dfs(c, b->b_next, a);
3682 for (i = 0; i < b->b_iused; i++) {
3683 instr = &b->b_instr[i];
3684 if (instr->i_jrel || instr->i_jabs)
3685 dfs(c, instr->i_target, a);
3686 }
3687 a->a_postorder[a->a_nblocks++] = b;
3688}
3689
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003690static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003691stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3692{
3693 int i;
3694 struct instr *instr;
3695 if (b->b_seen || b->b_startdepth >= depth)
3696 return maxdepth;
3697 b->b_seen = 1;
3698 b->b_startdepth = depth;
3699 for (i = 0; i < b->b_iused; i++) {
3700 instr = &b->b_instr[i];
3701 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3702 if (depth > maxdepth)
3703 maxdepth = depth;
3704 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3705 if (instr->i_jrel || instr->i_jabs) {
3706 maxdepth = stackdepth_walk(c, instr->i_target,
3707 depth, maxdepth);
3708 if (instr->i_opcode == JUMP_ABSOLUTE ||
3709 instr->i_opcode == JUMP_FORWARD) {
3710 goto out; /* remaining code is dead */
3711 }
3712 }
3713 }
3714 if (b->b_next)
3715 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3716out:
3717 b->b_seen = 0;
3718 return maxdepth;
3719}
3720
3721/* Find the flow path that needs the largest stack. We assume that
3722 * cycles in the flow graph have no net effect on the stack depth.
3723 */
3724static int
3725stackdepth(struct compiler *c)
3726{
3727 basicblock *b, *entryblock;
3728 entryblock = NULL;
3729 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3730 b->b_seen = 0;
3731 b->b_startdepth = INT_MIN;
3732 entryblock = b;
3733 }
3734 return stackdepth_walk(c, entryblock, 0, 0);
3735}
3736
3737static int
3738assemble_init(struct assembler *a, int nblocks, int firstlineno)
3739{
3740 memset(a, 0, sizeof(struct assembler));
3741 a->a_lineno = firstlineno;
3742 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3743 if (!a->a_bytecode)
3744 return 0;
3745 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3746 if (!a->a_lnotab)
3747 return 0;
3748 a->a_postorder = (basicblock **)PyObject_Malloc(
3749 sizeof(basicblock *) * nblocks);
3750 if (!a->a_postorder)
3751 return 0;
3752 return 1;
3753}
3754
3755static void
3756assemble_free(struct assembler *a)
3757{
3758 Py_XDECREF(a->a_bytecode);
3759 Py_XDECREF(a->a_lnotab);
3760 if (a->a_postorder)
3761 PyObject_Free(a->a_postorder);
3762}
3763
3764/* Return the size of a basic block in bytes. */
3765
3766static int
3767instrsize(struct instr *instr)
3768{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003769 if (!instr->i_hasarg)
3770 return 1;
3771 if (instr->i_oparg > 0xffff)
3772 return 6;
3773 return 3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003774}
3775
3776static int
3777blocksize(basicblock *b)
3778{
3779 int i;
3780 int size = 0;
3781
3782 for (i = 0; i < b->b_iused; i++)
3783 size += instrsize(&b->b_instr[i]);
3784 return size;
3785}
3786
3787/* All about a_lnotab.
3788
3789c_lnotab is an array of unsigned bytes disguised as a Python string.
3790It is used to map bytecode offsets to source code line #s (when needed
3791for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003792
Tim Peters2a7f3842001-06-09 09:26:21 +00003793The array is conceptually a list of
3794 (bytecode offset increment, line number increment)
3795pairs. The details are important and delicate, best illustrated by example:
3796
3797 byte code offset source code line number
3798 0 1
3799 6 2
3800 50 7
3801 350 307
3802 361 308
3803
3804The first trick is that these numbers aren't stored, only the increments
3805from one row to the next (this doesn't really work, but it's a start):
3806
3807 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3808
3809The second trick is that an unsigned byte can't hold negative values, or
3810values larger than 255, so (a) there's a deep assumption that byte code
3811offsets and their corresponding line #s both increase monotonically, and (b)
3812if at least one column jumps by more than 255 from one row to the next, more
3813than one pair is written to the table. In case #b, there's no way to know
3814from looking at the table later how many were written. That's the delicate
3815part. A user of c_lnotab desiring to find the source line number
3816corresponding to a bytecode address A should do something like this
3817
3818 lineno = addr = 0
3819 for addr_incr, line_incr in c_lnotab:
3820 addr += addr_incr
3821 if addr > A:
3822 return lineno
3823 lineno += line_incr
3824
3825In order for this to work, when the addr field increments by more than 255,
3826the line # increment in each pair generated must be 0 until the remaining addr
3827increment is < 256. So, in the example above, com_set_lineno should not (as
3828was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
3829255, 0, 45, 255, 0, 45.
3830*/
3831
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003832static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003833assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003834{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003835 int d_bytecode, d_lineno;
3836 int len;
3837 char *lnotab;
3838
3839 d_bytecode = a->a_offset - a->a_lineno_off;
3840 d_lineno = i->i_lineno - a->a_lineno;
3841
3842 assert(d_bytecode >= 0);
3843 assert(d_lineno >= 0);
3844
3845 if (d_lineno == 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003846 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003847
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003848 if (d_bytecode > 255) {
3849 int i, nbytes, ncodes = d_bytecode / 255;
3850 nbytes = a->a_lnotab_off + 2 * ncodes;
3851 len = PyString_GET_SIZE(a->a_lnotab);
3852 if (nbytes >= len) {
3853 if (len * 2 < nbytes)
3854 len = nbytes;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003855 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003856 len *= 2;
3857 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3858 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003859 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003860 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3861 for (i = 0; i < ncodes; i++) {
3862 *lnotab++ = 255;
3863 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003864 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003865 d_bytecode -= ncodes * 255;
3866 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003867 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003868 assert(d_bytecode <= 255);
3869 if (d_lineno > 255) {
3870 int i, nbytes, ncodes = d_lineno / 255;
3871 nbytes = a->a_lnotab_off + 2 * ncodes;
3872 len = PyString_GET_SIZE(a->a_lnotab);
3873 if (nbytes >= len) {
3874 if (len * 2 < nbytes)
3875 len = nbytes;
Guido van Rossum635abd21997-01-06 22:56:52 +00003876 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003877 len *= 2;
3878 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3879 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003880 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003881 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3882 *lnotab++ = 255;
3883 *lnotab++ = d_bytecode;
3884 d_bytecode = 0;
3885 for (i = 1; i < ncodes; i++) {
3886 *lnotab++ = 255;
3887 *lnotab++ = 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003888 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003889 d_lineno -= ncodes * 255;
3890 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003891 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003892
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003893 len = PyString_GET_SIZE(a->a_lnotab);
3894 if (a->a_lnotab_off + 2 >= len) {
3895 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003896 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003897 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003898 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003899
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003900 a->a_lnotab_off += 2;
3901 if (d_bytecode) {
3902 *lnotab++ = d_bytecode;
3903 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003904 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003905 else { /* First line of a block; def stmt, etc. */
3906 *lnotab++ = 0;
3907 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003908 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003909 a->a_lineno = i->i_lineno;
3910 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003911 return 1;
3912}
3913
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003914/* assemble_emit()
3915 Extend the bytecode with a new instruction.
3916 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003917*/
3918
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003919static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003920assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003921{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003922 int size, arg = 0, ext = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003923 int len = PyString_GET_SIZE(a->a_bytecode);
3924 char *code;
3925
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003926 size = instrsize(i);
3927 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003928 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003929 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003930 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003931 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003932 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003933 if (a->a_offset + size >= len) {
3934 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003935 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003936 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003937 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3938 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003939 if (size == 6) {
3940 assert(i->i_hasarg);
3941 *code++ = (char)EXTENDED_ARG;
3942 *code++ = ext & 0xff;
3943 *code++ = ext >> 8;
3944 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003945 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003946 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003947 if (i->i_hasarg) {
3948 assert(size == 3 || size == 6);
3949 *code++ = arg & 0xff;
3950 *code++ = arg >> 8;
3951 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003952 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003953}
3954
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003955static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003956assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003957{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003958 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003959 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003960 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003961
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003962 /* Compute the size of each block and fixup jump args.
3963 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003964start:
3965 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003966 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003967 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003968 bsize = blocksize(b);
3969 b->b_offset = totsize;
3970 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003971 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003972 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003973 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3974 bsize = b->b_offset;
3975 for (i = 0; i < b->b_iused; i++) {
3976 struct instr *instr = &b->b_instr[i];
3977 /* Relative jumps are computed relative to
3978 the instruction pointer after fetching
3979 the jump instruction.
3980 */
3981 bsize += instrsize(instr);
3982 if (instr->i_jabs)
3983 instr->i_oparg = instr->i_target->b_offset;
3984 else if (instr->i_jrel) {
3985 int delta = instr->i_target->b_offset - bsize;
3986 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003987 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003988 else
3989 continue;
3990 if (instr->i_oparg > 0xffff)
3991 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003992 }
3993 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003994
3995 /* XXX: This is an awful hack that could hurt performance, but
3996 on the bright side it should work until we come up
3997 with a better solution.
3998
3999 In the meantime, should the goto be dropped in favor
4000 of a loop?
4001
4002 The issue is that in the first loop blocksize() is called
4003 which calls instrsize() which requires i_oparg be set
4004 appropriately. There is a bootstrap problem because
4005 i_oparg is calculated in the second loop above.
4006
4007 So we loop until we stop seeing new EXTENDED_ARGs.
4008 The only EXTENDED_ARGs that could be popping up are
4009 ones in jump instructions. So this should converge
4010 fairly quickly.
4011 */
4012 if (last_extended_arg_count != extended_arg_count) {
4013 last_extended_arg_count = extended_arg_count;
4014 goto start;
4015 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004016}
4017
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004018static PyObject *
4019dict_keys_inorder(PyObject *dict, int offset)
4020{
4021 PyObject *tuple, *k, *v;
4022 int i, pos = 0, size = PyDict_Size(dict);
4023
4024 tuple = PyTuple_New(size);
4025 if (tuple == NULL)
4026 return NULL;
4027 while (PyDict_Next(dict, &pos, &k, &v)) {
4028 i = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004029 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004030 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004031 assert((i - offset) < size);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004032 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004033 PyTuple_SET_ITEM(tuple, i - offset, k);
4034 }
4035 return tuple;
4036}
4037
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004038static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004039compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004040{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004041 PySTEntryObject *ste = c->u->u_ste;
4042 int flags = 0, n;
4043 if (ste->ste_type != ModuleBlock)
4044 flags |= CO_NEWLOCALS;
4045 if (ste->ste_type == FunctionBlock) {
4046 if (!ste->ste_unoptimized)
4047 flags |= CO_OPTIMIZED;
4048 if (ste->ste_nested)
4049 flags |= CO_NESTED;
4050 if (ste->ste_generator)
4051 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004052 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004053 if (ste->ste_varargs)
4054 flags |= CO_VARARGS;
4055 if (ste->ste_varkeywords)
4056 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00004057 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004058 flags |= CO_GENERATOR;
4059 if (c->c_flags->cf_flags & CO_FUTURE_DIVISION)
4060 flags |= CO_FUTURE_DIVISION;
4061 n = PyDict_Size(c->u->u_freevars);
4062 if (n < 0)
4063 return -1;
4064 if (n == 0) {
4065 n = PyDict_Size(c->u->u_cellvars);
4066 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004067 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004068 if (n == 0) {
4069 flags |= CO_NOFREE;
4070 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004071 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004072
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004073 return flags;
4074}
4075
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004076static PyCodeObject *
4077makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004078{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004079 PyObject *tmp;
4080 PyCodeObject *co = NULL;
4081 PyObject *consts = NULL;
4082 PyObject *names = NULL;
4083 PyObject *varnames = NULL;
4084 PyObject *filename = NULL;
4085 PyObject *name = NULL;
4086 PyObject *freevars = NULL;
4087 PyObject *cellvars = NULL;
4088 PyObject *bytecode = NULL;
4089 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004090
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004091 tmp = dict_keys_inorder(c->u->u_consts, 0);
4092 if (!tmp)
4093 goto error;
4094 consts = PySequence_List(tmp); /* optimize_code requires a list */
4095 Py_DECREF(tmp);
4096
4097 names = dict_keys_inorder(c->u->u_names, 0);
4098 varnames = dict_keys_inorder(c->u->u_varnames, 0);
4099 if (!consts || !names || !varnames)
4100 goto error;
4101
4102 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4103 if (!cellvars)
4104 goto error;
4105 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4106 if (!freevars)
4107 goto error;
4108 filename = PyString_FromString(c->c_filename);
4109 if (!filename)
4110 goto error;
4111
4112 nlocals = PyDict_Size(c->u->u_varnames);
4113 flags = compute_code_flags(c);
4114 if (flags < 0)
4115 goto error;
4116
4117 bytecode = optimize_code(a->a_bytecode, consts, names, a->a_lnotab);
4118 if (!bytecode)
4119 goto error;
4120
4121 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4122 if (!tmp)
4123 goto error;
4124 Py_DECREF(consts);
4125 consts = tmp;
4126
4127 co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags,
4128 bytecode, consts, names, varnames,
4129 freevars, cellvars,
4130 filename, c->u->u_name,
4131 c->u->u_firstlineno,
4132 a->a_lnotab);
4133 error:
4134 Py_XDECREF(consts);
4135 Py_XDECREF(names);
4136 Py_XDECREF(varnames);
4137 Py_XDECREF(filename);
4138 Py_XDECREF(name);
4139 Py_XDECREF(freevars);
4140 Py_XDECREF(cellvars);
4141 Py_XDECREF(bytecode);
4142 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004143}
4144
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004145static PyCodeObject *
4146assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004147{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004148 basicblock *b, *entryblock;
4149 struct assembler a;
4150 int i, j, nblocks;
4151 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004152
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004153 /* Make sure every block that falls off the end returns None.
4154 XXX NEXT_BLOCK() isn't quite right, because if the last
4155 block ends with a jump or return b_next shouldn't set.
4156 */
4157 if (!c->u->u_curblock->b_return) {
4158 NEXT_BLOCK(c);
4159 if (addNone)
4160 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4161 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004162 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004163
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004164 nblocks = 0;
4165 entryblock = NULL;
4166 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4167 nblocks++;
4168 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004169 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004170
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004171 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4172 goto error;
4173 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004174
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004175 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004176 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004177
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004178 /* Emit code in reverse postorder from dfs. */
4179 for (i = a.a_nblocks - 1; i >= 0; i--) {
4180 basicblock *b = a.a_postorder[i];
4181 for (j = 0; j < b->b_iused; j++)
4182 if (!assemble_emit(&a, &b->b_instr[j]))
4183 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004184 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004185
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004186 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4187 goto error;
4188 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
4189 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004190
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004191 co = makecode(c, &a);
4192 error:
4193 assemble_free(&a);
4194 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004195}