blob: 4da81c0208fa5b7a0236967436184eb252eab843 [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 }
335 Py_DECREF(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000336 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000337 return dict;
338}
339
340/* Return new dict containing names from src that match scope(s).
341
342 src is a symbol table dictionary. If the scope of a name matches
343 either scope_type or flag is set, insert it into the new dict. The
344 values are integers, starting at offset and increasing by one for
345 each key.
346*/
347
348static PyObject *
349dictbytype(PyObject *src, int scope_type, int flag, int offset)
350{
351 int pos = 0, i = offset, scope;
352 PyObject *k, *v, *dest = PyDict_New();
353
354 assert(offset >= 0);
355 if (dest == NULL)
356 return NULL;
357
358 while (PyDict_Next(src, &pos, &k, &v)) {
359 /* XXX this should probably be a macro in symtable.h */
360 assert(PyInt_Check(v));
361 scope = (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
362
363 if (scope == scope_type || PyInt_AS_LONG(v) & flag) {
364 PyObject *tuple, *item = PyInt_FromLong(i);
365 if (item == NULL) {
366 Py_DECREF(dest);
367 return NULL;
368 }
369 i++;
370 tuple = Py_BuildValue("(OO)", k, k->ob_type);
371 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
372 Py_DECREF(item);
373 Py_DECREF(dest);
374 Py_XDECREF(tuple);
375 return NULL;
376 }
377 Py_DECREF(item);
378 Py_DECREF(tuple);
379 }
380 }
381 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000382}
383
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000384/* Begin: Peephole optimizations ----------------------------------------- */
385
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000386#define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1]))
387#define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD)
Raymond Hettinger5b75c382003-03-28 12:05:00 +0000388#define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP)
389#define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000390#define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000391#define CODESIZE(op) (HAS_ARG(op) ? 3 : 1)
392#define ISBASICBLOCK(blocks, start, bytes) (blocks[start]==blocks[start+bytes-1])
393
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000394/* Replace LOAD_CONST c1. LOAD_CONST c2 ... LOAD_CONST cn BUILD_TUPLE n
395 with LOAD_CONST (c1, c2, ... cn).
396 The consts table must still be in list form so that the
397 new constant (c1, c2, ... cn) can be appended.
398 Called with codestr pointing to the first LOAD_CONST.
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000399 Bails out with no change if one or more of the LOAD_CONSTs is missing.
400 Also works for BUILD_LIST when followed by an "in" or "not in" test.
401*/
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000402static int
403tuple_of_constants(unsigned char *codestr, int n, PyObject *consts)
404{
405 PyObject *newconst, *constant;
406 int i, arg, len_consts;
407
408 /* Pre-conditions */
409 assert(PyList_CheckExact(consts));
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000410 assert(codestr[n*3] == BUILD_TUPLE || codestr[n*3] == BUILD_LIST);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000411 assert(GETARG(codestr, (n*3)) == n);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000412 for (i=0 ; i<n ; i++)
Raymond Hettingereffb3932004-10-30 08:55:08 +0000413 assert(codestr[i*3] == LOAD_CONST);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000414
415 /* Buildup new tuple of constants */
416 newconst = PyTuple_New(n);
417 if (newconst == NULL)
418 return 0;
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000419 len_consts = PyList_GET_SIZE(consts);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000420 for (i=0 ; i<n ; i++) {
421 arg = GETARG(codestr, (i*3));
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000422 assert(arg < len_consts);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000423 constant = PyList_GET_ITEM(consts, arg);
424 Py_INCREF(constant);
425 PyTuple_SET_ITEM(newconst, i, constant);
426 }
427
428 /* Append folded constant onto consts */
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000429 if (PyList_Append(consts, newconst)) {
430 Py_DECREF(newconst);
431 return 0;
432 }
433 Py_DECREF(newconst);
434
435 /* Write NOPs over old LOAD_CONSTS and
436 add a new LOAD_CONST newconst on top of the BUILD_TUPLE n */
437 memset(codestr, NOP, n*3);
438 codestr[n*3] = LOAD_CONST;
439 SETARG(codestr, (n*3), len_consts);
440 return 1;
441}
442
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000443/* Replace LOAD_CONST c1. LOAD_CONST c2 BINOP
444 with LOAD_CONST binop(c1,c2)
445 The consts table must still be in list form so that the
446 new constant can be appended.
447 Called with codestr pointing to the first LOAD_CONST.
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000448 Abandons the transformation if the folding fails (i.e. 1+'a').
449 If the new constant is a sequence, only folds when the size
450 is below a threshold value. That keeps pyc files from
451 becoming large in the presence of code like: (None,)*1000.
452*/
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000453static int
454fold_binops_on_constants(unsigned char *codestr, PyObject *consts)
455{
456 PyObject *newconst, *v, *w;
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000457 int len_consts, opcode, size;
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000458
459 /* Pre-conditions */
460 assert(PyList_CheckExact(consts));
461 assert(codestr[0] == LOAD_CONST);
462 assert(codestr[3] == LOAD_CONST);
463
464 /* Create new constant */
465 v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
466 w = PyList_GET_ITEM(consts, GETARG(codestr, 3));
467 opcode = codestr[6];
468 switch (opcode) {
469 case BINARY_POWER:
470 newconst = PyNumber_Power(v, w, Py_None);
471 break;
472 case BINARY_MULTIPLY:
473 newconst = PyNumber_Multiply(v, w);
474 break;
475 case BINARY_DIVIDE:
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000476 /* Cannot fold this operation statically since
477 the result can depend on the run-time presence of the -Qnew flag */
Armin Rigo664b43b2005-01-07 18:10:51 +0000478 return 0;
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000479 case BINARY_TRUE_DIVIDE:
480 newconst = PyNumber_TrueDivide(v, w);
481 break;
482 case BINARY_FLOOR_DIVIDE:
483 newconst = PyNumber_FloorDivide(v, w);
484 break;
485 case BINARY_MODULO:
486 newconst = PyNumber_Remainder(v, w);
487 break;
488 case BINARY_ADD:
489 newconst = PyNumber_Add(v, w);
490 break;
491 case BINARY_SUBTRACT:
492 newconst = PyNumber_Subtract(v, w);
493 break;
494 case BINARY_SUBSCR:
495 newconst = PyObject_GetItem(v, w);
496 break;
497 case BINARY_LSHIFT:
498 newconst = PyNumber_Lshift(v, w);
499 break;
500 case BINARY_RSHIFT:
501 newconst = PyNumber_Rshift(v, w);
502 break;
503 case BINARY_AND:
504 newconst = PyNumber_And(v, w);
505 break;
506 case BINARY_XOR:
507 newconst = PyNumber_Xor(v, w);
508 break;
509 case BINARY_OR:
510 newconst = PyNumber_Or(v, w);
511 break;
512 default:
513 /* Called with an unknown opcode */
514 assert(0);
515 return 0;
516 }
517 if (newconst == NULL) {
518 PyErr_Clear();
519 return 0;
520 }
Raymond Hettinger9feb2672005-01-26 12:50:05 +0000521 size = PyObject_Size(newconst);
522 if (size == -1)
523 PyErr_Clear();
524 else if (size > 20) {
525 Py_DECREF(newconst);
526 return 0;
527 }
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000528
529 /* Append folded constant into consts table */
530 len_consts = PyList_GET_SIZE(consts);
531 if (PyList_Append(consts, newconst)) {
532 Py_DECREF(newconst);
533 return 0;
534 }
535 Py_DECREF(newconst);
536
537 /* Write NOP NOP NOP NOP LOAD_CONST newconst */
538 memset(codestr, NOP, 4);
539 codestr[4] = LOAD_CONST;
540 SETARG(codestr, 4, len_consts);
541 return 1;
542}
543
Raymond Hettinger80121492005-02-20 12:41:32 +0000544static int
545fold_unaryops_on_constants(unsigned char *codestr, PyObject *consts)
546{
Raymond Hettingere63a0782005-02-23 13:37:55 +0000547 PyObject *newconst=NULL, *v;
Raymond Hettinger80121492005-02-20 12:41:32 +0000548 int len_consts, opcode;
549
550 /* Pre-conditions */
551 assert(PyList_CheckExact(consts));
552 assert(codestr[0] == LOAD_CONST);
553
554 /* Create new constant */
555 v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
556 opcode = codestr[3];
557 switch (opcode) {
558 case UNARY_NEGATIVE:
Raymond Hettingere63a0782005-02-23 13:37:55 +0000559 /* Preserve the sign of -0.0 */
560 if (PyObject_IsTrue(v) == 1)
561 newconst = PyNumber_Negative(v);
Raymond Hettinger80121492005-02-20 12:41:32 +0000562 break;
563 case UNARY_CONVERT:
564 newconst = PyObject_Repr(v);
565 break;
566 case UNARY_INVERT:
567 newconst = PyNumber_Invert(v);
568 break;
569 default:
570 /* Called with an unknown opcode */
571 assert(0);
572 return 0;
573 }
574 if (newconst == NULL) {
575 PyErr_Clear();
576 return 0;
577 }
578
579 /* Append folded constant into consts table */
580 len_consts = PyList_GET_SIZE(consts);
581 if (PyList_Append(consts, newconst)) {
582 Py_DECREF(newconst);
583 return 0;
584 }
585 Py_DECREF(newconst);
586
587 /* Write NOP LOAD_CONST newconst */
588 codestr[0] = NOP;
589 codestr[1] = LOAD_CONST;
590 SETARG(codestr, 1, len_consts);
591 return 1;
592}
593
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000594static unsigned int *
595markblocks(unsigned char *code, int len)
596{
597 unsigned int *blocks = PyMem_Malloc(len*sizeof(int));
Raymond Hettingereffb3932004-10-30 08:55:08 +0000598 int i,j, opcode, blockcnt = 0;
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000599
600 if (blocks == NULL)
601 return NULL;
602 memset(blocks, 0, len*sizeof(int));
Raymond Hettingereffb3932004-10-30 08:55:08 +0000603
604 /* Mark labels in the first pass */
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000605 for (i=0 ; i<len ; i+=CODESIZE(opcode)) {
606 opcode = code[i];
607 switch (opcode) {
608 case FOR_ITER:
609 case JUMP_FORWARD:
610 case JUMP_IF_FALSE:
611 case JUMP_IF_TRUE:
612 case JUMP_ABSOLUTE:
613 case CONTINUE_LOOP:
614 case SETUP_LOOP:
615 case SETUP_EXCEPT:
616 case SETUP_FINALLY:
617 j = GETJUMPTGT(code, i);
Raymond Hettingereffb3932004-10-30 08:55:08 +0000618 blocks[j] = 1;
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000619 break;
620 }
621 }
Raymond Hettingereffb3932004-10-30 08:55:08 +0000622 /* Build block numbers in the second pass */
623 for (i=0 ; i<len ; i++) {
624 blockcnt += blocks[i]; /* increment blockcnt over labels */
625 blocks[i] = blockcnt;
626 }
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000627 return blocks;
628}
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000629
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000630/* Perform basic peephole optimizations to components of a code object.
631 The consts object should still be in list form to allow new constants
632 to be appended.
633
634 To keep the optimizer simple, it bails out (does nothing) for code
635 containing extended arguments or that has a length over 32,700. That
636 allows us to avoid overflow and sign issues. Likewise, it bails when
637 the lineno table has complex encoding for gaps >= 255.
638
639 Optimizations are restricted to simple transformations occuring within a
640 single basic block. All transformations keep the code size the same or
641 smaller. For those that reduce size, the gaps are initially filled with
642 NOPs. Later those NOPs are removed and the jump addresses retargeted in
643 a single pass. Line numbering is adjusted accordingly. */
644
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000645static PyObject *
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000646optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *lineno_obj)
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000647{
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000648 int i, j, codelen, nops, h, adj;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000649 int tgt, tgttgt, opcode;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000650 unsigned char *codestr = NULL;
651 unsigned char *lineno;
652 int *addrmap = NULL;
653 int new_line, cum_orig_line, last_line, tabsiz;
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000654 int cumlc=0, lastlc=0; /* Count runs of consecutive LOAD_CONST codes */
Raymond Hettingereffb3932004-10-30 08:55:08 +0000655 unsigned int *blocks = NULL;
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000656 char *name;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000657
Raymond Hettingereffb3932004-10-30 08:55:08 +0000658 /* Bail out if an exception is set */
659 if (PyErr_Occurred())
660 goto exitUnchanged;
661
Raymond Hettinger1792bfb2004-08-25 17:19:38 +0000662 /* Bypass optimization when the lineno table is too complex */
663 assert(PyString_Check(lineno_obj));
Brett Cannonc9371d42005-06-25 08:23:41 +0000664 lineno = (unsigned char*)PyString_AS_STRING(lineno_obj);
Raymond Hettinger1792bfb2004-08-25 17:19:38 +0000665 tabsiz = PyString_GET_SIZE(lineno_obj);
666 if (memchr(lineno, 255, tabsiz) != NULL)
667 goto exitUnchanged;
668
Raymond Hettingera12fa142004-08-24 04:34:16 +0000669 /* Avoid situations where jump retargeting could overflow */
Raymond Hettinger06cc9732004-09-28 17:22:12 +0000670 assert(PyString_Check(code));
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000671 codelen = PyString_Size(code);
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000672 if (codelen > 32700)
Raymond Hettingera12fa142004-08-24 04:34:16 +0000673 goto exitUnchanged;
674
675 /* Make a modifiable copy of the code string */
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000676 codestr = PyMem_Malloc(codelen);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000677 if (codestr == NULL)
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000678 goto exitUnchanged;
679 codestr = memcpy(codestr, PyString_AS_STRING(code), codelen);
Raymond Hettinger98bd1812004-08-06 19:46:34 +0000680
Raymond Hettinger07359a72005-02-21 20:03:14 +0000681 /* Verify that RETURN_VALUE terminates the codestring. This allows
682 the various transformation patterns to look ahead several
683 instructions without additional checks to make sure they are not
684 looking beyond the end of the code string.
685 */
686 if (codestr[codelen-1] != RETURN_VALUE)
687 goto exitUnchanged;
688
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000689 /* Mapping to new jump targets after NOPs are removed */
690 addrmap = PyMem_Malloc(codelen * sizeof(int));
691 if (addrmap == NULL)
692 goto exitUnchanged;
693
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000694 blocks = markblocks(codestr, codelen);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000695 if (blocks == NULL)
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000696 goto exitUnchanged;
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000697 assert(PyList_Check(consts));
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000698
Raymond Hettinger099ecfb2004-11-01 15:19:11 +0000699 for (i=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000700 opcode = codestr[i];
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000701
702 lastlc = cumlc;
703 cumlc = 0;
704
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000705 switch (opcode) {
706
Raymond Hettinger43ea47f2004-06-24 09:25:39 +0000707 /* Replace UNARY_NOT JUMP_IF_FALSE POP_TOP with
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000708 with JUMP_IF_TRUE POP_TOP */
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000709 case UNARY_NOT:
710 if (codestr[i+1] != JUMP_IF_FALSE ||
711 codestr[i+4] != POP_TOP ||
712 !ISBASICBLOCK(blocks,i,5))
713 continue;
714 tgt = GETJUMPTGT(codestr, (i+1));
715 if (codestr[tgt] != POP_TOP)
716 continue;
Raymond Hettinger43ea47f2004-06-24 09:25:39 +0000717 j = GETARG(codestr, i+1) + 1;
718 codestr[i] = JUMP_IF_TRUE;
719 SETARG(codestr, i, j);
720 codestr[i+3] = POP_TOP;
721 codestr[i+4] = NOP;
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000722 break;
723
724 /* not a is b --> a is not b
725 not a in b --> a not in b
726 not a is not b --> a is b
Raymond Hettingerb615bf02005-02-10 01:42:32 +0000727 not a not in b --> a in b
Raymond Hettingera1645742005-02-06 22:05:42 +0000728 */
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000729 case COMPARE_OP:
730 j = GETARG(codestr, i);
731 if (j < 6 || j > 9 ||
732 codestr[i+3] != UNARY_NOT ||
733 !ISBASICBLOCK(blocks,i,4))
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000734 continue;
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000735 SETARG(codestr, i, (j^1));
736 codestr[i+3] = NOP;
Tim Petersdb5860b2004-07-17 05:00:52 +0000737 break;
738
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000739 /* Replace LOAD_GLOBAL/LOAD_NAME None with LOAD_CONST None */
740 case LOAD_NAME:
741 case LOAD_GLOBAL:
742 j = GETARG(codestr, i);
743 name = PyString_AsString(PyTuple_GET_ITEM(names, j));
744 if (name == NULL || strcmp(name, "None") != 0)
745 continue;
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000746 for (j=0 ; j < PyList_GET_SIZE(consts) ; j++) {
747 if (PyList_GET_ITEM(consts, j) == Py_None) {
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000748 codestr[i] = LOAD_CONST;
749 SETARG(codestr, i, j);
Raymond Hettinger5dec0962004-11-02 04:20:10 +0000750 cumlc = lastlc + 1;
Raymond Hettinger76d962d2004-07-16 12:16:48 +0000751 break;
752 }
753 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000754 break;
755
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000756 /* Skip over LOAD_CONST trueconst JUMP_IF_FALSE xx POP_TOP */
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000757 case LOAD_CONST:
Raymond Hettinger23109ef2004-10-26 08:59:14 +0000758 cumlc = lastlc + 1;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000759 j = GETARG(codestr, i);
760 if (codestr[i+3] != JUMP_IF_FALSE ||
761 codestr[i+6] != POP_TOP ||
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000762 !ISBASICBLOCK(blocks,i,7) ||
763 !PyObject_IsTrue(PyList_GET_ITEM(consts, j)))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000764 continue;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000765 memset(codestr+i, NOP, 7);
Raymond Hettinger5dec0962004-11-02 04:20:10 +0000766 cumlc = 0;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000767 break;
768
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000769 /* Try to fold tuples of constants (includes a case for lists
770 which are only used for "in" and "not in" tests).
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000771 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000772 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
773 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000774 case BUILD_TUPLE:
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000775 case BUILD_LIST:
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000776 j = GETARG(codestr, i);
777 h = i - 3 * j;
778 if (h >= 0 &&
Raymond Hettingereffb3932004-10-30 08:55:08 +0000779 j <= lastlc &&
Brett Cannon5dc8ced2005-03-03 07:01:48 +0000780 ((opcode == BUILD_TUPLE &&
781 ISBASICBLOCK(blocks, h, 3*(j+1))) ||
782 (opcode == BUILD_LIST &&
Raymond Hettinger7fcb7862005-02-07 19:32:38 +0000783 codestr[i+3]==COMPARE_OP &&
784 ISBASICBLOCK(blocks, h, 3*(j+2)) &&
Brett Cannon5dc8ced2005-03-03 07:01:48 +0000785 (GETARG(codestr,i+3)==6 ||
786 GETARG(codestr,i+3)==7))) &&
787 tuple_of_constants(&codestr[h], j, consts)) {
Raymond Hettinger5dec0962004-11-02 04:20:10 +0000788 assert(codestr[i] == LOAD_CONST);
789 cumlc = 1;
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000790 break;
791 }
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000792 if (codestr[i+3] != UNPACK_SEQUENCE ||
793 !ISBASICBLOCK(blocks,i,6) ||
794 j != GETARG(codestr, i+3))
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000795 continue;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000796 if (j == 1) {
797 memset(codestr+i, NOP, 6);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000798 } else if (j == 2) {
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000799 codestr[i] = ROT_TWO;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000800 memset(codestr+i+1, NOP, 5);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000801 } else if (j == 3) {
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000802 codestr[i] = ROT_THREE;
803 codestr[i+1] = ROT_TWO;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000804 memset(codestr+i+2, NOP, 4);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000805 }
806 break;
807
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000808 /* Fold binary ops on constants.
809 LOAD_CONST c1 LOAD_CONST c2 BINOP --> LOAD_CONST binop(c1,c2) */
810 case BINARY_POWER:
811 case BINARY_MULTIPLY:
Raymond Hettingerc34f8672005-01-02 06:17:33 +0000812 case BINARY_TRUE_DIVIDE:
813 case BINARY_FLOOR_DIVIDE:
814 case BINARY_MODULO:
815 case BINARY_ADD:
816 case BINARY_SUBTRACT:
817 case BINARY_SUBSCR:
818 case BINARY_LSHIFT:
819 case BINARY_RSHIFT:
820 case BINARY_AND:
821 case BINARY_XOR:
822 case BINARY_OR:
823 if (lastlc >= 2 &&
824 ISBASICBLOCK(blocks, i-6, 7) &&
825 fold_binops_on_constants(&codestr[i-6], consts)) {
826 i -= 2;
827 assert(codestr[i] == LOAD_CONST);
828 cumlc = 1;
829 }
830 break;
831
Raymond Hettinger80121492005-02-20 12:41:32 +0000832 /* Fold unary ops on constants.
833 LOAD_CONST c1 UNARY_OP --> LOAD_CONST unary_op(c) */
834 case UNARY_NEGATIVE:
835 case UNARY_CONVERT:
836 case UNARY_INVERT:
837 if (lastlc >= 1 &&
838 ISBASICBLOCK(blocks, i-3, 4) &&
839 fold_unaryops_on_constants(&codestr[i-3], consts)) {
840 i -= 2;
841 assert(codestr[i] == LOAD_CONST);
842 cumlc = 1;
843 }
844 break;
845
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000846 /* Simplify conditional jump to conditional jump where the
847 result of the first test implies the success of a similar
848 test or the failure of the opposite test.
849 Arises in code like:
Raymond Hettingereffb3932004-10-30 08:55:08 +0000850 "if a and b:"
851 "if a or b:"
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000852 "a and b or c"
Armin Rigod7bcf4d2004-10-30 21:08:59 +0000853 "(a and b) and c"
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000854 x:JUMP_IF_FALSE y y:JUMP_IF_FALSE z --> x:JUMP_IF_FALSE z
Raymond Hettinger65d3c052004-08-25 15:15:56 +0000855 x:JUMP_IF_FALSE y y:JUMP_IF_TRUE z --> x:JUMP_IF_FALSE y+3
856 where y+3 is the instruction following the second test.
Raymond Hettingeref0a82b2004-08-25 03:18:29 +0000857 */
858 case JUMP_IF_FALSE:
859 case JUMP_IF_TRUE:
860 tgt = GETJUMPTGT(codestr, i);
861 j = codestr[tgt];
862 if (j == JUMP_IF_FALSE || j == JUMP_IF_TRUE) {
863 if (j == opcode) {
864 tgttgt = GETJUMPTGT(codestr, tgt) - i - 3;
865 SETARG(codestr, i, tgttgt);
866 } else {
867 tgt -= i;
868 SETARG(codestr, i, tgt);
869 }
870 break;
871 }
872 /* Intentional fallthrough */
873
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000874 /* Replace jumps to unconditional jumps */
Raymond Hettinger255a3d02003-04-15 10:35:07 +0000875 case FOR_ITER:
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000876 case JUMP_FORWARD:
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000877 case JUMP_ABSOLUTE:
878 case CONTINUE_LOOP:
879 case SETUP_LOOP:
880 case SETUP_EXCEPT:
881 case SETUP_FINALLY:
882 tgt = GETJUMPTGT(codestr, i);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000883 if (!UNCONDITIONAL_JUMP(codestr[tgt]))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000884 continue;
885 tgttgt = GETJUMPTGT(codestr, tgt);
886 if (opcode == JUMP_FORWARD) /* JMP_ABS can go backwards */
887 opcode = JUMP_ABSOLUTE;
Raymond Hettinger5b75c382003-03-28 12:05:00 +0000888 if (!ABSOLUTE_JUMP(opcode))
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000889 tgttgt -= i + 3; /* Calc relative jump addr */
890 if (tgttgt < 0) /* No backward relative jumps */
891 continue;
892 codestr[i] = opcode;
893 SETARG(codestr, i, tgttgt);
894 break;
895
896 case EXTENDED_ARG:
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000897 goto exitUnchanged;
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000898
899 /* Replace RETURN LOAD_CONST None RETURN with just RETURN */
900 case RETURN_VALUE:
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000901 if (i+4 >= codelen ||
902 codestr[i+4] != RETURN_VALUE ||
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000903 !ISBASICBLOCK(blocks,i,5))
904 continue;
905 memset(codestr+i+1, NOP, 4);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000906 break;
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000907 }
908 }
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000909
910 /* Fixup linenotab */
Raymond Hettinger099ecfb2004-11-01 15:19:11 +0000911 for (i=0, nops=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
912 addrmap[i] = i - nops;
913 if (codestr[i] == NOP)
914 nops++;
915 }
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000916 cum_orig_line = 0;
917 last_line = 0;
918 for (i=0 ; i < tabsiz ; i+=2) {
919 cum_orig_line += lineno[i];
920 new_line = addrmap[cum_orig_line];
Raymond Hettinger1792bfb2004-08-25 17:19:38 +0000921 assert (new_line - last_line < 255);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000922 lineno[i] =((unsigned char)(new_line - last_line));
923 last_line = new_line;
924 }
925
926 /* Remove NOPs and fixup jump targets */
927 for (i=0, h=0 ; i<codelen ; ) {
928 opcode = codestr[i];
929 switch (opcode) {
930 case NOP:
931 i++;
932 continue;
933
934 case JUMP_ABSOLUTE:
935 case CONTINUE_LOOP:
936 j = addrmap[GETARG(codestr, i)];
937 SETARG(codestr, i, j);
938 break;
939
940 case FOR_ITER:
941 case JUMP_FORWARD:
942 case JUMP_IF_FALSE:
943 case JUMP_IF_TRUE:
944 case SETUP_LOOP:
945 case SETUP_EXCEPT:
946 case SETUP_FINALLY:
947 j = addrmap[GETARG(codestr, i) + i + 3] - addrmap[i] - 3;
948 SETARG(codestr, i, j);
949 break;
950 }
951 adj = CODESIZE(opcode);
952 while (adj--)
953 codestr[h++] = codestr[i++];
954 }
Raymond Hettingera12fa142004-08-24 04:34:16 +0000955 assert(h + nops == codelen);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000956
957 code = PyString_FromStringAndSize((char *)codestr, h);
958 PyMem_Free(addrmap);
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000959 PyMem_Free(codestr);
Raymond Hettingerff5bc502004-03-21 15:12:00 +0000960 PyMem_Free(blocks);
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000961 return code;
962
963exitUnchanged:
Raymond Hettingereffb3932004-10-30 08:55:08 +0000964 if (blocks != NULL)
965 PyMem_Free(blocks);
Raymond Hettingerfd2d1f72004-08-23 23:37:48 +0000966 if (addrmap != NULL)
967 PyMem_Free(addrmap);
968 if (codestr != NULL)
969 PyMem_Free(codestr);
Raymond Hettingerf6f575a2003-03-26 01:07:54 +0000970 Py_INCREF(code);
971 return code;
972}
973
Raymond Hettinger2c31a052004-09-22 18:44:21 +0000974/* End: Peephole optimizations ----------------------------------------- */
975
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976/*
977
978Leave this debugging code for just a little longer.
979
980static void
981compiler_display_symbols(PyObject *name, PyObject *symbols)
982{
983 PyObject *key, *value;
984 int flags, pos = 0;
985
986 fprintf(stderr, "block %s\n", PyString_AS_STRING(name));
987 while (PyDict_Next(symbols, &pos, &key, &value)) {
988 flags = PyInt_AsLong(value);
989 fprintf(stderr, "var %s:", PyString_AS_STRING(key));
990 if (flags & DEF_GLOBAL)
991 fprintf(stderr, " declared_global");
992 if (flags & DEF_LOCAL)
993 fprintf(stderr, " local");
994 if (flags & DEF_PARAM)
995 fprintf(stderr, " param");
996 if (flags & DEF_STAR)
997 fprintf(stderr, " stararg");
998 if (flags & DEF_DOUBLESTAR)
999 fprintf(stderr, " starstar");
1000 if (flags & DEF_INTUPLE)
1001 fprintf(stderr, " tuple");
1002 if (flags & DEF_FREE)
1003 fprintf(stderr, " free");
1004 if (flags & DEF_FREE_GLOBAL)
1005 fprintf(stderr, " global");
1006 if (flags & DEF_FREE_CLASS)
1007 fprintf(stderr, " free/class");
1008 if (flags & DEF_IMPORT)
1009 fprintf(stderr, " import");
1010 fprintf(stderr, "\n");
1011 }
1012 fprintf(stderr, "\n");
1013}
1014*/
1015
1016static void
1017compiler_unit_check(struct compiler_unit *u)
1018{
1019 basicblock *block;
1020 for (block = u->u_blocks; block != NULL; block = block->b_list) {
1021 assert(block != (void *)0xcbcbcbcb);
1022 assert(block != (void *)0xfbfbfbfb);
1023 assert(block != (void *)0xdbdbdbdb);
1024 if (block->b_instr != NULL) {
1025 assert(block->b_ialloc > 0);
1026 assert(block->b_iused > 0);
1027 assert(block->b_ialloc >= block->b_iused);
1028 }
1029 else {
1030 assert (block->b_iused == 0);
1031 assert (block->b_ialloc == 0);
1032 }
1033 }
1034}
1035
1036static void
1037compiler_unit_free(struct compiler_unit *u)
1038{
1039 basicblock *b, *next;
1040
1041 compiler_unit_check(u);
1042 b = u->u_blocks;
1043 while (b != NULL) {
1044 if (b->b_instr)
1045 PyObject_Free((void *)b->b_instr);
1046 next = b->b_list;
1047 PyObject_Free((void *)b);
1048 b = next;
1049 }
1050 Py_XDECREF(u->u_ste);
1051 Py_XDECREF(u->u_name);
1052 Py_XDECREF(u->u_consts);
1053 Py_XDECREF(u->u_names);
1054 Py_XDECREF(u->u_varnames);
1055 Py_XDECREF(u->u_freevars);
1056 Py_XDECREF(u->u_cellvars);
1057 Py_XDECREF(u->u_private);
1058 PyObject_Free(u);
1059}
1060
1061static int
1062compiler_enter_scope(struct compiler *c, identifier name, void *key,
1063 int lineno)
1064{
1065 struct compiler_unit *u;
1066
1067 u = PyObject_Malloc(sizeof(struct compiler_unit));
1068 memset(u, 0, sizeof(struct compiler_unit));
1069 u->u_argcount = 0;
1070 u->u_ste = PySymtable_Lookup(c->c_st, key);
1071 if (!u->u_ste) {
1072 compiler_unit_free(u);
1073 return 0;
1074 }
1075 Py_INCREF(name);
1076 u->u_name = name;
1077 u->u_varnames = list2dict(u->u_ste->ste_varnames);
1078 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
1079 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
1080 PyDict_Size(u->u_cellvars));
1081
1082 u->u_blocks = NULL;
1083 u->u_tmpname = 0;
1084 u->u_nfblocks = 0;
1085 u->u_firstlineno = lineno;
1086 u->u_lineno = 0;
1087 u->u_lineno_set = false;
1088 u->u_consts = PyDict_New();
1089 if (!u->u_consts) {
1090 compiler_unit_free(u);
1091 return 0;
1092 }
1093 u->u_names = PyDict_New();
1094 if (!u->u_names) {
1095 compiler_unit_free(u);
1096 return 0;
1097 }
1098
1099 u->u_private = NULL;
1100
1101 /* Push the old compiler_unit on the stack. */
1102 if (c->u) {
1103 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
1104 if (PyList_Append(c->c_stack, wrapper) < 0) {
1105 compiler_unit_free(u);
1106 return 0;
1107 }
1108 Py_DECREF(wrapper);
1109 u->u_private = c->u->u_private;
1110 Py_XINCREF(u->u_private);
1111 }
1112 c->u = u;
1113
1114 c->c_nestlevel++;
1115 if (compiler_use_new_block(c) < 0)
1116 return 0;
1117
1118 return 1;
1119}
1120
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001121static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122compiler_exit_scope(struct compiler *c)
1123{
1124 int n;
1125 PyObject *wrapper;
1126
1127 c->c_nestlevel--;
1128 compiler_unit_free(c->u);
1129 /* Restore c->u to the parent unit. */
1130 n = PyList_GET_SIZE(c->c_stack) - 1;
1131 if (n >= 0) {
1132 wrapper = PyList_GET_ITEM(c->c_stack, n);
1133 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001134 /* we are deleting from a list so this really shouldn't fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135 if (PySequence_DelItem(c->c_stack, n) < 0)
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001136 Py_FatalError("compiler_exit_scope()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137 compiler_unit_check(c->u);
1138 }
1139 else
1140 c->u = NULL;
1141
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142}
1143
1144/* Allocate a new block and return a pointer to it.
1145 Returns NULL on error.
1146*/
1147
1148static basicblock *
1149compiler_new_block(struct compiler *c)
1150{
1151 basicblock *b;
1152 struct compiler_unit *u;
1153
1154 u = c->u;
1155 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
1156 if (b == NULL)
1157 return NULL;
1158 memset((void *)b, 0, sizeof(basicblock));
1159 assert (b->b_next == NULL);
1160 b->b_list = u->u_blocks;
1161 u->u_blocks = b;
1162 return b;
1163}
1164
1165static void
1166compiler_use_block(struct compiler *c, basicblock *block)
1167{
1168 assert (block != NULL);
1169 c->u->u_curblock = block;
1170}
1171
1172static basicblock *
1173compiler_use_new_block(struct compiler *c)
1174{
1175 basicblock *block = compiler_new_block(c);
1176 if (block == NULL)
1177 return NULL;
1178 c->u->u_curblock = block;
1179 return block;
1180}
1181
1182static basicblock *
1183compiler_next_block(struct compiler *c)
1184{
1185 basicblock *block = compiler_new_block(c);
1186 if (block == NULL)
1187 return NULL;
1188 c->u->u_curblock->b_next = block;
1189 c->u->u_curblock = block;
1190 return block;
1191}
1192
1193static basicblock *
1194compiler_use_next_block(struct compiler *c, basicblock *block)
1195{
1196 assert(block != NULL);
1197 c->u->u_curblock->b_next = block;
1198 c->u->u_curblock = block;
1199 return block;
1200}
1201
1202/* Returns the offset of the next instruction in the current block's
1203 b_instr array. Resizes the b_instr as necessary.
1204 Returns -1 on failure.
1205 */
1206
1207static int
1208compiler_next_instr(struct compiler *c, basicblock *b)
1209{
1210 assert(b != NULL);
1211 if (b->b_instr == NULL) {
1212 b->b_instr = PyObject_Malloc(sizeof(struct instr) *
1213 DEFAULT_BLOCK_SIZE);
1214 if (b->b_instr == NULL) {
1215 PyErr_NoMemory();
1216 return -1;
1217 }
1218 b->b_ialloc = DEFAULT_BLOCK_SIZE;
1219 memset((char *)b->b_instr, 0,
1220 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
1221 }
1222 else if (b->b_iused == b->b_ialloc) {
1223 size_t oldsize, newsize;
1224 oldsize = b->b_ialloc * sizeof(struct instr);
1225 newsize = oldsize << 1;
1226 if (newsize == 0) {
1227 PyErr_NoMemory();
1228 return -1;
1229 }
1230 b->b_ialloc <<= 1;
1231 b->b_instr = PyObject_Realloc((void *)b->b_instr, newsize);
1232 if (b->b_instr == NULL)
1233 return -1;
1234 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
1235 }
1236 return b->b_iused++;
1237}
1238
1239static void
1240compiler_set_lineno(struct compiler *c, int off)
1241{
1242 basicblock *b;
1243 if (c->u->u_lineno_set)
1244 return;
1245 c->u->u_lineno_set = true;
1246 b = c->u->u_curblock;
1247 b->b_instr[off].i_lineno = c->u->u_lineno;
1248}
1249
1250static int
1251opcode_stack_effect(int opcode, int oparg)
1252{
1253 switch (opcode) {
1254 case POP_TOP:
1255 return -1;
1256 case ROT_TWO:
1257 case ROT_THREE:
1258 return 0;
1259 case DUP_TOP:
1260 return 1;
1261 case ROT_FOUR:
1262 return 0;
1263
1264 case UNARY_POSITIVE:
1265 case UNARY_NEGATIVE:
1266 case UNARY_NOT:
1267 case UNARY_CONVERT:
1268 case UNARY_INVERT:
1269 return 0;
1270
1271 case BINARY_POWER:
1272 case BINARY_MULTIPLY:
1273 case BINARY_DIVIDE:
1274 case BINARY_MODULO:
1275 case BINARY_ADD:
1276 case BINARY_SUBTRACT:
1277 case BINARY_SUBSCR:
1278 case BINARY_FLOOR_DIVIDE:
1279 case BINARY_TRUE_DIVIDE:
1280 return -1;
1281 case INPLACE_FLOOR_DIVIDE:
1282 case INPLACE_TRUE_DIVIDE:
1283 return -1;
1284
1285 case SLICE+0:
1286 return 1;
1287 case SLICE+1:
1288 return 0;
1289 case SLICE+2:
1290 return 0;
1291 case SLICE+3:
1292 return -1;
1293
1294 case STORE_SLICE+0:
1295 return -2;
1296 case STORE_SLICE+1:
1297 return -3;
1298 case STORE_SLICE+2:
1299 return -3;
1300 case STORE_SLICE+3:
1301 return -4;
1302
1303 case DELETE_SLICE+0:
1304 return -1;
1305 case DELETE_SLICE+1:
1306 return -2;
1307 case DELETE_SLICE+2:
1308 return -2;
1309 case DELETE_SLICE+3:
1310 return -3;
1311
1312 case INPLACE_ADD:
1313 case INPLACE_SUBTRACT:
1314 case INPLACE_MULTIPLY:
1315 case INPLACE_DIVIDE:
1316 case INPLACE_MODULO:
1317 return -1;
1318 case STORE_SUBSCR:
1319 return -3;
1320 case DELETE_SUBSCR:
1321 return -2;
1322
1323 case BINARY_LSHIFT:
1324 case BINARY_RSHIFT:
1325 case BINARY_AND:
1326 case BINARY_XOR:
1327 case BINARY_OR:
1328 return -1;
1329 case INPLACE_POWER:
1330 return -1;
1331 case GET_ITER:
1332 return 0;
1333
1334 case PRINT_EXPR:
1335 return -1;
1336 case PRINT_ITEM:
1337 return -1;
1338 case PRINT_NEWLINE:
1339 return 0;
1340 case PRINT_ITEM_TO:
1341 return -2;
1342 case PRINT_NEWLINE_TO:
1343 return -1;
1344 case INPLACE_LSHIFT:
1345 case INPLACE_RSHIFT:
1346 case INPLACE_AND:
1347 case INPLACE_XOR:
1348 case INPLACE_OR:
1349 return -1;
1350 case BREAK_LOOP:
1351 return 0;
1352
1353 case LOAD_LOCALS:
1354 return 1;
1355 case RETURN_VALUE:
1356 return -1;
1357 case IMPORT_STAR:
1358 return -1;
1359 case EXEC_STMT:
1360 return -3;
1361 case YIELD_VALUE:
1362 return 0;
1363
1364 case POP_BLOCK:
1365 return 0;
1366 case END_FINALLY:
1367 return -1; /* or -2 or -3 if exception occurred */
1368 case BUILD_CLASS:
1369 return -2;
1370
1371 case STORE_NAME:
1372 return -1;
1373 case DELETE_NAME:
1374 return 0;
1375 case UNPACK_SEQUENCE:
1376 return oparg-1;
1377 case FOR_ITER:
1378 return 1;
1379
1380 case STORE_ATTR:
1381 return -2;
1382 case DELETE_ATTR:
1383 return -1;
1384 case STORE_GLOBAL:
1385 return -1;
1386 case DELETE_GLOBAL:
1387 return 0;
1388 case DUP_TOPX:
1389 return oparg;
1390 case LOAD_CONST:
1391 return 1;
1392 case LOAD_NAME:
1393 return 1;
1394 case BUILD_TUPLE:
1395 case BUILD_LIST:
1396 return 1-oparg;
1397 case BUILD_MAP:
1398 return 1;
1399 case LOAD_ATTR:
1400 return 0;
1401 case COMPARE_OP:
1402 return -1;
1403 case IMPORT_NAME:
1404 return 0;
1405 case IMPORT_FROM:
1406 return 1;
1407
1408 case JUMP_FORWARD:
1409 case JUMP_IF_FALSE:
1410 case JUMP_IF_TRUE:
1411 case JUMP_ABSOLUTE:
1412 return 0;
1413
1414 case LOAD_GLOBAL:
1415 return 1;
1416
1417 case CONTINUE_LOOP:
1418 return 0;
1419 case SETUP_LOOP:
1420 return 0;
1421 case SETUP_EXCEPT:
1422 case SETUP_FINALLY:
1423 return 3; /* actually pushed by an exception */
1424
1425 case LOAD_FAST:
1426 return 1;
1427 case STORE_FAST:
1428 return -1;
1429 case DELETE_FAST:
1430 return 0;
1431
1432 case RAISE_VARARGS:
1433 return -oparg;
1434#define NARGS(o) (((o) % 256) + 2*((o) / 256))
1435 case CALL_FUNCTION:
1436 return -NARGS(oparg);
1437 case CALL_FUNCTION_VAR:
1438 case CALL_FUNCTION_KW:
1439 return -NARGS(oparg)-1;
1440 case CALL_FUNCTION_VAR_KW:
1441 return -NARGS(oparg)-2;
1442#undef NARGS
1443 case MAKE_FUNCTION:
1444 return -oparg;
1445 case BUILD_SLICE:
1446 if (oparg == 3)
1447 return -2;
1448 else
1449 return -1;
1450
1451 case MAKE_CLOSURE:
1452 return -oparg;
1453 case LOAD_CLOSURE:
1454 return 1;
1455 case LOAD_DEREF:
1456 return 1;
1457 case STORE_DEREF:
1458 return -1;
1459 default:
1460 fprintf(stderr, "opcode = %d\n", opcode);
1461 Py_FatalError("opcode_stack_effect()");
1462
1463 }
1464 return 0; /* not reachable */
1465}
1466
1467/* Add an opcode with no argument.
1468 Returns 0 on failure, 1 on success.
1469*/
1470
1471static int
1472compiler_addop(struct compiler *c, int opcode)
1473{
1474 basicblock *b;
1475 struct instr *i;
1476 int off;
1477 off = compiler_next_instr(c, c->u->u_curblock);
1478 if (off < 0)
1479 return 0;
1480 b = c->u->u_curblock;
1481 i = &b->b_instr[off];
1482 i->i_opcode = opcode;
1483 i->i_hasarg = 0;
1484 if (opcode == RETURN_VALUE)
1485 b->b_return = 1;
1486 compiler_set_lineno(c, off);
1487 return 1;
1488}
1489
1490static int
1491compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
1492{
1493 PyObject *t, *v;
1494 int arg;
1495
1496 /* necessary to make sure types aren't coerced (e.g., int and long) */
Neil Schemenauer3a44aaa2005-10-23 17:21:54 +00001497 t = PyTuple_Pack(2, o, o->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001498 if (t == NULL)
1499 return -1;
1500
1501 v = PyDict_GetItem(dict, t);
1502 if (!v) {
1503 arg = PyDict_Size(dict);
1504 v = PyInt_FromLong(arg);
1505 if (!v) {
1506 Py_DECREF(t);
1507 return -1;
1508 }
1509 if (PyDict_SetItem(dict, t, v) < 0) {
1510 Py_DECREF(t);
1511 Py_DECREF(v);
1512 return -1;
1513 }
1514 Py_DECREF(v);
1515 }
1516 else
1517 arg = PyInt_AsLong(v);
1518 Py_DECREF(t);
1519 return arg;
1520}
1521
1522static int
1523compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
1524 PyObject *o)
1525{
1526 int arg = compiler_add_o(c, dict, o);
1527 if (arg < 0)
1528 return 0;
1529 return compiler_addop_i(c, opcode, arg);
1530}
1531
1532static int
1533compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
1534 PyObject *o)
1535{
1536 int arg;
1537 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1538 if (!mangled)
1539 return 0;
1540 arg = compiler_add_o(c, dict, mangled);
1541 Py_DECREF(mangled);
1542 if (arg < 0)
1543 return 0;
1544 return compiler_addop_i(c, opcode, arg);
1545}
1546
1547/* Add an opcode with an integer argument.
1548 Returns 0 on failure, 1 on success.
1549*/
1550
1551static int
1552compiler_addop_i(struct compiler *c, int opcode, int oparg)
1553{
1554 struct instr *i;
1555 int off;
1556 off = compiler_next_instr(c, c->u->u_curblock);
1557 if (off < 0)
1558 return 0;
1559 i = &c->u->u_curblock->b_instr[off];
1560 i->i_opcode = opcode;
1561 i->i_oparg = oparg;
1562 i->i_hasarg = 1;
1563 compiler_set_lineno(c, off);
1564 return 1;
1565}
1566
1567static int
1568compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1569{
1570 struct instr *i;
1571 int off;
1572
1573 assert(b != NULL);
1574 off = compiler_next_instr(c, c->u->u_curblock);
1575 if (off < 0)
1576 return 0;
1577 compiler_set_lineno(c, off);
1578 i = &c->u->u_curblock->b_instr[off];
1579 i->i_opcode = opcode;
1580 i->i_target = b;
1581 i->i_hasarg = 1;
1582 if (absolute)
1583 i->i_jabs = 1;
1584 else
1585 i->i_jrel = 1;
1586 return 1;
1587}
1588
1589/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1590 like to find better names.) NEW_BLOCK() creates a new block and sets
1591 it as the current block. NEXT_BLOCK() also creates an implicit jump
1592 from the current block to the new block.
1593*/
1594
1595/* XXX The returns inside these macros make it impossible to decref
1596 objects created in the local function.
1597*/
1598
1599
1600#define NEW_BLOCK(C) { \
1601 if (compiler_use_new_block((C)) == NULL) \
1602 return 0; \
1603}
1604
1605#define NEXT_BLOCK(C) { \
1606 if (compiler_next_block((C)) == NULL) \
1607 return 0; \
1608}
1609
1610#define ADDOP(C, OP) { \
1611 if (!compiler_addop((C), (OP))) \
1612 return 0; \
1613}
1614
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001615#define ADDOP_IN_SCOPE(C, OP) { \
1616 if (!compiler_addop((C), (OP))) { \
1617 compiler_exit_scope(c); \
1618 return 0; \
1619 } \
1620}
1621
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001622#define ADDOP_O(C, OP, O, TYPE) { \
1623 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1624 return 0; \
1625}
1626
1627#define ADDOP_NAME(C, OP, O, TYPE) { \
1628 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1629 return 0; \
1630}
1631
1632#define ADDOP_I(C, OP, O) { \
1633 if (!compiler_addop_i((C), (OP), (O))) \
1634 return 0; \
1635}
1636
1637#define ADDOP_JABS(C, OP, O) { \
1638 if (!compiler_addop_j((C), (OP), (O), 1)) \
1639 return 0; \
1640}
1641
1642#define ADDOP_JREL(C, OP, O) { \
1643 if (!compiler_addop_j((C), (OP), (O), 0)) \
1644 return 0; \
1645}
1646
1647/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1648 the ASDL name to synthesize the name of the C type and the visit function.
1649*/
1650
1651#define VISIT(C, TYPE, V) {\
1652 if (!compiler_visit_ ## TYPE((C), (V))) \
1653 return 0; \
1654}
1655
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001656#define VISIT_IN_SCOPE(C, TYPE, V) {\
1657 if (!compiler_visit_ ## TYPE((C), (V))) { \
1658 compiler_exit_scope(c); \
1659 return 0; \
1660 } \
1661}
1662
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001663#define VISIT_SLICE(C, V, CTX) {\
1664 if (!compiler_visit_slice((C), (V), (CTX))) \
1665 return 0; \
1666}
1667
1668#define VISIT_SEQ(C, TYPE, SEQ) { \
1669 int i; \
1670 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1671 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1672 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
1673 if (!compiler_visit_ ## TYPE((C), elt)) \
1674 return 0; \
1675 } \
1676}
1677
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001678#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
1679 int i; \
1680 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1681 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1682 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
1683 if (!compiler_visit_ ## TYPE((C), elt)) { \
1684 compiler_exit_scope(c); \
1685 return 0; \
1686 } \
1687 } \
1688}
1689
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690static int
1691compiler_isdocstring(stmt_ty s)
1692{
1693 if (s->kind != Expr_kind)
1694 return 0;
1695 return s->v.Expr.value->kind == Str_kind;
1696}
1697
1698/* Compile a sequence of statements, checking for a docstring. */
1699
1700static int
1701compiler_body(struct compiler *c, asdl_seq *stmts)
1702{
1703 int i = 0;
1704 stmt_ty st;
1705
1706 if (!asdl_seq_LEN(stmts))
1707 return 1;
1708 st = asdl_seq_GET(stmts, 0);
1709 if (compiler_isdocstring(st)) {
1710 i = 1;
1711 VISIT(c, expr, st->v.Expr.value);
1712 if (!compiler_nameop(c, __doc__, Store))
1713 return 0;
1714 }
1715 for (; i < asdl_seq_LEN(stmts); i++)
1716 VISIT(c, stmt, asdl_seq_GET(stmts, i));
1717 return 1;
1718}
1719
1720static PyCodeObject *
1721compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001722{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001723 PyCodeObject *co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001724 int addNone = 1;
1725 static PyObject *module;
1726 if (!module) {
1727 module = PyString_FromString("<module>");
1728 if (!module)
1729 return NULL;
1730 }
1731 if (!compiler_enter_scope(c, module, mod, 1))
Guido van Rossumd076c731998-10-07 19:42:25 +00001732 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001733 switch (mod->kind) {
1734 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001735 if (!compiler_body(c, mod->v.Module.body)) {
1736 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001737 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001738 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001739 break;
1740 case Interactive_kind:
1741 c->c_interactive = 1;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001742 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001743 break;
1744 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001745 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001746 addNone = 0;
1747 break;
1748 case Suite_kind:
1749 assert(0); /* XXX: what should we do here? */
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001750 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Suite.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001751 break;
1752 default:
1753 assert(0);
Guido van Rossumd076c731998-10-07 19:42:25 +00001754 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001755 co = assemble(c, addNone);
1756 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001757 return co;
1758}
1759
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001760/* The test for LOCAL must come before the test for FREE in order to
1761 handle classes where name is both local and free. The local var is
1762 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001763*/
1764
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001765static int
1766get_ref_type(struct compiler *c, PyObject *name)
1767{
1768 int scope = PyST_GetScope(c->u->u_ste, name);
1769 if (scope == 0) {
1770 char buf[350];
1771 PyOS_snprintf(buf, sizeof(buf),
1772 "unknown scope for %.100s in %.100s(%s) in %s\n"
1773 "symbols: %s\nlocals: %s\nglobals: %s\n",
1774 PyString_AS_STRING(name),
1775 PyString_AS_STRING(c->u->u_name),
1776 PyObject_REPR(c->u->u_ste->ste_id),
1777 c->c_filename,
1778 PyObject_REPR(c->u->u_ste->ste_symbols),
1779 PyObject_REPR(c->u->u_varnames),
1780 PyObject_REPR(c->u->u_names)
1781 );
1782 Py_FatalError(buf);
1783 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001784
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001785 return scope;
1786}
1787
1788static int
1789compiler_lookup_arg(PyObject *dict, PyObject *name)
1790{
1791 PyObject *k, *v;
1792 k = Py_BuildValue("(OO)", name, name->ob_type);
1793 if (k == NULL)
1794 return -1;
1795 v = PyDict_GetItem(dict, k);
1796 if (v == NULL)
1797 return -1;
1798 return PyInt_AS_LONG(v);
1799}
1800
1801static int
1802compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1803{
1804 int i, free = PyCode_GetNumFree(co);
1805 if (free == 0) {
1806 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1807 ADDOP_I(c, MAKE_FUNCTION, args);
1808 return 1;
1809 }
1810 for (i = 0; i < free; ++i) {
1811 /* Bypass com_addop_varname because it will generate
1812 LOAD_DEREF but LOAD_CLOSURE is needed.
1813 */
1814 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1815 int arg, reftype;
1816
1817 /* Special case: If a class contains a method with a
1818 free variable that has the same name as a method,
1819 the name will be considered free *and* local in the
1820 class. It should be handled by the closure, as
1821 well as by the normal name loookup logic.
1822 */
1823 reftype = get_ref_type(c, name);
1824 if (reftype == CELL)
1825 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1826 else /* (reftype == FREE) */
1827 arg = compiler_lookup_arg(c->u->u_freevars, name);
1828 if (arg == -1) {
1829 printf("lookup %s in %s %d %d\n"
1830 "freevars of %s: %s\n",
1831 PyObject_REPR(name),
1832 PyString_AS_STRING(c->u->u_name),
1833 reftype, arg,
1834 PyString_AS_STRING(co->co_name),
1835 PyObject_REPR(co->co_freevars));
1836 Py_FatalError("compiler_make_closure()");
1837 }
1838 ADDOP_I(c, LOAD_CLOSURE, arg);
1839 }
1840 ADDOP_I(c, BUILD_TUPLE, free);
1841 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1842 ADDOP_I(c, MAKE_CLOSURE, args);
1843 return 1;
1844}
1845
1846static int
1847compiler_decorators(struct compiler *c, asdl_seq* decos)
1848{
1849 int i;
1850
1851 if (!decos)
1852 return 1;
1853
1854 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1855 VISIT(c, expr, asdl_seq_GET(decos, i));
1856 }
1857 return 1;
1858}
1859
1860static int
1861compiler_arguments(struct compiler *c, arguments_ty args)
1862{
1863 int i;
1864 int n = asdl_seq_LEN(args->args);
1865 /* Correctly handle nested argument lists */
1866 for (i = 0; i < n; i++) {
1867 expr_ty arg = asdl_seq_GET(args->args, i);
1868 if (arg->kind == Tuple_kind) {
1869 PyObject *id = PyString_FromFormat(".%d", i);
1870 if (id == NULL) {
1871 return 0;
1872 }
1873 if (!compiler_nameop(c, id, Load)) {
1874 Py_DECREF(id);
1875 return 0;
1876 }
1877 Py_DECREF(id);
1878 VISIT(c, expr, arg);
1879 }
1880 }
1881 return 1;
1882}
1883
1884static int
1885compiler_function(struct compiler *c, stmt_ty s)
1886{
1887 PyCodeObject *co;
1888 PyObject *first_const = Py_None;
1889 arguments_ty args = s->v.FunctionDef.args;
1890 asdl_seq* decos = s->v.FunctionDef.decorators;
1891 stmt_ty st;
1892 int i, n, docstring;
1893
1894 assert(s->kind == FunctionDef_kind);
1895
1896 if (!compiler_decorators(c, decos))
1897 return 0;
1898 if (args->defaults)
1899 VISIT_SEQ(c, expr, args->defaults);
1900 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1901 s->lineno))
1902 return 0;
1903
1904 st = asdl_seq_GET(s->v.FunctionDef.body, 0);
1905 docstring = compiler_isdocstring(st);
1906 if (docstring)
1907 first_const = st->v.Expr.value->v.Str.s;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001908 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1909 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001910 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001911 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001912
1913 /* unpack nested arguments */
1914 compiler_arguments(c, args);
1915
1916 c->u->u_argcount = asdl_seq_LEN(args->args);
1917 n = asdl_seq_LEN(s->v.FunctionDef.body);
1918 /* if there was a docstring, we need to skip the first statement */
1919 for (i = docstring; i < n; i++) {
1920 stmt_ty s2 = asdl_seq_GET(s->v.FunctionDef.body, i);
1921 if (i == 0 && s2->kind == Expr_kind &&
1922 s2->v.Expr.value->kind == Str_kind)
1923 continue;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001924 VISIT_IN_SCOPE(c, stmt, s2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925 }
1926 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001927 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001928 if (co == NULL)
1929 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001930
1931 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
1932
1933 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1934 ADDOP_I(c, CALL_FUNCTION, 1);
1935 }
1936
1937 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1938}
1939
1940static int
1941compiler_class(struct compiler *c, stmt_ty s)
1942{
1943 int n;
1944 PyCodeObject *co;
1945 PyObject *str;
1946 /* push class name on stack, needed by BUILD_CLASS */
1947 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1948 /* push the tuple of base classes on the stack */
1949 n = asdl_seq_LEN(s->v.ClassDef.bases);
1950 if (n > 0)
1951 VISIT_SEQ(c, expr, s->v.ClassDef.bases);
1952 ADDOP_I(c, BUILD_TUPLE, n);
1953 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s,
1954 s->lineno))
1955 return 0;
1956 c->u->u_private = s->v.ClassDef.name;
1957 Py_INCREF(c->u->u_private);
1958 str = PyString_InternFromString("__name__");
1959 if (!str || !compiler_nameop(c, str, Load)) {
1960 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001961 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001962 return 0;
1963 }
1964
1965 Py_DECREF(str);
1966 str = PyString_InternFromString("__module__");
1967 if (!str || !compiler_nameop(c, str, Store)) {
1968 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001969 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001970 return 0;
1971 }
1972 Py_DECREF(str);
1973
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001974 if (!compiler_body(c, s->v.ClassDef.body)) {
1975 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001976 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001977 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001978
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001979 ADDOP_IN_SCOPE(c, LOAD_LOCALS);
1980 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001981 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001982 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001983 if (co == NULL)
1984 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001985
1986 compiler_make_closure(c, co, 0);
1987 ADDOP_I(c, CALL_FUNCTION, 0);
1988 ADDOP(c, BUILD_CLASS);
1989 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1990 return 0;
1991 return 1;
1992}
1993
1994static int
1995compiler_lambda(struct compiler *c, expr_ty e)
1996{
1997 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001998 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999 arguments_ty args = e->v.Lambda.args;
2000 assert(e->kind == Lambda_kind);
2001
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002002 if (!name) {
2003 name = PyString_InternFromString("<lambda>");
2004 if (!name)
2005 return 0;
2006 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002007
2008 if (args->defaults)
2009 VISIT_SEQ(c, expr, args->defaults);
2010 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2011 return 0;
2012
2013 /* unpack nested arguments */
2014 compiler_arguments(c, args);
2015
2016 c->u->u_argcount = asdl_seq_LEN(args->args);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00002017 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2018 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002019 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00002020 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021 if (co == NULL)
2022 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002023
2024 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025
2026 return 1;
2027}
2028
2029static int
2030compiler_print(struct compiler *c, stmt_ty s)
2031{
2032 int i, n;
2033 bool dest;
2034
2035 assert(s->kind == Print_kind);
2036 n = asdl_seq_LEN(s->v.Print.values);
2037 dest = false;
2038 if (s->v.Print.dest) {
2039 VISIT(c, expr, s->v.Print.dest);
2040 dest = true;
2041 }
2042 for (i = 0; i < n; i++) {
2043 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i);
2044 if (dest) {
2045 ADDOP(c, DUP_TOP);
2046 VISIT(c, expr, e);
2047 ADDOP(c, ROT_TWO);
2048 ADDOP(c, PRINT_ITEM_TO);
2049 }
2050 else {
2051 VISIT(c, expr, e);
2052 ADDOP(c, PRINT_ITEM);
2053 }
2054 }
2055 if (s->v.Print.nl) {
2056 if (dest)
2057 ADDOP(c, PRINT_NEWLINE_TO)
2058 else
2059 ADDOP(c, PRINT_NEWLINE)
2060 }
2061 else if (dest)
2062 ADDOP(c, POP_TOP);
2063 return 1;
2064}
2065
2066static int
2067compiler_if(struct compiler *c, stmt_ty s)
2068{
2069 basicblock *end, *next;
2070
2071 assert(s->kind == If_kind);
2072 end = compiler_new_block(c);
2073 if (end == NULL)
2074 return 0;
2075 next = compiler_new_block(c);
2076 if (next == NULL)
2077 return 0;
2078 VISIT(c, expr, s->v.If.test);
2079 ADDOP_JREL(c, JUMP_IF_FALSE, next);
2080 ADDOP(c, POP_TOP);
2081 VISIT_SEQ(c, stmt, s->v.If.body);
2082 ADDOP_JREL(c, JUMP_FORWARD, end);
2083 compiler_use_next_block(c, next);
2084 ADDOP(c, POP_TOP);
2085 if (s->v.If.orelse)
2086 VISIT_SEQ(c, stmt, s->v.If.orelse);
2087 compiler_use_next_block(c, end);
2088 return 1;
2089}
2090
2091static int
2092compiler_for(struct compiler *c, stmt_ty s)
2093{
2094 basicblock *start, *cleanup, *end;
2095
2096 start = compiler_new_block(c);
2097 cleanup = compiler_new_block(c);
2098 end = compiler_new_block(c);
2099 if (start == NULL || end == NULL || cleanup == NULL)
2100 return 0;
2101 ADDOP_JREL(c, SETUP_LOOP, end);
2102 if (!compiler_push_fblock(c, LOOP, start))
2103 return 0;
2104 VISIT(c, expr, s->v.For.iter);
2105 ADDOP(c, GET_ITER);
2106 compiler_use_next_block(c, start);
2107 ADDOP_JREL(c, FOR_ITER, cleanup);
2108 VISIT(c, expr, s->v.For.target);
2109 VISIT_SEQ(c, stmt, s->v.For.body);
2110 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2111 compiler_use_next_block(c, cleanup);
2112 ADDOP(c, POP_BLOCK);
2113 compiler_pop_fblock(c, LOOP, start);
2114 VISIT_SEQ(c, stmt, s->v.For.orelse);
2115 compiler_use_next_block(c, end);
2116 return 1;
2117}
2118
2119static int
2120compiler_while(struct compiler *c, stmt_ty s)
2121{
2122 basicblock *loop, *orelse, *end, *anchor = NULL;
2123 int constant = expr_constant(s->v.While.test);
2124
2125 if (constant == 0)
2126 return 1;
2127 loop = compiler_new_block(c);
2128 end = compiler_new_block(c);
2129 if (constant == -1) {
2130 anchor = compiler_new_block(c);
2131 if (anchor == NULL)
2132 return 0;
2133 }
2134 if (loop == NULL || end == NULL)
2135 return 0;
2136 if (s->v.While.orelse) {
2137 orelse = compiler_new_block(c);
2138 if (orelse == NULL)
2139 return 0;
2140 }
2141 else
2142 orelse = NULL;
2143
2144 ADDOP_JREL(c, SETUP_LOOP, end);
2145 compiler_use_next_block(c, loop);
2146 if (!compiler_push_fblock(c, LOOP, loop))
2147 return 0;
2148 if (constant == -1) {
2149 VISIT(c, expr, s->v.While.test);
2150 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
2151 ADDOP(c, POP_TOP);
2152 }
2153 VISIT_SEQ(c, stmt, s->v.While.body);
2154 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
2155
2156 /* XXX should the two POP instructions be in a separate block
2157 if there is no else clause ?
2158 */
2159
2160 if (constant == -1) {
2161 compiler_use_next_block(c, anchor);
2162 ADDOP(c, POP_TOP);
2163 ADDOP(c, POP_BLOCK);
2164 }
2165 compiler_pop_fblock(c, LOOP, loop);
2166 if (orelse != NULL)
2167 VISIT_SEQ(c, stmt, s->v.While.orelse);
2168 compiler_use_next_block(c, end);
2169
2170 return 1;
2171}
2172
2173static int
2174compiler_continue(struct compiler *c)
2175{
2176 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
2177 int i;
2178
2179 if (!c->u->u_nfblocks)
2180 return compiler_error(c, LOOP_ERROR_MSG);
2181 i = c->u->u_nfblocks - 1;
2182 switch (c->u->u_fblock[i].fb_type) {
2183 case LOOP:
2184 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
2185 break;
2186 case EXCEPT:
2187 case FINALLY_TRY:
2188 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP)
2189 ;
2190 if (i == -1)
2191 return compiler_error(c, LOOP_ERROR_MSG);
2192 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
2193 break;
2194 case FINALLY_END:
2195 return compiler_error(c,
2196 "'continue' not supported inside 'finally' clause");
2197 }
2198
2199 return 1;
2200}
2201
2202/* Code generated for "try: <body> finally: <finalbody>" is as follows:
2203
2204 SETUP_FINALLY L
2205 <code for body>
2206 POP_BLOCK
2207 LOAD_CONST <None>
2208 L: <code for finalbody>
2209 END_FINALLY
2210
2211 The special instructions use the block stack. Each block
2212 stack entry contains the instruction that created it (here
2213 SETUP_FINALLY), the level of the value stack at the time the
2214 block stack entry was created, and a label (here L).
2215
2216 SETUP_FINALLY:
2217 Pushes the current value stack level and the label
2218 onto the block stack.
2219 POP_BLOCK:
2220 Pops en entry from the block stack, and pops the value
2221 stack until its level is the same as indicated on the
2222 block stack. (The label is ignored.)
2223 END_FINALLY:
2224 Pops a variable number of entries from the *value* stack
2225 and re-raises the exception they specify. The number of
2226 entries popped depends on the (pseudo) exception type.
2227
2228 The block stack is unwound when an exception is raised:
2229 when a SETUP_FINALLY entry is found, the exception is pushed
2230 onto the value stack (and the exception condition is cleared),
2231 and the interpreter jumps to the label gotten from the block
2232 stack.
2233*/
2234
2235static int
2236compiler_try_finally(struct compiler *c, stmt_ty s)
2237{
2238 basicblock *body, *end;
2239 body = compiler_new_block(c);
2240 end = compiler_new_block(c);
2241 if (body == NULL || end == NULL)
2242 return 0;
2243
2244 ADDOP_JREL(c, SETUP_FINALLY, end);
2245 compiler_use_next_block(c, body);
2246 if (!compiler_push_fblock(c, FINALLY_TRY, body))
2247 return 0;
2248 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
2249 ADDOP(c, POP_BLOCK);
2250 compiler_pop_fblock(c, FINALLY_TRY, body);
2251
2252 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2253 compiler_use_next_block(c, end);
2254 if (!compiler_push_fblock(c, FINALLY_END, end))
2255 return 0;
2256 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
2257 ADDOP(c, END_FINALLY);
2258 compiler_pop_fblock(c, FINALLY_END, end);
2259
2260 return 1;
2261}
2262
2263/*
2264 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
2265 (The contents of the value stack is shown in [], with the top
2266 at the right; 'tb' is trace-back info, 'val' the exception's
2267 associated value, and 'exc' the exception.)
2268
2269 Value stack Label Instruction Argument
2270 [] SETUP_EXCEPT L1
2271 [] <code for S>
2272 [] POP_BLOCK
2273 [] JUMP_FORWARD L0
2274
2275 [tb, val, exc] L1: DUP )
2276 [tb, val, exc, exc] <evaluate E1> )
2277 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2278 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
2279 [tb, val, exc, 1] POP )
2280 [tb, val, exc] POP
2281 [tb, val] <assign to V1> (or POP if no V1)
2282 [tb] POP
2283 [] <code for S1>
2284 JUMP_FORWARD L0
2285
2286 [tb, val, exc, 0] L2: POP
2287 [tb, val, exc] DUP
2288 .............................etc.......................
2289
2290 [tb, val, exc, 0] Ln+1: POP
2291 [tb, val, exc] END_FINALLY # re-raise exception
2292
2293 [] L0: <next statement>
2294
2295 Of course, parts are not generated if Vi or Ei is not present.
2296*/
2297static int
2298compiler_try_except(struct compiler *c, stmt_ty s)
2299{
2300 basicblock *body, *orelse, *except, *end;
2301 int i, n;
2302
2303 body = compiler_new_block(c);
2304 except = compiler_new_block(c);
2305 orelse = compiler_new_block(c);
2306 end = compiler_new_block(c);
2307 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
2308 return 0;
2309 ADDOP_JREL(c, SETUP_EXCEPT, except);
2310 compiler_use_next_block(c, body);
2311 if (!compiler_push_fblock(c, EXCEPT, body))
2312 return 0;
2313 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
2314 ADDOP(c, POP_BLOCK);
2315 compiler_pop_fblock(c, EXCEPT, body);
2316 ADDOP_JREL(c, JUMP_FORWARD, orelse);
2317 n = asdl_seq_LEN(s->v.TryExcept.handlers);
2318 compiler_use_next_block(c, except);
2319 for (i = 0; i < n; i++) {
2320 excepthandler_ty handler = asdl_seq_GET(
2321 s->v.TryExcept.handlers, i);
2322 if (!handler->type && i < n-1)
2323 return compiler_error(c, "default 'except:' must be last");
2324 except = compiler_new_block(c);
2325 if (except == NULL)
2326 return 0;
2327 if (handler->type) {
2328 ADDOP(c, DUP_TOP);
2329 VISIT(c, expr, handler->type);
2330 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2331 ADDOP_JREL(c, JUMP_IF_FALSE, except);
2332 ADDOP(c, POP_TOP);
2333 }
2334 ADDOP(c, POP_TOP);
2335 if (handler->name) {
2336 VISIT(c, expr, handler->name);
2337 }
2338 else {
2339 ADDOP(c, POP_TOP);
2340 }
2341 ADDOP(c, POP_TOP);
2342 VISIT_SEQ(c, stmt, handler->body);
2343 ADDOP_JREL(c, JUMP_FORWARD, end);
2344 compiler_use_next_block(c, except);
2345 if (handler->type)
2346 ADDOP(c, POP_TOP);
2347 }
2348 ADDOP(c, END_FINALLY);
2349 compiler_use_next_block(c, orelse);
2350 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2351 compiler_use_next_block(c, end);
2352 return 1;
2353}
2354
2355static int
2356compiler_import_as(struct compiler *c, identifier name, identifier asname)
2357{
2358 /* The IMPORT_NAME opcode was already generated. This function
2359 merely needs to bind the result to a name.
2360
2361 If there is a dot in name, we need to split it and emit a
2362 LOAD_ATTR for each name.
2363 */
2364 const char *src = PyString_AS_STRING(name);
2365 const char *dot = strchr(src, '.');
2366 if (dot) {
2367 /* Consume the base module name to get the first attribute */
2368 src = dot + 1;
2369 while (dot) {
2370 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00002371 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002372 dot = strchr(src, '.');
Armin Rigo31441302005-10-21 12:57:31 +00002373 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002374 dot ? dot - src : strlen(src));
2375 ADDOP_O(c, LOAD_ATTR, attr, names);
2376 src = dot + 1;
2377 }
2378 }
2379 return compiler_nameop(c, asname, Store);
2380}
2381
2382static int
2383compiler_import(struct compiler *c, stmt_ty s)
2384{
2385 /* The Import node stores a module name like a.b.c as a single
2386 string. This is convenient for all cases except
2387 import a.b.c as d
2388 where we need to parse that string to extract the individual
2389 module names.
2390 XXX Perhaps change the representation to make this case simpler?
2391 */
2392 int i, n = asdl_seq_LEN(s->v.Import.names);
2393 for (i = 0; i < n; i++) {
2394 alias_ty alias = asdl_seq_GET(s->v.Import.names, i);
2395 int r;
2396
2397 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2398 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2399
2400 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002401 r = compiler_import_as(c, alias->name, alias->asname);
2402 if (!r)
2403 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002404 }
2405 else {
2406 identifier tmp = alias->name;
2407 const char *base = PyString_AS_STRING(alias->name);
2408 char *dot = strchr(base, '.');
2409 if (dot)
2410 tmp = PyString_FromStringAndSize(base,
2411 dot - base);
2412 r = compiler_nameop(c, tmp, Store);
2413 if (dot) {
2414 Py_DECREF(tmp);
2415 }
2416 if (!r)
2417 return r;
2418 }
2419 }
2420 return 1;
2421}
2422
2423static int
2424compiler_from_import(struct compiler *c, stmt_ty s)
2425{
2426 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
2427 int star = 0;
2428
2429 PyObject *names = PyTuple_New(n);
2430 if (!names)
2431 return 0;
2432
2433 /* build up the names */
2434 for (i = 0; i < n; i++) {
2435 alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
2436 Py_INCREF(alias->name);
2437 PyTuple_SET_ITEM(names, i, alias->name);
2438 }
2439
2440 if (s->lineno > c->c_future->ff_lineno) {
2441 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
2442 "__future__")) {
2443 Py_DECREF(names);
2444 return compiler_error(c,
2445 "from __future__ imports must occur "
2446 "at the beginning of the file");
2447
2448 }
2449 }
2450
2451 ADDOP_O(c, LOAD_CONST, names, consts);
2452 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2453 for (i = 0; i < n; i++) {
2454 alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
2455 identifier store_name;
2456
2457 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
2458 assert(n == 1);
2459 ADDOP(c, IMPORT_STAR);
2460 star = 1;
2461 break;
2462 }
2463
2464 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2465 store_name = alias->name;
2466 if (alias->asname)
2467 store_name = alias->asname;
2468
2469 if (!compiler_nameop(c, store_name, Store)) {
2470 Py_DECREF(names);
2471 return 0;
2472 }
2473 }
2474 if (!star)
2475 /* remove imported module */
2476 ADDOP(c, POP_TOP);
2477 return 1;
2478}
2479
2480static int
2481compiler_assert(struct compiler *c, stmt_ty s)
2482{
2483 static PyObject *assertion_error = NULL;
2484 basicblock *end;
2485
2486 if (Py_OptimizeFlag)
2487 return 1;
2488 if (assertion_error == NULL) {
2489 assertion_error = PyString_FromString("AssertionError");
2490 if (assertion_error == NULL)
2491 return 0;
2492 }
2493 VISIT(c, expr, s->v.Assert.test);
2494 end = compiler_new_block(c);
2495 if (end == NULL)
2496 return 0;
2497 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2498 ADDOP(c, POP_TOP);
2499 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2500 if (s->v.Assert.msg) {
2501 VISIT(c, expr, s->v.Assert.msg);
2502 ADDOP_I(c, RAISE_VARARGS, 2);
2503 }
2504 else {
2505 ADDOP_I(c, RAISE_VARARGS, 1);
2506 }
2507 compiler_use_block(c, end);
2508 ADDOP(c, POP_TOP);
2509 return 1;
2510}
2511
2512static int
2513compiler_visit_stmt(struct compiler *c, stmt_ty s)
2514{
2515 int i, n;
2516
2517 c->u->u_lineno = s->lineno;
2518 c->u->u_lineno_set = false;
2519 switch (s->kind) {
2520 case FunctionDef_kind:
2521 return compiler_function(c, s);
2522 case ClassDef_kind:
2523 return compiler_class(c, s);
2524 case Return_kind:
2525 if (c->u->u_ste->ste_type != FunctionBlock)
2526 return compiler_error(c, "'return' outside function");
2527 if (s->v.Return.value) {
2528 if (c->u->u_ste->ste_generator) {
2529 return compiler_error(c,
2530 "'return' with argument inside generator");
2531 }
2532 VISIT(c, expr, s->v.Return.value);
2533 }
2534 else
2535 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2536 ADDOP(c, RETURN_VALUE);
2537 break;
2538 case Delete_kind:
2539 VISIT_SEQ(c, expr, s->v.Delete.targets)
2540 break;
2541 case Assign_kind:
2542 n = asdl_seq_LEN(s->v.Assign.targets);
2543 VISIT(c, expr, s->v.Assign.value);
2544 for (i = 0; i < n; i++) {
2545 if (i < n - 1)
2546 ADDOP(c, DUP_TOP);
2547 VISIT(c, expr,
2548 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2549 }
2550 break;
2551 case AugAssign_kind:
2552 return compiler_augassign(c, s);
2553 case Print_kind:
2554 return compiler_print(c, s);
2555 case For_kind:
2556 return compiler_for(c, s);
2557 case While_kind:
2558 return compiler_while(c, s);
2559 case If_kind:
2560 return compiler_if(c, s);
2561 case Raise_kind:
2562 n = 0;
2563 if (s->v.Raise.type) {
2564 VISIT(c, expr, s->v.Raise.type);
2565 n++;
2566 if (s->v.Raise.inst) {
2567 VISIT(c, expr, s->v.Raise.inst);
2568 n++;
2569 if (s->v.Raise.tback) {
2570 VISIT(c, expr, s->v.Raise.tback);
2571 n++;
2572 }
2573 }
2574 }
2575 ADDOP_I(c, RAISE_VARARGS, n);
2576 break;
2577 case TryExcept_kind:
2578 return compiler_try_except(c, s);
2579 case TryFinally_kind:
2580 return compiler_try_finally(c, s);
2581 case Assert_kind:
2582 return compiler_assert(c, s);
2583 case Import_kind:
2584 return compiler_import(c, s);
2585 case ImportFrom_kind:
2586 return compiler_from_import(c, s);
2587 case Exec_kind:
2588 VISIT(c, expr, s->v.Exec.body);
2589 if (s->v.Exec.globals) {
2590 VISIT(c, expr, s->v.Exec.globals);
2591 if (s->v.Exec.locals) {
2592 VISIT(c, expr, s->v.Exec.locals);
2593 } else {
2594 ADDOP(c, DUP_TOP);
2595 }
2596 } else {
2597 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2598 ADDOP(c, DUP_TOP);
2599 }
2600 ADDOP(c, EXEC_STMT);
2601 break;
2602 case Global_kind:
2603 break;
2604 case Expr_kind:
2605 VISIT(c, expr, s->v.Expr.value);
2606 if (c->c_interactive && c->c_nestlevel <= 1) {
2607 ADDOP(c, PRINT_EXPR);
2608 }
2609 else {
2610 ADDOP(c, POP_TOP);
2611 }
2612 break;
2613 case Pass_kind:
2614 break;
2615 case Break_kind:
2616 if (!c->u->u_nfblocks)
2617 return compiler_error(c, "'break' outside loop");
2618 ADDOP(c, BREAK_LOOP);
2619 break;
2620 case Continue_kind:
2621 return compiler_continue(c);
2622 }
2623 return 1;
2624}
2625
2626static int
2627unaryop(unaryop_ty op)
2628{
2629 switch (op) {
2630 case Invert:
2631 return UNARY_INVERT;
2632 case Not:
2633 return UNARY_NOT;
2634 case UAdd:
2635 return UNARY_POSITIVE;
2636 case USub:
2637 return UNARY_NEGATIVE;
2638 }
2639 return 0;
2640}
2641
2642static int
2643binop(struct compiler *c, operator_ty op)
2644{
2645 switch (op) {
2646 case Add:
2647 return BINARY_ADD;
2648 case Sub:
2649 return BINARY_SUBTRACT;
2650 case Mult:
2651 return BINARY_MULTIPLY;
2652 case Div:
2653 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2654 return BINARY_TRUE_DIVIDE;
2655 else
2656 return BINARY_DIVIDE;
2657 case Mod:
2658 return BINARY_MODULO;
2659 case Pow:
2660 return BINARY_POWER;
2661 case LShift:
2662 return BINARY_LSHIFT;
2663 case RShift:
2664 return BINARY_RSHIFT;
2665 case BitOr:
2666 return BINARY_OR;
2667 case BitXor:
2668 return BINARY_XOR;
2669 case BitAnd:
2670 return BINARY_AND;
2671 case FloorDiv:
2672 return BINARY_FLOOR_DIVIDE;
2673 }
2674 return 0;
2675}
2676
2677static int
2678cmpop(cmpop_ty op)
2679{
2680 switch (op) {
2681 case Eq:
2682 return PyCmp_EQ;
2683 case NotEq:
2684 return PyCmp_NE;
2685 case Lt:
2686 return PyCmp_LT;
2687 case LtE:
2688 return PyCmp_LE;
2689 case Gt:
2690 return PyCmp_GT;
2691 case GtE:
2692 return PyCmp_GE;
2693 case Is:
2694 return PyCmp_IS;
2695 case IsNot:
2696 return PyCmp_IS_NOT;
2697 case In:
2698 return PyCmp_IN;
2699 case NotIn:
2700 return PyCmp_NOT_IN;
2701 }
2702 return PyCmp_BAD;
2703}
2704
2705static int
2706inplace_binop(struct compiler *c, operator_ty op)
2707{
2708 switch (op) {
2709 case Add:
2710 return INPLACE_ADD;
2711 case Sub:
2712 return INPLACE_SUBTRACT;
2713 case Mult:
2714 return INPLACE_MULTIPLY;
2715 case Div:
2716 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2717 return INPLACE_TRUE_DIVIDE;
2718 else
2719 return INPLACE_DIVIDE;
2720 case Mod:
2721 return INPLACE_MODULO;
2722 case Pow:
2723 return INPLACE_POWER;
2724 case LShift:
2725 return INPLACE_LSHIFT;
2726 case RShift:
2727 return INPLACE_RSHIFT;
2728 case BitOr:
2729 return INPLACE_OR;
2730 case BitXor:
2731 return INPLACE_XOR;
2732 case BitAnd:
2733 return INPLACE_AND;
2734 case FloorDiv:
2735 return INPLACE_FLOOR_DIVIDE;
2736 }
2737 assert(0);
2738 return 0;
2739}
2740
2741static int
2742compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2743{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002744 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002745 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2746
2747 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002748 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002749 /* XXX AugStore isn't used anywhere! */
2750
2751 /* First check for assignment to __debug__. Param? */
2752 if ((ctx == Store || ctx == AugStore || ctx == Del)
2753 && !strcmp(PyString_AS_STRING(name), "__debug__")) {
2754 return compiler_error(c, "can not assign to __debug__");
2755 }
2756
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002757 mangled = _Py_Mangle(c->u->u_private, name);
2758 if (!mangled)
2759 return 0;
2760
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002761 op = 0;
2762 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002763 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002764 switch (scope) {
2765 case FREE:
2766 dict = c->u->u_freevars;
2767 optype = OP_DEREF;
2768 break;
2769 case CELL:
2770 dict = c->u->u_cellvars;
2771 optype = OP_DEREF;
2772 break;
2773 case LOCAL:
2774 if (c->u->u_ste->ste_type == FunctionBlock)
2775 optype = OP_FAST;
2776 break;
2777 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002778 if (c->u->u_ste->ste_type == FunctionBlock &&
2779 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002780 optype = OP_GLOBAL;
2781 break;
2782 case GLOBAL_EXPLICIT:
2783 optype = OP_GLOBAL;
2784 break;
2785 }
2786
2787 /* XXX Leave assert here, but handle __doc__ and the like better */
2788 assert(scope || PyString_AS_STRING(name)[0] == '_');
2789
2790 switch (optype) {
2791 case OP_DEREF:
2792 switch (ctx) {
2793 case Load: op = LOAD_DEREF; break;
2794 case Store: op = STORE_DEREF; break;
2795 case AugLoad:
2796 case AugStore:
2797 break;
2798 case Del:
2799 PyErr_Format(PyExc_SyntaxError,
2800 "can not delete variable '%s' referenced "
2801 "in nested scope",
2802 PyString_AS_STRING(name));
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002803 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002804 return 0;
2805 break;
2806 case Param:
2807 assert(0); /* impossible */
2808 }
2809 break;
2810 case OP_FAST:
2811 switch (ctx) {
2812 case Load: op = LOAD_FAST; break;
2813 case Store: op = STORE_FAST; break;
2814 case Del: op = DELETE_FAST; break;
2815 case AugLoad:
2816 case AugStore:
2817 break;
2818 case Param:
2819 assert(0); /* impossible */
2820 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002821 ADDOP_O(c, op, mangled, varnames);
2822 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823 return 1;
2824 case OP_GLOBAL:
2825 switch (ctx) {
2826 case Load: op = LOAD_GLOBAL; break;
2827 case Store: op = STORE_GLOBAL; break;
2828 case Del: op = DELETE_GLOBAL; break;
2829 case AugLoad:
2830 case AugStore:
2831 break;
2832 case Param:
2833 assert(0); /* impossible */
2834 }
2835 break;
2836 case OP_NAME:
2837 switch (ctx) {
2838 case Load: op = LOAD_NAME; break;
2839 case Store: op = STORE_NAME; break;
2840 case Del: op = DELETE_NAME; break;
2841 case AugLoad:
2842 case AugStore:
2843 break;
2844 case Param:
2845 assert(0); /* impossible */
2846 }
2847 break;
2848 }
2849
2850 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002851 arg = compiler_add_o(c, dict, mangled);
2852 if (arg < 0)
2853 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002854 Py_DECREF(mangled);
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002855 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002856}
2857
2858static int
2859compiler_boolop(struct compiler *c, expr_ty e)
2860{
2861 basicblock *end;
2862 int jumpi, i, n;
2863 asdl_seq *s;
2864
2865 assert(e->kind == BoolOp_kind);
2866 if (e->v.BoolOp.op == And)
2867 jumpi = JUMP_IF_FALSE;
2868 else
2869 jumpi = JUMP_IF_TRUE;
2870 end = compiler_new_block(c);
2871 if (end < 0)
2872 return 0;
2873 s = e->v.BoolOp.values;
2874 n = asdl_seq_LEN(s) - 1;
2875 for (i = 0; i < n; ++i) {
2876 VISIT(c, expr, asdl_seq_GET(s, i));
2877 ADDOP_JREL(c, jumpi, end);
2878 ADDOP(c, POP_TOP)
2879 }
2880 VISIT(c, expr, asdl_seq_GET(s, n));
2881 compiler_use_next_block(c, end);
2882 return 1;
2883}
2884
2885static int
2886compiler_list(struct compiler *c, expr_ty e)
2887{
2888 int n = asdl_seq_LEN(e->v.List.elts);
2889 if (e->v.List.ctx == Store) {
2890 ADDOP_I(c, UNPACK_SEQUENCE, n);
2891 }
2892 VISIT_SEQ(c, expr, e->v.List.elts);
2893 if (e->v.List.ctx == Load) {
2894 ADDOP_I(c, BUILD_LIST, n);
2895 }
2896 return 1;
2897}
2898
2899static int
2900compiler_tuple(struct compiler *c, expr_ty e)
2901{
2902 int n = asdl_seq_LEN(e->v.Tuple.elts);
2903 if (e->v.Tuple.ctx == Store) {
2904 ADDOP_I(c, UNPACK_SEQUENCE, n);
2905 }
2906 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2907 if (e->v.Tuple.ctx == Load) {
2908 ADDOP_I(c, BUILD_TUPLE, n);
2909 }
2910 return 1;
2911}
2912
2913static int
2914compiler_compare(struct compiler *c, expr_ty e)
2915{
2916 int i, n;
2917 basicblock *cleanup = NULL;
2918
2919 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2920 VISIT(c, expr, e->v.Compare.left);
2921 n = asdl_seq_LEN(e->v.Compare.ops);
2922 assert(n > 0);
2923 if (n > 1) {
2924 cleanup = compiler_new_block(c);
2925 if (cleanup == NULL)
2926 return 0;
2927 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, 0));
2928 }
2929 for (i = 1; i < n; i++) {
2930 ADDOP(c, DUP_TOP);
2931 ADDOP(c, ROT_THREE);
2932 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
2933 ADDOP_I(c, COMPARE_OP,
2934 cmpop((cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i - 1)));
2935 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2936 NEXT_BLOCK(c);
2937 ADDOP(c, POP_TOP);
2938 if (i < (n - 1))
2939 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, i));
2940 }
2941 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, n - 1));
2942 ADDOP_I(c, COMPARE_OP,
2943 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
2944 cmpop((cmpop_ty)asdl_seq_GET(e->v.Compare.ops, n - 1)));
2945 if (n > 1) {
2946 basicblock *end = compiler_new_block(c);
2947 if (end == NULL)
2948 return 0;
2949 ADDOP_JREL(c, JUMP_FORWARD, end);
2950 compiler_use_next_block(c, cleanup);
2951 ADDOP(c, ROT_TWO);
2952 ADDOP(c, POP_TOP);
2953 compiler_use_next_block(c, end);
2954 }
2955 return 1;
2956}
2957
2958static int
2959compiler_call(struct compiler *c, expr_ty e)
2960{
2961 int n, code = 0;
2962
2963 VISIT(c, expr, e->v.Call.func);
2964 n = asdl_seq_LEN(e->v.Call.args);
2965 VISIT_SEQ(c, expr, e->v.Call.args);
2966 if (e->v.Call.keywords) {
2967 VISIT_SEQ(c, keyword, e->v.Call.keywords);
2968 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
2969 }
2970 if (e->v.Call.starargs) {
2971 VISIT(c, expr, e->v.Call.starargs);
2972 code |= 1;
2973 }
2974 if (e->v.Call.kwargs) {
2975 VISIT(c, expr, e->v.Call.kwargs);
2976 code |= 2;
2977 }
2978 switch (code) {
2979 case 0:
2980 ADDOP_I(c, CALL_FUNCTION, n);
2981 break;
2982 case 1:
2983 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2984 break;
2985 case 2:
2986 ADDOP_I(c, CALL_FUNCTION_KW, n);
2987 break;
2988 case 3:
2989 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2990 break;
2991 }
2992 return 1;
2993}
2994
2995static int
2996compiler_listcomp_generator(struct compiler *c, PyObject *tmpname,
2997 asdl_seq *generators, int gen_index,
2998 expr_ty elt)
2999{
3000 /* generate code for the iterator, then each of the ifs,
3001 and then write to the element */
3002
3003 comprehension_ty l;
3004 basicblock *start, *anchor, *skip, *if_cleanup;
3005 int i, n;
3006
3007 start = compiler_new_block(c);
3008 skip = compiler_new_block(c);
3009 if_cleanup = compiler_new_block(c);
3010 anchor = compiler_new_block(c);
3011
3012 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3013 anchor == NULL)
3014 return 0;
3015
3016 l = asdl_seq_GET(generators, gen_index);
3017 VISIT(c, expr, l->iter);
3018 ADDOP(c, GET_ITER);
3019 compiler_use_next_block(c, start);
3020 ADDOP_JREL(c, FOR_ITER, anchor);
3021 NEXT_BLOCK(c);
3022 VISIT(c, expr, l->target);
3023
3024 /* XXX this needs to be cleaned up...a lot! */
3025 n = asdl_seq_LEN(l->ifs);
3026 for (i = 0; i < n; i++) {
3027 expr_ty e = asdl_seq_GET(l->ifs, i);
3028 VISIT(c, expr, e);
3029 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
3030 NEXT_BLOCK(c);
3031 ADDOP(c, POP_TOP);
3032 }
3033
3034 if (++gen_index < asdl_seq_LEN(generators))
3035 if (!compiler_listcomp_generator(c, tmpname,
3036 generators, gen_index, elt))
3037 return 0;
3038
3039 /* only append after the last for generator */
3040 if (gen_index >= asdl_seq_LEN(generators)) {
3041 if (!compiler_nameop(c, tmpname, Load))
3042 return 0;
3043 VISIT(c, expr, elt);
3044 ADDOP_I(c, CALL_FUNCTION, 1);
3045 ADDOP(c, POP_TOP);
3046
3047 compiler_use_next_block(c, skip);
3048 }
3049 for (i = 0; i < n; i++) {
3050 ADDOP_I(c, JUMP_FORWARD, 1);
3051 if (i == 0)
3052 compiler_use_next_block(c, if_cleanup);
3053 ADDOP(c, POP_TOP);
3054 }
3055 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3056 compiler_use_next_block(c, anchor);
3057 /* delete the append method added to locals */
3058 if (gen_index == 1)
3059 if (!compiler_nameop(c, tmpname, Del))
3060 return 0;
3061
3062 return 1;
3063}
3064
3065static int
3066compiler_listcomp(struct compiler *c, expr_ty e)
3067{
3068 char tmpname[256];
3069 identifier tmp;
3070 int rc = 0;
3071 static identifier append;
3072 asdl_seq *generators = e->v.ListComp.generators;
3073
3074 assert(e->kind == ListComp_kind);
3075 if (!append) {
3076 append = PyString_InternFromString("append");
3077 if (!append)
3078 return 0;
3079 }
3080 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
3081 tmp = PyString_FromString(tmpname);
3082 if (!tmp)
3083 return 0;
3084 ADDOP_I(c, BUILD_LIST, 0);
3085 ADDOP(c, DUP_TOP);
3086 ADDOP_O(c, LOAD_ATTR, append, names);
3087 if (compiler_nameop(c, tmp, Store))
3088 rc = compiler_listcomp_generator(c, tmp, generators, 0,
3089 e->v.ListComp.elt);
3090 Py_DECREF(tmp);
3091 return rc;
3092}
3093
3094static int
3095compiler_genexp_generator(struct compiler *c,
3096 asdl_seq *generators, int gen_index,
3097 expr_ty elt)
3098{
3099 /* generate code for the iterator, then each of the ifs,
3100 and then write to the element */
3101
3102 comprehension_ty ge;
3103 basicblock *start, *anchor, *skip, *if_cleanup, *end;
3104 int i, n;
3105
3106 start = compiler_new_block(c);
3107 skip = compiler_new_block(c);
3108 if_cleanup = compiler_new_block(c);
3109 anchor = compiler_new_block(c);
3110 end = compiler_new_block(c);
3111
3112 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3113 anchor == NULL || end == NULL)
3114 return 0;
3115
3116 ge = asdl_seq_GET(generators, gen_index);
3117 ADDOP_JREL(c, SETUP_LOOP, end);
3118 if (!compiler_push_fblock(c, LOOP, start))
3119 return 0;
3120
3121 if (gen_index == 0) {
3122 /* Receive outermost iter as an implicit argument */
3123 c->u->u_argcount = 1;
3124 ADDOP_I(c, LOAD_FAST, 0);
3125 }
3126 else {
3127 /* Sub-iter - calculate on the fly */
3128 VISIT(c, expr, ge->iter);
3129 ADDOP(c, GET_ITER);
3130 }
3131 compiler_use_next_block(c, start);
3132 ADDOP_JREL(c, FOR_ITER, anchor);
3133 NEXT_BLOCK(c);
3134 VISIT(c, expr, ge->target);
3135
3136 /* XXX this needs to be cleaned up...a lot! */
3137 n = asdl_seq_LEN(ge->ifs);
3138 for (i = 0; i < n; i++) {
3139 expr_ty e = asdl_seq_GET(ge->ifs, i);
3140 VISIT(c, expr, e);
3141 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
3142 NEXT_BLOCK(c);
3143 ADDOP(c, POP_TOP);
3144 }
3145
3146 if (++gen_index < asdl_seq_LEN(generators))
3147 if (!compiler_genexp_generator(c, generators, gen_index, elt))
3148 return 0;
3149
3150 /* only append after the last 'for' generator */
3151 if (gen_index >= asdl_seq_LEN(generators)) {
3152 VISIT(c, expr, elt);
3153 ADDOP(c, YIELD_VALUE);
3154 ADDOP(c, POP_TOP);
3155
3156 compiler_use_next_block(c, skip);
3157 }
3158 for (i = 0; i < n; i++) {
3159 ADDOP_I(c, JUMP_FORWARD, 1);
3160 if (i == 0)
3161 compiler_use_next_block(c, if_cleanup);
3162
3163 ADDOP(c, POP_TOP);
3164 }
3165 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3166 compiler_use_next_block(c, anchor);
3167 ADDOP(c, POP_BLOCK);
3168 compiler_pop_fblock(c, LOOP, start);
3169 compiler_use_next_block(c, end);
3170
3171 return 1;
3172}
3173
3174static int
3175compiler_genexp(struct compiler *c, expr_ty e)
3176{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00003177 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003178 PyCodeObject *co;
3179 expr_ty outermost_iter = ((comprehension_ty)
3180 (asdl_seq_GET(e->v.GeneratorExp.generators,
3181 0)))->iter;
3182
Nick Coghlan944d3eb2005-11-16 12:46:55 +00003183 if (!name) {
3184 name = PyString_FromString("<genexpr>");
3185 if (!name)
3186 return 0;
3187 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003188
3189 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
3190 return 0;
3191 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
3192 e->v.GeneratorExp.elt);
3193 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00003194 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003195 if (co == NULL)
3196 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003197
3198 compiler_make_closure(c, co, 0);
3199 VISIT(c, expr, outermost_iter);
3200 ADDOP(c, GET_ITER);
3201 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003202
3203 return 1;
3204}
3205
3206static int
3207compiler_visit_keyword(struct compiler *c, keyword_ty k)
3208{
3209 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3210 VISIT(c, expr, k->value);
3211 return 1;
3212}
3213
3214/* Test whether expression is constant. For constants, report
3215 whether they are true or false.
3216
3217 Return values: 1 for true, 0 for false, -1 for non-constant.
3218 */
3219
3220static int
3221expr_constant(expr_ty e)
3222{
3223 switch (e->kind) {
3224 case Num_kind:
3225 return PyObject_IsTrue(e->v.Num.n);
3226 case Str_kind:
3227 return PyObject_IsTrue(e->v.Str.s);
3228 default:
3229 return -1;
3230 }
3231}
3232
3233static int
3234compiler_visit_expr(struct compiler *c, expr_ty e)
3235{
3236 int i, n;
3237
3238 if (e->lineno > c->u->u_lineno) {
3239 c->u->u_lineno = e->lineno;
3240 c->u->u_lineno_set = false;
3241 }
3242 switch (e->kind) {
3243 case BoolOp_kind:
3244 return compiler_boolop(c, e);
3245 case BinOp_kind:
3246 VISIT(c, expr, e->v.BinOp.left);
3247 VISIT(c, expr, e->v.BinOp.right);
3248 ADDOP(c, binop(c, e->v.BinOp.op));
3249 break;
3250 case UnaryOp_kind:
3251 VISIT(c, expr, e->v.UnaryOp.operand);
3252 ADDOP(c, unaryop(e->v.UnaryOp.op));
3253 break;
3254 case Lambda_kind:
3255 return compiler_lambda(c, e);
3256 case Dict_kind:
3257 /* XXX get rid of arg? */
3258 ADDOP_I(c, BUILD_MAP, 0);
3259 n = asdl_seq_LEN(e->v.Dict.values);
3260 /* We must arrange things just right for STORE_SUBSCR.
3261 It wants the stack to look like (value) (dict) (key) */
3262 for (i = 0; i < n; i++) {
3263 ADDOP(c, DUP_TOP);
3264 VISIT(c, expr, asdl_seq_GET(e->v.Dict.values, i));
3265 ADDOP(c, ROT_TWO);
3266 VISIT(c, expr, asdl_seq_GET(e->v.Dict.keys, i));
3267 ADDOP(c, STORE_SUBSCR);
3268 }
3269 break;
3270 case ListComp_kind:
3271 return compiler_listcomp(c, e);
3272 case GeneratorExp_kind:
3273 return compiler_genexp(c, e);
3274 case Yield_kind:
3275 if (c->u->u_ste->ste_type != FunctionBlock)
3276 return compiler_error(c, "'yield' outside function");
3277 /*
3278 for (i = 0; i < c->u->u_nfblocks; i++) {
3279 if (c->u->u_fblock[i].fb_type == FINALLY_TRY)
3280 return compiler_error(
3281 c, "'yield' not allowed in a 'try' "
3282 "block with a 'finally' clause");
3283 }
3284 */
3285 if (e->v.Yield.value) {
3286 VISIT(c, expr, e->v.Yield.value);
3287 }
3288 else {
3289 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3290 }
3291 ADDOP(c, YIELD_VALUE);
3292 break;
3293 case Compare_kind:
3294 return compiler_compare(c, e);
3295 case Call_kind:
3296 return compiler_call(c, e);
3297 case Repr_kind:
3298 VISIT(c, expr, e->v.Repr.value);
3299 ADDOP(c, UNARY_CONVERT);
3300 break;
3301 case Num_kind:
3302 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3303 break;
3304 case Str_kind:
3305 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3306 break;
3307 /* The following exprs can be assignment targets. */
3308 case Attribute_kind:
3309 if (e->v.Attribute.ctx != AugStore)
3310 VISIT(c, expr, e->v.Attribute.value);
3311 switch (e->v.Attribute.ctx) {
3312 case AugLoad:
3313 ADDOP(c, DUP_TOP);
3314 /* Fall through to load */
3315 case Load:
3316 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3317 break;
3318 case AugStore:
3319 ADDOP(c, ROT_TWO);
3320 /* Fall through to save */
3321 case Store:
3322 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3323 break;
3324 case Del:
3325 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3326 break;
3327 case Param:
3328 assert(0);
3329 break;
3330 }
3331 break;
3332 case Subscript_kind:
3333 switch (e->v.Subscript.ctx) {
3334 case AugLoad:
3335 VISIT(c, expr, e->v.Subscript.value);
3336 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3337 break;
3338 case Load:
3339 VISIT(c, expr, e->v.Subscript.value);
3340 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3341 break;
3342 case AugStore:
3343 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3344 break;
3345 case Store:
3346 VISIT(c, expr, e->v.Subscript.value);
3347 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3348 break;
3349 case Del:
3350 VISIT(c, expr, e->v.Subscript.value);
3351 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3352 break;
3353 case Param:
3354 assert(0);
3355 break;
3356 }
3357 break;
3358 case Name_kind:
3359 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3360 /* child nodes of List and Tuple will have expr_context set */
3361 case List_kind:
3362 return compiler_list(c, e);
3363 case Tuple_kind:
3364 return compiler_tuple(c, e);
3365 }
3366 return 1;
3367}
3368
3369static int
3370compiler_augassign(struct compiler *c, stmt_ty s)
3371{
3372 expr_ty e = s->v.AugAssign.target;
3373 expr_ty auge;
3374
3375 assert(s->kind == AugAssign_kind);
3376
3377 switch (e->kind) {
3378 case Attribute_kind:
3379 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
3380 AugLoad, e->lineno);
3381 if (auge == NULL)
3382 return 0;
3383 VISIT(c, expr, auge);
3384 VISIT(c, expr, s->v.AugAssign.value);
3385 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3386 auge->v.Attribute.ctx = AugStore;
3387 VISIT(c, expr, auge);
3388 free(auge);
3389 break;
3390 case Subscript_kind:
3391 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
3392 AugLoad, e->lineno);
3393 if (auge == NULL)
3394 return 0;
3395 VISIT(c, expr, auge);
3396 VISIT(c, expr, s->v.AugAssign.value);
3397 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3398 auge->v.Subscript.ctx = AugStore;
3399 VISIT(c, expr, auge);
3400 free(auge);
3401 break;
3402 case Name_kind:
3403 VISIT(c, expr, s->v.AugAssign.target);
3404 VISIT(c, expr, s->v.AugAssign.value);
3405 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3406 return compiler_nameop(c, e->v.Name.id, Store);
3407 default:
3408 fprintf(stderr,
3409 "invalid node type for augmented assignment\n");
3410 return 0;
3411 }
3412 return 1;
3413}
3414
3415static int
3416compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3417{
3418 struct fblockinfo *f;
3419 if (c->u->u_nfblocks >= CO_MAXBLOCKS)
3420 return 0;
3421 f = &c->u->u_fblock[c->u->u_nfblocks++];
3422 f->fb_type = t;
3423 f->fb_block = b;
3424 return 1;
3425}
3426
3427static void
3428compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3429{
3430 struct compiler_unit *u = c->u;
3431 assert(u->u_nfblocks > 0);
3432 u->u_nfblocks--;
3433 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3434 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3435}
3436
3437/* Raises a SyntaxError and returns 0.
3438 If something goes wrong, a different exception may be raised.
3439*/
3440
3441static int
3442compiler_error(struct compiler *c, const char *errstr)
3443{
3444 PyObject *loc;
3445 PyObject *u = NULL, *v = NULL;
3446
3447 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3448 if (!loc) {
3449 Py_INCREF(Py_None);
3450 loc = Py_None;
3451 }
3452 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3453 Py_None, loc);
3454 if (!u)
3455 goto exit;
3456 v = Py_BuildValue("(zO)", errstr, u);
3457 if (!v)
3458 goto exit;
3459 PyErr_SetObject(PyExc_SyntaxError, v);
3460 exit:
3461 Py_DECREF(loc);
3462 Py_XDECREF(u);
3463 Py_XDECREF(v);
3464 return 0;
3465}
3466
3467static int
3468compiler_handle_subscr(struct compiler *c, const char *kind,
3469 expr_context_ty ctx)
3470{
3471 int op = 0;
3472
3473 /* XXX this code is duplicated */
3474 switch (ctx) {
3475 case AugLoad: /* fall through to Load */
3476 case Load: op = BINARY_SUBSCR; break;
3477 case AugStore:/* fall through to Store */
3478 case Store: op = STORE_SUBSCR; break;
3479 case Del: op = DELETE_SUBSCR; break;
3480 case Param:
3481 fprintf(stderr,
3482 "invalid %s kind %d in subscript\n",
3483 kind, ctx);
3484 return 0;
3485 }
3486 if (ctx == AugLoad) {
3487 ADDOP_I(c, DUP_TOPX, 2);
3488 }
3489 else if (ctx == AugStore) {
3490 ADDOP(c, ROT_THREE);
3491 }
3492 ADDOP(c, op);
3493 return 1;
3494}
3495
3496static int
3497compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3498{
3499 int n = 2;
3500 assert(s->kind == Slice_kind);
3501
3502 /* only handles the cases where BUILD_SLICE is emitted */
3503 if (s->v.Slice.lower) {
3504 VISIT(c, expr, s->v.Slice.lower);
3505 }
3506 else {
3507 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3508 }
3509
3510 if (s->v.Slice.upper) {
3511 VISIT(c, expr, s->v.Slice.upper);
3512 }
3513 else {
3514 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3515 }
3516
3517 if (s->v.Slice.step) {
3518 n++;
3519 VISIT(c, expr, s->v.Slice.step);
3520 }
3521 ADDOP_I(c, BUILD_SLICE, n);
3522 return 1;
3523}
3524
3525static int
3526compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3527{
3528 int op = 0, slice_offset = 0, stack_count = 0;
3529
3530 assert(s->v.Slice.step == NULL);
3531 if (s->v.Slice.lower) {
3532 slice_offset++;
3533 stack_count++;
3534 if (ctx != AugStore)
3535 VISIT(c, expr, s->v.Slice.lower);
3536 }
3537 if (s->v.Slice.upper) {
3538 slice_offset += 2;
3539 stack_count++;
3540 if (ctx != AugStore)
3541 VISIT(c, expr, s->v.Slice.upper);
3542 }
3543
3544 if (ctx == AugLoad) {
3545 switch (stack_count) {
3546 case 0: ADDOP(c, DUP_TOP); break;
3547 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3548 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3549 }
3550 }
3551 else if (ctx == AugStore) {
3552 switch (stack_count) {
3553 case 0: ADDOP(c, ROT_TWO); break;
3554 case 1: ADDOP(c, ROT_THREE); break;
3555 case 2: ADDOP(c, ROT_FOUR); break;
3556 }
3557 }
3558
3559 switch (ctx) {
3560 case AugLoad: /* fall through to Load */
3561 case Load: op = SLICE; break;
3562 case AugStore:/* fall through to Store */
3563 case Store: op = STORE_SLICE; break;
3564 case Del: op = DELETE_SLICE; break;
3565 case Param: /* XXX impossible? */
3566 fprintf(stderr, "param invalid\n");
3567 assert(0);
3568 }
3569
3570 ADDOP(c, op + slice_offset);
3571 return 1;
3572}
3573
3574static int
3575compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3576 expr_context_ty ctx)
3577{
3578 switch (s->kind) {
3579 case Ellipsis_kind:
3580 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3581 break;
3582 case Slice_kind:
3583 return compiler_slice(c, s, ctx);
3584 break;
3585 case Index_kind:
3586 VISIT(c, expr, s->v.Index.value);
3587 break;
3588 case ExtSlice_kind:
3589 assert(0);
3590 break;
3591 }
3592 return 1;
3593}
3594
3595
3596static int
3597compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3598{
3599 switch (s->kind) {
3600 case Ellipsis_kind:
3601 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3602 break;
3603 case Slice_kind:
3604 if (!s->v.Slice.step)
3605 return compiler_simple_slice(c, s, ctx);
3606 if (!compiler_slice(c, s, ctx))
3607 return 0;
3608 if (ctx == AugLoad) {
3609 ADDOP_I(c, DUP_TOPX, 2);
3610 }
3611 else if (ctx == AugStore) {
3612 ADDOP(c, ROT_THREE);
3613 }
3614 return compiler_handle_subscr(c, "slice", ctx);
3615 break;
3616 case ExtSlice_kind: {
3617 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3618 for (i = 0; i < n; i++) {
3619 slice_ty sub = asdl_seq_GET(s->v.ExtSlice.dims, i);
3620 if (!compiler_visit_nested_slice(c, sub, ctx))
3621 return 0;
3622 }
3623 ADDOP_I(c, BUILD_TUPLE, n);
3624 return compiler_handle_subscr(c, "extended slice", ctx);
3625 break;
3626 }
3627 case Index_kind:
3628 if (ctx != AugStore)
3629 VISIT(c, expr, s->v.Index.value);
3630 return compiler_handle_subscr(c, "index", ctx);
3631 }
3632 return 1;
3633}
3634
3635/* do depth-first search of basic block graph, starting with block.
3636 post records the block indices in post-order.
3637
3638 XXX must handle implicit jumps from one block to next
3639*/
3640
3641static void
3642dfs(struct compiler *c, basicblock *b, struct assembler *a)
3643{
3644 int i;
3645 struct instr *instr = NULL;
3646
3647 if (b->b_seen)
3648 return;
3649 b->b_seen = 1;
3650 if (b->b_next != NULL)
3651 dfs(c, b->b_next, a);
3652 for (i = 0; i < b->b_iused; i++) {
3653 instr = &b->b_instr[i];
3654 if (instr->i_jrel || instr->i_jabs)
3655 dfs(c, instr->i_target, a);
3656 }
3657 a->a_postorder[a->a_nblocks++] = b;
3658}
3659
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003660static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003661stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3662{
3663 int i;
3664 struct instr *instr;
3665 if (b->b_seen || b->b_startdepth >= depth)
3666 return maxdepth;
3667 b->b_seen = 1;
3668 b->b_startdepth = depth;
3669 for (i = 0; i < b->b_iused; i++) {
3670 instr = &b->b_instr[i];
3671 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3672 if (depth > maxdepth)
3673 maxdepth = depth;
3674 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3675 if (instr->i_jrel || instr->i_jabs) {
3676 maxdepth = stackdepth_walk(c, instr->i_target,
3677 depth, maxdepth);
3678 if (instr->i_opcode == JUMP_ABSOLUTE ||
3679 instr->i_opcode == JUMP_FORWARD) {
3680 goto out; /* remaining code is dead */
3681 }
3682 }
3683 }
3684 if (b->b_next)
3685 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3686out:
3687 b->b_seen = 0;
3688 return maxdepth;
3689}
3690
3691/* Find the flow path that needs the largest stack. We assume that
3692 * cycles in the flow graph have no net effect on the stack depth.
3693 */
3694static int
3695stackdepth(struct compiler *c)
3696{
3697 basicblock *b, *entryblock;
3698 entryblock = NULL;
3699 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3700 b->b_seen = 0;
3701 b->b_startdepth = INT_MIN;
3702 entryblock = b;
3703 }
3704 return stackdepth_walk(c, entryblock, 0, 0);
3705}
3706
3707static int
3708assemble_init(struct assembler *a, int nblocks, int firstlineno)
3709{
3710 memset(a, 0, sizeof(struct assembler));
3711 a->a_lineno = firstlineno;
3712 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3713 if (!a->a_bytecode)
3714 return 0;
3715 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3716 if (!a->a_lnotab)
3717 return 0;
3718 a->a_postorder = (basicblock **)PyObject_Malloc(
3719 sizeof(basicblock *) * nblocks);
3720 if (!a->a_postorder)
3721 return 0;
3722 return 1;
3723}
3724
3725static void
3726assemble_free(struct assembler *a)
3727{
3728 Py_XDECREF(a->a_bytecode);
3729 Py_XDECREF(a->a_lnotab);
3730 if (a->a_postorder)
3731 PyObject_Free(a->a_postorder);
3732}
3733
3734/* Return the size of a basic block in bytes. */
3735
3736static int
3737instrsize(struct instr *instr)
3738{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003739 if (!instr->i_hasarg)
3740 return 1;
3741 if (instr->i_oparg > 0xffff)
3742 return 6;
3743 return 3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003744}
3745
3746static int
3747blocksize(basicblock *b)
3748{
3749 int i;
3750 int size = 0;
3751
3752 for (i = 0; i < b->b_iused; i++)
3753 size += instrsize(&b->b_instr[i]);
3754 return size;
3755}
3756
3757/* All about a_lnotab.
3758
3759c_lnotab is an array of unsigned bytes disguised as a Python string.
3760It is used to map bytecode offsets to source code line #s (when needed
3761for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003762
Tim Peters2a7f3842001-06-09 09:26:21 +00003763The array is conceptually a list of
3764 (bytecode offset increment, line number increment)
3765pairs. The details are important and delicate, best illustrated by example:
3766
3767 byte code offset source code line number
3768 0 1
3769 6 2
3770 50 7
3771 350 307
3772 361 308
3773
3774The first trick is that these numbers aren't stored, only the increments
3775from one row to the next (this doesn't really work, but it's a start):
3776
3777 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3778
3779The second trick is that an unsigned byte can't hold negative values, or
3780values larger than 255, so (a) there's a deep assumption that byte code
3781offsets and their corresponding line #s both increase monotonically, and (b)
3782if at least one column jumps by more than 255 from one row to the next, more
3783than one pair is written to the table. In case #b, there's no way to know
3784from looking at the table later how many were written. That's the delicate
3785part. A user of c_lnotab desiring to find the source line number
3786corresponding to a bytecode address A should do something like this
3787
3788 lineno = addr = 0
3789 for addr_incr, line_incr in c_lnotab:
3790 addr += addr_incr
3791 if addr > A:
3792 return lineno
3793 lineno += line_incr
3794
3795In order for this to work, when the addr field increments by more than 255,
3796the line # increment in each pair generated must be 0 until the remaining addr
3797increment is < 256. So, in the example above, com_set_lineno should not (as
3798was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
3799255, 0, 45, 255, 0, 45.
3800*/
3801
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003802static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003803assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003804{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003805 int d_bytecode, d_lineno;
3806 int len;
3807 char *lnotab;
3808
3809 d_bytecode = a->a_offset - a->a_lineno_off;
3810 d_lineno = i->i_lineno - a->a_lineno;
3811
3812 assert(d_bytecode >= 0);
3813 assert(d_lineno >= 0);
3814
3815 if (d_lineno == 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003816 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003817
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003818 if (d_bytecode > 255) {
3819 int i, nbytes, ncodes = d_bytecode / 255;
3820 nbytes = a->a_lnotab_off + 2 * ncodes;
3821 len = PyString_GET_SIZE(a->a_lnotab);
3822 if (nbytes >= len) {
3823 if (len * 2 < nbytes)
3824 len = nbytes;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003825 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003826 len *= 2;
3827 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3828 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003829 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003830 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3831 for (i = 0; i < ncodes; i++) {
3832 *lnotab++ = 255;
3833 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003834 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003835 d_bytecode -= ncodes * 255;
3836 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003837 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003838 assert(d_bytecode <= 255);
3839 if (d_lineno > 255) {
3840 int i, nbytes, ncodes = d_lineno / 255;
3841 nbytes = a->a_lnotab_off + 2 * ncodes;
3842 len = PyString_GET_SIZE(a->a_lnotab);
3843 if (nbytes >= len) {
3844 if (len * 2 < nbytes)
3845 len = nbytes;
Guido van Rossum635abd21997-01-06 22:56:52 +00003846 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003847 len *= 2;
3848 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3849 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003850 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003851 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3852 *lnotab++ = 255;
3853 *lnotab++ = d_bytecode;
3854 d_bytecode = 0;
3855 for (i = 1; i < ncodes; i++) {
3856 *lnotab++ = 255;
3857 *lnotab++ = 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003858 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003859 d_lineno -= ncodes * 255;
3860 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003861 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003862
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003863 len = PyString_GET_SIZE(a->a_lnotab);
3864 if (a->a_lnotab_off + 2 >= len) {
3865 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003866 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003867 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003868 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003869
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003870 a->a_lnotab_off += 2;
3871 if (d_bytecode) {
3872 *lnotab++ = d_bytecode;
3873 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003874 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003875 else { /* First line of a block; def stmt, etc. */
3876 *lnotab++ = 0;
3877 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003878 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003879 a->a_lineno = i->i_lineno;
3880 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003881 return 1;
3882}
3883
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003884/* assemble_emit()
3885 Extend the bytecode with a new instruction.
3886 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003887*/
3888
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003889static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003890assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003891{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003892 int size, arg = 0, ext = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003893 int len = PyString_GET_SIZE(a->a_bytecode);
3894 char *code;
3895
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003896 size = instrsize(i);
3897 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003898 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003899 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003900 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003901 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003902 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003903 if (a->a_offset + size >= len) {
3904 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003905 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003906 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003907 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3908 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003909 if (size == 6) {
3910 assert(i->i_hasarg);
3911 *code++ = (char)EXTENDED_ARG;
3912 *code++ = ext & 0xff;
3913 *code++ = ext >> 8;
3914 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003915 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003916 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003917 if (i->i_hasarg) {
3918 assert(size == 3 || size == 6);
3919 *code++ = arg & 0xff;
3920 *code++ = arg >> 8;
3921 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003922 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003923}
3924
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003925static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003926assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003927{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003928 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003929 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003930 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003931
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003932 /* Compute the size of each block and fixup jump args.
3933 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003934start:
3935 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003936 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003937 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003938 bsize = blocksize(b);
3939 b->b_offset = totsize;
3940 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003941 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003942 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003943 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3944 bsize = b->b_offset;
3945 for (i = 0; i < b->b_iused; i++) {
3946 struct instr *instr = &b->b_instr[i];
3947 /* Relative jumps are computed relative to
3948 the instruction pointer after fetching
3949 the jump instruction.
3950 */
3951 bsize += instrsize(instr);
3952 if (instr->i_jabs)
3953 instr->i_oparg = instr->i_target->b_offset;
3954 else if (instr->i_jrel) {
3955 int delta = instr->i_target->b_offset - bsize;
3956 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003957 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003958 else
3959 continue;
3960 if (instr->i_oparg > 0xffff)
3961 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003962 }
3963 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003964
3965 /* XXX: This is an awful hack that could hurt performance, but
3966 on the bright side it should work until we come up
3967 with a better solution.
3968
3969 In the meantime, should the goto be dropped in favor
3970 of a loop?
3971
3972 The issue is that in the first loop blocksize() is called
3973 which calls instrsize() which requires i_oparg be set
3974 appropriately. There is a bootstrap problem because
3975 i_oparg is calculated in the second loop above.
3976
3977 So we loop until we stop seeing new EXTENDED_ARGs.
3978 The only EXTENDED_ARGs that could be popping up are
3979 ones in jump instructions. So this should converge
3980 fairly quickly.
3981 */
3982 if (last_extended_arg_count != extended_arg_count) {
3983 last_extended_arg_count = extended_arg_count;
3984 goto start;
3985 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003986}
3987
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003988static PyObject *
3989dict_keys_inorder(PyObject *dict, int offset)
3990{
3991 PyObject *tuple, *k, *v;
3992 int i, pos = 0, size = PyDict_Size(dict);
3993
3994 tuple = PyTuple_New(size);
3995 if (tuple == NULL)
3996 return NULL;
3997 while (PyDict_Next(dict, &pos, &k, &v)) {
3998 i = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003999 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004000 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004001 assert((i - offset) < size);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004002 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004003 PyTuple_SET_ITEM(tuple, i - offset, k);
4004 }
4005 return tuple;
4006}
4007
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004008static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004009compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004010{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004011 PySTEntryObject *ste = c->u->u_ste;
4012 int flags = 0, n;
4013 if (ste->ste_type != ModuleBlock)
4014 flags |= CO_NEWLOCALS;
4015 if (ste->ste_type == FunctionBlock) {
4016 if (!ste->ste_unoptimized)
4017 flags |= CO_OPTIMIZED;
4018 if (ste->ste_nested)
4019 flags |= CO_NESTED;
4020 if (ste->ste_generator)
4021 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004022 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004023 if (ste->ste_varargs)
4024 flags |= CO_VARARGS;
4025 if (ste->ste_varkeywords)
4026 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00004027 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004028 flags |= CO_GENERATOR;
4029 if (c->c_flags->cf_flags & CO_FUTURE_DIVISION)
4030 flags |= CO_FUTURE_DIVISION;
4031 n = PyDict_Size(c->u->u_freevars);
4032 if (n < 0)
4033 return -1;
4034 if (n == 0) {
4035 n = PyDict_Size(c->u->u_cellvars);
4036 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004037 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004038 if (n == 0) {
4039 flags |= CO_NOFREE;
4040 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004041 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004042
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004043 return flags;
4044}
4045
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004046static PyCodeObject *
4047makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004048{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004049 PyObject *tmp;
4050 PyCodeObject *co = NULL;
4051 PyObject *consts = NULL;
4052 PyObject *names = NULL;
4053 PyObject *varnames = NULL;
4054 PyObject *filename = NULL;
4055 PyObject *name = NULL;
4056 PyObject *freevars = NULL;
4057 PyObject *cellvars = NULL;
4058 PyObject *bytecode = NULL;
4059 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004060
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004061 tmp = dict_keys_inorder(c->u->u_consts, 0);
4062 if (!tmp)
4063 goto error;
4064 consts = PySequence_List(tmp); /* optimize_code requires a list */
4065 Py_DECREF(tmp);
4066
4067 names = dict_keys_inorder(c->u->u_names, 0);
4068 varnames = dict_keys_inorder(c->u->u_varnames, 0);
4069 if (!consts || !names || !varnames)
4070 goto error;
4071
4072 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4073 if (!cellvars)
4074 goto error;
4075 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4076 if (!freevars)
4077 goto error;
4078 filename = PyString_FromString(c->c_filename);
4079 if (!filename)
4080 goto error;
4081
4082 nlocals = PyDict_Size(c->u->u_varnames);
4083 flags = compute_code_flags(c);
4084 if (flags < 0)
4085 goto error;
4086
4087 bytecode = optimize_code(a->a_bytecode, consts, names, a->a_lnotab);
4088 if (!bytecode)
4089 goto error;
4090
4091 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4092 if (!tmp)
4093 goto error;
4094 Py_DECREF(consts);
4095 consts = tmp;
4096
4097 co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags,
4098 bytecode, consts, names, varnames,
4099 freevars, cellvars,
4100 filename, c->u->u_name,
4101 c->u->u_firstlineno,
4102 a->a_lnotab);
4103 error:
4104 Py_XDECREF(consts);
4105 Py_XDECREF(names);
4106 Py_XDECREF(varnames);
4107 Py_XDECREF(filename);
4108 Py_XDECREF(name);
4109 Py_XDECREF(freevars);
4110 Py_XDECREF(cellvars);
4111 Py_XDECREF(bytecode);
4112 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004113}
4114
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004115static PyCodeObject *
4116assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004117{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004118 basicblock *b, *entryblock;
4119 struct assembler a;
4120 int i, j, nblocks;
4121 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004122
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004123 /* Make sure every block that falls off the end returns None.
4124 XXX NEXT_BLOCK() isn't quite right, because if the last
4125 block ends with a jump or return b_next shouldn't set.
4126 */
4127 if (!c->u->u_curblock->b_return) {
4128 NEXT_BLOCK(c);
4129 if (addNone)
4130 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4131 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004132 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004133
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004134 nblocks = 0;
4135 entryblock = NULL;
4136 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4137 nblocks++;
4138 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004139 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004140
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004141 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4142 goto error;
4143 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004144
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004145 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004146 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004147
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004148 /* Emit code in reverse postorder from dfs. */
4149 for (i = a.a_nblocks - 1; i >= 0; i--) {
4150 basicblock *b = a.a_postorder[i];
4151 for (j = 0; j < b->b_iused; j++)
4152 if (!assemble_emit(&a, &b->b_instr[j]))
4153 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004154 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004155
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004156 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4157 goto error;
4158 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
4159 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004160
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004161 co = makecode(c, &a);
4162 error:
4163 assemble_free(&a);
4164 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004165}