blob: 60b4933a9f60b4452782f38a7ee20212c6005525 [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);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000174static basicblock *compiler_use_new_block(struct compiler *);
175static int compiler_error(struct compiler *, const char *);
176static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
177
178static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
179static int compiler_visit_stmt(struct compiler *, stmt_ty);
180static int compiler_visit_keyword(struct compiler *, keyword_ty);
181static int compiler_visit_expr(struct compiler *, expr_ty);
182static int compiler_augassign(struct compiler *, stmt_ty);
183static int compiler_visit_slice(struct compiler *, slice_ty,
184 expr_context_ty);
185
186static int compiler_push_fblock(struct compiler *, enum fblocktype,
187 basicblock *);
188static void compiler_pop_fblock(struct compiler *, enum fblocktype,
189 basicblock *);
190
191static int inplace_binop(struct compiler *, operator_ty);
192static int expr_constant(expr_ty e);
193
194static PyCodeObject *assemble(struct compiler *, int addNone);
195static PyObject *__doc__;
196
197PyObject *
198_Py_Mangle(PyObject *private, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000199{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200 /* Name mangling: __private becomes _classname__private.
201 This is independent from how the name is used. */
202 const char *p, *name = PyString_AsString(ident);
203 char *buffer;
204 size_t nlen, plen;
205 if (private == NULL || name == NULL || name[0] != '_' || name[1] != '_') {
206 Py_INCREF(ident);
207 return ident;
208 }
209 p = PyString_AsString(private);
210 nlen = strlen(name);
211 if (name[nlen-1] == '_' && name[nlen-2] == '_') {
212 Py_INCREF(ident);
213 return ident; /* Don't mangle __whatever__ */
214 }
215 /* Strip leading underscores from class name */
216 while (*p == '_')
217 p++;
218 if (*p == '\0') {
219 Py_INCREF(ident);
220 return ident; /* Don't mangle if class is just underscores */
221 }
222 plen = strlen(p);
223 ident = PyString_FromStringAndSize(NULL, 1 + nlen + plen);
224 if (!ident)
225 return 0;
226 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
227 buffer = PyString_AS_STRING(ident);
228 buffer[0] = '_';
229 strncpy(buffer+1, p, plen);
230 strcpy(buffer+1+plen, name);
231 return ident;
Michael W. Hudson60934622004-08-12 17:56:29 +0000232}
233
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000234static int
235compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000236{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000237 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000238
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000239 c->c_stack = PyList_New(0);
240 if (!c->c_stack)
241 return 0;
242
243 return 1;
244}
245
246PyCodeObject *
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000247PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
248 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000249{
250 struct compiler c;
251 PyCodeObject *co = NULL;
252 PyCompilerFlags local_flags;
253 int merged;
254
255 if (!__doc__) {
256 __doc__ = PyString_InternFromString("__doc__");
257 if (!__doc__)
258 goto error;
259 }
260
261 if (!compiler_init(&c))
262 goto error;
263 c.c_filename = filename;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000264 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000265 c.c_future = PyFuture_FromAST(mod, filename);
266 if (c.c_future == NULL)
267 goto error;
268 if (!flags) {
269 local_flags.cf_flags = 0;
270 flags = &local_flags;
271 }
272 merged = c.c_future->ff_features | flags->cf_flags;
273 c.c_future->ff_features = merged;
274 flags->cf_flags = merged;
275 c.c_flags = flags;
276 c.c_nestlevel = 0;
277
278 c.c_st = PySymtable_Build(mod, filename, c.c_future);
279 if (c.c_st == NULL) {
280 if (!PyErr_Occurred())
281 PyErr_SetString(PyExc_SystemError, "no symtable");
282 goto error;
283 }
284
285 /* XXX initialize to NULL for now, need to handle */
286 c.c_encoding = NULL;
287
288 co = compiler_mod(&c, mod);
289
290 error:
291 compiler_free(&c);
292 return co;
293}
294
295PyCodeObject *
296PyNode_Compile(struct _node *n, const char *filename)
297{
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000298 PyCodeObject *co = NULL;
299 PyArena *arena;
300 arena = PyArena_New();
301 mod_ty mod = PyAST_FromNode(n, NULL, filename, arena);
302 if (mod)
303 co = PyAST_Compile(mod, filename, NULL, arena);
304 PyArena_Free(arena);
Raymond Hettinger37a724d2003-09-15 21:43:16 +0000305 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000306}
307
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000308static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000309compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000310{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000311 if (c->c_st)
312 PySymtable_Free(c->c_st);
313 if (c->c_future)
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000314 PyMem_Free(c->c_future);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000315 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000316}
317
Guido van Rossum79f25d91997-04-29 20:08:16 +0000318static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000320{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000321 int i, n;
322 PyObject *v, *k, *dict = PyDict_New();
Guido van Rossumd076c731998-10-07 19:42:25 +0000323
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000324 n = PyList_Size(list);
325 for (i = 0; i < n; i++) {
326 v = PyInt_FromLong(i);
327 if (!v) {
328 Py_DECREF(dict);
329 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000330 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000331 k = PyList_GET_ITEM(list, i);
332 k = Py_BuildValue("(OO)", k, k->ob_type);
333 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
334 Py_XDECREF(k);
335 Py_DECREF(v);
336 Py_DECREF(dict);
337 return NULL;
338 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000339 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000340 Py_DECREF(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000341 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000342 return dict;
343}
344
345/* Return new dict containing names from src that match scope(s).
346
347 src is a symbol table dictionary. If the scope of a name matches
348 either scope_type or flag is set, insert it into the new dict. The
349 values are integers, starting at offset and increasing by one for
350 each key.
351*/
352
353static PyObject *
354dictbytype(PyObject *src, int scope_type, int flag, int offset)
355{
356 int pos = 0, i = offset, scope;
357 PyObject *k, *v, *dest = PyDict_New();
358
359 assert(offset >= 0);
360 if (dest == NULL)
361 return NULL;
362
363 while (PyDict_Next(src, &pos, &k, &v)) {
364 /* XXX this should probably be a macro in symtable.h */
365 assert(PyInt_Check(v));
366 scope = (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
367
368 if (scope == scope_type || PyInt_AS_LONG(v) & flag) {
369 PyObject *tuple, *item = PyInt_FromLong(i);
370 if (item == NULL) {
371 Py_DECREF(dest);
372 return NULL;
373 }
374 i++;
375 tuple = Py_BuildValue("(OO)", k, k->ob_type);
376 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
377 Py_DECREF(item);
378 Py_DECREF(dest);
379 Py_XDECREF(tuple);
380 return NULL;
381 }
382 Py_DECREF(item);
383 Py_DECREF(tuple);
384 }
385 }
386 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000387}
388
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000389/* Begin: Peephole optimizations ----------------------------------------- */
390
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000391#define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1]))
392#define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD)
Raymond Hettinger5b75c382003-03-28 12:05:00 +0000393#define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP)
394#define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000395#define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000396#define CODESIZE(op) (HAS_ARG(op) ? 3 : 1)
397#define ISBASICBLOCK(blocks, start, bytes) (blocks[start]==blocks[start+bytes-1])
398
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000399/* Replace LOAD_CONST c1. LOAD_CONST c2 ... LOAD_CONST cn BUILD_TUPLE n
400 with LOAD_CONST (c1, c2, ... cn).
401 The consts table must still be in list form so that the
402 new constant (c1, c2, ... cn) can be appended.
403 Called with codestr pointing to the first LOAD_CONST.
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000404 Bails out with no change if one or more of the LOAD_CONSTs is missing.
405 Also works for BUILD_LIST when followed by an "in" or "not in" test.
406*/
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000407static int
408tuple_of_constants(unsigned char *codestr, int n, PyObject *consts)
409{
410 PyObject *newconst, *constant;
411 int i, arg, len_consts;
412
413 /* Pre-conditions */
414 assert(PyList_CheckExact(consts));
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000415 assert(codestr[n*3] == BUILD_TUPLE || codestr[n*3] == BUILD_LIST);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000416 assert(GETARG(codestr, (n*3)) == n);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000417 for (i=0 ; i<n ; i++)
Raymond Hettingereffb3932004-10-30 08:55:08 +0000418 assert(codestr[i*3] == LOAD_CONST);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000419
420 /* Buildup new tuple of constants */
421 newconst = PyTuple_New(n);
422 if (newconst == NULL)
423 return 0;
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000424 len_consts = PyList_GET_SIZE(consts);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000425 for (i=0 ; i<n ; i++) {
426 arg = GETARG(codestr, (i*3));
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000427 assert(arg < len_consts);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000428 constant = PyList_GET_ITEM(consts, arg);
429 Py_INCREF(constant);
430 PyTuple_SET_ITEM(newconst, i, constant);
431 }
432
433 /* Append folded constant onto consts */
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000434 if (PyList_Append(consts, newconst)) {
435 Py_DECREF(newconst);
436 return 0;
437 }
438 Py_DECREF(newconst);
439
440 /* Write NOPs over old LOAD_CONSTS and
441 add a new LOAD_CONST newconst on top of the BUILD_TUPLE n */
442 memset(codestr, NOP, n*3);
443 codestr[n*3] = LOAD_CONST;
444 SETARG(codestr, (n*3), len_consts);
445 return 1;
446}
447
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000448/* Replace LOAD_CONST c1. LOAD_CONST c2 BINOP
449 with LOAD_CONST binop(c1,c2)
450 The consts table must still be in list form so that the
451 new constant can be appended.
452 Called with codestr pointing to the first LOAD_CONST.
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000453 Abandons the transformation if the folding fails (i.e. 1+'a').
454 If the new constant is a sequence, only folds when the size
455 is below a threshold value. That keeps pyc files from
456 becoming large in the presence of code like: (None,)*1000.
457*/
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000458static int
459fold_binops_on_constants(unsigned char *codestr, PyObject *consts)
460{
461 PyObject *newconst, *v, *w;
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000462 int len_consts, opcode, size;
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000463
464 /* Pre-conditions */
465 assert(PyList_CheckExact(consts));
466 assert(codestr[0] == LOAD_CONST);
467 assert(codestr[3] == LOAD_CONST);
468
469 /* Create new constant */
470 v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
471 w = PyList_GET_ITEM(consts, GETARG(codestr, 3));
472 opcode = codestr[6];
473 switch (opcode) {
474 case BINARY_POWER:
475 newconst = PyNumber_Power(v, w, Py_None);
476 break;
477 case BINARY_MULTIPLY:
478 newconst = PyNumber_Multiply(v, w);
479 break;
480 case BINARY_DIVIDE:
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000481 /* Cannot fold this operation statically since
482 the result can depend on the run-time presence of the -Qnew flag */
Armin Rigo664b43b2005-01-07 18:10:51 +0000483 return 0;
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000484 case BINARY_TRUE_DIVIDE:
485 newconst = PyNumber_TrueDivide(v, w);
486 break;
487 case BINARY_FLOOR_DIVIDE:
488 newconst = PyNumber_FloorDivide(v, w);
489 break;
490 case BINARY_MODULO:
491 newconst = PyNumber_Remainder(v, w);
492 break;
493 case BINARY_ADD:
494 newconst = PyNumber_Add(v, w);
495 break;
496 case BINARY_SUBTRACT:
497 newconst = PyNumber_Subtract(v, w);
498 break;
499 case BINARY_SUBSCR:
500 newconst = PyObject_GetItem(v, w);
501 break;
502 case BINARY_LSHIFT:
503 newconst = PyNumber_Lshift(v, w);
504 break;
505 case BINARY_RSHIFT:
506 newconst = PyNumber_Rshift(v, w);
507 break;
508 case BINARY_AND:
509 newconst = PyNumber_And(v, w);
510 break;
511 case BINARY_XOR:
512 newconst = PyNumber_Xor(v, w);
513 break;
514 case BINARY_OR:
515 newconst = PyNumber_Or(v, w);
516 break;
517 default:
518 /* Called with an unknown opcode */
Neal Norwitz4737b232005-11-19 23:58:29 +0000519 PyErr_Format(PyExc_SystemError,
520 "unexpected binary operation %d on a constant",
521 opcode);
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000522 return 0;
523 }
524 if (newconst == NULL) {
525 PyErr_Clear();
526 return 0;
527 }
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000528 size = PyObject_Size(newconst);
529 if (size == -1)
530 PyErr_Clear();
531 else if (size > 20) {
532 Py_DECREF(newconst);
533 return 0;
534 }
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000535
536 /* Append folded constant into consts table */
537 len_consts = PyList_GET_SIZE(consts);
538 if (PyList_Append(consts, newconst)) {
539 Py_DECREF(newconst);
540 return 0;
541 }
542 Py_DECREF(newconst);
543
544 /* Write NOP NOP NOP NOP LOAD_CONST newconst */
545 memset(codestr, NOP, 4);
546 codestr[4] = LOAD_CONST;
547 SETARG(codestr, 4, len_consts);
548 return 1;
549}
550
Raymond Hettinger80121492005-02-20 12:41:32 +0000551static int
552fold_unaryops_on_constants(unsigned char *codestr, PyObject *consts)
553{
Raymond Hettingere63a0782005-02-23 13:37:55 +0000554 PyObject *newconst=NULL, *v;
Raymond Hettinger80121492005-02-20 12:41:32 +0000555 int len_consts, opcode;
556
557 /* Pre-conditions */
558 assert(PyList_CheckExact(consts));
559 assert(codestr[0] == LOAD_CONST);
560
561 /* Create new constant */
562 v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
563 opcode = codestr[3];
564 switch (opcode) {
565 case UNARY_NEGATIVE:
Raymond Hettingere63a0782005-02-23 13:37:55 +0000566 /* Preserve the sign of -0.0 */
567 if (PyObject_IsTrue(v) == 1)
568 newconst = PyNumber_Negative(v);
Raymond Hettinger80121492005-02-20 12:41:32 +0000569 break;
570 case UNARY_CONVERT:
571 newconst = PyObject_Repr(v);
572 break;
573 case UNARY_INVERT:
574 newconst = PyNumber_Invert(v);
575 break;
576 default:
577 /* Called with an unknown opcode */
Neal Norwitz4737b232005-11-19 23:58:29 +0000578 PyErr_Format(PyExc_SystemError,
579 "unexpected unary operation %d on a constant",
580 opcode);
Raymond Hettinger80121492005-02-20 12:41:32 +0000581 return 0;
582 }
583 if (newconst == NULL) {
584 PyErr_Clear();
585 return 0;
586 }
587
588 /* Append folded constant into consts table */
589 len_consts = PyList_GET_SIZE(consts);
590 if (PyList_Append(consts, newconst)) {
591 Py_DECREF(newconst);
592 return 0;
593 }
594 Py_DECREF(newconst);
595
596 /* Write NOP LOAD_CONST newconst */
597 codestr[0] = NOP;
598 codestr[1] = LOAD_CONST;
599 SETARG(codestr, 1, len_consts);
600 return 1;
601}
602
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000603static unsigned int *
604markblocks(unsigned char *code, int len)
605{
606 unsigned int *blocks = PyMem_Malloc(len*sizeof(int));
Raymond Hettingereffb3932004-10-30 08:55:08 +0000607 int i,j, opcode, blockcnt = 0;
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000608
609 if (blocks == NULL)
610 return NULL;
611 memset(blocks, 0, len*sizeof(int));
Raymond Hettingereffb3932004-10-30 08:55:08 +0000612
613 /* Mark labels in the first pass */
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000614 for (i=0 ; i<len ; i+=CODESIZE(opcode)) {
615 opcode = code[i];
616 switch (opcode) {
617 case FOR_ITER:
618 case JUMP_FORWARD:
619 case JUMP_IF_FALSE:
620 case JUMP_IF_TRUE:
621 case JUMP_ABSOLUTE:
622 case CONTINUE_LOOP:
623 case SETUP_LOOP:
624 case SETUP_EXCEPT:
625 case SETUP_FINALLY:
626 j = GETJUMPTGT(code, i);
Raymond Hettingereffb3932004-10-30 08:55:08 +0000627 blocks[j] = 1;
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000628 break;
629 }
630 }
Raymond Hettingereffb3932004-10-30 08:55:08 +0000631 /* Build block numbers in the second pass */
632 for (i=0 ; i<len ; i++) {
633 blockcnt += blocks[i]; /* increment blockcnt over labels */
634 blocks[i] = blockcnt;
635 }
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000636 return blocks;
637}
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000638
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000639/* Perform basic peephole optimizations to components of a code object.
640 The consts object should still be in list form to allow new constants
641 to be appended.
642
643 To keep the optimizer simple, it bails out (does nothing) for code
644 containing extended arguments or that has a length over 32,700. That
645 allows us to avoid overflow and sign issues. Likewise, it bails when
646 the lineno table has complex encoding for gaps >= 255.
647
648 Optimizations are restricted to simple transformations occuring within a
649 single basic block. All transformations keep the code size the same or
650 smaller. For those that reduce size, the gaps are initially filled with
651 NOPs. Later those NOPs are removed and the jump addresses retargeted in
652 a single pass. Line numbering is adjusted accordingly. */
653
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000654static PyObject *
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000655optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *lineno_obj)
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000656{
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000657 int i, j, codelen, nops, h, adj;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000658 int tgt, tgttgt, opcode;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000659 unsigned char *codestr = NULL;
660 unsigned char *lineno;
661 int *addrmap = NULL;
662 int new_line, cum_orig_line, last_line, tabsiz;
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000663 int cumlc=0, lastlc=0; /* Count runs of consecutive LOAD_CONST codes */
Raymond Hettingereffb3932004-10-30 08:55:08 +0000664 unsigned int *blocks = NULL;
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000665 char *name;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000666
Raymond Hettingereffb3932004-10-30 08:55:08 +0000667 /* Bail out if an exception is set */
668 if (PyErr_Occurred())
669 goto exitUnchanged;
670
Raymond Hettinger1792bfb2004-08-25 17:19:38 +0000671 /* Bypass optimization when the lineno table is too complex */
672 assert(PyString_Check(lineno_obj));
Brett Cannonc9371d42005-06-25 08:23:41 +0000673 lineno = (unsigned char*)PyString_AS_STRING(lineno_obj);
Raymond Hettinger1792bfb2004-08-25 17:19:38 +0000674 tabsiz = PyString_GET_SIZE(lineno_obj);
675 if (memchr(lineno, 255, tabsiz) != NULL)
676 goto exitUnchanged;
677
Raymond Hettingera12fa142004-08-24 04:34:16 +0000678 /* Avoid situations where jump retargeting could overflow */
Raymond Hettinger06cc9732004-09-28 17:22:12 +0000679 assert(PyString_Check(code));
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000680 codelen = PyString_Size(code);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000681 if (codelen > 32700)
Raymond Hettingera12fa142004-08-24 04:34:16 +0000682 goto exitUnchanged;
683
684 /* Make a modifiable copy of the code string */
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000685 codestr = PyMem_Malloc(codelen);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000686 if (codestr == NULL)
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000687 goto exitUnchanged;
688 codestr = memcpy(codestr, PyString_AS_STRING(code), codelen);
Raymond Hettinger98bd1812004-08-06 19:46:34 +0000689
Raymond Hettinger07359a72005-02-21 20:03:14 +0000690 /* Verify that RETURN_VALUE terminates the codestring. This allows
691 the various transformation patterns to look ahead several
692 instructions without additional checks to make sure they are not
693 looking beyond the end of the code string.
694 */
695 if (codestr[codelen-1] != RETURN_VALUE)
696 goto exitUnchanged;
697
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000698 /* Mapping to new jump targets after NOPs are removed */
699 addrmap = PyMem_Malloc(codelen * sizeof(int));
700 if (addrmap == NULL)
701 goto exitUnchanged;
702
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000703 blocks = markblocks(codestr, codelen);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000704 if (blocks == NULL)
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000705 goto exitUnchanged;
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000706 assert(PyList_Check(consts));
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000707
Raymond Hettinger099ecfb2004-11-01 15:19:11 +0000708 for (i=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000709 opcode = codestr[i];
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000710
711 lastlc = cumlc;
712 cumlc = 0;
713
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000714 switch (opcode) {
715
Raymond Hettinger43ea47f2004-06-24 09:25:39 +0000716 /* Replace UNARY_NOT JUMP_IF_FALSE POP_TOP with
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000717 with JUMP_IF_TRUE POP_TOP */
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000718 case UNARY_NOT:
719 if (codestr[i+1] != JUMP_IF_FALSE ||
720 codestr[i+4] != POP_TOP ||
721 !ISBASICBLOCK(blocks,i,5))
722 continue;
723 tgt = GETJUMPTGT(codestr, (i+1));
724 if (codestr[tgt] != POP_TOP)
725 continue;
Raymond Hettinger43ea47f2004-06-24 09:25:39 +0000726 j = GETARG(codestr, i+1) + 1;
727 codestr[i] = JUMP_IF_TRUE;
728 SETARG(codestr, i, j);
729 codestr[i+3] = POP_TOP;
730 codestr[i+4] = NOP;
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000731 break;
732
733 /* not a is b --> a is not b
734 not a in b --> a not in b
735 not a is not b --> a is b
Raymond Hettingerb615bf02005-02-10 01:42:32 +0000736 not a not in b --> a in b
Raymond Hettingera1645742005-02-06 22:05:42 +0000737 */
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000738 case COMPARE_OP:
739 j = GETARG(codestr, i);
740 if (j < 6 || j > 9 ||
741 codestr[i+3] != UNARY_NOT ||
742 !ISBASICBLOCK(blocks,i,4))
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000743 continue;
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000744 SETARG(codestr, i, (j^1));
745 codestr[i+3] = NOP;
Tim Petersdb5860b2004-07-17 05:00:52 +0000746 break;
747
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000748 /* Replace LOAD_GLOBAL/LOAD_NAME None with LOAD_CONST None */
749 case LOAD_NAME:
750 case LOAD_GLOBAL:
751 j = GETARG(codestr, i);
752 name = PyString_AsString(PyTuple_GET_ITEM(names, j));
753 if (name == NULL || strcmp(name, "None") != 0)
754 continue;
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000755 for (j=0 ; j < PyList_GET_SIZE(consts) ; j++) {
756 if (PyList_GET_ITEM(consts, j) == Py_None) {
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000757 codestr[i] = LOAD_CONST;
758 SETARG(codestr, i, j);
Raymond Hettinger5dec0962004-11-02 04:20:10 +0000759 cumlc = lastlc + 1;
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000760 break;
761 }
762 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000763 break;
764
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000765 /* Skip over LOAD_CONST trueconst JUMP_IF_FALSE xx POP_TOP */
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000766 case LOAD_CONST:
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000767 cumlc = lastlc + 1;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000768 j = GETARG(codestr, i);
769 if (codestr[i+3] != JUMP_IF_FALSE ||
770 codestr[i+6] != POP_TOP ||
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000771 !ISBASICBLOCK(blocks,i,7) ||
772 !PyObject_IsTrue(PyList_GET_ITEM(consts, j)))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000773 continue;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000774 memset(codestr+i, NOP, 7);
Raymond Hettinger5dec0962004-11-02 04:20:10 +0000775 cumlc = 0;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000776 break;
777
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000778 /* Try to fold tuples of constants (includes a case for lists
779 which are only used for "in" and "not in" tests).
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000780 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000781 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
782 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000783 case BUILD_TUPLE:
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000784 case BUILD_LIST:
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000785 j = GETARG(codestr, i);
786 h = i - 3 * j;
787 if (h >= 0 &&
Raymond Hettingereffb3932004-10-30 08:55:08 +0000788 j <= lastlc &&
Brett Cannon5dc8ced2005-03-03 07:01:48 +0000789 ((opcode == BUILD_TUPLE &&
790 ISBASICBLOCK(blocks, h, 3*(j+1))) ||
791 (opcode == BUILD_LIST &&
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000792 codestr[i+3]==COMPARE_OP &&
793 ISBASICBLOCK(blocks, h, 3*(j+2)) &&
Brett Cannon5dc8ced2005-03-03 07:01:48 +0000794 (GETARG(codestr,i+3)==6 ||
795 GETARG(codestr,i+3)==7))) &&
796 tuple_of_constants(&codestr[h], j, consts)) {
Raymond Hettinger5dec0962004-11-02 04:20:10 +0000797 assert(codestr[i] == LOAD_CONST);
798 cumlc = 1;
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000799 break;
800 }
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000801 if (codestr[i+3] != UNPACK_SEQUENCE ||
802 !ISBASICBLOCK(blocks,i,6) ||
803 j != GETARG(codestr, i+3))
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000804 continue;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000805 if (j == 1) {
806 memset(codestr+i, NOP, 6);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000807 } else if (j == 2) {
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000808 codestr[i] = ROT_TWO;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000809 memset(codestr+i+1, NOP, 5);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000810 } else if (j == 3) {
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000811 codestr[i] = ROT_THREE;
812 codestr[i+1] = ROT_TWO;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000813 memset(codestr+i+2, NOP, 4);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000814 }
815 break;
816
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000817 /* Fold binary ops on constants.
818 LOAD_CONST c1 LOAD_CONST c2 BINOP --> LOAD_CONST binop(c1,c2) */
819 case BINARY_POWER:
820 case BINARY_MULTIPLY:
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000821 case BINARY_TRUE_DIVIDE:
822 case BINARY_FLOOR_DIVIDE:
823 case BINARY_MODULO:
824 case BINARY_ADD:
825 case BINARY_SUBTRACT:
826 case BINARY_SUBSCR:
827 case BINARY_LSHIFT:
828 case BINARY_RSHIFT:
829 case BINARY_AND:
830 case BINARY_XOR:
831 case BINARY_OR:
832 if (lastlc >= 2 &&
833 ISBASICBLOCK(blocks, i-6, 7) &&
834 fold_binops_on_constants(&codestr[i-6], consts)) {
835 i -= 2;
836 assert(codestr[i] == LOAD_CONST);
837 cumlc = 1;
838 }
839 break;
840
Raymond Hettinger80121492005-02-20 12:41:32 +0000841 /* Fold unary ops on constants.
842 LOAD_CONST c1 UNARY_OP --> LOAD_CONST unary_op(c) */
843 case UNARY_NEGATIVE:
844 case UNARY_CONVERT:
845 case UNARY_INVERT:
846 if (lastlc >= 1 &&
847 ISBASICBLOCK(blocks, i-3, 4) &&
848 fold_unaryops_on_constants(&codestr[i-3], consts)) {
849 i -= 2;
850 assert(codestr[i] == LOAD_CONST);
851 cumlc = 1;
852 }
853 break;
854
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000855 /* Simplify conditional jump to conditional jump where the
856 result of the first test implies the success of a similar
857 test or the failure of the opposite test.
858 Arises in code like:
Raymond Hettingereffb3932004-10-30 08:55:08 +0000859 "if a and b:"
860 "if a or b:"
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000861 "a and b or c"
Armin Rigod7bcf4d2004-10-30 21:08:59 +0000862 "(a and b) and c"
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000863 x:JUMP_IF_FALSE y y:JUMP_IF_FALSE z --> x:JUMP_IF_FALSE z
Raymond Hettinger65d3c052004-08-25 15:15:56 +0000864 x:JUMP_IF_FALSE y y:JUMP_IF_TRUE z --> x:JUMP_IF_FALSE y+3
865 where y+3 is the instruction following the second test.
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000866 */
867 case JUMP_IF_FALSE:
868 case JUMP_IF_TRUE:
869 tgt = GETJUMPTGT(codestr, i);
870 j = codestr[tgt];
871 if (j == JUMP_IF_FALSE || j == JUMP_IF_TRUE) {
872 if (j == opcode) {
873 tgttgt = GETJUMPTGT(codestr, tgt) - i - 3;
874 SETARG(codestr, i, tgttgt);
875 } else {
876 tgt -= i;
877 SETARG(codestr, i, tgt);
878 }
879 break;
880 }
881 /* Intentional fallthrough */
882
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000883 /* Replace jumps to unconditional jumps */
Raymond Hettinger255a3d02003-04-15 10:35:07 +0000884 case FOR_ITER:
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000885 case JUMP_FORWARD:
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000886 case JUMP_ABSOLUTE:
887 case CONTINUE_LOOP:
888 case SETUP_LOOP:
889 case SETUP_EXCEPT:
890 case SETUP_FINALLY:
891 tgt = GETJUMPTGT(codestr, i);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000892 if (!UNCONDITIONAL_JUMP(codestr[tgt]))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000893 continue;
894 tgttgt = GETJUMPTGT(codestr, tgt);
895 if (opcode == JUMP_FORWARD) /* JMP_ABS can go backwards */
896 opcode = JUMP_ABSOLUTE;
Raymond Hettinger5b75c382003-03-28 12:05:00 +0000897 if (!ABSOLUTE_JUMP(opcode))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000898 tgttgt -= i + 3; /* Calc relative jump addr */
899 if (tgttgt < 0) /* No backward relative jumps */
900 continue;
901 codestr[i] = opcode;
902 SETARG(codestr, i, tgttgt);
903 break;
904
905 case EXTENDED_ARG:
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000906 goto exitUnchanged;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000907
908 /* Replace RETURN LOAD_CONST None RETURN with just RETURN */
909 case RETURN_VALUE:
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000910 if (i+4 >= codelen ||
911 codestr[i+4] != RETURN_VALUE ||
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000912 !ISBASICBLOCK(blocks,i,5))
913 continue;
914 memset(codestr+i+1, NOP, 4);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000915 break;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000916 }
917 }
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000918
919 /* Fixup linenotab */
Raymond Hettinger099ecfb2004-11-01 15:19:11 +0000920 for (i=0, nops=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
921 addrmap[i] = i - nops;
922 if (codestr[i] == NOP)
923 nops++;
924 }
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000925 cum_orig_line = 0;
926 last_line = 0;
927 for (i=0 ; i < tabsiz ; i+=2) {
928 cum_orig_line += lineno[i];
929 new_line = addrmap[cum_orig_line];
Raymond Hettinger1792bfb2004-08-25 17:19:38 +0000930 assert (new_line - last_line < 255);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000931 lineno[i] =((unsigned char)(new_line - last_line));
932 last_line = new_line;
933 }
934
935 /* Remove NOPs and fixup jump targets */
936 for (i=0, h=0 ; i<codelen ; ) {
937 opcode = codestr[i];
938 switch (opcode) {
939 case NOP:
940 i++;
941 continue;
942
943 case JUMP_ABSOLUTE:
944 case CONTINUE_LOOP:
945 j = addrmap[GETARG(codestr, i)];
946 SETARG(codestr, i, j);
947 break;
948
949 case FOR_ITER:
950 case JUMP_FORWARD:
951 case JUMP_IF_FALSE:
952 case JUMP_IF_TRUE:
953 case SETUP_LOOP:
954 case SETUP_EXCEPT:
955 case SETUP_FINALLY:
956 j = addrmap[GETARG(codestr, i) + i + 3] - addrmap[i] - 3;
957 SETARG(codestr, i, j);
958 break;
959 }
960 adj = CODESIZE(opcode);
961 while (adj--)
962 codestr[h++] = codestr[i++];
963 }
Raymond Hettingera12fa142004-08-24 04:34:16 +0000964 assert(h + nops == codelen);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000965
966 code = PyString_FromStringAndSize((char *)codestr, h);
967 PyMem_Free(addrmap);
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000968 PyMem_Free(codestr);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000969 PyMem_Free(blocks);
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000970 return code;
971
972exitUnchanged:
Raymond Hettingereffb3932004-10-30 08:55:08 +0000973 if (blocks != NULL)
974 PyMem_Free(blocks);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000975 if (addrmap != NULL)
976 PyMem_Free(addrmap);
977 if (codestr != NULL)
978 PyMem_Free(codestr);
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000979 Py_INCREF(code);
980 return code;
981}
982
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000983/* End: Peephole optimizations ----------------------------------------- */
984
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000985/*
986
987Leave this debugging code for just a little longer.
988
989static void
990compiler_display_symbols(PyObject *name, PyObject *symbols)
991{
992 PyObject *key, *value;
993 int flags, pos = 0;
994
995 fprintf(stderr, "block %s\n", PyString_AS_STRING(name));
996 while (PyDict_Next(symbols, &pos, &key, &value)) {
997 flags = PyInt_AsLong(value);
998 fprintf(stderr, "var %s:", PyString_AS_STRING(key));
999 if (flags & DEF_GLOBAL)
1000 fprintf(stderr, " declared_global");
1001 if (flags & DEF_LOCAL)
1002 fprintf(stderr, " local");
1003 if (flags & DEF_PARAM)
1004 fprintf(stderr, " param");
1005 if (flags & DEF_STAR)
1006 fprintf(stderr, " stararg");
1007 if (flags & DEF_DOUBLESTAR)
1008 fprintf(stderr, " starstar");
1009 if (flags & DEF_INTUPLE)
1010 fprintf(stderr, " tuple");
1011 if (flags & DEF_FREE)
1012 fprintf(stderr, " free");
1013 if (flags & DEF_FREE_GLOBAL)
1014 fprintf(stderr, " global");
1015 if (flags & DEF_FREE_CLASS)
1016 fprintf(stderr, " free/class");
1017 if (flags & DEF_IMPORT)
1018 fprintf(stderr, " import");
1019 fprintf(stderr, "\n");
1020 }
1021 fprintf(stderr, "\n");
1022}
1023*/
1024
1025static void
1026compiler_unit_check(struct compiler_unit *u)
1027{
1028 basicblock *block;
1029 for (block = u->u_blocks; block != NULL; block = block->b_list) {
1030 assert(block != (void *)0xcbcbcbcb);
1031 assert(block != (void *)0xfbfbfbfb);
1032 assert(block != (void *)0xdbdbdbdb);
1033 if (block->b_instr != NULL) {
1034 assert(block->b_ialloc > 0);
1035 assert(block->b_iused > 0);
1036 assert(block->b_ialloc >= block->b_iused);
1037 }
1038 else {
1039 assert (block->b_iused == 0);
1040 assert (block->b_ialloc == 0);
1041 }
1042 }
1043}
1044
1045static void
1046compiler_unit_free(struct compiler_unit *u)
1047{
1048 basicblock *b, *next;
1049
1050 compiler_unit_check(u);
1051 b = u->u_blocks;
1052 while (b != NULL) {
1053 if (b->b_instr)
1054 PyObject_Free((void *)b->b_instr);
1055 next = b->b_list;
1056 PyObject_Free((void *)b);
1057 b = next;
1058 }
1059 Py_XDECREF(u->u_ste);
1060 Py_XDECREF(u->u_name);
1061 Py_XDECREF(u->u_consts);
1062 Py_XDECREF(u->u_names);
1063 Py_XDECREF(u->u_varnames);
1064 Py_XDECREF(u->u_freevars);
1065 Py_XDECREF(u->u_cellvars);
1066 Py_XDECREF(u->u_private);
1067 PyObject_Free(u);
1068}
1069
1070static int
1071compiler_enter_scope(struct compiler *c, identifier name, void *key,
1072 int lineno)
1073{
1074 struct compiler_unit *u;
1075
1076 u = PyObject_Malloc(sizeof(struct compiler_unit));
Neal Norwitz87b801c2005-12-18 04:42:47 +00001077 if (!u) {
1078 PyErr_NoMemory();
1079 return 0;
1080 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081 memset(u, 0, sizeof(struct compiler_unit));
1082 u->u_argcount = 0;
1083 u->u_ste = PySymtable_Lookup(c->c_st, key);
1084 if (!u->u_ste) {
1085 compiler_unit_free(u);
Neal Norwitz87b801c2005-12-18 04:42:47 +00001086 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087 }
1088 Py_INCREF(name);
1089 u->u_name = name;
1090 u->u_varnames = list2dict(u->u_ste->ste_varnames);
1091 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
1092 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
1093 PyDict_Size(u->u_cellvars));
1094
1095 u->u_blocks = NULL;
1096 u->u_tmpname = 0;
1097 u->u_nfblocks = 0;
1098 u->u_firstlineno = lineno;
1099 u->u_lineno = 0;
1100 u->u_lineno_set = false;
1101 u->u_consts = PyDict_New();
1102 if (!u->u_consts) {
1103 compiler_unit_free(u);
1104 return 0;
1105 }
1106 u->u_names = PyDict_New();
1107 if (!u->u_names) {
1108 compiler_unit_free(u);
1109 return 0;
1110 }
1111
1112 u->u_private = NULL;
1113
1114 /* Push the old compiler_unit on the stack. */
1115 if (c->u) {
1116 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
1117 if (PyList_Append(c->c_stack, wrapper) < 0) {
1118 compiler_unit_free(u);
1119 return 0;
1120 }
1121 Py_DECREF(wrapper);
1122 u->u_private = c->u->u_private;
1123 Py_XINCREF(u->u_private);
1124 }
1125 c->u = u;
1126
1127 c->c_nestlevel++;
1128 if (compiler_use_new_block(c) < 0)
1129 return 0;
1130
1131 return 1;
1132}
1133
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001134static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135compiler_exit_scope(struct compiler *c)
1136{
1137 int n;
1138 PyObject *wrapper;
1139
1140 c->c_nestlevel--;
1141 compiler_unit_free(c->u);
1142 /* Restore c->u to the parent unit. */
1143 n = PyList_GET_SIZE(c->c_stack) - 1;
1144 if (n >= 0) {
1145 wrapper = PyList_GET_ITEM(c->c_stack, n);
1146 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001147 /* we are deleting from a list so this really shouldn't fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148 if (PySequence_DelItem(c->c_stack, n) < 0)
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001149 Py_FatalError("compiler_exit_scope()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150 compiler_unit_check(c->u);
1151 }
1152 else
1153 c->u = NULL;
1154
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155}
1156
1157/* Allocate a new block and return a pointer to it.
1158 Returns NULL on error.
1159*/
1160
1161static basicblock *
1162compiler_new_block(struct compiler *c)
1163{
1164 basicblock *b;
1165 struct compiler_unit *u;
1166
1167 u = c->u;
1168 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
Neal Norwitz87b801c2005-12-18 04:42:47 +00001169 if (b == NULL) {
1170 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171 return NULL;
Neal Norwitz87b801c2005-12-18 04:42:47 +00001172 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173 memset((void *)b, 0, sizeof(basicblock));
1174 assert (b->b_next == NULL);
1175 b->b_list = u->u_blocks;
1176 u->u_blocks = b;
1177 return b;
1178}
1179
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180static basicblock *
1181compiler_use_new_block(struct compiler *c)
1182{
1183 basicblock *block = compiler_new_block(c);
1184 if (block == NULL)
1185 return NULL;
1186 c->u->u_curblock = block;
1187 return block;
1188}
1189
1190static basicblock *
1191compiler_next_block(struct compiler *c)
1192{
1193 basicblock *block = compiler_new_block(c);
1194 if (block == NULL)
1195 return NULL;
1196 c->u->u_curblock->b_next = block;
1197 c->u->u_curblock = block;
1198 return block;
1199}
1200
1201static basicblock *
1202compiler_use_next_block(struct compiler *c, basicblock *block)
1203{
1204 assert(block != NULL);
1205 c->u->u_curblock->b_next = block;
1206 c->u->u_curblock = block;
1207 return block;
1208}
1209
1210/* Returns the offset of the next instruction in the current block's
1211 b_instr array. Resizes the b_instr as necessary.
1212 Returns -1 on failure.
1213 */
1214
1215static int
1216compiler_next_instr(struct compiler *c, basicblock *b)
1217{
1218 assert(b != NULL);
1219 if (b->b_instr == NULL) {
1220 b->b_instr = PyObject_Malloc(sizeof(struct instr) *
1221 DEFAULT_BLOCK_SIZE);
1222 if (b->b_instr == NULL) {
1223 PyErr_NoMemory();
1224 return -1;
1225 }
1226 b->b_ialloc = DEFAULT_BLOCK_SIZE;
1227 memset((char *)b->b_instr, 0,
1228 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
1229 }
1230 else if (b->b_iused == b->b_ialloc) {
1231 size_t oldsize, newsize;
1232 oldsize = b->b_ialloc * sizeof(struct instr);
1233 newsize = oldsize << 1;
1234 if (newsize == 0) {
1235 PyErr_NoMemory();
1236 return -1;
1237 }
1238 b->b_ialloc <<= 1;
1239 b->b_instr = PyObject_Realloc((void *)b->b_instr, newsize);
1240 if (b->b_instr == NULL)
1241 return -1;
1242 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
1243 }
1244 return b->b_iused++;
1245}
1246
1247static void
1248compiler_set_lineno(struct compiler *c, int off)
1249{
1250 basicblock *b;
1251 if (c->u->u_lineno_set)
1252 return;
1253 c->u->u_lineno_set = true;
1254 b = c->u->u_curblock;
1255 b->b_instr[off].i_lineno = c->u->u_lineno;
1256}
1257
1258static int
1259opcode_stack_effect(int opcode, int oparg)
1260{
1261 switch (opcode) {
1262 case POP_TOP:
1263 return -1;
1264 case ROT_TWO:
1265 case ROT_THREE:
1266 return 0;
1267 case DUP_TOP:
1268 return 1;
1269 case ROT_FOUR:
1270 return 0;
1271
1272 case UNARY_POSITIVE:
1273 case UNARY_NEGATIVE:
1274 case UNARY_NOT:
1275 case UNARY_CONVERT:
1276 case UNARY_INVERT:
1277 return 0;
1278
1279 case BINARY_POWER:
1280 case BINARY_MULTIPLY:
1281 case BINARY_DIVIDE:
1282 case BINARY_MODULO:
1283 case BINARY_ADD:
1284 case BINARY_SUBTRACT:
1285 case BINARY_SUBSCR:
1286 case BINARY_FLOOR_DIVIDE:
1287 case BINARY_TRUE_DIVIDE:
1288 return -1;
1289 case INPLACE_FLOOR_DIVIDE:
1290 case INPLACE_TRUE_DIVIDE:
1291 return -1;
1292
1293 case SLICE+0:
1294 return 1;
1295 case SLICE+1:
1296 return 0;
1297 case SLICE+2:
1298 return 0;
1299 case SLICE+3:
1300 return -1;
1301
1302 case STORE_SLICE+0:
1303 return -2;
1304 case STORE_SLICE+1:
1305 return -3;
1306 case STORE_SLICE+2:
1307 return -3;
1308 case STORE_SLICE+3:
1309 return -4;
1310
1311 case DELETE_SLICE+0:
1312 return -1;
1313 case DELETE_SLICE+1:
1314 return -2;
1315 case DELETE_SLICE+2:
1316 return -2;
1317 case DELETE_SLICE+3:
1318 return -3;
1319
1320 case INPLACE_ADD:
1321 case INPLACE_SUBTRACT:
1322 case INPLACE_MULTIPLY:
1323 case INPLACE_DIVIDE:
1324 case INPLACE_MODULO:
1325 return -1;
1326 case STORE_SUBSCR:
1327 return -3;
1328 case DELETE_SUBSCR:
1329 return -2;
1330
1331 case BINARY_LSHIFT:
1332 case BINARY_RSHIFT:
1333 case BINARY_AND:
1334 case BINARY_XOR:
1335 case BINARY_OR:
1336 return -1;
1337 case INPLACE_POWER:
1338 return -1;
1339 case GET_ITER:
1340 return 0;
1341
1342 case PRINT_EXPR:
1343 return -1;
1344 case PRINT_ITEM:
1345 return -1;
1346 case PRINT_NEWLINE:
1347 return 0;
1348 case PRINT_ITEM_TO:
1349 return -2;
1350 case PRINT_NEWLINE_TO:
1351 return -1;
1352 case INPLACE_LSHIFT:
1353 case INPLACE_RSHIFT:
1354 case INPLACE_AND:
1355 case INPLACE_XOR:
1356 case INPLACE_OR:
1357 return -1;
1358 case BREAK_LOOP:
1359 return 0;
1360
1361 case LOAD_LOCALS:
1362 return 1;
1363 case RETURN_VALUE:
1364 return -1;
1365 case IMPORT_STAR:
1366 return -1;
1367 case EXEC_STMT:
1368 return -3;
1369 case YIELD_VALUE:
1370 return 0;
1371
1372 case POP_BLOCK:
1373 return 0;
1374 case END_FINALLY:
1375 return -1; /* or -2 or -3 if exception occurred */
1376 case BUILD_CLASS:
1377 return -2;
1378
1379 case STORE_NAME:
1380 return -1;
1381 case DELETE_NAME:
1382 return 0;
1383 case UNPACK_SEQUENCE:
1384 return oparg-1;
1385 case FOR_ITER:
1386 return 1;
1387
1388 case STORE_ATTR:
1389 return -2;
1390 case DELETE_ATTR:
1391 return -1;
1392 case STORE_GLOBAL:
1393 return -1;
1394 case DELETE_GLOBAL:
1395 return 0;
1396 case DUP_TOPX:
1397 return oparg;
1398 case LOAD_CONST:
1399 return 1;
1400 case LOAD_NAME:
1401 return 1;
1402 case BUILD_TUPLE:
1403 case BUILD_LIST:
1404 return 1-oparg;
1405 case BUILD_MAP:
1406 return 1;
1407 case LOAD_ATTR:
1408 return 0;
1409 case COMPARE_OP:
1410 return -1;
1411 case IMPORT_NAME:
1412 return 0;
1413 case IMPORT_FROM:
1414 return 1;
1415
1416 case JUMP_FORWARD:
1417 case JUMP_IF_FALSE:
1418 case JUMP_IF_TRUE:
1419 case JUMP_ABSOLUTE:
1420 return 0;
1421
1422 case LOAD_GLOBAL:
1423 return 1;
1424
1425 case CONTINUE_LOOP:
1426 return 0;
1427 case SETUP_LOOP:
1428 return 0;
1429 case SETUP_EXCEPT:
1430 case SETUP_FINALLY:
1431 return 3; /* actually pushed by an exception */
1432
1433 case LOAD_FAST:
1434 return 1;
1435 case STORE_FAST:
1436 return -1;
1437 case DELETE_FAST:
1438 return 0;
1439
1440 case RAISE_VARARGS:
1441 return -oparg;
1442#define NARGS(o) (((o) % 256) + 2*((o) / 256))
1443 case CALL_FUNCTION:
1444 return -NARGS(oparg);
1445 case CALL_FUNCTION_VAR:
1446 case CALL_FUNCTION_KW:
1447 return -NARGS(oparg)-1;
1448 case CALL_FUNCTION_VAR_KW:
1449 return -NARGS(oparg)-2;
1450#undef NARGS
1451 case MAKE_FUNCTION:
1452 return -oparg;
1453 case BUILD_SLICE:
1454 if (oparg == 3)
1455 return -2;
1456 else
1457 return -1;
1458
1459 case MAKE_CLOSURE:
1460 return -oparg;
1461 case LOAD_CLOSURE:
1462 return 1;
1463 case LOAD_DEREF:
1464 return 1;
1465 case STORE_DEREF:
1466 return -1;
1467 default:
1468 fprintf(stderr, "opcode = %d\n", opcode);
1469 Py_FatalError("opcode_stack_effect()");
1470
1471 }
1472 return 0; /* not reachable */
1473}
1474
1475/* Add an opcode with no argument.
1476 Returns 0 on failure, 1 on success.
1477*/
1478
1479static int
1480compiler_addop(struct compiler *c, int opcode)
1481{
1482 basicblock *b;
1483 struct instr *i;
1484 int off;
1485 off = compiler_next_instr(c, c->u->u_curblock);
1486 if (off < 0)
1487 return 0;
1488 b = c->u->u_curblock;
1489 i = &b->b_instr[off];
1490 i->i_opcode = opcode;
1491 i->i_hasarg = 0;
1492 if (opcode == RETURN_VALUE)
1493 b->b_return = 1;
1494 compiler_set_lineno(c, off);
1495 return 1;
1496}
1497
1498static int
1499compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
1500{
1501 PyObject *t, *v;
1502 int arg;
1503
1504 /* necessary to make sure types aren't coerced (e.g., int and long) */
Neil Schemenauer3a44aaa2005-10-23 17:21:54 +00001505 t = PyTuple_Pack(2, o, o->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506 if (t == NULL)
1507 return -1;
1508
1509 v = PyDict_GetItem(dict, t);
1510 if (!v) {
1511 arg = PyDict_Size(dict);
1512 v = PyInt_FromLong(arg);
1513 if (!v) {
1514 Py_DECREF(t);
1515 return -1;
1516 }
1517 if (PyDict_SetItem(dict, t, v) < 0) {
1518 Py_DECREF(t);
1519 Py_DECREF(v);
1520 return -1;
1521 }
1522 Py_DECREF(v);
1523 }
1524 else
1525 arg = PyInt_AsLong(v);
1526 Py_DECREF(t);
1527 return arg;
1528}
1529
1530static int
1531compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
1532 PyObject *o)
1533{
1534 int arg = compiler_add_o(c, dict, o);
1535 if (arg < 0)
1536 return 0;
1537 return compiler_addop_i(c, opcode, arg);
1538}
1539
1540static int
1541compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
1542 PyObject *o)
1543{
1544 int arg;
1545 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1546 if (!mangled)
1547 return 0;
1548 arg = compiler_add_o(c, dict, mangled);
1549 Py_DECREF(mangled);
1550 if (arg < 0)
1551 return 0;
1552 return compiler_addop_i(c, opcode, arg);
1553}
1554
1555/* Add an opcode with an integer argument.
1556 Returns 0 on failure, 1 on success.
1557*/
1558
1559static int
1560compiler_addop_i(struct compiler *c, int opcode, int oparg)
1561{
1562 struct instr *i;
1563 int off;
1564 off = compiler_next_instr(c, c->u->u_curblock);
1565 if (off < 0)
1566 return 0;
1567 i = &c->u->u_curblock->b_instr[off];
1568 i->i_opcode = opcode;
1569 i->i_oparg = oparg;
1570 i->i_hasarg = 1;
1571 compiler_set_lineno(c, off);
1572 return 1;
1573}
1574
1575static int
1576compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1577{
1578 struct instr *i;
1579 int off;
1580
1581 assert(b != NULL);
1582 off = compiler_next_instr(c, c->u->u_curblock);
1583 if (off < 0)
1584 return 0;
1585 compiler_set_lineno(c, off);
1586 i = &c->u->u_curblock->b_instr[off];
1587 i->i_opcode = opcode;
1588 i->i_target = b;
1589 i->i_hasarg = 1;
1590 if (absolute)
1591 i->i_jabs = 1;
1592 else
1593 i->i_jrel = 1;
1594 return 1;
1595}
1596
1597/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1598 like to find better names.) NEW_BLOCK() creates a new block and sets
1599 it as the current block. NEXT_BLOCK() also creates an implicit jump
1600 from the current block to the new block.
1601*/
1602
1603/* XXX The returns inside these macros make it impossible to decref
1604 objects created in the local function.
1605*/
1606
1607
1608#define NEW_BLOCK(C) { \
1609 if (compiler_use_new_block((C)) == NULL) \
1610 return 0; \
1611}
1612
1613#define NEXT_BLOCK(C) { \
1614 if (compiler_next_block((C)) == NULL) \
1615 return 0; \
1616}
1617
1618#define ADDOP(C, OP) { \
1619 if (!compiler_addop((C), (OP))) \
1620 return 0; \
1621}
1622
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001623#define ADDOP_IN_SCOPE(C, OP) { \
1624 if (!compiler_addop((C), (OP))) { \
1625 compiler_exit_scope(c); \
1626 return 0; \
1627 } \
1628}
1629
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001630#define ADDOP_O(C, OP, O, TYPE) { \
1631 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1632 return 0; \
1633}
1634
1635#define ADDOP_NAME(C, OP, O, TYPE) { \
1636 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1637 return 0; \
1638}
1639
1640#define ADDOP_I(C, OP, O) { \
1641 if (!compiler_addop_i((C), (OP), (O))) \
1642 return 0; \
1643}
1644
1645#define ADDOP_JABS(C, OP, O) { \
1646 if (!compiler_addop_j((C), (OP), (O), 1)) \
1647 return 0; \
1648}
1649
1650#define ADDOP_JREL(C, OP, O) { \
1651 if (!compiler_addop_j((C), (OP), (O), 0)) \
1652 return 0; \
1653}
1654
1655/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1656 the ASDL name to synthesize the name of the C type and the visit function.
1657*/
1658
1659#define VISIT(C, TYPE, V) {\
1660 if (!compiler_visit_ ## TYPE((C), (V))) \
1661 return 0; \
1662}
1663
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001664#define VISIT_IN_SCOPE(C, TYPE, V) {\
1665 if (!compiler_visit_ ## TYPE((C), (V))) { \
1666 compiler_exit_scope(c); \
1667 return 0; \
1668 } \
1669}
1670
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001671#define VISIT_SLICE(C, V, CTX) {\
1672 if (!compiler_visit_slice((C), (V), (CTX))) \
1673 return 0; \
1674}
1675
1676#define VISIT_SEQ(C, TYPE, SEQ) { \
1677 int i; \
1678 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1679 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1680 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
1681 if (!compiler_visit_ ## TYPE((C), elt)) \
1682 return 0; \
1683 } \
1684}
1685
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001686#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
1687 int i; \
1688 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1689 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1690 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
1691 if (!compiler_visit_ ## TYPE((C), elt)) { \
1692 compiler_exit_scope(c); \
1693 return 0; \
1694 } \
1695 } \
1696}
1697
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698static int
1699compiler_isdocstring(stmt_ty s)
1700{
1701 if (s->kind != Expr_kind)
1702 return 0;
1703 return s->v.Expr.value->kind == Str_kind;
1704}
1705
1706/* Compile a sequence of statements, checking for a docstring. */
1707
1708static int
1709compiler_body(struct compiler *c, asdl_seq *stmts)
1710{
1711 int i = 0;
1712 stmt_ty st;
1713
1714 if (!asdl_seq_LEN(stmts))
1715 return 1;
1716 st = asdl_seq_GET(stmts, 0);
1717 if (compiler_isdocstring(st)) {
1718 i = 1;
1719 VISIT(c, expr, st->v.Expr.value);
1720 if (!compiler_nameop(c, __doc__, Store))
1721 return 0;
1722 }
1723 for (; i < asdl_seq_LEN(stmts); i++)
1724 VISIT(c, stmt, asdl_seq_GET(stmts, i));
1725 return 1;
1726}
1727
1728static PyCodeObject *
1729compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001730{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001731 PyCodeObject *co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001732 int addNone = 1;
1733 static PyObject *module;
1734 if (!module) {
1735 module = PyString_FromString("<module>");
1736 if (!module)
1737 return NULL;
1738 }
1739 if (!compiler_enter_scope(c, module, mod, 1))
Guido van Rossumd076c731998-10-07 19:42:25 +00001740 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001741 switch (mod->kind) {
1742 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001743 if (!compiler_body(c, mod->v.Module.body)) {
1744 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001746 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001747 break;
1748 case Interactive_kind:
1749 c->c_interactive = 1;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001750 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001751 break;
1752 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001753 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001754 addNone = 0;
1755 break;
1756 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001757 PyErr_SetString(PyExc_SystemError,
1758 "suite should not be possible");
1759 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001760 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001761 PyErr_Format(PyExc_SystemError,
1762 "module kind %d should not be possible",
1763 mod->kind);
1764 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001765 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001766 co = assemble(c, addNone);
1767 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001768 return co;
1769}
1770
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771/* The test for LOCAL must come before the test for FREE in order to
1772 handle classes where name is both local and free. The local var is
1773 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001774*/
1775
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001776static int
1777get_ref_type(struct compiler *c, PyObject *name)
1778{
1779 int scope = PyST_GetScope(c->u->u_ste, name);
1780 if (scope == 0) {
1781 char buf[350];
1782 PyOS_snprintf(buf, sizeof(buf),
1783 "unknown scope for %.100s in %.100s(%s) in %s\n"
1784 "symbols: %s\nlocals: %s\nglobals: %s\n",
1785 PyString_AS_STRING(name),
1786 PyString_AS_STRING(c->u->u_name),
1787 PyObject_REPR(c->u->u_ste->ste_id),
1788 c->c_filename,
1789 PyObject_REPR(c->u->u_ste->ste_symbols),
1790 PyObject_REPR(c->u->u_varnames),
1791 PyObject_REPR(c->u->u_names)
1792 );
1793 Py_FatalError(buf);
1794 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001795
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001796 return scope;
1797}
1798
1799static int
1800compiler_lookup_arg(PyObject *dict, PyObject *name)
1801{
1802 PyObject *k, *v;
1803 k = Py_BuildValue("(OO)", name, name->ob_type);
1804 if (k == NULL)
1805 return -1;
1806 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001807 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001808 if (v == NULL)
1809 return -1;
1810 return PyInt_AS_LONG(v);
1811}
1812
1813static int
1814compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1815{
1816 int i, free = PyCode_GetNumFree(co);
1817 if (free == 0) {
1818 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1819 ADDOP_I(c, MAKE_FUNCTION, args);
1820 return 1;
1821 }
1822 for (i = 0; i < free; ++i) {
1823 /* Bypass com_addop_varname because it will generate
1824 LOAD_DEREF but LOAD_CLOSURE is needed.
1825 */
1826 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1827 int arg, reftype;
1828
1829 /* Special case: If a class contains a method with a
1830 free variable that has the same name as a method,
1831 the name will be considered free *and* local in the
1832 class. It should be handled by the closure, as
1833 well as by the normal name loookup logic.
1834 */
1835 reftype = get_ref_type(c, name);
1836 if (reftype == CELL)
1837 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1838 else /* (reftype == FREE) */
1839 arg = compiler_lookup_arg(c->u->u_freevars, name);
1840 if (arg == -1) {
1841 printf("lookup %s in %s %d %d\n"
1842 "freevars of %s: %s\n",
1843 PyObject_REPR(name),
1844 PyString_AS_STRING(c->u->u_name),
1845 reftype, arg,
1846 PyString_AS_STRING(co->co_name),
1847 PyObject_REPR(co->co_freevars));
1848 Py_FatalError("compiler_make_closure()");
1849 }
1850 ADDOP_I(c, LOAD_CLOSURE, arg);
1851 }
1852 ADDOP_I(c, BUILD_TUPLE, free);
1853 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1854 ADDOP_I(c, MAKE_CLOSURE, args);
1855 return 1;
1856}
1857
1858static int
1859compiler_decorators(struct compiler *c, asdl_seq* decos)
1860{
1861 int i;
1862
1863 if (!decos)
1864 return 1;
1865
1866 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1867 VISIT(c, expr, asdl_seq_GET(decos, i));
1868 }
1869 return 1;
1870}
1871
1872static int
1873compiler_arguments(struct compiler *c, arguments_ty args)
1874{
1875 int i;
1876 int n = asdl_seq_LEN(args->args);
1877 /* Correctly handle nested argument lists */
1878 for (i = 0; i < n; i++) {
1879 expr_ty arg = asdl_seq_GET(args->args, i);
1880 if (arg->kind == Tuple_kind) {
1881 PyObject *id = PyString_FromFormat(".%d", i);
1882 if (id == NULL) {
1883 return 0;
1884 }
1885 if (!compiler_nameop(c, id, Load)) {
1886 Py_DECREF(id);
1887 return 0;
1888 }
1889 Py_DECREF(id);
1890 VISIT(c, expr, arg);
1891 }
1892 }
1893 return 1;
1894}
1895
1896static int
1897compiler_function(struct compiler *c, stmt_ty s)
1898{
1899 PyCodeObject *co;
1900 PyObject *first_const = Py_None;
1901 arguments_ty args = s->v.FunctionDef.args;
1902 asdl_seq* decos = s->v.FunctionDef.decorators;
1903 stmt_ty st;
1904 int i, n, docstring;
1905
1906 assert(s->kind == FunctionDef_kind);
1907
1908 if (!compiler_decorators(c, decos))
1909 return 0;
1910 if (args->defaults)
1911 VISIT_SEQ(c, expr, args->defaults);
1912 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1913 s->lineno))
1914 return 0;
1915
1916 st = asdl_seq_GET(s->v.FunctionDef.body, 0);
1917 docstring = compiler_isdocstring(st);
1918 if (docstring)
1919 first_const = st->v.Expr.value->v.Str.s;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001920 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1921 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001922 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001923 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001924
1925 /* unpack nested arguments */
1926 compiler_arguments(c, args);
1927
1928 c->u->u_argcount = asdl_seq_LEN(args->args);
1929 n = asdl_seq_LEN(s->v.FunctionDef.body);
1930 /* if there was a docstring, we need to skip the first statement */
1931 for (i = docstring; i < n; i++) {
1932 stmt_ty s2 = asdl_seq_GET(s->v.FunctionDef.body, i);
1933 if (i == 0 && s2->kind == Expr_kind &&
1934 s2->v.Expr.value->kind == Str_kind)
1935 continue;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001936 VISIT_IN_SCOPE(c, stmt, s2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001937 }
1938 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001939 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001940 if (co == NULL)
1941 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942
1943 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00001944 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001945
1946 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1947 ADDOP_I(c, CALL_FUNCTION, 1);
1948 }
1949
1950 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1951}
1952
1953static int
1954compiler_class(struct compiler *c, stmt_ty s)
1955{
1956 int n;
1957 PyCodeObject *co;
1958 PyObject *str;
1959 /* push class name on stack, needed by BUILD_CLASS */
1960 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1961 /* push the tuple of base classes on the stack */
1962 n = asdl_seq_LEN(s->v.ClassDef.bases);
1963 if (n > 0)
1964 VISIT_SEQ(c, expr, s->v.ClassDef.bases);
1965 ADDOP_I(c, BUILD_TUPLE, n);
1966 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s,
1967 s->lineno))
1968 return 0;
1969 c->u->u_private = s->v.ClassDef.name;
1970 Py_INCREF(c->u->u_private);
1971 str = PyString_InternFromString("__name__");
1972 if (!str || !compiler_nameop(c, str, Load)) {
1973 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001974 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001975 return 0;
1976 }
1977
1978 Py_DECREF(str);
1979 str = PyString_InternFromString("__module__");
1980 if (!str || !compiler_nameop(c, str, Store)) {
1981 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001982 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001983 return 0;
1984 }
1985 Py_DECREF(str);
1986
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001987 if (!compiler_body(c, s->v.ClassDef.body)) {
1988 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001989 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001990 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001991
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001992 ADDOP_IN_SCOPE(c, LOAD_LOCALS);
1993 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001994 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001995 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001996 if (co == NULL)
1997 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001998
1999 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00002000 Py_DECREF(co);
2001
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002002 ADDOP_I(c, CALL_FUNCTION, 0);
2003 ADDOP(c, BUILD_CLASS);
2004 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2005 return 0;
2006 return 1;
2007}
2008
2009static int
2010compiler_lambda(struct compiler *c, expr_ty e)
2011{
2012 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002013 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002014 arguments_ty args = e->v.Lambda.args;
2015 assert(e->kind == Lambda_kind);
2016
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002017 if (!name) {
2018 name = PyString_InternFromString("<lambda>");
2019 if (!name)
2020 return 0;
2021 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002022
2023 if (args->defaults)
2024 VISIT_SEQ(c, expr, args->defaults);
2025 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2026 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002027
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002028 /* unpack nested arguments */
2029 compiler_arguments(c, args);
2030
2031 c->u->u_argcount = asdl_seq_LEN(args->args);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00002032 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2033 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002034 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00002035 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002036 if (co == NULL)
2037 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002038
2039 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00002040 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002041
2042 return 1;
2043}
2044
2045static int
2046compiler_print(struct compiler *c, stmt_ty s)
2047{
2048 int i, n;
2049 bool dest;
2050
2051 assert(s->kind == Print_kind);
2052 n = asdl_seq_LEN(s->v.Print.values);
2053 dest = false;
2054 if (s->v.Print.dest) {
2055 VISIT(c, expr, s->v.Print.dest);
2056 dest = true;
2057 }
2058 for (i = 0; i < n; i++) {
2059 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i);
2060 if (dest) {
2061 ADDOP(c, DUP_TOP);
2062 VISIT(c, expr, e);
2063 ADDOP(c, ROT_TWO);
2064 ADDOP(c, PRINT_ITEM_TO);
2065 }
2066 else {
2067 VISIT(c, expr, e);
2068 ADDOP(c, PRINT_ITEM);
2069 }
2070 }
2071 if (s->v.Print.nl) {
2072 if (dest)
2073 ADDOP(c, PRINT_NEWLINE_TO)
2074 else
2075 ADDOP(c, PRINT_NEWLINE)
2076 }
2077 else if (dest)
2078 ADDOP(c, POP_TOP);
2079 return 1;
2080}
2081
2082static int
2083compiler_if(struct compiler *c, stmt_ty s)
2084{
2085 basicblock *end, *next;
2086
2087 assert(s->kind == If_kind);
2088 end = compiler_new_block(c);
2089 if (end == NULL)
2090 return 0;
2091 next = compiler_new_block(c);
2092 if (next == NULL)
2093 return 0;
2094 VISIT(c, expr, s->v.If.test);
2095 ADDOP_JREL(c, JUMP_IF_FALSE, next);
2096 ADDOP(c, POP_TOP);
2097 VISIT_SEQ(c, stmt, s->v.If.body);
2098 ADDOP_JREL(c, JUMP_FORWARD, end);
2099 compiler_use_next_block(c, next);
2100 ADDOP(c, POP_TOP);
2101 if (s->v.If.orelse)
2102 VISIT_SEQ(c, stmt, s->v.If.orelse);
2103 compiler_use_next_block(c, end);
2104 return 1;
2105}
2106
2107static int
2108compiler_for(struct compiler *c, stmt_ty s)
2109{
2110 basicblock *start, *cleanup, *end;
2111
2112 start = compiler_new_block(c);
2113 cleanup = compiler_new_block(c);
2114 end = compiler_new_block(c);
2115 if (start == NULL || end == NULL || cleanup == NULL)
2116 return 0;
2117 ADDOP_JREL(c, SETUP_LOOP, end);
2118 if (!compiler_push_fblock(c, LOOP, start))
2119 return 0;
2120 VISIT(c, expr, s->v.For.iter);
2121 ADDOP(c, GET_ITER);
2122 compiler_use_next_block(c, start);
2123 ADDOP_JREL(c, FOR_ITER, cleanup);
2124 VISIT(c, expr, s->v.For.target);
2125 VISIT_SEQ(c, stmt, s->v.For.body);
2126 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2127 compiler_use_next_block(c, cleanup);
2128 ADDOP(c, POP_BLOCK);
2129 compiler_pop_fblock(c, LOOP, start);
2130 VISIT_SEQ(c, stmt, s->v.For.orelse);
2131 compiler_use_next_block(c, end);
2132 return 1;
2133}
2134
2135static int
2136compiler_while(struct compiler *c, stmt_ty s)
2137{
2138 basicblock *loop, *orelse, *end, *anchor = NULL;
2139 int constant = expr_constant(s->v.While.test);
2140
2141 if (constant == 0)
2142 return 1;
2143 loop = compiler_new_block(c);
2144 end = compiler_new_block(c);
2145 if (constant == -1) {
2146 anchor = compiler_new_block(c);
2147 if (anchor == NULL)
2148 return 0;
2149 }
2150 if (loop == NULL || end == NULL)
2151 return 0;
2152 if (s->v.While.orelse) {
2153 orelse = compiler_new_block(c);
2154 if (orelse == NULL)
2155 return 0;
2156 }
2157 else
2158 orelse = NULL;
2159
2160 ADDOP_JREL(c, SETUP_LOOP, end);
2161 compiler_use_next_block(c, loop);
2162 if (!compiler_push_fblock(c, LOOP, loop))
2163 return 0;
2164 if (constant == -1) {
2165 VISIT(c, expr, s->v.While.test);
2166 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
2167 ADDOP(c, POP_TOP);
2168 }
2169 VISIT_SEQ(c, stmt, s->v.While.body);
2170 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
2171
2172 /* XXX should the two POP instructions be in a separate block
2173 if there is no else clause ?
2174 */
2175
2176 if (constant == -1) {
2177 compiler_use_next_block(c, anchor);
2178 ADDOP(c, POP_TOP);
2179 ADDOP(c, POP_BLOCK);
2180 }
2181 compiler_pop_fblock(c, LOOP, loop);
2182 if (orelse != NULL)
2183 VISIT_SEQ(c, stmt, s->v.While.orelse);
2184 compiler_use_next_block(c, end);
2185
2186 return 1;
2187}
2188
2189static int
2190compiler_continue(struct compiler *c)
2191{
2192 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
2193 int i;
2194
2195 if (!c->u->u_nfblocks)
2196 return compiler_error(c, LOOP_ERROR_MSG);
2197 i = c->u->u_nfblocks - 1;
2198 switch (c->u->u_fblock[i].fb_type) {
2199 case LOOP:
2200 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
2201 break;
2202 case EXCEPT:
2203 case FINALLY_TRY:
2204 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP)
2205 ;
2206 if (i == -1)
2207 return compiler_error(c, LOOP_ERROR_MSG);
2208 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
2209 break;
2210 case FINALLY_END:
2211 return compiler_error(c,
2212 "'continue' not supported inside 'finally' clause");
2213 }
2214
2215 return 1;
2216}
2217
2218/* Code generated for "try: <body> finally: <finalbody>" is as follows:
2219
2220 SETUP_FINALLY L
2221 <code for body>
2222 POP_BLOCK
2223 LOAD_CONST <None>
2224 L: <code for finalbody>
2225 END_FINALLY
2226
2227 The special instructions use the block stack. Each block
2228 stack entry contains the instruction that created it (here
2229 SETUP_FINALLY), the level of the value stack at the time the
2230 block stack entry was created, and a label (here L).
2231
2232 SETUP_FINALLY:
2233 Pushes the current value stack level and the label
2234 onto the block stack.
2235 POP_BLOCK:
2236 Pops en entry from the block stack, and pops the value
2237 stack until its level is the same as indicated on the
2238 block stack. (The label is ignored.)
2239 END_FINALLY:
2240 Pops a variable number of entries from the *value* stack
2241 and re-raises the exception they specify. The number of
2242 entries popped depends on the (pseudo) exception type.
2243
2244 The block stack is unwound when an exception is raised:
2245 when a SETUP_FINALLY entry is found, the exception is pushed
2246 onto the value stack (and the exception condition is cleared),
2247 and the interpreter jumps to the label gotten from the block
2248 stack.
2249*/
2250
2251static int
2252compiler_try_finally(struct compiler *c, stmt_ty s)
2253{
2254 basicblock *body, *end;
2255 body = compiler_new_block(c);
2256 end = compiler_new_block(c);
2257 if (body == NULL || end == NULL)
2258 return 0;
2259
2260 ADDOP_JREL(c, SETUP_FINALLY, end);
2261 compiler_use_next_block(c, body);
2262 if (!compiler_push_fblock(c, FINALLY_TRY, body))
2263 return 0;
2264 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
2265 ADDOP(c, POP_BLOCK);
2266 compiler_pop_fblock(c, FINALLY_TRY, body);
2267
2268 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2269 compiler_use_next_block(c, end);
2270 if (!compiler_push_fblock(c, FINALLY_END, end))
2271 return 0;
2272 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
2273 ADDOP(c, END_FINALLY);
2274 compiler_pop_fblock(c, FINALLY_END, end);
2275
2276 return 1;
2277}
2278
2279/*
2280 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
2281 (The contents of the value stack is shown in [], with the top
2282 at the right; 'tb' is trace-back info, 'val' the exception's
2283 associated value, and 'exc' the exception.)
2284
2285 Value stack Label Instruction Argument
2286 [] SETUP_EXCEPT L1
2287 [] <code for S>
2288 [] POP_BLOCK
2289 [] JUMP_FORWARD L0
2290
2291 [tb, val, exc] L1: DUP )
2292 [tb, val, exc, exc] <evaluate E1> )
2293 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2294 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
2295 [tb, val, exc, 1] POP )
2296 [tb, val, exc] POP
2297 [tb, val] <assign to V1> (or POP if no V1)
2298 [tb] POP
2299 [] <code for S1>
2300 JUMP_FORWARD L0
2301
2302 [tb, val, exc, 0] L2: POP
2303 [tb, val, exc] DUP
2304 .............................etc.......................
2305
2306 [tb, val, exc, 0] Ln+1: POP
2307 [tb, val, exc] END_FINALLY # re-raise exception
2308
2309 [] L0: <next statement>
2310
2311 Of course, parts are not generated if Vi or Ei is not present.
2312*/
2313static int
2314compiler_try_except(struct compiler *c, stmt_ty s)
2315{
2316 basicblock *body, *orelse, *except, *end;
2317 int i, n;
2318
2319 body = compiler_new_block(c);
2320 except = compiler_new_block(c);
2321 orelse = compiler_new_block(c);
2322 end = compiler_new_block(c);
2323 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
2324 return 0;
2325 ADDOP_JREL(c, SETUP_EXCEPT, except);
2326 compiler_use_next_block(c, body);
2327 if (!compiler_push_fblock(c, EXCEPT, body))
2328 return 0;
2329 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
2330 ADDOP(c, POP_BLOCK);
2331 compiler_pop_fblock(c, EXCEPT, body);
2332 ADDOP_JREL(c, JUMP_FORWARD, orelse);
2333 n = asdl_seq_LEN(s->v.TryExcept.handlers);
2334 compiler_use_next_block(c, except);
2335 for (i = 0; i < n; i++) {
2336 excepthandler_ty handler = asdl_seq_GET(
2337 s->v.TryExcept.handlers, i);
2338 if (!handler->type && i < n-1)
2339 return compiler_error(c, "default 'except:' must be last");
2340 except = compiler_new_block(c);
2341 if (except == NULL)
2342 return 0;
2343 if (handler->type) {
2344 ADDOP(c, DUP_TOP);
2345 VISIT(c, expr, handler->type);
2346 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2347 ADDOP_JREL(c, JUMP_IF_FALSE, except);
2348 ADDOP(c, POP_TOP);
2349 }
2350 ADDOP(c, POP_TOP);
2351 if (handler->name) {
2352 VISIT(c, expr, handler->name);
2353 }
2354 else {
2355 ADDOP(c, POP_TOP);
2356 }
2357 ADDOP(c, POP_TOP);
2358 VISIT_SEQ(c, stmt, handler->body);
2359 ADDOP_JREL(c, JUMP_FORWARD, end);
2360 compiler_use_next_block(c, except);
2361 if (handler->type)
2362 ADDOP(c, POP_TOP);
2363 }
2364 ADDOP(c, END_FINALLY);
2365 compiler_use_next_block(c, orelse);
2366 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2367 compiler_use_next_block(c, end);
2368 return 1;
2369}
2370
2371static int
2372compiler_import_as(struct compiler *c, identifier name, identifier asname)
2373{
2374 /* The IMPORT_NAME opcode was already generated. This function
2375 merely needs to bind the result to a name.
2376
2377 If there is a dot in name, we need to split it and emit a
2378 LOAD_ATTR for each name.
2379 */
2380 const char *src = PyString_AS_STRING(name);
2381 const char *dot = strchr(src, '.');
2382 if (dot) {
2383 /* Consume the base module name to get the first attribute */
2384 src = dot + 1;
2385 while (dot) {
2386 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00002387 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002388 dot = strchr(src, '.');
Armin Rigo31441302005-10-21 12:57:31 +00002389 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002390 dot ? dot - src : strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002391 if (!attr)
2392 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002393 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002394 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002395 src = dot + 1;
2396 }
2397 }
2398 return compiler_nameop(c, asname, Store);
2399}
2400
2401static int
2402compiler_import(struct compiler *c, stmt_ty s)
2403{
2404 /* The Import node stores a module name like a.b.c as a single
2405 string. This is convenient for all cases except
2406 import a.b.c as d
2407 where we need to parse that string to extract the individual
2408 module names.
2409 XXX Perhaps change the representation to make this case simpler?
2410 */
2411 int i, n = asdl_seq_LEN(s->v.Import.names);
2412 for (i = 0; i < n; i++) {
2413 alias_ty alias = asdl_seq_GET(s->v.Import.names, i);
2414 int r;
2415
2416 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2417 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2418
2419 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002420 r = compiler_import_as(c, alias->name, alias->asname);
2421 if (!r)
2422 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002423 }
2424 else {
2425 identifier tmp = alias->name;
2426 const char *base = PyString_AS_STRING(alias->name);
2427 char *dot = strchr(base, '.');
2428 if (dot)
2429 tmp = PyString_FromStringAndSize(base,
2430 dot - base);
2431 r = compiler_nameop(c, tmp, Store);
2432 if (dot) {
2433 Py_DECREF(tmp);
2434 }
2435 if (!r)
2436 return r;
2437 }
2438 }
2439 return 1;
2440}
2441
2442static int
2443compiler_from_import(struct compiler *c, stmt_ty s)
2444{
2445 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002446
2447 PyObject *names = PyTuple_New(n);
2448 if (!names)
2449 return 0;
2450
2451 /* build up the names */
2452 for (i = 0; i < n; i++) {
2453 alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
2454 Py_INCREF(alias->name);
2455 PyTuple_SET_ITEM(names, i, alias->name);
2456 }
2457
2458 if (s->lineno > c->c_future->ff_lineno) {
2459 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
2460 "__future__")) {
2461 Py_DECREF(names);
2462 return compiler_error(c,
2463 "from __future__ imports must occur "
2464 "at the beginning of the file");
2465
2466 }
2467 }
2468
2469 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002470 Py_DECREF(names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002471 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2472 for (i = 0; i < n; i++) {
2473 alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
2474 identifier store_name;
2475
2476 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
2477 assert(n == 1);
2478 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002479 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002480 }
2481
2482 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2483 store_name = alias->name;
2484 if (alias->asname)
2485 store_name = alias->asname;
2486
2487 if (!compiler_nameop(c, store_name, Store)) {
2488 Py_DECREF(names);
2489 return 0;
2490 }
2491 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002492 /* remove imported module */
2493 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002494 return 1;
2495}
2496
2497static int
2498compiler_assert(struct compiler *c, stmt_ty s)
2499{
2500 static PyObject *assertion_error = NULL;
2501 basicblock *end;
2502
2503 if (Py_OptimizeFlag)
2504 return 1;
2505 if (assertion_error == NULL) {
2506 assertion_error = PyString_FromString("AssertionError");
2507 if (assertion_error == NULL)
2508 return 0;
2509 }
2510 VISIT(c, expr, s->v.Assert.test);
2511 end = compiler_new_block(c);
2512 if (end == NULL)
2513 return 0;
2514 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2515 ADDOP(c, POP_TOP);
2516 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2517 if (s->v.Assert.msg) {
2518 VISIT(c, expr, s->v.Assert.msg);
2519 ADDOP_I(c, RAISE_VARARGS, 2);
2520 }
2521 else {
2522 ADDOP_I(c, RAISE_VARARGS, 1);
2523 }
Neal Norwitz51abbc72005-12-18 07:06:23 +00002524 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002525 ADDOP(c, POP_TOP);
2526 return 1;
2527}
2528
2529static int
2530compiler_visit_stmt(struct compiler *c, stmt_ty s)
2531{
2532 int i, n;
2533
2534 c->u->u_lineno = s->lineno;
2535 c->u->u_lineno_set = false;
2536 switch (s->kind) {
2537 case FunctionDef_kind:
2538 return compiler_function(c, s);
2539 case ClassDef_kind:
2540 return compiler_class(c, s);
2541 case Return_kind:
2542 if (c->u->u_ste->ste_type != FunctionBlock)
2543 return compiler_error(c, "'return' outside function");
2544 if (s->v.Return.value) {
2545 if (c->u->u_ste->ste_generator) {
2546 return compiler_error(c,
2547 "'return' with argument inside generator");
2548 }
2549 VISIT(c, expr, s->v.Return.value);
2550 }
2551 else
2552 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2553 ADDOP(c, RETURN_VALUE);
2554 break;
2555 case Delete_kind:
2556 VISIT_SEQ(c, expr, s->v.Delete.targets)
2557 break;
2558 case Assign_kind:
2559 n = asdl_seq_LEN(s->v.Assign.targets);
2560 VISIT(c, expr, s->v.Assign.value);
2561 for (i = 0; i < n; i++) {
2562 if (i < n - 1)
2563 ADDOP(c, DUP_TOP);
2564 VISIT(c, expr,
2565 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2566 }
2567 break;
2568 case AugAssign_kind:
2569 return compiler_augassign(c, s);
2570 case Print_kind:
2571 return compiler_print(c, s);
2572 case For_kind:
2573 return compiler_for(c, s);
2574 case While_kind:
2575 return compiler_while(c, s);
2576 case If_kind:
2577 return compiler_if(c, s);
2578 case Raise_kind:
2579 n = 0;
2580 if (s->v.Raise.type) {
2581 VISIT(c, expr, s->v.Raise.type);
2582 n++;
2583 if (s->v.Raise.inst) {
2584 VISIT(c, expr, s->v.Raise.inst);
2585 n++;
2586 if (s->v.Raise.tback) {
2587 VISIT(c, expr, s->v.Raise.tback);
2588 n++;
2589 }
2590 }
2591 }
2592 ADDOP_I(c, RAISE_VARARGS, n);
2593 break;
2594 case TryExcept_kind:
2595 return compiler_try_except(c, s);
2596 case TryFinally_kind:
2597 return compiler_try_finally(c, s);
2598 case Assert_kind:
2599 return compiler_assert(c, s);
2600 case Import_kind:
2601 return compiler_import(c, s);
2602 case ImportFrom_kind:
2603 return compiler_from_import(c, s);
2604 case Exec_kind:
2605 VISIT(c, expr, s->v.Exec.body);
2606 if (s->v.Exec.globals) {
2607 VISIT(c, expr, s->v.Exec.globals);
2608 if (s->v.Exec.locals) {
2609 VISIT(c, expr, s->v.Exec.locals);
2610 } else {
2611 ADDOP(c, DUP_TOP);
2612 }
2613 } else {
2614 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2615 ADDOP(c, DUP_TOP);
2616 }
2617 ADDOP(c, EXEC_STMT);
2618 break;
2619 case Global_kind:
2620 break;
2621 case Expr_kind:
2622 VISIT(c, expr, s->v.Expr.value);
2623 if (c->c_interactive && c->c_nestlevel <= 1) {
2624 ADDOP(c, PRINT_EXPR);
2625 }
2626 else {
2627 ADDOP(c, POP_TOP);
2628 }
2629 break;
2630 case Pass_kind:
2631 break;
2632 case Break_kind:
2633 if (!c->u->u_nfblocks)
2634 return compiler_error(c, "'break' outside loop");
2635 ADDOP(c, BREAK_LOOP);
2636 break;
2637 case Continue_kind:
2638 return compiler_continue(c);
2639 }
2640 return 1;
2641}
2642
2643static int
2644unaryop(unaryop_ty op)
2645{
2646 switch (op) {
2647 case Invert:
2648 return UNARY_INVERT;
2649 case Not:
2650 return UNARY_NOT;
2651 case UAdd:
2652 return UNARY_POSITIVE;
2653 case USub:
2654 return UNARY_NEGATIVE;
2655 }
2656 return 0;
2657}
2658
2659static int
2660binop(struct compiler *c, operator_ty op)
2661{
2662 switch (op) {
2663 case Add:
2664 return BINARY_ADD;
2665 case Sub:
2666 return BINARY_SUBTRACT;
2667 case Mult:
2668 return BINARY_MULTIPLY;
2669 case Div:
2670 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2671 return BINARY_TRUE_DIVIDE;
2672 else
2673 return BINARY_DIVIDE;
2674 case Mod:
2675 return BINARY_MODULO;
2676 case Pow:
2677 return BINARY_POWER;
2678 case LShift:
2679 return BINARY_LSHIFT;
2680 case RShift:
2681 return BINARY_RSHIFT;
2682 case BitOr:
2683 return BINARY_OR;
2684 case BitXor:
2685 return BINARY_XOR;
2686 case BitAnd:
2687 return BINARY_AND;
2688 case FloorDiv:
2689 return BINARY_FLOOR_DIVIDE;
2690 }
2691 return 0;
2692}
2693
2694static int
2695cmpop(cmpop_ty op)
2696{
2697 switch (op) {
2698 case Eq:
2699 return PyCmp_EQ;
2700 case NotEq:
2701 return PyCmp_NE;
2702 case Lt:
2703 return PyCmp_LT;
2704 case LtE:
2705 return PyCmp_LE;
2706 case Gt:
2707 return PyCmp_GT;
2708 case GtE:
2709 return PyCmp_GE;
2710 case Is:
2711 return PyCmp_IS;
2712 case IsNot:
2713 return PyCmp_IS_NOT;
2714 case In:
2715 return PyCmp_IN;
2716 case NotIn:
2717 return PyCmp_NOT_IN;
2718 }
2719 return PyCmp_BAD;
2720}
2721
2722static int
2723inplace_binop(struct compiler *c, operator_ty op)
2724{
2725 switch (op) {
2726 case Add:
2727 return INPLACE_ADD;
2728 case Sub:
2729 return INPLACE_SUBTRACT;
2730 case Mult:
2731 return INPLACE_MULTIPLY;
2732 case Div:
2733 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2734 return INPLACE_TRUE_DIVIDE;
2735 else
2736 return INPLACE_DIVIDE;
2737 case Mod:
2738 return INPLACE_MODULO;
2739 case Pow:
2740 return INPLACE_POWER;
2741 case LShift:
2742 return INPLACE_LSHIFT;
2743 case RShift:
2744 return INPLACE_RSHIFT;
2745 case BitOr:
2746 return INPLACE_OR;
2747 case BitXor:
2748 return INPLACE_XOR;
2749 case BitAnd:
2750 return INPLACE_AND;
2751 case FloorDiv:
2752 return INPLACE_FLOOR_DIVIDE;
2753 }
Neal Norwitz4737b232005-11-19 23:58:29 +00002754 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002755 "inplace binary op %d should not be possible", op);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002756 return 0;
2757}
2758
2759static int
2760compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2761{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002762 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002763 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2764
2765 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002766 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002767 /* XXX AugStore isn't used anywhere! */
2768
2769 /* First check for assignment to __debug__. Param? */
2770 if ((ctx == Store || ctx == AugStore || ctx == Del)
2771 && !strcmp(PyString_AS_STRING(name), "__debug__")) {
2772 return compiler_error(c, "can not assign to __debug__");
2773 }
2774
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002775 mangled = _Py_Mangle(c->u->u_private, name);
2776 if (!mangled)
2777 return 0;
2778
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002779 op = 0;
2780 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002781 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002782 switch (scope) {
2783 case FREE:
2784 dict = c->u->u_freevars;
2785 optype = OP_DEREF;
2786 break;
2787 case CELL:
2788 dict = c->u->u_cellvars;
2789 optype = OP_DEREF;
2790 break;
2791 case LOCAL:
2792 if (c->u->u_ste->ste_type == FunctionBlock)
2793 optype = OP_FAST;
2794 break;
2795 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002796 if (c->u->u_ste->ste_type == FunctionBlock &&
2797 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002798 optype = OP_GLOBAL;
2799 break;
2800 case GLOBAL_EXPLICIT:
2801 optype = OP_GLOBAL;
2802 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002803 default:
2804 /* scope can be 0 */
2805 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002806 }
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 Norwitz4e6bf492005-12-18 05:32:41 +00002827 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002828 PyErr_SetString(PyExc_SystemError,
2829 "param invalid for deref variable");
2830 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831 }
2832 break;
2833 case OP_FAST:
2834 switch (ctx) {
2835 case Load: op = LOAD_FAST; break;
2836 case Store: op = STORE_FAST; break;
2837 case Del: op = DELETE_FAST; break;
2838 case AugLoad:
2839 case AugStore:
2840 break;
2841 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002842 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002843 PyErr_SetString(PyExc_SystemError,
2844 "param invalid for local variable");
2845 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002846 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002847 ADDOP_O(c, op, mangled, varnames);
2848 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002849 return 1;
2850 case OP_GLOBAL:
2851 switch (ctx) {
2852 case Load: op = LOAD_GLOBAL; break;
2853 case Store: op = STORE_GLOBAL; break;
2854 case Del: op = DELETE_GLOBAL; break;
2855 case AugLoad:
2856 case AugStore:
2857 break;
2858 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002859 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002860 PyErr_SetString(PyExc_SystemError,
2861 "param invalid for global variable");
2862 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002863 }
2864 break;
2865 case OP_NAME:
2866 switch (ctx) {
2867 case Load: op = LOAD_NAME; break;
2868 case Store: op = STORE_NAME; break;
2869 case Del: op = DELETE_NAME; break;
2870 case AugLoad:
2871 case AugStore:
2872 break;
2873 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002874 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002875 PyErr_SetString(PyExc_SystemError,
2876 "param invalid for name variable");
2877 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002878 }
2879 break;
2880 }
2881
2882 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002883 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002884 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002885 if (arg < 0)
2886 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002887 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002888}
2889
2890static int
2891compiler_boolop(struct compiler *c, expr_ty e)
2892{
2893 basicblock *end;
2894 int jumpi, i, n;
2895 asdl_seq *s;
2896
2897 assert(e->kind == BoolOp_kind);
2898 if (e->v.BoolOp.op == And)
2899 jumpi = JUMP_IF_FALSE;
2900 else
2901 jumpi = JUMP_IF_TRUE;
2902 end = compiler_new_block(c);
2903 if (end < 0)
2904 return 0;
2905 s = e->v.BoolOp.values;
2906 n = asdl_seq_LEN(s) - 1;
2907 for (i = 0; i < n; ++i) {
2908 VISIT(c, expr, asdl_seq_GET(s, i));
2909 ADDOP_JREL(c, jumpi, end);
2910 ADDOP(c, POP_TOP)
2911 }
2912 VISIT(c, expr, asdl_seq_GET(s, n));
2913 compiler_use_next_block(c, end);
2914 return 1;
2915}
2916
2917static int
2918compiler_list(struct compiler *c, expr_ty e)
2919{
2920 int n = asdl_seq_LEN(e->v.List.elts);
2921 if (e->v.List.ctx == Store) {
2922 ADDOP_I(c, UNPACK_SEQUENCE, n);
2923 }
2924 VISIT_SEQ(c, expr, e->v.List.elts);
2925 if (e->v.List.ctx == Load) {
2926 ADDOP_I(c, BUILD_LIST, n);
2927 }
2928 return 1;
2929}
2930
2931static int
2932compiler_tuple(struct compiler *c, expr_ty e)
2933{
2934 int n = asdl_seq_LEN(e->v.Tuple.elts);
2935 if (e->v.Tuple.ctx == Store) {
2936 ADDOP_I(c, UNPACK_SEQUENCE, n);
2937 }
2938 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2939 if (e->v.Tuple.ctx == Load) {
2940 ADDOP_I(c, BUILD_TUPLE, n);
2941 }
2942 return 1;
2943}
2944
2945static int
2946compiler_compare(struct compiler *c, expr_ty e)
2947{
2948 int i, n;
2949 basicblock *cleanup = NULL;
2950
2951 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2952 VISIT(c, expr, e->v.Compare.left);
2953 n = asdl_seq_LEN(e->v.Compare.ops);
2954 assert(n > 0);
2955 if (n > 1) {
2956 cleanup = compiler_new_block(c);
2957 if (cleanup == NULL)
2958 return 0;
2959 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, 0));
2960 }
2961 for (i = 1; i < n; i++) {
2962 ADDOP(c, DUP_TOP);
2963 ADDOP(c, ROT_THREE);
2964 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
2965 ADDOP_I(c, COMPARE_OP,
2966 cmpop((cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i - 1)));
2967 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2968 NEXT_BLOCK(c);
2969 ADDOP(c, POP_TOP);
2970 if (i < (n - 1))
2971 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, i));
2972 }
2973 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, n - 1));
2974 ADDOP_I(c, COMPARE_OP,
2975 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
2976 cmpop((cmpop_ty)asdl_seq_GET(e->v.Compare.ops, n - 1)));
2977 if (n > 1) {
2978 basicblock *end = compiler_new_block(c);
2979 if (end == NULL)
2980 return 0;
2981 ADDOP_JREL(c, JUMP_FORWARD, end);
2982 compiler_use_next_block(c, cleanup);
2983 ADDOP(c, ROT_TWO);
2984 ADDOP(c, POP_TOP);
2985 compiler_use_next_block(c, end);
2986 }
2987 return 1;
2988}
2989
2990static int
2991compiler_call(struct compiler *c, expr_ty e)
2992{
2993 int n, code = 0;
2994
2995 VISIT(c, expr, e->v.Call.func);
2996 n = asdl_seq_LEN(e->v.Call.args);
2997 VISIT_SEQ(c, expr, e->v.Call.args);
2998 if (e->v.Call.keywords) {
2999 VISIT_SEQ(c, keyword, e->v.Call.keywords);
3000 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
3001 }
3002 if (e->v.Call.starargs) {
3003 VISIT(c, expr, e->v.Call.starargs);
3004 code |= 1;
3005 }
3006 if (e->v.Call.kwargs) {
3007 VISIT(c, expr, e->v.Call.kwargs);
3008 code |= 2;
3009 }
3010 switch (code) {
3011 case 0:
3012 ADDOP_I(c, CALL_FUNCTION, n);
3013 break;
3014 case 1:
3015 ADDOP_I(c, CALL_FUNCTION_VAR, n);
3016 break;
3017 case 2:
3018 ADDOP_I(c, CALL_FUNCTION_KW, n);
3019 break;
3020 case 3:
3021 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
3022 break;
3023 }
3024 return 1;
3025}
3026
3027static int
3028compiler_listcomp_generator(struct compiler *c, PyObject *tmpname,
3029 asdl_seq *generators, int gen_index,
3030 expr_ty elt)
3031{
3032 /* generate code for the iterator, then each of the ifs,
3033 and then write to the element */
3034
3035 comprehension_ty l;
3036 basicblock *start, *anchor, *skip, *if_cleanup;
3037 int i, n;
3038
3039 start = compiler_new_block(c);
3040 skip = compiler_new_block(c);
3041 if_cleanup = compiler_new_block(c);
3042 anchor = compiler_new_block(c);
3043
3044 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3045 anchor == NULL)
3046 return 0;
3047
3048 l = asdl_seq_GET(generators, gen_index);
3049 VISIT(c, expr, l->iter);
3050 ADDOP(c, GET_ITER);
3051 compiler_use_next_block(c, start);
3052 ADDOP_JREL(c, FOR_ITER, anchor);
3053 NEXT_BLOCK(c);
3054 VISIT(c, expr, l->target);
3055
3056 /* XXX this needs to be cleaned up...a lot! */
3057 n = asdl_seq_LEN(l->ifs);
3058 for (i = 0; i < n; i++) {
3059 expr_ty e = asdl_seq_GET(l->ifs, i);
3060 VISIT(c, expr, e);
3061 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
3062 NEXT_BLOCK(c);
3063 ADDOP(c, POP_TOP);
3064 }
3065
3066 if (++gen_index < asdl_seq_LEN(generators))
3067 if (!compiler_listcomp_generator(c, tmpname,
3068 generators, gen_index, elt))
3069 return 0;
3070
3071 /* only append after the last for generator */
3072 if (gen_index >= asdl_seq_LEN(generators)) {
3073 if (!compiler_nameop(c, tmpname, Load))
3074 return 0;
3075 VISIT(c, expr, elt);
3076 ADDOP_I(c, CALL_FUNCTION, 1);
3077 ADDOP(c, POP_TOP);
3078
3079 compiler_use_next_block(c, skip);
3080 }
3081 for (i = 0; i < n; i++) {
3082 ADDOP_I(c, JUMP_FORWARD, 1);
3083 if (i == 0)
3084 compiler_use_next_block(c, if_cleanup);
3085 ADDOP(c, POP_TOP);
3086 }
3087 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3088 compiler_use_next_block(c, anchor);
3089 /* delete the append method added to locals */
3090 if (gen_index == 1)
3091 if (!compiler_nameop(c, tmpname, Del))
3092 return 0;
3093
3094 return 1;
3095}
3096
3097static int
3098compiler_listcomp(struct compiler *c, expr_ty e)
3099{
3100 char tmpname[256];
3101 identifier tmp;
3102 int rc = 0;
3103 static identifier append;
3104 asdl_seq *generators = e->v.ListComp.generators;
3105
3106 assert(e->kind == ListComp_kind);
3107 if (!append) {
3108 append = PyString_InternFromString("append");
3109 if (!append)
3110 return 0;
3111 }
3112 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
3113 tmp = PyString_FromString(tmpname);
3114 if (!tmp)
3115 return 0;
3116 ADDOP_I(c, BUILD_LIST, 0);
3117 ADDOP(c, DUP_TOP);
3118 ADDOP_O(c, LOAD_ATTR, append, names);
3119 if (compiler_nameop(c, tmp, Store))
3120 rc = compiler_listcomp_generator(c, tmp, generators, 0,
3121 e->v.ListComp.elt);
3122 Py_DECREF(tmp);
3123 return rc;
3124}
3125
3126static int
3127compiler_genexp_generator(struct compiler *c,
3128 asdl_seq *generators, int gen_index,
3129 expr_ty elt)
3130{
3131 /* generate code for the iterator, then each of the ifs,
3132 and then write to the element */
3133
3134 comprehension_ty ge;
3135 basicblock *start, *anchor, *skip, *if_cleanup, *end;
3136 int i, n;
3137
3138 start = compiler_new_block(c);
3139 skip = compiler_new_block(c);
3140 if_cleanup = compiler_new_block(c);
3141 anchor = compiler_new_block(c);
3142 end = compiler_new_block(c);
3143
3144 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3145 anchor == NULL || end == NULL)
3146 return 0;
3147
3148 ge = asdl_seq_GET(generators, gen_index);
3149 ADDOP_JREL(c, SETUP_LOOP, end);
3150 if (!compiler_push_fblock(c, LOOP, start))
3151 return 0;
3152
3153 if (gen_index == 0) {
3154 /* Receive outermost iter as an implicit argument */
3155 c->u->u_argcount = 1;
3156 ADDOP_I(c, LOAD_FAST, 0);
3157 }
3158 else {
3159 /* Sub-iter - calculate on the fly */
3160 VISIT(c, expr, ge->iter);
3161 ADDOP(c, GET_ITER);
3162 }
3163 compiler_use_next_block(c, start);
3164 ADDOP_JREL(c, FOR_ITER, anchor);
3165 NEXT_BLOCK(c);
3166 VISIT(c, expr, ge->target);
3167
3168 /* XXX this needs to be cleaned up...a lot! */
3169 n = asdl_seq_LEN(ge->ifs);
3170 for (i = 0; i < n; i++) {
3171 expr_ty e = asdl_seq_GET(ge->ifs, i);
3172 VISIT(c, expr, e);
3173 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
3174 NEXT_BLOCK(c);
3175 ADDOP(c, POP_TOP);
3176 }
3177
3178 if (++gen_index < asdl_seq_LEN(generators))
3179 if (!compiler_genexp_generator(c, generators, gen_index, elt))
3180 return 0;
3181
3182 /* only append after the last 'for' generator */
3183 if (gen_index >= asdl_seq_LEN(generators)) {
3184 VISIT(c, expr, elt);
3185 ADDOP(c, YIELD_VALUE);
3186 ADDOP(c, POP_TOP);
3187
3188 compiler_use_next_block(c, skip);
3189 }
3190 for (i = 0; i < n; i++) {
3191 ADDOP_I(c, JUMP_FORWARD, 1);
3192 if (i == 0)
3193 compiler_use_next_block(c, if_cleanup);
3194
3195 ADDOP(c, POP_TOP);
3196 }
3197 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3198 compiler_use_next_block(c, anchor);
3199 ADDOP(c, POP_BLOCK);
3200 compiler_pop_fblock(c, LOOP, start);
3201 compiler_use_next_block(c, end);
3202
3203 return 1;
3204}
3205
3206static int
3207compiler_genexp(struct compiler *c, expr_ty e)
3208{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00003209 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003210 PyCodeObject *co;
3211 expr_ty outermost_iter = ((comprehension_ty)
3212 (asdl_seq_GET(e->v.GeneratorExp.generators,
3213 0)))->iter;
3214
Nick Coghlan944d3eb2005-11-16 12:46:55 +00003215 if (!name) {
3216 name = PyString_FromString("<genexpr>");
3217 if (!name)
3218 return 0;
3219 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003220
3221 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
3222 return 0;
3223 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
3224 e->v.GeneratorExp.elt);
3225 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00003226 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003227 if (co == NULL)
3228 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003229
3230 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00003231 Py_DECREF(co);
3232
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233 VISIT(c, expr, outermost_iter);
3234 ADDOP(c, GET_ITER);
3235 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003236
3237 return 1;
3238}
3239
3240static int
3241compiler_visit_keyword(struct compiler *c, keyword_ty k)
3242{
3243 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3244 VISIT(c, expr, k->value);
3245 return 1;
3246}
3247
3248/* Test whether expression is constant. For constants, report
3249 whether they are true or false.
3250
3251 Return values: 1 for true, 0 for false, -1 for non-constant.
3252 */
3253
3254static int
3255expr_constant(expr_ty e)
3256{
3257 switch (e->kind) {
3258 case Num_kind:
3259 return PyObject_IsTrue(e->v.Num.n);
3260 case Str_kind:
3261 return PyObject_IsTrue(e->v.Str.s);
3262 default:
3263 return -1;
3264 }
3265}
3266
3267static int
3268compiler_visit_expr(struct compiler *c, expr_ty e)
3269{
3270 int i, n;
3271
3272 if (e->lineno > c->u->u_lineno) {
3273 c->u->u_lineno = e->lineno;
3274 c->u->u_lineno_set = false;
3275 }
3276 switch (e->kind) {
3277 case BoolOp_kind:
3278 return compiler_boolop(c, e);
3279 case BinOp_kind:
3280 VISIT(c, expr, e->v.BinOp.left);
3281 VISIT(c, expr, e->v.BinOp.right);
3282 ADDOP(c, binop(c, e->v.BinOp.op));
3283 break;
3284 case UnaryOp_kind:
3285 VISIT(c, expr, e->v.UnaryOp.operand);
3286 ADDOP(c, unaryop(e->v.UnaryOp.op));
3287 break;
3288 case Lambda_kind:
3289 return compiler_lambda(c, e);
3290 case Dict_kind:
3291 /* XXX get rid of arg? */
3292 ADDOP_I(c, BUILD_MAP, 0);
3293 n = asdl_seq_LEN(e->v.Dict.values);
3294 /* We must arrange things just right for STORE_SUBSCR.
3295 It wants the stack to look like (value) (dict) (key) */
3296 for (i = 0; i < n; i++) {
3297 ADDOP(c, DUP_TOP);
3298 VISIT(c, expr, asdl_seq_GET(e->v.Dict.values, i));
3299 ADDOP(c, ROT_TWO);
3300 VISIT(c, expr, asdl_seq_GET(e->v.Dict.keys, i));
3301 ADDOP(c, STORE_SUBSCR);
3302 }
3303 break;
3304 case ListComp_kind:
3305 return compiler_listcomp(c, e);
3306 case GeneratorExp_kind:
3307 return compiler_genexp(c, e);
3308 case Yield_kind:
3309 if (c->u->u_ste->ste_type != FunctionBlock)
3310 return compiler_error(c, "'yield' outside function");
3311 /*
3312 for (i = 0; i < c->u->u_nfblocks; i++) {
3313 if (c->u->u_fblock[i].fb_type == FINALLY_TRY)
3314 return compiler_error(
3315 c, "'yield' not allowed in a 'try' "
3316 "block with a 'finally' clause");
3317 }
3318 */
3319 if (e->v.Yield.value) {
3320 VISIT(c, expr, e->v.Yield.value);
3321 }
3322 else {
3323 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3324 }
3325 ADDOP(c, YIELD_VALUE);
3326 break;
3327 case Compare_kind:
3328 return compiler_compare(c, e);
3329 case Call_kind:
3330 return compiler_call(c, e);
3331 case Repr_kind:
3332 VISIT(c, expr, e->v.Repr.value);
3333 ADDOP(c, UNARY_CONVERT);
3334 break;
3335 case Num_kind:
3336 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3337 break;
3338 case Str_kind:
3339 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3340 break;
3341 /* The following exprs can be assignment targets. */
3342 case Attribute_kind:
3343 if (e->v.Attribute.ctx != AugStore)
3344 VISIT(c, expr, e->v.Attribute.value);
3345 switch (e->v.Attribute.ctx) {
3346 case AugLoad:
3347 ADDOP(c, DUP_TOP);
3348 /* Fall through to load */
3349 case Load:
3350 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3351 break;
3352 case AugStore:
3353 ADDOP(c, ROT_TWO);
3354 /* Fall through to save */
3355 case Store:
3356 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3357 break;
3358 case Del:
3359 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3360 break;
3361 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003362 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003363 PyErr_SetString(PyExc_SystemError,
3364 "param invalid in attribute expression");
3365 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003366 }
3367 break;
3368 case Subscript_kind:
3369 switch (e->v.Subscript.ctx) {
3370 case AugLoad:
3371 VISIT(c, expr, e->v.Subscript.value);
3372 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3373 break;
3374 case Load:
3375 VISIT(c, expr, e->v.Subscript.value);
3376 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3377 break;
3378 case AugStore:
3379 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3380 break;
3381 case Store:
3382 VISIT(c, expr, e->v.Subscript.value);
3383 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3384 break;
3385 case Del:
3386 VISIT(c, expr, e->v.Subscript.value);
3387 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3388 break;
3389 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003390 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003391 PyErr_SetString(PyExc_SystemError,
3392 "param invalid in subscript expression");
3393 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003394 }
3395 break;
3396 case Name_kind:
3397 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3398 /* child nodes of List and Tuple will have expr_context set */
3399 case List_kind:
3400 return compiler_list(c, e);
3401 case Tuple_kind:
3402 return compiler_tuple(c, e);
3403 }
3404 return 1;
3405}
3406
3407static int
3408compiler_augassign(struct compiler *c, stmt_ty s)
3409{
3410 expr_ty e = s->v.AugAssign.target;
3411 expr_ty auge;
3412
3413 assert(s->kind == AugAssign_kind);
3414
3415 switch (e->kind) {
3416 case Attribute_kind:
3417 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003418 AugLoad, e->lineno, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003419 if (auge == NULL)
3420 return 0;
3421 VISIT(c, expr, auge);
3422 VISIT(c, expr, s->v.AugAssign.value);
3423 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3424 auge->v.Attribute.ctx = AugStore;
3425 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003426 break;
3427 case Subscript_kind:
3428 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003429 AugLoad, e->lineno, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003430 if (auge == NULL)
3431 return 0;
3432 VISIT(c, expr, auge);
3433 VISIT(c, expr, s->v.AugAssign.value);
3434 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3435 auge->v.Subscript.ctx = AugStore;
3436 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003437 break;
3438 case Name_kind:
3439 VISIT(c, expr, s->v.AugAssign.target);
3440 VISIT(c, expr, s->v.AugAssign.value);
3441 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3442 return compiler_nameop(c, e->v.Name.id, Store);
3443 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003444 PyErr_Format(PyExc_SystemError,
3445 "invalid node type (%d) for augmented assignment",
3446 e->kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003447 return 0;
3448 }
3449 return 1;
3450}
3451
3452static int
3453compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3454{
3455 struct fblockinfo *f;
3456 if (c->u->u_nfblocks >= CO_MAXBLOCKS)
3457 return 0;
3458 f = &c->u->u_fblock[c->u->u_nfblocks++];
3459 f->fb_type = t;
3460 f->fb_block = b;
3461 return 1;
3462}
3463
3464static void
3465compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3466{
3467 struct compiler_unit *u = c->u;
3468 assert(u->u_nfblocks > 0);
3469 u->u_nfblocks--;
3470 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3471 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3472}
3473
3474/* Raises a SyntaxError and returns 0.
3475 If something goes wrong, a different exception may be raised.
3476*/
3477
3478static int
3479compiler_error(struct compiler *c, const char *errstr)
3480{
3481 PyObject *loc;
3482 PyObject *u = NULL, *v = NULL;
3483
3484 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3485 if (!loc) {
3486 Py_INCREF(Py_None);
3487 loc = Py_None;
3488 }
3489 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3490 Py_None, loc);
3491 if (!u)
3492 goto exit;
3493 v = Py_BuildValue("(zO)", errstr, u);
3494 if (!v)
3495 goto exit;
3496 PyErr_SetObject(PyExc_SyntaxError, v);
3497 exit:
3498 Py_DECREF(loc);
3499 Py_XDECREF(u);
3500 Py_XDECREF(v);
3501 return 0;
3502}
3503
3504static int
3505compiler_handle_subscr(struct compiler *c, const char *kind,
3506 expr_context_ty ctx)
3507{
3508 int op = 0;
3509
3510 /* XXX this code is duplicated */
3511 switch (ctx) {
3512 case AugLoad: /* fall through to Load */
3513 case Load: op = BINARY_SUBSCR; break;
3514 case AugStore:/* fall through to Store */
3515 case Store: op = STORE_SUBSCR; break;
3516 case Del: op = DELETE_SUBSCR; break;
3517 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003518 PyErr_Format(PyExc_SystemError,
3519 "invalid %s kind %d in subscript\n",
3520 kind, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003521 return 0;
3522 }
3523 if (ctx == AugLoad) {
3524 ADDOP_I(c, DUP_TOPX, 2);
3525 }
3526 else if (ctx == AugStore) {
3527 ADDOP(c, ROT_THREE);
3528 }
3529 ADDOP(c, op);
3530 return 1;
3531}
3532
3533static int
3534compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3535{
3536 int n = 2;
3537 assert(s->kind == Slice_kind);
3538
3539 /* only handles the cases where BUILD_SLICE is emitted */
3540 if (s->v.Slice.lower) {
3541 VISIT(c, expr, s->v.Slice.lower);
3542 }
3543 else {
3544 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3545 }
3546
3547 if (s->v.Slice.upper) {
3548 VISIT(c, expr, s->v.Slice.upper);
3549 }
3550 else {
3551 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3552 }
3553
3554 if (s->v.Slice.step) {
3555 n++;
3556 VISIT(c, expr, s->v.Slice.step);
3557 }
3558 ADDOP_I(c, BUILD_SLICE, n);
3559 return 1;
3560}
3561
3562static int
3563compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3564{
3565 int op = 0, slice_offset = 0, stack_count = 0;
3566
3567 assert(s->v.Slice.step == NULL);
3568 if (s->v.Slice.lower) {
3569 slice_offset++;
3570 stack_count++;
3571 if (ctx != AugStore)
3572 VISIT(c, expr, s->v.Slice.lower);
3573 }
3574 if (s->v.Slice.upper) {
3575 slice_offset += 2;
3576 stack_count++;
3577 if (ctx != AugStore)
3578 VISIT(c, expr, s->v.Slice.upper);
3579 }
3580
3581 if (ctx == AugLoad) {
3582 switch (stack_count) {
3583 case 0: ADDOP(c, DUP_TOP); break;
3584 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3585 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3586 }
3587 }
3588 else if (ctx == AugStore) {
3589 switch (stack_count) {
3590 case 0: ADDOP(c, ROT_TWO); break;
3591 case 1: ADDOP(c, ROT_THREE); break;
3592 case 2: ADDOP(c, ROT_FOUR); break;
3593 }
3594 }
3595
3596 switch (ctx) {
3597 case AugLoad: /* fall through to Load */
3598 case Load: op = SLICE; break;
3599 case AugStore:/* fall through to Store */
3600 case Store: op = STORE_SLICE; break;
3601 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003602 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003603 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003604 PyErr_SetString(PyExc_SystemError,
3605 "param invalid in simple slice");
3606 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003607 }
3608
3609 ADDOP(c, op + slice_offset);
3610 return 1;
3611}
3612
3613static int
3614compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3615 expr_context_ty ctx)
3616{
3617 switch (s->kind) {
3618 case Ellipsis_kind:
3619 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3620 break;
3621 case Slice_kind:
3622 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003623 case Index_kind:
3624 VISIT(c, expr, s->v.Index.value);
3625 break;
3626 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003627 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003628 PyErr_SetString(PyExc_SystemError,
3629 "extended slice invalid in nested slice");
3630 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003631 }
3632 return 1;
3633}
3634
3635
3636static int
3637compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3638{
3639 switch (s->kind) {
3640 case Ellipsis_kind:
3641 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3642 break;
3643 case Slice_kind:
3644 if (!s->v.Slice.step)
3645 return compiler_simple_slice(c, s, ctx);
3646 if (!compiler_slice(c, s, ctx))
3647 return 0;
3648 if (ctx == AugLoad) {
3649 ADDOP_I(c, DUP_TOPX, 2);
3650 }
3651 else if (ctx == AugStore) {
3652 ADDOP(c, ROT_THREE);
3653 }
3654 return compiler_handle_subscr(c, "slice", ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003655 case ExtSlice_kind: {
3656 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3657 for (i = 0; i < n; i++) {
3658 slice_ty sub = asdl_seq_GET(s->v.ExtSlice.dims, i);
3659 if (!compiler_visit_nested_slice(c, sub, ctx))
3660 return 0;
3661 }
3662 ADDOP_I(c, BUILD_TUPLE, n);
3663 return compiler_handle_subscr(c, "extended slice", ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003664 }
3665 case Index_kind:
3666 if (ctx != AugStore)
3667 VISIT(c, expr, s->v.Index.value);
3668 return compiler_handle_subscr(c, "index", ctx);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003669 default:
3670 PyErr_Format(PyExc_SystemError,
3671 "invalid slice %d", s->kind);
3672 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003673 }
3674 return 1;
3675}
3676
3677/* do depth-first search of basic block graph, starting with block.
3678 post records the block indices in post-order.
3679
3680 XXX must handle implicit jumps from one block to next
3681*/
3682
3683static void
3684dfs(struct compiler *c, basicblock *b, struct assembler *a)
3685{
3686 int i;
3687 struct instr *instr = NULL;
3688
3689 if (b->b_seen)
3690 return;
3691 b->b_seen = 1;
3692 if (b->b_next != NULL)
3693 dfs(c, b->b_next, a);
3694 for (i = 0; i < b->b_iused; i++) {
3695 instr = &b->b_instr[i];
3696 if (instr->i_jrel || instr->i_jabs)
3697 dfs(c, instr->i_target, a);
3698 }
3699 a->a_postorder[a->a_nblocks++] = b;
3700}
3701
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003702static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003703stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3704{
3705 int i;
3706 struct instr *instr;
3707 if (b->b_seen || b->b_startdepth >= depth)
3708 return maxdepth;
3709 b->b_seen = 1;
3710 b->b_startdepth = depth;
3711 for (i = 0; i < b->b_iused; i++) {
3712 instr = &b->b_instr[i];
3713 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3714 if (depth > maxdepth)
3715 maxdepth = depth;
3716 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3717 if (instr->i_jrel || instr->i_jabs) {
3718 maxdepth = stackdepth_walk(c, instr->i_target,
3719 depth, maxdepth);
3720 if (instr->i_opcode == JUMP_ABSOLUTE ||
3721 instr->i_opcode == JUMP_FORWARD) {
3722 goto out; /* remaining code is dead */
3723 }
3724 }
3725 }
3726 if (b->b_next)
3727 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3728out:
3729 b->b_seen = 0;
3730 return maxdepth;
3731}
3732
3733/* Find the flow path that needs the largest stack. We assume that
3734 * cycles in the flow graph have no net effect on the stack depth.
3735 */
3736static int
3737stackdepth(struct compiler *c)
3738{
3739 basicblock *b, *entryblock;
3740 entryblock = NULL;
3741 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3742 b->b_seen = 0;
3743 b->b_startdepth = INT_MIN;
3744 entryblock = b;
3745 }
3746 return stackdepth_walk(c, entryblock, 0, 0);
3747}
3748
3749static int
3750assemble_init(struct assembler *a, int nblocks, int firstlineno)
3751{
3752 memset(a, 0, sizeof(struct assembler));
3753 a->a_lineno = firstlineno;
3754 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3755 if (!a->a_bytecode)
3756 return 0;
3757 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3758 if (!a->a_lnotab)
3759 return 0;
3760 a->a_postorder = (basicblock **)PyObject_Malloc(
3761 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003762 if (!a->a_postorder) {
3763 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003764 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003765 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003766 return 1;
3767}
3768
3769static void
3770assemble_free(struct assembler *a)
3771{
3772 Py_XDECREF(a->a_bytecode);
3773 Py_XDECREF(a->a_lnotab);
3774 if (a->a_postorder)
3775 PyObject_Free(a->a_postorder);
3776}
3777
3778/* Return the size of a basic block in bytes. */
3779
3780static int
3781instrsize(struct instr *instr)
3782{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003783 if (!instr->i_hasarg)
3784 return 1;
3785 if (instr->i_oparg > 0xffff)
3786 return 6;
3787 return 3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003788}
3789
3790static int
3791blocksize(basicblock *b)
3792{
3793 int i;
3794 int size = 0;
3795
3796 for (i = 0; i < b->b_iused; i++)
3797 size += instrsize(&b->b_instr[i]);
3798 return size;
3799}
3800
3801/* All about a_lnotab.
3802
3803c_lnotab is an array of unsigned bytes disguised as a Python string.
3804It is used to map bytecode offsets to source code line #s (when needed
3805for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003806
Tim Peters2a7f3842001-06-09 09:26:21 +00003807The array is conceptually a list of
3808 (bytecode offset increment, line number increment)
3809pairs. The details are important and delicate, best illustrated by example:
3810
3811 byte code offset source code line number
3812 0 1
3813 6 2
3814 50 7
3815 350 307
3816 361 308
3817
3818The first trick is that these numbers aren't stored, only the increments
3819from one row to the next (this doesn't really work, but it's a start):
3820
3821 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3822
3823The second trick is that an unsigned byte can't hold negative values, or
3824values larger than 255, so (a) there's a deep assumption that byte code
3825offsets and their corresponding line #s both increase monotonically, and (b)
3826if at least one column jumps by more than 255 from one row to the next, more
3827than one pair is written to the table. In case #b, there's no way to know
3828from looking at the table later how many were written. That's the delicate
3829part. A user of c_lnotab desiring to find the source line number
3830corresponding to a bytecode address A should do something like this
3831
3832 lineno = addr = 0
3833 for addr_incr, line_incr in c_lnotab:
3834 addr += addr_incr
3835 if addr > A:
3836 return lineno
3837 lineno += line_incr
3838
3839In order for this to work, when the addr field increments by more than 255,
3840the line # increment in each pair generated must be 0 until the remaining addr
3841increment is < 256. So, in the example above, com_set_lineno should not (as
3842was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
3843255, 0, 45, 255, 0, 45.
3844*/
3845
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003846static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003847assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003848{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003849 int d_bytecode, d_lineno;
3850 int len;
3851 char *lnotab;
3852
3853 d_bytecode = a->a_offset - a->a_lineno_off;
3854 d_lineno = i->i_lineno - a->a_lineno;
3855
3856 assert(d_bytecode >= 0);
3857 assert(d_lineno >= 0);
3858
3859 if (d_lineno == 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003860 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003861
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003862 if (d_bytecode > 255) {
3863 int i, nbytes, ncodes = d_bytecode / 255;
3864 nbytes = a->a_lnotab_off + 2 * ncodes;
3865 len = PyString_GET_SIZE(a->a_lnotab);
3866 if (nbytes >= len) {
3867 if (len * 2 < nbytes)
3868 len = nbytes;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003869 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003870 len *= 2;
3871 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3872 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003873 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003874 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3875 for (i = 0; i < ncodes; i++) {
3876 *lnotab++ = 255;
3877 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003878 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003879 d_bytecode -= ncodes * 255;
3880 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003881 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003882 assert(d_bytecode <= 255);
3883 if (d_lineno > 255) {
3884 int i, nbytes, ncodes = d_lineno / 255;
3885 nbytes = a->a_lnotab_off + 2 * ncodes;
3886 len = PyString_GET_SIZE(a->a_lnotab);
3887 if (nbytes >= len) {
3888 if (len * 2 < nbytes)
3889 len = nbytes;
Guido van Rossum635abd21997-01-06 22:56:52 +00003890 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003891 len *= 2;
3892 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3893 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003894 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003895 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3896 *lnotab++ = 255;
3897 *lnotab++ = d_bytecode;
3898 d_bytecode = 0;
3899 for (i = 1; i < ncodes; i++) {
3900 *lnotab++ = 255;
3901 *lnotab++ = 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003902 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003903 d_lineno -= ncodes * 255;
3904 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003905 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003906
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003907 len = PyString_GET_SIZE(a->a_lnotab);
3908 if (a->a_lnotab_off + 2 >= len) {
3909 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003910 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003911 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003912 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003913
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003914 a->a_lnotab_off += 2;
3915 if (d_bytecode) {
3916 *lnotab++ = d_bytecode;
3917 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003918 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003919 else { /* First line of a block; def stmt, etc. */
3920 *lnotab++ = 0;
3921 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003922 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003923 a->a_lineno = i->i_lineno;
3924 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003925 return 1;
3926}
3927
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003928/* assemble_emit()
3929 Extend the bytecode with a new instruction.
3930 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003931*/
3932
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003933static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003934assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003935{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003936 int size, arg = 0, ext = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003937 int len = PyString_GET_SIZE(a->a_bytecode);
3938 char *code;
3939
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003940 size = instrsize(i);
3941 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003942 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003943 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003944 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003945 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003946 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003947 if (a->a_offset + size >= len) {
3948 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003949 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003950 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003951 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3952 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003953 if (size == 6) {
3954 assert(i->i_hasarg);
3955 *code++ = (char)EXTENDED_ARG;
3956 *code++ = ext & 0xff;
3957 *code++ = ext >> 8;
3958 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003959 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003960 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003961 if (i->i_hasarg) {
3962 assert(size == 3 || size == 6);
3963 *code++ = arg & 0xff;
3964 *code++ = arg >> 8;
3965 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003966 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003967}
3968
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003969static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003970assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003971{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003972 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003973 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003974 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003975
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003976 /* Compute the size of each block and fixup jump args.
3977 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003978start:
3979 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003980 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003981 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003982 bsize = blocksize(b);
3983 b->b_offset = totsize;
3984 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003985 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003986 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003987 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3988 bsize = b->b_offset;
3989 for (i = 0; i < b->b_iused; i++) {
3990 struct instr *instr = &b->b_instr[i];
3991 /* Relative jumps are computed relative to
3992 the instruction pointer after fetching
3993 the jump instruction.
3994 */
3995 bsize += instrsize(instr);
3996 if (instr->i_jabs)
3997 instr->i_oparg = instr->i_target->b_offset;
3998 else if (instr->i_jrel) {
3999 int delta = instr->i_target->b_offset - bsize;
4000 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004001 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00004002 else
4003 continue;
4004 if (instr->i_oparg > 0xffff)
4005 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004006 }
4007 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00004008
4009 /* XXX: This is an awful hack that could hurt performance, but
4010 on the bright side it should work until we come up
4011 with a better solution.
4012
4013 In the meantime, should the goto be dropped in favor
4014 of a loop?
4015
4016 The issue is that in the first loop blocksize() is called
4017 which calls instrsize() which requires i_oparg be set
4018 appropriately. There is a bootstrap problem because
4019 i_oparg is calculated in the second loop above.
4020
4021 So we loop until we stop seeing new EXTENDED_ARGs.
4022 The only EXTENDED_ARGs that could be popping up are
4023 ones in jump instructions. So this should converge
4024 fairly quickly.
4025 */
4026 if (last_extended_arg_count != extended_arg_count) {
4027 last_extended_arg_count = extended_arg_count;
4028 goto start;
4029 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004030}
4031
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004032static PyObject *
4033dict_keys_inorder(PyObject *dict, int offset)
4034{
4035 PyObject *tuple, *k, *v;
4036 int i, pos = 0, size = PyDict_Size(dict);
4037
4038 tuple = PyTuple_New(size);
4039 if (tuple == NULL)
4040 return NULL;
4041 while (PyDict_Next(dict, &pos, &k, &v)) {
4042 i = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004043 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004044 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004045 assert((i - offset) < size);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004046 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004047 PyTuple_SET_ITEM(tuple, i - offset, k);
4048 }
4049 return tuple;
4050}
4051
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004052static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004053compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004054{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004055 PySTEntryObject *ste = c->u->u_ste;
4056 int flags = 0, n;
4057 if (ste->ste_type != ModuleBlock)
4058 flags |= CO_NEWLOCALS;
4059 if (ste->ste_type == FunctionBlock) {
4060 if (!ste->ste_unoptimized)
4061 flags |= CO_OPTIMIZED;
4062 if (ste->ste_nested)
4063 flags |= CO_NESTED;
4064 if (ste->ste_generator)
4065 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004066 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004067 if (ste->ste_varargs)
4068 flags |= CO_VARARGS;
4069 if (ste->ste_varkeywords)
4070 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00004071 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004072 flags |= CO_GENERATOR;
4073 if (c->c_flags->cf_flags & CO_FUTURE_DIVISION)
4074 flags |= CO_FUTURE_DIVISION;
4075 n = PyDict_Size(c->u->u_freevars);
4076 if (n < 0)
4077 return -1;
4078 if (n == 0) {
4079 n = PyDict_Size(c->u->u_cellvars);
4080 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004081 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004082 if (n == 0) {
4083 flags |= CO_NOFREE;
4084 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004085 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004086
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004087 return flags;
4088}
4089
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004090static PyCodeObject *
4091makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004092{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004093 PyObject *tmp;
4094 PyCodeObject *co = NULL;
4095 PyObject *consts = NULL;
4096 PyObject *names = NULL;
4097 PyObject *varnames = NULL;
4098 PyObject *filename = NULL;
4099 PyObject *name = NULL;
4100 PyObject *freevars = NULL;
4101 PyObject *cellvars = NULL;
4102 PyObject *bytecode = NULL;
4103 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004104
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004105 tmp = dict_keys_inorder(c->u->u_consts, 0);
4106 if (!tmp)
4107 goto error;
4108 consts = PySequence_List(tmp); /* optimize_code requires a list */
4109 Py_DECREF(tmp);
4110
4111 names = dict_keys_inorder(c->u->u_names, 0);
4112 varnames = dict_keys_inorder(c->u->u_varnames, 0);
4113 if (!consts || !names || !varnames)
4114 goto error;
4115
4116 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4117 if (!cellvars)
4118 goto error;
4119 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4120 if (!freevars)
4121 goto error;
4122 filename = PyString_FromString(c->c_filename);
4123 if (!filename)
4124 goto error;
4125
4126 nlocals = PyDict_Size(c->u->u_varnames);
4127 flags = compute_code_flags(c);
4128 if (flags < 0)
4129 goto error;
4130
4131 bytecode = optimize_code(a->a_bytecode, consts, names, a->a_lnotab);
4132 if (!bytecode)
4133 goto error;
4134
4135 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4136 if (!tmp)
4137 goto error;
4138 Py_DECREF(consts);
4139 consts = tmp;
4140
4141 co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags,
4142 bytecode, consts, names, varnames,
4143 freevars, cellvars,
4144 filename, c->u->u_name,
4145 c->u->u_firstlineno,
4146 a->a_lnotab);
4147 error:
4148 Py_XDECREF(consts);
4149 Py_XDECREF(names);
4150 Py_XDECREF(varnames);
4151 Py_XDECREF(filename);
4152 Py_XDECREF(name);
4153 Py_XDECREF(freevars);
4154 Py_XDECREF(cellvars);
4155 Py_XDECREF(bytecode);
4156 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004157}
4158
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004159static PyCodeObject *
4160assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004161{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004162 basicblock *b, *entryblock;
4163 struct assembler a;
4164 int i, j, nblocks;
4165 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004166
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004167 /* Make sure every block that falls off the end returns None.
4168 XXX NEXT_BLOCK() isn't quite right, because if the last
4169 block ends with a jump or return b_next shouldn't set.
4170 */
4171 if (!c->u->u_curblock->b_return) {
4172 NEXT_BLOCK(c);
4173 if (addNone)
4174 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4175 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004176 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004177
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004178 nblocks = 0;
4179 entryblock = NULL;
4180 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4181 nblocks++;
4182 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004183 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004184
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004185 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4186 goto error;
4187 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004188
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004189 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004190 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004191
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004192 /* Emit code in reverse postorder from dfs. */
4193 for (i = a.a_nblocks - 1; i >= 0; i--) {
4194 basicblock *b = a.a_postorder[i];
4195 for (j = 0; j < b->b_iused; j++)
4196 if (!assemble_emit(&a, &b->b_instr[j]))
4197 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004198 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004199
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004200 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4201 goto error;
4202 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
4203 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004204
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004205 co = makecode(c, &a);
4206 error:
4207 assemble_free(&a);
4208 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004209}