blob: 418194e1eef7ff016d4135ff9b15342302fed359 [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"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000026#include "ast.h"
27#include "code.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000028#include "compile.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000029#include "symtable.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000030#include "opcode.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000031
Guido van Rossum8e793d91997-03-03 19:13:14 +000032int Py_OptimizeFlag = 0;
33
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000034/*
35 ISSUES:
Guido van Rossum8861b741996-07-30 16:49:37 +000036
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000037 character encodings aren't handled
Jeremy Hyltone36f7782001-01-19 03:21:30 +000038
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000039 ref leaks in interpreter when press return on empty line
Jeremy Hyltoneab156f2001-01-30 01:24:43 +000040
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000041 opcode_stack_effect() function should be reviewed since stack depth bugs
42 could be really hard to find later.
Jeremy Hyltoneab156f2001-01-30 01:24:43 +000043
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000044 Dead code is being generated (i.e. after unconditional jumps).
45*/
Jeremy Hylton29906ee2001-02-27 04:23:34 +000046
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000047#define DEFAULT_BLOCK_SIZE 16
48#define DEFAULT_BLOCKS 8
49#define DEFAULT_CODE_SIZE 128
50#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000051
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000052struct instr {
53 int i_jabs : 1;
54 int i_jrel : 1;
55 int i_hasarg : 1;
56 unsigned char i_opcode;
57 int i_oparg;
58 struct basicblock_ *i_target; /* target block (if jump instruction) */
59 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000060};
61
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000062typedef struct basicblock_ {
63 /* next block in the list of blocks for a unit (don't confuse with
64 * b_next) */
65 struct basicblock_ *b_list;
66 /* number of instructions used */
67 int b_iused;
68 /* length of instruction array (b_instr) */
69 int b_ialloc;
70 /* pointer to an array of instructions, initially NULL */
71 struct instr *b_instr;
72 /* If b_next is non-NULL, it is a pointer to the next
73 block reached by normal control flow. */
74 struct basicblock_ *b_next;
75 /* b_seen is used to perform a DFS of basicblocks. */
76 int b_seen : 1;
77 /* b_return is true if a RETURN_VALUE opcode is inserted. */
78 int b_return : 1;
79 /* depth of stack upon entry of block, computed by stackdepth() */
80 int b_startdepth;
81 /* instruction offset for block, computed by assemble_jump_offsets() */
82 int b_offset;
83} basicblock;
84
85/* fblockinfo tracks the current frame block.
86
87 A frame block is used to handle loops, try/except, and try/finally.
88 It's called a frame block to distinguish it from a basic block in the
89 compiler IR.
90*/
91
92enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
93
94struct fblockinfo {
95 enum fblocktype fb_type;
96 basicblock *fb_block;
97};
98
99/* The following items change on entry and exit of code blocks.
100 They must be saved and restored when returning to a block.
101*/
102struct compiler_unit {
103 PySTEntryObject *u_ste;
104
105 PyObject *u_name;
106 /* The following fields are dicts that map objects to
107 the index of them in co_XXX. The index is used as
108 the argument for opcodes that refer to those collections.
109 */
110 PyObject *u_consts; /* all constants */
111 PyObject *u_names; /* all names */
112 PyObject *u_varnames; /* local variables */
113 PyObject *u_cellvars; /* cell variables */
114 PyObject *u_freevars; /* free variables */
115
116 PyObject *u_private; /* for private name mangling */
117
118 int u_argcount; /* number of arguments for block */
119 basicblock *u_blocks; /* pointer to list of blocks */
120 basicblock *u_curblock; /* pointer to current block */
121 int u_tmpname; /* temporary variables for list comps */
122
123 int u_nfblocks;
124 struct fblockinfo u_fblock[CO_MAXBLOCKS];
125
126 int u_firstlineno; /* the first lineno of the block */
127 int u_lineno; /* the lineno for the current stmt */
128 bool u_lineno_set; /* boolean to indicate whether instr
129 has been generated with current lineno */
130};
131
132/* This struct captures the global state of a compilation.
133
134 The u pointer points to the current compilation unit, while units
135 for enclosing blocks are stored in c_stack. The u and c_stack are
136 managed by compiler_enter_scope() and compiler_exit_scope().
137*/
138
139struct compiler {
140 const char *c_filename;
141 struct symtable *c_st;
142 PyFutureFeatures *c_future; /* pointer to module's __future__ */
143 PyCompilerFlags *c_flags;
144
145 int c_interactive;
146 int c_nestlevel;
147
148 struct compiler_unit *u; /* compiler state for current block */
149 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
150 char *c_encoding; /* source encoding (a borrowed reference) */
151};
152
153struct assembler {
154 PyObject *a_bytecode; /* string containing bytecode */
155 int a_offset; /* offset into bytecode */
156 int a_nblocks; /* number of reachable blocks */
157 basicblock **a_postorder; /* list of blocks in dfs postorder */
158 PyObject *a_lnotab; /* string containing lnotab */
159 int a_lnotab_off; /* offset into lnotab */
160 int a_lineno; /* last lineno of emitted instruction */
161 int a_lineno_off; /* bytecode offset of last lineno */
162};
163
164static int compiler_enter_scope(struct compiler *, identifier, void *, int);
165static void compiler_free(struct compiler *);
166static basicblock *compiler_new_block(struct compiler *);
167static int compiler_next_instr(struct compiler *, basicblock *);
168static int compiler_addop(struct compiler *, int);
169static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
170static int compiler_addop_i(struct compiler *, int, int);
171static int compiler_addop_j(struct compiler *, int, basicblock *, int);
172static void compiler_use_block(struct compiler *, basicblock *);
173static basicblock *compiler_use_new_block(struct compiler *);
174static int compiler_error(struct compiler *, const char *);
175static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
176
177static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
178static int compiler_visit_stmt(struct compiler *, stmt_ty);
179static int compiler_visit_keyword(struct compiler *, keyword_ty);
180static int compiler_visit_expr(struct compiler *, expr_ty);
181static int compiler_augassign(struct compiler *, stmt_ty);
182static int compiler_visit_slice(struct compiler *, slice_ty,
183 expr_context_ty);
184
185static int compiler_push_fblock(struct compiler *, enum fblocktype,
186 basicblock *);
187static void compiler_pop_fblock(struct compiler *, enum fblocktype,
188 basicblock *);
189
190static int inplace_binop(struct compiler *, operator_ty);
191static int expr_constant(expr_ty e);
192
193static PyCodeObject *assemble(struct compiler *, int addNone);
194static PyObject *__doc__;
195
196PyObject *
197_Py_Mangle(PyObject *private, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000198{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000199 /* Name mangling: __private becomes _classname__private.
200 This is independent from how the name is used. */
201 const char *p, *name = PyString_AsString(ident);
202 char *buffer;
203 size_t nlen, plen;
204 if (private == NULL || name == NULL || name[0] != '_' || name[1] != '_') {
205 Py_INCREF(ident);
206 return ident;
207 }
208 p = PyString_AsString(private);
209 nlen = strlen(name);
210 if (name[nlen-1] == '_' && name[nlen-2] == '_') {
211 Py_INCREF(ident);
212 return ident; /* Don't mangle __whatever__ */
213 }
214 /* Strip leading underscores from class name */
215 while (*p == '_')
216 p++;
217 if (*p == '\0') {
218 Py_INCREF(ident);
219 return ident; /* Don't mangle if class is just underscores */
220 }
221 plen = strlen(p);
222 ident = PyString_FromStringAndSize(NULL, 1 + nlen + plen);
223 if (!ident)
224 return 0;
225 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
226 buffer = PyString_AS_STRING(ident);
227 buffer[0] = '_';
228 strncpy(buffer+1, p, plen);
229 strcpy(buffer+1+plen, name);
230 return ident;
Michael W. Hudson60934622004-08-12 17:56:29 +0000231}
232
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000233static int
234compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000235{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000236 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000237
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000238 c->c_stack = PyList_New(0);
239 if (!c->c_stack)
240 return 0;
241
242 return 1;
243}
244
245PyCodeObject *
246PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags)
247{
248 struct compiler c;
249 PyCodeObject *co = NULL;
250 PyCompilerFlags local_flags;
251 int merged;
252
253 if (!__doc__) {
254 __doc__ = PyString_InternFromString("__doc__");
255 if (!__doc__)
256 goto error;
257 }
258
259 if (!compiler_init(&c))
260 goto error;
261 c.c_filename = filename;
262 c.c_future = PyFuture_FromAST(mod, filename);
263 if (c.c_future == NULL)
264 goto error;
265 if (!flags) {
266 local_flags.cf_flags = 0;
267 flags = &local_flags;
268 }
269 merged = c.c_future->ff_features | flags->cf_flags;
270 c.c_future->ff_features = merged;
271 flags->cf_flags = merged;
272 c.c_flags = flags;
273 c.c_nestlevel = 0;
274
275 c.c_st = PySymtable_Build(mod, filename, c.c_future);
276 if (c.c_st == NULL) {
277 if (!PyErr_Occurred())
278 PyErr_SetString(PyExc_SystemError, "no symtable");
279 goto error;
280 }
281
282 /* XXX initialize to NULL for now, need to handle */
283 c.c_encoding = NULL;
284
285 co = compiler_mod(&c, mod);
286
287 error:
288 compiler_free(&c);
289 return co;
290}
291
292PyCodeObject *
293PyNode_Compile(struct _node *n, const char *filename)
294{
295 PyCodeObject *co;
296 mod_ty mod = PyAST_FromNode(n, NULL, filename);
297 if (!mod)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000298 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000299 co = PyAST_Compile(mod, filename, NULL);
300 free_mod(mod);
Raymond Hettinger37a724d2003-09-15 21:43:16 +0000301 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000302}
303
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000304static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000305compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000306{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000307 if (c->c_st)
308 PySymtable_Free(c->c_st);
309 if (c->c_future)
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000310 PyMem_Free(c->c_future);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000311 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000312}
313
Guido van Rossum79f25d91997-04-29 20:08:16 +0000314static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000315list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000316{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000317 int i, n;
318 PyObject *v, *k, *dict = PyDict_New();
Guido van Rossumd076c731998-10-07 19:42:25 +0000319
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000320 n = PyList_Size(list);
321 for (i = 0; i < n; i++) {
322 v = PyInt_FromLong(i);
323 if (!v) {
324 Py_DECREF(dict);
325 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000326 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000327 k = PyList_GET_ITEM(list, i);
328 k = Py_BuildValue("(OO)", k, k->ob_type);
329 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
330 Py_XDECREF(k);
331 Py_DECREF(v);
332 Py_DECREF(dict);
333 return NULL;
334 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000335 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000336 Py_DECREF(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000337 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000338 return dict;
339}
340
341/* Return new dict containing names from src that match scope(s).
342
343 src is a symbol table dictionary. If the scope of a name matches
344 either scope_type or flag is set, insert it into the new dict. The
345 values are integers, starting at offset and increasing by one for
346 each key.
347*/
348
349static PyObject *
350dictbytype(PyObject *src, int scope_type, int flag, int offset)
351{
352 int pos = 0, i = offset, scope;
353 PyObject *k, *v, *dest = PyDict_New();
354
355 assert(offset >= 0);
356 if (dest == NULL)
357 return NULL;
358
359 while (PyDict_Next(src, &pos, &k, &v)) {
360 /* XXX this should probably be a macro in symtable.h */
361 assert(PyInt_Check(v));
362 scope = (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
363
364 if (scope == scope_type || PyInt_AS_LONG(v) & flag) {
365 PyObject *tuple, *item = PyInt_FromLong(i);
366 if (item == NULL) {
367 Py_DECREF(dest);
368 return NULL;
369 }
370 i++;
371 tuple = Py_BuildValue("(OO)", k, k->ob_type);
372 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
373 Py_DECREF(item);
374 Py_DECREF(dest);
375 Py_XDECREF(tuple);
376 return NULL;
377 }
378 Py_DECREF(item);
379 Py_DECREF(tuple);
380 }
381 }
382 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000383}
384
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000385/* Begin: Peephole optimizations ----------------------------------------- */
386
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000387#define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1]))
388#define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD)
Raymond Hettinger5b75c382003-03-28 12:05:00 +0000389#define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP)
390#define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000391#define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000392#define CODESIZE(op) (HAS_ARG(op) ? 3 : 1)
393#define ISBASICBLOCK(blocks, start, bytes) (blocks[start]==blocks[start+bytes-1])
394
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000395/* Replace LOAD_CONST c1. LOAD_CONST c2 ... LOAD_CONST cn BUILD_TUPLE n
396 with LOAD_CONST (c1, c2, ... cn).
397 The consts table must still be in list form so that the
398 new constant (c1, c2, ... cn) can be appended.
399 Called with codestr pointing to the first LOAD_CONST.
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000400 Bails out with no change if one or more of the LOAD_CONSTs is missing.
401 Also works for BUILD_LIST when followed by an "in" or "not in" test.
402*/
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000403static int
404tuple_of_constants(unsigned char *codestr, int n, PyObject *consts)
405{
406 PyObject *newconst, *constant;
407 int i, arg, len_consts;
408
409 /* Pre-conditions */
410 assert(PyList_CheckExact(consts));
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000411 assert(codestr[n*3] == BUILD_TUPLE || codestr[n*3] == BUILD_LIST);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000412 assert(GETARG(codestr, (n*3)) == n);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000413 for (i=0 ; i<n ; i++)
Raymond Hettingereffb3932004-10-30 08:55:08 +0000414 assert(codestr[i*3] == LOAD_CONST);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000415
416 /* Buildup new tuple of constants */
417 newconst = PyTuple_New(n);
418 if (newconst == NULL)
419 return 0;
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000420 len_consts = PyList_GET_SIZE(consts);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000421 for (i=0 ; i<n ; i++) {
422 arg = GETARG(codestr, (i*3));
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000423 assert(arg < len_consts);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000424 constant = PyList_GET_ITEM(consts, arg);
425 Py_INCREF(constant);
426 PyTuple_SET_ITEM(newconst, i, constant);
427 }
428
429 /* Append folded constant onto consts */
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000430 if (PyList_Append(consts, newconst)) {
431 Py_DECREF(newconst);
432 return 0;
433 }
434 Py_DECREF(newconst);
435
436 /* Write NOPs over old LOAD_CONSTS and
437 add a new LOAD_CONST newconst on top of the BUILD_TUPLE n */
438 memset(codestr, NOP, n*3);
439 codestr[n*3] = LOAD_CONST;
440 SETARG(codestr, (n*3), len_consts);
441 return 1;
442}
443
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000444/* Replace LOAD_CONST c1. LOAD_CONST c2 BINOP
445 with LOAD_CONST binop(c1,c2)
446 The consts table must still be in list form so that the
447 new constant can be appended.
448 Called with codestr pointing to the first LOAD_CONST.
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000449 Abandons the transformation if the folding fails (i.e. 1+'a').
450 If the new constant is a sequence, only folds when the size
451 is below a threshold value. That keeps pyc files from
452 becoming large in the presence of code like: (None,)*1000.
453*/
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000454static int
455fold_binops_on_constants(unsigned char *codestr, PyObject *consts)
456{
457 PyObject *newconst, *v, *w;
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000458 int len_consts, opcode, size;
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000459
460 /* Pre-conditions */
461 assert(PyList_CheckExact(consts));
462 assert(codestr[0] == LOAD_CONST);
463 assert(codestr[3] == LOAD_CONST);
464
465 /* Create new constant */
466 v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
467 w = PyList_GET_ITEM(consts, GETARG(codestr, 3));
468 opcode = codestr[6];
469 switch (opcode) {
470 case BINARY_POWER:
471 newconst = PyNumber_Power(v, w, Py_None);
472 break;
473 case BINARY_MULTIPLY:
474 newconst = PyNumber_Multiply(v, w);
475 break;
476 case BINARY_DIVIDE:
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000477 /* Cannot fold this operation statically since
478 the result can depend on the run-time presence of the -Qnew flag */
Armin Rigo664b43b2005-01-07 18:10:51 +0000479 return 0;
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000480 case BINARY_TRUE_DIVIDE:
481 newconst = PyNumber_TrueDivide(v, w);
482 break;
483 case BINARY_FLOOR_DIVIDE:
484 newconst = PyNumber_FloorDivide(v, w);
485 break;
486 case BINARY_MODULO:
487 newconst = PyNumber_Remainder(v, w);
488 break;
489 case BINARY_ADD:
490 newconst = PyNumber_Add(v, w);
491 break;
492 case BINARY_SUBTRACT:
493 newconst = PyNumber_Subtract(v, w);
494 break;
495 case BINARY_SUBSCR:
496 newconst = PyObject_GetItem(v, w);
497 break;
498 case BINARY_LSHIFT:
499 newconst = PyNumber_Lshift(v, w);
500 break;
501 case BINARY_RSHIFT:
502 newconst = PyNumber_Rshift(v, w);
503 break;
504 case BINARY_AND:
505 newconst = PyNumber_And(v, w);
506 break;
507 case BINARY_XOR:
508 newconst = PyNumber_Xor(v, w);
509 break;
510 case BINARY_OR:
511 newconst = PyNumber_Or(v, w);
512 break;
513 default:
514 /* Called with an unknown opcode */
Neal Norwitz4737b232005-11-19 23:58:29 +0000515 PyErr_Format(PyExc_SystemError,
516 "unexpected binary operation %d on a constant",
517 opcode);
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000518 return 0;
519 }
520 if (newconst == NULL) {
521 PyErr_Clear();
522 return 0;
523 }
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000524 size = PyObject_Size(newconst);
525 if (size == -1)
526 PyErr_Clear();
527 else if (size > 20) {
528 Py_DECREF(newconst);
529 return 0;
530 }
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000531
532 /* Append folded constant into consts table */
533 len_consts = PyList_GET_SIZE(consts);
534 if (PyList_Append(consts, newconst)) {
535 Py_DECREF(newconst);
536 return 0;
537 }
538 Py_DECREF(newconst);
539
540 /* Write NOP NOP NOP NOP LOAD_CONST newconst */
541 memset(codestr, NOP, 4);
542 codestr[4] = LOAD_CONST;
543 SETARG(codestr, 4, len_consts);
544 return 1;
545}
546
Raymond Hettinger80121492005-02-20 12:41:32 +0000547static int
548fold_unaryops_on_constants(unsigned char *codestr, PyObject *consts)
549{
Raymond Hettingere63a0782005-02-23 13:37:55 +0000550 PyObject *newconst=NULL, *v;
Raymond Hettinger80121492005-02-20 12:41:32 +0000551 int len_consts, opcode;
552
553 /* Pre-conditions */
554 assert(PyList_CheckExact(consts));
555 assert(codestr[0] == LOAD_CONST);
556
557 /* Create new constant */
558 v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
559 opcode = codestr[3];
560 switch (opcode) {
561 case UNARY_NEGATIVE:
Raymond Hettingere63a0782005-02-23 13:37:55 +0000562 /* Preserve the sign of -0.0 */
563 if (PyObject_IsTrue(v) == 1)
564 newconst = PyNumber_Negative(v);
Raymond Hettinger80121492005-02-20 12:41:32 +0000565 break;
566 case UNARY_CONVERT:
567 newconst = PyObject_Repr(v);
568 break;
569 case UNARY_INVERT:
570 newconst = PyNumber_Invert(v);
571 break;
572 default:
573 /* Called with an unknown opcode */
Neal Norwitz4737b232005-11-19 23:58:29 +0000574 PyErr_Format(PyExc_SystemError,
575 "unexpected unary operation %d on a constant",
576 opcode);
Raymond Hettinger80121492005-02-20 12:41:32 +0000577 return 0;
578 }
579 if (newconst == NULL) {
580 PyErr_Clear();
581 return 0;
582 }
583
584 /* Append folded constant into consts table */
585 len_consts = PyList_GET_SIZE(consts);
586 if (PyList_Append(consts, newconst)) {
587 Py_DECREF(newconst);
588 return 0;
589 }
590 Py_DECREF(newconst);
591
592 /* Write NOP LOAD_CONST newconst */
593 codestr[0] = NOP;
594 codestr[1] = LOAD_CONST;
595 SETARG(codestr, 1, len_consts);
596 return 1;
597}
598
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000599static unsigned int *
600markblocks(unsigned char *code, int len)
601{
602 unsigned int *blocks = PyMem_Malloc(len*sizeof(int));
Raymond Hettingereffb3932004-10-30 08:55:08 +0000603 int i,j, opcode, blockcnt = 0;
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000604
605 if (blocks == NULL)
606 return NULL;
607 memset(blocks, 0, len*sizeof(int));
Raymond Hettingereffb3932004-10-30 08:55:08 +0000608
609 /* Mark labels in the first pass */
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000610 for (i=0 ; i<len ; i+=CODESIZE(opcode)) {
611 opcode = code[i];
612 switch (opcode) {
613 case FOR_ITER:
614 case JUMP_FORWARD:
615 case JUMP_IF_FALSE:
616 case JUMP_IF_TRUE:
617 case JUMP_ABSOLUTE:
618 case CONTINUE_LOOP:
619 case SETUP_LOOP:
620 case SETUP_EXCEPT:
621 case SETUP_FINALLY:
622 j = GETJUMPTGT(code, i);
Raymond Hettingereffb3932004-10-30 08:55:08 +0000623 blocks[j] = 1;
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000624 break;
625 }
626 }
Raymond Hettingereffb3932004-10-30 08:55:08 +0000627 /* Build block numbers in the second pass */
628 for (i=0 ; i<len ; i++) {
629 blockcnt += blocks[i]; /* increment blockcnt over labels */
630 blocks[i] = blockcnt;
631 }
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000632 return blocks;
633}
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000634
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000635/* Perform basic peephole optimizations to components of a code object.
636 The consts object should still be in list form to allow new constants
637 to be appended.
638
639 To keep the optimizer simple, it bails out (does nothing) for code
640 containing extended arguments or that has a length over 32,700. That
641 allows us to avoid overflow and sign issues. Likewise, it bails when
642 the lineno table has complex encoding for gaps >= 255.
643
644 Optimizations are restricted to simple transformations occuring within a
645 single basic block. All transformations keep the code size the same or
646 smaller. For those that reduce size, the gaps are initially filled with
647 NOPs. Later those NOPs are removed and the jump addresses retargeted in
648 a single pass. Line numbering is adjusted accordingly. */
649
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000650static PyObject *
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000651optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *lineno_obj)
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000652{
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000653 int i, j, codelen, nops, h, adj;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000654 int tgt, tgttgt, opcode;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000655 unsigned char *codestr = NULL;
656 unsigned char *lineno;
657 int *addrmap = NULL;
658 int new_line, cum_orig_line, last_line, tabsiz;
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000659 int cumlc=0, lastlc=0; /* Count runs of consecutive LOAD_CONST codes */
Raymond Hettingereffb3932004-10-30 08:55:08 +0000660 unsigned int *blocks = NULL;
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000661 char *name;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000662
Raymond Hettingereffb3932004-10-30 08:55:08 +0000663 /* Bail out if an exception is set */
664 if (PyErr_Occurred())
665 goto exitUnchanged;
666
Raymond Hettinger1792bfb2004-08-25 17:19:38 +0000667 /* Bypass optimization when the lineno table is too complex */
668 assert(PyString_Check(lineno_obj));
Brett Cannonc9371d42005-06-25 08:23:41 +0000669 lineno = (unsigned char*)PyString_AS_STRING(lineno_obj);
Raymond Hettinger1792bfb2004-08-25 17:19:38 +0000670 tabsiz = PyString_GET_SIZE(lineno_obj);
671 if (memchr(lineno, 255, tabsiz) != NULL)
672 goto exitUnchanged;
673
Raymond Hettingera12fa142004-08-24 04:34:16 +0000674 /* Avoid situations where jump retargeting could overflow */
Raymond Hettinger06cc9732004-09-28 17:22:12 +0000675 assert(PyString_Check(code));
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000676 codelen = PyString_Size(code);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000677 if (codelen > 32700)
Raymond Hettingera12fa142004-08-24 04:34:16 +0000678 goto exitUnchanged;
679
680 /* Make a modifiable copy of the code string */
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000681 codestr = PyMem_Malloc(codelen);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000682 if (codestr == NULL)
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000683 goto exitUnchanged;
684 codestr = memcpy(codestr, PyString_AS_STRING(code), codelen);
Raymond Hettinger98bd1812004-08-06 19:46:34 +0000685
Raymond Hettinger07359a72005-02-21 20:03:14 +0000686 /* Verify that RETURN_VALUE terminates the codestring. This allows
687 the various transformation patterns to look ahead several
688 instructions without additional checks to make sure they are not
689 looking beyond the end of the code string.
690 */
691 if (codestr[codelen-1] != RETURN_VALUE)
692 goto exitUnchanged;
693
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000694 /* Mapping to new jump targets after NOPs are removed */
695 addrmap = PyMem_Malloc(codelen * sizeof(int));
696 if (addrmap == NULL)
697 goto exitUnchanged;
698
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000699 blocks = markblocks(codestr, codelen);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000700 if (blocks == NULL)
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000701 goto exitUnchanged;
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000702 assert(PyList_Check(consts));
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000703
Raymond Hettinger099ecfb2004-11-01 15:19:11 +0000704 for (i=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000705 opcode = codestr[i];
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000706
707 lastlc = cumlc;
708 cumlc = 0;
709
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000710 switch (opcode) {
711
Raymond Hettinger43ea47f2004-06-24 09:25:39 +0000712 /* Replace UNARY_NOT JUMP_IF_FALSE POP_TOP with
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000713 with JUMP_IF_TRUE POP_TOP */
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000714 case UNARY_NOT:
715 if (codestr[i+1] != JUMP_IF_FALSE ||
716 codestr[i+4] != POP_TOP ||
717 !ISBASICBLOCK(blocks,i,5))
718 continue;
719 tgt = GETJUMPTGT(codestr, (i+1));
720 if (codestr[tgt] != POP_TOP)
721 continue;
Raymond Hettinger43ea47f2004-06-24 09:25:39 +0000722 j = GETARG(codestr, i+1) + 1;
723 codestr[i] = JUMP_IF_TRUE;
724 SETARG(codestr, i, j);
725 codestr[i+3] = POP_TOP;
726 codestr[i+4] = NOP;
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000727 break;
728
729 /* not a is b --> a is not b
730 not a in b --> a not in b
731 not a is not b --> a is b
Raymond Hettingerb615bf02005-02-10 01:42:32 +0000732 not a not in b --> a in b
Raymond Hettingera1645742005-02-06 22:05:42 +0000733 */
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000734 case COMPARE_OP:
735 j = GETARG(codestr, i);
736 if (j < 6 || j > 9 ||
737 codestr[i+3] != UNARY_NOT ||
738 !ISBASICBLOCK(blocks,i,4))
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000739 continue;
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000740 SETARG(codestr, i, (j^1));
741 codestr[i+3] = NOP;
Tim Petersdb5860b2004-07-17 05:00:52 +0000742 break;
743
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000744 /* Replace LOAD_GLOBAL/LOAD_NAME None with LOAD_CONST None */
745 case LOAD_NAME:
746 case LOAD_GLOBAL:
747 j = GETARG(codestr, i);
748 name = PyString_AsString(PyTuple_GET_ITEM(names, j));
749 if (name == NULL || strcmp(name, "None") != 0)
750 continue;
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000751 for (j=0 ; j < PyList_GET_SIZE(consts) ; j++) {
752 if (PyList_GET_ITEM(consts, j) == Py_None) {
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000753 codestr[i] = LOAD_CONST;
754 SETARG(codestr, i, j);
Raymond Hettinger5dec0962004-11-02 04:20:10 +0000755 cumlc = lastlc + 1;
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000756 break;
757 }
758 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000759 break;
760
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000761 /* Skip over LOAD_CONST trueconst JUMP_IF_FALSE xx POP_TOP */
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000762 case LOAD_CONST:
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000763 cumlc = lastlc + 1;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000764 j = GETARG(codestr, i);
765 if (codestr[i+3] != JUMP_IF_FALSE ||
766 codestr[i+6] != POP_TOP ||
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000767 !ISBASICBLOCK(blocks,i,7) ||
768 !PyObject_IsTrue(PyList_GET_ITEM(consts, j)))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000769 continue;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000770 memset(codestr+i, NOP, 7);
Raymond Hettinger5dec0962004-11-02 04:20:10 +0000771 cumlc = 0;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000772 break;
773
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000774 /* Try to fold tuples of constants (includes a case for lists
775 which are only used for "in" and "not in" tests).
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000776 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000777 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
778 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000779 case BUILD_TUPLE:
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000780 case BUILD_LIST:
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000781 j = GETARG(codestr, i);
782 h = i - 3 * j;
783 if (h >= 0 &&
Raymond Hettingereffb3932004-10-30 08:55:08 +0000784 j <= lastlc &&
Brett Cannon5dc8ced2005-03-03 07:01:48 +0000785 ((opcode == BUILD_TUPLE &&
786 ISBASICBLOCK(blocks, h, 3*(j+1))) ||
787 (opcode == BUILD_LIST &&
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000788 codestr[i+3]==COMPARE_OP &&
789 ISBASICBLOCK(blocks, h, 3*(j+2)) &&
Brett Cannon5dc8ced2005-03-03 07:01:48 +0000790 (GETARG(codestr,i+3)==6 ||
791 GETARG(codestr,i+3)==7))) &&
792 tuple_of_constants(&codestr[h], j, consts)) {
Raymond Hettinger5dec0962004-11-02 04:20:10 +0000793 assert(codestr[i] == LOAD_CONST);
794 cumlc = 1;
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000795 break;
796 }
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000797 if (codestr[i+3] != UNPACK_SEQUENCE ||
798 !ISBASICBLOCK(blocks,i,6) ||
799 j != GETARG(codestr, i+3))
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000800 continue;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000801 if (j == 1) {
802 memset(codestr+i, NOP, 6);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000803 } else if (j == 2) {
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000804 codestr[i] = ROT_TWO;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000805 memset(codestr+i+1, NOP, 5);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000806 } else if (j == 3) {
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000807 codestr[i] = ROT_THREE;
808 codestr[i+1] = ROT_TWO;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000809 memset(codestr+i+2, NOP, 4);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000810 }
811 break;
812
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000813 /* Fold binary ops on constants.
814 LOAD_CONST c1 LOAD_CONST c2 BINOP --> LOAD_CONST binop(c1,c2) */
815 case BINARY_POWER:
816 case BINARY_MULTIPLY:
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000817 case BINARY_TRUE_DIVIDE:
818 case BINARY_FLOOR_DIVIDE:
819 case BINARY_MODULO:
820 case BINARY_ADD:
821 case BINARY_SUBTRACT:
822 case BINARY_SUBSCR:
823 case BINARY_LSHIFT:
824 case BINARY_RSHIFT:
825 case BINARY_AND:
826 case BINARY_XOR:
827 case BINARY_OR:
828 if (lastlc >= 2 &&
829 ISBASICBLOCK(blocks, i-6, 7) &&
830 fold_binops_on_constants(&codestr[i-6], consts)) {
831 i -= 2;
832 assert(codestr[i] == LOAD_CONST);
833 cumlc = 1;
834 }
835 break;
836
Raymond Hettinger80121492005-02-20 12:41:32 +0000837 /* Fold unary ops on constants.
838 LOAD_CONST c1 UNARY_OP --> LOAD_CONST unary_op(c) */
839 case UNARY_NEGATIVE:
840 case UNARY_CONVERT:
841 case UNARY_INVERT:
842 if (lastlc >= 1 &&
843 ISBASICBLOCK(blocks, i-3, 4) &&
844 fold_unaryops_on_constants(&codestr[i-3], consts)) {
845 i -= 2;
846 assert(codestr[i] == LOAD_CONST);
847 cumlc = 1;
848 }
849 break;
850
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000851 /* Simplify conditional jump to conditional jump where the
852 result of the first test implies the success of a similar
853 test or the failure of the opposite test.
854 Arises in code like:
Raymond Hettingereffb3932004-10-30 08:55:08 +0000855 "if a and b:"
856 "if a or b:"
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000857 "a and b or c"
Armin Rigod7bcf4d2004-10-30 21:08:59 +0000858 "(a and b) and c"
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000859 x:JUMP_IF_FALSE y y:JUMP_IF_FALSE z --> x:JUMP_IF_FALSE z
Raymond Hettinger65d3c052004-08-25 15:15:56 +0000860 x:JUMP_IF_FALSE y y:JUMP_IF_TRUE z --> x:JUMP_IF_FALSE y+3
861 where y+3 is the instruction following the second test.
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000862 */
863 case JUMP_IF_FALSE:
864 case JUMP_IF_TRUE:
865 tgt = GETJUMPTGT(codestr, i);
866 j = codestr[tgt];
867 if (j == JUMP_IF_FALSE || j == JUMP_IF_TRUE) {
868 if (j == opcode) {
869 tgttgt = GETJUMPTGT(codestr, tgt) - i - 3;
870 SETARG(codestr, i, tgttgt);
871 } else {
872 tgt -= i;
873 SETARG(codestr, i, tgt);
874 }
875 break;
876 }
877 /* Intentional fallthrough */
878
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000879 /* Replace jumps to unconditional jumps */
Raymond Hettinger255a3d02003-04-15 10:35:07 +0000880 case FOR_ITER:
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000881 case JUMP_FORWARD:
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000882 case JUMP_ABSOLUTE:
883 case CONTINUE_LOOP:
884 case SETUP_LOOP:
885 case SETUP_EXCEPT:
886 case SETUP_FINALLY:
887 tgt = GETJUMPTGT(codestr, i);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000888 if (!UNCONDITIONAL_JUMP(codestr[tgt]))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000889 continue;
890 tgttgt = GETJUMPTGT(codestr, tgt);
891 if (opcode == JUMP_FORWARD) /* JMP_ABS can go backwards */
892 opcode = JUMP_ABSOLUTE;
Raymond Hettinger5b75c382003-03-28 12:05:00 +0000893 if (!ABSOLUTE_JUMP(opcode))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000894 tgttgt -= i + 3; /* Calc relative jump addr */
895 if (tgttgt < 0) /* No backward relative jumps */
896 continue;
897 codestr[i] = opcode;
898 SETARG(codestr, i, tgttgt);
899 break;
900
901 case EXTENDED_ARG:
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000902 goto exitUnchanged;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000903
904 /* Replace RETURN LOAD_CONST None RETURN with just RETURN */
905 case RETURN_VALUE:
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000906 if (i+4 >= codelen ||
907 codestr[i+4] != RETURN_VALUE ||
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000908 !ISBASICBLOCK(blocks,i,5))
909 continue;
910 memset(codestr+i+1, NOP, 4);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000911 break;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000912 }
913 }
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000914
915 /* Fixup linenotab */
Raymond Hettinger099ecfb2004-11-01 15:19:11 +0000916 for (i=0, nops=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
917 addrmap[i] = i - nops;
918 if (codestr[i] == NOP)
919 nops++;
920 }
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000921 cum_orig_line = 0;
922 last_line = 0;
923 for (i=0 ; i < tabsiz ; i+=2) {
924 cum_orig_line += lineno[i];
925 new_line = addrmap[cum_orig_line];
Raymond Hettinger1792bfb2004-08-25 17:19:38 +0000926 assert (new_line - last_line < 255);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000927 lineno[i] =((unsigned char)(new_line - last_line));
928 last_line = new_line;
929 }
930
931 /* Remove NOPs and fixup jump targets */
932 for (i=0, h=0 ; i<codelen ; ) {
933 opcode = codestr[i];
934 switch (opcode) {
935 case NOP:
936 i++;
937 continue;
938
939 case JUMP_ABSOLUTE:
940 case CONTINUE_LOOP:
941 j = addrmap[GETARG(codestr, i)];
942 SETARG(codestr, i, j);
943 break;
944
945 case FOR_ITER:
946 case JUMP_FORWARD:
947 case JUMP_IF_FALSE:
948 case JUMP_IF_TRUE:
949 case SETUP_LOOP:
950 case SETUP_EXCEPT:
951 case SETUP_FINALLY:
952 j = addrmap[GETARG(codestr, i) + i + 3] - addrmap[i] - 3;
953 SETARG(codestr, i, j);
954 break;
955 }
956 adj = CODESIZE(opcode);
957 while (adj--)
958 codestr[h++] = codestr[i++];
959 }
Raymond Hettingera12fa142004-08-24 04:34:16 +0000960 assert(h + nops == codelen);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000961
962 code = PyString_FromStringAndSize((char *)codestr, h);
963 PyMem_Free(addrmap);
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000964 PyMem_Free(codestr);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000965 PyMem_Free(blocks);
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000966 return code;
967
968exitUnchanged:
Raymond Hettingereffb3932004-10-30 08:55:08 +0000969 if (blocks != NULL)
970 PyMem_Free(blocks);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000971 if (addrmap != NULL)
972 PyMem_Free(addrmap);
973 if (codestr != NULL)
974 PyMem_Free(codestr);
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000975 Py_INCREF(code);
976 return code;
977}
978
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000979/* End: Peephole optimizations ----------------------------------------- */
980
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981/*
982
983Leave this debugging code for just a little longer.
984
985static void
986compiler_display_symbols(PyObject *name, PyObject *symbols)
987{
988 PyObject *key, *value;
989 int flags, pos = 0;
990
991 fprintf(stderr, "block %s\n", PyString_AS_STRING(name));
992 while (PyDict_Next(symbols, &pos, &key, &value)) {
993 flags = PyInt_AsLong(value);
994 fprintf(stderr, "var %s:", PyString_AS_STRING(key));
995 if (flags & DEF_GLOBAL)
996 fprintf(stderr, " declared_global");
997 if (flags & DEF_LOCAL)
998 fprintf(stderr, " local");
999 if (flags & DEF_PARAM)
1000 fprintf(stderr, " param");
1001 if (flags & DEF_STAR)
1002 fprintf(stderr, " stararg");
1003 if (flags & DEF_DOUBLESTAR)
1004 fprintf(stderr, " starstar");
1005 if (flags & DEF_INTUPLE)
1006 fprintf(stderr, " tuple");
1007 if (flags & DEF_FREE)
1008 fprintf(stderr, " free");
1009 if (flags & DEF_FREE_GLOBAL)
1010 fprintf(stderr, " global");
1011 if (flags & DEF_FREE_CLASS)
1012 fprintf(stderr, " free/class");
1013 if (flags & DEF_IMPORT)
1014 fprintf(stderr, " import");
1015 fprintf(stderr, "\n");
1016 }
1017 fprintf(stderr, "\n");
1018}
1019*/
1020
1021static void
1022compiler_unit_check(struct compiler_unit *u)
1023{
1024 basicblock *block;
1025 for (block = u->u_blocks; block != NULL; block = block->b_list) {
1026 assert(block != (void *)0xcbcbcbcb);
1027 assert(block != (void *)0xfbfbfbfb);
1028 assert(block != (void *)0xdbdbdbdb);
1029 if (block->b_instr != NULL) {
1030 assert(block->b_ialloc > 0);
1031 assert(block->b_iused > 0);
1032 assert(block->b_ialloc >= block->b_iused);
1033 }
1034 else {
1035 assert (block->b_iused == 0);
1036 assert (block->b_ialloc == 0);
1037 }
1038 }
1039}
1040
1041static void
1042compiler_unit_free(struct compiler_unit *u)
1043{
1044 basicblock *b, *next;
1045
1046 compiler_unit_check(u);
1047 b = u->u_blocks;
1048 while (b != NULL) {
1049 if (b->b_instr)
1050 PyObject_Free((void *)b->b_instr);
1051 next = b->b_list;
1052 PyObject_Free((void *)b);
1053 b = next;
1054 }
1055 Py_XDECREF(u->u_ste);
1056 Py_XDECREF(u->u_name);
1057 Py_XDECREF(u->u_consts);
1058 Py_XDECREF(u->u_names);
1059 Py_XDECREF(u->u_varnames);
1060 Py_XDECREF(u->u_freevars);
1061 Py_XDECREF(u->u_cellvars);
1062 Py_XDECREF(u->u_private);
1063 PyObject_Free(u);
1064}
1065
1066static int
1067compiler_enter_scope(struct compiler *c, identifier name, void *key,
1068 int lineno)
1069{
1070 struct compiler_unit *u;
1071
1072 u = PyObject_Malloc(sizeof(struct compiler_unit));
1073 memset(u, 0, sizeof(struct compiler_unit));
1074 u->u_argcount = 0;
1075 u->u_ste = PySymtable_Lookup(c->c_st, key);
1076 if (!u->u_ste) {
1077 compiler_unit_free(u);
1078 return 0;
1079 }
1080 Py_INCREF(name);
1081 u->u_name = name;
1082 u->u_varnames = list2dict(u->u_ste->ste_varnames);
1083 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
1084 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
1085 PyDict_Size(u->u_cellvars));
1086
1087 u->u_blocks = NULL;
1088 u->u_tmpname = 0;
1089 u->u_nfblocks = 0;
1090 u->u_firstlineno = lineno;
1091 u->u_lineno = 0;
1092 u->u_lineno_set = false;
1093 u->u_consts = PyDict_New();
1094 if (!u->u_consts) {
1095 compiler_unit_free(u);
1096 return 0;
1097 }
1098 u->u_names = PyDict_New();
1099 if (!u->u_names) {
1100 compiler_unit_free(u);
1101 return 0;
1102 }
1103
1104 u->u_private = NULL;
1105
1106 /* Push the old compiler_unit on the stack. */
1107 if (c->u) {
1108 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
1109 if (PyList_Append(c->c_stack, wrapper) < 0) {
1110 compiler_unit_free(u);
1111 return 0;
1112 }
1113 Py_DECREF(wrapper);
1114 u->u_private = c->u->u_private;
1115 Py_XINCREF(u->u_private);
1116 }
1117 c->u = u;
1118
1119 c->c_nestlevel++;
1120 if (compiler_use_new_block(c) < 0)
1121 return 0;
1122
1123 return 1;
1124}
1125
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001126static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127compiler_exit_scope(struct compiler *c)
1128{
1129 int n;
1130 PyObject *wrapper;
1131
1132 c->c_nestlevel--;
1133 compiler_unit_free(c->u);
1134 /* Restore c->u to the parent unit. */
1135 n = PyList_GET_SIZE(c->c_stack) - 1;
1136 if (n >= 0) {
1137 wrapper = PyList_GET_ITEM(c->c_stack, n);
1138 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001139 /* we are deleting from a list so this really shouldn't fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001140 if (PySequence_DelItem(c->c_stack, n) < 0)
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001141 Py_FatalError("compiler_exit_scope()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142 compiler_unit_check(c->u);
1143 }
1144 else
1145 c->u = NULL;
1146
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147}
1148
1149/* Allocate a new block and return a pointer to it.
1150 Returns NULL on error.
1151*/
1152
1153static basicblock *
1154compiler_new_block(struct compiler *c)
1155{
1156 basicblock *b;
1157 struct compiler_unit *u;
1158
1159 u = c->u;
1160 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
1161 if (b == NULL)
1162 return NULL;
1163 memset((void *)b, 0, sizeof(basicblock));
1164 assert (b->b_next == NULL);
1165 b->b_list = u->u_blocks;
1166 u->u_blocks = b;
1167 return b;
1168}
1169
1170static void
1171compiler_use_block(struct compiler *c, basicblock *block)
1172{
1173 assert (block != NULL);
1174 c->u->u_curblock = block;
1175}
1176
1177static basicblock *
1178compiler_use_new_block(struct compiler *c)
1179{
1180 basicblock *block = compiler_new_block(c);
1181 if (block == NULL)
1182 return NULL;
1183 c->u->u_curblock = block;
1184 return block;
1185}
1186
1187static basicblock *
1188compiler_next_block(struct compiler *c)
1189{
1190 basicblock *block = compiler_new_block(c);
1191 if (block == NULL)
1192 return NULL;
1193 c->u->u_curblock->b_next = block;
1194 c->u->u_curblock = block;
1195 return block;
1196}
1197
1198static basicblock *
1199compiler_use_next_block(struct compiler *c, basicblock *block)
1200{
1201 assert(block != NULL);
1202 c->u->u_curblock->b_next = block;
1203 c->u->u_curblock = block;
1204 return block;
1205}
1206
1207/* Returns the offset of the next instruction in the current block's
1208 b_instr array. Resizes the b_instr as necessary.
1209 Returns -1 on failure.
1210 */
1211
1212static int
1213compiler_next_instr(struct compiler *c, basicblock *b)
1214{
1215 assert(b != NULL);
1216 if (b->b_instr == NULL) {
1217 b->b_instr = PyObject_Malloc(sizeof(struct instr) *
1218 DEFAULT_BLOCK_SIZE);
1219 if (b->b_instr == NULL) {
1220 PyErr_NoMemory();
1221 return -1;
1222 }
1223 b->b_ialloc = DEFAULT_BLOCK_SIZE;
1224 memset((char *)b->b_instr, 0,
1225 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
1226 }
1227 else if (b->b_iused == b->b_ialloc) {
1228 size_t oldsize, newsize;
1229 oldsize = b->b_ialloc * sizeof(struct instr);
1230 newsize = oldsize << 1;
1231 if (newsize == 0) {
1232 PyErr_NoMemory();
1233 return -1;
1234 }
1235 b->b_ialloc <<= 1;
1236 b->b_instr = PyObject_Realloc((void *)b->b_instr, newsize);
1237 if (b->b_instr == NULL)
1238 return -1;
1239 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
1240 }
1241 return b->b_iused++;
1242}
1243
1244static void
1245compiler_set_lineno(struct compiler *c, int off)
1246{
1247 basicblock *b;
1248 if (c->u->u_lineno_set)
1249 return;
1250 c->u->u_lineno_set = true;
1251 b = c->u->u_curblock;
1252 b->b_instr[off].i_lineno = c->u->u_lineno;
1253}
1254
1255static int
1256opcode_stack_effect(int opcode, int oparg)
1257{
1258 switch (opcode) {
1259 case POP_TOP:
1260 return -1;
1261 case ROT_TWO:
1262 case ROT_THREE:
1263 return 0;
1264 case DUP_TOP:
1265 return 1;
1266 case ROT_FOUR:
1267 return 0;
1268
1269 case UNARY_POSITIVE:
1270 case UNARY_NEGATIVE:
1271 case UNARY_NOT:
1272 case UNARY_CONVERT:
1273 case UNARY_INVERT:
1274 return 0;
1275
1276 case BINARY_POWER:
1277 case BINARY_MULTIPLY:
1278 case BINARY_DIVIDE:
1279 case BINARY_MODULO:
1280 case BINARY_ADD:
1281 case BINARY_SUBTRACT:
1282 case BINARY_SUBSCR:
1283 case BINARY_FLOOR_DIVIDE:
1284 case BINARY_TRUE_DIVIDE:
1285 return -1;
1286 case INPLACE_FLOOR_DIVIDE:
1287 case INPLACE_TRUE_DIVIDE:
1288 return -1;
1289
1290 case SLICE+0:
1291 return 1;
1292 case SLICE+1:
1293 return 0;
1294 case SLICE+2:
1295 return 0;
1296 case SLICE+3:
1297 return -1;
1298
1299 case STORE_SLICE+0:
1300 return -2;
1301 case STORE_SLICE+1:
1302 return -3;
1303 case STORE_SLICE+2:
1304 return -3;
1305 case STORE_SLICE+3:
1306 return -4;
1307
1308 case DELETE_SLICE+0:
1309 return -1;
1310 case DELETE_SLICE+1:
1311 return -2;
1312 case DELETE_SLICE+2:
1313 return -2;
1314 case DELETE_SLICE+3:
1315 return -3;
1316
1317 case INPLACE_ADD:
1318 case INPLACE_SUBTRACT:
1319 case INPLACE_MULTIPLY:
1320 case INPLACE_DIVIDE:
1321 case INPLACE_MODULO:
1322 return -1;
1323 case STORE_SUBSCR:
1324 return -3;
1325 case DELETE_SUBSCR:
1326 return -2;
1327
1328 case BINARY_LSHIFT:
1329 case BINARY_RSHIFT:
1330 case BINARY_AND:
1331 case BINARY_XOR:
1332 case BINARY_OR:
1333 return -1;
1334 case INPLACE_POWER:
1335 return -1;
1336 case GET_ITER:
1337 return 0;
1338
1339 case PRINT_EXPR:
1340 return -1;
1341 case PRINT_ITEM:
1342 return -1;
1343 case PRINT_NEWLINE:
1344 return 0;
1345 case PRINT_ITEM_TO:
1346 return -2;
1347 case PRINT_NEWLINE_TO:
1348 return -1;
1349 case INPLACE_LSHIFT:
1350 case INPLACE_RSHIFT:
1351 case INPLACE_AND:
1352 case INPLACE_XOR:
1353 case INPLACE_OR:
1354 return -1;
1355 case BREAK_LOOP:
1356 return 0;
1357
1358 case LOAD_LOCALS:
1359 return 1;
1360 case RETURN_VALUE:
1361 return -1;
1362 case IMPORT_STAR:
1363 return -1;
1364 case EXEC_STMT:
1365 return -3;
1366 case YIELD_VALUE:
1367 return 0;
1368
1369 case POP_BLOCK:
1370 return 0;
1371 case END_FINALLY:
1372 return -1; /* or -2 or -3 if exception occurred */
1373 case BUILD_CLASS:
1374 return -2;
1375
1376 case STORE_NAME:
1377 return -1;
1378 case DELETE_NAME:
1379 return 0;
1380 case UNPACK_SEQUENCE:
1381 return oparg-1;
1382 case FOR_ITER:
1383 return 1;
1384
1385 case STORE_ATTR:
1386 return -2;
1387 case DELETE_ATTR:
1388 return -1;
1389 case STORE_GLOBAL:
1390 return -1;
1391 case DELETE_GLOBAL:
1392 return 0;
1393 case DUP_TOPX:
1394 return oparg;
1395 case LOAD_CONST:
1396 return 1;
1397 case LOAD_NAME:
1398 return 1;
1399 case BUILD_TUPLE:
1400 case BUILD_LIST:
1401 return 1-oparg;
1402 case BUILD_MAP:
1403 return 1;
1404 case LOAD_ATTR:
1405 return 0;
1406 case COMPARE_OP:
1407 return -1;
1408 case IMPORT_NAME:
1409 return 0;
1410 case IMPORT_FROM:
1411 return 1;
1412
1413 case JUMP_FORWARD:
1414 case JUMP_IF_FALSE:
1415 case JUMP_IF_TRUE:
1416 case JUMP_ABSOLUTE:
1417 return 0;
1418
1419 case LOAD_GLOBAL:
1420 return 1;
1421
1422 case CONTINUE_LOOP:
1423 return 0;
1424 case SETUP_LOOP:
1425 return 0;
1426 case SETUP_EXCEPT:
1427 case SETUP_FINALLY:
1428 return 3; /* actually pushed by an exception */
1429
1430 case LOAD_FAST:
1431 return 1;
1432 case STORE_FAST:
1433 return -1;
1434 case DELETE_FAST:
1435 return 0;
1436
1437 case RAISE_VARARGS:
1438 return -oparg;
1439#define NARGS(o) (((o) % 256) + 2*((o) / 256))
1440 case CALL_FUNCTION:
1441 return -NARGS(oparg);
1442 case CALL_FUNCTION_VAR:
1443 case CALL_FUNCTION_KW:
1444 return -NARGS(oparg)-1;
1445 case CALL_FUNCTION_VAR_KW:
1446 return -NARGS(oparg)-2;
1447#undef NARGS
1448 case MAKE_FUNCTION:
1449 return -oparg;
1450 case BUILD_SLICE:
1451 if (oparg == 3)
1452 return -2;
1453 else
1454 return -1;
1455
1456 case MAKE_CLOSURE:
1457 return -oparg;
1458 case LOAD_CLOSURE:
1459 return 1;
1460 case LOAD_DEREF:
1461 return 1;
1462 case STORE_DEREF:
1463 return -1;
1464 default:
1465 fprintf(stderr, "opcode = %d\n", opcode);
1466 Py_FatalError("opcode_stack_effect()");
1467
1468 }
1469 return 0; /* not reachable */
1470}
1471
1472/* Add an opcode with no argument.
1473 Returns 0 on failure, 1 on success.
1474*/
1475
1476static int
1477compiler_addop(struct compiler *c, int opcode)
1478{
1479 basicblock *b;
1480 struct instr *i;
1481 int off;
1482 off = compiler_next_instr(c, c->u->u_curblock);
1483 if (off < 0)
1484 return 0;
1485 b = c->u->u_curblock;
1486 i = &b->b_instr[off];
1487 i->i_opcode = opcode;
1488 i->i_hasarg = 0;
1489 if (opcode == RETURN_VALUE)
1490 b->b_return = 1;
1491 compiler_set_lineno(c, off);
1492 return 1;
1493}
1494
1495static int
1496compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
1497{
1498 PyObject *t, *v;
1499 int arg;
1500
1501 /* necessary to make sure types aren't coerced (e.g., int and long) */
Neil Schemenauer3a44aaa2005-10-23 17:21:54 +00001502 t = PyTuple_Pack(2, o, o->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503 if (t == NULL)
1504 return -1;
1505
1506 v = PyDict_GetItem(dict, t);
1507 if (!v) {
1508 arg = PyDict_Size(dict);
1509 v = PyInt_FromLong(arg);
1510 if (!v) {
1511 Py_DECREF(t);
1512 return -1;
1513 }
1514 if (PyDict_SetItem(dict, t, v) < 0) {
1515 Py_DECREF(t);
1516 Py_DECREF(v);
1517 return -1;
1518 }
1519 Py_DECREF(v);
1520 }
1521 else
1522 arg = PyInt_AsLong(v);
1523 Py_DECREF(t);
1524 return arg;
1525}
1526
1527static int
1528compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
1529 PyObject *o)
1530{
1531 int arg = compiler_add_o(c, dict, o);
1532 if (arg < 0)
1533 return 0;
1534 return compiler_addop_i(c, opcode, arg);
1535}
1536
1537static int
1538compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
1539 PyObject *o)
1540{
1541 int arg;
1542 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1543 if (!mangled)
1544 return 0;
1545 arg = compiler_add_o(c, dict, mangled);
1546 Py_DECREF(mangled);
1547 if (arg < 0)
1548 return 0;
1549 return compiler_addop_i(c, opcode, arg);
1550}
1551
1552/* Add an opcode with an integer argument.
1553 Returns 0 on failure, 1 on success.
1554*/
1555
1556static int
1557compiler_addop_i(struct compiler *c, int opcode, int oparg)
1558{
1559 struct instr *i;
1560 int off;
1561 off = compiler_next_instr(c, c->u->u_curblock);
1562 if (off < 0)
1563 return 0;
1564 i = &c->u->u_curblock->b_instr[off];
1565 i->i_opcode = opcode;
1566 i->i_oparg = oparg;
1567 i->i_hasarg = 1;
1568 compiler_set_lineno(c, off);
1569 return 1;
1570}
1571
1572static int
1573compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1574{
1575 struct instr *i;
1576 int off;
1577
1578 assert(b != NULL);
1579 off = compiler_next_instr(c, c->u->u_curblock);
1580 if (off < 0)
1581 return 0;
1582 compiler_set_lineno(c, off);
1583 i = &c->u->u_curblock->b_instr[off];
1584 i->i_opcode = opcode;
1585 i->i_target = b;
1586 i->i_hasarg = 1;
1587 if (absolute)
1588 i->i_jabs = 1;
1589 else
1590 i->i_jrel = 1;
1591 return 1;
1592}
1593
1594/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1595 like to find better names.) NEW_BLOCK() creates a new block and sets
1596 it as the current block. NEXT_BLOCK() also creates an implicit jump
1597 from the current block to the new block.
1598*/
1599
1600/* XXX The returns inside these macros make it impossible to decref
1601 objects created in the local function.
1602*/
1603
1604
1605#define NEW_BLOCK(C) { \
1606 if (compiler_use_new_block((C)) == NULL) \
1607 return 0; \
1608}
1609
1610#define NEXT_BLOCK(C) { \
1611 if (compiler_next_block((C)) == NULL) \
1612 return 0; \
1613}
1614
1615#define ADDOP(C, OP) { \
1616 if (!compiler_addop((C), (OP))) \
1617 return 0; \
1618}
1619
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001620#define ADDOP_IN_SCOPE(C, OP) { \
1621 if (!compiler_addop((C), (OP))) { \
1622 compiler_exit_scope(c); \
1623 return 0; \
1624 } \
1625}
1626
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001627#define ADDOP_O(C, OP, O, TYPE) { \
1628 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1629 return 0; \
1630}
1631
1632#define ADDOP_NAME(C, OP, O, TYPE) { \
1633 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1634 return 0; \
1635}
1636
1637#define ADDOP_I(C, OP, O) { \
1638 if (!compiler_addop_i((C), (OP), (O))) \
1639 return 0; \
1640}
1641
1642#define ADDOP_JABS(C, OP, O) { \
1643 if (!compiler_addop_j((C), (OP), (O), 1)) \
1644 return 0; \
1645}
1646
1647#define ADDOP_JREL(C, OP, O) { \
1648 if (!compiler_addop_j((C), (OP), (O), 0)) \
1649 return 0; \
1650}
1651
1652/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1653 the ASDL name to synthesize the name of the C type and the visit function.
1654*/
1655
1656#define VISIT(C, TYPE, V) {\
1657 if (!compiler_visit_ ## TYPE((C), (V))) \
1658 return 0; \
1659}
1660
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001661#define VISIT_IN_SCOPE(C, TYPE, V) {\
1662 if (!compiler_visit_ ## TYPE((C), (V))) { \
1663 compiler_exit_scope(c); \
1664 return 0; \
1665 } \
1666}
1667
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668#define VISIT_SLICE(C, V, CTX) {\
1669 if (!compiler_visit_slice((C), (V), (CTX))) \
1670 return 0; \
1671}
1672
1673#define VISIT_SEQ(C, TYPE, SEQ) { \
1674 int i; \
1675 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1676 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1677 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
1678 if (!compiler_visit_ ## TYPE((C), elt)) \
1679 return 0; \
1680 } \
1681}
1682
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001683#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
1684 int i; \
1685 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1686 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1687 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
1688 if (!compiler_visit_ ## TYPE((C), elt)) { \
1689 compiler_exit_scope(c); \
1690 return 0; \
1691 } \
1692 } \
1693}
1694
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695static int
1696compiler_isdocstring(stmt_ty s)
1697{
1698 if (s->kind != Expr_kind)
1699 return 0;
1700 return s->v.Expr.value->kind == Str_kind;
1701}
1702
1703/* Compile a sequence of statements, checking for a docstring. */
1704
1705static int
1706compiler_body(struct compiler *c, asdl_seq *stmts)
1707{
1708 int i = 0;
1709 stmt_ty st;
1710
1711 if (!asdl_seq_LEN(stmts))
1712 return 1;
1713 st = asdl_seq_GET(stmts, 0);
1714 if (compiler_isdocstring(st)) {
1715 i = 1;
1716 VISIT(c, expr, st->v.Expr.value);
1717 if (!compiler_nameop(c, __doc__, Store))
1718 return 0;
1719 }
1720 for (; i < asdl_seq_LEN(stmts); i++)
1721 VISIT(c, stmt, asdl_seq_GET(stmts, i));
1722 return 1;
1723}
1724
1725static PyCodeObject *
1726compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001727{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001728 PyCodeObject *co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001729 int addNone = 1;
1730 static PyObject *module;
1731 if (!module) {
1732 module = PyString_FromString("<module>");
1733 if (!module)
1734 return NULL;
1735 }
1736 if (!compiler_enter_scope(c, module, mod, 1))
Guido van Rossumd076c731998-10-07 19:42:25 +00001737 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738 switch (mod->kind) {
1739 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001740 if (!compiler_body(c, mod->v.Module.body)) {
1741 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001742 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001743 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001744 break;
1745 case Interactive_kind:
1746 c->c_interactive = 1;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001747 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001748 break;
1749 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001750 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001751 addNone = 0;
1752 break;
1753 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001754 PyErr_SetString(PyExc_SystemError,
1755 "suite should not be possible");
1756 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001757 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001758 PyErr_Format(PyExc_SystemError,
1759 "module kind %d should not be possible",
1760 mod->kind);
1761 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001762 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763 co = assemble(c, addNone);
1764 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001765 return co;
1766}
1767
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768/* The test for LOCAL must come before the test for FREE in order to
1769 handle classes where name is both local and free. The local var is
1770 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001771*/
1772
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001773static int
1774get_ref_type(struct compiler *c, PyObject *name)
1775{
1776 int scope = PyST_GetScope(c->u->u_ste, name);
1777 if (scope == 0) {
1778 char buf[350];
1779 PyOS_snprintf(buf, sizeof(buf),
1780 "unknown scope for %.100s in %.100s(%s) in %s\n"
1781 "symbols: %s\nlocals: %s\nglobals: %s\n",
1782 PyString_AS_STRING(name),
1783 PyString_AS_STRING(c->u->u_name),
1784 PyObject_REPR(c->u->u_ste->ste_id),
1785 c->c_filename,
1786 PyObject_REPR(c->u->u_ste->ste_symbols),
1787 PyObject_REPR(c->u->u_varnames),
1788 PyObject_REPR(c->u->u_names)
1789 );
1790 Py_FatalError(buf);
1791 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001792
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001793 return scope;
1794}
1795
1796static int
1797compiler_lookup_arg(PyObject *dict, PyObject *name)
1798{
1799 PyObject *k, *v;
1800 k = Py_BuildValue("(OO)", name, name->ob_type);
1801 if (k == NULL)
1802 return -1;
1803 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001804 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001805 if (v == NULL)
1806 return -1;
1807 return PyInt_AS_LONG(v);
1808}
1809
1810static int
1811compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1812{
1813 int i, free = PyCode_GetNumFree(co);
1814 if (free == 0) {
1815 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1816 ADDOP_I(c, MAKE_FUNCTION, args);
1817 return 1;
1818 }
1819 for (i = 0; i < free; ++i) {
1820 /* Bypass com_addop_varname because it will generate
1821 LOAD_DEREF but LOAD_CLOSURE is needed.
1822 */
1823 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1824 int arg, reftype;
1825
1826 /* Special case: If a class contains a method with a
1827 free variable that has the same name as a method,
1828 the name will be considered free *and* local in the
1829 class. It should be handled by the closure, as
1830 well as by the normal name loookup logic.
1831 */
1832 reftype = get_ref_type(c, name);
1833 if (reftype == CELL)
1834 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1835 else /* (reftype == FREE) */
1836 arg = compiler_lookup_arg(c->u->u_freevars, name);
1837 if (arg == -1) {
1838 printf("lookup %s in %s %d %d\n"
1839 "freevars of %s: %s\n",
1840 PyObject_REPR(name),
1841 PyString_AS_STRING(c->u->u_name),
1842 reftype, arg,
1843 PyString_AS_STRING(co->co_name),
1844 PyObject_REPR(co->co_freevars));
1845 Py_FatalError("compiler_make_closure()");
1846 }
1847 ADDOP_I(c, LOAD_CLOSURE, arg);
1848 }
1849 ADDOP_I(c, BUILD_TUPLE, free);
1850 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1851 ADDOP_I(c, MAKE_CLOSURE, args);
1852 return 1;
1853}
1854
1855static int
1856compiler_decorators(struct compiler *c, asdl_seq* decos)
1857{
1858 int i;
1859
1860 if (!decos)
1861 return 1;
1862
1863 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1864 VISIT(c, expr, asdl_seq_GET(decos, i));
1865 }
1866 return 1;
1867}
1868
1869static int
1870compiler_arguments(struct compiler *c, arguments_ty args)
1871{
1872 int i;
1873 int n = asdl_seq_LEN(args->args);
1874 /* Correctly handle nested argument lists */
1875 for (i = 0; i < n; i++) {
1876 expr_ty arg = asdl_seq_GET(args->args, i);
1877 if (arg->kind == Tuple_kind) {
1878 PyObject *id = PyString_FromFormat(".%d", i);
1879 if (id == NULL) {
1880 return 0;
1881 }
1882 if (!compiler_nameop(c, id, Load)) {
1883 Py_DECREF(id);
1884 return 0;
1885 }
1886 Py_DECREF(id);
1887 VISIT(c, expr, arg);
1888 }
1889 }
1890 return 1;
1891}
1892
1893static int
1894compiler_function(struct compiler *c, stmt_ty s)
1895{
1896 PyCodeObject *co;
1897 PyObject *first_const = Py_None;
1898 arguments_ty args = s->v.FunctionDef.args;
1899 asdl_seq* decos = s->v.FunctionDef.decorators;
1900 stmt_ty st;
1901 int i, n, docstring;
1902
1903 assert(s->kind == FunctionDef_kind);
1904
1905 if (!compiler_decorators(c, decos))
1906 return 0;
1907 if (args->defaults)
1908 VISIT_SEQ(c, expr, args->defaults);
1909 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1910 s->lineno))
1911 return 0;
1912
1913 st = asdl_seq_GET(s->v.FunctionDef.body, 0);
1914 docstring = compiler_isdocstring(st);
1915 if (docstring)
1916 first_const = st->v.Expr.value->v.Str.s;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001917 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1918 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001919 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001920 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921
1922 /* unpack nested arguments */
1923 compiler_arguments(c, args);
1924
1925 c->u->u_argcount = asdl_seq_LEN(args->args);
1926 n = asdl_seq_LEN(s->v.FunctionDef.body);
1927 /* if there was a docstring, we need to skip the first statement */
1928 for (i = docstring; i < n; i++) {
1929 stmt_ty s2 = asdl_seq_GET(s->v.FunctionDef.body, i);
1930 if (i == 0 && s2->kind == Expr_kind &&
1931 s2->v.Expr.value->kind == Str_kind)
1932 continue;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001933 VISIT_IN_SCOPE(c, stmt, s2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001934 }
1935 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001936 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001937 if (co == NULL)
1938 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001939
1940 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00001941 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942
1943 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1944 ADDOP_I(c, CALL_FUNCTION, 1);
1945 }
1946
1947 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1948}
1949
1950static int
1951compiler_class(struct compiler *c, stmt_ty s)
1952{
1953 int n;
1954 PyCodeObject *co;
1955 PyObject *str;
1956 /* push class name on stack, needed by BUILD_CLASS */
1957 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1958 /* push the tuple of base classes on the stack */
1959 n = asdl_seq_LEN(s->v.ClassDef.bases);
1960 if (n > 0)
1961 VISIT_SEQ(c, expr, s->v.ClassDef.bases);
1962 ADDOP_I(c, BUILD_TUPLE, n);
1963 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s,
1964 s->lineno))
1965 return 0;
1966 c->u->u_private = s->v.ClassDef.name;
1967 Py_INCREF(c->u->u_private);
1968 str = PyString_InternFromString("__name__");
1969 if (!str || !compiler_nameop(c, str, Load)) {
1970 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001971 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001972 return 0;
1973 }
1974
1975 Py_DECREF(str);
1976 str = PyString_InternFromString("__module__");
1977 if (!str || !compiler_nameop(c, str, Store)) {
1978 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001979 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001980 return 0;
1981 }
1982 Py_DECREF(str);
1983
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001984 if (!compiler_body(c, s->v.ClassDef.body)) {
1985 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001987 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001988
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001989 ADDOP_IN_SCOPE(c, LOAD_LOCALS);
1990 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001991 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001992 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001993 if (co == NULL)
1994 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001995
1996 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00001997 Py_DECREF(co);
1998
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999 ADDOP_I(c, CALL_FUNCTION, 0);
2000 ADDOP(c, BUILD_CLASS);
2001 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2002 return 0;
2003 return 1;
2004}
2005
2006static int
2007compiler_lambda(struct compiler *c, expr_ty e)
2008{
2009 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002010 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011 arguments_ty args = e->v.Lambda.args;
2012 assert(e->kind == Lambda_kind);
2013
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002014 if (!name) {
2015 name = PyString_InternFromString("<lambda>");
2016 if (!name)
2017 return 0;
2018 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002019
2020 if (args->defaults)
2021 VISIT_SEQ(c, expr, args->defaults);
2022 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2023 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002024
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025 /* unpack nested arguments */
2026 compiler_arguments(c, args);
2027
2028 c->u->u_argcount = asdl_seq_LEN(args->args);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00002029 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2030 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002031 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00002032 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002033 if (co == NULL)
2034 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035
2036 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00002037 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002038
2039 return 1;
2040}
2041
2042static int
2043compiler_print(struct compiler *c, stmt_ty s)
2044{
2045 int i, n;
2046 bool dest;
2047
2048 assert(s->kind == Print_kind);
2049 n = asdl_seq_LEN(s->v.Print.values);
2050 dest = false;
2051 if (s->v.Print.dest) {
2052 VISIT(c, expr, s->v.Print.dest);
2053 dest = true;
2054 }
2055 for (i = 0; i < n; i++) {
2056 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i);
2057 if (dest) {
2058 ADDOP(c, DUP_TOP);
2059 VISIT(c, expr, e);
2060 ADDOP(c, ROT_TWO);
2061 ADDOP(c, PRINT_ITEM_TO);
2062 }
2063 else {
2064 VISIT(c, expr, e);
2065 ADDOP(c, PRINT_ITEM);
2066 }
2067 }
2068 if (s->v.Print.nl) {
2069 if (dest)
2070 ADDOP(c, PRINT_NEWLINE_TO)
2071 else
2072 ADDOP(c, PRINT_NEWLINE)
2073 }
2074 else if (dest)
2075 ADDOP(c, POP_TOP);
2076 return 1;
2077}
2078
2079static int
2080compiler_if(struct compiler *c, stmt_ty s)
2081{
2082 basicblock *end, *next;
2083
2084 assert(s->kind == If_kind);
2085 end = compiler_new_block(c);
2086 if (end == NULL)
2087 return 0;
2088 next = compiler_new_block(c);
2089 if (next == NULL)
2090 return 0;
2091 VISIT(c, expr, s->v.If.test);
2092 ADDOP_JREL(c, JUMP_IF_FALSE, next);
2093 ADDOP(c, POP_TOP);
2094 VISIT_SEQ(c, stmt, s->v.If.body);
2095 ADDOP_JREL(c, JUMP_FORWARD, end);
2096 compiler_use_next_block(c, next);
2097 ADDOP(c, POP_TOP);
2098 if (s->v.If.orelse)
2099 VISIT_SEQ(c, stmt, s->v.If.orelse);
2100 compiler_use_next_block(c, end);
2101 return 1;
2102}
2103
2104static int
2105compiler_for(struct compiler *c, stmt_ty s)
2106{
2107 basicblock *start, *cleanup, *end;
2108
2109 start = compiler_new_block(c);
2110 cleanup = compiler_new_block(c);
2111 end = compiler_new_block(c);
2112 if (start == NULL || end == NULL || cleanup == NULL)
2113 return 0;
2114 ADDOP_JREL(c, SETUP_LOOP, end);
2115 if (!compiler_push_fblock(c, LOOP, start))
2116 return 0;
2117 VISIT(c, expr, s->v.For.iter);
2118 ADDOP(c, GET_ITER);
2119 compiler_use_next_block(c, start);
2120 ADDOP_JREL(c, FOR_ITER, cleanup);
2121 VISIT(c, expr, s->v.For.target);
2122 VISIT_SEQ(c, stmt, s->v.For.body);
2123 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2124 compiler_use_next_block(c, cleanup);
2125 ADDOP(c, POP_BLOCK);
2126 compiler_pop_fblock(c, LOOP, start);
2127 VISIT_SEQ(c, stmt, s->v.For.orelse);
2128 compiler_use_next_block(c, end);
2129 return 1;
2130}
2131
2132static int
2133compiler_while(struct compiler *c, stmt_ty s)
2134{
2135 basicblock *loop, *orelse, *end, *anchor = NULL;
2136 int constant = expr_constant(s->v.While.test);
2137
2138 if (constant == 0)
2139 return 1;
2140 loop = compiler_new_block(c);
2141 end = compiler_new_block(c);
2142 if (constant == -1) {
2143 anchor = compiler_new_block(c);
2144 if (anchor == NULL)
2145 return 0;
2146 }
2147 if (loop == NULL || end == NULL)
2148 return 0;
2149 if (s->v.While.orelse) {
2150 orelse = compiler_new_block(c);
2151 if (orelse == NULL)
2152 return 0;
2153 }
2154 else
2155 orelse = NULL;
2156
2157 ADDOP_JREL(c, SETUP_LOOP, end);
2158 compiler_use_next_block(c, loop);
2159 if (!compiler_push_fblock(c, LOOP, loop))
2160 return 0;
2161 if (constant == -1) {
2162 VISIT(c, expr, s->v.While.test);
2163 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
2164 ADDOP(c, POP_TOP);
2165 }
2166 VISIT_SEQ(c, stmt, s->v.While.body);
2167 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
2168
2169 /* XXX should the two POP instructions be in a separate block
2170 if there is no else clause ?
2171 */
2172
2173 if (constant == -1) {
2174 compiler_use_next_block(c, anchor);
2175 ADDOP(c, POP_TOP);
2176 ADDOP(c, POP_BLOCK);
2177 }
2178 compiler_pop_fblock(c, LOOP, loop);
2179 if (orelse != NULL)
2180 VISIT_SEQ(c, stmt, s->v.While.orelse);
2181 compiler_use_next_block(c, end);
2182
2183 return 1;
2184}
2185
2186static int
2187compiler_continue(struct compiler *c)
2188{
2189 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
2190 int i;
2191
2192 if (!c->u->u_nfblocks)
2193 return compiler_error(c, LOOP_ERROR_MSG);
2194 i = c->u->u_nfblocks - 1;
2195 switch (c->u->u_fblock[i].fb_type) {
2196 case LOOP:
2197 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
2198 break;
2199 case EXCEPT:
2200 case FINALLY_TRY:
2201 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP)
2202 ;
2203 if (i == -1)
2204 return compiler_error(c, LOOP_ERROR_MSG);
2205 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
2206 break;
2207 case FINALLY_END:
2208 return compiler_error(c,
2209 "'continue' not supported inside 'finally' clause");
2210 }
2211
2212 return 1;
2213}
2214
2215/* Code generated for "try: <body> finally: <finalbody>" is as follows:
2216
2217 SETUP_FINALLY L
2218 <code for body>
2219 POP_BLOCK
2220 LOAD_CONST <None>
2221 L: <code for finalbody>
2222 END_FINALLY
2223
2224 The special instructions use the block stack. Each block
2225 stack entry contains the instruction that created it (here
2226 SETUP_FINALLY), the level of the value stack at the time the
2227 block stack entry was created, and a label (here L).
2228
2229 SETUP_FINALLY:
2230 Pushes the current value stack level and the label
2231 onto the block stack.
2232 POP_BLOCK:
2233 Pops en entry from the block stack, and pops the value
2234 stack until its level is the same as indicated on the
2235 block stack. (The label is ignored.)
2236 END_FINALLY:
2237 Pops a variable number of entries from the *value* stack
2238 and re-raises the exception they specify. The number of
2239 entries popped depends on the (pseudo) exception type.
2240
2241 The block stack is unwound when an exception is raised:
2242 when a SETUP_FINALLY entry is found, the exception is pushed
2243 onto the value stack (and the exception condition is cleared),
2244 and the interpreter jumps to the label gotten from the block
2245 stack.
2246*/
2247
2248static int
2249compiler_try_finally(struct compiler *c, stmt_ty s)
2250{
2251 basicblock *body, *end;
2252 body = compiler_new_block(c);
2253 end = compiler_new_block(c);
2254 if (body == NULL || end == NULL)
2255 return 0;
2256
2257 ADDOP_JREL(c, SETUP_FINALLY, end);
2258 compiler_use_next_block(c, body);
2259 if (!compiler_push_fblock(c, FINALLY_TRY, body))
2260 return 0;
2261 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
2262 ADDOP(c, POP_BLOCK);
2263 compiler_pop_fblock(c, FINALLY_TRY, body);
2264
2265 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2266 compiler_use_next_block(c, end);
2267 if (!compiler_push_fblock(c, FINALLY_END, end))
2268 return 0;
2269 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
2270 ADDOP(c, END_FINALLY);
2271 compiler_pop_fblock(c, FINALLY_END, end);
2272
2273 return 1;
2274}
2275
2276/*
2277 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
2278 (The contents of the value stack is shown in [], with the top
2279 at the right; 'tb' is trace-back info, 'val' the exception's
2280 associated value, and 'exc' the exception.)
2281
2282 Value stack Label Instruction Argument
2283 [] SETUP_EXCEPT L1
2284 [] <code for S>
2285 [] POP_BLOCK
2286 [] JUMP_FORWARD L0
2287
2288 [tb, val, exc] L1: DUP )
2289 [tb, val, exc, exc] <evaluate E1> )
2290 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2291 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
2292 [tb, val, exc, 1] POP )
2293 [tb, val, exc] POP
2294 [tb, val] <assign to V1> (or POP if no V1)
2295 [tb] POP
2296 [] <code for S1>
2297 JUMP_FORWARD L0
2298
2299 [tb, val, exc, 0] L2: POP
2300 [tb, val, exc] DUP
2301 .............................etc.......................
2302
2303 [tb, val, exc, 0] Ln+1: POP
2304 [tb, val, exc] END_FINALLY # re-raise exception
2305
2306 [] L0: <next statement>
2307
2308 Of course, parts are not generated if Vi or Ei is not present.
2309*/
2310static int
2311compiler_try_except(struct compiler *c, stmt_ty s)
2312{
2313 basicblock *body, *orelse, *except, *end;
2314 int i, n;
2315
2316 body = compiler_new_block(c);
2317 except = compiler_new_block(c);
2318 orelse = compiler_new_block(c);
2319 end = compiler_new_block(c);
2320 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
2321 return 0;
2322 ADDOP_JREL(c, SETUP_EXCEPT, except);
2323 compiler_use_next_block(c, body);
2324 if (!compiler_push_fblock(c, EXCEPT, body))
2325 return 0;
2326 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
2327 ADDOP(c, POP_BLOCK);
2328 compiler_pop_fblock(c, EXCEPT, body);
2329 ADDOP_JREL(c, JUMP_FORWARD, orelse);
2330 n = asdl_seq_LEN(s->v.TryExcept.handlers);
2331 compiler_use_next_block(c, except);
2332 for (i = 0; i < n; i++) {
2333 excepthandler_ty handler = asdl_seq_GET(
2334 s->v.TryExcept.handlers, i);
2335 if (!handler->type && i < n-1)
2336 return compiler_error(c, "default 'except:' must be last");
2337 except = compiler_new_block(c);
2338 if (except == NULL)
2339 return 0;
2340 if (handler->type) {
2341 ADDOP(c, DUP_TOP);
2342 VISIT(c, expr, handler->type);
2343 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2344 ADDOP_JREL(c, JUMP_IF_FALSE, except);
2345 ADDOP(c, POP_TOP);
2346 }
2347 ADDOP(c, POP_TOP);
2348 if (handler->name) {
2349 VISIT(c, expr, handler->name);
2350 }
2351 else {
2352 ADDOP(c, POP_TOP);
2353 }
2354 ADDOP(c, POP_TOP);
2355 VISIT_SEQ(c, stmt, handler->body);
2356 ADDOP_JREL(c, JUMP_FORWARD, end);
2357 compiler_use_next_block(c, except);
2358 if (handler->type)
2359 ADDOP(c, POP_TOP);
2360 }
2361 ADDOP(c, END_FINALLY);
2362 compiler_use_next_block(c, orelse);
2363 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2364 compiler_use_next_block(c, end);
2365 return 1;
2366}
2367
2368static int
2369compiler_import_as(struct compiler *c, identifier name, identifier asname)
2370{
2371 /* The IMPORT_NAME opcode was already generated. This function
2372 merely needs to bind the result to a name.
2373
2374 If there is a dot in name, we need to split it and emit a
2375 LOAD_ATTR for each name.
2376 */
2377 const char *src = PyString_AS_STRING(name);
2378 const char *dot = strchr(src, '.');
2379 if (dot) {
2380 /* Consume the base module name to get the first attribute */
2381 src = dot + 1;
2382 while (dot) {
2383 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00002384 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002385 dot = strchr(src, '.');
Armin Rigo31441302005-10-21 12:57:31 +00002386 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002387 dot ? dot - src : strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002388 if (!attr)
2389 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002390 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002391 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002392 src = dot + 1;
2393 }
2394 }
2395 return compiler_nameop(c, asname, Store);
2396}
2397
2398static int
2399compiler_import(struct compiler *c, stmt_ty s)
2400{
2401 /* The Import node stores a module name like a.b.c as a single
2402 string. This is convenient for all cases except
2403 import a.b.c as d
2404 where we need to parse that string to extract the individual
2405 module names.
2406 XXX Perhaps change the representation to make this case simpler?
2407 */
2408 int i, n = asdl_seq_LEN(s->v.Import.names);
2409 for (i = 0; i < n; i++) {
2410 alias_ty alias = asdl_seq_GET(s->v.Import.names, i);
2411 int r;
2412
2413 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2414 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2415
2416 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002417 r = compiler_import_as(c, alias->name, alias->asname);
2418 if (!r)
2419 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002420 }
2421 else {
2422 identifier tmp = alias->name;
2423 const char *base = PyString_AS_STRING(alias->name);
2424 char *dot = strchr(base, '.');
2425 if (dot)
2426 tmp = PyString_FromStringAndSize(base,
2427 dot - base);
2428 r = compiler_nameop(c, tmp, Store);
2429 if (dot) {
2430 Py_DECREF(tmp);
2431 }
2432 if (!r)
2433 return r;
2434 }
2435 }
2436 return 1;
2437}
2438
2439static int
2440compiler_from_import(struct compiler *c, stmt_ty s)
2441{
2442 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
2443 int star = 0;
2444
2445 PyObject *names = PyTuple_New(n);
2446 if (!names)
2447 return 0;
2448
2449 /* build up the names */
2450 for (i = 0; i < n; i++) {
2451 alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
2452 Py_INCREF(alias->name);
2453 PyTuple_SET_ITEM(names, i, alias->name);
2454 }
2455
2456 if (s->lineno > c->c_future->ff_lineno) {
2457 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
2458 "__future__")) {
2459 Py_DECREF(names);
2460 return compiler_error(c,
2461 "from __future__ imports must occur "
2462 "at the beginning of the file");
2463
2464 }
2465 }
2466
2467 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002468 Py_DECREF(names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002469 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2470 for (i = 0; i < n; i++) {
2471 alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
2472 identifier store_name;
2473
2474 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
2475 assert(n == 1);
2476 ADDOP(c, IMPORT_STAR);
2477 star = 1;
2478 break;
2479 }
2480
2481 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2482 store_name = alias->name;
2483 if (alias->asname)
2484 store_name = alias->asname;
2485
2486 if (!compiler_nameop(c, store_name, Store)) {
2487 Py_DECREF(names);
2488 return 0;
2489 }
2490 }
2491 if (!star)
2492 /* remove imported module */
2493 ADDOP(c, POP_TOP);
2494 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 }
2524 compiler_use_block(c, end);
2525 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,
2755 "inplace binary op %d should not be possible",
2756 op);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002757 return 0;
2758}
2759
2760static int
2761compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2762{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002763 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002764 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2765
2766 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002767 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002768 /* XXX AugStore isn't used anywhere! */
2769
2770 /* First check for assignment to __debug__. Param? */
2771 if ((ctx == Store || ctx == AugStore || ctx == Del)
2772 && !strcmp(PyString_AS_STRING(name), "__debug__")) {
2773 return compiler_error(c, "can not assign to __debug__");
2774 }
2775
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002776 mangled = _Py_Mangle(c->u->u_private, name);
2777 if (!mangled)
2778 return 0;
2779
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002780 op = 0;
2781 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002782 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002783 switch (scope) {
2784 case FREE:
2785 dict = c->u->u_freevars;
2786 optype = OP_DEREF;
2787 break;
2788 case CELL:
2789 dict = c->u->u_cellvars;
2790 optype = OP_DEREF;
2791 break;
2792 case LOCAL:
2793 if (c->u->u_ste->ste_type == FunctionBlock)
2794 optype = OP_FAST;
2795 break;
2796 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002797 if (c->u->u_ste->ste_type == FunctionBlock &&
2798 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002799 optype = OP_GLOBAL;
2800 break;
2801 case GLOBAL_EXPLICIT:
2802 optype = OP_GLOBAL;
2803 break;
2804 }
2805
2806 /* XXX Leave assert here, but handle __doc__ and the like better */
2807 assert(scope || PyString_AS_STRING(name)[0] == '_');
2808
2809 switch (optype) {
2810 case OP_DEREF:
2811 switch (ctx) {
2812 case Load: op = LOAD_DEREF; break;
2813 case Store: op = STORE_DEREF; break;
2814 case AugLoad:
2815 case AugStore:
2816 break;
2817 case Del:
2818 PyErr_Format(PyExc_SyntaxError,
2819 "can not delete variable '%s' referenced "
2820 "in nested scope",
2821 PyString_AS_STRING(name));
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002822 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002824 case Param:
Neal Norwitz4737b232005-11-19 23:58:29 +00002825 PyErr_SetString(PyExc_SystemError,
2826 "param invalid for deref variable");
2827 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002828 }
2829 break;
2830 case OP_FAST:
2831 switch (ctx) {
2832 case Load: op = LOAD_FAST; break;
2833 case Store: op = STORE_FAST; break;
2834 case Del: op = DELETE_FAST; break;
2835 case AugLoad:
2836 case AugStore:
2837 break;
2838 case Param:
Neal Norwitz4737b232005-11-19 23:58:29 +00002839 PyErr_SetString(PyExc_SystemError,
2840 "param invalid for local variable");
2841 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002842 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002843 ADDOP_O(c, op, mangled, varnames);
2844 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002845 return 1;
2846 case OP_GLOBAL:
2847 switch (ctx) {
2848 case Load: op = LOAD_GLOBAL; break;
2849 case Store: op = STORE_GLOBAL; break;
2850 case Del: op = DELETE_GLOBAL; break;
2851 case AugLoad:
2852 case AugStore:
2853 break;
2854 case Param:
Neal Norwitz4737b232005-11-19 23:58:29 +00002855 PyErr_SetString(PyExc_SystemError,
2856 "param invalid for global variable");
2857 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002858 }
2859 break;
2860 case OP_NAME:
2861 switch (ctx) {
2862 case Load: op = LOAD_NAME; break;
2863 case Store: op = STORE_NAME; break;
2864 case Del: op = DELETE_NAME; break;
2865 case AugLoad:
2866 case AugStore:
2867 break;
2868 case Param:
Neal Norwitz4737b232005-11-19 23:58:29 +00002869 PyErr_SetString(PyExc_SystemError,
2870 "param invalid for name variable");
2871 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002872 }
2873 break;
2874 }
2875
2876 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002877 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002878 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002879 if (arg < 0)
2880 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002881 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882}
2883
2884static int
2885compiler_boolop(struct compiler *c, expr_ty e)
2886{
2887 basicblock *end;
2888 int jumpi, i, n;
2889 asdl_seq *s;
2890
2891 assert(e->kind == BoolOp_kind);
2892 if (e->v.BoolOp.op == And)
2893 jumpi = JUMP_IF_FALSE;
2894 else
2895 jumpi = JUMP_IF_TRUE;
2896 end = compiler_new_block(c);
2897 if (end < 0)
2898 return 0;
2899 s = e->v.BoolOp.values;
2900 n = asdl_seq_LEN(s) - 1;
2901 for (i = 0; i < n; ++i) {
2902 VISIT(c, expr, asdl_seq_GET(s, i));
2903 ADDOP_JREL(c, jumpi, end);
2904 ADDOP(c, POP_TOP)
2905 }
2906 VISIT(c, expr, asdl_seq_GET(s, n));
2907 compiler_use_next_block(c, end);
2908 return 1;
2909}
2910
2911static int
2912compiler_list(struct compiler *c, expr_ty e)
2913{
2914 int n = asdl_seq_LEN(e->v.List.elts);
2915 if (e->v.List.ctx == Store) {
2916 ADDOP_I(c, UNPACK_SEQUENCE, n);
2917 }
2918 VISIT_SEQ(c, expr, e->v.List.elts);
2919 if (e->v.List.ctx == Load) {
2920 ADDOP_I(c, BUILD_LIST, n);
2921 }
2922 return 1;
2923}
2924
2925static int
2926compiler_tuple(struct compiler *c, expr_ty e)
2927{
2928 int n = asdl_seq_LEN(e->v.Tuple.elts);
2929 if (e->v.Tuple.ctx == Store) {
2930 ADDOP_I(c, UNPACK_SEQUENCE, n);
2931 }
2932 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2933 if (e->v.Tuple.ctx == Load) {
2934 ADDOP_I(c, BUILD_TUPLE, n);
2935 }
2936 return 1;
2937}
2938
2939static int
2940compiler_compare(struct compiler *c, expr_ty e)
2941{
2942 int i, n;
2943 basicblock *cleanup = NULL;
2944
2945 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2946 VISIT(c, expr, e->v.Compare.left);
2947 n = asdl_seq_LEN(e->v.Compare.ops);
2948 assert(n > 0);
2949 if (n > 1) {
2950 cleanup = compiler_new_block(c);
2951 if (cleanup == NULL)
2952 return 0;
2953 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, 0));
2954 }
2955 for (i = 1; i < n; i++) {
2956 ADDOP(c, DUP_TOP);
2957 ADDOP(c, ROT_THREE);
2958 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
2959 ADDOP_I(c, COMPARE_OP,
2960 cmpop((cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i - 1)));
2961 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2962 NEXT_BLOCK(c);
2963 ADDOP(c, POP_TOP);
2964 if (i < (n - 1))
2965 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, i));
2966 }
2967 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, n - 1));
2968 ADDOP_I(c, COMPARE_OP,
2969 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
2970 cmpop((cmpop_ty)asdl_seq_GET(e->v.Compare.ops, n - 1)));
2971 if (n > 1) {
2972 basicblock *end = compiler_new_block(c);
2973 if (end == NULL)
2974 return 0;
2975 ADDOP_JREL(c, JUMP_FORWARD, end);
2976 compiler_use_next_block(c, cleanup);
2977 ADDOP(c, ROT_TWO);
2978 ADDOP(c, POP_TOP);
2979 compiler_use_next_block(c, end);
2980 }
2981 return 1;
2982}
2983
2984static int
2985compiler_call(struct compiler *c, expr_ty e)
2986{
2987 int n, code = 0;
2988
2989 VISIT(c, expr, e->v.Call.func);
2990 n = asdl_seq_LEN(e->v.Call.args);
2991 VISIT_SEQ(c, expr, e->v.Call.args);
2992 if (e->v.Call.keywords) {
2993 VISIT_SEQ(c, keyword, e->v.Call.keywords);
2994 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
2995 }
2996 if (e->v.Call.starargs) {
2997 VISIT(c, expr, e->v.Call.starargs);
2998 code |= 1;
2999 }
3000 if (e->v.Call.kwargs) {
3001 VISIT(c, expr, e->v.Call.kwargs);
3002 code |= 2;
3003 }
3004 switch (code) {
3005 case 0:
3006 ADDOP_I(c, CALL_FUNCTION, n);
3007 break;
3008 case 1:
3009 ADDOP_I(c, CALL_FUNCTION_VAR, n);
3010 break;
3011 case 2:
3012 ADDOP_I(c, CALL_FUNCTION_KW, n);
3013 break;
3014 case 3:
3015 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
3016 break;
3017 }
3018 return 1;
3019}
3020
3021static int
3022compiler_listcomp_generator(struct compiler *c, PyObject *tmpname,
3023 asdl_seq *generators, int gen_index,
3024 expr_ty elt)
3025{
3026 /* generate code for the iterator, then each of the ifs,
3027 and then write to the element */
3028
3029 comprehension_ty l;
3030 basicblock *start, *anchor, *skip, *if_cleanup;
3031 int i, n;
3032
3033 start = compiler_new_block(c);
3034 skip = compiler_new_block(c);
3035 if_cleanup = compiler_new_block(c);
3036 anchor = compiler_new_block(c);
3037
3038 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3039 anchor == NULL)
3040 return 0;
3041
3042 l = asdl_seq_GET(generators, gen_index);
3043 VISIT(c, expr, l->iter);
3044 ADDOP(c, GET_ITER);
3045 compiler_use_next_block(c, start);
3046 ADDOP_JREL(c, FOR_ITER, anchor);
3047 NEXT_BLOCK(c);
3048 VISIT(c, expr, l->target);
3049
3050 /* XXX this needs to be cleaned up...a lot! */
3051 n = asdl_seq_LEN(l->ifs);
3052 for (i = 0; i < n; i++) {
3053 expr_ty e = asdl_seq_GET(l->ifs, i);
3054 VISIT(c, expr, e);
3055 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
3056 NEXT_BLOCK(c);
3057 ADDOP(c, POP_TOP);
3058 }
3059
3060 if (++gen_index < asdl_seq_LEN(generators))
3061 if (!compiler_listcomp_generator(c, tmpname,
3062 generators, gen_index, elt))
3063 return 0;
3064
3065 /* only append after the last for generator */
3066 if (gen_index >= asdl_seq_LEN(generators)) {
3067 if (!compiler_nameop(c, tmpname, Load))
3068 return 0;
3069 VISIT(c, expr, elt);
3070 ADDOP_I(c, CALL_FUNCTION, 1);
3071 ADDOP(c, POP_TOP);
3072
3073 compiler_use_next_block(c, skip);
3074 }
3075 for (i = 0; i < n; i++) {
3076 ADDOP_I(c, JUMP_FORWARD, 1);
3077 if (i == 0)
3078 compiler_use_next_block(c, if_cleanup);
3079 ADDOP(c, POP_TOP);
3080 }
3081 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3082 compiler_use_next_block(c, anchor);
3083 /* delete the append method added to locals */
3084 if (gen_index == 1)
3085 if (!compiler_nameop(c, tmpname, Del))
3086 return 0;
3087
3088 return 1;
3089}
3090
3091static int
3092compiler_listcomp(struct compiler *c, expr_ty e)
3093{
3094 char tmpname[256];
3095 identifier tmp;
3096 int rc = 0;
3097 static identifier append;
3098 asdl_seq *generators = e->v.ListComp.generators;
3099
3100 assert(e->kind == ListComp_kind);
3101 if (!append) {
3102 append = PyString_InternFromString("append");
3103 if (!append)
3104 return 0;
3105 }
3106 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
3107 tmp = PyString_FromString(tmpname);
3108 if (!tmp)
3109 return 0;
3110 ADDOP_I(c, BUILD_LIST, 0);
3111 ADDOP(c, DUP_TOP);
3112 ADDOP_O(c, LOAD_ATTR, append, names);
3113 if (compiler_nameop(c, tmp, Store))
3114 rc = compiler_listcomp_generator(c, tmp, generators, 0,
3115 e->v.ListComp.elt);
3116 Py_DECREF(tmp);
3117 return rc;
3118}
3119
3120static int
3121compiler_genexp_generator(struct compiler *c,
3122 asdl_seq *generators, int gen_index,
3123 expr_ty elt)
3124{
3125 /* generate code for the iterator, then each of the ifs,
3126 and then write to the element */
3127
3128 comprehension_ty ge;
3129 basicblock *start, *anchor, *skip, *if_cleanup, *end;
3130 int i, n;
3131
3132 start = compiler_new_block(c);
3133 skip = compiler_new_block(c);
3134 if_cleanup = compiler_new_block(c);
3135 anchor = compiler_new_block(c);
3136 end = compiler_new_block(c);
3137
3138 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3139 anchor == NULL || end == NULL)
3140 return 0;
3141
3142 ge = asdl_seq_GET(generators, gen_index);
3143 ADDOP_JREL(c, SETUP_LOOP, end);
3144 if (!compiler_push_fblock(c, LOOP, start))
3145 return 0;
3146
3147 if (gen_index == 0) {
3148 /* Receive outermost iter as an implicit argument */
3149 c->u->u_argcount = 1;
3150 ADDOP_I(c, LOAD_FAST, 0);
3151 }
3152 else {
3153 /* Sub-iter - calculate on the fly */
3154 VISIT(c, expr, ge->iter);
3155 ADDOP(c, GET_ITER);
3156 }
3157 compiler_use_next_block(c, start);
3158 ADDOP_JREL(c, FOR_ITER, anchor);
3159 NEXT_BLOCK(c);
3160 VISIT(c, expr, ge->target);
3161
3162 /* XXX this needs to be cleaned up...a lot! */
3163 n = asdl_seq_LEN(ge->ifs);
3164 for (i = 0; i < n; i++) {
3165 expr_ty e = asdl_seq_GET(ge->ifs, i);
3166 VISIT(c, expr, e);
3167 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
3168 NEXT_BLOCK(c);
3169 ADDOP(c, POP_TOP);
3170 }
3171
3172 if (++gen_index < asdl_seq_LEN(generators))
3173 if (!compiler_genexp_generator(c, generators, gen_index, elt))
3174 return 0;
3175
3176 /* only append after the last 'for' generator */
3177 if (gen_index >= asdl_seq_LEN(generators)) {
3178 VISIT(c, expr, elt);
3179 ADDOP(c, YIELD_VALUE);
3180 ADDOP(c, POP_TOP);
3181
3182 compiler_use_next_block(c, skip);
3183 }
3184 for (i = 0; i < n; i++) {
3185 ADDOP_I(c, JUMP_FORWARD, 1);
3186 if (i == 0)
3187 compiler_use_next_block(c, if_cleanup);
3188
3189 ADDOP(c, POP_TOP);
3190 }
3191 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3192 compiler_use_next_block(c, anchor);
3193 ADDOP(c, POP_BLOCK);
3194 compiler_pop_fblock(c, LOOP, start);
3195 compiler_use_next_block(c, end);
3196
3197 return 1;
3198}
3199
3200static int
3201compiler_genexp(struct compiler *c, expr_ty e)
3202{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00003203 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003204 PyCodeObject *co;
3205 expr_ty outermost_iter = ((comprehension_ty)
3206 (asdl_seq_GET(e->v.GeneratorExp.generators,
3207 0)))->iter;
3208
Nick Coghlan944d3eb2005-11-16 12:46:55 +00003209 if (!name) {
3210 name = PyString_FromString("<genexpr>");
3211 if (!name)
3212 return 0;
3213 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003214
3215 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
3216 return 0;
3217 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
3218 e->v.GeneratorExp.elt);
3219 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00003220 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003221 if (co == NULL)
3222 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003223
3224 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00003225 Py_DECREF(co);
3226
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003227 VISIT(c, expr, outermost_iter);
3228 ADDOP(c, GET_ITER);
3229 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003230
3231 return 1;
3232}
3233
3234static int
3235compiler_visit_keyword(struct compiler *c, keyword_ty k)
3236{
3237 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3238 VISIT(c, expr, k->value);
3239 return 1;
3240}
3241
3242/* Test whether expression is constant. For constants, report
3243 whether they are true or false.
3244
3245 Return values: 1 for true, 0 for false, -1 for non-constant.
3246 */
3247
3248static int
3249expr_constant(expr_ty e)
3250{
3251 switch (e->kind) {
3252 case Num_kind:
3253 return PyObject_IsTrue(e->v.Num.n);
3254 case Str_kind:
3255 return PyObject_IsTrue(e->v.Str.s);
3256 default:
3257 return -1;
3258 }
3259}
3260
3261static int
3262compiler_visit_expr(struct compiler *c, expr_ty e)
3263{
3264 int i, n;
3265
3266 if (e->lineno > c->u->u_lineno) {
3267 c->u->u_lineno = e->lineno;
3268 c->u->u_lineno_set = false;
3269 }
3270 switch (e->kind) {
3271 case BoolOp_kind:
3272 return compiler_boolop(c, e);
3273 case BinOp_kind:
3274 VISIT(c, expr, e->v.BinOp.left);
3275 VISIT(c, expr, e->v.BinOp.right);
3276 ADDOP(c, binop(c, e->v.BinOp.op));
3277 break;
3278 case UnaryOp_kind:
3279 VISIT(c, expr, e->v.UnaryOp.operand);
3280 ADDOP(c, unaryop(e->v.UnaryOp.op));
3281 break;
3282 case Lambda_kind:
3283 return compiler_lambda(c, e);
3284 case Dict_kind:
3285 /* XXX get rid of arg? */
3286 ADDOP_I(c, BUILD_MAP, 0);
3287 n = asdl_seq_LEN(e->v.Dict.values);
3288 /* We must arrange things just right for STORE_SUBSCR.
3289 It wants the stack to look like (value) (dict) (key) */
3290 for (i = 0; i < n; i++) {
3291 ADDOP(c, DUP_TOP);
3292 VISIT(c, expr, asdl_seq_GET(e->v.Dict.values, i));
3293 ADDOP(c, ROT_TWO);
3294 VISIT(c, expr, asdl_seq_GET(e->v.Dict.keys, i));
3295 ADDOP(c, STORE_SUBSCR);
3296 }
3297 break;
3298 case ListComp_kind:
3299 return compiler_listcomp(c, e);
3300 case GeneratorExp_kind:
3301 return compiler_genexp(c, e);
3302 case Yield_kind:
3303 if (c->u->u_ste->ste_type != FunctionBlock)
3304 return compiler_error(c, "'yield' outside function");
3305 /*
3306 for (i = 0; i < c->u->u_nfblocks; i++) {
3307 if (c->u->u_fblock[i].fb_type == FINALLY_TRY)
3308 return compiler_error(
3309 c, "'yield' not allowed in a 'try' "
3310 "block with a 'finally' clause");
3311 }
3312 */
3313 if (e->v.Yield.value) {
3314 VISIT(c, expr, e->v.Yield.value);
3315 }
3316 else {
3317 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3318 }
3319 ADDOP(c, YIELD_VALUE);
3320 break;
3321 case Compare_kind:
3322 return compiler_compare(c, e);
3323 case Call_kind:
3324 return compiler_call(c, e);
3325 case Repr_kind:
3326 VISIT(c, expr, e->v.Repr.value);
3327 ADDOP(c, UNARY_CONVERT);
3328 break;
3329 case Num_kind:
3330 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3331 break;
3332 case Str_kind:
3333 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3334 break;
3335 /* The following exprs can be assignment targets. */
3336 case Attribute_kind:
3337 if (e->v.Attribute.ctx != AugStore)
3338 VISIT(c, expr, e->v.Attribute.value);
3339 switch (e->v.Attribute.ctx) {
3340 case AugLoad:
3341 ADDOP(c, DUP_TOP);
3342 /* Fall through to load */
3343 case Load:
3344 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3345 break;
3346 case AugStore:
3347 ADDOP(c, ROT_TWO);
3348 /* Fall through to save */
3349 case Store:
3350 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3351 break;
3352 case Del:
3353 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3354 break;
3355 case Param:
Neal Norwitz4737b232005-11-19 23:58:29 +00003356 PyErr_SetString(PyExc_SystemError,
3357 "param invalid in attribute expression");
3358 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003359 }
3360 break;
3361 case Subscript_kind:
3362 switch (e->v.Subscript.ctx) {
3363 case AugLoad:
3364 VISIT(c, expr, e->v.Subscript.value);
3365 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3366 break;
3367 case Load:
3368 VISIT(c, expr, e->v.Subscript.value);
3369 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3370 break;
3371 case AugStore:
3372 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3373 break;
3374 case Store:
3375 VISIT(c, expr, e->v.Subscript.value);
3376 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3377 break;
3378 case Del:
3379 VISIT(c, expr, e->v.Subscript.value);
3380 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3381 break;
3382 case Param:
Neal Norwitz4737b232005-11-19 23:58:29 +00003383 PyErr_SetString(PyExc_SystemError,
3384 "param invalid in subscript expression");
3385 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003386 }
3387 break;
3388 case Name_kind:
3389 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3390 /* child nodes of List and Tuple will have expr_context set */
3391 case List_kind:
3392 return compiler_list(c, e);
3393 case Tuple_kind:
3394 return compiler_tuple(c, e);
3395 }
3396 return 1;
3397}
3398
3399static int
3400compiler_augassign(struct compiler *c, stmt_ty s)
3401{
3402 expr_ty e = s->v.AugAssign.target;
3403 expr_ty auge;
3404
3405 assert(s->kind == AugAssign_kind);
3406
3407 switch (e->kind) {
3408 case Attribute_kind:
3409 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
3410 AugLoad, e->lineno);
3411 if (auge == NULL)
3412 return 0;
3413 VISIT(c, expr, auge);
3414 VISIT(c, expr, s->v.AugAssign.value);
3415 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3416 auge->v.Attribute.ctx = AugStore;
3417 VISIT(c, expr, auge);
3418 free(auge);
3419 break;
3420 case Subscript_kind:
3421 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
3422 AugLoad, e->lineno);
3423 if (auge == NULL)
3424 return 0;
3425 VISIT(c, expr, auge);
3426 VISIT(c, expr, s->v.AugAssign.value);
3427 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3428 auge->v.Subscript.ctx = AugStore;
3429 VISIT(c, expr, auge);
3430 free(auge);
3431 break;
3432 case Name_kind:
3433 VISIT(c, expr, s->v.AugAssign.target);
3434 VISIT(c, expr, s->v.AugAssign.value);
3435 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3436 return compiler_nameop(c, e->v.Name.id, Store);
3437 default:
3438 fprintf(stderr,
3439 "invalid node type for augmented assignment\n");
3440 return 0;
3441 }
3442 return 1;
3443}
3444
3445static int
3446compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3447{
3448 struct fblockinfo *f;
3449 if (c->u->u_nfblocks >= CO_MAXBLOCKS)
3450 return 0;
3451 f = &c->u->u_fblock[c->u->u_nfblocks++];
3452 f->fb_type = t;
3453 f->fb_block = b;
3454 return 1;
3455}
3456
3457static void
3458compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3459{
3460 struct compiler_unit *u = c->u;
3461 assert(u->u_nfblocks > 0);
3462 u->u_nfblocks--;
3463 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3464 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3465}
3466
3467/* Raises a SyntaxError and returns 0.
3468 If something goes wrong, a different exception may be raised.
3469*/
3470
3471static int
3472compiler_error(struct compiler *c, const char *errstr)
3473{
3474 PyObject *loc;
3475 PyObject *u = NULL, *v = NULL;
3476
3477 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3478 if (!loc) {
3479 Py_INCREF(Py_None);
3480 loc = Py_None;
3481 }
3482 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3483 Py_None, loc);
3484 if (!u)
3485 goto exit;
3486 v = Py_BuildValue("(zO)", errstr, u);
3487 if (!v)
3488 goto exit;
3489 PyErr_SetObject(PyExc_SyntaxError, v);
3490 exit:
3491 Py_DECREF(loc);
3492 Py_XDECREF(u);
3493 Py_XDECREF(v);
3494 return 0;
3495}
3496
3497static int
3498compiler_handle_subscr(struct compiler *c, const char *kind,
3499 expr_context_ty ctx)
3500{
3501 int op = 0;
3502
3503 /* XXX this code is duplicated */
3504 switch (ctx) {
3505 case AugLoad: /* fall through to Load */
3506 case Load: op = BINARY_SUBSCR; break;
3507 case AugStore:/* fall through to Store */
3508 case Store: op = STORE_SUBSCR; break;
3509 case Del: op = DELETE_SUBSCR; break;
3510 case Param:
3511 fprintf(stderr,
3512 "invalid %s kind %d in subscript\n",
3513 kind, ctx);
3514 return 0;
3515 }
3516 if (ctx == AugLoad) {
3517 ADDOP_I(c, DUP_TOPX, 2);
3518 }
3519 else if (ctx == AugStore) {
3520 ADDOP(c, ROT_THREE);
3521 }
3522 ADDOP(c, op);
3523 return 1;
3524}
3525
3526static int
3527compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3528{
3529 int n = 2;
3530 assert(s->kind == Slice_kind);
3531
3532 /* only handles the cases where BUILD_SLICE is emitted */
3533 if (s->v.Slice.lower) {
3534 VISIT(c, expr, s->v.Slice.lower);
3535 }
3536 else {
3537 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3538 }
3539
3540 if (s->v.Slice.upper) {
3541 VISIT(c, expr, s->v.Slice.upper);
3542 }
3543 else {
3544 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3545 }
3546
3547 if (s->v.Slice.step) {
3548 n++;
3549 VISIT(c, expr, s->v.Slice.step);
3550 }
3551 ADDOP_I(c, BUILD_SLICE, n);
3552 return 1;
3553}
3554
3555static int
3556compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3557{
3558 int op = 0, slice_offset = 0, stack_count = 0;
3559
3560 assert(s->v.Slice.step == NULL);
3561 if (s->v.Slice.lower) {
3562 slice_offset++;
3563 stack_count++;
3564 if (ctx != AugStore)
3565 VISIT(c, expr, s->v.Slice.lower);
3566 }
3567 if (s->v.Slice.upper) {
3568 slice_offset += 2;
3569 stack_count++;
3570 if (ctx != AugStore)
3571 VISIT(c, expr, s->v.Slice.upper);
3572 }
3573
3574 if (ctx == AugLoad) {
3575 switch (stack_count) {
3576 case 0: ADDOP(c, DUP_TOP); break;
3577 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3578 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3579 }
3580 }
3581 else if (ctx == AugStore) {
3582 switch (stack_count) {
3583 case 0: ADDOP(c, ROT_TWO); break;
3584 case 1: ADDOP(c, ROT_THREE); break;
3585 case 2: ADDOP(c, ROT_FOUR); break;
3586 }
3587 }
3588
3589 switch (ctx) {
3590 case AugLoad: /* fall through to Load */
3591 case Load: op = SLICE; break;
3592 case AugStore:/* fall through to Store */
3593 case Store: op = STORE_SLICE; break;
3594 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003595 case Param:
3596 PyErr_SetString(PyExc_SystemError,
3597 "param invalid in simple slice");
3598 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003599 }
3600
3601 ADDOP(c, op + slice_offset);
3602 return 1;
3603}
3604
3605static int
3606compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3607 expr_context_ty ctx)
3608{
3609 switch (s->kind) {
3610 case Ellipsis_kind:
3611 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3612 break;
3613 case Slice_kind:
3614 return compiler_slice(c, s, ctx);
3615 break;
3616 case Index_kind:
3617 VISIT(c, expr, s->v.Index.value);
3618 break;
3619 case ExtSlice_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00003620 PyErr_SetString(PyExc_SystemError,
3621 "extended slice invalid in nested slice");
3622 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003623 }
3624 return 1;
3625}
3626
3627
3628static int
3629compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3630{
3631 switch (s->kind) {
3632 case Ellipsis_kind:
3633 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3634 break;
3635 case Slice_kind:
3636 if (!s->v.Slice.step)
3637 return compiler_simple_slice(c, s, ctx);
3638 if (!compiler_slice(c, s, ctx))
3639 return 0;
3640 if (ctx == AugLoad) {
3641 ADDOP_I(c, DUP_TOPX, 2);
3642 }
3643 else if (ctx == AugStore) {
3644 ADDOP(c, ROT_THREE);
3645 }
3646 return compiler_handle_subscr(c, "slice", ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003647 case ExtSlice_kind: {
3648 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3649 for (i = 0; i < n; i++) {
3650 slice_ty sub = asdl_seq_GET(s->v.ExtSlice.dims, i);
3651 if (!compiler_visit_nested_slice(c, sub, ctx))
3652 return 0;
3653 }
3654 ADDOP_I(c, BUILD_TUPLE, n);
3655 return compiler_handle_subscr(c, "extended slice", ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003656 }
3657 case Index_kind:
3658 if (ctx != AugStore)
3659 VISIT(c, expr, s->v.Index.value);
3660 return compiler_handle_subscr(c, "index", ctx);
3661 }
3662 return 1;
3663}
3664
3665/* do depth-first search of basic block graph, starting with block.
3666 post records the block indices in post-order.
3667
3668 XXX must handle implicit jumps from one block to next
3669*/
3670
3671static void
3672dfs(struct compiler *c, basicblock *b, struct assembler *a)
3673{
3674 int i;
3675 struct instr *instr = NULL;
3676
3677 if (b->b_seen)
3678 return;
3679 b->b_seen = 1;
3680 if (b->b_next != NULL)
3681 dfs(c, b->b_next, a);
3682 for (i = 0; i < b->b_iused; i++) {
3683 instr = &b->b_instr[i];
3684 if (instr->i_jrel || instr->i_jabs)
3685 dfs(c, instr->i_target, a);
3686 }
3687 a->a_postorder[a->a_nblocks++] = b;
3688}
3689
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003690static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003691stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3692{
3693 int i;
3694 struct instr *instr;
3695 if (b->b_seen || b->b_startdepth >= depth)
3696 return maxdepth;
3697 b->b_seen = 1;
3698 b->b_startdepth = depth;
3699 for (i = 0; i < b->b_iused; i++) {
3700 instr = &b->b_instr[i];
3701 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3702 if (depth > maxdepth)
3703 maxdepth = depth;
3704 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3705 if (instr->i_jrel || instr->i_jabs) {
3706 maxdepth = stackdepth_walk(c, instr->i_target,
3707 depth, maxdepth);
3708 if (instr->i_opcode == JUMP_ABSOLUTE ||
3709 instr->i_opcode == JUMP_FORWARD) {
3710 goto out; /* remaining code is dead */
3711 }
3712 }
3713 }
3714 if (b->b_next)
3715 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3716out:
3717 b->b_seen = 0;
3718 return maxdepth;
3719}
3720
3721/* Find the flow path that needs the largest stack. We assume that
3722 * cycles in the flow graph have no net effect on the stack depth.
3723 */
3724static int
3725stackdepth(struct compiler *c)
3726{
3727 basicblock *b, *entryblock;
3728 entryblock = NULL;
3729 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3730 b->b_seen = 0;
3731 b->b_startdepth = INT_MIN;
3732 entryblock = b;
3733 }
3734 return stackdepth_walk(c, entryblock, 0, 0);
3735}
3736
3737static int
3738assemble_init(struct assembler *a, int nblocks, int firstlineno)
3739{
3740 memset(a, 0, sizeof(struct assembler));
3741 a->a_lineno = firstlineno;
3742 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3743 if (!a->a_bytecode)
3744 return 0;
3745 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3746 if (!a->a_lnotab)
3747 return 0;
3748 a->a_postorder = (basicblock **)PyObject_Malloc(
3749 sizeof(basicblock *) * nblocks);
3750 if (!a->a_postorder)
3751 return 0;
3752 return 1;
3753}
3754
3755static void
3756assemble_free(struct assembler *a)
3757{
3758 Py_XDECREF(a->a_bytecode);
3759 Py_XDECREF(a->a_lnotab);
3760 if (a->a_postorder)
3761 PyObject_Free(a->a_postorder);
3762}
3763
3764/* Return the size of a basic block in bytes. */
3765
3766static int
3767instrsize(struct instr *instr)
3768{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003769 if (!instr->i_hasarg)
3770 return 1;
3771 if (instr->i_oparg > 0xffff)
3772 return 6;
3773 return 3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003774}
3775
3776static int
3777blocksize(basicblock *b)
3778{
3779 int i;
3780 int size = 0;
3781
3782 for (i = 0; i < b->b_iused; i++)
3783 size += instrsize(&b->b_instr[i]);
3784 return size;
3785}
3786
3787/* All about a_lnotab.
3788
3789c_lnotab is an array of unsigned bytes disguised as a Python string.
3790It is used to map bytecode offsets to source code line #s (when needed
3791for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003792
Tim Peters2a7f3842001-06-09 09:26:21 +00003793The array is conceptually a list of
3794 (bytecode offset increment, line number increment)
3795pairs. The details are important and delicate, best illustrated by example:
3796
3797 byte code offset source code line number
3798 0 1
3799 6 2
3800 50 7
3801 350 307
3802 361 308
3803
3804The first trick is that these numbers aren't stored, only the increments
3805from one row to the next (this doesn't really work, but it's a start):
3806
3807 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3808
3809The second trick is that an unsigned byte can't hold negative values, or
3810values larger than 255, so (a) there's a deep assumption that byte code
3811offsets and their corresponding line #s both increase monotonically, and (b)
3812if at least one column jumps by more than 255 from one row to the next, more
3813than one pair is written to the table. In case #b, there's no way to know
3814from looking at the table later how many were written. That's the delicate
3815part. A user of c_lnotab desiring to find the source line number
3816corresponding to a bytecode address A should do something like this
3817
3818 lineno = addr = 0
3819 for addr_incr, line_incr in c_lnotab:
3820 addr += addr_incr
3821 if addr > A:
3822 return lineno
3823 lineno += line_incr
3824
3825In order for this to work, when the addr field increments by more than 255,
3826the line # increment in each pair generated must be 0 until the remaining addr
3827increment is < 256. So, in the example above, com_set_lineno should not (as
3828was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
3829255, 0, 45, 255, 0, 45.
3830*/
3831
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003832static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003833assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003834{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003835 int d_bytecode, d_lineno;
3836 int len;
3837 char *lnotab;
3838
3839 d_bytecode = a->a_offset - a->a_lineno_off;
3840 d_lineno = i->i_lineno - a->a_lineno;
3841
3842 assert(d_bytecode >= 0);
3843 assert(d_lineno >= 0);
3844
3845 if (d_lineno == 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003846 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003847
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003848 if (d_bytecode > 255) {
3849 int i, nbytes, ncodes = d_bytecode / 255;
3850 nbytes = a->a_lnotab_off + 2 * ncodes;
3851 len = PyString_GET_SIZE(a->a_lnotab);
3852 if (nbytes >= len) {
3853 if (len * 2 < nbytes)
3854 len = nbytes;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003855 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003856 len *= 2;
3857 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3858 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003859 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003860 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3861 for (i = 0; i < ncodes; i++) {
3862 *lnotab++ = 255;
3863 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003864 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003865 d_bytecode -= ncodes * 255;
3866 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003867 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003868 assert(d_bytecode <= 255);
3869 if (d_lineno > 255) {
3870 int i, nbytes, ncodes = d_lineno / 255;
3871 nbytes = a->a_lnotab_off + 2 * ncodes;
3872 len = PyString_GET_SIZE(a->a_lnotab);
3873 if (nbytes >= len) {
3874 if (len * 2 < nbytes)
3875 len = nbytes;
Guido van Rossum635abd21997-01-06 22:56:52 +00003876 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003877 len *= 2;
3878 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3879 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003880 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003881 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3882 *lnotab++ = 255;
3883 *lnotab++ = d_bytecode;
3884 d_bytecode = 0;
3885 for (i = 1; i < ncodes; i++) {
3886 *lnotab++ = 255;
3887 *lnotab++ = 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003888 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003889 d_lineno -= ncodes * 255;
3890 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003891 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003892
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003893 len = PyString_GET_SIZE(a->a_lnotab);
3894 if (a->a_lnotab_off + 2 >= len) {
3895 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003896 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003897 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003898 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003899
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003900 a->a_lnotab_off += 2;
3901 if (d_bytecode) {
3902 *lnotab++ = d_bytecode;
3903 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003904 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003905 else { /* First line of a block; def stmt, etc. */
3906 *lnotab++ = 0;
3907 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003908 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003909 a->a_lineno = i->i_lineno;
3910 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003911 return 1;
3912}
3913
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003914/* assemble_emit()
3915 Extend the bytecode with a new instruction.
3916 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003917*/
3918
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003919static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003920assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003921{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003922 int size, arg = 0, ext = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003923 int len = PyString_GET_SIZE(a->a_bytecode);
3924 char *code;
3925
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003926 size = instrsize(i);
3927 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003928 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003929 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003930 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003931 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003932 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003933 if (a->a_offset + size >= len) {
3934 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003935 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003936 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003937 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3938 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003939 if (size == 6) {
3940 assert(i->i_hasarg);
3941 *code++ = (char)EXTENDED_ARG;
3942 *code++ = ext & 0xff;
3943 *code++ = ext >> 8;
3944 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003945 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003946 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003947 if (i->i_hasarg) {
3948 assert(size == 3 || size == 6);
3949 *code++ = arg & 0xff;
3950 *code++ = arg >> 8;
3951 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003952 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003953}
3954
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003955static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003956assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003957{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003958 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003959 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003960 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003961
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003962 /* Compute the size of each block and fixup jump args.
3963 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003964start:
3965 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003966 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003967 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003968 bsize = blocksize(b);
3969 b->b_offset = totsize;
3970 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003971 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003972 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003973 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3974 bsize = b->b_offset;
3975 for (i = 0; i < b->b_iused; i++) {
3976 struct instr *instr = &b->b_instr[i];
3977 /* Relative jumps are computed relative to
3978 the instruction pointer after fetching
3979 the jump instruction.
3980 */
3981 bsize += instrsize(instr);
3982 if (instr->i_jabs)
3983 instr->i_oparg = instr->i_target->b_offset;
3984 else if (instr->i_jrel) {
3985 int delta = instr->i_target->b_offset - bsize;
3986 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003987 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003988 else
3989 continue;
3990 if (instr->i_oparg > 0xffff)
3991 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003992 }
3993 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003994
3995 /* XXX: This is an awful hack that could hurt performance, but
3996 on the bright side it should work until we come up
3997 with a better solution.
3998
3999 In the meantime, should the goto be dropped in favor
4000 of a loop?
4001
4002 The issue is that in the first loop blocksize() is called
4003 which calls instrsize() which requires i_oparg be set
4004 appropriately. There is a bootstrap problem because
4005 i_oparg is calculated in the second loop above.
4006
4007 So we loop until we stop seeing new EXTENDED_ARGs.
4008 The only EXTENDED_ARGs that could be popping up are
4009 ones in jump instructions. So this should converge
4010 fairly quickly.
4011 */
4012 if (last_extended_arg_count != extended_arg_count) {
4013 last_extended_arg_count = extended_arg_count;
4014 goto start;
4015 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004016}
4017
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004018static PyObject *
4019dict_keys_inorder(PyObject *dict, int offset)
4020{
4021 PyObject *tuple, *k, *v;
4022 int i, pos = 0, size = PyDict_Size(dict);
4023
4024 tuple = PyTuple_New(size);
4025 if (tuple == NULL)
4026 return NULL;
4027 while (PyDict_Next(dict, &pos, &k, &v)) {
4028 i = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004029 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004030 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004031 assert((i - offset) < size);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004032 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004033 PyTuple_SET_ITEM(tuple, i - offset, k);
4034 }
4035 return tuple;
4036}
4037
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004038static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004039compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004040{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004041 PySTEntryObject *ste = c->u->u_ste;
4042 int flags = 0, n;
4043 if (ste->ste_type != ModuleBlock)
4044 flags |= CO_NEWLOCALS;
4045 if (ste->ste_type == FunctionBlock) {
4046 if (!ste->ste_unoptimized)
4047 flags |= CO_OPTIMIZED;
4048 if (ste->ste_nested)
4049 flags |= CO_NESTED;
4050 if (ste->ste_generator)
4051 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004052 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004053 if (ste->ste_varargs)
4054 flags |= CO_VARARGS;
4055 if (ste->ste_varkeywords)
4056 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00004057 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004058 flags |= CO_GENERATOR;
4059 if (c->c_flags->cf_flags & CO_FUTURE_DIVISION)
4060 flags |= CO_FUTURE_DIVISION;
4061 n = PyDict_Size(c->u->u_freevars);
4062 if (n < 0)
4063 return -1;
4064 if (n == 0) {
4065 n = PyDict_Size(c->u->u_cellvars);
4066 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004067 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004068 if (n == 0) {
4069 flags |= CO_NOFREE;
4070 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004071 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004072
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004073 return flags;
4074}
4075
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004076static PyCodeObject *
4077makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004078{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004079 PyObject *tmp;
4080 PyCodeObject *co = NULL;
4081 PyObject *consts = NULL;
4082 PyObject *names = NULL;
4083 PyObject *varnames = NULL;
4084 PyObject *filename = NULL;
4085 PyObject *name = NULL;
4086 PyObject *freevars = NULL;
4087 PyObject *cellvars = NULL;
4088 PyObject *bytecode = NULL;
4089 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004090
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004091 tmp = dict_keys_inorder(c->u->u_consts, 0);
4092 if (!tmp)
4093 goto error;
4094 consts = PySequence_List(tmp); /* optimize_code requires a list */
4095 Py_DECREF(tmp);
4096
4097 names = dict_keys_inorder(c->u->u_names, 0);
4098 varnames = dict_keys_inorder(c->u->u_varnames, 0);
4099 if (!consts || !names || !varnames)
4100 goto error;
4101
4102 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4103 if (!cellvars)
4104 goto error;
4105 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4106 if (!freevars)
4107 goto error;
4108 filename = PyString_FromString(c->c_filename);
4109 if (!filename)
4110 goto error;
4111
4112 nlocals = PyDict_Size(c->u->u_varnames);
4113 flags = compute_code_flags(c);
4114 if (flags < 0)
4115 goto error;
4116
4117 bytecode = optimize_code(a->a_bytecode, consts, names, a->a_lnotab);
4118 if (!bytecode)
4119 goto error;
4120
4121 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4122 if (!tmp)
4123 goto error;
4124 Py_DECREF(consts);
4125 consts = tmp;
4126
4127 co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags,
4128 bytecode, consts, names, varnames,
4129 freevars, cellvars,
4130 filename, c->u->u_name,
4131 c->u->u_firstlineno,
4132 a->a_lnotab);
4133 error:
4134 Py_XDECREF(consts);
4135 Py_XDECREF(names);
4136 Py_XDECREF(varnames);
4137 Py_XDECREF(filename);
4138 Py_XDECREF(name);
4139 Py_XDECREF(freevars);
4140 Py_XDECREF(cellvars);
4141 Py_XDECREF(bytecode);
4142 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004143}
4144
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004145static PyCodeObject *
4146assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004147{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004148 basicblock *b, *entryblock;
4149 struct assembler a;
4150 int i, j, nblocks;
4151 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004152
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004153 /* Make sure every block that falls off the end returns None.
4154 XXX NEXT_BLOCK() isn't quite right, because if the last
4155 block ends with a jump or return b_next shouldn't set.
4156 */
4157 if (!c->u->u_curblock->b_return) {
4158 NEXT_BLOCK(c);
4159 if (addNone)
4160 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4161 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004162 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004163
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004164 nblocks = 0;
4165 entryblock = NULL;
4166 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4167 nblocks++;
4168 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004169 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004170
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004171 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4172 goto error;
4173 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004174
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004175 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004176 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004177
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004178 /* Emit code in reverse postorder from dfs. */
4179 for (i = a.a_nblocks - 1; i >= 0; i--) {
4180 basicblock *b = a.a_postorder[i];
4181 for (j = 0; j < b->b_iused; j++)
4182 if (!assemble_emit(&a, &b->b_instr[j]))
4183 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004184 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004185
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004186 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4187 goto error;
4188 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
4189 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004190
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004191 co = makecode(c, &a);
4192 error:
4193 assemble_free(&a);
4194 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004195}